blob: aa840e2e3c8d173e90926ca6f292ba235d3494c7 [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"
30
31#include "util/Util.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032
Elliott Hughes9a6ec582015-08-19 10:38:36 -070033#ifdef _WIN32
Adam Lesinskica2fc352015-04-03 12:08:26 -070034// Windows includes.
35#include <direct.h>
36#endif
37
Adam Lesinskid5083f62017-01-16 15:07:21 -080038using android::StringPiece;
39
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040namespace aapt {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070041namespace file {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042
Adam Lesinskice5e56e2016-10-21 17:56:45 -070043FileType GetFileType(const StringPiece& path) {
44 struct stat sb;
45 if (stat(path.data(), &sb) < 0) {
46 if (errno == ENOENT || errno == ENOTDIR) {
47 return FileType::kNonexistant;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080048 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070049 return FileType::kUnknown;
50 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 if (S_ISREG(sb.st_mode)) {
53 return FileType::kRegular;
54 } else if (S_ISDIR(sb.st_mode)) {
55 return FileType::kDirectory;
56 } else if (S_ISCHR(sb.st_mode)) {
57 return FileType::kCharDev;
58 } else if (S_ISBLK(sb.st_mode)) {
59 return FileType::kBlockDev;
60 } else if (S_ISFIFO(sb.st_mode)) {
61 return FileType::kFifo;
Adam Lesinskica2fc352015-04-03 12:08:26 -070062#if defined(S_ISLNK)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063 } else if (S_ISLNK(sb.st_mode)) {
64 return FileType::kSymlink;
Adam Lesinskica2fc352015-04-03 12:08:26 -070065#endif
66#if defined(S_ISSOCK)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070067 } else if (S_ISSOCK(sb.st_mode)) {
68 return FileType::kSocket;
Adam Lesinskica2fc352015-04-03 12:08:26 -070069#endif
Adam Lesinskice5e56e2016-10-21 17:56:45 -070070 } else {
71 return FileType::kUnknown;
72 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080073}
74
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075inline static int MkdirImpl(const StringPiece& path) {
Elliott Hughes9a6ec582015-08-19 10:38:36 -070076#ifdef _WIN32
Adam Lesinskid5083f62017-01-16 15:07:21 -080077 return _mkdir(path.to_string().c_str());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080078#else
Adam Lesinskid5083f62017-01-16 15:07:21 -080079 return mkdir(path.to_string().c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080080#endif
81}
82
83bool mkdirs(const StringPiece& path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070084 const char* start = path.begin();
85 const char* end = path.end();
86 for (const char* current = start; current != end; ++current) {
87 if (*current == sDirSep && current != start) {
88 StringPiece parent_path(start, current - start);
89 int result = MkdirImpl(parent_path);
90 if (result < 0 && errno != EEXIST) {
Adam Lesinskic51562c2016-04-28 11:12:38 -070091 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070092 }
Adam Lesinskic51562c2016-04-28 11:12:38 -070093 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 }
95 return MkdirImpl(path) == 0 || errno == EEXIST;
Adam Lesinskic51562c2016-04-28 11:12:38 -070096}
97
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098StringPiece GetStem(const StringPiece& path) {
99 const char* start = path.begin();
100 const char* end = path.end();
101 for (const char* current = end - 1; current != start - 1; --current) {
102 if (*current == sDirSep) {
103 return StringPiece(start, current - start);
104 }
105 }
106 return {};
107}
108
109StringPiece GetFilename(const StringPiece& path) {
110 const char* end = path.end();
111 const char* last_dir_sep = path.begin();
112 for (const char* c = path.begin(); c != end; ++c) {
113 if (*c == sDirSep) {
114 last_dir_sep = c + 1;
115 }
116 }
117 return StringPiece(last_dir_sep, end - last_dir_sep);
118}
119
120StringPiece GetExtension(const StringPiece& path) {
121 StringPiece filename = GetFilename(path);
122 const char* const end = filename.end();
123 const char* c = std::find(filename.begin(), end, '.');
124 if (c != end) {
125 return StringPiece(c, end - c);
126 }
127 return {};
128}
129
130void AppendPath(std::string* base, StringPiece part) {
131 CHECK(base != nullptr);
132 const bool base_has_trailing_sep =
133 (!base->empty() && *(base->end() - 1) == sDirSep);
134 const bool part_has_leading_sep =
135 (!part.empty() && *(part.begin()) == sDirSep);
136 if (base_has_trailing_sep && part_has_leading_sep) {
137 // Remove the part's leading sep
138 part = part.substr(1, part.size() - 1);
139 } else if (!base_has_trailing_sep && !part_has_leading_sep) {
140 // None of the pieces has a separator.
141 *base += sDirSep;
142 }
143 base->append(part.data(), part.size());
144}
145
146std::string PackageToPath(const StringPiece& package) {
147 std::string out_path;
148 for (StringPiece part : util::Tokenize(package, '.')) {
149 AppendPath(&out_path, part);
150 }
151 return out_path;
152}
153
154Maybe<android::FileMap> MmapPath(const StringPiece& path,
155 std::string* out_error) {
156 std::unique_ptr<FILE, decltype(fclose)*> f = {fopen(path.data(), "rb"),
157 fclose};
158 if (!f) {
159 if (out_error) *out_error = android::base::SystemErrorCodeToString(errno);
160 return {};
161 }
162
163 int fd = fileno(f.get());
164
165 struct stat filestats = {};
166 if (fstat(fd, &filestats) != 0) {
167 if (out_error) *out_error = android::base::SystemErrorCodeToString(errno);
168 return {};
169 }
170
171 android::FileMap filemap;
172 if (filestats.st_size == 0) {
173 // mmap doesn't like a length of 0. Instead we return an empty FileMap.
174 return std::move(filemap);
175 }
176
177 if (!filemap.create(path.data(), fd, 0, filestats.st_size, true)) {
178 if (out_error) *out_error = android::base::SystemErrorCodeToString(errno);
179 return {};
180 }
181 return std::move(filemap);
182}
183
184bool AppendArgsFromFile(const StringPiece& path,
185 std::vector<std::string>* out_arglist,
186 std::string* out_error) {
187 std::string contents;
Adam Lesinskid5083f62017-01-16 15:07:21 -0800188 if (!android::base::ReadFileToString(path.to_string(), &contents)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700189 if (out_error) *out_error = "failed to read argument-list file";
190 return false;
191 }
192
193 for (StringPiece line : util::Tokenize(contents, ' ')) {
194 line = util::TrimWhitespace(line);
195 if (!line.empty()) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800196 out_arglist->push_back(line.to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700197 }
198 }
199 return true;
200}
201
202bool FileFilter::SetPattern(const StringPiece& pattern) {
203 pattern_tokens_ = util::SplitAndLowercase(pattern, ':');
204 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800205}
206
207bool FileFilter::operator()(const std::string& filename, FileType type) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700208 if (filename == "." || filename == "..") {
209 return false;
210 }
211
212 const char kDir[] = "dir";
213 const char kFile[] = "file";
214 const size_t filename_len = filename.length();
215 bool chatty = true;
216 for (const std::string& token : pattern_tokens_) {
217 const char* token_str = token.c_str();
218 if (*token_str == '!') {
219 chatty = false;
220 token_str++;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800221 }
222
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700223 if (strncasecmp(token_str, kDir, sizeof(kDir)) == 0) {
224 if (type != FileType::kDirectory) {
225 continue;
226 }
227 token_str += sizeof(kDir);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800228 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229
230 if (strncasecmp(token_str, kFile, sizeof(kFile)) == 0) {
231 if (type != FileType::kRegular) {
232 continue;
233 }
234 token_str += sizeof(kFile);
235 }
236
237 bool ignore = false;
238 size_t n = strlen(token_str);
239 if (*token_str == '*') {
240 // Math suffix.
241 token_str++;
242 n--;
243 if (n <= filename_len) {
244 ignore =
245 strncasecmp(token_str, filename.c_str() + filename_len - n, n) == 0;
246 }
247 } else if (n > 1 && token_str[n - 1] == '*') {
248 // Match prefix.
249 ignore = strncasecmp(token_str, filename.c_str(), n - 1) == 0;
250 } else {
251 ignore = strcasecmp(token_str, filename.c_str()) == 0;
252 }
253
254 if (ignore) {
255 if (chatty) {
256 diag_->Warn(DiagMessage()
257 << "skipping "
258 << (type == FileType::kDirectory ? "dir '" : "file '")
259 << filename << "' due to ignore pattern '" << token << "'");
260 }
261 return false;
262 }
263 }
264 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800265}
266
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700267} // namespace file
268} // namespace aapt