blob: 349abbd236f16bb35fcaa3f1e41ed3c1dc4abeac [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
17#include "Files.h"
18#include "Util.h"
19
20#include <cerrno>
21#include <dirent.h>
22#include <string>
23#include <sys/stat.h>
24
Adam Lesinskica2fc352015-04-03 12:08:26 -070025#ifdef HAVE_MS_C_RUNTIME
26// Windows includes.
27#include <direct.h>
28#endif
29
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080030namespace aapt {
31
32FileType getFileType(const StringPiece& path) {
33 struct stat sb;
34 if (stat(path.data(), &sb) < 0) {
35 if (errno == ENOENT || errno == ENOTDIR) {
36 return FileType::kNonexistant;
37 }
38 return FileType::kUnknown;
39 }
40
41 if (S_ISREG(sb.st_mode)) {
42 return FileType::kRegular;
43 } else if (S_ISDIR(sb.st_mode)) {
44 return FileType::kDirectory;
45 } else if (S_ISCHR(sb.st_mode)) {
46 return FileType::kCharDev;
47 } else if (S_ISBLK(sb.st_mode)) {
48 return FileType::kBlockDev;
49 } else if (S_ISFIFO(sb.st_mode)) {
50 return FileType::kFifo;
Adam Lesinskica2fc352015-04-03 12:08:26 -070051#if defined(S_ISLNK)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080052 } else if (S_ISLNK(sb.st_mode)) {
53 return FileType::kSymlink;
Adam Lesinskica2fc352015-04-03 12:08:26 -070054#endif
55#if defined(S_ISSOCK)
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056 } else if (S_ISSOCK(sb.st_mode)) {
57 return FileType::kSocket;
Adam Lesinskica2fc352015-04-03 12:08:26 -070058#endif
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059 } else {
60 return FileType::kUnknown;
61 }
62}
63
64std::vector<std::string> listFiles(const StringPiece& root) {
65 DIR* dir = opendir(root.data());
66 if (dir == nullptr) {
67 Logger::error(Source{ root.toString() })
68 << "unable to open file: "
69 << strerror(errno)
70 << "."
71 << std::endl;
72 return {};
73 }
74
75 std::vector<std::string> files;
76 dirent* entry;
77 while ((entry = readdir(dir))) {
78 files.emplace_back(entry->d_name);
79 }
80
81 closedir(dir);
82 return files;
83}
84
85inline static int mkdirImpl(const StringPiece& path) {
86#ifdef HAVE_MS_C_RUNTIME
87 return _mkdir(path.toString().c_str());
88#else
89 return mkdir(path.toString().c_str(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP);
90#endif
91}
92
93bool mkdirs(const StringPiece& path) {
94 const char* start = path.begin();
95 const char* end = path.end();
96 for (const char* current = start; current != end; ++current) {
97 if (*current == sDirSep) {
98 StringPiece parentPath(start, current - start);
99 int result = mkdirImpl(parentPath);
100 if (result < 0 && errno != EEXIST) {
101 return false;
102 }
103 }
104 }
105 return mkdirImpl(path) == 0 || errno == EEXIST;
106}
107
108bool FileFilter::setPattern(const StringPiece& pattern) {
109 mPatternTokens = util::splitAndLowercase(pattern, ':');
110 return true;
111}
112
113bool FileFilter::operator()(const std::string& filename, FileType type) const {
114 if (filename == "." || filename == "..") {
115 return false;
116 }
117
118 const char kDir[] = "dir";
119 const char kFile[] = "file";
120 const size_t filenameLen = filename.length();
121 bool chatty = true;
122 for (const std::string& token : mPatternTokens) {
123 const char* tokenStr = token.c_str();
124 if (*tokenStr == '!') {
125 chatty = false;
126 tokenStr++;
127 }
128
129 if (strncasecmp(tokenStr, kDir, sizeof(kDir)) == 0) {
130 if (type != FileType::kDirectory) {
131 continue;
132 }
133 tokenStr += sizeof(kDir);
134 }
135
136 if (strncasecmp(tokenStr, kFile, sizeof(kFile)) == 0) {
137 if (type != FileType::kRegular) {
138 continue;
139 }
140 tokenStr += sizeof(kFile);
141 }
142
143 bool ignore = false;
144 size_t n = strlen(tokenStr);
145 if (*tokenStr == '*') {
146 // Math suffix.
147 tokenStr++;
148 n--;
149 if (n <= filenameLen) {
150 ignore = strncasecmp(tokenStr, filename.c_str() + filenameLen - n, n) == 0;
151 }
152 } else if (n > 1 && tokenStr[n - 1] == '*') {
153 // Match prefix.
154 ignore = strncasecmp(tokenStr, filename.c_str(), n - 1) == 0;
155 } else {
156 ignore = strcasecmp(tokenStr, filename.c_str()) == 0;
157 }
158
159 if (ignore) {
160 if (chatty) {
161 Logger::warn()
162 << "skipping " <<
163 (type == FileType::kDirectory ? "dir '" : "file '")
164 << filename
165 << "' due to ignore pattern '"
166 << token
167 << "'."
168 << std::endl;
169 }
170 return false;
171 }
172 }
173 return true;
174}
175
176
177} // namespace aapt