File.h revision 004511660671511ae88e0e837a6f92db28eadaef
1/*
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 <list>
21#include <memory>
22#include <vector>
23
24#include "android-base/macros.h"
25
26#include "Source.h"
27#include "io/Data.h"
28#include "util/Util.h"
29
30namespace aapt {
31namespace io {
32
33// Interface for a file, which could be a real file on the file system, or a
34// file inside a ZIP archive.
35class IFile {
36 public:
37  virtual ~IFile() = default;
38
39  // 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.
44  virtual std::unique_ptr<IData> OpenAsData() = 0;
45
46  virtual std::unique_ptr<io::InputStream> OpenInputStream() = 0;
47
48  // 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.
51  virtual const Source& GetSource() const = 0;
52
53  IFile* CreateFileSegment(size_t offset, size_t len);
54
55  // Returns whether the file was compressed before it was stored in memory.
56  virtual bool WasCompressed() {
57    return false;
58  }
59
60 private:
61  // Any segments created from this IFile need to be owned by this IFile, so
62  // keep them
63  // in a list. This will never be read, so we prefer better insertion
64  // performance
65  // than cache locality, hence the list.
66  std::list<std::unique_ptr<IFile>> segments_;
67};
68
69// An IFile that wraps an underlying IFile but limits it to a subsection of that file.
70class FileSegment : public IFile {
71 public:
72  explicit FileSegment(IFile* file, size_t offset, size_t len)
73      : file_(file), offset_(offset), len_(len) {}
74
75  std::unique_ptr<IData> OpenAsData() override;
76  std::unique_ptr<io::InputStream> OpenInputStream() override;
77
78  const Source& GetSource() const override {
79    return file_->GetSource();
80  }
81
82 private:
83  DISALLOW_COPY_AND_ASSIGN(FileSegment);
84
85  IFile* file_;
86  size_t offset_;
87  size_t len_;
88};
89
90class IFileCollectionIterator {
91 public:
92  virtual ~IFileCollectionIterator() = default;
93
94  virtual bool HasNext() = 0;
95  virtual IFile* Next() = 0;
96};
97
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.
100class IFileCollection {
101 public:
102  virtual ~IFileCollection() = default;
103
104  virtual IFile* FindFile(const android::StringPiece& path) = 0;
105  virtual std::unique_ptr<IFileCollectionIterator> Iterator() = 0;
106};
107
108}  // namespace io
109}  // namespace aapt
110
111#endif /* AAPT_IO_FILE_H */
112