]> git.ipfire.org Git - thirdparty/git.git/blame - archive-tar.c
branch: add test for -m renaming multiple config sections
[thirdparty/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
29493589 30#else
3f789719 31#define USTAR_MAX_SIZE 077777777777UL
dddbad72
JS
32#endif
33#if TIME_MAX == 0xFFFFFFFF
34#define USTAR_MAX_MTIME TIME_MAX
35#else
3f789719 36#define USTAR_MAX_MTIME 077777777777ULL
29493589 37#endif
d1657b57 38
3d74982f
RS
39/* writes out the whole block, but only if it is full */
40static void write_if_needed(void)
41{
42 if (offset == BLOCKSIZE) {
43 write_or_die(1, block, BLOCKSIZE);
44 offset = 0;
45 }
46}
47
48/*
49 * queues up writes, so that all our write(2) calls write exactly one
50 * full block; pads writes to RECORDSIZE
51 */
5544049d 52static void do_write_blocked(const void *data, unsigned long size)
3d74982f
RS
53{
54 const char *buf = data;
3d74982f
RS
55
56 if (offset) {
57 unsigned long chunk = BLOCKSIZE - offset;
58 if (size < chunk)
59 chunk = size;
60 memcpy(block + offset, buf, chunk);
61 size -= chunk;
62 offset += chunk;
63 buf += chunk;
64 write_if_needed();
65 }
66 while (size >= BLOCKSIZE) {
67 write_or_die(1, buf, BLOCKSIZE);
68 size -= BLOCKSIZE;
69 buf += BLOCKSIZE;
70 }
71 if (size) {
72 memcpy(block + offset, buf, size);
73 offset += size;
74 }
5544049d
NTND
75}
76
77static void finish_record(void)
78{
79 unsigned long tail;
3d74982f
RS
80 tail = offset % RECORDSIZE;
81 if (tail) {
82 memset(block + offset, 0, RECORDSIZE - tail);
83 offset += RECORDSIZE - tail;
84 }
85 write_if_needed();
86}
87
5544049d
NTND
88static void write_blocked(const void *data, unsigned long size)
89{
90 do_write_blocked(data, size);
91 finish_record();
92}
93
3d74982f
RS
94/*
95 * The end of tar archives is marked by 2*512 nul bytes and after that
96 * follows the rest of the block (if any).
97 */
98static void write_trailer(void)
99{
100 int tail = BLOCKSIZE - offset;
101 memset(block + offset, 0, tail);
102 write_or_die(1, block, BLOCKSIZE);
103 if (tail < 2 * RECORDSIZE) {
104 memset(block, 0, offset);
105 write_or_die(1, block, BLOCKSIZE);
106 }
107}
108
5544049d
NTND
109/*
110 * queues up writes, so that all our write(2) calls write exactly one
111 * full block; pads writes to RECORDSIZE
112 */
113static int stream_blocked(const unsigned char *sha1)
114{
115 struct git_istream *st;
116 enum object_type type;
117 unsigned long sz;
118 char buf[BLOCKSIZE];
119 ssize_t readlen;
120
121 st = open_istream(sha1, &type, &sz, NULL);
122 if (!st)
123 return error("cannot stream blob %s", sha1_to_hex(sha1));
124 for (;;) {
125 readlen = read_istream(st, buf, sizeof(buf));
126 if (readlen <= 0)
127 break;
128 do_write_blocked(buf, readlen);
129 }
130 close_istream(st);
131 if (!readlen)
132 finish_record();
133 return readlen;
134}
135
3d74982f
RS
136/*
137 * pax extended header records have the format "%u %s=%s\n". %u contains
138 * the size of the whole string (including the %u), the first %s is the
139 * keyword, the second one is the value. This function constructs such a
140 * string and appends it to a struct strbuf.
141 */
142static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
143 const char *value, unsigned int valuelen)
144{
7a604f16 145 int len, tmp;
3d74982f
RS
146
147 /* "%u %s=%s\n" */
148 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
149 for (tmp = len; tmp > 9; tmp /= 10)
150 len++;
151
7a604f16
PH
152 strbuf_grow(sb, len);
153 strbuf_addf(sb, "%u %s=", len, keyword);
154 strbuf_add(sb, value, valuelen);
155 strbuf_addch(sb, '\n');
3d74982f
RS
156}
157
d1657b57
JK
158/*
159 * Like strbuf_append_ext_header, but for numeric values.
160 */
161static void strbuf_append_ext_header_uint(struct strbuf *sb,
162 const char *keyword,
163 uintmax_t value)
164{
165 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
166 int len;
167
168 len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
169 strbuf_append_ext_header(sb, keyword, buf, len);
170}
171
3d74982f
RS
172static unsigned int ustar_header_chksum(const struct ustar_header *header)
173{
a5a46eb9 174 const unsigned char *p = (const unsigned char *)header;
3d74982f 175 unsigned int chksum = 0;
a5a46eb9 176 while (p < (const unsigned char *)header->chksum)
3d74982f
RS
177 chksum += *p++;
178 chksum += sizeof(header->chksum) * ' ';
179 p += sizeof(header->chksum);
a5a46eb9 180 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
3d74982f
RS
181 chksum += *p++;
182 return chksum;
183}
184
562e25ab 185static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
3d74982f 186{
562e25ab 187 size_t i = pathlen;
22f0dcd9
RS
188 if (i > 1 && path[i - 1] == '/')
189 i--;
3d74982f
RS
190 if (i > maxlen)
191 i = maxlen;
192 do {
193 i--;
562e25ab 194 } while (i > 0 && path[i] != '/');
3d74982f
RS
195 return i;
196}
197
d240d410
NTND
198static void prepare_header(struct archiver_args *args,
199 struct ustar_header *header,
200 unsigned int mode, unsigned long size)
201{
f2f02675
JK
202 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
203 xsnprintf(header->size, sizeof(header->size), "%011lo", S_ISREG(mode) ? size : 0);
204 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
d240d410 205
f2f02675
JK
206 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
207 xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
d240d410
NTND
208 strlcpy(header->uname, "root", sizeof(header->uname));
209 strlcpy(header->gname, "root", sizeof(header->gname));
f2f02675
JK
210 xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
211 xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
d240d410
NTND
212
213 memcpy(header->magic, "ustar", 6);
214 memcpy(header->version, "00", 2);
215
9e6c1e91 216 xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
d240d410
NTND
217}
218
560b0e8f
RS
219static void write_extended_header(struct archiver_args *args,
220 const unsigned char *sha1,
221 const void *buffer, unsigned long size)
d240d410
NTND
222{
223 struct ustar_header header;
224 unsigned int mode;
225 memset(&header, 0, sizeof(header));
226 *header.typeflag = TYPEFLAG_EXT_HEADER;
15c6ef7b 227 mode = 0100666;
f2f02675 228 xsnprintf(header.name, sizeof(header.name), "%s.paxheader", sha1_to_hex(sha1));
d240d410
NTND
229 prepare_header(args, &header, mode, size);
230 write_blocked(&header, sizeof(header));
231 write_blocked(buffer, size);
d240d410
NTND
232}
233
562e25ab 234static int write_tar_entry(struct archiver_args *args,
9cb513b7
NTND
235 const unsigned char *sha1,
236 const char *path, size_t pathlen,
237 unsigned int mode)
3d74982f
RS
238{
239 struct ustar_header header;
f285a2d7 240 struct strbuf ext_header = STRBUF_INIT;
9cb513b7 241 unsigned int old_mode = mode;
d1657b57 242 unsigned long size, size_in_header;
9cb513b7 243 void *buffer;
562e25ab 244 int err = 0;
3d74982f
RS
245
246 memset(&header, 0, sizeof(header));
3d74982f 247
85390709
NTND
248 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
249 *header.typeflag = TYPEFLAG_DIR;
250 mode = (mode | 0777) & ~tar_umask;
251 } else if (S_ISLNK(mode)) {
252 *header.typeflag = TYPEFLAG_LNK;
253 mode |= 0777;
254 } else if (S_ISREG(mode)) {
255 *header.typeflag = TYPEFLAG_REG;
256 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
3d74982f 257 } else {
85390709
NTND
258 return error("unsupported file mode: 0%o (SHA1: %s)",
259 mode, sha1_to_hex(sha1));
260 }
261 if (pathlen > sizeof(header.name)) {
262 size_t plen = get_path_prefix(path, pathlen,
263 sizeof(header.prefix));
264 size_t rest = pathlen - plen - 1;
265 if (plen > 0 && rest <= sizeof(header.name)) {
266 memcpy(header.prefix, path, plen);
108332c7 267 memcpy(header.name, path + plen + 1, rest);
3d74982f 268 } else {
f2f02675
JK
269 xsnprintf(header.name, sizeof(header.name), "%s.data",
270 sha1_to_hex(sha1));
85390709
NTND
271 strbuf_append_ext_header(&ext_header, "path",
272 path, pathlen);
3d74982f 273 }
85390709
NTND
274 } else
275 memcpy(header.name, path, pathlen);
3d74982f 276
5544049d
NTND
277 if (S_ISREG(mode) && !args->convert &&
278 sha1_object_info(sha1, &size) == OBJ_BLOB &&
279 size > big_file_threshold)
280 buffer = NULL;
281 else if (S_ISLNK(mode) || S_ISREG(mode)) {
9cb513b7
NTND
282 enum object_type type;
283 buffer = sha1_file_to_archive(args, path, sha1, old_mode, &type, &size);
284 if (!buffer)
285 return error("cannot read %s", sha1_to_hex(sha1));
286 } else {
287 buffer = NULL;
288 size = 0;
289 }
290
291 if (S_ISLNK(mode)) {
3d74982f 292 if (size > sizeof(header.linkname)) {
f2f02675
JK
293 xsnprintf(header.linkname, sizeof(header.linkname),
294 "see %s.paxheader", sha1_to_hex(sha1));
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) {
560b0e8f
RS
310 write_extended_header(args, sha1, ext_header.buf,
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
319 err = stream_blocked(sha1);
320 }
9cb513b7 321 free(buffer);
562e25ab 322 return err;
3d74982f
RS
323}
324
5caeeb83 325static void write_global_extended_header(struct archiver_args *args)
3d74982f 326{
562e25ab 327 const unsigned char *sha1 = args->commit_sha1;
f285a2d7 328 struct strbuf ext_header = STRBUF_INIT;
d240d410
NTND
329 struct ustar_header header;
330 unsigned int mode;
7a604f16 331
6e8e0991
JK
332 if (sha1)
333 strbuf_append_ext_header(&ext_header, "comment",
334 sha1_to_hex(sha1), 40);
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
358static struct archiver *find_tar_filter(const char *name, int len)
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;
374 int namelen;
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) {
381 ar = xcalloc(1, sizeof(*ar));
382 ar->name = xmemdupz(name, namelen);
383 ar->write_archive = write_tar_filter_archive;
384 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
385 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
386 tar_filters[nr_tar_filters++] = ar;
387 }
388
389 if (!strcmp(type, "command")) {
390 if (!value)
391 return config_error_nonbool(var);
392 free(ar->data);
393 ar->data = xstrdup(value);
394 return 0;
395 }
7b97730b
JK
396 if (!strcmp(type, "remote")) {
397 if (git_config_bool(var, value))
398 ar->flags |= ARCHIVER_REMOTE;
399 else
400 ar->flags &= ~ARCHIVER_REMOTE;
401 return 0;
402 }
767cf457
JK
403
404 return 0;
405}
406
ef90d6d4 407static int git_tar_config(const char *var, const char *value, void *cb)
3d74982f
RS
408{
409 if (!strcmp(var, "tar.umask")) {
cc1816b0 410 if (value && !strcmp(value, "user")) {
3d74982f
RS
411 tar_umask = umask(0);
412 umask(tar_umask);
413 } else {
414 tar_umask = git_config_int(var, value);
415 }
416 return 0;
417 }
767cf457
JK
418
419 return tar_filter_config(var, value, cb);
3d74982f
RS
420}
421
4d7c9898
JK
422static int write_tar_archive(const struct archiver *ar,
423 struct archiver_args *args)
3d74982f 424{
562e25ab 425 int err = 0;
3d74982f 426
5caeeb83
JK
427 write_global_extended_header(args);
428 err = write_archive_entries(args, write_tar_entry);
562e25ab
RS
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}