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 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)
599 struct got_imsg_index_pack_progress iprogress;
601 iprogress.nobj_total = nobj_total;
602 iprogress.nobj_indexed = nobj_indexed;
603 iprogress.nobj_loose = nobj_loose;
604 iprogress.nobj_resolved = nobj_resolved;
606 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
607 &iprogress, sizeof(iprogress)) == -1)
608 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
610 return got_privsep_flush_imsg(ibuf);
613 static const struct got_error *
614 send_index_pack_done(struct imsgbuf *ibuf)
616 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
617 return got_error_from_errno("imsg_compose FETCH");
618 return got_privsep_flush_imsg(ibuf);
622 static const struct got_error *
623 index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
624 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_sha1_expected,
625 struct imsgbuf *ibuf)
627 const struct got_error *err;
628 struct got_packfile_hdr hdr;
629 struct got_packidx packidx;
631 char pack_sha1[SHA1_DIGEST_LENGTH];
632 int nobj, nvalid, nloose, nresolved = 0, i;
633 struct got_indexed_object *objects = NULL, *obj;
635 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
637 int pass, have_ref_deltas = 0, first_delta_idx = -1;
639 int p_indexed = 0, last_p_indexed = -1;
640 int p_resolved = 0, last_p_resolved = -1;
642 /* Require that pack file header and SHA1 trailer are present. */
643 if (pack->filesize < sizeof(hdr) + SHA1_DIGEST_LENGTH)
644 return got_error_msg(GOT_ERR_BAD_PACKFILE,
648 memcpy(&hdr, pack->map, sizeof(hdr));
649 mapoff += sizeof(hdr);
651 r = read(pack->fd, &hdr, sizeof(hdr));
653 return got_error_from_errno("read");
655 return got_error_msg(GOT_ERR_BAD_PACKFILE,
659 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
660 return got_error_msg(GOT_ERR_BAD_PACKFILE,
661 "bad packfile signature");
662 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
663 return got_error_msg(GOT_ERR_BAD_PACKFILE,
664 "bad packfile version");
665 nobj = be32toh(hdr.nobjects);
667 return got_error_msg(GOT_ERR_BAD_PACKFILE,
668 "bad packfile with zero objects");
670 /* We compute the SHA1 of pack file contents and verify later on. */
672 SHA1Update(&ctx, (void *)&hdr, sizeof(hdr));
675 * Create an in-memory pack index which will grow as objects
676 * IDs in the pack file are discovered. Only fields used to
677 * read deltified objects will be needed by the pack.c library
678 * code, so setting up just a pack index header is sufficient.
680 memset(&packidx, 0, sizeof(packidx));
681 packidx.hdr.magic = malloc(sizeof(uint32_t));
682 if (packidx.hdr.magic == NULL)
683 return got_error_from_errno("malloc");
684 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
685 packidx.hdr.version = malloc(sizeof(uint32_t));
686 if (packidx.hdr.version == NULL) {
687 err = got_error_from_errno("malloc");
690 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
691 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
693 if (packidx.hdr.fanout_table == NULL) {
694 err = got_error_from_errno("calloc");
697 packidx.hdr.sorted_ids = calloc(nobj,
698 sizeof(struct got_packidx_object_id));
699 if (packidx.hdr.sorted_ids == NULL) {
700 err = got_error_from_errno("calloc");
703 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
704 if (packidx.hdr.crc32 == NULL) {
705 err = got_error_from_errno("calloc");
708 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
709 if (packidx.hdr.offsets == NULL) {
710 err = got_error_from_errno("calloc");
713 /* Large offsets table is empty for pack files < 2 GB. */
714 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
715 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
716 if (packidx.hdr.large_offsets == NULL) {
717 err = got_error_from_errno("calloc");
724 objects = calloc(nobj, sizeof(struct got_indexed_object));
726 return got_error_from_errno("calloc");
729 * First pass: locate all objects and identify un-deltified objects.
731 * When this pass has completed we will know offset, type, size, and
732 * CRC information for all objects in this pack file. We won't know
733 * any of the actual object IDs of deltified objects yet since we
734 * will not yet attempt to combine deltas.
737 for (i = 0; i < nobj; i++) {
738 /* Don't send too many progress privsep messages. */
739 p_indexed = ((i + 1) * 100) / nobj;
740 if (p_indexed != last_p_indexed) {
741 err = send_index_pack_progress(ibuf, nobj, i + 1,
745 last_p_indexed = p_indexed;
749 obj->crc = crc32(0L, NULL, 0);
751 /* Store offset to type+size information for this object. */
755 obj->off = lseek(pack->fd, 0, SEEK_CUR);
756 if (obj->off == -1) {
757 err = got_error_from_errno("lseek");
762 err = read_packed_object(pack, obj, tmpfile, &ctx);
767 mapoff += obj->tslen + obj->len;
769 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
771 err = got_error_from_errno("lseek");
776 if (obj->type == GOT_OBJ_TYPE_BLOB ||
777 obj->type == GOT_OBJ_TYPE_TREE ||
778 obj->type == GOT_OBJ_TYPE_COMMIT ||
779 obj->type == GOT_OBJ_TYPE_TAG) {
783 if (first_delta_idx == -1)
785 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
792 * Having done a full pass over the pack file and can now
793 * verify its checksum.
795 SHA1Final(pack_sha1, &ctx);
796 if (memcmp(pack_sha1_expected, pack_sha1, SHA1_DIGEST_LENGTH) != 0) {
797 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
798 "pack file checksum mismatch");
802 /* Verify the SHA1 checksum stored at the end of the pack file. */
804 memcpy(pack_sha1_expected, pack->map +
805 pack->filesize - SHA1_DIGEST_LENGTH,
809 if (lseek(pack->fd, -SHA1_DIGEST_LENGTH, SEEK_END) == -1) {
810 err = got_error_from_errno("lseek");
813 n = read(pack->fd, pack_sha1_expected, SHA1_DIGEST_LENGTH);
815 err = got_error_from_errno("read");
818 if (n != SHA1_DIGEST_LENGTH) {
819 err = got_error(GOT_ERR_IO);
823 if (memcmp(pack_sha1, pack_sha1_expected, SHA1_DIGEST_LENGTH) != 0) {
824 err = got_error_msg(GOT_ERR_BAD_PACKFILE,
825 "bad checksum in pack file trailer");
829 if (first_delta_idx == -1)
832 /* In order to resolve ref deltas we need an in-progress pack index. */
834 make_packidx(&packidx, nobj, objects);
837 * Second pass: We can now resolve deltas to compute the IDs of
838 * objects which appear in deltified form. Because deltas can be
839 * chained this pass may require a couple of iterations until all
840 * IDs of deltified objects have been discovered.
843 while (nvalid != nobj) {
846 * This loop will only run once unless the pack file
847 * contains ref deltas which refer to objects located
848 * later in the pack file, which is unusual.
849 * Offset deltas can always be resolved in one pass
850 * unless the packfile is corrupt.
852 for (i = first_delta_idx; i < nobj; i++) {
854 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
855 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
861 if (pack->map == NULL && lseek(pack->fd,
862 obj->off + obj->tslen, SEEK_SET) == -1) {
863 err = got_error_from_errno("lseek");
867 err = resolve_deltified_object(pack, &packidx, obj,
868 tmpfile, delta_base_file, delta_accum_file);
870 if (err->code != GOT_ERR_NO_OBJ)
873 * We cannot resolve this object yet because
874 * a delta base is unknown. Try again later.
882 update_packidx(&packidx, nobj, obj);
883 /* Don't send too many progress privsep messages. */
884 p_resolved = ((nresolved + n) * 100) / nobj;
885 if (p_resolved != last_p_resolved) {
886 err = send_index_pack_progress(ibuf, nobj,
887 nobj, nloose, nresolved + n);
890 last_p_resolved = p_resolved;
894 if (pass++ > 3 && n == 0) {
896 snprintf(msg, sizeof(msg), "could not resolve "
897 "any of deltas; packfile could be corrupt");
898 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
906 if (nloose + nresolved != nobj) {
908 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
909 nloose + nresolved, nobj);
910 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
914 err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved);
918 make_packidx(&packidx, nobj, objects);
924 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
925 putbe32(buf + 4, GOT_PACKIDX_VERSION);
926 err = hwrite(idxfd, buf, 8, &ctx);
929 err = hwrite(idxfd, packidx.hdr.fanout_table,
930 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
933 err = hwrite(idxfd, packidx.hdr.sorted_ids,
934 nobj * SHA1_DIGEST_LENGTH, &ctx);
937 err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
940 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
944 if (packidx.nlargeobj > 0) {
945 err = hwrite(idxfd, packidx.hdr.large_offsets,
946 packidx.nlargeobj * sizeof(uint64_t), &ctx);
950 err = hwrite(idxfd, pack_sha1, SHA1_DIGEST_LENGTH, &ctx);
954 SHA1Final(packidx_hash, &ctx);
955 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
957 err = got_error_from_errno("write");
960 if (w != sizeof(packidx_hash)) {
961 err = got_error(GOT_ERR_IO);
966 free(packidx.hdr.magic);
967 free(packidx.hdr.version);
968 free(packidx.hdr.fanout_table);
969 free(packidx.hdr.sorted_ids);
970 free(packidx.hdr.offsets);
971 free(packidx.hdr.large_offsets);
976 main(int argc, char **argv)
978 const struct got_error *err = NULL, *close_err;
982 int idxfd = -1, tmpfd = -1;
984 struct got_pack pack;
985 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
993 for (i = 0; i < nitems(tmpfiles); i++)
996 memset(&pack, 0, sizeof(pack));
998 pack.delta_cache = got_delta_cache_alloc(500,
999 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
1000 if (pack.delta_cache == NULL) {
1001 err = got_error_from_errno("got_delta_cache_alloc");
1005 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1007 /* revoke access to most system calls */
1008 if (pledge("stdio recvfd", NULL) == -1) {
1009 err = got_error_from_errno("pledge");
1010 got_privsep_send_error(&ibuf, err);
1014 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1017 if (imsg.hdr.type == GOT_IMSG_STOP)
1019 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
1020 err = got_error(GOT_ERR_PRIVSEP_MSG);
1023 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
1024 err = got_error(GOT_ERR_PRIVSEP_LEN);
1027 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
1030 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1033 if (imsg.hdr.type == GOT_IMSG_STOP)
1035 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
1036 err = got_error(GOT_ERR_PRIVSEP_MSG);
1039 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1040 err = got_error(GOT_ERR_PRIVSEP_LEN);
1045 for (i = 0; i < nitems(tmpfiles); i++) {
1046 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1049 if (imsg.hdr.type == GOT_IMSG_STOP)
1051 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
1052 err = got_error(GOT_ERR_PRIVSEP_MSG);
1055 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
1056 err = got_error(GOT_ERR_PRIVSEP_LEN);
1060 tmpfiles[i] = fdopen(tmpfd, "w+");
1061 if (tmpfiles[i] == NULL) {
1062 err = got_error_from_errno("fdopen");
1068 if (lseek(pack.fd, 0, SEEK_END) == -1) {
1069 err = got_error_from_errno("lseek");
1072 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
1073 if (packfile_size == -1) {
1074 err = got_error_from_errno("lseek");
1077 pack.filesize = packfile_size; /* XXX off_t vs size_t */
1079 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
1080 err = got_error_from_errno("lseek");
1084 #ifndef GOT_PACK_NO_MMAP
1085 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
1087 if (pack.map == MAP_FAILED)
1088 pack.map = NULL; /* fall back to read(2) */
1090 err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
1093 close_err = got_pack_close(&pack);
1094 if (close_err && err == NULL)
1096 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1097 err = got_error_from_errno("close");
1098 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
1099 err = got_error_from_errno("close");
1100 for (i = 0; i < nitems(tmpfiles); i++) {
1101 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
1103 err = got_error_from_errno("fclose");
1107 err = send_index_pack_done(&ibuf);
1109 got_privsep_send_error(&ibuf, err);
1110 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1111 got_privsep_send_error(&ibuf, err);