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