blob: f4a128eca9d1e5e4491cc3bda61e40163d88e07c [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"
23#include "util/Util.h"
Adam Lesinskia6fe3452015-12-09 15:20:52 -080024
25namespace aapt {
26namespace io {
27
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028ZipFile::ZipFile(ZipArchiveHandle handle, const ZipEntry& entry,
29 const Source& source)
30 : zip_handle_(handle), zip_entry_(entry), source_(source) {}
Adam Lesinskia6fe3452015-12-09 15:20:52 -080031
Adam Lesinskice5e56e2016-10-21 17:56:45 -070032std::unique_ptr<IData> ZipFile::OpenAsData() {
33 if (zip_entry_.method == kCompressStored) {
34 int fd = GetFileDescriptor(zip_handle_);
Adam Lesinskia6fe3452015-12-09 15:20:52 -080035
Adam Lesinskice5e56e2016-10-21 17:56:45 -070036 android::FileMap file_map;
37 bool result = file_map.create(nullptr, fd, zip_entry_.offset,
38 zip_entry_.uncompressed_length, true);
39 if (!result) {
40 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080041 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070042 return util::make_unique<MmappedData>(std::move(file_map));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080043
Adam Lesinskice5e56e2016-10-21 17:56:45 -070044 } else {
45 std::unique_ptr<uint8_t[]> data =
46 std::unique_ptr<uint8_t[]>(new uint8_t[zip_entry_.uncompressed_length]);
47 int32_t result =
48 ExtractToMemory(zip_handle_, &zip_entry_, data.get(),
49 static_cast<uint32_t>(zip_entry_.uncompressed_length));
Adam Lesinskia6fe3452015-12-09 15:20:52 -080050 if (result != 0) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070051 return {};
Adam Lesinskia6fe3452015-12-09 15:20:52 -080052 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070053 return util::make_unique<MallocData>(std::move(data),
54 zip_entry_.uncompressed_length);
55 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -080056}
57
Adam Lesinskice5e56e2016-10-21 17:56:45 -070058const Source& ZipFile::GetSource() const { return source_; }
59
60ZipFileCollectionIterator::ZipFileCollectionIterator(
61 ZipFileCollection* collection)
62 : current_(collection->files_.begin()), end_(collection->files_.end()) {}
63
64bool ZipFileCollectionIterator::HasNext() { return current_ != end_; }
65
66IFile* ZipFileCollectionIterator::Next() {
67 IFile* result = current_->second.get();
68 ++current_;
69 return result;
Adam Lesinskia6fe3452015-12-09 15:20:52 -080070}
71
Adam Lesinskice5e56e2016-10-21 17:56:45 -070072ZipFileCollection::ZipFileCollection() : handle_(nullptr) {}
73
74std::unique_ptr<ZipFileCollection> ZipFileCollection::Create(
75 const StringPiece& path, std::string* out_error) {
76 constexpr static const int32_t kEmptyArchive = -6;
77
78 std::unique_ptr<ZipFileCollection> collection =
79 std::unique_ptr<ZipFileCollection>(new ZipFileCollection());
80
81 int32_t result = OpenArchive(path.data(), &collection->handle_);
82 if (result != 0) {
83 // If a zip is empty, result will be an error code. This is fine and we
84 // should
85 // return an empty ZipFileCollection.
86 if (result == kEmptyArchive) {
87 return collection;
88 }
89
90 if (out_error) *out_error = ErrorCodeString(result);
91 return {};
92 }
93
94 void* cookie = nullptr;
95 result = StartIteration(collection->handle_, &cookie, nullptr, nullptr);
96 if (result != 0) {
97 if (out_error) *out_error = ErrorCodeString(result);
98 return {};
99 }
100
101 using IterationEnder = std::unique_ptr<void, decltype(EndIteration)*>;
102 IterationEnder iteration_ender(cookie, EndIteration);
103
104 ZipString zip_entry_name;
105 ZipEntry zip_data;
106 while ((result = Next(cookie, &zip_data, &zip_entry_name)) == 0) {
107 std::string zip_entry_path =
108 std::string(reinterpret_cast<const char*>(zip_entry_name.name),
109 zip_entry_name.name_length);
110 std::string nested_path = path.ToString() + "@" + zip_entry_path;
111 collection->files_[zip_entry_path] = util::make_unique<ZipFile>(
112 collection->handle_, zip_data, Source(nested_path));
113 }
114
115 if (result != -1) {
116 if (out_error) *out_error = ErrorCodeString(result);
117 return {};
118 }
119 return collection;
120}
121
122IFile* ZipFileCollection::FindFile(const StringPiece& path) {
123 auto iter = files_.find(path.ToString());
124 if (iter != files_.end()) {
125 return iter->second.get();
126 }
127 return nullptr;
128}
129
130std::unique_ptr<IFileCollectionIterator> ZipFileCollection::Iterator() {
131 return util::make_unique<ZipFileCollectionIterator>(this);
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800132}
133
134ZipFileCollection::~ZipFileCollection() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 if (handle_) {
136 CloseArchive(handle_);
137 }
Adam Lesinskia6fe3452015-12-09 15:20:52 -0800138}
139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140} // namespace io
141} // namespace aapt