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 GIT_INLINE(bool) has_entry(git_indexer
*idx
, git_oid
*id
)
331 k
= kh_get(oid
, idx
->pack
->idx_cache
, id
);
332 return (k
!= kh_end(idx
->pack
->idx_cache
));
335 static int save_entry(git_indexer
*idx
, struct entry
*entry
, struct git_pack_entry
*pentry
, git_off_t entry_start
)
340 if (entry_start
> UINT31_MAX
) {
341 entry
->offset
= UINT32_MAX
;
342 entry
->offset_long
= entry_start
;
344 entry
->offset
= (uint32_t)entry_start
;
347 pentry
->offset
= entry_start
;
348 k
= kh_put(oid
, idx
->pack
->idx_cache
, &pentry
->sha1
, &error
);
351 giterr_set(GITERR_INDEXER
, "cannot insert object into pack");
355 kh_value(idx
->pack
->idx_cache
, k
) = pentry
;
357 /* Add the object to the list */
358 if (git_vector_insert(&idx
->objects
, entry
) < 0)
361 for (i
= entry
->oid
.id
[0]; i
< 256; ++i
) {
368 static int hash_and_save(git_indexer
*idx
, git_rawobj
*obj
, git_off_t entry_start
)
373 struct git_pack_entry
*pentry
= NULL
;
375 entry
= git__calloc(1, sizeof(*entry
));
376 GITERR_CHECK_ALLOC(entry
);
378 if (git_odb__hashobj(&oid
, obj
) < 0) {
379 giterr_set(GITERR_INDEXER
, "Failed to hash object");
383 pentry
= git__calloc(1, sizeof(struct git_pack_entry
));
384 GITERR_CHECK_ALLOC(pentry
);
386 git_oid_cpy(&pentry
->sha1
, &oid
);
387 git_oid_cpy(&entry
->oid
, &oid
);
388 entry
->crc
= crc32(0L, Z_NULL
, 0);
390 entry_size
= (size_t)(idx
->off
- entry_start
);
391 if (crc_object(&entry
->crc
, &idx
->pack
->mwf
, entry_start
, entry_size
) < 0)
394 return save_entry(idx
, entry
, pentry
, entry_start
);
399 git__free(obj
->data
);
403 static int do_progress_callback(git_indexer
*idx
, git_transfer_progress
*stats
)
405 if (idx
->progress_cb
)
406 return giterr_set_after_callback_function(
407 idx
->progress_cb(stats
, idx
->progress_payload
),
412 /* Hash everything but the last 20B of input */
413 static void hash_partially(git_indexer
*idx
, const uint8_t *data
, size_t size
)
415 size_t to_expell
, to_keep
;
420 /* Easy case, dump the buffer and the data minus the last 20 bytes */
421 if (size
>= GIT_OID_RAWSZ
) {
422 git_hash_update(&idx
->trailer
, idx
->inbuf
, idx
->inbuf_len
);
423 git_hash_update(&idx
->trailer
, data
, size
- GIT_OID_RAWSZ
);
425 data
+= size
- GIT_OID_RAWSZ
;
426 memcpy(idx
->inbuf
, data
, GIT_OID_RAWSZ
);
427 idx
->inbuf_len
= GIT_OID_RAWSZ
;
431 /* We can just append */
432 if (idx
->inbuf_len
+ size
<= GIT_OID_RAWSZ
) {
433 memcpy(idx
->inbuf
+ idx
->inbuf_len
, data
, size
);
434 idx
->inbuf_len
+= size
;
438 /* We need to partially drain the buffer and then append */
439 to_keep
= GIT_OID_RAWSZ
- size
;
440 to_expell
= idx
->inbuf_len
- to_keep
;
442 git_hash_update(&idx
->trailer
, idx
->inbuf
, to_expell
);
444 memmove(idx
->inbuf
, idx
->inbuf
+ to_expell
, to_keep
);
445 memcpy(idx
->inbuf
+ to_keep
, data
, size
);
446 idx
->inbuf_len
+= size
- to_expell
;
449 static int write_at(git_indexer
*idx
, const void *data
, git_off_t offset
, size_t size
)
451 git_file fd
= idx
->pack
->mwf
.fd
;
454 git_off_t page_start
;
455 unsigned char *map_data
;
459 assert(data
&& size
);
461 if ((error
= git__page_size(&page_size
)) < 0)
464 /* the offset needs to be at the beginning of the a page boundary */
465 page_offset
= offset
% page_size
;
466 page_start
= offset
- page_offset
;
468 if ((error
= p_mmap(&map
, page_offset
+ size
, GIT_PROT_WRITE
, GIT_MAP_SHARED
, fd
, page_start
)) < 0)
471 map_data
= (unsigned char *)map
.data
;
472 memcpy(map_data
+ page_offset
, data
, size
);
478 static int append_to_pack(git_indexer
*idx
, const void *data
, size_t size
)
480 git_off_t current_size
= idx
->pack
->mwf
.size
;
485 /* add the extra space we need at the end */
486 if (p_ftruncate(idx
->pack
->mwf
.fd
, current_size
+ size
) < 0) {
487 giterr_set(GITERR_OS
, "Failed to increase size of pack file '%s'", idx
->pack
->pack_name
);
491 return write_at(idx
, data
, idx
->pack
->mwf
.size
, size
);
494 int git_indexer_append(git_indexer
*idx
, const void *data
, size_t size
, git_transfer_progress
*stats
)
498 struct git_pack_header
*hdr
= &idx
->hdr
;
499 git_mwindow_file
*mwf
= &idx
->pack
->mwf
;
501 assert(idx
&& data
&& stats
);
503 processed
= stats
->indexed_objects
;
505 if ((error
= append_to_pack(idx
, data
, size
)) < 0)
508 hash_partially(idx
, data
, (int)size
);
510 /* Make sure we set the new size of the pack */
511 idx
->pack
->mwf
.size
+= size
;
513 if (!idx
->parsed_header
) {
514 unsigned int total_objects
;
516 if ((unsigned)idx
->pack
->mwf
.size
< sizeof(struct git_pack_header
))
519 if ((error
= parse_header(&idx
->hdr
, idx
->pack
)) < 0)
522 idx
->parsed_header
= 1;
523 idx
->nr_objects
= ntohl(hdr
->hdr_entries
);
524 idx
->off
= sizeof(struct git_pack_header
);
526 /* for now, limit to 2^32 objects */
527 assert(idx
->nr_objects
== (size_t)((unsigned int)idx
->nr_objects
));
528 if (idx
->nr_objects
== (size_t)((unsigned int)idx
->nr_objects
))
529 total_objects
= (unsigned int)idx
->nr_objects
;
531 total_objects
= UINT_MAX
;
533 idx
->pack
->idx_cache
= git_oidmap_alloc();
534 GITERR_CHECK_ALLOC(idx
->pack
->idx_cache
);
536 idx
->pack
->has_cache
= 1;
537 if (git_vector_init(&idx
->objects
, total_objects
, objects_cmp
) < 0)
540 if (git_vector_init(&idx
->deltas
, total_objects
/ 2, NULL
) < 0)
543 stats
->received_objects
= 0;
544 stats
->local_objects
= 0;
545 stats
->total_deltas
= 0;
546 stats
->indexed_deltas
= 0;
547 processed
= stats
->indexed_objects
= 0;
548 stats
->total_objects
= total_objects
;
550 if ((error
= do_progress_callback(idx
, stats
)) != 0)
554 /* Now that we have data in the pack, let's try to parse it */
556 /* As the file grows any windows we try to use will be out of date */
557 git_mwindow_free_all(mwf
);
559 while (processed
< idx
->nr_objects
) {
560 git_packfile_stream
*stream
= &idx
->stream
;
561 git_off_t entry_start
= idx
->off
;
564 git_mwindow
*w
= NULL
;
566 if (idx
->pack
->mwf
.size
<= idx
->off
+ 20)
569 if (!idx
->have_stream
) {
570 error
= git_packfile_unpack_header(&entry_size
, &type
, mwf
, &w
, &idx
->off
);
571 if (error
== GIT_EBUFS
) {
572 idx
->off
= entry_start
;
578 git_mwindow_close(&w
);
579 idx
->entry_start
= entry_start
;
580 git_hash_init(&idx
->hash_ctx
);
582 if (type
== GIT_OBJ_REF_DELTA
|| type
== GIT_OBJ_OFS_DELTA
) {
583 error
= advance_delta_offset(idx
, type
);
584 if (error
== GIT_EBUFS
) {
585 idx
->off
= entry_start
;
594 hash_header(&idx
->hash_ctx
, entry_size
, type
);
597 idx
->have_stream
= 1;
599 error
= git_packfile_stream_open(stream
, idx
->pack
, idx
->off
);
604 if (idx
->have_delta
) {
605 error
= read_object_stream(idx
, stream
);
607 error
= hash_object_stream(idx
, stream
);
610 idx
->off
= stream
->curpos
;
611 if (error
== GIT_EBUFS
)
614 /* We want to free the stream reasorces no matter what here */
615 idx
->have_stream
= 0;
616 git_packfile_stream_free(stream
);
621 if (idx
->have_delta
) {
622 error
= store_delta(idx
);
624 error
= store_object(idx
);
630 if (!idx
->have_delta
) {
631 stats
->indexed_objects
= (unsigned int)++processed
;
633 stats
->received_objects
++;
635 if ((error
= do_progress_callback(idx
, stats
)) != 0)
642 git_mwindow_free_all(mwf
);
646 static int index_path(git_buf
*path
, git_indexer
*idx
, const char *suffix
)
648 const char prefix
[] = "pack-";
649 size_t slash
= (size_t)path
->size
;
651 /* search backwards for '/' */
652 while (slash
> 0 && path
->ptr
[slash
- 1] != '/')
655 if (git_buf_grow(path
, slash
+ 1 + strlen(prefix
) +
656 GIT_OID_HEXSZ
+ strlen(suffix
) + 1) < 0)
659 git_buf_truncate(path
, slash
);
660 git_buf_puts(path
, prefix
);
661 git_oid_fmt(path
->ptr
+ git_buf_len(path
), &idx
->hash
);
662 path
->size
+= GIT_OID_HEXSZ
;
663 git_buf_puts(path
, suffix
);
665 return git_buf_oom(path
) ? -1 : 0;
669 * Rewind the packfile by the trailer, as we might need to fix the
670 * packfile by injecting objects at the tail and must overwrite it.
672 static void seek_back_trailer(git_indexer
*idx
)
674 idx
->pack
->mwf
.size
-= GIT_OID_RAWSZ
;
675 git_mwindow_free_all(&idx
->pack
->mwf
);
678 static int inject_object(git_indexer
*idx
, git_oid
*id
)
682 struct git_pack_entry
*pentry
= NULL
;
684 unsigned char hdr
[64];
685 git_buf buf
= GIT_BUF_INIT
;
686 git_off_t entry_start
;
691 seek_back_trailer(idx
);
692 entry_start
= idx
->pack
->mwf
.size
;
694 if (git_odb_read(&obj
, idx
->odb
, id
) < 0) {
695 giterr_set(GITERR_INDEXER
, "missing delta bases");
699 data
= git_odb_object_data(obj
);
700 len
= git_odb_object_size(obj
);
702 entry
= git__calloc(1, sizeof(*entry
));
703 GITERR_CHECK_ALLOC(entry
);
705 entry
->crc
= crc32(0L, Z_NULL
, 0);
707 /* Write out the object header */
708 hdr_len
= git_packfile__object_header(hdr
, len
, git_odb_object_type(obj
));
709 if ((error
= append_to_pack(idx
, hdr
, hdr_len
)) < 0)
712 idx
->pack
->mwf
.size
+= hdr_len
;
713 entry
->crc
= crc32(entry
->crc
, hdr
, (uInt
)hdr_len
);
715 if ((error
= git_zstream_deflatebuf(&buf
, data
, len
)) < 0)
718 /* And then the compressed object */
719 if ((error
= append_to_pack(idx
, buf
.ptr
, buf
.size
)) < 0)
722 idx
->pack
->mwf
.size
+= buf
.size
;
723 entry
->crc
= htonl(crc32(entry
->crc
, (unsigned char *)buf
.ptr
, (uInt
)buf
.size
));
726 /* Write a fake trailer so the pack functions play ball */
728 if ((error
= append_to_pack(idx
, &foo
, GIT_OID_RAWSZ
)) < 0)
731 idx
->pack
->mwf
.size
+= GIT_OID_RAWSZ
;
733 pentry
= git__calloc(1, sizeof(struct git_pack_entry
));
734 GITERR_CHECK_ALLOC(pentry
);
736 git_oid_cpy(&pentry
->sha1
, id
);
737 git_oid_cpy(&entry
->oid
, id
);
738 idx
->off
= entry_start
+ hdr_len
+ len
;
740 error
= save_entry(idx
, entry
, pentry
, entry_start
);
748 git_odb_object_free(obj
);
752 static int fix_thin_pack(git_indexer
*idx
, git_transfer_progress
*stats
)
754 int error
, found_ref_delta
= 0;
756 struct delta_info
*delta
;
759 git_mwindow
*w
= NULL
;
760 git_off_t curpos
= 0;
761 unsigned char *base_info
;
762 unsigned int left
= 0;
765 assert(git_vector_length(&idx
->deltas
) > 0);
767 if (idx
->odb
== NULL
) {
768 giterr_set(GITERR_INDEXER
, "cannot fix a thin pack without an ODB");
772 /* Loop until we find the first REF delta */
773 git_vector_foreach(&idx
->deltas
, i
, delta
) {
777 curpos
= delta
->delta_off
;
778 error
= git_packfile_unpack_header(&size
, &type
, &idx
->pack
->mwf
, &w
, &curpos
);
779 git_mwindow_close(&w
);
783 if (type
== GIT_OBJ_REF_DELTA
) {
789 if (!found_ref_delta
) {
790 giterr_set(GITERR_INDEXER
, "no REF_DELTA found, cannot inject object");
794 /* curpos now points to the base information, which is an OID */
795 base_info
= git_mwindow_open(&idx
->pack
->mwf
, &w
, curpos
, GIT_OID_RAWSZ
, &left
);
796 if (base_info
== NULL
) {
797 giterr_set(GITERR_INDEXER
, "failed to map delta information");
801 git_oid_fromraw(&base
, base_info
);
802 git_mwindow_close(&w
);
804 if (has_entry(idx
, &base
))
807 if (inject_object(idx
, &base
) < 0)
810 stats
->local_objects
++;
815 static int resolve_deltas(git_indexer
*idx
, git_transfer_progress
*stats
)
818 struct delta_info
*delta
;
819 int progressed
= 0, non_null
= 0, progress_cb_result
;
821 while (idx
->deltas
.length
> 0) {
824 git_vector_foreach(&idx
->deltas
, i
, delta
) {
831 idx
->off
= delta
->delta_off
;
832 if (git_packfile_unpack(&obj
, idx
->pack
, &idx
->off
) < 0)
835 if (hash_and_save(idx
, &obj
, delta
->delta_off
) < 0)
839 stats
->indexed_objects
++;
840 stats
->indexed_deltas
++;
842 if ((progress_cb_result
= do_progress_callback(idx
, stats
)) < 0)
843 return progress_cb_result
;
845 /* remove from the list */
846 git_vector_set(NULL
, &idx
->deltas
, i
, NULL
);
850 /* if none were actually set, we're done */
854 if (!progressed
&& (fix_thin_pack(idx
, stats
) < 0)) {
862 static int update_header_and_rehash(git_indexer
*idx
, git_transfer_progress
*stats
)
865 size_t chunk
= 1024*1024;
866 git_off_t hashed
= 0;
867 git_mwindow
*w
= NULL
;
868 git_mwindow_file
*mwf
;
871 mwf
= &idx
->pack
->mwf
;
873 git_hash_init(&idx
->trailer
);
876 /* Update the header to include the numer of local objects we injected */
877 idx
->hdr
.hdr_entries
= htonl(stats
->total_objects
+ stats
->local_objects
);
878 if (write_at(idx
, &idx
->hdr
, 0, sizeof(struct git_pack_header
)) < 0)
882 * We now use the same technique as before to determine the
883 * hash. We keep reading up to the end and let
884 * hash_partially() keep the existing trailer out of the
887 git_mwindow_free_all(mwf
);
889 while (hashed
< mwf
->size
) {
890 ptr
= git_mwindow_open(mwf
, &w
, hashed
, chunk
, &left
);
894 hash_partially(idx
, ptr
, left
);
897 git_mwindow_close(&w
);
903 int git_indexer_commit(git_indexer
*idx
, git_transfer_progress
*stats
)
905 git_mwindow
*w
= NULL
;
906 unsigned int i
, long_offsets
= 0, left
;
908 struct git_pack_idx_header hdr
;
909 git_buf filename
= GIT_BUF_INIT
;
911 git_oid trailer_hash
, file_hash
;
913 git_filebuf index_file
= {0};
914 void *packfile_trailer
;
916 if (git_hash_ctx_init(&ctx
) < 0)
919 /* Test for this before resolve_deltas(), as it plays with idx->off */
920 if (idx
->off
< idx
->pack
->mwf
.size
- 20) {
921 giterr_set(GITERR_INDEXER
, "Unexpected data at the end of the pack");
925 packfile_trailer
= git_mwindow_open(&idx
->pack
->mwf
, &w
, idx
->pack
->mwf
.size
- GIT_OID_RAWSZ
, GIT_OID_RAWSZ
, &left
);
926 if (packfile_trailer
== NULL
) {
927 git_mwindow_close(&w
);
931 /* Compare the packfile trailer as it was sent to us and what we calculated */
932 git_oid_fromraw(&file_hash
, packfile_trailer
);
933 git_mwindow_close(&w
);
935 git_hash_final(&trailer_hash
, &idx
->trailer
);
936 if (git_oid_cmp(&file_hash
, &trailer_hash
)) {
937 giterr_set(GITERR_INDEXER
, "packfile trailer mismatch");
941 /* Freeze the number of deltas */
942 stats
->total_deltas
= stats
->total_objects
- stats
->indexed_objects
;
944 if ((error
= resolve_deltas(idx
, stats
)) < 0)
947 if (stats
->indexed_objects
!= stats
->total_objects
) {
948 giterr_set(GITERR_INDEXER
, "early EOF");
952 if (stats
->local_objects
> 0) {
953 if (update_header_and_rehash(idx
, stats
) < 0)
956 git_hash_final(&trailer_hash
, &idx
->trailer
);
957 write_at(idx
, &trailer_hash
, idx
->pack
->mwf
.size
- GIT_OID_RAWSZ
, GIT_OID_RAWSZ
);
960 git_vector_sort(&idx
->objects
);
962 git_buf_sets(&filename
, idx
->pack
->pack_name
);
963 git_buf_shorten(&filename
, strlen("pack"));
964 git_buf_puts(&filename
, "idx");
965 if (git_buf_oom(&filename
))
968 if (git_filebuf_open(&index_file
, filename
.ptr
,
969 GIT_FILEBUF_HASH_CONTENTS
, idx
->mode
) < 0)
972 /* Write out the header */
973 hdr
.idx_signature
= htonl(PACK_IDX_SIGNATURE
);
974 hdr
.idx_version
= htonl(2);
975 git_filebuf_write(&index_file
, &hdr
, sizeof(hdr
));
977 /* Write out the fanout table */
978 for (i
= 0; i
< 256; ++i
) {
979 uint32_t n
= htonl(idx
->fanout
[i
]);
980 git_filebuf_write(&index_file
, &n
, sizeof(n
));
983 /* Write out the object names (SHA-1 hashes) */
984 git_vector_foreach(&idx
->objects
, i
, entry
) {
985 git_filebuf_write(&index_file
, &entry
->oid
, sizeof(git_oid
));
986 git_hash_update(&ctx
, &entry
->oid
, GIT_OID_RAWSZ
);
988 git_hash_final(&idx
->hash
, &ctx
);
990 /* Write out the CRC32 values */
991 git_vector_foreach(&idx
->objects
, i
, entry
) {
992 git_filebuf_write(&index_file
, &entry
->crc
, sizeof(uint32_t));
995 /* Write out the offsets */
996 git_vector_foreach(&idx
->objects
, i
, entry
) {
999 if (entry
->offset
== UINT32_MAX
)
1000 n
= htonl(0x80000000 | long_offsets
++);
1002 n
= htonl(entry
->offset
);
1004 git_filebuf_write(&index_file
, &n
, sizeof(uint32_t));
1007 /* Write out the long offsets */
1008 git_vector_foreach(&idx
->objects
, i
, entry
) {
1011 if (entry
->offset
!= UINT32_MAX
)
1014 split
[0] = htonl(entry
->offset_long
>> 32);
1015 split
[1] = htonl(entry
->offset_long
& 0xffffffff);
1017 git_filebuf_write(&index_file
, &split
, sizeof(uint32_t) * 2);
1020 /* Write out the packfile trailer to the index */
1021 if (git_filebuf_write(&index_file
, &trailer_hash
, GIT_OID_RAWSZ
) < 0)
1024 /* Write out the hash of the idx */
1025 if (git_filebuf_hash(&trailer_hash
, &index_file
) < 0)
1028 git_filebuf_write(&index_file
, &trailer_hash
, sizeof(git_oid
));
1030 /* Figure out what the final name should be */
1031 if (index_path(&filename
, idx
, ".idx") < 0)
1035 if (git_filebuf_commit_at(&index_file
, filename
.ptr
) < 0)
1038 git_mwindow_free_all(&idx
->pack
->mwf
);
1039 /* We need to close the descriptor here so Windows doesn't choke on commit_at */
1040 if (p_close(idx
->pack
->mwf
.fd
) < 0) {
1041 giterr_set(GITERR_OS
, "failed to close packfile");
1045 idx
->pack
->mwf
.fd
= -1;
1047 if (index_path(&filename
, idx
, ".pack") < 0)
1050 /* And don't forget to rename the packfile to its new place. */
1051 p_rename(idx
->pack
->pack_name
, git_buf_cstr(&filename
));
1053 git_buf_free(&filename
);
1054 git_hash_ctx_cleanup(&ctx
);
1058 git_mwindow_free_all(&idx
->pack
->mwf
);
1059 git_filebuf_cleanup(&index_file
);
1060 git_buf_free(&filename
);
1061 git_hash_ctx_cleanup(&ctx
);
1065 void git_indexer_free(git_indexer
*idx
)
1070 git_vector_free_deep(&idx
->objects
);
1072 if (idx
->pack
&& idx
->pack
->idx_cache
) {
1073 struct git_pack_entry
*pentry
;
1075 idx
->pack
->idx_cache
, pentry
, { git__free(pentry
); });
1077 git_oidmap_free(idx
->pack
->idx_cache
);
1080 git_vector_free_deep(&idx
->deltas
);
1082 if (!git_mutex_lock(&git__mwindow_mutex
)) {
1083 git_packfile_free(idx
->pack
);
1084 git_mutex_unlock(&git__mwindow_mutex
);
1087 git_hash_ctx_cleanup(&idx
->trailer
);
1088 git_hash_ctx_cleanup(&idx
->hash_ctx
);