]> git.ipfire.org Git - thirdparty/git.git/blob - archive-tar.c
win32: special-case `ENOSPC` when writing to a pipe
[thirdparty/git.git] / archive-tar.c
1 /*
2 * Copyright (c) 2005, 2006 Rene Scharfe
3 */
4 #include "git-compat-util.h"
5 #include "config.h"
6 #include "gettext.h"
7 #include "git-zlib.h"
8 #include "hex.h"
9 #include "tar.h"
10 #include "archive.h"
11 #include "object-store-ll.h"
12 #include "streaming.h"
13 #include "run-command.h"
14 #include "write-or-die.h"
15
16 #define RECORDSIZE (512)
17 #define BLOCKSIZE (RECORDSIZE * 20)
18
19 static char block[BLOCKSIZE];
20 static unsigned long offset;
21
22 static int tar_umask = 002;
23
24 static int write_tar_filter_archive(const struct archiver *ar,
25 struct archiver_args *args);
26
27 /*
28 * This is the max value that a ustar size header can specify, as it is fixed
29 * at 11 octal digits. POSIX specifies that we switch to extended headers at
30 * this size.
31 *
32 * Likewise for the mtime (which happens to use a buffer of the same size).
33 */
34 #if ULONG_MAX == 0xFFFFFFFF
35 #define USTAR_MAX_SIZE ULONG_MAX
36 #else
37 #define USTAR_MAX_SIZE 077777777777UL
38 #endif
39 #if TIME_MAX == 0xFFFFFFFF
40 #define USTAR_MAX_MTIME TIME_MAX
41 #else
42 #define USTAR_MAX_MTIME 077777777777ULL
43 #endif
44
45 static void tar_write_block(const void *buf)
46 {
47 write_or_die(1, buf, BLOCKSIZE);
48 }
49
50 static void (*write_block)(const void *) = tar_write_block;
51
52 /* writes out the whole block, but only if it is full */
53 static void write_if_needed(void)
54 {
55 if (offset == BLOCKSIZE) {
56 write_block(block);
57 offset = 0;
58 }
59 }
60
61 /*
62 * queues up writes, so that all our write(2) calls write exactly one
63 * full block; pads writes to RECORDSIZE
64 */
65 static void do_write_blocked(const void *data, unsigned long size)
66 {
67 const char *buf = data;
68
69 if (offset) {
70 unsigned long chunk = BLOCKSIZE - offset;
71 if (size < chunk)
72 chunk = size;
73 memcpy(block + offset, buf, chunk);
74 size -= chunk;
75 offset += chunk;
76 buf += chunk;
77 write_if_needed();
78 }
79 while (size >= BLOCKSIZE) {
80 write_block(buf);
81 size -= BLOCKSIZE;
82 buf += BLOCKSIZE;
83 }
84 if (size) {
85 memcpy(block + offset, buf, size);
86 offset += size;
87 }
88 }
89
90 static void finish_record(void)
91 {
92 unsigned long tail;
93 tail = offset % RECORDSIZE;
94 if (tail) {
95 memset(block + offset, 0, RECORDSIZE - tail);
96 offset += RECORDSIZE - tail;
97 }
98 write_if_needed();
99 }
100
101 static void write_blocked(const void *data, unsigned long size)
102 {
103 do_write_blocked(data, size);
104 finish_record();
105 }
106
107 /*
108 * The end of tar archives is marked by 2*512 nul bytes and after that
109 * follows the rest of the block (if any).
110 */
111 static void write_trailer(void)
112 {
113 int tail = BLOCKSIZE - offset;
114 memset(block + offset, 0, tail);
115 write_block(block);
116 if (tail < 2 * RECORDSIZE) {
117 memset(block, 0, offset);
118 write_block(block);
119 }
120 }
121
122 /*
123 * queues up writes, so that all our write(2) calls write exactly one
124 * full block; pads writes to RECORDSIZE
125 */
126 static int stream_blocked(struct repository *r, const struct object_id *oid)
127 {
128 struct git_istream *st;
129 enum object_type type;
130 unsigned long sz;
131 char buf[BLOCKSIZE];
132 ssize_t readlen;
133
134 st = open_istream(r, oid, &type, &sz, NULL);
135 if (!st)
136 return error(_("cannot stream blob %s"), oid_to_hex(oid));
137 for (;;) {
138 readlen = read_istream(st, buf, sizeof(buf));
139 if (readlen <= 0)
140 break;
141 do_write_blocked(buf, readlen);
142 }
143 close_istream(st);
144 if (!readlen)
145 finish_record();
146 return readlen;
147 }
148
149 /*
150 * pax extended header records have the format "%u %s=%s\n". %u contains
151 * the size of the whole string (including the %u), the first %s is the
152 * keyword, the second one is the value. This function constructs such a
153 * string and appends it to a struct strbuf.
154 */
155 static void strbuf_append_ext_header(struct strbuf *sb, const char *keyword,
156 const char *value, size_t valuelen)
157 {
158 size_t orig_len = sb->len;
159 size_t len, tmp;
160
161 /* "%u %s=%s\n" */
162 len = 1 + 1 + strlen(keyword) + 1 + valuelen + 1;
163 for (tmp = 1; len / 10 >= tmp; tmp *= 10)
164 len++;
165
166 strbuf_grow(sb, len);
167 strbuf_addf(sb, "%"PRIuMAX" %s=", (uintmax_t)len, keyword);
168 strbuf_add(sb, value, valuelen);
169 strbuf_addch(sb, '\n');
170
171 if (len != sb->len - orig_len)
172 BUG("pax extended header length miscalculated as %"PRIuMAX
173 ", should be %"PRIuMAX,
174 (uintmax_t)len, (uintmax_t)(sb->len - orig_len));
175 }
176
177 /*
178 * Like strbuf_append_ext_header, but for numeric values.
179 */
180 static void strbuf_append_ext_header_uint(struct strbuf *sb,
181 const char *keyword,
182 uintmax_t value)
183 {
184 char buf[40]; /* big enough for 2^128 in decimal, plus NUL */
185 int len;
186
187 len = xsnprintf(buf, sizeof(buf), "%"PRIuMAX, value);
188 strbuf_append_ext_header(sb, keyword, buf, len);
189 }
190
191 static unsigned int ustar_header_chksum(const struct ustar_header *header)
192 {
193 const unsigned char *p = (const unsigned char *)header;
194 unsigned int chksum = 0;
195 while (p < (const unsigned char *)header->chksum)
196 chksum += *p++;
197 chksum += sizeof(header->chksum) * ' ';
198 p += sizeof(header->chksum);
199 while (p < (const unsigned char *)header + sizeof(struct ustar_header))
200 chksum += *p++;
201 return chksum;
202 }
203
204 static size_t get_path_prefix(const char *path, size_t pathlen, size_t maxlen)
205 {
206 size_t i = pathlen;
207 if (i > 1 && path[i - 1] == '/')
208 i--;
209 if (i > maxlen)
210 i = maxlen;
211 do {
212 i--;
213 } while (i > 0 && path[i] != '/');
214 return i;
215 }
216
217 static void prepare_header(struct archiver_args *args,
218 struct ustar_header *header,
219 unsigned int mode, unsigned long size)
220 {
221 xsnprintf(header->mode, sizeof(header->mode), "%07o", mode & 07777);
222 xsnprintf(header->size, sizeof(header->size), "%011"PRIoMAX , S_ISREG(mode) ? (uintmax_t)size : (uintmax_t)0);
223 xsnprintf(header->mtime, sizeof(header->mtime), "%011lo", (unsigned long) args->time);
224
225 xsnprintf(header->uid, sizeof(header->uid), "%07o", 0);
226 xsnprintf(header->gid, sizeof(header->gid), "%07o", 0);
227 strlcpy(header->uname, "root", sizeof(header->uname));
228 strlcpy(header->gname, "root", sizeof(header->gname));
229 xsnprintf(header->devmajor, sizeof(header->devmajor), "%07o", 0);
230 xsnprintf(header->devminor, sizeof(header->devminor), "%07o", 0);
231
232 memcpy(header->magic, "ustar", 6);
233 memcpy(header->version, "00", 2);
234
235 xsnprintf(header->chksum, sizeof(header->chksum), "%07o", ustar_header_chksum(header));
236 }
237
238 static void write_extended_header(struct archiver_args *args,
239 const struct object_id *oid,
240 const void *buffer, unsigned long size)
241 {
242 struct ustar_header header;
243 unsigned int mode;
244 memset(&header, 0, sizeof(header));
245 *header.typeflag = TYPEFLAG_EXT_HEADER;
246 mode = 0100666;
247 xsnprintf(header.name, sizeof(header.name), "%s.paxheader", oid_to_hex(oid));
248 prepare_header(args, &header, mode, size);
249 write_blocked(&header, sizeof(header));
250 write_blocked(buffer, size);
251 }
252
253 static int write_tar_entry(struct archiver_args *args,
254 const struct object_id *oid,
255 const char *path, size_t pathlen,
256 unsigned int mode,
257 void *buffer, unsigned long size)
258 {
259 struct ustar_header header;
260 struct strbuf ext_header = STRBUF_INIT;
261 unsigned long size_in_header;
262 int err = 0;
263
264 memset(&header, 0, sizeof(header));
265
266 if (S_ISDIR(mode) || S_ISGITLINK(mode)) {
267 *header.typeflag = TYPEFLAG_DIR;
268 mode = (mode | 0777) & ~tar_umask;
269 } else if (S_ISLNK(mode)) {
270 *header.typeflag = TYPEFLAG_LNK;
271 mode |= 0777;
272 } else if (S_ISREG(mode)) {
273 *header.typeflag = TYPEFLAG_REG;
274 mode = (mode | ((mode & 0100) ? 0777 : 0666)) & ~tar_umask;
275 } else {
276 return error(_("unsupported file mode: 0%o (SHA1: %s)"),
277 mode, oid_to_hex(oid));
278 }
279 if (pathlen > sizeof(header.name)) {
280 size_t plen = get_path_prefix(path, pathlen,
281 sizeof(header.prefix));
282 size_t rest = pathlen - plen - 1;
283 if (plen > 0 && rest <= sizeof(header.name)) {
284 memcpy(header.prefix, path, plen);
285 memcpy(header.name, path + plen + 1, rest);
286 } else {
287 xsnprintf(header.name, sizeof(header.name), "%s.data",
288 oid_to_hex(oid));
289 strbuf_append_ext_header(&ext_header, "path",
290 path, pathlen);
291 }
292 } else
293 memcpy(header.name, path, pathlen);
294
295 if (S_ISLNK(mode)) {
296 if (size > sizeof(header.linkname)) {
297 xsnprintf(header.linkname, sizeof(header.linkname),
298 "see %s.paxheader", oid_to_hex(oid));
299 strbuf_append_ext_header(&ext_header, "linkpath",
300 buffer, size);
301 } else
302 memcpy(header.linkname, buffer, size);
303 }
304
305 size_in_header = size;
306 if (S_ISREG(mode) && size > USTAR_MAX_SIZE) {
307 size_in_header = 0;
308 strbuf_append_ext_header_uint(&ext_header, "size", size);
309 }
310
311 prepare_header(args, &header, mode, size_in_header);
312
313 if (ext_header.len > 0) {
314 write_extended_header(args, oid, ext_header.buf,
315 ext_header.len);
316 }
317 strbuf_release(&ext_header);
318 write_blocked(&header, sizeof(header));
319 if (S_ISREG(mode) && size > 0) {
320 if (buffer)
321 write_blocked(buffer, size);
322 else
323 err = stream_blocked(args->repo, oid);
324 }
325 return err;
326 }
327
328 static void write_global_extended_header(struct archiver_args *args)
329 {
330 const struct object_id *oid = args->commit_oid;
331 struct strbuf ext_header = STRBUF_INIT;
332 struct ustar_header header;
333 unsigned int mode;
334
335 if (oid)
336 strbuf_append_ext_header(&ext_header, "comment",
337 oid_to_hex(oid),
338 the_hash_algo->hexsz);
339 if (args->time > USTAR_MAX_MTIME) {
340 strbuf_append_ext_header_uint(&ext_header, "mtime",
341 args->time);
342 args->time = USTAR_MAX_MTIME;
343 }
344
345 if (!ext_header.len)
346 return;
347
348 memset(&header, 0, sizeof(header));
349 *header.typeflag = TYPEFLAG_GLOBAL_HEADER;
350 mode = 0100666;
351 xsnprintf(header.name, sizeof(header.name), "pax_global_header");
352 prepare_header(args, &header, mode, ext_header.len);
353 write_blocked(&header, sizeof(header));
354 write_blocked(ext_header.buf, ext_header.len);
355 strbuf_release(&ext_header);
356 }
357
358 static struct archiver **tar_filters;
359 static int nr_tar_filters;
360 static int alloc_tar_filters;
361
362 static struct archiver *find_tar_filter(const char *name, size_t len)
363 {
364 int i;
365 for (i = 0; i < nr_tar_filters; i++) {
366 struct archiver *ar = tar_filters[i];
367 if (!strncmp(ar->name, name, len) && !ar->name[len])
368 return ar;
369 }
370 return NULL;
371 }
372
373 static int tar_filter_config(const char *var, const char *value,
374 void *data UNUSED)
375 {
376 struct archiver *ar;
377 const char *name;
378 const char *type;
379 size_t namelen;
380
381 if (parse_config_key(var, "tar", &name, &namelen, &type) < 0 || !name)
382 return 0;
383
384 ar = find_tar_filter(name, namelen);
385 if (!ar) {
386 CALLOC_ARRAY(ar, 1);
387 ar->name = xmemdupz(name, namelen);
388 ar->write_archive = write_tar_filter_archive;
389 ar->flags = ARCHIVER_WANT_COMPRESSION_LEVELS |
390 ARCHIVER_HIGH_COMPRESSION_LEVELS;
391 ALLOC_GROW(tar_filters, nr_tar_filters + 1, alloc_tar_filters);
392 tar_filters[nr_tar_filters++] = ar;
393 }
394
395 if (!strcmp(type, "command")) {
396 if (!value)
397 return config_error_nonbool(var);
398 free(ar->filter_command);
399 ar->filter_command = xstrdup(value);
400 return 0;
401 }
402 if (!strcmp(type, "remote")) {
403 if (git_config_bool(var, value))
404 ar->flags |= ARCHIVER_REMOTE;
405 else
406 ar->flags &= ~ARCHIVER_REMOTE;
407 return 0;
408 }
409
410 return 0;
411 }
412
413 static int git_tar_config(const char *var, const char *value,
414 const struct config_context *ctx, void *cb)
415 {
416 if (!strcmp(var, "tar.umask")) {
417 if (value && !strcmp(value, "user")) {
418 tar_umask = umask(0);
419 umask(tar_umask);
420 } else {
421 tar_umask = git_config_int(var, value, ctx->kvi);
422 }
423 return 0;
424 }
425
426 return tar_filter_config(var, value, cb);
427 }
428
429 static int write_tar_archive(const struct archiver *ar UNUSED,
430 struct archiver_args *args)
431 {
432 int err = 0;
433
434 write_global_extended_header(args);
435 err = write_archive_entries(args, write_tar_entry);
436 if (!err)
437 write_trailer();
438 return err;
439 }
440
441 static git_zstream gzstream;
442 static unsigned char outbuf[16384];
443
444 static void tgz_deflate(int flush)
445 {
446 while (gzstream.avail_in || flush == Z_FINISH) {
447 int status = git_deflate(&gzstream, flush);
448 if (!gzstream.avail_out || status == Z_STREAM_END) {
449 write_or_die(1, outbuf, gzstream.next_out - outbuf);
450 gzstream.next_out = outbuf;
451 gzstream.avail_out = sizeof(outbuf);
452 if (status == Z_STREAM_END)
453 break;
454 }
455 if (status != Z_OK && status != Z_BUF_ERROR)
456 die(_("deflate error (%d)"), status);
457 }
458 }
459
460 static void tgz_write_block(const void *data)
461 {
462 gzstream.next_in = (void *)data;
463 gzstream.avail_in = BLOCKSIZE;
464 tgz_deflate(Z_NO_FLUSH);
465 }
466
467 static const char internal_gzip_command[] = "git archive gzip";
468
469 static int write_tar_filter_archive(const struct archiver *ar,
470 struct archiver_args *args)
471 {
472 #if ZLIB_VERNUM >= 0x1221
473 struct gz_header_s gzhead = { .os = 3 }; /* Unix, for reproducibility */
474 #endif
475 struct strbuf cmd = STRBUF_INIT;
476 struct child_process filter = CHILD_PROCESS_INIT;
477 int r;
478
479 if (!ar->filter_command)
480 BUG("tar-filter archiver called with no filter defined");
481
482 if (!strcmp(ar->filter_command, internal_gzip_command)) {
483 write_block = tgz_write_block;
484 git_deflate_init_gzip(&gzstream, args->compression_level);
485 #if ZLIB_VERNUM >= 0x1221
486 if (deflateSetHeader(&gzstream.z, &gzhead) != Z_OK)
487 BUG("deflateSetHeader() called too late");
488 #endif
489 gzstream.next_out = outbuf;
490 gzstream.avail_out = sizeof(outbuf);
491
492 r = write_tar_archive(ar, args);
493
494 tgz_deflate(Z_FINISH);
495 git_deflate_end(&gzstream);
496 return r;
497 }
498
499 strbuf_addstr(&cmd, ar->filter_command);
500 if (args->compression_level >= 0)
501 strbuf_addf(&cmd, " -%d", args->compression_level);
502
503 strvec_push(&filter.args, cmd.buf);
504 filter.use_shell = 1;
505 filter.in = -1;
506 filter.silent_exec_failure = 1;
507
508 if (start_command(&filter) < 0)
509 die_errno(_("unable to start '%s' filter"), cmd.buf);
510 close(1);
511 if (dup2(filter.in, 1) < 0)
512 die_errno(_("unable to redirect descriptor"));
513 close(filter.in);
514
515 r = write_tar_archive(ar, args);
516
517 close(1);
518 if (finish_command(&filter) != 0)
519 die(_("'%s' filter reported error"), cmd.buf);
520
521 strbuf_release(&cmd);
522 return r;
523 }
524
525 static struct archiver tar_archiver = {
526 .name = "tar",
527 .write_archive = write_tar_archive,
528 .flags = ARCHIVER_REMOTE,
529 };
530
531 void init_tar_archiver(void)
532 {
533 int i;
534 register_archiver(&tar_archiver);
535
536 tar_filter_config("tar.tgz.command", internal_gzip_command, NULL);
537 tar_filter_config("tar.tgz.remote", "true", NULL);
538 tar_filter_config("tar.tar.gz.command", internal_gzip_command, NULL);
539 tar_filter_config("tar.tar.gz.remote", "true", NULL);
540 git_config(git_tar_config, NULL);
541 for (i = 0; i < nr_tar_filters; i++) {
542 /* omit any filters that never had a command configured */
543 if (tar_filters[i]->filter_command)
544 register_archiver(tar_filters[i]);
545 }
546 }