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