• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <iterator>
20 #include <optional>
21 #include <string>
22 #include <string_view>
23 
24 #include <dirent.h>
25 
26 namespace android::incfs::path {
27 
28 namespace details {
29 void appendNextPath(std::string& res, std::string_view c);
30 }
31 
32 std::string fromFd(int fd);
33 bool isAbsolute(std::string_view path);
34 std::string normalize(std::string_view path);
35 
36 std::string_view relativize(std::string_view parent, std::string_view nested);
relativize(const char * parent,const char * nested)37 inline std::string_view relativize(const char* parent, const char* nested) {
38     return relativize(std::string_view(parent), std::string_view(nested));
39 }
relativize(std::string_view parent,const char * nested)40 inline std::string_view relativize(std::string_view parent, const char* nested) {
41     return relativize(parent, std::string_view(nested));
42 }
relativize(const char * parent,std::string_view nested)43 inline std::string_view relativize(const char* parent, std::string_view nested) {
44     return relativize(std::string_view(parent), nested);
45 }
46 
47 std::string_view relativize(std::string&& parent, std::string_view nested) = delete;
48 std::string_view relativize(std::string_view parent, std::string&& nested) = delete;
49 
50 // Note: some system headers #define 'dirname' and 'basename' as macros
51 
baseName(std::string_view path)52 inline std::string_view baseName(std::string_view path) {
53     using namespace std::literals;
54     if (path.empty()) {
55         return {};
56     }
57     if (path == "/"sv) {
58         return "/"sv;
59     }
60     auto pos = path.rfind('/');
61     while (!path.empty() && pos == path.size() - 1) {
62         path.remove_suffix(1);
63         pos = path.rfind('/');
64     }
65     if (pos == path.npos) {
66         return path.empty() ? "/"sv : path;
67     }
68     return path.substr(pos + 1);
69 }
70 
dirName(std::string_view path)71 inline std::string_view dirName(std::string_view path) {
72     using namespace std::literals;
73     if (path.empty()) {
74         return {};
75     }
76     if (path == "/"sv) {
77         return "/"sv;
78     }
79     const auto pos = path.rfind('/');
80     if (pos == 0) {
81         return "/"sv;
82     }
83     if (pos == path.npos) {
84         return "."sv;
85     }
86     return path.substr(0, pos);
87 }
88 
89 // Split the |full| path into its directory and basename components.
90 // This modifies the input string to null-terminate the output directory
91 std::pair<std::string_view, std::string_view> splitDirBase(std::string& full);
92 
93 int isEmptyDir(std::string_view dir);
94 bool startsWith(std::string_view path, std::string_view prefix);
95 bool endsWith(std::string_view path, std::string_view prefix);
96 
openDir(const char * path)97 inline auto openDir(const char* path) {
98     struct closer {
99         void operator()(DIR* d) const { ::closedir(d); }
100     };
101     auto dir = std::unique_ptr<DIR, closer>(::opendir(path));
102     return dir;
103 }
104 
105 template <class... Paths>
join(std::string && first,std::string_view second,Paths &&...paths)106 std::string join(std::string&& first, std::string_view second, Paths&&... paths) {
107     std::string& result = first;
108     {
109         using std::size;
110         result.reserve(first.size() + second.size() + 1 + (sizeof...(paths) + ... + size(paths)));
111     }
112     (details::appendNextPath(result, second), ...,
113      details::appendNextPath(result, std::forward<Paths>(paths)));
114     return result;
115 }
116 
117 template <class... Paths>
join(std::string_view first,std::string_view second,Paths &&...paths)118 std::string join(std::string_view first, std::string_view second, Paths&&... paths) {
119     return join(std::string(), first, second, std::forward<Paths>(paths)...);
120 }
121 template <class... Paths>
join(const char * first,std::string_view second,Paths &&...paths)122 std::string join(const char* first, std::string_view second, Paths&&... paths) {
123     return path::join(std::string_view(first), second, std::forward<Paths>(paths)...);
124 }
125 
126 } // namespace android::incfs::path
127