blob: a157dbd808932fb07640088e9746d68da66f9fa9 [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"
25#include "utils/FileMap.h"
26
27#include "Diagnostics.h"
28#include "Maybe.h"
29#include "Source.h"
30#include "util/StringPiece.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 = '\\';
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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053FileType GetFileType(const StringPiece& path);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080054
55/*
56 * Appends a path to `base`, separated by the directory separator.
57 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058void AppendPath(std::string* base, StringPiece part);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -080059
60/*
61 * Makes all the directories in `path`. The last element in the path
62 * is interpreted as a directory.
63 */
64bool mkdirs(const StringPiece& path);
65
Adam Lesinski4d3a9872015-04-09 19:53:22 -070066/**
67 * Returns all but the last part of the path.
68 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070069StringPiece GetStem(const StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070070
71/**
72 * Returns the last part of the path with extension.
73 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074StringPiece GetFilename(const StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075
76/**
77 * Returns the extension of the path. This is the entire string after
78 * the first '.' of the last part of the path.
79 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080StringPiece GetExtension(const StringPiece& path);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070081
82/**
83 * Converts a package name (com.android.app) to a path: com/android/app
84 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070085std::string PackageToPath(const StringPiece& package);
Adam Lesinski1ab598f2015-08-14 14:26:04 -070086
87/**
88 * Creates a FileMap for the file at path.
89 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090Maybe<android::FileMap> MmapPath(const StringPiece& path,
91 std::string* out_error);
Adam Lesinski4d3a9872015-04-09 19:53:22 -070092
Adam Lesinskic51562c2016-04-28 11:12:38 -070093/**
94 * Reads the file at path and appends each line to the outArgList vector.
95 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096bool AppendArgsFromFile(const StringPiece& path,
97 std::vector<std::string>* out_arglist,
98 std::string* out_error);
Adam Lesinskic51562c2016-04-28 11:12:38 -070099
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800100/*
101 * Filter that determines which resource files/directories are
102 * processed by AAPT. Takes a pattern string supplied by the user.
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 * Pattern format is specified in the FileFilter::SetPattern() method.
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800104 */
105class FileFilter {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700106 public:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700107 explicit FileFilter(IDiagnostics* diag) : diag_(diag) {}
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700108
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 /*
110 * Patterns syntax:
111 * - Delimiter is :
112 * - Entry can start with the flag ! to avoid printing a warning
113 * about the file being ignored.
114 * - Entry can have the flag "<dir>" to match only directories
115 * or <file> to match only files. Default is to match both.
116 * - Entry can be a simplified glob "<prefix>*" or "*<suffix>"
117 * where prefix/suffix must have at least 1 character (so that
118 * we don't match a '*' catch-all pattern.)
119 * - The special filenames "." and ".." are always ignored.
120 * - Otherwise the full string is matched.
121 * - match is not case-sensitive.
122 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 bool SetPattern(const StringPiece& pattern);
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 /**
126 * Applies the filter, returning true for pass, false for fail.
127 */
128 bool operator()(const std::string& filename, FileType type) const;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800129
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700130 private:
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 DISALLOW_COPY_AND_ASSIGN(FileFilter);
132
133 IDiagnostics* diag_;
134 std::vector<std::string> pattern_tokens_;
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800135};
136
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137} // namespace file
138} // namespace aapt
Adam Lesinski6f6ceb72014-11-14 14:48:12 -0800139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140#endif // AAPT_FILES_H