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