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