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