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