1 /*
2 * Copyright (C) 2015 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 "util/Files.h"
18
19 #include <dirent.h>
20 #include <sys/stat.h>
21
22 #include <algorithm>
23 #include <cerrno>
24 #include <cstdio>
25 #include <string>
26
27 #include "android-base/errors.h"
28 #include "android-base/file.h"
29 #include "android-base/logging.h"
30 #include "android-base/unique_fd.h"
31 #include "android-base/utf8.h"
32
33 #include "util/Util.h"
34
35 #ifdef _WIN32
36 // Windows includes.
37 #include <windows.h>
38 #endif
39
40 using ::android::FileMap;
41 using ::android::StringPiece;
42 using ::android::base::ReadFileToString;
43 using ::android::base::SystemErrorCodeToString;
44 using ::android::base::unique_fd;
45
46 namespace aapt {
47 namespace file {
48
49 #ifdef _WIN32
GetFileType(const std::string & path)50 FileType GetFileType(const std::string& path) {
51 std::wstring path_utf16;
52 if (!::android::base::UTF8PathToWindowsLongPath(path.c_str(), &path_utf16)) {
53 return FileType::kNonExistant;
54 }
55
56 DWORD result = GetFileAttributesW(path_utf16.c_str());
57 if (result == INVALID_FILE_ATTRIBUTES) {
58 return FileType::kNonExistant;
59 }
60
61 if (result & FILE_ATTRIBUTE_DIRECTORY) {
62 return FileType::kDirectory;
63 }
64
65 // Too many types to consider, just let open fail later.
66 return FileType::kRegular;
67 }
68 #else
69 FileType GetFileType(const std::string& path) {
70 struct stat sb;
71 int result = stat(path.c_str(), &sb);
72
73 if (result == -1) {
74 if (errno == ENOENT || errno == ENOTDIR) {
75 return FileType::kNonExistant;
76 }
77 return FileType::kUnknown;
78 }
79
80 if (S_ISREG(sb.st_mode)) {
81 return FileType::kRegular;
82 } else if (S_ISDIR(sb.st_mode)) {
83 return FileType::kDirectory;
84 } else if (S_ISCHR(sb.st_mode)) {
85 return FileType::kCharDev;
86 } else if (S_ISBLK(sb.st_mode)) {
87 return FileType::kBlockDev;
88 } else if (S_ISFIFO(sb.st_mode)) {
89 return FileType::kFifo;
90 #if defined(S_ISLNK)
91 } else if (S_ISLNK(sb.st_mode)) {
92 return FileType::kSymlink;
93 #endif
94 #if defined(S_ISSOCK)
95 } else if (S_ISSOCK(sb.st_mode)) {
96 return FileType::kSocket;
97 #endif
98 } else {
99 return FileType::kUnknown;
100 }
101 }
102 #endif
103
mkdirs(const std::string & path)104 bool mkdirs(const std::string& path) {
105 #ifdef _WIN32
106 // Start after the long path prefix if present.
107 bool require_drive = false;
108 size_t current_pos = 0u;
109 if (util::StartsWith(path, R"(\\?\)")) {
110 require_drive = true;
111 current_pos = 4u;
112 }
113
114 // Start after the drive path if present.
115 if (path.size() >= 3 && path[current_pos + 1] == ':' &&
116 (path[current_pos + 2] == '\\' || path[current_pos + 2] == '/')) {
117 current_pos += 3u;
118 } else if (require_drive) {
119 return false;
120 }
121 #else
122 // Start after the first character so that we don't consume the root '/'.
123 // This is safe to do with unicode because '/' will never match with a continuation character.
124 size_t current_pos = 1u;
125 #endif
126 constexpr const mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP;
127 while ((current_pos = path.find(sDirSep, current_pos)) != std::string::npos) {
128 std::string parent_path = path.substr(0, current_pos);
129 if (parent_path.empty()) {
130 continue;
131 }
132
133 int result = ::android::base::utf8::mkdir(parent_path.c_str(), mode);
134 if (result < 0 && errno != EEXIST) {
135 return false;
136 }
137 current_pos += 1;
138 }
139 return ::android::base::utf8::mkdir(path.c_str(), mode) == 0 || errno == EEXIST;
140 }
141
GetStem(StringPiece path)142 StringPiece GetStem(StringPiece path) {
143 const char* start = path.begin();
144 const char* end = path.end();
145 for (const char* current = end - 1; current != start - 1; --current) {
146 if (*current == sDirSep) {
147 return StringPiece(start, current - start);
148 }
149 }
150 return {};
151 }
152
GetFilename(StringPiece path)153 StringPiece GetFilename(StringPiece path) {
154 const char* end = path.end();
155 const char* last_dir_sep = path.begin();
156 for (const char* c = path.begin(); c != end; ++c) {
157 if (*c == sDirSep || *c == sInvariantDirSep) {
158 last_dir_sep = c + 1;
159 }
160 }
161 return StringPiece(last_dir_sep, end - last_dir_sep);
162 }
163
GetExtension(StringPiece path)164 StringPiece GetExtension(StringPiece path) {
165 StringPiece filename = GetFilename(path);
166 const char* const end = filename.end();
167 const char* c = std::find(filename.begin(), end, '.');
168 if (c != end) {
169 return StringPiece(c, end - c);
170 }
171 return {};
172 }
173
IsHidden(android::StringPiece path)174 bool IsHidden(android::StringPiece path) {
175 return util::StartsWith(GetFilename(path), ".");
176 }
177
AppendPath(std::string * base,StringPiece part)178 void AppendPath(std::string* base, StringPiece part) {
179 CHECK(base != nullptr);
180 const bool base_has_trailing_sep = (!base->empty() && *(base->end() - 1) == sDirSep);
181 const bool part_has_leading_sep = (!part.empty() && *(part.begin()) == sDirSep);
182 if (base_has_trailing_sep && part_has_leading_sep) {
183 // Remove the part's leading sep
184 part = part.substr(1, part.size() - 1);
185 } else if (!base_has_trailing_sep && !part_has_leading_sep) {
186 // None of the pieces has a separator.
187 *base += sDirSep;
188 }
189 base->append(part.data(), part.size());
190 }
191
BuildPath(std::vector<const StringPiece> && args)192 std::string BuildPath(std::vector<const StringPiece>&& args) {
193 if (args.empty()) {
194 return "";
195 }
196 std::string out{args[0]};
197 for (int i = 1; i < args.size(); i++) {
198 file::AppendPath(&out, args[i]);
199 }
200 return out;
201 }
202
PackageToPath(StringPiece package)203 std::string PackageToPath(StringPiece package) {
204 std::string out_path;
205 for (StringPiece part : util::Tokenize(package, '.')) {
206 AppendPath(&out_path, part);
207 }
208 return out_path;
209 }
210
MmapPath(const std::string & path,std::string * out_error)211 std::optional<FileMap> MmapPath(const std::string& path, std::string* out_error) {
212 int flags = O_RDONLY | O_CLOEXEC | O_BINARY;
213 unique_fd fd(TEMP_FAILURE_RETRY(::android::base::utf8::open(path.c_str(), flags)));
214 if (fd == -1) {
215 if (out_error) {
216 *out_error = SystemErrorCodeToString(errno);
217 }
218 return {};
219 }
220
221 struct stat filestats = {};
222 if (fstat(fd, &filestats) != 0) {
223 if (out_error) {
224 *out_error = SystemErrorCodeToString(errno);
225 }
226 return {};
227 }
228
229 FileMap filemap;
230 if (filestats.st_size == 0) {
231 // mmap doesn't like a length of 0. Instead we return an empty FileMap.
232 return std::move(filemap);
233 }
234
235 if (!filemap.create(path.c_str(), fd, 0, filestats.st_size, true)) {
236 if (out_error) {
237 *out_error = SystemErrorCodeToString(errno);
238 }
239 return {};
240 }
241 return std::move(filemap);
242 }
243
AppendArgsFromFile(StringPiece path,std::vector<std::string> * out_arglist,std::string * out_error)244 bool AppendArgsFromFile(StringPiece path, std::vector<std::string>* out_arglist,
245 std::string* out_error) {
246 std::string contents;
247 if (!ReadFileToString(std::string(path), &contents, true /*follow_symlinks*/)) {
248 if (out_error) {
249 *out_error = "failed to read argument-list file";
250 }
251 return false;
252 }
253
254 for (StringPiece line : util::Tokenize(contents, '\n')) {
255 line = util::TrimWhitespace(line);
256 for (StringPiece arg : util::Tokenize(line, ' ')) {
257 arg = util::TrimWhitespace(arg);
258 if (!arg.empty()) {
259 out_arglist->emplace_back(arg);
260 }
261 }
262 }
263 return true;
264 }
265
AppendSetArgsFromFile(StringPiece path,std::unordered_set<std::string> * out_argset,std::string * out_error)266 bool AppendSetArgsFromFile(StringPiece path, std::unordered_set<std::string>* out_argset,
267 std::string* out_error) {
268 std::string contents;
269 if (!ReadFileToString(std::string(path), &contents, true /*follow_symlinks*/)) {
270 if (out_error) {
271 *out_error = "failed to read argument-list file";
272 }
273 return false;
274 }
275
276 for (StringPiece line : util::Tokenize(contents, '\n')) {
277 line = util::TrimWhitespace(line);
278 for (StringPiece arg : util::Tokenize(line, ' ')) {
279 arg = util::TrimWhitespace(arg);
280 if (!arg.empty()) {
281 out_argset->emplace(arg);
282 }
283 }
284 }
285 return true;
286 }
287
SetPattern(StringPiece pattern)288 bool FileFilter::SetPattern(StringPiece pattern) {
289 pattern_tokens_ = util::SplitAndLowercase(pattern, ':');
290 return true;
291 }
292
operator ()(const std::string & filename,FileType type) const293 bool FileFilter::operator()(const std::string& filename, FileType type) const {
294 if (filename == "." || filename == "..") {
295 return false;
296 }
297
298 const char kDir[] = "dir";
299 const char kFile[] = "file";
300 const size_t filename_len = filename.length();
301 bool chatty = true;
302 for (const std::string& token : pattern_tokens_) {
303 const char* token_str = token.c_str();
304 if (*token_str == '!') {
305 chatty = false;
306 token_str++;
307 }
308
309 if (strncasecmp(token_str, kDir, sizeof(kDir)) == 0) {
310 if (type != FileType::kDirectory) {
311 continue;
312 }
313 token_str += sizeof(kDir);
314 }
315
316 if (strncasecmp(token_str, kFile, sizeof(kFile)) == 0) {
317 if (type != FileType::kRegular) {
318 continue;
319 }
320 token_str += sizeof(kFile);
321 }
322
323 bool ignore = false;
324 size_t n = strlen(token_str);
325 if (*token_str == '*') {
326 // Math suffix.
327 token_str++;
328 n--;
329 if (n <= filename_len) {
330 ignore =
331 strncasecmp(token_str, filename.c_str() + filename_len - n, n) == 0;
332 }
333 } else if (n > 1 && token_str[n - 1] == '*') {
334 // Match prefix.
335 ignore = strncasecmp(token_str, filename.c_str(), n - 1) == 0;
336 } else {
337 ignore = strcasecmp(token_str, filename.c_str()) == 0;
338 }
339
340 if (ignore) {
341 if (chatty) {
342 diag_->Warn(android::DiagMessage()
343 << "skipping " << (type == FileType::kDirectory ? "dir '" : "file '")
344 << filename << "' due to ignore pattern '" << token << "'");
345 }
346 return false;
347 }
348 }
349 return true;
350 }
351
FindFiles(android::StringPiece path,android::IDiagnostics * diag,const FileFilter * filter)352 std::optional<std::vector<std::string>> FindFiles(android::StringPiece path,
353 android::IDiagnostics* diag,
354 const FileFilter* filter) {
355 const auto& root_dir = path;
356 std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()), closedir);
357 if (!d) {
358 diag->Error(android::DiagMessage() << SystemErrorCodeToString(errno) << ": " << root_dir);
359 return {};
360 }
361
362 std::vector<std::string> files;
363 std::vector<std::string> subdirs;
364 while (struct dirent* entry = readdir(d.get())) {
365 if (util::StartsWith(entry->d_name, ".")) {
366 continue;
367 }
368
369 std::string file_name = entry->d_name;
370 std::string full_path{root_dir};
371 AppendPath(&full_path, file_name);
372 const FileType file_type = GetFileType(full_path);
373
374 if (filter != nullptr) {
375 if (!(*filter)(file_name, file_type)) {
376 continue;
377 }
378 }
379
380 if (file_type == file::FileType::kDirectory) {
381 subdirs.push_back(std::move(file_name));
382 } else {
383 files.push_back(std::move(file_name));
384 }
385 }
386
387 // Now process subdirs.
388 for (const std::string& subdir : subdirs) {
389 std::string full_subdir{root_dir};
390 AppendPath(&full_subdir, subdir);
391 std::optional<std::vector<std::string>> subfiles = FindFiles(full_subdir, diag, filter);
392 if (!subfiles) {
393 return {};
394 }
395
396 for (const std::string& subfile : subfiles.value()) {
397 std::string new_file = subdir;
398 AppendPath(&new_file, subfile);
399 files.push_back(new_file);
400 }
401 }
402 return files;
403 }
404
405 } // namespace file
406 } // namespace aapt
407