t/t0211-trace2-perf.sh: fix typo patern -> pattern
[alt-git.git] / archive-tar.c
blob3e4822b68409b85d36436bd42da862991274cd81
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "cache.h"
5 #include "config.h"
6 #include "tar.h"
7 #include "archive.h"
8 #include "object-store.h"
9 #include "streaming.h"
10 #include "run-command.h"
12 #define RECORDSIZE (512)
13 #define BLOCKSIZE (RECORDSIZE * 20)
15 static char block[BLOCKSIZE];
16 static unsigned long offset;
18 static int tar_umask = 002;
20 static int write_tar_filter_archive(const struct archiver *ar,
21 struct archiver_args *args);
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.
28 * Likewise for the mtime (which happens to use a buffer of the same size).
30 #if ULONG_MAX == 0xFFFFFFFF
31 #define USTAR_MAX_SIZE ULONG_MAX
32 #else
33 #define USTAR_MAX_SIZE 077777777777UL
34 #endif
35 #if TIME_MAX == 0xFFFFFFFF
36 #define USTAR_MAX_MTIME TIME_MAX
37 #else
38 #define USTAR_MAX_MTIME 077777777777ULL
39 #endif
41 static void tar_write_block(const void *buf)
43 write_or_die(1, buf, BLOCKSIZE);
46 static void (*write_block)(const void *) = tar_write_block;
48 /* writes out the whole block, but only if it is full */
49 static void write_if_needed(void)
51 if (offset == BLOCKSIZE) {
52 write_block(block);
53 offset = 0;
58 * queues up writes, so that all our write(2) calls write exactly one
59 * full block; pads writes to RECORDSIZE
61 static void do_write_blocked(const void *data, unsigned long size)
63 const char *buf = data;
65 if (offset) {
66 unsigned long chunk = BLOCKSIZE - offset;
67 if (size < chunk)
68 chunk = size;
69 memcpy(block + offset, buf, chunk);
70 size -= chunk;
71 offset += chunk;
72 buf += chunk;
73 write_if_needed();
75 while (size >= BLOCKSIZE) {
76 write_block(buf);
77 size -= BLOCKSIZE;
78 buf += BLOCKSIZE;
80 if (size) {
81 memcpy(block + offset, buf, size);
82 offset += size;
86 static void finish_record(void)
88 unsigned long tail;
89 tail = offset % RECORDSIZE;
90 if (tail) {
91 memset(block + offset, 0, RECORDSIZE - tail);
92 offset += RECORDSIZE - tail;
94 write_if_needed();
97 static void write_blocked(const void *data, unsigned long size)
99 do_write_blocked(data, size);
100 finish_record();
104 * The end of tar archives is marked by 2*512 nul bytes and after that
105 * follows the rest of the block (if any).
107 static void write_trailer(void)
109 int tail = BLOCKSIZE - offset;
110 memset(block + offset, 0, tail);
111 write_block(block);
112 if (tail < 2 * RECORDSIZE) {
113 memset(block, 0, offset);
114 write_block(block);
119 * queues up writes, so that all our write(2) calls write exactly one
120 * full block; pads writes to RECORDSIZE
122 static int stream_blocked(struct repository *r, const struct object_id *oid)
124 struct git_istream *st;
125 enum object_type type;
126 unsigned long sz;
127 char buf[BLOCKSIZE];
128 ssize_t readlen;
130 st = open_istream(r, oid, &type, &sz, NULL);
131 if (!st)
132 return error(_("cannot stream blob %s"), oid_to_hex(oid));
133 for (;;) {
134 readlen = read_istream(st, buf, sizeof(buf));
135 if (readlen <= 0)
136 break;
137 do_write_blocked(buf, readlen);
139 close_istream(st);
140 if (!readlen)
141 finish_record();
142 return readlen;
146 * pax extended header records have the format "%u %s=%s\n". %u contains
147 * the size of the whole string (including the %u), the first %s is the
148 * keyword, the second one is the value. This function constructs such a
149 * string and appends it to a struct strbuf.
151 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
152 const char *value, size_t valuelen)
154 size_t orig_len = sb->len;
155 size_t len, tmp;
157 /* "%u %s=%s\n" */
158 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
159 for (tmp = 1; len / 10 >= tmp; tmp *= 10)
160 len++;
162 strbuf_grow(sb, len);
163 strbuf_addf(sb, "%"PRIuMAX" %s=", (uintmax_t)len, keyword);
164 strbuf_add(sb, value, valuelen);
165 strbuf_addch(sb, '\n');
167 if (len != sb->len - orig_len)
168 BUG("pax extended header length miscalculated as %"PRIuMAX
169 ", should be %"PRIuMAX,
170 (uintmax_t)len, (uintmax_t)(sb->len - orig_len));
174 * Like strbuf_append_ext_header, but for numeric values.
176 static void strbuf_append_ext_header_uint(struct strbuf *sb,
177 const char *keyword,
178 uintmax_t value)
180 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
181 int len;
183 len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
184 strbuf_append_ext_header(sb, keyword, buf, len);
187 static unsigned int ustar_header_chksum(const struct ustar_header *header)
189 const unsigned char *p = (const unsigned char *)header;
190 unsigned int chksum = 0;
191 while (p < (const unsigned char *)header->chksum)
192 chksum += *p++;
193 chksum += sizeof(header->chksum) * ' ';
194 p += sizeof(header->chksum);
195 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
196 chksum += *p++;
197 return chksum;
200 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
202 size_t i = pathlen;
203 if (i > 1 && path[i - 1] == '/')
204 i--;
205 if (i > maxlen)
206 i = maxlen;
207 do {
208 i--;
209 } while (i > 0 && path[i] != '/');
210 return i;
213 static void prepare_header(struct archiver_args *args,
214 struct ustar_header *header,
215 unsigned int mode, unsigned long size)
217 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
218 xsnprintf(header->size, sizeof(header->size), "%011"PRIoMAX , S_ISREG(mode) ? (uintmax_t)size : (uintmax_t)0);
219 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
221 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
222 xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
223 strlcpy(header->uname, "root", sizeof(header->uname));
224 strlcpy(header->gname, "root", sizeof(header->gname));
225 xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
226 xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
228 memcpy(header->magic, "ustar", 6);
229 memcpy(header->version, "00", 2);
231 xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
234 static void write_extended_header(struct archiver_args *args,
235 const struct object_id *oid,
236 const void *buffer, unsigned long size)
238 struct ustar_header header;
239 unsigned int mode;
240 memset(&header, 0, sizeof(header));
241 *header.typeflag = TYPEFLAG_EXT_HEADER;
242 mode = 0100666;
243 xsnprintf(header.name, sizeof(header.name), "%s.paxheader", oid_to_hex(oid));
244 prepare_header(args, &header, mode, size);
245 write_blocked(&header, sizeof(header));
246 write_blocked(buffer, size);
249 static int write_tar_entry(struct archiver_args *args,
250 const struct object_id *oid,
251 const char *path, size_t pathlen,
252 unsigned int mode,
253 void *buffer, unsigned long size)
255 struct ustar_header header;
256 struct strbuf ext_header = STRBUF_INIT;
257 unsigned long size_in_header;
258 int err = 0;
260 memset(&header, 0, sizeof(header));
262 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
263 *header.typeflag = TYPEFLAG_DIR;
264 mode = (mode | 0777) & ~tar_umask;
265 } else if (S_ISLNK(mode)) {
266 *header.typeflag = TYPEFLAG_LNK;
267 mode |= 0777;
268 } else if (S_ISREG(mode)) {
269 *header.typeflag = TYPEFLAG_REG;
270 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
271 } else {
272 return error(_("unsupported file mode: 0%o (SHA1: %s)"),
273 mode, oid_to_hex(oid));
275 if (pathlen > sizeof(header.name)) {
276 size_t plen = get_path_prefix(path, pathlen,
277 sizeof(header.prefix));
278 size_t rest = pathlen - plen - 1;
279 if (plen > 0 && rest <= sizeof(header.name)) {
280 memcpy(header.prefix, path, plen);
281 memcpy(header.name, path + plen + 1, rest);
282 } else {
283 xsnprintf(header.name, sizeof(header.name), "%s.data",
284 oid_to_hex(oid));
285 strbuf_append_ext_header(&ext_header, "path",
286 path, pathlen);
288 } else
289 memcpy(header.name, path, pathlen);
291 if (S_ISLNK(mode)) {
292 if (size > sizeof(header.linkname)) {
293 xsnprintf(header.linkname, sizeof(header.linkname),
294 "see %s.paxheader", oid_to_hex(oid));
295 strbuf_append_ext_header(&ext_header, "linkpath",
296 buffer, size);
297 } else
298 memcpy(header.linkname, buffer, size);
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);
307 prepare_header(args, &header, mode, size_in_header);
309 if (ext_header.len > 0) {
310 write_extended_header(args, oid, ext_header.buf,
311 ext_header.len);
313 strbuf_release(&ext_header);
314 write_blocked(&header, sizeof(header));
315 if (S_ISREG(mode) && size > 0) {
316 if (buffer)
317 write_blocked(buffer, size);
318 else
319 err = stream_blocked(args->repo, oid);
321 return err;
324 static void write_global_extended_header(struct archiver_args *args)
326 const struct object_id *oid = args->commit_oid;
327 struct strbuf ext_header = STRBUF_INIT;
328 struct ustar_header header;
329 unsigned int mode;
331 if (oid)
332 strbuf_append_ext_header(&ext_header, "comment",
333 oid_to_hex(oid),
334 the_hash_algo->hexsz);
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;
341 if (!ext_header.len)
342 return;
344 memset(&header, 0, sizeof(header));
345 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
346 mode = 0100666;
347 xsnprintf(header.name, sizeof(header.name), "pax_global_header");
348 prepare_header(args, &header, mode, ext_header.len);
349 write_blocked(&header, sizeof(header));
350 write_blocked(ext_header.buf, ext_header.len);
351 strbuf_release(&ext_header);
354 static struct archiver **tar_filters;
355 static int nr_tar_filters;
356 static int alloc_tar_filters;
358 static struct archiver *find_tar_filter(const char *name, size_t len)
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;
366 return NULL;
369 static int tar_filter_config(const char *var, const char *value,
370 void *data UNUSED)
372 struct archiver *ar;
373 const char *name;
374 const char *type;
375 size_t namelen;
377 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
378 return 0;
380 ar = find_tar_filter(name, namelen);
381 if (!ar) {
382 CALLOC_ARRAY(ar, 1);
383 ar->name = xmemdupz(name, namelen);
384 ar->write_archive = write_tar_filter_archive;
385 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS |
386 ARCHIVER_HIGH_COMPRESSION_LEVELS;
387 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
388 tar_filters[nr_tar_filters++] = ar;
391 if (!strcmp(type, "command")) {
392 if (!value)
393 return config_error_nonbool(var);
394 free(ar->filter_command);
395 ar->filter_command = xstrdup(value);
396 return 0;
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;
406 return 0;
409 static int git_tar_config(const char *var, const char *value, void *cb)
411 if (!strcmp(var, "tar.umask")) {
412 if (value && !strcmp(value, "user")) {
413 tar_umask = umask(0);
414 umask(tar_umask);
415 } else {
416 tar_umask = git_config_int(var, value);
418 return 0;
421 return tar_filter_config(var, value, cb);
424 static int write_tar_archive(const struct archiver *ar UNUSED,
425 struct archiver_args *args)
427 int err = 0;
429 write_global_extended_header(args);
430 err = write_archive_entries(args, write_tar_entry);
431 if (!err)
432 write_trailer();
433 return err;
436 static git_zstream gzstream;
437 static unsigned char outbuf[16384];
439 static void tgz_deflate(int flush)
441 while (gzstream.avail_in || flush == Z_FINISH) {
442 int status = git_deflate(&gzstream, flush);
443 if (!gzstream.avail_out || status == Z_STREAM_END) {
444 write_or_die(1, outbuf, gzstream.next_out - outbuf);
445 gzstream.next_out = outbuf;
446 gzstream.avail_out = sizeof(outbuf);
447 if (status == Z_STREAM_END)
448 break;
450 if (status != Z_OK && status != Z_BUF_ERROR)
451 die(_("deflate error (%d)"), status);
455 static void tgz_write_block(const void *data)
457 gzstream.next_in = (void *)data;
458 gzstream.avail_in = BLOCKSIZE;
459 tgz_deflate(Z_NO_FLUSH);
462 static const char internal_gzip_command[] = "git archive gzip";
464 static int write_tar_filter_archive(const struct archiver *ar,
465 struct archiver_args *args)
467 #if ZLIB_VERNUM >= 0x1221
468 struct gz_header_s gzhead = { .os = 3 }; /* Unix, for reproducibility */
469 #endif
470 struct strbuf cmd = STRBUF_INIT;
471 struct child_process filter = CHILD_PROCESS_INIT;
472 int r;
474 if (!ar->filter_command)
475 BUG("tar-filter archiver called with no filter defined");
477 if (!strcmp(ar->filter_command, internal_gzip_command)) {
478 write_block = tgz_write_block;
479 git_deflate_init_gzip(&gzstream, args->compression_level);
480 #if ZLIB_VERNUM >= 0x1221
481 if (deflateSetHeader(&gzstream.z, &gzhead) != Z_OK)
482 BUG("deflateSetHeader() called too late");
483 #endif
484 gzstream.next_out = outbuf;
485 gzstream.avail_out = sizeof(outbuf);
487 r = write_tar_archive(ar, args);
489 tgz_deflate(Z_FINISH);
490 git_deflate_end(&gzstream);
491 return r;
494 strbuf_addstr(&cmd, ar->filter_command);
495 if (args->compression_level >= 0)
496 strbuf_addf(&cmd, " -%d", args->compression_level);
498 strvec_push(&filter.args, cmd.buf);
499 filter.use_shell = 1;
500 filter.in = -1;
502 if (start_command(&filter) < 0)
503 die_errno(_("unable to start '%s' filter"), cmd.buf);
504 close(1);
505 if (dup2(filter.in, 1) < 0)
506 die_errno(_("unable to redirect descriptor"));
507 close(filter.in);
509 r = write_tar_archive(ar, args);
511 close(1);
512 if (finish_command(&filter) != 0)
513 die(_("'%s' filter reported error"), cmd.buf);
515 strbuf_release(&cmd);
516 return r;
519 static struct archiver tar_archiver = {
520 .name = "tar",
521 .write_archive = write_tar_archive,
522 .flags = ARCHIVER_REMOTE,
525 void init_tar_archiver(void)
527 int i;
528 register_archiver(&tar_archiver);
530 tar_filter_config("tar.tgz.command", internal_gzip_command, NULL);
531 tar_filter_config("tar.tgz.remote", "true", NULL);
532 tar_filter_config("tar.tar.gz.command", internal_gzip_command, NULL);
533 tar_filter_config("tar.tar.gz.remote", "true", NULL);
534 git_config(git_tar_config, NULL);
535 for (i = 0; i < nr_tar_filters; i++) {
536 /* omit any filters that never had a command configured */
537 if (tar_filters[i]->filter_command)
538 register_archiver(tar_filters[i]);