]> git.ipfire.org Git - thirdparty/git.git/blob - archive-tar.c
alloc.h: move ALLOC_GROW() functions from cache.h
[thirdparty/git.git] / archive-tar.c
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "git-compat-util.h"
5 #include "alloc.h"
6 #include "config.h"
7 #include "tar.h"
8 #include "archive.h"
9 #include "object-store.h"
10 #include "streaming.h"
11 #include "run-command.h"
12
13 #define RECORDSIZE (512)
14 #define BLOCKSIZE (RECORDSIZE * 20)
15
16 static char block[BLOCKSIZE];
17 static unsigned long offset;
18
19 static int tar_umask = 002;
20
21 static int write_tar_filter_archive(const struct archiver *ar,
22 struct archiver_args *args);
23
24 /*
25 * This is the max value that a ustar size header can specify, as it is fixed
26 * at 11 octal digits. POSIX specifies that we switch to extended headers at
27 * this size.
28 *
29 * Likewise for the mtime (which happens to use a buffer of the same size).
30 */
31 #if ULONG_MAX == 0xFFFFFFFF
32 #define USTAR_MAX_SIZE ULONG_MAX
33 #else
34 #define USTAR_MAX_SIZE 077777777777UL
35 #endif
36 #if TIME_MAX == 0xFFFFFFFF
37 #define USTAR_MAX_MTIME TIME_MAX
38 #else
39 #define USTAR_MAX_MTIME 077777777777ULL
40 #endif
41
42 static void tar_write_block(const void *buf)
43 {
44 write_or_die(1, buf, BLOCKSIZE);
45 }
46
47 static void (*write_block)(const void *) = tar_write_block;
48
49 /* writes out the whole block, but only if it is full */
50 static void write_if_needed(void)
51 {
52 if (offset == BLOCKSIZE) {
53 write_block(block);
54 offset = 0;
55 }
56 }
57
58 /*
59 * queues up writes, so that all our write(2) calls write exactly one
60 * full block; pads writes to RECORDSIZE
61 */
62 static void do_write_blocked(const void *data, unsigned long size)
63 {
64 const char *buf = data;
65
66 if (offset) {
67 unsigned long chunk = BLOCKSIZE - offset;
68 if (size < chunk)
69 chunk = size;
70 memcpy(block + offset, buf, chunk);
71 size -= chunk;
72 offset += chunk;
73 buf += chunk;
74 write_if_needed();
75 }
76 while (size >= BLOCKSIZE) {
77 write_block(buf);
78 size -= BLOCKSIZE;
79 buf += BLOCKSIZE;
80 }
81 if (size) {
82 memcpy(block + offset, buf, size);
83 offset += size;
84 }
85 }
86
87 static void finish_record(void)
88 {
89 unsigned long tail;
90 tail = offset % RECORDSIZE;
91 if (tail) {
92 memset(block + offset, 0, RECORDSIZE - tail);
93 offset += RECORDSIZE - tail;
94 }
95 write_if_needed();
96 }
97
98 static void write_blocked(const void *data, unsigned long size)
99 {
100 do_write_blocked(data, size);
101 finish_record();
102 }
103
104 /*
105 * The end of tar archives is marked by 2*512 nul bytes and after that
106 * follows the rest of the block (if any).
107 */
108 static void write_trailer(void)
109 {
110 int tail = BLOCKSIZE - offset;
111 memset(block + offset, 0, tail);
112 write_block(block);
113 if (tail < 2 * RECORDSIZE) {
114 memset(block, 0, offset);
115 write_block(block);
116 }
117 }
118
119 /*
120 * queues up writes, so that all our write(2) calls write exactly one
121 * full block; pads writes to RECORDSIZE
122 */
123 static int stream_blocked(struct repository *r, const struct object_id *oid)
124 {
125 struct git_istream *st;
126 enum object_type type;
127 unsigned long sz;
128 char buf[BLOCKSIZE];
129 ssize_t readlen;
130
131 st = open_istream(r, oid, &type, &sz, NULL);
132 if (!st)
133 return error(_("cannot stream blob %s"), oid_to_hex(oid));
134 for (;;) {
135 readlen = read_istream(st, buf, sizeof(buf));
136 if (readlen <= 0)
137 break;
138 do_write_blocked(buf, readlen);
139 }
140 close_istream(st);
141 if (!readlen)
142 finish_record();
143 return readlen;
144 }
145
146 /*
147 * pax extended header records have the format "%u %s=%s\n". %u contains
148 * the size of the whole string (including the %u), the first %s is the
149 * keyword, the second one is the value. This function constructs such a
150 * string and appends it to a struct strbuf.
151 */
152 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
153 const char *value, size_t valuelen)
154 {
155 size_t orig_len = sb->len;
156 size_t len, tmp;
157
158 /* "%u %s=%s\n" */
159 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
160 for (tmp = 1; len / 10 >= tmp; tmp *= 10)
161 len++;
162
163 strbuf_grow(sb, len);
164 strbuf_addf(sb, "%"PRIuMAX" %s=", (uintmax_t)len, keyword);
165 strbuf_add(sb, value, valuelen);
166 strbuf_addch(sb, '\n');
167
168 if (len != sb->len - orig_len)
169 BUG("pax extended header length miscalculated as %"PRIuMAX
170 ", should be %"PRIuMAX,
171 (uintmax_t)len, (uintmax_t)(sb->len - orig_len));
172 }
173
174 /*
175 * Like strbuf_append_ext_header, but for numeric values.
176 */
177 static void strbuf_append_ext_header_uint(struct strbuf *sb,
178 const char *keyword,
179 uintmax_t value)
180 {
181 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
182 int len;
183
184 len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
185 strbuf_append_ext_header(sb, keyword, buf, len);
186 }
187
188 static unsigned int ustar_header_chksum(const struct ustar_header *header)
189 {
190 const unsigned char *p = (const unsigned char *)header;
191 unsigned int chksum = 0;
192 while (p < (const unsigned char *)header->chksum)
193 chksum += *p++;
194 chksum += sizeof(header->chksum) * ' ';
195 p += sizeof(header->chksum);
196 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
197 chksum += *p++;
198 return chksum;
199 }
200
201 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
202 {
203 size_t i = pathlen;
204 if (i > 1 && path[i - 1] == '/')
205 i--;
206 if (i > maxlen)
207 i = maxlen;
208 do {
209 i--;
210 } while (i > 0 && path[i] != '/');
211 return i;
212 }
213
214 static void prepare_header(struct archiver_args *args,
215 struct ustar_header *header,
216 unsigned int mode, unsigned long size)
217 {
218 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
219 xsnprintf(header->size, sizeof(header->size), "%011"PRIoMAX , S_ISREG(mode) ? (uintmax_t)size : (uintmax_t)0);
220 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
221
222 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
223 xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
224 strlcpy(header->uname, "root", sizeof(header->uname));
225 strlcpy(header->gname, "root", sizeof(header->gname));
226 xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
227 xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
228
229 memcpy(header->magic, "ustar", 6);
230 memcpy(header->version, "00", 2);
231
232 xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
233 }
234
235 static void write_extended_header(struct archiver_args *args,
236 const struct object_id *oid,
237 const void *buffer, unsigned long size)
238 {
239 struct ustar_header header;
240 unsigned int mode;
241 memset(&header, 0, sizeof(header));
242 *header.typeflag = TYPEFLAG_EXT_HEADER;
243 mode = 0100666;
244 xsnprintf(header.name, sizeof(header.name), "%s.paxheader", oid_to_hex(oid));
245 prepare_header(args, &header, mode, size);
246 write_blocked(&header, sizeof(header));
247 write_blocked(buffer, size);
248 }
249
250 static int write_tar_entry(struct archiver_args *args,
251 const struct object_id *oid,
252 const char *path, size_t pathlen,
253 unsigned int mode,
254 void *buffer, unsigned long size)
255 {
256 struct ustar_header header;
257 struct strbuf ext_header = STRBUF_INIT;
258 unsigned long size_in_header;
259 int err = 0;
260
261 memset(&header, 0, sizeof(header));
262
263 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
264 *header.typeflag = TYPEFLAG_DIR;
265 mode = (mode | 0777) & ~tar_umask;
266 } else if (S_ISLNK(mode)) {
267 *header.typeflag = TYPEFLAG_LNK;
268 mode |= 0777;
269 } else if (S_ISREG(mode)) {
270 *header.typeflag = TYPEFLAG_REG;
271 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
272 } else {
273 return error(_("unsupported file mode: 0%o (SHA1: %s)"),
274 mode, oid_to_hex(oid));
275 }
276 if (pathlen > sizeof(header.name)) {
277 size_t plen = get_path_prefix(path, pathlen,
278 sizeof(header.prefix));
279 size_t rest = pathlen - plen - 1;
280 if (plen > 0 && rest <= sizeof(header.name)) {
281 memcpy(header.prefix, path, plen);
282 memcpy(header.name, path + plen + 1, rest);
283 } else {
284 xsnprintf(header.name, sizeof(header.name), "%s.data",
285 oid_to_hex(oid));
286 strbuf_append_ext_header(&ext_header, "path",
287 path, pathlen);
288 }
289 } else
290 memcpy(header.name, path, pathlen);
291
292 if (S_ISLNK(mode)) {
293 if (size > sizeof(header.linkname)) {
294 xsnprintf(header.linkname, sizeof(header.linkname),
295 "see %s.paxheader", oid_to_hex(oid));
296 strbuf_append_ext_header(&ext_header, "linkpath",
297 buffer, size);
298 } else
299 memcpy(header.linkname, buffer, size);
300 }
301
302 size_in_header = size;
303 if (S_ISREG(mode) && size > USTAR_MAX_SIZE) {
304 size_in_header = 0;
305 strbuf_append_ext_header_uint(&ext_header, "size", size);
306 }
307
308 prepare_header(args, &header, mode, size_in_header);
309
310 if (ext_header.len > 0) {
311 write_extended_header(args, oid, ext_header.buf,
312 ext_header.len);
313 }
314 strbuf_release(&ext_header);
315 write_blocked(&header, sizeof(header));
316 if (S_ISREG(mode) && size > 0) {
317 if (buffer)
318 write_blocked(buffer, size);
319 else
320 err = stream_blocked(args->repo, oid);
321 }
322 return err;
323 }
324
325 static void write_global_extended_header(struct archiver_args *args)
326 {
327 const struct object_id *oid = args->commit_oid;
328 struct strbuf ext_header = STRBUF_INIT;
329 struct ustar_header header;
330 unsigned int mode;
331
332 if (oid)
333 strbuf_append_ext_header(&ext_header, "comment",
334 oid_to_hex(oid),
335 the_hash_algo->hexsz);
336 if (args->time > USTAR_MAX_MTIME) {
337 strbuf_append_ext_header_uint(&ext_header, "mtime",
338 args->time);
339 args->time = USTAR_MAX_MTIME;
340 }
341
342 if (!ext_header.len)
343 return;
344
345 memset(&header, 0, sizeof(header));
346 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
347 mode = 0100666;
348 xsnprintf(header.name, sizeof(header.name), "pax_global_header");
349 prepare_header(args, &header, mode, ext_header.len);
350 write_blocked(&header, sizeof(header));
351 write_blocked(ext_header.buf, ext_header.len);
352 strbuf_release(&ext_header);
353 }
354
355 static struct archiver **tar_filters;
356 static int nr_tar_filters;
357 static int alloc_tar_filters;
358
359 static struct archiver *find_tar_filter(const char *name, size_t len)
360 {
361 int i;
362 for (i = 0; i < nr_tar_filters; i++) {
363 struct archiver *ar = tar_filters[i];
364 if (!strncmp(ar->name, name, len) && !ar->name[len])
365 return ar;
366 }
367 return NULL;
368 }
369
370 static int tar_filter_config(const char *var, const char *value,
371 void *data UNUSED)
372 {
373 struct archiver *ar;
374 const char *name;
375 const char *type;
376 size_t namelen;
377
378 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
379 return 0;
380
381 ar = find_tar_filter(name, namelen);
382 if (!ar) {
383 CALLOC_ARRAY(ar, 1);
384 ar->name = xmemdupz(name, namelen);
385 ar->write_archive = write_tar_filter_archive;
386 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS |
387 ARCHIVER_HIGH_COMPRESSION_LEVELS;
388 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
389 tar_filters[nr_tar_filters++] = ar;
390 }
391
392 if (!strcmp(type, "command")) {
393 if (!value)
394 return config_error_nonbool(var);
395 free(ar->filter_command);
396 ar->filter_command = xstrdup(value);
397 return 0;
398 }
399 if (!strcmp(type, "remote")) {
400 if (git_config_bool(var, value))
401 ar->flags |= ARCHIVER_REMOTE;
402 else
403 ar->flags &= ~ARCHIVER_REMOTE;
404 return 0;
405 }
406
407 return 0;
408 }
409
410 static int git_tar_config(const char *var, const char *value, void *cb)
411 {
412 if (!strcmp(var, "tar.umask")) {
413 if (value && !strcmp(value, "user")) {
414 tar_umask = umask(0);
415 umask(tar_umask);
416 } else {
417 tar_umask = git_config_int(var, value);
418 }
419 return 0;
420 }
421
422 return tar_filter_config(var, value, cb);
423 }
424
425 static int write_tar_archive(const struct archiver *ar UNUSED,
426 struct archiver_args *args)
427 {
428 int err = 0;
429
430 write_global_extended_header(args);
431 err = write_archive_entries(args, write_tar_entry);
432 if (!err)
433 write_trailer();
434 return err;
435 }
436
437 static git_zstream gzstream;
438 static unsigned char outbuf[16384];
439
440 static void tgz_deflate(int flush)
441 {
442 while (gzstream.avail_in || flush == Z_FINISH) {
443 int status = git_deflate(&gzstream, flush);
444 if (!gzstream.avail_out || status == Z_STREAM_END) {
445 write_or_die(1, outbuf, gzstream.next_out - outbuf);
446 gzstream.next_out = outbuf;
447 gzstream.avail_out = sizeof(outbuf);
448 if (status == Z_STREAM_END)
449 break;
450 }
451 if (status != Z_OK && status != Z_BUF_ERROR)
452 die(_("deflate error (%d)"), status);
453 }
454 }
455
456 static void tgz_write_block(const void *data)
457 {
458 gzstream.next_in = (void *)data;
459 gzstream.avail_in = BLOCKSIZE;
460 tgz_deflate(Z_NO_FLUSH);
461 }
462
463 static const char internal_gzip_command[] = "git archive gzip";
464
465 static int write_tar_filter_archive(const struct archiver *ar,
466 struct archiver_args *args)
467 {
468 #if ZLIB_VERNUM >= 0x1221
469 struct gz_header_s gzhead = { .os = 3 }; /* Unix, for reproducibility */
470 #endif
471 struct strbuf cmd = STRBUF_INIT;
472 struct child_process filter = CHILD_PROCESS_INIT;
473 int r;
474
475 if (!ar->filter_command)
476 BUG("tar-filter archiver called with no filter defined");
477
478 if (!strcmp(ar->filter_command, internal_gzip_command)) {
479 write_block = tgz_write_block;
480 git_deflate_init_gzip(&gzstream, args->compression_level);
481 #if ZLIB_VERNUM >= 0x1221
482 if (deflateSetHeader(&gzstream.z, &gzhead) != Z_OK)
483 BUG("deflateSetHeader() called too late");
484 #endif
485 gzstream.next_out = outbuf;
486 gzstream.avail_out = sizeof(outbuf);
487
488 r = write_tar_archive(ar, args);
489
490 tgz_deflate(Z_FINISH);
491 git_deflate_end(&gzstream);
492 return r;
493 }
494
495 strbuf_addstr(&cmd, ar->filter_command);
496 if (args->compression_level >= 0)
497 strbuf_addf(&cmd, " -%d", args->compression_level);
498
499 strvec_push(&filter.args, cmd.buf);
500 filter.use_shell = 1;
501 filter.in = -1;
502 filter.silent_exec_failure = 1;
503
504 if (start_command(&filter) < 0)
505 die_errno(_("unable to start '%s' filter"), cmd.buf);
506 close(1);
507 if (dup2(filter.in, 1) < 0)
508 die_errno(_("unable to redirect descriptor"));
509 close(filter.in);
510
511 r = write_tar_archive(ar, args);
512
513 close(1);
514 if (finish_command(&filter) != 0)
515 die(_("'%s' filter reported error"), cmd.buf);
516
517 strbuf_release(&cmd);
518 return r;
519 }
520
521 static struct archiver tar_archiver = {
522 .name = "tar",
523 .write_archive = write_tar_archive,
524 .flags = ARCHIVER_REMOTE,
525 };
526
527 void init_tar_archiver(void)
528 {
529 int i;
530 register_archiver(&tar_archiver);
531
532 tar_filter_config("tar.tgz.command", internal_gzip_command, NULL);
533 tar_filter_config("tar.tgz.remote", "true", NULL);
534 tar_filter_config("tar.tar.gz.command", internal_gzip_command, NULL);
535 tar_filter_config("tar.tar.gz.remote", "true", NULL);
536 git_config(git_tar_config, NULL);
537 for (i = 0; i < nr_tar_filters; i++) {
538 /* omit any filters that never had a command configured */
539 if (tar_filters[i]->filter_command)
540 register_archiver(tar_filters[i]);
541 }
542 }