blob: 673d1b75e66084d8b4fe82560028fdcbd601ed4c [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"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000025#include "androidfw/Source.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070026#include "io/Data.h"
Ryan Mitchell0ce89732018-10-03 09:20:57 -070027#include "util/Files.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "util/Util.h"
29
Adam Lesinskia40e9722015-11-24 19:11:46 -080030namespace aapt {
31namespace io {
32
Adam Lesinski06460ef2017-03-14 18:52:13 -070033// Interface for a file, which could be a real file on the file system, or a
34// file inside a ZIP archive.
Adam Lesinskia40e9722015-11-24 19:11:46 -080035class IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070036 public:
37 virtual ~IFile() = default;
Adam Lesinskia40e9722015-11-24 19:11:46 -080038
Adam Lesinski06460ef2017-03-14 18:52:13 -070039 // 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.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 virtual std::unique_ptr<IData> OpenAsData() = 0;
Adam Lesinskia40e9722015-11-24 19:11:46 -080045
Adam Lesinski00451162017-10-03 07:44:08 -070046 virtual std::unique_ptr<io::InputStream> OpenInputStream() = 0;
47
Adam Lesinski06460ef2017-03-14 18:52:13 -070048 // 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.
Jeremy Meyer56f36e82022-05-20 20:35:42 +000051 virtual const android::Source& GetSource() const = 0;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070052
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 IFile* CreateFileSegment(size_t offset, size_t len);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070054
Adam Lesinski06460ef2017-03-14 18:52:13 -070055 // Returns whether the file was compressed before it was stored in memory.
Pierre Lecesne970732d2017-02-02 23:13:58 +000056 virtual bool WasCompressed() {
57 return false;
58 }
59
Mark Punzalane5671592023-09-02 00:00:30 +000060 // 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
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 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.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070071 std::list<std::unique_ptr<IFile>> segments_;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070072};
73
Adam Lesinski06460ef2017-03-14 18:52:13 -070074// An IFile that wraps an underlying IFile but limits it to a subsection of that file.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070075class FileSegment : public IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 public:
77 explicit FileSegment(IFile* file, size_t offset, size_t len)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070078 : file_(file), offset_(offset), len_(len) {}
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070079
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 std::unique_ptr<IData> OpenAsData() override;
Adam Lesinski00451162017-10-03 07:44:08 -070081 std::unique_ptr<io::InputStream> OpenInputStream() override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070082
Jeremy Meyer56f36e82022-05-20 20:35:42 +000083 const android::Source& GetSource() const override {
Adam Lesinski00451162017-10-03 07:44:08 -070084 return file_->GetSource();
85 }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070086
Mark Punzalane5671592023-09-02 00:00:30 +000087 bool GetModificationTime(struct tm* buf) const override {
88 return file_->GetModificationTime(buf);
89 };
90
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 private:
92 DISALLOW_COPY_AND_ASSIGN(FileSegment);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070093
Adam Lesinskice5e56e2016-10-21 17:56:45 -070094 IFile* file_;
95 size_t offset_;
96 size_t len_;
Adam Lesinskia40e9722015-11-24 19:11:46 -080097};
98
Adam Lesinskia6fe3452015-12-09 15:20:52 -080099class IFileCollectionIterator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 public:
101 virtual ~IFileCollectionIterator() = default;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800102
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700103 virtual bool HasNext() = 0;
104 virtual IFile* Next() = 0;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800105};
106
Adam Lesinski06460ef2017-03-14 18:52:13 -0700107// 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.
Adam Lesinskia40e9722015-11-24 19:11:46 -0800109class IFileCollection {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 public:
111 virtual ~IFileCollection() = default;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800112
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700113 virtual IFile* FindFile(android::StringPiece path) = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700114 virtual std::unique_ptr<IFileCollectionIterator> Iterator() = 0;
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700115 virtual char GetDirSeparator() = 0;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800116};
117
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700118} // namespace io
119} // namespace aapt
Adam Lesinskia40e9722015-11-24 19:11:46 -0800120
121#endif /* AAPT_IO_FILE_H */