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