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