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 "Diagnostics.h" 21 #include "Maybe.h" 22 #include "Source.h" 23 24 #include "util/StringPiece.h" 25 26 #include <utils/FileMap.h> 27 #include <cassert> 28 #include <memory> 29 #include <string> 30 #include <vector> 31 32 namespace aapt { 33 namespace file { 34 35 #ifdef _WIN32 36 constexpr const char sDirSep = '\\'; 37 #else 38 constexpr const char sDirSep = '/'; 39 #endif 40 41 enum class FileType { 42 kUnknown = 0, 43 kNonexistant, 44 kRegular, 45 kDirectory, 46 kCharDev, 47 kBlockDev, 48 kFifo, 49 kSymlink, 50 kSocket, 51 }; 52 53 FileType getFileType(const StringPiece& path); 54 55 /* 56 * Lists files under the directory `root`. Files are listed 57 * with just their leaf (filename) names. 58 */ 59 std::vector<std::string> listFiles(const StringPiece& root); 60 61 /* 62 * Appends a path to `base`, separated by the directory separator. 63 */ 64 void appendPath(std::string* base, StringPiece part); 65 66 /* 67 * Makes all the directories in `path`. The last element in the path 68 * is interpreted as a directory. 69 */ 70 bool mkdirs(const StringPiece& path); 71 72 /** 73 * Returns all but the last part of the path. 74 */ 75 StringPiece getStem(const StringPiece& path); 76 77 /** 78 * Returns the last part of the path with extension. 79 */ 80 StringPiece getFilename(const StringPiece& path); 81 82 /** 83 * Returns the extension of the path. This is the entire string after 84 * the first '.' of the last part of the path. 85 */ 86 StringPiece getExtension(const StringPiece& path); 87 88 /** 89 * Converts a package name (com.android.app) to a path: com/android/app 90 */ 91 std::string packageToPath(const StringPiece& package); 92 93 /** 94 * Creates a FileMap for the file at path. 95 */ 96 Maybe<android::FileMap> mmapPath(const StringPiece& path, std::string* outError); 97 98 /** 99 * Reads the file at path and appends each line to the outArgList vector. 100 */ 101 bool appendArgsFromFile(const StringPiece& path, std::vector<std::string>* outArgList, 102 std::string* outError); 103 104 /* 105 * Filter that determines which resource files/directories are 106 * processed by AAPT. Takes a pattern string supplied by the user. 107 * Pattern format is specified in the 108 * FileFilter::setPattern(const std::string&) method. 109 */ 110 class FileFilter { 111 public: FileFilter(IDiagnostics * diag)112 FileFilter(IDiagnostics* diag) : mDiag(diag) { 113 } 114 115 /* 116 * Patterns syntax: 117 * - Delimiter is : 118 * - Entry can start with the flag ! to avoid printing a warning 119 * about the file being ignored. 120 * - Entry can have the flag "<dir>" to match only directories 121 * or <file> to match only files. Default is to match both. 122 * - Entry can be a simplified glob "<prefix>*" or "*<suffix>" 123 * where prefix/suffix must have at least 1 character (so that 124 * we don't match a '*' catch-all pattern.) 125 * - The special filenames "." and ".." are always ignored. 126 * - Otherwise the full string is matched. 127 * - match is not case-sensitive. 128 */ 129 bool setPattern(const StringPiece& pattern); 130 131 /** 132 * Applies the filter, returning true for pass, false for fail. 133 */ 134 bool operator()(const std::string& filename, FileType type) const; 135 136 private: 137 IDiagnostics* mDiag; 138 std::vector<std::string> mPatternTokens; 139 }; 140 141 } // namespace file 142 } // namespace aapt 143 144 #endif // AAPT_FILES_H 145