blob: 042ff0e6c1b9acc623b36e6b4b362b01659b7f3e [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"
18#include "util/Util.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080019
Adam Lesinski803c7c82016-04-06 16:09:43 -070020#include <algorithm>
Adam Lesinskic51562c2016-04-28 11:12:38 -070021#include <android-base/file.h>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include <cerrno>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include <cstdio>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <dirent.h>
25#include <string>
26#include <sys/stat.h>
27
Elliott Hughes9a6ec582015-08-19 10:38:36 -070028#ifdef _WIN32
Adam Lesinskica2fc352015-04-03 12:08:26 -070029// Windows includes.
30#include <direct.h>
31#endif
32
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033namespace aapt {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034namespace file {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035
36FileType getFileType(const StringPiece& path) {
37 struct stat sb;
38 if (stat(path.data(), &sb) < 0) {
39 if (errno == ENOENT || errno == ENOTDIR) {
40 return FileType::kNonexistant;
41 }
42 return FileType::kUnknown;
43 }
44
45 if (S_ISREG(sb.st_mode)) {
46 return FileType::kRegular;
47 } else if (S_ISDIR(sb.st_mode)) {
48 return FileType::kDirectory;
49 } else if (S_ISCHR(sb.st_mode)) {
50 return FileType::kCharDev;
51 } else if (S_ISBLK(sb.st_mode)) {
52 return FileType::kBlockDev;
53 } else if (S_ISFIFO(sb.st_mode)) {
54 return FileType::kFifo;
Adam Lesinskica2fc352015-04-03 12:08:26 -070055#if defined(S_ISLNK)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056 } else if (S_ISLNK(sb.st_mode)) {
57 return FileType::kSymlink;
Adam Lesinskica2fc352015-04-03 12:08:26 -070058#endif
59#if defined(S_ISSOCK)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060 } else if (S_ISSOCK(sb.st_mode)) {
61 return FileType::kSocket;
Adam Lesinskica2fc352015-04-03 12:08:26 -070062#endif
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080063 } else {
64 return FileType::kUnknown;
65 }
66}
67
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068std::vector<std::string> listFiles(const StringPiece& root, std::string* outError) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080069 DIR* dir = opendir(root.data());
70 if (dir == nullptr) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070071 if (outError) {
72 std::stringstream errorStr;
73 errorStr << "unable to open file: " << strerror(errno);
74 *outError = errorStr.str();
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075 }
George Burgess IV56f5f7b2016-10-11 18:08:23 -070076 return {};
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080077 }
78
79 std::vector<std::string> files;
80 dirent* entry;
81 while ((entry = readdir(dir))) {
82 files.emplace_back(entry->d_name);
83 }
84
85 closedir(dir);
86 return files;
87}
88
89inline static int mkdirImpl(const StringPiece& path) {
Elliott Hughes9a6ec582015-08-19 10:38:36 -070090#ifdef _WIN32
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080091 return _mkdir(path.toString().c_str());
92#else
93 return mkdir(path.toString().c_str(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
94#endif
95}
96
97bool mkdirs(const StringPiece& path) {
98 const char* start = path.begin();
99 const char* end = path.end();
100 for (const char* current = start; current != end; ++current) {
Adam Lesinski96917c22016-03-09 13:11:25 -0800101 if (*current == sDirSep && current != start) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800102 StringPiece parentPath(start, current - start);
103 int result = mkdirImpl(parentPath);
104 if (result < 0 && errno != EEXIST) {
105 return false;
106 }
107 }
108 }
109 return mkdirImpl(path) == 0 || errno == EEXIST;
110}
111
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700112StringPiece getStem(const StringPiece& path) {
Adam Lesinski4d3a9872015-04-09 19:53:22 -0700113 const char* start = path.begin();
114 const char* end = path.end();
115 for (const char* current = end - 1; current != start - 1; --current) {
116 if (*current == sDirSep) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700117 return StringPiece(start, current - start);
Adam Lesinski4d3a9872015-04-09 19:53:22 -0700118 }
119 }
120 return {};
121}
122
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123StringPiece getFilename(const StringPiece& path) {
124 const char* end = path.end();
125 const char* lastDirSep = path.begin();
126 for (const char* c = path.begin(); c != end; ++c) {
127 if (*c == sDirSep) {
128 lastDirSep = c + 1;
129 }
130 }
131 return StringPiece(lastDirSep, end - lastDirSep);
132}
133
134StringPiece getExtension(const StringPiece& path) {
135 StringPiece filename = getFilename(path);
136 const char* const end = filename.end();
137 const char* c = std::find(filename.begin(), end, '.');
138 if (c != end) {
139 return StringPiece(c, end - c);
140 }
141 return {};
142}
143
Adam Lesinski96917c22016-03-09 13:11:25 -0800144void appendPath(std::string* base, StringPiece part) {
145 assert(base);
146 const bool baseHasTrailingSep = (!base->empty() && *(base->end() - 1) == sDirSep);
147 const bool partHasLeadingSep = (!part.empty() && *(part.begin()) == sDirSep);
148 if (baseHasTrailingSep && partHasLeadingSep) {
149 // Remove the part's leading sep
150 part = part.substr(1, part.size() - 1);
151 } else if (!baseHasTrailingSep && !partHasLeadingSep) {
152 // None of the pieces has a separator.
153 *base += sDirSep;
154 }
155 base->append(part.data(), part.size());
156}
157
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700158std::string packageToPath(const StringPiece& package) {
159 std::string outPath;
160 for (StringPiece part : util::tokenize<char>(package, '.')) {
161 appendPath(&outPath, part);
162 }
163 return outPath;
164}
165
166Maybe<android::FileMap> mmapPath(const StringPiece& path, std::string* outError) {
167 std::unique_ptr<FILE, decltype(fclose)*> f = { fopen(path.data(), "rb"), fclose };
168 if (!f) {
169 if (outError) *outError = strerror(errno);
170 return {};
171 }
172
173 int fd = fileno(f.get());
174
175 struct stat fileStats = {};
176 if (fstat(fd, &fileStats) != 0) {
177 if (outError) *outError = strerror(errno);
178 return {};
179 }
180
181 android::FileMap fileMap;
Adam Lesinski52364f72016-01-11 13:10:24 -0800182 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 Lesinski1ab598f2015-08-14 14:26:04 -0700187 if (!fileMap.create(path.data(), fd, 0, fileStats.st_size, true)) {
188 if (outError) *outError = strerror(errno);
189 return {};
190 }
191 return std::move(fileMap);
192}
193
Adam Lesinskic51562c2016-04-28 11:12:38 -0700194bool appendArgsFromFile(const StringPiece& path, std::vector<std::string>* outArgList,
195 std::string* outError) {
196 std::string contents;
197 if (!android::base::ReadFileToString(path.toString(), &contents)) {
198 if (outError) *outError = "failed to read argument-list file";
199 return false;
200 }
201
202 for (StringPiece line : util::tokenize<char>(contents, ' ')) {
203 line = util::trimWhitespace(line);
204 if (!line.empty()) {
205 outArgList->push_back(line.toString());
206 }
207 }
208 return true;
209}
210
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800211bool FileFilter::setPattern(const StringPiece& pattern) {
212 mPatternTokens = util::splitAndLowercase(pattern, ':');
213 return true;
214}
215
216bool FileFilter::operator()(const std::string& filename, FileType type) const {
217 if (filename == "." || filename == "..") {
218 return false;
219 }
220
221 const char kDir[] = "dir";
222 const char kFile[] = "file";
223 const size_t filenameLen = filename.length();
224 bool chatty = true;
225 for (const std::string& token : mPatternTokens) {
226 const char* tokenStr = token.c_str();
227 if (*tokenStr == '!') {
228 chatty = false;
229 tokenStr++;
230 }
231
232 if (strncasecmp(tokenStr, kDir, sizeof(kDir)) == 0) {
233 if (type != FileType::kDirectory) {
234 continue;
235 }
236 tokenStr += sizeof(kDir);
237 }
238
239 if (strncasecmp(tokenStr, kFile, sizeof(kFile)) == 0) {
240 if (type != FileType::kRegular) {
241 continue;
242 }
243 tokenStr += sizeof(kFile);
244 }
245
246 bool ignore = false;
247 size_t n = strlen(tokenStr);
248 if (*tokenStr == '*') {
249 // Math suffix.
250 tokenStr++;
251 n--;
252 if (n <= filenameLen) {
253 ignore = strncasecmp(tokenStr, filename.c_str() + filenameLen - n, n) == 0;
254 }
255 } else if (n > 1 && tokenStr[n - 1] == '*') {
256 // Match prefix.
257 ignore = strncasecmp(tokenStr, filename.c_str(), n - 1) == 0;
258 } else {
259 ignore = strcasecmp(tokenStr, filename.c_str()) == 0;
260 }
261
262 if (ignore) {
263 if (chatty) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700264 mDiag->warn(DiagMessage() << "skipping "
265 << (type == FileType::kDirectory ? "dir '" : "file '")
266 << filename << "' due to ignore pattern '"
267 << token << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800268 }
269 return false;
270 }
271 }
272 return true;
273}
274
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700275} // namespace file
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800276} // namespace aapt