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 get_obj_type_label(const char **label, int obj_type)
108 const struct got_error *err = NULL;
111 case GOT_OBJ_TYPE_BLOB:
112 *label = GOT_OBJ_LABEL_BLOB;
114 case GOT_OBJ_TYPE_TREE:
115 *label = GOT_OBJ_LABEL_TREE;
117 case GOT_OBJ_TYPE_COMMIT:
118 *label = GOT_OBJ_LABEL_COMMIT;
120 case GOT_OBJ_TYPE_TAG:
121 *label = GOT_OBJ_LABEL_TAG;
125 err = got_error(GOT_ERR_OBJ_TYPE);
132 static const struct got_error *
133 read_checksum(uint32_t *crc, SHA1_CTX *sha1_ctx, int fd, size_t len)
139 for (n = len; n > 0; n -= r){
140 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
142 return got_error_from_errno("read");
146 *crc = crc32(*crc, buf, r);
148 SHA1Update(sha1_ctx, buf, r);
154 static const struct got_error *
155 read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
160 for (n = len; n > 0; n -= r) {
161 r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
165 return got_ferror(f, GOT_ERR_IO);
167 SHA1Update(ctx, buf, r);
173 static const struct got_error *
174 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
175 FILE *tmpfile, SHA1_CTX *pack_sha1_ctx)
177 const struct got_error *err = NULL;
179 uint8_t *data = NULL;
184 const char *obj_label;
185 size_t mapoff = obj->off;
186 struct got_inflate_checksum csum;
188 memset(&csum, 0, sizeof(csum));
189 csum.input_sha1 = pack_sha1_ctx;
190 csum.input_crc = &obj->crc;
192 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
193 &obj->tslen, pack, obj->off);
198 obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
199 SHA1Update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
200 mapoff += obj->tslen;
202 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
203 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
204 return got_error_from_errno("lseek");
205 err = read_checksum(&obj->crc, pack_sha1_ctx,
206 pack->fd, obj->tslen);
212 case GOT_OBJ_TYPE_BLOB:
213 case GOT_OBJ_TYPE_COMMIT:
214 case GOT_OBJ_TYPE_TREE:
215 case GOT_OBJ_TYPE_TAG:
216 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
217 if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
218 err = got_error_from_errno("fseek");
222 err = got_inflate_to_file_mmap(&datalen,
223 &obj->len, &csum, pack->map, mapoff,
224 pack->filesize - mapoff, tmpfile);
226 err = got_inflate_to_file_fd(&datalen,
227 &obj->len, &csum, pack->fd, tmpfile);
231 err = got_inflate_to_mem_mmap(&data, &datalen,
232 &obj->len, &csum, pack->map, mapoff,
233 pack->filesize - mapoff);
235 err = got_inflate_to_mem_fd(&data, &datalen,
236 &obj->len, &csum, obj->size, pack->fd);
242 err = get_obj_type_label(&obj_label, obj->type);
247 if (asprintf(&header, "%s %lld", obj_label,
248 (long long)obj->size) == -1) {
249 err = got_error_from_errno("asprintf");
253 headerlen = strlen(header) + 1;
254 SHA1Update(&ctx, header, headerlen);
255 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
256 err = read_file_sha1(&ctx, tmpfile, datalen);
263 SHA1Update(&ctx, data, datalen);
264 SHA1Final(obj->id.hash, &ctx);
268 case GOT_OBJ_TYPE_REF_DELTA:
269 memset(obj->id.hash, 0xff, SHA1_DIGEST_LENGTH);
271 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
272 err = got_error(GOT_ERR_BAD_PACKFILE);
275 if (mapoff + SHA1_DIGEST_LENGTH > SIZE_MAX) {
276 err = got_error_fmt(GOT_ERR_RANGE,
277 "mapoff %lld would overflow size_t",
278 (long long)mapoff + SHA1_DIGEST_LENGTH);
281 memcpy(obj->delta.ref.ref_id.hash, pack->map + mapoff,
283 obj->crc = crc32(obj->crc, pack->map + mapoff,
285 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
287 mapoff += SHA1_DIGEST_LENGTH;
288 err = got_inflate_to_mem_mmap(NULL, &datalen,
289 &obj->len, &csum, pack->map, mapoff,
290 pack->filesize - mapoff);
294 n = read(pack->fd, obj->delta.ref.ref_id.hash,
297 err = got_error_from_errno("read");
300 if (n < sizeof(obj->id)) {
301 err = got_error(GOT_ERR_BAD_PACKFILE);
304 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.hash,
306 SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.hash,
308 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
309 &csum, obj->size, pack->fd);
313 obj->len += SHA1_DIGEST_LENGTH;
315 case GOT_OBJ_TYPE_OFFSET_DELTA:
316 memset(obj->id.hash, 0xff, SHA1_DIGEST_LENGTH);
317 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
318 &obj->delta.ofs.base_offsetlen, pack, obj->off,
324 if (mapoff + obj->delta.ofs.base_offsetlen >=
326 err = got_error(GOT_ERR_BAD_PACKFILE);
330 if (mapoff + obj->delta.ofs.base_offsetlen >
332 err = got_error_fmt(GOT_ERR_RANGE,
333 "mapoff %lld would overflow size_t",
335 + obj->delta.ofs.base_offsetlen);
338 obj->crc = crc32(obj->crc, pack->map + mapoff,
339 obj->delta.ofs.base_offsetlen);
340 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
341 obj->delta.ofs.base_offsetlen);
342 mapoff += obj->delta.ofs.base_offsetlen;
343 err = got_inflate_to_mem_mmap(NULL, &datalen,
344 &obj->len, &csum, pack->map, mapoff,
345 pack->filesize - mapoff);
350 * XXX Seek back and get CRC and SHA1 of on-disk
353 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
355 err = got_error_from_errno("lseek");
358 err = read_checksum(&obj->crc, pack_sha1_ctx,
359 pack->fd, obj->delta.ofs.base_offsetlen);
363 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
364 &csum, obj->size, pack->fd);
368 obj->len += obj->delta.ofs.base_offsetlen;
371 err = got_error(GOT_ERR_OBJ_TYPE);
378 const struct got_error *
379 got_pack_hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
383 SHA1Update(ctx, buf, len);
385 w = write(fd, buf, len);
387 return got_error_from_errno("write");
389 return got_error(GOT_ERR_IO);
394 static const struct got_error *
395 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
396 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
397 FILE *delta_accum_file)
399 const struct got_error *err = NULL;
400 struct got_delta_chain deltas;
401 struct got_delta *delta;
409 const char *obj_label;
412 STAILQ_INIT(&deltas.entries);
414 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
415 obj->off, obj->tslen, obj->type, obj->size,
416 GOT_DELTA_CHAIN_RECURSION_MAX);
420 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
423 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
425 rewind(delta_base_file);
426 rewind(delta_accum_file);
427 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
428 pack, tmpfile, delta_base_file, delta_accum_file);
432 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
438 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
441 err = get_obj_type_label(&obj_label, base_obj_type);
444 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
445 err = got_error_from_errno("asprintf");
448 headerlen = strlen(header) + 1;
450 SHA1Update(&ctx, header, headerlen);
451 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
452 err = read_file_sha1(&ctx, tmpfile, len);
456 SHA1Update(&ctx, buf, len);
457 SHA1Final(obj->id.hash, &ctx);
461 while (!STAILQ_EMPTY(&deltas.entries)) {
462 delta = STAILQ_FIRST(&deltas.entries);
463 STAILQ_REMOVE_HEAD(&deltas.entries, entry);
469 /* Determine the slot in the pack index a given object ID should use. */
471 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
473 u_int8_t id0 = sha1[0];
474 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
475 int left = 0, right = nindexed - 1;
479 idlen = got_hash_digest_length(packidx->algo);
482 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
484 while (left <= right) {
485 struct got_packidx_object_id *oid;
487 i = ((left + right) / 2);
488 oid = packidx->hdr.sorted_ids + idlen * i;
490 cmp = memcmp(sha1, oid->hash, idlen);
492 return -1; /* object already indexed */
504 print_packidx(struct got_packidx *packidx)
506 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
507 struct got_packidx_object_id *oid;
511 idlen = got_hash_digest_length(packidx->algo);
513 fprintf(stderr, "object IDs:\n");
514 for (i = 0; i < nindexed; i++) {
515 char hex[GOT_OBJECT_ID_HEX_MAXLEN];
517 oid = packidx->hdr.sorted_ids + idlen * i;
518 got_hash_digest_to_str(oid->hash, hex, sizeof(hex),
520 fprintf(stderr, "%s\n", hex);
522 fprintf(stderr, "\n");
524 fprintf(stderr, "object offsets:\n");
525 for (i = 0; i < nindexed; i++) {
526 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
527 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
528 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
529 fprintf(stderr, "%u -> %llu\n", offset,
530 be64toh(packidx->hdr.large_offsets[j]));
532 fprintf(stderr, "%u\n", offset);
534 fprintf(stderr, "\n");
536 fprintf(stderr, "fanout table:");
537 for (i = 0; i <= 0xff; i++)
538 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
539 fprintf(stderr, "\n");
544 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
545 struct got_indexed_object *obj)
550 idlen = got_hash_digest_length(packidx->algo);
552 memcpy(packidx->hdr.sorted_ids + idx * idlen, obj->id.hash,
554 packidx->hdr.crc32[idx] = htobe32(obj->crc);
555 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
556 packidx->hdr.offsets[idx] = htobe32(obj->off);
558 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
559 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
560 packidx->hdr.large_offsets[packidx->nlargeobj] =
562 packidx->nlargeobj++;
565 for (i = obj->id.hash[0]; i <= 0xff; i++) {
566 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
567 packidx->hdr.fanout_table[i] = htobe32(n + 1);
572 indexed_obj_cmp(const void *pa, const void *pb)
574 struct got_indexed_object *a, *b;
576 a = (struct got_indexed_object *)pa;
577 b = (struct got_indexed_object *)pb;
578 return got_object_id_cmp(&a->id, &b->id);
582 make_packidx(struct got_packidx *packidx, uint32_t nobj,
583 struct got_indexed_object *objects)
585 struct got_indexed_object *obj;
589 qsort(objects, nobj, sizeof(struct got_indexed_object),
592 memset(packidx->hdr.fanout_table, 0,
593 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
594 packidx->nlargeobj = 0;
596 for (i = 0; i < nobj; i++) {
599 add_indexed_object(packidx, idx++, obj);
604 update_packidx(struct got_packidx *packidx, uint32_t nobj,
605 struct got_indexed_object *obj)
609 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
611 idlen = got_hash_digest_length(packidx->algo);
613 idx = find_object_idx(packidx, obj->id.hash);
615 return; /* object already indexed */
617 memmove(packidx->hdr.sorted_ids + (idx + 1) * idlen,
618 packidx->hdr.sorted_ids + idx * idlen,
619 idlen * (nindexed - idx));
620 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
621 sizeof(uint32_t) * (nindexed - idx));
623 add_indexed_object(packidx, idx, obj);
626 static const struct got_error *
627 report_progress(uint32_t nobj_total, uint32_t nobj_indexed, uint32_t nobj_loose,
628 uint32_t nobj_resolved, struct got_ratelimit *rl,
629 got_pack_index_progress_cb progress_cb, void *progress_arg)
631 const struct got_error *err;
635 err = got_ratelimit_check(&elapsed, rl);
640 return progress_cb(progress_arg, nobj_total, nobj_indexed, nobj_loose,
644 const struct got_error *
645 got_pack_index(struct got_pack *pack, int idxfd, FILE *tmpfile,
646 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
647 got_pack_index_progress_cb progress_cb, void *progress_arg,
648 struct got_ratelimit *rl)
650 const struct got_error *err;
651 struct got_packfile_hdr hdr;
652 struct got_packidx packidx;
654 char pack_sha1[SHA1_DIGEST_LENGTH];
655 uint32_t nobj, nvalid, nloose, nresolved = 0, i;
656 struct got_indexed_object *objects = NULL, *obj;
658 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
660 int pass, have_ref_deltas = 0, first_delta_idx = -1;
662 int p_indexed = 0, last_p_indexed = -1;
663 int p_resolved = 0, last_p_resolved = -1;
665 /* Require that pack file header and SHA1 trailer are present. */
666 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
667 return got_error_msg(GOT_ERR_BAD_PACKFILE,
671 memcpy(&hdr, pack->map, sizeof(hdr));
672 mapoff += sizeof(hdr);
674 r = read(pack->fd, &hdr, sizeof(hdr));
676 return got_error_from_errno("read");
678 return got_error_msg(GOT_ERR_BAD_PACKFILE,
682 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
683 return got_error_msg(GOT_ERR_BAD_PACKFILE,
684 "bad packfile signature");
685 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
686 return got_error_msg(GOT_ERR_BAD_PACKFILE,
687 "bad packfile version");
688 nobj = be32toh(hdr.nobjects);
690 return got_error_msg(GOT_ERR_BAD_PACKFILE,
691 "bad packfile with zero objects");
693 /* We compute the SHA1 of pack file contents and verify later on. */
695 SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
698 * Create an in-memory pack index which will grow as objects
699 * IDs in the pack file are discovered. Only fields used to
700 * read deltified objects will be needed by the pack.c library
701 * code, so setting up just a pack index header is sufficient.
703 memset(&packidx, 0, sizeof(packidx));
704 packidx.hdr.magic = malloc(sizeof(uint32_t));
705 if (packidx.hdr.magic == NULL)
706 return got_error_from_errno("malloc");
707 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
708 packidx.hdr.version = malloc(sizeof(uint32_t));
709 if (packidx.hdr.version == NULL) {
710 err = got_error_from_errno("malloc");
713 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
714 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
716 if (packidx.hdr.fanout_table == NULL) {
717 err = got_error_from_errno("calloc");
720 packidx.hdr.sorted_ids = calloc(nobj, SHA1_DIGEST_LENGTH);
721 if (packidx.hdr.sorted_ids == NULL) {
722 err = got_error_from_errno("calloc");
725 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
726 if (packidx.hdr.crc32 == NULL) {
727 err = got_error_from_errno("calloc");
730 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
731 if (packidx.hdr.offsets == NULL) {
732 err = got_error_from_errno("calloc");
735 /* Large offsets table is empty for pack files < 2 GB. */
736 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
737 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
738 if (packidx.hdr.large_offsets == NULL) {
739 err = got_error_from_errno("calloc");
746 objects = calloc(nobj, sizeof(struct got_indexed_object));
748 return got_error_from_errno("calloc");
751 * First pass: locate all objects and identify un-deltified objects.
753 * When this pass has completed we will know offset, type, size, and
754 * CRC information for all objects in this pack file. We won't know
755 * any of the actual object IDs of deltified objects yet since we
756 * will not yet attempt to combine deltas.
759 for (i = 0; i < nobj; i++) {
760 /* Don't send too many progress privsep messages. */
761 p_indexed = ((i + 1) * 100) / nobj;
762 if (p_indexed != last_p_indexed) {
763 err = report_progress(nobj, i + 1, nloose, 0,
764 rl, progress_cb, progress_arg);
767 last_p_indexed = p_indexed;
771 obj->crc = crc32(0L, NULL, 0);
773 /* Store offset to type+size information for this object. */
777 obj->off = lseek(pack->fd, 0, SEEK_CUR);
778 if (obj->off == -1) {
779 err = got_error_from_errno("lseek");
784 err = read_packed_object(pack, obj, tmpfile, &ctx);
789 mapoff += obj->tslen + obj->len;
791 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
793 err = got_error_from_errno("lseek");
798 if (obj->type == GOT_OBJ_TYPE_BLOB ||
799 obj->type == GOT_OBJ_TYPE_TREE ||
800 obj->type == GOT_OBJ_TYPE_COMMIT ||
801 obj->type == GOT_OBJ_TYPE_TAG) {
805 if (first_delta_idx == -1)
807 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
814 * Having done a full pass over the pack file and can now
815 * verify its checksum.
817 SHA1Final(pack_sha1, &ctx);
819 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
820 err = got_error(GOT_ERR_PACKFILE_CSUM);
824 /* Verify the SHA1 checksum stored at the end of the pack file. */
826 if (pack->filesize > SIZE_MAX) {
827 err = got_error_fmt(GOT_ERR_RANGE,
828 "filesize %lld overflows size_t",
829 (long long)pack->filesize);
833 memcpy(pack_sha1_expected, pack->map +
834 pack->filesize - SHA1_DIGEST_LENGTH,
838 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
839 err = got_error_from_errno("lseek");
842 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
844 err = got_error_from_errno("read");
847 if (n != SHA1_DIGEST_LENGTH) {
848 err = got_error(GOT_ERR_IO);
852 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
853 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
854 "bad checksum in pack file trailer");
858 if (first_delta_idx == -1)
861 /* In order to resolve ref deltas we need an in-progress pack index. */
863 make_packidx(&packidx, nobj, objects);
866 * Second pass: We can now resolve deltas to compute the IDs of
867 * objects which appear in deltified form. Because deltas can be
868 * chained this pass may require a couple of iterations until all
869 * IDs of deltified objects have been discovered.
872 while (nvalid != nobj) {
875 * This loop will only run once unless the pack file
876 * contains ref deltas which refer to objects located
877 * later in the pack file, which is unusual.
878 * Offset deltas can always be resolved in one pass
879 * unless the packfile is corrupt.
881 for (i = first_delta_idx; i < nobj; i++) {
883 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
884 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
890 if (pack->map == NULL && lseek(pack->fd,
891 obj->off + obj->tslen, SEEK_SET) == -1) {
892 err = got_error_from_errno("lseek");
896 err = resolve_deltified_object(pack, &packidx, obj,
897 tmpfile, delta_base_file, delta_accum_file);
899 if (err->code != GOT_ERR_NO_OBJ)
902 * We cannot resolve this object yet because
903 * a delta base is unknown. Try again later.
911 update_packidx(&packidx, nobj, obj);
912 /* Don't send too many progress privsep messages. */
913 p_resolved = ((nresolved + n) * 100) / nobj;
914 if (p_resolved != last_p_resolved) {
915 err = report_progress(nobj, nobj,
916 nloose, nresolved + n, rl,
917 progress_cb, progress_arg);
920 last_p_resolved = p_resolved;
924 if (pass++ > 3 && n == 0) {
925 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
926 "could not resolve any of deltas; packfile could "
934 if (nloose + nresolved != nobj) {
936 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
937 nloose + nresolved, nobj);
938 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
942 err = report_progress(nobj, nobj, nloose, nresolved, NULL,
943 progress_cb, progress_arg);
947 make_packidx(&packidx, nobj, objects);
953 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
954 putbe32(buf + 4, GOT_PACKIDX_VERSION);
955 err = got_pack_hwrite(idxfd, buf, 8, &ctx);
958 err = got_pack_hwrite(idxfd, packidx.hdr.fanout_table,
959 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
962 got_pack_hwrite(idxfd, packidx.hdr.sorted_ids,
963 nobj * SHA1_DIGEST_LENGTH, &ctx);
966 err = got_pack_hwrite(idxfd, packidx.hdr.crc32,
967 nobj * sizeof(uint32_t), &ctx);
970 err = got_pack_hwrite(idxfd, packidx.hdr.offsets,
971 nobj * sizeof(uint32_t), &ctx);
974 if (packidx.nlargeobj > 0) {
975 err = got_pack_hwrite(idxfd, packidx.hdr.large_offsets,
976 packidx.nlargeobj * sizeof(uint64_t), &ctx);
980 err = got_pack_hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
984 SHA1Final(packidx_hash, &ctx);
985 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
987 err = got_error_from_errno("write");
990 if (w != sizeof(packidx_hash)) {
991 err = got_error(GOT_ERR_IO);
996 free(packidx.hdr.magic);
997 free(packidx.hdr.version);
998 free(packidx.hdr.fanout_table);
999 free(packidx.hdr.sorted_ids);
1000 free(packidx.hdr.offsets);
1001 free(packidx.hdr.large_offsets);