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
;
452 size_t mmap_alignment
;
454 git_off_t page_start
;
455 unsigned char *map_data
;
459 assert(data
&& size
);
461 if ((error
= git__mmap_alignment(&mmap_alignment
)) < 0)
464 /* the offset needs to be at the mmap boundary for the platform */
465 page_offset
= offset
% mmap_alignment
;
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
)
481 size_t mmap_alignment
;
483 git_off_t page_start
;
484 git_off_t current_size
= idx
->pack
->mwf
.size
;
485 int fd
= idx
->pack
->mwf
.fd
;
491 if ((error
= git__mmap_alignment(&mmap_alignment
)) < 0)
494 /* Write a single byte to force the file system to allocate space now or
495 * report an error, since we can't report errors when writing using mmap.
496 * Round the size up to the nearest page so that we only need to perform file
497 * I/O when we add a page, instead of whenever we write even a single byte. */
498 new_size
= current_size
+ size
;
499 page_offset
= new_size
% mmap_alignment
;
500 page_start
= new_size
- page_offset
;
502 if (p_lseek(fd
, page_start
+ mmap_alignment
- 1, SEEK_SET
) < 0 ||
503 p_write(idx
->pack
->mwf
.fd
, data
, 1) < 0) {
504 giterr_set(GITERR_OS
, "cannot extend packfile '%s'", idx
->pack
->pack_name
);
508 return write_at(idx
, data
, idx
->pack
->mwf
.size
, size
);
511 int git_indexer_append(git_indexer
*idx
, const void *data
, size_t size
, git_transfer_progress
*stats
)
515 struct git_pack_header
*hdr
= &idx
->hdr
;
516 git_mwindow_file
*mwf
= &idx
->pack
->mwf
;
518 assert(idx
&& data
&& stats
);
520 processed
= stats
->indexed_objects
;
522 if ((error
= append_to_pack(idx
, data
, size
)) < 0)
525 hash_partially(idx
, data
, (int)size
);
527 /* Make sure we set the new size of the pack */
528 idx
->pack
->mwf
.size
+= size
;
530 if (!idx
->parsed_header
) {
531 unsigned int total_objects
;
533 if ((unsigned)idx
->pack
->mwf
.size
< sizeof(struct git_pack_header
))
536 if ((error
= parse_header(&idx
->hdr
, idx
->pack
)) < 0)
539 idx
->parsed_header
= 1;
540 idx
->nr_objects
= ntohl(hdr
->hdr_entries
);
541 idx
->off
= sizeof(struct git_pack_header
);
543 /* for now, limit to 2^32 objects */
544 assert(idx
->nr_objects
== (size_t)((unsigned int)idx
->nr_objects
));
545 if (idx
->nr_objects
== (size_t)((unsigned int)idx
->nr_objects
))
546 total_objects
= (unsigned int)idx
->nr_objects
;
548 total_objects
= UINT_MAX
;
550 idx
->pack
->idx_cache
= git_oidmap_alloc();
551 GITERR_CHECK_ALLOC(idx
->pack
->idx_cache
);
553 idx
->pack
->has_cache
= 1;
554 if (git_vector_init(&idx
->objects
, total_objects
, objects_cmp
) < 0)
557 if (git_vector_init(&idx
->deltas
, total_objects
/ 2, NULL
) < 0)
560 stats
->received_objects
= 0;
561 stats
->local_objects
= 0;
562 stats
->total_deltas
= 0;
563 stats
->indexed_deltas
= 0;
564 processed
= stats
->indexed_objects
= 0;
565 stats
->total_objects
= total_objects
;
567 if ((error
= do_progress_callback(idx
, stats
)) != 0)
571 /* Now that we have data in the pack, let's try to parse it */
573 /* As the file grows any windows we try to use will be out of date */
574 git_mwindow_free_all(mwf
);
576 while (processed
< idx
->nr_objects
) {
577 git_packfile_stream
*stream
= &idx
->stream
;
578 git_off_t entry_start
= idx
->off
;
581 git_mwindow
*w
= NULL
;
583 if (idx
->pack
->mwf
.size
<= idx
->off
+ 20)
586 if (!idx
->have_stream
) {
587 error
= git_packfile_unpack_header(&entry_size
, &type
, mwf
, &w
, &idx
->off
);
588 if (error
== GIT_EBUFS
) {
589 idx
->off
= entry_start
;
595 git_mwindow_close(&w
);
596 idx
->entry_start
= entry_start
;
597 git_hash_init(&idx
->hash_ctx
);
599 if (type
== GIT_OBJ_REF_DELTA
|| type
== GIT_OBJ_OFS_DELTA
) {
600 error
= advance_delta_offset(idx
, type
);
601 if (error
== GIT_EBUFS
) {
602 idx
->off
= entry_start
;
611 hash_header(&idx
->hash_ctx
, entry_size
, type
);
614 idx
->have_stream
= 1;
616 error
= git_packfile_stream_open(stream
, idx
->pack
, idx
->off
);
621 if (idx
->have_delta
) {
622 error
= read_object_stream(idx
, stream
);
624 error
= hash_object_stream(idx
, stream
);
627 idx
->off
= stream
->curpos
;
628 if (error
== GIT_EBUFS
)
631 /* We want to free the stream reasorces no matter what here */
632 idx
->have_stream
= 0;
633 git_packfile_stream_free(stream
);
638 if (idx
->have_delta
) {
639 error
= store_delta(idx
);
641 error
= store_object(idx
);
647 if (!idx
->have_delta
) {
648 stats
->indexed_objects
= (unsigned int)++processed
;
650 stats
->received_objects
++;
652 if ((error
= do_progress_callback(idx
, stats
)) != 0)
659 git_mwindow_free_all(mwf
);
663 static int index_path(git_buf
*path
, git_indexer
*idx
, const char *suffix
)
665 const char prefix
[] = "pack-";
666 size_t slash
= (size_t)path
->size
;
668 /* search backwards for '/' */
669 while (slash
> 0 && path
->ptr
[slash
- 1] != '/')
672 if (git_buf_grow(path
, slash
+ 1 + strlen(prefix
) +
673 GIT_OID_HEXSZ
+ strlen(suffix
) + 1) < 0)
676 git_buf_truncate(path
, slash
);
677 git_buf_puts(path
, prefix
);
678 git_oid_fmt(path
->ptr
+ git_buf_len(path
), &idx
->hash
);
679 path
->size
+= GIT_OID_HEXSZ
;
680 git_buf_puts(path
, suffix
);
682 return git_buf_oom(path
) ? -1 : 0;
686 * Rewind the packfile by the trailer, as we might need to fix the
687 * packfile by injecting objects at the tail and must overwrite it.
689 static void seek_back_trailer(git_indexer
*idx
)
691 idx
->pack
->mwf
.size
-= GIT_OID_RAWSZ
;
692 git_mwindow_free_all(&idx
->pack
->mwf
);
695 static int inject_object(git_indexer
*idx
, git_oid
*id
)
699 struct git_pack_entry
*pentry
= NULL
;
701 unsigned char hdr
[64];
702 git_buf buf
= GIT_BUF_INIT
;
703 git_off_t entry_start
;
708 seek_back_trailer(idx
);
709 entry_start
= idx
->pack
->mwf
.size
;
711 if (git_odb_read(&obj
, idx
->odb
, id
) < 0) {
712 giterr_set(GITERR_INDEXER
, "missing delta bases");
716 data
= git_odb_object_data(obj
);
717 len
= git_odb_object_size(obj
);
719 entry
= git__calloc(1, sizeof(*entry
));
720 GITERR_CHECK_ALLOC(entry
);
722 entry
->crc
= crc32(0L, Z_NULL
, 0);
724 /* Write out the object header */
725 hdr_len
= git_packfile__object_header(hdr
, len
, git_odb_object_type(obj
));
726 if ((error
= append_to_pack(idx
, hdr
, hdr_len
)) < 0)
729 idx
->pack
->mwf
.size
+= hdr_len
;
730 entry
->crc
= crc32(entry
->crc
, hdr
, (uInt
)hdr_len
);
732 if ((error
= git_zstream_deflatebuf(&buf
, data
, len
)) < 0)
735 /* And then the compressed object */
736 if ((error
= append_to_pack(idx
, buf
.ptr
, buf
.size
)) < 0)
739 idx
->pack
->mwf
.size
+= buf
.size
;
740 entry
->crc
= htonl(crc32(entry
->crc
, (unsigned char *)buf
.ptr
, (uInt
)buf
.size
));
743 /* Write a fake trailer so the pack functions play ball */
745 if ((error
= append_to_pack(idx
, &foo
, GIT_OID_RAWSZ
)) < 0)
748 idx
->pack
->mwf
.size
+= GIT_OID_RAWSZ
;
750 pentry
= git__calloc(1, sizeof(struct git_pack_entry
));
751 GITERR_CHECK_ALLOC(pentry
);
753 git_oid_cpy(&pentry
->sha1
, id
);
754 git_oid_cpy(&entry
->oid
, id
);
755 idx
->off
= entry_start
+ hdr_len
+ len
;
757 error
= save_entry(idx
, entry
, pentry
, entry_start
);
765 git_odb_object_free(obj
);
769 static int fix_thin_pack(git_indexer
*idx
, git_transfer_progress
*stats
)
771 int error
, found_ref_delta
= 0;
773 struct delta_info
*delta
;
776 git_mwindow
*w
= NULL
;
777 git_off_t curpos
= 0;
778 unsigned char *base_info
;
779 unsigned int left
= 0;
782 assert(git_vector_length(&idx
->deltas
) > 0);
784 if (idx
->odb
== NULL
) {
785 giterr_set(GITERR_INDEXER
, "cannot fix a thin pack without an ODB");
789 /* Loop until we find the first REF delta */
790 git_vector_foreach(&idx
->deltas
, i
, delta
) {
794 curpos
= delta
->delta_off
;
795 error
= git_packfile_unpack_header(&size
, &type
, &idx
->pack
->mwf
, &w
, &curpos
);
799 if (type
== GIT_OBJ_REF_DELTA
) {
805 if (!found_ref_delta
) {
806 giterr_set(GITERR_INDEXER
, "no REF_DELTA found, cannot inject object");
810 /* curpos now points to the base information, which is an OID */
811 base_info
= git_mwindow_open(&idx
->pack
->mwf
, &w
, curpos
, GIT_OID_RAWSZ
, &left
);
812 if (base_info
== NULL
) {
813 giterr_set(GITERR_INDEXER
, "failed to map delta information");
817 git_oid_fromraw(&base
, base_info
);
818 git_mwindow_close(&w
);
820 if (has_entry(idx
, &base
))
823 if (inject_object(idx
, &base
) < 0)
826 stats
->local_objects
++;
831 static int resolve_deltas(git_indexer
*idx
, git_transfer_progress
*stats
)
834 struct delta_info
*delta
;
835 int progressed
= 0, non_null
= 0, progress_cb_result
;
837 while (idx
->deltas
.length
> 0) {
840 git_vector_foreach(&idx
->deltas
, i
, delta
) {
841 git_rawobj obj
= {NULL
};
847 idx
->off
= delta
->delta_off
;
848 if (git_packfile_unpack(&obj
, idx
->pack
, &idx
->off
) < 0)
851 if (hash_and_save(idx
, &obj
, delta
->delta_off
) < 0)
855 stats
->indexed_objects
++;
856 stats
->indexed_deltas
++;
858 if ((progress_cb_result
= do_progress_callback(idx
, stats
)) < 0)
859 return progress_cb_result
;
861 /* remove from the list */
862 git_vector_set(NULL
, &idx
->deltas
, i
, NULL
);
866 /* if none were actually set, we're done */
870 if (!progressed
&& (fix_thin_pack(idx
, stats
) < 0)) {
878 static int update_header_and_rehash(git_indexer
*idx
, git_transfer_progress
*stats
)
881 size_t chunk
= 1024*1024;
882 git_off_t hashed
= 0;
883 git_mwindow
*w
= NULL
;
884 git_mwindow_file
*mwf
;
887 mwf
= &idx
->pack
->mwf
;
889 git_hash_init(&idx
->trailer
);
892 /* Update the header to include the numer of local objects we injected */
893 idx
->hdr
.hdr_entries
= htonl(stats
->total_objects
+ stats
->local_objects
);
894 if (write_at(idx
, &idx
->hdr
, 0, sizeof(struct git_pack_header
)) < 0)
898 * We now use the same technique as before to determine the
899 * hash. We keep reading up to the end and let
900 * hash_partially() keep the existing trailer out of the
903 git_mwindow_free_all(mwf
);
905 while (hashed
< mwf
->size
) {
906 ptr
= git_mwindow_open(mwf
, &w
, hashed
, chunk
, &left
);
910 hash_partially(idx
, ptr
, left
);
913 git_mwindow_close(&w
);
919 int git_indexer_commit(git_indexer
*idx
, git_transfer_progress
*stats
)
921 git_mwindow
*w
= NULL
;
922 unsigned int i
, long_offsets
= 0, left
;
924 struct git_pack_idx_header hdr
;
925 git_buf filename
= GIT_BUF_INIT
;
927 git_oid trailer_hash
, file_hash
;
929 git_filebuf index_file
= {0};
930 void *packfile_trailer
;
932 if (!idx
->parsed_header
) {
933 giterr_set(GITERR_INDEXER
, "incomplete pack header");
937 if (git_hash_ctx_init(&ctx
) < 0)
940 /* Test for this before resolve_deltas(), as it plays with idx->off */
941 if (idx
->off
+ 20 < idx
->pack
->mwf
.size
) {
942 giterr_set(GITERR_INDEXER
, "unexpected data at the end of the pack");
946 packfile_trailer
= git_mwindow_open(&idx
->pack
->mwf
, &w
, idx
->pack
->mwf
.size
- GIT_OID_RAWSZ
, GIT_OID_RAWSZ
, &left
);
947 if (packfile_trailer
== NULL
) {
948 git_mwindow_close(&w
);
952 /* Compare the packfile trailer as it was sent to us and what we calculated */
953 git_oid_fromraw(&file_hash
, packfile_trailer
);
954 git_mwindow_close(&w
);
956 git_hash_final(&trailer_hash
, &idx
->trailer
);
957 if (git_oid_cmp(&file_hash
, &trailer_hash
)) {
958 giterr_set(GITERR_INDEXER
, "packfile trailer mismatch");
962 /* Freeze the number of deltas */
963 stats
->total_deltas
= stats
->total_objects
- stats
->indexed_objects
;
965 if ((error
= resolve_deltas(idx
, stats
)) < 0)
968 if (stats
->indexed_objects
!= stats
->total_objects
) {
969 giterr_set(GITERR_INDEXER
, "early EOF");
973 if (stats
->local_objects
> 0) {
974 if (update_header_and_rehash(idx
, stats
) < 0)
977 git_hash_final(&trailer_hash
, &idx
->trailer
);
978 write_at(idx
, &trailer_hash
, idx
->pack
->mwf
.size
- GIT_OID_RAWSZ
, GIT_OID_RAWSZ
);
981 git_vector_sort(&idx
->objects
);
983 git_buf_sets(&filename
, idx
->pack
->pack_name
);
984 git_buf_shorten(&filename
, strlen("pack"));
985 git_buf_puts(&filename
, "idx");
986 if (git_buf_oom(&filename
))
989 if (git_filebuf_open(&index_file
, filename
.ptr
,
990 GIT_FILEBUF_HASH_CONTENTS
, idx
->mode
) < 0)
993 /* Write out the header */
994 hdr
.idx_signature
= htonl(PACK_IDX_SIGNATURE
);
995 hdr
.idx_version
= htonl(2);
996 git_filebuf_write(&index_file
, &hdr
, sizeof(hdr
));
998 /* Write out the fanout table */
999 for (i
= 0; i
< 256; ++i
) {
1000 uint32_t n
= htonl(idx
->fanout
[i
]);
1001 git_filebuf_write(&index_file
, &n
, sizeof(n
));
1004 /* Write out the object names (SHA-1 hashes) */
1005 git_vector_foreach(&idx
->objects
, i
, entry
) {
1006 git_filebuf_write(&index_file
, &entry
->oid
, sizeof(git_oid
));
1007 git_hash_update(&ctx
, &entry
->oid
, GIT_OID_RAWSZ
);
1009 git_hash_final(&idx
->hash
, &ctx
);
1011 /* Write out the CRC32 values */
1012 git_vector_foreach(&idx
->objects
, i
, entry
) {
1013 git_filebuf_write(&index_file
, &entry
->crc
, sizeof(uint32_t));
1016 /* Write out the offsets */
1017 git_vector_foreach(&idx
->objects
, i
, entry
) {
1020 if (entry
->offset
== UINT32_MAX
)
1021 n
= htonl(0x80000000 | long_offsets
++);
1023 n
= htonl(entry
->offset
);
1025 git_filebuf_write(&index_file
, &n
, sizeof(uint32_t));
1028 /* Write out the long offsets */
1029 git_vector_foreach(&idx
->objects
, i
, entry
) {
1032 if (entry
->offset
!= UINT32_MAX
)
1035 split
[0] = htonl(entry
->offset_long
>> 32);
1036 split
[1] = htonl(entry
->offset_long
& 0xffffffff);
1038 git_filebuf_write(&index_file
, &split
, sizeof(uint32_t) * 2);
1041 /* Write out the packfile trailer to the index */
1042 if (git_filebuf_write(&index_file
, &trailer_hash
, GIT_OID_RAWSZ
) < 0)
1045 /* Write out the hash of the idx */
1046 if (git_filebuf_hash(&trailer_hash
, &index_file
) < 0)
1049 git_filebuf_write(&index_file
, &trailer_hash
, sizeof(git_oid
));
1051 /* Figure out what the final name should be */
1052 if (index_path(&filename
, idx
, ".idx") < 0)
1056 if (git_filebuf_commit_at(&index_file
, filename
.ptr
) < 0)
1059 git_mwindow_free_all(&idx
->pack
->mwf
);
1061 /* Truncate file to undo rounding up to next page_size in append_to_pack */
1062 if (p_ftruncate(idx
->pack
->mwf
.fd
, idx
->pack
->mwf
.size
) < 0) {
1063 giterr_set(GITERR_OS
, "failed to truncate pack file '%s'", idx
->pack
->pack_name
);
1067 /* We need to close the descriptor here so Windows doesn't choke on commit_at */
1068 if (p_close(idx
->pack
->mwf
.fd
) < 0) {
1069 giterr_set(GITERR_OS
, "failed to close packfile");
1073 idx
->pack
->mwf
.fd
= -1;
1075 if (index_path(&filename
, idx
, ".pack") < 0)
1078 /* And don't forget to rename the packfile to its new place. */
1079 p_rename(idx
->pack
->pack_name
, git_buf_cstr(&filename
));
1081 git_buf_free(&filename
);
1082 git_hash_ctx_cleanup(&ctx
);
1086 git_mwindow_free_all(&idx
->pack
->mwf
);
1087 git_filebuf_cleanup(&index_file
);
1088 git_buf_free(&filename
);
1089 git_hash_ctx_cleanup(&ctx
);
1093 void git_indexer_free(git_indexer
*idx
)
1098 git_vector_free_deep(&idx
->objects
);
1100 if (idx
->pack
&& idx
->pack
->idx_cache
) {
1101 struct git_pack_entry
*pentry
;
1103 idx
->pack
->idx_cache
, pentry
, { git__free(pentry
); });
1105 git_oidmap_free(idx
->pack
->idx_cache
);
1108 git_vector_free_deep(&idx
->deltas
);
1110 if (!git_mutex_lock(&git__mwindow_mutex
)) {
1111 git_packfile_free(idx
->pack
);
1112 git_mutex_unlock(&git__mwindow_mutex
);
1115 git_hash_ctx_cleanup(&idx
->trailer
);
1116 git_hash_ctx_cleanup(&idx
->hash_ctx
);