blob: fc2e45e74b4d113043407e9d69809721acd4ad63 [file] [log] [blame]
Adam Lesinskia6fe3452015-12-09 15:20:52 -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
Adam Lesinskia6fe3452015-12-09 15:20:52 -080017#include "io/FileSystem.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070018
Ryan Mitchellf3649d62018-08-02 16:16:45 -070019#include <dirent.h>
20
21#include "android-base/errors.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080022#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070023#include "utils/FileMap.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024#include "Source.h"
Adam Lesinski00451162017-10-03 07:44:08 -070025#include "io/FileStream.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080026#include "util/Files.h"
Ryan Mitchell4382e442021-07-14 12:53:01 -070027
Adam Lesinskia6fe3452015-12-09 15:20:52 -080028#include "util/Util.h"
29
Adam Lesinski00451162017-10-03 07:44:08 -070030using ::android::StringPiece;
Ryan Mitchellf3649d62018-08-02 16:16:45 -070031using ::android::base::SystemErrorCodeToString;
Adam Lesinskid5083f62017-01-16 15:07:21 -080032
Adam Lesinskia6fe3452015-12-09 15:20:52 -080033namespace aapt {
34namespace io {
35
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036RegularFile::RegularFile(const Source& source) : source_(source) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -080037
Adam Lesinskice5e56e2016-10-21 17:56:45 -070038std::unique_ptr<IData> RegularFile::OpenAsData() {
39 android::FileMap map;
Ryan Mitchell4382e442021-07-14 12:53:01 -070040 if (std::optional<android::FileMap> map = file::MmapPath(source_.path, nullptr)) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 if (map.value().getDataPtr() && map.value().getDataLength() > 0) {
42 return util::make_unique<MmappedData>(std::move(map.value()));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080043 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 return util::make_unique<EmptyData>();
45 }
46 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080047}
48
Adam Lesinski00451162017-10-03 07:44:08 -070049std::unique_ptr<io::InputStream> RegularFile::OpenInputStream() {
50 return util::make_unique<FileInputStream>(source_.path);
51}
52
53const Source& RegularFile::GetSource() const {
54 return source_;
55}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056
57FileCollectionIterator::FileCollectionIterator(FileCollection* collection)
58 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
59
Adam Lesinski00451162017-10-03 07:44:08 -070060bool FileCollectionIterator::HasNext() {
61 return current_ != end_;
62}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070063
64IFile* FileCollectionIterator::Next() {
65 IFile* result = current_->second.get();
66 ++current_;
67 return result;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080068}
69
Ryan Mitchellf3649d62018-08-02 16:16:45 -070070std::unique_ptr<FileCollection> FileCollection::Create(const 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
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -080081 std::vector<std::string> sorted_files;
Ryan Mitchellf3649d62018-08-02 16:16:45 -070082 while (struct dirent *entry = readdir(d.get())) {
83 std::string prefix_path = root.to_string();
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
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800108 sorted_files.push_back(full_path);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700109 }
110 }
111
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800112 std::sort(sorted_files.begin(), sorted_files.end());
113 for (const std::string& full_path : sorted_files) {
114 collection->InsertFile(full_path);
115 }
116
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700117 return collection;
118}
119
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700120IFile* FileCollection::InsertFile(const StringPiece& path) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800121 return (files_[path.to_string()] = util::make_unique<RegularFile>(Source(path))).get();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800122}
123
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700124IFile* FileCollection::FindFile(const StringPiece& path) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800125 auto iter = files_.find(path.to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700126 if (iter != files_.end()) {
127 return iter->second.get();
128 }
129 return nullptr;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800130}
131
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700132std::unique_ptr<IFileCollectionIterator> FileCollection::Iterator() {
133 return util::make_unique<FileCollectionIterator>(this);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800134}
135
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700136char FileCollection::GetDirSeparator() {
137 return file::sDirSep;
138}
139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140} // namespace io
141} // namespace aapt