blob: 481a4cdb6ad0995a6b3a1a147c9a5a656c30ccf8 [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>
Izabela Orlowska0faba5f2018-06-01 12:06:31 +010022#include <unordered_set>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080023#include <vector>
24
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025#include "android-base/macros.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080026#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070027#include "utils/FileMap.h"
28
29#include "Diagnostics.h"
30#include "Maybe.h"
31#include "Source.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080033namespace aapt {
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034namespace file {
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080035
36#ifdef _WIN32
37constexpr const char sDirSep = '\\';
Adam Lesinski448a15c2017-07-25 10:59:26 -070038constexpr const char sPathSep = ';';
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080039#else
40constexpr const char sDirSep = '/';
Adam Lesinski448a15c2017-07-25 10:59:26 -070041constexpr const char sPathSep = ':';
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080042#endif
43
44enum class FileType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070045 kUnknown = 0,
46 kNonexistant,
47 kRegular,
48 kDirectory,
49 kCharDev,
50 kBlockDev,
51 kFifo,
52 kSymlink,
53 kSocket,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054};
55
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070056FileType GetFileType(const std::string& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080057
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070058// Appends a path to `base`, separated by the directory separator.
Adam Lesinskid5083f62017-01-16 15:07:21 -080059void AppendPath(std::string* base, android::StringPiece part);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080060
Ryan Mitchell479fa392019-01-02 17:15:39 -080061// Concatenates the list of paths and separates each part with the directory separator.
62std::string BuildPath(std::vector<const android::StringPiece>&& args);
63
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070064// Makes all the directories in `path`. The last element in the path is interpreted as a directory.
65bool mkdirs(const std::string& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080066
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070067// Returns all but the last part of the path.
Adam Lesinskid5083f62017-01-16 15:07:21 -080068android::StringPiece GetStem(const android::StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070069
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070070// Returns the last part of the path with extension.
Adam Lesinskid5083f62017-01-16 15:07:21 -080071android::StringPiece GetFilename(const android::StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070073// Returns the extension of the path. This is the entire string after the first '.' of the last part
74// of the path.
Adam Lesinskid5083f62017-01-16 15:07:21 -080075android::StringPiece GetExtension(const android::StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076
Ryan Mitchellf3649d62018-08-02 16:16:45 -070077// Returns whether or not the name of the file or directory is a hidden file name
78bool IsHidden(const android::StringPiece& path);
79
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070080// Converts a package name (com.android.app) to a path: com/android/app
Adam Lesinskid5083f62017-01-16 15:07:21 -080081std::string PackageToPath(const android::StringPiece& package);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070082
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070083// Creates a FileMap for the file at path.
84Maybe<android::FileMap> MmapPath(const std::string& path, std::string* out_error);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070085
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070086// Reads the file at path and appends each line to the outArgList vector.
Adam Lesinskid5083f62017-01-16 15:07:21 -080087bool AppendArgsFromFile(const android::StringPiece& path, std::vector<std::string>* out_arglist,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070088 std::string* out_error);
Adam Lesinskic51562c2016-04-28 11:12:38 -070089
Izabela Orlowska0faba5f2018-06-01 12:06:31 +010090// Reads the file at path and appends each line to the outargset set.
91bool AppendSetArgsFromFile(const android::StringPiece& path,
92 std::unordered_set<std::string>* out_argset, std::string* out_error);
93
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070094// 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.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080097class FileFilter {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070098 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -070099 explicit FileFilter(IDiagnostics* diag) : diag_(diag) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700100
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700101 // 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.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800113 bool SetPattern(const android::StringPiece& pattern);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800114
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700115 // Applies the filter, returning true for pass, false for fail.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 bool operator()(const std::string& filename, FileType type) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800117
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 DISALLOW_COPY_AND_ASSIGN(FileFilter);
120
121 IDiagnostics* diag_;
122 std::vector<std::string> pattern_tokens_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800123};
124
Adam Lesinskib39ad7c2017-03-13 11:40:48 -0700125// Returns a list of files relative to the directory identified by `path`.
126// An optional FileFilter filters out any files that don't pass.
127Maybe<std::vector<std::string>> FindFiles(const android::StringPiece& path, IDiagnostics* diag,
128 const FileFilter* filter = nullptr);
129
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130} // namespace file
131} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800132
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133#endif // AAPT_FILES_H