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 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 17 | #include "io/FileSystem.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 18 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | |
| 21 | #include "android-base/errors.h" |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 22 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 23 | #include "utils/FileMap.h" |
| 24 | |
| 25 | #include "Source.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 26 | #include "io/FileStream.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 27 | #include "util/Files.h" |
| 28 | #include "util/Maybe.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 29 | #include "util/Util.h" |
| 30 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 31 | using ::android::StringPiece; |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 32 | using ::android::base::SystemErrorCodeToString; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 33 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 34 | namespace aapt { |
| 35 | namespace io { |
| 36 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 37 | RegularFile::RegularFile(const Source& source) : source_(source) {} |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 38 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 39 | std::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 Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 44 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 45 | return util::make_unique<EmptyData>(); |
| 46 | } |
| 47 | return {}; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 48 | } |
| 49 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 50 | std::unique_ptr<io::InputStream> RegularFile::OpenInputStream() { |
| 51 | return util::make_unique<FileInputStream>(source_.path); |
| 52 | } |
| 53 | |
| 54 | const Source& RegularFile::GetSource() const { |
| 55 | return source_; |
| 56 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 57 | |
| 58 | FileCollectionIterator::FileCollectionIterator(FileCollection* collection) |
| 59 | : current_(collection->files_.begin()), end_(collection->files_.end()) {} |
| 60 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 61 | bool FileCollectionIterator::HasNext() { |
| 62 | return current_ != end_; |
| 63 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | |
| 65 | IFile* FileCollectionIterator::Next() { |
| 66 | IFile* result = current_->second.get(); |
| 67 | ++current_; |
| 68 | return result; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Ryan Mitchell | f3649d6 | 2018-08-02 16:16:45 -0700 | [diff] [blame] | 71 | std::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 | |
| 82 | 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 | |
| 108 | collection->InsertFile(full_path); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return collection; |
| 113 | } |
| 114 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 115 | IFile* FileCollection::InsertFile(const StringPiece& path) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 116 | return (files_[path.to_string()] = util::make_unique<RegularFile>(Source(path))).get(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 117 | } |
| 118 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 119 | IFile* FileCollection::FindFile(const StringPiece& path) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 120 | auto iter = files_.find(path.to_string()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 121 | if (iter != files_.end()) { |
| 122 | return iter->second.get(); |
| 123 | } |
| 124 | return nullptr; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 127 | std::unique_ptr<IFileCollectionIterator> FileCollection::Iterator() { |
| 128 | return util::make_unique<FileCollectionIterator>(this); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 129 | } |
| 130 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 131 | } // namespace io |
| 132 | } // namespace aapt |