2 * Copyright (c) 2005, 2006 Rene Scharfe
8 #include "run-command.h"
10 #define RECORDSIZE (512)
11 #define BLOCKSIZE (RECORDSIZE * 20)
13 static char block[BLOCKSIZE];
14 static unsigned long offset;
16 static int tar_umask = 002;
18 static int write_tar_filter_archive(const struct archiver *ar,
19 struct archiver_args *args);
22 * This is the max value that a ustar size header can specify, as it is fixed
23 * at 11 octal digits. POSIX specifies that we switch to extended headers at
26 * Likewise for the mtime (which happens to use a buffer of the same size).
28 #if ULONG_MAX == 0xFFFFFFFF
29 #define USTAR_MAX_SIZE ULONG_MAX
31 #define USTAR_MAX_SIZE 077777777777UL
33 #if TIME_MAX == 0xFFFFFFFF
34 #define USTAR_MAX_MTIME TIME_MAX
36 #define USTAR_MAX_MTIME 077777777777ULL
39 /* writes out the whole block, but only if it is full */
40 static void write_if_needed(void)
42 if (offset == BLOCKSIZE) {
43 write_or_die(1, block, BLOCKSIZE);
49 * queues up writes, so that all our write(2) calls write exactly one
50 * full block; pads writes to RECORDSIZE
52 static void do_write_blocked(const void *data, unsigned long size)
54 const char *buf = data;
57 unsigned long chunk = BLOCKSIZE - offset;
60 memcpy(block + offset, buf, chunk);
66 while (size >= BLOCKSIZE) {
67 write_or_die(1, buf, BLOCKSIZE);
72 memcpy(block + offset, buf, size);
77 static void finish_record(void)
80 tail = offset % RECORDSIZE;
82 memset(block + offset, 0, RECORDSIZE - tail);
83 offset += RECORDSIZE - tail;
88 static void write_blocked(const void *data, unsigned long size)
90 do_write_blocked(data, size);
95 * The end of tar archives is marked by 2*512 nul bytes and after that
96 * follows the rest of the block (if any).
98 static void write_trailer(void)
100 int tail = BLOCKSIZE - offset;
101 memset(block + offset, 0, tail);
102 write_or_die(1, block, BLOCKSIZE);
103 if (tail < 2 * RECORDSIZE) {
104 memset(block, 0, offset);
105 write_or_die(1, block, BLOCKSIZE);
110 * queues up writes, so that all our write(2) calls write exactly one
111 * full block; pads writes to RECORDSIZE
113 static int stream_blocked(const unsigned char *sha1)
115 struct git_istream *st;
116 enum object_type type;
121 st = open_istream(sha1, &type, &sz, NULL);
123 return error("cannot stream blob %s", sha1_to_hex(sha1));
125 readlen = read_istream(st, buf, sizeof(buf));
128 do_write_blocked(buf, readlen);
137 * pax extended header records have the format "%u %s=%s\n". %u contains
138 * the size of the whole string (including the %u), the first %s is the
139 * keyword, the second one is the value. This function constructs such a
140 * string and appends it to a struct strbuf.
142 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
143 const char *value, unsigned int valuelen)
148 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
149 for (tmp = len; tmp > 9; tmp /= 10)
152 strbuf_grow(sb, len);
153 strbuf_addf(sb, "%u %s=", len, keyword);
154 strbuf_add(sb, value, valuelen);
155 strbuf_addch(sb, '\n');
159 * Like strbuf_append_ext_header, but for numeric values.
161 static void strbuf_append_ext_header_uint(struct strbuf *sb,
165 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
168 len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
169 strbuf_append_ext_header(sb, keyword, buf, len);
172 static unsigned int ustar_header_chksum(const struct ustar_header *header)
174 const unsigned char *p = (const unsigned char *)header;
175 unsigned int chksum = 0;
176 while (p < (const unsigned char *)header->chksum)
178 chksum += sizeof(header->chksum) * ' ';
179 p += sizeof(header->chksum);
180 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
185 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
188 if (i > 1 && path[i - 1] == '/')
194 } while (i > 0 && path[i] != '/');
198 static void prepare_header(struct archiver_args *args,
199 struct ustar_header *header,
200 unsigned int mode, unsigned long size)
202 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
203 xsnprintf(header->size, sizeof(header->size), "%011lo", S_ISREG(mode) ? size : 0);
204 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
206 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
207 xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
208 strlcpy(header->uname, "root", sizeof(header->uname));
209 strlcpy(header->gname, "root", sizeof(header->gname));
210 xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
211 xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
213 memcpy(header->magic, "ustar", 6);
214 memcpy(header->version, "00", 2);
216 xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
219 static void write_extended_header(struct archiver_args *args,
220 const unsigned char *sha1,
221 const void *buffer, unsigned long size)
223 struct ustar_header header;
225 memset(&header, 0, sizeof(header));
226 *header.typeflag = TYPEFLAG_EXT_HEADER;
228 xsnprintf(header.name, sizeof(header.name), "%s.paxheader", sha1_to_hex(sha1));
229 prepare_header(args, &header, mode, size);
230 write_blocked(&header, sizeof(header));
231 write_blocked(buffer, size);
234 static int write_tar_entry(struct archiver_args *args,
235 const unsigned char *sha1,
236 const char *path, size_t pathlen,
239 struct ustar_header header;
240 struct strbuf ext_header = STRBUF_INIT;
241 unsigned int old_mode = mode;
242 unsigned long size, size_in_header;
246 memset(&header, 0, sizeof(header));
248 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
249 *header.typeflag = TYPEFLAG_DIR;
250 mode = (mode | 0777) & ~tar_umask;
251 } else if (S_ISLNK(mode)) {
252 *header.typeflag = TYPEFLAG_LNK;
254 } else if (S_ISREG(mode)) {
255 *header.typeflag = TYPEFLAG_REG;
256 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
258 return error("unsupported file mode: 0%o (SHA1: %s)",
259 mode, sha1_to_hex(sha1));
261 if (pathlen > sizeof(header.name)) {
262 size_t plen = get_path_prefix(path, pathlen,
263 sizeof(header.prefix));
264 size_t rest = pathlen - plen - 1;
265 if (plen > 0 && rest <= sizeof(header.name)) {
266 memcpy(header.prefix, path, plen);
267 memcpy(header.name, path + plen + 1, rest);
269 xsnprintf(header.name, sizeof(header.name), "%s.data",
271 strbuf_append_ext_header(&ext_header, "path",
275 memcpy(header.name, path, pathlen);
277 if (S_ISREG(mode) && !args->convert &&
278 sha1_object_info(sha1, &size) == OBJ_BLOB &&
279 size > big_file_threshold)
281 else if (S_ISLNK(mode) || S_ISREG(mode)) {
282 enum object_type type;
283 buffer = sha1_file_to_archive(args, path, sha1, old_mode, &type, &size);
285 return error("cannot read %s", sha1_to_hex(sha1));
292 if (size > sizeof(header.linkname)) {
293 xsnprintf(header.linkname, sizeof(header.linkname),
294 "see %s.paxheader", sha1_to_hex(sha1));
295 strbuf_append_ext_header(&ext_header, "linkpath",
298 memcpy(header.linkname, buffer, size);
301 size_in_header = size;
302 if (S_ISREG(mode) && size > USTAR_MAX_SIZE) {
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, sha1, ext_header.buf,
313 strbuf_release(&ext_header);
314 write_blocked(&header, sizeof(header));
315 if (S_ISREG(mode) && size > 0) {
317 write_blocked(buffer, size);
319 err = stream_blocked(sha1);
325 static void write_global_extended_header(struct archiver_args *args)
327 const unsigned char *sha1 = args->commit_sha1;
328 struct strbuf ext_header = STRBUF_INIT;
329 struct ustar_header header;
333 strbuf_append_ext_header(&ext_header, "comment",
334 sha1_to_hex(sha1), 40);
335 if (args->time > USTAR_MAX_MTIME) {
336 strbuf_append_ext_header_uint(&ext_header, "mtime",
338 args->time = USTAR_MAX_MTIME;
344 memset(&header, 0, sizeof(header));
345 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
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, int len)
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])
369 static int tar_filter_config(const char *var, const char *value, void *data)
376 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
379 ar = find_tar_filter(name, namelen);
381 ar = xcalloc(1, sizeof(*ar));
382 ar->name = xmemdupz(name, namelen);
383 ar->write_archive = write_tar_filter_archive;
384 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
385 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
386 tar_filters[nr_tar_filters++] = ar;
389 if (!strcmp(type, "command")) {
391 return config_error_nonbool(var);
393 ar->data = xstrdup(value);
396 if (!strcmp(type, "remote")) {
397 if (git_config_bool(var, value))
398 ar->flags |= ARCHIVER_REMOTE;
400 ar->flags &= ~ARCHIVER_REMOTE;
407 static int git_tar_config(const char *var, const char *value, void *cb)
409 if (!strcmp(var, "tar.umask")) {
410 if (value && !strcmp(value, "user")) {
411 tar_umask = umask(0);
414 tar_umask = git_config_int(var, value);
419 return tar_filter_config(var, value, cb);
422 static int write_tar_archive(const struct archiver *ar,
423 struct archiver_args *args)
427 write_global_extended_header(args);
428 err = write_archive_entries(args, write_tar_entry);
434 static int write_tar_filter_archive(const struct archiver *ar,
435 struct archiver_args *args)
437 struct strbuf cmd = STRBUF_INIT;
438 struct child_process filter = CHILD_PROCESS_INIT;
443 die("BUG: tar-filter archiver called with no filter defined");
445 strbuf_addstr(&cmd, ar->data);
446 if (args->compression_level >= 0)
447 strbuf_addf(&cmd, " -%d", args->compression_level);
452 filter.use_shell = 1;
455 if (start_command(&filter) < 0)
456 die_errno("unable to start '%s' filter", argv[0]);
458 if (dup2(filter.in, 1) < 0)
459 die_errno("unable to redirect descriptor");
462 r = write_tar_archive(ar, args);
465 if (finish_command(&filter) != 0)
466 die("'%s' filter reported error", argv[0]);
468 strbuf_release(&cmd);
472 static struct archiver tar_archiver = {
478 void init_tar_archiver(void)
481 register_archiver(&tar_archiver);
483 tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
484 tar_filter_config("tar.tgz.remote", "true", NULL);
485 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
486 tar_filter_config("tar.tar.gz.remote", "true", NULL);
487 git_config(git_tar_config, NULL);
488 for (i = 0; i < nr_tar_filters; i++) {
489 /* omit any filters that never had a command configured */
490 if (tar_filters[i]->data)
491 register_archiver(tar_filters[i]);