]> git.ipfire.org Git - thirdparty/git.git/blame - archive-tar.c
Git 1.7.11-rc0
[thirdparty/git.git] / archive-tar.c
CommitLineData
3d74982f
RS
1/*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
3d74982f 4#include "cache.h"
3d74982f 5#include "tar.h"
3d74982f 6#include "archive.h"
5544049d 7#include "streaming.h"
767cf457 8#include "run-command.h"
3d74982f
RS
9
10#define RECORDSIZE (512)
11#define BLOCKSIZE (RECORDSIZE * 20)
12
13static char block[BLOCKSIZE];
14static unsigned long offset;
15
f08b3b0e 16static int tar_umask = 002;
3d74982f 17
767cf457
JK
18static int write_tar_filter_archive(const struct archiver *ar,
19 struct archiver_args *args);
20
3d74982f
RS
21/* writes out the whole block, but only if it is full */
22static void write_if_needed(void)
23{
24 if (offset == BLOCKSIZE) {
25 write_or_die(1, block, BLOCKSIZE);
26 offset = 0;
27 }
28}
29
30/*
31 * queues up writes, so that all our write(2) calls write exactly one
32 * full block; pads writes to RECORDSIZE
33 */
5544049d 34static void do_write_blocked(const void *data, unsigned long size)
3d74982f
RS
35{
36 const char *buf = data;
3d74982f
RS
37
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();
47 }
48 while (size >= BLOCKSIZE) {
49 write_or_die(1, buf, BLOCKSIZE);
50 size -= BLOCKSIZE;
51 buf += BLOCKSIZE;
52 }
53 if (size) {
54 memcpy(block + offset, buf, size);
55 offset += size;
56 }
5544049d
NTND
57}
58
59static void finish_record(void)
60{
61 unsigned long tail;
3d74982f
RS
62 tail = offset % RECORDSIZE;
63 if (tail) {
64 memset(block + offset, 0, RECORDSIZE - tail);
65 offset += RECORDSIZE - tail;
66 }
67 write_if_needed();
68}
69
5544049d
NTND
70static void write_blocked(const void *data, unsigned long size)
71{
72 do_write_blocked(data, size);
73 finish_record();
74}
75
3d74982f
RS
76/*
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).
79 */
80static void write_trailer(void)
81{
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);
88 }
89}
90
5544049d
NTND
91/*
92 * queues up writes, so that all our write(2) calls write exactly one
93 * full block; pads writes to RECORDSIZE
94 */
95static int stream_blocked(const unsigned char *sha1)
96{
97 struct git_istream *st;
98 enum object_type type;
99 unsigned long sz;
100 char buf[BLOCKSIZE];
101 ssize_t readlen;
102
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);
111 }
112 close_istream(st);
113 if (!readlen)
114 finish_record();
115 return readlen;
116}
117
3d74982f
RS
118/*
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.
123 */
124static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
125 const char *value, unsigned int valuelen)
126{
7a604f16 127 int len, tmp;
3d74982f
RS
128
129 /* "%u %s=%s\n" */
130 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
131 for (tmp = len; tmp > 9; tmp /= 10)
132 len++;
133
7a604f16
PH
134 strbuf_grow(sb, len);
135 strbuf_addf(sb, "%u %s=", len, keyword);
136 strbuf_add(sb, value, valuelen);
137 strbuf_addch(sb, '\n');
3d74982f
RS
138}
139
140static unsigned int ustar_header_chksum(const struct ustar_header *header)
141{
bf38245b 142 const char *p = (const char *)header;
3d74982f
RS
143 unsigned int chksum = 0;
144 while (p < header->chksum)
145 chksum += *p++;
146 chksum += sizeof(header->chksum) * ' ';
147 p += sizeof(header->chksum);
bf38245b 148 while (p < (const char *)header + sizeof(struct ustar_header))
3d74982f
RS
149 chksum += *p++;
150 return chksum;
151}
152
562e25ab 153static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
3d74982f 154{
562e25ab 155 size_t i = pathlen;
3d74982f
RS
156 if (i > maxlen)
157 i = maxlen;
158 do {
159 i--;
562e25ab 160 } while (i > 0 && path[i] != '/');
3d74982f
RS
161 return i;
162}
163
d240d410
NTND
164static void prepare_header(struct archiver_args *args,
165 struct ustar_header *header,
166 unsigned int mode, unsigned long size)
167{
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);
171
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);
178
179 memcpy(header->magic, "ustar", 6);
180 memcpy(header->version, "00", 2);
181
182 sprintf(header->chksum, "%07o", ustar_header_chksum(header));
183}
184
185static int write_extended_header(struct archiver_args *args,
186 const unsigned char *sha1,
187 const void *buffer, unsigned long size)
188{
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;
199}
200
562e25ab 201static int write_tar_entry(struct archiver_args *args,
9cb513b7
NTND
202 const unsigned char *sha1,
203 const char *path, size_t pathlen,
204 unsigned int mode)
3d74982f
RS
205{
206 struct ustar_header header;
f285a2d7 207 struct strbuf ext_header = STRBUF_INIT;
9cb513b7
NTND
208 unsigned int old_mode = mode;
209 unsigned long size;
210 void *buffer;
562e25ab 211 int err = 0;
3d74982f
RS
212
213 memset(&header, 0, sizeof(header));
3d74982f 214
85390709
NTND
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;
3d74982f 224 } else {
85390709
NTND
225 return error("unsupported file mode: 0%o (SHA1: %s)",
226 mode, sha1_to_hex(sha1));
227 }
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);
3d74982f 235 } else {
85390709
NTND
236 sprintf(header.name, "%s.data",
237 sha1_to_hex(sha1));
238 strbuf_append_ext_header(&ext_header, "path",
239 path, pathlen);
3d74982f 240 }
85390709
NTND
241 } else
242 memcpy(header.name, path, pathlen);
3d74982f 243
5544049d
NTND
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)) {
9cb513b7
NTND
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;
256 }
257
258 if (S_ISLNK(mode)) {
3d74982f
RS
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);
266 }
267
d240d410 268 prepare_header(args, &header, mode, size);
3d74982f
RS
269
270 if (ext_header.len > 0) {
d240d410
NTND
271 err = write_extended_header(args, sha1, ext_header.buf,
272 ext_header.len);
9cb513b7
NTND
273 if (err) {
274 free(buffer);
562e25ab 275 return err;
9cb513b7 276 }
3d74982f 277 }
7a604f16 278 strbuf_release(&ext_header);
3d74982f 279 write_blocked(&header, sizeof(header));
5544049d
NTND
280 if (S_ISREG(mode) && size > 0) {
281 if (buffer)
282 write_blocked(buffer, size);
283 else
284 err = stream_blocked(sha1);
285 }
9cb513b7 286 free(buffer);
562e25ab 287 return err;
3d74982f
RS
288}
289
562e25ab 290static int write_global_extended_header(struct archiver_args *args)
3d74982f 291{
562e25ab 292 const unsigned char *sha1 = args->commit_sha1;
f285a2d7 293 struct strbuf ext_header = STRBUF_INIT;
d240d410
NTND
294 struct ustar_header header;
295 unsigned int mode;
296 int err = 0;
7a604f16 297
3d74982f 298 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
d240d410
NTND
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);
7a604f16 306 strbuf_release(&ext_header);
562e25ab 307 return err;
3d74982f
RS
308}
309
767cf457
JK
310static struct archiver **tar_filters;
311static int nr_tar_filters;
312static int alloc_tar_filters;
313
314static struct archiver *find_tar_filter(const char *name, int len)
315{
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;
321 }
322 return NULL;
323}
324
325static int tar_filter_config(const char *var, const char *value, void *data)
326{
327 struct archiver *ar;
328 const char *dot;
329 const char *name;
330 const char *type;
331 int namelen;
332
333 if (prefixcmp(var, "tar."))
334 return 0;
335 dot = strrchr(var, '.');
336 if (dot == var + 9)
337 return 0;
338
339 name = var + 4;
340 namelen = dot - name;
341 type = dot + 1;
342
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;
351 }
352
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;
359 }
7b97730b
JK
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;
366 }
767cf457
JK
367
368 return 0;
369}
370
ef90d6d4 371static int git_tar_config(const char *var, const char *value, void *cb)
3d74982f
RS
372{
373 if (!strcmp(var, "tar.umask")) {
cc1816b0 374 if (value && !strcmp(value, "user")) {
3d74982f
RS
375 tar_umask = umask(0);
376 umask(tar_umask);
377 } else {
378 tar_umask = git_config_int(var, value);
379 }
380 return 0;
381 }
767cf457
JK
382
383 return tar_filter_config(var, value, cb);
3d74982f
RS
384}
385
4d7c9898
JK
386static int write_tar_archive(const struct archiver *ar,
387 struct archiver_args *args)
3d74982f 388{
562e25ab 389 int err = 0;
3d74982f 390
3d74982f 391 if (args->commit_sha1)
562e25ab
RS
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;
3d74982f 398}
13e0f88d 399
767cf457
JK
400static int write_tar_filter_archive(const struct archiver *ar,
401 struct archiver_args *args)
402{
403 struct strbuf cmd = STRBUF_INIT;
404 struct child_process filter;
405 const char *argv[2];
406 int r;
407
408 if (!ar->data)
409 die("BUG: tar-filter archiver called with no filter defined");
410
411 strbuf_addstr(&cmd, ar->data);
412 if (args->compression_level >= 0)
413 strbuf_addf(&cmd, " -%d", args->compression_level);
414
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;
421
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);
428
429 r = write_tar_archive(ar, args);
430
431 close(1);
432 if (finish_command(&filter) != 0)
433 die("'%s' filter reported error", argv[0]);
434
435 strbuf_release(&cmd);
436 return r;
437}
438
13e0f88d
JK
439static struct archiver tar_archiver = {
440 "tar",
441 write_tar_archive,
7b97730b 442 ARCHIVER_REMOTE
13e0f88d
JK
443};
444
445void init_tar_archiver(void)
446{
767cf457 447 int i;
13e0f88d 448 register_archiver(&tar_archiver);
767cf457 449
0e804e09 450 tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
7b97730b 451 tar_filter_config("tar.tgz.remote", "true", NULL);
0e804e09 452 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
7b97730b 453 tar_filter_config("tar.tar.gz.remote", "true", NULL);
13e0f88d 454 git_config(git_tar_config, NULL);
767cf457
JK
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]);
459 }
13e0f88d 460}