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 <list> 21 #include <memory> 22 #include <vector> 23 24 #include "android-base/macros.h" 25 #include "androidfw/Source.h" 26 #include "io/Data.h" 27 #include "util/Files.h" 28 #include "util/Util.h" 29 30 namespace aapt { 31 namespace io { 32 33 // Interface for a file, which could be a real file on the file system, or a 34 // file inside a ZIP archive. 35 class IFile { 36 public: 37 virtual ~IFile() = default; 38 39 // Open the file and return it as a block of contiguous memory. How this 40 // occurs is implementation dependent. For example, if this is a file on the file 41 // system, it may simply mmap the contents. If this file represents a compressed file in a 42 // ZIP archive, it may need to inflate it to memory, incurring a copy. 43 // Returns nullptr on failure. 44 virtual std::unique_ptr<IData> OpenAsData() = 0; 45 46 virtual std::unique_ptr<android::InputStream> OpenInputStream() = 0; 47 48 // Returns the source of this file. This is for presentation to the user and 49 // may not be a valid file system path (for example, it may contain a '@' sign to separate 50 // the files within a ZIP archive from the path to the containing ZIP archive. 51 virtual const android::Source& GetSource() const = 0; 52 53 IFile* CreateFileSegment(size_t offset, size_t len); 54 55 // Returns whether the file was compressed before it was stored in memory. WasCompressed()56 virtual bool WasCompressed() { 57 return false; 58 } 59 60 // Fills in buf with the last modification time of the file. Returns true if successful, 61 // otherwise false (i.e., the operation is not supported or the file system is unable to provide 62 // a last modification time). 63 virtual bool GetModificationTime(struct tm* buf) const = 0; 64 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>> segments_; 72 }; 73 74 // An IFile that wraps an underlying IFile but limits it to a subsection of that file. 75 class FileSegment : public IFile { 76 public: FileSegment(IFile * file,size_t offset,size_t len)77 explicit FileSegment(IFile* file, size_t offset, size_t len) 78 : file_(file), offset_(offset), len_(len) {} 79 80 std::unique_ptr<IData> OpenAsData() override; 81 std::unique_ptr<android::InputStream> OpenInputStream() override; 82 GetSource()83 const android::Source& GetSource() const override { 84 return file_->GetSource(); 85 } 86 GetModificationTime(struct tm * buf)87 bool GetModificationTime(struct tm* buf) const override { 88 return file_->GetModificationTime(buf); 89 }; 90 91 private: 92 DISALLOW_COPY_AND_ASSIGN(FileSegment); 93 94 IFile* file_; 95 size_t offset_; 96 size_t len_; 97 }; 98 99 class IFileCollectionIterator { 100 public: 101 virtual ~IFileCollectionIterator() = default; 102 103 virtual bool HasNext() = 0; 104 virtual IFile* Next() = 0; 105 }; 106 107 // Interface for a collection of files, all of which share a common source. That source may 108 // simply be the filesystem, or a ZIP archive. 109 class IFileCollection { 110 public: 111 virtual ~IFileCollection() = default; 112 113 virtual IFile* FindFile(android::StringPiece path) = 0; 114 virtual std::unique_ptr<IFileCollectionIterator> Iterator() = 0; 115 virtual char GetDirSeparator() = 0; 116 }; 117 118 } // namespace io 119 } // namespace aapt 120 121 #endif /* AAPT_IO_FILE_H */ 122