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>
20 #include <sys/syslimits.h>
22 #include <sys/types.h>
42 #include "got_error.h"
43 #include "got_object.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"
56 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
59 struct got_indexed_object {
60 struct got_object_id id;
63 * Has this object been fully resolved?
64 * If so, we know its ID, otherwise we don't and 'id' is invalid.
68 /* Offset of type+size field for this object in pack file. */
71 /* Type+size values parsed from pack file. */
75 /* Length of on-disk type+size data. */
78 /* Length of object data following type+size. */
86 struct got_object_id ref_id;
89 /* For offset deltas. */
91 size_t base_offsetlen;
97 putbe32(char *b, uint32_t n)
105 static const struct got_error *
106 get_obj_type_label(const char **label, int obj_type)
108 const struct got_error *err = NULL;
111 case GOT_OBJ_TYPE_BLOB:
112 *label = GOT_OBJ_LABEL_BLOB;
114 case GOT_OBJ_TYPE_TREE:
115 *label = GOT_OBJ_LABEL_TREE;
117 case GOT_OBJ_TYPE_COMMIT:
118 *label = GOT_OBJ_LABEL_COMMIT;
120 case GOT_OBJ_TYPE_TAG:
121 *label = GOT_OBJ_LABEL_TAG;
125 err = got_error(GOT_ERR_OBJ_TYPE);
132 static const struct got_error *
133 read_crc(uint32_t *crc, int fd, size_t len)
139 for (n = len; n > 0; n -= r){
140 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
142 return got_error_from_errno("read");
145 *crc = crc32(*crc, buf, r);
151 static const struct got_error *
152 read_file_sha1(SHA1_CTX *ctx, FILE *f, size_t len)
157 for (n = len; n > 0; n -= r) {
158 r = fread(buf, 1, n > sizeof(buf) ? sizeof(buf) : n, f);
162 return got_ferror(f, GOT_ERR_IO);
164 SHA1Update(ctx, buf, r);
170 static const struct got_error *
171 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj,
174 const struct got_error *err = NULL;
176 uint8_t *data = NULL;
181 const char *obj_label;
182 size_t mapoff = obj->off;
184 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
185 &obj->tslen, pack, obj->off);
190 obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
191 mapoff += obj->tslen;
193 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
194 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
195 return got_error_from_errno("lseek");
196 err = read_crc(&obj->crc, pack->fd, obj->tslen);
202 case GOT_OBJ_TYPE_BLOB:
203 case GOT_OBJ_TYPE_COMMIT:
204 case GOT_OBJ_TYPE_TREE:
205 case GOT_OBJ_TYPE_TAG:
206 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
207 if (fseek(tmpfile, 0L, SEEK_SET) == -1) {
208 err = got_error_from_errno("fseek");
212 err = got_inflate_to_file_mmap(&datalen,
213 &obj->len, &obj->crc, pack->map, mapoff,
214 pack->filesize - mapoff, tmpfile);
216 err = got_inflate_to_file_fd(&datalen,
217 &obj->len, &obj->crc, pack->fd, tmpfile);
221 err = got_inflate_to_mem_mmap(&data, &datalen,
222 &obj->len, &obj->crc, pack->map, mapoff,
223 pack->filesize - mapoff);
225 err = got_inflate_to_mem_fd(&data, &datalen,
226 &obj->len, &obj->crc, obj->size, pack->fd);
232 err = get_obj_type_label(&obj_label, obj->type);
237 if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
238 err = got_error_from_errno("asprintf");
242 headerlen = strlen(header) + 1;
243 SHA1Update(&ctx, header, headerlen);
244 if (obj->size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
245 err = read_file_sha1(&ctx, tmpfile, datalen);
249 SHA1Update(&ctx, data, datalen);
250 SHA1Final(obj->id.sha1, &ctx);
254 case GOT_OBJ_TYPE_REF_DELTA:
255 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
257 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
258 err = got_error(GOT_ERR_BAD_PACKFILE);
261 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
263 obj->crc = crc32(obj->crc, pack->map + mapoff,
265 mapoff += SHA1_DIGEST_LENGTH;
266 err = got_inflate_to_mem_mmap(NULL, &datalen,
267 &obj->len, &obj->crc, pack->map, mapoff,
268 pack->filesize - mapoff);
272 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
275 err = got_error_from_errno("read");
278 if (n < sizeof(obj->id)) {
279 err = got_error(GOT_ERR_BAD_PACKFILE);
282 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
284 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
285 &obj->crc, obj->size, pack->fd);
289 obj->len += SHA1_DIGEST_LENGTH;
291 case GOT_OBJ_TYPE_OFFSET_DELTA:
292 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
293 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
294 &obj->delta.ofs.base_offsetlen, pack, obj->off,
300 obj->crc = crc32(obj->crc, pack->map + mapoff,
301 obj->delta.ofs.base_offsetlen);
302 mapoff += obj->delta.ofs.base_offsetlen;
303 err = got_inflate_to_mem_mmap(NULL, &datalen,
304 &obj->len, &obj->crc, pack->map, mapoff,
305 pack->filesize - mapoff);
310 * XXX Seek back and get the CRC of on-disk
313 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
315 err = got_error_from_errno("lseek");
318 err = read_crc(&obj->crc, pack->fd,
319 obj->delta.ofs.base_offsetlen);
323 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
324 &obj->crc, obj->size, pack->fd);
328 obj->len += obj->delta.ofs.base_offsetlen;
331 err = got_error(GOT_ERR_OBJ_TYPE);
338 static const struct got_error *
339 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
343 SHA1Update(ctx, buf, len);
345 w = write(fd, buf, len);
347 return got_error_from_errno("write");
349 return got_error(GOT_ERR_IO);
354 static const struct got_error *
355 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
356 struct got_indexed_object *obj, FILE *tmpfile, FILE *delta_base_file,
357 FILE *delta_accum_file)
359 const struct got_error *err = NULL;
360 struct got_delta_chain deltas;
361 struct got_delta *delta;
369 const char *obj_label;
372 SIMPLEQ_INIT(&deltas.entries);
374 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
375 obj->off, obj->tslen, obj->type, obj->size,
376 GOT_DELTA_CHAIN_RECURSION_MAX);
380 err = got_pack_get_delta_chain_max_size(&max_size, &deltas, pack);
383 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
385 rewind(delta_base_file);
386 rewind(delta_accum_file);
387 err = got_pack_dump_delta_chain_to_file(&len, &deltas,
388 pack, tmpfile, delta_base_file, delta_accum_file);
392 err = got_pack_dump_delta_chain_to_mem(&buf, &len,
398 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
401 err = get_obj_type_label(&obj_label, base_obj_type);
404 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
405 err = got_error_from_errno("asprintf");
408 headerlen = strlen(header) + 1;
410 SHA1Update(&ctx, header, headerlen);
411 if (max_size > GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
412 err = read_file_sha1(&ctx, tmpfile, len);
416 SHA1Update(&ctx, buf, len);
417 SHA1Final(obj->id.sha1, &ctx);
421 while (!SIMPLEQ_EMPTY(&deltas.entries)) {
422 delta = SIMPLEQ_FIRST(&deltas.entries);
423 SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
429 /* Determine the slot in the pack index a given object ID should use. */
431 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
433 u_int8_t id0 = sha1[0];
434 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
435 int left = 0, right = nindexed - 1;
439 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
441 while (left <= right) {
442 struct got_packidx_object_id *oid;
444 i = ((left + right) / 2);
445 oid = &packidx->hdr.sorted_ids[i];
447 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
449 return -1; /* object already indexed */
461 print_packidx(struct got_packidx *packidx)
463 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
466 fprintf(stderr, "object IDs:\n");
467 for (i = 0; i < nindexed; i++) {
468 char hex[SHA1_DIGEST_STRING_LENGTH];
469 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
471 fprintf(stderr, "%s\n", hex);
473 fprintf(stderr, "\n");
475 fprintf(stderr, "object offsets:\n");
476 for (i = 0; i < nindexed; i++) {
477 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
478 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
479 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
480 fprintf(stderr, "%u -> %llu\n", offset,
481 be64toh(packidx->hdr.large_offsets[j]));
483 fprintf(stderr, "%u\n", offset);
485 fprintf(stderr, "\n");
487 fprintf(stderr, "fanout table:");
488 for (i = 0; i <= 0xff; i++)
489 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
490 fprintf(stderr, "\n");
495 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
496 struct got_indexed_object *obj)
500 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
502 packidx->hdr.crc32[idx] = htobe32(obj->crc);
503 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
504 packidx->hdr.offsets[idx] = htobe32(obj->off);
506 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
507 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
508 packidx->hdr.large_offsets[packidx->nlargeobj] =
510 packidx->nlargeobj++;
513 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
514 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
515 packidx->hdr.fanout_table[i] = htobe32(n + 1);
520 indexed_obj_cmp(const void *pa, const void *pb)
522 struct got_indexed_object *a, *b;
524 a = (struct got_indexed_object *)pa;
525 b = (struct got_indexed_object *)pb;
526 return got_object_id_cmp(&a->id, &b->id);
530 make_packidx(struct got_packidx *packidx, int nobj,
531 struct got_indexed_object *objects)
533 struct got_indexed_object *obj;
537 qsort(objects, nobj, sizeof(struct got_indexed_object),
540 memset(packidx->hdr.fanout_table, 0,
541 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
542 packidx->nlargeobj = 0;
544 for (i = 0; i < nobj; i++) {
547 add_indexed_object(packidx, idx++, obj);
552 update_packidx(struct got_packidx *packidx, int nobj,
553 struct got_indexed_object *obj)
556 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
558 idx = find_object_idx(packidx, obj->id.sha1);
560 char hex[SHA1_DIGEST_STRING_LENGTH];
561 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
562 return; /* object already indexed */
565 memmove(&packidx->hdr.sorted_ids[idx + 1],
566 &packidx->hdr.sorted_ids[idx],
567 sizeof(struct got_packidx_object_id) * (nindexed - idx));
568 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
569 sizeof(uint32_t) * (nindexed - idx));
571 add_indexed_object(packidx, idx, obj);
574 static const struct got_error *
575 index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
576 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_hash,
577 struct imsgbuf *ibuf)
579 const struct got_error *err;
580 struct got_packfile_hdr hdr;
581 struct got_packidx packidx;
583 int nobj, nvalid, nloose, nresolved = 0, i;
584 struct got_indexed_object *objects = NULL, *obj;
586 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
588 int pass, have_ref_deltas = 0, first_delta_idx = -1;
590 int p_indexed = 0, last_p_indexed = -1;
591 int p_resolved = 0, last_p_resolved = -1;
593 /* Check pack file header. */
595 if (pack->filesize < sizeof(hdr))
596 return got_error_msg(GOT_ERR_BAD_PACKFILE,
597 "short packfile header");
598 memcpy(&hdr, pack->map, sizeof(hdr));
599 mapoff += sizeof(hdr);
601 r = read(pack->fd, &hdr, sizeof(hdr));
603 return got_error_from_errno("read");
605 return got_error_msg(GOT_ERR_BAD_PACKFILE,
606 "short packfile header");
609 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
610 return got_error_msg(GOT_ERR_BAD_PACKFILE,
611 "bad packfile signature");
612 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
613 return got_error_msg(GOT_ERR_BAD_PACKFILE,
614 "bad packfile version");
615 nobj = betoh32(hdr.nobjects);
617 return got_error_msg(GOT_ERR_BAD_PACKFILE,
618 "bad packfile with zero objects");
621 * Create an in-memory pack index which will grow as objects
622 * IDs in the pack file are discovered. Only fields used to
623 * read deltified objects will be needed by the pack.c library
624 * code, so setting up just a pack index header is sufficient.
626 memset(&packidx, 0, sizeof(packidx));
627 packidx.hdr.magic = malloc(sizeof(uint32_t));
628 if (packidx.hdr.magic == NULL)
629 return got_error_from_errno("calloc");
630 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
631 packidx.hdr.version = malloc(sizeof(uint32_t));
632 if (packidx.hdr.version == NULL) {
633 err = got_error_from_errno("malloc");
636 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
637 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
639 if (packidx.hdr.fanout_table == NULL) {
640 err = got_error_from_errno("calloc");
643 packidx.hdr.sorted_ids = calloc(nobj,
644 sizeof(struct got_packidx_object_id));
645 if (packidx.hdr.sorted_ids == NULL) {
646 err = got_error_from_errno("calloc");
649 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
650 if (packidx.hdr.crc32 == NULL) {
651 err = got_error_from_errno("calloc");
654 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
655 if (packidx.hdr.offsets == NULL) {
656 err = got_error_from_errno("calloc");
659 /* Large offsets table is empty for pack files < 2 GB. */
660 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
661 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
662 if (packidx.hdr.large_offsets == NULL) {
663 err = got_error_from_errno("calloc");
670 objects = calloc(nobj, sizeof(struct got_indexed_object));
672 return got_error_from_errno("calloc");
675 * First pass: locate all objects and identify un-deltified objects.
677 * When this pass has completed we will know offset, type, size, and
678 * CRC information for all objects in this pack file. We won't know
679 * any of the actual object IDs of deltified objects yet since we
680 * will not yet attempt to combine deltas.
683 for (i = 0; i < nobj; i++) {
684 /* Don't send too many progress privsep messages. */
685 p_indexed = ((i + 1) * 100) / nobj;
686 if (p_indexed != last_p_indexed) {
687 err = got_privsep_send_index_pack_progress(ibuf,
688 nobj, i + 1, nloose, 0);
691 last_p_indexed = p_indexed;
695 obj->crc = crc32(0L, NULL, 0);
697 /* Store offset to type+size information for this object. */
701 obj->off = lseek(pack->fd, 0, SEEK_CUR);
702 if (obj->off == -1) {
703 err = got_error_from_errno("lseek");
708 err = read_packed_object(pack, obj, tmpfile);
713 mapoff += obj->tslen + obj->len;
715 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
717 err = got_error_from_errno("lseek");
722 if (obj->type == GOT_OBJ_TYPE_BLOB ||
723 obj->type == GOT_OBJ_TYPE_TREE ||
724 obj->type == GOT_OBJ_TYPE_COMMIT ||
725 obj->type == GOT_OBJ_TYPE_TAG) {
729 if (first_delta_idx == -1)
731 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
737 if (first_delta_idx == -1)
740 /* In order to resolve ref deltas we need an in-progress pack index. */
742 make_packidx(&packidx, nobj, objects);
745 * Second pass: We can now resolve deltas to compute the IDs of
746 * objects which appear in deltified form. Because deltas can be
747 * chained this pass may require a couple of iterations until all
748 * IDs of deltified objects have been discovered.
751 while (nvalid != nobj) {
754 * This loop will only run once unless the pack file
755 * contains ref deltas which refer to objects located
756 * later in the pack file, which is unusual.
757 * Offset deltas can always be resolved in one pass
758 * unless the packfile is corrupt.
760 for (i = first_delta_idx; i < nobj; i++) {
762 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
763 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
769 if (pack->map == NULL && lseek(pack->fd,
770 obj->off + obj->tslen, SEEK_SET) == -1) {
771 err = got_error_from_errno("lseek");
775 err = resolve_deltified_object(pack, &packidx, obj,
776 tmpfile, delta_base_file, delta_accum_file);
778 if (err->code != GOT_ERR_NO_OBJ)
781 * We cannot resolve this object yet because
782 * a delta base is unknown. Try again later.
790 update_packidx(&packidx, nobj, obj);
791 /* Don't send too many progress privsep messages. */
792 p_resolved = ((nresolved + n) * 100) / nobj;
793 if (p_resolved != last_p_resolved) {
794 err = got_privsep_send_index_pack_progress(ibuf,
795 nobj, nobj, nloose, nresolved + n);
798 last_p_resolved = p_resolved;
802 if (pass++ > 3 && n == 0) {
804 snprintf(msg, sizeof(msg), "could not resolve "
805 "any of deltas; packfile could be corrupt");
806 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
814 if (nloose + nresolved != nobj) {
816 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
817 nloose + nresolved, nobj);
818 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
822 err = got_privsep_send_index_pack_progress(ibuf, nobj, nobj,
827 make_packidx(&packidx, nobj, objects);
833 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
834 putbe32(buf + 4, GOT_PACKIDX_VERSION);
835 err = hwrite(idxfd, buf, 8, &ctx);
838 err = hwrite(idxfd, packidx.hdr.fanout_table,
839 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
842 err = hwrite(idxfd, packidx.hdr.sorted_ids,
843 nobj * SHA1_DIGEST_LENGTH, &ctx);
846 err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
849 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
853 if (packidx.nlargeobj > 0) {
854 err = hwrite(idxfd, packidx.hdr.large_offsets,
855 packidx.nlargeobj * sizeof(uint64_t), &ctx);
859 err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
863 SHA1Final(packidx_hash, &ctx);
864 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
866 err = got_error_from_errno("write");
869 if (w != sizeof(packidx_hash)) {
870 err = got_error(GOT_ERR_IO);
875 free(packidx.hdr.magic);
876 free(packidx.hdr.version);
877 free(packidx.hdr.fanout_table);
878 free(packidx.hdr.sorted_ids);
879 free(packidx.hdr.offsets);
880 free(packidx.hdr.large_offsets);
885 main(int argc, char **argv)
887 const struct got_error *err = NULL, *close_err;
890 int idxfd = -1, tmpfd = -1, i;
892 struct got_pack pack;
893 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
901 for (i = 0; i < nitems(tmpfiles); i++)
904 memset(&pack, 0, sizeof(pack));
906 pack.delta_cache = got_delta_cache_alloc(500,
907 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
908 if (pack.delta_cache == NULL) {
909 err = got_error_from_errno("got_delta_cache_alloc");
913 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
915 /* revoke access to most system calls */
916 if (pledge("stdio recvfd", NULL) == -1) {
917 err = got_error_from_errno("pledge");
918 got_privsep_send_error(&ibuf, err);
922 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
925 if (imsg.hdr.type == GOT_IMSG_STOP)
927 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
928 err = got_error(GOT_ERR_PRIVSEP_MSG);
931 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
932 err = got_error(GOT_ERR_PRIVSEP_LEN);
935 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
938 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
941 if (imsg.hdr.type == GOT_IMSG_STOP)
943 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
944 err = got_error(GOT_ERR_PRIVSEP_MSG);
947 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
948 err = got_error(GOT_ERR_PRIVSEP_LEN);
953 for (i = 0; i < nitems(tmpfiles); i++) {
954 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
957 if (imsg.hdr.type == GOT_IMSG_STOP)
959 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
960 err = got_error(GOT_ERR_PRIVSEP_MSG);
963 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
964 err = got_error(GOT_ERR_PRIVSEP_LEN);
968 tmpfiles[i] = fdopen(tmpfd, "w+");
969 if (tmpfiles[i] == NULL) {
970 err = got_error_from_errno("fdopen");
976 if (lseek(pack.fd, 0, SEEK_END) == -1) {
977 err = got_error_from_errno("lseek");
980 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
981 if (packfile_size == -1) {
982 err = got_error_from_errno("lseek");
985 pack.filesize = packfile_size; /* XXX off_t vs size_t */
987 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
988 err = got_error_from_errno("lseek");
992 #ifndef GOT_PACK_NO_MMAP
993 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
995 if (pack.map == MAP_FAILED)
996 pack.map = NULL; /* fall back to read(2) */
998 err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
1001 close_err = got_pack_close(&pack);
1002 if (close_err && err == NULL)
1004 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1005 err = got_error_from_errno("close");
1006 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
1007 err = got_error_from_errno("close");
1008 for (i = 0; i < nitems(tmpfiles); i++) {
1009 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
1011 err = got_error_from_errno("close");
1015 err = got_privsep_send_index_pack_done(&ibuf);
1017 got_privsep_send_error(&ibuf, err);
1018 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1019 got_privsep_send_error(&ibuf, err);