blob: ee95712f157d950e0f53a5df885776395da90a05 [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>
Ryan Mitchell4382e442021-07-14 12:53:01 -070021#include <optional>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080022#include <string>
Izabela Orlowska0faba5f2018-06-01 12:06:31 +010023#include <unordered_set>
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080024#include <vector>
25
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "android-base/macros.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000027#include "androidfw/IDiagnostics.h"
28#include "androidfw/Source.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080029#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070030#include "utils/FileMap.h"
31
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
Luke Edgar9109a412021-11-08 12:57:40 +000043constexpr const char sInvariantDirSep = '/';
44
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080045enum class FileType {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070046 kUnknown = 0,
Ryan Mitchell4382e442021-07-14 12:53:01 -070047 kNonExistant,
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 kRegular,
49 kDirectory,
50 kCharDev,
51 kBlockDev,
52 kFifo,
53 kSymlink,
54 kSocket,
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080055};
56
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070057FileType GetFileType(const std::string& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080058
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070059// Appends a path to `base`, separated by the directory separator.
Adam Lesinskid5083f62017-01-16 15:07:21 -080060void AppendPath(std::string* base, android::StringPiece part);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080061
Ryan Mitchell479fa392019-01-02 17:15:39 -080062// Concatenates the list of paths and separates each part with the directory separator.
63std::string BuildPath(std::vector<const android::StringPiece>&& args);
64
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070065// Makes all the directories in `path`. The last element in the path is interpreted as a directory.
66bool mkdirs(const std::string& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080067
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070068// Returns all but the last part of the path.
Adam Lesinskid5083f62017-01-16 15:07:21 -080069android::StringPiece GetStem(const android::StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070071// Returns the last part of the path with extension.
Adam Lesinskid5083f62017-01-16 15:07:21 -080072android::StringPiece GetFilename(const android::StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070073
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070074// Returns the extension of the path. This is the entire string after the first '.' of the last part
75// of the path.
Adam Lesinskid5083f62017-01-16 15:07:21 -080076android::StringPiece GetExtension(const android::StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070077
Ryan Mitchellf3649d62018-08-02 16:16:45 -070078// Returns whether or not the name of the file or directory is a hidden file name
79bool IsHidden(const android::StringPiece& path);
80
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070081// Converts a package name (com.android.app) to a path: com/android/app
Adam Lesinskid5083f62017-01-16 15:07:21 -080082std::string PackageToPath(const android::StringPiece& package);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070083
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070084// Creates a FileMap for the file at path.
Ryan Mitchell4382e442021-07-14 12:53:01 -070085std::optional<android::FileMap> MmapPath(const std::string& path, std::string* out_error);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070086
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070087// Reads the file at path and appends each line to the outArgList vector.
Adam Lesinskid5083f62017-01-16 15:07:21 -080088bool AppendArgsFromFile(const android::StringPiece& path, std::vector<std::string>* out_arglist,
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 std::string* out_error);
Adam Lesinskic51562c2016-04-28 11:12:38 -070090
Izabela Orlowska0faba5f2018-06-01 12:06:31 +010091// Reads the file at path and appends each line to the outargset set.
92bool AppendSetArgsFromFile(const android::StringPiece& path,
93 std::unordered_set<std::string>* out_argset, std::string* out_error);
94
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070095// Filter that determines which resource files/directories are
96// processed by AAPT. Takes a pattern string supplied by the user.
97// Pattern format is specified in the FileFilter::SetPattern() method.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080098class FileFilter {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 public:
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000100 explicit FileFilter(android::IDiagnostics* diag) : diag_(diag) {
101 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700102
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700103 // Patterns syntax:
104 // - Delimiter is :
105 // - Entry can start with the flag ! to avoid printing a warning
106 // about the file being ignored.
107 // - Entry can have the flag "<dir>" to match only directories
108 // or <file> to match only files. Default is to match both.
109 // - Entry can be a simplified glob "<prefix>*" or "*<suffix>"
110 // where prefix/suffix must have at least 1 character (so that
111 // we don't match a '*' catch-all pattern.)
112 // - The special filenames "." and ".." are always ignored.
113 // - Otherwise the full string is matched.
114 // - match is not case-sensitive.
Adam Lesinskid5083f62017-01-16 15:07:21 -0800115 bool SetPattern(const android::StringPiece& pattern);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800116
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700117 // Applies the filter, returning true for pass, false for fail.
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118 bool operator()(const std::string& filename, FileType type) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800119
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700120 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121 DISALLOW_COPY_AND_ASSIGN(FileFilter);
122
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000123 android::IDiagnostics* diag_;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124 std::vector<std::string> pattern_tokens_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800125};
126
Adam Lesinskib39ad7c2017-03-13 11:40:48 -0700127// Returns a list of files relative to the directory identified by `path`.
128// An optional FileFilter filters out any files that don't pass.
Ryan Mitchell4382e442021-07-14 12:53:01 -0700129std::optional<std::vector<std::string>> FindFiles(const android::StringPiece& path,
Jeremy Meyer56f36e82022-05-20 20:35:42 +0000130 android::IDiagnostics* diag,
Ryan Mitchell4382e442021-07-14 12:53:01 -0700131 const FileFilter* filter = nullptr);
Adam Lesinskib39ad7c2017-03-13 11:40:48 -0700132
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133} // namespace file
134} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136#endif // AAPT_FILES_H