Coverage Report

Created: 2025-04-28 18:24

/root/doris/be/src/util/path_util.cpp
Line
Count
Source (jump to first uncovered line)
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#include "util/path_util.h"
19
20
// Use the POSIX version of dirname(3). See `man 3 dirname`
21
#include <libgen.h>
22
23
#include "gutil/strings/split.h"
24
#include "gutil/strings/stringpiece.h"
25
#include "gutil/strings/strip.h"
26
27
using std::string;
28
using std::vector;
29
using strings::SkipEmpty;
30
using strings::Split;
31
32
namespace doris {
33
namespace path_util {
34
35
const string kTmpInfix = ".doristmp";
36
37
1.18k
std::string join_path_segments(const string& a, const string& b) {
38
1.18k
    if (a.empty()) {
39
1
        return b;
40
1.18k
    } else if (b.empty()) {
41
1
        return a;
42
1.18k
    } else {
43
1.18k
        return StripSuffixString(a, "/") + "/" + StripPrefixString(b, "/");
44
1.18k
    }
45
1.18k
}
46
47
0
std::vector<string> join_path_segments_v(const std::vector<string>& v, const string& s) {
48
0
    std::vector<string> out;
49
0
    for (const string& path : v) {
50
0
        out.emplace_back(join_path_segments(path, s));
51
0
    }
52
0
    return out;
53
0
}
54
55
7
std::vector<string> split_path(const string& path) {
56
7
    if (path.empty()) {
57
1
        return {};
58
1
    }
59
6
    std::vector<string> segments;
60
6
    if (path[0] == '/') {
61
4
        segments.emplace_back("/");
62
4
    }
63
6
    std::vector<StringPiece> pieces = Split(path, "/", SkipEmpty());
64
9
    for (const StringPiece& piece : pieces) {
65
9
        segments.emplace_back(piece.data(), piece.size());
66
9
    }
67
6
    return segments;
68
7
}
69
70
// strdup use malloc to obtain memory for the new string, it should be freed with free.
71
// but std::unique_ptr use delete to free memory by default, so it should specify free memory using free
72
73
18
std::string dir_name(const string& path) {
74
18
    std::vector<char> path_copy(path.c_str(), path.c_str() + path.size() + 1);
75
18
    return dirname(&path_copy[0]);
76
18
}
77
78
51
std::string base_name(const string& path) {
79
51
    std::vector<char> path_copy(path.c_str(), path.c_str() + path.size() + 1);
80
51
    return basename(&path_copy[0]);
81
51
}
82
83
39
std::string file_extension(const string& path) {
84
39
    string file_name = base_name(path);
85
39
    if (file_name == "." || file_name == "..") {
86
3
        return "";
87
3
    }
88
89
36
    string::size_type pos = file_name.rfind(".");
90
36
    return pos == string::npos ? "" : file_name.substr(pos);
91
39
}
92
93
} // namespace path_util
94
} // namespace doris