1 /*
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 #include "io/FileSystem.h"
18
19 #include <dirent.h>
20
21 #include "android-base/errors.h"
22 #include "androidfw/Source.h"
23 #include "androidfw/StringPiece.h"
24 #include "io/FileStream.h"
25 #include "util/Files.h"
26 #include "util/Util.h"
27 #include "utils/FileMap.h"
28
29 using ::android::StringPiece;
30 using ::android::base::SystemErrorCodeToString;
31
32 namespace aapt {
33 namespace io {
34
RegularFile(const android::Source & source)35 RegularFile::RegularFile(const android::Source& source) : source_(source) {
36 }
37
OpenAsData()38 std::unique_ptr<IData> RegularFile::OpenAsData() {
39 android::FileMap map;
40 if (std::optional<android::FileMap> map = file::MmapPath(source_.path, nullptr)) {
41 if (map.value().getDataPtr() && map.value().getDataLength() > 0) {
42 return util::make_unique<MmappedData>(std::move(map.value()));
43 }
44 return util::make_unique<EmptyData>();
45 }
46 return {};
47 }
48
OpenInputStream()49 std::unique_ptr<io::InputStream> RegularFile::OpenInputStream() {
50 return util::make_unique<FileInputStream>(source_.path);
51 }
52
GetSource() const53 const android::Source& RegularFile::GetSource() const {
54 return source_;
55 }
56
FileCollectionIterator(FileCollection * collection)57 FileCollectionIterator::FileCollectionIterator(FileCollection* collection)
58 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
59
HasNext()60 bool FileCollectionIterator::HasNext() {
61 return current_ != end_;
62 }
63
Next()64 IFile* FileCollectionIterator::Next() {
65 IFile* result = current_->second.get();
66 ++current_;
67 return result;
68 }
69
Create(android::StringPiece root,std::string * outError)70 std::unique_ptr<FileCollection> FileCollection::Create(android::StringPiece root,
71 std::string* outError) {
72 std::unique_ptr<FileCollection> collection =
73 std::unique_ptr<FileCollection>(new FileCollection());
74
75 std::unique_ptr<DIR, decltype(closedir) *> d(opendir(root.data()), closedir);
76 if (!d) {
77 *outError = "failed to open directory: " + SystemErrorCodeToString(errno);
78 return nullptr;
79 }
80
81 std::vector<std::string> sorted_files;
82 while (struct dirent *entry = readdir(d.get())) {
83 std::string prefix_path(root);
84 file::AppendPath(&prefix_path, entry->d_name);
85
86 // The directory to iterate over looking for files
87 if (file::GetFileType(prefix_path) != file::FileType::kDirectory
88 || file::IsHidden(prefix_path)) {
89 continue;
90 }
91
92 std::unique_ptr<DIR, decltype(closedir)*> subdir(opendir(prefix_path.data()), closedir);
93 if (!subdir) {
94 *outError = "failed to open directory: " + SystemErrorCodeToString(errno);
95 return nullptr;
96 }
97
98 while (struct dirent* leaf_entry = readdir(subdir.get())) {
99 std::string full_path = prefix_path;
100 file::AppendPath(&full_path, leaf_entry->d_name);
101
102 // Do not add folders to the file collection
103 if (file::GetFileType(full_path) == file::FileType::kDirectory
104 || file::IsHidden(full_path)) {
105 continue;
106 }
107
108 sorted_files.push_back(full_path);
109 }
110 }
111
112 std::sort(sorted_files.begin(), sorted_files.end());
113 for (const std::string& full_path : sorted_files) {
114 collection->InsertFile(full_path);
115 }
116
117 return collection;
118 }
119
InsertFile(StringPiece path)120 IFile* FileCollection::InsertFile(StringPiece path) {
121 auto file = util::make_unique<RegularFile>(android::Source(path));
122 auto it = files_.lower_bound(path);
123 if (it != files_.end() && it->first == path) {
124 it->second = std::move(file);
125 } else {
126 it = files_.emplace_hint(it, path, std::move(file));
127 }
128 return it->second.get();
129 }
130
FindFile(StringPiece path)131 IFile* FileCollection::FindFile(StringPiece path) {
132 auto iter = files_.find(path);
133 if (iter != files_.end()) {
134 return iter->second.get();
135 }
136 return nullptr;
137 }
138
Iterator()139 std::unique_ptr<IFileCollectionIterator> FileCollection::Iterator() {
140 return util::make_unique<FileCollectionIterator>(this);
141 }
142
GetDirSeparator()143 char FileCollection::GetDirSeparator() {
144 return file::sDirSep;
145 }
146
147 } // namespace io
148 } // namespace aapt
149