Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 497 lines (427 sloc) 13.069 kb
e4fbbfe Add git-zip-tree
Rene Scharfe authored
1 /*
2 * Copyright (c) 2006 Rene Scharfe
3 */
4 #include "cache.h"
ec06bff git-archive: wire up ZIP format.
Franck Bui-Huu authored
5 #include "archive.h"
2158f88 archive-zip: streaming for stored files
René Scharfe authored
6 #include "streaming.h"
88182ba archive-zip: support UTF-8 paths
René Scharfe authored
7 #include "utf8.h"
4aff646 archive-zip: mark text files in archives
René Scharfe authored
8 #include "userdiff.h"
9 #include "xdiff-interface.h"
e4fbbfe Add git-zip-tree
Rene Scharfe authored
10
11 static int zip_date;
12 static int zip_time;
13
14 static unsigned char *zip_dir;
15 static unsigned int zip_dir_size;
16
17 static unsigned int zip_offset;
18 static unsigned int zip_dir_offset;
19 static unsigned int zip_dir_entries;
20
21 #define ZIP_DIRECTORY_MIN_SIZE (1024 * 1024)
88182ba archive-zip: support UTF-8 paths
René Scharfe authored
22 #define ZIP_STREAM (1 << 3)
23 #define ZIP_UTF8 (1 << 11)
e4fbbfe Add git-zip-tree
Rene Scharfe authored
24
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];
0ea865c archive-zip: don't use sizeof(struct ...)
René Scharfe authored
37 unsigned char _end[1];
e4fbbfe Add git-zip-tree
Rene Scharfe authored
38 };
39
2158f88 archive-zip: streaming for stored files
René Scharfe authored
40 struct zip_data_desc {
41 unsigned char magic[4];
42 unsigned char crc32[4];
43 unsigned char compressed_size[4];
44 unsigned char size[4];
45 unsigned char _end[1];
46 };
47
e4fbbfe Add git-zip-tree
Rene Scharfe authored
48 struct zip_dir_header {
49 unsigned char magic[4];
50 unsigned char creator_version[2];
51 unsigned char version[2];
52 unsigned char flags[2];
53 unsigned char compression_method[2];
54 unsigned char mtime[2];
55 unsigned char mdate[2];
56 unsigned char crc32[4];
57 unsigned char compressed_size[4];
58 unsigned char size[4];
59 unsigned char filename_length[2];
60 unsigned char extra_length[2];
61 unsigned char comment_length[2];
62 unsigned char disk[2];
63 unsigned char attr1[2];
64 unsigned char attr2[4];
65 unsigned char offset[4];
0ea865c archive-zip: don't use sizeof(struct ...)
René Scharfe authored
66 unsigned char _end[1];
e4fbbfe Add git-zip-tree
Rene Scharfe authored
67 };
68
69 struct zip_dir_trailer {
70 unsigned char magic[4];
71 unsigned char disk[2];
72 unsigned char directory_start_disk[2];
73 unsigned char entries_on_this_disk[2];
74 unsigned char entries[2];
75 unsigned char size[4];
76 unsigned char offset[4];
77 unsigned char comment_length[2];
0ea865c archive-zip: don't use sizeof(struct ...)
René Scharfe authored
78 unsigned char _end[1];
e4fbbfe Add git-zip-tree
Rene Scharfe authored
79 };
80
227bf59 archive-zip: write extended timestamp
René Scharfe authored
81 struct zip_extra_mtime {
82 unsigned char magic[2];
83 unsigned char extra_size[2];
84 unsigned char flags[1];
85 unsigned char mtime[4];
86 unsigned char _end[1];
87 };
88
0ea865c archive-zip: don't use sizeof(struct ...)
René Scharfe authored
89 /*
90 * On ARM, padding is added at the end of the struct, so a simple
91 * sizeof(struct ...) reports two bytes more than the payload size
92 * we're interested in.
93 */
94 #define ZIP_LOCAL_HEADER_SIZE offsetof(struct zip_local_header, _end)
2158f88 archive-zip: streaming for stored files
René Scharfe authored
95 #define ZIP_DATA_DESC_SIZE offsetof(struct zip_data_desc, _end)
0ea865c archive-zip: don't use sizeof(struct ...)
René Scharfe authored
96 #define ZIP_DIR_HEADER_SIZE offsetof(struct zip_dir_header, _end)
97 #define ZIP_DIR_TRAILER_SIZE offsetof(struct zip_dir_trailer, _end)
227bf59 archive-zip: write extended timestamp
René Scharfe authored
98 #define ZIP_EXTRA_MTIME_SIZE offsetof(struct zip_extra_mtime, _end)
99 #define ZIP_EXTRA_MTIME_PAYLOAD_SIZE \
100 (ZIP_EXTRA_MTIME_SIZE - offsetof(struct zip_extra_mtime, flags))
0ea865c archive-zip: don't use sizeof(struct ...)
René Scharfe authored
101
e4fbbfe Add git-zip-tree
Rene Scharfe authored
102 static void copy_le16(unsigned char *dest, unsigned int n)
103 {
104 dest[0] = 0xff & n;
105 dest[1] = 0xff & (n >> 010);
106 }
107
108 static void copy_le32(unsigned char *dest, unsigned int n)
109 {
110 dest[0] = 0xff & n;
111 dest[1] = 0xff & (n >> 010);
112 dest[2] = 0xff & (n >> 020);
113 dest[3] = 0xff & (n >> 030);
114 }
115
c3c2e1a archive-zip: use deflateInit2() to ask for raw compressed data
René Scharfe authored
116 static void *zlib_deflate_raw(void *data, unsigned long size,
117 int compression_level,
118 unsigned long *compressed_size)
e4fbbfe Add git-zip-tree
Rene Scharfe authored
119 {
ef49a7a @gitster zlib: zlib can only process 4GB at a time
gitster authored
120 git_zstream stream;
e4fbbfe Add git-zip-tree
Rene Scharfe authored
121 unsigned long maxsize;
122 void *buffer;
123 int result;
124
c3c2e1a archive-zip: use deflateInit2() to ask for raw compressed data
René Scharfe authored
125 git_deflate_init_raw(&stream, compression_level);
225a6f1 @gitster zlib: wrap deflateBound() too
gitster authored
126 maxsize = git_deflate_bound(&stream, size);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
127 buffer = xmalloc(maxsize);
128
129 stream.next_in = data;
130 stream.avail_in = size;
131 stream.next_out = buffer;
132 stream.avail_out = maxsize;
133
134 do {
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored
135 result = git_deflate(&stream, Z_FINISH);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
136 } while (result == Z_OK);
137
138 if (result != Z_STREAM_END) {
139 free(buffer);
140 return NULL;
141 }
142
55bb5c9 @gitster zlib: wrap deflate side of the API
gitster authored
143 git_deflate_end(&stream);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
144 *compressed_size = stream.total_out;
145
146 return buffer;
147 }
148
2158f88 archive-zip: streaming for stored files
René Scharfe authored
149 static void write_zip_data_desc(unsigned long size,
150 unsigned long compressed_size,
151 unsigned long crc)
152 {
153 struct zip_data_desc trailer;
154
155 copy_le32(trailer.magic, 0x08074b50);
156 copy_le32(trailer.crc32, crc);
157 copy_le32(trailer.compressed_size, compressed_size);
158 copy_le32(trailer.size, size);
159 write_or_die(1, &trailer, ZIP_DATA_DESC_SIZE);
160 }
161
ebf5374 archive-zip: factor out helpers for writing sizes and CRC
René Scharfe authored
162 static void set_zip_dir_data_desc(struct zip_dir_header *header,
163 unsigned long size,
164 unsigned long compressed_size,
165 unsigned long crc)
166 {
167 copy_le32(header->crc32, crc);
168 copy_le32(header->compressed_size, compressed_size);
169 copy_le32(header->size, size);
170 }
171
172 static void set_zip_header_data_desc(struct zip_local_header *header,
173 unsigned long size,
174 unsigned long compressed_size,
175 unsigned long crc)
176 {
177 copy_le32(header->crc32, crc);
178 copy_le32(header->compressed_size, compressed_size);
179 copy_le32(header->size, size);
180 }
181
88182ba archive-zip: support UTF-8 paths
René Scharfe authored
182 static int has_only_ascii(const char *s)
183 {
184 for (;;) {
185 int c = *s++;
186 if (c == '\0')
187 return 1;
188 if (!isascii(c))
189 return 0;
190 }
191 }
192
4aff646 archive-zip: mark text files in archives
René Scharfe authored
193 static int entry_is_binary(const char *path, const void *buffer, size_t size)
194 {
195 struct userdiff_driver *driver = userdiff_find_by_path(path);
196 if (!driver)
197 driver = userdiff_find_by_name("default");
198 if (driver->binary != -1)
199 return driver->binary;
200 return buffer_is_binary(buffer, size);
201 }
202
2158f88 archive-zip: streaming for stored files
René Scharfe authored
203 #define STREAM_BUFFER_SIZE (1024 * 16)
204
562e25a archive: centralize archive entry writing
René Scharfe authored
205 static int write_zip_entry(struct archiver_args *args,
9cb513b @pclouds archive: delegate blob reading to backend
pclouds authored
206 const unsigned char *sha1,
207 const char *path, size_t pathlen,
208 unsigned int mode)
e4fbbfe Add git-zip-tree
Rene Scharfe authored
209 {
210 struct zip_local_header header;
211 struct zip_dir_header dirent;
227bf59 archive-zip: write extended timestamp
René Scharfe authored
212 struct zip_extra_mtime extra;
bb52d22 @gitster Revert "archive-zip: support UTF-8 paths"
gitster authored
213 unsigned long attr2;
e4fbbfe Add git-zip-tree
Rene Scharfe authored
214 unsigned long compressed_size;
215 unsigned long crc;
216 unsigned long direntsize;
217 int method;
218 unsigned char *out;
219 void *deflated = NULL;
9cb513b @pclouds archive: delegate blob reading to backend
pclouds authored
220 void *buffer;
2158f88 archive-zip: streaming for stored files
René Scharfe authored
221 struct git_istream *stream = NULL;
222 unsigned long flags = 0;
9cb513b @pclouds archive: delegate blob reading to backend
pclouds authored
223 unsigned long size;
4aff646 archive-zip: mark text files in archives
René Scharfe authored
224 int is_binary = -1;
225 const char *path_without_prefix = path + args->baselen;
e4fbbfe Add git-zip-tree
Rene Scharfe authored
226
38f4d13 sparse fix: Using plain integer as NULL pointer
Rene Scharfe authored
227 crc = crc32(0, NULL, 0);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
228
88182ba archive-zip: support UTF-8 paths
René Scharfe authored
229 if (!has_only_ascii(path)) {
230 if (is_utf8(path))
231 flags |= ZIP_UTF8;
232 else
233 warning("Path is not valid UTF-8: %s", path);
234 }
235
e4fbbfe Add git-zip-tree
Rene Scharfe authored
236 if (pathlen > 0xffff) {
562e25a archive: centralize archive entry writing
René Scharfe authored
237 return error("path too long (%d chars, SHA1: %s): %s",
238 (int)pathlen, sha1_to_hex(sha1), path);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
239 }
240
302b928 @tali rename dirlink to gitlink.
tali authored
241 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
e4fbbfe Add git-zip-tree
Rene Scharfe authored
242 method = 0;
62cdce1 git-archive --format=zip: add symlink support
Rene Scharfe authored
243 attr2 = 16;
e4fbbfe Add git-zip-tree
Rene Scharfe authored
244 out = NULL;
60df6bd archive-zip: remove uncompressed_size
René Scharfe authored
245 size = 0;
e4fbbfe Add git-zip-tree
Rene Scharfe authored
246 compressed_size = 0;
9cb513b @pclouds archive: delegate blob reading to backend
pclouds authored
247 buffer = NULL;
62cdce1 git-archive --format=zip: add symlink support
Rene Scharfe authored
248 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
2158f88 archive-zip: streaming for stored files
René Scharfe authored
249 enum object_type type = sha1_object_info(sha1, &size);
9cb513b @pclouds archive: delegate blob reading to backend
pclouds authored
250
62cdce1 git-archive --format=zip: add symlink support
Rene Scharfe authored
251 method = 0;
bb52d22 @gitster Revert "archive-zip: support UTF-8 paths"
gitster authored
252 attr2 = S_ISLNK(mode) ? ((mode | 0777) << 16) :
253 (mode & 0111) ? ((mode) << 16) : 0;
2158f88 archive-zip: streaming for stored files
René Scharfe authored
254 if (S_ISREG(mode) && args->compression_level != 0 && size > 0)
62cdce1 git-archive --format=zip: add symlink support
Rene Scharfe authored
255 method = 8;
2158f88 archive-zip: streaming for stored files
René Scharfe authored
256
257 if (S_ISREG(mode) && type == OBJ_BLOB && !args->convert &&
c743c21 archive-zip: streaming for deflated files
René Scharfe authored
258 size > big_file_threshold) {
2158f88 archive-zip: streaming for stored files
René Scharfe authored
259 stream = open_istream(sha1, &type, &size, NULL);
260 if (!stream)
261 return error("cannot stream blob %s",
262 sha1_to_hex(sha1));
263 flags |= ZIP_STREAM;
264 out = buffer = NULL;
265 } else {
266 buffer = sha1_file_to_archive(args, path, sha1, mode,
267 &type, &size);
268 if (!buffer)
269 return error("cannot read %s",
270 sha1_to_hex(sha1));
271 crc = crc32(crc, buffer, size);
4aff646 archive-zip: mark text files in archives
René Scharfe authored
272 is_binary = entry_is_binary(path_without_prefix,
273 buffer, size);
2158f88 archive-zip: streaming for stored files
René Scharfe authored
274 out = buffer;
275 }
d3c1472 archive-zip: fix compressed size for stored export-subst files
René Scharfe authored
276 compressed_size = (method == 0) ? size : 0;
e4fbbfe Add git-zip-tree
Rene Scharfe authored
277 } else {
562e25a archive: centralize archive entry writing
René Scharfe authored
278 return error("unsupported file mode: 0%o (SHA1: %s)", mode,
279 sha1_to_hex(sha1));
e4fbbfe Add git-zip-tree
Rene Scharfe authored
280 }
281
2158f88 archive-zip: streaming for stored files
René Scharfe authored
282 if (buffer && method == 8) {
c3c2e1a archive-zip: use deflateInit2() to ask for raw compressed data
René Scharfe authored
283 out = deflated = zlib_deflate_raw(buffer, size,
284 args->compression_level,
285 &compressed_size);
286 if (!out || compressed_size >= size) {
287 out = buffer;
e4fbbfe Add git-zip-tree
Rene Scharfe authored
288 method = 0;
289 compressed_size = size;
290 }
291 }
292
227bf59 archive-zip: write extended timestamp
René Scharfe authored
293 copy_le16(extra.magic, 0x5455);
294 copy_le16(extra.extra_size, ZIP_EXTRA_MTIME_PAYLOAD_SIZE);
295 extra.flags[0] = 1; /* just mtime */
296 copy_le32(extra.mtime, args->time);
297
e4fbbfe Add git-zip-tree
Rene Scharfe authored
298 /* make sure we have enough free space in the dictionary */
227bf59 archive-zip: write extended timestamp
René Scharfe authored
299 direntsize = ZIP_DIR_HEADER_SIZE + pathlen + ZIP_EXTRA_MTIME_SIZE;
e4fbbfe Add git-zip-tree
Rene Scharfe authored
300 while (zip_dir_size < zip_dir_offset + direntsize) {
301 zip_dir_size += ZIP_DIRECTORY_MIN_SIZE;
302 zip_dir = xrealloc(zip_dir, zip_dir_size);
303 }
304
305 copy_le32(dirent.magic, 0x02014b50);
bb52d22 @gitster Revert "archive-zip: support UTF-8 paths"
gitster authored
306 copy_le16(dirent.creator_version,
307 S_ISLNK(mode) || (S_ISREG(mode) && (mode & 0111)) ? 0x0317 : 0);
cf72fb0 git-archive --format=zip: use default version ID
Rene Scharfe authored
308 copy_le16(dirent.version, 10);
2158f88 archive-zip: streaming for stored files
René Scharfe authored
309 copy_le16(dirent.flags, flags);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
310 copy_le16(dirent.compression_method, method);
311 copy_le16(dirent.mtime, zip_time);
312 copy_le16(dirent.mdate, zip_date);
ebf5374 archive-zip: factor out helpers for writing sizes and CRC
René Scharfe authored
313 set_zip_dir_data_desc(&dirent, size, compressed_size, crc);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
314 copy_le16(dirent.filename_length, pathlen);
227bf59 archive-zip: write extended timestamp
René Scharfe authored
315 copy_le16(dirent.extra_length, ZIP_EXTRA_MTIME_SIZE);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
316 copy_le16(dirent.comment_length, 0);
317 copy_le16(dirent.disk, 0);
62cdce1 git-archive --format=zip: add symlink support
Rene Scharfe authored
318 copy_le32(dirent.attr2, attr2);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
319 copy_le32(dirent.offset, zip_offset);
320
321 copy_le32(header.magic, 0x04034b50);
cf72fb0 git-archive --format=zip: use default version ID
Rene Scharfe authored
322 copy_le16(header.version, 10);
2158f88 archive-zip: streaming for stored files
René Scharfe authored
323 copy_le16(header.flags, flags);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
324 copy_le16(header.compression_method, method);
325 copy_le16(header.mtime, zip_time);
326 copy_le16(header.mdate, zip_date);
5ea2c84 archive-zip: write uncompressed size into header even with streaming
René Scharfe authored
327 set_zip_header_data_desc(&header, size, compressed_size, crc);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
328 copy_le16(header.filename_length, pathlen);
227bf59 archive-zip: write extended timestamp
René Scharfe authored
329 copy_le16(header.extra_length, ZIP_EXTRA_MTIME_SIZE);
0ea865c archive-zip: don't use sizeof(struct ...)
René Scharfe authored
330 write_or_die(1, &header, ZIP_LOCAL_HEADER_SIZE);
331 zip_offset += ZIP_LOCAL_HEADER_SIZE;
e4fbbfe Add git-zip-tree
Rene Scharfe authored
332 write_or_die(1, path, pathlen);
333 zip_offset += pathlen;
227bf59 archive-zip: write extended timestamp
René Scharfe authored
334 write_or_die(1, &extra, ZIP_EXTRA_MTIME_SIZE);
335 zip_offset += ZIP_EXTRA_MTIME_SIZE;
2158f88 archive-zip: streaming for stored files
René Scharfe authored
336 if (stream && method == 0) {
337 unsigned char buf[STREAM_BUFFER_SIZE];
338 ssize_t readlen;
339
340 for (;;) {
341 readlen = read_istream(stream, buf, sizeof(buf));
342 if (readlen <= 0)
343 break;
344 crc = crc32(crc, buf, readlen);
4aff646 archive-zip: mark text files in archives
René Scharfe authored
345 if (is_binary == -1)
346 is_binary = entry_is_binary(path_without_prefix,
347 buf, readlen);
2158f88 archive-zip: streaming for stored files
René Scharfe authored
348 write_or_die(1, buf, readlen);
349 }
350 close_istream(stream);
351 if (readlen)
352 return readlen;
353
354 compressed_size = size;
355 zip_offset += compressed_size;
356
357 write_zip_data_desc(size, compressed_size, crc);
358 zip_offset += ZIP_DATA_DESC_SIZE;
359
360 set_zip_dir_data_desc(&dirent, size, compressed_size, crc);
c743c21 archive-zip: streaming for deflated files
René Scharfe authored
361 } else if (stream && method == 8) {
362 unsigned char buf[STREAM_BUFFER_SIZE];
363 ssize_t readlen;
364 git_zstream zstream;
365 int result;
366 size_t out_len;
367 unsigned char compressed[STREAM_BUFFER_SIZE * 2];
368
c3c2e1a archive-zip: use deflateInit2() to ask for raw compressed data
René Scharfe authored
369 git_deflate_init_raw(&zstream, args->compression_level);
c743c21 archive-zip: streaming for deflated files
René Scharfe authored
370
371 compressed_size = 0;
372 zstream.next_out = compressed;
373 zstream.avail_out = sizeof(compressed);
374
375 for (;;) {
376 readlen = read_istream(stream, buf, sizeof(buf));
377 if (readlen <= 0)
378 break;
379 crc = crc32(crc, buf, readlen);
4aff646 archive-zip: mark text files in archives
René Scharfe authored
380 if (is_binary == -1)
381 is_binary = entry_is_binary(path_without_prefix,
382 buf, readlen);
c743c21 archive-zip: streaming for deflated files
René Scharfe authored
383
384 zstream.next_in = buf;
385 zstream.avail_in = readlen;
386 result = git_deflate(&zstream, 0);
387 if (result != Z_OK)
388 die("deflate error (%d)", result);
c3c2e1a archive-zip: use deflateInit2() to ask for raw compressed data
René Scharfe authored
389 out_len = zstream.next_out - compressed;
c743c21 archive-zip: streaming for deflated files
René Scharfe authored
390
391 if (out_len > 0) {
c3c2e1a archive-zip: use deflateInit2() to ask for raw compressed data
René Scharfe authored
392 write_or_die(1, compressed, out_len);
c743c21 archive-zip: streaming for deflated files
René Scharfe authored
393 compressed_size += out_len;
394 zstream.next_out = compressed;
395 zstream.avail_out = sizeof(compressed);
396 }
397
398 }
399 close_istream(stream);
400 if (readlen)
401 return readlen;
402
403 zstream.next_in = buf;
404 zstream.avail_in = 0;
405 result = git_deflate(&zstream, Z_FINISH);
406 if (result != Z_STREAM_END)
407 die("deflate error (%d)", result);
408
409 git_deflate_end(&zstream);
c3c2e1a archive-zip: use deflateInit2() to ask for raw compressed data
René Scharfe authored
410 out_len = zstream.next_out - compressed;
411 write_or_die(1, compressed, out_len);
c743c21 archive-zip: streaming for deflated files
René Scharfe authored
412 compressed_size += out_len;
413 zip_offset += compressed_size;
414
415 write_zip_data_desc(size, compressed_size, crc);
416 zip_offset += ZIP_DATA_DESC_SIZE;
417
418 set_zip_dir_data_desc(&dirent, size, compressed_size, crc);
2158f88 archive-zip: streaming for stored files
René Scharfe authored
419 } else if (compressed_size > 0) {
e4fbbfe Add git-zip-tree
Rene Scharfe authored
420 write_or_die(1, out, compressed_size);
421 zip_offset += compressed_size;
422 }
423
424 free(deflated);
9cb513b @pclouds archive: delegate blob reading to backend
pclouds authored
425 free(buffer);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
426
4aff646 archive-zip: mark text files in archives
René Scharfe authored
427 copy_le16(dirent.attr1, !is_binary);
428
ebf5374 archive-zip: factor out helpers for writing sizes and CRC
René Scharfe authored
429 memcpy(zip_dir + zip_dir_offset, &dirent, ZIP_DIR_HEADER_SIZE);
430 zip_dir_offset += ZIP_DIR_HEADER_SIZE;
431 memcpy(zip_dir + zip_dir_offset, path, pathlen);
432 zip_dir_offset += pathlen;
227bf59 archive-zip: write extended timestamp
René Scharfe authored
433 memcpy(zip_dir + zip_dir_offset, &extra, ZIP_EXTRA_MTIME_SIZE);
434 zip_dir_offset += ZIP_EXTRA_MTIME_SIZE;
ebf5374 archive-zip: factor out helpers for writing sizes and CRC
René Scharfe authored
435 zip_dir_entries++;
436
562e25a archive: centralize archive entry writing
René Scharfe authored
437 return 0;
e4fbbfe Add git-zip-tree
Rene Scharfe authored
438 }
439
440 static void write_zip_trailer(const unsigned char *sha1)
441 {
442 struct zip_dir_trailer trailer;
443
444 copy_le32(trailer.magic, 0x06054b50);
445 copy_le16(trailer.disk, 0);
446 copy_le16(trailer.directory_start_disk, 0);
447 copy_le16(trailer.entries_on_this_disk, zip_dir_entries);
448 copy_le16(trailer.entries, zip_dir_entries);
449 copy_le32(trailer.size, zip_dir_offset);
450 copy_le32(trailer.offset, zip_offset);
aeecdcd @bk2204 zip: use GIT_SHA1_HEXSZ for trailers
bk2204 authored
451 copy_le16(trailer.comment_length, sha1 ? GIT_SHA1_HEXSZ : 0);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
452
453 write_or_die(1, zip_dir, zip_dir_offset);
0ea865c archive-zip: don't use sizeof(struct ...)
René Scharfe authored
454 write_or_die(1, &trailer, ZIP_DIR_TRAILER_SIZE);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
455 if (sha1)
aeecdcd @bk2204 zip: use GIT_SHA1_HEXSZ for trailers
bk2204 authored
456 write_or_die(1, sha1_to_hex(sha1), GIT_SHA1_HEXSZ);
e4fbbfe Add git-zip-tree
Rene Scharfe authored
457 }
458
459 static void dos_time(time_t *time, int *dos_date, int *dos_time)
460 {
461 struct tm *t = localtime(time);
462
463 *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
464 (t->tm_year + 1900 - 1980) * 512;
465 *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
466 }
467
4d7c989 @peff archive: pass archiver struct to write_archive callback
peff authored
468 static int write_zip_archive(const struct archiver *ar,
469 struct archiver_args *args)
ec06bff git-archive: wire up ZIP format.
Franck Bui-Huu authored
470 {
562e25a archive: centralize archive entry writing
René Scharfe authored
471 int err;
472
ec06bff git-archive: wire up ZIP format.
Franck Bui-Huu authored
473 dos_time(&args->time, &zip_date, &zip_time);
474
475 zip_dir = xmalloc(ZIP_DIRECTORY_MIN_SIZE);
476 zip_dir_size = ZIP_DIRECTORY_MIN_SIZE;
562e25a archive: centralize archive entry writing
René Scharfe authored
477
478 err = write_archive_entries(args, write_zip_entry);
479 if (!err)
480 write_zip_trailer(args->commit_sha1);
ec06bff git-archive: wire up ZIP format.
Franck Bui-Huu authored
481
482 free(zip_dir);
483
562e25a archive: centralize archive entry writing
René Scharfe authored
484 return err;
ec06bff git-archive: wire up ZIP format.
Franck Bui-Huu authored
485 }
13e0f88 @peff archive: refactor list of archive formats
peff authored
486
487 static struct archiver zip_archiver = {
488 "zip",
489 write_zip_archive,
7b97730 @peff upload-archive: allow user to turn off filters
peff authored
490 ARCHIVER_WANT_COMPRESSION_LEVELS|ARCHIVER_REMOTE
13e0f88 @peff archive: refactor list of archive formats
peff authored
491 };
492
493 void init_zip_archiver(void)
494 {
495 register_archiver(&zip_archiver);
496 }
Something went wrong with that request. Please try again.