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