blob: 6ad3366e4622a85b7b18eb705d1ef1a4ae682f6c [file] [log] [blame]
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -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 Lesinskiad4ad8c2015-10-05 18:16:18 -070017#include "ziparchive/zip_writer.h"
18
Adam Lesinski537713b2017-03-16 13:23:51 -070019#include <sys/param.h>
Adam Lesinskie2fa70b2017-03-29 16:10:11 -070020#include <sys/stat.h>
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070021#include <zlib.h>
Jiyong Parkcd997e62017-06-30 17:23:33 +090022#include <cstdio>
23#define DEF_MEM_LEVEL 8 // normally in zutil.h?
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070024
Adam Lesinski537713b2017-03-16 13:23:51 -070025#include <memory>
26#include <vector>
27
28#include "android-base/logging.h"
Adam Lesinskib02d6902017-03-23 11:57:05 -070029#include "utils/Compat.h"
Adam Lesinski537713b2017-03-16 13:23:51 -070030#include "utils/Log.h"
31
32#include "entry_name_utils-inl.h"
33#include "zip_archive_common.h"
34
Christopher Ferris5e9f3d42016-01-19 10:33:03 -080035#if !defined(powerof2)
Jiyong Parkcd997e62017-06-30 17:23:33 +090036#define powerof2(x) ((((x)-1) & (x)) == 0)
Christopher Ferris5e9f3d42016-01-19 10:33:03 -080037#endif
38
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070039/* Zip compression methods we support */
40enum {
Jiyong Parkcd997e62017-06-30 17:23:33 +090041 kCompressStored = 0, // no compression
42 kCompressDeflated = 8, // standard deflate
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070043};
44
Adam Lesinski591fd392015-10-06 15:23:46 -070045// Size of the output buffer used for compression.
46static const size_t kBufSize = 32768u;
47
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070048// No error, operation completed successfully.
49static const int32_t kNoError = 0;
50
51// The ZipWriter is in a bad state.
52static const int32_t kInvalidState = -1;
53
54// There was an IO error while writing to disk.
55static const int32_t kIoError = -2;
56
57// The zip entry name was invalid.
58static const int32_t kInvalidEntryName = -3;
59
Adam Lesinski591fd392015-10-06 15:23:46 -070060// An error occurred in zlib.
61static const int32_t kZlibError = -4;
62
Christopher Ferris5e9f3d42016-01-19 10:33:03 -080063// The start aligned function was called with the aligned flag.
64static const int32_t kInvalidAlign32Flag = -5;
65
66// The alignment parameter is not a power of 2.
67static const int32_t kInvalidAlignment = -6;
68
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070069static const char* sErrorCodes[] = {
Jiyong Parkcd997e62017-06-30 17:23:33 +090070 "Invalid state", "IO error", "Invalid entry name", "Zlib error",
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070071};
72
73const char* ZipWriter::ErrorCodeString(int32_t error_code) {
74 if (error_code < 0 && (-error_code) < static_cast<int32_t>(arraysize(sErrorCodes))) {
75 return sErrorCodes[-error_code];
76 }
77 return nullptr;
78}
79
Adam Lesinski591fd392015-10-06 15:23:46 -070080static void DeleteZStream(z_stream* stream) {
81 deflateEnd(stream);
82 delete stream;
83}
84
Jiyong Parkcd997e62017-06-30 17:23:33 +090085ZipWriter::ZipWriter(FILE* f)
86 : file_(f),
87 seekable_(false),
88 current_offset_(0),
89 state_(State::kWritingZip),
90 z_stream_(nullptr, DeleteZStream),
91 buffer_(kBufSize) {
Adam Lesinskie2fa70b2017-03-29 16:10:11 -070092 // Check if the file is seekable (regular file). If fstat fails, that's fine, subsequent calls
93 // will fail as well.
94 struct stat file_stats;
95 if (fstat(fileno(f), &file_stats) == 0) {
96 seekable_ = S_ISREG(file_stats.st_mode);
97 }
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -070098}
99
Jiyong Parkcd997e62017-06-30 17:23:33 +0900100ZipWriter::ZipWriter(ZipWriter&& writer)
101 : file_(writer.file_),
102 seekable_(writer.seekable_),
103 current_offset_(writer.current_offset_),
104 state_(writer.state_),
105 files_(std::move(writer.files_)),
106 z_stream_(std::move(writer.z_stream_)),
107 buffer_(std::move(writer.buffer_)) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700108 writer.file_ = nullptr;
109 writer.state_ = State::kError;
110}
111
112ZipWriter& ZipWriter::operator=(ZipWriter&& writer) {
113 file_ = writer.file_;
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700114 seekable_ = writer.seekable_;
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700115 current_offset_ = writer.current_offset_;
116 state_ = writer.state_;
117 files_ = std::move(writer.files_);
Adam Lesinski591fd392015-10-06 15:23:46 -0700118 z_stream_ = std::move(writer.z_stream_);
119 buffer_ = std::move(writer.buffer_);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700120 writer.file_ = nullptr;
121 writer.state_ = State::kError;
122 return *this;
123}
124
125int32_t ZipWriter::HandleError(int32_t error_code) {
126 state_ = State::kError;
Adam Lesinski591fd392015-10-06 15:23:46 -0700127 z_stream_.reset();
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700128 return error_code;
129}
130
131int32_t ZipWriter::StartEntry(const char* path, size_t flags) {
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800132 uint32_t alignment = 0;
133 if (flags & kAlign32) {
134 flags &= ~kAlign32;
135 alignment = 4;
136 }
137 return StartAlignedEntryWithTime(path, flags, time_t(), alignment);
138}
139
140int32_t ZipWriter::StartAlignedEntry(const char* path, size_t flags, uint32_t alignment) {
141 return StartAlignedEntryWithTime(path, flags, time_t(), alignment);
142}
143
144int32_t ZipWriter::StartEntryWithTime(const char* path, size_t flags, time_t time) {
145 uint32_t alignment = 0;
146 if (flags & kAlign32) {
147 flags &= ~kAlign32;
148 alignment = 4;
149 }
150 return StartAlignedEntryWithTime(path, flags, time, alignment);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700151}
152
153static void ExtractTimeAndDate(time_t when, uint16_t* out_time, uint16_t* out_date) {
154 /* round up to an even number of seconds */
155 when = static_cast<time_t>((static_cast<unsigned long>(when) + 1) & (~1));
156
157 struct tm* ptm;
158#if !defined(_WIN32)
Jiyong Parkcd997e62017-06-30 17:23:33 +0900159 struct tm tm_result;
160 ptm = localtime_r(&when, &tm_result);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700161#else
Jiyong Parkcd997e62017-06-30 17:23:33 +0900162 ptm = localtime(&when);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700163#endif
164
165 int year = ptm->tm_year;
166 if (year < 80) {
167 year = 80;
168 }
169
170 *out_date = (year - 80) << 9 | (ptm->tm_mon + 1) << 5 | ptm->tm_mday;
171 *out_time = ptm->tm_hour << 11 | ptm->tm_min << 5 | ptm->tm_sec >> 1;
172}
173
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700174static void CopyFromFileEntry(const ZipWriter::FileEntry& src, bool use_data_descriptor,
175 LocalFileHeader* dst) {
176 dst->lfh_signature = LocalFileHeader::kSignature;
177 if (use_data_descriptor) {
178 // Set this flag to denote that a DataDescriptor struct will appear after the data,
179 // containing the crc and size fields.
180 dst->gpb_flags |= kGPBDDFlagMask;
181
182 // The size and crc fields must be 0.
183 dst->compressed_size = 0u;
184 dst->uncompressed_size = 0u;
185 dst->crc32 = 0u;
186 } else {
187 dst->compressed_size = src.compressed_size;
188 dst->uncompressed_size = src.uncompressed_size;
189 dst->crc32 = src.crc32;
190 }
191 dst->compression_method = src.compression_method;
192 dst->last_mod_time = src.last_mod_time;
193 dst->last_mod_date = src.last_mod_date;
194 dst->file_name_length = src.path.size();
195 dst->extra_field_length = src.padding_length;
196}
197
Jiyong Parkcd997e62017-06-30 17:23:33 +0900198int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags, time_t time,
199 uint32_t alignment) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700200 if (state_ != State::kWritingZip) {
201 return kInvalidState;
202 }
203
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800204 if (flags & kAlign32) {
205 return kInvalidAlign32Flag;
206 }
207
208 if (powerof2(alignment) == 0) {
209 return kInvalidAlignment;
210 }
211
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700212 FileEntry file_entry = {};
213 file_entry.local_file_header_offset = current_offset_;
214 file_entry.path = path;
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700215
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700216 if (!IsValidEntryName(reinterpret_cast<const uint8_t*>(file_entry.path.data()),
217 file_entry.path.size())) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700218 return kInvalidEntryName;
219 }
220
Adam Lesinski591fd392015-10-06 15:23:46 -0700221 if (flags & ZipWriter::kCompress) {
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700222 file_entry.compression_method = kCompressDeflated;
Adam Lesinski591fd392015-10-06 15:23:46 -0700223
224 int32_t result = PrepareDeflate();
225 if (result != kNoError) {
226 return result;
227 }
228 } else {
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700229 file_entry.compression_method = kCompressStored;
Adam Lesinski591fd392015-10-06 15:23:46 -0700230 }
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700231
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700232 ExtractTimeAndDate(time, &file_entry.last_mod_time, &file_entry.last_mod_date);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700233
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700234 off_t offset = current_offset_ + sizeof(LocalFileHeader) + file_entry.path.size();
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800235 std::vector<char> zero_padding;
236 if (alignment != 0 && (offset & (alignment - 1))) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700237 // Pad the extra field so the data will be aligned.
Christopher Ferris5e9f3d42016-01-19 10:33:03 -0800238 uint16_t padding = alignment - (offset % alignment);
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700239 file_entry.padding_length = padding;
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700240 offset += padding;
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700241 zero_padding.resize(padding, 0);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700242 }
243
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700244 LocalFileHeader header = {};
245 // Always start expecting a data descriptor. When the data has finished being written,
246 // if it is possible to seek back, the GPB flag will reset and the sizes written.
247 CopyFromFileEntry(file_entry, true /*use_data_descriptor*/, &header);
248
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700249 if (fwrite(&header, sizeof(header), 1, file_) != 1) {
250 return HandleError(kIoError);
251 }
252
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700253 if (fwrite(path, sizeof(*path), file_entry.path.size(), file_) != file_entry.path.size()) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700254 return HandleError(kIoError);
255 }
256
Jiyong Parkcd997e62017-06-30 17:23:33 +0900257 if (file_entry.padding_length != 0 && fwrite(zero_padding.data(), 1, file_entry.padding_length,
258 file_) != file_entry.padding_length) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700259 return HandleError(kIoError);
260 }
261
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700262 current_file_entry_ = std::move(file_entry);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700263 current_offset_ = offset;
264 state_ = State::kWritingEntry;
265 return kNoError;
266}
267
Adam Lesinski537713b2017-03-16 13:23:51 -0700268int32_t ZipWriter::DiscardLastEntry() {
269 if (state_ != State::kWritingZip || files_.empty()) {
270 return kInvalidState;
271 }
272
273 FileEntry& last_entry = files_.back();
274 current_offset_ = last_entry.local_file_header_offset;
275 if (fseeko(file_, current_offset_, SEEK_SET) != 0) {
276 return HandleError(kIoError);
277 }
278 files_.pop_back();
279 return kNoError;
280}
281
282int32_t ZipWriter::GetLastEntry(FileEntry* out_entry) {
283 CHECK(out_entry != nullptr);
284
285 if (files_.empty()) {
286 return kInvalidState;
287 }
288 *out_entry = files_.back();
289 return kNoError;
290}
291
Adam Lesinski591fd392015-10-06 15:23:46 -0700292int32_t ZipWriter::PrepareDeflate() {
Adam Lesinski537713b2017-03-16 13:23:51 -0700293 CHECK(state_ == State::kWritingZip);
Adam Lesinski591fd392015-10-06 15:23:46 -0700294
295 // Initialize the z_stream for compression.
Jiyong Parkcd997e62017-06-30 17:23:33 +0900296 z_stream_ = std::unique_ptr<z_stream, void (*)(z_stream*)>(new z_stream(), DeleteZStream);
Adam Lesinski591fd392015-10-06 15:23:46 -0700297
Colin Cross7c6c7f02016-09-16 10:15:51 -0700298#pragma GCC diagnostic push
299#pragma GCC diagnostic ignored "-Wold-style-cast"
Adam Lesinski591fd392015-10-06 15:23:46 -0700300 int zerr = deflateInit2(z_stream_.get(), Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS,
301 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
Colin Cross7c6c7f02016-09-16 10:15:51 -0700302#pragma GCC diagnostic pop
303
Adam Lesinski591fd392015-10-06 15:23:46 -0700304 if (zerr != Z_OK) {
305 if (zerr == Z_VERSION_ERROR) {
306 ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION);
307 return HandleError(kZlibError);
308 } else {
309 ALOGE("deflateInit2 failed (zerr=%d)", zerr);
310 return HandleError(kZlibError);
311 }
312 }
313
314 z_stream_->next_out = buffer_.data();
315 z_stream_->avail_out = buffer_.size();
316 return kNoError;
317}
318
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700319int32_t ZipWriter::WriteBytes(const void* data, size_t len) {
320 if (state_ != State::kWritingEntry) {
321 return HandleError(kInvalidState);
322 }
323
Adam Lesinski591fd392015-10-06 15:23:46 -0700324 int32_t result = kNoError;
Adam Lesinski537713b2017-03-16 13:23:51 -0700325 if (current_file_entry_.compression_method & kCompressDeflated) {
326 result = CompressBytes(&current_file_entry_, data, len);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700327 } else {
Adam Lesinski537713b2017-03-16 13:23:51 -0700328 result = StoreBytes(&current_file_entry_, data, len);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700329 }
330
Adam Lesinski591fd392015-10-06 15:23:46 -0700331 if (result != kNoError) {
332 return result;
333 }
334
Jiyong Parkcd997e62017-06-30 17:23:33 +0900335 current_file_entry_.crc32 =
336 crc32(current_file_entry_.crc32, reinterpret_cast<const Bytef*>(data), len);
Adam Lesinski537713b2017-03-16 13:23:51 -0700337 current_file_entry_.uncompressed_size += len;
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700338 return kNoError;
339}
340
Adam Lesinski537713b2017-03-16 13:23:51 -0700341int32_t ZipWriter::StoreBytes(FileEntry* file, const void* data, size_t len) {
342 CHECK(state_ == State::kWritingEntry);
Adam Lesinski591fd392015-10-06 15:23:46 -0700343
344 if (fwrite(data, 1, len, file_) != len) {
345 return HandleError(kIoError);
346 }
347 file->compressed_size += len;
348 current_offset_ += len;
349 return kNoError;
350}
351
Adam Lesinski537713b2017-03-16 13:23:51 -0700352int32_t ZipWriter::CompressBytes(FileEntry* file, const void* data, size_t len) {
353 CHECK(state_ == State::kWritingEntry);
354 CHECK(z_stream_);
355 CHECK(z_stream_->next_out != nullptr);
356 CHECK(z_stream_->avail_out != 0);
Adam Lesinski591fd392015-10-06 15:23:46 -0700357
358 // Prepare the input.
359 z_stream_->next_in = reinterpret_cast<const uint8_t*>(data);
360 z_stream_->avail_in = len;
361
362 while (z_stream_->avail_in > 0) {
363 // We have more data to compress.
364 int zerr = deflate(z_stream_.get(), Z_NO_FLUSH);
365 if (zerr != Z_OK) {
366 return HandleError(kZlibError);
367 }
368
369 if (z_stream_->avail_out == 0) {
370 // The output is full, let's write it to disk.
Christopher Ferrisa2a32b02015-11-04 17:54:32 -0800371 size_t write_bytes = z_stream_->next_out - buffer_.data();
372 if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) {
Adam Lesinski591fd392015-10-06 15:23:46 -0700373 return HandleError(kIoError);
374 }
Christopher Ferrisa2a32b02015-11-04 17:54:32 -0800375 file->compressed_size += write_bytes;
376 current_offset_ += write_bytes;
Adam Lesinski591fd392015-10-06 15:23:46 -0700377
378 // Reset the output buffer for the next input.
379 z_stream_->next_out = buffer_.data();
380 z_stream_->avail_out = buffer_.size();
381 }
382 }
383 return kNoError;
384}
385
Adam Lesinski537713b2017-03-16 13:23:51 -0700386int32_t ZipWriter::FlushCompressedBytes(FileEntry* file) {
387 CHECK(state_ == State::kWritingEntry);
388 CHECK(z_stream_);
389 CHECK(z_stream_->next_out != nullptr);
390 CHECK(z_stream_->avail_out != 0);
Adam Lesinski591fd392015-10-06 15:23:46 -0700391
Christopher Ferrisa2a32b02015-11-04 17:54:32 -0800392 // Keep deflating while there isn't enough space in the buffer to
393 // to complete the compress.
394 int zerr;
395 while ((zerr = deflate(z_stream_.get(), Z_FINISH)) == Z_OK) {
Adam Lesinski537713b2017-03-16 13:23:51 -0700396 CHECK(z_stream_->avail_out == 0);
Christopher Ferrisa2a32b02015-11-04 17:54:32 -0800397 size_t write_bytes = z_stream_->next_out - buffer_.data();
398 if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) {
399 return HandleError(kIoError);
400 }
401 file->compressed_size += write_bytes;
402 current_offset_ += write_bytes;
403
404 z_stream_->next_out = buffer_.data();
405 z_stream_->avail_out = buffer_.size();
406 }
Adam Lesinski591fd392015-10-06 15:23:46 -0700407 if (zerr != Z_STREAM_END) {
408 return HandleError(kZlibError);
409 }
410
Christopher Ferrisa2a32b02015-11-04 17:54:32 -0800411 size_t write_bytes = z_stream_->next_out - buffer_.data();
412 if (write_bytes != 0) {
413 if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) {
Adam Lesinski591fd392015-10-06 15:23:46 -0700414 return HandleError(kIoError);
415 }
Christopher Ferrisa2a32b02015-11-04 17:54:32 -0800416 file->compressed_size += write_bytes;
417 current_offset_ += write_bytes;
Adam Lesinski591fd392015-10-06 15:23:46 -0700418 }
419 z_stream_.reset();
420 return kNoError;
421}
422
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700423int32_t ZipWriter::FinishEntry() {
424 if (state_ != State::kWritingEntry) {
425 return kInvalidState;
426 }
427
Adam Lesinski537713b2017-03-16 13:23:51 -0700428 if (current_file_entry_.compression_method & kCompressDeflated) {
429 int32_t result = FlushCompressedBytes(&current_file_entry_);
Adam Lesinski591fd392015-10-06 15:23:46 -0700430 if (result != kNoError) {
431 return result;
432 }
433 }
434
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700435 if ((current_file_entry_.compression_method & kCompressDeflated) || !seekable_) {
436 // Some versions of ZIP don't allow STORED data to have a trailing DataDescriptor.
437 // If this file is not seekable, or if the data is compressed, write a DataDescriptor.
438 const uint32_t sig = DataDescriptor::kOptSignature;
439 if (fwrite(&sig, sizeof(sig), 1, file_) != 1) {
440 return HandleError(kIoError);
441 }
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700442
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700443 DataDescriptor dd = {};
444 dd.crc32 = current_file_entry_.crc32;
445 dd.compressed_size = current_file_entry_.compressed_size;
446 dd.uncompressed_size = current_file_entry_.uncompressed_size;
447 if (fwrite(&dd, sizeof(dd), 1, file_) != 1) {
448 return HandleError(kIoError);
449 }
450 current_offset_ += sizeof(DataDescriptor::kOptSignature) + sizeof(dd);
451 } else {
452 // Seek back to the header and rewrite to include the size.
453 if (fseeko(file_, current_file_entry_.local_file_header_offset, SEEK_SET) != 0) {
454 return HandleError(kIoError);
455 }
456
457 LocalFileHeader header = {};
458 CopyFromFileEntry(current_file_entry_, false /*use_data_descriptor*/, &header);
459
460 if (fwrite(&header, sizeof(header), 1, file_) != 1) {
461 return HandleError(kIoError);
462 }
463
464 if (fseeko(file_, current_offset_, SEEK_SET) != 0) {
465 return HandleError(kIoError);
466 }
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700467 }
468
Adam Lesinski537713b2017-03-16 13:23:51 -0700469 files_.emplace_back(std::move(current_file_entry_));
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700470 state_ = State::kWritingZip;
471 return kNoError;
472}
473
474int32_t ZipWriter::Finish() {
475 if (state_ != State::kWritingZip) {
476 return kInvalidState;
477 }
478
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700479 off_t startOfCdr = current_offset_;
Adam Lesinski537713b2017-03-16 13:23:51 -0700480 for (FileEntry& file : files_) {
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700481 CentralDirectoryRecord cdr = {};
482 cdr.record_signature = CentralDirectoryRecord::kSignature;
Adam Lesinskid987c9d2017-04-06 18:55:47 -0700483 if ((file.compression_method & kCompressDeflated) || !seekable_) {
484 cdr.gpb_flags |= kGPBDDFlagMask;
485 }
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700486 cdr.compression_method = file.compression_method;
487 cdr.last_mod_time = file.last_mod_time;
488 cdr.last_mod_date = file.last_mod_date;
489 cdr.crc32 = file.crc32;
490 cdr.compressed_size = file.compressed_size;
491 cdr.uncompressed_size = file.uncompressed_size;
492 cdr.file_name_length = file.path.size();
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700493 cdr.local_file_header_offset = static_cast<uint32_t>(file.local_file_header_offset);
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700494 if (fwrite(&cdr, sizeof(cdr), 1, file_) != 1) {
495 return HandleError(kIoError);
496 }
497
498 if (fwrite(file.path.data(), 1, file.path.size(), file_) != file.path.size()) {
499 return HandleError(kIoError);
500 }
501
502 current_offset_ += sizeof(cdr) + file.path.size();
503 }
504
505 EocdRecord er = {};
506 er.eocd_signature = EocdRecord::kSignature;
Adam Lesinski044c7902015-10-20 12:41:49 -0700507 er.disk_num = 0;
508 er.cd_start_disk = 0;
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700509 er.num_records_on_disk = files_.size();
510 er.num_records = files_.size();
511 er.cd_size = current_offset_ - startOfCdr;
512 er.cd_start_offset = startOfCdr;
513
514 if (fwrite(&er, sizeof(er), 1, file_) != 1) {
515 return HandleError(kIoError);
516 }
517
Adam Lesinski537713b2017-03-16 13:23:51 -0700518 current_offset_ += sizeof(er);
519
520 // Since we can BackUp() and potentially finish writing at an offset less than one we had
521 // already written at, we must truncate the file.
522
Adam Lesinskie2fa70b2017-03-29 16:10:11 -0700523 if (ftruncate(fileno(file_), current_offset_) != 0) {
Adam Lesinski537713b2017-03-16 13:23:51 -0700524 return HandleError(kIoError);
525 }
526
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700527 if (fflush(file_) != 0) {
528 return HandleError(kIoError);
529 }
530
Adam Lesinskiad4ad8c2015-10-05 18:16:18 -0700531 state_ = State::kDone;
532 return kNoError;
533}