blob: 1ef9743f65cf097fd1d2c6dfe342df968695e806 [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"
28#include "util/Util.h"
29
Adam Lesinskia40e9722015-11-24 19:11:46 -080030namespace aapt {
31namespace io {
32
33/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070034 * Interface for a file, which could be a real file on the file system, or a
35 * file inside
Adam Lesinskia40e9722015-11-24 19:11:46 -080036 * a ZIP archive.
37 */
38class IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070039 public:
40 virtual ~IFile() = default;
Adam Lesinskia40e9722015-11-24 19:11:46 -080041
Adam Lesinskicacb28f2016-10-19 12:18:14 -070042 /**
43 * Open the file and return it as a block of contiguous memory. How this
44 * occurs is
45 * implementation dependent. For example, if this is a file on the file
46 * system, it may
47 * simply mmap the contents. If this file represents a compressed file in a
48 * ZIP archive,
49 * it may need to inflate it to memory, incurring a copy.
50 *
51 * Returns nullptr on failure.
52 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 virtual std::unique_ptr<IData> OpenAsData() = 0;
Adam Lesinskia40e9722015-11-24 19:11:46 -080054
Adam Lesinskicacb28f2016-10-19 12:18:14 -070055 /**
56 * Returns the source of this file. This is for presentation to the user and
57 * may not be a
58 * valid file system path (for example, it may contain a '@' sign to separate
59 * the files within
60 * a ZIP archive from the path to the containing ZIP archive.
61 */
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 virtual const Source& GetSource() const = 0;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070063
Adam Lesinskice5e56e2016-10-21 17:56:45 -070064 IFile* CreateFileSegment(size_t offset, size_t len);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070065
Pierre Lecesne970732d2017-02-02 23:13:58 +000066 /** Returns whether the file was compressed before it was stored in memory. */
67 virtual bool WasCompressed() {
68 return false;
69 }
70
Adam Lesinskicacb28f2016-10-19 12:18:14 -070071 private:
72 // Any segments created from this IFile need to be owned by this IFile, so
73 // keep them
74 // in a list. This will never be read, so we prefer better insertion
75 // performance
76 // than cache locality, hence the list.
Adam Lesinskice5e56e2016-10-21 17:56:45 -070077 std::list<std::unique_ptr<IFile>> segments_;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070078};
79
80/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 * An IFile that wraps an underlying IFile but limits it to a subsection of that
82 * file.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070083 */
84class FileSegment : public IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 public:
86 explicit FileSegment(IFile* file, size_t offset, size_t len)
Adam Lesinskice5e56e2016-10-21 17:56:45 -070087 : file_(file), offset_(offset), len_(len) {}
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070088
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 std::unique_ptr<IData> OpenAsData() override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070090
Adam Lesinskice5e56e2016-10-21 17:56:45 -070091 const Source& GetSource() const override { return file_->GetSource(); }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070092
Adam Lesinskicacb28f2016-10-19 12:18:14 -070093 private:
94 DISALLOW_COPY_AND_ASSIGN(FileSegment);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070095
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 IFile* file_;
97 size_t offset_;
98 size_t len_;
Adam Lesinskia40e9722015-11-24 19:11:46 -080099};
100
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800101class IFileCollectionIterator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700102 public:
103 virtual ~IFileCollectionIterator() = default;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800104
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 virtual bool HasNext() = 0;
106 virtual IFile* Next() = 0;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800107};
108
Adam Lesinskia40e9722015-11-24 19:11:46 -0800109/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700110 * Interface for a collection of files, all of which share a common source. That
111 * source may
Adam Lesinskia40e9722015-11-24 19:11:46 -0800112 * simply be the filesystem, or a ZIP archive.
113 */
114class IFileCollection {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700115 public:
116 virtual ~IFileCollection() = default;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800117
Adam Lesinskid5083f62017-01-16 15:07:21 -0800118 virtual IFile* FindFile(const android::StringPiece& path) = 0;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700119 virtual std::unique_ptr<IFileCollectionIterator> Iterator() = 0;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800120};
121
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700122} // namespace io
123} // namespace aapt
Adam Lesinskia40e9722015-11-24 19:11:46 -0800124
125#endif /* AAPT_IO_FILE_H */