blob: 565aad6f228422dd5ea77c2982d62f22209b39e2 [file] [log] [blame]
Adam Lesinskia40e9722015-11-24 19:11:46 -08001/*
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 Lesinski5eeaadd2016-08-25 12:26:56 -070020#include <list>
Adam Lesinskia40e9722015-11-24 19:11:46 -080021#include <memory>
22#include <vector>
23
Adam Lesinskice5e56e2016-10-21 17:56:45 -070024#include "android-base/macros.h"
25
26#include "Source.h"
27#include "io/Data.h"
Ryan Mitchell0ce89732018-10-03 09:20:57 -070028#include "util/Files.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070029#include "util/Util.h"
30
Adam Lesinskia40e9722015-11-24 19:11:46 -080031namespace aapt {
32namespace io {
33
Adam Lesinski06460ef2017-03-14 18:52:13 -070034// Interface for a file, which could be a real file on the file system, or a
35// file inside a ZIP archive.
Adam Lesinskia40e9722015-11-24 19:11:46 -080036class IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070037 public:
38 virtual ~IFile() = default;
Adam Lesinskia40e9722015-11-24 19:11:46 -080039
Adam Lesinski06460ef2017-03-14 18:52:13 -070040 // 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 Lesinskice5e56e2016-10-21 17:56:45 -070045 virtual std::unique_ptr<IData> OpenAsData() = 0;
Adam Lesinskia40e9722015-11-24 19:11:46 -080046
Adam Lesinski00451162017-10-03 07:44:08 -070047 virtual std::unique_ptr<io::InputStream> OpenInputStream() = 0;
48
Adam Lesinski06460ef2017-03-14 18:52:13 -070049 // 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 Lesinskice5e56e2016-10-21 17:56:45 -070052 virtual const Source& GetSource() const = 0;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070053
Adam Lesinskice5e56e2016-10-21 17:56:45 -070054 IFile* CreateFileSegment(size_t offset, size_t len);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070055
Adam Lesinski06460ef2017-03-14 18:52:13 -070056 // Returns whether the file was compressed before it was stored in memory.
Pierre Lecesne970732d2017-02-02 23:13:58 +000057 virtual bool WasCompressed() {
58 return false;
59 }
60
Adam Lesinskicacb28f2016-10-19 12:18:14 -070061 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 Lesinskice5e56e2016-10-21 17:56:45 -070067 std::list<std::unique_ptr<IFile>> segments_;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070068};
69
Adam Lesinski06460ef2017-03-14 18:52:13 -070070// An IFile that wraps an underlying IFile but limits it to a subsection of that file.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070071class FileSegment : public IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070072 public:
73 explicit FileSegment(IFile* file, size_t offset, size_t len)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 : file_(file), offset_(offset), len_(len) {}
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070075
Adam Lesinskice5e56e2016-10-21 17:56:45 -070076 std::unique_ptr<IData> OpenAsData() override;
Adam Lesinski00451162017-10-03 07:44:08 -070077 std::unique_ptr<io::InputStream> OpenInputStream() override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070078
Adam Lesinski00451162017-10-03 07:44:08 -070079 const Source& GetSource() const override {
80 return file_->GetSource();
81 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 private:
84 DISALLOW_COPY_AND_ASSIGN(FileSegment);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070085
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 IFile* file_;
87 size_t offset_;
88 size_t len_;
Adam Lesinskia40e9722015-11-24 19:11:46 -080089};
90
Adam Lesinskia6fe3452015-12-09 15:20:52 -080091class IFileCollectionIterator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070092 public:
93 virtual ~IFileCollectionIterator() = default;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080094
Adam Lesinskice5e56e2016-10-21 17:56:45 -070095 virtual bool HasNext() = 0;
96 virtual IFile* Next() = 0;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080097};
98
Adam Lesinski06460ef2017-03-14 18:52:13 -070099// 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 Lesinskia40e9722015-11-24 19:11:46 -0800101class IFileCollection {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 public:
103 virtual ~IFileCollection() = default;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800104
Adam Lesinskid5083f62017-01-16 15:07:21 -0800105 virtual IFile* FindFile(const android::StringPiece& path) = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700106 virtual std::unique_ptr<IFileCollectionIterator> Iterator() = 0;
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700107 virtual char GetDirSeparator() = 0;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800108};
109
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110} // namespace io
111} // namespace aapt
Adam Lesinskia40e9722015-11-24 19:11:46 -0800112
113#endif /* AAPT_IO_FILE_H */