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