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 | |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 19 | #include "androidfw/StringPiece.h" |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 20 | #include "utils/FileMap.h" |
| 21 | |
| 22 | #include "Source.h" |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 23 | #include "io/FileStream.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 24 | #include "util/Files.h" |
| 25 | #include "util/Maybe.h" |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 26 | #include "util/Util.h" |
| 27 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 28 | using ::android::StringPiece; |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 29 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 30 | namespace aapt { |
| 31 | namespace io { |
| 32 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 33 | RegularFile::RegularFile(const Source& source) : source_(source) {} |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 34 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 35 | std::unique_ptr<IData> RegularFile::OpenAsData() { |
| 36 | android::FileMap map; |
| 37 | if (Maybe<android::FileMap> map = file::MmapPath(source_.path, nullptr)) { |
| 38 | if (map.value().getDataPtr() && map.value().getDataLength() > 0) { |
| 39 | return util::make_unique<MmappedData>(std::move(map.value())); |
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 | return util::make_unique<EmptyData>(); |
| 42 | } |
| 43 | return {}; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 44 | } |
| 45 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 46 | std::unique_ptr<io::InputStream> RegularFile::OpenInputStream() { |
| 47 | return util::make_unique<FileInputStream>(source_.path); |
| 48 | } |
| 49 | |
| 50 | const Source& RegularFile::GetSource() const { |
| 51 | return source_; |
| 52 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | |
| 54 | FileCollectionIterator::FileCollectionIterator(FileCollection* collection) |
| 55 | : current_(collection->files_.begin()), end_(collection->files_.end()) {} |
| 56 | |
Adam Lesinski | 0045116 | 2017-10-03 07:44:08 -0700 | [diff] [blame] | 57 | bool FileCollectionIterator::HasNext() { |
| 58 | return current_ != end_; |
| 59 | } |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 60 | |
| 61 | IFile* FileCollectionIterator::Next() { |
| 62 | IFile* result = current_->second.get(); |
| 63 | ++current_; |
| 64 | return result; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 65 | } |
| 66 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 67 | IFile* FileCollection::InsertFile(const StringPiece& path) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 68 | return (files_[path.to_string()] = util::make_unique<RegularFile>(Source(path))).get(); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 71 | IFile* FileCollection::FindFile(const StringPiece& path) { |
Adam Lesinski | d5083f6 | 2017-01-16 15:07:21 -0800 | [diff] [blame] | 72 | auto iter = files_.find(path.to_string()); |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 73 | if (iter != files_.end()) { |
| 74 | return iter->second.get(); |
| 75 | } |
| 76 | return nullptr; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 77 | } |
| 78 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 79 | std::unique_ptr<IFileCollectionIterator> FileCollection::Iterator() { |
| 80 | return util::make_unique<FileCollectionIterator>(this); |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 81 | } |
| 82 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 83 | } // namespace io |
| 84 | } // namespace aapt |