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 std::string_view dirName(std::string_view path);
52 std::string_view baseName(std::string_view path);
53 
54 // Split the |full| path into its directory and basename components.
55 // This modifies the input string to null-terminate the output directory
56 std::pair<std::string_view, std::string_view> splitDirBase(std::string& full);
57 
58 int isEmptyDir(std::string_view dir);
59 bool startsWith(std::string_view path, std::string_view prefix);
60 bool endsWith(std::string_view path, std::string_view prefix);
61 
openDir(const char * path)62 inline auto openDir(const char* path) {
63     auto dir = std::unique_ptr<DIR, decltype(&closedir)>(::opendir(path), &::closedir);
64     return dir;
65 }
66 
67 template <class... Paths>
join(std::string_view first,std::string_view second,Paths &&...paths)68 std::string join(std::string_view first, std::string_view second, Paths&&... paths) {
69     std::string result;
70     {
71         using std::size;
72         result.reserve(first.size() + second.size() + 1 + (sizeof...(paths) + ... + size(paths)));
73     }
74     result.assign(first);
75     (details::appendNextPath(result, second), ..., details::appendNextPath(result, paths));
76     return result;
77 }
78 
79 } // namespace android::incfs::path
80