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