blob: bb093abbce634e57784626a1eb69adc872a69dc4 [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 Lesinski6f6ceb72014-11-14 14:48:12 -080021#include <cerrno>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070022#include <cstdio>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <dirent.h>
24#include <string>
25#include <sys/stat.h>
26
Elliott Hughes9a6ec582015-08-19 10:38:36 -070027#ifdef _WIN32
Adam Lesinskica2fc352015-04-03 12:08:26 -070028// Windows includes.
29#include <direct.h>
30#endif
31
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032namespace aapt {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033namespace file {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034
35FileType getFileType(const StringPiece& path) {
36 struct stat sb;
37 if (stat(path.data(), &sb) < 0) {
38 if (errno == ENOENT || errno == ENOTDIR) {
39 return FileType::kNonexistant;
40 }
41 return FileType::kUnknown;
42 }
43
44 if (S_ISREG(sb.st_mode)) {
45 return FileType::kRegular;
46 } else if (S_ISDIR(sb.st_mode)) {
47 return FileType::kDirectory;
48 } else if (S_ISCHR(sb.st_mode)) {
49 return FileType::kCharDev;
50 } else if (S_ISBLK(sb.st_mode)) {
51 return FileType::kBlockDev;
52 } else if (S_ISFIFO(sb.st_mode)) {
53 return FileType::kFifo;
Adam Lesinskica2fc352015-04-03 12:08:26 -070054#if defined(S_ISLNK)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055 } else if (S_ISLNK(sb.st_mode)) {
56 return FileType::kSymlink;
Adam Lesinskica2fc352015-04-03 12:08:26 -070057#endif
58#if defined(S_ISSOCK)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059 } else if (S_ISSOCK(sb.st_mode)) {
60 return FileType::kSocket;
Adam Lesinskica2fc352015-04-03 12:08:26 -070061#endif
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062 } else {
63 return FileType::kUnknown;
64 }
65}
66
Adam Lesinski1ab598f2015-08-14 14:26:04 -070067std::vector<std::string> listFiles(const StringPiece& root, std::string* outError) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080068 DIR* dir = opendir(root.data());
69 if (dir == nullptr) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070 if (outError) {
71 std::stringstream errorStr;
72 errorStr << "unable to open file: " << strerror(errno);
73 *outError = errorStr.str();
74 return {};
75 }
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080076 }
77
78 std::vector<std::string> files;
79 dirent* entry;
80 while ((entry = readdir(dir))) {
81 files.emplace_back(entry->d_name);
82 }
83
84 closedir(dir);
85 return files;
86}
87
88inline static int mkdirImpl(const StringPiece& path) {
Elliott Hughes9a6ec582015-08-19 10:38:36 -070089#ifdef _WIN32
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080090 return _mkdir(path.toString().c_str());
91#else
92 return mkdir(path.toString().c_str(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
93#endif
94}
95
96bool mkdirs(const StringPiece& path) {
97 const char* start = path.begin();
98 const char* end = path.end();
99 for (const char* current = start; current != end; ++current) {
Adam Lesinski96917c22016-03-09 13:11:25 -0800100 if (*current == sDirSep && current != start) {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800101 StringPiece parentPath(start, current - start);
102 int result = mkdirImpl(parentPath);
103 if (result < 0 && errno != EEXIST) {
104 return false;
105 }
106 }
107 }
108 return mkdirImpl(path) == 0 || errno == EEXIST;
109}
110
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700111StringPiece getStem(const StringPiece& path) {
Adam Lesinski4d3a9872015-04-09 19:53:22 -0700112 const char* start = path.begin();
113 const char* end = path.end();
114 for (const char* current = end - 1; current != start - 1; --current) {
115 if (*current == sDirSep) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700116 return StringPiece(start, current - start);
Adam Lesinski4d3a9872015-04-09 19:53:22 -0700117 }
118 }
119 return {};
120}
121
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700122StringPiece getFilename(const StringPiece& path) {
123 const char* end = path.end();
124 const char* lastDirSep = path.begin();
125 for (const char* c = path.begin(); c != end; ++c) {
126 if (*c == sDirSep) {
127 lastDirSep = c + 1;
128 }
129 }
130 return StringPiece(lastDirSep, end - lastDirSep);
131}
132
133StringPiece getExtension(const StringPiece& path) {
134 StringPiece filename = getFilename(path);
135 const char* const end = filename.end();
136 const char* c = std::find(filename.begin(), end, '.');
137 if (c != end) {
138 return StringPiece(c, end - c);
139 }
140 return {};
141}
142
Adam Lesinski96917c22016-03-09 13:11:25 -0800143void appendPath(std::string* base, StringPiece part) {
144 assert(base);
145 const bool baseHasTrailingSep = (!base->empty() && *(base->end() - 1) == sDirSep);
146 const bool partHasLeadingSep = (!part.empty() && *(part.begin()) == sDirSep);
147 if (baseHasTrailingSep && partHasLeadingSep) {
148 // Remove the part's leading sep
149 part = part.substr(1, part.size() - 1);
150 } else if (!baseHasTrailingSep && !partHasLeadingSep) {
151 // None of the pieces has a separator.
152 *base += sDirSep;
153 }
154 base->append(part.data(), part.size());
155}
156
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157std::string packageToPath(const StringPiece& package) {
158 std::string outPath;
159 for (StringPiece part : util::tokenize<char>(package, '.')) {
160 appendPath(&outPath, part);
161 }
162 return outPath;
163}
164
165Maybe<android::FileMap> mmapPath(const StringPiece& path, std::string* outError) {
166 std::unique_ptr<FILE, decltype(fclose)*> f = { fopen(path.data(), "rb"), fclose };
167 if (!f) {
168 if (outError) *outError = strerror(errno);
169 return {};
170 }
171
172 int fd = fileno(f.get());
173
174 struct stat fileStats = {};
175 if (fstat(fd, &fileStats) != 0) {
176 if (outError) *outError = strerror(errno);
177 return {};
178 }
179
180 android::FileMap fileMap;
Adam Lesinski52364f72016-01-11 13:10:24 -0800181 if (fileStats.st_size == 0) {
182 // mmap doesn't like a length of 0. Instead we return an empty FileMap.
183 return std::move(fileMap);
184 }
185
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700186 if (!fileMap.create(path.data(), fd, 0, fileStats.st_size, true)) {
187 if (outError) *outError = strerror(errno);
188 return {};
189 }
190 return std::move(fileMap);
191}
192
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800193bool FileFilter::setPattern(const StringPiece& pattern) {
194 mPatternTokens = util::splitAndLowercase(pattern, ':');
195 return true;
196}
197
198bool FileFilter::operator()(const std::string& filename, FileType type) const {
199 if (filename == "." || filename == "..") {
200 return false;
201 }
202
203 const char kDir[] = "dir";
204 const char kFile[] = "file";
205 const size_t filenameLen = filename.length();
206 bool chatty = true;
207 for (const std::string& token : mPatternTokens) {
208 const char* tokenStr = token.c_str();
209 if (*tokenStr == '!') {
210 chatty = false;
211 tokenStr++;
212 }
213
214 if (strncasecmp(tokenStr, kDir, sizeof(kDir)) == 0) {
215 if (type != FileType::kDirectory) {
216 continue;
217 }
218 tokenStr += sizeof(kDir);
219 }
220
221 if (strncasecmp(tokenStr, kFile, sizeof(kFile)) == 0) {
222 if (type != FileType::kRegular) {
223 continue;
224 }
225 tokenStr += sizeof(kFile);
226 }
227
228 bool ignore = false;
229 size_t n = strlen(tokenStr);
230 if (*tokenStr == '*') {
231 // Math suffix.
232 tokenStr++;
233 n--;
234 if (n <= filenameLen) {
235 ignore = strncasecmp(tokenStr, filename.c_str() + filenameLen - n, n) == 0;
236 }
237 } else if (n > 1 && tokenStr[n - 1] == '*') {
238 // Match prefix.
239 ignore = strncasecmp(tokenStr, filename.c_str(), n - 1) == 0;
240 } else {
241 ignore = strcasecmp(tokenStr, filename.c_str()) == 0;
242 }
243
244 if (ignore) {
245 if (chatty) {
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700246 mDiag->warn(DiagMessage() << "skipping "
247 << (type == FileType::kDirectory ? "dir '" : "file '")
248 << filename << "' due to ignore pattern '"
249 << token << "'");
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800250 }
251 return false;
252 }
253 }
254 return true;
255}
256
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700257} // namespace file
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800258} // namespace aapt