blob: d90c6b692a7ceaf85021bdc77fcc61ca2c584e32 [file] [log] [blame]
Adam Lesinski6f6ceb72014-11-14 14:48:12 -08001/*
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 Lesinski1ab598f2015-08-14 14:26:04 -070020#include "Diagnostics.h"
21#include "Maybe.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include "Source.h"
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023
Adam Lesinski1ab598f2015-08-14 14:26:04 -070024#include "util/StringPiece.h"
25
26#include <utils/FileMap.h>
Adam Lesinskica2fc352015-04-03 12:08:26 -070027#include <cassert>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070028#include <memory>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080029#include <string>
30#include <vector>
31
32namespace aapt {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070033namespace file {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080034
35#ifdef _WIN32
36constexpr const char sDirSep = '\\';
37#else
38constexpr const char sDirSep = '/';
39#endif
40
41enum class FileType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 kUnknown = 0,
43 kNonexistant,
44 kRegular,
45 kDirectory,
46 kCharDev,
47 kBlockDev,
48 kFifo,
49 kSymlink,
50 kSocket,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080051};
52
53FileType getFileType(const StringPiece& path);
54
55/*
56 * Lists files under the directory `root`. Files are listed
57 * with just their leaf (filename) names.
58 */
59std::vector<std::string> listFiles(const StringPiece& root);
60
61/*
62 * Appends a path to `base`, separated by the directory separator.
63 */
Adam Lesinski96917c22016-03-09 13:11:25 -080064void appendPath(std::string* base, StringPiece part);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080065
66/*
67 * Makes all the directories in `path`. The last element in the path
68 * is interpreted as a directory.
69 */
70bool mkdirs(const StringPiece& path);
71
Adam Lesinski4d3a9872015-04-09 19:53:22 -070072/**
73 * Returns all but the last part of the path.
74 */
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075StringPiece getStem(const StringPiece& path);
76
77/**
78 * Returns the last part of the path with extension.
79 */
80StringPiece getFilename(const StringPiece& path);
81
82/**
83 * Returns the extension of the path. This is the entire string after
84 * the first '.' of the last part of the path.
85 */
86StringPiece getExtension(const StringPiece& path);
87
88/**
89 * Converts a package name (com.android.app) to a path: com/android/app
90 */
91std::string packageToPath(const StringPiece& package);
92
93/**
94 * Creates a FileMap for the file at path.
95 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096Maybe<android::FileMap> mmapPath(const StringPiece& path,
97 std::string* outError);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070098
Adam Lesinskic51562c2016-04-28 11:12:38 -070099/**
100 * Reads the file at path and appends each line to the outArgList vector.
101 */
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102bool appendArgsFromFile(const StringPiece& path,
103 std::vector<std::string>* outArgList,
Adam Lesinskic51562c2016-04-28 11:12:38 -0700104 std::string* outError);
105
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106/*
107 * Filter that determines which resource files/directories are
108 * processed by AAPT. Takes a pattern string supplied by the user.
109 * Pattern format is specified in the
110 * FileFilter::setPattern(const std::string&) method.
111 */
112class FileFilter {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700113 public:
114 explicit FileFilter(IDiagnostics* diag) : mDiag(diag) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700115
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 /*
117 * Patterns syntax:
118 * - Delimiter is :
119 * - Entry can start with the flag ! to avoid printing a warning
120 * about the file being ignored.
121 * - Entry can have the flag "<dir>" to match only directories
122 * or <file> to match only files. Default is to match both.
123 * - Entry can be a simplified glob "<prefix>*" or "*<suffix>"
124 * where prefix/suffix must have at least 1 character (so that
125 * we don't match a '*' catch-all pattern.)
126 * - The special filenames "." and ".." are always ignored.
127 * - Otherwise the full string is matched.
128 * - match is not case-sensitive.
129 */
130 bool setPattern(const StringPiece& pattern);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800131
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 /**
133 * Applies the filter, returning true for pass, false for fail.
134 */
135 bool operator()(const std::string& filename, FileType type) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 private:
138 IDiagnostics* mDiag;
139 std::vector<std::string> mPatternTokens;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800140};
141
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142} // namespace file
143} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800144
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145#endif // AAPT_FILES_H