blob: cb5bbe96b8b793a1ea29147c60f44db161f799ae [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
Jeremy Meyer56f36e82022-05-20 20:35:42 +000019#include "androidfw/Source.h"
Fabien Sanglard2d34e762019-02-21 15:13:29 -080020#include "trace/TraceBuffer.h"
Ryan Mitchellf3649d62018-08-02 16:16:45 -070021#include "util/Files.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070022#include "util/Util.h"
Jeremy Meyer56f36e82022-05-20 20:35:42 +000023#include "utils/FileMap.h"
24#include "ziparchive/zip_archive.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080025
Adam Lesinski00451162017-10-03 07:44:08 -070026using ::android::StringPiece;
Adam Lesinskid5083f62017-01-16 15:07:21 -080027
Adam Lesinskia6fe3452015-12-09 15:20:52 -080028namespace aapt {
29namespace io {
30
Jeremy Meyer56f36e82022-05-20 20:35:42 +000031ZipFile::ZipFile(ZipArchiveHandle handle, const ZipEntry& entry, const android::Source& source)
32 : zip_handle_(handle), zip_entry_(entry), source_(source) {
33}
Adam Lesinskia6fe3452015-12-09 15:20:52 -080034
Adam Lesinskice5e56e2016-10-21 17:56:45 -070035std::unique_ptr<IData> ZipFile::OpenAsData() {
Ryan Mitchellf6fe9b62018-09-24 12:13:31 -070036 // 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
Adam Lesinskice5e56e2016-10-21 17:56:45 -070041 if (zip_entry_.method == kCompressStored) {
42 int fd = GetFileDescriptor(zip_handle_);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080043
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 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 {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080049 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070050 return util::make_unique<MmappedData>(std::move(file_map));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080051
Adam Lesinskice5e56e2016-10-21 17:56:45 -070052 } 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));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080058 if (result != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070059 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080060 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 return util::make_unique<MallocData>(std::move(data),
62 zip_entry_.uncompressed_length);
63 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -080064}
65
Adam Lesinski00451162017-10-03 07:44:08 -070066std::unique_ptr<io::InputStream> ZipFile::OpenInputStream() {
67 return OpenAsData();
68}
69
Jeremy Meyer56f36e82022-05-20 20:35:42 +000070const android::Source& ZipFile::GetSource() const {
Adam Lesinski00451162017-10-03 07:44:08 -070071 return source_;
72}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070073
Pierre Lecesne970732d2017-02-02 23:13:58 +000074bool ZipFile::WasCompressed() {
75 return zip_entry_.method != kCompressStored;
76}
77
Mark Punzalane5671592023-09-02 00:00:30 +000078bool ZipFile::GetModificationTime(struct tm* buf) const {
79 if (buf == nullptr) {
80 return false;
81 }
82 *buf = zip_entry_.GetModificationTime();
83 return true;
84}
85
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086ZipFileCollectionIterator::ZipFileCollectionIterator(
87 ZipFileCollection* collection)
88 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
89
Adam Lesinski00451162017-10-03 07:44:08 -070090bool ZipFileCollectionIterator::HasNext() {
91 return current_ != end_;
92}
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093
94IFile* ZipFileCollectionIterator::Next() {
Pierre Lecesne880d65b2017-02-02 22:33:17 +000095 IFile* result = current_->get();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070096 ++current_;
97 return result;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080098}
99
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700100ZipFileCollection::ZipFileCollection() : handle_(nullptr) {}
101
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700102std::unique_ptr<ZipFileCollection> ZipFileCollection::Create(StringPiece path,
103 std::string* out_error) {
Fabien Sanglard2d34e762019-02-21 15:13:29 -0800104 TRACE_CALL();
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700105 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;
Elliott Hughesf31f1262019-05-10 17:02:14 -0700124 result = StartIteration(collection->handle_, &cookie);
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700125 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
Elliott Hughes78de4f92019-06-14 15:28:38 -0700133 std::string zip_entry_path;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700134 ZipEntry zip_data;
Elliott Hughes78de4f92019-06-14 15:28:38 -0700135 while ((result = Next(cookie, &zip_data, &zip_entry_path)) == 0) {
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700136 // Do not add folders to the file collection
137 if (util::EndsWith(zip_entry_path, "/")) {
138 continue;
139 }
140
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700141 std::unique_ptr<IFile> file = util::make_unique<ZipFile>(collection->handle_, zip_data,
142 android::Source(zip_entry_path, path));
Pierre Lecesne880d65b2017-02-02 22:33:17 +0000143 collection->files_by_name_[zip_entry_path] = file.get();
144 collection->files_.push_back(std::move(file));
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 }
146
147 if (result != -1) {
148 if (out_error) *out_error = ErrorCodeString(result);
149 return {};
150 }
Ryan Mitchellf3649d62018-08-02 16:16:45 -0700151
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 return collection;
153}
154
Yurii Zubrytskyia5775142022-11-02 17:49:49 -0700155IFile* ZipFileCollection::FindFile(StringPiece path) {
156 auto iter = files_by_name_.find(path);
Pierre Lecesne880d65b2017-02-02 22:33:17 +0000157 if (iter != files_by_name_.end()) {
158 return iter->second;
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 }
160 return nullptr;
161}
162
163std::unique_ptr<IFileCollectionIterator> ZipFileCollection::Iterator() {
164 return util::make_unique<ZipFileCollectionIterator>(this);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800165}
166
Ryan Mitchell0ce89732018-10-03 09:20:57 -0700167char 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
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800174ZipFileCollection::~ZipFileCollection() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700175 if (handle_) {
176 CloseArchive(handle_);
177 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800178}
179
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700180} // namespace io
181} // namespace aapt