blob: c20b053c37b102c9a90095a94df5770f3bf2236b [file] [log] [blame]
Adam Lesinski1ab598f2015-08-14 14:26:04 -07001/*
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 Lesinski46708052017-09-29 14:49:15 -070017#include "format/Archive.h"
Adam Lesinski1ab598f2015-08-14 14:26:04 -070018
Adam Lesinskia40e9722015-11-24 19:11:46 -080019#include <cstdio>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070020#include <memory>
21#include <string>
22#include <vector>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023
Adam Lesinski06460ef2017-03-14 18:52:13 -070024#include "android-base/errors.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070025#include "android-base/macros.h"
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070026#include "android-base/utf8.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080027#include "androidfw/StringPiece.h"
Adam Lesinskice5e56e2016-10-21 17:56:45 -070028#include "ziparchive/zip_writer.h"
29
30#include "util/Files.h"
Adam Lesinskid5083f62017-01-16 15:07:21 -080031
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070032using ::android::StringPiece;
33using ::android::base::SystemErrorCodeToString;
Adam Lesinskice5e56e2016-10-21 17:56:45 -070034
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035namespace aapt {
36
37namespace {
38
Adam Lesinskice5e56e2016-10-21 17:56:45 -070039class DirectoryWriter : public IArchiveWriter {
40 public:
41 DirectoryWriter() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070042
Adam Lesinski06460ef2017-03-14 18:52:13 -070043 bool Open(const StringPiece& out_dir) {
Adam Lesinskid5083f62017-01-16 15:07:21 -080044 dir_ = out_dir.to_string();
Adam Lesinskice5e56e2016-10-21 17:56:45 -070045 file::FileType type = file::GetFileType(dir_);
Ryan Mitchell4382e442021-07-14 12:53:01 -070046 if (type == file::FileType::kNonExistant) {
Adam Lesinski06460ef2017-03-14 18:52:13 -070047 error_ = "directory does not exist";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070048 return false;
49 } else if (type != file::FileType::kDirectory) {
Adam Lesinski06460ef2017-03-14 18:52:13 -070050 error_ = "not a directory";
Adam Lesinskicacb28f2016-10-19 12:18:14 -070051 return false;
52 }
53 return true;
54 }
55
Adam Lesinskice5e56e2016-10-21 17:56:45 -070056 bool StartEntry(const StringPiece& path, uint32_t flags) override {
57 if (file_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070059 }
60
Adam Lesinskice5e56e2016-10-21 17:56:45 -070061 std::string full_path = dir_;
62 file::AppendPath(&full_path, path);
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070063 file::mkdirs(file::GetStem(full_path).to_string());
Adam Lesinskia40e9722015-11-24 19:11:46 -080064
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070065 file_ = {::android::base::utf8::fopen(full_path.c_str(), "wb"), fclose};
Adam Lesinskice5e56e2016-10-21 17:56:45 -070066 if (!file_) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070067 error_ = SystemErrorCodeToString(errno);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070068 return false;
69 }
70 return true;
71 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070072
Adam Lesinski06460ef2017-03-14 18:52:13 -070073 bool Write(const void* data, int len) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -070074 if (!file_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070075 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070076 }
77
Adam Lesinski06460ef2017-03-14 18:52:13 -070078 if (fwrite(data, 1, len, file_.get()) != static_cast<size_t>(len)) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -070079 error_ = SystemErrorCodeToString(errno);
Adam Lesinskice5e56e2016-10-21 17:56:45 -070080 file_.reset(nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070081 return false;
82 }
83 return true;
84 }
85
Adam Lesinskice5e56e2016-10-21 17:56:45 -070086 bool FinishEntry() override {
87 if (!file_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 return false;
89 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070090 file_.reset(nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 return true;
92 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -070093
Adam Lesinski06460ef2017-03-14 18:52:13 -070094 bool WriteFile(const StringPiece& path, uint32_t flags, io::InputStream* in) override {
95 if (!StartEntry(path, flags)) {
96 return false;
97 }
98
99 const void* data = nullptr;
100 size_t len = 0;
101 while (in->Next(&data, &len)) {
102 if (!Write(data, static_cast<int>(len))) {
103 return false;
104 }
105 }
Winson5e7370d2019-04-30 13:55:27 -0700106
107 if (in->HadError()) {
108 error_ = in->GetError();
109 return false;
110 }
111
112 return FinishEntry();
Adam Lesinski06460ef2017-03-14 18:52:13 -0700113 }
114
Adam Lesinski46708052017-09-29 14:49:15 -0700115 bool HadError() const override {
116 return !error_.empty();
117 }
Adam Lesinski06460ef2017-03-14 18:52:13 -0700118
Adam Lesinski46708052017-09-29 14:49:15 -0700119 std::string GetError() const override {
120 return error_;
121 }
Adam Lesinski06460ef2017-03-14 18:52:13 -0700122
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700123 private:
124 DISALLOW_COPY_AND_ASSIGN(DirectoryWriter);
125
126 std::string dir_;
127 std::unique_ptr<FILE, decltype(fclose)*> file_ = {nullptr, fclose};
Adam Lesinski06460ef2017-03-14 18:52:13 -0700128 std::string error_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700129};
130
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131class ZipFileWriter : public IArchiveWriter {
132 public:
133 ZipFileWriter() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700134
Adam Lesinski06460ef2017-03-14 18:52:13 -0700135 bool Open(const StringPiece& path) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700136 file_ = {::android::base::utf8::fopen(path.to_string().c_str(), "w+b"), fclose};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700137 if (!file_) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700138 error_ = SystemErrorCodeToString(errno);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700139 return false;
140 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700141 writer_ = util::make_unique<ZipWriter>(file_.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700142 return true;
143 }
144
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700145 bool StartEntry(const StringPiece& path, uint32_t flags) override {
146 if (!writer_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148 }
149
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700150 size_t zip_flags = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700151 if (flags & ArchiveEntry::kCompress) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700152 zip_flags |= ZipWriter::kCompress;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800153 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700154
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700155 if (flags & ArchiveEntry::kAlign) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700156 zip_flags |= ZipWriter::kAlign32;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800157 }
158
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700159 int32_t result = writer_->StartEntry(path.data(), zip_flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700160 if (result != 0) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700161 error_ = ZipWriter::ErrorCodeString(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700162 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700164 return true;
165 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Adam Lesinski06460ef2017-03-14 18:52:13 -0700167 bool Write(const void* data, int len) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700168 int32_t result = writer_->WriteBytes(data, len);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700169 if (result != 0) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700170 error_ = ZipWriter::ErrorCodeString(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700171 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700172 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700173 return true;
174 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700176 bool FinishEntry() override {
177 int32_t result = writer_->FinishEntry();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700178 if (result != 0) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700179 error_ = ZipWriter::ErrorCodeString(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700180 return false;
181 }
182 return true;
183 }
184
Adam Lesinski06460ef2017-03-14 18:52:13 -0700185 bool WriteFile(const StringPiece& path, uint32_t flags, io::InputStream* in) override {
186 while (true) {
187 if (!StartEntry(path, flags)) {
188 return false;
189 }
190
191 const void* data = nullptr;
192 size_t len = 0;
193 while (in->Next(&data, &len)) {
194 if (!Write(data, static_cast<int>(len))) {
195 return false;
196 }
197 }
198
199 if (in->HadError()) {
Winson5e7370d2019-04-30 13:55:27 -0700200 error_ = in->GetError();
Adam Lesinski06460ef2017-03-14 18:52:13 -0700201 return false;
202 }
203
204 if (!FinishEntry()) {
205 return false;
206 }
207
208 // Check to see if the file was compressed enough. This is preserving behavior of AAPT.
209 if ((flags & ArchiveEntry::kCompress) != 0 && in->CanRewind()) {
210 ZipWriter::FileEntry last_entry;
211 int32_t result = writer_->GetLastEntry(&last_entry);
212 CHECK(result == 0);
213 if (last_entry.compressed_size + (last_entry.compressed_size / 10) >
214 last_entry.uncompressed_size) {
215 // The file was not compressed enough, rewind and store it uncompressed.
216 if (!in->Rewind()) {
217 // Well we tried, may as well keep what we had.
218 return true;
219 }
220
221 int32_t result = writer_->DiscardLastEntry();
222 if (result != 0) {
223 error_ = ZipWriter::ErrorCodeString(result);
224 return false;
225 }
226 flags &= ~ArchiveEntry::kCompress;
227
228 continue;
229 }
230 }
231 return true;
232 }
233 }
234
Adam Lesinski46708052017-09-29 14:49:15 -0700235 bool HadError() const override {
236 return !error_.empty();
237 }
Adam Lesinski06460ef2017-03-14 18:52:13 -0700238
Adam Lesinski46708052017-09-29 14:49:15 -0700239 std::string GetError() const override {
240 return error_;
241 }
Adam Lesinski06460ef2017-03-14 18:52:13 -0700242
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700243 virtual ~ZipFileWriter() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700244 if (writer_) {
245 writer_->Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700246 }
247 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700248
249 private:
250 DISALLOW_COPY_AND_ASSIGN(ZipFileWriter);
251
252 std::unique_ptr<FILE, decltype(fclose)*> file_ = {nullptr, fclose};
253 std::unique_ptr<ZipWriter> writer_;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700254 std::string error_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700255};
256
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700258
Adam Lesinski06460ef2017-03-14 18:52:13 -0700259std::unique_ptr<IArchiveWriter> CreateDirectoryArchiveWriter(IDiagnostics* diag,
260 const StringPiece& path) {
261 std::unique_ptr<DirectoryWriter> writer = util::make_unique<DirectoryWriter>();
262 if (!writer->Open(path)) {
263 diag->Error(DiagMessage(path) << writer->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 return {};
265 }
266 return std::move(writer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700267}
268
Adam Lesinski06460ef2017-03-14 18:52:13 -0700269std::unique_ptr<IArchiveWriter> CreateZipFileArchiveWriter(IDiagnostics* diag,
270 const StringPiece& path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700271 std::unique_ptr<ZipFileWriter> writer = util::make_unique<ZipFileWriter>();
Adam Lesinski06460ef2017-03-14 18:52:13 -0700272 if (!writer->Open(path)) {
273 diag->Error(DiagMessage(path) << writer->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700274 return {};
275 }
276 return std::move(writer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700277}
278
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700279} // namespace aapt