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>
41 #include "got_error.h"
42 #include "got_object.h"
44 #include "got_lib_sha1.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_inflate.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_parse.h"
49 #include "got_lib_object_idset.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_delta_cache.h"
54 struct got_indexed_object {
55 struct got_object_id id;
58 * Has this object been fully resolved?
59 * If so, we know its ID, otherwise we don't and 'id' is invalid.
63 /* Offset of type+size field for this object in pack file. */
66 /* Type+size values parsed from pack file. */
70 /* Length of on-disk type+size data. */
73 /* Length of object data following type+size. */
81 struct got_object_id ref_id;
84 /* For offset deltas. */
86 size_t base_offsetlen;
91 #define PUTBE32(b, n)\
99 #define PUTBE64(b, n)\
101 (b)[0] = (n) >> 56; \
102 (b)[1] = (n) >> 48; \
103 (b)[2] = (n) >> 40; \
104 (b)[3] = (n) >> 32; \
105 (b)[4] = (n) >> 24; \
106 (b)[5] = (n) >> 16; \
111 static const struct got_error *
112 get_obj_type_label(const char **label, int obj_type)
114 const struct got_error *err = NULL;
117 case GOT_OBJ_TYPE_BLOB:
118 *label = GOT_OBJ_LABEL_BLOB;
120 case GOT_OBJ_TYPE_TREE:
121 *label = GOT_OBJ_LABEL_TREE;
123 case GOT_OBJ_TYPE_COMMIT:
124 *label = GOT_OBJ_LABEL_COMMIT;
126 case GOT_OBJ_TYPE_TAG:
127 *label = GOT_OBJ_LABEL_TAG;
131 err = got_error(GOT_ERR_OBJ_TYPE);
138 static const struct got_error *
139 read_crc(uint32_t *crc, int fd, size_t len)
145 for (n = len; n > 0; n -= r){
146 r = read(fd, buf, n > sizeof(buf) ? sizeof(buf) : n);
148 return got_error_from_errno("read");
151 *crc = crc32(*crc, buf, r);
157 static const struct got_error *
158 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
160 const struct got_error *err = NULL;
167 const char *obj_label;
169 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size,
170 &obj->tslen, pack, obj->off);
174 /* XXX Seek back and get the CRC of on-disk type+size bytes. */
175 if (lseek(pack->fd, obj->off, SEEK_SET) == -1)
176 return got_error_from_errno("lseek");
177 err = read_crc(&obj->crc, pack->fd, obj->tslen);
182 case GOT_OBJ_TYPE_BLOB:
183 case GOT_OBJ_TYPE_COMMIT:
184 case GOT_OBJ_TYPE_TREE:
185 case GOT_OBJ_TYPE_TAG:
186 /* XXX TODO reading large objects into memory is bad! */
187 err = got_inflate_to_mem_fd(&data, &datalen, &obj->len,
188 &obj->crc, obj->size, pack->fd);
192 err = get_obj_type_label(&obj_label, obj->type);
197 if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
198 err = got_error_from_errno("asprintf");
202 headerlen = strlen(header) + 1;
203 SHA1Update(&ctx, header, headerlen);
204 SHA1Update(&ctx, data, datalen);
205 SHA1Final(obj->id.sha1, &ctx);
209 case GOT_OBJ_TYPE_REF_DELTA:
210 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
211 n = read(pack->fd, &obj->delta.ref.ref_id.sha1,
214 err = got_error_from_errno("read");
217 if (n < sizeof(obj->id)) {
218 err = got_error(GOT_ERR_BAD_PACKFILE);
221 obj->crc = crc32(obj->crc, obj->delta.ref.ref_id.sha1,
223 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
224 &obj->crc, obj->size, pack->fd);
227 obj->len += SHA1_DIGEST_LENGTH;
229 case GOT_OBJ_TYPE_OFFSET_DELTA:
230 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
231 err = got_pack_parse_offset_delta(&obj->delta.ofs.base_offset,
232 &obj->delta.ofs.base_offsetlen, pack, obj->off,
237 /* XXX Seek back and get the CRC of on-disk offset bytes. */
238 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET) == -1) {
239 err = got_error_from_errno("lseek");
242 err = read_crc(&obj->crc, pack->fd,
243 obj->delta.ofs.base_offsetlen);
247 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len,
248 &obj->crc, obj->size, pack->fd);
251 obj->len += obj->delta.ofs.base_offsetlen;
254 err = got_error(GOT_ERR_OBJ_TYPE);
261 static const struct got_error *
262 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
266 SHA1Update(ctx, buf, len);
268 w = write(fd, buf, len);
270 return got_error_from_errno("write");
272 return got_error(GOT_ERR_IO);
277 static const struct got_error *
278 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
279 struct got_indexed_object *obj)
281 const struct got_error *err = NULL;
282 struct got_delta_chain deltas;
283 struct got_delta *delta;
290 const char *obj_label;
293 SIMPLEQ_INIT(&deltas.entries);
295 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
296 obj->off, obj->tslen, obj->type, obj->size,
297 GOT_DELTA_CHAIN_RECURSION_MAX);
301 /* XXX TODO reading large objects into memory is bad! */
302 err = got_pack_dump_delta_chain_to_mem(&buf, &len, &deltas, pack);
308 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
311 err = get_obj_type_label(&obj_label, base_obj_type);
314 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
315 err = got_error_from_errno("asprintf");
318 headerlen = strlen(header) + 1;
319 SHA1Update(&ctx, header, headerlen);
320 SHA1Update(&ctx, buf, len);
321 SHA1Final(obj->id.sha1, &ctx);
324 while (!SIMPLEQ_EMPTY(&deltas.entries)) {
325 delta = SIMPLEQ_FIRST(&deltas.entries);
326 SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
332 /* Determine the slot in the pack index a given object ID should use. */
334 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
336 u_int8_t id0 = sha1[0];
337 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
338 int left = 0, right = nindexed - 1;
342 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
344 while (left <= right) {
345 struct got_packidx_object_id *oid;
347 i = ((left + right) / 2);
348 oid = &packidx->hdr.sorted_ids[i];
350 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
352 return -1; /* object already indexed */
364 print_packidx(struct got_packidx *packidx)
366 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
369 fprintf(stderr, "object IDs:\n");
370 for (i = 0; i < nindexed; i++) {
371 char hex[SHA1_DIGEST_STRING_LENGTH];
372 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
374 fprintf(stderr, "%s\n", hex);
376 fprintf(stderr, "\n");
378 fprintf(stderr, "object offsets:\n");
379 for (i = 0; i < nindexed; i++) {
380 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
381 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
382 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
383 fprintf(stderr, "%u -> %llu\n", offset,
384 be64toh(packidx->hdr.large_offsets[j]));
386 fprintf(stderr, "%u\n", offset);
388 fprintf(stderr, "\n");
390 fprintf(stderr, "fanout table:");
391 for (i = 0; i <= 0xff; i++)
392 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
393 fprintf(stderr, "\n");
398 add_indexed_object(struct got_packidx *packidx, uint32_t idx,
399 struct got_indexed_object *obj)
403 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
405 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
406 packidx->hdr.offsets[idx] = htobe32(obj->off);
408 packidx->hdr.offsets[idx] = htobe32(packidx->nlargeobj |
409 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
410 packidx->hdr.large_offsets[packidx->nlargeobj] =
412 packidx->nlargeobj++;
415 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
416 uint32_t n = be32toh(packidx->hdr.fanout_table[i]);
417 packidx->hdr.fanout_table[i] = htobe32(n + 1);
422 indexed_obj_cmp(const void *pa, const void *pb)
424 struct got_indexed_object *a, *b;
426 a = *(struct got_indexed_object **)pa;
427 b = *(struct got_indexed_object **)pb;
428 return got_object_id_cmp(&a->id, &b->id);
432 make_packidx(struct got_packidx *packidx, int nobj,
433 struct got_indexed_object **objects)
435 struct got_indexed_object *obj;
439 mergesort(objects, nobj, sizeof(struct got_indexed_object *),
442 memset(packidx->hdr.fanout_table, 0,
443 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t));
444 packidx->nlargeobj = 0;
446 for (i = 0; i < nobj; i++) {
449 add_indexed_object(packidx, idx++, obj);
454 update_packidx(struct got_packidx *packidx, int nobj,
455 struct got_indexed_object *obj)
458 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
460 idx = find_object_idx(packidx, obj->id.sha1);
462 char hex[SHA1_DIGEST_STRING_LENGTH];
463 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
464 return; /* object already indexed */
467 memmove(&packidx->hdr.sorted_ids[idx + 1],
468 &packidx->hdr.sorted_ids[idx],
469 sizeof(struct got_packidx_object_id) * (nindexed - idx));
470 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
471 sizeof(uint32_t) * (nindexed - idx));
473 add_indexed_object(packidx, idx, obj);
476 static const struct got_error *
477 index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
478 struct imsgbuf *ibuf)
480 const struct got_error *err;
481 struct got_packfile_hdr hdr;
482 struct got_packidx packidx;
484 int nobj, nvalid, nloose, nresolved = 0, i;
485 struct got_indexed_object **objects = NULL, *obj;
487 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
489 int pass, have_ref_deltas = 0;
491 /* Check pack file header. */
492 r = read(pack->fd, &hdr, sizeof(hdr));
494 return got_error_from_errno("read");
496 return got_error_msg(GOT_ERR_BAD_PACKFILE,
497 "short packfile header");
499 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
500 return got_error_msg(GOT_ERR_BAD_PACKFILE,
501 "bad packfile signature");
502 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
503 return got_error_msg(GOT_ERR_BAD_PACKFILE,
504 "bad packfile version");
505 nobj = betoh32(hdr.nobjects);
507 return got_error_msg(GOT_ERR_BAD_PACKFILE,
508 "bad packfile with zero objects");
511 * Create an in-memory pack index which will grow as objects
512 * IDs in the pack file are discovered. Only fields used to
513 * read deltified objects will be needed by the pack.c library
514 * code, so setting up just a pack index header is sufficient.
516 memset(&packidx, 0, sizeof(packidx));
517 packidx.hdr.magic = malloc(sizeof(uint32_t));
518 if (packidx.hdr.magic == NULL)
519 return got_error_from_errno("calloc");
520 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
521 packidx.hdr.version = malloc(sizeof(uint32_t));
522 if (packidx.hdr.version == NULL) {
523 err = got_error_from_errno("malloc");
526 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
527 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
529 if (packidx.hdr.fanout_table == NULL) {
530 err = got_error_from_errno("calloc");
533 packidx.hdr.sorted_ids = calloc(nobj,
534 sizeof(struct got_packidx_object_id));
535 if (packidx.hdr.sorted_ids == NULL) {
536 err = got_error_from_errno("calloc");
539 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
540 if (packidx.hdr.offsets == NULL) {
541 err = got_error_from_errno("calloc");
544 /* Large offsets table is empty for pack files < 2 GB. */
545 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
546 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
547 if (packidx.hdr.large_offsets == NULL) {
548 err = got_error_from_errno("calloc");
555 objects = calloc(nobj, sizeof(struct got_indexed_object *));
557 return got_error_from_errno("calloc");
560 * First pass: locate all objects and identify un-deltified objects.
562 * When this pass has completed we will know offset, type, size, and
563 * CRC information for all objects in this pack file. We won't know
564 * any of the actual object IDs of deltified objects yet since we
565 * will not yet attempt to combine deltas.
568 for (i = 0; i < nobj; i++) {
569 err = got_privsep_send_index_pack_progress(ibuf, nobj, i + 1,
574 obj = calloc(1, sizeof(*obj));
576 err = got_error_from_errno("calloc");
579 obj->crc = crc32(0L, NULL, 0);
581 /* Store offset to type+size information for this object. */
582 obj->off = lseek(pack->fd, 0, SEEK_CUR);
583 if (obj->off == -1) {
584 err = got_error_from_errno("lseek");
588 err = read_packed_object(pack, obj);
594 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
596 err = got_error_from_errno("lseek");
600 if (obj->type == GOT_OBJ_TYPE_BLOB ||
601 obj->type == GOT_OBJ_TYPE_TREE ||
602 obj->type == GOT_OBJ_TYPE_COMMIT ||
603 obj->type == GOT_OBJ_TYPE_TAG) {
604 objects[i]->valid = 1;
606 } else if (obj->type == GOT_OBJ_TYPE_REF_DELTA)
611 /* In order to resolve ref deltas we need an in-progress pack index. */
613 make_packidx(&packidx, nobj, objects);
616 * Second pass: We can now resolve deltas to compute the IDs of
617 * objects which appear in deltified form. Because deltas can be
618 * chained this pass may require a couple of iterations until all
619 * IDs of deltified objects have been discovered.
622 while (nvalid != nobj) {
624 for (i = 0; i < nobj; i++) {
625 if (objects[i]->type != GOT_OBJ_TYPE_REF_DELTA &&
626 objects[i]->type != GOT_OBJ_TYPE_OFFSET_DELTA)
629 if (objects[i]->valid)
633 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET)
635 err = got_error_from_errno("lseek");
639 err = resolve_deltified_object(pack, &packidx, obj);
641 if (err->code != GOT_ERR_NO_OBJ)
644 * We cannot resolve this object yet because
645 * a delta base is unknown. Try again later.
650 objects[i]->valid = 1;
653 update_packidx(&packidx, nobj, obj);
654 err = got_privsep_send_index_pack_progress(ibuf,
655 nobj, nobj, nloose, nresolved + n);
660 if (pass++ > 3 && n == 0) {
662 snprintf(msg, sizeof(msg), "could not resolve "
663 "any of deltas; packfile could be corrupt");
664 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
668 if (nloose + nresolved == nobj) {
670 snprintf(msg, sizeof(msg),
671 "fix point reached too early: %d/%d/%d",
672 nvalid, nresolved, nobj);
673 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
680 if (nloose + nresolved != nobj) {
682 snprintf(msg, sizeof(msg),
683 "discovered only %d of %d objects",
684 nloose + nresolved, nobj);
685 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
689 make_packidx(&packidx, nobj, objects);
692 err = hwrite(idxfd, "\xfftOc\x00\x00\x00\x02", 8, &ctx);
695 err = hwrite(idxfd, packidx.hdr.fanout_table,
696 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
699 err = hwrite(idxfd, packidx.hdr.sorted_ids,
700 nobj * SHA1_DIGEST_LENGTH, &ctx);
703 for(i = 0; i < nobj; i++){
704 PUTBE32(buf, objects[i]->crc);
705 err = hwrite(idxfd, buf, 4, &ctx);
709 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t),
713 if (packidx.nlargeobj > 0) {
714 err = hwrite(idxfd, packidx.hdr.large_offsets,
715 packidx.nlargeobj * sizeof(uint64_t), &ctx);
719 err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
723 SHA1Final(packidx_hash, &ctx);
724 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
726 err = got_error_from_errno("write");
729 if (w != sizeof(packidx_hash)) {
730 err = got_error(GOT_ERR_IO);
734 free(packidx.hdr.magic);
735 free(packidx.hdr.version);
736 free(packidx.hdr.fanout_table);
737 free(packidx.hdr.sorted_ids);
738 free(packidx.hdr.offsets);
739 free(packidx.hdr.large_offsets);
744 main(int argc, char **argv)
746 const struct got_error *err = NULL, *close_err;
750 struct got_pack pack;
751 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
759 memset(&pack, 0, sizeof(pack));
761 pack.delta_cache = got_delta_cache_alloc(100,
762 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
763 if (pack.delta_cache == NULL) {
764 err = got_error_from_errno("got_delta_cache_alloc");
768 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
770 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
773 if (imsg.hdr.type == GOT_IMSG_STOP)
775 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
776 err = got_error(GOT_ERR_PRIVSEP_MSG);
779 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
780 err = got_error(GOT_ERR_PRIVSEP_LEN);
783 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
786 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
789 if (imsg.hdr.type == GOT_IMSG_STOP)
791 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
792 err = got_error(GOT_ERR_PRIVSEP_MSG);
795 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
796 err = got_error(GOT_ERR_PRIVSEP_LEN);
801 if (lseek(pack.fd, 0, SEEK_END) == -1) {
802 err = got_error_from_errno("lseek");
805 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
806 if (packfile_size == -1) {
807 err = got_error_from_errno("lseek");
810 pack.filesize = packfile_size; /* XXX off_t vs size_t */
812 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
813 err = got_error_from_errno("lseek");
817 err = index_pack(&pack, idxfd, pack_hash, &ibuf);
819 close_err = got_pack_close(&pack);
820 if (close_err && err == NULL)
822 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
823 err = got_error_from_errno("close");
826 err = got_privsep_send_index_pack_done(&ibuf);
828 got_privsep_send_error(&ibuf, err);
829 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
830 got_privsep_send_error(&ibuf, err);