Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 1 | /* |
| 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 17 | #include "util/Files.h" |
| 18 | #include "util/Util.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 19 | |
| 20 | #include <cerrno> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 21 | #include <cstdio> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 22 | #include <dirent.h> |
| 23 | #include <string> |
| 24 | #include <sys/stat.h> |
| 25 | |
Elliott Hughes | 9a6ec58 | 2015-08-19 10:38:36 -0700 | [diff] [blame] | 26 | #ifdef _WIN32 |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 27 | // Windows includes. |
| 28 | #include <direct.h> |
| 29 | #endif |
| 30 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 31 | namespace aapt { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 32 | namespace file { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 33 | |
| 34 | FileType getFileType(const StringPiece& path) { |
| 35 | struct stat sb; |
| 36 | if (stat(path.data(), &sb) < 0) { |
| 37 | if (errno == ENOENT || errno == ENOTDIR) { |
| 38 | return FileType::kNonexistant; |
| 39 | } |
| 40 | return FileType::kUnknown; |
| 41 | } |
| 42 | |
| 43 | if (S_ISREG(sb.st_mode)) { |
| 44 | return FileType::kRegular; |
| 45 | } else if (S_ISDIR(sb.st_mode)) { |
| 46 | return FileType::kDirectory; |
| 47 | } else if (S_ISCHR(sb.st_mode)) { |
| 48 | return FileType::kCharDev; |
| 49 | } else if (S_ISBLK(sb.st_mode)) { |
| 50 | return FileType::kBlockDev; |
| 51 | } else if (S_ISFIFO(sb.st_mode)) { |
| 52 | return FileType::kFifo; |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 53 | #if defined(S_ISLNK) |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 54 | } else if (S_ISLNK(sb.st_mode)) { |
| 55 | return FileType::kSymlink; |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 56 | #endif |
| 57 | #if defined(S_ISSOCK) |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 58 | } else if (S_ISSOCK(sb.st_mode)) { |
| 59 | return FileType::kSocket; |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 60 | #endif |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 61 | } else { |
| 62 | return FileType::kUnknown; |
| 63 | } |
| 64 | } |
| 65 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 66 | std::vector<std::string> listFiles(const StringPiece& root, std::string* outError) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 67 | DIR* dir = opendir(root.data()); |
| 68 | if (dir == nullptr) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 69 | if (outError) { |
| 70 | std::stringstream errorStr; |
| 71 | errorStr << "unable to open file: " << strerror(errno); |
| 72 | *outError = errorStr.str(); |
| 73 | return {}; |
| 74 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | std::vector<std::string> files; |
| 78 | dirent* entry; |
| 79 | while ((entry = readdir(dir))) { |
| 80 | files.emplace_back(entry->d_name); |
| 81 | } |
| 82 | |
| 83 | closedir(dir); |
| 84 | return files; |
| 85 | } |
| 86 | |
| 87 | inline static int mkdirImpl(const StringPiece& path) { |
Elliott Hughes | 9a6ec58 | 2015-08-19 10:38:36 -0700 | [diff] [blame] | 88 | #ifdef _WIN32 |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 89 | return _mkdir(path.toString().c_str()); |
| 90 | #else |
| 91 | return mkdir(path.toString().c_str(), S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP); |
| 92 | #endif |
| 93 | } |
| 94 | |
| 95 | bool mkdirs(const StringPiece& path) { |
| 96 | const char* start = path.begin(); |
| 97 | const char* end = path.end(); |
| 98 | for (const char* current = start; current != end; ++current) { |
| 99 | if (*current == sDirSep) { |
| 100 | StringPiece parentPath(start, current - start); |
| 101 | int result = mkdirImpl(parentPath); |
| 102 | if (result < 0 && errno != EEXIST) { |
| 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | return mkdirImpl(path) == 0 || errno == EEXIST; |
| 108 | } |
| 109 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 110 | StringPiece getStem(const StringPiece& path) { |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 111 | const char* start = path.begin(); |
| 112 | const char* end = path.end(); |
| 113 | for (const char* current = end - 1; current != start - 1; --current) { |
| 114 | if (*current == sDirSep) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 115 | return StringPiece(start, current - start); |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | return {}; |
| 119 | } |
| 120 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 121 | StringPiece getFilename(const StringPiece& path) { |
| 122 | const char* end = path.end(); |
| 123 | const char* lastDirSep = path.begin(); |
| 124 | for (const char* c = path.begin(); c != end; ++c) { |
| 125 | if (*c == sDirSep) { |
| 126 | lastDirSep = c + 1; |
| 127 | } |
| 128 | } |
| 129 | return StringPiece(lastDirSep, end - lastDirSep); |
| 130 | } |
| 131 | |
| 132 | StringPiece getExtension(const StringPiece& path) { |
| 133 | StringPiece filename = getFilename(path); |
| 134 | const char* const end = filename.end(); |
| 135 | const char* c = std::find(filename.begin(), end, '.'); |
| 136 | if (c != end) { |
| 137 | return StringPiece(c, end - c); |
| 138 | } |
| 139 | return {}; |
| 140 | } |
| 141 | |
| 142 | std::string packageToPath(const StringPiece& package) { |
| 143 | std::string outPath; |
| 144 | for (StringPiece part : util::tokenize<char>(package, '.')) { |
| 145 | appendPath(&outPath, part); |
| 146 | } |
| 147 | return outPath; |
| 148 | } |
| 149 | |
| 150 | Maybe<android::FileMap> mmapPath(const StringPiece& path, std::string* outError) { |
| 151 | std::unique_ptr<FILE, decltype(fclose)*> f = { fopen(path.data(), "rb"), fclose }; |
| 152 | if (!f) { |
| 153 | if (outError) *outError = strerror(errno); |
| 154 | return {}; |
| 155 | } |
| 156 | |
| 157 | int fd = fileno(f.get()); |
| 158 | |
| 159 | struct stat fileStats = {}; |
| 160 | if (fstat(fd, &fileStats) != 0) { |
| 161 | if (outError) *outError = strerror(errno); |
| 162 | return {}; |
| 163 | } |
| 164 | |
| 165 | android::FileMap fileMap; |
| 166 | if (!fileMap.create(path.data(), fd, 0, fileStats.st_size, true)) { |
| 167 | if (outError) *outError = strerror(errno); |
| 168 | return {}; |
| 169 | } |
| 170 | return std::move(fileMap); |
| 171 | } |
| 172 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 173 | bool FileFilter::setPattern(const StringPiece& pattern) { |
| 174 | mPatternTokens = util::splitAndLowercase(pattern, ':'); |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | bool FileFilter::operator()(const std::string& filename, FileType type) const { |
| 179 | if (filename == "." || filename == "..") { |
| 180 | return false; |
| 181 | } |
| 182 | |
| 183 | const char kDir[] = "dir"; |
| 184 | const char kFile[] = "file"; |
| 185 | const size_t filenameLen = filename.length(); |
| 186 | bool chatty = true; |
| 187 | for (const std::string& token : mPatternTokens) { |
| 188 | const char* tokenStr = token.c_str(); |
| 189 | if (*tokenStr == '!') { |
| 190 | chatty = false; |
| 191 | tokenStr++; |
| 192 | } |
| 193 | |
| 194 | if (strncasecmp(tokenStr, kDir, sizeof(kDir)) == 0) { |
| 195 | if (type != FileType::kDirectory) { |
| 196 | continue; |
| 197 | } |
| 198 | tokenStr += sizeof(kDir); |
| 199 | } |
| 200 | |
| 201 | if (strncasecmp(tokenStr, kFile, sizeof(kFile)) == 0) { |
| 202 | if (type != FileType::kRegular) { |
| 203 | continue; |
| 204 | } |
| 205 | tokenStr += sizeof(kFile); |
| 206 | } |
| 207 | |
| 208 | bool ignore = false; |
| 209 | size_t n = strlen(tokenStr); |
| 210 | if (*tokenStr == '*') { |
| 211 | // Math suffix. |
| 212 | tokenStr++; |
| 213 | n--; |
| 214 | if (n <= filenameLen) { |
| 215 | ignore = strncasecmp(tokenStr, filename.c_str() + filenameLen - n, n) == 0; |
| 216 | } |
| 217 | } else if (n > 1 && tokenStr[n - 1] == '*') { |
| 218 | // Match prefix. |
| 219 | ignore = strncasecmp(tokenStr, filename.c_str(), n - 1) == 0; |
| 220 | } else { |
| 221 | ignore = strcasecmp(tokenStr, filename.c_str()) == 0; |
| 222 | } |
| 223 | |
| 224 | if (ignore) { |
| 225 | if (chatty) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 226 | mDiag->warn(DiagMessage() << "skipping " |
| 227 | << (type == FileType::kDirectory ? "dir '" : "file '") |
| 228 | << filename << "' due to ignore pattern '" |
| 229 | << token << "'"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 230 | } |
| 231 | return false; |
| 232 | } |
| 233 | } |
| 234 | return true; |
| 235 | } |
| 236 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame^] | 237 | } // namespace file |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 238 | } // namespace aapt |