Coverage Report

Created: 2025-05-08 00:55

/root/doris/be/src/util/path_util.cpp
Line
Count
Source
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 <absl/strings/strip.h>
22
#include <libgen.h>
23
24
using std::string;
25
using std::vector;
26
27
namespace doris {
28
namespace path_util {
29
30
1.20k
std::string join_path_segments(const string& a, const string& b) {
31
1.20k
    if (a.empty()) {
32
1
        return b;
33
1.20k
    } else if (b.empty()) {
34
1
        return a;
35
1.20k
    } else {
36
1.20k
        return std::string(absl::StripSuffix(a, "/")) + "/" +
37
1.20k
               std::string(absl::StripPrefix(b, "/"));
38
1.20k
    }
39
1.20k
}
40
41
// strdup use malloc to obtain memory for the new string, it should be freed with free.
42
// but std::unique_ptr use delete to free memory by default, so it should specify free memory using free
43
44
21
std::string dir_name(const string& path) {
45
21
    std::vector<char> path_copy(path.c_str(), path.c_str() + path.size() + 1);
46
21
    return dirname(&path_copy[0]);
47
21
}
48
49
76
std::string base_name(const string& path) {
50
76
    std::vector<char> path_copy(path.c_str(), path.c_str() + path.size() + 1);
51
76
    return basename(&path_copy[0]);
52
76
}
53
54
64
std::string file_extension(const string& path) {
55
64
    string file_name = base_name(path);
56
64
    if (file_name == "." || file_name == "..") {
57
3
        return "";
58
3
    }
59
60
61
    string::size_type pos = file_name.rfind(".");
61
61
    return pos == string::npos ? "" : file_name.substr(pos);
62
64
}
63
64
} // namespace path_util
65
} // namespace doris