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. */
79 struct got_object_id ref_id;
81 /* For offset deltas. */
83 size_t base_offsetlen;
86 #define PUTBE32(b, n)\
94 #define PUTBE64(b, n)\
100 (b)[4] = (n) >> 24; \
101 (b)[5] = (n) >> 16; \
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);
134 static const struct got_error *
135 read_packed_object(struct got_pack *pack, struct got_indexed_object *obj)
137 const struct got_error *err = NULL;
144 const char *obj_label;
146 err = got_pack_parse_object_type_and_size(&obj->type, &obj->size, &obj->tslen,
152 case GOT_OBJ_TYPE_BLOB:
153 case GOT_OBJ_TYPE_COMMIT:
154 case GOT_OBJ_TYPE_TREE:
155 case GOT_OBJ_TYPE_TAG:
156 /* XXX TODO reading large objects into memory is bad! */
157 err = got_inflate_to_mem_fd(&data, &datalen, &obj->len, pack->fd);
161 err = get_obj_type_label(&obj_label, obj->type);
164 if (asprintf(&header, "%s %lld", obj_label, obj->size) == -1) {
165 err = got_error_from_errno("asprintf");
169 headerlen = strlen(header) + 1;
170 SHA1Update(&ctx, header, headerlen);
171 SHA1Update(&ctx, data, datalen);
172 SHA1Final(obj->id.sha1, &ctx);
176 case GOT_OBJ_TYPE_REF_DELTA:
177 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
178 n = read(pack->fd, &obj->ref_id.sha1, SHA1_DIGEST_LENGTH);
180 err = got_error_from_errno("read");
183 if (n < sizeof(obj->id)) {
184 err = got_error(GOT_ERR_BAD_PACKFILE);
187 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len, pack->fd);
190 obj->len += SHA1_DIGEST_LENGTH;
192 case GOT_OBJ_TYPE_OFFSET_DELTA:
193 memset(obj->id.sha1, 0xff, SHA1_DIGEST_LENGTH);
194 err = got_pack_parse_offset_delta(&obj->base_offset,
195 &obj->base_offsetlen, pack, obj->off, obj->tslen);
198 err = got_inflate_to_mem_fd(NULL, &datalen, &obj->len, pack->fd);
201 obj->len += obj->base_offsetlen;
204 err = got_error(GOT_ERR_OBJ_TYPE);
211 static const struct got_error *
212 hwrite(int fd, void *buf, int len, SHA1_CTX *ctx)
216 SHA1Update(ctx, buf, len);
218 w = write(fd, buf, len);
220 return got_error_from_errno("write");
222 return got_error(GOT_ERR_IO);
227 static const struct got_error *
228 object_crc(int packfd, struct got_indexed_object *obj)
235 if (lseek(packfd, obj->off + obj->tslen, SEEK_SET) == -1)
236 return got_error_from_errno("lseek");
238 obj->crc = crc32(0L, NULL, 0);
239 for (n = obj->len; n > 0; n -= r){
240 r = read(packfd, buf, n > sizeof(buf) ? sizeof(buf) : n);
242 return got_error_from_errno("read");
245 obj->crc = crc32(obj->crc, buf, r);
252 indexed_obj_cmp(const void *pa, const void *pb)
254 struct got_indexed_object *a, *b;
256 a = *(struct got_indexed_object **)pa;
257 b = *(struct got_indexed_object **)pb;
258 return got_object_id_cmp(&a->id, &b->id);
262 static const struct got_error *
263 resolve_deltified_object(struct got_pack *pack, struct got_packidx *packidx,
264 struct got_indexed_object *obj)
266 const struct got_error *err = NULL;
267 struct got_delta_chain deltas;
268 struct got_delta *delta;
275 const char *obj_label;
278 SIMPLEQ_INIT(&deltas.entries);
280 err = got_pack_resolve_delta_chain(&deltas, packidx, pack,
281 obj->off, obj->tslen, obj->type, obj->size,
282 GOT_DELTA_CHAIN_RECURSION_MAX);
286 /* XXX TODO reading large objects into memory is bad! */
287 err = got_pack_dump_delta_chain_to_mem(&buf, &len, &deltas, pack);
293 err = got_delta_chain_get_base_type(&base_obj_type, &deltas);
296 err = get_obj_type_label(&obj_label, base_obj_type);
299 if (asprintf(&header, "%s %zd", obj_label, len) == -1) {
300 err = got_error_from_errno("asprintf");
303 headerlen = strlen(header) + 1;
304 SHA1Update(&ctx, header, headerlen);
305 SHA1Update(&ctx, buf, len);
306 SHA1Final(obj->id.sha1, &ctx);
309 while (!SIMPLEQ_EMPTY(&deltas.entries)) {
310 delta = SIMPLEQ_FIRST(&deltas.entries);
311 SIMPLEQ_REMOVE_HEAD(&deltas.entries, entry);
317 /* Determine the slot in the pack index a given object ID should use. */
319 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
321 u_int8_t id0 = sha1[0];
322 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
323 int left = 0, right = nindexed - 1;
327 left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
329 while (left <= right) {
330 struct got_packidx_object_id *oid;
332 i = ((left + right) / 2);
333 oid = &packidx->hdr.sorted_ids[i];
335 cmp = memcmp(sha1, oid->sha1, SHA1_DIGEST_LENGTH);
337 return -1; /* object already indexed */
349 print_packidx(struct got_packidx *packidx)
351 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
354 fprintf(stderr, "object IDs:\n");
355 for (i = 0; i < nindexed; i++) {
356 char hex[SHA1_DIGEST_STRING_LENGTH];
357 got_sha1_digest_to_str(packidx->hdr.sorted_ids[i].sha1,
359 fprintf(stderr, "%s\n", hex);
361 fprintf(stderr, "\n");
363 fprintf(stderr, "object offsets:\n");
364 for (i = 0; i < nindexed; i++) {
365 uint32_t offset = be32toh(packidx->hdr.offsets[i]);
366 if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
367 int j = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
368 fprintf(stderr, "%u -> %llu\n", offset,
369 be64toh(packidx->hdr.large_offsets[j]));
371 fprintf(stderr, "%u\n", offset);
373 fprintf(stderr, "\n");
375 fprintf(stderr, "fanout table:");
376 for (i = 0; i <= 0xff; i++)
377 fprintf(stderr, " %u", be32toh(packidx->hdr.fanout_table[i]));
378 fprintf(stderr, "\n");
383 update_packidx(int *nlarge, struct got_packidx *packidx, int nobj,
384 struct got_indexed_object *obj)
387 uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
389 idx = find_object_idx(packidx, obj->id.sha1);
391 char hex[SHA1_DIGEST_STRING_LENGTH];
392 got_sha1_digest_to_str(obj->id.sha1, hex, sizeof(hex));
393 return; /* object already indexed */
396 memmove(&packidx->hdr.sorted_ids[idx + 1],
397 &packidx->hdr.sorted_ids[idx],
398 sizeof(struct got_packidx_object_id) * (nindexed - idx));
399 memmove(&packidx->hdr.offsets[idx + 1], &packidx->hdr.offsets[idx],
400 sizeof(uint32_t) * (nindexed - idx));
402 memcpy(packidx->hdr.sorted_ids[idx].sha1, obj->id.sha1,
404 if (obj->off < GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
405 packidx->hdr.offsets[idx] = htobe32(obj->off);
407 packidx->hdr.offsets[idx] = htobe32(*nlarge |
408 GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX);
409 packidx->hdr.large_offsets[*nlarge] = htobe64(obj->off);
413 for (i = obj->id.sha1[0]; i <= 0xff; i++) {
414 n = be32toh(packidx->hdr.fanout_table[i]);
415 packidx->hdr.fanout_table[i] = htobe32(n + 1);
419 static const struct got_error *
420 index_pack(struct got_pack *pack, int idxfd, uint8_t *pack_hash,
421 struct imsgbuf *ibuf)
423 const struct got_error *err;
424 struct got_packfile_hdr hdr;
425 struct got_packidx packidx;
427 int nobj, nvalid, nloose, nlarge = 0, nresolved = 0, i;
428 struct got_indexed_object **objects = NULL, *obj;
430 uint8_t packidx_hash[SHA1_DIGEST_LENGTH];
434 /* Check pack file header. */
435 r = read(pack->fd, &hdr, sizeof(hdr));
437 return got_error_from_errno("read");
439 return got_error_msg(GOT_ERR_BAD_PACKFILE,
440 "short packfile header");
442 if (hdr.signature != htobe32(GOT_PACKFILE_SIGNATURE))
443 return got_error_msg(GOT_ERR_BAD_PACKFILE,
444 "bad packfile signature");
445 if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
446 return got_error_msg(GOT_ERR_BAD_PACKFILE,
447 "bad packfile version");
448 nobj = betoh32(hdr.nobjects);
450 return got_error_msg(GOT_ERR_BAD_PACKFILE,
451 "bad packfile with zero objects");
454 * Create an in-memory pack index which will grow as objects
455 * IDs in the pack file are discovered. Only fields used to
456 * read deltified objects will be needed by the pack.c library
457 * code, so setting up just a pack index header is sufficient.
459 memset(&packidx, 0, sizeof(packidx));
460 packidx.hdr.magic = malloc(sizeof(uint32_t));
461 if (packidx.hdr.magic == NULL)
462 return got_error_from_errno("calloc");
463 *packidx.hdr.magic = htobe32(GOT_PACKIDX_V2_MAGIC);
464 packidx.hdr.version = malloc(sizeof(uint32_t));
465 if (packidx.hdr.version == NULL) {
466 err = got_error_from_errno("malloc");
469 *packidx.hdr.version = htobe32(GOT_PACKIDX_VERSION);
470 packidx.hdr.fanout_table = calloc(GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS,
472 if (packidx.hdr.fanout_table == NULL) {
473 err = got_error_from_errno("calloc");
476 packidx.hdr.sorted_ids = calloc(nobj,
477 sizeof(struct got_packidx_object_id));
478 if (packidx.hdr.sorted_ids == NULL) {
479 err = got_error_from_errno("calloc");
482 packidx.hdr.offsets = calloc(nobj, sizeof(uint32_t));
483 if (packidx.hdr.offsets == NULL) {
484 err = got_error_from_errno("calloc");
487 /* Large offsets table is empty for pack files < 2 GB. */
488 if (pack->filesize >= GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
489 packidx.hdr.large_offsets = calloc(nobj, sizeof(uint64_t));
490 if (packidx.hdr.large_offsets == NULL) {
491 err = got_error_from_errno("calloc");
498 objects = calloc(nobj, sizeof(struct got_indexed_object *));
500 return got_error_from_errno("calloc");
503 * First pass: locate all objects and identify un-deltified objects.
505 * When this pass has completed we will know offset, type, size, and
506 * CRC information for all objects in this pack file. We won't know
507 * any of the actual object IDs of deltified objects yet since we
508 * will not yet attempt to combine deltas.
511 for (i = 0; i < nobj; i++) {
512 err = got_privsep_send_index_pack_progress(ibuf, nobj, i + 1,
517 obj = calloc(1, sizeof(*obj));
519 err = got_error_from_errno("calloc");
523 /* Store offset to type+size information for this object. */
524 obj->off = lseek(pack->fd, 0, SEEK_CUR);
525 if (obj->off == -1) {
526 err = got_error_from_errno("lseek");
530 err = read_packed_object(pack, obj);
537 err = object_crc(pack->fd, obj);
542 if (obj->type == GOT_OBJ_TYPE_BLOB ||
543 obj->type == GOT_OBJ_TYPE_TREE ||
544 obj->type == GOT_OBJ_TYPE_COMMIT ||
545 obj->type == GOT_OBJ_TYPE_TAG) {
546 objects[i]->valid = 1;
548 update_packidx(&nlarge, &packidx, nobj, obj);
551 if (lseek(pack->fd, obj->off + obj->tslen + obj->len,
553 err = got_error_from_errno("lseek");
560 * Second pass: We can now resolve deltas to compute the IDs of
561 * objects which appear in deltified form. Because deltas can be
562 * chained this pass may require a couple of iterations until all
563 * IDs of deltified objects have been discovered.
566 while (nvalid != nobj) {
568 for (i = 0; i < nobj; i++) {
569 if (objects[i]->type != GOT_OBJ_TYPE_REF_DELTA &&
570 objects[i]->type != GOT_OBJ_TYPE_OFFSET_DELTA)
573 if (objects[i]->valid)
577 if (lseek(pack->fd, obj->off + obj->tslen, SEEK_SET) == -1) {
578 err = got_error_from_errno("lseek");
582 err = resolve_deltified_object(pack, &packidx, obj);
584 if (err->code != GOT_ERR_NO_OBJ)
587 * We cannot resolve this object yet because
588 * a delta base is unknown. Try again later.
593 objects[i]->valid = 1;
595 update_packidx(&nlarge, &packidx, nobj, obj);
596 err = got_privsep_send_index_pack_progress(ibuf, nobj, nobj,
597 nloose, nresolved + n);
602 if (pass++ > 3 && n == 0) {
604 snprintf(msg, sizeof(msg), "could not resolve "
605 "any of deltas; packfile could be corrupt");
606 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
610 if (nloose + nresolved == nobj) {
612 snprintf(msg, sizeof(msg),
613 "fix point reached too early: %d/%d/%d", nvalid, nresolved, nobj);
614 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
621 if (nloose + nresolved != nobj) {
623 snprintf(msg, sizeof(msg),
624 "discovered only %d of %d objects", nloose + nresolved, nobj);
625 err = got_error_msg(GOT_ERR_BAD_PACKFILE, msg);
629 /* We may have seen duplicates. Update our total object count. */
630 nobj = betoh32(packidx.hdr.fanout_table[0xff]);
633 err = hwrite(idxfd, "\xfftOc\x00\x00\x00\x02", 8, &ctx);
636 err = hwrite(idxfd, packidx.hdr.fanout_table,
637 GOT_PACKIDX_V2_FANOUT_TABLE_ITEMS * sizeof(uint32_t), &ctx);
640 err = hwrite(idxfd, packidx.hdr.sorted_ids,
641 nobj * SHA1_DIGEST_LENGTH, &ctx);
644 for(i = 0; i < nobj; i++){
645 PUTBE32(buf, objects[i]->crc);
646 err = hwrite(idxfd, buf, 4, &ctx);
650 err = hwrite(idxfd, packidx.hdr.offsets, nobj * sizeof(uint32_t), &ctx);
654 err = hwrite(idxfd, packidx.hdr.large_offsets,
655 nlarge * sizeof(uint64_t), &ctx);
659 err = hwrite(idxfd, pack_hash, SHA1_DIGEST_LENGTH, &ctx);
663 SHA1Final(packidx_hash, &ctx);
664 w = write(idxfd, packidx_hash, sizeof(packidx_hash));
666 err = got_error_from_errno("write");
669 if (w != sizeof(packidx_hash)) {
670 err = got_error(GOT_ERR_IO);
674 free(packidx.hdr.magic);
675 free(packidx.hdr.version);
676 free(packidx.hdr.fanout_table);
677 free(packidx.hdr.sorted_ids);
678 free(packidx.hdr.offsets);
679 free(packidx.hdr.large_offsets);
684 main(int argc, char **argv)
686 const struct got_error *err = NULL, *close_err;
690 struct got_pack pack;
691 uint8_t pack_hash[SHA1_DIGEST_LENGTH];
699 memset(&pack, 0, sizeof(pack));
701 pack.delta_cache = got_delta_cache_alloc(100,
702 GOT_DELTA_RESULT_SIZE_CACHED_MAX);
703 if (pack.delta_cache == NULL) {
704 err = got_error_from_errno("got_delta_cache_alloc");
708 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
710 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
713 if (imsg.hdr.type == GOT_IMSG_STOP)
715 if (imsg.hdr.type != GOT_IMSG_IDXPACK_REQUEST) {
716 err = got_error(GOT_ERR_PRIVSEP_MSG);
719 if (imsg.hdr.len - IMSG_HEADER_SIZE != sizeof(pack_hash)) {
720 err = got_error(GOT_ERR_PRIVSEP_LEN);
723 memcpy(pack_hash, imsg.data, sizeof(pack_hash));
726 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
729 if (imsg.hdr.type == GOT_IMSG_STOP)
731 if (imsg.hdr.type != GOT_IMSG_TMPFD) {
732 err = got_error(GOT_ERR_PRIVSEP_MSG);
735 if (imsg.hdr.len - IMSG_HEADER_SIZE != 0) {
736 err = got_error(GOT_ERR_PRIVSEP_LEN);
741 if (lseek(pack.fd, 0, SEEK_END) == -1) {
742 err = got_error_from_errno("lseek");
745 packfile_size = lseek(pack.fd, 0, SEEK_CUR);
746 if (packfile_size == -1) {
747 err = got_error_from_errno("lseek");
750 pack.filesize = packfile_size; /* XXX off_t vs size_t */
752 if (lseek(pack.fd, 0, SEEK_SET) == -1) {
753 err = got_error_from_errno("lseek");
757 err = index_pack(&pack, idxfd, pack_hash, &ibuf);
759 close_err = got_pack_close(&pack);
760 if (close_err && err == NULL)
762 if (idxfd != -1 && close(idxfd) == -1 && err == NULL)
763 err = got_error_from_errno("close");
766 err = got_privsep_send_index_pack_done(&ibuf);
768 got_privsep_send_error(&ibuf, err);
769 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
770 got_privsep_send_error(&ibuf, err);