Merge branch 'jc/repack-threads'
[git.git] / archive-tar.c
blob073e60ebd3c366b42298ae443eee230407d4e3a7
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "cache.h"
5 #include "tar.h"
6 #include "archive.h"
7 #include "streaming.h"
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
24 * this size.
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
30 #else
31 #define USTAR_MAX_SIZE 077777777777UL
32 #endif
33 #if TIME_MAX == 0xFFFFFFFF
34 #define USTAR_MAX_MTIME TIME_MAX
35 #else
36 #define USTAR_MAX_MTIME 077777777777ULL
37 #endif
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);
44 offset = 0;
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;
56 if (offset) {
57 unsigned long chunk = BLOCKSIZE - offset;
58 if (size < chunk)
59 chunk = size;
60 memcpy(block + offset, buf, chunk);
61 size -= chunk;
62 offset += chunk;
63 buf += chunk;
64 write_if_needed();
66 while (size >= BLOCKSIZE) {
67 write_or_die(1, buf, BLOCKSIZE);
68 size -= BLOCKSIZE;
69 buf += BLOCKSIZE;
71 if (size) {
72 memcpy(block + offset, buf, size);
73 offset += size;
77 static void finish_record(void)
79 unsigned long tail;
80 tail = offset % RECORDSIZE;
81 if (tail) {
82 memset(block + offset, 0, RECORDSIZE - tail);
83 offset += RECORDSIZE - tail;
85 write_if_needed();
88 static void write_blocked(const void *data, unsigned long size)
90 do_write_blocked(data, size);
91 finish_record();
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;
117 unsigned long sz;
118 char buf[BLOCKSIZE];
119 ssize_t readlen;
121 st = open_istream(sha1, &type, &sz, NULL);
122 if (!st)
123 return error("cannot stream blob %s", sha1_to_hex(sha1));
124 for (;;) {
125 readlen = read_istream(st, buf, sizeof(buf));
126 if (readlen <= 0)
127 break;
128 do_write_blocked(buf, readlen);
130 close_istream(st);
131 if (!readlen)
132 finish_record();
133 return 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)
145 int len, tmp;
147 /* "%u %s=%s\n" */
148 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
149 for (tmp = len; tmp > 9; tmp /= 10)
150 len++;
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,
162 const char *keyword,
163 uintmax_t value)
165 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
166 int len;
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)
177 chksum += *p++;
178 chksum += sizeof(header->chksum) * ' ';
179 p += sizeof(header->chksum);
180 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
181 chksum += *p++;
182 return chksum;
185 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
187 size_t i = pathlen;
188 if (i > 1 && path[i - 1] == '/')
189 i--;
190 if (i > maxlen)
191 i = maxlen;
192 do {
193 i--;
194 } while (i > 0 && path[i] != '/');
195 return 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;
224 unsigned int mode;
225 memset(&header, 0, sizeof(header));
226 *header.typeflag = TYPEFLAG_EXT_HEADER;
227 mode = 0100666;
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,
237 unsigned int mode)
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;
243 void *buffer;
244 int err = 0;
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;
253 mode |= 0777;
254 } else if (S_ISREG(mode)) {
255 *header.typeflag = TYPEFLAG_REG;
256 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
257 } else {
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);
268 } else {
269 xsnprintf(header.name, sizeof(header.name), "%s.data",
270 sha1_to_hex(sha1));
271 strbuf_append_ext_header(&ext_header, "path",
272 path, pathlen);
274 } else
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)
280 buffer = NULL;
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);
284 if (!buffer)
285 return error("cannot read %s", sha1_to_hex(sha1));
286 } else {
287 buffer = NULL;
288 size = 0;
291 if (S_ISLNK(mode)) {
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",
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, sha1, 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(sha1);
321 free(buffer);
322 return err;
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;
330 unsigned int mode;
332 if (sha1)
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",
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, int 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 int 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 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")) {
390 if (!value)
391 return config_error_nonbool(var);
392 free(ar->data);
393 ar->data = xstrdup(value);
394 return 0;
396 if (!strcmp(type, "remote")) {
397 if (git_config_bool(var, value))
398 ar->flags |= ARCHIVER_REMOTE;
399 else
400 ar->flags &= ~ARCHIVER_REMOTE;
401 return 0;
404 return 0;
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);
412 umask(tar_umask);
413 } else {
414 tar_umask = git_config_int(var, value);
416 return 0;
419 return tar_filter_config(var, value, cb);
422 static int write_tar_archive(const struct archiver *ar,
423 struct archiver_args *args)
425 int err = 0;
427 write_global_extended_header(args);
428 err = write_archive_entries(args, write_tar_entry);
429 if (!err)
430 write_trailer();
431 return err;
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;
439 const char *argv[2];
440 int r;
442 if (!ar->data)
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);
449 argv[0] = cmd.buf;
450 argv[1] = NULL;
451 filter.argv = argv;
452 filter.use_shell = 1;
453 filter.in = -1;
455 if (start_command(&filter) < 0)
456 die_errno("unable to start '%s' filter", argv[0]);
457 close(1);
458 if (dup2(filter.in, 1) < 0)
459 die_errno("unable to redirect descriptor");
460 close(filter.in);
462 r = write_tar_archive(ar, args);
464 close(1);
465 if (finish_command(&filter) != 0)
466 die("'%s' filter reported error", argv[0]);
468 strbuf_release(&cmd);
469 return r;
472 static struct archiver tar_archiver = {
473 "tar",
474 write_tar_archive,
475 ARCHIVER_REMOTE
478 void init_tar_archiver(void)
480 int i;
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]);