]> git.ipfire.org Git - thirdparty/git.git/blame - archive-tar.c
archive: provide builtin .tar.gz filter
[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"
767cf457 7#include "run-command.h"
3d74982f
RS
8
9#define RECORDSIZE (512)
10#define BLOCKSIZE (RECORDSIZE * 20)
11
12static char block[BLOCKSIZE];
13static unsigned long offset;
14
f08b3b0e 15static int tar_umask = 002;
3d74982f 16
767cf457
JK
17static int write_tar_filter_archive(const struct archiver *ar,
18 struct archiver_args *args);
19
3d74982f
RS
20/* writes out the whole block, but only if it is full */
21static void write_if_needed(void)
22{
23 if (offset == BLOCKSIZE) {
24 write_or_die(1, block, BLOCKSIZE);
25 offset = 0;
26 }
27}
28
29/*
30 * queues up writes, so that all our write(2) calls write exactly one
31 * full block; pads writes to RECORDSIZE
32 */
33static void write_blocked(const void *data, unsigned long size)
34{
35 const char *buf = data;
36 unsigned long tail;
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 }
57 tail = offset % RECORDSIZE;
58 if (tail) {
59 memset(block + offset, 0, RECORDSIZE - tail);
60 offset += RECORDSIZE - tail;
61 }
62 write_if_needed();
63}
64
65/*
66 * The end of tar archives is marked by 2*512 nul bytes and after that
67 * follows the rest of the block (if any).
68 */
69static void write_trailer(void)
70{
71 int tail = BLOCKSIZE - offset;
72 memset(block + offset, 0, tail);
73 write_or_die(1, block, BLOCKSIZE);
74 if (tail < 2 * RECORDSIZE) {
75 memset(block, 0, offset);
76 write_or_die(1, block, BLOCKSIZE);
77 }
78}
79
3d74982f
RS
80/*
81 * pax extended header records have the format "%u %s=%s\n". %u contains
82 * the size of the whole string (including the %u), the first %s is the
83 * keyword, the second one is the value. This function constructs such a
84 * string and appends it to a struct strbuf.
85 */
86static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
87 const char *value, unsigned int valuelen)
88{
7a604f16 89 int len, tmp;
3d74982f
RS
90
91 /* "%u %s=%s\n" */
92 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
93 for (tmp = len; tmp > 9; tmp /= 10)
94 len++;
95
7a604f16
PH
96 strbuf_grow(sb, len);
97 strbuf_addf(sb, "%u %s=", len, keyword);
98 strbuf_add(sb, value, valuelen);
99 strbuf_addch(sb, '\n');
3d74982f
RS
100}
101
102static unsigned int ustar_header_chksum(const struct ustar_header *header)
103{
104 char *p = (char *)header;
105 unsigned int chksum = 0;
106 while (p < header->chksum)
107 chksum += *p++;
108 chksum += sizeof(header->chksum) * ' ';
109 p += sizeof(header->chksum);
110 while (p < (char *)header + sizeof(struct ustar_header))
111 chksum += *p++;
112 return chksum;
113}
114
562e25ab 115static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
3d74982f 116{
562e25ab 117 size_t i = pathlen;
3d74982f
RS
118 if (i > maxlen)
119 i = maxlen;
120 do {
121 i--;
562e25ab 122 } while (i > 0 && path[i] != '/');
3d74982f
RS
123 return i;
124}
125
562e25ab
RS
126static int write_tar_entry(struct archiver_args *args,
127 const unsigned char *sha1, const char *path, size_t pathlen,
128 unsigned int mode, void *buffer, unsigned long size)
3d74982f
RS
129{
130 struct ustar_header header;
f285a2d7 131 struct strbuf ext_header = STRBUF_INIT;
562e25ab 132 int err = 0;
3d74982f
RS
133
134 memset(&header, 0, sizeof(header));
3d74982f
RS
135
136 if (!sha1) {
137 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
138 mode = 0100666;
139 strcpy(header.name, "pax_global_header");
140 } else if (!path) {
141 *header.typeflag = TYPEFLAG_EXT_HEADER;
142 mode = 0100666;
143 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
144 } else {
302b9282 145 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
3d74982f
RS
146 *header.typeflag = TYPEFLAG_DIR;
147 mode = (mode | 0777) & ~tar_umask;
148 } else if (S_ISLNK(mode)) {
149 *header.typeflag = TYPEFLAG_LNK;
150 mode |= 0777;
151 } else if (S_ISREG(mode)) {
152 *header.typeflag = TYPEFLAG_REG;
153 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
154 } else {
562e25ab
RS
155 return error("unsupported file mode: 0%o (SHA1: %s)",
156 mode, sha1_to_hex(sha1));
3d74982f 157 }
562e25ab
RS
158 if (pathlen > sizeof(header.name)) {
159 size_t plen = get_path_prefix(path, pathlen,
160 sizeof(header.prefix));
161 size_t rest = pathlen - plen - 1;
3d74982f 162 if (plen > 0 && rest <= sizeof(header.name)) {
562e25ab
RS
163 memcpy(header.prefix, path, plen);
164 memcpy(header.name, path + plen + 1, rest);
3d74982f
RS
165 } else {
166 sprintf(header.name, "%s.data",
167 sha1_to_hex(sha1));
168 strbuf_append_ext_header(&ext_header, "path",
562e25ab 169 path, pathlen);
3d74982f
RS
170 }
171 } else
562e25ab 172 memcpy(header.name, path, pathlen);
3d74982f
RS
173 }
174
175 if (S_ISLNK(mode) && buffer) {
176 if (size > sizeof(header.linkname)) {
177 sprintf(header.linkname, "see %s.paxheader",
178 sha1_to_hex(sha1));
179 strbuf_append_ext_header(&ext_header, "linkpath",
180 buffer, size);
181 } else
182 memcpy(header.linkname, buffer, size);
183 }
184
185 sprintf(header.mode, "%07o", mode & 07777);
186 sprintf(header.size, "%011lo", S_ISREG(mode) ? size : 0);
3e1629f5 187 sprintf(header.mtime, "%011lo", (unsigned long) args->time);
3d74982f 188
3d74982f
RS
189 sprintf(header.uid, "%07o", 0);
190 sprintf(header.gid, "%07o", 0);
f08b3b0e
RS
191 strlcpy(header.uname, "root", sizeof(header.uname));
192 strlcpy(header.gname, "root", sizeof(header.gname));
3d74982f
RS
193 sprintf(header.devmajor, "%07o", 0);
194 sprintf(header.devminor, "%07o", 0);
195
196 memcpy(header.magic, "ustar", 6);
197 memcpy(header.version, "00", 2);
198
199 sprintf(header.chksum, "%07o", ustar_header_chksum(&header));
200
201 if (ext_header.len > 0) {
562e25ab
RS
202 err = write_tar_entry(args, sha1, NULL, 0, 0, ext_header.buf,
203 ext_header.len);
204 if (err)
205 return err;
3d74982f 206 }
7a604f16 207 strbuf_release(&ext_header);
3d74982f
RS
208 write_blocked(&header, sizeof(header));
209 if (S_ISREG(mode) && buffer && size > 0)
210 write_blocked(buffer, size);
562e25ab 211 return err;
3d74982f
RS
212}
213
562e25ab 214static int write_global_extended_header(struct archiver_args *args)
3d74982f 215{
562e25ab 216 const unsigned char *sha1 = args->commit_sha1;
f285a2d7 217 struct strbuf ext_header = STRBUF_INIT;
562e25ab 218 int err;
7a604f16 219
3d74982f 220 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
562e25ab
RS
221 err = write_tar_entry(args, NULL, NULL, 0, 0, ext_header.buf,
222 ext_header.len);
7a604f16 223 strbuf_release(&ext_header);
562e25ab 224 return err;
3d74982f
RS
225}
226
767cf457
JK
227static struct archiver **tar_filters;
228static int nr_tar_filters;
229static int alloc_tar_filters;
230
231static struct archiver *find_tar_filter(const char *name, int len)
232{
233 int i;
234 for (i = 0; i < nr_tar_filters; i++) {
235 struct archiver *ar = tar_filters[i];
236 if (!strncmp(ar->name, name, len) && !ar->name[len])
237 return ar;
238 }
239 return NULL;
240}
241
242static int tar_filter_config(const char *var, const char *value, void *data)
243{
244 struct archiver *ar;
245 const char *dot;
246 const char *name;
247 const char *type;
248 int namelen;
249
250 if (prefixcmp(var, "tar."))
251 return 0;
252 dot = strrchr(var, '.');
253 if (dot == var + 9)
254 return 0;
255
256 name = var + 4;
257 namelen = dot - name;
258 type = dot + 1;
259
260 ar = find_tar_filter(name, namelen);
261 if (!ar) {
262 ar = xcalloc(1, sizeof(*ar));
263 ar->name = xmemdupz(name, namelen);
264 ar->write_archive = write_tar_filter_archive;
265 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
266 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
267 tar_filters[nr_tar_filters++] = ar;
268 }
269
270 if (!strcmp(type, "command")) {
271 if (!value)
272 return config_error_nonbool(var);
273 free(ar->data);
274 ar->data = xstrdup(value);
275 return 0;
276 }
277
278 return 0;
279}
280
ef90d6d4 281static int git_tar_config(const char *var, const char *value, void *cb)
3d74982f
RS
282{
283 if (!strcmp(var, "tar.umask")) {
cc1816b0 284 if (value && !strcmp(value, "user")) {
3d74982f
RS
285 tar_umask = umask(0);
286 umask(tar_umask);
287 } else {
288 tar_umask = git_config_int(var, value);
289 }
290 return 0;
291 }
767cf457
JK
292
293 return tar_filter_config(var, value, cb);
3d74982f
RS
294}
295
4d7c9898
JK
296static int write_tar_archive(const struct archiver *ar,
297 struct archiver_args *args)
3d74982f 298{
562e25ab 299 int err = 0;
3d74982f 300
3d74982f 301 if (args->commit_sha1)
562e25ab
RS
302 err = write_global_extended_header(args);
303 if (!err)
304 err = write_archive_entries(args, write_tar_entry);
305 if (!err)
306 write_trailer();
307 return err;
3d74982f 308}
13e0f88d 309
767cf457
JK
310static int write_tar_filter_archive(const struct archiver *ar,
311 struct archiver_args *args)
312{
313 struct strbuf cmd = STRBUF_INIT;
314 struct child_process filter;
315 const char *argv[2];
316 int r;
317
318 if (!ar->data)
319 die("BUG: tar-filter archiver called with no filter defined");
320
321 strbuf_addstr(&cmd, ar->data);
322 if (args->compression_level >= 0)
323 strbuf_addf(&cmd, " -%d", args->compression_level);
324
325 memset(&filter, 0, sizeof(filter));
326 argv[0] = cmd.buf;
327 argv[1] = NULL;
328 filter.argv = argv;
329 filter.use_shell = 1;
330 filter.in = -1;
331
332 if (start_command(&filter) < 0)
333 die_errno("unable to start '%s' filter", argv[0]);
334 close(1);
335 if (dup2(filter.in, 1) < 0)
336 die_errno("unable to redirect descriptor");
337 close(filter.in);
338
339 r = write_tar_archive(ar, args);
340
341 close(1);
342 if (finish_command(&filter) != 0)
343 die("'%s' filter reported error", argv[0]);
344
345 strbuf_release(&cmd);
346 return r;
347}
348
13e0f88d
JK
349static struct archiver tar_archiver = {
350 "tar",
351 write_tar_archive,
352 0
353};
354
355void init_tar_archiver(void)
356{
767cf457 357 int i;
13e0f88d 358 register_archiver(&tar_archiver);
767cf457 359
0e804e09
JK
360 tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
361 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
13e0f88d 362 git_config(git_tar_config, NULL);
767cf457
JK
363 for (i = 0; i < nr_tar_filters; i++) {
364 /* omit any filters that never had a command configured */
365 if (tar_filters[i]->data)
366 register_archiver(tar_filters[i]);
367 }
13e0f88d 368}