blob: 807981edf66393d2fd4cd328e33e3607ede4f2f8 [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
20#include "Source.h"
21#include "io/Data.h"
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070022#include "util/Util.h"
Adam Lesinskia40e9722015-11-24 19:11:46 -080023
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070024#include <android-base/macros.h>
25#include <list>
Adam Lesinskia40e9722015-11-24 19:11:46 -080026#include <memory>
27#include <vector>
28
29namespace aapt {
30namespace io {
31
32/**
33 * Interface for a file, which could be a real file on the file system, or a file inside
34 * a ZIP archive.
35 */
36class IFile {
37public:
38 virtual ~IFile() = default;
39
40 /**
41 * Open the file and return it as a block of contiguous memory. How this occurs is
42 * implementation dependent. For example, if this is a file on the file system, it may
43 * simply mmap the contents. If this file represents a compressed file in a ZIP archive,
44 * it may need to inflate it to memory, incurring a copy.
45 *
46 * Returns nullptr on failure.
47 */
48 virtual std::unique_ptr<IData> openAsData() = 0;
49
50 /**
51 * Returns the source of this file. This is for presentation to the user and may not be a
52 * valid file system path (for example, it may contain a '@' sign to separate the files within
53 * a ZIP archive from the path to the containing ZIP archive.
54 */
55 virtual const Source& getSource() const = 0;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070056
57 IFile* createFileSegment(size_t offset, size_t len);
58
59private:
60 // Any segments created from this IFile need to be owned by this IFile, so keep them
61 // in a list. This will never be read, so we prefer better insertion performance
62 // than cache locality, hence the list.
63 std::list<std::unique_ptr<IFile>> mSegments;
64};
65
66/**
67 * An IFile that wraps an underlying IFile but limits it to a subsection of that file.
68 */
69class FileSegment : public IFile {
70public:
71 explicit FileSegment(IFile* file, size_t offset, size_t len) :
72 mFile(file), mOffset(offset), mLen(len) {
73 }
74
75 std::unique_ptr<IData> openAsData() override;
76
77 const Source& getSource() const override {
78 return mFile->getSource();
79 }
80
81private:
82 DISALLOW_COPY_AND_ASSIGN(FileSegment);
83
84 IFile* mFile;
85 size_t mOffset;
86 size_t mLen;
Adam Lesinskia40e9722015-11-24 19:11:46 -080087};
88
Adam Lesinskia6fe3452015-12-09 15:20:52 -080089class IFileCollectionIterator {
90public:
91 virtual ~IFileCollectionIterator() = default;
92
93 virtual bool hasNext() = 0;
94 virtual IFile* next() = 0;
95};
96
Adam Lesinskia40e9722015-11-24 19:11:46 -080097/**
98 * Interface for a collection of files, all of which share a common source. That source may
99 * simply be the filesystem, or a ZIP archive.
100 */
101class IFileCollection {
102public:
103 virtual ~IFileCollection() = default;
104
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800105 virtual IFile* findFile(const StringPiece& path) = 0;
106 virtual std::unique_ptr<IFileCollectionIterator> iterator() = 0;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800107};
108
109} // namespace io
110} // namespace aapt
111
112#endif /* AAPT_IO_FILE_H */