blob: 012f44631a9f234c2644a73f232c69e55dd9e838 [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/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070033 * Interface for a file, which could be a real file on the file system, or a
34 * file inside
Adam Lesinskia40e9722015-11-24 19:11:46 -080035 * a ZIP archive.
36 */
37class IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070038 public:
39 virtual ~IFile() = default;
Adam Lesinskia40e9722015-11-24 19:11:46 -080040
Adam Lesinskicacb28f2016-10-19 12:18:14 -070041 /**
42 * Open the file and return it as a block of contiguous memory. How this
43 * occurs is
44 * implementation dependent. For example, if this is a file on the file
45 * system, it may
46 * simply mmap the contents. If this file represents a compressed file in a
47 * ZIP archive,
48 * it may need to inflate it to memory, incurring a copy.
49 *
50 * Returns nullptr on failure.
51 */
52 virtual std::unique_ptr<IData> openAsData() = 0;
Adam Lesinskia40e9722015-11-24 19:11:46 -080053
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 /**
55 * Returns the source of this file. This is for presentation to the user and
56 * may not be a
57 * valid file system path (for example, it may contain a '@' sign to separate
58 * the files within
59 * a ZIP archive from the path to the containing ZIP archive.
60 */
61 virtual const Source& getSource() const = 0;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070062
Adam Lesinskicacb28f2016-10-19 12:18:14 -070063 IFile* createFileSegment(size_t offset, size_t len);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070064
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.
71 std::list<std::unique_ptr<IFile>> mSegments;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070072};
73
74/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 * An IFile that wraps an underlying IFile but limits it to a subsection of that
76 * file.
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070077 */
78class FileSegment : public IFile {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070079 public:
80 explicit FileSegment(IFile* file, size_t offset, size_t len)
81 : mFile(file), mOffset(offset), mLen(len) {}
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070082
Adam Lesinskicacb28f2016-10-19 12:18:14 -070083 std::unique_ptr<IData> openAsData() override;
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070084
Adam Lesinskicacb28f2016-10-19 12:18:14 -070085 const Source& getSource() const override { return mFile->getSource(); }
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070086
Adam Lesinskicacb28f2016-10-19 12:18:14 -070087 private:
88 DISALLOW_COPY_AND_ASSIGN(FileSegment);
Adam Lesinski5eeaadd2016-08-25 12:26:56 -070089
Adam Lesinskicacb28f2016-10-19 12:18:14 -070090 IFile* mFile;
91 size_t mOffset;
92 size_t mLen;
Adam Lesinskia40e9722015-11-24 19:11:46 -080093};
94
Adam Lesinskia6fe3452015-12-09 15:20:52 -080095class IFileCollectionIterator {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070096 public:
97 virtual ~IFileCollectionIterator() = default;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080098
Adam Lesinskicacb28f2016-10-19 12:18:14 -070099 virtual bool hasNext() = 0;
100 virtual IFile* next() = 0;
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800101};
102
Adam Lesinskia40e9722015-11-24 19:11:46 -0800103/**
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700104 * Interface for a collection of files, all of which share a common source. That
105 * source may
Adam Lesinskia40e9722015-11-24 19:11:46 -0800106 * simply be the filesystem, or a ZIP archive.
107 */
108class IFileCollection {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700109 public:
110 virtual ~IFileCollection() = default;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800111
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700112 virtual IFile* findFile(const StringPiece& path) = 0;
113 virtual std::unique_ptr<IFileCollectionIterator> iterator() = 0;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800114};
115
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116} // namespace io
117} // namespace aapt
Adam Lesinskia40e9722015-11-24 19:11:46 -0800118
119#endif /* AAPT_IO_FILE_H */