Git fork
at reftables-rust 614 lines 17 kB view raw
1#include "git-compat-util.h" 2#include "environment.h" 3#include "gettext.h" 4#include "hex.h" 5#include "pack.h" 6#include "csum-file.h" 7#include "remote.h" 8#include "chunk-format.h" 9#include "object-file.h" 10#include "pack-mtimes.h" 11#include "pack-objects.h" 12#include "pack-revindex.h" 13#include "path.h" 14#include "repository.h" 15#include "strbuf.h" 16 17void reset_pack_idx_option(struct pack_idx_option *opts) 18{ 19 memset(opts, 0, sizeof(*opts)); 20 opts->version = 2; 21 opts->off32_limit = 0x7fffffff; 22 opts->delta_base_cache_limit = DEFAULT_DELTA_BASE_CACHE_LIMIT; 23} 24 25static int sha1_compare(const void *_a, const void *_b) 26{ 27 struct pack_idx_entry *a = *(struct pack_idx_entry **)_a; 28 struct pack_idx_entry *b = *(struct pack_idx_entry **)_b; 29 return oidcmp(&a->oid, &b->oid); 30} 31 32static int cmp_uint32(const void *a_, const void *b_) 33{ 34 uint32_t a = *((uint32_t *)a_); 35 uint32_t b = *((uint32_t *)b_); 36 37 return (a < b) ? -1 : (a != b); 38} 39 40static int need_large_offset(off_t offset, const struct pack_idx_option *opts) 41{ 42 uint32_t ofsval; 43 44 if ((offset >> 31) || (opts->off32_limit < offset)) 45 return 1; 46 if (!opts->anomaly_nr) 47 return 0; 48 ofsval = offset; 49 return !!bsearch(&ofsval, opts->anomaly, opts->anomaly_nr, 50 sizeof(ofsval), cmp_uint32); 51} 52 53/* 54 * The *sha1 contains the pack content SHA1 hash. 55 * The objects array passed in will be sorted by SHA1 on exit. 56 */ 57const char *write_idx_file(struct repository *repo, 58 const char *index_name, struct pack_idx_entry **objects, 59 int nr_objects, const struct pack_idx_option *opts, 60 const unsigned char *sha1) 61{ 62 struct hashfile *f; 63 struct pack_idx_entry **sorted_by_sha, **list, **last; 64 off_t last_obj_offset = 0; 65 int i, fd; 66 uint32_t index_version; 67 68 if (nr_objects) { 69 sorted_by_sha = objects; 70 list = sorted_by_sha; 71 last = sorted_by_sha + nr_objects; 72 for (i = 0; i < nr_objects; ++i) { 73 if (objects[i]->offset > last_obj_offset) 74 last_obj_offset = objects[i]->offset; 75 } 76 QSORT(sorted_by_sha, nr_objects, sha1_compare); 77 } 78 else 79 sorted_by_sha = list = last = NULL; 80 81 if (opts->flags & WRITE_IDX_VERIFY) { 82 assert(index_name); 83 f = hashfd_check(repo->hash_algo, index_name); 84 } else { 85 if (!index_name) { 86 struct strbuf tmp_file = STRBUF_INIT; 87 fd = odb_mkstemp(repo->objects, &tmp_file, 88 "pack/tmp_idx_XXXXXX"); 89 index_name = strbuf_detach(&tmp_file, NULL); 90 } else { 91 unlink(index_name); 92 fd = xopen(index_name, O_CREAT|O_EXCL|O_WRONLY, 0600); 93 } 94 f = hashfd(repo->hash_algo, fd, index_name); 95 } 96 97 /* if last object's offset is >= 2^31 we should use index V2 */ 98 index_version = need_large_offset(last_obj_offset, opts) ? 2 : opts->version; 99 100 /* index versions 2 and above need a header */ 101 if (index_version >= 2) { 102 struct pack_idx_header hdr; 103 hdr.idx_signature = htonl(PACK_IDX_SIGNATURE); 104 hdr.idx_version = htonl(index_version); 105 hashwrite(f, &hdr, sizeof(hdr)); 106 } 107 108 /* 109 * Write the first-level table (the list is sorted, 110 * but we use a 256-entry lookup to be able to avoid 111 * having to do eight extra binary search iterations). 112 */ 113 for (i = 0; i < 256; i++) { 114 struct pack_idx_entry **next = list; 115 while (next < last) { 116 struct pack_idx_entry *obj = *next; 117 if (obj->oid.hash[0] != i) 118 break; 119 next++; 120 } 121 hashwrite_be32(f, next - sorted_by_sha); 122 list = next; 123 } 124 125 /* 126 * Write the actual SHA1 entries.. 127 */ 128 list = sorted_by_sha; 129 for (i = 0; i < nr_objects; i++) { 130 struct pack_idx_entry *obj = *list++; 131 if (index_version < 2) 132 hashwrite_be32(f, obj->offset); 133 hashwrite(f, obj->oid.hash, repo->hash_algo->rawsz); 134 if ((opts->flags & WRITE_IDX_STRICT) && 135 (i && oideq(&list[-2]->oid, &obj->oid))) 136 die("The same object %s appears twice in the pack", 137 oid_to_hex(&obj->oid)); 138 } 139 140 if (index_version >= 2) { 141 unsigned int nr_large_offset = 0; 142 143 /* write the crc32 table */ 144 list = sorted_by_sha; 145 for (i = 0; i < nr_objects; i++) { 146 struct pack_idx_entry *obj = *list++; 147 hashwrite_be32(f, obj->crc32); 148 } 149 150 /* write the 32-bit offset table */ 151 list = sorted_by_sha; 152 for (i = 0; i < nr_objects; i++) { 153 struct pack_idx_entry *obj = *list++; 154 uint32_t offset; 155 156 offset = (need_large_offset(obj->offset, opts) 157 ? (0x80000000 | nr_large_offset++) 158 : obj->offset); 159 hashwrite_be32(f, offset); 160 } 161 162 /* write the large offset table */ 163 list = sorted_by_sha; 164 while (nr_large_offset) { 165 struct pack_idx_entry *obj = *list++; 166 uint64_t offset = obj->offset; 167 168 if (!need_large_offset(offset, opts)) 169 continue; 170 hashwrite_be64(f, offset); 171 nr_large_offset--; 172 } 173 } 174 175 hashwrite(f, sha1, repo->hash_algo->rawsz); 176 finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA, 177 CSUM_HASH_IN_STREAM | CSUM_CLOSE | 178 ((opts->flags & WRITE_IDX_VERIFY) ? 0 : CSUM_FSYNC)); 179 return index_name; 180} 181 182static int pack_order_cmp(const void *va, const void *vb, void *ctx) 183{ 184 struct pack_idx_entry **objects = ctx; 185 186 off_t oa = objects[*(uint32_t*)va]->offset; 187 off_t ob = objects[*(uint32_t*)vb]->offset; 188 189 if (oa < ob) 190 return -1; 191 if (oa > ob) 192 return 1; 193 return 0; 194} 195 196static void write_rev_header(const struct git_hash_algo *hash_algo, 197 struct hashfile *f) 198{ 199 hashwrite_be32(f, RIDX_SIGNATURE); 200 hashwrite_be32(f, RIDX_VERSION); 201 hashwrite_be32(f, oid_version(hash_algo)); 202} 203 204static void write_rev_index_positions(struct hashfile *f, 205 uint32_t *pack_order, 206 uint32_t nr_objects) 207{ 208 uint32_t i; 209 for (i = 0; i < nr_objects; i++) 210 hashwrite_be32(f, pack_order[i]); 211} 212 213static void write_rev_trailer(const struct git_hash_algo *hash_algo, 214 struct hashfile *f, const unsigned char *hash) 215{ 216 hashwrite(f, hash, hash_algo->rawsz); 217} 218 219char *write_rev_file(struct repository *repo, 220 const char *rev_name, 221 struct pack_idx_entry **objects, 222 uint32_t nr_objects, 223 const unsigned char *hash, 224 unsigned flags) 225{ 226 uint32_t *pack_order; 227 uint32_t i; 228 char *ret; 229 230 if (!(flags & WRITE_REV) && !(flags & WRITE_REV_VERIFY)) 231 return NULL; 232 233 ALLOC_ARRAY(pack_order, nr_objects); 234 for (i = 0; i < nr_objects; i++) 235 pack_order[i] = i; 236 QSORT_S(pack_order, nr_objects, pack_order_cmp, objects); 237 238 ret = write_rev_file_order(repo, rev_name, pack_order, nr_objects, 239 hash, flags); 240 241 free(pack_order); 242 243 return ret; 244} 245 246char *write_rev_file_order(struct repository *repo, 247 const char *rev_name, 248 uint32_t *pack_order, 249 uint32_t nr_objects, 250 const unsigned char *hash, 251 unsigned flags) 252{ 253 struct hashfile *f; 254 char *path; 255 int fd; 256 257 if ((flags & WRITE_REV) && (flags & WRITE_REV_VERIFY)) 258 die(_("cannot both write and verify reverse index")); 259 260 if (flags & WRITE_REV) { 261 if (!rev_name) { 262 struct strbuf tmp_file = STRBUF_INIT; 263 fd = odb_mkstemp(repo->objects, &tmp_file, 264 "pack/tmp_rev_XXXXXX"); 265 path = strbuf_detach(&tmp_file, NULL); 266 } else { 267 unlink(rev_name); 268 fd = xopen(rev_name, O_CREAT|O_EXCL|O_WRONLY, 0600); 269 path = xstrdup(rev_name); 270 } 271 f = hashfd(repo->hash_algo, fd, path); 272 } else if (flags & WRITE_REV_VERIFY) { 273 struct stat statbuf; 274 if (stat(rev_name, &statbuf)) { 275 if (errno == ENOENT) { 276 /* .rev files are optional */ 277 return NULL; 278 } else 279 die_errno(_("could not stat: %s"), rev_name); 280 } 281 f = hashfd_check(repo->hash_algo, rev_name); 282 path = xstrdup(rev_name); 283 } else { 284 return NULL; 285 } 286 287 write_rev_header(repo->hash_algo, f); 288 289 write_rev_index_positions(f, pack_order, nr_objects); 290 write_rev_trailer(repo->hash_algo, f, hash); 291 292 if (adjust_shared_perm(repo, path) < 0) 293 die(_("failed to make %s readable"), path); 294 295 finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA, 296 CSUM_HASH_IN_STREAM | CSUM_CLOSE | 297 ((flags & WRITE_IDX_VERIFY) ? 0 : CSUM_FSYNC)); 298 299 return path; 300} 301 302static void write_mtimes_header(const struct git_hash_algo *hash_algo, 303 struct hashfile *f) 304{ 305 hashwrite_be32(f, MTIMES_SIGNATURE); 306 hashwrite_be32(f, MTIMES_VERSION); 307 hashwrite_be32(f, oid_version(hash_algo)); 308} 309 310/* 311 * Writes the object mtimes of "objects" for use in a .mtimes file. 312 * Note that objects must be in lexicographic (index) order, which is 313 * the expected ordering of these values in the .mtimes file. 314 */ 315static void write_mtimes_objects(struct hashfile *f, 316 struct packing_data *to_pack, 317 struct pack_idx_entry **objects, 318 uint32_t nr_objects) 319{ 320 uint32_t i; 321 for (i = 0; i < nr_objects; i++) { 322 struct object_entry *e = (struct object_entry*)objects[i]; 323 hashwrite_be32(f, oe_cruft_mtime(to_pack, e)); 324 } 325} 326 327static void write_mtimes_trailer(const struct git_hash_algo *hash_algo, 328 struct hashfile *f, const unsigned char *hash) 329{ 330 hashwrite(f, hash, hash_algo->rawsz); 331} 332 333static char *write_mtimes_file(struct repository *repo, 334 struct packing_data *to_pack, 335 struct pack_idx_entry **objects, 336 uint32_t nr_objects, 337 const unsigned char *hash) 338{ 339 struct strbuf tmp_file = STRBUF_INIT; 340 char *mtimes_name; 341 struct hashfile *f; 342 int fd; 343 344 if (!to_pack) 345 BUG("cannot call write_mtimes_file with NULL packing_data"); 346 347 fd = odb_mkstemp(repo->objects, &tmp_file, "pack/tmp_mtimes_XXXXXX"); 348 mtimes_name = strbuf_detach(&tmp_file, NULL); 349 f = hashfd(repo->hash_algo, fd, mtimes_name); 350 351 write_mtimes_header(repo->hash_algo, f); 352 write_mtimes_objects(f, to_pack, objects, nr_objects); 353 write_mtimes_trailer(repo->hash_algo, f, hash); 354 355 if (adjust_shared_perm(repo, mtimes_name) < 0) 356 die(_("failed to make %s readable"), mtimes_name); 357 358 finalize_hashfile(f, NULL, FSYNC_COMPONENT_PACK_METADATA, 359 CSUM_HASH_IN_STREAM | CSUM_CLOSE | CSUM_FSYNC); 360 361 return mtimes_name; 362} 363 364off_t write_pack_header(struct hashfile *f, uint32_t nr_entries) 365{ 366 struct pack_header hdr; 367 368 hdr.hdr_signature = htonl(PACK_SIGNATURE); 369 hdr.hdr_version = htonl(PACK_VERSION); 370 hdr.hdr_entries = htonl(nr_entries); 371 hashwrite(f, &hdr, sizeof(hdr)); 372 return sizeof(hdr); 373} 374 375/* 376 * Update pack header with object_count and compute new SHA1 for pack data 377 * associated to pack_fd, and write that SHA1 at the end. That new SHA1 378 * is also returned in new_pack_sha1. 379 * 380 * If partial_pack_sha1 is non null, then the SHA1 of the existing pack 381 * (without the header update) is computed and validated against the 382 * one provided in partial_pack_sha1. The validation is performed at 383 * partial_pack_offset bytes in the pack file. The SHA1 of the remaining 384 * data (i.e. from partial_pack_offset to the end) is then computed and 385 * returned in partial_pack_sha1. 386 * 387 * Note that new_pack_sha1 is updated last, so both new_pack_sha1 and 388 * partial_pack_sha1 can refer to the same buffer if the caller is not 389 * interested in the resulting SHA1 of pack data above partial_pack_offset. 390 */ 391void fixup_pack_header_footer(const struct git_hash_algo *hash_algo, 392 int pack_fd, 393 unsigned char *new_pack_hash, 394 const char *pack_name, 395 uint32_t object_count, 396 unsigned char *partial_pack_hash, 397 off_t partial_pack_offset) 398{ 399 int aligned_sz, buf_sz = 8 * 1024; 400 struct git_hash_ctx old_hash_ctx, new_hash_ctx; 401 struct pack_header hdr; 402 char *buf; 403 ssize_t read_result; 404 405 hash_algo->init_fn(&old_hash_ctx); 406 hash_algo->init_fn(&new_hash_ctx); 407 408 if (lseek(pack_fd, 0, SEEK_SET) != 0) 409 die_errno("Failed seeking to start of '%s'", pack_name); 410 read_result = read_in_full(pack_fd, &hdr, sizeof(hdr)); 411 if (read_result < 0) 412 die_errno("Unable to reread header of '%s'", pack_name); 413 else if (read_result != sizeof(hdr)) 414 die_errno("Unexpected short read for header of '%s'", 415 pack_name); 416 if (lseek(pack_fd, 0, SEEK_SET) != 0) 417 die_errno("Failed seeking to start of '%s'", pack_name); 418 git_hash_update(&old_hash_ctx, &hdr, sizeof(hdr)); 419 hdr.hdr_entries = htonl(object_count); 420 git_hash_update(&new_hash_ctx, &hdr, sizeof(hdr)); 421 write_or_die(pack_fd, &hdr, sizeof(hdr)); 422 partial_pack_offset -= sizeof(hdr); 423 424 buf = xmalloc(buf_sz); 425 aligned_sz = buf_sz - sizeof(hdr); 426 for (;;) { 427 ssize_t m, n; 428 m = (partial_pack_hash && partial_pack_offset < aligned_sz) ? 429 partial_pack_offset : aligned_sz; 430 n = xread(pack_fd, buf, m); 431 if (!n) 432 break; 433 if (n < 0) 434 die_errno("Failed to checksum '%s'", pack_name); 435 git_hash_update(&new_hash_ctx, buf, n); 436 437 aligned_sz -= n; 438 if (!aligned_sz) 439 aligned_sz = buf_sz; 440 441 if (!partial_pack_hash) 442 continue; 443 444 git_hash_update(&old_hash_ctx, buf, n); 445 partial_pack_offset -= n; 446 if (partial_pack_offset == 0) { 447 unsigned char hash[GIT_MAX_RAWSZ]; 448 git_hash_final(hash, &old_hash_ctx); 449 if (!hasheq(hash, partial_pack_hash, 450 hash_algo)) 451 die("Unexpected checksum for %s " 452 "(disk corruption?)", pack_name); 453 /* 454 * Now let's compute the SHA1 of the remainder of the 455 * pack, which also means making partial_pack_offset 456 * big enough not to matter anymore. 457 */ 458 hash_algo->init_fn(&old_hash_ctx); 459 partial_pack_offset = ~partial_pack_offset; 460 partial_pack_offset -= MSB(partial_pack_offset, 1); 461 } 462 } 463 free(buf); 464 465 if (partial_pack_hash) 466 git_hash_final(partial_pack_hash, &old_hash_ctx); 467 git_hash_final(new_pack_hash, &new_hash_ctx); 468 write_or_die(pack_fd, new_pack_hash, hash_algo->rawsz); 469 fsync_component_or_die(FSYNC_COMPONENT_PACK, pack_fd, pack_name); 470} 471 472char *index_pack_lockfile(struct repository *r, int ip_out, int *is_well_formed) 473{ 474 char packname[GIT_MAX_HEXSZ + 6]; 475 const int len = r->hash_algo->hexsz + 6; 476 477 /* 478 * The first thing we expect from index-pack's output 479 * is "pack\t%40s\n" or "keep\t%40s\n" (46 bytes) where 480 * %40s is the newly created pack SHA1 name. In the "keep" 481 * case, we need it to remove the corresponding .keep file 482 * later on. If we don't get that then tough luck with it. 483 */ 484 if (read_in_full(ip_out, packname, len) == len && packname[len-1] == '\n') { 485 const char *name; 486 487 if (is_well_formed) 488 *is_well_formed = 1; 489 packname[len-1] = 0; 490 if (skip_prefix(packname, "keep\t", &name)) 491 return xstrfmt("%s/pack/pack-%s.keep", 492 repo_get_object_directory(r), name); 493 return NULL; 494 } 495 if (is_well_formed) 496 *is_well_formed = 0; 497 return NULL; 498} 499 500/* 501 * The per-object header is a pretty dense thing, which is 502 * - first byte: low four bits are "size", then three bits of "type", 503 * and the high bit is "size continues". 504 * - each byte afterwards: low seven bits are size continuation, 505 * with the high bit being "size continues" 506 */ 507int encode_in_pack_object_header(unsigned char *hdr, int hdr_len, 508 enum object_type type, uintmax_t size) 509{ 510 int n = 1; 511 unsigned char c; 512 513 if (type < OBJ_COMMIT || type > OBJ_REF_DELTA) 514 die("bad type %d", type); 515 516 c = (type << 4) | (size & 15); 517 size >>= 4; 518 while (size) { 519 if (n == hdr_len) 520 die("object size is too enormous to format"); 521 *hdr++ = c | 0x80; 522 c = size & 0x7f; 523 size >>= 7; 524 n++; 525 } 526 *hdr = c; 527 return n; 528} 529 530struct hashfile *create_tmp_packfile(struct repository *repo, 531 char **pack_tmp_name) 532{ 533 struct strbuf tmpname = STRBUF_INIT; 534 int fd; 535 536 fd = odb_mkstemp(repo->objects, &tmpname, "pack/tmp_pack_XXXXXX"); 537 *pack_tmp_name = strbuf_detach(&tmpname, NULL); 538 return hashfd(repo->hash_algo, fd, *pack_tmp_name); 539} 540 541static void rename_tmp_packfile(struct repository *repo, 542 struct strbuf *name_prefix, const char *source, 543 const char *ext) 544{ 545 size_t name_prefix_len = name_prefix->len; 546 547 strbuf_addstr(name_prefix, ext); 548 if (finalize_object_file(repo, source, name_prefix->buf)) 549 die("unable to rename temporary file to '%s'", 550 name_prefix->buf); 551 strbuf_setlen(name_prefix, name_prefix_len); 552} 553 554void rename_tmp_packfile_idx(struct repository *repo, 555 struct strbuf *name_buffer, 556 char **idx_tmp_name) 557{ 558 rename_tmp_packfile(repo, name_buffer, *idx_tmp_name, "idx"); 559} 560 561void stage_tmp_packfiles(struct repository *repo, 562 struct strbuf *name_buffer, 563 const char *pack_tmp_name, 564 struct pack_idx_entry **written_list, 565 uint32_t nr_written, 566 struct packing_data *to_pack, 567 struct pack_idx_option *pack_idx_opts, 568 unsigned char hash[], 569 char **idx_tmp_name) 570{ 571 char *rev_tmp_name = NULL; 572 char *mtimes_tmp_name = NULL; 573 574 if (adjust_shared_perm(repo, pack_tmp_name)) 575 die_errno("unable to make temporary pack file readable"); 576 577 *idx_tmp_name = (char *)write_idx_file(repo, NULL, written_list, 578 nr_written, pack_idx_opts, hash); 579 if (adjust_shared_perm(repo, *idx_tmp_name)) 580 die_errno("unable to make temporary index file readable"); 581 582 rev_tmp_name = write_rev_file(repo, NULL, written_list, nr_written, 583 hash, pack_idx_opts->flags); 584 585 if (pack_idx_opts->flags & WRITE_MTIMES) { 586 mtimes_tmp_name = write_mtimes_file(repo, to_pack, 587 written_list, nr_written, 588 hash); 589 } 590 591 rename_tmp_packfile(repo, name_buffer, pack_tmp_name, "pack"); 592 if (rev_tmp_name) 593 rename_tmp_packfile(repo, name_buffer, rev_tmp_name, "rev"); 594 if (mtimes_tmp_name) 595 rename_tmp_packfile(repo, name_buffer, mtimes_tmp_name, "mtimes"); 596 597 free(rev_tmp_name); 598 free(mtimes_tmp_name); 599} 600 601void write_promisor_file(const char *promisor_name, struct ref **sought, int nr_sought) 602{ 603 int i, err; 604 FILE *output = xfopen(promisor_name, "w"); 605 606 for (i = 0; i < nr_sought; i++) 607 fprintf(output, "%s %s\n", oid_to_hex(&sought[i]->old_oid), 608 sought[i]->name); 609 610 err = ferror(output); 611 err |= fclose(output); 612 if (err) 613 die(_("could not write '%s' promisor file"), promisor_name); 614}