1#include"builtin.h" 2#include"cache.h" 3#include"repository.h" 4#include"config.h" 5#include"attr.h" 6#include"object.h" 7#include"blob.h" 8#include"commit.h" 9#include"tag.h" 10#include"tree.h" 11#include"delta.h" 12#include"pack.h" 13#include"pack-revindex.h" 14#include"csum-file.h" 15#include"tree-walk.h" 16#include"diff.h" 17#include"revision.h" 18#include"list-objects.h" 19#include"list-objects-filter.h" 20#include"list-objects-filter-options.h" 21#include"pack-objects.h" 22#include"progress.h" 23#include"refs.h" 24#include"streaming.h" 25#include"thread-utils.h" 26#include"pack-bitmap.h" 27#include"reachable.h" 28#include"sha1-array.h" 29#include"argv-array.h" 30#include"list.h" 31#include"packfile.h" 32#include"object-store.h" 33 34static const char*pack_usage[] = { 35N_("git pack-objects --stdout [<options>...] [< <ref-list> | < <object-list>]"), 36N_("git pack-objects [<options>...] <base-name> [< <ref-list> | < <object-list>]"), 37 NULL 38}; 39 40/* 41 * Objects we are going to pack are collected in the `to_pack` structure. 42 * It contains an array (dynamically expanded) of the object data, and a map 43 * that can resolve SHA1s to their position in the array. 44 */ 45static struct packing_data to_pack; 46 47static struct pack_idx_entry **written_list; 48static uint32_t nr_result, nr_written; 49 50static int non_empty; 51static int reuse_delta =1, reuse_object =1; 52static int keep_unreachable, unpack_unreachable, include_tag; 53static timestamp_t unpack_unreachable_expiration; 54static int pack_loose_unreachable; 55static int local; 56static int have_non_local_packs; 57static int incremental; 58static int ignore_packed_keep; 59static int allow_ofs_delta; 60static struct pack_idx_option pack_idx_opts; 61static const char*base_name; 62static int progress =1; 63static int window =10; 64static unsigned long pack_size_limit; 65static int depth =50; 66static int delta_search_threads; 67static int pack_to_stdout; 68static int num_preferred_base; 69static struct progress *progress_state; 70 71static struct packed_git *reuse_packfile; 72static uint32_t reuse_packfile_objects; 73static off_t reuse_packfile_offset; 74 75static int use_bitmap_index_default =1; 76static int use_bitmap_index = -1; 77static int write_bitmap_index; 78static uint16_t write_bitmap_options; 79 80static int exclude_promisor_objects; 81 82static unsigned long delta_cache_size =0; 83static unsigned long max_delta_cache_size =256*1024*1024; 84static unsigned long cache_max_small_delta_size =1000; 85 86static unsigned long window_memory_limit =0; 87 88static struct list_objects_filter_options filter_options; 89 90enum missing_action { 91 MA_ERROR =0,/* fail if any missing objects are encountered */ 92 MA_ALLOW_ANY,/* silently allow ALL missing objects */ 93 MA_ALLOW_PROMISOR,/* silently allow all missing PROMISOR objects */ 94}; 95static enum missing_action arg_missing_action; 96static show_object_fn fn_show_object; 97 98/* 99 * stats 100 */ 101static uint32_t written, written_delta; 102static uint32_t reused, reused_delta; 103 104/* 105 * Indexed commits 106 */ 107static struct commit **indexed_commits; 108static unsigned int indexed_commits_nr; 109static unsigned int indexed_commits_alloc; 110 111static voidindex_commit_for_bitmap(struct commit *commit) 112{ 113if(indexed_commits_nr >= indexed_commits_alloc) { 114 indexed_commits_alloc = (indexed_commits_alloc +32) *2; 115REALLOC_ARRAY(indexed_commits, indexed_commits_alloc); 116} 117 118 indexed_commits[indexed_commits_nr++] = commit; 119} 120 121static void*get_delta(struct object_entry *entry) 122{ 123unsigned long size, base_size, delta_size; 124void*buf, *base_buf, *delta_buf; 125enum object_type type; 126 127 buf =read_object_file(&entry->idx.oid, &type, &size); 128if(!buf) 129die("unable to read%s",oid_to_hex(&entry->idx.oid)); 130 base_buf =read_object_file(&entry->delta->idx.oid, &type, &base_size); 131if(!base_buf) 132die("unable to read%s", 133oid_to_hex(&entry->delta->idx.oid)); 134 delta_buf =diff_delta(base_buf, base_size, 135 buf, size, &delta_size,0); 136if(!delta_buf || delta_size != entry->delta_size) 137die("delta size changed"); 138free(buf); 139free(base_buf); 140return delta_buf; 141} 142 143static unsigned longdo_compress(void**pptr,unsigned long size) 144{ 145 git_zstream stream; 146void*in, *out; 147unsigned long maxsize; 148 149git_deflate_init(&stream, pack_compression_level); 150 maxsize =git_deflate_bound(&stream, size); 151 152 in = *pptr; 153 out =xmalloc(maxsize); 154*pptr = out; 155 156 stream.next_in = in; 157 stream.avail_in = size; 158 stream.next_out = out; 159 stream.avail_out = maxsize; 160while(git_deflate(&stream, Z_FINISH) == Z_OK) 161;/* nothing */ 162git_deflate_end(&stream); 163 164free(in); 165return stream.total_out; 166} 167 168static unsigned longwrite_large_blob_data(struct git_istream *st,struct hashfile *f, 169const struct object_id *oid) 170{ 171 git_zstream stream; 172unsigned char ibuf[1024*16]; 173unsigned char obuf[1024*16]; 174unsigned long olen =0; 175 176git_deflate_init(&stream, pack_compression_level); 177 178for(;;) { 179 ssize_t readlen; 180int zret = Z_OK; 181 readlen =read_istream(st, ibuf,sizeof(ibuf)); 182if(readlen == -1) 183die(_("unable to read%s"),oid_to_hex(oid)); 184 185 stream.next_in = ibuf; 186 stream.avail_in = readlen; 187while((stream.avail_in || readlen ==0) && 188(zret == Z_OK || zret == Z_BUF_ERROR)) { 189 stream.next_out = obuf; 190 stream.avail_out =sizeof(obuf); 191 zret =git_deflate(&stream, readlen ?0: Z_FINISH); 192hashwrite(f, obuf, stream.next_out - obuf); 193 olen += stream.next_out - obuf; 194} 195if(stream.avail_in) 196die(_("deflate error (%d)"), zret); 197if(readlen ==0) { 198if(zret != Z_STREAM_END) 199die(_("deflate error (%d)"), zret); 200break; 201} 202} 203git_deflate_end(&stream); 204return olen; 205} 206 207/* 208 * we are going to reuse the existing object data as is. make 209 * sure it is not corrupt. 210 */ 211static intcheck_pack_inflate(struct packed_git *p, 212struct pack_window **w_curs, 213 off_t offset, 214 off_t len, 215unsigned long expect) 216{ 217 git_zstream stream; 218unsigned char fakebuf[4096], *in; 219int st; 220 221memset(&stream,0,sizeof(stream)); 222git_inflate_init(&stream); 223do{ 224 in =use_pack(p, w_curs, offset, &stream.avail_in); 225 stream.next_in = in; 226 stream.next_out = fakebuf; 227 stream.avail_out =sizeof(fakebuf); 228 st =git_inflate(&stream, Z_FINISH); 229 offset += stream.next_in - in; 230}while(st == Z_OK || st == Z_BUF_ERROR); 231git_inflate_end(&stream); 232return(st == Z_STREAM_END && 233 stream.total_out == expect && 234 stream.total_in == len) ?0: -1; 235} 236 237static voidcopy_pack_data(struct hashfile *f, 238struct packed_git *p, 239struct pack_window **w_curs, 240 off_t offset, 241 off_t len) 242{ 243unsigned char*in; 244unsigned long avail; 245 246while(len) { 247 in =use_pack(p, w_curs, offset, &avail); 248if(avail > len) 249 avail = (unsigned long)len; 250hashwrite(f, in, avail); 251 offset += avail; 252 len -= avail; 253} 254} 255 256/* Return 0 if we will bust the pack-size limit */ 257static unsigned longwrite_no_reuse_object(struct hashfile *f,struct object_entry *entry, 258unsigned long limit,int usable_delta) 259{ 260unsigned long size, datalen; 261unsigned char header[MAX_PACK_OBJECT_HEADER], 262 dheader[MAX_PACK_OBJECT_HEADER]; 263unsigned hdrlen; 264enum object_type type; 265void*buf; 266struct git_istream *st = NULL; 267 268if(!usable_delta) { 269if(entry->type == OBJ_BLOB && 270 entry->size > big_file_threshold && 271(st =open_istream(&entry->idx.oid, &type, &size, NULL)) != NULL) 272 buf = NULL; 273else{ 274 buf =read_object_file(&entry->idx.oid, &type, &size); 275if(!buf) 276die(_("unable to read%s"), 277oid_to_hex(&entry->idx.oid)); 278} 279/* 280 * make sure no cached delta data remains from a 281 * previous attempt before a pack split occurred. 282 */ 283FREE_AND_NULL(entry->delta_data); 284 entry->z_delta_size =0; 285}else if(entry->delta_data) { 286 size = entry->delta_size; 287 buf = entry->delta_data; 288 entry->delta_data = NULL; 289 type = (allow_ofs_delta && entry->delta->idx.offset) ? 290 OBJ_OFS_DELTA : OBJ_REF_DELTA; 291}else{ 292 buf =get_delta(entry); 293 size = entry->delta_size; 294 type = (allow_ofs_delta && entry->delta->idx.offset) ? 295 OBJ_OFS_DELTA : OBJ_REF_DELTA; 296} 297 298if(st)/* large blob case, just assume we don't compress well */ 299 datalen = size; 300else if(entry->z_delta_size) 301 datalen = entry->z_delta_size; 302else 303 datalen =do_compress(&buf, size); 304 305/* 306 * The object header is a byte of 'type' followed by zero or 307 * more bytes of length. 308 */ 309 hdrlen =encode_in_pack_object_header(header,sizeof(header), 310 type, size); 311 312if(type == OBJ_OFS_DELTA) { 313/* 314 * Deltas with relative base contain an additional 315 * encoding of the relative offset for the delta 316 * base from this object's position in the pack. 317 */ 318 off_t ofs = entry->idx.offset - entry->delta->idx.offset; 319unsigned pos =sizeof(dheader) -1; 320 dheader[pos] = ofs &127; 321while(ofs >>=7) 322 dheader[--pos] =128| (--ofs &127); 323if(limit && hdrlen +sizeof(dheader) - pos + datalen +20>= limit) { 324if(st) 325close_istream(st); 326free(buf); 327return0; 328} 329hashwrite(f, header, hdrlen); 330hashwrite(f, dheader + pos,sizeof(dheader) - pos); 331 hdrlen +=sizeof(dheader) - pos; 332}else if(type == OBJ_REF_DELTA) { 333/* 334 * Deltas with a base reference contain 335 * an additional 20 bytes for the base sha1. 336 */ 337if(limit && hdrlen +20+ datalen +20>= limit) { 338if(st) 339close_istream(st); 340free(buf); 341return0; 342} 343hashwrite(f, header, hdrlen); 344hashwrite(f, entry->delta->idx.oid.hash,20); 345 hdrlen +=20; 346}else{ 347if(limit && hdrlen + datalen +20>= limit) { 348if(st) 349close_istream(st); 350free(buf); 351return0; 352} 353hashwrite(f, header, hdrlen); 354} 355if(st) { 356 datalen =write_large_blob_data(st, f, &entry->idx.oid); 357close_istream(st); 358}else{ 359hashwrite(f, buf, datalen); 360free(buf); 361} 362 363return hdrlen + datalen; 364} 365 366/* Return 0 if we will bust the pack-size limit */ 367static off_t write_reuse_object(struct hashfile *f,struct object_entry *entry, 368unsigned long limit,int usable_delta) 369{ 370struct packed_git *p = entry->in_pack; 371struct pack_window *w_curs = NULL; 372struct revindex_entry *revidx; 373 off_t offset; 374enum object_type type = entry->type; 375 off_t datalen; 376unsigned char header[MAX_PACK_OBJECT_HEADER], 377 dheader[MAX_PACK_OBJECT_HEADER]; 378unsigned hdrlen; 379 380if(entry->delta) 381 type = (allow_ofs_delta && entry->delta->idx.offset) ? 382 OBJ_OFS_DELTA : OBJ_REF_DELTA; 383 hdrlen =encode_in_pack_object_header(header,sizeof(header), 384 type, entry->size); 385 386 offset = entry->in_pack_offset; 387 revidx =find_pack_revindex(p, offset); 388 datalen = revidx[1].offset - offset; 389if(!pack_to_stdout && p->index_version >1&& 390check_pack_crc(p, &w_curs, offset, datalen, revidx->nr)) { 391error("bad packed object CRC for%s", 392oid_to_hex(&entry->idx.oid)); 393unuse_pack(&w_curs); 394returnwrite_no_reuse_object(f, entry, limit, usable_delta); 395} 396 397 offset += entry->in_pack_header_size; 398 datalen -= entry->in_pack_header_size; 399 400if(!pack_to_stdout && p->index_version ==1&& 401check_pack_inflate(p, &w_curs, offset, datalen, entry->size)) { 402error("corrupt packed object for%s", 403oid_to_hex(&entry->idx.oid)); 404unuse_pack(&w_curs); 405returnwrite_no_reuse_object(f, entry, limit, usable_delta); 406} 407 408if(type == OBJ_OFS_DELTA) { 409 off_t ofs = entry->idx.offset - entry->delta->idx.offset; 410unsigned pos =sizeof(dheader) -1; 411 dheader[pos] = ofs &127; 412while(ofs >>=7) 413 dheader[--pos] =128| (--ofs &127); 414if(limit && hdrlen +sizeof(dheader) - pos + datalen +20>= limit) { 415unuse_pack(&w_curs); 416return0; 417} 418hashwrite(f, header, hdrlen); 419hashwrite(f, dheader + pos,sizeof(dheader) - pos); 420 hdrlen +=sizeof(dheader) - pos; 421 reused_delta++; 422}else if(type == OBJ_REF_DELTA) { 423if(limit && hdrlen +20+ datalen +20>= limit) { 424unuse_pack(&w_curs); 425return0; 426} 427hashwrite(f, header, hdrlen); 428hashwrite(f, entry->delta->idx.oid.hash,20); 429 hdrlen +=20; 430 reused_delta++; 431}else{ 432if(limit && hdrlen + datalen +20>= limit) { 433unuse_pack(&w_curs); 434return0; 435} 436hashwrite(f, header, hdrlen); 437} 438copy_pack_data(f, p, &w_curs, offset, datalen); 439unuse_pack(&w_curs); 440 reused++; 441return hdrlen + datalen; 442} 443 444/* Return 0 if we will bust the pack-size limit */ 445static off_t write_object(struct hashfile *f, 446struct object_entry *entry, 447 off_t write_offset) 448{ 449unsigned long limit; 450 off_t len; 451int usable_delta, to_reuse; 452 453if(!pack_to_stdout) 454crc32_begin(f); 455 456/* apply size limit if limited packsize and not first object */ 457if(!pack_size_limit || !nr_written) 458 limit =0; 459else if(pack_size_limit <= write_offset) 460/* 461 * the earlier object did not fit the limit; avoid 462 * mistaking this with unlimited (i.e. limit = 0). 463 */ 464 limit =1; 465else 466 limit = pack_size_limit - write_offset; 467 468if(!entry->delta) 469 usable_delta =0;/* no delta */ 470else if(!pack_size_limit) 471 usable_delta =1;/* unlimited packfile */ 472else if(entry->delta->idx.offset == (off_t)-1) 473 usable_delta =0;/* base was written to another pack */ 474else if(entry->delta->idx.offset) 475 usable_delta =1;/* base already exists in this pack */ 476else 477 usable_delta =0;/* base could end up in another pack */ 478 479if(!reuse_object) 480 to_reuse =0;/* explicit */ 481else if(!entry->in_pack) 482 to_reuse =0;/* can't reuse what we don't have */ 483else if(entry->type == OBJ_REF_DELTA || entry->type == OBJ_OFS_DELTA) 484/* check_object() decided it for us ... */ 485 to_reuse = usable_delta; 486/* ... but pack split may override that */ 487else if(entry->type != entry->in_pack_type) 488 to_reuse =0;/* pack has delta which is unusable */ 489else if(entry->delta) 490 to_reuse =0;/* we want to pack afresh */ 491else 492 to_reuse =1;/* we have it in-pack undeltified, 493 * and we do not need to deltify it. 494 */ 495 496if(!to_reuse) 497 len =write_no_reuse_object(f, entry, limit, usable_delta); 498else 499 len =write_reuse_object(f, entry, limit, usable_delta); 500if(!len) 501return0; 502 503if(usable_delta) 504 written_delta++; 505 written++; 506if(!pack_to_stdout) 507 entry->idx.crc32 =crc32_end(f); 508return len; 509} 510 511enum write_one_status { 512 WRITE_ONE_SKIP = -1,/* already written */ 513 WRITE_ONE_BREAK =0,/* writing this will bust the limit; not written */ 514 WRITE_ONE_WRITTEN =1,/* normal */ 515 WRITE_ONE_RECURSIVE =2/* already scheduled to be written */ 516}; 517 518static enum write_one_status write_one(struct hashfile *f, 519struct object_entry *e, 520 off_t *offset) 521{ 522 off_t size; 523int recursing; 524 525/* 526 * we set offset to 1 (which is an impossible value) to mark 527 * the fact that this object is involved in "write its base 528 * first before writing a deltified object" recursion. 529 */ 530 recursing = (e->idx.offset ==1); 531if(recursing) { 532warning("recursive delta detected for object%s", 533oid_to_hex(&e->idx.oid)); 534return WRITE_ONE_RECURSIVE; 535}else if(e->idx.offset || e->preferred_base) { 536/* offset is non zero if object is written already. */ 537return WRITE_ONE_SKIP; 538} 539 540/* if we are deltified, write out base object first. */ 541if(e->delta) { 542 e->idx.offset =1;/* now recurse */ 543switch(write_one(f, e->delta, offset)) { 544case WRITE_ONE_RECURSIVE: 545/* we cannot depend on this one */ 546 e->delta = NULL; 547break; 548default: 549break; 550case WRITE_ONE_BREAK: 551 e->idx.offset = recursing; 552return WRITE_ONE_BREAK; 553} 554} 555 556 e->idx.offset = *offset; 557 size =write_object(f, e, *offset); 558if(!size) { 559 e->idx.offset = recursing; 560return WRITE_ONE_BREAK; 561} 562 written_list[nr_written++] = &e->idx; 563 564/* make sure off_t is sufficiently large not to wrap */ 565if(signed_add_overflows(*offset, size)) 566die("pack too large for current definition of off_t"); 567*offset += size; 568return WRITE_ONE_WRITTEN; 569} 570 571static intmark_tagged(const char*path,const struct object_id *oid,int flag, 572void*cb_data) 573{ 574struct object_id peeled; 575struct object_entry *entry =packlist_find(&to_pack, oid->hash, NULL); 576 577if(entry) 578 entry->tagged =1; 579if(!peel_ref(path, &peeled)) { 580 entry =packlist_find(&to_pack, peeled.hash, NULL); 581if(entry) 582 entry->tagged =1; 583} 584return0; 585} 586 587staticinlinevoidadd_to_write_order(struct object_entry **wo, 588unsigned int*endp, 589struct object_entry *e) 590{ 591if(e->filled) 592return; 593 wo[(*endp)++] = e; 594 e->filled =1; 595} 596 597static voidadd_descendants_to_write_order(struct object_entry **wo, 598unsigned int*endp, 599struct object_entry *e) 600{ 601int add_to_order =1; 602while(e) { 603if(add_to_order) { 604struct object_entry *s; 605/* add this node... */ 606add_to_write_order(wo, endp, e); 607/* all its siblings... */ 608for(s = e->delta_sibling; s; s = s->delta_sibling) { 609add_to_write_order(wo, endp, s); 610} 611} 612/* drop down a level to add left subtree nodes if possible */ 613if(e->delta_child) { 614 add_to_order =1; 615 e = e->delta_child; 616}else{ 617 add_to_order =0; 618/* our sibling might have some children, it is next */ 619if(e->delta_sibling) { 620 e = e->delta_sibling; 621continue; 622} 623/* go back to our parent node */ 624 e = e->delta; 625while(e && !e->delta_sibling) { 626/* we're on the right side of a subtree, keep 627 * going up until we can go right again */ 628 e = e->delta; 629} 630if(!e) { 631/* done- we hit our original root node */ 632return; 633} 634/* pass it off to sibling at this level */ 635 e = e->delta_sibling; 636} 637}; 638} 639 640static voidadd_family_to_write_order(struct object_entry **wo, 641unsigned int*endp, 642struct object_entry *e) 643{ 644struct object_entry *root; 645 646for(root = e; root->delta; root = root->delta) 647;/* nothing */ 648add_descendants_to_write_order(wo, endp, root); 649} 650 651static struct object_entry **compute_write_order(void) 652{ 653unsigned int i, wo_end, last_untagged; 654 655struct object_entry **wo; 656struct object_entry *objects = to_pack.objects; 657 658for(i =0; i < to_pack.nr_objects; i++) { 659 objects[i].tagged =0; 660 objects[i].filled =0; 661 objects[i].delta_child = NULL; 662 objects[i].delta_sibling = NULL; 663} 664 665/* 666 * Fully connect delta_child/delta_sibling network. 667 * Make sure delta_sibling is sorted in the original 668 * recency order. 669 */ 670for(i = to_pack.nr_objects; i >0;) { 671struct object_entry *e = &objects[--i]; 672if(!e->delta) 673continue; 674/* Mark me as the first child */ 675 e->delta_sibling = e->delta->delta_child; 676 e->delta->delta_child = e; 677} 678 679/* 680 * Mark objects that are at the tip of tags. 681 */ 682for_each_tag_ref(mark_tagged, NULL); 683 684/* 685 * Give the objects in the original recency order until 686 * we see a tagged tip. 687 */ 688ALLOC_ARRAY(wo, to_pack.nr_objects); 689for(i = wo_end =0; i < to_pack.nr_objects; i++) { 690if(objects[i].tagged) 691break; 692add_to_write_order(wo, &wo_end, &objects[i]); 693} 694 last_untagged = i; 695 696/* 697 * Then fill all the tagged tips. 698 */ 699for(; i < to_pack.nr_objects; i++) { 700if(objects[i].tagged) 701add_to_write_order(wo, &wo_end, &objects[i]); 702} 703 704/* 705 * And then all remaining commits and tags. 706 */ 707for(i = last_untagged; i < to_pack.nr_objects; i++) { 708if(objects[i].type != OBJ_COMMIT && 709 objects[i].type != OBJ_TAG) 710continue; 711add_to_write_order(wo, &wo_end, &objects[i]); 712} 713 714/* 715 * And then all the trees. 716 */ 717for(i = last_untagged; i < to_pack.nr_objects; i++) { 718if(objects[i].type != OBJ_TREE) 719continue; 720add_to_write_order(wo, &wo_end, &objects[i]); 721} 722 723/* 724 * Finally all the rest in really tight order 725 */ 726for(i = last_untagged; i < to_pack.nr_objects; i++) { 727if(!objects[i].filled) 728add_family_to_write_order(wo, &wo_end, &objects[i]); 729} 730 731if(wo_end != to_pack.nr_objects) 732die("ordered%uobjects, expected %"PRIu32, wo_end, to_pack.nr_objects); 733 734return wo; 735} 736 737static off_t write_reused_pack(struct hashfile *f) 738{ 739unsigned char buffer[8192]; 740 off_t to_write, total; 741int fd; 742 743if(!is_pack_valid(reuse_packfile)) 744die("packfile is invalid:%s", reuse_packfile->pack_name); 745 746 fd =git_open(reuse_packfile->pack_name); 747if(fd <0) 748die_errno("unable to open packfile for reuse:%s", 749 reuse_packfile->pack_name); 750 751if(lseek(fd,sizeof(struct pack_header), SEEK_SET) == -1) 752die_errno("unable to seek in reused packfile"); 753 754if(reuse_packfile_offset <0) 755 reuse_packfile_offset = reuse_packfile->pack_size -20; 756 757 total = to_write = reuse_packfile_offset -sizeof(struct pack_header); 758 759while(to_write) { 760int read_pack =xread(fd, buffer,sizeof(buffer)); 761 762if(read_pack <=0) 763die_errno("unable to read from reused packfile"); 764 765if(read_pack > to_write) 766 read_pack = to_write; 767 768hashwrite(f, buffer, read_pack); 769 to_write -= read_pack; 770 771/* 772 * We don't know the actual number of objects written, 773 * only how many bytes written, how many bytes total, and 774 * how many objects total. So we can fake it by pretending all 775 * objects we are writing are the same size. This gives us a 776 * smooth progress meter, and at the end it matches the true 777 * answer. 778 */ 779 written = reuse_packfile_objects * 780(((double)(total - to_write)) / total); 781display_progress(progress_state, written); 782} 783 784close(fd); 785 written = reuse_packfile_objects; 786display_progress(progress_state, written); 787return reuse_packfile_offset -sizeof(struct pack_header); 788} 789 790static const char no_split_warning[] =N_( 791"disabling bitmap writing, packs are split due to pack.packSizeLimit" 792); 793 794static voidwrite_pack_file(void) 795{ 796uint32_t i =0, j; 797struct hashfile *f; 798 off_t offset; 799uint32_t nr_remaining = nr_result; 800time_t last_mtime =0; 801struct object_entry **write_order; 802 803if(progress > pack_to_stdout) 804 progress_state =start_progress(_("Writing objects"), nr_result); 805ALLOC_ARRAY(written_list, to_pack.nr_objects); 806 write_order =compute_write_order(); 807 808do{ 809struct object_id oid; 810char*pack_tmp_name = NULL; 811 812if(pack_to_stdout) 813 f =hashfd_throughput(1,"<stdout>", progress_state); 814else 815 f =create_tmp_packfile(&pack_tmp_name); 816 817 offset =write_pack_header(f, nr_remaining); 818 819if(reuse_packfile) { 820 off_t packfile_size; 821assert(pack_to_stdout); 822 823 packfile_size =write_reused_pack(f); 824 offset += packfile_size; 825} 826 827 nr_written =0; 828for(; i < to_pack.nr_objects; i++) { 829struct object_entry *e = write_order[i]; 830if(write_one(f, e, &offset) == WRITE_ONE_BREAK) 831break; 832display_progress(progress_state, written); 833} 834 835/* 836 * Did we write the wrong # entries in the header? 837 * If so, rewrite it like in fast-import 838 */ 839if(pack_to_stdout) { 840finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_CLOSE); 841}else if(nr_written == nr_remaining) { 842finalize_hashfile(f, oid.hash, CSUM_HASH_IN_STREAM | CSUM_FSYNC | CSUM_CLOSE); 843}else{ 844int fd =finalize_hashfile(f, oid.hash,0); 845fixup_pack_header_footer(fd, oid.hash, pack_tmp_name, 846 nr_written, oid.hash, offset); 847close(fd); 848if(write_bitmap_index) { 849warning(_(no_split_warning)); 850 write_bitmap_index =0; 851} 852} 853 854if(!pack_to_stdout) { 855struct stat st; 856struct strbuf tmpname = STRBUF_INIT; 857 858/* 859 * Packs are runtime accessed in their mtime 860 * order since newer packs are more likely to contain 861 * younger objects. So if we are creating multiple 862 * packs then we should modify the mtime of later ones 863 * to preserve this property. 864 */ 865if(stat(pack_tmp_name, &st) <0) { 866warning_errno("failed to stat%s", pack_tmp_name); 867}else if(!last_mtime) { 868 last_mtime = st.st_mtime; 869}else{ 870struct utimbuf utb; 871 utb.actime = st.st_atime; 872 utb.modtime = --last_mtime; 873if(utime(pack_tmp_name, &utb) <0) 874warning_errno("failed utime() on%s", pack_tmp_name); 875} 876 877strbuf_addf(&tmpname,"%s-", base_name); 878 879if(write_bitmap_index) { 880bitmap_writer_set_checksum(oid.hash); 881bitmap_writer_build_type_index(written_list, nr_written); 882} 883 884finish_tmp_packfile(&tmpname, pack_tmp_name, 885 written_list, nr_written, 886&pack_idx_opts, oid.hash); 887 888if(write_bitmap_index) { 889strbuf_addf(&tmpname,"%s.bitmap",oid_to_hex(&oid)); 890 891stop_progress(&progress_state); 892 893bitmap_writer_show_progress(progress); 894bitmap_writer_reuse_bitmaps(&to_pack); 895bitmap_writer_select_commits(indexed_commits, indexed_commits_nr, -1); 896bitmap_writer_build(&to_pack); 897bitmap_writer_finish(written_list, nr_written, 898 tmpname.buf, write_bitmap_options); 899 write_bitmap_index =0; 900} 901 902strbuf_release(&tmpname); 903free(pack_tmp_name); 904puts(oid_to_hex(&oid)); 905} 906 907/* mark written objects as written to previous pack */ 908for(j =0; j < nr_written; j++) { 909 written_list[j]->offset = (off_t)-1; 910} 911 nr_remaining -= nr_written; 912}while(nr_remaining && i < to_pack.nr_objects); 913 914free(written_list); 915free(write_order); 916stop_progress(&progress_state); 917if(written != nr_result) 918die("wrote %"PRIu32" objects while expecting %"PRIu32, 919 written, nr_result); 920} 921 922static intno_try_delta(const char*path) 923{ 924static struct attr_check *check; 925 926if(!check) 927 check =attr_check_initl("delta", NULL); 928if(git_check_attr(path, check)) 929return0; 930if(ATTR_FALSE(check->items[0].value)) 931return1; 932return0; 933} 934 935/* 936 * When adding an object, check whether we have already added it 937 * to our packing list. If so, we can skip. However, if we are 938 * being asked to excludei t, but the previous mention was to include 939 * it, make sure to adjust its flags and tweak our numbers accordingly. 940 * 941 * As an optimization, we pass out the index position where we would have 942 * found the item, since that saves us from having to look it up again a 943 * few lines later when we want to add the new entry. 944 */ 945static inthave_duplicate_entry(const struct object_id *oid, 946int exclude, 947uint32_t*index_pos) 948{ 949struct object_entry *entry; 950 951 entry =packlist_find(&to_pack, oid->hash, index_pos); 952if(!entry) 953return0; 954 955if(exclude) { 956if(!entry->preferred_base) 957 nr_result--; 958 entry->preferred_base =1; 959} 960 961return1; 962} 963 964static intwant_found_object(int exclude,struct packed_git *p) 965{ 966if(exclude) 967return1; 968if(incremental) 969return0; 970 971/* 972 * When asked to do --local (do not include an object that appears in a 973 * pack we borrow from elsewhere) or --honor-pack-keep (do not include 974 * an object that appears in a pack marked with .keep), finding a pack 975 * that matches the criteria is sufficient for us to decide to omit it. 976 * However, even if this pack does not satisfy the criteria, we need to 977 * make sure no copy of this object appears in _any_ pack that makes us 978 * to omit the object, so we need to check all the packs. 979 * 980 * We can however first check whether these options can possible matter; 981 * if they do not matter we know we want the object in generated pack. 982 * Otherwise, we signal "-1" at the end to tell the caller that we do 983 * not know either way, and it needs to check more packs. 984 */ 985if(!ignore_packed_keep && 986(!local || !have_non_local_packs)) 987return1; 988 989if(local && !p->pack_local) 990return0; 991if(ignore_packed_keep && p->pack_local && p->pack_keep) 992return0; 993 994/* we don't know yet; keep looking for more packs */ 995return-1; 996} 997 998/* 999 * Check whether we want the object in the pack (e.g., we do not want1000 * objects found in non-local stores if the "--local" option was used).1001 *1002 * If the caller already knows an existing pack it wants to take the object1003 * from, that is passed in *found_pack and *found_offset; otherwise this1004 * function finds if there is any pack that has the object and returns the pack1005 * and its offset in these variables.1006 */1007static intwant_object_in_pack(const struct object_id *oid,1008int exclude,1009struct packed_git **found_pack,1010 off_t *found_offset)1011{1012int want;1013struct list_head *pos;10141015if(!exclude && local &&has_loose_object_nonlocal(oid->hash))1016return0;10171018/*1019 * If we already know the pack object lives in, start checks from that1020 * pack - in the usual case when neither --local was given nor .keep files1021 * are present we will determine the answer right now.1022 */1023if(*found_pack) {1024 want =want_found_object(exclude, *found_pack);1025if(want != -1)1026return want;1027}1028list_for_each(pos,get_packed_git_mru(the_repository)) {1029struct packed_git *p =list_entry(pos,struct packed_git, mru);1030 off_t offset;10311032if(p == *found_pack)1033 offset = *found_offset;1034else1035 offset =find_pack_entry_one(oid->hash, p);10361037if(offset) {1038if(!*found_pack) {1039if(!is_pack_valid(p))1040continue;1041*found_offset = offset;1042*found_pack = p;1043}1044 want =want_found_object(exclude, p);1045if(!exclude && want >0)1046list_move(&p->mru,1047get_packed_git_mru(the_repository));1048if(want != -1)1049return want;1050}1051}10521053return1;1054}10551056static voidcreate_object_entry(const struct object_id *oid,1057enum object_type type,1058uint32_t hash,1059int exclude,1060int no_try_delta,1061uint32_t index_pos,1062struct packed_git *found_pack,1063 off_t found_offset)1064{1065struct object_entry *entry;10661067 entry =packlist_alloc(&to_pack, oid->hash, index_pos);1068 entry->hash = hash;1069if(type)1070 entry->type = type;1071if(exclude)1072 entry->preferred_base =1;1073else1074 nr_result++;1075if(found_pack) {1076 entry->in_pack = found_pack;1077 entry->in_pack_offset = found_offset;1078}10791080 entry->no_try_delta = no_try_delta;1081}10821083static const char no_closure_warning[] =N_(1084"disabling bitmap writing, as some objects are not being packed"1085);10861087static intadd_object_entry(const struct object_id *oid,enum object_type type,1088const char*name,int exclude)1089{1090struct packed_git *found_pack = NULL;1091 off_t found_offset =0;1092uint32_t index_pos;10931094if(have_duplicate_entry(oid, exclude, &index_pos))1095return0;10961097if(!want_object_in_pack(oid, exclude, &found_pack, &found_offset)) {1098/* The pack is missing an object, so it will not have closure */1099if(write_bitmap_index) {1100warning(_(no_closure_warning));1101 write_bitmap_index =0;1102}1103return0;1104}11051106create_object_entry(oid, type,pack_name_hash(name),1107 exclude, name &&no_try_delta(name),1108 index_pos, found_pack, found_offset);11091110display_progress(progress_state, nr_result);1111return1;1112}11131114static intadd_object_entry_from_bitmap(const struct object_id *oid,1115enum object_type type,1116int flags,uint32_t name_hash,1117struct packed_git *pack, off_t offset)1118{1119uint32_t index_pos;11201121if(have_duplicate_entry(oid,0, &index_pos))1122return0;11231124if(!want_object_in_pack(oid,0, &pack, &offset))1125return0;11261127create_object_entry(oid, type, name_hash,0,0, index_pos, pack, offset);11281129display_progress(progress_state, nr_result);1130return1;1131}11321133struct pbase_tree_cache {1134struct object_id oid;1135int ref;1136int temporary;1137void*tree_data;1138unsigned long tree_size;1139};11401141static struct pbase_tree_cache *(pbase_tree_cache[256]);1142static intpbase_tree_cache_ix(const struct object_id *oid)1143{1144return oid->hash[0] %ARRAY_SIZE(pbase_tree_cache);1145}1146static intpbase_tree_cache_ix_incr(int ix)1147{1148return(ix+1) %ARRAY_SIZE(pbase_tree_cache);1149}11501151static struct pbase_tree {1152struct pbase_tree *next;1153/* This is a phony "cache" entry; we are not1154 * going to evict it or find it through _get()1155 * mechanism -- this is for the toplevel node that1156 * would almost always change with any commit.1157 */1158struct pbase_tree_cache pcache;1159} *pbase_tree;11601161static struct pbase_tree_cache *pbase_tree_get(const struct object_id *oid)1162{1163struct pbase_tree_cache *ent, *nent;1164void*data;1165unsigned long size;1166enum object_type type;1167int neigh;1168int my_ix =pbase_tree_cache_ix(oid);1169int available_ix = -1;11701171/* pbase-tree-cache acts as a limited hashtable.1172 * your object will be found at your index or within a few1173 * slots after that slot if it is cached.1174 */1175for(neigh =0; neigh <8; neigh++) {1176 ent = pbase_tree_cache[my_ix];1177if(ent && !oidcmp(&ent->oid, oid)) {1178 ent->ref++;1179return ent;1180}1181else if(((available_ix <0) && (!ent || !ent->ref)) ||1182((0<= available_ix) &&1183(!ent && pbase_tree_cache[available_ix])))1184 available_ix = my_ix;1185if(!ent)1186break;1187 my_ix =pbase_tree_cache_ix_incr(my_ix);1188}11891190/* Did not find one. Either we got a bogus request or1191 * we need to read and perhaps cache.1192 */1193 data =read_object_file(oid, &type, &size);1194if(!data)1195return NULL;1196if(type != OBJ_TREE) {1197free(data);1198return NULL;1199}12001201/* We need to either cache or return a throwaway copy */12021203if(available_ix <0)1204 ent = NULL;1205else{1206 ent = pbase_tree_cache[available_ix];1207 my_ix = available_ix;1208}12091210if(!ent) {1211 nent =xmalloc(sizeof(*nent));1212 nent->temporary = (available_ix <0);1213}1214else{1215/* evict and reuse */1216free(ent->tree_data);1217 nent = ent;1218}1219oidcpy(&nent->oid, oid);1220 nent->tree_data = data;1221 nent->tree_size = size;1222 nent->ref =1;1223if(!nent->temporary)1224 pbase_tree_cache[my_ix] = nent;1225return nent;1226}12271228static voidpbase_tree_put(struct pbase_tree_cache *cache)1229{1230if(!cache->temporary) {1231 cache->ref--;1232return;1233}1234free(cache->tree_data);1235free(cache);1236}12371238static intname_cmp_len(const char*name)1239{1240int i;1241for(i =0; name[i] && name[i] !='\n'&& name[i] !='/'; i++)1242;1243return i;1244}12451246static voidadd_pbase_object(struct tree_desc *tree,1247const char*name,1248int cmplen,1249const char*fullname)1250{1251struct name_entry entry;1252int cmp;12531254while(tree_entry(tree,&entry)) {1255if(S_ISGITLINK(entry.mode))1256continue;1257 cmp =tree_entry_len(&entry) != cmplen ?1:1258memcmp(name, entry.path, cmplen);1259if(cmp >0)1260continue;1261if(cmp <0)1262return;1263if(name[cmplen] !='/') {1264add_object_entry(entry.oid,1265object_type(entry.mode),1266 fullname,1);1267return;1268}1269if(S_ISDIR(entry.mode)) {1270struct tree_desc sub;1271struct pbase_tree_cache *tree;1272const char*down = name+cmplen+1;1273int downlen =name_cmp_len(down);12741275 tree =pbase_tree_get(entry.oid);1276if(!tree)1277return;1278init_tree_desc(&sub, tree->tree_data, tree->tree_size);12791280add_pbase_object(&sub, down, downlen, fullname);1281pbase_tree_put(tree);1282}1283}1284}12851286static unsigned*done_pbase_paths;1287static int done_pbase_paths_num;1288static int done_pbase_paths_alloc;1289static intdone_pbase_path_pos(unsigned hash)1290{1291int lo =0;1292int hi = done_pbase_paths_num;1293while(lo < hi) {1294int mi = lo + (hi - lo) /2;1295if(done_pbase_paths[mi] == hash)1296return mi;1297if(done_pbase_paths[mi] < hash)1298 hi = mi;1299else1300 lo = mi +1;1301}1302return-lo-1;1303}13041305static intcheck_pbase_path(unsigned hash)1306{1307int pos =done_pbase_path_pos(hash);1308if(0<= pos)1309return1;1310 pos = -pos -1;1311ALLOC_GROW(done_pbase_paths,1312 done_pbase_paths_num +1,1313 done_pbase_paths_alloc);1314 done_pbase_paths_num++;1315if(pos < done_pbase_paths_num)1316MOVE_ARRAY(done_pbase_paths + pos +1, done_pbase_paths + pos,1317 done_pbase_paths_num - pos -1);1318 done_pbase_paths[pos] = hash;1319return0;1320}13211322static voidadd_preferred_base_object(const char*name)1323{1324struct pbase_tree *it;1325int cmplen;1326unsigned hash =pack_name_hash(name);13271328if(!num_preferred_base ||check_pbase_path(hash))1329return;13301331 cmplen =name_cmp_len(name);1332for(it = pbase_tree; it; it = it->next) {1333if(cmplen ==0) {1334add_object_entry(&it->pcache.oid, OBJ_TREE, NULL,1);1335}1336else{1337struct tree_desc tree;1338init_tree_desc(&tree, it->pcache.tree_data, it->pcache.tree_size);1339add_pbase_object(&tree, name, cmplen, name);1340}1341}1342}13431344static voidadd_preferred_base(struct object_id *oid)1345{1346struct pbase_tree *it;1347void*data;1348unsigned long size;1349struct object_id tree_oid;13501351if(window <= num_preferred_base++)1352return;13531354 data =read_object_with_reference(oid, tree_type, &size, &tree_oid);1355if(!data)1356return;13571358for(it = pbase_tree; it; it = it->next) {1359if(!oidcmp(&it->pcache.oid, &tree_oid)) {1360free(data);1361return;1362}1363}13641365 it =xcalloc(1,sizeof(*it));1366 it->next = pbase_tree;1367 pbase_tree = it;13681369oidcpy(&it->pcache.oid, &tree_oid);1370 it->pcache.tree_data = data;1371 it->pcache.tree_size = size;1372}13731374static voidcleanup_preferred_base(void)1375{1376struct pbase_tree *it;1377unsigned i;13781379 it = pbase_tree;1380 pbase_tree = NULL;1381while(it) {1382struct pbase_tree *tmp = it;1383 it = tmp->next;1384free(tmp->pcache.tree_data);1385free(tmp);1386}13871388for(i =0; i <ARRAY_SIZE(pbase_tree_cache); i++) {1389if(!pbase_tree_cache[i])1390continue;1391free(pbase_tree_cache[i]->tree_data);1392FREE_AND_NULL(pbase_tree_cache[i]);1393}13941395FREE_AND_NULL(done_pbase_paths);1396 done_pbase_paths_num = done_pbase_paths_alloc =0;1397}13981399static voidcheck_object(struct object_entry *entry)1400{1401if(entry->in_pack) {1402struct packed_git *p = entry->in_pack;1403struct pack_window *w_curs = NULL;1404const unsigned char*base_ref = NULL;1405struct object_entry *base_entry;1406unsigned long used, used_0;1407unsigned long avail;1408 off_t ofs;1409unsigned char*buf, c;14101411 buf =use_pack(p, &w_curs, entry->in_pack_offset, &avail);14121413/*1414 * We want in_pack_type even if we do not reuse delta1415 * since non-delta representations could still be reused.1416 */1417 used =unpack_object_header_buffer(buf, avail,1418&entry->in_pack_type,1419&entry->size);1420if(used ==0)1421goto give_up;14221423/*1424 * Determine if this is a delta and if so whether we can1425 * reuse it or not. Otherwise let's find out as cheaply as1426 * possible what the actual type and size for this object is.1427 */1428switch(entry->in_pack_type) {1429default:1430/* Not a delta hence we've already got all we need. */1431 entry->type = entry->in_pack_type;1432 entry->in_pack_header_size = used;1433if(entry->type < OBJ_COMMIT || entry->type > OBJ_BLOB)1434goto give_up;1435unuse_pack(&w_curs);1436return;1437case OBJ_REF_DELTA:1438if(reuse_delta && !entry->preferred_base)1439 base_ref =use_pack(p, &w_curs,1440 entry->in_pack_offset + used, NULL);1441 entry->in_pack_header_size = used +20;1442break;1443case OBJ_OFS_DELTA:1444 buf =use_pack(p, &w_curs,1445 entry->in_pack_offset + used, NULL);1446 used_0 =0;1447 c = buf[used_0++];1448 ofs = c &127;1449while(c &128) {1450 ofs +=1;1451if(!ofs ||MSB(ofs,7)) {1452error("delta base offset overflow in pack for%s",1453oid_to_hex(&entry->idx.oid));1454goto give_up;1455}1456 c = buf[used_0++];1457 ofs = (ofs <<7) + (c &127);1458}1459 ofs = entry->in_pack_offset - ofs;1460if(ofs <=0|| ofs >= entry->in_pack_offset) {1461error("delta base offset out of bound for%s",1462oid_to_hex(&entry->idx.oid));1463goto give_up;1464}1465if(reuse_delta && !entry->preferred_base) {1466struct revindex_entry *revidx;1467 revidx =find_pack_revindex(p, ofs);1468if(!revidx)1469goto give_up;1470 base_ref =nth_packed_object_sha1(p, revidx->nr);1471}1472 entry->in_pack_header_size = used + used_0;1473break;1474}14751476if(base_ref && (base_entry =packlist_find(&to_pack, base_ref, NULL))) {1477/*1478 * If base_ref was set above that means we wish to1479 * reuse delta data, and we even found that base1480 * in the list of objects we want to pack. Goodie!1481 *1482 * Depth value does not matter - find_deltas() will1483 * never consider reused delta as the base object to1484 * deltify other objects against, in order to avoid1485 * circular deltas.1486 */1487 entry->type = entry->in_pack_type;1488 entry->delta = base_entry;1489 entry->delta_size = entry->size;1490 entry->delta_sibling = base_entry->delta_child;1491 base_entry->delta_child = entry;1492unuse_pack(&w_curs);1493return;1494}14951496if(entry->type) {1497/*1498 * This must be a delta and we already know what the1499 * final object type is. Let's extract the actual1500 * object size from the delta header.1501 */1502 entry->size =get_size_from_delta(p, &w_curs,1503 entry->in_pack_offset + entry->in_pack_header_size);1504if(entry->size ==0)1505goto give_up;1506unuse_pack(&w_curs);1507return;1508}15091510/*1511 * No choice but to fall back to the recursive delta walk1512 * with sha1_object_info() to find about the object type1513 * at this point...1514 */1515 give_up:1516unuse_pack(&w_curs);1517}15181519 entry->type =oid_object_info(&entry->idx.oid, &entry->size);1520/*1521 * The error condition is checked in prepare_pack(). This is1522 * to permit a missing preferred base object to be ignored1523 * as a preferred base. Doing so can result in a larger1524 * pack file, but the transfer will still take place.1525 */1526}15271528static intpack_offset_sort(const void*_a,const void*_b)1529{1530const struct object_entry *a = *(struct object_entry **)_a;1531const struct object_entry *b = *(struct object_entry **)_b;15321533/* avoid filesystem trashing with loose objects */1534if(!a->in_pack && !b->in_pack)1535returnoidcmp(&a->idx.oid, &b->idx.oid);15361537if(a->in_pack < b->in_pack)1538return-1;1539if(a->in_pack > b->in_pack)1540return1;1541return a->in_pack_offset < b->in_pack_offset ? -1:1542(a->in_pack_offset > b->in_pack_offset);1543}15441545/*1546 * Drop an on-disk delta we were planning to reuse. Naively, this would1547 * just involve blanking out the "delta" field, but we have to deal1548 * with some extra book-keeping:1549 *1550 * 1. Removing ourselves from the delta_sibling linked list.1551 *1552 * 2. Updating our size/type to the non-delta representation. These were1553 * either not recorded initially (size) or overwritten with the delta type1554 * (type) when check_object() decided to reuse the delta.1555 *1556 * 3. Resetting our delta depth, as we are now a base object.1557 */1558static voiddrop_reused_delta(struct object_entry *entry)1559{1560struct object_entry **p = &entry->delta->delta_child;1561struct object_info oi = OBJECT_INFO_INIT;15621563while(*p) {1564if(*p == entry)1565*p = (*p)->delta_sibling;1566else1567 p = &(*p)->delta_sibling;1568}1569 entry->delta = NULL;1570 entry->depth =0;15711572 oi.sizep = &entry->size;1573 oi.typep = &entry->type;1574if(packed_object_info(entry->in_pack, entry->in_pack_offset, &oi) <0) {1575/*1576 * We failed to get the info from this pack for some reason;1577 * fall back to sha1_object_info, which may find another copy.1578 * And if that fails, the error will be recorded in entry->type1579 * and dealt with in prepare_pack().1580 */1581 entry->type =oid_object_info(&entry->idx.oid, &entry->size);1582}1583}15841585/*1586 * Follow the chain of deltas from this entry onward, throwing away any links1587 * that cause us to hit a cycle (as determined by the DFS state flags in1588 * the entries).1589 *1590 * We also detect too-long reused chains that would violate our --depth1591 * limit.1592 */1593static voidbreak_delta_chains(struct object_entry *entry)1594{1595/*1596 * The actual depth of each object we will write is stored as an int,1597 * as it cannot exceed our int "depth" limit. But before we break1598 * changes based no that limit, we may potentially go as deep as the1599 * number of objects, which is elsewhere bounded to a uint32_t.1600 */1601uint32_t total_depth;1602struct object_entry *cur, *next;16031604for(cur = entry, total_depth =0;1605 cur;1606 cur = cur->delta, total_depth++) {1607if(cur->dfs_state == DFS_DONE) {1608/*1609 * We've already seen this object and know it isn't1610 * part of a cycle. We do need to append its depth1611 * to our count.1612 */1613 total_depth += cur->depth;1614break;1615}16161617/*1618 * We break cycles before looping, so an ACTIVE state (or any1619 * other cruft which made its way into the state variable)1620 * is a bug.1621 */1622if(cur->dfs_state != DFS_NONE)1623die("BUG: confusing delta dfs state in first pass:%d",1624 cur->dfs_state);16251626/*1627 * Now we know this is the first time we've seen the object. If1628 * it's not a delta, we're done traversing, but we'll mark it1629 * done to save time on future traversals.1630 */1631if(!cur->delta) {1632 cur->dfs_state = DFS_DONE;1633break;1634}16351636/*1637 * Mark ourselves as active and see if the next step causes1638 * us to cycle to another active object. It's important to do1639 * this _before_ we loop, because it impacts where we make the1640 * cut, and thus how our total_depth counter works.1641 * E.g., We may see a partial loop like:1642 *1643 * A -> B -> C -> D -> B1644 *1645 * Cutting B->C breaks the cycle. But now the depth of A is1646 * only 1, and our total_depth counter is at 3. The size of the1647 * error is always one less than the size of the cycle we1648 * broke. Commits C and D were "lost" from A's chain.1649 *1650 * If we instead cut D->B, then the depth of A is correct at 3.1651 * We keep all commits in the chain that we examined.1652 */1653 cur->dfs_state = DFS_ACTIVE;1654if(cur->delta->dfs_state == DFS_ACTIVE) {1655drop_reused_delta(cur);1656 cur->dfs_state = DFS_DONE;1657break;1658}1659}16601661/*1662 * And now that we've gone all the way to the bottom of the chain, we1663 * need to clear the active flags and set the depth fields as1664 * appropriate. Unlike the loop above, which can quit when it drops a1665 * delta, we need to keep going to look for more depth cuts. So we need1666 * an extra "next" pointer to keep going after we reset cur->delta.1667 */1668for(cur = entry; cur; cur = next) {1669 next = cur->delta;16701671/*1672 * We should have a chain of zero or more ACTIVE states down to1673 * a final DONE. We can quit after the DONE, because either it1674 * has no bases, or we've already handled them in a previous1675 * call.1676 */1677if(cur->dfs_state == DFS_DONE)1678break;1679else if(cur->dfs_state != DFS_ACTIVE)1680die("BUG: confusing delta dfs state in second pass:%d",1681 cur->dfs_state);16821683/*1684 * If the total_depth is more than depth, then we need to snip1685 * the chain into two or more smaller chains that don't exceed1686 * the maximum depth. Most of the resulting chains will contain1687 * (depth + 1) entries (i.e., depth deltas plus one base), and1688 * the last chain (i.e., the one containing entry) will contain1689 * whatever entries are left over, namely1690 * (total_depth % (depth + 1)) of them.1691 *1692 * Since we are iterating towards decreasing depth, we need to1693 * decrement total_depth as we go, and we need to write to the1694 * entry what its final depth will be after all of the1695 * snipping. Since we're snipping into chains of length (depth1696 * + 1) entries, the final depth of an entry will be its1697 * original depth modulo (depth + 1). Any time we encounter an1698 * entry whose final depth is supposed to be zero, we snip it1699 * from its delta base, thereby making it so.1700 */1701 cur->depth = (total_depth--) % (depth +1);1702if(!cur->depth)1703drop_reused_delta(cur);17041705 cur->dfs_state = DFS_DONE;1706}1707}17081709static voidget_object_details(void)1710{1711uint32_t i;1712struct object_entry **sorted_by_offset;17131714 sorted_by_offset =xcalloc(to_pack.nr_objects,sizeof(struct object_entry *));1715for(i =0; i < to_pack.nr_objects; i++)1716 sorted_by_offset[i] = to_pack.objects + i;1717QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);17181719for(i =0; i < to_pack.nr_objects; i++) {1720struct object_entry *entry = sorted_by_offset[i];1721check_object(entry);1722if(big_file_threshold < entry->size)1723 entry->no_try_delta =1;1724}17251726/*1727 * This must happen in a second pass, since we rely on the delta1728 * information for the whole list being completed.1729 */1730for(i =0; i < to_pack.nr_objects; i++)1731break_delta_chains(&to_pack.objects[i]);17321733free(sorted_by_offset);1734}17351736/*1737 * We search for deltas in a list sorted by type, by filename hash, and then1738 * by size, so that we see progressively smaller and smaller files.1739 * That's because we prefer deltas to be from the bigger file1740 * to the smaller -- deletes are potentially cheaper, but perhaps1741 * more importantly, the bigger file is likely the more recent1742 * one. The deepest deltas are therefore the oldest objects which are1743 * less susceptible to be accessed often.1744 */1745static inttype_size_sort(const void*_a,const void*_b)1746{1747const struct object_entry *a = *(struct object_entry **)_a;1748const struct object_entry *b = *(struct object_entry **)_b;17491750if(a->type > b->type)1751return-1;1752if(a->type < b->type)1753return1;1754if(a->hash > b->hash)1755return-1;1756if(a->hash < b->hash)1757return1;1758if(a->preferred_base > b->preferred_base)1759return-1;1760if(a->preferred_base < b->preferred_base)1761return1;1762if(a->size > b->size)1763return-1;1764if(a->size < b->size)1765return1;1766return a < b ? -1: (a > b);/* newest first */1767}17681769struct unpacked {1770struct object_entry *entry;1771void*data;1772struct delta_index *index;1773unsigned depth;1774};17751776static intdelta_cacheable(unsigned long src_size,unsigned long trg_size,1777unsigned long delta_size)1778{1779if(max_delta_cache_size && delta_cache_size + delta_size > max_delta_cache_size)1780return0;17811782if(delta_size < cache_max_small_delta_size)1783return1;17841785/* cache delta, if objects are large enough compared to delta size */1786if((src_size >>20) + (trg_size >>21) > (delta_size >>10))1787return1;17881789return0;1790}17911792#ifndef NO_PTHREADS17931794static pthread_mutex_t read_mutex;1795#define read_lock() pthread_mutex_lock(&read_mutex)1796#define read_unlock() pthread_mutex_unlock(&read_mutex)17971798static pthread_mutex_t cache_mutex;1799#define cache_lock() pthread_mutex_lock(&cache_mutex)1800#define cache_unlock() pthread_mutex_unlock(&cache_mutex)18011802static pthread_mutex_t progress_mutex;1803#define progress_lock() pthread_mutex_lock(&progress_mutex)1804#define progress_unlock() pthread_mutex_unlock(&progress_mutex)18051806#else18071808#define read_lock() (void)01809#define read_unlock() (void)01810#define cache_lock() (void)01811#define cache_unlock() (void)01812#define progress_lock() (void)01813#define progress_unlock() (void)018141815#endif18161817static inttry_delta(struct unpacked *trg,struct unpacked *src,1818unsigned max_depth,unsigned long*mem_usage)1819{1820struct object_entry *trg_entry = trg->entry;1821struct object_entry *src_entry = src->entry;1822unsigned long trg_size, src_size, delta_size, sizediff, max_size, sz;1823unsigned ref_depth;1824enum object_type type;1825void*delta_buf;18261827/* Don't bother doing diffs between different types */1828if(trg_entry->type != src_entry->type)1829return-1;18301831/*1832 * We do not bother to try a delta that we discarded on an1833 * earlier try, but only when reusing delta data. Note that1834 * src_entry that is marked as the preferred_base should always1835 * be considered, as even if we produce a suboptimal delta against1836 * it, we will still save the transfer cost, as we already know1837 * the other side has it and we won't send src_entry at all.1838 */1839if(reuse_delta && trg_entry->in_pack &&1840 trg_entry->in_pack == src_entry->in_pack &&1841!src_entry->preferred_base &&1842 trg_entry->in_pack_type != OBJ_REF_DELTA &&1843 trg_entry->in_pack_type != OBJ_OFS_DELTA)1844return0;18451846/* Let's not bust the allowed depth. */1847if(src->depth >= max_depth)1848return0;18491850/* Now some size filtering heuristics. */1851 trg_size = trg_entry->size;1852if(!trg_entry->delta) {1853 max_size = trg_size/2-20;1854 ref_depth =1;1855}else{1856 max_size = trg_entry->delta_size;1857 ref_depth = trg->depth;1858}1859 max_size = (uint64_t)max_size * (max_depth - src->depth) /1860(max_depth - ref_depth +1);1861if(max_size ==0)1862return0;1863 src_size = src_entry->size;1864 sizediff = src_size < trg_size ? trg_size - src_size :0;1865if(sizediff >= max_size)1866return0;1867if(trg_size < src_size /32)1868return0;18691870/* Load data if not already done */1871if(!trg->data) {1872read_lock();1873 trg->data =read_object_file(&trg_entry->idx.oid, &type, &sz);1874read_unlock();1875if(!trg->data)1876die("object%scannot be read",1877oid_to_hex(&trg_entry->idx.oid));1878if(sz != trg_size)1879die("object%sinconsistent object length (%lu vs%lu)",1880oid_to_hex(&trg_entry->idx.oid), sz,1881 trg_size);1882*mem_usage += sz;1883}1884if(!src->data) {1885read_lock();1886 src->data =read_object_file(&src_entry->idx.oid, &type, &sz);1887read_unlock();1888if(!src->data) {1889if(src_entry->preferred_base) {1890static int warned =0;1891if(!warned++)1892warning("object%scannot be read",1893oid_to_hex(&src_entry->idx.oid));1894/*1895 * Those objects are not included in the1896 * resulting pack. Be resilient and ignore1897 * them if they can't be read, in case the1898 * pack could be created nevertheless.1899 */1900return0;1901}1902die("object%scannot be read",1903oid_to_hex(&src_entry->idx.oid));1904}1905if(sz != src_size)1906die("object%sinconsistent object length (%lu vs%lu)",1907oid_to_hex(&src_entry->idx.oid), sz,1908 src_size);1909*mem_usage += sz;1910}1911if(!src->index) {1912 src->index =create_delta_index(src->data, src_size);1913if(!src->index) {1914static int warned =0;1915if(!warned++)1916warning("suboptimal pack - out of memory");1917return0;1918}1919*mem_usage +=sizeof_delta_index(src->index);1920}19211922 delta_buf =create_delta(src->index, trg->data, trg_size, &delta_size, max_size);1923if(!delta_buf)1924return0;19251926if(trg_entry->delta) {1927/* Prefer only shallower same-sized deltas. */1928if(delta_size == trg_entry->delta_size &&1929 src->depth +1>= trg->depth) {1930free(delta_buf);1931return0;1932}1933}19341935/*1936 * Handle memory allocation outside of the cache1937 * accounting lock. Compiler will optimize the strangeness1938 * away when NO_PTHREADS is defined.1939 */1940free(trg_entry->delta_data);1941cache_lock();1942if(trg_entry->delta_data) {1943 delta_cache_size -= trg_entry->delta_size;1944 trg_entry->delta_data = NULL;1945}1946if(delta_cacheable(src_size, trg_size, delta_size)) {1947 delta_cache_size += delta_size;1948cache_unlock();1949 trg_entry->delta_data =xrealloc(delta_buf, delta_size);1950}else{1951cache_unlock();1952free(delta_buf);1953}19541955 trg_entry->delta = src_entry;1956 trg_entry->delta_size = delta_size;1957 trg->depth = src->depth +1;19581959return1;1960}19611962static unsigned intcheck_delta_limit(struct object_entry *me,unsigned int n)1963{1964struct object_entry *child = me->delta_child;1965unsigned int m = n;1966while(child) {1967unsigned int c =check_delta_limit(child, n +1);1968if(m < c)1969 m = c;1970 child = child->delta_sibling;1971}1972return m;1973}19741975static unsigned longfree_unpacked(struct unpacked *n)1976{1977unsigned long freed_mem =sizeof_delta_index(n->index);1978free_delta_index(n->index);1979 n->index = NULL;1980if(n->data) {1981 freed_mem += n->entry->size;1982FREE_AND_NULL(n->data);1983}1984 n->entry = NULL;1985 n->depth =0;1986return freed_mem;1987}19881989static voidfind_deltas(struct object_entry **list,unsigned*list_size,1990int window,int depth,unsigned*processed)1991{1992uint32_t i, idx =0, count =0;1993struct unpacked *array;1994unsigned long mem_usage =0;19951996 array =xcalloc(window,sizeof(struct unpacked));19971998for(;;) {1999struct object_entry *entry;2000struct unpacked *n = array + idx;2001int j, max_depth, best_base = -1;20022003progress_lock();2004if(!*list_size) {2005progress_unlock();2006break;2007}2008 entry = *list++;2009(*list_size)--;2010if(!entry->preferred_base) {2011(*processed)++;2012display_progress(progress_state, *processed);2013}2014progress_unlock();20152016 mem_usage -=free_unpacked(n);2017 n->entry = entry;20182019while(window_memory_limit &&2020 mem_usage > window_memory_limit &&2021 count >1) {2022uint32_t tail = (idx + window - count) % window;2023 mem_usage -=free_unpacked(array + tail);2024 count--;2025}20262027/* We do not compute delta to *create* objects we are not2028 * going to pack.2029 */2030if(entry->preferred_base)2031goto next;20322033/*2034 * If the current object is at pack edge, take the depth the2035 * objects that depend on the current object into account2036 * otherwise they would become too deep.2037 */2038 max_depth = depth;2039if(entry->delta_child) {2040 max_depth -=check_delta_limit(entry,0);2041if(max_depth <=0)2042goto next;2043}20442045 j = window;2046while(--j >0) {2047int ret;2048uint32_t other_idx = idx + j;2049struct unpacked *m;2050if(other_idx >= window)2051 other_idx -= window;2052 m = array + other_idx;2053if(!m->entry)2054break;2055 ret =try_delta(n, m, max_depth, &mem_usage);2056if(ret <0)2057break;2058else if(ret >0)2059 best_base = other_idx;2060}20612062/*2063 * If we decided to cache the delta data, then it is best2064 * to compress it right away. First because we have to do2065 * it anyway, and doing it here while we're threaded will2066 * save a lot of time in the non threaded write phase,2067 * as well as allow for caching more deltas within2068 * the same cache size limit.2069 * ...2070 * But only if not writing to stdout, since in that case2071 * the network is most likely throttling writes anyway,2072 * and therefore it is best to go to the write phase ASAP2073 * instead, as we can afford spending more time compressing2074 * between writes at that moment.2075 */2076if(entry->delta_data && !pack_to_stdout) {2077 entry->z_delta_size =do_compress(&entry->delta_data,2078 entry->delta_size);2079cache_lock();2080 delta_cache_size -= entry->delta_size;2081 delta_cache_size += entry->z_delta_size;2082cache_unlock();2083}20842085/* if we made n a delta, and if n is already at max2086 * depth, leaving it in the window is pointless. we2087 * should evict it first.2088 */2089if(entry->delta && max_depth <= n->depth)2090continue;20912092/*2093 * Move the best delta base up in the window, after the2094 * currently deltified object, to keep it longer. It will2095 * be the first base object to be attempted next.2096 */2097if(entry->delta) {2098struct unpacked swap = array[best_base];2099int dist = (window + idx - best_base) % window;2100int dst = best_base;2101while(dist--) {2102int src = (dst +1) % window;2103 array[dst] = array[src];2104 dst = src;2105}2106 array[dst] = swap;2107}21082109 next:2110 idx++;2111if(count +1< window)2112 count++;2113if(idx >= window)2114 idx =0;2115}21162117for(i =0; i < window; ++i) {2118free_delta_index(array[i].index);2119free(array[i].data);2120}2121free(array);2122}21232124#ifndef NO_PTHREADS21252126static voidtry_to_free_from_threads(size_t size)2127{2128read_lock();2129release_pack_memory(size);2130read_unlock();2131}21322133static try_to_free_t old_try_to_free_routine;21342135/*2136 * The main thread waits on the condition that (at least) one of the workers2137 * has stopped working (which is indicated in the .working member of2138 * struct thread_params).2139 * When a work thread has completed its work, it sets .working to 0 and2140 * signals the main thread and waits on the condition that .data_ready2141 * becomes 1.2142 */21432144struct thread_params {2145 pthread_t thread;2146struct object_entry **list;2147unsigned list_size;2148unsigned remaining;2149int window;2150int depth;2151int working;2152int data_ready;2153 pthread_mutex_t mutex;2154 pthread_cond_t cond;2155unsigned*processed;2156};21572158static pthread_cond_t progress_cond;21592160/*2161 * Mutex and conditional variable can't be statically-initialized on Windows.2162 */2163static voidinit_threaded_search(void)2164{2165init_recursive_mutex(&read_mutex);2166pthread_mutex_init(&cache_mutex, NULL);2167pthread_mutex_init(&progress_mutex, NULL);2168pthread_cond_init(&progress_cond, NULL);2169 old_try_to_free_routine =set_try_to_free_routine(try_to_free_from_threads);2170}21712172static voidcleanup_threaded_search(void)2173{2174set_try_to_free_routine(old_try_to_free_routine);2175pthread_cond_destroy(&progress_cond);2176pthread_mutex_destroy(&read_mutex);2177pthread_mutex_destroy(&cache_mutex);2178pthread_mutex_destroy(&progress_mutex);2179}21802181static void*threaded_find_deltas(void*arg)2182{2183struct thread_params *me = arg;21842185progress_lock();2186while(me->remaining) {2187progress_unlock();21882189find_deltas(me->list, &me->remaining,2190 me->window, me->depth, me->processed);21912192progress_lock();2193 me->working =0;2194pthread_cond_signal(&progress_cond);2195progress_unlock();21962197/*2198 * We must not set ->data_ready before we wait on the2199 * condition because the main thread may have set it to 12200 * before we get here. In order to be sure that new2201 * work is available if we see 1 in ->data_ready, it2202 * was initialized to 0 before this thread was spawned2203 * and we reset it to 0 right away.2204 */2205pthread_mutex_lock(&me->mutex);2206while(!me->data_ready)2207pthread_cond_wait(&me->cond, &me->mutex);2208 me->data_ready =0;2209pthread_mutex_unlock(&me->mutex);22102211progress_lock();2212}2213progress_unlock();2214/* leave ->working 1 so that this doesn't get more work assigned */2215return NULL;2216}22172218static voidll_find_deltas(struct object_entry **list,unsigned list_size,2219int window,int depth,unsigned*processed)2220{2221struct thread_params *p;2222int i, ret, active_threads =0;22232224init_threaded_search();22252226if(delta_search_threads <=1) {2227find_deltas(list, &list_size, window, depth, processed);2228cleanup_threaded_search();2229return;2230}2231if(progress > pack_to_stdout)2232fprintf(stderr,"Delta compression using up to%dthreads.\n",2233 delta_search_threads);2234 p =xcalloc(delta_search_threads,sizeof(*p));22352236/* Partition the work amongst work threads. */2237for(i =0; i < delta_search_threads; i++) {2238unsigned sub_size = list_size / (delta_search_threads - i);22392240/* don't use too small segments or no deltas will be found */2241if(sub_size <2*window && i+1< delta_search_threads)2242 sub_size =0;22432244 p[i].window = window;2245 p[i].depth = depth;2246 p[i].processed = processed;2247 p[i].working =1;2248 p[i].data_ready =0;22492250/* try to split chunks on "path" boundaries */2251while(sub_size && sub_size < list_size &&2252 list[sub_size]->hash &&2253 list[sub_size]->hash == list[sub_size-1]->hash)2254 sub_size++;22552256 p[i].list = list;2257 p[i].list_size = sub_size;2258 p[i].remaining = sub_size;22592260 list += sub_size;2261 list_size -= sub_size;2262}22632264/* Start work threads. */2265for(i =0; i < delta_search_threads; i++) {2266if(!p[i].list_size)2267continue;2268pthread_mutex_init(&p[i].mutex, NULL);2269pthread_cond_init(&p[i].cond, NULL);2270 ret =pthread_create(&p[i].thread, NULL,2271 threaded_find_deltas, &p[i]);2272if(ret)2273die("unable to create thread:%s",strerror(ret));2274 active_threads++;2275}22762277/*2278 * Now let's wait for work completion. Each time a thread is done2279 * with its work, we steal half of the remaining work from the2280 * thread with the largest number of unprocessed objects and give2281 * it to that newly idle thread. This ensure good load balancing2282 * until the remaining object list segments are simply too short2283 * to be worth splitting anymore.2284 */2285while(active_threads) {2286struct thread_params *target = NULL;2287struct thread_params *victim = NULL;2288unsigned sub_size =0;22892290progress_lock();2291for(;;) {2292for(i =0; !target && i < delta_search_threads; i++)2293if(!p[i].working)2294 target = &p[i];2295if(target)2296break;2297pthread_cond_wait(&progress_cond, &progress_mutex);2298}22992300for(i =0; i < delta_search_threads; i++)2301if(p[i].remaining >2*window &&2302(!victim || victim->remaining < p[i].remaining))2303 victim = &p[i];2304if(victim) {2305 sub_size = victim->remaining /2;2306 list = victim->list + victim->list_size - sub_size;2307while(sub_size && list[0]->hash &&2308 list[0]->hash == list[-1]->hash) {2309 list++;2310 sub_size--;2311}2312if(!sub_size) {2313/*2314 * It is possible for some "paths" to have2315 * so many objects that no hash boundary2316 * might be found. Let's just steal the2317 * exact half in that case.2318 */2319 sub_size = victim->remaining /2;2320 list -= sub_size;2321}2322 target->list = list;2323 victim->list_size -= sub_size;2324 victim->remaining -= sub_size;2325}2326 target->list_size = sub_size;2327 target->remaining = sub_size;2328 target->working =1;2329progress_unlock();23302331pthread_mutex_lock(&target->mutex);2332 target->data_ready =1;2333pthread_cond_signal(&target->cond);2334pthread_mutex_unlock(&target->mutex);23352336if(!sub_size) {2337pthread_join(target->thread, NULL);2338pthread_cond_destroy(&target->cond);2339pthread_mutex_destroy(&target->mutex);2340 active_threads--;2341}2342}2343cleanup_threaded_search();2344free(p);2345}23462347#else2348#define ll_find_deltas(l, s, w, d, p) find_deltas(l, &s, w, d, p)2349#endif23502351static voidadd_tag_chain(const struct object_id *oid)2352{2353struct tag *tag;23542355/*2356 * We catch duplicates already in add_object_entry(), but we'd2357 * prefer to do this extra check to avoid having to parse the2358 * tag at all if we already know that it's being packed (e.g., if2359 * it was included via bitmaps, we would not have parsed it2360 * previously).2361 */2362if(packlist_find(&to_pack, oid->hash, NULL))2363return;23642365 tag =lookup_tag(oid);2366while(1) {2367if(!tag ||parse_tag(tag) || !tag->tagged)2368die("unable to pack objects reachable from tag%s",2369oid_to_hex(oid));23702371add_object_entry(&tag->object.oid, OBJ_TAG, NULL,0);23722373if(tag->tagged->type != OBJ_TAG)2374return;23752376 tag = (struct tag *)tag->tagged;2377}2378}23792380static intadd_ref_tag(const char*path,const struct object_id *oid,int flag,void*cb_data)2381{2382struct object_id peeled;23832384if(starts_with(path,"refs/tags/") &&/* is a tag? */2385!peel_ref(path, &peeled) &&/* peelable? */2386packlist_find(&to_pack, peeled.hash, NULL))/* object packed? */2387add_tag_chain(oid);2388return0;2389}23902391static voidprepare_pack(int window,int depth)2392{2393struct object_entry **delta_list;2394uint32_t i, nr_deltas;2395unsigned n;23962397get_object_details();23982399/*2400 * If we're locally repacking then we need to be doubly careful2401 * from now on in order to make sure no stealth corruption gets2402 * propagated to the new pack. Clients receiving streamed packs2403 * should validate everything they get anyway so no need to incur2404 * the additional cost here in that case.2405 */2406if(!pack_to_stdout)2407 do_check_packed_object_crc =1;24082409if(!to_pack.nr_objects || !window || !depth)2410return;24112412ALLOC_ARRAY(delta_list, to_pack.nr_objects);2413 nr_deltas = n =0;24142415for(i =0; i < to_pack.nr_objects; i++) {2416struct object_entry *entry = to_pack.objects + i;24172418if(entry->delta)2419/* This happens if we decided to reuse existing2420 * delta from a pack. "reuse_delta &&" is implied.2421 */2422continue;24232424if(entry->size <50)2425continue;24262427if(entry->no_try_delta)2428continue;24292430if(!entry->preferred_base) {2431 nr_deltas++;2432if(entry->type <0)2433die("unable to get type of object%s",2434oid_to_hex(&entry->idx.oid));2435}else{2436if(entry->type <0) {2437/*2438 * This object is not found, but we2439 * don't have to include it anyway.2440 */2441continue;2442}2443}24442445 delta_list[n++] = entry;2446}24472448if(nr_deltas && n >1) {2449unsigned nr_done =0;2450if(progress)2451 progress_state =start_progress(_("Compressing objects"),2452 nr_deltas);2453QSORT(delta_list, n, type_size_sort);2454ll_find_deltas(delta_list, n, window+1, depth, &nr_done);2455stop_progress(&progress_state);2456if(nr_done != nr_deltas)2457die("inconsistency with delta count");2458}2459free(delta_list);2460}24612462static intgit_pack_config(const char*k,const char*v,void*cb)2463{2464if(!strcmp(k,"pack.window")) {2465 window =git_config_int(k, v);2466return0;2467}2468if(!strcmp(k,"pack.windowmemory")) {2469 window_memory_limit =git_config_ulong(k, v);2470return0;2471}2472if(!strcmp(k,"pack.depth")) {2473 depth =git_config_int(k, v);2474return0;2475}2476if(!strcmp(k,"pack.deltacachesize")) {2477 max_delta_cache_size =git_config_int(k, v);2478return0;2479}2480if(!strcmp(k,"pack.deltacachelimit")) {2481 cache_max_small_delta_size =git_config_int(k, v);2482return0;2483}2484if(!strcmp(k,"pack.writebitmaphashcache")) {2485if(git_config_bool(k, v))2486 write_bitmap_options |= BITMAP_OPT_HASH_CACHE;2487else2488 write_bitmap_options &= ~BITMAP_OPT_HASH_CACHE;2489}2490if(!strcmp(k,"pack.usebitmaps")) {2491 use_bitmap_index_default =git_config_bool(k, v);2492return0;2493}2494if(!strcmp(k,"pack.threads")) {2495 delta_search_threads =git_config_int(k, v);2496if(delta_search_threads <0)2497die("invalid number of threads specified (%d)",2498 delta_search_threads);2499#ifdef NO_PTHREADS2500if(delta_search_threads !=1) {2501warning("no threads support, ignoring%s", k);2502 delta_search_threads =0;2503}2504#endif2505return0;2506}2507if(!strcmp(k,"pack.indexversion")) {2508 pack_idx_opts.version =git_config_int(k, v);2509if(pack_idx_opts.version >2)2510die("bad pack.indexversion=%"PRIu32,2511 pack_idx_opts.version);2512return0;2513}2514returngit_default_config(k, v, cb);2515}25162517static voidread_object_list_from_stdin(void)2518{2519char line[GIT_MAX_HEXSZ +1+ PATH_MAX +2];2520struct object_id oid;2521const char*p;25222523for(;;) {2524if(!fgets(line,sizeof(line), stdin)) {2525if(feof(stdin))2526break;2527if(!ferror(stdin))2528die("fgets returned NULL, not EOF, not error!");2529if(errno != EINTR)2530die_errno("fgets");2531clearerr(stdin);2532continue;2533}2534if(line[0] =='-') {2535if(get_oid_hex(line+1, &oid))2536die("expected edge object ID, got garbage:\n%s",2537 line);2538add_preferred_base(&oid);2539continue;2540}2541if(parse_oid_hex(line, &oid, &p))2542die("expected object ID, got garbage:\n%s", line);25432544add_preferred_base_object(p +1);2545add_object_entry(&oid,0, p +1,0);2546}2547}25482549/* Remember to update object flag allocation in object.h */2550#define OBJECT_ADDED (1u<<20)25512552static voidshow_commit(struct commit *commit,void*data)2553{2554add_object_entry(&commit->object.oid, OBJ_COMMIT, NULL,0);2555 commit->object.flags |= OBJECT_ADDED;25562557if(write_bitmap_index)2558index_commit_for_bitmap(commit);2559}25602561static voidshow_object(struct object *obj,const char*name,void*data)2562{2563add_preferred_base_object(name);2564add_object_entry(&obj->oid, obj->type, name,0);2565 obj->flags |= OBJECT_ADDED;2566}25672568static voidshow_object__ma_allow_any(struct object *obj,const char*name,void*data)2569{2570assert(arg_missing_action == MA_ALLOW_ANY);25712572/*2573 * Quietly ignore ALL missing objects. This avoids problems with2574 * staging them now and getting an odd error later.2575 */2576if(!has_object_file(&obj->oid))2577return;25782579show_object(obj, name, data);2580}25812582static voidshow_object__ma_allow_promisor(struct object *obj,const char*name,void*data)2583{2584assert(arg_missing_action == MA_ALLOW_PROMISOR);25852586/*2587 * Quietly ignore EXPECTED missing objects. This avoids problems with2588 * staging them now and getting an odd error later.2589 */2590if(!has_object_file(&obj->oid) &&is_promisor_object(&obj->oid))2591return;25922593show_object(obj, name, data);2594}25952596static intoption_parse_missing_action(const struct option *opt,2597const char*arg,int unset)2598{2599assert(arg);2600assert(!unset);26012602if(!strcmp(arg,"error")) {2603 arg_missing_action = MA_ERROR;2604 fn_show_object = show_object;2605return0;2606}26072608if(!strcmp(arg,"allow-any")) {2609 arg_missing_action = MA_ALLOW_ANY;2610 fetch_if_missing =0;2611 fn_show_object = show_object__ma_allow_any;2612return0;2613}26142615if(!strcmp(arg,"allow-promisor")) {2616 arg_missing_action = MA_ALLOW_PROMISOR;2617 fetch_if_missing =0;2618 fn_show_object = show_object__ma_allow_promisor;2619return0;2620}26212622die(_("invalid value for --missing"));2623return0;2624}26252626static voidshow_edge(struct commit *commit)2627{2628add_preferred_base(&commit->object.oid);2629}26302631struct in_pack_object {2632 off_t offset;2633struct object *object;2634};26352636struct in_pack {2637unsigned int alloc;2638unsigned int nr;2639struct in_pack_object *array;2640};26412642static voidmark_in_pack_object(struct object *object,struct packed_git *p,struct in_pack *in_pack)2643{2644 in_pack->array[in_pack->nr].offset =find_pack_entry_one(object->oid.hash, p);2645 in_pack->array[in_pack->nr].object = object;2646 in_pack->nr++;2647}26482649/*2650 * Compare the objects in the offset order, in order to emulate the2651 * "git rev-list --objects" output that produced the pack originally.2652 */2653static intofscmp(const void*a_,const void*b_)2654{2655struct in_pack_object *a = (struct in_pack_object *)a_;2656struct in_pack_object *b = (struct in_pack_object *)b_;26572658if(a->offset < b->offset)2659return-1;2660else if(a->offset > b->offset)2661return1;2662else2663returnoidcmp(&a->object->oid, &b->object->oid);2664}26652666static voidadd_objects_in_unpacked_packs(struct rev_info *revs)2667{2668struct packed_git *p;2669struct in_pack in_pack;2670uint32_t i;26712672memset(&in_pack,0,sizeof(in_pack));26732674for(p =get_packed_git(the_repository); p; p = p->next) {2675struct object_id oid;2676struct object *o;26772678if(!p->pack_local || p->pack_keep)2679continue;2680if(open_pack_index(p))2681die("cannot open pack index");26822683ALLOC_GROW(in_pack.array,2684 in_pack.nr + p->num_objects,2685 in_pack.alloc);26862687for(i =0; i < p->num_objects; i++) {2688nth_packed_object_oid(&oid, p, i);2689 o =lookup_unknown_object(oid.hash);2690if(!(o->flags & OBJECT_ADDED))2691mark_in_pack_object(o, p, &in_pack);2692 o->flags |= OBJECT_ADDED;2693}2694}26952696if(in_pack.nr) {2697QSORT(in_pack.array, in_pack.nr, ofscmp);2698for(i =0; i < in_pack.nr; i++) {2699struct object *o = in_pack.array[i].object;2700add_object_entry(&o->oid, o->type,"",0);2701}2702}2703free(in_pack.array);2704}27052706static intadd_loose_object(const struct object_id *oid,const char*path,2707void*data)2708{2709enum object_type type =oid_object_info(oid, NULL);27102711if(type <0) {2712warning("loose object at%scould not be examined", path);2713return0;2714}27152716add_object_entry(oid, type,"",0);2717return0;2718}27192720/*2721 * We actually don't even have to worry about reachability here.2722 * add_object_entry will weed out duplicates, so we just add every2723 * loose object we find.2724 */2725static voidadd_unreachable_loose_objects(void)2726{2727for_each_loose_file_in_objdir(get_object_directory(),2728 add_loose_object,2729 NULL, NULL, NULL);2730}27312732static inthas_sha1_pack_kept_or_nonlocal(const struct object_id *oid)2733{2734static struct packed_git *last_found = (void*)1;2735struct packed_git *p;27362737 p = (last_found != (void*)1) ? last_found :2738get_packed_git(the_repository);27392740while(p) {2741if((!p->pack_local || p->pack_keep) &&2742find_pack_entry_one(oid->hash, p)) {2743 last_found = p;2744return1;2745}2746if(p == last_found)2747 p =get_packed_git(the_repository);2748else2749 p = p->next;2750if(p == last_found)2751 p = p->next;2752}2753return0;2754}27552756/*2757 * Store a list of sha1s that are should not be discarded2758 * because they are either written too recently, or are2759 * reachable from another object that was.2760 *2761 * This is filled by get_object_list.2762 */2763static struct oid_array recent_objects;27642765static intloosened_object_can_be_discarded(const struct object_id *oid,2766 timestamp_t mtime)2767{2768if(!unpack_unreachable_expiration)2769return0;2770if(mtime > unpack_unreachable_expiration)2771return0;2772if(oid_array_lookup(&recent_objects, oid) >=0)2773return0;2774return1;2775}27762777static voidloosen_unused_packed_objects(struct rev_info *revs)2778{2779struct packed_git *p;2780uint32_t i;2781struct object_id oid;27822783for(p =get_packed_git(the_repository); p; p = p->next) {2784if(!p->pack_local || p->pack_keep)2785continue;27862787if(open_pack_index(p))2788die("cannot open pack index");27892790for(i =0; i < p->num_objects; i++) {2791nth_packed_object_oid(&oid, p, i);2792if(!packlist_find(&to_pack, oid.hash, NULL) &&2793!has_sha1_pack_kept_or_nonlocal(&oid) &&2794!loosened_object_can_be_discarded(&oid, p->mtime))2795if(force_object_loose(&oid, p->mtime))2796die("unable to force loose object");2797}2798}2799}28002801/*2802 * This tracks any options which pack-reuse code expects to be on, or which a2803 * reader of the pack might not understand, and which would therefore prevent2804 * blind reuse of what we have on disk.2805 */2806static intpack_options_allow_reuse(void)2807{2808return pack_to_stdout &&2809 allow_ofs_delta &&2810!ignore_packed_keep &&2811(!local || !have_non_local_packs) &&2812!incremental;2813}28142815static intget_object_list_from_bitmap(struct rev_info *revs)2816{2817if(prepare_bitmap_walk(revs) <0)2818return-1;28192820if(pack_options_allow_reuse() &&2821!reuse_partial_packfile_from_bitmap(2822&reuse_packfile,2823&reuse_packfile_objects,2824&reuse_packfile_offset)) {2825assert(reuse_packfile_objects);2826 nr_result += reuse_packfile_objects;2827display_progress(progress_state, nr_result);2828}28292830traverse_bitmap_commit_list(&add_object_entry_from_bitmap);2831return0;2832}28332834static voidrecord_recent_object(struct object *obj,2835const char*name,2836void*data)2837{2838oid_array_append(&recent_objects, &obj->oid);2839}28402841static voidrecord_recent_commit(struct commit *commit,void*data)2842{2843oid_array_append(&recent_objects, &commit->object.oid);2844}28452846static voidget_object_list(int ac,const char**av)2847{2848struct rev_info revs;2849char line[1000];2850int flags =0;28512852init_revisions(&revs, NULL);2853 save_commit_buffer =0;2854setup_revisions(ac, av, &revs, NULL);28552856/* make sure shallows are read */2857is_repository_shallow();28582859while(fgets(line,sizeof(line), stdin) != NULL) {2860int len =strlen(line);2861if(len && line[len -1] =='\n')2862 line[--len] =0;2863if(!len)2864break;2865if(*line =='-') {2866if(!strcmp(line,"--not")) {2867 flags ^= UNINTERESTING;2868 write_bitmap_index =0;2869continue;2870}2871if(starts_with(line,"--shallow ")) {2872struct object_id oid;2873if(get_oid_hex(line +10, &oid))2874die("not an SHA-1 '%s'", line +10);2875register_shallow(&oid);2876 use_bitmap_index =0;2877continue;2878}2879die("not a rev '%s'", line);2880}2881if(handle_revision_arg(line, &revs, flags, REVARG_CANNOT_BE_FILENAME))2882die("bad revision '%s'", line);2883}28842885if(use_bitmap_index && !get_object_list_from_bitmap(&revs))2886return;28872888if(prepare_revision_walk(&revs))2889die("revision walk setup failed");2890mark_edges_uninteresting(&revs, show_edge);28912892if(!fn_show_object)2893 fn_show_object = show_object;2894traverse_commit_list_filtered(&filter_options, &revs,2895 show_commit, fn_show_object, NULL,2896 NULL);28972898if(unpack_unreachable_expiration) {2899 revs.ignore_missing_links =1;2900if(add_unseen_recent_objects_to_traversal(&revs,2901 unpack_unreachable_expiration))2902die("unable to add recent objects");2903if(prepare_revision_walk(&revs))2904die("revision walk setup failed");2905traverse_commit_list(&revs, record_recent_commit,2906 record_recent_object, NULL);2907}29082909if(keep_unreachable)2910add_objects_in_unpacked_packs(&revs);2911if(pack_loose_unreachable)2912add_unreachable_loose_objects();2913if(unpack_unreachable)2914loosen_unused_packed_objects(&revs);29152916oid_array_clear(&recent_objects);2917}29182919static intoption_parse_index_version(const struct option *opt,2920const char*arg,int unset)2921{2922char*c;2923const char*val = arg;2924 pack_idx_opts.version =strtoul(val, &c,10);2925if(pack_idx_opts.version >2)2926die(_("unsupported index version%s"), val);2927if(*c ==','&& c[1])2928 pack_idx_opts.off32_limit =strtoul(c+1, &c,0);2929if(*c || pack_idx_opts.off32_limit &0x80000000)2930die(_("bad index version '%s'"), val);2931return0;2932}29332934static intoption_parse_unpack_unreachable(const struct option *opt,2935const char*arg,int unset)2936{2937if(unset) {2938 unpack_unreachable =0;2939 unpack_unreachable_expiration =0;2940}2941else{2942 unpack_unreachable =1;2943if(arg)2944 unpack_unreachable_expiration =approxidate(arg);2945}2946return0;2947}29482949intcmd_pack_objects(int argc,const char**argv,const char*prefix)2950{2951int use_internal_rev_list =0;2952int thin =0;2953int shallow =0;2954int all_progress_implied =0;2955struct argv_array rp = ARGV_ARRAY_INIT;2956int rev_list_unpacked =0, rev_list_all =0, rev_list_reflog =0;2957int rev_list_index =0;2958struct option pack_objects_options[] = {2959OPT_SET_INT('q',"quiet", &progress,2960N_("do not show progress meter"),0),2961OPT_SET_INT(0,"progress", &progress,2962N_("show progress meter"),1),2963OPT_SET_INT(0,"all-progress", &progress,2964N_("show progress meter during object writing phase"),2),2965OPT_BOOL(0,"all-progress-implied",2966&all_progress_implied,2967N_("similar to --all-progress when progress meter is shown")),2968{ OPTION_CALLBACK,0,"index-version", NULL,N_("version[,offset]"),2969N_("write the pack index file in the specified idx format version"),29700, option_parse_index_version },2971OPT_MAGNITUDE(0,"max-pack-size", &pack_size_limit,2972N_("maximum size of each output pack file")),2973OPT_BOOL(0,"local", &local,2974N_("ignore borrowed objects from alternate object store")),2975OPT_BOOL(0,"incremental", &incremental,2976N_("ignore packed objects")),2977OPT_INTEGER(0,"window", &window,2978N_("limit pack window by objects")),2979OPT_MAGNITUDE(0,"window-memory", &window_memory_limit,2980N_("limit pack window by memory in addition to object limit")),2981OPT_INTEGER(0,"depth", &depth,2982N_("maximum length of delta chain allowed in the resulting pack")),2983OPT_BOOL(0,"reuse-delta", &reuse_delta,2984N_("reuse existing deltas")),2985OPT_BOOL(0,"reuse-object", &reuse_object,2986N_("reuse existing objects")),2987OPT_BOOL(0,"delta-base-offset", &allow_ofs_delta,2988N_("use OFS_DELTA objects")),2989OPT_INTEGER(0,"threads", &delta_search_threads,2990N_("use threads when searching for best delta matches")),2991OPT_BOOL(0,"non-empty", &non_empty,2992N_("do not create an empty pack output")),2993OPT_BOOL(0,"revs", &use_internal_rev_list,2994N_("read revision arguments from standard input")),2995{ OPTION_SET_INT,0,"unpacked", &rev_list_unpacked, NULL,2996N_("limit the objects to those that are not yet packed"),2997 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},2998{ OPTION_SET_INT,0,"all", &rev_list_all, NULL,2999N_("include objects reachable from any reference"),3000 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3001{ OPTION_SET_INT,0,"reflog", &rev_list_reflog, NULL,3002N_("include objects referred by reflog entries"),3003 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3004{ OPTION_SET_INT,0,"indexed-objects", &rev_list_index, NULL,3005N_("include objects referred to by the index"),3006 PARSE_OPT_NOARG | PARSE_OPT_NONEG, NULL,1},3007OPT_BOOL(0,"stdout", &pack_to_stdout,3008N_("output pack to stdout")),3009OPT_BOOL(0,"include-tag", &include_tag,3010N_("include tag objects that refer to objects to be packed")),3011OPT_BOOL(0,"keep-unreachable", &keep_unreachable,3012N_("keep unreachable objects")),3013OPT_BOOL(0,"pack-loose-unreachable", &pack_loose_unreachable,3014N_("pack loose unreachable objects")),3015{ OPTION_CALLBACK,0,"unpack-unreachable", NULL,N_("time"),3016N_("unpack unreachable objects newer than <time>"),3017 PARSE_OPT_OPTARG, option_parse_unpack_unreachable },3018OPT_BOOL(0,"thin", &thin,3019N_("create thin packs")),3020OPT_BOOL(0,"shallow", &shallow,3021N_("create packs suitable for shallow fetches")),3022OPT_BOOL(0,"honor-pack-keep", &ignore_packed_keep,3023N_("ignore packs that have companion .keep file")),3024OPT_INTEGER(0,"compression", &pack_compression_level,3025N_("pack compression level")),3026OPT_SET_INT(0,"keep-true-parents", &grafts_replace_parents,3027N_("do not hide commits by grafts"),0),3028OPT_BOOL(0,"use-bitmap-index", &use_bitmap_index,3029N_("use a bitmap index if available to speed up counting objects")),3030OPT_BOOL(0,"write-bitmap-index", &write_bitmap_index,3031N_("write a bitmap index together with the pack index")),3032OPT_PARSE_LIST_OBJECTS_FILTER(&filter_options),3033{ OPTION_CALLBACK,0,"missing", NULL,N_("action"),3034N_("handling for missing objects"), PARSE_OPT_NONEG,3035 option_parse_missing_action },3036OPT_BOOL(0,"exclude-promisor-objects", &exclude_promisor_objects,3037N_("do not pack objects in promisor packfiles")),3038OPT_END(),3039};30403041 check_replace_refs =0;30423043reset_pack_idx_option(&pack_idx_opts);3044git_config(git_pack_config, NULL);30453046 progress =isatty(2);3047 argc =parse_options(argc, argv, prefix, pack_objects_options,3048 pack_usage,0);30493050if(argc) {3051 base_name = argv[0];3052 argc--;3053}3054if(pack_to_stdout != !base_name || argc)3055usage_with_options(pack_usage, pack_objects_options);30563057argv_array_push(&rp,"pack-objects");3058if(thin) {3059 use_internal_rev_list =1;3060argv_array_push(&rp, shallow3061?"--objects-edge-aggressive"3062:"--objects-edge");3063}else3064argv_array_push(&rp,"--objects");30653066if(rev_list_all) {3067 use_internal_rev_list =1;3068argv_array_push(&rp,"--all");3069}3070if(rev_list_reflog) {3071 use_internal_rev_list =1;3072argv_array_push(&rp,"--reflog");3073}3074if(rev_list_index) {3075 use_internal_rev_list =1;3076argv_array_push(&rp,"--indexed-objects");3077}3078if(rev_list_unpacked) {3079 use_internal_rev_list =1;3080argv_array_push(&rp,"--unpacked");3081}30823083if(exclude_promisor_objects) {3084 use_internal_rev_list =1;3085 fetch_if_missing =0;3086argv_array_push(&rp,"--exclude-promisor-objects");3087}30883089if(!reuse_object)3090 reuse_delta =0;3091if(pack_compression_level == -1)3092 pack_compression_level = Z_DEFAULT_COMPRESSION;3093else if(pack_compression_level <0|| pack_compression_level > Z_BEST_COMPRESSION)3094die("bad pack compression level%d", pack_compression_level);30953096if(!delta_search_threads)/* --threads=0 means autodetect */3097 delta_search_threads =online_cpus();30983099#ifdef NO_PTHREADS3100if(delta_search_threads !=1)3101warning("no threads support, ignoring --threads");3102#endif3103if(!pack_to_stdout && !pack_size_limit)3104 pack_size_limit = pack_size_limit_cfg;3105if(pack_to_stdout && pack_size_limit)3106die("--max-pack-size cannot be used to build a pack for transfer.");3107if(pack_size_limit && pack_size_limit <1024*1024) {3108warning("minimum pack size limit is 1 MiB");3109 pack_size_limit =1024*1024;3110}31113112if(!pack_to_stdout && thin)3113die("--thin cannot be used to build an indexable pack.");31143115if(keep_unreachable && unpack_unreachable)3116die("--keep-unreachable and --unpack-unreachable are incompatible.");3117if(!rev_list_all || !rev_list_reflog || !rev_list_index)3118 unpack_unreachable_expiration =0;31193120if(filter_options.choice) {3121if(!pack_to_stdout)3122die("cannot use --filter without --stdout.");3123 use_bitmap_index =0;3124}31253126/*3127 * "soft" reasons not to use bitmaps - for on-disk repack by default we want3128 *3129 * - to produce good pack (with bitmap index not-yet-packed objects are3130 * packed in suboptimal order).3131 *3132 * - to use more robust pack-generation codepath (avoiding possible3133 * bugs in bitmap code and possible bitmap index corruption).3134 */3135if(!pack_to_stdout)3136 use_bitmap_index_default =0;31373138if(use_bitmap_index <0)3139 use_bitmap_index = use_bitmap_index_default;31403141/* "hard" reasons not to use bitmaps; these just won't work at all */3142if(!use_internal_rev_list || (!pack_to_stdout && write_bitmap_index) ||is_repository_shallow())3143 use_bitmap_index =0;31443145if(pack_to_stdout || !rev_list_all)3146 write_bitmap_index =0;31473148if(progress && all_progress_implied)3149 progress =2;31503151if(ignore_packed_keep) {3152struct packed_git *p;3153for(p =get_packed_git(the_repository); p; p = p->next)3154if(p->pack_local && p->pack_keep)3155break;3156if(!p)/* no keep-able packs found */3157 ignore_packed_keep =0;3158}3159if(local) {3160/*3161 * unlike ignore_packed_keep above, we do not want to3162 * unset "local" based on looking at packs, as it3163 * also covers non-local objects3164 */3165struct packed_git *p;3166for(p =get_packed_git(the_repository); p; p = p->next) {3167if(!p->pack_local) {3168 have_non_local_packs =1;3169break;3170}3171}3172}31733174if(progress)3175 progress_state =start_progress(_("Counting objects"),0);3176if(!use_internal_rev_list)3177read_object_list_from_stdin();3178else{3179get_object_list(rp.argc, rp.argv);3180argv_array_clear(&rp);3181}3182cleanup_preferred_base();3183if(include_tag && nr_result)3184for_each_ref(add_ref_tag, NULL);3185stop_progress(&progress_state);31863187if(non_empty && !nr_result)3188return0;3189if(nr_result)3190prepare_pack(window, depth);3191write_pack_file();3192if(progress)3193fprintf(stderr,"Total %"PRIu32" (delta %"PRIu32"),"3194" reused %"PRIu32" (delta %"PRIu32")\n",3195 written, written_delta, reused, reused_delta);3196return0;3197}