Merge branch 'rs/parse-options-with-keep-unknown-abbrev-fix' into maint-2.43
[alt-git.git] / archive-tar.c
blobf2a0ed77523b457fa17497498d3ffb4499773a1f
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "git-compat-util.h"
5 #include "config.h"
6 #include "gettext.h"
7 #include "git-zlib.h"
8 #include "hex.h"
9 #include "tar.h"
10 #include "archive.h"
11 #include "object-store-ll.h"
12 #include "strbuf.h"
13 #include "streaming.h"
14 #include "run-command.h"
15 #include "write-or-die.h"
17 #define RECORDSIZE (512)
18 #define BLOCKSIZE (RECORDSIZE * 20)
20 static char block[BLOCKSIZE];
21 static unsigned long offset;
23 static int tar_umask = 002;
25 static int write_tar_filter_archive(const struct archiver *ar,
26 struct archiver_args *args);
29 * This is the max value that a ustar size header can specify, as it is fixed
30 * at 11 octal digits. POSIX specifies that we switch to extended headers at
31 * this size.
33 * Likewise for the mtime (which happens to use a buffer of the same size).
35 #if ULONG_MAX == 0xFFFFFFFF
36 #define USTAR_MAX_SIZE ULONG_MAX
37 #else
38 #define USTAR_MAX_SIZE 077777777777UL
39 #endif
40 #if TIME_MAX == 0xFFFFFFFF
41 #define USTAR_MAX_MTIME TIME_MAX
42 #else
43 #define USTAR_MAX_MTIME 077777777777ULL
44 #endif
46 static void tar_write_block(const void *buf)
48 write_or_die(1, buf, BLOCKSIZE);
51 static void (*write_block)(const void *) = tar_write_block;
53 /* writes out the whole block, but only if it is full */
54 static void write_if_needed(void)
56 if (offset == BLOCKSIZE) {
57 write_block(block);
58 offset = 0;
63 * queues up writes, so that all our write(2) calls write exactly one
64 * full block; pads writes to RECORDSIZE
66 static void do_write_blocked(const void *data, unsigned long size)
68 const char *buf = data;
70 if (offset) {
71 unsigned long chunk = BLOCKSIZE - offset;
72 if (size < chunk)
73 chunk = size;
74 memcpy(block + offset, buf, chunk);
75 size -= chunk;
76 offset += chunk;
77 buf += chunk;
78 write_if_needed();
80 while (size >= BLOCKSIZE) {
81 write_block(buf);
82 size -= BLOCKSIZE;
83 buf += BLOCKSIZE;
85 if (size) {
86 memcpy(block + offset, buf, size);
87 offset += size;
91 static void finish_record(void)
93 unsigned long tail;
94 tail = offset % RECORDSIZE;
95 if (tail) {
96 memset(block + offset, 0, RECORDSIZE - tail);
97 offset += RECORDSIZE - tail;
99 write_if_needed();
102 static void write_blocked(const void *data, unsigned long size)
104 do_write_blocked(data, size);
105 finish_record();
109 * The end of tar archives is marked by 2*512 nul bytes and after that
110 * follows the rest of the block (if any).
112 static void write_trailer(void)
114 int tail = BLOCKSIZE - offset;
115 memset(block + offset, 0, tail);
116 write_block(block);
117 if (tail < 2 * RECORDSIZE) {
118 memset(block, 0, offset);
119 write_block(block);
124 * queues up writes, so that all our write(2) calls write exactly one
125 * full block; pads writes to RECORDSIZE
127 static int stream_blocked(struct repository *r, const struct object_id *oid)
129 struct git_istream *st;
130 enum object_type type;
131 unsigned long sz;
132 char buf[BLOCKSIZE];
133 ssize_t readlen;
135 st = open_istream(r, oid, &type, &sz, NULL);
136 if (!st)
137 return error(_("cannot stream blob %s"), oid_to_hex(oid));
138 for (;;) {
139 readlen = read_istream(st, buf, sizeof(buf));
140 if (readlen <= 0)
141 break;
142 do_write_blocked(buf, readlen);
144 close_istream(st);
145 if (!readlen)
146 finish_record();
147 return readlen;
151 * pax extended header records have the format "%u %s=%s\n". %u contains
152 * the size of the whole string (including the %u), the first %s is the
153 * keyword, the second one is the value. This function constructs such a
154 * string and appends it to a struct strbuf.
156 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
157 const char *value, size_t valuelen)
159 size_t orig_len = sb->len;
160 size_t len, tmp;
162 /* "%u %s=%s\n" */
163 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
164 for (tmp = 1; len / 10 >= tmp; tmp *= 10)
165 len++;
167 strbuf_grow(sb, len);
168 strbuf_addf(sb, "%"PRIuMAX" %s=", (uintmax_t)len, keyword);
169 strbuf_add(sb, value, valuelen);
170 strbuf_addch(sb, '\n');
172 if (len != sb->len - orig_len)
173 BUG("pax extended header length miscalculated as %"PRIuMAX
174 ", should be %"PRIuMAX,
175 (uintmax_t)len, (uintmax_t)(sb->len - orig_len));
179 * Like strbuf_append_ext_header, but for numeric values.
181 static void strbuf_append_ext_header_uint(struct strbuf *sb,
182 const char *keyword,
183 uintmax_t value)
185 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
186 int len;
188 len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
189 strbuf_append_ext_header(sb, keyword, buf, len);
192 static unsigned int ustar_header_chksum(const struct ustar_header *header)
194 const unsigned char *p = (const unsigned char *)header;
195 unsigned int chksum = 0;
196 while (p < (const unsigned char *)header->chksum)
197 chksum += *p++;
198 chksum += sizeof(header->chksum) * ' ';
199 p += sizeof(header->chksum);
200 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
201 chksum += *p++;
202 return chksum;
205 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
207 size_t i = pathlen;
208 if (i > 1 && path[i - 1] == '/')
209 i--;
210 if (i > maxlen)
211 i = maxlen;
212 do {
213 i--;
214 } while (i > 0 && path[i] != '/');
215 return i;
218 static void prepare_header(struct archiver_args *args,
219 struct ustar_header *header,
220 unsigned int mode, unsigned long size)
222 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
223 xsnprintf(header->size, sizeof(header->size), "%011"PRIoMAX , S_ISREG(mode) ? (uintmax_t)size : (uintmax_t)0);
224 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
226 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
227 xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
228 strlcpy(header->uname, "root", sizeof(header->uname));
229 strlcpy(header->gname, "root", sizeof(header->gname));
230 xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
231 xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
233 memcpy(header->magic, "ustar", 6);
234 memcpy(header->version, "00", 2);
236 xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
239 static void write_extended_header(struct archiver_args *args,
240 const struct object_id *oid,
241 const void *buffer, unsigned long size)
243 struct ustar_header header;
244 unsigned int mode;
245 memset(&header, 0, sizeof(header));
246 *header.typeflag = TYPEFLAG_EXT_HEADER;
247 mode = 0100666;
248 xsnprintf(header.name, sizeof(header.name), "%s.paxheader", oid_to_hex(oid));
249 prepare_header(args, &header, mode, size);
250 write_blocked(&header, sizeof(header));
251 write_blocked(buffer, size);
254 static int write_tar_entry(struct archiver_args *args,
255 const struct object_id *oid,
256 const char *path, size_t pathlen,
257 unsigned int mode,
258 void *buffer, unsigned long size)
260 struct ustar_header header;
261 struct strbuf ext_header = STRBUF_INIT;
262 unsigned long size_in_header;
263 int err = 0;
265 memset(&header, 0, sizeof(header));
267 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
268 *header.typeflag = TYPEFLAG_DIR;
269 mode = (mode | 0777) & ~tar_umask;
270 } else if (S_ISLNK(mode)) {
271 *header.typeflag = TYPEFLAG_LNK;
272 mode |= 0777;
273 } else if (S_ISREG(mode)) {
274 *header.typeflag = TYPEFLAG_REG;
275 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
276 } else {
277 return error(_("unsupported file mode: 0%o (SHA1: %s)"),
278 mode, oid_to_hex(oid));
280 if (pathlen > sizeof(header.name)) {
281 size_t plen = get_path_prefix(path, pathlen,
282 sizeof(header.prefix));
283 size_t rest = pathlen - plen - 1;
284 if (plen > 0 && rest <= sizeof(header.name)) {
285 memcpy(header.prefix, path, plen);
286 memcpy(header.name, path + plen + 1, rest);
287 } else {
288 xsnprintf(header.name, sizeof(header.name), "%s.data",
289 oid_to_hex(oid));
290 strbuf_append_ext_header(&ext_header, "path",
291 path, pathlen);
293 } else
294 memcpy(header.name, path, pathlen);
296 if (S_ISLNK(mode)) {
297 if (size > sizeof(header.linkname)) {
298 xsnprintf(header.linkname, sizeof(header.linkname),
299 "see %s.paxheader", oid_to_hex(oid));
300 strbuf_append_ext_header(&ext_header, "linkpath",
301 buffer, size);
302 } else
303 memcpy(header.linkname, buffer, size);
306 size_in_header = size;
307 if (S_ISREG(mode) && size > USTAR_MAX_SIZE) {
308 size_in_header = 0;
309 strbuf_append_ext_header_uint(&ext_header, "size", size);
312 prepare_header(args, &header, mode, size_in_header);
314 if (ext_header.len > 0) {
315 write_extended_header(args, oid, ext_header.buf,
316 ext_header.len);
318 strbuf_release(&ext_header);
319 write_blocked(&header, sizeof(header));
320 if (S_ISREG(mode) && size > 0) {
321 if (buffer)
322 write_blocked(buffer, size);
323 else
324 err = stream_blocked(args->repo, oid);
326 return err;
329 static void write_global_extended_header(struct archiver_args *args)
331 const struct object_id *oid = args->commit_oid;
332 struct strbuf ext_header = STRBUF_INIT;
333 struct ustar_header header;
334 unsigned int mode;
336 if (oid)
337 strbuf_append_ext_header(&ext_header, "comment",
338 oid_to_hex(oid),
339 the_hash_algo->hexsz);
340 if (args->time > USTAR_MAX_MTIME) {
341 strbuf_append_ext_header_uint(&ext_header, "mtime",
342 args->time);
343 args->time = USTAR_MAX_MTIME;
346 if (!ext_header.len)
347 return;
349 memset(&header, 0, sizeof(header));
350 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
351 mode = 0100666;
352 xsnprintf(header.name, sizeof(header.name), "pax_global_header");
353 prepare_header(args, &header, mode, ext_header.len);
354 write_blocked(&header, sizeof(header));
355 write_blocked(ext_header.buf, ext_header.len);
356 strbuf_release(&ext_header);
359 static struct archiver **tar_filters;
360 static int nr_tar_filters;
361 static int alloc_tar_filters;
363 static struct archiver *find_tar_filter(const char *name, size_t len)
365 int i;
366 for (i = 0; i < nr_tar_filters; i++) {
367 struct archiver *ar = tar_filters[i];
368 if (!strncmp(ar->name, name, len) && !ar->name[len])
369 return ar;
371 return NULL;
374 static int tar_filter_config(const char *var, const char *value,
375 void *data UNUSED)
377 struct archiver *ar;
378 const char *name;
379 const char *type;
380 size_t namelen;
382 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
383 return 0;
385 ar = find_tar_filter(name, namelen);
386 if (!ar) {
387 CALLOC_ARRAY(ar, 1);
388 ar->name = xmemdupz(name, namelen);
389 ar->write_archive = write_tar_filter_archive;
390 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS |
391 ARCHIVER_HIGH_COMPRESSION_LEVELS;
392 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
393 tar_filters[nr_tar_filters++] = ar;
396 if (!strcmp(type, "command")) {
397 if (!value)
398 return config_error_nonbool(var);
399 free(ar->filter_command);
400 ar->filter_command = xstrdup(value);
401 return 0;
403 if (!strcmp(type, "remote")) {
404 if (git_config_bool(var, value))
405 ar->flags |= ARCHIVER_REMOTE;
406 else
407 ar->flags &= ~ARCHIVER_REMOTE;
408 return 0;
411 return 0;
414 static int git_tar_config(const char *var, const char *value,
415 const struct config_context *ctx, void *cb)
417 if (!strcmp(var, "tar.umask")) {
418 if (value && !strcmp(value, "user")) {
419 tar_umask = umask(0);
420 umask(tar_umask);
421 } else {
422 tar_umask = git_config_int(var, value, ctx->kvi);
424 return 0;
427 return tar_filter_config(var, value, cb);
430 static int write_tar_archive(const struct archiver *ar UNUSED,
431 struct archiver_args *args)
433 int err = 0;
435 write_global_extended_header(args);
436 err = write_archive_entries(args, write_tar_entry);
437 if (!err)
438 write_trailer();
439 return err;
442 static git_zstream gzstream;
443 static unsigned char outbuf[16384];
445 static void tgz_deflate(int flush)
447 while (gzstream.avail_in || flush == Z_FINISH) {
448 int status = git_deflate(&gzstream, flush);
449 if (!gzstream.avail_out || status == Z_STREAM_END) {
450 write_or_die(1, outbuf, gzstream.next_out - outbuf);
451 gzstream.next_out = outbuf;
452 gzstream.avail_out = sizeof(outbuf);
453 if (status == Z_STREAM_END)
454 break;
456 if (status != Z_OK && status != Z_BUF_ERROR)
457 die(_("deflate error (%d)"), status);
461 static void tgz_write_block(const void *data)
463 gzstream.next_in = (void *)data;
464 gzstream.avail_in = BLOCKSIZE;
465 tgz_deflate(Z_NO_FLUSH);
468 static const char internal_gzip_command[] = "git archive gzip";
470 static int write_tar_filter_archive(const struct archiver *ar,
471 struct archiver_args *args)
473 #if ZLIB_VERNUM >= 0x1221
474 struct gz_header_s gzhead = { .os = 3 }; /* Unix, for reproducibility */
475 #endif
476 struct strbuf cmd = STRBUF_INIT;
477 struct child_process filter = CHILD_PROCESS_INIT;
478 int r;
480 if (!ar->filter_command)
481 BUG("tar-filter archiver called with no filter defined");
483 if (!strcmp(ar->filter_command, internal_gzip_command)) {
484 write_block = tgz_write_block;
485 git_deflate_init_gzip(&gzstream, args->compression_level);
486 #if ZLIB_VERNUM >= 0x1221
487 if (deflateSetHeader(&gzstream.z, &gzhead) != Z_OK)
488 BUG("deflateSetHeader() called too late");
489 #endif
490 gzstream.next_out = outbuf;
491 gzstream.avail_out = sizeof(outbuf);
493 r = write_tar_archive(ar, args);
495 tgz_deflate(Z_FINISH);
496 git_deflate_end(&gzstream);
497 return r;
500 strbuf_addstr(&cmd, ar->filter_command);
501 if (args->compression_level >= 0)
502 strbuf_addf(&cmd, " -%d", args->compression_level);
504 strvec_push(&filter.args, cmd.buf);
505 filter.use_shell = 1;
506 filter.in = -1;
507 filter.silent_exec_failure = 1;
509 if (start_command(&filter) < 0)
510 die_errno(_("unable to start '%s' filter"), cmd.buf);
511 close(1);
512 if (dup2(filter.in, 1) < 0)
513 die_errno(_("unable to redirect descriptor"));
514 close(filter.in);
516 r = write_tar_archive(ar, args);
518 close(1);
519 if (finish_command(&filter) != 0)
520 die(_("'%s' filter reported error"), cmd.buf);
522 strbuf_release(&cmd);
523 return r;
526 static struct archiver tar_archiver = {
527 .name = "tar",
528 .write_archive = write_tar_archive,
529 .flags = ARCHIVER_REMOTE,
532 void init_tar_archiver(void)
534 int i;
535 register_archiver(&tar_archiver);
537 tar_filter_config("tar.tgz.command", internal_gzip_command, NULL);
538 tar_filter_config("tar.tgz.remote", "true", NULL);
539 tar_filter_config("tar.tar.gz.command", internal_gzip_command, NULL);
540 tar_filter_config("tar.tar.gz.remote", "true", NULL);
541 git_config(git_tar_config, NULL);
542 for (i = 0; i < nr_tar_filters; i++) {
543 /* omit any filters that never had a command configured */
544 if (tar_filters[i]->filter_command)
545 register_archiver(tar_filters[i]);