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 send_index_pack_progress(struct imsgbuf *ibuf, int nobj_total,
576 int nobj_indexed, int nobj_loose, int nobj_resolved)
578 struct got_imsg_index_pack_progress iprogress;
580 iprogress.nobj_total = nobj_total;
581 iprogress.nobj_indexed = nobj_indexed;
582 iprogress.nobj_loose = nobj_loose;
583 iprogress.nobj_resolved = nobj_resolved;
585 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_PROGRESS, 0, 0, -1,
586 &iprogress, sizeof(iprogress)) == -1)
587 return got_error_from_errno("imsg_compose IDXPACK_PROGRESS");
589 return got_privsep_flush_imsg(ibuf);
592 static const struct got_error *
593 send_index_pack_done(struct imsgbuf *ibuf)
595 if (imsg_compose(ibuf, GOT_IMSG_IDXPACK_DONE, 0, 0, -1, NULL, 0) == -1)
596 return got_error_from_errno("imsg_compose FETCH");
597 return got_privsep_flush_imsg(ibuf);
601 static const struct got_error *
602 index_pack(struct got_pack *pack, int idxfd, FILE *tmpfile,
603 FILE *delta_base_file, FILE *delta_accum_file, uint8_t *pack_hash,
604 struct imsgbuf *ibuf)
606 const struct got_error *err;
607 struct got_packfile_hdr hdr;
608 struct got_packidx packidx;
610 int nobj, nvalid, nloose, nresolved = 0, i;
611 struct got_indexed_object *objects = NULL, *obj;
613 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
615 int pass, have_ref_deltas = 0, first_delta_idx = -1;
617 int p_indexed = 0, last_p_indexed = -1;
618 int p_resolved = 0, last_p_resolved = -1;
620 /* Check pack file header. */
622 if (pack->filesize < sizeof(hdr))
623 return got_error_msg(GOT_ERR_BAD_PACKFILE,
624 "short packfile header");
625 memcpy(&hdr, pack->map, sizeof(hdr));
626 mapoff += sizeof(hdr);
628 r = read(pack->fd, &hdr, sizeof(hdr));
630 return got_error_from_errno("read");
632 return got_error_msg(GOT_ERR_BAD_PACKFILE,
633 "short packfile header");
636 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
637 return got_error_msg(GOT_ERR_BAD_PACKFILE,
638 "bad packfile signature");
639 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
640 return got_error_msg(GOT_ERR_BAD_PACKFILE,
641 "bad packfile version");
642 nobj = betoh32(hdr.nobjects);
644 return got_error_msg(GOT_ERR_BAD_PACKFILE,
645 "bad packfile with zero objects");
648 * Create an in-memory pack index which will grow as objects
649 * IDs in the pack file are discovered. Only fields used to
650 * read deltified objects will be needed by the pack.c library
651 * code, so setting up just a pack index header is sufficient.
653 memset(&packidx, 0, sizeof(packidx));
654 packidx.hdr.magic = malloc(sizeof(uint32_t));
655 if (packidx.hdr.magic == NULL)
656 return got_error_from_errno("calloc");
657 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
658 packidx.hdr.version = malloc(sizeof(uint32_t));
659 if (packidx.hdr.version == NULL) {
660 err = got_error_from_errno("malloc");
663 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
664 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
666 if (packidx.hdr.fanout_table == NULL) {
667 err = got_error_from_errno("calloc");
670 packidx.hdr.sorted_ids = calloc(nobj,
671 sizeof(struct got_packidx_object_id));
672 if (packidx.hdr.sorted_ids == NULL) {
673 err = got_error_from_errno("calloc");
676 packidx.hdr.crc32 = calloc(nobj, sizeof(uint32_t));
677 if (packidx.hdr.crc32 == NULL) {
678 err = got_error_from_errno("calloc");
681 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
682 if (packidx.hdr.offsets == NULL) {
683 err = got_error_from_errno("calloc");
686 /* Large offsets table is empty for pack files < 2 GB. */
687 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
688 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
689 if (packidx.hdr.large_offsets == NULL) {
690 err = got_error_from_errno("calloc");
697 objects = calloc(nobj, sizeof(struct got_indexed_object));
699 return got_error_from_errno("calloc");
702 * First pass: locate all objects and identify un-deltified objects.
704 * When this pass has completed we will know offset, type, size, and
705 * CRC information for all objects in this pack file. We won't know
706 * any of the actual object IDs of deltified objects yet since we
707 * will not yet attempt to combine deltas.
710 for (i = 0; i < nobj; i++) {
711 /* Don't send too many progress privsep messages. */
712 p_indexed = ((i + 1) * 100) / nobj;
713 if (p_indexed != last_p_indexed) {
714 err = send_index_pack_progress(ibuf, nobj, i + 1,
718 last_p_indexed = p_indexed;
722 obj->crc = crc32(0L, NULL, 0);
724 /* Store offset to type+size information for this object. */
728 obj->off = lseek(pack->fd, 0, SEEK_CUR);
729 if (obj->off == -1) {
730 err = got_error_from_errno("lseek");
735 err = read_packed_object(pack, obj, tmpfile);
740 mapoff += obj->tslen + obj->len;
742 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
744 err = got_error_from_errno("lseek");
749 if (obj->type == GOT_OBJ_TYPE_BLOB ||
750 obj->type == GOT_OBJ_TYPE_TREE ||
751 obj->type == GOT_OBJ_TYPE_COMMIT ||
752 obj->type == GOT_OBJ_TYPE_TAG) {
756 if (first_delta_idx == -1)
758 if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
764 if (first_delta_idx == -1)
767 /* In order to resolve ref deltas we need an in-progress pack index. */
769 make_packidx(&packidx, nobj, objects);
772 * Second pass: We can now resolve deltas to compute the IDs of
773 * objects which appear in deltified form. Because deltas can be
774 * chained this pass may require a couple of iterations until all
775 * IDs of deltified objects have been discovered.
778 while (nvalid != nobj) {
781 * This loop will only run once unless the pack file
782 * contains ref deltas which refer to objects located
783 * later in the pack file, which is unusual.
784 * Offset deltas can always be resolved in one pass
785 * unless the packfile is corrupt.
787 for (i = first_delta_idx; i < nobj; i++) {
789 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
790 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
796 if (pack->map == NULL && lseek(pack->fd,
797 obj->off + obj->tslen, SEEK_SET) == -1) {
798 err = got_error_from_errno("lseek");
802 err = resolve_deltified_object(pack, &packidx, obj,
803 tmpfile, delta_base_file, delta_accum_file);
805 if (err->code != GOT_ERR_NO_OBJ)
808 * We cannot resolve this object yet because
809 * a delta base is unknown. Try again later.
817 update_packidx(&packidx, nobj, obj);
818 /* Don't send too many progress privsep messages. */
819 p_resolved = ((nresolved + n) * 100) / nobj;
820 if (p_resolved != last_p_resolved) {
821 err = send_index_pack_progress(ibuf, nobj,
822 nobj, nloose, nresolved + n);
825 last_p_resolved = p_resolved;
829 if (pass++ > 3 && n == 0) {
831 snprintf(msg, sizeof(msg), "could not resolve "
832 "any of deltas; packfile could be corrupt");
833 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
841 if (nloose + nresolved != nobj) {
843 snprintf(msg, sizeof(msg), "discovered only %d of %d objects",
844 nloose + nresolved, nobj);
845 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
849 err = send_index_pack_progress(ibuf, nobj, nobj, nloose, nresolved);
853 make_packidx(&packidx, nobj, objects);
859 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
860 putbe32(buf + 4, GOT_PACKIDX_VERSION);
861 err = hwrite(idxfd, buf, 8, &ctx);
864 err = hwrite(idxfd, packidx.hdr.fanout_table,
865 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
868 err = hwrite(idxfd, packidx.hdr.sorted_ids,
869 nobj * SHA1_DIGEST_LENGTH, &ctx);
872 err = hwrite(idxfd, packidx.hdr.crc32, nobj * sizeof(uint32_t), &ctx);
875 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
879 if (packidx.nlargeobj > 0) {
880 err = hwrite(idxfd, packidx.hdr.large_offsets,
881 packidx.nlargeobj * sizeof(uint64_t), &ctx);
885 err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
889 SHA1Final(packidx_hash, &ctx);
890 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
892 err = got_error_from_errno("write");
895 if (w != sizeof(packidx_hash)) {
896 err = got_error(GOT_ERR_IO);
901 free(packidx.hdr.magic);
902 free(packidx.hdr.version);
903 free(packidx.hdr.fanout_table);
904 free(packidx.hdr.sorted_ids);
905 free(packidx.hdr.offsets);
906 free(packidx.hdr.large_offsets);
911 main(int argc, char **argv)
913 const struct got_error *err = NULL, *close_err;
916 int idxfd = -1, tmpfd = -1, i;
918 struct got_pack pack;
919 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
927 for (i = 0; i < nitems(tmpfiles); i++)
930 memset(&pack, 0, sizeof(pack));
932 pack.delta_cache = got_delta_cache_alloc(500,
933 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
934 if (pack.delta_cache == NULL) {
935 err = got_error_from_errno("got_delta_cache_alloc");
939 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
941 /* revoke access to most system calls */
942 if (pledge("stdio recvfd", NULL) == -1) {
943 err = got_error_from_errno("pledge");
944 got_privsep_send_error(&ibuf, err);
948 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
951 if (imsg.hdr.type == GOT_IMSG_STOP)
953 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
954 err = got_error(GOT_ERR_PRIVSEP_MSG);
957 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
958 err = got_error(GOT_ERR_PRIVSEP_LEN);
961 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
964 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
967 if (imsg.hdr.type == GOT_IMSG_STOP)
969 if (imsg.hdr.type != GOT_IMSG_IDXPACK_OUTFD) {
970 err = got_error(GOT_ERR_PRIVSEP_MSG);
973 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
974 err = got_error(GOT_ERR_PRIVSEP_LEN);
979 for (i = 0; i < nitems(tmpfiles); i++) {
980 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
983 if (imsg.hdr.type == GOT_IMSG_STOP)
985 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
986 err = got_error(GOT_ERR_PRIVSEP_MSG);
989 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
990 err = got_error(GOT_ERR_PRIVSEP_LEN);
994 tmpfiles[i] = fdopen(tmpfd, "w+");
995 if (tmpfiles[i] == NULL) {
996 err = got_error_from_errno("fdopen");
1002 if (lseek(pack.fd, 0, SEEK_END) == -1) {
1003 err = got_error_from_errno("lseek");
1006 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
1007 if (packfile_size == -1) {
1008 err = got_error_from_errno("lseek");
1011 pack.filesize = packfile_size; /* XXX off_t vs size_t */
1013 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
1014 err = got_error_from_errno("lseek");
1018 #ifndef GOT_PACK_NO_MMAP
1019 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
1021 if (pack.map == MAP_FAILED)
1022 pack.map = NULL; /* fall back to read(2) */
1024 err = index_pack(&pack, idxfd, tmpfiles[0], tmpfiles[1], tmpfiles[2],
1027 close_err = got_pack_close(&pack);
1028 if (close_err && err == NULL)
1030 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
1031 err = got_error_from_errno("close");
1032 if (tmpfd != -1 && close(tmpfd) == -1 && err == NULL)
1033 err = got_error_from_errno("close");
1034 for (i = 0; i < nitems(tmpfiles); i++) {
1035 if (tmpfiles[i] != NULL && fclose(tmpfiles[i]) == EOF &&
1037 err = got_error_from_errno("close");
1041 err = send_index_pack_done(&ibuf);
1043 got_privsep_send_error(&ibuf, err);
1044 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1045 got_privsep_send_error(&ibuf, err);