Merge branch 'nd/cache-tree-ita'
[git.git] / archive-tar.c
blob55682404d59051f9fe7be34f19d084fc502b92b5
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 #define USTAR_MAX_MTIME ULONG_MAX
31 #else
32 #define USTAR_MAX_SIZE 077777777777UL
33 #define USTAR_MAX_MTIME 077777777777UL
34 #endif
36 /* writes out the whole block, but only if it is full */
37 static void write_if_needed(void)
39 if (offset == BLOCKSIZE) {
40 write_or_die(1, block, BLOCKSIZE);
41 offset = 0;
46 * queues up writes, so that all our write(2) calls write exactly one
47 * full block; pads writes to RECORDSIZE
49 static void do_write_blocked(const void *data, unsigned long size)
51 const char *buf = data;
53 if (offset) {
54 unsigned long chunk = BLOCKSIZE - offset;
55 if (size < chunk)
56 chunk = size;
57 memcpy(block + offset, buf, chunk);
58 size -= chunk;
59 offset += chunk;
60 buf += chunk;
61 write_if_needed();
63 while (size >= BLOCKSIZE) {
64 write_or_die(1, buf, BLOCKSIZE);
65 size -= BLOCKSIZE;
66 buf += BLOCKSIZE;
68 if (size) {
69 memcpy(block + offset, buf, size);
70 offset += size;
74 static void finish_record(void)
76 unsigned long tail;
77 tail = offset % RECORDSIZE;
78 if (tail) {
79 memset(block + offset, 0, RECORDSIZE - tail);
80 offset += RECORDSIZE - tail;
82 write_if_needed();
85 static void write_blocked(const void *data, unsigned long size)
87 do_write_blocked(data, size);
88 finish_record();
92 * The end of tar archives is marked by 2*512 nul bytes and after that
93 * follows the rest of the block (if any).
95 static void write_trailer(void)
97 int tail = BLOCKSIZE - offset;
98 memset(block + offset, 0, tail);
99 write_or_die(1, block, BLOCKSIZE);
100 if (tail < 2 * RECORDSIZE) {
101 memset(block, 0, offset);
102 write_or_die(1, block, BLOCKSIZE);
107 * queues up writes, so that all our write(2) calls write exactly one
108 * full block; pads writes to RECORDSIZE
110 static int stream_blocked(const unsigned char *sha1)
112 struct git_istream *st;
113 enum object_type type;
114 unsigned long sz;
115 char buf[BLOCKSIZE];
116 ssize_t readlen;
118 st = open_istream(sha1, &type, &sz, NULL);
119 if (!st)
120 return error("cannot stream blob %s", sha1_to_hex(sha1));
121 for (;;) {
122 readlen = read_istream(st, buf, sizeof(buf));
123 if (readlen <= 0)
124 break;
125 do_write_blocked(buf, readlen);
127 close_istream(st);
128 if (!readlen)
129 finish_record();
130 return readlen;
134 * pax extended header records have the format "%u %s=%s\n". %u contains
135 * the size of the whole string (including the %u), the first %s is the
136 * keyword, the second one is the value. This function constructs such a
137 * string and appends it to a struct strbuf.
139 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
140 const char *value, unsigned int valuelen)
142 int len, tmp;
144 /* "%u %s=%s\n" */
145 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
146 for (tmp = len; tmp > 9; tmp /= 10)
147 len++;
149 strbuf_grow(sb, len);
150 strbuf_addf(sb, "%u %s=", len, keyword);
151 strbuf_add(sb, value, valuelen);
152 strbuf_addch(sb, '\n');
156 * Like strbuf_append_ext_header, but for numeric values.
158 static void strbuf_append_ext_header_uint(struct strbuf *sb,
159 const char *keyword,
160 uintmax_t value)
162 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
163 int len;
165 len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
166 strbuf_append_ext_header(sb, keyword, buf, len);
169 static unsigned int ustar_header_chksum(const struct ustar_header *header)
171 const unsigned char *p = (const unsigned char *)header;
172 unsigned int chksum = 0;
173 while (p < (const unsigned char *)header->chksum)
174 chksum += *p++;
175 chksum += sizeof(header->chksum) * ' ';
176 p += sizeof(header->chksum);
177 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
178 chksum += *p++;
179 return chksum;
182 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
184 size_t i = pathlen;
185 if (i > 1 && path[i - 1] == '/')
186 i--;
187 if (i > maxlen)
188 i = maxlen;
189 do {
190 i--;
191 } while (i > 0 && path[i] != '/');
192 return i;
195 static void prepare_header(struct archiver_args *args,
196 struct ustar_header *header,
197 unsigned int mode, unsigned long size)
199 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
200 xsnprintf(header->size, sizeof(header->size), "%011lo", S_ISREG(mode) ? size : 0);
201 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
203 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
204 xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
205 strlcpy(header->uname, "root", sizeof(header->uname));
206 strlcpy(header->gname, "root", sizeof(header->gname));
207 xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
208 xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
210 memcpy(header->magic, "ustar", 6);
211 memcpy(header->version, "00", 2);
213 xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
216 static int write_extended_header(struct archiver_args *args,
217 const unsigned char *sha1,
218 const void *buffer, unsigned long size)
220 struct ustar_header header;
221 unsigned int mode;
222 memset(&header, 0, sizeof(header));
223 *header.typeflag = TYPEFLAG_EXT_HEADER;
224 mode = 0100666;
225 xsnprintf(header.name, sizeof(header.name), "%s.paxheader", sha1_to_hex(sha1));
226 prepare_header(args, &header, mode, size);
227 write_blocked(&header, sizeof(header));
228 write_blocked(buffer, size);
229 return 0;
232 static int write_tar_entry(struct archiver_args *args,
233 const unsigned char *sha1,
234 const char *path, size_t pathlen,
235 unsigned int mode)
237 struct ustar_header header;
238 struct strbuf ext_header = STRBUF_INIT;
239 unsigned int old_mode = mode;
240 unsigned long size, size_in_header;
241 void *buffer;
242 int err = 0;
244 memset(&header, 0, sizeof(header));
246 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
247 *header.typeflag = TYPEFLAG_DIR;
248 mode = (mode | 0777) & ~tar_umask;
249 } else if (S_ISLNK(mode)) {
250 *header.typeflag = TYPEFLAG_LNK;
251 mode |= 0777;
252 } else if (S_ISREG(mode)) {
253 *header.typeflag = TYPEFLAG_REG;
254 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
255 } else {
256 return error("unsupported file mode: 0%o (SHA1: %s)",
257 mode, sha1_to_hex(sha1));
259 if (pathlen > sizeof(header.name)) {
260 size_t plen = get_path_prefix(path, pathlen,
261 sizeof(header.prefix));
262 size_t rest = pathlen - plen - 1;
263 if (plen > 0 && rest <= sizeof(header.name)) {
264 memcpy(header.prefix, path, plen);
265 memcpy(header.name, path + plen + 1, rest);
266 } else {
267 xsnprintf(header.name, sizeof(header.name), "%s.data",
268 sha1_to_hex(sha1));
269 strbuf_append_ext_header(&ext_header, "path",
270 path, pathlen);
272 } else
273 memcpy(header.name, path, pathlen);
275 if (S_ISREG(mode) && !args->convert &&
276 sha1_object_info(sha1, &size) == OBJ_BLOB &&
277 size > big_file_threshold)
278 buffer = NULL;
279 else if (S_ISLNK(mode) || S_ISREG(mode)) {
280 enum object_type type;
281 buffer = sha1_file_to_archive(args, path, sha1, old_mode, &type, &size);
282 if (!buffer)
283 return error("cannot read %s", sha1_to_hex(sha1));
284 } else {
285 buffer = NULL;
286 size = 0;
289 if (S_ISLNK(mode)) {
290 if (size > sizeof(header.linkname)) {
291 xsnprintf(header.linkname, sizeof(header.linkname),
292 "see %s.paxheader", sha1_to_hex(sha1));
293 strbuf_append_ext_header(&ext_header, "linkpath",
294 buffer, size);
295 } else
296 memcpy(header.linkname, buffer, size);
299 size_in_header = size;
300 if (S_ISREG(mode) && size > USTAR_MAX_SIZE) {
301 size_in_header = 0;
302 strbuf_append_ext_header_uint(&ext_header, "size", size);
305 prepare_header(args, &header, mode, size_in_header);
307 if (ext_header.len > 0) {
308 err = write_extended_header(args, sha1, ext_header.buf,
309 ext_header.len);
310 if (err) {
311 free(buffer);
312 return err;
315 strbuf_release(&ext_header);
316 write_blocked(&header, sizeof(header));
317 if (S_ISREG(mode) && size > 0) {
318 if (buffer)
319 write_blocked(buffer, size);
320 else
321 err = stream_blocked(sha1);
323 free(buffer);
324 return err;
327 static void write_global_extended_header(struct archiver_args *args)
329 const unsigned char *sha1 = args->commit_sha1;
330 struct strbuf ext_header = STRBUF_INIT;
331 struct ustar_header header;
332 unsigned int mode;
334 if (sha1)
335 strbuf_append_ext_header(&ext_header, "comment",
336 sha1_to_hex(sha1), 40);
337 if (args->time > USTAR_MAX_MTIME) {
338 strbuf_append_ext_header_uint(&ext_header, "mtime",
339 args->time);
340 args->time = USTAR_MAX_MTIME;
343 if (!ext_header.len)
344 return;
346 memset(&header, 0, sizeof(header));
347 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
348 mode = 0100666;
349 xsnprintf(header.name, sizeof(header.name), "pax_global_header");
350 prepare_header(args, &header, mode, ext_header.len);
351 write_blocked(&header, sizeof(header));
352 write_blocked(ext_header.buf, ext_header.len);
353 strbuf_release(&ext_header);
356 static struct archiver **tar_filters;
357 static int nr_tar_filters;
358 static int alloc_tar_filters;
360 static struct archiver *find_tar_filter(const char *name, int len)
362 int i;
363 for (i = 0; i < nr_tar_filters; i++) {
364 struct archiver *ar = tar_filters[i];
365 if (!strncmp(ar->name, name, len) && !ar->name[len])
366 return ar;
368 return NULL;
371 static int tar_filter_config(const char *var, const char *value, void *data)
373 struct archiver *ar;
374 const char *name;
375 const char *type;
376 int namelen;
378 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
379 return 0;
381 ar = find_tar_filter(name, namelen);
382 if (!ar) {
383 ar = xcalloc(1, sizeof(*ar));
384 ar->name = xmemdupz(name, namelen);
385 ar->write_archive = write_tar_filter_archive;
386 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
387 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
388 tar_filters[nr_tar_filters++] = ar;
391 if (!strcmp(type, "command")) {
392 if (!value)
393 return config_error_nonbool(var);
394 free(ar->data);
395 ar->data = xstrdup(value);
396 return 0;
398 if (!strcmp(type, "remote")) {
399 if (git_config_bool(var, value))
400 ar->flags |= ARCHIVER_REMOTE;
401 else
402 ar->flags &= ~ARCHIVER_REMOTE;
403 return 0;
406 return 0;
409 static int git_tar_config(const char *var, const char *value, void *cb)
411 if (!strcmp(var, "tar.umask")) {
412 if (value && !strcmp(value, "user")) {
413 tar_umask = umask(0);
414 umask(tar_umask);
415 } else {
416 tar_umask = git_config_int(var, value);
418 return 0;
421 return tar_filter_config(var, value, cb);
424 static int write_tar_archive(const struct archiver *ar,
425 struct archiver_args *args)
427 int err = 0;
429 write_global_extended_header(args);
430 err = write_archive_entries(args, write_tar_entry);
431 if (!err)
432 write_trailer();
433 return err;
436 static int write_tar_filter_archive(const struct archiver *ar,
437 struct archiver_args *args)
439 struct strbuf cmd = STRBUF_INIT;
440 struct child_process filter = CHILD_PROCESS_INIT;
441 const char *argv[2];
442 int r;
444 if (!ar->data)
445 die("BUG: tar-filter archiver called with no filter defined");
447 strbuf_addstr(&cmd, ar->data);
448 if (args->compression_level >= 0)
449 strbuf_addf(&cmd, " -%d", args->compression_level);
451 argv[0] = cmd.buf;
452 argv[1] = NULL;
453 filter.argv = argv;
454 filter.use_shell = 1;
455 filter.in = -1;
457 if (start_command(&filter) < 0)
458 die_errno("unable to start '%s' filter", argv[0]);
459 close(1);
460 if (dup2(filter.in, 1) < 0)
461 die_errno("unable to redirect descriptor");
462 close(filter.in);
464 r = write_tar_archive(ar, args);
466 close(1);
467 if (finish_command(&filter) != 0)
468 die("'%s' filter reported error", argv[0]);
470 strbuf_release(&cmd);
471 return r;
474 static struct archiver tar_archiver = {
475 "tar",
476 write_tar_archive,
477 ARCHIVER_REMOTE
480 void init_tar_archiver(void)
482 int i;
483 register_archiver(&tar_archiver);
485 tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
486 tar_filter_config("tar.tgz.remote", "true", NULL);
487 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
488 tar_filter_config("tar.tar.gz.remote", "true", NULL);
489 git_config(git_tar_config, NULL);
490 for (i = 0; i < nr_tar_filters; i++) {
491 /* omit any filters that never had a command configured */
492 if (tar_filters[i]->data)
493 register_archiver(tar_filters[i]);