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 | |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 21 | #include <cerrno> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 22 | #include <cstdio> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | #include <dirent.h> |
| 24 | #include <string> |
| 25 | #include <sys/stat.h> |
| 26 | |
Elliott Hughes | 9a6ec58 | 2015-08-19 10:38:36 -0700 | [diff] [blame] | 27 | #ifdef _WIN32 |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 28 | // Windows includes. |
| 29 | #include <direct.h> |
| 30 | #endif |
| 31 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 32 | namespace aapt { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 33 | namespace file { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 34 | |
| 35 | FileType 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 Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 54 | #if defined(S_ISLNK) |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 55 | } else if (S_ISLNK(sb.st_mode)) { |
| 56 | return FileType::kSymlink; |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 57 | #endif |
| 58 | #if defined(S_ISSOCK) |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 59 | } else if (S_ISSOCK(sb.st_mode)) { |
| 60 | return FileType::kSocket; |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 61 | #endif |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 62 | } else { |
| 63 | return FileType::kUnknown; |
| 64 | } |
| 65 | } |
| 66 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 67 | std::vector<std::string> listFiles(const StringPiece& root, std::string* outError) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 68 | DIR* dir = opendir(root.data()); |
| 69 | if (dir == nullptr) { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 70 | if (outError) { |
| 71 | std::stringstream errorStr; |
| 72 | errorStr << "unable to open file: " << strerror(errno); |
| 73 | *outError = errorStr.str(); |
| 74 | return {}; |
| 75 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | } |
| 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 | |
| 88 | inline static int mkdirImpl(const StringPiece& path) { |
Elliott Hughes | 9a6ec58 | 2015-08-19 10:38:36 -0700 | [diff] [blame] | 89 | #ifdef _WIN32 |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 90 | 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 | |
| 96 | bool 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 Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 100 | if (*current == sDirSep && current != start) { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 101 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 111 | StringPiece getStem(const StringPiece& path) { |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 112 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 116 | return StringPiece(start, current - start); |
Adam Lesinski | 4d3a987 | 2015-04-09 19:53:22 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | return {}; |
| 120 | } |
| 121 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 122 | StringPiece 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 | |
| 133 | StringPiece 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 Lesinski | 96917c2 | 2016-03-09 13:11:25 -0800 | [diff] [blame] | 143 | void 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 157 | std::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 | |
| 165 | Maybe<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 Lesinski | 52364f7 | 2016-01-11 13:10:24 -0800 | [diff] [blame] | 181 | 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 186 | 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 Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 193 | bool FileFilter::setPattern(const StringPiece& pattern) { |
| 194 | mPatternTokens = util::splitAndLowercase(pattern, ':'); |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | bool 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 Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 246 | mDiag->warn(DiagMessage() << "skipping " |
| 247 | << (type == FileType::kDirectory ? "dir '" : "file '") |
| 248 | << filename << "' due to ignore pattern '" |
| 249 | << token << "'"); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 250 | } |
| 251 | return false; |
| 252 | } |
| 253 | } |
| 254 | return true; |
| 255 | } |
| 256 | |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 257 | } // namespace file |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 258 | } // namespace aapt |