archive: delegate blob reading to backend
[git/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
d240d410
NTND
126static void prepare_header(struct archiver_args *args,
127 struct ustar_header *header,
128 unsigned int mode, unsigned long size)
129{
130 sprintf(header->mode, "%07o", mode & 07777);
131 sprintf(header->size, "%011lo", S_ISREG(mode) ? size : 0);
132 sprintf(header->mtime, "%011lo", (unsigned long) args->time);
133
134 sprintf(header->uid, "%07o", 0);
135 sprintf(header->gid, "%07o", 0);
136 strlcpy(header->uname, "root", sizeof(header->uname));
137 strlcpy(header->gname, "root", sizeof(header->gname));
138 sprintf(header->devmajor, "%07o", 0);
139 sprintf(header->devminor, "%07o", 0);
140
141 memcpy(header->magic, "ustar", 6);
142 memcpy(header->version, "00", 2);
143
144 sprintf(header->chksum, "%07o", ustar_header_chksum(header));
145}
146
147static int write_extended_header(struct archiver_args *args,
148 const unsigned char *sha1,
149 const void *buffer, unsigned long size)
150{
151 struct ustar_header header;
152 unsigned int mode;
153 memset(&header, 0, sizeof(header));
154 *header.typeflag = TYPEFLAG_EXT_HEADER;
155 mode = 0100666;
156 sprintf(header.name, "%s.paxheader", sha1_to_hex(sha1));
157 prepare_header(args, &header, mode, size);
158 write_blocked(&header, sizeof(header));
159 write_blocked(buffer, size);
160 return 0;
161}
162
562e25ab 163static int write_tar_entry(struct archiver_args *args,
9cb513b7
NTND
164 const unsigned char *sha1,
165 const char *path, size_t pathlen,
166 unsigned int mode)
3d74982f
RS
167{
168 struct ustar_header header;
f285a2d7 169 struct strbuf ext_header = STRBUF_INIT;
9cb513b7
NTND
170 unsigned int old_mode = mode;
171 unsigned long size;
172 void *buffer;
562e25ab 173 int err = 0;
3d74982f
RS
174
175 memset(&header, 0, sizeof(header));
3d74982f 176
85390709
NTND
177 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
178 *header.typeflag = TYPEFLAG_DIR;
179 mode = (mode | 0777) & ~tar_umask;
180 } else if (S_ISLNK(mode)) {
181 *header.typeflag = TYPEFLAG_LNK;
182 mode |= 0777;
183 } else if (S_ISREG(mode)) {
184 *header.typeflag = TYPEFLAG_REG;
185 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
3d74982f 186 } else {
85390709
NTND
187 return error("unsupported file mode: 0%o (SHA1: %s)",
188 mode, sha1_to_hex(sha1));
189 }
190 if (pathlen > sizeof(header.name)) {
191 size_t plen = get_path_prefix(path, pathlen,
192 sizeof(header.prefix));
193 size_t rest = pathlen - plen - 1;
194 if (plen > 0 && rest <= sizeof(header.name)) {
195 memcpy(header.prefix, path, plen);
196 memcpy(header.name, path + plen + 1, rest);
3d74982f 197 } else {
85390709
NTND
198 sprintf(header.name, "%s.data",
199 sha1_to_hex(sha1));
200 strbuf_append_ext_header(&ext_header, "path",
201 path, pathlen);
3d74982f 202 }
85390709
NTND
203 } else
204 memcpy(header.name, path, pathlen);
3d74982f 205
9cb513b7
NTND
206 if (S_ISLNK(mode) || S_ISREG(mode)) {
207 enum object_type type;
208 buffer = sha1_file_to_archive(args, path, sha1, old_mode, &type, &size);
209 if (!buffer)
210 return error("cannot read %s", sha1_to_hex(sha1));
211 } else {
212 buffer = NULL;
213 size = 0;
214 }
215
216 if (S_ISLNK(mode)) {
3d74982f
RS
217 if (size > sizeof(header.linkname)) {
218 sprintf(header.linkname, "see %s.paxheader",
219 sha1_to_hex(sha1));
220 strbuf_append_ext_header(&ext_header, "linkpath",
221 buffer, size);
222 } else
223 memcpy(header.linkname, buffer, size);
224 }
225
d240d410 226 prepare_header(args, &header, mode, size);
3d74982f
RS
227
228 if (ext_header.len > 0) {
d240d410
NTND
229 err = write_extended_header(args, sha1, ext_header.buf,
230 ext_header.len);
9cb513b7
NTND
231 if (err) {
232 free(buffer);
562e25ab 233 return err;
9cb513b7 234 }
3d74982f 235 }
7a604f16 236 strbuf_release(&ext_header);
3d74982f
RS
237 write_blocked(&header, sizeof(header));
238 if (S_ISREG(mode) && buffer && size > 0)
239 write_blocked(buffer, size);
9cb513b7 240 free(buffer);
562e25ab 241 return err;
3d74982f
RS
242}
243
562e25ab 244static int write_global_extended_header(struct archiver_args *args)
3d74982f 245{
562e25ab 246 const unsigned char *sha1 = args->commit_sha1;
f285a2d7 247 struct strbuf ext_header = STRBUF_INIT;
d240d410
NTND
248 struct ustar_header header;
249 unsigned int mode;
250 int err = 0;
7a604f16 251
3d74982f 252 strbuf_append_ext_header(&ext_header, "comment", sha1_to_hex(sha1), 40);
d240d410
NTND
253 memset(&header, 0, sizeof(header));
254 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
255 mode = 0100666;
256 strcpy(header.name, "pax_global_header");
257 prepare_header(args, &header, mode, ext_header.len);
258 write_blocked(&header, sizeof(header));
259 write_blocked(ext_header.buf, ext_header.len);
7a604f16 260 strbuf_release(&ext_header);
562e25ab 261 return err;
3d74982f
RS
262}
263
767cf457
JK
264static struct archiver **tar_filters;
265static int nr_tar_filters;
266static int alloc_tar_filters;
267
268static struct archiver *find_tar_filter(const char *name, int len)
269{
270 int i;
271 for (i = 0; i < nr_tar_filters; i++) {
272 struct archiver *ar = tar_filters[i];
273 if (!strncmp(ar->name, name, len) && !ar->name[len])
274 return ar;
275 }
276 return NULL;
277}
278
279static int tar_filter_config(const char *var, const char *value, void *data)
280{
281 struct archiver *ar;
282 const char *dot;
283 const char *name;
284 const char *type;
285 int namelen;
286
287 if (prefixcmp(var, "tar."))
288 return 0;
289 dot = strrchr(var, '.');
290 if (dot == var + 9)
291 return 0;
292
293 name = var + 4;
294 namelen = dot - name;
295 type = dot + 1;
296
297 ar = find_tar_filter(name, namelen);
298 if (!ar) {
299 ar = xcalloc(1, sizeof(*ar));
300 ar->name = xmemdupz(name, namelen);
301 ar->write_archive = write_tar_filter_archive;
302 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS;
303 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
304 tar_filters[nr_tar_filters++] = ar;
305 }
306
307 if (!strcmp(type, "command")) {
308 if (!value)
309 return config_error_nonbool(var);
310 free(ar->data);
311 ar->data = xstrdup(value);
312 return 0;
313 }
7b97730b
JK
314 if (!strcmp(type, "remote")) {
315 if (git_config_bool(var, value))
316 ar->flags |= ARCHIVER_REMOTE;
317 else
318 ar->flags &= ~ARCHIVER_REMOTE;
319 return 0;
320 }
767cf457
JK
321
322 return 0;
323}
324
ef90d6d4 325static int git_tar_config(const char *var, const char *value, void *cb)
3d74982f
RS
326{
327 if (!strcmp(var, "tar.umask")) {
cc1816b0 328 if (value && !strcmp(value, "user")) {
3d74982f
RS
329 tar_umask = umask(0);
330 umask(tar_umask);
331 } else {
332 tar_umask = git_config_int(var, value);
333 }
334 return 0;
335 }
767cf457
JK
336
337 return tar_filter_config(var, value, cb);
3d74982f
RS
338}
339
4d7c9898
JK
340static int write_tar_archive(const struct archiver *ar,
341 struct archiver_args *args)
3d74982f 342{
562e25ab 343 int err = 0;
3d74982f 344
3d74982f 345 if (args->commit_sha1)
562e25ab
RS
346 err = write_global_extended_header(args);
347 if (!err)
348 err = write_archive_entries(args, write_tar_entry);
349 if (!err)
350 write_trailer();
351 return err;
3d74982f 352}
13e0f88d 353
767cf457
JK
354static int write_tar_filter_archive(const struct archiver *ar,
355 struct archiver_args *args)
356{
357 struct strbuf cmd = STRBUF_INIT;
358 struct child_process filter;
359 const char *argv[2];
360 int r;
361
362 if (!ar->data)
363 die("BUG: tar-filter archiver called with no filter defined");
364
365 strbuf_addstr(&cmd, ar->data);
366 if (args->compression_level >= 0)
367 strbuf_addf(&cmd, " -%d", args->compression_level);
368
369 memset(&filter, 0, sizeof(filter));
370 argv[0] = cmd.buf;
371 argv[1] = NULL;
372 filter.argv = argv;
373 filter.use_shell = 1;
374 filter.in = -1;
375
376 if (start_command(&filter) < 0)
377 die_errno("unable to start '%s' filter", argv[0]);
378 close(1);
379 if (dup2(filter.in, 1) < 0)
380 die_errno("unable to redirect descriptor");
381 close(filter.in);
382
383 r = write_tar_archive(ar, args);
384
385 close(1);
386 if (finish_command(&filter) != 0)
387 die("'%s' filter reported error", argv[0]);
388
389 strbuf_release(&cmd);
390 return r;
391}
392
13e0f88d
JK
393static struct archiver tar_archiver = {
394 "tar",
395 write_tar_archive,
7b97730b 396 ARCHIVER_REMOTE
13e0f88d
JK
397};
398
399void init_tar_archiver(void)
400{
767cf457 401 int i;
13e0f88d 402 register_archiver(&tar_archiver);
767cf457 403
0e804e09 404 tar_filter_config("tar.tgz.command", "gzip -cn", NULL);
7b97730b 405 tar_filter_config("tar.tgz.remote", "true", NULL);
0e804e09 406 tar_filter_config("tar.tar.gz.command", "gzip -cn", NULL);
7b97730b 407 tar_filter_config("tar.tar.gz.remote", "true", NULL);
13e0f88d 408 git_config(git_tar_config, NULL);
767cf457
JK
409 for (i = 0; i < nr_tar_filters; i++) {
410 /* omit any filters that never had a command configured */
411 if (tar_filters[i]->data)
412 register_archiver(tar_filters[i]);
413 }
13e0f88d 414}