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