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 #include "io/ZipArchive.h"
18
19 #include "androidfw/Source.h"
20 #include "trace/TraceBuffer.h"
21 #include "util/Files.h"
22 #include "util/Util.h"
23 #include "utils/FileMap.h"
24 #include "ziparchive/zip_archive.h"
25
26 using ::android::StringPiece;
27
28 namespace aapt {
29 namespace io {
30
ZipFile(ZipArchiveHandle handle,const ZipEntry & entry,const android::Source & source)31 ZipFile::ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const android::Source& source)
32 : zip_handle_(handle), zip_entry_(entry), source_(source) {
33 }
34
OpenAsData()35 std::unique_ptr<IData> ZipFile::OpenAsData() {
36 // The file will fail to be mmaped if it is empty
37 if (zip_entry_.uncompressed_length == 0) {
38 return util::make_unique<EmptyData>();
39 }
40
41 if (zip_entry_.method == kCompressStored) {
42 int fd = GetFileDescriptor(zip_handle_);
43
44 android::FileMap file_map;
45 bool result = file_map.create(nullptr, fd, zip_entry_.offset,
46 zip_entry_.uncompressed_length, true);
47 if (!result) {
48 return {};
49 }
50 return util::make_unique<MmappedData>(std::move(file_map));
51
52 } else {
53 std::unique_ptr<uint8_t[]> data =
54 std::unique_ptr<uint8_t[]>(new uint8_t[zip_entry_.uncompressed_length]);
55 int32_t result =
56 ExtractToMemory(zip_handle_, &zip_entry_, data.get(),
57 static_cast<uint32_t>(zip_entry_.uncompressed_length));
58 if (result != 0) {
59 return {};
60 }
61 return util::make_unique<MallocData>(std::move(data),
62 zip_entry_.uncompressed_length);
63 }
64 }
65
OpenInputStream()66 std::unique_ptr<android::InputStream> ZipFile::OpenInputStream() {
67 return OpenAsData();
68 }
69
GetSource() const70 const android::Source& ZipFile::GetSource() const {
71 return source_;
72 }
73
WasCompressed()74 bool ZipFile::WasCompressed() {
75 return zip_entry_.method != kCompressStored;
76 }
77
GetModificationTime(struct tm * buf) const78 bool ZipFile::GetModificationTime(struct tm* buf) const {
79 if (buf == nullptr) {
80 return false;
81 }
82 *buf = zip_entry_.GetModificationTime();
83 return true;
84 }
85
ZipFileCollectionIterator(ZipFileCollection * collection)86 ZipFileCollectionIterator::ZipFileCollectionIterator(
87 ZipFileCollection* collection)
88 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
89
HasNext()90 bool ZipFileCollectionIterator::HasNext() {
91 return current_ != end_;
92 }
93
Next()94 IFile* ZipFileCollectionIterator::Next() {
95 IFile* result = current_->get();
96 ++current_;
97 return result;
98 }
99
ZipFileCollection()100 ZipFileCollection::ZipFileCollection() : handle_(nullptr) {}
101
Create(StringPiece path,std::string * out_error)102 std::unique_ptr<ZipFileCollection> ZipFileCollection::Create(StringPiece path,
103 std::string* out_error) {
104 TRACE_CALL();
105 constexpr static const int32_t kEmptyArchive = -6;
106
107 std::unique_ptr<ZipFileCollection> collection =
108 std::unique_ptr<ZipFileCollection>(new ZipFileCollection());
109
110 int32_t result = OpenArchive(path.data(), &collection->handle_);
111 if (result != 0) {
112 // If a zip is empty, result will be an error code. This is fine and we
113 // should
114 // return an empty ZipFileCollection.
115 if (result == kEmptyArchive) {
116 return collection;
117 }
118
119 if (out_error) *out_error = ErrorCodeString(result);
120 return {};
121 }
122
123 void* cookie = nullptr;
124 result = StartIteration(collection->handle_, &cookie);
125 if (result != 0) {
126 if (out_error) *out_error = ErrorCodeString(result);
127 return {};
128 }
129
130 using IterationEnder = std::unique_ptr<void, decltype(EndIteration)*>;
131 IterationEnder iteration_ender(cookie, EndIteration);
132
133 std::string zip_entry_path;
134 ZipEntry zip_data;
135 while ((result = Next(cookie, &zip_data, &zip_entry_path)) == 0) {
136 // Do not add folders to the file collection
137 if (util::EndsWith(zip_entry_path, "/")) {
138 continue;
139 }
140
141 std::unique_ptr<IFile> file = util::make_unique<ZipFile>(collection->handle_, zip_data,
142 android::Source(zip_entry_path, path));
143 collection->files_by_name_[zip_entry_path] = file.get();
144 collection->files_.push_back(std::move(file));
145 }
146
147 if (result != -1) {
148 if (out_error) *out_error = ErrorCodeString(result);
149 return {};
150 }
151
152 return collection;
153 }
154
FindFile(StringPiece path)155 IFile* ZipFileCollection::FindFile(StringPiece path) {
156 auto iter = files_by_name_.find(path);
157 if (iter != files_by_name_.end()) {
158 return iter->second;
159 }
160 return nullptr;
161 }
162
Iterator()163 std::unique_ptr<IFileCollectionIterator> ZipFileCollection::Iterator() {
164 return util::make_unique<ZipFileCollectionIterator>(this);
165 }
166
GetDirSeparator()167 char ZipFileCollection::GetDirSeparator() {
168 // According to the zip file specification, section 4.4.17.1:
169 // "All slashes MUST be forward slashes '/' as opposed to backwards slashes '\' for compatibility
170 // with Amiga and UNIX file systems etc."
171 return '/';
172 }
173
~ZipFileCollection()174 ZipFileCollection::~ZipFileCollection() {
175 if (handle_) {
176 CloseArchive(handle_);
177 }
178 }
179
180 } // namespace io
181 } // namespace aapt
182