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