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.
18 #include <sys/queue.h>
21 #include <sys/types.h>
40 #include "got_error.h"
41 #include "got_object.h"
43 #include "got_compat.h"
45 #include "got_lib_sha1.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_inflate.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_parse.h"
50 #include "got_lib_object_idset.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_delta_cache.h"
54 #include "got_lib_ratelimit.h"
57 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
60 struct got_indexed_object {
61 struct got_object_id id;
64 * Has this object been fully resolved?
65 * If so, we know its ID, otherwise we don't and 'id' is invalid.
69 /* Offset of type+size field for this object in pack file. */
72 /* Type+size values parsed from pack file. */
76 /* Length of on-disk type+size data. */
79 /* Length of object data following type+size. */
87 struct got_object_id ref_id;
90 /* For offset deltas. */
92 size_t base_offsetlen;
98 putbe32(char *b, uint32_t n)
106 static const struct got_error *
107 get_obj_type_label(const char **label, int obj_type)
109 const struct got_error *err = NULL;
112 case GOT_OBJ_TYPE_BLOB:
113 *label = GOT_OBJ_LABEL_BLOB;
115 case GOT_OBJ_TYPE_TREE:
116 *label = GOT_OBJ_LABEL_TREE;
118 case GOT_OBJ_TYPE_COMMIT:
119 *label = GOT_OBJ_LABEL_COMMIT;
121 case GOT_OBJ_TYPE_TAG:
122 *label = GOT_OBJ_LABEL_TAG;
126 err = got_error(GOT_ERR_OBJ_TYPE);
133 static const struct got_error *
134 read_checksum(uint32_t *crc, SHA1_CTX *sha1_ctx, int fd, size_t len)
140 for (n = len; n > 0; n -= r){
141 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
143 return got_error_from_errno("read");
147 *crc = crc32(*crc, buf, r);
149 SHA1Update(sha1_ctx, buf, r);
155 static const struct got_error *
156 read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
161 for (n = len; n > 0; n -= r) {
162 r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
166 return got_ferror(f, GOT_ERR_IO);
168 SHA1Update(ctx, buf, r);
174 static const struct got_error *
175 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
176 FILE *tmpfile, SHA1_CTX *pack_sha1_ctx)
178 const struct got_error *err = NULL;
180 uint8_t *data = NULL;
185 const char *obj_label;
186 size_t mapoff = obj->off;
187 struct got_inflate_checksum csum;
189 memset(&csum, 0, sizeof(csum));
190 csum.input_sha1 = pack_sha1_ctx;
191 csum.input_crc = &obj->crc;
193 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
194 &obj->tslen, pack, obj->off);
199 obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
200 SHA1Update(pack_sha1_ctx, pack->map + mapoff, obj->tslen);
201 mapoff += obj->tslen;
203 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
204 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
205 return got_error_from_errno("lseek");
206 err = read_checksum(&obj->crc, pack_sha1_ctx,
207 pack->fd, obj->tslen);
213 case GOT_OBJ_TYPE_BLOB:
214 case GOT_OBJ_TYPE_COMMIT:
215 case GOT_OBJ_TYPE_TREE:
216 case GOT_OBJ_TYPE_TAG:
217 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
218 if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
219 err = got_error_from_errno("fseek");
223 err = got_inflate_to_file_mmap(&datalen,
224 &obj->len, &csum, pack->map, mapoff,
225 pack->filesize - mapoff, tmpfile);
227 err = got_inflate_to_file_fd(&datalen,
228 &obj->len, &csum, pack->fd, tmpfile);
232 err = got_inflate_to_mem_mmap(&data, &datalen,
233 &obj->len, &csum, pack->map, mapoff,
234 pack->filesize - mapoff);
236 err = got_inflate_to_mem_fd(&data, &datalen,
237 &obj->len, &csum, obj->size, pack->fd);
243 err = get_obj_type_label(&obj_label, obj->type);
248 if (asprintf(&header, "%s %lld", obj_label,
249 (long long)obj->size) == -1) {
250 err = got_error_from_errno("asprintf");
254 headerlen = strlen(header) + 1;
255 SHA1Update(&ctx, header, headerlen);
256 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
257 err = read_file_sha1(&ctx, tmpfile, datalen);
264 SHA1Update(&ctx, data, datalen);
265 SHA1Final(obj->id.sha1, &ctx);
269 case GOT_OBJ_TYPE_REF_DELTA:
270 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
272 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
273 err = got_error(GOT_ERR_BAD_PACKFILE);
276 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
278 obj->crc = crc32(obj->crc, pack->map + mapoff,
280 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
282 mapoff += SHA1_DIGEST_LENGTH;
283 err = got_inflate_to_mem_mmap(NULL, &datalen,
284 &obj->len, &csum, pack->map, mapoff,
285 pack->filesize - mapoff);
289 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
292 err = got_error_from_errno("read");
295 if (n < sizeof(obj->id)) {
296 err = got_error(GOT_ERR_BAD_PACKFILE);
299 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
301 SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
303 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
304 &csum, obj->size, pack->fd);
308 obj->len += SHA1_DIGEST_LENGTH;
310 case GOT_OBJ_TYPE_OFFSET_DELTA:
311 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
312 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
313 &obj->delta.ofs.base_offsetlen, pack, obj->off,
319 obj->crc = crc32(obj->crc, pack->map + mapoff,
320 obj->delta.ofs.base_offsetlen);
321 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
322 obj->delta.ofs.base_offsetlen);
323 mapoff += obj->delta.ofs.base_offsetlen;
324 err = got_inflate_to_mem_mmap(NULL, &datalen,
325 &obj->len, &csum, pack->map, mapoff,
326 pack->filesize - mapoff);
331 * XXX Seek back and get CRC and SHA1 of on-disk
334 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
336 err = got_error_from_errno("lseek");
339 err = read_checksum(&obj->crc, pack_sha1_ctx,
340 pack->fd, obj->delta.ofs.base_offsetlen);
344 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
345 &csum, obj->size, pack->fd);
349 obj->len += obj->delta.ofs.base_offsetlen;
352 err = got_error(GOT_ERR_OBJ_TYPE);
359 static const struct got_error *
360 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
364 SHA1Update(ctx, buf, len);
366 w = write(fd, buf, len);
368 return got_error_from_errno("write");
370 return got_error(GOT_ERR_IO);
375 static const struct got_error *
376 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
377 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
378 FILE *delta_accum_file)
380 const struct got_error *err = NULL;
381 struct got_delta_chain deltas;
382 struct got_delta *delta;
390 const char *obj_label;
393 STAILQ_INIT(&deltas.entries);
395 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
396 obj->off, obj->tslen, obj->type, obj->size,
397 GOT_DELTA_CHAIN_RECURSION_MAX);
401 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
404 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
406 rewind(delta_base_file);
407 rewind(delta_accum_file);
408 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
409 pack, tmpfile, delta_base_file, delta_accum_file);
413 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
419 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
422 err = get_obj_type_label(&obj_label, base_obj_type);
425 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
426 err = got_error_from_errno("asprintf");
429 headerlen = strlen(header) + 1;
431 SHA1Update(&ctx, header, headerlen);
432 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
433 err = read_file_sha1(&ctx, tmpfile, len);
437 SHA1Update(&ctx, buf, len);
438 SHA1Final(obj->id.sha1, &ctx);
442 while (!STAILQ_EMPTY(&deltas.entries)) {
443 delta = STAILQ_FIRST(&deltas.entries);
444 STAILQ_REMOVE_HEAD(&deltas.entries, entry);
450 /* Determine the slot in the pack index a given object ID should use. */
452 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
454 u_int8_t id0 = sha1[0];
455 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
456 int left = 0, right = nindexed - 1;
460 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
462 while (left <= right) {
463 struct got_packidx_object_id *oid;
465 i = ((left + right) / 2);
466 oid = &packidx->hdr.sorted_ids[i];
468 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
470 return -1; /* object already indexed */
482 print_packidx(struct got_packidx *packidx)
484 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
487 fprintf(stderr, "object IDs:\n");
488 for (i = 0; i < nindexed; i++) {
489 char hex[SHA1_DIGEST_STRING_LENGTH];
490 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
492 fprintf(stderr, "%s\n", hex);
494 fprintf(stderr, "\n");
496 fprintf(stderr, "object offsets:\n");
497 for (i = 0; i < nindexed; i++) {
498 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
499 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
500 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
501 fprintf(stderr, "%u -> %llu\n", offset,
502 be64toh(packidx->hdr.large_offsets[j]));
504 fprintf(stderr, "%u\n", offset);
506 fprintf(stderr, "\n");
508 fprintf(stderr, "fanout table:");
509 for (i = 0; i <= 0xff; i++)
510 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
511 fprintf(stderr, "\n");
516 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
517 struct got_indexed_object *obj)
521 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
523 packidx->hdr.crc32[idx] = htobe32(obj->crc);
524 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
525 packidx->hdr.offsets[idx] = htobe32(obj->off);
527 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
528 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
529 packidx->hdr.large_offsets[packidx->nlargeobj] =
531 packidx->nlargeobj++;
534 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
535 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
536 packidx->hdr.fanout_table[i] = htobe32(n + 1);
541 indexed_obj_cmp(const void *pa, const void *pb)
543 struct got_indexed_object *a, *b;
545 a = (struct got_indexed_object *)pa;
546 b = (struct got_indexed_object *)pb;
547 return got_object_id_cmp(&a->id, &b->id);
551 make_packidx(struct got_packidx *packidx, int nobj,
552 struct got_indexed_object *objects)
554 struct got_indexed_object *obj;
558 qsort(objects, nobj, sizeof(struct got_indexed_object),
561 memset(packidx->hdr.fanout_table, 0,
562 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
563 packidx->nlargeobj = 0;
565 for (i = 0; i < nobj; i++) {
568 add_indexed_object(packidx, idx++, obj);
573 update_packidx(struct got_packidx *packidx, int nobj,
574 struct got_indexed_object *obj)
577 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
579 idx = find_object_idx(packidx, obj->id.sha1);
581 char hex[SHA1_DIGEST_STRING_LENGTH];
582 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
583 return; /* object already indexed */
586 memmove(&packidx->hdr.sorted_ids[idx + 1],
587 &packidx->hdr.sorted_ids[idx],
588 sizeof(struct got_packidx_object_id) * (nindexed - idx));
589 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
590 sizeof(uint32_t) * (nindexed - idx));
592 add_indexed_object(packidx, idx, obj);
595 static const struct got_error *
596 send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
597 int nobj_indexed, int nobj_loose, int nobj_resolved,
598 struct got_ratelimit *rl)
600 const struct got_error *err;
601 struct got_imsg_index_pack_progress iprogress;
605 err = got_ratelimit_check(&elapsed, rl);
610 iprogress.nobj_total = nobj_total;
611 iprogress.nobj_indexed = nobj_indexed;
612 iprogress.nobj_loose = nobj_loose;
613 iprogress.nobj_resolved = nobj_resolved;
615 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
616 &iprogress, sizeof(iprogress)) == -1)
617 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
619 return got_privsep_flush_imsg(ibuf);
622 static const struct got_error *
623 send_index_pack_done(struct imsgbuf *ibuf)
625 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
626 return got_error_from_errno("imsg_compose FETCH");
627 return got_privsep_flush_imsg(ibuf);
631 static const struct got_error *
632 index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
633 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
634 struct imsgbuf *ibuf)
636 const struct got_error *err;
637 struct got_packfile_hdr hdr;
638 struct got_packidx packidx;
640 char pack_sha1[SHA1_DIGEST_LENGTH];
641 int nobj, nvalid, nloose, nresolved = 0, i;
642 struct got_indexed_object *objects = NULL, *obj;
644 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
646 int pass, have_ref_deltas = 0, first_delta_idx = -1;
648 int p_indexed = 0, last_p_indexed = -1;
649 int p_resolved = 0, last_p_resolved = -1;
650 struct got_ratelimit rl;
652 /* Require that pack file header and SHA1 trailer are present. */
653 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
654 return got_error_msg(GOT_ERR_BAD_PACKFILE,
658 memcpy(&hdr, pack->map, sizeof(hdr));
659 mapoff += sizeof(hdr);
661 r = read(pack->fd, &hdr, sizeof(hdr));
663 return got_error_from_errno("read");
665 return got_error_msg(GOT_ERR_BAD_PACKFILE,
669 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
670 return got_error_msg(GOT_ERR_BAD_PACKFILE,
671 "bad packfile signature");
672 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
673 return got_error_msg(GOT_ERR_BAD_PACKFILE,
674 "bad packfile version");
675 nobj = be32toh(hdr.nobjects);
677 return got_error_msg(GOT_ERR_BAD_PACKFILE,
678 "bad packfile with zero objects");
680 /* We compute the SHA1 of pack file contents and verify later on. */
682 SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
685 * Create an in-memory pack index which will grow as objects
686 * IDs in the pack file are discovered. Only fields used to
687 * read deltified objects will be needed by the pack.c library
688 * code, so setting up just a pack index header is sufficient.
690 memset(&packidx, 0, sizeof(packidx));
691 packidx.hdr.magic = malloc(sizeof(uint32_t));
692 if (packidx.hdr.magic == NULL)
693 return got_error_from_errno("malloc");
694 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
695 packidx.hdr.version = malloc(sizeof(uint32_t));
696 if (packidx.hdr.version == NULL) {
697 err = got_error_from_errno("malloc");
700 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
701 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
703 if (packidx.hdr.fanout_table == NULL) {
704 err = got_error_from_errno("calloc");
707 packidx.hdr.sorted_ids = calloc(nobj,
708 sizeof(struct got_packidx_object_id));
709 if (packidx.hdr.sorted_ids == NULL) {
710 err = got_error_from_errno("calloc");
713 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
714 if (packidx.hdr.crc32 == NULL) {
715 err = got_error_from_errno("calloc");
718 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
719 if (packidx.hdr.offsets == NULL) {
720 err = got_error_from_errno("calloc");
723 /* Large offsets table is empty for pack files < 2 GB. */
724 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
725 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
726 if (packidx.hdr.large_offsets == NULL) {
727 err = got_error_from_errno("calloc");
734 objects = calloc(nobj, sizeof(struct got_indexed_object));
736 return got_error_from_errno("calloc");
738 got_ratelimit_init(&rl, 0, 500);
741 * First pass: locate all objects and identify un-deltified objects.
743 * When this pass has completed we will know offset, type, size, and
744 * CRC information for all objects in this pack file. We won't know
745 * any of the actual object IDs of deltified objects yet since we
746 * will not yet attempt to combine deltas.
749 for (i = 0; i < nobj; i++) {
750 /* Don't send too many progress privsep messages. */
751 p_indexed = ((i + 1) * 100) / nobj;
752 if (p_indexed != last_p_indexed) {
753 err = send_index_pack_progress(ibuf, nobj, i + 1,
757 last_p_indexed = p_indexed;
761 obj->crc = crc32(0L, NULL, 0);
763 /* Store offset to type+size information for this object. */
767 obj->off = lseek(pack->fd, 0, SEEK_CUR);
768 if (obj->off == -1) {
769 err = got_error_from_errno("lseek");
774 err = read_packed_object(pack, obj, tmpfile, &ctx);
779 mapoff += obj->tslen + obj->len;
781 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
783 err = got_error_from_errno("lseek");
788 if (obj->type == GOT_OBJ_TYPE_BLOB ||
789 obj->type == GOT_OBJ_TYPE_TREE ||
790 obj->type == GOT_OBJ_TYPE_COMMIT ||
791 obj->type == GOT_OBJ_TYPE_TAG) {
795 if (first_delta_idx == -1)
797 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
804 * Having done a full pass over the pack file and can now
805 * verify its checksum.
807 SHA1Final(pack_sha1, &ctx);
808 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
809 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
810 "pack file checksum mismatch");
814 /* Verify the SHA1 checksum stored at the end of the pack file. */
816 memcpy(pack_sha1_expected, pack->map +
817 pack->filesize - SHA1_DIGEST_LENGTH,
821 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
822 err = got_error_from_errno("lseek");
825 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
827 err = got_error_from_errno("read");
830 if (n != SHA1_DIGEST_LENGTH) {
831 err = got_error(GOT_ERR_IO);
835 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
836 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
837 "bad checksum in pack file trailer");
841 if (first_delta_idx == -1)
844 /* In order to resolve ref deltas we need an in-progress pack index. */
846 make_packidx(&packidx, nobj, objects);
849 * Second pass: We can now resolve deltas to compute the IDs of
850 * objects which appear in deltified form. Because deltas can be
851 * chained this pass may require a couple of iterations until all
852 * IDs of deltified objects have been discovered.
855 while (nvalid != nobj) {
858 * This loop will only run once unless the pack file
859 * contains ref deltas which refer to objects located
860 * later in the pack file, which is unusual.
861 * Offset deltas can always be resolved in one pass
862 * unless the packfile is corrupt.
864 for (i = first_delta_idx; i < nobj; i++) {
866 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
867 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
873 if (pack->map == NULL && lseek(pack->fd,
874 obj->off + obj->tslen, SEEK_SET) == -1) {
875 err = got_error_from_errno("lseek");
879 err = resolve_deltified_object(pack, &packidx, obj,
880 tmpfile, delta_base_file, delta_accum_file);
882 if (err->code != GOT_ERR_NO_OBJ)
885 * We cannot resolve this object yet because
886 * a delta base is unknown. Try again later.
894 update_packidx(&packidx, nobj, obj);
895 /* Don't send too many progress privsep messages. */
896 p_resolved = ((nresolved + n) * 100) / nobj;
897 if (p_resolved != last_p_resolved) {
898 err = send_index_pack_progress(ibuf, nobj,
899 nobj, nloose, nresolved + n, &rl);
902 last_p_resolved = p_resolved;
906 if (pass++ > 3 && n == 0) {
908 snprintf(msg, sizeof(msg), "could not resolve "
909 "any of deltas; packfile could be corrupt");
910 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
918 if (nloose + nresolved != nobj) {
920 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
921 nloose + nresolved, nobj);
922 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
926 err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved,
931 make_packidx(&packidx, nobj, objects);
937 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
938 putbe32(buf + 4, GOT_PACKIDX_VERSION);
939 err = hwrite(idxfd, buf, 8, &ctx);
942 err = hwrite(idxfd, packidx.hdr.fanout_table,
943 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
946 err = hwrite(idxfd, packidx.hdr.sorted_ids,
947 nobj * SHA1_DIGEST_LENGTH, &ctx);
950 err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
953 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
957 if (packidx.nlargeobj > 0) {
958 err = hwrite(idxfd, packidx.hdr.large_offsets,
959 packidx.nlargeobj * sizeof(uint64_t), &ctx);
963 err = hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
967 SHA1Final(packidx_hash, &ctx);
968 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
970 err = got_error_from_errno("write");
973 if (w != sizeof(packidx_hash)) {
974 err = got_error(GOT_ERR_IO);
979 free(packidx.hdr.magic);
980 free(packidx.hdr.version);
981 free(packidx.hdr.fanout_table);
982 free(packidx.hdr.sorted_ids);
983 free(packidx.hdr.offsets);
984 free(packidx.hdr.large_offsets);
989 main(int argc, char **argv)
991 const struct got_error *err = NULL, *close_err;
995 int idxfd = -1, tmpfd = -1;
997 struct got_pack pack;
998 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
1001 static int attached;
1006 for (i = 0; i < nitems(tmpfiles); i++)
1009 memset(&pack, 0, sizeof(pack));
1011 err = got_delta_cache_alloc(&pack.delta_cache);
1015 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1017 /* revoke access to most system calls */
1018 if (pledge("stdio recvfd", NULL) == -1) {
1019 err = got_error_from_errno("pledge");
1020 got_privsep_send_error(&ibuf, err);
1024 /* revoke fs access */
1025 if (landlock_no_fs() == -1) {
1026 err = got_error_from_errno("landlock_no_fs");
1027 got_privsep_send_error(&ibuf, err);
1030 if (cap_enter() == -1) {
1031 err = got_error_from_errno("cap_enter");
1032 got_privsep_send_error(&ibuf, err);
1036 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1039 if (imsg.hdr.type == GOT_IMSG_STOP)
1041 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
1042 err = got_error(GOT_ERR_PRIVSEP_MSG);
1045 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
1046 err = got_error(GOT_ERR_PRIVSEP_LEN);
1049 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
1052 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1055 if (imsg.hdr.type == GOT_IMSG_STOP)
1057 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
1058 err = got_error(GOT_ERR_PRIVSEP_MSG);
1061 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1062 err = got_error(GOT_ERR_PRIVSEP_LEN);
1067 for (i = 0; i < nitems(tmpfiles); i++) {
1068 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1071 if (imsg.hdr.type == GOT_IMSG_STOP)
1073 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
1074 err = got_error(GOT_ERR_PRIVSEP_MSG);
1077 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1078 err = got_error(GOT_ERR_PRIVSEP_LEN);
1082 tmpfiles[i] = fdopen(tmpfd, "w+");
1083 if (tmpfiles[i] == NULL) {
1084 err = got_error_from_errno("fdopen");
1090 if (lseek(pack.fd, 0, SEEK_END) == -1) {
1091 err = got_error_from_errno("lseek");
1094 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
1095 if (packfile_size == -1) {
1096 err = got_error_from_errno("lseek");
1099 pack.filesize = packfile_size; /* XXX off_t vs size_t */
1101 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
1102 err = got_error_from_errno("lseek");
1106 #ifndef GOT_PACK_NO_MMAP
1107 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
1109 if (pack.map == MAP_FAILED)
1110 pack.map = NULL; /* fall back to read(2) */
1112 err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
1115 close_err = got_pack_close(&pack);
1116 if (close_err && err == NULL)
1118 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1119 err = got_error_from_errno("close");
1120 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
1121 err = got_error_from_errno("close");
1122 for (i = 0; i < nitems(tmpfiles); i++) {
1123 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
1125 err = got_error_from_errno("fclose");
1129 err = send_index_pack_done(&ibuf);
1131 got_privsep_send_error(&ibuf, err);
1132 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1133 got_privsep_send_error(&ibuf, err);