setup: teach discover_git_directory to respect the commondir
[git.git] / archive-tar.c
blobd65ac016df09ccdb7bc7952291de12ec5912bc8d
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 "streaming.h"
9 #include "run-command.h"
11 #define RECORDSIZE (512)
12 #define BLOCKSIZE (RECORDSIZE * 20)
14 static char block[BLOCKSIZE];
15 static unsigned long offset;
17 static int tar_umask = 002;
19 static int write_tar_filter_archive(const struct archiver *ar,
20 struct archiver_args *args);
23 * This is the max value that a ustar size header can specify, as it is fixed
24 * at 11 octal digits. POSIX specifies that we switch to extended headers at
25 * this size.
27 * Likewise for the mtime (which happens to use a buffer of the same size).
29 #if ULONG_MAX == 0xFFFFFFFF
30 #define USTAR_MAX_SIZE ULONG_MAX
31 #define USTAR_MAX_MTIME ULONG_MAX
32 #else
33 #define USTAR_MAX_SIZE 077777777777UL
34 #define USTAR_MAX_MTIME 077777777777UL
35 #endif
37 /* writes out the whole block, but only if it is full */
38 static void write_if_needed(void)
40 if (offset == BLOCKSIZE) {
41 write_or_die(1, block, BLOCKSIZE);
42 offset = 0;
47 * queues up writes, so that all our write(2) calls write exactly one
48 * full block; pads writes to RECORDSIZE
50 static void do_write_blocked(const void *data, unsigned long size)
52 const char *buf = data;
54 if (offset) {
55 unsigned long chunk = BLOCKSIZE - offset;
56 if (size < chunk)
57 chunk = size;
58 memcpy(block + offset, buf, chunk);
59 size -= chunk;
60 offset += chunk;
61 buf += chunk;
62 write_if_needed();
64 while (size >= BLOCKSIZE) {
65 write_or_die(1, buf, BLOCKSIZE);
66 size -= BLOCKSIZE;
67 buf += BLOCKSIZE;
69 if (size) {
70 memcpy(block + offset, buf, size);
71 offset += size;
75 static void finish_record(void)
77 unsigned long tail;
78 tail = offset % RECORDSIZE;
79 if (tail) {
80 memset(block + offset, 0, RECORDSIZE - tail);
81 offset += RECORDSIZE - tail;
83 write_if_needed();
86 static void write_blocked(const void *data, unsigned long size)
88 do_write_blocked(data, size);
89 finish_record();
93 * The end of tar archives is marked by 2*512 nul bytes and after that
94 * follows the rest of the block (if any).
96 static void write_trailer(void)
98 int tail = BLOCKSIZE - offset;
99 memset(block + offset, 0, tail);
100 write_or_die(1, block, BLOCKSIZE);
101 if (tail < 2 * RECORDSIZE) {
102 memset(block, 0, offset);
103 write_or_die(1, block, BLOCKSIZE);
108 * queues up writes, so that all our write(2) calls write exactly one
109 * full block; pads writes to RECORDSIZE
111 static int stream_blocked(const unsigned char *sha1)
113 struct git_istream *st;
114 enum object_type type;
115 unsigned long sz;
116 char buf[BLOCKSIZE];
117 ssize_t readlen;
119 st = open_istream(sha1, &type, &sz, NULL);
120 if (!st)
121 return error("cannot stream blob %s", sha1_to_hex(sha1));
122 for (;;) {
123 readlen = read_istream(st, buf, sizeof(buf));
124 if (readlen <= 0)
125 break;
126 do_write_blocked(buf, readlen);
128 close_istream(st);
129 if (!readlen)
130 finish_record();
131 return readlen;
135 * pax extended header records have the format "%u %s=%s\n". %u contains
136 * the size of the whole string (including the %u), the first %s is the
137 * keyword, the second one is the value. This function constructs such a
138 * string and appends it to a struct strbuf.
140 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
141 const char *value, unsigned int valuelen)
143 int len, tmp;
145 /* "%u %s=%s\n" */
146 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
147 for (tmp = len; tmp > 9; tmp /= 10)
148 len++;
150 strbuf_grow(sb, len);
151 strbuf_addf(sb, "%u %s=", len, keyword);
152 strbuf_add(sb, value, valuelen);
153 strbuf_addch(sb, '\n');
157 * Like strbuf_append_ext_header, but for numeric values.
159 static void strbuf_append_ext_header_uint(struct strbuf *sb,
160 const char *keyword,
161 uintmax_t value)
163 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
164 int len;
166 len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
167 strbuf_append_ext_header(sb, keyword, buf, len);
170 static unsigned int ustar_header_chksum(const struct ustar_header *header)
172 const unsigned char *p = (const unsigned char *)header;
173 unsigned int chksum = 0;
174 while (p < (const unsigned char *)header->chksum)
175 chksum += *p++;
176 chksum += sizeof(header->chksum) * ' ';
177 p += sizeof(header->chksum);
178 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
179 chksum += *p++;
180 return chksum;
183 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
185 size_t i = pathlen;
186 if (i > 1 && path[i - 1] == '/')
187 i--;
188 if (i > maxlen)
189 i = maxlen;
190 do {
191 i--;
192 } while (i > 0 && path[i] != '/');
193 return i;
196 static void prepare_header(struct archiver_args *args,
197 struct ustar_header *header,
198 unsigned int mode, unsigned long size)
200 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
201 xsnprintf(header->size, sizeof(header->size), "%011lo", S_ISREG(mode) ? size : 0);
202 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
204 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
205 xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
206 strlcpy(header->uname, "root", sizeof(header->uname));
207 strlcpy(header->gname, "root", sizeof(header->gname));
208 xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
209 xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
211 memcpy(header->magic, "ustar", 6);
212 memcpy(header->version, "00", 2);
214 xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
217 static void write_extended_header(struct archiver_args *args,
218 const unsigned char *sha1,
219 const void *buffer, unsigned long size)
221 struct ustar_header header;
222 unsigned int mode;
223 memset(&header, 0, sizeof(header));
224 *header.typeflag = TYPEFLAG_EXT_HEADER;
225 mode = 0100666;
226 xsnprintf(header.name, sizeof(header.name), "%s.paxheader", sha1_to_hex(sha1));
227 prepare_header(args, &header, mode, size);
228 write_blocked(&header, sizeof(header));
229 write_blocked(buffer, size);
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 write_extended_header(args, sha1, ext_header.buf,
309 ext_header.len);
311 strbuf_release(&ext_header);
312 write_blocked(&header, sizeof(header));
313 if (S_ISREG(mode) && size > 0) {
314 if (buffer)
315 write_blocked(buffer, size);
316 else
317 err = stream_blocked(sha1);
319 free(buffer);
320 return err;
323 static void write_global_extended_header(struct archiver_args *args)
325 const unsigned char *sha1 = args->commit_sha1;
326 struct strbuf ext_header = STRBUF_INIT;
327 struct ustar_header header;
328 unsigned int mode;
330 if (sha1)
331 strbuf_append_ext_header(&ext_header, "comment",
332 sha1_to_hex(sha1), 40);
333 if (args->time > USTAR_MAX_MTIME) {
334 strbuf_append_ext_header_uint(&ext_header, "mtime",
335 args->time);
336 args->time = USTAR_MAX_MTIME;
339 if (!ext_header.len)
340 return;
342 memset(&header, 0, sizeof(header));
343 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
344 mode = 0100666;
345 xsnprintf(header.name, sizeof(header.name), "pax_global_header");
346 prepare_header(args, &header, mode, ext_header.len);
347 write_blocked(&header, sizeof(header));
348 write_blocked(ext_header.buf, ext_header.len);
349 strbuf_release(&ext_header);
352 static struct archiver **tar_filters;
353 static int nr_tar_filters;
354 static int alloc_tar_filters;
356 static struct archiver *find_tar_filter(const char *name, int len)
358 int i;
359 for (i = 0; i < nr_tar_filters; i++) {
360 struct archiver *ar = tar_filters[i];
361 if (!strncmp(ar->name, name, len) && !ar->name[len])
362 return ar;
364 return NULL;
367 static int tar_filter_config(const char *var, const char *value, void *data)
369 struct archiver *ar;
370 const char *name;
371 const char *type;
372 int namelen;
374 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
375 return 0;
377 ar = find_tar_filter(name, namelen);
378 if (!ar) {
379 ar = xcalloc(1, sizeof(*ar));
380 ar->name = xmemdupz(name, namelen);
381 ar->write_archive = write_tar_filter_archive;
382 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
383 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
384 tar_filters[nr_tar_filters++] = ar;
387 if (!strcmp(type, "command")) {
388 if (!value)
389 return config_error_nonbool(var);
390 free(ar->data);
391 ar->data = xstrdup(value);
392 return 0;
394 if (!strcmp(type, "remote")) {
395 if (git_config_bool(var, value))
396 ar->flags |= ARCHIVER_REMOTE;
397 else
398 ar->flags &= ~ARCHIVER_REMOTE;
399 return 0;
402 return 0;
405 static int git_tar_config(const char *var, const char *value, void *cb)
407 if (!strcmp(var, "tar.umask")) {
408 if (value && !strcmp(value, "user")) {
409 tar_umask = umask(0);
410 umask(tar_umask);
411 } else {
412 tar_umask = git_config_int(var, value);
414 return 0;
417 return tar_filter_config(var, value, cb);
420 static int write_tar_archive(const struct archiver *ar,
421 struct archiver_args *args)
423 int err = 0;
425 write_global_extended_header(args);
426 err = write_archive_entries(args, write_tar_entry);
427 if (!err)
428 write_trailer();
429 return err;
432 static int write_tar_filter_archive(const struct archiver *ar,
433 struct archiver_args *args)
435 struct strbuf cmd = STRBUF_INIT;
436 struct child_process filter = CHILD_PROCESS_INIT;
437 const char *argv[2];
438 int r;
440 if (!ar->data)
441 die("BUG: tar-filter archiver called with no filter defined");
443 strbuf_addstr(&cmd, ar->data);
444 if (args->compression_level >= 0)
445 strbuf_addf(&cmd, " -%d", args->compression_level);
447 argv[0] = cmd.buf;
448 argv[1] = NULL;
449 filter.argv = argv;
450 filter.use_shell = 1;
451 filter.in = -1;
453 if (start_command(&filter) < 0)
454 die_errno("unable to start '%s' filter", argv[0]);
455 close(1);
456 if (dup2(filter.in, 1) < 0)
457 die_errno("unable to redirect descriptor");
458 close(filter.in);
460 r = write_tar_archive(ar, args);
462 close(1);
463 if (finish_command(&filter) != 0)
464 die("'%s' filter reported error", argv[0]);
466 strbuf_release(&cmd);
467 return r;
470 static struct archiver tar_archiver = {
471 "tar",
472 write_tar_archive,
473 ARCHIVER_REMOTE
476 void init_tar_archiver(void)
478 int i;
479 register_archiver(&tar_archiver);
481 tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
482 tar_filter_config("tar.tgz.remote", "true", NULL);
483 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
484 tar_filter_config("tar.tar.gz.remote", "true", NULL);
485 git_config(git_tar_config, NULL);
486 for (i = 0; i < nr_tar_filters; i++) {
487 /* omit any filters that never had a command configured */
488 if (tar_filters[i]->data)
489 register_archiver(tar_filters[i]);