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" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
| 19 | #include <dirent.h> |
| 20 | #include <sys/stat.h> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 21 | |
Adam Lesinski | 803c7c8 | 2016-04-06 16:09:43 -0700 | [diff] [blame] | 22 | #include <algorithm> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 23 | #include <cerrno> |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 24 | #include <cstdio> |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 25 | #include <string> |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 26 | |
| 27 | #include "android-base/errors.h" |
| 28 | #include "android-base/file.h" |
| 29 | #include "android-base/logging.h" |
| 30 | |
| 31 | #include "util/Util.h" |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 32 | |
Elliott Hughes | 9a6ec58 | 2015-08-19 10:38:36 -0700 | [diff] [blame] | 33 | #ifdef _WIN32 |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 34 | // Windows includes. |
| 35 | #include <direct.h> |
| 36 | #endif |
| 37 | |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 38 | namespace aapt { |
Adam Lesinski | 1ab598f | 2015-08-14 14:26:04 -0700 | [diff] [blame] | 39 | namespace file { |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | FileType GetFileType(const StringPiece& path) { |
| 42 | struct stat sb; |
| 43 | if (stat(path.data(), &sb) < 0) { |
| 44 | if (errno == ENOENT || errno == ENOTDIR) { |
| 45 | return FileType::kNonexistant; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 46 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | return FileType::kUnknown; |
| 48 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 49 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 50 | if (S_ISREG(sb.st_mode)) { |
| 51 | return FileType::kRegular; |
| 52 | } else if (S_ISDIR(sb.st_mode)) { |
| 53 | return FileType::kDirectory; |
| 54 | } else if (S_ISCHR(sb.st_mode)) { |
| 55 | return FileType::kCharDev; |
| 56 | } else if (S_ISBLK(sb.st_mode)) { |
| 57 | return FileType::kBlockDev; |
| 58 | } else if (S_ISFIFO(sb.st_mode)) { |
| 59 | return FileType::kFifo; |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 60 | #if defined(S_ISLNK) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 61 | } else if (S_ISLNK(sb.st_mode)) { |
| 62 | return FileType::kSymlink; |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 63 | #endif |
| 64 | #if defined(S_ISSOCK) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 65 | } else if (S_ISSOCK(sb.st_mode)) { |
| 66 | return FileType::kSocket; |
Adam Lesinski | ca2fc35 | 2015-04-03 12:08:26 -0700 | [diff] [blame] | 67 | #endif |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 68 | } else { |
| 69 | return FileType::kUnknown; |
| 70 | } |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 71 | } |
| 72 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | inline static int MkdirImpl(const StringPiece& path) { |
Elliott Hughes | 9a6ec58 | 2015-08-19 10:38:36 -0700 | [diff] [blame] | 74 | #ifdef _WIN32 |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 75 | return _mkdir(path.ToString().c_str()); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 76 | #else |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | return mkdir(path.ToString().c_str(), |
| 78 | S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IXGRP); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 79 | #endif |
| 80 | } |
| 81 | |
| 82 | bool mkdirs(const StringPiece& path) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | const char* start = path.begin(); |
| 84 | const char* end = path.end(); |
| 85 | for (const char* current = start; current != end; ++current) { |
| 86 | if (*current == sDirSep && current != start) { |
| 87 | StringPiece parent_path(start, current - start); |
| 88 | int result = MkdirImpl(parent_path); |
| 89 | if (result < 0 && errno != EEXIST) { |
Adam Lesinski | c51562c | 2016-04-28 11:12:38 -0700 | [diff] [blame] | 90 | return false; |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | } |
Adam Lesinski | c51562c | 2016-04-28 11:12:38 -0700 | [diff] [blame] | 92 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 93 | } |
| 94 | return MkdirImpl(path) == 0 || errno == EEXIST; |
Adam Lesinski | c51562c | 2016-04-28 11:12:38 -0700 | [diff] [blame] | 95 | } |
| 96 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 97 | StringPiece GetStem(const StringPiece& path) { |
| 98 | const char* start = path.begin(); |
| 99 | const char* end = path.end(); |
| 100 | for (const char* current = end - 1; current != start - 1; --current) { |
| 101 | if (*current == sDirSep) { |
| 102 | return StringPiece(start, current - start); |
| 103 | } |
| 104 | } |
| 105 | return {}; |
| 106 | } |
| 107 | |
| 108 | StringPiece GetFilename(const StringPiece& path) { |
| 109 | const char* end = path.end(); |
| 110 | const char* last_dir_sep = path.begin(); |
| 111 | for (const char* c = path.begin(); c != end; ++c) { |
| 112 | if (*c == sDirSep) { |
| 113 | last_dir_sep = c + 1; |
| 114 | } |
| 115 | } |
| 116 | return StringPiece(last_dir_sep, end - last_dir_sep); |
| 117 | } |
| 118 | |
| 119 | StringPiece GetExtension(const StringPiece& path) { |
| 120 | StringPiece filename = GetFilename(path); |
| 121 | const char* const end = filename.end(); |
| 122 | const char* c = std::find(filename.begin(), end, '.'); |
| 123 | if (c != end) { |
| 124 | return StringPiece(c, end - c); |
| 125 | } |
| 126 | return {}; |
| 127 | } |
| 128 | |
| 129 | void AppendPath(std::string* base, StringPiece part) { |
| 130 | CHECK(base != nullptr); |
| 131 | const bool base_has_trailing_sep = |
| 132 | (!base->empty() && *(base->end() - 1) == sDirSep); |
| 133 | const bool part_has_leading_sep = |
| 134 | (!part.empty() && *(part.begin()) == sDirSep); |
| 135 | if (base_has_trailing_sep && part_has_leading_sep) { |
| 136 | // Remove the part's leading sep |
| 137 | part = part.substr(1, part.size() - 1); |
| 138 | } else if (!base_has_trailing_sep && !part_has_leading_sep) { |
| 139 | // None of the pieces has a separator. |
| 140 | *base += sDirSep; |
| 141 | } |
| 142 | base->append(part.data(), part.size()); |
| 143 | } |
| 144 | |
| 145 | std::string PackageToPath(const StringPiece& package) { |
| 146 | std::string out_path; |
| 147 | for (StringPiece part : util::Tokenize(package, '.')) { |
| 148 | AppendPath(&out_path, part); |
| 149 | } |
| 150 | return out_path; |
| 151 | } |
| 152 | |
| 153 | Maybe<android::FileMap> MmapPath(const StringPiece& path, |
| 154 | std::string* out_error) { |
| 155 | std::unique_ptr<FILE, decltype(fclose)*> f = {fopen(path.data(), "rb"), |
| 156 | fclose}; |
| 157 | if (!f) { |
| 158 | if (out_error) *out_error = android::base::SystemErrorCodeToString(errno); |
| 159 | return {}; |
| 160 | } |
| 161 | |
| 162 | int fd = fileno(f.get()); |
| 163 | |
| 164 | struct stat filestats = {}; |
| 165 | if (fstat(fd, &filestats) != 0) { |
| 166 | if (out_error) *out_error = android::base::SystemErrorCodeToString(errno); |
| 167 | return {}; |
| 168 | } |
| 169 | |
| 170 | android::FileMap filemap; |
| 171 | if (filestats.st_size == 0) { |
| 172 | // mmap doesn't like a length of 0. Instead we return an empty FileMap. |
| 173 | return std::move(filemap); |
| 174 | } |
| 175 | |
| 176 | if (!filemap.create(path.data(), fd, 0, filestats.st_size, true)) { |
| 177 | if (out_error) *out_error = android::base::SystemErrorCodeToString(errno); |
| 178 | return {}; |
| 179 | } |
| 180 | return std::move(filemap); |
| 181 | } |
| 182 | |
| 183 | bool AppendArgsFromFile(const StringPiece& path, |
| 184 | std::vector<std::string>* out_arglist, |
| 185 | std::string* out_error) { |
| 186 | std::string contents; |
| 187 | if (!android::base::ReadFileToString(path.ToString(), &contents)) { |
| 188 | if (out_error) *out_error = "failed to read argument-list file"; |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | for (StringPiece line : util::Tokenize(contents, ' ')) { |
| 193 | line = util::TrimWhitespace(line); |
| 194 | if (!line.empty()) { |
| 195 | out_arglist->push_back(line.ToString()); |
| 196 | } |
| 197 | } |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | bool FileFilter::SetPattern(const StringPiece& pattern) { |
| 202 | pattern_tokens_ = util::SplitAndLowercase(pattern, ':'); |
| 203 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | bool FileFilter::operator()(const std::string& filename, FileType type) const { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 207 | if (filename == "." || filename == "..") { |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | const char kDir[] = "dir"; |
| 212 | const char kFile[] = "file"; |
| 213 | const size_t filename_len = filename.length(); |
| 214 | bool chatty = true; |
| 215 | for (const std::string& token : pattern_tokens_) { |
| 216 | const char* token_str = token.c_str(); |
| 217 | if (*token_str == '!') { |
| 218 | chatty = false; |
| 219 | token_str++; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 220 | } |
| 221 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 222 | if (strncasecmp(token_str, kDir, sizeof(kDir)) == 0) { |
| 223 | if (type != FileType::kDirectory) { |
| 224 | continue; |
| 225 | } |
| 226 | token_str += sizeof(kDir); |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 227 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 228 | |
| 229 | if (strncasecmp(token_str, kFile, sizeof(kFile)) == 0) { |
| 230 | if (type != FileType::kRegular) { |
| 231 | continue; |
| 232 | } |
| 233 | token_str += sizeof(kFile); |
| 234 | } |
| 235 | |
| 236 | bool ignore = false; |
| 237 | size_t n = strlen(token_str); |
| 238 | if (*token_str == '*') { |
| 239 | // Math suffix. |
| 240 | token_str++; |
| 241 | n--; |
| 242 | if (n <= filename_len) { |
| 243 | ignore = |
| 244 | strncasecmp(token_str, filename.c_str() + filename_len - n, n) == 0; |
| 245 | } |
| 246 | } else if (n > 1 && token_str[n - 1] == '*') { |
| 247 | // Match prefix. |
| 248 | ignore = strncasecmp(token_str, filename.c_str(), n - 1) == 0; |
| 249 | } else { |
| 250 | ignore = strcasecmp(token_str, filename.c_str()) == 0; |
| 251 | } |
| 252 | |
| 253 | if (ignore) { |
| 254 | if (chatty) { |
| 255 | diag_->Warn(DiagMessage() |
| 256 | << "skipping " |
| 257 | << (type == FileType::kDirectory ? "dir '" : "file '") |
| 258 | << filename << "' due to ignore pattern '" << token << "'"); |
| 259 | } |
| 260 | return false; |
| 261 | } |
| 262 | } |
| 263 | return true; |
Adam Lesinski | 6f6ceb7 | 2014-11-14 14:48:12 -0800 | [diff] [blame] | 264 | } |
| 265 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 266 | } // namespace file |
| 267 | } // namespace aapt |