blob: f034607ea8f57d3ffaa45e25dcb58c94ba7f7ec3 [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 Lesinski6f6ceb72014-11-14 14:48:12 -080038namespace aapt {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070039namespace file {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080040
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041FileType GetFileType(const StringPiece& path) {
42 struct stat sb;
43 if (stat(path.data(), &sb) < 0) {
44 if (errno == ENOENT || errno == ENOTDIR) {
45 return FileType::kNonexistant;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080046 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070047 return FileType::kUnknown;
48 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080049
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 if (S_ISREG(sb.st_mode)) {
51 return FileType::kRegular;
52 } else if (S_ISDIR(sb.st_mode)) {
53 return FileType::kDirectory;
54 } else if (S_ISCHR(sb.st_mode)) {
55 return FileType::kCharDev;
56 } else if (S_ISBLK(sb.st_mode)) {
57 return FileType::kBlockDev;
58 } else if (S_ISFIFO(sb.st_mode)) {
59 return FileType::kFifo;
Adam Lesinskica2fc352015-04-03 12:08:26 -070060#if defined(S_ISLNK)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 } else if (S_ISLNK(sb.st_mode)) {
62 return FileType::kSymlink;
Adam Lesinskica2fc352015-04-03 12:08:26 -070063#endif
64#if defined(S_ISSOCK)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070065 } else if (S_ISSOCK(sb.st_mode)) {
66 return FileType::kSocket;
Adam Lesinskica2fc352015-04-03 12:08:26 -070067#endif
Adam Lesinskice5e56e2016-10-21 17:56:45 -070068 } else {
69 return FileType::kUnknown;
70 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080071}
72
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073inline static int MkdirImpl(const StringPiece& path) {
Elliott Hughes9a6ec582015-08-19 10:38:36 -070074#ifdef _WIN32
Adam Lesinskice5e56e2016-10-21 17:56:45 -070075 return _mkdir(path.ToString().c_str());
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076#else
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 return mkdir(path.ToString().c_str(),
78 S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080079#endif
80}
81
82bool mkdirs(const StringPiece& path) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070083 const char* start = path.begin();
84 const char* end = path.end();
85 for (const char* current = start; current != end; ++current) {
86 if (*current == sDirSep && current != start) {
87 StringPiece parent_path(start, current - start);
88 int result = MkdirImpl(parent_path);
89 if (result < 0 && errno != EEXIST) {
Adam Lesinskic51562c2016-04-28 11:12:38 -070090 return false;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 }
Adam Lesinskic51562c2016-04-28 11:12:38 -070092 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093 }
94 return MkdirImpl(path) == 0 || errno == EEXIST;
Adam Lesinskic51562c2016-04-28 11:12:38 -070095}
96
Adam Lesinskice5e56e2016-10-21 17:56:45 -070097StringPiece GetStem(const StringPiece& path) {
98 const char* start = path.begin();
99 const char* end = path.end();
100 for (const char* current = end - 1; current != start - 1; --current) {
101 if (*current == sDirSep) {
102 return StringPiece(start, current - start);
103 }
104 }
105 return {};
106}
107
108StringPiece GetFilename(const StringPiece& path) {
109 const char* end = path.end();
110 const char* last_dir_sep = path.begin();
111 for (const char* c = path.begin(); c != end; ++c) {
112 if (*c == sDirSep) {
113 last_dir_sep = c + 1;
114 }
115 }
116 return StringPiece(last_dir_sep, end - last_dir_sep);
117}
118
119StringPiece GetExtension(const StringPiece& path) {
120 StringPiece filename = GetFilename(path);
121 const char* const end = filename.end();
122 const char* c = std::find(filename.begin(), end, '.');
123 if (c != end) {
124 return StringPiece(c, end - c);
125 }
126 return {};
127}
128
129void AppendPath(std::string* base, StringPiece part) {
130 CHECK(base != nullptr);
131 const bool base_has_trailing_sep =
132 (!base->empty() && *(base->end() - 1) == sDirSep);
133 const bool part_has_leading_sep =
134 (!part.empty() && *(part.begin()) == sDirSep);
135 if (base_has_trailing_sep && part_has_leading_sep) {
136 // Remove the part's leading sep
137 part = part.substr(1, part.size() - 1);
138 } else if (!base_has_trailing_sep && !part_has_leading_sep) {
139 // None of the pieces has a separator.
140 *base += sDirSep;
141 }
142 base->append(part.data(), part.size());
143}
144
145std::string PackageToPath(const StringPiece& package) {
146 std::string out_path;
147 for (StringPiece part : util::Tokenize(package, '.')) {
148 AppendPath(&out_path, part);
149 }
150 return out_path;
151}
152
153Maybe<android::FileMap> MmapPath(const StringPiece& path,
154 std::string* out_error) {
155 std::unique_ptr<FILE, decltype(fclose)*> f = {fopen(path.data(), "rb"),
156 fclose};
157 if (!f) {
158 if (out_error) *out_error = android::base::SystemErrorCodeToString(errno);
159 return {};
160 }
161
162 int fd = fileno(f.get());
163
164 struct stat filestats = {};
165 if (fstat(fd, &filestats) != 0) {
166 if (out_error) *out_error = android::base::SystemErrorCodeToString(errno);
167 return {};
168 }
169
170 android::FileMap filemap;
171 if (filestats.st_size == 0) {
172 // mmap doesn't like a length of 0. Instead we return an empty FileMap.
173 return std::move(filemap);
174 }
175
176 if (!filemap.create(path.data(), fd, 0, filestats.st_size, true)) {
177 if (out_error) *out_error = android::base::SystemErrorCodeToString(errno);
178 return {};
179 }
180 return std::move(filemap);
181}
182
183bool AppendArgsFromFile(const StringPiece& path,
184 std::vector<std::string>* out_arglist,
185 std::string* out_error) {
186 std::string contents;
187 if (!android::base::ReadFileToString(path.ToString(), &contents)) {
188 if (out_error) *out_error = "failed to read argument-list file";
189 return false;
190 }
191
192 for (StringPiece line : util::Tokenize(contents, ' ')) {
193 line = util::TrimWhitespace(line);
194 if (!line.empty()) {
195 out_arglist->push_back(line.ToString());
196 }
197 }
198 return true;
199}
200
201bool FileFilter::SetPattern(const StringPiece& pattern) {
202 pattern_tokens_ = util::SplitAndLowercase(pattern, ':');
203 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800204}
205
206bool FileFilter::operator()(const std::string& filename, FileType type) const {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700207 if (filename == "." || filename == "..") {
208 return false;
209 }
210
211 const char kDir[] = "dir";
212 const char kFile[] = "file";
213 const size_t filename_len = filename.length();
214 bool chatty = true;
215 for (const std::string& token : pattern_tokens_) {
216 const char* token_str = token.c_str();
217 if (*token_str == '!') {
218 chatty = false;
219 token_str++;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800220 }
221
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700222 if (strncasecmp(token_str, kDir, sizeof(kDir)) == 0) {
223 if (type != FileType::kDirectory) {
224 continue;
225 }
226 token_str += sizeof(kDir);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800227 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700228
229 if (strncasecmp(token_str, kFile, sizeof(kFile)) == 0) {
230 if (type != FileType::kRegular) {
231 continue;
232 }
233 token_str += sizeof(kFile);
234 }
235
236 bool ignore = false;
237 size_t n = strlen(token_str);
238 if (*token_str == '*') {
239 // Math suffix.
240 token_str++;
241 n--;
242 if (n <= filename_len) {
243 ignore =
244 strncasecmp(token_str, filename.c_str() + filename_len - n, n) == 0;
245 }
246 } else if (n > 1 && token_str[n - 1] == '*') {
247 // Match prefix.
248 ignore = strncasecmp(token_str, filename.c_str(), n - 1) == 0;
249 } else {
250 ignore = strcasecmp(token_str, filename.c_str()) == 0;
251 }
252
253 if (ignore) {
254 if (chatty) {
255 diag_->Warn(DiagMessage()
256 << "skipping "
257 << (type == FileType::kDirectory ? "dir '" : "file '")
258 << filename << "' due to ignore pattern '" << token << "'");
259 }
260 return false;
261 }
262 }
263 return true;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800264}
265
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700266} // namespace file
267} // namespace aapt