2 * Copyright (C) the libgit2 contributors. All rights reserved.
4 * This file is part of libgit2, distributed under the GNU GPL v2 with
5 * a Linking Exception. For full terms see the included COPYING file.
8 #include "git2/indexer.h"
9 #include "git2/object.h"
23 extern git_mutex git__mwindow_mutex
;
25 #define UINT31_MAX (0x7FFFFFFF)
35 unsigned int parsed_header
:1,
39 struct git_pack_header hdr
;
40 struct git_pack_file
*pack
;
43 git_off_t entry_start
;
44 git_packfile_stream stream
;
48 unsigned int fanout
[256];
49 git_hash_ctx hash_ctx
;
51 git_transfer_progress_cb progress_cb
;
52 void *progress_payload
;
55 /* Needed to look up objects which we want to inject to fix a thin pack */
58 /* Fields for calculating the packfile trailer (hash of everything before it) */
59 char inbuf
[GIT_OID_RAWSZ
];
68 const git_oid
*git_indexer_hash(const git_indexer
*idx
)
73 static int parse_header(struct git_pack_header
*hdr
, struct git_pack_file
*pack
)
78 if ((error
= p_mmap(&map
, sizeof(*hdr
), GIT_PROT_READ
, GIT_MAP_SHARED
, pack
->mwf
.fd
, 0)) < 0)
81 memcpy(hdr
, map
.data
, sizeof(*hdr
));
84 /* Verify we recognize this pack file format. */
85 if (hdr
->hdr_signature
!= ntohl(PACK_SIGNATURE
)) {
86 giterr_set(GITERR_INDEXER
, "Wrong pack signature");
90 if (!pack_version_ok(hdr
->hdr_version
)) {
91 giterr_set(GITERR_INDEXER
, "Wrong pack version");
98 static int objects_cmp(const void *a
, const void *b
)
100 const struct entry
*entrya
= a
;
101 const struct entry
*entryb
= b
;
103 return git_oid__cmp(&entrya
->oid
, &entryb
->oid
);
111 git_transfer_progress_cb progress_cb
,
112 void *progress_payload
)
115 git_buf path
= GIT_BUF_INIT
, tmp_path
= GIT_BUF_INIT
;
116 static const char suff
[] = "/pack";
119 idx
= git__calloc(1, sizeof(git_indexer
));
120 GITERR_CHECK_ALLOC(idx
);
122 idx
->progress_cb
= progress_cb
;
123 idx
->progress_payload
= progress_payload
;
124 idx
->mode
= mode
? mode
: GIT_PACK_FILE_MODE
;
125 git_hash_ctx_init(&idx
->hash_ctx
);
126 git_hash_ctx_init(&idx
->trailer
);
128 error
= git_buf_joinpath(&path
, prefix
, suff
);
132 fd
= git_futils_mktmp(&tmp_path
, git_buf_cstr(&path
), idx
->mode
);
137 error
= git_packfile_alloc(&idx
->pack
, git_buf_cstr(&tmp_path
));
138 git_buf_free(&tmp_path
);
143 idx
->pack
->mwf
.fd
= fd
;
144 if ((error
= git_mwindow_file_register(&idx
->pack
->mwf
)) < 0)
155 git_buf_free(&tmp_path
);
160 /* Try to store the delta so we can try to resolve it later */
161 static int store_delta(git_indexer
*idx
)
163 struct delta_info
*delta
;
165 delta
= git__calloc(1, sizeof(struct delta_info
));
166 GITERR_CHECK_ALLOC(delta
);
167 delta
->delta_off
= idx
->entry_start
;
169 if (git_vector_insert(&idx
->deltas
, delta
) < 0)
175 static void hash_header(git_hash_ctx
*ctx
, git_off_t len
, git_otype type
)
180 hdrlen
= git_odb__format_object_header(buffer
, sizeof(buffer
), (size_t)len
, type
);
181 git_hash_update(ctx
, buffer
, hdrlen
);
184 static int hash_object_stream(git_indexer
*idx
, git_packfile_stream
*stream
)
188 assert(idx
&& stream
);
191 if ((read
= git_packfile_stream_read(stream
, idx
->objbuf
, sizeof(idx
->objbuf
))) < 0)
194 git_hash_update(&idx
->hash_ctx
, idx
->objbuf
, read
);
203 /* In order to create the packfile stream, we need to skip over the delta base description */
204 static int advance_delta_offset(git_indexer
*idx
, git_otype type
)
206 git_mwindow
*w
= NULL
;
208 assert(type
== GIT_OBJ_REF_DELTA
|| type
== GIT_OBJ_OFS_DELTA
);
210 if (type
== GIT_OBJ_REF_DELTA
) {
211 idx
->off
+= GIT_OID_RAWSZ
;
213 git_off_t base_off
= get_delta_base(idx
->pack
, &w
, &idx
->off
, type
, idx
->entry_start
);
214 git_mwindow_close(&w
);
216 return (int)base_off
;
222 /* Read from the stream and discard any output */
223 static int read_object_stream(git_indexer
*idx
, git_packfile_stream
*stream
)
230 read
= git_packfile_stream_read(stream
, idx
->objbuf
, sizeof(idx
->objbuf
));
239 static int crc_object(uint32_t *crc_out
, git_mwindow_file
*mwf
, git_off_t start
, git_off_t size
)
243 unsigned int left
, len
;
244 git_mwindow
*w
= NULL
;
246 crc
= crc32(0L, Z_NULL
, 0);
248 ptr
= git_mwindow_open(mwf
, &w
, start
, (size_t)size
, &left
);
252 len
= min(left
, (unsigned int)size
);
253 crc
= crc32(crc
, ptr
, len
);
256 git_mwindow_close(&w
);
259 *crc_out
= htonl(crc
);
263 static int store_object(git_indexer
*idx
)
269 git_off_t entry_size
;
270 struct git_pack_entry
*pentry
;
271 git_off_t entry_start
= idx
->entry_start
;
273 entry
= git__calloc(1, sizeof(*entry
));
274 GITERR_CHECK_ALLOC(entry
);
276 pentry
= git__calloc(1, sizeof(struct git_pack_entry
));
277 GITERR_CHECK_ALLOC(pentry
);
279 git_hash_final(&oid
, &idx
->hash_ctx
);
280 entry_size
= idx
->off
- entry_start
;
281 if (entry_start
> UINT31_MAX
) {
282 entry
->offset
= UINT32_MAX
;
283 entry
->offset_long
= entry_start
;
285 entry
->offset
= (uint32_t)entry_start
;
288 git_oid_cpy(&pentry
->sha1
, &oid
);
289 pentry
->offset
= entry_start
;
291 k
= kh_put(oid
, idx
->pack
->idx_cache
, &pentry
->sha1
, &error
);
299 giterr_set(GITERR_INDEXER
, "duplicate object %s found in pack", git_oid_tostr_s(&pentry
->sha1
));
305 kh_value(idx
->pack
->idx_cache
, k
) = pentry
;
307 git_oid_cpy(&entry
->oid
, &oid
);
309 if (crc_object(&entry
->crc
, &idx
->pack
->mwf
, entry_start
, entry_size
) < 0)
312 /* Add the object to the list */
313 if (git_vector_insert(&idx
->objects
, entry
) < 0)
316 for (i
= oid
.id
[0]; i
< 256; ++i
) {
328 static int save_entry(git_indexer
*idx
, struct entry
*entry
, struct git_pack_entry
*pentry
, git_off_t entry_start
)
333 if (entry_start
> UINT31_MAX
) {
334 entry
->offset
= UINT32_MAX
;
335 entry
->offset_long
= entry_start
;
337 entry
->offset
= (uint32_t)entry_start
;
340 pentry
->offset
= entry_start
;
341 k
= kh_put(oid
, idx
->pack
->idx_cache
, &pentry
->sha1
, &error
);
345 kh_value(idx
->pack
->idx_cache
, k
) = pentry
;
347 /* Add the object to the list */
348 if (git_vector_insert(&idx
->objects
, entry
) < 0)
351 for (i
= entry
->oid
.id
[0]; i
< 256; ++i
) {
358 static int hash_and_save(git_indexer
*idx
, git_rawobj
*obj
, git_off_t entry_start
)
363 struct git_pack_entry
*pentry
= NULL
;
365 entry
= git__calloc(1, sizeof(*entry
));
366 GITERR_CHECK_ALLOC(entry
);
368 if (git_odb__hashobj(&oid
, obj
) < 0) {
369 giterr_set(GITERR_INDEXER
, "Failed to hash object");
373 pentry
= git__calloc(1, sizeof(struct git_pack_entry
));
374 GITERR_CHECK_ALLOC(pentry
);
376 git_oid_cpy(&pentry
->sha1
, &oid
);
377 git_oid_cpy(&entry
->oid
, &oid
);
378 entry
->crc
= crc32(0L, Z_NULL
, 0);
380 entry_size
= (size_t)(idx
->off
- entry_start
);
381 if (crc_object(&entry
->crc
, &idx
->pack
->mwf
, entry_start
, entry_size
) < 0)
384 return save_entry(idx
, entry
, pentry
, entry_start
);
389 git__free(obj
->data
);
393 static int do_progress_callback(git_indexer
*idx
, git_transfer_progress
*stats
)
395 if (idx
->progress_cb
)
396 return giterr_set_after_callback_function(
397 idx
->progress_cb(stats
, idx
->progress_payload
),
402 /* Hash everything but the last 20B of input */
403 static void hash_partially(git_indexer
*idx
, const uint8_t *data
, size_t size
)
405 size_t to_expell
, to_keep
;
410 /* Easy case, dump the buffer and the data minus the last 20 bytes */
411 if (size
>= GIT_OID_RAWSZ
) {
412 git_hash_update(&idx
->trailer
, idx
->inbuf
, idx
->inbuf_len
);
413 git_hash_update(&idx
->trailer
, data
, size
- GIT_OID_RAWSZ
);
415 data
+= size
- GIT_OID_RAWSZ
;
416 memcpy(idx
->inbuf
, data
, GIT_OID_RAWSZ
);
417 idx
->inbuf_len
= GIT_OID_RAWSZ
;
421 /* We can just append */
422 if (idx
->inbuf_len
+ size
<= GIT_OID_RAWSZ
) {
423 memcpy(idx
->inbuf
+ idx
->inbuf_len
, data
, size
);
424 idx
->inbuf_len
+= size
;
428 /* We need to partially drain the buffer and then append */
429 to_keep
= GIT_OID_RAWSZ
- size
;
430 to_expell
= idx
->inbuf_len
- to_keep
;
432 git_hash_update(&idx
->trailer
, idx
->inbuf
, to_expell
);
434 memmove(idx
->inbuf
, idx
->inbuf
+ to_expell
, to_keep
);
435 memcpy(idx
->inbuf
+ to_keep
, data
, size
);
436 idx
->inbuf_len
+= size
- to_expell
;
439 static int write_at(git_indexer
*idx
, const void *data
, git_off_t offset
, size_t size
)
441 git_file fd
= idx
->pack
->mwf
.fd
;
444 git_off_t page_start
;
445 unsigned char *map_data
;
449 assert(data
&& size
);
451 if ((error
= git__page_size(&page_size
)) < 0)
454 /* the offset needs to be at the beginning of the a page boundary */
455 page_offset
= offset
% page_size
;
456 page_start
= offset
- page_offset
;
458 if ((error
= p_mmap(&map
, page_offset
+ size
, GIT_PROT_WRITE
, GIT_MAP_SHARED
, fd
, page_start
)) < 0)
461 map_data
= (unsigned char *)map
.data
;
462 memcpy(map_data
+ page_offset
, data
, size
);
468 static int append_to_pack(git_indexer
*idx
, const void *data
, size_t size
)
470 git_off_t current_size
= idx
->pack
->mwf
.size
;
475 /* add the extra space we need at the end */
476 if (p_ftruncate(idx
->pack
->mwf
.fd
, current_size
+ size
) < 0) {
477 giterr_set(GITERR_OS
, "Failed to increase size of pack file '%s'", idx
->pack
->pack_name
);
481 return write_at(idx
, data
, idx
->pack
->mwf
.size
, size
);
484 int git_indexer_append(git_indexer
*idx
, const void *data
, size_t size
, git_transfer_progress
*stats
)
488 struct git_pack_header
*hdr
= &idx
->hdr
;
489 git_mwindow_file
*mwf
= &idx
->pack
->mwf
;
491 assert(idx
&& data
&& stats
);
493 processed
= stats
->indexed_objects
;
495 if ((error
= append_to_pack(idx
, data
, size
)) < 0)
498 hash_partially(idx
, data
, (int)size
);
500 /* Make sure we set the new size of the pack */
501 idx
->pack
->mwf
.size
+= size
;
503 if (!idx
->parsed_header
) {
504 unsigned int total_objects
;
506 if ((unsigned)idx
->pack
->mwf
.size
< sizeof(struct git_pack_header
))
509 if ((error
= parse_header(&idx
->hdr
, idx
->pack
)) < 0)
512 idx
->parsed_header
= 1;
513 idx
->nr_objects
= ntohl(hdr
->hdr_entries
);
514 idx
->off
= sizeof(struct git_pack_header
);
516 /* for now, limit to 2^32 objects */
517 assert(idx
->nr_objects
== (size_t)((unsigned int)idx
->nr_objects
));
518 if (idx
->nr_objects
== (size_t)((unsigned int)idx
->nr_objects
))
519 total_objects
= (unsigned int)idx
->nr_objects
;
521 total_objects
= UINT_MAX
;
523 idx
->pack
->idx_cache
= git_oidmap_alloc();
524 GITERR_CHECK_ALLOC(idx
->pack
->idx_cache
);
526 idx
->pack
->has_cache
= 1;
527 if (git_vector_init(&idx
->objects
, total_objects
, objects_cmp
) < 0)
530 if (git_vector_init(&idx
->deltas
, total_objects
/ 2, NULL
) < 0)
533 stats
->received_objects
= 0;
534 stats
->local_objects
= 0;
535 stats
->total_deltas
= 0;
536 stats
->indexed_deltas
= 0;
537 processed
= stats
->indexed_objects
= 0;
538 stats
->total_objects
= total_objects
;
540 if ((error
= do_progress_callback(idx
, stats
)) != 0)
544 /* Now that we have data in the pack, let's try to parse it */
546 /* As the file grows any windows we try to use will be out of date */
547 git_mwindow_free_all(mwf
);
549 while (processed
< idx
->nr_objects
) {
550 git_packfile_stream
*stream
= &idx
->stream
;
551 git_off_t entry_start
= idx
->off
;
554 git_mwindow
*w
= NULL
;
556 if (idx
->pack
->mwf
.size
<= idx
->off
+ 20)
559 if (!idx
->have_stream
) {
560 error
= git_packfile_unpack_header(&entry_size
, &type
, mwf
, &w
, &idx
->off
);
561 if (error
== GIT_EBUFS
) {
562 idx
->off
= entry_start
;
568 git_mwindow_close(&w
);
569 idx
->entry_start
= entry_start
;
570 git_hash_init(&idx
->hash_ctx
);
572 if (type
== GIT_OBJ_REF_DELTA
|| type
== GIT_OBJ_OFS_DELTA
) {
573 error
= advance_delta_offset(idx
, type
);
574 if (error
== GIT_EBUFS
) {
575 idx
->off
= entry_start
;
584 hash_header(&idx
->hash_ctx
, entry_size
, type
);
587 idx
->have_stream
= 1;
589 error
= git_packfile_stream_open(stream
, idx
->pack
, idx
->off
);
594 if (idx
->have_delta
) {
595 error
= read_object_stream(idx
, stream
);
597 error
= hash_object_stream(idx
, stream
);
600 idx
->off
= stream
->curpos
;
601 if (error
== GIT_EBUFS
)
604 /* We want to free the stream reasorces no matter what here */
605 idx
->have_stream
= 0;
606 git_packfile_stream_free(stream
);
611 if (idx
->have_delta
) {
612 error
= store_delta(idx
);
614 error
= store_object(idx
);
620 if (!idx
->have_delta
) {
621 stats
->indexed_objects
= (unsigned int)++processed
;
623 stats
->received_objects
++;
625 if ((error
= do_progress_callback(idx
, stats
)) != 0)
632 git_mwindow_free_all(mwf
);
636 static int index_path(git_buf
*path
, git_indexer
*idx
, const char *suffix
)
638 const char prefix
[] = "pack-";
639 size_t slash
= (size_t)path
->size
;
641 /* search backwards for '/' */
642 while (slash
> 0 && path
->ptr
[slash
- 1] != '/')
645 if (git_buf_grow(path
, slash
+ 1 + strlen(prefix
) +
646 GIT_OID_HEXSZ
+ strlen(suffix
) + 1) < 0)
649 git_buf_truncate(path
, slash
);
650 git_buf_puts(path
, prefix
);
651 git_oid_fmt(path
->ptr
+ git_buf_len(path
), &idx
->hash
);
652 path
->size
+= GIT_OID_HEXSZ
;
653 git_buf_puts(path
, suffix
);
655 return git_buf_oom(path
) ? -1 : 0;
659 * Rewind the packfile by the trailer, as we might need to fix the
660 * packfile by injecting objects at the tail and must overwrite it.
662 static void seek_back_trailer(git_indexer
*idx
)
664 idx
->pack
->mwf
.size
-= GIT_OID_RAWSZ
;
665 git_mwindow_free_all(&idx
->pack
->mwf
);
668 static int inject_object(git_indexer
*idx
, git_oid
*id
)
672 struct git_pack_entry
*pentry
= NULL
;
674 unsigned char hdr
[64];
675 git_buf buf
= GIT_BUF_INIT
;
676 git_off_t entry_start
;
681 seek_back_trailer(idx
);
682 entry_start
= idx
->pack
->mwf
.size
;
684 if (git_odb_read(&obj
, idx
->odb
, id
) < 0) {
685 giterr_set(GITERR_INDEXER
, "missing delta bases");
689 data
= git_odb_object_data(obj
);
690 len
= git_odb_object_size(obj
);
692 entry
= git__calloc(1, sizeof(*entry
));
693 GITERR_CHECK_ALLOC(entry
);
695 entry
->crc
= crc32(0L, Z_NULL
, 0);
697 /* Write out the object header */
698 hdr_len
= git_packfile__object_header(hdr
, len
, git_odb_object_type(obj
));
699 if ((error
= append_to_pack(idx
, hdr
, hdr_len
)) < 0)
702 idx
->pack
->mwf
.size
+= hdr_len
;
703 entry
->crc
= crc32(entry
->crc
, hdr
, (uInt
)hdr_len
);
705 if ((error
= git_zstream_deflatebuf(&buf
, data
, len
)) < 0)
708 /* And then the compressed object */
709 if ((error
= append_to_pack(idx
, buf
.ptr
, buf
.size
)) < 0)
712 idx
->pack
->mwf
.size
+= buf
.size
;
713 entry
->crc
= htonl(crc32(entry
->crc
, (unsigned char *)buf
.ptr
, (uInt
)buf
.size
));
716 /* Write a fake trailer so the pack functions play ball */
718 if ((error
= append_to_pack(idx
, &foo
, GIT_OID_RAWSZ
)) < 0)
721 idx
->pack
->mwf
.size
+= GIT_OID_RAWSZ
;
723 pentry
= git__calloc(1, sizeof(struct git_pack_entry
));
724 GITERR_CHECK_ALLOC(pentry
);
726 git_oid_cpy(&pentry
->sha1
, id
);
727 git_oid_cpy(&entry
->oid
, id
);
728 idx
->off
= entry_start
+ hdr_len
+ len
;
730 error
= save_entry(idx
, entry
, pentry
, entry_start
);
738 git_odb_object_free(obj
);
742 static int fix_thin_pack(git_indexer
*idx
, git_transfer_progress
*stats
)
744 int error
, found_ref_delta
= 0;
746 struct delta_info
*delta
;
749 git_mwindow
*w
= NULL
;
750 git_off_t curpos
= 0;
751 unsigned char *base_info
;
752 unsigned int left
= 0;
755 assert(git_vector_length(&idx
->deltas
) > 0);
757 if (idx
->odb
== NULL
) {
758 giterr_set(GITERR_INDEXER
, "cannot fix a thin pack without an ODB");
762 /* Loop until we find the first REF delta */
763 git_vector_foreach(&idx
->deltas
, i
, delta
) {
767 curpos
= delta
->delta_off
;
768 error
= git_packfile_unpack_header(&size
, &type
, &idx
->pack
->mwf
, &w
, &curpos
);
769 git_mwindow_close(&w
);
773 if (type
== GIT_OBJ_REF_DELTA
) {
779 if (!found_ref_delta
) {
780 giterr_set(GITERR_INDEXER
, "no REF_DELTA found, cannot inject object");
784 /* curpos now points to the base information, which is an OID */
785 base_info
= git_mwindow_open(&idx
->pack
->mwf
, &w
, curpos
, GIT_OID_RAWSZ
, &left
);
786 if (base_info
== NULL
) {
787 giterr_set(GITERR_INDEXER
, "failed to map delta information");
791 git_oid_fromraw(&base
, base_info
);
792 git_mwindow_close(&w
);
794 if (inject_object(idx
, &base
) < 0)
797 stats
->local_objects
++;
802 static int resolve_deltas(git_indexer
*idx
, git_transfer_progress
*stats
)
805 struct delta_info
*delta
;
806 int progressed
= 0, non_null
= 0, progress_cb_result
;
808 while (idx
->deltas
.length
> 0) {
811 git_vector_foreach(&idx
->deltas
, i
, delta
) {
818 idx
->off
= delta
->delta_off
;
819 if (git_packfile_unpack(&obj
, idx
->pack
, &idx
->off
) < 0)
822 if (hash_and_save(idx
, &obj
, delta
->delta_off
) < 0)
826 stats
->indexed_objects
++;
827 stats
->indexed_deltas
++;
829 if ((progress_cb_result
= do_progress_callback(idx
, stats
)) < 0)
830 return progress_cb_result
;
832 /* remove from the list */
833 git_vector_set(NULL
, &idx
->deltas
, i
, NULL
);
837 /* if none were actually set, we're done */
841 if (!progressed
&& (fix_thin_pack(idx
, stats
) < 0)) {
849 static int update_header_and_rehash(git_indexer
*idx
, git_transfer_progress
*stats
)
852 size_t chunk
= 1024*1024;
853 git_off_t hashed
= 0;
854 git_mwindow
*w
= NULL
;
855 git_mwindow_file
*mwf
;
858 mwf
= &idx
->pack
->mwf
;
860 git_hash_init(&idx
->trailer
);
863 /* Update the header to include the numer of local objects we injected */
864 idx
->hdr
.hdr_entries
= htonl(stats
->total_objects
+ stats
->local_objects
);
865 if (write_at(idx
, &idx
->hdr
, 0, sizeof(struct git_pack_header
)) < 0)
869 * We now use the same technique as before to determine the
870 * hash. We keep reading up to the end and let
871 * hash_partially() keep the existing trailer out of the
874 git_mwindow_free_all(mwf
);
876 while (hashed
< mwf
->size
) {
877 ptr
= git_mwindow_open(mwf
, &w
, hashed
, chunk
, &left
);
881 hash_partially(idx
, ptr
, left
);
884 git_mwindow_close(&w
);
890 int git_indexer_commit(git_indexer
*idx
, git_transfer_progress
*stats
)
892 git_mwindow
*w
= NULL
;
893 unsigned int i
, long_offsets
= 0, left
;
895 struct git_pack_idx_header hdr
;
896 git_buf filename
= GIT_BUF_INIT
;
898 git_oid trailer_hash
, file_hash
;
900 git_filebuf index_file
= {0};
901 void *packfile_trailer
;
903 if (git_hash_ctx_init(&ctx
) < 0)
906 /* Test for this before resolve_deltas(), as it plays with idx->off */
907 if (idx
->off
< idx
->pack
->mwf
.size
- 20) {
908 giterr_set(GITERR_INDEXER
, "Unexpected data at the end of the pack");
912 packfile_trailer
= git_mwindow_open(&idx
->pack
->mwf
, &w
, idx
->pack
->mwf
.size
- GIT_OID_RAWSZ
, GIT_OID_RAWSZ
, &left
);
913 if (packfile_trailer
== NULL
) {
914 git_mwindow_close(&w
);
918 /* Compare the packfile trailer as it was sent to us and what we calculated */
919 git_oid_fromraw(&file_hash
, packfile_trailer
);
920 git_mwindow_close(&w
);
922 git_hash_final(&trailer_hash
, &idx
->trailer
);
923 if (git_oid_cmp(&file_hash
, &trailer_hash
)) {
924 giterr_set(GITERR_INDEXER
, "packfile trailer mismatch");
928 /* Freeze the number of deltas */
929 stats
->total_deltas
= stats
->total_objects
- stats
->indexed_objects
;
931 if ((error
= resolve_deltas(idx
, stats
)) < 0)
934 if (stats
->indexed_objects
!= stats
->total_objects
) {
935 giterr_set(GITERR_INDEXER
, "early EOF");
939 if (stats
->local_objects
> 0) {
940 if (update_header_and_rehash(idx
, stats
) < 0)
943 git_hash_final(&trailer_hash
, &idx
->trailer
);
944 write_at(idx
, &trailer_hash
, idx
->pack
->mwf
.size
- GIT_OID_RAWSZ
, GIT_OID_RAWSZ
);
947 git_vector_sort(&idx
->objects
);
949 git_buf_sets(&filename
, idx
->pack
->pack_name
);
950 git_buf_shorten(&filename
, strlen("pack"));
951 git_buf_puts(&filename
, "idx");
952 if (git_buf_oom(&filename
))
955 if (git_filebuf_open(&index_file
, filename
.ptr
,
956 GIT_FILEBUF_HASH_CONTENTS
, idx
->mode
) < 0)
959 /* Write out the header */
960 hdr
.idx_signature
= htonl(PACK_IDX_SIGNATURE
);
961 hdr
.idx_version
= htonl(2);
962 git_filebuf_write(&index_file
, &hdr
, sizeof(hdr
));
964 /* Write out the fanout table */
965 for (i
= 0; i
< 256; ++i
) {
966 uint32_t n
= htonl(idx
->fanout
[i
]);
967 git_filebuf_write(&index_file
, &n
, sizeof(n
));
970 /* Write out the object names (SHA-1 hashes) */
971 git_vector_foreach(&idx
->objects
, i
, entry
) {
972 git_filebuf_write(&index_file
, &entry
->oid
, sizeof(git_oid
));
973 git_hash_update(&ctx
, &entry
->oid
, GIT_OID_RAWSZ
);
975 git_hash_final(&idx
->hash
, &ctx
);
977 /* Write out the CRC32 values */
978 git_vector_foreach(&idx
->objects
, i
, entry
) {
979 git_filebuf_write(&index_file
, &entry
->crc
, sizeof(uint32_t));
982 /* Write out the offsets */
983 git_vector_foreach(&idx
->objects
, i
, entry
) {
986 if (entry
->offset
== UINT32_MAX
)
987 n
= htonl(0x80000000 | long_offsets
++);
989 n
= htonl(entry
->offset
);
991 git_filebuf_write(&index_file
, &n
, sizeof(uint32_t));
994 /* Write out the long offsets */
995 git_vector_foreach(&idx
->objects
, i
, entry
) {
998 if (entry
->offset
!= UINT32_MAX
)
1001 split
[0] = htonl(entry
->offset_long
>> 32);
1002 split
[1] = htonl(entry
->offset_long
& 0xffffffff);
1004 git_filebuf_write(&index_file
, &split
, sizeof(uint32_t) * 2);
1007 /* Write out the packfile trailer to the index */
1008 if (git_filebuf_write(&index_file
, &trailer_hash
, GIT_OID_RAWSZ
) < 0)
1011 /* Write out the hash of the idx */
1012 if (git_filebuf_hash(&trailer_hash
, &index_file
) < 0)
1015 git_filebuf_write(&index_file
, &trailer_hash
, sizeof(git_oid
));
1017 /* Figure out what the final name should be */
1018 if (index_path(&filename
, idx
, ".idx") < 0)
1022 if (git_filebuf_commit_at(&index_file
, filename
.ptr
) < 0)
1025 git_mwindow_free_all(&idx
->pack
->mwf
);
1026 /* We need to close the descriptor here so Windows doesn't choke on commit_at */
1027 if (p_close(idx
->pack
->mwf
.fd
) < 0) {
1028 giterr_set(GITERR_OS
, "failed to close packfile");
1032 idx
->pack
->mwf
.fd
= -1;
1034 if (index_path(&filename
, idx
, ".pack") < 0)
1037 /* And don't forget to rename the packfile to its new place. */
1038 p_rename(idx
->pack
->pack_name
, git_buf_cstr(&filename
));
1040 git_buf_free(&filename
);
1041 git_hash_ctx_cleanup(&ctx
);
1045 git_mwindow_free_all(&idx
->pack
->mwf
);
1046 git_filebuf_cleanup(&index_file
);
1047 git_buf_free(&filename
);
1048 git_hash_ctx_cleanup(&ctx
);
1052 void git_indexer_free(git_indexer
*idx
)
1057 git_vector_free_deep(&idx
->objects
);
1059 if (idx
->pack
&& idx
->pack
->idx_cache
) {
1060 struct git_pack_entry
*pentry
;
1062 idx
->pack
->idx_cache
, pentry
, { git__free(pentry
); });
1064 git_oidmap_free(idx
->pack
->idx_cache
);
1067 git_vector_free_deep(&idx
->deltas
);
1069 if (!git_mutex_lock(&git__mwindow_mutex
)) {
1070 git_packfile_free(idx
->pack
);
1071 git_mutex_unlock(&git__mwindow_mutex
);
1074 git_hash_ctx_cleanup(&idx
->trailer
);
1075 git_hash_ctx_cleanup(&idx
->hash_ctx
);