blob: 4380586b1d3cf4e2f44b8a8aee97db80a248683b [file] [log] [blame]
Adam Lesinskia6fe3452015-12-09 15:20:52 -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
Adam Lesinskia6fe3452015-12-09 15:20:52 -080017#include "io/ZipArchive.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080018
Adam Lesinskice5e56e2016-10-21 17:56:45 -070019#include "utils/FileMap.h"
20#include "ziparchive/zip_archive.h"
21
22#include "Source.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080023#include "trace/TraceBuffer.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070024#include "util/Files.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025#include "util/Util.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080026
Adam Lesinski00451162017-10-03 07:44:08 -070027using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080028
Adam Lesinskia6fe3452015-12-09 15:20:52 -080029namespace aapt {
30namespace io {
31
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032ZipFile::ZipFile(ZipArchiveHandle handle, const ZipEntry& entry,
33 const Source& source)
34 : zip_handle_(handle), zip_entry_(entry), source_(source) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -080035
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036std::unique_ptr<IData> ZipFile::OpenAsData() {
Ryan Mitchellf6fe9b62018-09-24 12:13:31 -070037 // The file will fail to be mmaped if it is empty
38 if (zip_entry_.uncompressed_length == 0) {
39 return util::make_unique<EmptyData>();
40 }
41
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 if (zip_entry_.method == kCompressStored) {
43 int fd = GetFileDescriptor(zip_handle_);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080044
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 android::FileMap file_map;
46 bool result = file_map.create(nullptr, fd, zip_entry_.offset,
47 zip_entry_.uncompressed_length, true);
48 if (!result) {
49 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080050 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 return util::make_unique<MmappedData>(std::move(file_map));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080052
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 } else {
54 std::unique_ptr<uint8_t[]> data =
55 std::unique_ptr<uint8_t[]>(new uint8_t[zip_entry_.uncompressed_length]);
56 int32_t result =
57 ExtractToMemory(zip_handle_, &zip_entry_, data.get(),
58 static_cast<uint32_t>(zip_entry_.uncompressed_length));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080059 if (result != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070060 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080061 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070062 return util::make_unique<MallocData>(std::move(data),
63 zip_entry_.uncompressed_length);
64 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -080065}
66
Adam Lesinski00451162017-10-03 07:44:08 -070067std::unique_ptr<io::InputStream> ZipFile::OpenInputStream() {
68 return OpenAsData();
69}
70
71const Source& ZipFile::GetSource() const {
72 return source_;
73}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074
Pierre Lecesne970732d2017-02-02 23:13:58 +000075bool ZipFile::WasCompressed() {
76 return zip_entry_.method != kCompressStored;
77}
78
Adam Lesinskice5e56e2016-10-21 17:56:45 -070079ZipFileCollectionIterator::ZipFileCollectionIterator(
80 ZipFileCollection* collection)
81 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
82
Adam Lesinski00451162017-10-03 07:44:08 -070083bool ZipFileCollectionIterator::HasNext() {
84 return current_ != end_;
85}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086
87IFile* ZipFileCollectionIterator::Next() {
Pierre Lecesne880d65b2017-02-02 22:33:17 +000088 IFile* result = current_->get();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070089 ++current_;
90 return result;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080091}
92
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093ZipFileCollection::ZipFileCollection() : handle_(nullptr) {}
94
95std::unique_ptr<ZipFileCollection> ZipFileCollection::Create(
96 const StringPiece& path, std::string* out_error) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -080097 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070098 constexpr static const int32_t kEmptyArchive = -6;
99
100 std::unique_ptr<ZipFileCollection> collection =
101 std::unique_ptr<ZipFileCollection>(new ZipFileCollection());
102
103 int32_t result = OpenArchive(path.data(), &collection->handle_);
104 if (result != 0) {
105 // If a zip is empty, result will be an error code. This is fine and we
106 // should
107 // return an empty ZipFileCollection.
108 if (result == kEmptyArchive) {
109 return collection;
110 }
111
112 if (out_error) *out_error = ErrorCodeString(result);
113 return {};
114 }
115
116 void* cookie = nullptr;
Elliott Hughesf31f1262019-05-10 17:02:14 -0700117 result = StartIteration(collection->handle_, &cookie);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700118 if (result != 0) {
119 if (out_error) *out_error = ErrorCodeString(result);
120 return {};
121 }
122
123 using IterationEnder = std::unique_ptr<void, decltype(EndIteration)*>;
124 IterationEnder iteration_ender(cookie, EndIteration);
125
Elliott Hughes78de4f92019-06-14 15:28:38 -0700126 std::string zip_entry_path;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 ZipEntry zip_data;
Elliott Hughes78de4f92019-06-14 15:28:38 -0700128 while ((result = Next(cookie, &zip_data, &zip_entry_path)) == 0) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700129 // Do not add folders to the file collection
130 if (util::EndsWith(zip_entry_path, "/")) {
131 continue;
132 }
133
134 std::unique_ptr<IFile> file = util::make_unique<ZipFile>(collection->handle_, zip_data,
135 Source(zip_entry_path, path.to_string()));
Pierre Lecesne880d65b2017-02-02 22:33:17 +0000136 collection->files_by_name_[zip_entry_path] = file.get();
137 collection->files_.push_back(std::move(file));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700138 }
139
140 if (result != -1) {
141 if (out_error) *out_error = ErrorCodeString(result);
142 return {};
143 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 return collection;
146}
147
148IFile* ZipFileCollection::FindFile(const StringPiece& path) {
Pierre Lecesne880d65b2017-02-02 22:33:17 +0000149 auto iter = files_by_name_.find(path.to_string());
150 if (iter != files_by_name_.end()) {
151 return iter->second;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 }
153 return nullptr;
154}
155
156std::unique_ptr<IFileCollectionIterator> ZipFileCollection::Iterator() {
157 return util::make_unique<ZipFileCollectionIterator>(this);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800158}
159
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700160char ZipFileCollection::GetDirSeparator() {
161 // According to the zip file specification, section 4.4.17.1:
162 // "All slashes MUST be forward slashes '/' as opposed to backwards slashes '\' for compatibility
163 // with Amiga and UNIX file systems etc."
164 return '/';
165}
166
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800167ZipFileCollection::~ZipFileCollection() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 if (handle_) {
169 CloseArchive(handle_);
170 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800171}
172
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700173} // namespace io
174} // namespace aapt