blob: 6d28bdb5ef42bd7699c9e74708f92047d873bc2f [file] [log] [blame]
Adam Lesinski622f3042015-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 Lesinski622f3042015-10-05 18:16:18 -070017#include "ziparchive/zip_writer.h"
18
Adam Lesinski622f3042015-10-05 18:16:18 -070019#include <cstdio>
Adam Lesinski2e216de2017-03-16 13:23:51 -070020#include <sys/param.h>
Adam Lesinskif61aaf42017-03-29 16:10:11 -070021#include <sys/stat.h>
Adam Lesinski622f3042015-10-05 18:16:18 -070022#include <zlib.h>
Adam Lesinskic322b432015-10-06 15:23:46 -070023#define DEF_MEM_LEVEL 8 // normally in zutil.h?
Adam Lesinski622f3042015-10-05 18:16:18 -070024
Adam Lesinski2e216de2017-03-16 13:23:51 -070025#include <memory>
26#include <vector>
27
28#include "android-base/logging.h"
Adam Lesinski48bd4852017-03-23 11:57:05 -070029#include "utils/Compat.h"
Adam Lesinski2e216de2017-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 Ferris72082ab2016-01-19 10:33:03 -080035#if !defined(powerof2)
36#define powerof2(x) ((((x)-1)&(x))==0)
37#endif
38
Adam Lesinski622f3042015-10-05 18:16:18 -070039/* Zip compression methods we support */
40enum {
41 kCompressStored = 0, // no compression
42 kCompressDeflated = 8, // standard deflate
43};
44
Adam Lesinskic322b432015-10-06 15:23:46 -070045// Size of the output buffer used for compression.
46static const size_t kBufSize = 32768u;
47
Adam Lesinski622f3042015-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 Lesinskic322b432015-10-06 15:23:46 -070060// An error occurred in zlib.
61static const int32_t kZlibError = -4;
62
Christopher Ferris72082ab2016-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 Lesinski622f3042015-10-05 18:16:18 -070069static const char* sErrorCodes[] = {
70 "Invalid state",
71 "IO error",
72 "Invalid entry name",
Adam Lesinskic322b432015-10-06 15:23:46 -070073 "Zlib error",
Adam Lesinski622f3042015-10-05 18:16:18 -070074};
75
76const char* ZipWriter::ErrorCodeString(int32_t error_code) {
77 if (error_code < 0 && (-error_code) < static_cast<int32_t>(arraysize(sErrorCodes))) {
78 return sErrorCodes[-error_code];
79 }
80 return nullptr;
81}
82
Adam Lesinskic322b432015-10-06 15:23:46 -070083static void DeleteZStream(z_stream* stream) {
84 deflateEnd(stream);
85 delete stream;
86}
87
Adam Lesinskif61aaf42017-03-29 16:10:11 -070088ZipWriter::ZipWriter(FILE* f) : file_(f), seekable_(false), current_offset_(0),
89 state_(State::kWritingZip), z_stream_(nullptr, DeleteZStream),
90 buffer_(kBufSize) {
91 // Check if the file is seekable (regular file). If fstat fails, that's fine, subsequent calls
92 // will fail as well.
93 struct stat file_stats;
94 if (fstat(fileno(f), &file_stats) == 0) {
95 seekable_ = S_ISREG(file_stats.st_mode);
96 }
Adam Lesinski622f3042015-10-05 18:16:18 -070097}
98
99ZipWriter::ZipWriter(ZipWriter&& writer) : file_(writer.file_),
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700100 seekable_(writer.seekable_),
Adam Lesinski622f3042015-10-05 18:16:18 -0700101 current_offset_(writer.current_offset_),
102 state_(writer.state_),
Adam Lesinskic322b432015-10-06 15:23:46 -0700103 files_(std::move(writer.files_)),
104 z_stream_(std::move(writer.z_stream_)),
105 buffer_(std::move(writer.buffer_)){
Adam Lesinski622f3042015-10-05 18:16:18 -0700106 writer.file_ = nullptr;
107 writer.state_ = State::kError;
108}
109
110ZipWriter& ZipWriter::operator=(ZipWriter&& writer) {
111 file_ = writer.file_;
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700112 seekable_ = writer.seekable_;
Adam Lesinski622f3042015-10-05 18:16:18 -0700113 current_offset_ = writer.current_offset_;
114 state_ = writer.state_;
115 files_ = std::move(writer.files_);
Adam Lesinskic322b432015-10-06 15:23:46 -0700116 z_stream_ = std::move(writer.z_stream_);
117 buffer_ = std::move(writer.buffer_);
Adam Lesinski622f3042015-10-05 18:16:18 -0700118 writer.file_ = nullptr;
119 writer.state_ = State::kError;
120 return *this;
121}
122
123int32_t ZipWriter::HandleError(int32_t error_code) {
124 state_ = State::kError;
Adam Lesinskic322b432015-10-06 15:23:46 -0700125 z_stream_.reset();
Adam Lesinski622f3042015-10-05 18:16:18 -0700126 return error_code;
127}
128
129int32_t ZipWriter::StartEntry(const char* path, size_t flags) {
Christopher Ferris72082ab2016-01-19 10:33:03 -0800130 uint32_t alignment = 0;
131 if (flags & kAlign32) {
132 flags &= ~kAlign32;
133 alignment = 4;
134 }
135 return StartAlignedEntryWithTime(path, flags, time_t(), alignment);
136}
137
138int32_t ZipWriter::StartAlignedEntry(const char* path, size_t flags, uint32_t alignment) {
139 return StartAlignedEntryWithTime(path, flags, time_t(), alignment);
140}
141
142int32_t ZipWriter::StartEntryWithTime(const char* path, size_t flags, time_t time) {
143 uint32_t alignment = 0;
144 if (flags & kAlign32) {
145 flags &= ~kAlign32;
146 alignment = 4;
147 }
148 return StartAlignedEntryWithTime(path, flags, time, alignment);
Adam Lesinski622f3042015-10-05 18:16:18 -0700149}
150
151static void ExtractTimeAndDate(time_t when, uint16_t* out_time, uint16_t* out_date) {
152 /* round up to an even number of seconds */
153 when = static_cast<time_t>((static_cast<unsigned long>(when) + 1) & (~1));
154
155 struct tm* ptm;
156#if !defined(_WIN32)
157 struct tm tm_result;
158 ptm = localtime_r(&when, &tm_result);
159#else
160 ptm = localtime(&when);
161#endif
162
163 int year = ptm->tm_year;
164 if (year < 80) {
165 year = 80;
166 }
167
168 *out_date = (year - 80) << 9 | (ptm->tm_mon + 1) << 5 | ptm->tm_mday;
169 *out_time = ptm->tm_hour << 11 | ptm->tm_min << 5 | ptm->tm_sec >> 1;
170}
171
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700172static void CopyFromFileEntry(const ZipWriter::FileEntry& src, bool use_data_descriptor,
173 LocalFileHeader* dst) {
174 dst->lfh_signature = LocalFileHeader::kSignature;
175 if (use_data_descriptor) {
176 // Set this flag to denote that a DataDescriptor struct will appear after the data,
177 // containing the crc and size fields.
178 dst->gpb_flags |= kGPBDDFlagMask;
179
180 // The size and crc fields must be 0.
181 dst->compressed_size = 0u;
182 dst->uncompressed_size = 0u;
183 dst->crc32 = 0u;
184 } else {
185 dst->compressed_size = src.compressed_size;
186 dst->uncompressed_size = src.uncompressed_size;
187 dst->crc32 = src.crc32;
188 }
189 dst->compression_method = src.compression_method;
190 dst->last_mod_time = src.last_mod_time;
191 dst->last_mod_date = src.last_mod_date;
192 dst->file_name_length = src.path.size();
193 dst->extra_field_length = src.padding_length;
194}
195
Christopher Ferris72082ab2016-01-19 10:33:03 -0800196int32_t ZipWriter::StartAlignedEntryWithTime(const char* path, size_t flags,
197 time_t time, uint32_t alignment) {
Adam Lesinski622f3042015-10-05 18:16:18 -0700198 if (state_ != State::kWritingZip) {
199 return kInvalidState;
200 }
201
Christopher Ferris72082ab2016-01-19 10:33:03 -0800202 if (flags & kAlign32) {
203 return kInvalidAlign32Flag;
204 }
205
206 if (powerof2(alignment) == 0) {
207 return kInvalidAlignment;
208 }
209
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700210 FileEntry file_entry = {};
211 file_entry.local_file_header_offset = current_offset_;
212 file_entry.path = path;
Adam Lesinski622f3042015-10-05 18:16:18 -0700213
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700214 if (!IsValidEntryName(reinterpret_cast<const uint8_t*>(file_entry.path.data()),
215 file_entry.path.size())) {
Adam Lesinski622f3042015-10-05 18:16:18 -0700216 return kInvalidEntryName;
217 }
218
Adam Lesinskic322b432015-10-06 15:23:46 -0700219 if (flags & ZipWriter::kCompress) {
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700220 file_entry.compression_method = kCompressDeflated;
Adam Lesinskic322b432015-10-06 15:23:46 -0700221
222 int32_t result = PrepareDeflate();
223 if (result != kNoError) {
224 return result;
225 }
226 } else {
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700227 file_entry.compression_method = kCompressStored;
Adam Lesinskic322b432015-10-06 15:23:46 -0700228 }
Adam Lesinski622f3042015-10-05 18:16:18 -0700229
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700230 ExtractTimeAndDate(time, &file_entry.last_mod_time, &file_entry.last_mod_date);
Adam Lesinski622f3042015-10-05 18:16:18 -0700231
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700232 off_t offset = current_offset_ + sizeof(LocalFileHeader) + file_entry.path.size();
Christopher Ferris72082ab2016-01-19 10:33:03 -0800233 std::vector<char> zero_padding;
234 if (alignment != 0 && (offset & (alignment - 1))) {
Adam Lesinski622f3042015-10-05 18:16:18 -0700235 // Pad the extra field so the data will be aligned.
Christopher Ferris72082ab2016-01-19 10:33:03 -0800236 uint16_t padding = alignment - (offset % alignment);
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700237 file_entry.padding_length = padding;
Adam Lesinski622f3042015-10-05 18:16:18 -0700238 offset += padding;
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700239 zero_padding.resize(padding, 0);
Adam Lesinski622f3042015-10-05 18:16:18 -0700240 }
241
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700242 LocalFileHeader header = {};
243 // Always start expecting a data descriptor. When the data has finished being written,
244 // if it is possible to seek back, the GPB flag will reset and the sizes written.
245 CopyFromFileEntry(file_entry, true /*use_data_descriptor*/, &header);
246
Adam Lesinski622f3042015-10-05 18:16:18 -0700247 if (fwrite(&header, sizeof(header), 1, file_) != 1) {
248 return HandleError(kIoError);
249 }
250
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700251 if (fwrite(path, sizeof(*path), file_entry.path.size(), file_) != file_entry.path.size()) {
Adam Lesinski622f3042015-10-05 18:16:18 -0700252 return HandleError(kIoError);
253 }
254
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700255 if (file_entry.padding_length != 0 &&
256 fwrite(zero_padding.data(), 1, file_entry.padding_length, file_)
257 != file_entry.padding_length) {
Adam Lesinski622f3042015-10-05 18:16:18 -0700258 return HandleError(kIoError);
259 }
260
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700261 current_file_entry_ = std::move(file_entry);
Adam Lesinski622f3042015-10-05 18:16:18 -0700262 current_offset_ = offset;
263 state_ = State::kWritingEntry;
264 return kNoError;
265}
266
Adam Lesinski2e216de2017-03-16 13:23:51 -0700267int32_t ZipWriter::DiscardLastEntry() {
268 if (state_ != State::kWritingZip || files_.empty()) {
269 return kInvalidState;
270 }
271
272 FileEntry& last_entry = files_.back();
273 current_offset_ = last_entry.local_file_header_offset;
274 if (fseeko(file_, current_offset_, SEEK_SET) != 0) {
275 return HandleError(kIoError);
276 }
277 files_.pop_back();
278 return kNoError;
279}
280
281int32_t ZipWriter::GetLastEntry(FileEntry* out_entry) {
282 CHECK(out_entry != nullptr);
283
284 if (files_.empty()) {
285 return kInvalidState;
286 }
287 *out_entry = files_.back();
288 return kNoError;
289}
290
Adam Lesinskic322b432015-10-06 15:23:46 -0700291int32_t ZipWriter::PrepareDeflate() {
Adam Lesinski2e216de2017-03-16 13:23:51 -0700292 CHECK(state_ == State::kWritingZip);
Adam Lesinskic322b432015-10-06 15:23:46 -0700293
294 // Initialize the z_stream for compression.
295 z_stream_ = std::unique_ptr<z_stream, void(*)(z_stream*)>(new z_stream(), DeleteZStream);
296
Colin Crossdf227762016-09-16 10:15:51 -0700297#pragma GCC diagnostic push
298#pragma GCC diagnostic ignored "-Wold-style-cast"
Adam Lesinskic322b432015-10-06 15:23:46 -0700299 int zerr = deflateInit2(z_stream_.get(), Z_BEST_COMPRESSION, Z_DEFLATED, -MAX_WBITS,
300 DEF_MEM_LEVEL, Z_DEFAULT_STRATEGY);
Colin Crossdf227762016-09-16 10:15:51 -0700301#pragma GCC diagnostic pop
302
Adam Lesinskic322b432015-10-06 15:23:46 -0700303 if (zerr != Z_OK) {
304 if (zerr == Z_VERSION_ERROR) {
305 ALOGE("Installed zlib is not compatible with linked version (%s)", ZLIB_VERSION);
306 return HandleError(kZlibError);
307 } else {
308 ALOGE("deflateInit2 failed (zerr=%d)", zerr);
309 return HandleError(kZlibError);
310 }
311 }
312
313 z_stream_->next_out = buffer_.data();
314 z_stream_->avail_out = buffer_.size();
315 return kNoError;
316}
317
Adam Lesinski622f3042015-10-05 18:16:18 -0700318int32_t ZipWriter::WriteBytes(const void* data, size_t len) {
319 if (state_ != State::kWritingEntry) {
320 return HandleError(kInvalidState);
321 }
322
Adam Lesinskic322b432015-10-06 15:23:46 -0700323 int32_t result = kNoError;
Adam Lesinski2e216de2017-03-16 13:23:51 -0700324 if (current_file_entry_.compression_method & kCompressDeflated) {
325 result = CompressBytes(&current_file_entry_, data, len);
Adam Lesinski622f3042015-10-05 18:16:18 -0700326 } else {
Adam Lesinski2e216de2017-03-16 13:23:51 -0700327 result = StoreBytes(&current_file_entry_, data, len);
Adam Lesinski622f3042015-10-05 18:16:18 -0700328 }
329
Adam Lesinskic322b432015-10-06 15:23:46 -0700330 if (result != kNoError) {
331 return result;
332 }
333
Adam Lesinski2e216de2017-03-16 13:23:51 -0700334 current_file_entry_.crc32 = crc32(current_file_entry_.crc32,
335 reinterpret_cast<const Bytef*>(data), len);
336 current_file_entry_.uncompressed_size += len;
Adam Lesinski622f3042015-10-05 18:16:18 -0700337 return kNoError;
338}
339
Adam Lesinski2e216de2017-03-16 13:23:51 -0700340int32_t ZipWriter::StoreBytes(FileEntry* file, const void* data, size_t len) {
341 CHECK(state_ == State::kWritingEntry);
Adam Lesinskic322b432015-10-06 15:23:46 -0700342
343 if (fwrite(data, 1, len, file_) != len) {
344 return HandleError(kIoError);
345 }
346 file->compressed_size += len;
347 current_offset_ += len;
348 return kNoError;
349}
350
Adam Lesinski2e216de2017-03-16 13:23:51 -0700351int32_t ZipWriter::CompressBytes(FileEntry* file, const void* data, size_t len) {
352 CHECK(state_ == State::kWritingEntry);
353 CHECK(z_stream_);
354 CHECK(z_stream_->next_out != nullptr);
355 CHECK(z_stream_->avail_out != 0);
Adam Lesinskic322b432015-10-06 15:23:46 -0700356
357 // Prepare the input.
358 z_stream_->next_in = reinterpret_cast<const uint8_t*>(data);
359 z_stream_->avail_in = len;
360
361 while (z_stream_->avail_in > 0) {
362 // We have more data to compress.
363 int zerr = deflate(z_stream_.get(), Z_NO_FLUSH);
364 if (zerr != Z_OK) {
365 return HandleError(kZlibError);
366 }
367
368 if (z_stream_->avail_out == 0) {
369 // The output is full, let's write it to disk.
Christopher Ferrisfcc081f2015-11-04 17:54:32 -0800370 size_t write_bytes = z_stream_->next_out - buffer_.data();
371 if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) {
Adam Lesinskic322b432015-10-06 15:23:46 -0700372 return HandleError(kIoError);
373 }
Christopher Ferrisfcc081f2015-11-04 17:54:32 -0800374 file->compressed_size += write_bytes;
375 current_offset_ += write_bytes;
Adam Lesinskic322b432015-10-06 15:23:46 -0700376
377 // Reset the output buffer for the next input.
378 z_stream_->next_out = buffer_.data();
379 z_stream_->avail_out = buffer_.size();
380 }
381 }
382 return kNoError;
383}
384
Adam Lesinski2e216de2017-03-16 13:23:51 -0700385int32_t ZipWriter::FlushCompressedBytes(FileEntry* file) {
386 CHECK(state_ == State::kWritingEntry);
387 CHECK(z_stream_);
388 CHECK(z_stream_->next_out != nullptr);
389 CHECK(z_stream_->avail_out != 0);
Adam Lesinskic322b432015-10-06 15:23:46 -0700390
Christopher Ferrisfcc081f2015-11-04 17:54:32 -0800391 // Keep deflating while there isn't enough space in the buffer to
392 // to complete the compress.
393 int zerr;
394 while ((zerr = deflate(z_stream_.get(), Z_FINISH)) == Z_OK) {
Adam Lesinski2e216de2017-03-16 13:23:51 -0700395 CHECK(z_stream_->avail_out == 0);
Christopher Ferrisfcc081f2015-11-04 17:54:32 -0800396 size_t write_bytes = z_stream_->next_out - buffer_.data();
397 if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) {
398 return HandleError(kIoError);
399 }
400 file->compressed_size += write_bytes;
401 current_offset_ += write_bytes;
402
403 z_stream_->next_out = buffer_.data();
404 z_stream_->avail_out = buffer_.size();
405 }
Adam Lesinskic322b432015-10-06 15:23:46 -0700406 if (zerr != Z_STREAM_END) {
407 return HandleError(kZlibError);
408 }
409
Christopher Ferrisfcc081f2015-11-04 17:54:32 -0800410 size_t write_bytes = z_stream_->next_out - buffer_.data();
411 if (write_bytes != 0) {
412 if (fwrite(buffer_.data(), 1, write_bytes, file_) != write_bytes) {
Adam Lesinskic322b432015-10-06 15:23:46 -0700413 return HandleError(kIoError);
414 }
Christopher Ferrisfcc081f2015-11-04 17:54:32 -0800415 file->compressed_size += write_bytes;
416 current_offset_ += write_bytes;
Adam Lesinskic322b432015-10-06 15:23:46 -0700417 }
418 z_stream_.reset();
419 return kNoError;
420}
421
Adam Lesinski622f3042015-10-05 18:16:18 -0700422int32_t ZipWriter::FinishEntry() {
423 if (state_ != State::kWritingEntry) {
424 return kInvalidState;
425 }
426
Adam Lesinski2e216de2017-03-16 13:23:51 -0700427 if (current_file_entry_.compression_method & kCompressDeflated) {
428 int32_t result = FlushCompressedBytes(&current_file_entry_);
Adam Lesinskic322b432015-10-06 15:23:46 -0700429 if (result != kNoError) {
430 return result;
431 }
432 }
433
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700434 if ((current_file_entry_.compression_method & kCompressDeflated) || !seekable_) {
435 // Some versions of ZIP don't allow STORED data to have a trailing DataDescriptor.
436 // If this file is not seekable, or if the data is compressed, write a DataDescriptor.
437 const uint32_t sig = DataDescriptor::kOptSignature;
438 if (fwrite(&sig, sizeof(sig), 1, file_) != 1) {
439 return HandleError(kIoError);
440 }
Adam Lesinski622f3042015-10-05 18:16:18 -0700441
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700442 DataDescriptor dd = {};
443 dd.crc32 = current_file_entry_.crc32;
444 dd.compressed_size = current_file_entry_.compressed_size;
445 dd.uncompressed_size = current_file_entry_.uncompressed_size;
446 if (fwrite(&dd, sizeof(dd), 1, file_) != 1) {
447 return HandleError(kIoError);
448 }
449 current_offset_ += sizeof(DataDescriptor::kOptSignature) + sizeof(dd);
450 } else {
451 // Seek back to the header and rewrite to include the size.
452 if (fseeko(file_, current_file_entry_.local_file_header_offset, SEEK_SET) != 0) {
453 return HandleError(kIoError);
454 }
455
456 LocalFileHeader header = {};
457 CopyFromFileEntry(current_file_entry_, false /*use_data_descriptor*/, &header);
458
459 if (fwrite(&header, sizeof(header), 1, file_) != 1) {
460 return HandleError(kIoError);
461 }
462
463 if (fseeko(file_, current_offset_, SEEK_SET) != 0) {
464 return HandleError(kIoError);
465 }
Adam Lesinski622f3042015-10-05 18:16:18 -0700466 }
467
Adam Lesinski2e216de2017-03-16 13:23:51 -0700468 files_.emplace_back(std::move(current_file_entry_));
Adam Lesinski622f3042015-10-05 18:16:18 -0700469 state_ = State::kWritingZip;
470 return kNoError;
471}
472
473int32_t ZipWriter::Finish() {
474 if (state_ != State::kWritingZip) {
475 return kInvalidState;
476 }
477
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700478 off_t startOfCdr = current_offset_;
Adam Lesinski2e216de2017-03-16 13:23:51 -0700479 for (FileEntry& file : files_) {
Adam Lesinski622f3042015-10-05 18:16:18 -0700480 CentralDirectoryRecord cdr = {};
481 cdr.record_signature = CentralDirectoryRecord::kSignature;
Adam Lesinski253cc062017-04-06 18:55:47 -0700482 if ((file.compression_method & kCompressDeflated) || !seekable_) {
483 cdr.gpb_flags |= kGPBDDFlagMask;
484 }
Adam Lesinski622f3042015-10-05 18:16:18 -0700485 cdr.compression_method = file.compression_method;
486 cdr.last_mod_time = file.last_mod_time;
487 cdr.last_mod_date = file.last_mod_date;
488 cdr.crc32 = file.crc32;
489 cdr.compressed_size = file.compressed_size;
490 cdr.uncompressed_size = file.uncompressed_size;
491 cdr.file_name_length = file.path.size();
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700492 cdr.local_file_header_offset = static_cast<uint32_t>(file.local_file_header_offset);
Adam Lesinski622f3042015-10-05 18:16:18 -0700493 if (fwrite(&cdr, sizeof(cdr), 1, file_) != 1) {
494 return HandleError(kIoError);
495 }
496
497 if (fwrite(file.path.data(), 1, file.path.size(), file_) != file.path.size()) {
498 return HandleError(kIoError);
499 }
500
501 current_offset_ += sizeof(cdr) + file.path.size();
502 }
503
504 EocdRecord er = {};
505 er.eocd_signature = EocdRecord::kSignature;
Adam Lesinskia8d71d62015-10-20 12:41:49 -0700506 er.disk_num = 0;
507 er.cd_start_disk = 0;
Adam Lesinski622f3042015-10-05 18:16:18 -0700508 er.num_records_on_disk = files_.size();
509 er.num_records = files_.size();
510 er.cd_size = current_offset_ - startOfCdr;
511 er.cd_start_offset = startOfCdr;
512
513 if (fwrite(&er, sizeof(er), 1, file_) != 1) {
514 return HandleError(kIoError);
515 }
516
Adam Lesinski2e216de2017-03-16 13:23:51 -0700517 current_offset_ += sizeof(er);
518
519 // Since we can BackUp() and potentially finish writing at an offset less than one we had
520 // already written at, we must truncate the file.
521
Adam Lesinskif61aaf42017-03-29 16:10:11 -0700522 if (ftruncate(fileno(file_), current_offset_) != 0) {
Adam Lesinski2e216de2017-03-16 13:23:51 -0700523 return HandleError(kIoError);
524 }
525
Adam Lesinski622f3042015-10-05 18:16:18 -0700526 if (fflush(file_) != 0) {
527 return HandleError(kIoError);
528 }
529
Adam Lesinski622f3042015-10-05 18:16:18 -0700530 state_ = State::kDone;
531 return kNoError;
532}