blob: 5f8bd063f9b0943b6e4dda5d50c21774fa761b58 [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
17#include "flatten/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_);
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 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 }
106 return !in->HadError();
107 }
108
109 bool HadError() const override { return !error_.empty(); }
110
111 std::string GetError() const override { return error_; }
112
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700113 private:
114 DISALLOW_COPY_AND_ASSIGN(DirectoryWriter);
115
116 std::string dir_;
117 std::unique_ptr<FILE, decltype(fclose)*> file_ = {nullptr, fclose};
Adam Lesinski06460ef2017-03-14 18:52:13 -0700118 std::string error_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700119};
120
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700121class ZipFileWriter : public IArchiveWriter {
122 public:
123 ZipFileWriter() = default;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700124
Adam Lesinski06460ef2017-03-14 18:52:13 -0700125 bool Open(const StringPiece& path) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700126 file_ = {::android::base::utf8::fopen(path.to_string().c_str(), "w+b"), fclose};
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700127 if (!file_) {
Adam Lesinskiefeb7af2017-08-02 14:57:43 -0700128 error_ = SystemErrorCodeToString(errno);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 return false;
130 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700131 writer_ = util::make_unique<ZipWriter>(file_.get());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 return true;
133 }
134
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700135 bool StartEntry(const StringPiece& path, uint32_t flags) override {
136 if (!writer_) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700138 }
139
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700140 size_t zip_flags = 0;
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700141 if (flags & ArchiveEntry::kCompress) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700142 zip_flags |= ZipWriter::kCompress;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800143 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700144
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700145 if (flags & ArchiveEntry::kAlign) {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700146 zip_flags |= ZipWriter::kAlign32;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800147 }
148
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700149 int32_t result = writer_->StartEntry(path.data(), zip_flags);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700150 if (result != 0) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700151 error_ = ZipWriter::ErrorCodeString(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700152 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700153 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700154 return true;
155 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700156
Adam Lesinski06460ef2017-03-14 18:52:13 -0700157 bool Write(const void* data, int len) override {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700158 int32_t result = writer_->WriteBytes(data, len);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700159 if (result != 0) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700160 error_ = ZipWriter::ErrorCodeString(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700161 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700162 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700163 return true;
164 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700165
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700166 bool FinishEntry() override {
167 int32_t result = writer_->FinishEntry();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700168 if (result != 0) {
Adam Lesinski06460ef2017-03-14 18:52:13 -0700169 error_ = ZipWriter::ErrorCodeString(result);
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700170 return false;
171 }
172 return true;
173 }
174
Adam Lesinski06460ef2017-03-14 18:52:13 -0700175 bool WriteFile(const StringPiece& path, uint32_t flags, io::InputStream* in) override {
176 while (true) {
177 if (!StartEntry(path, flags)) {
178 return false;
179 }
180
181 const void* data = nullptr;
182 size_t len = 0;
183 while (in->Next(&data, &len)) {
184 if (!Write(data, static_cast<int>(len))) {
185 return false;
186 }
187 }
188
189 if (in->HadError()) {
190 return false;
191 }
192
193 if (!FinishEntry()) {
194 return false;
195 }
196
197 // Check to see if the file was compressed enough. This is preserving behavior of AAPT.
198 if ((flags & ArchiveEntry::kCompress) != 0 && in->CanRewind()) {
199 ZipWriter::FileEntry last_entry;
200 int32_t result = writer_->GetLastEntry(&last_entry);
201 CHECK(result == 0);
202 if (last_entry.compressed_size + (last_entry.compressed_size / 10) >
203 last_entry.uncompressed_size) {
204 // The file was not compressed enough, rewind and store it uncompressed.
205 if (!in->Rewind()) {
206 // Well we tried, may as well keep what we had.
207 return true;
208 }
209
210 int32_t result = writer_->DiscardLastEntry();
211 if (result != 0) {
212 error_ = ZipWriter::ErrorCodeString(result);
213 return false;
214 }
215 flags &= ~ArchiveEntry::kCompress;
216
217 continue;
218 }
219 }
220 return true;
221 }
222 }
223
224 bool HadError() const override { return !error_.empty(); }
225
226 std::string GetError() const override { return error_; }
227
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700228 virtual ~ZipFileWriter() {
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700229 if (writer_) {
230 writer_->Finish();
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700231 }
232 }
Adam Lesinskice5e56e2016-10-21 17:56:45 -0700233
234 private:
235 DISALLOW_COPY_AND_ASSIGN(ZipFileWriter);
236
237 std::unique_ptr<FILE, decltype(fclose)*> file_ = {nullptr, fclose};
238 std::unique_ptr<ZipWriter> writer_;
Adam Lesinski06460ef2017-03-14 18:52:13 -0700239 std::string error_;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700240};
241
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700242} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700243
Adam Lesinski06460ef2017-03-14 18:52:13 -0700244std::unique_ptr<IArchiveWriter> CreateDirectoryArchiveWriter(IDiagnostics* diag,
245 const StringPiece& path) {
246 std::unique_ptr<DirectoryWriter> writer = util::make_unique<DirectoryWriter>();
247 if (!writer->Open(path)) {
248 diag->Error(DiagMessage(path) << writer->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700249 return {};
250 }
251 return std::move(writer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700252}
253
Adam Lesinski06460ef2017-03-14 18:52:13 -0700254std::unique_ptr<IArchiveWriter> CreateZipFileArchiveWriter(IDiagnostics* diag,
255 const StringPiece& path) {
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700256 std::unique_ptr<ZipFileWriter> writer = util::make_unique<ZipFileWriter>();
Adam Lesinski06460ef2017-03-14 18:52:13 -0700257 if (!writer->Open(path)) {
258 diag->Error(DiagMessage(path) << writer->GetError());
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700259 return {};
260 }
261 return std::move(writer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700262}
263
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700264} // namespace aapt