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