Coverage Report

Created: 2025-03-11 12:30

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