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"
55 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
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);
259 SHA1Update(&ctx, data, datalen);
260 SHA1Final(obj->id.sha1, &ctx);
264 case GOT_OBJ_TYPE_REF_DELTA:
265 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
267 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
268 err = got_error(GOT_ERR_BAD_PACKFILE);
271 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
273 obj->crc = crc32(obj->crc, pack->map + mapoff,
275 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
277 mapoff += SHA1_DIGEST_LENGTH;
278 err = got_inflate_to_mem_mmap(NULL, &datalen,
279 &obj->len, &csum, pack->map, mapoff,
280 pack->filesize - mapoff);
284 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
287 err = got_error_from_errno("read");
290 if (n < sizeof(obj->id)) {
291 err = got_error(GOT_ERR_BAD_PACKFILE);
294 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
296 SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
298 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
299 &csum, obj->size, pack->fd);
303 obj->len += SHA1_DIGEST_LENGTH;
305 case GOT_OBJ_TYPE_OFFSET_DELTA:
306 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
307 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
308 &obj->delta.ofs.base_offsetlen, pack, obj->off,
314 obj->crc = crc32(obj->crc, pack->map + mapoff,
315 obj->delta.ofs.base_offsetlen);
316 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
317 obj->delta.ofs.base_offsetlen);
318 mapoff += obj->delta.ofs.base_offsetlen;
319 err = got_inflate_to_mem_mmap(NULL, &datalen,
320 &obj->len, &csum, pack->map, mapoff,
321 pack->filesize - mapoff);
326 * XXX Seek back and get CRC and SHA1 of on-disk
329 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
331 err = got_error_from_errno("lseek");
334 err = read_checksum(&obj->crc, pack_sha1_ctx,
335 pack->fd, obj->delta.ofs.base_offsetlen);
339 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
340 &csum, obj->size, pack->fd);
344 obj->len += obj->delta.ofs.base_offsetlen;
347 err = got_error(GOT_ERR_OBJ_TYPE);
354 static const struct got_error *
355 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
359 SHA1Update(ctx, buf, len);
361 w = write(fd, buf, len);
363 return got_error_from_errno("write");
365 return got_error(GOT_ERR_IO);
370 static const struct got_error *
371 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
372 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
373 FILE *delta_accum_file)
375 const struct got_error *err = NULL;
376 struct got_delta_chain deltas;
377 struct got_delta *delta;
385 const char *obj_label;
388 STAILQ_INIT(&deltas.entries);
390 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
391 obj->off, obj->tslen, obj->type, obj->size,
392 GOT_DELTA_CHAIN_RECURSION_MAX);
396 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
399 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
401 rewind(delta_base_file);
402 rewind(delta_accum_file);
403 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
404 pack, tmpfile, delta_base_file, delta_accum_file);
408 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
414 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
417 err = get_obj_type_label(&obj_label, base_obj_type);
420 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
421 err = got_error_from_errno("asprintf");
424 headerlen = strlen(header) + 1;
426 SHA1Update(&ctx, header, headerlen);
427 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
428 err = read_file_sha1(&ctx, tmpfile, len);
432 SHA1Update(&ctx, buf, len);
433 SHA1Final(obj->id.sha1, &ctx);
437 while (!STAILQ_EMPTY(&deltas.entries)) {
438 delta = STAILQ_FIRST(&deltas.entries);
439 STAILQ_REMOVE_HEAD(&deltas.entries, entry);
445 /* Determine the slot in the pack index a given object ID should use. */
447 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
449 u_int8_t id0 = sha1[0];
450 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
451 int left = 0, right = nindexed - 1;
455 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
457 while (left <= right) {
458 struct got_packidx_object_id *oid;
460 i = ((left + right) / 2);
461 oid = &packidx->hdr.sorted_ids[i];
463 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
465 return -1; /* object already indexed */
477 print_packidx(struct got_packidx *packidx)
479 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
482 fprintf(stderr, "object IDs:\n");
483 for (i = 0; i < nindexed; i++) {
484 char hex[SHA1_DIGEST_STRING_LENGTH];
485 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
487 fprintf(stderr, "%s\n", hex);
489 fprintf(stderr, "\n");
491 fprintf(stderr, "object offsets:\n");
492 for (i = 0; i < nindexed; i++) {
493 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
494 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
495 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
496 fprintf(stderr, "%u -> %llu\n", offset,
497 be64toh(packidx->hdr.large_offsets[j]));
499 fprintf(stderr, "%u\n", offset);
501 fprintf(stderr, "\n");
503 fprintf(stderr, "fanout table:");
504 for (i = 0; i <= 0xff; i++)
505 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
506 fprintf(stderr, "\n");
511 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
512 struct got_indexed_object *obj)
516 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
518 packidx->hdr.crc32[idx] = htobe32(obj->crc);
519 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
520 packidx->hdr.offsets[idx] = htobe32(obj->off);
522 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
523 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
524 packidx->hdr.large_offsets[packidx->nlargeobj] =
526 packidx->nlargeobj++;
529 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
530 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
531 packidx->hdr.fanout_table[i] = htobe32(n + 1);
536 indexed_obj_cmp(const void *pa, const void *pb)
538 struct got_indexed_object *a, *b;
540 a = (struct got_indexed_object *)pa;
541 b = (struct got_indexed_object *)pb;
542 return got_object_id_cmp(&a->id, &b->id);
546 make_packidx(struct got_packidx *packidx, int nobj,
547 struct got_indexed_object *objects)
549 struct got_indexed_object *obj;
553 qsort(objects, nobj, sizeof(struct got_indexed_object),
556 memset(packidx->hdr.fanout_table, 0,
557 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
558 packidx->nlargeobj = 0;
560 for (i = 0; i < nobj; i++) {
563 add_indexed_object(packidx, idx++, obj);
568 update_packidx(struct got_packidx *packidx, int nobj,
569 struct got_indexed_object *obj)
572 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
574 idx = find_object_idx(packidx, obj->id.sha1);
576 char hex[SHA1_DIGEST_STRING_LENGTH];
577 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
578 return; /* object already indexed */
581 memmove(&packidx->hdr.sorted_ids[idx + 1],
582 &packidx->hdr.sorted_ids[idx],
583 sizeof(struct got_packidx_object_id) * (nindexed - idx));
584 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
585 sizeof(uint32_t) * (nindexed - idx));
587 add_indexed_object(packidx, idx, obj);
590 static const struct got_error *
591 send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
592 int nobj_indexed, int nobj_loose, int nobj_resolved)
594 struct got_imsg_index_pack_progress iprogress;
596 iprogress.nobj_total = nobj_total;
597 iprogress.nobj_indexed = nobj_indexed;
598 iprogress.nobj_loose = nobj_loose;
599 iprogress.nobj_resolved = nobj_resolved;
601 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
602 &iprogress, sizeof(iprogress)) == -1)
603 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
605 return got_privsep_flush_imsg(ibuf);
608 static const struct got_error *
609 send_index_pack_done(struct imsgbuf *ibuf)
611 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
612 return got_error_from_errno("imsg_compose FETCH");
613 return got_privsep_flush_imsg(ibuf);
617 static const struct got_error *
618 index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
619 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
620 struct imsgbuf *ibuf)
622 const struct got_error *err;
623 struct got_packfile_hdr hdr;
624 struct got_packidx packidx;
626 char pack_sha1[SHA1_DIGEST_LENGTH];
627 int nobj, nvalid, nloose, nresolved = 0, i;
628 struct got_indexed_object *objects = NULL, *obj;
630 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
632 int pass, have_ref_deltas = 0, first_delta_idx = -1;
634 int p_indexed = 0, last_p_indexed = -1;
635 int p_resolved = 0, last_p_resolved = -1;
637 /* Require that pack file header and SHA1 trailer are present. */
638 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
639 return got_error_msg(GOT_ERR_BAD_PACKFILE,
643 memcpy(&hdr, pack->map, sizeof(hdr));
644 mapoff += sizeof(hdr);
646 r = read(pack->fd, &hdr, sizeof(hdr));
648 return got_error_from_errno("read");
650 return got_error_msg(GOT_ERR_BAD_PACKFILE,
654 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
655 return got_error_msg(GOT_ERR_BAD_PACKFILE,
656 "bad packfile signature");
657 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
658 return got_error_msg(GOT_ERR_BAD_PACKFILE,
659 "bad packfile version");
660 nobj = be32toh(hdr.nobjects);
662 return got_error_msg(GOT_ERR_BAD_PACKFILE,
663 "bad packfile with zero objects");
665 /* We compute the SHA1 of pack file contents and verify later on. */
667 SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
670 * Create an in-memory pack index which will grow as objects
671 * IDs in the pack file are discovered. Only fields used to
672 * read deltified objects will be needed by the pack.c library
673 * code, so setting up just a pack index header is sufficient.
675 memset(&packidx, 0, sizeof(packidx));
676 packidx.hdr.magic = malloc(sizeof(uint32_t));
677 if (packidx.hdr.magic == NULL)
678 return got_error_from_errno("calloc");
679 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
680 packidx.hdr.version = malloc(sizeof(uint32_t));
681 if (packidx.hdr.version == NULL) {
682 err = got_error_from_errno("malloc");
685 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
686 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
688 if (packidx.hdr.fanout_table == NULL) {
689 err = got_error_from_errno("calloc");
692 packidx.hdr.sorted_ids = calloc(nobj,
693 sizeof(struct got_packidx_object_id));
694 if (packidx.hdr.sorted_ids == NULL) {
695 err = got_error_from_errno("calloc");
698 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
699 if (packidx.hdr.crc32 == NULL) {
700 err = got_error_from_errno("calloc");
703 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
704 if (packidx.hdr.offsets == NULL) {
705 err = got_error_from_errno("calloc");
708 /* Large offsets table is empty for pack files < 2 GB. */
709 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
710 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
711 if (packidx.hdr.large_offsets == NULL) {
712 err = got_error_from_errno("calloc");
719 objects = calloc(nobj, sizeof(struct got_indexed_object));
721 return got_error_from_errno("calloc");
724 * First pass: locate all objects and identify un-deltified objects.
726 * When this pass has completed we will know offset, type, size, and
727 * CRC information for all objects in this pack file. We won't know
728 * any of the actual object IDs of deltified objects yet since we
729 * will not yet attempt to combine deltas.
732 for (i = 0; i < nobj; i++) {
733 /* Don't send too many progress privsep messages. */
734 p_indexed = ((i + 1) * 100) / nobj;
735 if (p_indexed != last_p_indexed) {
736 err = send_index_pack_progress(ibuf, nobj, i + 1,
740 last_p_indexed = p_indexed;
744 obj->crc = crc32(0L, NULL, 0);
746 /* Store offset to type+size information for this object. */
750 obj->off = lseek(pack->fd, 0, SEEK_CUR);
751 if (obj->off == -1) {
752 err = got_error_from_errno("lseek");
757 err = read_packed_object(pack, obj, tmpfile, &ctx);
762 mapoff += obj->tslen + obj->len;
764 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
766 err = got_error_from_errno("lseek");
771 if (obj->type == GOT_OBJ_TYPE_BLOB ||
772 obj->type == GOT_OBJ_TYPE_TREE ||
773 obj->type == GOT_OBJ_TYPE_COMMIT ||
774 obj->type == GOT_OBJ_TYPE_TAG) {
778 if (first_delta_idx == -1)
780 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
787 * Having done a full pass over the pack file and can now
788 * verify its checksum.
790 SHA1Final(pack_sha1, &ctx);
791 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
792 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
793 "pack file checksum mismatch");
797 /* Verify the SHA1 checksum stored at the end of the pack file. */
799 memcpy(pack_sha1_expected, pack->map +
800 pack->filesize - SHA1_DIGEST_LENGTH,
804 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
805 err = got_error_from_errno("lseek");
808 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
810 err = got_error_from_errno("read");
813 if (n != SHA1_DIGEST_LENGTH) {
814 err = got_error(GOT_ERR_IO);
818 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
819 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
820 "bad checksum in pack file trailer");
824 if (first_delta_idx == -1)
827 /* In order to resolve ref deltas we need an in-progress pack index. */
829 make_packidx(&packidx, nobj, objects);
832 * Second pass: We can now resolve deltas to compute the IDs of
833 * objects which appear in deltified form. Because deltas can be
834 * chained this pass may require a couple of iterations until all
835 * IDs of deltified objects have been discovered.
838 while (nvalid != nobj) {
841 * This loop will only run once unless the pack file
842 * contains ref deltas which refer to objects located
843 * later in the pack file, which is unusual.
844 * Offset deltas can always be resolved in one pass
845 * unless the packfile is corrupt.
847 for (i = first_delta_idx; i < nobj; i++) {
849 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
850 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
856 if (pack->map == NULL && lseek(pack->fd,
857 obj->off + obj->tslen, SEEK_SET) == -1) {
858 err = got_error_from_errno("lseek");
862 err = resolve_deltified_object(pack, &packidx, obj,
863 tmpfile, delta_base_file, delta_accum_file);
865 if (err->code != GOT_ERR_NO_OBJ)
868 * We cannot resolve this object yet because
869 * a delta base is unknown. Try again later.
877 update_packidx(&packidx, nobj, obj);
878 /* Don't send too many progress privsep messages. */
879 p_resolved = ((nresolved + n) * 100) / nobj;
880 if (p_resolved != last_p_resolved) {
881 err = send_index_pack_progress(ibuf, nobj,
882 nobj, nloose, nresolved + n);
885 last_p_resolved = p_resolved;
889 if (pass++ > 3 && n == 0) {
891 snprintf(msg, sizeof(msg), "could not resolve "
892 "any of deltas; packfile could be corrupt");
893 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
901 if (nloose + nresolved != nobj) {
903 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
904 nloose + nresolved, nobj);
905 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
909 err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved);
913 make_packidx(&packidx, nobj, objects);
919 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
920 putbe32(buf + 4, GOT_PACKIDX_VERSION);
921 err = hwrite(idxfd, buf, 8, &ctx);
924 err = hwrite(idxfd, packidx.hdr.fanout_table,
925 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
928 err = hwrite(idxfd, packidx.hdr.sorted_ids,
929 nobj * SHA1_DIGEST_LENGTH, &ctx);
932 err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
935 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
939 if (packidx.nlargeobj > 0) {
940 err = hwrite(idxfd, packidx.hdr.large_offsets,
941 packidx.nlargeobj * sizeof(uint64_t), &ctx);
945 err = hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
949 SHA1Final(packidx_hash, &ctx);
950 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
952 err = got_error_from_errno("write");
955 if (w != sizeof(packidx_hash)) {
956 err = got_error(GOT_ERR_IO);
961 free(packidx.hdr.magic);
962 free(packidx.hdr.version);
963 free(packidx.hdr.fanout_table);
964 free(packidx.hdr.sorted_ids);
965 free(packidx.hdr.offsets);
966 free(packidx.hdr.large_offsets);
971 main(int argc, char **argv)
973 const struct got_error *err = NULL, *close_err;
977 int idxfd = -1, tmpfd = -1;
979 struct got_pack pack;
980 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
988 for (i = 0; i < nitems(tmpfiles); i++)
991 memset(&pack, 0, sizeof(pack));
993 pack.delta_cache = got_delta_cache_alloc(500,
994 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
995 if (pack.delta_cache == NULL) {
996 err = got_error_from_errno("got_delta_cache_alloc");
1000 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1002 /* revoke access to most system calls */
1003 if (pledge("stdio recvfd", NULL) == -1) {
1004 err = got_error_from_errno("pledge");
1005 got_privsep_send_error(&ibuf, err);
1009 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1012 if (imsg.hdr.type == GOT_IMSG_STOP)
1014 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
1015 err = got_error(GOT_ERR_PRIVSEP_MSG);
1018 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
1019 err = got_error(GOT_ERR_PRIVSEP_LEN);
1022 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
1025 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1028 if (imsg.hdr.type == GOT_IMSG_STOP)
1030 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
1031 err = got_error(GOT_ERR_PRIVSEP_MSG);
1034 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1035 err = got_error(GOT_ERR_PRIVSEP_LEN);
1040 for (i = 0; i < nitems(tmpfiles); i++) {
1041 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1044 if (imsg.hdr.type == GOT_IMSG_STOP)
1046 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
1047 err = got_error(GOT_ERR_PRIVSEP_MSG);
1050 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1051 err = got_error(GOT_ERR_PRIVSEP_LEN);
1055 tmpfiles[i] = fdopen(tmpfd, "w+");
1056 if (tmpfiles[i] == NULL) {
1057 err = got_error_from_errno("fdopen");
1063 if (lseek(pack.fd, 0, SEEK_END) == -1) {
1064 err = got_error_from_errno("lseek");
1067 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
1068 if (packfile_size == -1) {
1069 err = got_error_from_errno("lseek");
1072 pack.filesize = packfile_size; /* XXX off_t vs size_t */
1074 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
1075 err = got_error_from_errno("lseek");
1079 #ifndef GOT_PACK_NO_MMAP
1080 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
1082 if (pack.map == MAP_FAILED)
1083 pack.map = NULL; /* fall back to read(2) */
1085 err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
1088 close_err = got_pack_close(&pack);
1089 if (close_err && err == NULL)
1091 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1092 err = got_error_from_errno("close");
1093 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
1094 err = got_error_from_errno("close");
1095 for (i = 0; i < nitems(tmpfiles); i++) {
1096 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
1098 err = got_error_from_errno("close");
1102 err = send_index_pack_done(&ibuf);
1104 got_privsep_send_error(&ibuf, err);
1105 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1106 got_privsep_send_error(&ibuf, err);