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