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 if (mapoff + SHA1_DIGEST_LENGTH > SIZE_MAX) {
275 err = got_error_fmt(GOT_ERR_RANGE,
276 "mapoff %lld would overflow size_t",
277 (long long)mapoff + SHA1_DIGEST_LENGTH);
280 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
282 obj->crc = crc32(obj->crc, pack->map + mapoff,
284 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
286 mapoff += SHA1_DIGEST_LENGTH;
287 err = got_inflate_to_mem_mmap(NULL, &datalen,
288 &obj->len, &csum, pack->map, mapoff,
289 pack->filesize - mapoff);
293 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
296 err = got_error_from_errno("read");
299 if (n < sizeof(obj->id)) {
300 err = got_error(GOT_ERR_BAD_PACKFILE);
303 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
305 SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
307 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
308 &csum, obj->size, pack->fd);
312 obj->len += SHA1_DIGEST_LENGTH;
314 case GOT_OBJ_TYPE_OFFSET_DELTA:
315 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
316 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
317 &obj->delta.ofs.base_offsetlen, pack, obj->off,
323 if (mapoff + obj->delta.ofs.base_offsetlen >=
325 err = got_error(GOT_ERR_BAD_PACKFILE);
329 if (mapoff + obj->delta.ofs.base_offsetlen >
331 err = got_error_fmt(GOT_ERR_RANGE,
332 "mapoff %lld would overflow size_t",
334 + obj->delta.ofs.base_offsetlen);
337 obj->crc = crc32(obj->crc, pack->map + mapoff,
338 obj->delta.ofs.base_offsetlen);
339 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
340 obj->delta.ofs.base_offsetlen);
341 mapoff += obj->delta.ofs.base_offsetlen;
342 err = got_inflate_to_mem_mmap(NULL, &datalen,
343 &obj->len, &csum, pack->map, mapoff,
344 pack->filesize - mapoff);
349 * XXX Seek back and get CRC and SHA1 of on-disk
352 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
354 err = got_error_from_errno("lseek");
357 err = read_checksum(&obj->crc, pack_sha1_ctx,
358 pack->fd, obj->delta.ofs.base_offsetlen);
362 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
363 &csum, obj->size, pack->fd);
367 obj->len += obj->delta.ofs.base_offsetlen;
370 err = got_error(GOT_ERR_OBJ_TYPE);
377 const struct got_error *
378 got_pack_hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
382 SHA1Update(ctx, buf, len);
384 w = write(fd, buf, len);
386 return got_error_from_errno("write");
388 return got_error(GOT_ERR_IO);
393 static const struct got_error *
394 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
395 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
396 FILE *delta_accum_file)
398 const struct got_error *err = NULL;
399 struct got_delta_chain deltas;
400 struct got_delta *delta;
408 const char *obj_label;
411 STAILQ_INIT(&deltas.entries);
413 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
414 obj->off, obj->tslen, obj->type, obj->size,
415 GOT_DELTA_CHAIN_RECURSION_MAX);
419 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
422 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
424 rewind(delta_base_file);
425 rewind(delta_accum_file);
426 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
427 pack, tmpfile, delta_base_file, delta_accum_file);
431 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
437 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
440 err = get_obj_type_label(&obj_label, base_obj_type);
443 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
444 err = got_error_from_errno("asprintf");
447 headerlen = strlen(header) + 1;
449 SHA1Update(&ctx, header, headerlen);
450 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
451 err = read_file_sha1(&ctx, tmpfile, len);
455 SHA1Update(&ctx, buf, len);
456 SHA1Final(obj->id.sha1, &ctx);
460 while (!STAILQ_EMPTY(&deltas.entries)) {
461 delta = STAILQ_FIRST(&deltas.entries);
462 STAILQ_REMOVE_HEAD(&deltas.entries, entry);
468 /* Determine the slot in the pack index a given object ID should use. */
470 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
472 u_int8_t id0 = sha1[0];
473 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
474 int left = 0, right = nindexed - 1;
478 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
480 while (left <= right) {
481 struct got_packidx_object_id *oid;
483 i = ((left + right) / 2);
484 oid = &packidx->hdr.sorted_ids[i];
486 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
488 return -1; /* object already indexed */
500 print_packidx(struct got_packidx *packidx)
502 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
505 fprintf(stderr, "object IDs:\n");
506 for (i = 0; i < nindexed; i++) {
507 char hex[SHA1_DIGEST_STRING_LENGTH];
508 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
510 fprintf(stderr, "%s\n", hex);
512 fprintf(stderr, "\n");
514 fprintf(stderr, "object offsets:\n");
515 for (i = 0; i < nindexed; i++) {
516 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
517 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
518 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
519 fprintf(stderr, "%u -> %llu\n", offset,
520 be64toh(packidx->hdr.large_offsets[j]));
522 fprintf(stderr, "%u\n", offset);
524 fprintf(stderr, "\n");
526 fprintf(stderr, "fanout table:");
527 for (i = 0; i <= 0xff; i++)
528 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
529 fprintf(stderr, "\n");
534 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
535 struct got_indexed_object *obj)
539 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
541 packidx->hdr.crc32[idx] = htobe32(obj->crc);
542 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
543 packidx->hdr.offsets[idx] = htobe32(obj->off);
545 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
546 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
547 packidx->hdr.large_offsets[packidx->nlargeobj] =
549 packidx->nlargeobj++;
552 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
553 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
554 packidx->hdr.fanout_table[i] = htobe32(n + 1);
559 indexed_obj_cmp(const void *pa, const void *pb)
561 struct got_indexed_object *a, *b;
563 a = (struct got_indexed_object *)pa;
564 b = (struct got_indexed_object *)pb;
565 return got_object_id_cmp(&a->id, &b->id);
569 make_packidx(struct got_packidx *packidx, uint32_t nobj,
570 struct got_indexed_object *objects)
572 struct got_indexed_object *obj;
576 qsort(objects, nobj, sizeof(struct got_indexed_object),
579 memset(packidx->hdr.fanout_table, 0,
580 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
581 packidx->nlargeobj = 0;
583 for (i = 0; i < nobj; i++) {
586 add_indexed_object(packidx, idx++, obj);
591 update_packidx(struct got_packidx *packidx, uint32_t nobj,
592 struct got_indexed_object *obj)
595 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
597 idx = find_object_idx(packidx, obj->id.sha1);
599 return; /* object already indexed */
601 memmove(&packidx->hdr.sorted_ids[idx + 1],
602 &packidx->hdr.sorted_ids[idx],
603 sizeof(struct got_packidx_object_id) * (nindexed - idx));
604 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
605 sizeof(uint32_t) * (nindexed - idx));
607 add_indexed_object(packidx, idx, obj);
610 static const struct got_error *
611 report_progress(uint32_t nobj_total, uint32_t nobj_indexed, uint32_t nobj_loose,
612 uint32_t nobj_resolved, struct got_ratelimit *rl,
613 got_pack_index_progress_cb progress_cb, void *progress_arg)
615 const struct got_error *err;
619 err = got_ratelimit_check(&elapsed, rl);
624 return progress_cb(progress_arg, nobj_total, nobj_indexed, nobj_loose,
628 const struct got_error *
629 got_pack_index(struct got_pack *pack, int idxfd, FILE *tmpfile,
630 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
631 got_pack_index_progress_cb progress_cb, void *progress_arg,
632 struct got_ratelimit *rl)
634 const struct got_error *err;
635 struct got_packfile_hdr hdr;
636 struct got_packidx packidx;
638 char pack_sha1[SHA1_DIGEST_LENGTH];
639 uint32_t nobj, nvalid, nloose, nresolved = 0, i;
640 struct got_indexed_object *objects = NULL, *obj;
642 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
644 int pass, have_ref_deltas = 0, first_delta_idx = -1;
646 int p_indexed = 0, last_p_indexed = -1;
647 int p_resolved = 0, last_p_resolved = -1;
649 /* Require that pack file header and SHA1 trailer are present. */
650 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
651 return got_error_msg(GOT_ERR_BAD_PACKFILE,
655 memcpy(&hdr, pack->map, sizeof(hdr));
656 mapoff += sizeof(hdr);
658 r = read(pack->fd, &hdr, sizeof(hdr));
660 return got_error_from_errno("read");
662 return got_error_msg(GOT_ERR_BAD_PACKFILE,
666 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
667 return got_error_msg(GOT_ERR_BAD_PACKFILE,
668 "bad packfile signature");
669 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
670 return got_error_msg(GOT_ERR_BAD_PACKFILE,
671 "bad packfile version");
672 nobj = be32toh(hdr.nobjects);
674 return got_error_msg(GOT_ERR_BAD_PACKFILE,
675 "bad packfile with zero objects");
677 /* We compute the SHA1 of pack file contents and verify later on. */
679 SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
682 * Create an in-memory pack index which will grow as objects
683 * IDs in the pack file are discovered. Only fields used to
684 * read deltified objects will be needed by the pack.c library
685 * code, so setting up just a pack index header is sufficient.
687 memset(&packidx, 0, sizeof(packidx));
688 packidx.hdr.magic = malloc(sizeof(uint32_t));
689 if (packidx.hdr.magic == NULL)
690 return got_error_from_errno("malloc");
691 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
692 packidx.hdr.version = malloc(sizeof(uint32_t));
693 if (packidx.hdr.version == NULL) {
694 err = got_error_from_errno("malloc");
697 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
698 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
700 if (packidx.hdr.fanout_table == NULL) {
701 err = got_error_from_errno("calloc");
704 packidx.hdr.sorted_ids = calloc(nobj,
705 sizeof(struct got_packidx_object_id));
706 if (packidx.hdr.sorted_ids == NULL) {
707 err = got_error_from_errno("calloc");
710 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
711 if (packidx.hdr.crc32 == NULL) {
712 err = got_error_from_errno("calloc");
715 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
716 if (packidx.hdr.offsets == NULL) {
717 err = got_error_from_errno("calloc");
720 /* Large offsets table is empty for pack files < 2 GB. */
721 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
722 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
723 if (packidx.hdr.large_offsets == NULL) {
724 err = got_error_from_errno("calloc");
731 objects = calloc(nobj, sizeof(struct got_indexed_object));
733 return got_error_from_errno("calloc");
736 * First pass: locate all objects and identify un-deltified objects.
738 * When this pass has completed we will know offset, type, size, and
739 * CRC information for all objects in this pack file. We won't know
740 * any of the actual object IDs of deltified objects yet since we
741 * will not yet attempt to combine deltas.
744 for (i = 0; i < nobj; i++) {
745 /* Don't send too many progress privsep messages. */
746 p_indexed = ((i + 1) * 100) / nobj;
747 if (p_indexed != last_p_indexed) {
748 err = report_progress(nobj, i + 1, nloose, 0,
749 rl, progress_cb, progress_arg);
752 last_p_indexed = p_indexed;
756 obj->crc = crc32(0L, NULL, 0);
758 /* Store offset to type+size information for this object. */
762 obj->off = lseek(pack->fd, 0, SEEK_CUR);
763 if (obj->off == -1) {
764 err = got_error_from_errno("lseek");
769 err = read_packed_object(pack, obj, tmpfile, &ctx);
774 mapoff += obj->tslen + obj->len;
776 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
778 err = got_error_from_errno("lseek");
783 if (obj->type == GOT_OBJ_TYPE_BLOB ||
784 obj->type == GOT_OBJ_TYPE_TREE ||
785 obj->type == GOT_OBJ_TYPE_COMMIT ||
786 obj->type == GOT_OBJ_TYPE_TAG) {
790 if (first_delta_idx == -1)
792 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
799 * Having done a full pass over the pack file and can now
800 * verify its checksum.
802 SHA1Final(pack_sha1, &ctx);
804 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
805 err = got_error(GOT_ERR_PACKFILE_CSUM);
809 /* Verify the SHA1 checksum stored at the end of the pack file. */
811 if (pack->filesize > SIZE_MAX) {
812 err = got_error_fmt(GOT_ERR_RANGE,
813 "filesize %lld overflows size_t",
814 (long long)pack->filesize);
818 memcpy(pack_sha1_expected, pack->map +
819 pack->filesize - SHA1_DIGEST_LENGTH,
823 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
824 err = got_error_from_errno("lseek");
827 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
829 err = got_error_from_errno("read");
832 if (n != SHA1_DIGEST_LENGTH) {
833 err = got_error(GOT_ERR_IO);
837 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
838 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
839 "bad checksum in pack file trailer");
843 if (first_delta_idx == -1)
846 /* In order to resolve ref deltas we need an in-progress pack index. */
848 make_packidx(&packidx, nobj, objects);
851 * Second pass: We can now resolve deltas to compute the IDs of
852 * objects which appear in deltified form. Because deltas can be
853 * chained this pass may require a couple of iterations until all
854 * IDs of deltified objects have been discovered.
857 while (nvalid != nobj) {
860 * This loop will only run once unless the pack file
861 * contains ref deltas which refer to objects located
862 * later in the pack file, which is unusual.
863 * Offset deltas can always be resolved in one pass
864 * unless the packfile is corrupt.
866 for (i = first_delta_idx; i < nobj; i++) {
868 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
869 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
875 if (pack->map == NULL && lseek(pack->fd,
876 obj->off + obj->tslen, SEEK_SET) == -1) {
877 err = got_error_from_errno("lseek");
881 err = resolve_deltified_object(pack, &packidx, obj,
882 tmpfile, delta_base_file, delta_accum_file);
884 if (err->code != GOT_ERR_NO_OBJ)
887 * We cannot resolve this object yet because
888 * a delta base is unknown. Try again later.
896 update_packidx(&packidx, nobj, obj);
897 /* Don't send too many progress privsep messages. */
898 p_resolved = ((nresolved + n) * 100) / nobj;
899 if (p_resolved != last_p_resolved) {
900 err = report_progress(nobj, nobj,
901 nloose, nresolved + n, rl,
902 progress_cb, progress_arg);
905 last_p_resolved = p_resolved;
909 if (pass++ > 3 && n == 0) {
910 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
911 "could not resolve any of deltas; packfile could "
919 if (nloose + nresolved != nobj) {
921 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
922 nloose + nresolved, nobj);
923 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
927 err = report_progress(nobj, nobj, nloose, nresolved, NULL,
928 progress_cb, progress_arg);
932 make_packidx(&packidx, nobj, objects);
938 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
939 putbe32(buf + 4, GOT_PACKIDX_VERSION);
940 err = got_pack_hwrite(idxfd, buf, 8, &ctx);
943 err = got_pack_hwrite(idxfd, packidx.hdr.fanout_table,
944 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
947 err = got_pack_hwrite(idxfd, packidx.hdr.sorted_ids,
948 nobj * SHA1_DIGEST_LENGTH, &ctx);
951 err = got_pack_hwrite(idxfd, packidx.hdr.crc32,
952 nobj * sizeof(uint32_t), &ctx);
955 err = got_pack_hwrite(idxfd, packidx.hdr.offsets,
956 nobj * sizeof(uint32_t), &ctx);
959 if (packidx.nlargeobj > 0) {
960 err = got_pack_hwrite(idxfd, packidx.hdr.large_offsets,
961 packidx.nlargeobj * sizeof(uint64_t), &ctx);
965 err = got_pack_hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
969 SHA1Final(packidx_hash, &ctx);
970 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
972 err = got_error_from_errno("write");
975 if (w != sizeof(packidx_hash)) {
976 err = got_error(GOT_ERR_IO);
981 free(packidx.hdr.magic);
982 free(packidx.hdr.version);
983 free(packidx.hdr.fanout_table);
984 free(packidx.hdr.sorted_ids);
985 free(packidx.hdr.offsets);
986 free(packidx.hdr.large_offsets);