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