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 constexpr auto procfsFdDir = std::string_view("/proc/self/fd");
33 
34 std::string fromFd(int fd);
35 std::string procfsForFd(int fd);
36 std::string readlink(std::string_view path);
37 bool isAbsolute(std::string_view path);
38 std::string normalize(std::string_view path);
39 
40 std::string_view relativize(std::string_view parent, std::string_view nested);
relativize(const char * parent,const char * nested)41 inline std::string_view relativize(const char* parent, const char* nested) {
42     return relativize(std::string_view(parent), std::string_view(nested));
43 }
relativize(std::string_view parent,const char * nested)44 inline std::string_view relativize(std::string_view parent, const char* nested) {
45     return relativize(parent, std::string_view(nested));
46 }
relativize(const char * parent,std::string_view nested)47 inline std::string_view relativize(const char* parent, std::string_view nested) {
48     return relativize(std::string_view(parent), nested);
49 }
50 
51 std::string_view relativize(std::string&& parent, std::string_view nested) = delete;
52 std::string_view relativize(std::string_view parent, std::string&& nested) = delete;
53 
54 // Note: some system headers #define 'dirname' and 'basename' as macros
55 
baseName(std::string_view path)56 inline std::string_view baseName(std::string_view path) {
57     using namespace std::literals;
58     if (path.empty()) {
59         return {};
60     }
61     if (path == "/"sv) {
62         return "/"sv;
63     }
64     auto pos = path.rfind('/');
65     while (!path.empty() && pos == path.size() - 1) {
66         path.remove_suffix(1);
67         pos = path.rfind('/');
68     }
69     if (pos == path.npos) {
70         return path.empty() ? "/"sv : path;
71     }
72     return path.substr(pos + 1);
73 }
74 
dirName(std::string_view path)75 inline std::string_view dirName(std::string_view path) {
76     using namespace std::literals;
77     if (path.empty()) {
78         return {};
79     }
80     if (path == "/"sv) {
81         return "/"sv;
82     }
83     const auto pos = path.rfind('/');
84     if (pos == 0) {
85         return "/"sv;
86     }
87     if (pos == path.npos) {
88         return "."sv;
89     }
90     return path.substr(0, pos);
91 }
92 
93 // Split the |full| path into its directory and basename components.
94 // This modifies the input string to null-terminate the output directory
95 std::pair<std::string_view, std::string_view> splitDirBase(std::string& full);
96 
97 int isEmptyDir(std::string_view dir);
98 bool startsWith(std::string_view path, std::string_view prefix);
99 bool endsWith(std::string_view path, std::string_view prefix);
100 
101 struct PathDirCloser {
operatorPathDirCloser102     void operator()(DIR* d) const { ::closedir(d); }
103 };
104 
openDir(const char * path)105 inline auto openDir(const char* path) {
106     auto dir = std::unique_ptr<DIR, PathDirCloser>(::opendir(path));
107     return dir;
108 }
109 
openDir(int dirFd)110 inline auto openDir(int dirFd) {
111     auto dir = std::unique_ptr<DIR, PathDirCloser>(::fdopendir(dirFd));
112     return dir;
113 }
114 
115 template <class... Paths>
join(std::string && first,std::string_view second,Paths &&...paths)116 std::string join(std::string&& first, std::string_view second, Paths&&... paths) {
117     std::string& result = first;
118     {
119         using std::size;
120         result.reserve(first.size() + second.size() + 1 + (sizeof...(paths) + ... + size(paths)));
121     }
122     (details::appendNextPath(result, second), ...,
123      details::appendNextPath(result, std::forward<Paths>(paths)));
124     return result;
125 }
126 
127 template <class... Paths>
join(std::string_view first,std::string_view second,Paths &&...paths)128 std::string join(std::string_view first, std::string_view second, Paths&&... paths) {
129     return join(std::string(), first, second, std::forward<Paths>(paths)...);
130 }
131 template <class... Paths>
join(const char * first,std::string_view second,Paths &&...paths)132 std::string join(const char* first, std::string_view second, Paths&&... paths) {
133     return path::join(std::string_view(first), second, std::forward<Paths>(paths)...);
134 }
135 
136 } // namespace android::incfs::path
137