]> git.ipfire.org Git - thirdparty/git.git/blame - archive-tar.c
archive-tar: add internal gzip implementation
[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
369static int tar_filter_config(const char *var, const char *value, void *data)
370{
371 struct archiver *ar;
767cf457
JK
372 const char *name;
373 const char *type;
f5914f4b 374 size_t namelen;
767cf457 375
785a0429 376 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
767cf457 377 return 0;
767cf457
JK
378
379 ar = find_tar_filter(name, namelen);
380 if (!ar) {
ca56dadb 381 CALLOC_ARRAY(ar, 1);
767cf457
JK
382 ar->name = xmemdupz(name, namelen);
383 ar->write_archive = write_tar_filter_archive;
cde8ea9c
RS
384 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS |
385 ARCHIVER_HIGH_COMPRESSION_LEVELS;
767cf457
JK
386 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
387 tar_filters[nr_tar_filters++] = ar;
388 }
389
390 if (!strcmp(type, "command")) {
391 if (!value)
392 return config_error_nonbool(var);
96b9e515
RS
393 free(ar->filter_command);
394 ar->filter_command = xstrdup(value);
767cf457
JK
395 return 0;
396 }
7b97730b
JK
397 if (!strcmp(type, "remote")) {
398 if (git_config_bool(var, value))
399 ar->flags |= ARCHIVER_REMOTE;
400 else
401 ar->flags &= ~ARCHIVER_REMOTE;
402 return 0;
403 }
767cf457
JK
404
405 return 0;
406}
407
ef90d6d4 408static int git_tar_config(const char *var, const char *value, void *cb)
3d74982f
RS
409{
410 if (!strcmp(var, "tar.umask")) {
cc1816b0 411 if (value && !strcmp(value, "user")) {
3d74982f
RS
412 tar_umask = umask(0);
413 umask(tar_umask);
414 } else {
415 tar_umask = git_config_int(var, value);
416 }
417 return 0;
418 }
767cf457
JK
419
420 return tar_filter_config(var, value, cb);
3d74982f
RS
421}
422
4d7c9898
JK
423static int write_tar_archive(const struct archiver *ar,
424 struct archiver_args *args)
3d74982f 425{
562e25ab 426 int err = 0;
3d74982f 427
5caeeb83
JK
428 write_global_extended_header(args);
429 err = write_archive_entries(args, write_tar_entry);
562e25ab
RS
430 if (!err)
431 write_trailer();
432 return err;
3d74982f 433}
13e0f88d 434
76d76026
RS
435static git_zstream gzstream;
436static unsigned char outbuf[16384];
437
438static void tgz_deflate(int flush)
439{
440 while (gzstream.avail_in || flush == Z_FINISH) {
441 int status = git_deflate(&gzstream, flush);
442 if (!gzstream.avail_out || status == Z_STREAM_END) {
443 write_or_die(1, outbuf, gzstream.next_out - outbuf);
444 gzstream.next_out = outbuf;
445 gzstream.avail_out = sizeof(outbuf);
446 if (status == Z_STREAM_END)
447 break;
448 }
449 if (status != Z_OK && status != Z_BUF_ERROR)
450 die(_("deflate error (%d)"), status);
451 }
452}
453
454static void tgz_write_block(const void *data)
455{
456 gzstream.next_in = (void *)data;
457 gzstream.avail_in = BLOCKSIZE;
458 tgz_deflate(Z_NO_FLUSH);
459}
460
461static const char internal_gzip_command[] = "git archive gzip";
462
767cf457
JK
463static int write_tar_filter_archive(const struct archiver *ar,
464 struct archiver_args *args)
465{
466 struct strbuf cmd = STRBUF_INIT;
d3180279 467 struct child_process filter = CHILD_PROCESS_INIT;
767cf457
JK
468 int r;
469
96b9e515 470 if (!ar->filter_command)
033abf97 471 BUG("tar-filter archiver called with no filter defined");
767cf457 472
76d76026
RS
473 if (!strcmp(ar->filter_command, internal_gzip_command)) {
474 write_block = tgz_write_block;
475 git_deflate_init_gzip(&gzstream, args->compression_level);
476 gzstream.next_out = outbuf;
477 gzstream.avail_out = sizeof(outbuf);
478
479 r = write_tar_archive(ar, args);
480
481 tgz_deflate(Z_FINISH);
482 git_deflate_end(&gzstream);
483 return r;
484 }
485
96b9e515 486 strbuf_addstr(&cmd, ar->filter_command);
767cf457
JK
487 if (args->compression_level >= 0)
488 strbuf_addf(&cmd, " -%d", args->compression_level);
489
7f14609e 490 strvec_push(&filter.args, cmd.buf);
767cf457
JK
491 filter.use_shell = 1;
492 filter.in = -1;
493
494 if (start_command(&filter) < 0)
7f14609e 495 die_errno(_("unable to start '%s' filter"), cmd.buf);
767cf457
JK
496 close(1);
497 if (dup2(filter.in, 1) < 0)
d0482e69 498 die_errno(_("unable to redirect descriptor"));
767cf457
JK
499 close(filter.in);
500
501 r = write_tar_archive(ar, args);
502
503 close(1);
504 if (finish_command(&filter) != 0)
7f14609e 505 die(_("'%s' filter reported error"), cmd.buf);
767cf457
JK
506
507 strbuf_release(&cmd);
508 return r;
509}
510
13e0f88d 511static struct archiver tar_archiver = {
4fbedd4d
ÆAB
512 .name = "tar",
513 .write_archive = write_tar_archive,
514 .flags = ARCHIVER_REMOTE,
13e0f88d
JK
515};
516
517void init_tar_archiver(void)
518{
767cf457 519 int i;
13e0f88d 520 register_archiver(&tar_archiver);
767cf457 521
0e804e09 522 tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
7b97730b 523 tar_filter_config("tar.tgz.remote", "true", NULL);
0e804e09 524 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
7b97730b 525 tar_filter_config("tar.tar.gz.remote", "true", NULL);
13e0f88d 526 git_config(git_tar_config, NULL);
767cf457
JK
527 for (i = 0; i < nr_tar_filters; i++) {
528 /* omit any filters that never had a command configured */
96b9e515 529 if (tar_filters[i]->filter_command)
767cf457
JK
530 register_archiver(tar_filters[i]);
531 }
13e0f88d 532}