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>
43 #include "got_error.h"
44 #include "got_object.h"
46 #include "got_lib_sha1.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_inflate.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_parse.h"
51 #include "got_lib_object_idset.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_delta_cache.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 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);
260 SHA1Update(&ctx, data, datalen);
261 SHA1Final(obj->id.sha1, &ctx);
265 case GOT_OBJ_TYPE_REF_DELTA:
266 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
268 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
269 err = got_error(GOT_ERR_BAD_PACKFILE);
272 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
274 obj->crc = crc32(obj->crc, pack->map + mapoff,
276 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
278 mapoff += SHA1_DIGEST_LENGTH;
279 err = got_inflate_to_mem_mmap(NULL, &datalen,
280 &obj->len, &csum, pack->map, mapoff,
281 pack->filesize - mapoff);
285 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
288 err = got_error_from_errno("read");
291 if (n < sizeof(obj->id)) {
292 err = got_error(GOT_ERR_BAD_PACKFILE);
295 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
297 SHA1Update(pack_sha1_ctx, obj->delta.ref.ref_id.sha1,
299 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
300 &csum, obj->size, pack->fd);
304 obj->len += SHA1_DIGEST_LENGTH;
306 case GOT_OBJ_TYPE_OFFSET_DELTA:
307 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
308 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
309 &obj->delta.ofs.base_offsetlen, pack, obj->off,
315 obj->crc = crc32(obj->crc, pack->map + mapoff,
316 obj->delta.ofs.base_offsetlen);
317 SHA1Update(pack_sha1_ctx, pack->map + mapoff,
318 obj->delta.ofs.base_offsetlen);
319 mapoff += obj->delta.ofs.base_offsetlen;
320 err = got_inflate_to_mem_mmap(NULL, &datalen,
321 &obj->len, &csum, pack->map, mapoff,
322 pack->filesize - mapoff);
327 * XXX Seek back and get CRC and SHA1 of on-disk
330 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
332 err = got_error_from_errno("lseek");
335 err = read_checksum(&obj->crc, pack_sha1_ctx,
336 pack->fd, obj->delta.ofs.base_offsetlen);
340 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
341 &csum, obj->size, pack->fd);
345 obj->len += obj->delta.ofs.base_offsetlen;
348 err = got_error(GOT_ERR_OBJ_TYPE);
355 static const struct got_error *
356 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
360 SHA1Update(ctx, buf, len);
362 w = write(fd, buf, len);
364 return got_error_from_errno("write");
366 return got_error(GOT_ERR_IO);
371 static const struct got_error *
372 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
373 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
374 FILE *delta_accum_file)
376 const struct got_error *err = NULL;
377 struct got_delta_chain deltas;
378 struct got_delta *delta;
386 const char *obj_label;
389 SIMPLEQ_INIT(&deltas.entries);
391 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
392 obj->off, obj->tslen, obj->type, obj->size,
393 GOT_DELTA_CHAIN_RECURSION_MAX);
397 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
400 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
402 rewind(delta_base_file);
403 rewind(delta_accum_file);
404 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
405 pack, tmpfile, delta_base_file, delta_accum_file);
409 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
415 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
418 err = get_obj_type_label(&obj_label, base_obj_type);
421 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
422 err = got_error_from_errno("asprintf");
425 headerlen = strlen(header) + 1;
427 SHA1Update(&ctx, header, headerlen);
428 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
429 err = read_file_sha1(&ctx, tmpfile, len);
433 SHA1Update(&ctx, buf, len);
434 SHA1Final(obj->id.sha1, &ctx);
438 while (!SIMPLEQ_EMPTY(&deltas.entries)) {
439 delta = SIMPLEQ_FIRST(&deltas.entries);
440 SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
446 /* Determine the slot in the pack index a given object ID should use. */
448 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
450 u_int8_t id0 = sha1[0];
451 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
452 int left = 0, right = nindexed - 1;
456 left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
458 while (left <= right) {
459 struct got_packidx_object_id *oid;
461 i = ((left + right) / 2);
462 oid = &packidx->hdr.sorted_ids[i];
464 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
466 return -1; /* object already indexed */
478 print_packidx(struct got_packidx *packidx)
480 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
483 fprintf(stderr, "object IDs:\n");
484 for (i = 0; i < nindexed; i++) {
485 char hex[SHA1_DIGEST_STRING_LENGTH];
486 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
488 fprintf(stderr, "%s\n", hex);
490 fprintf(stderr, "\n");
492 fprintf(stderr, "object offsets:\n");
493 for (i = 0; i < nindexed; i++) {
494 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
495 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
496 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
497 fprintf(stderr, "%u -> %llu\n", offset,
498 be64toh(packidx->hdr.large_offsets[j]));
500 fprintf(stderr, "%u\n", offset);
502 fprintf(stderr, "\n");
504 fprintf(stderr, "fanout table:");
505 for (i = 0; i <= 0xff; i++)
506 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
507 fprintf(stderr, "\n");
512 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
513 struct got_indexed_object *obj)
517 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
519 packidx->hdr.crc32[idx] = htobe32(obj->crc);
520 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
521 packidx->hdr.offsets[idx] = htobe32(obj->off);
523 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
524 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
525 packidx->hdr.large_offsets[packidx->nlargeobj] =
527 packidx->nlargeobj++;
530 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
531 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
532 packidx->hdr.fanout_table[i] = htobe32(n + 1);
537 indexed_obj_cmp(const void *pa, const void *pb)
539 struct got_indexed_object *a, *b;
541 a = (struct got_indexed_object *)pa;
542 b = (struct got_indexed_object *)pb;
543 return got_object_id_cmp(&a->id, &b->id);
547 make_packidx(struct got_packidx *packidx, int nobj,
548 struct got_indexed_object *objects)
550 struct got_indexed_object *obj;
554 qsort(objects, nobj, sizeof(struct got_indexed_object),
557 memset(packidx->hdr.fanout_table, 0,
558 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
559 packidx->nlargeobj = 0;
561 for (i = 0; i < nobj; i++) {
564 add_indexed_object(packidx, idx++, obj);
569 update_packidx(struct got_packidx *packidx, int nobj,
570 struct got_indexed_object *obj)
573 uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
575 idx = find_object_idx(packidx, obj->id.sha1);
577 char hex[SHA1_DIGEST_STRING_LENGTH];
578 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
579 return; /* object already indexed */
582 memmove(&packidx->hdr.sorted_ids[idx + 1],
583 &packidx->hdr.sorted_ids[idx],
584 sizeof(struct got_packidx_object_id) * (nindexed - idx));
585 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
586 sizeof(uint32_t) * (nindexed - idx));
588 add_indexed_object(packidx, idx, obj);
591 static const struct got_error *
592 send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
593 int nobj_indexed, int nobj_loose, int nobj_resolved)
595 struct got_imsg_index_pack_progress iprogress;
597 iprogress.nobj_total = nobj_total;
598 iprogress.nobj_indexed = nobj_indexed;
599 iprogress.nobj_loose = nobj_loose;
600 iprogress.nobj_resolved = nobj_resolved;
602 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
603 &iprogress, sizeof(iprogress)) == -1)
604 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
606 return got_privsep_flush_imsg(ibuf);
609 static const struct got_error *
610 send_index_pack_done(struct imsgbuf *ibuf)
612 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
613 return got_error_from_errno("imsg_compose FETCH");
614 return got_privsep_flush_imsg(ibuf);
618 static const struct got_error *
619 index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
620 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
621 struct imsgbuf *ibuf)
623 const struct got_error *err;
624 struct got_packfile_hdr hdr;
625 struct got_packidx packidx;
627 char pack_sha1[SHA1_DIGEST_LENGTH];
628 int nobj, nvalid, nloose, nresolved = 0, i;
629 struct got_indexed_object *objects = NULL, *obj;
631 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
633 int pass, have_ref_deltas = 0, first_delta_idx = -1;
635 int p_indexed = 0, last_p_indexed = -1;
636 int p_resolved = 0, last_p_resolved = -1;
638 /* Require that pack file header and SHA1 trailer are present. */
639 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
640 return got_error_msg(GOT_ERR_BAD_PACKFILE,
644 memcpy(&hdr, pack->map, sizeof(hdr));
645 mapoff += sizeof(hdr);
647 r = read(pack->fd, &hdr, sizeof(hdr));
649 return got_error_from_errno("read");
651 return got_error_msg(GOT_ERR_BAD_PACKFILE,
655 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
656 return got_error_msg(GOT_ERR_BAD_PACKFILE,
657 "bad packfile signature");
658 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
659 return got_error_msg(GOT_ERR_BAD_PACKFILE,
660 "bad packfile version");
661 nobj = be32toh(hdr.nobjects);
663 return got_error_msg(GOT_ERR_BAD_PACKFILE,
664 "bad packfile with zero objects");
666 /* We compute the SHA1 of pack file contents and verify later on. */
668 SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
671 * Create an in-memory pack index which will grow as objects
672 * IDs in the pack file are discovered. Only fields used to
673 * read deltified objects will be needed by the pack.c library
674 * code, so setting up just a pack index header is sufficient.
676 memset(&packidx, 0, sizeof(packidx));
677 packidx.hdr.magic = malloc(sizeof(uint32_t));
678 if (packidx.hdr.magic == NULL)
679 return got_error_from_errno("calloc");
680 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
681 packidx.hdr.version = malloc(sizeof(uint32_t));
682 if (packidx.hdr.version == NULL) {
683 err = got_error_from_errno("malloc");
686 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
687 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
689 if (packidx.hdr.fanout_table == NULL) {
690 err = got_error_from_errno("calloc");
693 packidx.hdr.sorted_ids = calloc(nobj,
694 sizeof(struct got_packidx_object_id));
695 if (packidx.hdr.sorted_ids == NULL) {
696 err = got_error_from_errno("calloc");
699 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
700 if (packidx.hdr.crc32 == NULL) {
701 err = got_error_from_errno("calloc");
704 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
705 if (packidx.hdr.offsets == NULL) {
706 err = got_error_from_errno("calloc");
709 /* Large offsets table is empty for pack files < 2 GB. */
710 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
711 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
712 if (packidx.hdr.large_offsets == NULL) {
713 err = got_error_from_errno("calloc");
720 objects = calloc(nobj, sizeof(struct got_indexed_object));
722 return got_error_from_errno("calloc");
725 * First pass: locate all objects and identify un-deltified objects.
727 * When this pass has completed we will know offset, type, size, and
728 * CRC information for all objects in this pack file. We won't know
729 * any of the actual object IDs of deltified objects yet since we
730 * will not yet attempt to combine deltas.
733 for (i = 0; i < nobj; i++) {
734 /* Don't send too many progress privsep messages. */
735 p_indexed = ((i + 1) * 100) / nobj;
736 if (p_indexed != last_p_indexed) {
737 err = send_index_pack_progress(ibuf, nobj, i + 1,
741 last_p_indexed = p_indexed;
745 obj->crc = crc32(0L, NULL, 0);
747 /* Store offset to type+size information for this object. */
751 obj->off = lseek(pack->fd, 0, SEEK_CUR);
752 if (obj->off == -1) {
753 err = got_error_from_errno("lseek");
758 err = read_packed_object(pack, obj, tmpfile, &ctx);
763 mapoff += obj->tslen + obj->len;
765 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
767 err = got_error_from_errno("lseek");
772 if (obj->type == GOT_OBJ_TYPE_BLOB ||
773 obj->type == GOT_OBJ_TYPE_TREE ||
774 obj->type == GOT_OBJ_TYPE_COMMIT ||
775 obj->type == GOT_OBJ_TYPE_TAG) {
779 if (first_delta_idx == -1)
781 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
788 * Having done a full pass over the pack file and can now
789 * verify its checksum.
791 SHA1Final(pack_sha1, &ctx);
792 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
793 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
794 "pack file checksum mismatch");
798 /* Verify the SHA1 checksum stored at the end of the pack file. */
800 memcpy(pack_sha1_expected, pack->map +
801 pack->filesize - SHA1_DIGEST_LENGTH,
805 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
806 err = got_error_from_errno("lseek");
809 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
811 err = got_error_from_errno("read");
814 if (n != SHA1_DIGEST_LENGTH) {
815 err = got_error(GOT_ERR_IO);
819 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
820 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
821 "bad checksum in pack file trailer");
825 if (first_delta_idx == -1)
828 /* In order to resolve ref deltas we need an in-progress pack index. */
830 make_packidx(&packidx, nobj, objects);
833 * Second pass: We can now resolve deltas to compute the IDs of
834 * objects which appear in deltified form. Because deltas can be
835 * chained this pass may require a couple of iterations until all
836 * IDs of deltified objects have been discovered.
839 while (nvalid != nobj) {
842 * This loop will only run once unless the pack file
843 * contains ref deltas which refer to objects located
844 * later in the pack file, which is unusual.
845 * Offset deltas can always be resolved in one pass
846 * unless the packfile is corrupt.
848 for (i = first_delta_idx; i < nobj; i++) {
850 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
851 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
857 if (pack->map == NULL && lseek(pack->fd,
858 obj->off + obj->tslen, SEEK_SET) == -1) {
859 err = got_error_from_errno("lseek");
863 err = resolve_deltified_object(pack, &packidx, obj,
864 tmpfile, delta_base_file, delta_accum_file);
866 if (err->code != GOT_ERR_NO_OBJ)
869 * We cannot resolve this object yet because
870 * a delta base is unknown. Try again later.
878 update_packidx(&packidx, nobj, obj);
879 /* Don't send too many progress privsep messages. */
880 p_resolved = ((nresolved + n) * 100) / nobj;
881 if (p_resolved != last_p_resolved) {
882 err = send_index_pack_progress(ibuf, nobj,
883 nobj, nloose, nresolved + n);
886 last_p_resolved = p_resolved;
890 if (pass++ > 3 && n == 0) {
892 snprintf(msg, sizeof(msg), "could not resolve "
893 "any of deltas; packfile could be corrupt");
894 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
902 if (nloose + nresolved != nobj) {
904 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
905 nloose + nresolved, nobj);
906 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
910 err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved);
914 make_packidx(&packidx, nobj, objects);
920 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
921 putbe32(buf + 4, GOT_PACKIDX_VERSION);
922 err = hwrite(idxfd, buf, 8, &ctx);
925 err = hwrite(idxfd, packidx.hdr.fanout_table,
926 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
929 err = hwrite(idxfd, packidx.hdr.sorted_ids,
930 nobj * SHA1_DIGEST_LENGTH, &ctx);
933 err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
936 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
940 if (packidx.nlargeobj > 0) {
941 err = hwrite(idxfd, packidx.hdr.large_offsets,
942 packidx.nlargeobj * sizeof(uint64_t), &ctx);
946 err = hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
950 SHA1Final(packidx_hash, &ctx);
951 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
953 err = got_error_from_errno("write");
956 if (w != sizeof(packidx_hash)) {
957 err = got_error(GOT_ERR_IO);
962 free(packidx.hdr.magic);
963 free(packidx.hdr.version);
964 free(packidx.hdr.fanout_table);
965 free(packidx.hdr.sorted_ids);
966 free(packidx.hdr.offsets);
967 free(packidx.hdr.large_offsets);
972 main(int argc, char **argv)
974 const struct got_error *err = NULL, *close_err;
977 int idxfd = -1, tmpfd = -1, i;
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);