blob: ae08f65882173581e1f94d02bd741e46ada2f395 [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"
18#include "util/Files.h"
19#include "util/StringPiece.h"
20
Adam Lesinskicacb28f2016-10-19 12:18:14 -070021#include <ziparchive/zip_writer.h>
Adam Lesinskia40e9722015-11-24 19:11:46 -080022#include <cstdio>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070023#include <memory>
24#include <string>
25#include <vector>
Adam Lesinski1ab598f2015-08-14 14:26:04 -070026
27namespace aapt {
28
29namespace {
30
31struct DirectoryWriter : public IArchiveWriter {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070032 std::string mOutDir;
33 std::unique_ptr<FILE, decltype(fclose)*> mFile = {nullptr, fclose};
Adam Lesinski1ab598f2015-08-14 14:26:04 -070034
Adam Lesinskicacb28f2016-10-19 12:18:14 -070035 bool open(IDiagnostics* diag, const StringPiece& outDir) {
36 mOutDir = outDir.toString();
37 file::FileType type = file::getFileType(mOutDir);
38 if (type == file::FileType::kNonexistant) {
39 diag->error(DiagMessage() << "directory " << mOutDir
40 << " does not exist");
41 return false;
42 } else if (type != file::FileType::kDirectory) {
43 diag->error(DiagMessage() << mOutDir << " is not a directory");
44 return false;
45 }
46 return true;
47 }
48
49 bool startEntry(const StringPiece& path, uint32_t flags) override {
50 if (mFile) {
51 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070052 }
53
Adam Lesinskicacb28f2016-10-19 12:18:14 -070054 std::string fullPath = mOutDir;
55 file::appendPath(&fullPath, path);
56 file::mkdirs(file::getStem(fullPath));
Adam Lesinskia40e9722015-11-24 19:11:46 -080057
Adam Lesinskicacb28f2016-10-19 12:18:14 -070058 mFile = {fopen(fullPath.data(), "wb"), fclose};
59 if (!mFile) {
60 return false;
61 }
62 return true;
63 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070064
Adam Lesinskicacb28f2016-10-19 12:18:14 -070065 bool writeEntry(const BigBuffer& buffer) override {
66 if (!mFile) {
67 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070068 }
69
Adam Lesinskicacb28f2016-10-19 12:18:14 -070070 for (const BigBuffer::Block& b : buffer) {
71 if (fwrite(b.buffer.get(), 1, b.size, mFile.get()) != b.size) {
Adam Lesinskia40e9722015-11-24 19:11:46 -080072 mFile.reset(nullptr);
Adam Lesinskicacb28f2016-10-19 12:18:14 -070073 return false;
74 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070075 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -070076 return true;
77 }
78
79 bool writeEntry(const void* data, size_t len) override {
80 if (fwrite(data, 1, len, mFile.get()) != len) {
81 mFile.reset(nullptr);
82 return false;
83 }
84 return true;
85 }
86
87 bool finishEntry() override {
88 if (!mFile) {
89 return false;
90 }
91 mFile.reset(nullptr);
92 return true;
93 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -070094};
95
96struct ZipFileWriter : public IArchiveWriter {
Adam Lesinskicacb28f2016-10-19 12:18:14 -070097 std::unique_ptr<FILE, decltype(fclose)*> mFile = {nullptr, fclose};
98 std::unique_ptr<ZipWriter> mWriter;
Adam Lesinski1ab598f2015-08-14 14:26:04 -070099
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700100 bool open(IDiagnostics* diag, const StringPiece& path) {
101 mFile = {fopen(path.data(), "w+b"), fclose};
102 if (!mFile) {
103 diag->error(DiagMessage() << "failed to open " << path << ": "
104 << strerror(errno));
105 return false;
106 }
107 mWriter = util::make_unique<ZipWriter>(mFile.get());
108 return true;
109 }
110
111 bool startEntry(const StringPiece& path, uint32_t flags) override {
112 if (!mWriter) {
113 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700114 }
115
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700116 size_t zipFlags = 0;
117 if (flags & ArchiveEntry::kCompress) {
118 zipFlags |= ZipWriter::kCompress;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800119 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700120
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700121 if (flags & ArchiveEntry::kAlign) {
122 zipFlags |= ZipWriter::kAlign32;
Adam Lesinskia40e9722015-11-24 19:11:46 -0800123 }
124
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700125 int32_t result = mWriter->StartEntry(path.data(), zipFlags);
126 if (result != 0) {
127 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700128 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700129 return true;
130 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700131
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700132 bool writeEntry(const void* data, size_t len) override {
133 int32_t result = mWriter->WriteBytes(data, len);
134 if (result != 0) {
135 return false;
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700136 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700137 return true;
138 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700139
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700140 bool writeEntry(const BigBuffer& buffer) override {
141 for (const BigBuffer::Block& b : buffer) {
142 int32_t result = mWriter->WriteBytes(b.buffer.get(), b.size);
143 if (result != 0) {
144 return false;
145 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700146 }
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700147 return true;
148 }
149
150 bool finishEntry() override {
151 int32_t result = mWriter->FinishEntry();
152 if (result != 0) {
153 return false;
154 }
155 return true;
156 }
157
158 virtual ~ZipFileWriter() {
159 if (mWriter) {
160 mWriter->Finish();
161 }
162 }
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700163};
164
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700165} // namespace
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700166
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700167std::unique_ptr<IArchiveWriter> createDirectoryArchiveWriter(
168 IDiagnostics* diag, const StringPiece& path) {
169 std::unique_ptr<DirectoryWriter> writer =
170 util::make_unique<DirectoryWriter>();
171 if (!writer->open(diag, path)) {
172 return {};
173 }
174 return std::move(writer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700175}
176
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700177std::unique_ptr<IArchiveWriter> createZipFileArchiveWriter(
178 IDiagnostics* diag, const StringPiece& path) {
179 std::unique_ptr<ZipFileWriter> writer = util::make_unique<ZipFileWriter>();
180 if (!writer->open(diag, path)) {
181 return {};
182 }
183 return std::move(writer);
Adam Lesinski1ab598f2015-08-14 14:26:04 -0700184}
185
Adam Lesinskicacb28f2016-10-19 12:18:14 -0700186} // namespace aapt