74e30f6205f41112dc2bafe9371a790aca55f70c
[git/git.git] / archive-zip.c
1 /*
2 * Copyright (c) 2006 Rene Scharfe
3 */
4 #include "cache.h"
5 #include "commit.h"
6 #include "blob.h"
7 #include "tree.h"
8 #include "quote.h"
9 #include "builtin.h"
10 #include "archive.h"
11
12 static int verbose;
13 static int zip_date;
14 static int zip_time;
15 static const struct commit *commit;
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];
38 unsigned char _end[1];
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];
59 unsigned char _end[1];
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];
71 unsigned char _end[1];
72 };
73
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
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;
160 unsigned long attr2;
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;
171 enum object_type type;
172 void *buffer = NULL;
173 void *deflated = NULL;
174
175 crc = crc32(0, NULL, 0);
176
177 path = construct_path(base, baselen, filename, S_ISDIR(mode), &pathlen);
178 if (verbose)
179 fprintf(stderr, "%s\n", path);
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
186 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
187 method = 0;
188 attr2 = 16;
189 result = (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
190 out = NULL;
191 uncompressed_size = 0;
192 compressed_size = 0;
193 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
194 method = 0;
195 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
196 (mode & 0111) ? ((mode) << 16) : 0;
197 if (S_ISREG(mode) && zlib_compression_level != 0)
198 method = 8;
199 result = 0;
200 buffer = sha1_file_to_archive(path, sha1, mode, &type, &size,
201 commit);
202 if (!buffer)
203 die("cannot read %s", sha1_to_hex(sha1));
204 crc = crc32(crc, buffer, size);
205 out = buffer;
206 uncompressed_size = size;
207 compressed_size = size;
208 } else {
209 error("unsupported file mode: 0%o (SHA1: %s)", mode,
210 sha1_to_hex(sha1));
211 goto out;
212 }
213
214 if (method == 8) {
215 deflated = zlib_deflate(buffer, size, &compressed_size);
216 if (deflated && compressed_size - 6 < size) {
217 /* ZLIB --> raw compressed data (see RFC 1950) */
218 /* CMF and FLG ... */
219 out = (unsigned char *)deflated + 2;
220 compressed_size -= 6; /* ... and ADLER32 */
221 } else {
222 method = 0;
223 compressed_size = size;
224 }
225 }
226
227 /* make sure we have enough free space in the dictionary */
228 direntsize = ZIP_DIR_HEADER_SIZE + pathlen;
229 while (zip_dir_size < zip_dir_offset + direntsize) {
230 zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
231 zip_dir = xrealloc(zip_dir, zip_dir_size);
232 }
233
234 copy_le32(dirent.magic, 0x02014b50);
235 copy_le16(dirent.creator_version,
236 S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
237 copy_le16(dirent.version, 10);
238 copy_le16(dirent.flags, 0);
239 copy_le16(dirent.compression_method, method);
240 copy_le16(dirent.mtime, zip_time);
241 copy_le16(dirent.mdate, zip_date);
242 copy_le32(dirent.crc32, crc);
243 copy_le32(dirent.compressed_size, compressed_size);
244 copy_le32(dirent.size, uncompressed_size);
245 copy_le16(dirent.filename_length, pathlen);
246 copy_le16(dirent.extra_length, 0);
247 copy_le16(dirent.comment_length, 0);
248 copy_le16(dirent.disk, 0);
249 copy_le16(dirent.attr1, 0);
250 copy_le32(dirent.attr2, attr2);
251 copy_le32(dirent.offset, zip_offset);
252 memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
253 zip_dir_offset += ZIP_DIR_HEADER_SIZE;
254 memcpy(zip_dir + zip_dir_offset, path, pathlen);
255 zip_dir_offset += pathlen;
256 zip_dir_entries++;
257
258 copy_le32(header.magic, 0x04034b50);
259 copy_le16(header.version, 10);
260 copy_le16(header.flags, 0);
261 copy_le16(header.compression_method, method);
262 copy_le16(header.mtime, zip_time);
263 copy_le16(header.mdate, zip_date);
264 copy_le32(header.crc32, crc);
265 copy_le32(header.compressed_size, compressed_size);
266 copy_le32(header.size, uncompressed_size);
267 copy_le16(header.filename_length, pathlen);
268 copy_le16(header.extra_length, 0);
269 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
270 zip_offset += ZIP_LOCAL_HEADER_SIZE;
271 write_or_die(1, path, pathlen);
272 zip_offset += pathlen;
273 if (compressed_size > 0) {
274 write_or_die(1, out, compressed_size);
275 zip_offset += compressed_size;
276 }
277
278 out:
279 free(buffer);
280 free(deflated);
281 free(path);
282
283 return result;
284 }
285
286 static void write_zip_trailer(const unsigned char *sha1)
287 {
288 struct zip_dir_trailer trailer;
289
290 copy_le32(trailer.magic, 0x06054b50);
291 copy_le16(trailer.disk, 0);
292 copy_le16(trailer.directory_start_disk, 0);
293 copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
294 copy_le16(trailer.entries, zip_dir_entries);
295 copy_le32(trailer.size, zip_dir_offset);
296 copy_le32(trailer.offset, zip_offset);
297 copy_le16(trailer.comment_length, sha1 ? 40 : 0);
298
299 write_or_die(1, zip_dir, zip_dir_offset);
300 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
301 if (sha1)
302 write_or_die(1, sha1_to_hex(sha1), 40);
303 }
304
305 static void dos_time(time_t *time, int *dos_date, int *dos_time)
306 {
307 struct tm *t = localtime(time);
308
309 *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
310 (t->tm_year + 1900 - 1980) * 512;
311 *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
312 }
313
314 int write_zip_archive(struct archiver_args *args)
315 {
316 int plen = strlen(args->base);
317
318 dos_time(&args->time, &zip_date, &zip_time);
319
320 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
321 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
322 verbose = args->verbose;
323 commit = args->commit;
324
325 if (args->base && plen > 0 && args->base[plen - 1] == '/') {
326 char *base = xstrdup(args->base);
327 int baselen = strlen(base);
328
329 while (baselen > 0 && base[baselen - 1] == '/')
330 base[--baselen] = '\0';
331 write_zip_entry(args->tree->object.sha1, "", 0, base, 040777, 0);
332 free(base);
333 }
334 read_tree_recursive(args->tree, args->base, plen, 0,
335 args->pathspec, write_zip_entry);
336 write_zip_trailer(args->commit_sha1);
337
338 free(zip_dir);
339
340 return 0;
341 }
342
343 void *parse_extra_zip_args(int argc, const char **argv)
344 {
345 for (; argc > 0; argc--, argv++) {
346 const char *arg = argv[0];
347
348 if (arg[0] == '-' && isdigit(arg[1]) && arg[2] == '\0')
349 zlib_compression_level = arg[1] - '0';
350 else
351 die("Unknown argument for zip format: %s", arg);
352 }
353 return NULL;
354 }