blob: 6f97efe37921580d0062b022475082694ea9843d [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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
Adam Lesinski1ab598f2015-08-14 14:26:04 -070017#include "util/Files.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
19#include <dirent.h>
20#include <sys/stat.h>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021
Adam Lesinski803c7c82016-04-06 16:09:43 -070022#include <algorithm>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <cerrno>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070024#include <cstdio>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080025#include <string>
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026
27#include "android-base/errors.h"
28#include "android-base/file.h"
29#include "android-base/logging.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070030#include "android-base/unique_fd.h"
31#include "android-base/utf8.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032
33#include "util/Util.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034
Elliott Hughes9a6ec582015-08-19 10:38:36 -070035#ifdef _WIN32
Adam Lesinskica2fc352015-04-03 12:08:26 -070036// Windows includes.
37#include <direct.h>
38#endif
39
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070040using ::android::FileMap;
41using ::android::StringPiece;
42using ::android::base::ReadFileToString;
43using ::android::base::SystemErrorCodeToString;
44using ::android::base::unique_fd;
Adam Lesinskid5083f62017-01-16 15:07:21 -080045
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046namespace aapt {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070047namespace file {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070049FileType GetFileType(const std::string& path) {
50// TODO(adamlesinski): I'd like to move this to ::android::base::utf8 but Windows does some macro
51// trickery with 'stat' and things don't override very well.
52#ifdef _WIN32
53 std::wstring path_utf16;
54 if (!::android::base::UTF8PathToWindowsLongPath(path.c_str(), &path_utf16)) {
55 return FileType::kNonexistant;
56 }
57
58 struct _stat64 sb;
59 int result = _wstat64(path_utf16.c_str(), &sb);
60#else
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 struct stat sb;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070062 int result = stat(path.c_str(), &sb);
63#endif
64
65 if (result == -1) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 if (errno == ENOENT || errno == ENOTDIR) {
67 return FileType::kNonexistant;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069 return FileType::kUnknown;
70 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072 if (S_ISREG(sb.st_mode)) {
73 return FileType::kRegular;
74 } else if (S_ISDIR(sb.st_mode)) {
75 return FileType::kDirectory;
76 } else if (S_ISCHR(sb.st_mode)) {
77 return FileType::kCharDev;
78 } else if (S_ISBLK(sb.st_mode)) {
79 return FileType::kBlockDev;
80 } else if (S_ISFIFO(sb.st_mode)) {
81 return FileType::kFifo;
Adam Lesinskica2fc352015-04-03 12:08:26 -070082#if defined(S_ISLNK)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 } else if (S_ISLNK(sb.st_mode)) {
84 return FileType::kSymlink;
Adam Lesinskica2fc352015-04-03 12:08:26 -070085#endif
86#if defined(S_ISSOCK)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 } else if (S_ISSOCK(sb.st_mode)) {
88 return FileType::kSocket;
Adam Lesinskica2fc352015-04-03 12:08:26 -070089#endif
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 } else {
91 return FileType::kUnknown;
92 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080093}
94
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070095bool mkdirs(const std::string& path) {
96 constexpr const mode_t mode = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP;
97 size_t current_pos = 0u;
98 while ((current_pos = path.find(sDirSep, current_pos)) != std::string::npos) {
99 std::string parent_path = path.substr(0, current_pos);
100 int result = ::android::base::utf8::mkdir(parent_path.c_str(), mode);
101 if (result < 0 && errno != EEXIST) {
102 return false;
Adam Lesinskic51562c2016-04-28 11:12:38 -0700103 }
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700104 current_pos += 1;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 }
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700106 return ::android::base::utf8::mkdir(path.c_str(), mode) == 0 || errno == EEXIST;
Adam Lesinskic51562c2016-04-28 11:12:38 -0700107}
108
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700109StringPiece GetStem(const StringPiece& path) {
110 const char* start = path.begin();
111 const char* end = path.end();
112 for (const char* current = end - 1; current != start - 1; --current) {
113 if (*current == sDirSep) {
114 return StringPiece(start, current - start);
115 }
116 }
117 return {};
118}
119
120StringPiece GetFilename(const StringPiece& path) {
121 const char* end = path.end();
122 const char* last_dir_sep = path.begin();
123 for (const char* c = path.begin(); c != end; ++c) {
124 if (*c == sDirSep) {
125 last_dir_sep = c + 1;
126 }
127 }
128 return StringPiece(last_dir_sep, end - last_dir_sep);
129}
130
131StringPiece GetExtension(const StringPiece& path) {
132 StringPiece filename = GetFilename(path);
133 const char* const end = filename.end();
134 const char* c = std::find(filename.begin(), end, '.');
135 if (c != end) {
136 return StringPiece(c, end - c);
137 }
138 return {};
139}
140
141void AppendPath(std::string* base, StringPiece part) {
142 CHECK(base != nullptr);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700143 const bool base_has_trailing_sep = (!base->empty() && *(base->end() - 1) == sDirSep);
144 const bool part_has_leading_sep = (!part.empty() && *(part.begin()) == sDirSep);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 if (base_has_trailing_sep && part_has_leading_sep) {
146 // Remove the part's leading sep
147 part = part.substr(1, part.size() - 1);
148 } else if (!base_has_trailing_sep && !part_has_leading_sep) {
149 // None of the pieces has a separator.
150 *base += sDirSep;
151 }
152 base->append(part.data(), part.size());
153}
154
155std::string PackageToPath(const StringPiece& package) {
156 std::string out_path;
157 for (StringPiece part : util::Tokenize(package, '.')) {
158 AppendPath(&out_path, part);
159 }
160 return out_path;
161}
162
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700163Maybe<FileMap> MmapPath(const std::string& path, std::string* out_error) {
164 int flags = O_RDONLY | O_CLOEXEC | O_BINARY;
165 unique_fd fd(TEMP_FAILURE_RETRY(::android::base::utf8::open(path.c_str(), flags)));
166 if (fd == -1) {
167 if (out_error) {
168 *out_error = SystemErrorCodeToString(errno);
169 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700170 return {};
171 }
172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173 struct stat filestats = {};
174 if (fstat(fd, &filestats) != 0) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700175 if (out_error) {
176 *out_error = SystemErrorCodeToString(errno);
177 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700178 return {};
179 }
180
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700181 FileMap filemap;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700182 if (filestats.st_size == 0) {
183 // mmap doesn't like a length of 0. Instead we return an empty FileMap.
184 return std::move(filemap);
185 }
186
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700187 if (!filemap.create(path.c_str(), fd, 0, filestats.st_size, true)) {
188 if (out_error) {
189 *out_error = SystemErrorCodeToString(errno);
190 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700191 return {};
192 }
193 return std::move(filemap);
194}
195
Adam Lesinski2354b562017-05-26 16:31:38 -0700196bool AppendArgsFromFile(const StringPiece& path, std::vector<std::string>* out_arglist,
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 std::string* out_error) {
198 std::string contents;
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700199 if (!ReadFileToString(path.to_string(), &contents, true /*follow_symlinks*/)) {
Adam Lesinski2354b562017-05-26 16:31:38 -0700200 if (out_error) {
201 *out_error = "failed to read argument-list file";
202 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700203 return false;
204 }
205
206 for (StringPiece line : util::Tokenize(contents, ' ')) {
207 line = util::TrimWhitespace(line);
208 if (!line.empty()) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800209 out_arglist->push_back(line.to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700210 }
211 }
212 return true;
213}
214
215bool FileFilter::SetPattern(const StringPiece& pattern) {
216 pattern_tokens_ = util::SplitAndLowercase(pattern, ':');
217 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800218}
219
220bool FileFilter::operator()(const std::string& filename, FileType type) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700221 if (filename == "." || filename == "..") {
222 return false;
223 }
224
225 const char kDir[] = "dir";
226 const char kFile[] = "file";
227 const size_t filename_len = filename.length();
228 bool chatty = true;
229 for (const std::string& token : pattern_tokens_) {
230 const char* token_str = token.c_str();
231 if (*token_str == '!') {
232 chatty = false;
233 token_str++;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800234 }
235
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700236 if (strncasecmp(token_str, kDir, sizeof(kDir)) == 0) {
237 if (type != FileType::kDirectory) {
238 continue;
239 }
240 token_str += sizeof(kDir);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800241 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700242
243 if (strncasecmp(token_str, kFile, sizeof(kFile)) == 0) {
244 if (type != FileType::kRegular) {
245 continue;
246 }
247 token_str += sizeof(kFile);
248 }
249
250 bool ignore = false;
251 size_t n = strlen(token_str);
252 if (*token_str == '*') {
253 // Math suffix.
254 token_str++;
255 n--;
256 if (n <= filename_len) {
257 ignore =
258 strncasecmp(token_str, filename.c_str() + filename_len - n, n) == 0;
259 }
260 } else if (n > 1 && token_str[n - 1] == '*') {
261 // Match prefix.
262 ignore = strncasecmp(token_str, filename.c_str(), n - 1) == 0;
263 } else {
264 ignore = strcasecmp(token_str, filename.c_str()) == 0;
265 }
266
267 if (ignore) {
268 if (chatty) {
269 diag_->Warn(DiagMessage()
270 << "skipping "
271 << (type == FileType::kDirectory ? "dir '" : "file '")
272 << filename << "' due to ignore pattern '" << token << "'");
273 }
274 return false;
275 }
276 }
277 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800278}
279
Adam Lesinskib39ad7c2017-03-13 11:40:48 -0700280Maybe<std::vector<std::string>> FindFiles(const android::StringPiece& path, IDiagnostics* diag,
281 const FileFilter* filter) {
282 const std::string root_dir = path.to_string();
283 std::unique_ptr<DIR, decltype(closedir)*> d(opendir(root_dir.data()), closedir);
284 if (!d) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700285 diag->Error(DiagMessage() << SystemErrorCodeToString(errno));
Adam Lesinskib39ad7c2017-03-13 11:40:48 -0700286 return {};
287 }
288
289 std::vector<std::string> files;
290 std::vector<std::string> subdirs;
291 while (struct dirent* entry = readdir(d.get())) {
292 if (util::StartsWith(entry->d_name, ".")) {
293 continue;
294 }
295
296 std::string file_name = entry->d_name;
297 std::string full_path = root_dir;
298 AppendPath(&full_path, file_name);
299 const FileType file_type = GetFileType(full_path);
300
301 if (filter != nullptr) {
302 if (!(*filter)(file_name, file_type)) {
303 continue;
304 }
305 }
306
307 if (file_type == file::FileType::kDirectory) {
308 subdirs.push_back(std::move(file_name));
309 } else {
310 files.push_back(std::move(file_name));
311 }
312 }
313
314 // Now process subdirs.
315 for (const std::string& subdir : subdirs) {
316 std::string full_subdir = root_dir;
317 AppendPath(&full_subdir, subdir);
318 Maybe<std::vector<std::string>> subfiles = FindFiles(full_subdir, diag, filter);
319 if (!subfiles) {
320 return {};
321 }
322
323 for (const std::string& subfile : subfiles.value()) {
324 std::string new_file = subdir;
325 AppendPath(&new_file, subfile);
326 files.push_back(new_file);
327 }
328 }
329 return files;
330}
331
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700332} // namespace file
333} // namespace aapt