2 * Copyright (c) 2019 Ori Bernstein <ori@openbsd.org>
3 * Copyright (c) 2020 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.
20 #include <sys/types.h>
39 #include "got_error.h"
40 #include "got_object.h"
42 #include "got_compat.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_object_idset.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_delta_cache.h"
53 #include "got_lib_ratelimit.h"
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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.sha1, &ctx);
268 case GOT_OBJ_TYPE_REF_DELTA:
269 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
271 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
272 err = got_error(GOT_ERR_BAD_PACKFILE);
275 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
277 obj->crc = crc32(obj->crc, pack->map + mapoff,
279 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
281 mapoff += SHA1_DIGEST_LENGTH;
282 err = got_inflate_to_mem_mmap(NULL, &datalen,
283 &obj->len, &csum, pack->map, mapoff,
284 pack->filesize - mapoff);
288 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
291 err = got_error_from_errno("read");
294 if (n < sizeof(obj->id)) {
295 err = got_error(GOT_ERR_BAD_PACKFILE);
298 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
300 SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
302 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
303 &csum, obj->size, pack->fd);
307 obj->len += SHA1_DIGEST_LENGTH;
309 case GOT_OBJ_TYPE_OFFSET_DELTA:
310 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
311 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
312 &obj->delta.ofs.base_offsetlen, pack, obj->off,
318 obj->crc = crc32(obj->crc, pack->map + mapoff,
319 obj->delta.ofs.base_offsetlen);
320 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
321 obj->delta.ofs.base_offsetlen);
322 mapoff += obj->delta.ofs.base_offsetlen;
323 err = got_inflate_to_mem_mmap(NULL, &datalen,
324 &obj->len, &csum, pack->map, mapoff,
325 pack->filesize - mapoff);
330 * XXX Seek back and get CRC and SHA1 of on-disk
333 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
335 err = got_error_from_errno("lseek");
338 err = read_checksum(&obj->crc, pack_sha1_ctx,
339 pack->fd, obj->delta.ofs.base_offsetlen);
343 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
344 &csum, obj->size, pack->fd);
348 obj->len += obj->delta.ofs.base_offsetlen;
351 err = got_error(GOT_ERR_OBJ_TYPE);
358 static const struct got_error *
359 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
363 SHA1Update(ctx, buf, len);
365 w = write(fd, buf, len);
367 return got_error_from_errno("write");
369 return got_error(GOT_ERR_IO);
374 static const struct got_error *
375 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
376 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
377 FILE *delta_accum_file)
379 const struct got_error *err = NULL;
380 struct got_delta_chain deltas;
381 struct got_delta *delta;
389 const char *obj_label;
392 STAILQ_INIT(&deltas.entries);
394 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
395 obj->off, obj->tslen, obj->type, obj->size,
396 GOT_DELTA_CHAIN_RECURSION_MAX);
400 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
403 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
405 rewind(delta_base_file);
406 rewind(delta_accum_file);
407 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
408 pack, tmpfile, delta_base_file, delta_accum_file);
412 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
418 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
421 err = get_obj_type_label(&obj_label, base_obj_type);
424 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
425 err = got_error_from_errno("asprintf");
428 headerlen = strlen(header) + 1;
430 SHA1Update(&ctx, header, headerlen);
431 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
432 err = read_file_sha1(&ctx, tmpfile, len);
436 SHA1Update(&ctx, buf, len);
437 SHA1Final(obj->id.sha1, &ctx);
441 while (!STAILQ_EMPTY(&deltas.entries)) {
442 delta = STAILQ_FIRST(&deltas.entries);
443 STAILQ_REMOVE_HEAD(&deltas.entries, entry);
449 /* Determine the slot in the pack index a given object ID should use. */
451 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
453 u_int8_t id0 = sha1[0];
454 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
455 int left = 0, right = nindexed - 1;
459 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
461 while (left <= right) {
462 struct got_packidx_object_id *oid;
464 i = ((left + right) / 2);
465 oid = &packidx->hdr.sorted_ids[i];
467 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
469 return -1; /* object already indexed */
481 print_packidx(struct got_packidx *packidx)
483 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
486 fprintf(stderr, "object IDs:\n");
487 for (i = 0; i < nindexed; i++) {
488 char hex[SHA1_DIGEST_STRING_LENGTH];
489 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
491 fprintf(stderr, "%s\n", hex);
493 fprintf(stderr, "\n");
495 fprintf(stderr, "object offsets:\n");
496 for (i = 0; i < nindexed; i++) {
497 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
498 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
499 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
500 fprintf(stderr, "%u -> %llu\n", offset,
501 be64toh(packidx->hdr.large_offsets[j]));
503 fprintf(stderr, "%u\n", offset);
505 fprintf(stderr, "\n");
507 fprintf(stderr, "fanout table:");
508 for (i = 0; i <= 0xff; i++)
509 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
510 fprintf(stderr, "\n");
515 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
516 struct got_indexed_object *obj)
520 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
522 packidx->hdr.crc32[idx] = htobe32(obj->crc);
523 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
524 packidx->hdr.offsets[idx] = htobe32(obj->off);
526 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
527 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
528 packidx->hdr.large_offsets[packidx->nlargeobj] =
530 packidx->nlargeobj++;
533 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
534 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
535 packidx->hdr.fanout_table[i] = htobe32(n + 1);
540 indexed_obj_cmp(const void *pa, const void *pb)
542 struct got_indexed_object *a, *b;
544 a = (struct got_indexed_object *)pa;
545 b = (struct got_indexed_object *)pb;
546 return got_object_id_cmp(&a->id, &b->id);
550 make_packidx(struct got_packidx *packidx, int nobj,
551 struct got_indexed_object *objects)
553 struct got_indexed_object *obj;
557 qsort(objects, nobj, sizeof(struct got_indexed_object),
560 memset(packidx->hdr.fanout_table, 0,
561 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
562 packidx->nlargeobj = 0;
564 for (i = 0; i < nobj; i++) {
567 add_indexed_object(packidx, idx++, obj);
572 update_packidx(struct got_packidx *packidx, int nobj,
573 struct got_indexed_object *obj)
576 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
578 idx = find_object_idx(packidx, obj->id.sha1);
580 char hex[SHA1_DIGEST_STRING_LENGTH];
581 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
582 return; /* object already indexed */
585 memmove(&packidx->hdr.sorted_ids[idx + 1],
586 &packidx->hdr.sorted_ids[idx],
587 sizeof(struct got_packidx_object_id) * (nindexed - idx));
588 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
589 sizeof(uint32_t) * (nindexed - idx));
591 add_indexed_object(packidx, idx, obj);
594 static const struct got_error *
595 send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
596 int nobj_indexed, int nobj_loose, int nobj_resolved,
597 struct got_ratelimit *rl)
599 const struct got_error *err;
600 struct got_imsg_index_pack_progress iprogress;
604 err = got_ratelimit_check(&elapsed, rl);
609 iprogress.nobj_total = nobj_total;
610 iprogress.nobj_indexed = nobj_indexed;
611 iprogress.nobj_loose = nobj_loose;
612 iprogress.nobj_resolved = nobj_resolved;
614 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
615 &iprogress, sizeof(iprogress)) == -1)
616 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
618 return got_privsep_flush_imsg(ibuf);
621 static const struct got_error *
622 send_index_pack_done(struct imsgbuf *ibuf)
624 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
625 return got_error_from_errno("imsg_compose FETCH");
626 return got_privsep_flush_imsg(ibuf);
630 static const struct got_error *
631 index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
632 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
633 struct imsgbuf *ibuf)
635 const struct got_error *err;
636 struct got_packfile_hdr hdr;
637 struct got_packidx packidx;
639 char pack_sha1[SHA1_DIGEST_LENGTH];
640 int nobj, nvalid, nloose, nresolved = 0, i;
641 struct got_indexed_object *objects = NULL, *obj;
643 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
645 int pass, have_ref_deltas = 0, first_delta_idx = -1;
647 int p_indexed = 0, last_p_indexed = -1;
648 int p_resolved = 0, last_p_resolved = -1;
649 struct got_ratelimit rl;
651 /* Require that pack file header and SHA1 trailer are present. */
652 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
653 return got_error_msg(GOT_ERR_BAD_PACKFILE,
657 memcpy(&hdr, pack->map, sizeof(hdr));
658 mapoff += sizeof(hdr);
660 r = read(pack->fd, &hdr, sizeof(hdr));
662 return got_error_from_errno("read");
664 return got_error_msg(GOT_ERR_BAD_PACKFILE,
668 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
669 return got_error_msg(GOT_ERR_BAD_PACKFILE,
670 "bad packfile signature");
671 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
672 return got_error_msg(GOT_ERR_BAD_PACKFILE,
673 "bad packfile version");
674 nobj = be32toh(hdr.nobjects);
676 return got_error_msg(GOT_ERR_BAD_PACKFILE,
677 "bad packfile with zero objects");
679 /* We compute the SHA1 of pack file contents and verify later on. */
681 SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
684 * Create an in-memory pack index which will grow as objects
685 * IDs in the pack file are discovered. Only fields used to
686 * read deltified objects will be needed by the pack.c library
687 * code, so setting up just a pack index header is sufficient.
689 memset(&packidx, 0, sizeof(packidx));
690 packidx.hdr.magic = malloc(sizeof(uint32_t));
691 if (packidx.hdr.magic == NULL)
692 return got_error_from_errno("malloc");
693 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
694 packidx.hdr.version = malloc(sizeof(uint32_t));
695 if (packidx.hdr.version == NULL) {
696 err = got_error_from_errno("malloc");
699 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
700 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
702 if (packidx.hdr.fanout_table == NULL) {
703 err = got_error_from_errno("calloc");
706 packidx.hdr.sorted_ids = calloc(nobj,
707 sizeof(struct got_packidx_object_id));
708 if (packidx.hdr.sorted_ids == NULL) {
709 err = got_error_from_errno("calloc");
712 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
713 if (packidx.hdr.crc32 == NULL) {
714 err = got_error_from_errno("calloc");
717 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
718 if (packidx.hdr.offsets == NULL) {
719 err = got_error_from_errno("calloc");
722 /* Large offsets table is empty for pack files < 2 GB. */
723 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
724 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
725 if (packidx.hdr.large_offsets == NULL) {
726 err = got_error_from_errno("calloc");
733 objects = calloc(nobj, sizeof(struct got_indexed_object));
735 return got_error_from_errno("calloc");
737 got_ratelimit_init(&rl, 0, 500);
740 * First pass: locate all objects and identify un-deltified objects.
742 * When this pass has completed we will know offset, type, size, and
743 * CRC information for all objects in this pack file. We won't know
744 * any of the actual object IDs of deltified objects yet since we
745 * will not yet attempt to combine deltas.
748 for (i = 0; i < nobj; i++) {
749 /* Don't send too many progress privsep messages. */
750 p_indexed = ((i + 1) * 100) / nobj;
751 if (p_indexed != last_p_indexed) {
752 err = send_index_pack_progress(ibuf, nobj, i + 1,
756 last_p_indexed = p_indexed;
760 obj->crc = crc32(0L, NULL, 0);
762 /* Store offset to type+size information for this object. */
766 obj->off = lseek(pack->fd, 0, SEEK_CUR);
767 if (obj->off == -1) {
768 err = got_error_from_errno("lseek");
773 err = read_packed_object(pack, obj, tmpfile, &ctx);
778 mapoff += obj->tslen + obj->len;
780 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
782 err = got_error_from_errno("lseek");
787 if (obj->type == GOT_OBJ_TYPE_BLOB ||
788 obj->type == GOT_OBJ_TYPE_TREE ||
789 obj->type == GOT_OBJ_TYPE_COMMIT ||
790 obj->type == GOT_OBJ_TYPE_TAG) {
794 if (first_delta_idx == -1)
796 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
803 * Having done a full pass over the pack file and can now
804 * verify its checksum.
806 SHA1Final(pack_sha1, &ctx);
807 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
808 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
809 "pack file checksum mismatch");
813 /* Verify the SHA1 checksum stored at the end of the pack file. */
815 memcpy(pack_sha1_expected, pack->map +
816 pack->filesize - SHA1_DIGEST_LENGTH,
820 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
821 err = got_error_from_errno("lseek");
824 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
826 err = got_error_from_errno("read");
829 if (n != SHA1_DIGEST_LENGTH) {
830 err = got_error(GOT_ERR_IO);
834 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
835 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
836 "bad checksum in pack file trailer");
840 if (first_delta_idx == -1)
843 /* In order to resolve ref deltas we need an in-progress pack index. */
845 make_packidx(&packidx, nobj, objects);
848 * Second pass: We can now resolve deltas to compute the IDs of
849 * objects which appear in deltified form. Because deltas can be
850 * chained this pass may require a couple of iterations until all
851 * IDs of deltified objects have been discovered.
854 while (nvalid != nobj) {
857 * This loop will only run once unless the pack file
858 * contains ref deltas which refer to objects located
859 * later in the pack file, which is unusual.
860 * Offset deltas can always be resolved in one pass
861 * unless the packfile is corrupt.
863 for (i = first_delta_idx; i < nobj; i++) {
865 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
866 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
872 if (pack->map == NULL && lseek(pack->fd,
873 obj->off + obj->tslen, SEEK_SET) == -1) {
874 err = got_error_from_errno("lseek");
878 err = resolve_deltified_object(pack, &packidx, obj,
879 tmpfile, delta_base_file, delta_accum_file);
881 if (err->code != GOT_ERR_NO_OBJ)
884 * We cannot resolve this object yet because
885 * a delta base is unknown. Try again later.
893 update_packidx(&packidx, nobj, obj);
894 /* Don't send too many progress privsep messages. */
895 p_resolved = ((nresolved + n) * 100) / nobj;
896 if (p_resolved != last_p_resolved) {
897 err = send_index_pack_progress(ibuf, nobj,
898 nobj, nloose, nresolved + n, &rl);
901 last_p_resolved = p_resolved;
905 if (pass++ > 3 && n == 0) {
907 snprintf(msg, sizeof(msg), "could not resolve "
908 "any of deltas; packfile could be corrupt");
909 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
917 if (nloose + nresolved != nobj) {
919 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
920 nloose + nresolved, nobj);
921 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
925 err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved,
930 make_packidx(&packidx, nobj, objects);
936 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
937 putbe32(buf + 4, GOT_PACKIDX_VERSION);
938 err = hwrite(idxfd, buf, 8, &ctx);
941 err = hwrite(idxfd, packidx.hdr.fanout_table,
942 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
945 err = hwrite(idxfd, packidx.hdr.sorted_ids,
946 nobj * SHA1_DIGEST_LENGTH, &ctx);
949 err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
952 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
956 if (packidx.nlargeobj > 0) {
957 err = hwrite(idxfd, packidx.hdr.large_offsets,
958 packidx.nlargeobj * sizeof(uint64_t), &ctx);
962 err = hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
966 SHA1Final(packidx_hash, &ctx);
967 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
969 err = got_error_from_errno("write");
972 if (w != sizeof(packidx_hash)) {
973 err = got_error(GOT_ERR_IO);
978 free(packidx.hdr.magic);
979 free(packidx.hdr.version);
980 free(packidx.hdr.fanout_table);
981 free(packidx.hdr.sorted_ids);
982 free(packidx.hdr.offsets);
983 free(packidx.hdr.large_offsets);
988 main(int argc, char **argv)
990 const struct got_error *err = NULL, *close_err;
994 int idxfd = -1, tmpfd = -1;
996 struct got_pack pack;
997 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
1000 static int attached;
1005 for (i = 0; i < nitems(tmpfiles); i++)
1008 memset(&pack, 0, sizeof(pack));
1010 err = got_delta_cache_alloc(&pack.delta_cache);
1014 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1016 /* revoke access to most system calls */
1017 if (pledge("stdio recvfd", NULL) == -1) {
1018 err = got_error_from_errno("pledge");
1019 got_privsep_send_error(&ibuf, err);
1023 /* revoke fs access */
1024 if (landlock_no_fs() == -1) {
1025 err = got_error_from_errno("landlock_no_fs");
1026 got_privsep_send_error(&ibuf, err);
1029 if (cap_enter() == -1) {
1030 err = got_error_from_errno("cap_enter");
1031 got_privsep_send_error(&ibuf, err);
1035 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1038 if (imsg.hdr.type == GOT_IMSG_STOP)
1040 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
1041 err = got_error(GOT_ERR_PRIVSEP_MSG);
1044 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
1045 err = got_error(GOT_ERR_PRIVSEP_LEN);
1048 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
1051 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1054 if (imsg.hdr.type == GOT_IMSG_STOP)
1056 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
1057 err = got_error(GOT_ERR_PRIVSEP_MSG);
1060 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1061 err = got_error(GOT_ERR_PRIVSEP_LEN);
1066 for (i = 0; i < nitems(tmpfiles); i++) {
1067 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1070 if (imsg.hdr.type == GOT_IMSG_STOP)
1072 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
1073 err = got_error(GOT_ERR_PRIVSEP_MSG);
1076 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1077 err = got_error(GOT_ERR_PRIVSEP_LEN);
1081 tmpfiles[i] = fdopen(tmpfd, "w+");
1082 if (tmpfiles[i] == NULL) {
1083 err = got_error_from_errno("fdopen");
1089 if (lseek(pack.fd, 0, SEEK_END) == -1) {
1090 err = got_error_from_errno("lseek");
1093 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
1094 if (packfile_size == -1) {
1095 err = got_error_from_errno("lseek");
1098 pack.filesize = packfile_size; /* XXX off_t vs size_t */
1100 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
1101 err = got_error_from_errno("lseek");
1105 #ifndef GOT_PACK_NO_MMAP
1106 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
1108 if (pack.map == MAP_FAILED)
1109 pack.map = NULL; /* fall back to read(2) */
1111 err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
1114 close_err = got_pack_close(&pack);
1115 if (close_err && err == NULL)
1117 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1118 err = got_error_from_errno("close");
1119 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
1120 err = got_error_from_errno("close");
1121 for (i = 0; i < nitems(tmpfiles); i++) {
1122 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
1124 err = got_error_from_errno("fclose");
1128 err = send_index_pack_done(&ibuf);
1130 got_privsep_send_error(&ibuf, err);
1131 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1132 got_privsep_send_error(&ibuf, err);