Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 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 | |
Mark Punzalan | e567159 | 2023-09-02 00:00:30 +0000 | [diff] [blame] | 17 | #define _POSIX_THREAD_SAFE_FUNCTIONS // For mingw localtime_r(). |
| 18 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 19 | #include "io/FileSystem.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 21 | #include <dirent.h> |
Mark Punzalan | e567159 | 2023-09-02 00:00:30 +0000 | [diff] [blame] | 22 | #include <sys/stat.h> |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 23 | |
| 24 | #include "android-base/errors.h" |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 25 | #include "androidfw/Source.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 26 | #include "androidfw/StringPiece.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 27 | #include "io/FileStream.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 28 | #include "util/Files.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 29 | #include "util/Util.h" |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 30 | #include "utils/FileMap.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 31 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 32 | using ::android::StringPiece; |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 33 | using ::android::base::SystemErrorCodeToString; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 34 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 35 | namespace aapt { |
| 36 | namespace io { |
| 37 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 38 | RegularFile::RegularFile(const android::Source& source) : source_(source) { |
| 39 | } |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 40 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 41 | std::unique_ptr<IData> RegularFile::OpenAsData() { |
| 42 | android::FileMap map; |
Ryan Mitchell | 4382e44 | 2021-07-14 12:53:01 -0700 | [diff] [blame] | 43 | if (std::optional<android::FileMap> map = file::MmapPath(source_.path, nullptr)) { |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 44 | if (map.value().getDataPtr() && map.value().getDataLength() > 0) { |
| 45 | return util::make_unique<MmappedData>(std::move(map.value())); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 46 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 47 | return util::make_unique<EmptyData>(); |
| 48 | } |
| 49 | return {}; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 50 | } |
| 51 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 52 | std::unique_ptr<io::InputStream> RegularFile::OpenInputStream() { |
| 53 | return util::make_unique<FileInputStream>(source_.path); |
| 54 | } |
| 55 | |
Jeremy Meyer | 56f36e8 | 2022-05-20 20:35:42 +0000 | [diff] [blame] | 56 | const android::Source& RegularFile::GetSource() const { |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 57 | return source_; |
| 58 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 59 | |
Mark Punzalan | e567159 | 2023-09-02 00:00:30 +0000 | [diff] [blame] | 60 | bool RegularFile::GetModificationTime(struct tm* buf) const { |
| 61 | if (buf == nullptr) { |
| 62 | return false; |
| 63 | } |
| 64 | struct stat stat_buf; |
| 65 | if (stat(source_.path.c_str(), &stat_buf) != 0) { |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | struct tm* ptm; |
| 70 | struct tm tm_result; |
| 71 | ptm = localtime_r(&stat_buf.st_mtime, &tm_result); |
| 72 | |
| 73 | *buf = *ptm; |
| 74 | return true; |
| 75 | } |
| 76 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 77 | FileCollectionIterator::FileCollectionIterator(FileCollection* collection) |
| 78 | : current_(collection->files_.begin()), end_(collection->files_.end()) {} |
| 79 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 80 | bool FileCollectionIterator::HasNext() { |
| 81 | return current_ != end_; |
| 82 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | |
| 84 | IFile* FileCollectionIterator::Next() { |
| 85 | IFile* result = current_->second.get(); |
| 86 | ++current_; |
| 87 | return result; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 88 | } |
| 89 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 90 | std::unique_ptr<FileCollection> FileCollection::Create(android::StringPiece root, |
| 91 | std::string* outError) { |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 92 | std::unique_ptr<FileCollection> collection = |
| 93 | std::unique_ptr<FileCollection>(new FileCollection()); |
| 94 | |
| 95 | std::unique_ptr<DIR, decltype(closedir) *> d(opendir(root.data()), closedir); |
| 96 | if (!d) { |
| 97 | *outError = "failed to open directory: " + SystemErrorCodeToString(errno); |
| 98 | return nullptr; |
| 99 | } |
| 100 | |
Ryan Mitchell | f22ed8d | 2019-02-20 08:05:31 -0800 | [diff] [blame] | 101 | std::vector<std::string> sorted_files; |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 102 | while (struct dirent *entry = readdir(d.get())) { |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 103 | std::string prefix_path(root); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 104 | file::AppendPath(&prefix_path, entry->d_name); |
| 105 | |
| 106 | // The directory to iterate over looking for files |
| 107 | if (file::GetFileType(prefix_path) != file::FileType::kDirectory |
| 108 | || file::IsHidden(prefix_path)) { |
| 109 | continue; |
| 110 | } |
| 111 | |
| 112 | std::unique_ptr<DIR, decltype(closedir)*> subdir(opendir(prefix_path.data()), closedir); |
| 113 | if (!subdir) { |
| 114 | *outError = "failed to open directory: " + SystemErrorCodeToString(errno); |
| 115 | return nullptr; |
| 116 | } |
| 117 | |
| 118 | while (struct dirent* leaf_entry = readdir(subdir.get())) { |
| 119 | std::string full_path = prefix_path; |
| 120 | file::AppendPath(&full_path, leaf_entry->d_name); |
| 121 | |
| 122 | // Do not add folders to the file collection |
| 123 | if (file::GetFileType(full_path) == file::FileType::kDirectory |
| 124 | || file::IsHidden(full_path)) { |
| 125 | continue; |
| 126 | } |
| 127 | |
Ryan Mitchell | f22ed8d | 2019-02-20 08:05:31 -0800 | [diff] [blame] | 128 | sorted_files.push_back(full_path); |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
Ryan Mitchell | f22ed8d | 2019-02-20 08:05:31 -0800 | [diff] [blame] | 132 | std::sort(sorted_files.begin(), sorted_files.end()); |
| 133 | for (const std::string& full_path : sorted_files) { |
| 134 | collection->InsertFile(full_path); |
| 135 | } |
| 136 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 137 | return collection; |
| 138 | } |
| 139 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 140 | IFile* FileCollection::InsertFile(StringPiece path) { |
| 141 | auto file = util::make_unique<RegularFile>(android::Source(path)); |
| 142 | auto it = files_.lower_bound(path); |
| 143 | if (it != files_.end() && it->first == path) { |
| 144 | it->second = std::move(file); |
| 145 | } else { |
| 146 | it = files_.emplace_hint(it, path, std::move(file)); |
| 147 | } |
| 148 | return it->second.get(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 149 | } |
| 150 | |
Yurii Zubrytskyi | a577514 | 2022-11-02 17:49:49 -0700 | [diff] [blame] | 151 | IFile* FileCollection::FindFile(StringPiece path) { |
| 152 | auto iter = files_.find(path); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 153 | if (iter != files_.end()) { |
| 154 | return iter->second.get(); |
| 155 | } |
| 156 | return nullptr; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 159 | std::unique_ptr<IFileCollectionIterator> FileCollection::Iterator() { |
| 160 | return util::make_unique<FileCollectionIterator>(this); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Ryan Mitchell | 0ce8973 | 2018-10-03 09:20:57 -0700 | [diff] [blame] | 163 | char FileCollection::GetDirSeparator() { |
| 164 | return file::sDirSep; |
| 165 | } |
| 166 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 167 | } // namespace io |
| 168 | } // namespace aapt |