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