blob: d152a9cc7e62c8b381cb8b6bf19d27d366602d31 [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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070034
Adam Lesinski1ab598f2015-08-14 14:26:04 -070035namespace aapt {
36
37namespace {
38
Adam Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070045 file::FileType type = file::GetFileType(dir_);
Adam Lesinskicacb28f2016-10-19 12:18:14 -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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-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 Lesinskice5e56e22016-10-21 17:56:45 -070086 bool FinishEntry() override {
87 if (!file_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070088 return false;
89 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -070090 file_.reset(nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070091 return true;
92 }
Adam Lesinskice5e56e22016-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 }
106 return !in->HadError();
107 }
108
Adam Lesinski46708052017-09-29 14:49:15 -0700109 bool HadError() const override {
110 return !error_.empty();
111 }
Adam Lesinski06460ef2017-03-14 18:52:13 -0700112
Adam Lesinski46708052017-09-29 14:49:15 -0700113 std::string GetError() const override {
114 return error_;
115 }
Adam Lesinski06460ef2017-03-14 18:52:13 -0700116
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700117 private:
118 DISALLOW_COPY_AND_ASSIGN(DirectoryWriter);
119
120 std::string dir_;
121 std::unique_ptr<FILE, decltype(fclose)*> file_ = {nullptr, fclose};
Adam Lesinski06460ef2017-03-14 18:52:13 -0700122 std::string error_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700123};
124
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700125class ZipFileWriter : public IArchiveWriter {
126 public:
127 ZipFileWriter() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128
Adam Lesinski06460ef2017-03-14 18:52:13 -0700129 bool Open(const StringPiece& path) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700130 file_ = {::android::base::utf8::fopen(path.to_string().c_str(), "w+b"), fclose};
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700131 if (!file_) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700132 error_ = SystemErrorCodeToString(errno);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700133 return false;
134 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700135 writer_ = util::make_unique<ZipWriter>(file_.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700136 return true;
137 }
138
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700139 bool StartEntry(const StringPiece& path, uint32_t flags) override {
140 if (!writer_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700142 }
143
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700144 size_t zip_flags = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 if (flags & ArchiveEntry::kCompress) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700146 zip_flags |= ZipWriter::kCompress;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800147 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700148
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700149 if (flags & ArchiveEntry::kAlign) {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700150 zip_flags |= ZipWriter::kAlign32;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800151 }
152
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700153 int32_t result = writer_->StartEntry(path.data(), zip_flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 if (result != 0) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700155 error_ = ZipWriter::ErrorCodeString(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700156 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700157 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700158 return true;
159 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700160
Adam Lesinski06460ef2017-03-14 18:52:13 -0700161 bool Write(const void* data, int len) override {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700162 int32_t result = writer_->WriteBytes(data, len);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 if (result != 0) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700164 error_ = ZipWriter::ErrorCodeString(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167 return true;
168 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700169
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700170 bool FinishEntry() override {
171 int32_t result = writer_->FinishEntry();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700172 if (result != 0) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700173 error_ = ZipWriter::ErrorCodeString(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700174 return false;
175 }
176 return true;
177 }
178
Adam Lesinski06460ef2017-03-14 18:52:13 -0700179 bool WriteFile(const StringPiece& path, uint32_t flags, io::InputStream* in) override {
180 while (true) {
181 if (!StartEntry(path, flags)) {
182 return false;
183 }
184
185 const void* data = nullptr;
186 size_t len = 0;
187 while (in->Next(&data, &len)) {
188 if (!Write(data, static_cast<int>(len))) {
189 return false;
190 }
191 }
192
193 if (in->HadError()) {
194 return false;
195 }
196
197 if (!FinishEntry()) {
198 return false;
199 }
200
201 // Check to see if the file was compressed enough. This is preserving behavior of AAPT.
202 if ((flags & ArchiveEntry::kCompress) != 0 && in->CanRewind()) {
203 ZipWriter::FileEntry last_entry;
204 int32_t result = writer_->GetLastEntry(&last_entry);
205 CHECK(result == 0);
206 if (last_entry.compressed_size + (last_entry.compressed_size / 10) >
207 last_entry.uncompressed_size) {
208 // The file was not compressed enough, rewind and store it uncompressed.
209 if (!in->Rewind()) {
210 // Well we tried, may as well keep what we had.
211 return true;
212 }
213
214 int32_t result = writer_->DiscardLastEntry();
215 if (result != 0) {
216 error_ = ZipWriter::ErrorCodeString(result);
217 return false;
218 }
219 flags &= ~ArchiveEntry::kCompress;
220
221 continue;
222 }
223 }
224 return true;
225 }
226 }
227
Adam Lesinski46708052017-09-29 14:49:15 -0700228 bool HadError() const override {
229 return !error_.empty();
230 }
Adam Lesinski06460ef2017-03-14 18:52:13 -0700231
Adam Lesinski46708052017-09-29 14:49:15 -0700232 std::string GetError() const override {
233 return error_;
234 }
Adam Lesinski06460ef2017-03-14 18:52:13 -0700235
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700236 virtual ~ZipFileWriter() {
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700237 if (writer_) {
238 writer_->Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700239 }
240 }
Adam Lesinskice5e56e22016-10-21 17:56:45 -0700241
242 private:
243 DISALLOW_COPY_AND_ASSIGN(ZipFileWriter);
244
245 std::unique_ptr<FILE, decltype(fclose)*> file_ = {nullptr, fclose};
246 std::unique_ptr<ZipWriter> writer_;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700247 std::string error_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700248};
249
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700250} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700251
Adam Lesinski06460ef2017-03-14 18:52:13 -0700252std::unique_ptr<IArchiveWriter> CreateDirectoryArchiveWriter(IDiagnostics* diag,
253 const StringPiece& path) {
254 std::unique_ptr<DirectoryWriter> writer = util::make_unique<DirectoryWriter>();
255 if (!writer->Open(path)) {
256 diag->Error(DiagMessage(path) << writer->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700257 return {};
258 }
259 return std::move(writer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700260}
261
Adam Lesinski06460ef2017-03-14 18:52:13 -0700262std::unique_ptr<IArchiveWriter> CreateZipFileArchiveWriter(IDiagnostics* diag,
263 const StringPiece& path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264 std::unique_ptr<ZipFileWriter> writer = util::make_unique<ZipFileWriter>();
Adam Lesinski06460ef2017-03-14 18:52:13 -0700265 if (!writer->Open(path)) {
266 diag->Error(DiagMessage(path) << writer->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700267 return {};
268 }
269 return std::move(writer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700270}
271
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700272} // namespace aapt