Merge branch 'rj/bisect-already-used-branch'
[git/debian.git] / archive-tar.c
blobee27fa0b39b8e082fa8680d44b8ff54a0637658f
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "git-compat-util.h"
5 #include "alloc.h"
6 #include "config.h"
7 #include "hex.h"
8 #include "tar.h"
9 #include "archive.h"
10 #include "object-store.h"
11 #include "streaming.h"
12 #include "run-command.h"
14 #define RECORDSIZE (512)
15 #define BLOCKSIZE (RECORDSIZE * 20)
17 static char block[BLOCKSIZE];
18 static unsigned long offset;
20 static int tar_umask = 002;
22 static int write_tar_filter_archive(const struct archiver *ar,
23 struct archiver_args *args);
26 * This is the max value that a ustar size header can specify, as it is fixed
27 * at 11 octal digits. POSIX specifies that we switch to extended headers at
28 * this size.
30 * Likewise for the mtime (which happens to use a buffer of the same size).
32 #if ULONG_MAX == 0xFFFFFFFF
33 #define USTAR_MAX_SIZE ULONG_MAX
34 #else
35 #define USTAR_MAX_SIZE 077777777777UL
36 #endif
37 #if TIME_MAX == 0xFFFFFFFF
38 #define USTAR_MAX_MTIME TIME_MAX
39 #else
40 #define USTAR_MAX_MTIME 077777777777ULL
41 #endif
43 static void tar_write_block(const void *buf)
45 write_or_die(1, buf, BLOCKSIZE);
48 static void (*write_block)(const void *) = tar_write_block;
50 /* writes out the whole block, but only if it is full */
51 static void write_if_needed(void)
53 if (offset == BLOCKSIZE) {
54 write_block(block);
55 offset = 0;
60 * queues up writes, so that all our write(2) calls write exactly one
61 * full block; pads writes to RECORDSIZE
63 static void do_write_blocked(const void *data, unsigned long size)
65 const char *buf = data;
67 if (offset) {
68 unsigned long chunk = BLOCKSIZE - offset;
69 if (size < chunk)
70 chunk = size;
71 memcpy(block + offset, buf, chunk);
72 size -= chunk;
73 offset += chunk;
74 buf += chunk;
75 write_if_needed();
77 while (size >= BLOCKSIZE) {
78 write_block(buf);
79 size -= BLOCKSIZE;
80 buf += BLOCKSIZE;
82 if (size) {
83 memcpy(block + offset, buf, size);
84 offset += size;
88 static void finish_record(void)
90 unsigned long tail;
91 tail = offset % RECORDSIZE;
92 if (tail) {
93 memset(block + offset, 0, RECORDSIZE - tail);
94 offset += RECORDSIZE - tail;
96 write_if_needed();
99 static void write_blocked(const void *data, unsigned long size)
101 do_write_blocked(data, size);
102 finish_record();
106 * The end of tar archives is marked by 2*512 nul bytes and after that
107 * follows the rest of the block (if any).
109 static void write_trailer(void)
111 int tail = BLOCKSIZE - offset;
112 memset(block + offset, 0, tail);
113 write_block(block);
114 if (tail < 2 * RECORDSIZE) {
115 memset(block, 0, offset);
116 write_block(block);
121 * queues up writes, so that all our write(2) calls write exactly one
122 * full block; pads writes to RECORDSIZE
124 static int stream_blocked(struct repository *r, const struct object_id *oid)
126 struct git_istream *st;
127 enum object_type type;
128 unsigned long sz;
129 char buf[BLOCKSIZE];
130 ssize_t readlen;
132 st = open_istream(r, oid, &type, &sz, NULL);
133 if (!st)
134 return error(_("cannot stream blob %s"), oid_to_hex(oid));
135 for (;;) {
136 readlen = read_istream(st, buf, sizeof(buf));
137 if (readlen <= 0)
138 break;
139 do_write_blocked(buf, readlen);
141 close_istream(st);
142 if (!readlen)
143 finish_record();
144 return readlen;
148 * pax extended header records have the format "%u %s=%s\n". %u contains
149 * the size of the whole string (including the %u), the first %s is the
150 * keyword, the second one is the value. This function constructs such a
151 * string and appends it to a struct strbuf.
153 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
154 const char *value, size_t valuelen)
156 size_t orig_len = sb->len;
157 size_t len, tmp;
159 /* "%u %s=%s\n" */
160 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
161 for (tmp = 1; len / 10 >= tmp; tmp *= 10)
162 len++;
164 strbuf_grow(sb, len);
165 strbuf_addf(sb, "%"PRIuMAX" %s=", (uintmax_t)len, keyword);
166 strbuf_add(sb, value, valuelen);
167 strbuf_addch(sb, '\n');
169 if (len != sb->len - orig_len)
170 BUG("pax extended header length miscalculated as %"PRIuMAX
171 ", should be %"PRIuMAX,
172 (uintmax_t)len, (uintmax_t)(sb->len - orig_len));
176 * Like strbuf_append_ext_header, but for numeric values.
178 static void strbuf_append_ext_header_uint(struct strbuf *sb,
179 const char *keyword,
180 uintmax_t value)
182 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
183 int len;
185 len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
186 strbuf_append_ext_header(sb, keyword, buf, len);
189 static unsigned int ustar_header_chksum(const struct ustar_header *header)
191 const unsigned char *p = (const unsigned char *)header;
192 unsigned int chksum = 0;
193 while (p < (const unsigned char *)header->chksum)
194 chksum += *p++;
195 chksum += sizeof(header->chksum) * ' ';
196 p += sizeof(header->chksum);
197 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
198 chksum += *p++;
199 return chksum;
202 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
204 size_t i = pathlen;
205 if (i > 1 && path[i - 1] == '/')
206 i--;
207 if (i > maxlen)
208 i = maxlen;
209 do {
210 i--;
211 } while (i > 0 && path[i] != '/');
212 return i;
215 static void prepare_header(struct archiver_args *args,
216 struct ustar_header *header,
217 unsigned int mode, unsigned long size)
219 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
220 xsnprintf(header->size, sizeof(header->size), "%011"PRIoMAX , S_ISREG(mode) ? (uintmax_t)size : (uintmax_t)0);
221 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
223 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
224 xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
225 strlcpy(header->uname, "root", sizeof(header->uname));
226 strlcpy(header->gname, "root", sizeof(header->gname));
227 xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
228 xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
230 memcpy(header->magic, "ustar", 6);
231 memcpy(header->version, "00", 2);
233 xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
236 static void write_extended_header(struct archiver_args *args,
237 const struct object_id *oid,
238 const void *buffer, unsigned long size)
240 struct ustar_header header;
241 unsigned int mode;
242 memset(&header, 0, sizeof(header));
243 *header.typeflag = TYPEFLAG_EXT_HEADER;
244 mode = 0100666;
245 xsnprintf(header.name, sizeof(header.name), "%s.paxheader", oid_to_hex(oid));
246 prepare_header(args, &header, mode, size);
247 write_blocked(&header, sizeof(header));
248 write_blocked(buffer, size);
251 static int write_tar_entry(struct archiver_args *args,
252 const struct object_id *oid,
253 const char *path, size_t pathlen,
254 unsigned int mode,
255 void *buffer, unsigned long size)
257 struct ustar_header header;
258 struct strbuf ext_header = STRBUF_INIT;
259 unsigned long size_in_header;
260 int err = 0;
262 memset(&header, 0, sizeof(header));
264 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
265 *header.typeflag = TYPEFLAG_DIR;
266 mode = (mode | 0777) & ~tar_umask;
267 } else if (S_ISLNK(mode)) {
268 *header.typeflag = TYPEFLAG_LNK;
269 mode |= 0777;
270 } else if (S_ISREG(mode)) {
271 *header.typeflag = TYPEFLAG_REG;
272 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
273 } else {
274 return error(_("unsupported file mode: 0%o (SHA1: %s)"),
275 mode, oid_to_hex(oid));
277 if (pathlen > sizeof(header.name)) {
278 size_t plen = get_path_prefix(path, pathlen,
279 sizeof(header.prefix));
280 size_t rest = pathlen - plen - 1;
281 if (plen > 0 && rest <= sizeof(header.name)) {
282 memcpy(header.prefix, path, plen);
283 memcpy(header.name, path + plen + 1, rest);
284 } else {
285 xsnprintf(header.name, sizeof(header.name), "%s.data",
286 oid_to_hex(oid));
287 strbuf_append_ext_header(&ext_header, "path",
288 path, pathlen);
290 } else
291 memcpy(header.name, path, pathlen);
293 if (S_ISLNK(mode)) {
294 if (size > sizeof(header.linkname)) {
295 xsnprintf(header.linkname, sizeof(header.linkname),
296 "see %s.paxheader", oid_to_hex(oid));
297 strbuf_append_ext_header(&ext_header, "linkpath",
298 buffer, size);
299 } else
300 memcpy(header.linkname, buffer, size);
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);
309 prepare_header(args, &header, mode, size_in_header);
311 if (ext_header.len > 0) {
312 write_extended_header(args, oid, ext_header.buf,
313 ext_header.len);
315 strbuf_release(&ext_header);
316 write_blocked(&header, sizeof(header));
317 if (S_ISREG(mode) && size > 0) {
318 if (buffer)
319 write_blocked(buffer, size);
320 else
321 err = stream_blocked(args->repo, oid);
323 return err;
326 static void write_global_extended_header(struct archiver_args *args)
328 const struct object_id *oid = args->commit_oid;
329 struct strbuf ext_header = STRBUF_INIT;
330 struct ustar_header header;
331 unsigned int mode;
333 if (oid)
334 strbuf_append_ext_header(&ext_header, "comment",
335 oid_to_hex(oid),
336 the_hash_algo->hexsz);
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;
343 if (!ext_header.len)
344 return;
346 memset(&header, 0, sizeof(header));
347 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
348 mode = 0100666;
349 xsnprintf(header.name, sizeof(header.name), "pax_global_header");
350 prepare_header(args, &header, mode, ext_header.len);
351 write_blocked(&header, sizeof(header));
352 write_blocked(ext_header.buf, ext_header.len);
353 strbuf_release(&ext_header);
356 static struct archiver **tar_filters;
357 static int nr_tar_filters;
358 static int alloc_tar_filters;
360 static struct archiver *find_tar_filter(const char *name, size_t len)
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;
368 return NULL;
371 static int tar_filter_config(const char *var, const char *value,
372 void *data UNUSED)
374 struct archiver *ar;
375 const char *name;
376 const char *type;
377 size_t namelen;
379 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
380 return 0;
382 ar = find_tar_filter(name, namelen);
383 if (!ar) {
384 CALLOC_ARRAY(ar, 1);
385 ar->name = xmemdupz(name, namelen);
386 ar->write_archive = write_tar_filter_archive;
387 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS |
388 ARCHIVER_HIGH_COMPRESSION_LEVELS;
389 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
390 tar_filters[nr_tar_filters++] = ar;
393 if (!strcmp(type, "command")) {
394 if (!value)
395 return config_error_nonbool(var);
396 free(ar->filter_command);
397 ar->filter_command = xstrdup(value);
398 return 0;
400 if (!strcmp(type, "remote")) {
401 if (git_config_bool(var, value))
402 ar->flags |= ARCHIVER_REMOTE;
403 else
404 ar->flags &= ~ARCHIVER_REMOTE;
405 return 0;
408 return 0;
411 static int git_tar_config(const char *var, const char *value, void *cb)
413 if (!strcmp(var, "tar.umask")) {
414 if (value && !strcmp(value, "user")) {
415 tar_umask = umask(0);
416 umask(tar_umask);
417 } else {
418 tar_umask = git_config_int(var, value);
420 return 0;
423 return tar_filter_config(var, value, cb);
426 static int write_tar_archive(const struct archiver *ar UNUSED,
427 struct archiver_args *args)
429 int err = 0;
431 write_global_extended_header(args);
432 err = write_archive_entries(args, write_tar_entry);
433 if (!err)
434 write_trailer();
435 return err;
438 static git_zstream gzstream;
439 static unsigned char outbuf[16384];
441 static void tgz_deflate(int flush)
443 while (gzstream.avail_in || flush == Z_FINISH) {
444 int status = git_deflate(&gzstream, flush);
445 if (!gzstream.avail_out || status == Z_STREAM_END) {
446 write_or_die(1, outbuf, gzstream.next_out - outbuf);
447 gzstream.next_out = outbuf;
448 gzstream.avail_out = sizeof(outbuf);
449 if (status == Z_STREAM_END)
450 break;
452 if (status != Z_OK && status != Z_BUF_ERROR)
453 die(_("deflate error (%d)"), status);
457 static void tgz_write_block(const void *data)
459 gzstream.next_in = (void *)data;
460 gzstream.avail_in = BLOCKSIZE;
461 tgz_deflate(Z_NO_FLUSH);
464 static const char internal_gzip_command[] = "git archive gzip";
466 static int write_tar_filter_archive(const struct archiver *ar,
467 struct archiver_args *args)
469 #if ZLIB_VERNUM >= 0x1221
470 struct gz_header_s gzhead = { .os = 3 }; /* Unix, for reproducibility */
471 #endif
472 struct strbuf cmd = STRBUF_INIT;
473 struct child_process filter = CHILD_PROCESS_INIT;
474 int r;
476 if (!ar->filter_command)
477 BUG("tar-filter archiver called with no filter defined");
479 if (!strcmp(ar->filter_command, internal_gzip_command)) {
480 write_block = tgz_write_block;
481 git_deflate_init_gzip(&gzstream, args->compression_level);
482 #if ZLIB_VERNUM >= 0x1221
483 if (deflateSetHeader(&gzstream.z, &gzhead) != Z_OK)
484 BUG("deflateSetHeader() called too late");
485 #endif
486 gzstream.next_out = outbuf;
487 gzstream.avail_out = sizeof(outbuf);
489 r = write_tar_archive(ar, args);
491 tgz_deflate(Z_FINISH);
492 git_deflate_end(&gzstream);
493 return r;
496 strbuf_addstr(&cmd, ar->filter_command);
497 if (args->compression_level >= 0)
498 strbuf_addf(&cmd, " -%d", args->compression_level);
500 strvec_push(&filter.args, cmd.buf);
501 filter.use_shell = 1;
502 filter.in = -1;
503 filter.silent_exec_failure = 1;
505 if (start_command(&filter) < 0)
506 die_errno(_("unable to start '%s' filter"), cmd.buf);
507 close(1);
508 if (dup2(filter.in, 1) < 0)
509 die_errno(_("unable to redirect descriptor"));
510 close(filter.in);
512 r = write_tar_archive(ar, args);
514 close(1);
515 if (finish_command(&filter) != 0)
516 die(_("'%s' filter reported error"), cmd.buf);
518 strbuf_release(&cmd);
519 return r;
522 static struct archiver tar_archiver = {
523 .name = "tar",
524 .write_archive = write_tar_archive,
525 .flags = ARCHIVER_REMOTE,
528 void init_tar_archiver(void)
530 int i;
531 register_archiver(&tar_archiver);
533 tar_filter_config("tar.tgz.command", internal_gzip_command, NULL);
534 tar_filter_config("tar.tgz.remote", "true", NULL);
535 tar_filter_config("tar.tar.gz.command", internal_gzip_command, NULL);
536 tar_filter_config("tar.tar.gz.remote", "true", NULL);
537 git_config(git_tar_config, NULL);
538 for (i = 0; i < nr_tar_filters; i++) {
539 /* omit any filters that never had a command configured */
540 if (tar_filters[i]->filter_command)
541 register_archiver(tar_filters[i]);