summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAdam Lesinski2017-03-16 15:23:51 -0500
committerAdam Lesinski2017-03-22 18:46:42 -0500
commit537713bace8975d02576225536ec55bd61f9ca91 (patch)
tree426a4efd55768e8085f760968365ed19c819e36c /include
parenta4bc98941af87ba95bb72b8be59e74ad5686e7d8 (diff)
downloadplatform-system-core-537713bace8975d02576225536ec55bd61f9ca91.tar.gz
platform-system-core-537713bace8975d02576225536ec55bd61f9ca91.tar.xz
platform-system-core-537713bace8975d02576225536ec55bd61f9ca91.zip
libziparchive: Add ability to backup in ZipWriter
Based on the compressed size of a file entry, the decision needs to be made to instead store the file uncompressed. This adds support to ZipWriter to backup its last file entry. The file is now always truncated when the EOCD is written out, to account for the case where a file entry is backed-up and the resulting file written is much smaller, leaving garbage data at the end of the file. This change also includes a rename of FileInfo -> FileEntry. This struct was private (now public), so it shouldn't affect any clients. Bug: 35461578 Test: make ziparchive-tests Change-Id: I23dc584406274ab7b8ce62b3fbc3562ca4c2603e
Diffstat (limited to 'include')
-rw-r--r--include/ziparchive/zip_writer.h56
1 files changed, 37 insertions, 19 deletions
diff --git a/include/ziparchive/zip_writer.h b/include/ziparchive/zip_writer.h
index 0b6ede45c..41ca2e1a3 100644
--- a/include/ziparchive/zip_writer.h
+++ b/include/ziparchive/zip_writer.h
@@ -17,15 +17,16 @@
17#ifndef LIBZIPARCHIVE_ZIPWRITER_H_ 17#ifndef LIBZIPARCHIVE_ZIPWRITER_H_
18#define LIBZIPARCHIVE_ZIPWRITER_H_ 18#define LIBZIPARCHIVE_ZIPWRITER_H_
19 19
20#include "android-base/macros.h"
21#include <utils/Compat.h>
22
23#include <cstdio> 20#include <cstdio>
24#include <ctime> 21#include <ctime>
22#include <zlib.h>
23
25#include <memory> 24#include <memory>
26#include <string> 25#include <string>
27#include <vector> 26#include <vector>
28#include <zlib.h> 27
28#include "android-base/macros.h"
29#include "utils/Compat.h"
29 30
30/** 31/**
31 * Writes a Zip file via a stateful interface. 32 * Writes a Zip file via a stateful interface.
@@ -63,6 +64,20 @@ public:
63 kAlign32 = 0x02, 64 kAlign32 = 0x02,
64 }; 65 };
65 66
67 /**
68 * A struct representing a zip file entry.
69 */
70 struct FileEntry {
71 std::string path;
72 uint16_t compression_method;
73 uint32_t crc32;
74 uint32_t compressed_size;
75 uint32_t uncompressed_size;
76 uint16_t last_mod_time;
77 uint16_t last_mod_date;
78 uint32_t local_file_header_offset;
79 };
80
66 static const char* ErrorCodeString(int32_t error_code); 81 static const char* ErrorCodeString(int32_t error_code);
67 82
68 /** 83 /**
@@ -122,6 +137,19 @@ public:
122 int32_t FinishEntry(); 137 int32_t FinishEntry();
123 138
124 /** 139 /**
140 * Discards the last-written entry. Can only be called after an entry has been written using
141 * FinishEntry().
142 * Returns 0 on success, and an error value < 0 on failure.
143 */
144 int32_t DiscardLastEntry();
145
146 /**
147 * Sets `out_entry` to the last entry written after a call to FinishEntry().
148 * Returns 0 on success, and an error value < 0 if no entries have been written.
149 */
150 int32_t GetLastEntry(FileEntry* out_entry);
151
152 /**
125 * Writes the Central Directory Headers and flushes the zip file stream. 153 * Writes the Central Directory Headers and flushes the zip file stream.
126 * Returns 0 on success, and an error value < 0 on failure. 154 * Returns 0 on success, and an error value < 0 on failure.
127 */ 155 */
@@ -130,22 +158,11 @@ public:
130private: 158private:
131 DISALLOW_COPY_AND_ASSIGN(ZipWriter); 159 DISALLOW_COPY_AND_ASSIGN(ZipWriter);
132 160
133 struct FileInfo {
134 std::string path;
135 uint16_t compression_method;
136 uint32_t crc32;
137 uint32_t compressed_size;
138 uint32_t uncompressed_size;
139 uint16_t last_mod_time;
140 uint16_t last_mod_date;
141 uint32_t local_file_header_offset;
142 };
143
144 int32_t HandleError(int32_t error_code); 161 int32_t HandleError(int32_t error_code);
145 int32_t PrepareDeflate(); 162 int32_t PrepareDeflate();
146 int32_t StoreBytes(FileInfo* file, const void* data, size_t len); 163 int32_t StoreBytes(FileEntry* file, const void* data, size_t len);
147 int32_t CompressBytes(FileInfo* file, const void* data, size_t len); 164 int32_t CompressBytes(FileEntry* file, const void* data, size_t len);
148 int32_t FlushCompressedBytes(FileInfo* file); 165 int32_t FlushCompressedBytes(FileEntry* file);
149 166
150 enum class State { 167 enum class State {
151 kWritingZip, 168 kWritingZip,
@@ -157,7 +174,8 @@ private:
157 FILE* file_; 174 FILE* file_;
158 off64_t current_offset_; 175 off64_t current_offset_;
159 State state_; 176 State state_;
160 std::vector<FileInfo> files_; 177 std::vector<FileEntry> files_;
178 FileEntry current_file_entry_;
161 179
162 std::unique_ptr<z_stream, void(*)(z_stream*)> z_stream_; 180 std::unique_ptr<z_stream, void(*)(z_stream*)> z_stream_;
163 std::vector<uint8_t> buffer_; 181 std::vector<uint8_t> buffer_;