Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -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 | |
| 17 | #ifndef AAPT_IO_FILE_H |
| 18 | #define AAPT_IO_FILE_H |
| 19 | |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 20 | #include <list> |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 21 | #include <memory> |
| 22 | #include <vector> |
| 23 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 24 | #include "android-base/macros.h" |
| 25 | |
| 26 | #include "Source.h" |
| 27 | #include "io/Data.h" |
| 28 | #include "util/Util.h" |
| 29 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 30 | namespace aapt { |
| 31 | namespace io { |
| 32 | |
| 33 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 34 | * Interface for a file, which could be a real file on the file system, or a |
| 35 | * file inside |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 36 | * a ZIP archive. |
| 37 | */ |
| 38 | class IFile { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 39 | public: |
| 40 | virtual ~IFile() = default; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 41 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 42 | /** |
| 43 | * Open the file and return it as a block of contiguous memory. How this |
| 44 | * occurs is |
| 45 | * implementation dependent. For example, if this is a file on the file |
| 46 | * system, it may |
| 47 | * simply mmap the contents. If this file represents a compressed file in a |
| 48 | * ZIP archive, |
| 49 | * it may need to inflate it to memory, incurring a copy. |
| 50 | * |
| 51 | * Returns nullptr on failure. |
| 52 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 53 | virtual std::unique_ptr<IData> OpenAsData() = 0; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 54 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 55 | /** |
| 56 | * Returns the source of this file. This is for presentation to the user and |
| 57 | * may not be a |
| 58 | * valid file system path (for example, it may contain a '@' sign to separate |
| 59 | * the files within |
| 60 | * a ZIP archive from the path to the containing ZIP archive. |
| 61 | */ |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 62 | virtual const Source& GetSource() const = 0; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 63 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 64 | IFile* CreateFileSegment(size_t offset, size_t len); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 65 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 66 | private: |
| 67 | // Any segments created from this IFile need to be owned by this IFile, so |
| 68 | // keep them |
| 69 | // in a list. This will never be read, so we prefer better insertion |
| 70 | // performance |
| 71 | // than cache locality, hence the list. |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 72 | std::list<std::unique_ptr<IFile>> segments_; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 76 | * An IFile that wraps an underlying IFile but limits it to a subsection of that |
| 77 | * file. |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 78 | */ |
| 79 | class FileSegment : public IFile { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 80 | public: |
| 81 | explicit FileSegment(IFile* file, size_t offset, size_t len) |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 82 | : file_(file), offset_(offset), len_(len) {} |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 83 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 84 | std::unique_ptr<IData> OpenAsData() override; |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 85 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 86 | const Source& GetSource() const override { return file_->GetSource(); } |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 87 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 88 | private: |
| 89 | DISALLOW_COPY_AND_ASSIGN(FileSegment); |
Adam Lesinski | 5eeaadd | 2016-08-25 12:26:56 -0700 | [diff] [blame] | 90 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 91 | IFile* file_; |
| 92 | size_t offset_; |
| 93 | size_t len_; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 94 | }; |
| 95 | |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 96 | class IFileCollectionIterator { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 97 | public: |
| 98 | virtual ~IFileCollectionIterator() = default; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 99 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 100 | virtual bool HasNext() = 0; |
| 101 | virtual IFile* Next() = 0; |
Adam Lesinski | a6fe345 | 2015-12-09 15:20:52 -0800 | [diff] [blame] | 102 | }; |
| 103 | |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 104 | /** |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 105 | * Interface for a collection of files, all of which share a common source. That |
| 106 | * source may |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 107 | * simply be the filesystem, or a ZIP archive. |
| 108 | */ |
| 109 | class IFileCollection { |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 110 | public: |
| 111 | virtual ~IFileCollection() = default; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 112 | |
Adam Lesinski | ce5e56e | 2016-10-21 17:56:45 -0700 | [diff] [blame] | 113 | virtual IFile* FindFile(const StringPiece& path) = 0; |
| 114 | virtual std::unique_ptr<IFileCollectionIterator> Iterator() = 0; |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
Adam Lesinski | cacb28f | 2016-10-19 12:18:14 -0700 | [diff] [blame] | 117 | } // namespace io |
| 118 | } // namespace aapt |
Adam Lesinski | a40e972 | 2015-11-24 19:11:46 -0800 | [diff] [blame] | 119 | |
| 120 | #endif /* AAPT_IO_FILE_H */ |