add context pointer to read_tree_recursive()
[git/git.git] / archive-zip.c
CommitLineData
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 12static int verbose;
e4fbbfe9
RS
13static int zip_date;
14static int zip_time;
8460b2fc 15static const struct commit *commit;
ac7fa277 16static size_t base_len;
e4fbbfe9
RS
17
18static unsigned char *zip_dir;
19static unsigned int zip_dir_size;
20
21static unsigned int zip_offset;
22static unsigned int zip_dir_offset;
23static unsigned int zip_dir_entries;
24
25#define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
26
27struct 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];
0ea865ce 39 unsigned char _end[1];
e4fbbfe9
RS
40};
41
42struct 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];
0ea865ce 60 unsigned char _end[1];
e4fbbfe9
RS
61};
62
63struct 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];
0ea865ce 72 unsigned char _end[1];
e4fbbfe9
RS
73};
74
0ea865ce
RS
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
e4fbbfe9
RS
84static void copy_le16(unsigned char *dest, unsigned int n)
85{
86 dest[0] = 0xff & n;
87 dest[1] = 0xff & (n >> 010);
88}
89
90static 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
98static 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
131static 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
671f0707
RS
155static int write_zip_entry(const unsigned char *sha1, const char *base,
156 int baselen, const char *filename, unsigned mode, int stage,
157 void *context)
e4fbbfe9
RS
158{
159 struct zip_local_header header;
160 struct zip_dir_header dirent;
62cdce17 161 unsigned long attr2;
e4fbbfe9
RS
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;
21666f1a 172 enum object_type type;
e4fbbfe9
RS
173 void *buffer = NULL;
174 void *deflated = NULL;
175
38f4d138 176 crc = crc32(0, NULL, 0);
e4fbbfe9
RS
177
178 path = construct_path(base, baselen, filename, S_ISDIR(mode), &pathlen);
008d896d
RS
179 if (is_archive_path_ignored(path + base_len))
180 return 0;
e0ffb248
JH
181 if (verbose)
182 fprintf(stderr, "%s\n", path);
e4fbbfe9
RS
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
302b9282 189 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
e4fbbfe9 190 method = 0;
62cdce17 191 attr2 = 16;
02851e0b 192 result = (S_ISDIR(mode) ? READ_TREE_RECURSIVE : 0);
e4fbbfe9
RS
193 out = NULL;
194 uncompressed_size = 0;
195 compressed_size = 0;
62cdce17
RS
196 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
197 method = 0;
76bf8d0e
DP
198 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
199 (mode & 0111) ? ((mode) << 16) : 0;
62cdce17
RS
200 if (S_ISREG(mode) && zlib_compression_level != 0)
201 method = 8;
e4fbbfe9 202 result = 0;
ac7fa277
RS
203 buffer = sha1_file_to_archive(path + base_len, sha1, mode,
204 &type, &size, commit);
e4fbbfe9
RS
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 */
0ea865ce 231 direntsize = ZIP_DIR_HEADER_SIZE + pathlen;
e4fbbfe9
RS
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);
76bf8d0e
DP
238 copy_le16(dirent.creator_version,
239 S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
cf72fb07 240 copy_le16(dirent.version, 10);
e4fbbfe9
RS
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);
62cdce17 253 copy_le32(dirent.attr2, attr2);
e4fbbfe9 254 copy_le32(dirent.offset, zip_offset);
0ea865ce
RS
255 memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
256 zip_dir_offset += ZIP_DIR_HEADER_SIZE;
e4fbbfe9
RS
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);
cf72fb07 262 copy_le16(header.version, 10);
e4fbbfe9
RS
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);
0ea865ce
RS
272 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
273 zip_offset += ZIP_LOCAL_HEADER_SIZE;
e4fbbfe9
RS
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
281out:
282 free(buffer);
283 free(deflated);
284 free(path);
285
286 return result;
287}
288
289static 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);
0ea865ce 303 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
e4fbbfe9
RS
304 if (sha1)
305 write_or_die(1, sha1_to_hex(sha1), 40);
306}
307
308static 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
ec06bff5
FBH
317int 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;
e0ffb248 325 verbose = args->verbose;
8460b2fc 326 commit = args->commit;
ac7fa277 327 base_len = args->base ? strlen(args->base) : 0;
ec06bff5
FBH
328
329 if (args->base && plen > 0 && args->base[plen - 1] == '/') {
326711c1 330 char *base = xstrdup(args->base);
ec06bff5
FBH
331 int baselen = strlen(base);
332
333 while (baselen > 0 && base[baselen - 1] == '/')
334 base[--baselen] = '\0';
671f0707
RS
335 write_zip_entry(args->tree->object.sha1, "", 0, base, 040777,
336 0, NULL);
ec06bff5
FBH
337 free(base);
338 }
339 read_tree_recursive(args->tree, args->base, plen, 0,
671f0707 340 args->pathspec, write_zip_entry, NULL);
ec06bff5
FBH
341 write_zip_trailer(args->commit_sha1);
342
343 free(zip_dir);
344
345 return 0;
346}
854c4168
RS
347
348void *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}