blob: e15f935cad27df70f0ca1457908a0c4efe33075f [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"
24
25#include "Source.h"
Adam Lesinski00451162017-10-03 07:44:08 -070026#include "io/FileStream.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080027#include "util/Files.h"
28#include "util/Maybe.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080029#include "util/Util.h"
30
Adam Lesinski00451162017-10-03 07:44:08 -070031using ::android::StringPiece;
Ryan Mitchellf3649d62018-08-02 16:16:45 -070032using ::android::base::SystemErrorCodeToString;
Adam Lesinskid5083f62017-01-16 15:07:21 -080033
Adam Lesinskia6fe3452015-12-09 15:20:52 -080034namespace aapt {
35namespace io {
36
Adam Lesinskice5e56e2016-10-21 17:56:45 -070037RegularFile::RegularFile(const Source& source) : source_(source) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -080038
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039std::unique_ptr<IData> RegularFile::OpenAsData() {
40 android::FileMap map;
41 if (Maybe<android::FileMap> map = file::MmapPath(source_.path, nullptr)) {
42 if (map.value().getDataPtr() && map.value().getDataLength() > 0) {
43 return util::make_unique<MmappedData>(std::move(map.value()));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080044 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 return util::make_unique<EmptyData>();
46 }
47 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080048}
49
Adam Lesinski00451162017-10-03 07:44:08 -070050std::unique_ptr<io::InputStream> RegularFile::OpenInputStream() {
51 return util::make_unique<FileInputStream>(source_.path);
52}
53
54const Source& RegularFile::GetSource() const {
55 return source_;
56}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070057
58FileCollectionIterator::FileCollectionIterator(FileCollection* collection)
59 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
60
Adam Lesinski00451162017-10-03 07:44:08 -070061bool FileCollectionIterator::HasNext() {
62 return current_ != end_;
63}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064
65IFile* FileCollectionIterator::Next() {
66 IFile* result = current_->second.get();
67 ++current_;
68 return result;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080069}
70
Ryan Mitchellf3649d62018-08-02 16:16:45 -070071std::unique_ptr<FileCollection> FileCollection::Create(const android::StringPiece& root,
72 std::string* outError) {
73 std::unique_ptr<FileCollection> collection =
74 std::unique_ptr<FileCollection>(new FileCollection());
75
76 std::unique_ptr<DIR, decltype(closedir) *> d(opendir(root.data()), closedir);
77 if (!d) {
78 *outError = "failed to open directory: " + SystemErrorCodeToString(errno);
79 return nullptr;
80 }
81
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -080082 std::vector<std::string> sorted_files;
Ryan Mitchellf3649d62018-08-02 16:16:45 -070083 while (struct dirent *entry = readdir(d.get())) {
84 std::string prefix_path = root.to_string();
85 file::AppendPath(&prefix_path, entry->d_name);
86
87 // The directory to iterate over looking for files
88 if (file::GetFileType(prefix_path) != file::FileType::kDirectory
89 || file::IsHidden(prefix_path)) {
90 continue;
91 }
92
93 std::unique_ptr<DIR, decltype(closedir)*> subdir(opendir(prefix_path.data()), closedir);
94 if (!subdir) {
95 *outError = "failed to open directory: " + SystemErrorCodeToString(errno);
96 return nullptr;
97 }
98
99 while (struct dirent* leaf_entry = readdir(subdir.get())) {
100 std::string full_path = prefix_path;
101 file::AppendPath(&full_path, leaf_entry->d_name);
102
103 // Do not add folders to the file collection
104 if (file::GetFileType(full_path) == file::FileType::kDirectory
105 || file::IsHidden(full_path)) {
106 continue;
107 }
108
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800109 sorted_files.push_back(full_path);
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700110 }
111 }
112
Ryan Mitchellf22ed8d2019-02-20 08:05:31 -0800113 std::sort(sorted_files.begin(), sorted_files.end());
114 for (const std::string& full_path : sorted_files) {
115 collection->InsertFile(full_path);
116 }
117
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700118 return collection;
119}
120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121IFile* FileCollection::InsertFile(const StringPiece& path) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800122 return (files_[path.to_string()] = util::make_unique<RegularFile>(Source(path))).get();
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800123}
124
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125IFile* FileCollection::FindFile(const StringPiece& path) {
Adam Lesinskid5083f62017-01-16 15:07:21 -0800126 auto iter = files_.find(path.to_string());
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 if (iter != files_.end()) {
128 return iter->second.get();
129 }
130 return nullptr;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800131}
132
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700133std::unique_ptr<IFileCollectionIterator> FileCollection::Iterator() {
134 return util::make_unique<FileCollectionIterator>(this);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800135}
136
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700137char FileCollection::GetDirSeparator() {
138 return file::sDirSep;
139}
140
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141} // namespace io
142} // namespace aapt