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"
55 struct got_indexed_object {
56 struct got_object_id id;
59 * Has this object been fully resolved?
60 * If so, we know its ID, otherwise we don't and 'id' is invalid.
64 /* Offset of type+size field for this object in pack file. */
67 /* Type+size values parsed from pack file. */
71 /* Length of on-disk type+size data. */
74 /* Length of object data following type+size. */
82 struct got_object_id ref_id;
85 /* For offset deltas. */
87 size_t base_offsetlen;
93 putbe32(char *b, uint32_t n)
101 static const struct got_error *
102 get_obj_type_label(const char **label, int obj_type)
104 const struct got_error *err = NULL;
107 case GOT_OBJ_TYPE_BLOB:
108 *label = GOT_OBJ_LABEL_BLOB;
110 case GOT_OBJ_TYPE_TREE:
111 *label = GOT_OBJ_LABEL_TREE;
113 case GOT_OBJ_TYPE_COMMIT:
114 *label = GOT_OBJ_LABEL_COMMIT;
116 case GOT_OBJ_TYPE_TAG:
117 *label = GOT_OBJ_LABEL_TAG;
121 err = got_error(GOT_ERR_OBJ_TYPE);
128 static const struct got_error *
129 read_crc(uint32_t *crc, int fd, size_t len)
135 for (n = len; n > 0; n -= r){
136 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
138 return got_error_from_errno("read");
141 *crc = crc32(*crc, buf, r);
147 static const struct got_error *
148 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
150 const struct got_error *err = NULL;
157 const char *obj_label;
158 size_t mapoff = obj->off;
160 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
161 &obj->tslen, pack, obj->off);
166 obj->crc = crc32(obj->crc, pack->map + mapoff, obj->tslen);
167 mapoff += obj->tslen;
169 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
170 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
171 return got_error_from_errno("lseek");
172 err = read_crc(&obj->crc, pack->fd, obj->tslen);
178 case GOT_OBJ_TYPE_BLOB:
179 case GOT_OBJ_TYPE_COMMIT:
180 case GOT_OBJ_TYPE_TREE:
181 case GOT_OBJ_TYPE_TAG:
182 /* XXX TODO reading large objects into memory is bad! */
184 err = got_inflate_to_mem_mmap(&data, &datalen,
185 &obj->len, &obj->crc, pack->map, mapoff,
186 pack->filesize - mapoff);
188 err = got_inflate_to_mem_fd(&data, &datalen,
189 &obj->len, &obj->crc, obj->size, pack->fd);
194 err = get_obj_type_label(&obj_label, obj->type);
199 if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
200 err = got_error_from_errno("asprintf");
204 headerlen = strlen(header) + 1;
205 SHA1Update(&ctx, header, headerlen);
206 SHA1Update(&ctx, data, datalen);
207 SHA1Final(obj->id.sha1, &ctx);
211 case GOT_OBJ_TYPE_REF_DELTA:
212 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
214 if (mapoff + SHA1_DIGEST_LENGTH >= pack->filesize) {
215 err = got_error(GOT_ERR_BAD_PACKFILE);
218 memcpy(obj->delta.ref.ref_id.sha1, pack->map + mapoff,
220 obj->crc = crc32(obj->crc, pack->map + mapoff,
222 mapoff += SHA1_DIGEST_LENGTH;
223 err = got_inflate_to_mem_mmap(NULL, &datalen,
224 &obj->len, &obj->crc, pack->map, mapoff,
225 pack->filesize - mapoff);
229 n = read(pack->fd, obj->delta.ref.ref_id.sha1,
232 err = got_error_from_errno("read");
235 if (n < sizeof(obj->id)) {
236 err = got_error(GOT_ERR_BAD_PACKFILE);
239 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
241 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
242 &obj->crc, obj->size, pack->fd);
246 obj->len += SHA1_DIGEST_LENGTH;
248 case GOT_OBJ_TYPE_OFFSET_DELTA:
249 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
250 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
251 &obj->delta.ofs.base_offsetlen, pack, obj->off,
257 obj->crc = crc32(obj->crc, pack->map + mapoff,
258 obj->delta.ofs.base_offsetlen);
259 mapoff += obj->delta.ofs.base_offsetlen;
260 err = got_inflate_to_mem_mmap(NULL, &datalen,
261 &obj->len, &obj->crc, pack->map, mapoff,
262 pack->filesize - mapoff);
267 * XXX Seek back and get the CRC of on-disk
270 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
272 err = got_error_from_errno("lseek");
275 err = read_crc(&obj->crc, pack->fd,
276 obj->delta.ofs.base_offsetlen);
280 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
281 &obj->crc, obj->size, pack->fd);
285 obj->len += obj->delta.ofs.base_offsetlen;
288 err = got_error(GOT_ERR_OBJ_TYPE);
295 static const struct got_error *
296 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
300 SHA1Update(ctx, buf, len);
302 w = write(fd, buf, len);
304 return got_error_from_errno("write");
306 return got_error(GOT_ERR_IO);
311 static const struct got_error *
312 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
313 struct got_indexed_object *obj)
315 const struct got_error *err = NULL;
316 struct got_delta_chain deltas;
317 struct got_delta *delta;
324 const char *obj_label;
327 SIMPLEQ_INIT(&deltas.entries);
329 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
330 obj->off, obj->tslen, obj->type, obj->size,
331 GOT_DELTA_CHAIN_RECURSION_MAX);
335 /* XXX TODO reading large objects into memory is bad! */
336 err = got_pack_dump_delta_chain_to_mem(&buf, &len, &deltas, pack);
342 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
345 err = get_obj_type_label(&obj_label, base_obj_type);
348 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
349 err = got_error_from_errno("asprintf");
352 headerlen = strlen(header) + 1;
353 SHA1Update(&ctx, header, headerlen);
354 SHA1Update(&ctx, buf, len);
355 SHA1Final(obj->id.sha1, &ctx);
359 while (!SIMPLEQ_EMPTY(&deltas.entries)) {
360 delta = SIMPLEQ_FIRST(&deltas.entries);
361 SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
367 /* Determine the slot in the pack index a given object ID should use. */
369 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
371 u_int8_t id0 = sha1[0];
372 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
373 int left = 0, right = nindexed - 1;
377 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
379 while (left <= right) {
380 struct got_packidx_object_id *oid;
382 i = ((left + right) / 2);
383 oid = &packidx->hdr.sorted_ids[i];
385 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
387 return -1; /* object already indexed */
399 print_packidx(struct got_packidx *packidx)
401 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
404 fprintf(stderr, "object IDs:\n");
405 for (i = 0; i < nindexed; i++) {
406 char hex[SHA1_DIGEST_STRING_LENGTH];
407 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
409 fprintf(stderr, "%s\n", hex);
411 fprintf(stderr, "\n");
413 fprintf(stderr, "object offsets:\n");
414 for (i = 0; i < nindexed; i++) {
415 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
416 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
417 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
418 fprintf(stderr, "%u -> %llu\n", offset,
419 be64toh(packidx->hdr.large_offsets[j]));
421 fprintf(stderr, "%u\n", offset);
423 fprintf(stderr, "\n");
425 fprintf(stderr, "fanout table:");
426 for (i = 0; i <= 0xff; i++)
427 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
428 fprintf(stderr, "\n");
433 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
434 struct got_indexed_object *obj)
438 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
440 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
441 packidx->hdr.offsets[idx] = htobe32(obj->off);
443 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
444 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
445 packidx->hdr.large_offsets[packidx->nlargeobj] =
447 packidx->nlargeobj++;
450 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
451 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
452 packidx->hdr.fanout_table[i] = htobe32(n + 1);
457 indexed_obj_cmp(const void *pa, const void *pb)
459 struct got_indexed_object *a, *b;
461 a = (struct got_indexed_object *)pa;
462 b = (struct got_indexed_object *)pb;
463 return got_object_id_cmp(&a->id, &b->id);
467 make_packidx(struct got_packidx *packidx, int nobj,
468 struct got_indexed_object *objects)
470 struct got_indexed_object *obj;
474 mergesort(objects, nobj, sizeof(struct got_indexed_object),
477 memset(packidx->hdr.fanout_table, 0,
478 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
479 packidx->nlargeobj = 0;
481 for (i = 0; i < nobj; i++) {
484 add_indexed_object(packidx, idx++, obj);
489 update_packidx(struct got_packidx *packidx, int nobj,
490 struct got_indexed_object *obj)
493 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
495 idx = find_object_idx(packidx, obj->id.sha1);
497 char hex[SHA1_DIGEST_STRING_LENGTH];
498 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
499 return; /* object already indexed */
502 memmove(&packidx->hdr.sorted_ids[idx + 1],
503 &packidx->hdr.sorted_ids[idx],
504 sizeof(struct got_packidx_object_id) * (nindexed - idx));
505 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
506 sizeof(uint32_t) * (nindexed - idx));
508 add_indexed_object(packidx, idx, obj);
511 static const struct got_error *
512 index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
513 struct imsgbuf *ibuf)
515 const struct got_error *err;
516 struct got_packfile_hdr hdr;
517 struct got_packidx packidx;
519 int nobj, nvalid, nloose, nresolved = 0, i;
520 struct got_indexed_object *objects = NULL, *obj;
522 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
524 int pass, have_ref_deltas = 0;
527 /* Check pack file header. */
529 if (pack->filesize < sizeof(hdr))
530 return got_error_msg(GOT_ERR_BAD_PACKFILE,
531 "short packfile header");
532 memcpy(&hdr, pack->map, sizeof(hdr));
533 mapoff += sizeof(hdr);
535 r = read(pack->fd, &hdr, sizeof(hdr));
537 return got_error_from_errno("read");
539 return got_error_msg(GOT_ERR_BAD_PACKFILE,
540 "short packfile header");
543 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
544 return got_error_msg(GOT_ERR_BAD_PACKFILE,
545 "bad packfile signature");
546 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
547 return got_error_msg(GOT_ERR_BAD_PACKFILE,
548 "bad packfile version");
549 nobj = betoh32(hdr.nobjects);
551 return got_error_msg(GOT_ERR_BAD_PACKFILE,
552 "bad packfile with zero objects");
555 * Create an in-memory pack index which will grow as objects
556 * IDs in the pack file are discovered. Only fields used to
557 * read deltified objects will be needed by the pack.c library
558 * code, so setting up just a pack index header is sufficient.
560 memset(&packidx, 0, sizeof(packidx));
561 packidx.hdr.magic = malloc(sizeof(uint32_t));
562 if (packidx.hdr.magic == NULL)
563 return got_error_from_errno("calloc");
564 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
565 packidx.hdr.version = malloc(sizeof(uint32_t));
566 if (packidx.hdr.version == NULL) {
567 err = got_error_from_errno("malloc");
570 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
571 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
573 if (packidx.hdr.fanout_table == NULL) {
574 err = got_error_from_errno("calloc");
577 packidx.hdr.sorted_ids = calloc(nobj,
578 sizeof(struct got_packidx_object_id));
579 if (packidx.hdr.sorted_ids == NULL) {
580 err = got_error_from_errno("calloc");
583 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
584 if (packidx.hdr.offsets == NULL) {
585 err = got_error_from_errno("calloc");
588 /* Large offsets table is empty for pack files < 2 GB. */
589 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
590 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
591 if (packidx.hdr.large_offsets == NULL) {
592 err = got_error_from_errno("calloc");
599 objects = calloc(nobj, sizeof(struct got_indexed_object));
601 return got_error_from_errno("calloc");
604 * First pass: locate all objects and identify un-deltified objects.
606 * When this pass has completed we will know offset, type, size, and
607 * CRC information for all objects in this pack file. We won't know
608 * any of the actual object IDs of deltified objects yet since we
609 * will not yet attempt to combine deltas.
612 for (i = 0; i < nobj; i++) {
613 err = got_privsep_send_index_pack_progress(ibuf, nobj, i + 1,
619 obj->crc = crc32(0L, NULL, 0);
621 /* Store offset to type+size information for this object. */
625 obj->off = lseek(pack->fd, 0, SEEK_CUR);
626 if (obj->off == -1) {
627 err = got_error_from_errno("lseek");
632 err = read_packed_object(pack, obj);
637 mapoff += obj->tslen + obj->len;
639 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
641 err = got_error_from_errno("lseek");
646 if (obj->type == GOT_OBJ_TYPE_BLOB ||
647 obj->type == GOT_OBJ_TYPE_TREE ||
648 obj->type == GOT_OBJ_TYPE_COMMIT ||
649 obj->type == GOT_OBJ_TYPE_TAG) {
652 } else if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
657 /* In order to resolve ref deltas we need an in-progress pack index. */
659 make_packidx(&packidx, nobj, objects);
662 * Second pass: We can now resolve deltas to compute the IDs of
663 * objects which appear in deltified form. Because deltas can be
664 * chained this pass may require a couple of iterations until all
665 * IDs of deltified objects have been discovered.
668 while (nvalid != nobj) {
670 for (i = 0; i < nobj; i++) {
672 if (obj->type != GOT_OBJ_TYPE_REF_DELTA &&
673 obj->type != GOT_OBJ_TYPE_OFFSET_DELTA)
679 if (pack->map == NULL &&
680 lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
682 err = got_error_from_errno("lseek");
686 err = resolve_deltified_object(pack, &packidx, obj);
688 if (err->code != GOT_ERR_NO_OBJ)
691 * We cannot resolve this object yet because
692 * a delta base is unknown. Try again later.
700 update_packidx(&packidx, nobj, obj);
701 err = got_privsep_send_index_pack_progress(ibuf,
702 nobj, nobj, nloose, nresolved + n);
707 if (pass++ > 3 && n == 0) {
709 snprintf(msg, sizeof(msg), "could not resolve "
710 "any of deltas; packfile could be corrupt");
711 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
715 if (nloose + nresolved == nobj) {
717 snprintf(msg, sizeof(msg),
718 "fix point reached too early: %d/%d/%d",
719 nvalid, nresolved, nobj);
720 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
727 if (nloose + nresolved != nobj) {
729 snprintf(msg, sizeof(msg),
730 "discovered only %d of %d objects",
731 nloose + nresolved, nobj);
732 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
736 make_packidx(&packidx, nobj, objects);
739 putbe32(buf, GOT_PACKIDX_V2_MAGIC);
740 putbe32(buf + 4, GOT_PACKIDX_VERSION);
741 err = hwrite(idxfd, buf, 8, &ctx);
744 err = hwrite(idxfd, packidx.hdr.fanout_table,
745 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
748 err = hwrite(idxfd, packidx.hdr.sorted_ids,
749 nobj * SHA1_DIGEST_LENGTH, &ctx);
752 for(i = 0; i < nobj; i++){
753 putbe32(buf, objects[i].crc);
754 err = hwrite(idxfd, buf, 4, &ctx);
758 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
762 if (packidx.nlargeobj > 0) {
763 err = hwrite(idxfd, packidx.hdr.large_offsets,
764 packidx.nlargeobj * sizeof(uint64_t), &ctx);
768 err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
772 SHA1Final(packidx_hash, &ctx);
773 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
775 err = got_error_from_errno("write");
778 if (w != sizeof(packidx_hash)) {
779 err = got_error(GOT_ERR_IO);
784 free(packidx.hdr.magic);
785 free(packidx.hdr.version);
786 free(packidx.hdr.fanout_table);
787 free(packidx.hdr.sorted_ids);
788 free(packidx.hdr.offsets);
789 free(packidx.hdr.large_offsets);
794 main(int argc, char **argv)
796 const struct got_error *err = NULL, *close_err;
800 struct got_pack pack;
801 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
809 memset(&pack, 0, sizeof(pack));
811 pack.delta_cache = got_delta_cache_alloc(100,
812 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
813 if (pack.delta_cache == NULL) {
814 err = got_error_from_errno("got_delta_cache_alloc");
818 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
820 /* revoke access to most system calls */
821 if (pledge("stdio recvfd", NULL) == -1) {
822 err = got_error_from_errno("pledge");
823 got_privsep_send_error(&ibuf, err);
827 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
830 if (imsg.hdr.type == GOT_IMSG_STOP)
832 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
833 err = got_error(GOT_ERR_PRIVSEP_MSG);
836 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
837 err = got_error(GOT_ERR_PRIVSEP_LEN);
840 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
843 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
846 if (imsg.hdr.type == GOT_IMSG_STOP)
848 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
849 err = got_error(GOT_ERR_PRIVSEP_MSG);
852 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
853 err = got_error(GOT_ERR_PRIVSEP_LEN);
858 if (lseek(pack.fd, 0, SEEK_END) == -1) {
859 err = got_error_from_errno("lseek");
862 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
863 if (packfile_size == -1) {
864 err = got_error_from_errno("lseek");
867 pack.filesize = packfile_size; /* XXX off_t vs size_t */
869 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
870 err = got_error_from_errno("lseek");
874 #ifndef GOT_PACK_NO_MMAP
875 pack.map = mmap(NULL, pack.filesize, PROT_READ, MAP_PRIVATE,
877 if (pack.map == MAP_FAILED)
878 pack.map = NULL; /* fall back to read(2) */
880 err = index_pack(&pack, idxfd, pack_hash, &ibuf);
882 close_err = got_pack_close(&pack);
883 if (close_err && err == NULL)
885 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
886 err = got_error_from_errno("close");
889 err = got_privsep_send_index_pack_done(&ibuf);
891 got_privsep_send_error(&ibuf, err);
892 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
893 got_privsep_send_error(&ibuf, err);