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 #include "path.h"
18
19 #include <android-base/strings.h>
20 #include <android-base/logging.h>
21
22 #include <algorithm>
23 #include <iterator>
24 #include <limits>
25 #include <memory>
26
27 #include <dirent.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 using namespace std::literals;
33
34 namespace android::incremental::path {
35
operator ()(char l,char r) const36 bool PathCharsLess::operator()(char l, char r) const {
37 int ll = l == '/' ? std::numeric_limits<char>::min() - 1 : l;
38 int rr = r == '/' ? std::numeric_limits<char>::min() - 1 : r;
39 return ll < rr;
40 }
41
operator ()(std::string_view l,std::string_view r) const42 bool PathLess::operator()(std::string_view l, std::string_view r) const {
43 return std::lexicographical_compare(std::begin(l), std::end(l), std::begin(r), std::end(r),
44 PathCharsLess());
45 }
46
preparePathComponent(std::string_view & path,bool trimFront)47 static void preparePathComponent(std::string_view& path, bool trimFront) {
48 if (trimFront) {
49 while (!path.empty() && path.front() == '/') {
50 path.remove_prefix(1);
51 }
52 }
53 while (!path.empty() && path.back() == '/') {
54 path.remove_suffix(1);
55 }
56 }
57
append_next_path(std::string & target,std::string_view path)58 void details::append_next_path(std::string& target, std::string_view path) {
59 preparePathComponent(path, true);
60 if (path.empty()) {
61 return;
62 }
63 if (!target.empty() && !target.ends_with('/')) {
64 target.push_back('/');
65 }
66 target += path;
67 }
68
relativize(std::string_view parent,std::string_view nested)69 std::string_view relativize(std::string_view parent, std::string_view nested) {
70 if (!nested.starts_with(parent)) {
71 return nested;
72 }
73 if (nested.size() == parent.size()) {
74 return {};
75 }
76 if (nested[parent.size()] != '/') {
77 return nested;
78 }
79 auto relative = nested.substr(parent.size());
80 while (relative.front() == '/') {
81 relative.remove_prefix(1);
82 }
83 return relative;
84 }
85
isAbsolute(std::string_view path)86 bool isAbsolute(std::string_view path) {
87 return !path.empty() && path[0] == '/';
88 }
89
normalize(std::string_view path)90 std::string normalize(std::string_view path) {
91 if (path.empty()) {
92 return {};
93 }
94 if (path.starts_with("../"sv)) {
95 return {};
96 }
97
98 std::string result;
99 if (isAbsolute(path)) {
100 path.remove_prefix(1);
101 } else {
102 char buffer[PATH_MAX];
103 if (!::getcwd(buffer, sizeof(buffer))) {
104 return {};
105 }
106 result += buffer;
107 }
108
109 size_t start = 0;
110 size_t end = 0;
111 for (; end != path.npos; start = end + 1) {
112 end = path.find('/', start);
113 // Next component, excluding the separator
114 auto part = path.substr(start, end - start);
115 if (part.empty() || part == "."sv) {
116 continue;
117 }
118 if (part == ".."sv) {
119 if (result.empty()) {
120 return {};
121 }
122 auto lastPos = result.rfind('/');
123 if (lastPos == result.npos) {
124 result.clear();
125 } else {
126 result.resize(lastPos);
127 }
128 continue;
129 }
130 result += '/';
131 result += part;
132 }
133
134 return result;
135 }
136
basename(std::string_view path)137 std::string_view basename(std::string_view path) {
138 if (path.empty()) {
139 return {};
140 }
141 if (path == "/"sv) {
142 return "/"sv;
143 }
144 auto pos = path.rfind('/');
145 while (!path.empty() && pos == path.size() - 1) {
146 path.remove_suffix(1);
147 pos = path.rfind('/');
148 }
149 if (pos == path.npos) {
150 return path.empty() ? "/"sv : path;
151 }
152 return path.substr(pos + 1);
153 }
154
dirname(std::string_view path)155 std::string_view dirname(std::string_view path) {
156 if (path.empty()) {
157 return {};
158 }
159 if (path == "/"sv) {
160 return "/"sv;
161 }
162 const auto pos = path.rfind('/');
163 if (pos == 0) {
164 return "/"sv;
165 }
166 if (pos == path.npos) {
167 return "."sv;
168 }
169 return path.substr(0, pos);
170 }
171
CStrWrapper(std::string_view sv)172 details::CStrWrapper::CStrWrapper(std::string_view sv) {
173 if (sv[sv.size()] == '\0') {
174 mCstr = sv.data();
175 } else {
176 mCopy.emplace(sv);
177 mCstr = mCopy->c_str();
178 }
179 }
180
isEmptyDir(std::string_view dir)181 std::optional<bool> isEmptyDir(std::string_view dir) {
182 const auto d = std::unique_ptr<DIR, decltype(&::closedir)>{::opendir(c_str(dir)), ::closedir};
183 if (!d) {
184 if (errno == EPERM || errno == EACCES) {
185 return std::nullopt;
186 }
187 return false;
188 }
189 while (auto entry = ::readdir(d.get())) {
190 if (entry->d_type != DT_DIR) {
191 return false;
192 }
193 if (entry->d_name != "."sv && entry->d_name != ".."sv) {
194 return false;
195 }
196 }
197 return true;
198 }
199
startsWith(std::string_view path,std::string_view prefix)200 bool startsWith(std::string_view path, std::string_view prefix) {
201 if (!base::StartsWith(path, prefix)) {
202 return false;
203 }
204 return path.size() == prefix.size() || path[prefix.size()] == '/';
205 }
206
207 } // namespace android::incremental::path
208