Allow non-fast-forward of remote tracking branches in default clone
[tgit.git] / archive-zip.c
blobf31b8ed8236c3fcad10ac9a84e2f508f11e1a6cf
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"
12 static int verbose;
13 static int zip_date;
14 static int zip_time;
16 static unsigned char *zip_dir;
17 static unsigned int zip_dir_size;
19 static unsigned int zip_offset;
20 static unsigned int zip_dir_offset;
21 static unsigned int zip_dir_entries;
23 #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
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];
37 unsigned char _end[1];
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];
58 unsigned char _end[1];
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];
70 unsigned char _end[1];
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.
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)
82 static void copy_le16(unsigned char *dest, unsigned int n)
84 dest[0] = 0xff & n;
85 dest[1] = 0xff & (n >> 010);
88 static void copy_le32(unsigned char *dest, unsigned int n)
90 dest[0] = 0xff & n;
91 dest[1] = 0xff & (n >> 010);
92 dest[2] = 0xff & (n >> 020);
93 dest[3] = 0xff & (n >> 030);
96 static void *zlib_deflate(void *data, unsigned long size,
97 unsigned long *compressed_size)
99 z_stream stream;
100 unsigned long maxsize;
101 void *buffer;
102 int result;
104 memset(&stream, 0, sizeof(stream));
105 deflateInit(&stream, zlib_compression_level);
106 maxsize = deflateBound(&stream, size);
107 buffer = xmalloc(maxsize);
109 stream.next_in = data;
110 stream.avail_in = size;
111 stream.next_out = buffer;
112 stream.avail_out = maxsize;
114 do {
115 result = deflate(&stream, Z_FINISH);
116 } while (result == Z_OK);
118 if (result != Z_STREAM_END) {
119 free(buffer);
120 return NULL;
123 deflateEnd(&stream);
124 *compressed_size = stream.total_out;
126 return buffer;
129 static char *construct_path(const char *base, int baselen,
130 const char *filename, int isdir, int *pathlen)
132 int filenamelen = strlen(filename);
133 int len = baselen + filenamelen;
134 char *path, *p;
136 if (isdir)
137 len++;
138 p = path = xmalloc(len + 1);
140 memcpy(p, base, baselen);
141 p += baselen;
142 memcpy(p, filename, filenamelen);
143 p += filenamelen;
144 if (isdir)
145 *p++ = '/';
146 *p = '\0';
148 *pathlen = len;
150 return path;
153 static int write_zip_entry(const unsigned char *sha1,
154 const char *base, int baselen,
155 const char *filename, unsigned mode, int stage)
157 struct zip_local_header header;
158 struct zip_dir_header dirent;
159 unsigned long attr2;
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;
170 char type[20];
171 void *buffer = NULL;
172 void *deflated = NULL;
174 crc = crc32(0, NULL, 0);
176 path = construct_path(base, baselen, filename, S_ISDIR(mode), &pathlen);
177 if (verbose)
178 fprintf(stderr, "%s\n", path);
179 if (pathlen > 0xffff) {
180 error("path too long (%d chars, SHA1: %s): %s", pathlen,
181 sha1_to_hex(sha1), path);
182 goto out;
185 if (S_ISDIR(mode)) {
186 method = 0;
187 attr2 = 16;
188 result = READ_TREE_RECURSIVE;
189 out = NULL;
190 uncompressed_size = 0;
191 compressed_size = 0;
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;
197 result = 0;
198 buffer = read_sha1_file(sha1, type, &size);
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;
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;
224 /* make sure we have enough free space in the dictionary */
225 direntsize = ZIP_DIR_HEADER_SIZE + pathlen;
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);
231 copy_le32(dirent.magic, 0x02014b50);
232 copy_le16(dirent.creator_version, S_ISLNK(mode) ? 0x0317 : 0);
233 copy_le16(dirent.version, 10);
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);
246 copy_le32(dirent.attr2, attr2);
247 copy_le32(dirent.offset, zip_offset);
248 memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
249 zip_dir_offset += ZIP_DIR_HEADER_SIZE;
250 memcpy(zip_dir + zip_dir_offset, path, pathlen);
251 zip_dir_offset += pathlen;
252 zip_dir_entries++;
254 copy_le32(header.magic, 0x04034b50);
255 copy_le16(header.version, 10);
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);
265 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
266 zip_offset += ZIP_LOCAL_HEADER_SIZE;
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;
274 out:
275 free(buffer);
276 free(deflated);
277 free(path);
279 return result;
282 static void write_zip_trailer(const unsigned char *sha1)
284 struct zip_dir_trailer trailer;
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);
295 write_or_die(1, zip_dir, zip_dir_offset);
296 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
297 if (sha1)
298 write_or_die(1, sha1_to_hex(sha1), 40);
301 static void dos_time(time_t *time, int *dos_date, int *dos_time)
303 struct tm *t = localtime(time);
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;
310 int write_zip_archive(struct archiver_args *args)
312 int plen = strlen(args->base);
314 dos_time(&args->time, &zip_date, &zip_time);
316 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
317 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
318 verbose = args->verbose;
320 if (args->base && plen > 0 && args->base[plen - 1] == '/') {
321 char *base = xstrdup(args->base);
322 int baselen = strlen(base);
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);
329 read_tree_recursive(args->tree, args->base, plen, 0,
330 args->pathspec, write_zip_entry);
331 write_zip_trailer(args->commit_sha1);
333 free(zip_dir);
335 return 0;
338 void *parse_extra_zip_args(int argc, const char **argv)
340 for (; argc > 0; argc--, argv++) {
341 const char *arg = argv[0];
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);
348 return NULL;