Set the default help format to html for msys builds.
[git/dscho.git] / archive-tar.c
blob0ba3f25cf579d6629566a39d4d5ac19b51293ed5
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);
21 /* writes out the whole block, but only if it is full */
22 static void write_if_needed(void)
24 if (offset == BLOCKSIZE) {
25 write_or_die(1, block, BLOCKSIZE);
26 offset = 0;
31 * queues up writes, so that all our write(2) calls write exactly one
32 * full block; pads writes to RECORDSIZE
34 static void do_write_blocked(const void *data, unsigned long size)
36 const char *buf = data;
38 if (offset) {
39 unsigned long chunk = BLOCKSIZE - offset;
40 if (size < chunk)
41 chunk = size;
42 memcpy(block + offset, buf, chunk);
43 size -= chunk;
44 offset += chunk;
45 buf += chunk;
46 write_if_needed();
48 while (size >= BLOCKSIZE) {
49 write_or_die(1, buf, BLOCKSIZE);
50 size -= BLOCKSIZE;
51 buf += BLOCKSIZE;
53 if (size) {
54 memcpy(block + offset, buf, size);
55 offset += size;
59 static void finish_record(void)
61 unsigned long tail;
62 tail = offset % RECORDSIZE;
63 if (tail) {
64 memset(block + offset, 0, RECORDSIZE - tail);
65 offset += RECORDSIZE - tail;
67 write_if_needed();
70 static void write_blocked(const void *data, unsigned long size)
72 do_write_blocked(data, size);
73 finish_record();
77 * The end of tar archives is marked by 2*512 nul bytes and after that
78 * follows the rest of the block (if any).
80 static void write_trailer(void)
82 int tail = BLOCKSIZE - offset;
83 memset(block + offset, 0, tail);
84 write_or_die(1, block, BLOCKSIZE);
85 if (tail < 2 * RECORDSIZE) {
86 memset(block, 0, offset);
87 write_or_die(1, block, BLOCKSIZE);
92 * queues up writes, so that all our write(2) calls write exactly one
93 * full block; pads writes to RECORDSIZE
95 static int stream_blocked(const unsigned char *sha1)
97 struct git_istream *st;
98 enum object_type type;
99 unsigned long sz;
100 char buf[BLOCKSIZE];
101 ssize_t readlen;
103 st = open_istream(sha1, &type, &sz, NULL);
104 if (!st)
105 return error("cannot stream blob %s", sha1_to_hex(sha1));
106 for (;;) {
107 readlen = read_istream(st, buf, sizeof(buf));
108 if (readlen <= 0)
109 break;
110 do_write_blocked(buf, readlen);
112 close_istream(st);
113 if (!readlen)
114 finish_record();
115 return readlen;
119 * pax extended header records have the format "%u %s=%s\n". %u contains
120 * the size of the whole string (including the %u), the first %s is the
121 * keyword, the second one is the value. This function constructs such a
122 * string and appends it to a struct strbuf.
124 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
125 const char *value, unsigned int valuelen)
127 int len, tmp;
129 /* "%u %s=%s\n" */
130 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
131 for (tmp = len; tmp > 9; tmp /= 10)
132 len++;
134 strbuf_grow(sb, len);
135 strbuf_addf(sb, "%u %s=", len, keyword);
136 strbuf_add(sb, value, valuelen);
137 strbuf_addch(sb, '\n');
140 static unsigned int ustar_header_chksum(const struct ustar_header *header)
142 const unsigned char *p = (const unsigned char *)header;
143 unsigned int chksum = 0;
144 while (p < (const unsigned char *)header->chksum)
145 chksum += *p++;
146 chksum += sizeof(header->chksum) * ' ';
147 p += sizeof(header->chksum);
148 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
149 chksum += *p++;
150 return chksum;
153 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
155 size_t i = pathlen;
156 if (i > maxlen)
157 i = maxlen;
158 do {
159 i--;
160 } while (i > 0 && path[i] != '/');
161 return i;
164 static void prepare_header(struct archiver_args *args,
165 struct ustar_header *header,
166 unsigned int mode, unsigned long size)
168 sprintf(header->mode, "%07o", mode & 07777);
169 sprintf(header->size, "%011lo", S_ISREG(mode) ? size : 0);
170 sprintf(header->mtime, "%011lo", (unsigned long) args->time);
172 sprintf(header->uid, "%07o", 0);
173 sprintf(header->gid, "%07o", 0);
174 strlcpy(header->uname, "root", sizeof(header->uname));
175 strlcpy(header->gname, "root", sizeof(header->gname));
176 sprintf(header->devmajor, "%07o", 0);
177 sprintf(header->devminor, "%07o", 0);
179 memcpy(header->magic, "ustar", 6);
180 memcpy(header->version, "00", 2);
182 sprintf(header->chksum, "%07o", ustar_header_chksum(header));
185 static int write_extended_header(struct archiver_args *args,
186 const unsigned char *sha1,
187 const void *buffer, unsigned long size)
189 struct ustar_header header;
190 unsigned int mode;
191 memset(&header, 0, sizeof(header));
192 *header.typeflag = TYPEFLAG_EXT_HEADER;
193 mode = 0100666;
194 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
195 prepare_header(args, &header, mode, size);
196 write_blocked(&header, sizeof(header));
197 write_blocked(buffer, size);
198 return 0;
201 static int write_tar_entry(struct archiver_args *args,
202 const unsigned char *sha1,
203 const char *path, size_t pathlen,
204 unsigned int mode)
206 struct ustar_header header;
207 struct strbuf ext_header = STRBUF_INIT;
208 unsigned int old_mode = mode;
209 unsigned long size;
210 void *buffer;
211 int err = 0;
213 memset(&header, 0, sizeof(header));
215 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
216 *header.typeflag = TYPEFLAG_DIR;
217 mode = (mode | 0777) & ~tar_umask;
218 } else if (S_ISLNK(mode)) {
219 *header.typeflag = TYPEFLAG_LNK;
220 mode |= 0777;
221 } else if (S_ISREG(mode)) {
222 *header.typeflag = TYPEFLAG_REG;
223 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
224 } else {
225 return error("unsupported file mode: 0%o (SHA1: %s)",
226 mode, sha1_to_hex(sha1));
228 if (pathlen > sizeof(header.name)) {
229 size_t plen = get_path_prefix(path, pathlen,
230 sizeof(header.prefix));
231 size_t rest = pathlen - plen - 1;
232 if (plen > 0 && rest <= sizeof(header.name)) {
233 memcpy(header.prefix, path, plen);
234 memcpy(header.name, path + plen + 1, rest);
235 } else {
236 sprintf(header.name, "%s.data",
237 sha1_to_hex(sha1));
238 strbuf_append_ext_header(&ext_header, "path",
239 path, pathlen);
241 } else
242 memcpy(header.name, path, pathlen);
244 if (S_ISREG(mode) && !args->convert &&
245 sha1_object_info(sha1, &size) == OBJ_BLOB &&
246 size > big_file_threshold)
247 buffer = NULL;
248 else if (S_ISLNK(mode) || S_ISREG(mode)) {
249 enum object_type type;
250 buffer = sha1_file_to_archive(args, path, sha1, old_mode, &type, &size);
251 if (!buffer)
252 return error("cannot read %s", sha1_to_hex(sha1));
253 } else {
254 buffer = NULL;
255 size = 0;
258 if (S_ISLNK(mode)) {
259 if (size > sizeof(header.linkname)) {
260 sprintf(header.linkname, "see %s.paxheader",
261 sha1_to_hex(sha1));
262 strbuf_append_ext_header(&ext_header, "linkpath",
263 buffer, size);
264 } else
265 memcpy(header.linkname, buffer, size);
268 prepare_header(args, &header, mode, size);
270 if (ext_header.len > 0) {
271 err = write_extended_header(args, sha1, ext_header.buf,
272 ext_header.len);
273 if (err) {
274 free(buffer);
275 return err;
278 strbuf_release(&ext_header);
279 write_blocked(&header, sizeof(header));
280 if (S_ISREG(mode) && size > 0) {
281 if (buffer)
282 write_blocked(buffer, size);
283 else
284 err = stream_blocked(sha1);
286 free(buffer);
287 return err;
290 static int write_global_extended_header(struct archiver_args *args)
292 const unsigned char *sha1 = args->commit_sha1;
293 struct strbuf ext_header = STRBUF_INIT;
294 struct ustar_header header;
295 unsigned int mode;
296 int err = 0;
298 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
299 memset(&header, 0, sizeof(header));
300 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
301 mode = 0100666;
302 strcpy(header.name, "pax_global_header");
303 prepare_header(args, &header, mode, ext_header.len);
304 write_blocked(&header, sizeof(header));
305 write_blocked(ext_header.buf, ext_header.len);
306 strbuf_release(&ext_header);
307 return err;
310 static struct archiver **tar_filters;
311 static int nr_tar_filters;
312 static int alloc_tar_filters;
314 static struct archiver *find_tar_filter(const char *name, int len)
316 int i;
317 for (i = 0; i < nr_tar_filters; i++) {
318 struct archiver *ar = tar_filters[i];
319 if (!strncmp(ar->name, name, len) && !ar->name[len])
320 return ar;
322 return NULL;
325 static int tar_filter_config(const char *var, const char *value, void *data)
327 struct archiver *ar;
328 const char *dot;
329 const char *name;
330 const char *type;
331 int namelen;
333 if (prefixcmp(var, "tar."))
334 return 0;
335 dot = strrchr(var, '.');
336 if (dot == var + 9)
337 return 0;
339 name = var + 4;
340 namelen = dot - name;
341 type = dot + 1;
343 ar = find_tar_filter(name, namelen);
344 if (!ar) {
345 ar = xcalloc(1, sizeof(*ar));
346 ar->name = xmemdupz(name, namelen);
347 ar->write_archive = write_tar_filter_archive;
348 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
349 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
350 tar_filters[nr_tar_filters++] = ar;
353 if (!strcmp(type, "command")) {
354 if (!value)
355 return config_error_nonbool(var);
356 free(ar->data);
357 ar->data = xstrdup(value);
358 return 0;
360 if (!strcmp(type, "remote")) {
361 if (git_config_bool(var, value))
362 ar->flags |= ARCHIVER_REMOTE;
363 else
364 ar->flags &= ~ARCHIVER_REMOTE;
365 return 0;
368 return 0;
371 static int git_tar_config(const char *var, const char *value, void *cb)
373 if (!strcmp(var, "tar.umask")) {
374 if (value && !strcmp(value, "user")) {
375 tar_umask = umask(0);
376 umask(tar_umask);
377 } else {
378 tar_umask = git_config_int(var, value);
380 return 0;
383 return tar_filter_config(var, value, cb);
386 static int write_tar_archive(const struct archiver *ar,
387 struct archiver_args *args)
389 int err = 0;
391 if (args->commit_sha1)
392 err = write_global_extended_header(args);
393 if (!err)
394 err = write_archive_entries(args, write_tar_entry);
395 if (!err)
396 write_trailer();
397 return err;
400 static int write_tar_filter_archive(const struct archiver *ar,
401 struct archiver_args *args)
403 struct strbuf cmd = STRBUF_INIT;
404 struct child_process filter;
405 const char *argv[2];
406 int r;
408 if (!ar->data)
409 die("BUG: tar-filter archiver called with no filter defined");
411 strbuf_addstr(&cmd, ar->data);
412 if (args->compression_level >= 0)
413 strbuf_addf(&cmd, " -%d", args->compression_level);
415 memset(&filter, 0, sizeof(filter));
416 argv[0] = cmd.buf;
417 argv[1] = NULL;
418 filter.argv = argv;
419 filter.use_shell = 1;
420 filter.in = -1;
422 if (start_command(&filter) < 0)
423 die_errno("unable to start '%s' filter", argv[0]);
424 close(1);
425 if (dup2(filter.in, 1) < 0)
426 die_errno("unable to redirect descriptor");
427 close(filter.in);
429 r = write_tar_archive(ar, args);
431 close(1);
432 if (finish_command(&filter) != 0)
433 die("'%s' filter reported error", argv[0]);
435 strbuf_release(&cmd);
436 return r;
439 static struct archiver tar_archiver = {
440 "tar",
441 write_tar_archive,
442 ARCHIVER_REMOTE
445 void init_tar_archiver(void)
447 int i;
448 register_archiver(&tar_archiver);
450 tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
451 tar_filter_config("tar.tgz.remote", "true", NULL);
452 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
453 tar_filter_config("tar.tar.gz.remote", "true", NULL);
454 git_config(git_tar_config, NULL);
455 for (i = 0; i < nr_tar_filters; i++) {
456 /* omit any filters that never had a command configured */
457 if (tar_filters[i]->data)
458 register_archiver(tar_filters[i]);