2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2020, 2022 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/queue.h>
21 #include <sys/types.h>
43 #include "got_error.h"
44 #include "got_object.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_parse.h"
51 #include "got_lib_object_idset.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_ratelimit.h"
55 #include "got_lib_pack_index.h"
56 #include "got_lib_delta_cache.h"
58 struct got_indexed_object {
59 struct got_object_id id;
62 * Has this object been fully resolved?
63 * If so, we know its ID, otherwise we don't and 'id' is invalid.
67 /* Offset of type+size field for this object in pack file. */
70 /* Type+size values parsed from pack file. */
74 /* Length of on-disk type+size data. */
77 /* Length of object data following type+size. */
85 struct got_object_id ref_id;
88 /* For offset deltas. */
90 size_t base_offsetlen;
96 putbe32(char *b, uint32_t n)
104 static const struct got_error *
105 get_obj_type_label(const char **label, int obj_type)
107 const struct got_error *err = NULL;
110 case GOT_OBJ_TYPE_BLOB:
111 *label = GOT_OBJ_LABEL_BLOB;
113 case GOT_OBJ_TYPE_TREE:
114 *label = GOT_OBJ_LABEL_TREE;
116 case GOT_OBJ_TYPE_COMMIT:
117 *label = GOT_OBJ_LABEL_COMMIT;
119 case GOT_OBJ_TYPE_TAG:
120 *label = GOT_OBJ_LABEL_TAG;
124 err = got_error(GOT_ERR_OBJ_TYPE);
131 static const struct got_error *
132 read_checksum(uint32_t *crc, SHA1_CTX *sha1_ctx, int fd, size_t len)
138 for (n = len; n > 0; n -= r){
139 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
141 return got_error_from_errno("read");
145 *crc = crc32(*crc, buf, r);
147 SHA1Update(sha1_ctx, buf, r);
153 static const struct got_error *
154 read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
159 for (n = len; n > 0; n -= r) {
160 r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
164 return got_ferror(f, GOT_ERR_IO);
166 SHA1Update(ctx, buf, r);
172 static const struct got_error *
173 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
174 FILE *tmpfile, SHA1_CTX *pack_sha1_ctx)
176 const struct got_error *err = NULL;
178 uint8_t *data = NULL;
183 const char *obj_label;
184 size_t mapoff = obj->off;
185 struct got_inflate_checksum csum;
187 memset(&csum, 0, sizeof(csum));
188 csum.input_sha1 = pack_sha1_ctx;
189 csum.input_crc = &obj->crc;
191 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
192 &obj->tslen, pack, obj->off);
197 obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
198 SHA1Update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
199 mapoff += obj->tslen;
201 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
202 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
203 return got_error_from_errno("lseek");
204 err = read_checksum(&obj->crc, pack_sha1_ctx,
205 pack->fd, obj->tslen);
211 case GOT_OBJ_TYPE_BLOB:
212 case GOT_OBJ_TYPE_COMMIT:
213 case GOT_OBJ_TYPE_TREE:
214 case GOT_OBJ_TYPE_TAG:
215 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
216 if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
217 err = got_error_from_errno("fseek");
221 err = got_inflate_to_file_mmap(&datalen,
222 &obj->len, &csum, pack->map, mapoff,
223 pack->filesize - mapoff, tmpfile);
225 err = got_inflate_to_file_fd(&datalen,
226 &obj->len, &csum, pack->fd, tmpfile);
230 err = got_inflate_to_mem_mmap(&data, &datalen,
231 &obj->len, &csum, pack->map, mapoff,
232 pack->filesize - mapoff);
234 err = got_inflate_to_mem_fd(&data, &datalen,
235 &obj->len, &csum, obj->size, pack->fd);
241 err = get_obj_type_label(&obj_label, obj->type);
246 if (asprintf(&header, "%s %lld", obj_label,
247 (long long)obj->size) == -1) {
248 err = got_error_from_errno("asprintf");
252 headerlen = strlen(header) + 1;
253 SHA1Update(&ctx, header, headerlen);
254 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
255 err = read_file_sha1(&ctx, tmpfile, datalen);
262 SHA1Update(&ctx, data, datalen);
263 SHA1Final(obj->id.sha1, &ctx);
267 case GOT_OBJ_TYPE_REF_DELTA:
268 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
270 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
271 err = got_error(GOT_ERR_BAD_PACKFILE);
274 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
276 obj->crc = crc32(obj->crc, pack->map + mapoff,
278 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
280 mapoff += SHA1_DIGEST_LENGTH;
281 err = got_inflate_to_mem_mmap(NULL, &datalen,
282 &obj->len, &csum, pack->map, mapoff,
283 pack->filesize - mapoff);
287 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
290 err = got_error_from_errno("read");
293 if (n < sizeof(obj->id)) {
294 err = got_error(GOT_ERR_BAD_PACKFILE);
297 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
299 SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
301 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
302 &csum, obj->size, pack->fd);
306 obj->len += SHA1_DIGEST_LENGTH;
308 case GOT_OBJ_TYPE_OFFSET_DELTA:
309 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
310 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
311 &obj->delta.ofs.base_offsetlen, pack, obj->off,
317 obj->crc = crc32(obj->crc, pack->map + mapoff,
318 obj->delta.ofs.base_offsetlen);
319 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
320 obj->delta.ofs.base_offsetlen);
321 mapoff += obj->delta.ofs.base_offsetlen;
322 err = got_inflate_to_mem_mmap(NULL, &datalen,
323 &obj->len, &csum, pack->map, mapoff,
324 pack->filesize - mapoff);
329 * XXX Seek back and get CRC and SHA1 of on-disk
332 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
334 err = got_error_from_errno("lseek");
337 err = read_checksum(&obj->crc, pack_sha1_ctx,
338 pack->fd, obj->delta.ofs.base_offsetlen);
342 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
343 &csum, obj->size, pack->fd);
347 obj->len += obj->delta.ofs.base_offsetlen;
350 err = got_error(GOT_ERR_OBJ_TYPE);
357 const struct got_error *
358 got_pack_hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
362 SHA1Update(ctx, buf, len);
364 w = write(fd, buf, len);
366 return got_error_from_errno("write");
368 return got_error(GOT_ERR_IO);
373 static const struct got_error *
374 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
375 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
376 FILE *delta_accum_file)
378 const struct got_error *err = NULL;
379 struct got_delta_chain deltas;
380 struct got_delta *delta;
388 const char *obj_label;
391 STAILQ_INIT(&deltas.entries);
393 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
394 obj->off, obj->tslen, obj->type, obj->size,
395 GOT_DELTA_CHAIN_RECURSION_MAX);
399 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
402 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
404 rewind(delta_base_file);
405 rewind(delta_accum_file);
406 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
407 pack, tmpfile, delta_base_file, delta_accum_file);
411 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
417 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
420 err = get_obj_type_label(&obj_label, base_obj_type);
423 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
424 err = got_error_from_errno("asprintf");
427 headerlen = strlen(header) + 1;
429 SHA1Update(&ctx, header, headerlen);
430 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
431 err = read_file_sha1(&ctx, tmpfile, len);
435 SHA1Update(&ctx, buf, len);
436 SHA1Final(obj->id.sha1, &ctx);
440 while (!STAILQ_EMPTY(&deltas.entries)) {
441 delta = STAILQ_FIRST(&deltas.entries);
442 STAILQ_REMOVE_HEAD(&deltas.entries, entry);
448 /* Determine the slot in the pack index a given object ID should use. */
450 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
452 u_int8_t id0 = sha1[0];
453 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
454 int left = 0, right = nindexed - 1;
458 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
460 while (left <= right) {
461 struct got_packidx_object_id *oid;
463 i = ((left + right) / 2);
464 oid = &packidx->hdr.sorted_ids[i];
466 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
468 return -1; /* object already indexed */
480 print_packidx(struct got_packidx *packidx)
482 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
485 fprintf(stderr, "object IDs:\n");
486 for (i = 0; i < nindexed; i++) {
487 char hex[SHA1_DIGEST_STRING_LENGTH];
488 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
490 fprintf(stderr, "%s\n", hex);
492 fprintf(stderr, "\n");
494 fprintf(stderr, "object offsets:\n");
495 for (i = 0; i < nindexed; i++) {
496 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
497 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
498 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
499 fprintf(stderr, "%u -> %llu\n", offset,
500 be64toh(packidx->hdr.large_offsets[j]));
502 fprintf(stderr, "%u\n", offset);
504 fprintf(stderr, "\n");
506 fprintf(stderr, "fanout table:");
507 for (i = 0; i <= 0xff; i++)
508 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
509 fprintf(stderr, "\n");
514 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
515 struct got_indexed_object *obj)
519 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
521 packidx->hdr.crc32[idx] = htobe32(obj->crc);
522 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
523 packidx->hdr.offsets[idx] = htobe32(obj->off);
525 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
526 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
527 packidx->hdr.large_offsets[packidx->nlargeobj] =
529 packidx->nlargeobj++;
532 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
533 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
534 packidx->hdr.fanout_table[i] = htobe32(n + 1);
539 indexed_obj_cmp(const void *pa, const void *pb)
541 struct got_indexed_object *a, *b;
543 a = (struct got_indexed_object *)pa;
544 b = (struct got_indexed_object *)pb;
545 return got_object_id_cmp(&a->id, &b->id);
549 make_packidx(struct got_packidx *packidx, uint32_t nobj,
550 struct got_indexed_object *objects)
552 struct got_indexed_object *obj;
556 qsort(objects, nobj, sizeof(struct got_indexed_object),
559 memset(packidx->hdr.fanout_table, 0,
560 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
561 packidx->nlargeobj = 0;
563 for (i = 0; i < nobj; i++) {
566 add_indexed_object(packidx, idx++, obj);
571 update_packidx(struct got_packidx *packidx, uint32_t nobj,
572 struct got_indexed_object *obj)
575 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
577 idx = find_object_idx(packidx, obj->id.sha1);
579 char hex[SHA1_DIGEST_STRING_LENGTH];
580 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
581 return; /* object already indexed */
584 memmove(&packidx->hdr.sorted_ids[idx + 1],
585 &packidx->hdr.sorted_ids[idx],
586 sizeof(struct got_packidx_object_id) * (nindexed - idx));
587 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
588 sizeof(uint32_t) * (nindexed - idx));
590 add_indexed_object(packidx, idx, obj);
593 static const struct got_error *
594 report_progress(uint32_t nobj_total, uint32_t nobj_indexed, uint32_t nobj_loose,
595 uint32_t nobj_resolved, struct got_ratelimit *rl,
596 got_pack_index_progress_cb progress_cb, void *progress_arg)
598 const struct got_error *err;
602 err = got_ratelimit_check(&elapsed, rl);
607 return progress_cb(progress_arg, nobj_total, nobj_indexed, nobj_loose,
611 const struct got_error *
612 got_pack_index(struct got_pack *pack, int idxfd, FILE *tmpfile,
613 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
614 got_pack_index_progress_cb progress_cb, void *progress_arg,
615 struct got_ratelimit *rl)
617 const struct got_error *err;
618 struct got_packfile_hdr hdr;
619 struct got_packidx packidx;
621 char pack_sha1[SHA1_DIGEST_LENGTH];
622 uint32_t nobj, nvalid, nloose, nresolved = 0, i;
623 struct got_indexed_object *objects = NULL, *obj;
625 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
627 int pass, have_ref_deltas = 0, first_delta_idx = -1;
629 int p_indexed = 0, last_p_indexed = -1;
630 int p_resolved = 0, last_p_resolved = -1;
632 /* Require that pack file header and SHA1 trailer are present. */
633 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
634 return got_error_msg(GOT_ERR_BAD_PACKFILE,
638 memcpy(&hdr, pack->map, sizeof(hdr));
639 mapoff += sizeof(hdr);
641 r = read(pack->fd, &hdr, sizeof(hdr));
643 return got_error_from_errno("read");
645 return got_error_msg(GOT_ERR_BAD_PACKFILE,
649 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
650 return got_error_msg(GOT_ERR_BAD_PACKFILE,
651 "bad packfile signature");
652 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
653 return got_error_msg(GOT_ERR_BAD_PACKFILE,
654 "bad packfile version");
655 nobj = be32toh(hdr.nobjects);
657 return got_error_msg(GOT_ERR_BAD_PACKFILE,
658 "bad packfile with zero objects");
660 /* We compute the SHA1 of pack file contents and verify later on. */
662 SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
665 * Create an in-memory pack index which will grow as objects
666 * IDs in the pack file are discovered. Only fields used to
667 * read deltified objects will be needed by the pack.c library
668 * code, so setting up just a pack index header is sufficient.
670 memset(&packidx, 0, sizeof(packidx));
671 packidx.hdr.magic = malloc(sizeof(uint32_t));
672 if (packidx.hdr.magic == NULL)
673 return got_error_from_errno("malloc");
674 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
675 packidx.hdr.version = malloc(sizeof(uint32_t));
676 if (packidx.hdr.version == NULL) {
677 err = got_error_from_errno("malloc");
680 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
681 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
683 if (packidx.hdr.fanout_table == NULL) {
684 err = got_error_from_errno("calloc");
687 packidx.hdr.sorted_ids = calloc(nobj,
688 sizeof(struct got_packidx_object_id));
689 if (packidx.hdr.sorted_ids == NULL) {
690 err = got_error_from_errno("calloc");
693 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
694 if (packidx.hdr.crc32 == NULL) {
695 err = got_error_from_errno("calloc");
698 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
699 if (packidx.hdr.offsets == NULL) {
700 err = got_error_from_errno("calloc");
703 /* Large offsets table is empty for pack files < 2 GB. */
704 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
705 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
706 if (packidx.hdr.large_offsets == NULL) {
707 err = got_error_from_errno("calloc");
714 objects = calloc(nobj, sizeof(struct got_indexed_object));
716 return got_error_from_errno("calloc");
719 * First pass: locate all objects and identify un-deltified objects.
721 * When this pass has completed we will know offset, type, size, and
722 * CRC information for all objects in this pack file. We won't know
723 * any of the actual object IDs of deltified objects yet since we
724 * will not yet attempt to combine deltas.
727 for (i = 0; i < nobj; i++) {
728 /* Don't send too many progress privsep messages. */
729 p_indexed = ((i + 1) * 100) / nobj;
730 if (p_indexed != last_p_indexed) {
731 err = report_progress(nobj, i + 1, nloose, 0,
732 rl, progress_cb, progress_arg);
735 last_p_indexed = p_indexed;
739 obj->crc = crc32(0L, NULL, 0);
741 /* Store offset to type+size information for this object. */
745 obj->off = lseek(pack->fd, 0, SEEK_CUR);
746 if (obj->off == -1) {
747 err = got_error_from_errno("lseek");
752 err = read_packed_object(pack, obj, tmpfile, &ctx);
757 mapoff += obj->tslen + obj->len;
759 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
761 err = got_error_from_errno("lseek");
766 if (obj->type == GOT_OBJ_TYPE_BLOB ||
767 obj->type == GOT_OBJ_TYPE_TREE ||
768 obj->type == GOT_OBJ_TYPE_COMMIT ||
769 obj->type == GOT_OBJ_TYPE_TAG) {
773 if (first_delta_idx == -1)
775 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
782 * Having done a full pass over the pack file and can now
783 * verify its checksum.
785 SHA1Final(pack_sha1, &ctx);
787 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
788 err = got_error(GOT_ERR_PACKFILE_CSUM);
792 /* Verify the SHA1 checksum stored at the end of the pack file. */
794 memcpy(pack_sha1_expected, pack->map +
795 pack->filesize - SHA1_DIGEST_LENGTH,
799 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
800 err = got_error_from_errno("lseek");
803 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
805 err = got_error_from_errno("read");
808 if (n != SHA1_DIGEST_LENGTH) {
809 err = got_error(GOT_ERR_IO);
813 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
814 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
815 "bad checksum in pack file trailer");
819 if (first_delta_idx == -1)
822 /* In order to resolve ref deltas we need an in-progress pack index. */
824 make_packidx(&packidx, nobj, objects);
827 * Second pass: We can now resolve deltas to compute the IDs of
828 * objects which appear in deltified form. Because deltas can be
829 * chained this pass may require a couple of iterations until all
830 * IDs of deltified objects have been discovered.
833 while (nvalid != nobj) {
836 * This loop will only run once unless the pack file
837 * contains ref deltas which refer to objects located
838 * later in the pack file, which is unusual.
839 * Offset deltas can always be resolved in one pass
840 * unless the packfile is corrupt.
842 for (i = first_delta_idx; i < nobj; i++) {
844 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
845 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
851 if (pack->map == NULL && lseek(pack->fd,
852 obj->off + obj->tslen, SEEK_SET) == -1) {
853 err = got_error_from_errno("lseek");
857 err = resolve_deltified_object(pack, &packidx, obj,
858 tmpfile, delta_base_file, delta_accum_file);
860 if (err->code != GOT_ERR_NO_OBJ)
863 * We cannot resolve this object yet because
864 * a delta base is unknown. Try again later.
872 update_packidx(&packidx, nobj, obj);
873 /* Don't send too many progress privsep messages. */
874 p_resolved = ((nresolved + n) * 100) / nobj;
875 if (p_resolved != last_p_resolved) {
876 err = report_progress(nobj, nobj,
877 nloose, nresolved + n, rl,
878 progress_cb, progress_arg);
881 last_p_resolved = p_resolved;
885 if (pass++ > 3 && n == 0) {
886 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
887 "could not resolve any of deltas; packfile could "
895 if (nloose + nresolved != nobj) {
897 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
898 nloose + nresolved, nobj);
899 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
903 err = report_progress(nobj, nobj, nloose, nresolved, NULL,
904 progress_cb, progress_arg);
908 make_packidx(&packidx, nobj, objects);
914 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
915 putbe32(buf + 4, GOT_PACKIDX_VERSION);
916 err = got_pack_hwrite(idxfd, buf, 8, &ctx);
919 err = got_pack_hwrite(idxfd, packidx.hdr.fanout_table,
920 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
923 err = got_pack_hwrite(idxfd, packidx.hdr.sorted_ids,
924 nobj * SHA1_DIGEST_LENGTH, &ctx);
927 err = got_pack_hwrite(idxfd, packidx.hdr.crc32,
928 nobj * sizeof(uint32_t), &ctx);
931 err = got_pack_hwrite(idxfd, packidx.hdr.offsets,
932 nobj * sizeof(uint32_t), &ctx);
935 if (packidx.nlargeobj > 0) {
936 err = got_pack_hwrite(idxfd, packidx.hdr.large_offsets,
937 packidx.nlargeobj * sizeof(uint64_t), &ctx);
941 err = got_pack_hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
945 SHA1Final(packidx_hash, &ctx);
946 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
948 err = got_error_from_errno("write");
951 if (w != sizeof(packidx_hash)) {
952 err = got_error(GOT_ERR_IO);
957 free(packidx.hdr.magic);
958 free(packidx.hdr.version);
959 free(packidx.hdr.fanout_table);
960 free(packidx.hdr.sorted_ids);
961 free(packidx.hdr.offsets);
962 free(packidx.hdr.large_offsets);