• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/IDiagnostics.h"
28 #include "androidfw/Source.h"
29 #include "androidfw/StringPiece.h"
30 #include "utils/FileMap.h"
31 
32 namespace aapt {
33 namespace file {
34 
35 #ifdef _WIN32
36 constexpr const char sDirSep = '\\';
37 constexpr const char sPathSep = ';';
38 #else
39 constexpr const char sDirSep = '/';
40 constexpr const char sPathSep = ':';
41 #endif
42 
43 constexpr const char sInvariantDirSep = '/';
44 
45 enum class FileType {
46   kUnknown = 0,
47   kNonExistant,
48   kRegular,
49   kDirectory,
50   kCharDev,
51   kBlockDev,
52   kFifo,
53   kSymlink,
54   kSocket,
55 };
56 
57 FileType GetFileType(const std::string& path);
58 
59 // Appends a path to `base`, separated by the directory separator.
60 void AppendPath(std::string* base, android::StringPiece part);
61 
62 // Concatenates the list of paths and separates each part with the directory separator.
63 std::string BuildPath(std::vector<const android::StringPiece>&& args);
64 
65 // Makes all the directories in `path`. The last element in the path is interpreted as a directory.
66 bool mkdirs(const std::string& path);
67 
68 // Returns all but the last part of the path.
69 android::StringPiece GetStem(android::StringPiece path);
70 
71 // Returns the last part of the path with extension.
72 android::StringPiece GetFilename(android::StringPiece path);
73 
74 // Returns the extension of the path. This is the entire string after the first '.' of the last part
75 // of the path.
76 android::StringPiece GetExtension(android::StringPiece path);
77 
78 // Returns whether or not the name of the file or directory is a hidden file name
79 bool IsHidden(android::StringPiece path);
80 
81 // Converts a package name (com.android.app) to a path: com/android/app
82 std::string PackageToPath(android::StringPiece package);
83 
84 // Creates a FileMap for the file at path.
85 std::optional<android::FileMap> MmapPath(const std::string& path, std::string* out_error);
86 
87 // Reads the file at path and appends each line to the outArgList vector.
88 bool AppendArgsFromFile(android::StringPiece path, std::vector<std::string>* out_arglist,
89                         std::string* out_error);
90 
91 // Reads the file at path and appends each line to the outargset set.
92 bool AppendSetArgsFromFile(android::StringPiece path, std::unordered_set<std::string>* out_argset,
93                            std::string* out_error);
94 
95 // Filter that determines which resource files/directories are
96 // processed by AAPT. Takes a pattern string supplied by the user.
97 // Pattern format is specified in the FileFilter::SetPattern() method.
98 class FileFilter {
99  public:
FileFilter(android::IDiagnostics * diag)100   explicit FileFilter(android::IDiagnostics* diag) : diag_(diag) {
101   }
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(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   android::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(android::StringPiece path,
130                                                   android::IDiagnostics* diag,
131                                                   const FileFilter* filter = nullptr);
132 
133 }  // namespace file
134 }  // namespace aapt
135 
136 #endif  // AAPT_FILES_H
137