wrapper.h: move declarations for wrapper.c functions from cache.h
[git/debian.git] / archive-tar.c
blob16ee133bbf57fd573caf636bf7a0cac371ac4403
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 "gettext.h"
8 #include "hex.h"
9 #include "tar.h"
10 #include "archive.h"
11 #include "object-store.h"
12 #include "streaming.h"
13 #include "run-command.h"
15 #define RECORDSIZE (512)
16 #define BLOCKSIZE (RECORDSIZE * 20)
18 static char block[BLOCKSIZE];
19 static unsigned long offset;
21 static int tar_umask = 002;
23 static int write_tar_filter_archive(const struct archiver *ar,
24 struct archiver_args *args);
27 * This is the max value that a ustar size header can specify, as it is fixed
28 * at 11 octal digits. POSIX specifies that we switch to extended headers at
29 * this size.
31 * Likewise for the mtime (which happens to use a buffer of the same size).
33 #if ULONG_MAX == 0xFFFFFFFF
34 #define USTAR_MAX_SIZE ULONG_MAX
35 #else
36 #define USTAR_MAX_SIZE 077777777777UL
37 #endif
38 #if TIME_MAX == 0xFFFFFFFF
39 #define USTAR_MAX_MTIME TIME_MAX
40 #else
41 #define USTAR_MAX_MTIME 077777777777ULL
42 #endif
44 static void tar_write_block(const void *buf)
46 write_or_die(1, buf, BLOCKSIZE);
49 static void (*write_block)(const void *) = tar_write_block;
51 /* writes out the whole block, but only if it is full */
52 static void write_if_needed(void)
54 if (offset == BLOCKSIZE) {
55 write_block(block);
56 offset = 0;
61 * queues up writes, so that all our write(2) calls write exactly one
62 * full block; pads writes to RECORDSIZE
64 static void do_write_blocked(const void *data, unsigned long size)
66 const char *buf = data;
68 if (offset) {
69 unsigned long chunk = BLOCKSIZE - offset;
70 if (size < chunk)
71 chunk = size;
72 memcpy(block + offset, buf, chunk);
73 size -= chunk;
74 offset += chunk;
75 buf += chunk;
76 write_if_needed();
78 while (size >= BLOCKSIZE) {
79 write_block(buf);
80 size -= BLOCKSIZE;
81 buf += BLOCKSIZE;
83 if (size) {
84 memcpy(block + offset, buf, size);
85 offset += size;
89 static void finish_record(void)
91 unsigned long tail;
92 tail = offset % RECORDSIZE;
93 if (tail) {
94 memset(block + offset, 0, RECORDSIZE - tail);
95 offset += RECORDSIZE - tail;
97 write_if_needed();
100 static void write_blocked(const void *data, unsigned long size)
102 do_write_blocked(data, size);
103 finish_record();
107 * The end of tar archives is marked by 2*512 nul bytes and after that
108 * follows the rest of the block (if any).
110 static void write_trailer(void)
112 int tail = BLOCKSIZE - offset;
113 memset(block + offset, 0, tail);
114 write_block(block);
115 if (tail < 2 * RECORDSIZE) {
116 memset(block, 0, offset);
117 write_block(block);
122 * queues up writes, so that all our write(2) calls write exactly one
123 * full block; pads writes to RECORDSIZE
125 static int stream_blocked(struct repository *r, const struct object_id *oid)
127 struct git_istream *st;
128 enum object_type type;
129 unsigned long sz;
130 char buf[BLOCKSIZE];
131 ssize_t readlen;
133 st = open_istream(r, oid, &type, &sz, NULL);
134 if (!st)
135 return error(_("cannot stream blob %s"), oid_to_hex(oid));
136 for (;;) {
137 readlen = read_istream(st, buf, sizeof(buf));
138 if (readlen <= 0)
139 break;
140 do_write_blocked(buf, readlen);
142 close_istream(st);
143 if (!readlen)
144 finish_record();
145 return readlen;
149 * pax extended header records have the format "%u %s=%s\n". %u contains
150 * the size of the whole string (including the %u), the first %s is the
151 * keyword, the second one is the value. This function constructs such a
152 * string and appends it to a struct strbuf.
154 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
155 const char *value, size_t valuelen)
157 size_t orig_len = sb->len;
158 size_t len, tmp;
160 /* "%u %s=%s\n" */
161 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
162 for (tmp = 1; len / 10 >= tmp; tmp *= 10)
163 len++;
165 strbuf_grow(sb, len);
166 strbuf_addf(sb, "%"PRIuMAX" %s=", (uintmax_t)len, keyword);
167 strbuf_add(sb, value, valuelen);
168 strbuf_addch(sb, '\n');
170 if (len != sb->len - orig_len)
171 BUG("pax extended header length miscalculated as %"PRIuMAX
172 ", should be %"PRIuMAX,
173 (uintmax_t)len, (uintmax_t)(sb->len - orig_len));
177 * Like strbuf_append_ext_header, but for numeric values.
179 static void strbuf_append_ext_header_uint(struct strbuf *sb,
180 const char *keyword,
181 uintmax_t value)
183 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
184 int len;
186 len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
187 strbuf_append_ext_header(sb, keyword, buf, len);
190 static unsigned int ustar_header_chksum(const struct ustar_header *header)
192 const unsigned char *p = (const unsigned char *)header;
193 unsigned int chksum = 0;
194 while (p < (const unsigned char *)header->chksum)
195 chksum += *p++;
196 chksum += sizeof(header->chksum) * ' ';
197 p += sizeof(header->chksum);
198 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
199 chksum += *p++;
200 return chksum;
203 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
205 size_t i = pathlen;
206 if (i > 1 && path[i - 1] == '/')
207 i--;
208 if (i > maxlen)
209 i = maxlen;
210 do {
211 i--;
212 } while (i > 0 && path[i] != '/');
213 return i;
216 static void prepare_header(struct archiver_args *args,
217 struct ustar_header *header,
218 unsigned int mode, unsigned long size)
220 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
221 xsnprintf(header->size, sizeof(header->size), "%011"PRIoMAX , S_ISREG(mode) ? (uintmax_t)size : (uintmax_t)0);
222 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
224 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
225 xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
226 strlcpy(header->uname, "root", sizeof(header->uname));
227 strlcpy(header->gname, "root", sizeof(header->gname));
228 xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
229 xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
231 memcpy(header->magic, "ustar", 6);
232 memcpy(header->version, "00", 2);
234 xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
237 static void write_extended_header(struct archiver_args *args,
238 const struct object_id *oid,
239 const void *buffer, unsigned long size)
241 struct ustar_header header;
242 unsigned int mode;
243 memset(&header, 0, sizeof(header));
244 *header.typeflag = TYPEFLAG_EXT_HEADER;
245 mode = 0100666;
246 xsnprintf(header.name, sizeof(header.name), "%s.paxheader", oid_to_hex(oid));
247 prepare_header(args, &header, mode, size);
248 write_blocked(&header, sizeof(header));
249 write_blocked(buffer, size);
252 static int write_tar_entry(struct archiver_args *args,
253 const struct object_id *oid,
254 const char *path, size_t pathlen,
255 unsigned int mode,
256 void *buffer, unsigned long size)
258 struct ustar_header header;
259 struct strbuf ext_header = STRBUF_INIT;
260 unsigned long size_in_header;
261 int err = 0;
263 memset(&header, 0, sizeof(header));
265 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
266 *header.typeflag = TYPEFLAG_DIR;
267 mode = (mode | 0777) & ~tar_umask;
268 } else if (S_ISLNK(mode)) {
269 *header.typeflag = TYPEFLAG_LNK;
270 mode |= 0777;
271 } else if (S_ISREG(mode)) {
272 *header.typeflag = TYPEFLAG_REG;
273 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
274 } else {
275 return error(_("unsupported file mode: 0%o (SHA1: %s)"),
276 mode, oid_to_hex(oid));
278 if (pathlen > sizeof(header.name)) {
279 size_t plen = get_path_prefix(path, pathlen,
280 sizeof(header.prefix));
281 size_t rest = pathlen - plen - 1;
282 if (plen > 0 && rest <= sizeof(header.name)) {
283 memcpy(header.prefix, path, plen);
284 memcpy(header.name, path + plen + 1, rest);
285 } else {
286 xsnprintf(header.name, sizeof(header.name), "%s.data",
287 oid_to_hex(oid));
288 strbuf_append_ext_header(&ext_header, "path",
289 path, pathlen);
291 } else
292 memcpy(header.name, path, pathlen);
294 if (S_ISLNK(mode)) {
295 if (size > sizeof(header.linkname)) {
296 xsnprintf(header.linkname, sizeof(header.linkname),
297 "see %s.paxheader", oid_to_hex(oid));
298 strbuf_append_ext_header(&ext_header, "linkpath",
299 buffer, size);
300 } else
301 memcpy(header.linkname, buffer, size);
304 size_in_header = size;
305 if (S_ISREG(mode) && size > USTAR_MAX_SIZE) {
306 size_in_header = 0;
307 strbuf_append_ext_header_uint(&ext_header, "size", size);
310 prepare_header(args, &header, mode, size_in_header);
312 if (ext_header.len > 0) {
313 write_extended_header(args, oid, ext_header.buf,
314 ext_header.len);
316 strbuf_release(&ext_header);
317 write_blocked(&header, sizeof(header));
318 if (S_ISREG(mode) && size > 0) {
319 if (buffer)
320 write_blocked(buffer, size);
321 else
322 err = stream_blocked(args->repo, oid);
324 return err;
327 static void write_global_extended_header(struct archiver_args *args)
329 const struct object_id *oid = args->commit_oid;
330 struct strbuf ext_header = STRBUF_INIT;
331 struct ustar_header header;
332 unsigned int mode;
334 if (oid)
335 strbuf_append_ext_header(&ext_header, "comment",
336 oid_to_hex(oid),
337 the_hash_algo->hexsz);
338 if (args->time > USTAR_MAX_MTIME) {
339 strbuf_append_ext_header_uint(&ext_header, "mtime",
340 args->time);
341 args->time = USTAR_MAX_MTIME;
344 if (!ext_header.len)
345 return;
347 memset(&header, 0, sizeof(header));
348 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
349 mode = 0100666;
350 xsnprintf(header.name, sizeof(header.name), "pax_global_header");
351 prepare_header(args, &header, mode, ext_header.len);
352 write_blocked(&header, sizeof(header));
353 write_blocked(ext_header.buf, ext_header.len);
354 strbuf_release(&ext_header);
357 static struct archiver **tar_filters;
358 static int nr_tar_filters;
359 static int alloc_tar_filters;
361 static struct archiver *find_tar_filter(const char *name, size_t len)
363 int i;
364 for (i = 0; i < nr_tar_filters; i++) {
365 struct archiver *ar = tar_filters[i];
366 if (!strncmp(ar->name, name, len) && !ar->name[len])
367 return ar;
369 return NULL;
372 static int tar_filter_config(const char *var, const char *value,
373 void *data UNUSED)
375 struct archiver *ar;
376 const char *name;
377 const char *type;
378 size_t namelen;
380 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
381 return 0;
383 ar = find_tar_filter(name, namelen);
384 if (!ar) {
385 CALLOC_ARRAY(ar, 1);
386 ar->name = xmemdupz(name, namelen);
387 ar->write_archive = write_tar_filter_archive;
388 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS |
389 ARCHIVER_HIGH_COMPRESSION_LEVELS;
390 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
391 tar_filters[nr_tar_filters++] = ar;
394 if (!strcmp(type, "command")) {
395 if (!value)
396 return config_error_nonbool(var);
397 free(ar->filter_command);
398 ar->filter_command = xstrdup(value);
399 return 0;
401 if (!strcmp(type, "remote")) {
402 if (git_config_bool(var, value))
403 ar->flags |= ARCHIVER_REMOTE;
404 else
405 ar->flags &= ~ARCHIVER_REMOTE;
406 return 0;
409 return 0;
412 static int git_tar_config(const char *var, const char *value, void *cb)
414 if (!strcmp(var, "tar.umask")) {
415 if (value && !strcmp(value, "user")) {
416 tar_umask = umask(0);
417 umask(tar_umask);
418 } else {
419 tar_umask = git_config_int(var, value);
421 return 0;
424 return tar_filter_config(var, value, cb);
427 static int write_tar_archive(const struct archiver *ar UNUSED,
428 struct archiver_args *args)
430 int err = 0;
432 write_global_extended_header(args);
433 err = write_archive_entries(args, write_tar_entry);
434 if (!err)
435 write_trailer();
436 return err;
439 static git_zstream gzstream;
440 static unsigned char outbuf[16384];
442 static void tgz_deflate(int flush)
444 while (gzstream.avail_in || flush == Z_FINISH) {
445 int status = git_deflate(&gzstream, flush);
446 if (!gzstream.avail_out || status == Z_STREAM_END) {
447 write_or_die(1, outbuf, gzstream.next_out - outbuf);
448 gzstream.next_out = outbuf;
449 gzstream.avail_out = sizeof(outbuf);
450 if (status == Z_STREAM_END)
451 break;
453 if (status != Z_OK && status != Z_BUF_ERROR)
454 die(_("deflate error (%d)"), status);
458 static void tgz_write_block(const void *data)
460 gzstream.next_in = (void *)data;
461 gzstream.avail_in = BLOCKSIZE;
462 tgz_deflate(Z_NO_FLUSH);
465 static const char internal_gzip_command[] = "git archive gzip";
467 static int write_tar_filter_archive(const struct archiver *ar,
468 struct archiver_args *args)
470 #if ZLIB_VERNUM >= 0x1221
471 struct gz_header_s gzhead = { .os = 3 }; /* Unix, for reproducibility */
472 #endif
473 struct strbuf cmd = STRBUF_INIT;
474 struct child_process filter = CHILD_PROCESS_INIT;
475 int r;
477 if (!ar->filter_command)
478 BUG("tar-filter archiver called with no filter defined");
480 if (!strcmp(ar->filter_command, internal_gzip_command)) {
481 write_block = tgz_write_block;
482 git_deflate_init_gzip(&gzstream, args->compression_level);
483 #if ZLIB_VERNUM >= 0x1221
484 if (deflateSetHeader(&gzstream.z, &gzhead) != Z_OK)
485 BUG("deflateSetHeader() called too late");
486 #endif
487 gzstream.next_out = outbuf;
488 gzstream.avail_out = sizeof(outbuf);
490 r = write_tar_archive(ar, args);
492 tgz_deflate(Z_FINISH);
493 git_deflate_end(&gzstream);
494 return r;
497 strbuf_addstr(&cmd, ar->filter_command);
498 if (args->compression_level >= 0)
499 strbuf_addf(&cmd, " -%d", args->compression_level);
501 strvec_push(&filter.args, cmd.buf);
502 filter.use_shell = 1;
503 filter.in = -1;
504 filter.silent_exec_failure = 1;
506 if (start_command(&filter) < 0)
507 die_errno(_("unable to start '%s' filter"), cmd.buf);
508 close(1);
509 if (dup2(filter.in, 1) < 0)
510 die_errno(_("unable to redirect descriptor"));
511 close(filter.in);
513 r = write_tar_archive(ar, args);
515 close(1);
516 if (finish_command(&filter) != 0)
517 die(_("'%s' filter reported error"), cmd.buf);
519 strbuf_release(&cmd);
520 return r;
523 static struct archiver tar_archiver = {
524 .name = "tar",
525 .write_archive = write_tar_archive,
526 .flags = ARCHIVER_REMOTE,
529 void init_tar_archiver(void)
531 int i;
532 register_archiver(&tar_archiver);
534 tar_filter_config("tar.tgz.command", internal_gzip_command, NULL);
535 tar_filter_config("tar.tgz.remote", "true", NULL);
536 tar_filter_config("tar.tar.gz.command", internal_gzip_command, NULL);
537 tar_filter_config("tar.tar.gz.remote", "true", NULL);
538 git_config(git_tar_config, NULL);
539 for (i = 0; i < nr_tar_filters; i++) {
540 /* omit any filters that never had a command configured */
541 if (tar_filters[i]->filter_command)
542 register_archiver(tar_filters[i]);