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 17 #ifndef AAPT_FILES_H 18 #define AAPT_FILES_H 19 20 #include <memory> 21 #include <optional> 22 #include <string> 23 #include <unordered_set> 24 #include <vector> 25 26 #include "android-base/macros.h" 27 #include "androidfw/StringPiece.h" 28 #include "utils/FileMap.h" 29 30 #include "Diagnostics.h" 31 #include "Source.h" 32 33 namespace aapt { 34 namespace file { 35 36 #ifdef _WIN32 37 constexpr const char sDirSep = '\\'; 38 constexpr const char sPathSep = ';'; 39 #else 40 constexpr const char sDirSep = '/'; 41 constexpr const char sPathSep = ':'; 42 #endif 43 44 constexpr const char sInvariantDirSep = '/'; 45 46 enum class FileType { 47 kUnknown = 0, 48 kNonExistant, 49 kRegular, 50 kDirectory, 51 kCharDev, 52 kBlockDev, 53 kFifo, 54 kSymlink, 55 kSocket, 56 }; 57 58 FileType GetFileType(const std::string& path); 59 60 // Appends a path to `base`, separated by the directory separator. 61 void AppendPath(std::string* base, android::StringPiece part); 62 63 // Concatenates the list of paths and separates each part with the directory separator. 64 std::string BuildPath(std::vector<const android::StringPiece>&& args); 65 66 // Makes all the directories in `path`. The last element in the path is interpreted as a directory. 67 bool mkdirs(const std::string& path); 68 69 // Returns all but the last part of the path. 70 android::StringPiece GetStem(const android::StringPiece& path); 71 72 // Returns the last part of the path with extension. 73 android::StringPiece GetFilename(const android::StringPiece& path); 74 75 // Returns the extension of the path. This is the entire string after the first '.' of the last part 76 // of the path. 77 android::StringPiece GetExtension(const android::StringPiece& path); 78 79 // Returns whether or not the name of the file or directory is a hidden file name 80 bool IsHidden(const android::StringPiece& path); 81 82 // Converts a package name (com.android.app) to a path: com/android/app 83 std::string PackageToPath(const android::StringPiece& package); 84 85 // Creates a FileMap for the file at path. 86 std::optional<android::FileMap> MmapPath(const std::string& path, std::string* out_error); 87 88 // Reads the file at path and appends each line to the outArgList vector. 89 bool AppendArgsFromFile(const android::StringPiece& path, std::vector<std::string>* out_arglist, 90 std::string* out_error); 91 92 // Reads the file at path and appends each line to the outargset set. 93 bool AppendSetArgsFromFile(const android::StringPiece& path, 94 std::unordered_set<std::string>* out_argset, std::string* out_error); 95 96 // Filter that determines which resource files/directories are 97 // processed by AAPT. Takes a pattern string supplied by the user. 98 // Pattern format is specified in the FileFilter::SetPattern() method. 99 class FileFilter { 100 public: FileFilter(IDiagnostics * diag)101 explicit FileFilter(IDiagnostics* diag) : diag_(diag) {} 102 103 // Patterns syntax: 104 // - Delimiter is : 105 // - Entry can start with the flag ! to avoid printing a warning 106 // about the file being ignored. 107 // - Entry can have the flag "<dir>" to match only directories 108 // or <file> to match only files. Default is to match both. 109 // - Entry can be a simplified glob "<prefix>*" or "*<suffix>" 110 // where prefix/suffix must have at least 1 character (so that 111 // we don't match a '*' catch-all pattern.) 112 // - The special filenames "." and ".." are always ignored. 113 // - Otherwise the full string is matched. 114 // - match is not case-sensitive. 115 bool SetPattern(const android::StringPiece& pattern); 116 117 // Applies the filter, returning true for pass, false for fail. 118 bool operator()(const std::string& filename, FileType type) const; 119 120 private: 121 DISALLOW_COPY_AND_ASSIGN(FileFilter); 122 123 IDiagnostics* diag_; 124 std::vector<std::string> pattern_tokens_; 125 }; 126 127 // Returns a list of files relative to the directory identified by `path`. 128 // An optional FileFilter filters out any files that don't pass. 129 std::optional<std::vector<std::string>> FindFiles(const android::StringPiece& path, 130 IDiagnostics* diag, 131 const FileFilter* filter = nullptr); 132 133 } // namespace file 134 } // namespace aapt 135 136 #endif // AAPT_FILES_H 137