blob: b26e4fa26de6d5ff6e6b20b32fef8e5af0d3575a [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 <memory>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080021#include <string>
22#include <vector>
23
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024#include "android-base/macros.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080025#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "utils/FileMap.h"
27
28#include "Diagnostics.h"
29#include "Maybe.h"
30#include "Source.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070031
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080032namespace 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 = '\\';
Adam Lesinski448a15c2017-07-25 10:59:26 -070037constexpr const char sPathSep = ';';
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080038#else
39constexpr const char sDirSep = '/';
Adam Lesinski448a15c2017-07-25 10:59:26 -070040constexpr const char sPathSep = ':';
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080041#endif
42
43enum class FileType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070044 kUnknown = 0,
45 kNonexistant,
46 kRegular,
47 kDirectory,
48 kCharDev,
49 kBlockDev,
50 kFifo,
51 kSymlink,
52 kSocket,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080053};
54
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070055FileType GetFileType(const std::string& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080056
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070057// Appends a path to `base`, separated by the directory separator.
Adam Lesinskid5083f62017-01-16 15:07:21 -080058void AppendPath(std::string* base, android::StringPiece part);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070060// Makes all the directories in `path`. The last element in the path is interpreted as a directory.
61bool mkdirs(const std::string& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080062
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070063// Returns all but the last part of the path.
Adam Lesinskid5083f62017-01-16 15:07:21 -080064android::StringPiece GetStem(const android::StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070065
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070066// Returns the last part of the path with extension.
Adam Lesinskid5083f62017-01-16 15:07:21 -080067android::StringPiece GetFilename(const android::StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070069// Returns the extension of the path. This is the entire string after the first '.' of the last part
70// of the path.
Adam Lesinskid5083f62017-01-16 15:07:21 -080071android::StringPiece GetExtension(const android::StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070073// Converts a package name (com.android.app) to a path: com/android/app
Adam Lesinskid5083f62017-01-16 15:07:21 -080074std::string PackageToPath(const android::StringPiece& package);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070076// Creates a FileMap for the file at path.
77Maybe<android::FileMap> MmapPath(const std::string& path, std::string* out_error);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070078
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070079// Reads the file at path and appends each line to the outArgList vector.
Adam Lesinskid5083f62017-01-16 15:07:21 -080080bool AppendArgsFromFile(const android::StringPiece& path, std::vector<std::string>* out_arglist,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070081 std::string* out_error);
Adam Lesinskic51562c2016-04-28 11:12:38 -070082
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070083// 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.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080086class FileFilter {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 explicit FileFilter(IDiagnostics* diag) : diag_(diag) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -070089
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070090 // 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.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800102 bool SetPattern(const android::StringPiece& pattern);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800103
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700104 // Applies the filter, returning true for pass, false for fail.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700105 bool operator()(const std::string& filename, FileType type) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800106
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700107 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700108 DISALLOW_COPY_AND_ASSIGN(FileFilter);
109
110 IDiagnostics* diag_;
111 std::vector<std::string> pattern_tokens_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800112};
113
Adam Lesinskib39ad7c2017-03-13 11:40:48 -0700114// Returns a list of files relative to the directory identified by `path`.
115// An optional FileFilter filters out any files that don't pass.
116Maybe<std::vector<std::string>> FindFiles(const android::StringPiece& path, IDiagnostics* diag,
117 const FileFilter* filter = nullptr);
118
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700119} // namespace file
120} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122#endif // AAPT_FILES_H