blob: c6ed96ee74ec10f5c9ffb6f520193326d4704b6b [file] [log] [blame]
Rene Scharfe3d749822006-09-24 17:31:10 +02001/*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
Rene Scharfe3d749822006-09-24 17:31:10 +02004#include "cache.h"
Brandon Williamsb2141fc2017-06-14 11:07:36 -07005#include "config.h"
Rene Scharfe3d749822006-09-24 17:31:10 +02006#include "tar.h"
Rene Scharfe3d749822006-09-24 17:31:10 +02007#include "archive.h"
Nguyễn Thái Ngọc Duy55440492012-05-03 08:51:04 +07008#include "streaming.h"
Jeff King767cf452011-06-21 21:26:31 -04009#include "run-command.h"
Rene Scharfe3d749822006-09-24 17:31:10 +020010
11#define RECORDSIZE (512)
12#define BLOCKSIZE (RECORDSIZE * 20)
13
14static char block[BLOCKSIZE];
15static unsigned long offset;
16
René Scharfef08b3b02007-01-05 23:30:22 +010017static int tar_umask = 002;
Rene Scharfe3d749822006-09-24 17:31:10 +020018
Jeff King767cf452011-06-21 21:26:31 -040019static int write_tar_filter_archive(const struct archiver *ar,
20 struct archiver_args *args);
21
Jeff Kingd1657b52016-06-30 05:09:16 -040022/*
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.
Jeff King6e8e0992016-06-30 05:09:20 -040026 *
27 * Likewise for the mtime (which happens to use a buffer of the same size).
Jeff Kingd1657b52016-06-30 05:09:16 -040028 */
Junio C Hamano29493582016-07-14 13:04:43 -070029#if ULONG_MAX == 0xFFFFFFFF
30#define USTAR_MAX_SIZE ULONG_MAX
Junio C Hamano29493582016-07-14 13:04:43 -070031#else
Ramsay Jones3f789712017-05-08 21:34:58 +010032#define USTAR_MAX_SIZE 077777777777UL
Johannes Schindelindddbad72017-04-26 21:29:31 +020033#endif
34#if TIME_MAX == 0xFFFFFFFF
35#define USTAR_MAX_MTIME TIME_MAX
36#else
Ramsay Jones3f789712017-05-08 21:34:58 +010037#define USTAR_MAX_MTIME 077777777777ULL
Junio C Hamano29493582016-07-14 13:04:43 -070038#endif
Jeff Kingd1657b52016-06-30 05:09:16 -040039
Rene Scharfe3d749822006-09-24 17:31:10 +020040/* writes out the whole block, but only if it is full */
41static void write_if_needed(void)
42{
43 if (offset == BLOCKSIZE) {
44 write_or_die(1, block, BLOCKSIZE);
45 offset = 0;
46 }
47}
48
49/*
50 * queues up writes, so that all our write(2) calls write exactly one
51 * full block; pads writes to RECORDSIZE
52 */
Nguyễn Thái Ngọc Duy55440492012-05-03 08:51:04 +070053static void do_write_blocked(const void *data, unsigned long size)
Rene Scharfe3d749822006-09-24 17:31:10 +020054{
55 const char *buf = data;
Rene Scharfe3d749822006-09-24 17:31:10 +020056
57 if (offset) {
58 unsigned long chunk = BLOCKSIZE - offset;
59 if (size < chunk)
60 chunk = size;
61 memcpy(block + offset, buf, chunk);
62 size -= chunk;
63 offset += chunk;
64 buf += chunk;
65 write_if_needed();
66 }
67 while (size >= BLOCKSIZE) {
68 write_or_die(1, buf, BLOCKSIZE);
69 size -= BLOCKSIZE;
70 buf += BLOCKSIZE;
71 }
72 if (size) {
73 memcpy(block + offset, buf, size);
74 offset += size;
75 }
Nguyễn Thái Ngọc Duy55440492012-05-03 08:51:04 +070076}
77
78static void finish_record(void)
79{
80 unsigned long tail;
Rene Scharfe3d749822006-09-24 17:31:10 +020081 tail = offset % RECORDSIZE;
82 if (tail) {
83 memset(block + offset, 0, RECORDSIZE - tail);
84 offset += RECORDSIZE - tail;
85 }
86 write_if_needed();
87}
88
Nguyễn Thái Ngọc Duy55440492012-05-03 08:51:04 +070089static void write_blocked(const void *data, unsigned long size)
90{
91 do_write_blocked(data, size);
92 finish_record();
93}
94
Rene Scharfe3d749822006-09-24 17:31:10 +020095/*
96 * The end of tar archives is marked by 2*512 nul bytes and after that
97 * follows the rest of the block (if any).
98 */
99static void write_trailer(void)
100{
101 int tail = BLOCKSIZE - offset;
102 memset(block + offset, 0, tail);
103 write_or_die(1, block, BLOCKSIZE);
104 if (tail < 2 * RECORDSIZE) {
105 memset(block, 0, offset);
106 write_or_die(1, block, BLOCKSIZE);
107 }
108}
109
Rene Scharfe3d749822006-09-24 17:31:10 +0200110/*
Nguyễn Thái Ngọc Duy55440492012-05-03 08:51:04 +0700111 * queues up writes, so that all our write(2) calls write exactly one
112 * full block; pads writes to RECORDSIZE
113 */
114static int stream_blocked(const unsigned char *sha1)
115{
116 struct git_istream *st;
117 enum object_type type;
118 unsigned long sz;
119 char buf[BLOCKSIZE];
120 ssize_t readlen;
121
122 st = open_istream(sha1, &type, &sz, NULL);
123 if (!st)
124 return error("cannot stream blob %s", sha1_to_hex(sha1));
125 for (;;) {
126 readlen = read_istream(st, buf, sizeof(buf));
127 if (readlen <= 0)
128 break;
129 do_write_blocked(buf, readlen);
130 }
131 close_istream(st);
132 if (!readlen)
133 finish_record();
134 return readlen;
135}
136
137/*
Rene Scharfe3d749822006-09-24 17:31:10 +0200138 * pax extended header records have the format "%u %s=%s\n". %u contains
139 * the size of the whole string (including the %u), the first %s is the
140 * keyword, the second one is the value. This function constructs such a
141 * string and appends it to a struct strbuf.
142 */
143static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
144 const char *value, unsigned int valuelen)
145{
Pierre Habouzit7a604f12007-09-06 13:20:06 +0200146 int len, tmp;
Rene Scharfe3d749822006-09-24 17:31:10 +0200147
148 /* "%u %s=%s\n" */
149 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
150 for (tmp = len; tmp > 9; tmp /= 10)
151 len++;
152
Pierre Habouzit7a604f12007-09-06 13:20:06 +0200153 strbuf_grow(sb, len);
154 strbuf_addf(sb, "%u %s=", len, keyword);
155 strbuf_add(sb, value, valuelen);
156 strbuf_addch(sb, '\n');
Rene Scharfe3d749822006-09-24 17:31:10 +0200157}
158
Jeff Kingd1657b52016-06-30 05:09:16 -0400159/*
160 * Like strbuf_append_ext_header, but for numeric values.
161 */
162static void strbuf_append_ext_header_uint(struct strbuf *sb,
163 const char *keyword,
164 uintmax_t value)
165{
166 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
167 int len;
168
169 len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
170 strbuf_append_ext_header(sb, keyword, buf, len);
171}
172
Rene Scharfe3d749822006-09-24 17:31:10 +0200173static unsigned int ustar_header_chksum(const struct ustar_header *header)
174{
Junio C Hamanoa5a46eb2012-06-13 10:42:25 -0700175 const unsigned char *p = (const unsigned char *)header;
Rene Scharfe3d749822006-09-24 17:31:10 +0200176 unsigned int chksum = 0;
Junio C Hamanoa5a46eb2012-06-13 10:42:25 -0700177 while (p < (const unsigned char *)header->chksum)
Rene Scharfe3d749822006-09-24 17:31:10 +0200178 chksum += *p++;
179 chksum += sizeof(header->chksum) * ' ';
180 p += sizeof(header->chksum);
Junio C Hamanoa5a46eb2012-06-13 10:42:25 -0700181 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
Rene Scharfe3d749822006-09-24 17:31:10 +0200182 chksum += *p++;
183 return chksum;
184}
185
René Scharfe562e25a2008-07-14 21:22:24 +0200186static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
Rene Scharfe3d749822006-09-24 17:31:10 +0200187{
René Scharfe562e25a2008-07-14 21:22:24 +0200188 size_t i = pathlen;
René Scharfe22f0dcd2013-01-05 23:49:54 +0100189 if (i > 1 && path[i - 1] == '/')
190 i--;
Rene Scharfe3d749822006-09-24 17:31:10 +0200191 if (i > maxlen)
192 i = maxlen;
193 do {
194 i--;
René Scharfe562e25a2008-07-14 21:22:24 +0200195 } while (i > 0 && path[i] != '/');
Rene Scharfe3d749822006-09-24 17:31:10 +0200196 return i;
197}
198
Nguyễn Thái Ngọc Duyd240d412012-05-03 08:51:01 +0700199static void prepare_header(struct archiver_args *args,
200 struct ustar_header *header,
201 unsigned int mode, unsigned long size)
202{
Jeff Kingf2f02672015-09-24 17:06:24 -0400203 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
204 xsnprintf(header->size, sizeof(header->size), "%011lo", S_ISREG(mode) ? size : 0);
205 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
Nguyễn Thái Ngọc Duyd240d412012-05-03 08:51:01 +0700206
Jeff Kingf2f02672015-09-24 17:06:24 -0400207 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
208 xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
Nguyễn Thái Ngọc Duyd240d412012-05-03 08:51:01 +0700209 strlcpy(header->uname, "root", sizeof(header->uname));
210 strlcpy(header->gname, "root", sizeof(header->gname));
Jeff Kingf2f02672015-09-24 17:06:24 -0400211 xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
212 xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
Nguyễn Thái Ngọc Duyd240d412012-05-03 08:51:01 +0700213
214 memcpy(header->magic, "ustar", 6);
215 memcpy(header->version, "00", 2);
216
Jeff King9e6c1e92016-05-26 00:28:08 -0400217 xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
Nguyễn Thái Ngọc Duyd240d412012-05-03 08:51:01 +0700218}
219
René Scharfe560b0e82016-08-06 16:35:38 +0200220static void write_extended_header(struct archiver_args *args,
221 const unsigned char *sha1,
222 const void *buffer, unsigned long size)
Nguyễn Thái Ngọc Duyd240d412012-05-03 08:51:01 +0700223{
224 struct ustar_header header;
225 unsigned int mode;
226 memset(&header, 0, sizeof(header));
227 *header.typeflag = TYPEFLAG_EXT_HEADER;
Junio C Hamano15c6ef72014-10-20 12:04:46 -0700228 mode = 0100666;
Jeff Kingf2f02672015-09-24 17:06:24 -0400229 xsnprintf(header.name, sizeof(header.name), "%s.paxheader", sha1_to_hex(sha1));
Nguyễn Thái Ngọc Duyd240d412012-05-03 08:51:01 +0700230 prepare_header(args, &header, mode, size);
231 write_blocked(&header, sizeof(header));
232 write_blocked(buffer, size);
Nguyễn Thái Ngọc Duyd240d412012-05-03 08:51:01 +0700233}
234
René Scharfe562e25a2008-07-14 21:22:24 +0200235static int write_tar_entry(struct archiver_args *args,
Nguyễn Thái Ngọc Duy9cb513b2012-05-03 08:51:03 +0700236 const unsigned char *sha1,
237 const char *path, size_t pathlen,
238 unsigned int mode)
Rene Scharfe3d749822006-09-24 17:31:10 +0200239{
240 struct ustar_header header;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500241 struct strbuf ext_header = STRBUF_INIT;
Nguyễn Thái Ngọc Duy9cb513b2012-05-03 08:51:03 +0700242 unsigned int old_mode = mode;
Jeff Kingd1657b52016-06-30 05:09:16 -0400243 unsigned long size, size_in_header;
Nguyễn Thái Ngọc Duy9cb513b2012-05-03 08:51:03 +0700244 void *buffer;
René Scharfe562e25a2008-07-14 21:22:24 +0200245 int err = 0;
Rene Scharfe3d749822006-09-24 17:31:10 +0200246
247 memset(&header, 0, sizeof(header));
Rene Scharfe3d749822006-09-24 17:31:10 +0200248
Nguyễn Thái Ngọc Duy85390702012-05-03 08:51:02 +0700249 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
250 *header.typeflag = TYPEFLAG_DIR;
251 mode = (mode | 0777) & ~tar_umask;
252 } else if (S_ISLNK(mode)) {
253 *header.typeflag = TYPEFLAG_LNK;
254 mode |= 0777;
255 } else if (S_ISREG(mode)) {
256 *header.typeflag = TYPEFLAG_REG;
257 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
Rene Scharfe3d749822006-09-24 17:31:10 +0200258 } else {
Nguyễn Thái Ngọc Duy85390702012-05-03 08:51:02 +0700259 return error("unsupported file mode: 0%o (SHA1: %s)",
260 mode, sha1_to_hex(sha1));
Rene Scharfe3d749822006-09-24 17:31:10 +0200261 }
Nguyễn Thái Ngọc Duy85390702012-05-03 08:51:02 +0700262 if (pathlen > sizeof(header.name)) {
263 size_t plen = get_path_prefix(path, pathlen,
264 sizeof(header.prefix));
265 size_t rest = pathlen - plen - 1;
266 if (plen > 0 && rest <= sizeof(header.name)) {
267 memcpy(header.prefix, path, plen);
Jeff King108332c2015-09-24 17:03:49 -0400268 memcpy(header.name, path + plen + 1, rest);
Nguyễn Thái Ngọc Duy85390702012-05-03 08:51:02 +0700269 } else {
Jeff Kingf2f02672015-09-24 17:06:24 -0400270 xsnprintf(header.name, sizeof(header.name), "%s.data",
271 sha1_to_hex(sha1));
Nguyễn Thái Ngọc Duy85390702012-05-03 08:51:02 +0700272 strbuf_append_ext_header(&ext_header, "path",
273 path, pathlen);
274 }
275 } else
276 memcpy(header.name, path, pathlen);
Rene Scharfe3d749822006-09-24 17:31:10 +0200277
Nguyễn Thái Ngọc Duy55440492012-05-03 08:51:04 +0700278 if (S_ISREG(mode) && !args->convert &&
279 sha1_object_info(sha1, &size) == OBJ_BLOB &&
280 size > big_file_threshold)
281 buffer = NULL;
282 else if (S_ISLNK(mode) || S_ISREG(mode)) {
Nguyễn Thái Ngọc Duy9cb513b2012-05-03 08:51:03 +0700283 enum object_type type;
284 buffer = sha1_file_to_archive(args, path, sha1, old_mode, &type, &size);
285 if (!buffer)
286 return error("cannot read %s", sha1_to_hex(sha1));
287 } else {
288 buffer = NULL;
289 size = 0;
290 }
291
292 if (S_ISLNK(mode)) {
Rene Scharfe3d749822006-09-24 17:31:10 +0200293 if (size > sizeof(header.linkname)) {
Jeff Kingf2f02672015-09-24 17:06:24 -0400294 xsnprintf(header.linkname, sizeof(header.linkname),
295 "see %s.paxheader", sha1_to_hex(sha1));
Rene Scharfe3d749822006-09-24 17:31:10 +0200296 strbuf_append_ext_header(&ext_header, "linkpath",
297 buffer, size);
298 } else
299 memcpy(header.linkname, buffer, size);
300 }
301
Jeff Kingd1657b52016-06-30 05:09:16 -0400302 size_in_header = size;
303 if (S_ISREG(mode) && size > USTAR_MAX_SIZE) {
304 size_in_header = 0;
305 strbuf_append_ext_header_uint(&ext_header, "size", size);
306 }
307
308 prepare_header(args, &header, mode, size_in_header);
Rene Scharfe3d749822006-09-24 17:31:10 +0200309
310 if (ext_header.len > 0) {
René Scharfe560b0e82016-08-06 16:35:38 +0200311 write_extended_header(args, sha1, ext_header.buf,
312 ext_header.len);
Rene Scharfe3d749822006-09-24 17:31:10 +0200313 }
Pierre Habouzit7a604f12007-09-06 13:20:06 +0200314 strbuf_release(&ext_header);
Rene Scharfe3d749822006-09-24 17:31:10 +0200315 write_blocked(&header, sizeof(header));
Nguyễn Thái Ngọc Duy55440492012-05-03 08:51:04 +0700316 if (S_ISREG(mode) && size > 0) {
317 if (buffer)
318 write_blocked(buffer, size);
319 else
320 err = stream_blocked(sha1);
321 }
Nguyễn Thái Ngọc Duy9cb513b2012-05-03 08:51:03 +0700322 free(buffer);
René Scharfe562e25a2008-07-14 21:22:24 +0200323 return err;
Rene Scharfe3d749822006-09-24 17:31:10 +0200324}
325
Jeff King5caeeb82016-06-30 05:09:26 -0400326static void write_global_extended_header(struct archiver_args *args)
Rene Scharfe3d749822006-09-24 17:31:10 +0200327{
René Scharfe562e25a2008-07-14 21:22:24 +0200328 const unsigned char *sha1 = args->commit_sha1;
Brandon Caseyf285a2d2008-10-09 14:12:12 -0500329 struct strbuf ext_header = STRBUF_INIT;
Nguyễn Thái Ngọc Duyd240d412012-05-03 08:51:01 +0700330 struct ustar_header header;
331 unsigned int mode;
Pierre Habouzit7a604f12007-09-06 13:20:06 +0200332
Jeff King6e8e0992016-06-30 05:09:20 -0400333 if (sha1)
334 strbuf_append_ext_header(&ext_header, "comment",
335 sha1_to_hex(sha1), 40);
336 if (args->time > USTAR_MAX_MTIME) {
337 strbuf_append_ext_header_uint(&ext_header, "mtime",
338 args->time);
339 args->time = USTAR_MAX_MTIME;
340 }
341
342 if (!ext_header.len)
Jeff King5caeeb82016-06-30 05:09:26 -0400343 return;
Jeff King6e8e0992016-06-30 05:09:20 -0400344
Nguyễn Thái Ngọc Duyd240d412012-05-03 08:51:01 +0700345 memset(&header, 0, sizeof(header));
346 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
Junio C Hamano15c6ef72014-10-20 12:04:46 -0700347 mode = 0100666;
Jeff King5096d492015-09-24 17:06:08 -0400348 xsnprintf(header.name, sizeof(header.name), "pax_global_header");
Nguyễn Thái Ngọc Duyd240d412012-05-03 08:51:01 +0700349 prepare_header(args, &header, mode, ext_header.len);
350 write_blocked(&header, sizeof(header));
351 write_blocked(ext_header.buf, ext_header.len);
Pierre Habouzit7a604f12007-09-06 13:20:06 +0200352 strbuf_release(&ext_header);
Rene Scharfe3d749822006-09-24 17:31:10 +0200353}
354
Jeff King767cf452011-06-21 21:26:31 -0400355static struct archiver **tar_filters;
356static int nr_tar_filters;
357static int alloc_tar_filters;
358
359static struct archiver *find_tar_filter(const char *name, int len)
360{
361 int i;
362 for (i = 0; i < nr_tar_filters; i++) {
363 struct archiver *ar = tar_filters[i];
364 if (!strncmp(ar->name, name, len) && !ar->name[len])
365 return ar;
366 }
367 return NULL;
368}
369
370static int tar_filter_config(const char *var, const char *value, void *data)
371{
372 struct archiver *ar;
Jeff King767cf452011-06-21 21:26:31 -0400373 const char *name;
374 const char *type;
375 int namelen;
376
Jeff King785a0422013-01-23 01:23:27 -0500377 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
Jeff King767cf452011-06-21 21:26:31 -0400378 return 0;
Jeff King767cf452011-06-21 21:26:31 -0400379
380 ar = find_tar_filter(name, namelen);
381 if (!ar) {
382 ar = xcalloc(1, sizeof(*ar));
383 ar->name = xmemdupz(name, namelen);
384 ar->write_archive = write_tar_filter_archive;
385 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
386 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
387 tar_filters[nr_tar_filters++] = ar;
388 }
389
390 if (!strcmp(type, "command")) {
391 if (!value)
392 return config_error_nonbool(var);
393 free(ar->data);
394 ar->data = xstrdup(value);
395 return 0;
396 }
Jeff King7b977302011-06-21 23:17:35 -0400397 if (!strcmp(type, "remote")) {
398 if (git_config_bool(var, value))
399 ar->flags |= ARCHIVER_REMOTE;
400 else
401 ar->flags &= ~ARCHIVER_REMOTE;
402 return 0;
403 }
Jeff King767cf452011-06-21 21:26:31 -0400404
405 return 0;
406}
407
Johannes Schindelinef90d6d2008-05-14 18:46:53 +0100408static int git_tar_config(const char *var, const char *value, void *cb)
Rene Scharfe3d749822006-09-24 17:31:10 +0200409{
410 if (!strcmp(var, "tar.umask")) {
Junio C Hamanocc1816b2008-02-08 20:38:22 -0800411 if (value && !strcmp(value, "user")) {
Rene Scharfe3d749822006-09-24 17:31:10 +0200412 tar_umask = umask(0);
413 umask(tar_umask);
414 } else {
415 tar_umask = git_config_int(var, value);
416 }
417 return 0;
418 }
Jeff King767cf452011-06-21 21:26:31 -0400419
420 return tar_filter_config(var, value, cb);
Rene Scharfe3d749822006-09-24 17:31:10 +0200421}
422
Jeff King4d7c9892011-06-21 21:24:07 -0400423static int write_tar_archive(const struct archiver *ar,
424 struct archiver_args *args)
Rene Scharfe3d749822006-09-24 17:31:10 +0200425{
René Scharfe562e25a2008-07-14 21:22:24 +0200426 int err = 0;
427
Jeff King5caeeb82016-06-30 05:09:26 -0400428 write_global_extended_header(args);
429 err = write_archive_entries(args, write_tar_entry);
René Scharfe562e25a2008-07-14 21:22:24 +0200430 if (!err)
431 write_trailer();
432 return err;
Rene Scharfe3d749822006-09-24 17:31:10 +0200433}
Jeff King13e0f882011-06-21 21:23:33 -0400434
Jeff King767cf452011-06-21 21:26:31 -0400435static int write_tar_filter_archive(const struct archiver *ar,
436 struct archiver_args *args)
437{
438 struct strbuf cmd = STRBUF_INIT;
René Scharfed3180272014-08-19 21:09:35 +0200439 struct child_process filter = CHILD_PROCESS_INIT;
Jeff King767cf452011-06-21 21:26:31 -0400440 const char *argv[2];
441 int r;
442
443 if (!ar->data)
444 die("BUG: tar-filter archiver called with no filter defined");
445
446 strbuf_addstr(&cmd, ar->data);
447 if (args->compression_level >= 0)
448 strbuf_addf(&cmd, " -%d", args->compression_level);
449
Jeff King767cf452011-06-21 21:26:31 -0400450 argv[0] = cmd.buf;
451 argv[1] = NULL;
452 filter.argv = argv;
453 filter.use_shell = 1;
454 filter.in = -1;
455
456 if (start_command(&filter) < 0)
457 die_errno("unable to start '%s' filter", argv[0]);
458 close(1);
459 if (dup2(filter.in, 1) < 0)
460 die_errno("unable to redirect descriptor");
461 close(filter.in);
462
463 r = write_tar_archive(ar, args);
464
465 close(1);
466 if (finish_command(&filter) != 0)
467 die("'%s' filter reported error", argv[0]);
468
469 strbuf_release(&cmd);
470 return r;
471}
472
Jeff King13e0f882011-06-21 21:23:33 -0400473static struct archiver tar_archiver = {
474 "tar",
475 write_tar_archive,
Jeff King7b977302011-06-21 23:17:35 -0400476 ARCHIVER_REMOTE
Jeff King13e0f882011-06-21 21:23:33 -0400477};
478
479void init_tar_archiver(void)
480{
Jeff King767cf452011-06-21 21:26:31 -0400481 int i;
Jeff King13e0f882011-06-21 21:23:33 -0400482 register_archiver(&tar_archiver);
Jeff King767cf452011-06-21 21:26:31 -0400483
Jeff King0e804e02011-06-21 21:27:35 -0400484 tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
Jeff King7b977302011-06-21 23:17:35 -0400485 tar_filter_config("tar.tgz.remote", "true", NULL);
Jeff King0e804e02011-06-21 21:27:35 -0400486 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
Jeff King7b977302011-06-21 23:17:35 -0400487 tar_filter_config("tar.tar.gz.remote", "true", NULL);
Jeff King13e0f882011-06-21 21:23:33 -0400488 git_config(git_tar_config, NULL);
Jeff King767cf452011-06-21 21:26:31 -0400489 for (i = 0; i < nr_tar_filters; i++) {
490 /* omit any filters that never had a command configured */
491 if (tar_filters[i]->data)
492 register_archiver(tar_filters[i]);
493 }
Jeff King13e0f882011-06-21 21:23:33 -0400494}