]>
Commit | Line | Data |
---|---|---|
e4fbbfe9 RS |
1 | /* |
2 | * Copyright (c) 2006 Rene Scharfe | |
3 | */ | |
e4fbbfe9 RS |
4 | #include "cache.h" |
5 | #include "commit.h" | |
6 | #include "blob.h" | |
7 | #include "tree.h" | |
8 | #include "quote.h" | |
9 | #include "builtin.h" | |
ec06bff5 | 10 | #include "archive.h" |
e4fbbfe9 | 11 | |
e0ffb248 | 12 | static int verbose; |
e4fbbfe9 RS |
13 | static int zip_date; |
14 | static int zip_time; | |
15 | ||
16 | static unsigned char *zip_dir; | |
17 | static unsigned int zip_dir_size; | |
18 | ||
19 | static unsigned int zip_offset; | |
20 | static unsigned int zip_dir_offset; | |
21 | static unsigned int zip_dir_entries; | |
22 | ||
23 | #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024) | |
24 | ||
25 | struct zip_local_header { | |
26 | unsigned char magic[4]; | |
27 | unsigned char version[2]; | |
28 | unsigned char flags[2]; | |
29 | unsigned char compression_method[2]; | |
30 | unsigned char mtime[2]; | |
31 | unsigned char mdate[2]; | |
32 | unsigned char crc32[4]; | |
33 | unsigned char compressed_size[4]; | |
34 | unsigned char size[4]; | |
35 | unsigned char filename_length[2]; | |
36 | unsigned char extra_length[2]; | |
0ea865ce | 37 | unsigned char _end[1]; |
e4fbbfe9 RS |
38 | }; |
39 | ||
40 | struct zip_dir_header { | |
41 | unsigned char magic[4]; | |
42 | unsigned char creator_version[2]; | |
43 | unsigned char version[2]; | |
44 | unsigned char flags[2]; | |
45 | unsigned char compression_method[2]; | |
46 | unsigned char mtime[2]; | |
47 | unsigned char mdate[2]; | |
48 | unsigned char crc32[4]; | |
49 | unsigned char compressed_size[4]; | |
50 | unsigned char size[4]; | |
51 | unsigned char filename_length[2]; | |
52 | unsigned char extra_length[2]; | |
53 | unsigned char comment_length[2]; | |
54 | unsigned char disk[2]; | |
55 | unsigned char attr1[2]; | |
56 | unsigned char attr2[4]; | |
57 | unsigned char offset[4]; | |
0ea865ce | 58 | unsigned char _end[1]; |
e4fbbfe9 RS |
59 | }; |
60 | ||
61 | struct zip_dir_trailer { | |
62 | unsigned char magic[4]; | |
63 | unsigned char disk[2]; | |
64 | unsigned char directory_start_disk[2]; | |
65 | unsigned char entries_on_this_disk[2]; | |
66 | unsigned char entries[2]; | |
67 | unsigned char size[4]; | |
68 | unsigned char offset[4]; | |
69 | unsigned char comment_length[2]; | |
0ea865ce | 70 | unsigned char _end[1]; |
e4fbbfe9 RS |
71 | }; |
72 | ||
0ea865ce RS |
73 | /* |
74 | * On ARM, padding is added at the end of the struct, so a simple | |
75 | * sizeof(struct ...) reports two bytes more than the payload size | |
76 | * we're interested in. | |
77 | */ | |
78 | #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end) | |
79 | #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end) | |
80 | #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end) | |
81 | ||
e4fbbfe9 RS |
82 | static void copy_le16(unsigned char *dest, unsigned int n) |
83 | { | |
84 | dest[0] = 0xff & n; | |
85 | dest[1] = 0xff & (n >> 010); | |
86 | } | |
87 | ||
88 | static void copy_le32(unsigned char *dest, unsigned int n) | |
89 | { | |
90 | dest[0] = 0xff & n; | |
91 | dest[1] = 0xff & (n >> 010); | |
92 | dest[2] = 0xff & (n >> 020); | |
93 | dest[3] = 0xff & (n >> 030); | |
94 | } | |
95 | ||
96 | static void *zlib_deflate(void *data, unsigned long size, | |
97 | unsigned long *compressed_size) | |
98 | { | |
99 | z_stream stream; | |
100 | unsigned long maxsize; | |
101 | void *buffer; | |
102 | int result; | |
103 | ||
104 | memset(&stream, 0, sizeof(stream)); | |
105 | deflateInit(&stream, zlib_compression_level); | |
106 | maxsize = deflateBound(&stream, size); | |
107 | buffer = xmalloc(maxsize); | |
108 | ||
109 | stream.next_in = data; | |
110 | stream.avail_in = size; | |
111 | stream.next_out = buffer; | |
112 | stream.avail_out = maxsize; | |
113 | ||
114 | do { | |
115 | result = deflate(&stream, Z_FINISH); | |
116 | } while (result == Z_OK); | |
117 | ||
118 | if (result != Z_STREAM_END) { | |
119 | free(buffer); | |
120 | return NULL; | |
121 | } | |
122 | ||
123 | deflateEnd(&stream); | |
124 | *compressed_size = stream.total_out; | |
125 | ||
126 | return buffer; | |
127 | } | |
128 | ||
129 | static char *construct_path(const char *base, int baselen, | |
130 | const char *filename, int isdir, int *pathlen) | |
131 | { | |
132 | int filenamelen = strlen(filename); | |
133 | int len = baselen + filenamelen; | |
134 | char *path, *p; | |
135 | ||
136 | if (isdir) | |
137 | len++; | |
138 | p = path = xmalloc(len + 1); | |
139 | ||
140 | memcpy(p, base, baselen); | |
141 | p += baselen; | |
142 | memcpy(p, filename, filenamelen); | |
143 | p += filenamelen; | |
144 | if (isdir) | |
145 | *p++ = '/'; | |
146 | *p = '\0'; | |
147 | ||
148 | *pathlen = len; | |
149 | ||
150 | return path; | |
151 | } | |
152 | ||
153 | static int write_zip_entry(const unsigned char *sha1, | |
154 | const char *base, int baselen, | |
155 | const char *filename, unsigned mode, int stage) | |
156 | { | |
157 | struct zip_local_header header; | |
158 | struct zip_dir_header dirent; | |
62cdce17 | 159 | unsigned long attr2; |
e4fbbfe9 RS |
160 | unsigned long compressed_size; |
161 | unsigned long uncompressed_size; | |
162 | unsigned long crc; | |
163 | unsigned long direntsize; | |
164 | unsigned long size; | |
165 | int method; | |
166 | int result = -1; | |
167 | int pathlen; | |
168 | unsigned char *out; | |
169 | char *path; | |
21666f1a | 170 | enum object_type type; |
e4fbbfe9 RS |
171 | void *buffer = NULL; |
172 | void *deflated = NULL; | |
173 | ||
38f4d138 | 174 | crc = crc32(0, NULL, 0); |
e4fbbfe9 RS |
175 | |
176 | path = construct_path(base, baselen, filename, S_ISDIR(mode), &pathlen); | |
e0ffb248 JH |
177 | if (verbose) |
178 | fprintf(stderr, "%s\n", path); | |
e4fbbfe9 RS |
179 | if (pathlen > 0xffff) { |
180 | error("path too long (%d chars, SHA1: %s): %s", pathlen, | |
181 | sha1_to_hex(sha1), path); | |
182 | goto out; | |
183 | } | |
184 | ||
185 | if (S_ISDIR(mode)) { | |
186 | method = 0; | |
62cdce17 | 187 | attr2 = 16; |
e4fbbfe9 RS |
188 | result = READ_TREE_RECURSIVE; |
189 | out = NULL; | |
190 | uncompressed_size = 0; | |
191 | compressed_size = 0; | |
62cdce17 RS |
192 | } else if (S_ISREG(mode) || S_ISLNK(mode)) { |
193 | method = 0; | |
194 | attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) : 0; | |
195 | if (S_ISREG(mode) && zlib_compression_level != 0) | |
196 | method = 8; | |
e4fbbfe9 | 197 | result = 0; |
21666f1a | 198 | buffer = read_sha1_file(sha1, &type, &size); |
e4fbbfe9 RS |
199 | if (!buffer) |
200 | die("cannot read %s", sha1_to_hex(sha1)); | |
201 | crc = crc32(crc, buffer, size); | |
202 | out = buffer; | |
203 | uncompressed_size = size; | |
204 | compressed_size = size; | |
205 | } else { | |
206 | error("unsupported file mode: 0%o (SHA1: %s)", mode, | |
207 | sha1_to_hex(sha1)); | |
208 | goto out; | |
209 | } | |
210 | ||
211 | if (method == 8) { | |
212 | deflated = zlib_deflate(buffer, size, &compressed_size); | |
213 | if (deflated && compressed_size - 6 < size) { | |
214 | /* ZLIB --> raw compressed data (see RFC 1950) */ | |
215 | /* CMF and FLG ... */ | |
216 | out = (unsigned char *)deflated + 2; | |
217 | compressed_size -= 6; /* ... and ADLER32 */ | |
218 | } else { | |
219 | method = 0; | |
220 | compressed_size = size; | |
221 | } | |
222 | } | |
223 | ||
224 | /* make sure we have enough free space in the dictionary */ | |
0ea865ce | 225 | direntsize = ZIP_DIR_HEADER_SIZE + pathlen; |
e4fbbfe9 RS |
226 | while (zip_dir_size < zip_dir_offset + direntsize) { |
227 | zip_dir_size += ZIP_DIRECTORY_MIN_SIZE; | |
228 | zip_dir = xrealloc(zip_dir, zip_dir_size); | |
229 | } | |
230 | ||
231 | copy_le32(dirent.magic, 0x02014b50); | |
62cdce17 | 232 | copy_le16(dirent.creator_version, S_ISLNK(mode) ? 0x0317 : 0); |
cf72fb07 | 233 | copy_le16(dirent.version, 10); |
e4fbbfe9 RS |
234 | copy_le16(dirent.flags, 0); |
235 | copy_le16(dirent.compression_method, method); | |
236 | copy_le16(dirent.mtime, zip_time); | |
237 | copy_le16(dirent.mdate, zip_date); | |
238 | copy_le32(dirent.crc32, crc); | |
239 | copy_le32(dirent.compressed_size, compressed_size); | |
240 | copy_le32(dirent.size, uncompressed_size); | |
241 | copy_le16(dirent.filename_length, pathlen); | |
242 | copy_le16(dirent.extra_length, 0); | |
243 | copy_le16(dirent.comment_length, 0); | |
244 | copy_le16(dirent.disk, 0); | |
245 | copy_le16(dirent.attr1, 0); | |
62cdce17 | 246 | copy_le32(dirent.attr2, attr2); |
e4fbbfe9 | 247 | copy_le32(dirent.offset, zip_offset); |
0ea865ce RS |
248 | memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE); |
249 | zip_dir_offset += ZIP_DIR_HEADER_SIZE; | |
e4fbbfe9 RS |
250 | memcpy(zip_dir + zip_dir_offset, path, pathlen); |
251 | zip_dir_offset += pathlen; | |
252 | zip_dir_entries++; | |
253 | ||
254 | copy_le32(header.magic, 0x04034b50); | |
cf72fb07 | 255 | copy_le16(header.version, 10); |
e4fbbfe9 RS |
256 | copy_le16(header.flags, 0); |
257 | copy_le16(header.compression_method, method); | |
258 | copy_le16(header.mtime, zip_time); | |
259 | copy_le16(header.mdate, zip_date); | |
260 | copy_le32(header.crc32, crc); | |
261 | copy_le32(header.compressed_size, compressed_size); | |
262 | copy_le32(header.size, uncompressed_size); | |
263 | copy_le16(header.filename_length, pathlen); | |
264 | copy_le16(header.extra_length, 0); | |
0ea865ce RS |
265 | write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE); |
266 | zip_offset += ZIP_LOCAL_HEADER_SIZE; | |
e4fbbfe9 RS |
267 | write_or_die(1, path, pathlen); |
268 | zip_offset += pathlen; | |
269 | if (compressed_size > 0) { | |
270 | write_or_die(1, out, compressed_size); | |
271 | zip_offset += compressed_size; | |
272 | } | |
273 | ||
274 | out: | |
275 | free(buffer); | |
276 | free(deflated); | |
277 | free(path); | |
278 | ||
279 | return result; | |
280 | } | |
281 | ||
282 | static void write_zip_trailer(const unsigned char *sha1) | |
283 | { | |
284 | struct zip_dir_trailer trailer; | |
285 | ||
286 | copy_le32(trailer.magic, 0x06054b50); | |
287 | copy_le16(trailer.disk, 0); | |
288 | copy_le16(trailer.directory_start_disk, 0); | |
289 | copy_le16(trailer.entries_on_this_disk, zip_dir_entries); | |
290 | copy_le16(trailer.entries, zip_dir_entries); | |
291 | copy_le32(trailer.size, zip_dir_offset); | |
292 | copy_le32(trailer.offset, zip_offset); | |
293 | copy_le16(trailer.comment_length, sha1 ? 40 : 0); | |
294 | ||
295 | write_or_die(1, zip_dir, zip_dir_offset); | |
0ea865ce | 296 | write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE); |
e4fbbfe9 RS |
297 | if (sha1) |
298 | write_or_die(1, sha1_to_hex(sha1), 40); | |
299 | } | |
300 | ||
301 | static void dos_time(time_t *time, int *dos_date, int *dos_time) | |
302 | { | |
303 | struct tm *t = localtime(time); | |
304 | ||
305 | *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 + | |
306 | (t->tm_year + 1900 - 1980) * 512; | |
307 | *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048; | |
308 | } | |
309 | ||
ec06bff5 FBH |
310 | int write_zip_archive(struct archiver_args *args) |
311 | { | |
312 | int plen = strlen(args->base); | |
313 | ||
314 | dos_time(&args->time, &zip_date, &zip_time); | |
315 | ||
316 | zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE); | |
317 | zip_dir_size = ZIP_DIRECTORY_MIN_SIZE; | |
e0ffb248 | 318 | verbose = args->verbose; |
ec06bff5 FBH |
319 | |
320 | if (args->base && plen > 0 && args->base[plen - 1] == '/') { | |
326711c1 | 321 | char *base = xstrdup(args->base); |
ec06bff5 FBH |
322 | int baselen = strlen(base); |
323 | ||
324 | while (baselen > 0 && base[baselen - 1] == '/') | |
325 | base[--baselen] = '\0'; | |
326 | write_zip_entry(args->tree->object.sha1, "", 0, base, 040777, 0); | |
327 | free(base); | |
328 | } | |
329 | read_tree_recursive(args->tree, args->base, plen, 0, | |
330 | args->pathspec, write_zip_entry); | |
331 | write_zip_trailer(args->commit_sha1); | |
332 | ||
333 | free(zip_dir); | |
334 | ||
335 | return 0; | |
336 | } | |
854c4168 RS |
337 | |
338 | void *parse_extra_zip_args(int argc, const char **argv) | |
339 | { | |
340 | for (; argc > 0; argc--, argv++) { | |
341 | const char *arg = argv[0]; | |
342 | ||
343 | if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0') | |
344 | zlib_compression_level = arg[1] - '0'; | |
345 | else | |
346 | die("Unknown argument for zip format: %s", arg); | |
347 | } | |
348 | return NULL; | |
349 | } |