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