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>
44 #include "got_error.h"
45 #include "got_object.h"
47 #include "got_lib_hash.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_inflate.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_parse.h"
52 #include "got_lib_object_idset.h"
53 #include "got_lib_privsep.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_ratelimit.h"
56 #include "got_lib_pack_index.h"
57 #include "got_lib_delta_cache.h"
59 struct got_indexed_object {
60 struct got_object_id id;
63 * Has this object been fully resolved?
64 * If so, we know its ID, otherwise we don't and 'id' is invalid.
68 /* Offset of type+size field for this object in pack file. */
71 /* Type+size values parsed from pack file. */
75 /* Length of on-disk type+size data. */
78 /* Length of object data following type+size. */
86 struct got_object_id ref_id;
89 /* For offset deltas. */
91 size_t base_offsetlen;
97 putbe32(char *b, uint32_t n)
105 static const struct got_error *
106 read_checksum(uint32_t *crc, struct got_hash *ctx, int fd, size_t len)
112 for (n = len; n > 0; n -= r){
113 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
115 return got_error_from_errno("read");
119 *crc = crc32(*crc, buf, r);
121 got_hash_update(ctx, buf, r);
127 static const struct got_error *
128 read_file_digest(struct got_hash *ctx, FILE *f, size_t len)
133 for (n = len; n > 0; n -= r) {
134 r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
138 return got_ferror(f, GOT_ERR_IO);
140 got_hash_update(ctx, buf, r);
146 static const struct got_error *
147 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
148 FILE *tmpfile, struct got_hash *pack_sha1_ctx)
150 const struct got_error *err = NULL;
152 uint8_t *data = NULL;
157 const char *obj_label;
158 size_t mapoff = obj->off;
159 struct got_inflate_checksum csum;
161 memset(&csum, 0, sizeof(csum));
162 csum.input_ctx = pack_sha1_ctx;
163 csum.input_crc = &obj->crc;
165 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
166 &obj->tslen, pack, obj->off);
171 obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
172 got_hash_update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
173 mapoff += obj->tslen;
175 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
176 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
177 return got_error_from_errno("lseek");
178 err = read_checksum(&obj->crc, pack_sha1_ctx,
179 pack->fd, obj->tslen);
185 case GOT_OBJ_TYPE_BLOB:
186 case GOT_OBJ_TYPE_COMMIT:
187 case GOT_OBJ_TYPE_TREE:
188 case GOT_OBJ_TYPE_TAG:
189 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
190 if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
191 err = got_error_from_errno("fseek");
195 err = got_inflate_to_file_mmap(&datalen,
196 &obj->len, &csum, pack->map, mapoff,
197 pack->filesize - mapoff, tmpfile);
199 err = got_inflate_to_file_fd(&datalen,
200 &obj->len, &csum, pack->fd, tmpfile);
204 err = got_inflate_to_mem_mmap(&data, &datalen,
205 &obj->len, &csum, pack->map, mapoff,
206 pack->filesize - mapoff);
208 err = got_inflate_to_mem_fd(&data, &datalen,
209 &obj->len, &csum, obj->size, pack->fd);
214 got_hash_init(&ctx, GOT_HASH_SHA1);
215 err = got_object_type_label(&obj_label, obj->type);
220 if (asprintf(&header, "%s %lld", obj_label,
221 (long long)obj->size) == -1) {
222 err = got_error_from_errno("asprintf");
226 headerlen = strlen(header) + 1;
227 got_hash_update(&ctx, header, headerlen);
228 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
229 err = read_file_digest(&ctx, tmpfile, datalen);
236 got_hash_update(&ctx, data, datalen);
237 got_hash_final_object_id(&ctx, &obj->id);
241 case GOT_OBJ_TYPE_REF_DELTA:
242 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
244 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
245 err = got_error(GOT_ERR_BAD_PACKFILE);
248 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
250 obj->crc = crc32(obj->crc, pack->map + mapoff,
252 got_hash_update(pack_sha1_ctx, pack->map + mapoff,
254 mapoff += SHA1_DIGEST_LENGTH;
255 err = got_inflate_to_mem_mmap(NULL, &datalen,
256 &obj->len, &csum, pack->map, mapoff,
257 pack->filesize - mapoff);
261 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
264 err = got_error_from_errno("read");
267 if (n < sizeof(obj->id)) {
268 err = got_error(GOT_ERR_BAD_PACKFILE);
271 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
273 got_hash_update(pack_sha1_ctx,
274 obj->delta.ref.ref_id.sha1, SHA1_DIGEST_LENGTH);
275 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
276 &csum, obj->size, pack->fd);
280 obj->len += SHA1_DIGEST_LENGTH;
282 case GOT_OBJ_TYPE_OFFSET_DELTA:
283 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
284 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
285 &obj->delta.ofs.base_offsetlen, pack, obj->off,
291 if (mapoff + obj->delta.ofs.base_offsetlen >=
293 err = got_error(GOT_ERR_BAD_PACKFILE);
297 if (mapoff + obj->delta.ofs.base_offsetlen >
299 err = got_error_fmt(GOT_ERR_RANGE,
300 "mapoff %lld would overflow size_t",
302 + obj->delta.ofs.base_offsetlen);
306 obj->crc = crc32(obj->crc, pack->map + mapoff,
307 obj->delta.ofs.base_offsetlen);
308 got_hash_update(pack_sha1_ctx, pack->map + mapoff,
309 obj->delta.ofs.base_offsetlen);
310 mapoff += obj->delta.ofs.base_offsetlen;
311 err = got_inflate_to_mem_mmap(NULL, &datalen,
312 &obj->len, &csum, pack->map, mapoff,
313 pack->filesize - mapoff);
318 * XXX Seek back and get CRC and SHA1 of on-disk
321 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
323 err = got_error_from_errno("lseek");
326 err = read_checksum(&obj->crc, pack_sha1_ctx,
327 pack->fd, obj->delta.ofs.base_offsetlen);
331 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
332 &csum, obj->size, pack->fd);
336 obj->len += obj->delta.ofs.base_offsetlen;
339 err = got_error(GOT_ERR_OBJ_TYPE);
346 const struct got_error *
347 got_pack_hwrite(int fd, void *buf, int len, struct got_hash *ctx)
351 got_hash_update(ctx, buf, len);
353 w = write(fd, buf, len);
355 return got_error_from_errno("write");
357 return got_error(GOT_ERR_IO);
362 static const struct got_error *
363 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
364 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
365 FILE *delta_accum_file)
367 const struct got_error *err = NULL;
368 struct got_delta_chain deltas;
369 struct got_delta *delta;
377 const char *obj_label;
380 STAILQ_INIT(&deltas.entries);
382 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
383 obj->off, obj->tslen, obj->type, obj->size,
384 GOT_DELTA_CHAIN_RECURSION_MAX);
388 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
391 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
393 rewind(delta_base_file);
394 rewind(delta_accum_file);
395 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
396 pack, tmpfile, delta_base_file, delta_accum_file);
400 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
406 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
409 err = got_object_type_label(&obj_label, base_obj_type);
412 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
413 err = got_error_from_errno("asprintf");
416 headerlen = strlen(header) + 1;
417 got_hash_init(&ctx, GOT_HASH_SHA1);
418 got_hash_update(&ctx, header, headerlen);
419 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
420 err = read_file_digest(&ctx, tmpfile, len);
424 got_hash_update(&ctx, buf, len);
425 got_hash_final_object_id(&ctx, &obj->id);
429 while (!STAILQ_EMPTY(&deltas.entries)) {
430 delta = STAILQ_FIRST(&deltas.entries);
431 STAILQ_REMOVE_HEAD(&deltas.entries, entry);
437 /* Determine the slot in the pack index a given object ID should use. */
439 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
441 u_int8_t id0 = sha1[0];
442 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
443 int left = 0, right = nindexed - 1;
447 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
449 while (left <= right) {
450 struct got_packidx_object_id *oid;
452 i = ((left + right) / 2);
453 oid = &packidx->hdr.sorted_ids[i];
455 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
457 return -1; /* object already indexed */
469 print_packidx(struct got_packidx *packidx)
471 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
474 fprintf(stderr, "object IDs:\n");
475 for (i = 0; i < nindexed; i++) {
476 char hex[SHA1_DIGEST_STRING_LENGTH];
477 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
479 fprintf(stderr, "%s\n", hex);
481 fprintf(stderr, "\n");
483 fprintf(stderr, "object offsets:\n");
484 for (i = 0; i < nindexed; i++) {
485 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
486 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
487 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
488 fprintf(stderr, "%u -> %llu\n", offset,
489 be64toh(packidx->hdr.large_offsets[j]));
491 fprintf(stderr, "%u\n", offset);
493 fprintf(stderr, "\n");
495 fprintf(stderr, "fanout table:");
496 for (i = 0; i <= 0xff; i++)
497 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
498 fprintf(stderr, "\n");
503 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
504 struct got_indexed_object *obj)
508 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
510 packidx->hdr.crc32[idx] = htobe32(obj->crc);
511 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
512 packidx->hdr.offsets[idx] = htobe32(obj->off);
514 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
515 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
516 packidx->hdr.large_offsets[packidx->nlargeobj] =
518 packidx->nlargeobj++;
521 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
522 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
523 packidx->hdr.fanout_table[i] = htobe32(n + 1);
528 indexed_obj_cmp(const void *pa, const void *pb)
530 struct got_indexed_object *a, *b;
532 a = (struct got_indexed_object *)pa;
533 b = (struct got_indexed_object *)pb;
534 return got_object_id_cmp(&a->id, &b->id);
538 make_packidx(struct got_packidx *packidx, uint32_t nobj,
539 struct got_indexed_object *objects)
541 struct got_indexed_object *obj;
545 qsort(objects, nobj, sizeof(struct got_indexed_object),
548 memset(packidx->hdr.fanout_table, 0,
549 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
550 packidx->nlargeobj = 0;
552 for (i = 0; i < nobj; i++) {
555 add_indexed_object(packidx, idx++, obj);
560 update_packidx(struct got_packidx *packidx, uint32_t nobj,
561 struct got_indexed_object *obj)
564 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
566 idx = find_object_idx(packidx, obj->id.sha1);
568 return; /* object already indexed */
570 memmove(&packidx->hdr.sorted_ids[idx + 1],
571 &packidx->hdr.sorted_ids[idx],
572 sizeof(struct got_packidx_object_id) * (nindexed - idx));
573 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
574 sizeof(uint32_t) * (nindexed - idx));
576 add_indexed_object(packidx, idx, obj);
579 static const struct got_error *
580 report_progress(uint32_t nobj_total, uint32_t nobj_indexed, uint32_t nobj_loose,
581 uint32_t nobj_resolved, struct got_ratelimit *rl,
582 got_pack_index_progress_cb progress_cb, void *progress_arg)
584 const struct got_error *err;
588 err = got_ratelimit_check(&elapsed, rl);
593 return progress_cb(progress_arg, nobj_total, nobj_indexed, nobj_loose,
597 const struct got_error *
598 got_pack_index(struct got_pack *pack, int idxfd, FILE *tmpfile,
599 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
600 got_pack_index_progress_cb progress_cb, void *progress_arg,
601 struct got_ratelimit *rl)
603 const struct got_error *err;
604 struct got_packfile_hdr hdr;
605 struct got_packidx packidx;
607 char pack_sha1[SHA1_DIGEST_LENGTH];
608 uint32_t nobj, nvalid, nloose, nresolved = 0, i;
609 struct got_indexed_object *objects = NULL, *obj;
611 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
613 int pass, have_ref_deltas = 0, first_delta_idx = -1;
615 int p_indexed = 0, last_p_indexed = -1;
616 int p_resolved = 0, last_p_resolved = -1;
618 /* Require that pack file header and SHA1 trailer are present. */
619 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
620 return got_error_msg(GOT_ERR_BAD_PACKFILE,
624 memcpy(&hdr, pack->map, sizeof(hdr));
625 mapoff += sizeof(hdr);
627 r = read(pack->fd, &hdr, sizeof(hdr));
629 return got_error_from_errno("read");
631 return got_error_msg(GOT_ERR_BAD_PACKFILE,
635 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
636 return got_error_msg(GOT_ERR_BAD_PACKFILE,
637 "bad packfile signature");
638 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
639 return got_error_msg(GOT_ERR_BAD_PACKFILE,
640 "bad packfile version");
641 nobj = be32toh(hdr.nobjects);
643 return got_error_msg(GOT_ERR_BAD_PACKFILE,
644 "bad packfile with zero objects");
646 /* We compute the SHA1 of pack file contents and verify later on. */
647 got_hash_init(&ctx, GOT_HASH_SHA1);
648 got_hash_update(&ctx, &hdr, sizeof(hdr));
651 * Create an in-memory pack index which will grow as objects
652 * IDs in the pack file are discovered. Only fields used to
653 * read deltified objects will be needed by the pack.c library
654 * code, so setting up just a pack index header is sufficient.
656 memset(&packidx, 0, sizeof(packidx));
657 packidx.hdr.magic = malloc(sizeof(uint32_t));
658 if (packidx.hdr.magic == NULL)
659 return got_error_from_errno("malloc");
660 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
661 packidx.hdr.version = malloc(sizeof(uint32_t));
662 if (packidx.hdr.version == NULL) {
663 err = got_error_from_errno("malloc");
666 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
667 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
669 if (packidx.hdr.fanout_table == NULL) {
670 err = got_error_from_errno("calloc");
673 packidx.hdr.sorted_ids = calloc(nobj,
674 sizeof(struct got_packidx_object_id));
675 if (packidx.hdr.sorted_ids == NULL) {
676 err = got_error_from_errno("calloc");
679 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
680 if (packidx.hdr.crc32 == NULL) {
681 err = got_error_from_errno("calloc");
684 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
685 if (packidx.hdr.offsets == NULL) {
686 err = got_error_from_errno("calloc");
689 /* Large offsets table is empty for pack files < 2 GB. */
690 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
691 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
692 if (packidx.hdr.large_offsets == NULL) {
693 err = got_error_from_errno("calloc");
700 objects = calloc(nobj, sizeof(struct got_indexed_object));
702 return got_error_from_errno("calloc");
705 * First pass: locate all objects and identify un-deltified objects.
707 * When this pass has completed we will know offset, type, size, and
708 * CRC information for all objects in this pack file. We won't know
709 * any of the actual object IDs of deltified objects yet since we
710 * will not yet attempt to combine deltas.
713 for (i = 0; i < nobj; i++) {
714 /* Don't send too many progress privsep messages. */
715 p_indexed = ((i + 1) * 100) / nobj;
716 if (p_indexed != last_p_indexed) {
717 err = report_progress(nobj, i + 1, nloose, 0,
718 rl, progress_cb, progress_arg);
721 last_p_indexed = p_indexed;
725 obj->crc = crc32(0L, NULL, 0);
727 /* Store offset to type+size information for this object. */
731 obj->off = lseek(pack->fd, 0, SEEK_CUR);
732 if (obj->off == -1) {
733 err = got_error_from_errno("lseek");
738 err = read_packed_object(pack, obj, tmpfile, &ctx);
743 mapoff += obj->tslen + obj->len;
745 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
747 err = got_error_from_errno("lseek");
752 if (obj->type == GOT_OBJ_TYPE_BLOB ||
753 obj->type == GOT_OBJ_TYPE_TREE ||
754 obj->type == GOT_OBJ_TYPE_COMMIT ||
755 obj->type == GOT_OBJ_TYPE_TAG) {
759 if (first_delta_idx == -1)
761 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
768 * Having done a full pass over the pack file and can now
769 * verify its checksum.
771 got_hash_final(&ctx, pack_sha1);
773 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
774 err = got_error(GOT_ERR_PACKFILE_CSUM);
778 /* Verify the SHA1 checksum stored at the end of the pack file. */
780 if (pack->filesize > SIZE_MAX) {
781 err = got_error_fmt(GOT_ERR_RANGE,
782 "filesize %lld overflows size_t",
783 (long long)pack->filesize);
787 memcpy(pack_sha1_expected, pack->map +
788 pack->filesize - SHA1_DIGEST_LENGTH,
792 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
793 err = got_error_from_errno("lseek");
796 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
798 err = got_error_from_errno("read");
801 if (n != SHA1_DIGEST_LENGTH) {
802 err = got_error(GOT_ERR_IO);
806 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
807 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
808 "bad checksum in pack file trailer");
812 if (first_delta_idx == -1)
815 /* In order to resolve ref deltas we need an in-progress pack index. */
817 make_packidx(&packidx, nobj, objects);
820 * Second pass: We can now resolve deltas to compute the IDs of
821 * objects which appear in deltified form. Because deltas can be
822 * chained this pass may require a couple of iterations until all
823 * IDs of deltified objects have been discovered.
826 while (nvalid != nobj) {
829 * This loop will only run once unless the pack file
830 * contains ref deltas which refer to objects located
831 * later in the pack file, which is unusual.
832 * Offset deltas can always be resolved in one pass
833 * unless the packfile is corrupt.
835 for (i = first_delta_idx; i < nobj; i++) {
837 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
838 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
844 if (pack->map == NULL && lseek(pack->fd,
845 obj->off + obj->tslen, SEEK_SET) == -1) {
846 err = got_error_from_errno("lseek");
850 err = resolve_deltified_object(pack, &packidx, obj,
851 tmpfile, delta_base_file, delta_accum_file);
853 if (err->code != GOT_ERR_NO_OBJ)
856 * We cannot resolve this object yet because
857 * a delta base is unknown. Try again later.
865 update_packidx(&packidx, nobj, obj);
866 /* Don't send too many progress privsep messages. */
867 p_resolved = ((nresolved + n) * 100) / nobj;
868 if (p_resolved != last_p_resolved) {
869 err = report_progress(nobj, nobj,
870 nloose, nresolved + n, rl,
871 progress_cb, progress_arg);
874 last_p_resolved = p_resolved;
878 if (pass++ > 3 && n == 0) {
879 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
880 "could not resolve any of deltas; packfile could "
888 if (nloose + nresolved != nobj) {
890 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
891 nloose + nresolved, nobj);
892 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
896 err = report_progress(nobj, nobj, nloose, nresolved, NULL,
897 progress_cb, progress_arg);
901 make_packidx(&packidx, nobj, objects);
906 got_hash_init(&ctx, GOT_HASH_SHA1);
907 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
908 putbe32(buf + 4, GOT_PACKIDX_VERSION);
909 err = got_pack_hwrite(idxfd, buf, 8, &ctx);
912 err = got_pack_hwrite(idxfd, packidx.hdr.fanout_table,
913 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
916 err = got_pack_hwrite(idxfd, packidx.hdr.sorted_ids,
917 nobj * SHA1_DIGEST_LENGTH, &ctx);
920 err = got_pack_hwrite(idxfd, packidx.hdr.crc32,
921 nobj * sizeof(uint32_t), &ctx);
924 err = got_pack_hwrite(idxfd, packidx.hdr.offsets,
925 nobj * sizeof(uint32_t), &ctx);
928 if (packidx.nlargeobj > 0) {
929 err = got_pack_hwrite(idxfd, packidx.hdr.large_offsets,
930 packidx.nlargeobj * sizeof(uint64_t), &ctx);
934 err = got_pack_hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
938 got_hash_final(&ctx, packidx_hash);
939 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
941 err = got_error_from_errno("write");
944 if (w != sizeof(packidx_hash)) {
945 err = got_error(GOT_ERR_IO);
950 free(packidx.hdr.magic);
951 free(packidx.hdr.version);
952 free(packidx.hdr.fanout_table);
953 free(packidx.hdr.sorted_ids);
954 free(packidx.hdr.offsets);
955 free(packidx.hdr.large_offsets);