builtin/notes.c: let parse-options parse subcommands
[git/debian.git] / archive-tar.c
blob3d77e0f75095743d3da9874832e2f8bdccf5e896
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, void *data)
371 struct archiver *ar;
372 const char *name;
373 const char *type;
374 size_t namelen;
376 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
377 return 0;
379 ar = find_tar_filter(name, namelen);
380 if (!ar) {
381 CALLOC_ARRAY(ar, 1);
382 ar->name = xmemdupz(name, namelen);
383 ar->write_archive = write_tar_filter_archive;
384 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS |
385 ARCHIVER_HIGH_COMPRESSION_LEVELS;
386 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
387 tar_filters[nr_tar_filters++] = ar;
390 if (!strcmp(type, "command")) {
391 if (!value)
392 return config_error_nonbool(var);
393 free(ar->filter_command);
394 ar->filter_command = xstrdup(value);
395 return 0;
397 if (!strcmp(type, "remote")) {
398 if (git_config_bool(var, value))
399 ar->flags |= ARCHIVER_REMOTE;
400 else
401 ar->flags &= ~ARCHIVER_REMOTE;
402 return 0;
405 return 0;
408 static int git_tar_config(const char *var, const char *value, void *cb)
410 if (!strcmp(var, "tar.umask")) {
411 if (value && !strcmp(value, "user")) {
412 tar_umask = umask(0);
413 umask(tar_umask);
414 } else {
415 tar_umask = git_config_int(var, value);
417 return 0;
420 return tar_filter_config(var, value, cb);
423 static int write_tar_archive(const struct archiver *ar,
424 struct archiver_args *args)
426 int err = 0;
428 write_global_extended_header(args);
429 err = write_archive_entries(args, write_tar_entry);
430 if (!err)
431 write_trailer();
432 return err;
435 static git_zstream gzstream;
436 static unsigned char outbuf[16384];
438 static void tgz_deflate(int flush)
440 while (gzstream.avail_in || flush == Z_FINISH) {
441 int status = git_deflate(&gzstream, flush);
442 if (!gzstream.avail_out || status == Z_STREAM_END) {
443 write_or_die(1, outbuf, gzstream.next_out - outbuf);
444 gzstream.next_out = outbuf;
445 gzstream.avail_out = sizeof(outbuf);
446 if (status == Z_STREAM_END)
447 break;
449 if (status != Z_OK && status != Z_BUF_ERROR)
450 die(_("deflate error (%d)"), status);
454 static void tgz_write_block(const void *data)
456 gzstream.next_in = (void *)data;
457 gzstream.avail_in = BLOCKSIZE;
458 tgz_deflate(Z_NO_FLUSH);
461 static const char internal_gzip_command[] = "git archive gzip";
463 static int write_tar_filter_archive(const struct archiver *ar,
464 struct archiver_args *args)
466 #if ZLIB_VERNUM >= 0x1221
467 struct gz_header_s gzhead = { .os = 3 }; /* Unix, for reproducibility */
468 #endif
469 struct strbuf cmd = STRBUF_INIT;
470 struct child_process filter = CHILD_PROCESS_INIT;
471 int r;
473 if (!ar->filter_command)
474 BUG("tar-filter archiver called with no filter defined");
476 if (!strcmp(ar->filter_command, internal_gzip_command)) {
477 write_block = tgz_write_block;
478 git_deflate_init_gzip(&gzstream, args->compression_level);
479 #if ZLIB_VERNUM >= 0x1221
480 if (deflateSetHeader(&gzstream.z, &gzhead) != Z_OK)
481 BUG("deflateSetHeader() called too late");
482 #endif
483 gzstream.next_out = outbuf;
484 gzstream.avail_out = sizeof(outbuf);
486 r = write_tar_archive(ar, args);
488 tgz_deflate(Z_FINISH);
489 git_deflate_end(&gzstream);
490 return r;
493 strbuf_addstr(&cmd, ar->filter_command);
494 if (args->compression_level >= 0)
495 strbuf_addf(&cmd, " -%d", args->compression_level);
497 strvec_push(&filter.args, cmd.buf);
498 filter.use_shell = 1;
499 filter.in = -1;
501 if (start_command(&filter) < 0)
502 die_errno(_("unable to start '%s' filter"), cmd.buf);
503 close(1);
504 if (dup2(filter.in, 1) < 0)
505 die_errno(_("unable to redirect descriptor"));
506 close(filter.in);
508 r = write_tar_archive(ar, args);
510 close(1);
511 if (finish_command(&filter) != 0)
512 die(_("'%s' filter reported error"), cmd.buf);
514 strbuf_release(&cmd);
515 return r;
518 static struct archiver tar_archiver = {
519 .name = "tar",
520 .write_archive = write_tar_archive,
521 .flags = ARCHIVER_REMOTE,
524 void init_tar_archiver(void)
526 int i;
527 register_archiver(&tar_archiver);
529 tar_filter_config("tar.tgz.command", internal_gzip_command, NULL);
530 tar_filter_config("tar.tgz.remote", "true", NULL);
531 tar_filter_config("tar.tar.gz.command", internal_gzip_command, NULL);
532 tar_filter_config("tar.tar.gz.remote", "true", NULL);
533 git_config(git_tar_config, NULL);
534 for (i = 0; i < nr_tar_filters; i++) {
535 /* omit any filters that never had a command configured */
536 if (tar_filters[i]->filter_command)
537 register_archiver(tar_filters[i]);