2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 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/types.h>
31 #if defined(__FreeBSD__)
35 #include "got_error.h"
36 #include "got_cancel.h"
37 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository_admin.h"
41 #include "got_opentemp.h"
43 #include "got_lib_deltify.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_deflate.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_ratelimit.h"
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 struct got_pack_meta {
63 struct got_object_id id;
69 /* The best delta we picked */
70 struct got_pack_meta *head;
71 struct got_pack_meta *prev;
72 unsigned char *delta_buf; /* if not encoded in delta cache file */
73 off_t delta_offset; /* offset in delta cache file */
74 off_t delta_len; /* encoded delta length */
77 int have_reused_delta;
78 off_t reused_delta_offset; /* offset of delta in reused pack file */
79 struct got_object_id *base_obj_id;
81 /* Only used for delta window */
82 struct got_delta_table *dtab;
84 /* Only used for writing offset deltas */
88 struct got_pack_metavec {
89 struct got_pack_meta **meta;
94 static const struct got_error *
95 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
96 const char *path, int obj_type, time_t mtime)
98 const struct got_error *err = NULL;
99 struct got_pack_meta *m;
103 m = calloc(1, sizeof(*m));
105 return got_error_from_errno("calloc");
107 memcpy(&m->id, id, sizeof(m->id));
109 m->path = strdup(path);
110 if (m->path == NULL) {
111 err = got_error_from_errno("strdup");
116 m->obj_type = obj_type;
123 clear_meta(struct got_pack_meta *meta)
129 free(meta->delta_buf);
130 meta->delta_buf = NULL;
131 free(meta->base_obj_id);
132 meta->base_obj_id = NULL;
136 free_nmeta(struct got_pack_meta **meta, int nmeta)
140 for (i = 0; i < nmeta; i++)
146 delta_order_cmp(const void *pa, const void *pb)
148 struct got_pack_meta *a, *b;
151 a = *(struct got_pack_meta **)pa;
152 b = *(struct got_pack_meta **)pb;
154 if (a->obj_type != b->obj_type)
155 return a->obj_type - b->obj_type;
156 cmp = strcmp(a->path, b->path);
159 if (a->mtime != b->mtime)
160 return a->mtime - b->mtime;
161 return got_object_id_cmp(&a->id, &b->id);
165 delta_size(struct got_delta_instruction *deltas, int ndeltas)
169 for (i = 0; i < ndeltas; i++) {
171 size += GOT_DELTA_SIZE_SHIFT;
173 size += deltas[i].len + 1;
178 static const struct got_error *
179 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
183 if (*len + nseg >= *sz) {
184 while (*len + nseg >= *sz)
186 n = realloc(*p, *sz);
188 return got_error_from_errno("realloc");
191 memcpy(*p + *len, seg, nseg);
196 static const struct got_error *
197 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
198 struct got_delta_instruction *deltas, int ndeltas,
199 off_t delta_size, off_t base_size)
201 const struct got_error *err;
202 unsigned char buf[16], *bp;
206 struct got_delta_instruction *d;
208 m->delta_buf = malloc(delta_size);
209 if (m->delta_buf == NULL)
210 return got_error_from_errno("calloc");
212 /* base object size */
213 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
214 n = base_size >> GOT_DELTA_SIZE_SHIFT;
215 for (i = 1; n > 0; i++) {
216 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
217 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
218 n >>= GOT_DELTA_SIZE_SHIFT;
220 err = append(&m->delta_buf, &len, &delta_size, buf, i);
224 /* target object size */
225 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
226 n = o->size >> GOT_DELTA_SIZE_SHIFT;
227 for (i = 1; n > 0; i++) {
228 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
229 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
230 n >>= GOT_DELTA_SIZE_SHIFT;
232 err = append(&m->delta_buf, &len, &delta_size, buf, i);
236 for (j = 0; j < ndeltas; j++) {
241 buf[0] = GOT_DELTA_BASE_COPY;
242 for (i = 0; i < 4; i++) {
243 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
252 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
253 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
254 for (i = 0; i < 3 && n > 0; i++) {
255 buf[0] |= 1 << (i + 4);
260 err = append(&m->delta_buf, &len, &delta_size,
264 } else if (o->f == NULL) {
266 while (n != d->len) {
267 buf[0] = (d->len - n < 127) ? d->len - n : 127;
268 err = append(&m->delta_buf, &len, &delta_size,
272 err = append(&m->delta_buf, &len, &delta_size,
273 o->data + o->hdrlen + d->offset + n,
282 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
283 return got_error_from_errno("fseeko");
285 while (n != d->len) {
286 buf[0] = (d->len - n < 127) ? d->len - n : 127;
287 err = append(&m->delta_buf, &len, &delta_size,
291 r = fread(content, 1, buf[0], o->f);
293 return got_ferror(o->f, GOT_ERR_IO);
294 err = append(&m->delta_buf, &len, &delta_size,
307 static const struct got_error *
308 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
309 struct got_delta_instruction *deltas, int ndeltas,
310 off_t base_size, FILE *f)
312 unsigned char buf[16], *bp;
316 struct got_delta_instruction *d;
318 /* base object size */
319 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
320 n = base_size >> GOT_DELTA_SIZE_SHIFT;
321 for (i = 1; n > 0; i++) {
322 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
323 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
324 n >>= GOT_DELTA_SIZE_SHIFT;
326 w = fwrite(buf, 1, i, f);
328 return got_ferror(f, GOT_ERR_IO);
330 /* target object size */
331 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
332 n = o->size >> GOT_DELTA_SIZE_SHIFT;
333 for (i = 1; n > 0; i++) {
334 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
335 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
336 n >>= GOT_DELTA_SIZE_SHIFT;
338 w = fwrite(buf, 1, i, f);
340 return got_ferror(f, GOT_ERR_IO);
342 for (j = 0; j < ndeltas; j++) {
347 buf[0] = GOT_DELTA_BASE_COPY;
348 for (i = 0; i < 4; i++) {
349 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
358 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
359 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
360 for (i = 0; i < 3 && n > 0; i++) {
361 buf[0] |= 1 << (i + 4);
366 w = fwrite(buf, 1, bp - buf, f);
368 return got_ferror(f, GOT_ERR_IO);
369 } else if (o->f == NULL) {
371 while (n != d->len) {
372 buf[0] = (d->len - n < 127) ? d->len - n : 127;
373 w = fwrite(buf, 1, 1, f);
375 return got_ferror(f, GOT_ERR_IO);
376 w = fwrite(o->data + o->hdrlen + d->offset + n,
379 return got_ferror(f, GOT_ERR_IO);
385 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
386 return got_error_from_errno("fseeko");
388 while (n != d->len) {
389 buf[0] = (d->len - n < 127) ? d->len - n : 127;
390 w = fwrite(buf, 1, 1, f);
392 return got_ferror(f, GOT_ERR_IO);
393 r = fread(content, 1, buf[0], o->f);
395 return got_ferror(o->f, GOT_ERR_IO);
396 w = fwrite(content, 1, buf[0], f);
398 return got_ferror(f, GOT_ERR_IO);
404 m->delta_len = ftello(f) - m->delta_offset;
408 static const struct got_error *
409 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
410 struct got_ratelimit *rl, off_t packfile_size, int ncommits,
411 int nobj_total, int obj_deltify, int nobj_written)
413 const struct got_error *err;
416 if (progress_cb == NULL)
419 err = got_ratelimit_check(&elapsed, rl);
423 return progress_cb(progress_arg, packfile_size, ncommits,
424 nobj_total, obj_deltify, nobj_written);
427 static const struct got_error *
428 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
430 if (v->nmeta == v->metasz){
431 size_t newsize = 2 * v->metasz;
432 struct got_pack_meta **new;
433 new = reallocarray(v->meta, newsize, sizeof(*new));
435 return got_error_from_errno("reallocarray");
440 v->meta[v->nmeta++] = m;
444 static const struct got_error *
445 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
446 struct got_object_idset *idset, struct got_pack *pack,
447 struct got_packidx *packidx, int delta_cache_fd,
448 struct got_repository *repo)
450 const struct got_error *err = NULL;
451 struct got_pack_meta *base = NULL;
452 struct got_object_id *base_obj_id = NULL;
453 off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
454 uint64_t base_size, result_size;
456 if (m->have_reused_delta)
459 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
460 &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
461 packidx, idx, &m->id, repo);
465 if (delta_offset + delta_len < delta_offset)
466 return got_error(GOT_ERR_BAD_PACKFILE);
468 base = got_object_idset_get(idset, base_obj_id);
472 m->delta_len = delta_len;
473 m->delta_offset = delta_cache_offset;
475 m->size = result_size;
476 m->have_reused_delta = 1;
477 m->reused_delta_offset = delta_offset;
478 m->base_obj_id = base_obj_id;
480 err = add_meta(m, v);
486 static const struct got_error *
487 find_pack_for_reuse(struct got_packidx **best_packidx,
488 struct got_repository *repo)
490 const struct got_error *err;
491 struct got_pathlist_head packidx_paths;
492 struct got_pathlist_entry *pe;
493 const char *best_packidx_path = NULL;
496 TAILQ_INIT(&packidx_paths);
497 *best_packidx = NULL;
499 err = got_repo_list_packidx(&packidx_paths, repo);
503 TAILQ_FOREACH(pe, &packidx_paths, entry) {
504 const char *path_packidx = pe->path;
505 struct got_packidx *packidx;
508 err = got_repo_get_packidx(&packidx, path_packidx, repo);
512 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
513 if (nobj > nobj_max) {
514 best_packidx_path = path_packidx;
519 if (best_packidx_path) {
520 err = got_repo_get_packidx(best_packidx, best_packidx_path,
524 TAILQ_FOREACH(pe, &packidx_paths, entry)
525 free((void *)pe->path);
526 got_pathlist_free(&packidx_paths);
530 struct search_deltas_arg {
531 struct got_packidx *packidx;
532 struct got_pack *pack;
533 struct got_object_idset *idset;
534 struct got_pack_metavec *v;
536 struct got_repository *repo;
537 got_pack_progress_cb progress_cb;
539 struct got_ratelimit *rl;
540 got_cancel_cb cancel_cb;
545 static const struct got_error *
546 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
548 const struct got_error *err;
549 struct got_pack_meta *m = data;
550 struct search_deltas_arg *a = arg;
552 struct got_object *obj = NULL;
555 err = (*a->cancel_cb)(a->cancel_arg);
560 if (!got_repo_check_packidx_bloom_filter(a->repo,
561 a->packidx->path_packidx, id))
564 obj_idx = got_packidx_get_object_idx(a->packidx, id);
569 * Opening and closing an object just to check its flags
570 * is a bit expensive. We could have an imsg which requests
571 * plain type/size information for an object without doing
572 * work such as traversing the object's entire delta chain
573 * to find the base object type, and other such info which
574 * we don't really need here.
576 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
577 a->packidx, obj_idx, a->repo);
581 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
582 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
583 a->delta_cache_fd, a->repo);
586 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
587 0L, a->ncommits, got_object_idset_num_elements(a->idset),
591 got_object_close(obj);
595 static const struct got_error *
596 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
597 int delta_cache_fd, int ncommits, struct got_repository *repo,
598 got_pack_progress_cb progress_cb, void *progress_arg,
599 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
601 const struct got_error *err = NULL;
602 char *path_packfile = NULL;
603 struct got_packidx *packidx;
604 struct got_pack *pack;
605 struct search_deltas_arg sda;
607 err = find_pack_for_reuse(&packidx, repo);
614 err = got_packidx_get_packfile_path(&path_packfile,
615 packidx->path_packidx);
619 pack = got_repo_get_cached_pack(repo, path_packfile);
621 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
626 sda.packidx = packidx;
630 sda.delta_cache_fd = delta_cache_fd;
632 sda.progress_cb = progress_cb;
633 sda.progress_arg = progress_arg;
635 sda.cancel_cb = cancel_cb;
636 sda.cancel_arg = cancel_arg;
637 sda.ncommits = ncommits;
638 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
644 static const struct got_error *
645 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncommits,
646 int nreused, FILE *delta_cache, struct got_repository *repo,
647 got_pack_progress_cb progress_cb, void *progress_arg,
648 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
650 const struct got_error *err = NULL;
651 struct got_pack_meta *m = NULL, *base = NULL;
652 struct got_raw_object *raw = NULL, *base_raw = NULL;
653 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
654 int i, j, ndeltas, best_ndeltas;
655 off_t size, best_size;
656 const int max_base_candidates = 3;
657 size_t delta_memsize = 0;
658 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
661 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
662 for (i = 0; i < nmeta; i++) {
664 err = (*cancel_cb)(cancel_arg);
668 err = report_progress(progress_cb, progress_arg, rl,
669 0L, ncommits, nreused + nmeta, nreused + i, 0);
674 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
675 m->obj_type == GOT_OBJ_TYPE_TAG)
678 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
683 if (raw->f == NULL) {
684 err = got_deltify_init_mem(&m->dtab, raw->data,
685 raw->hdrlen, raw->size + raw->hdrlen);
687 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
688 raw->size + raw->hdrlen);
693 if (i > max_base_candidates) {
694 struct got_pack_meta *n = NULL;
695 n = meta[i - (max_base_candidates + 1)];
696 got_deltify_free(n->dtab);
700 best_size = raw->size;
702 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
704 err = (*cancel_cb)(cancel_arg);
709 /* long chains make unpacking slow, avoid such bases */
710 if (base->nchain >= 128 ||
711 base->obj_type != m->obj_type)
714 err = got_object_raw_open(&base_raw, &outfd, repo,
719 if (raw->f == NULL && base_raw->f == NULL) {
720 err = got_deltify_mem_mem(&deltas, &ndeltas,
721 raw->data, raw->hdrlen,
722 raw->size + raw->hdrlen,
723 base->dtab, base_raw->data,
725 base_raw->size + base_raw->hdrlen);
726 } else if (raw->f == NULL) {
727 err = got_deltify_mem_file(&deltas, &ndeltas,
728 raw->data, raw->hdrlen,
729 raw->size + raw->hdrlen,
730 base->dtab, base_raw->f,
732 base_raw->size + base_raw->hdrlen);
733 } else if (base_raw->f == NULL) {
734 err = got_deltify_file_mem(&deltas, &ndeltas,
736 raw->size + raw->hdrlen,
737 base->dtab, base_raw->data,
739 base_raw->size + base_raw->hdrlen);
741 err = got_deltify(&deltas, &ndeltas,
743 raw->size + raw->hdrlen,
744 base->dtab, base_raw->f, base_raw->hdrlen,
745 base_raw->size + base_raw->hdrlen);
747 got_object_raw_close(base_raw);
752 size = delta_size(deltas, ndeltas);
753 if (size + 32 < best_size){
755 * if we already picked a best delta,
760 best_deltas = deltas;
761 best_ndeltas = ndeltas;
763 m->nchain = base->nchain + 1;
765 m->head = base->head;
775 if (best_ndeltas > 0) {
776 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
777 delta_memsize + best_size <= max_delta_memsize) {
778 delta_memsize += best_size;
779 err = encode_delta_in_mem(m, raw, best_deltas,
780 best_ndeltas, best_size, m->prev->size);
782 m->delta_offset = ftello(delta_cache);
785 * Storing compressed delta data in the delta
786 * cache file would probably be more efficient
787 * than writing uncompressed delta data here
788 * and compressing it while writing the pack
789 * file. This would also allow for reusing
790 * deltas in their compressed form.
792 err = encode_delta(m, raw, best_deltas,
793 best_ndeltas, m->prev->size, delta_cache);
802 got_object_raw_close(raw);
806 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
807 got_deltify_free(meta[i]->dtab);
808 meta[i]->dtab = NULL;
811 got_object_raw_close(raw);
813 got_object_raw_close(base_raw);
814 if (outfd != -1 && close(outfd) == -1 && err == NULL)
815 err = got_error_from_errno("close");
821 static const struct got_error *
822 search_packidx(int *found, struct got_object_id *id,
823 struct got_repository *repo)
825 const struct got_error *err = NULL;
826 struct got_packidx *packidx = NULL;
831 err = got_repo_search_packidx(&packidx, &idx, repo, id);
833 *found = 1; /* object is already packed */
834 else if (err->code == GOT_ERR_NO_OBJ)
839 static const int obj_types[] = {
845 GOT_OBJ_TYPE_OFFSET_DELTA,
846 GOT_OBJ_TYPE_REF_DELTA
849 static const struct got_error *
850 add_object(int want_meta, struct got_object_idset *idset,
851 struct got_object_id *id, const char *path, int obj_type,
852 time_t mtime, int loose_obj_only, struct got_repository *repo)
854 const struct got_error *err;
855 struct got_pack_meta *m = NULL;
857 if (loose_obj_only) {
859 err = search_packidx(&is_packed, id, repo);
867 err = alloc_meta(&m, id, path, obj_type, mtime);
872 return got_object_idset_add(idset, id, m);
875 static const struct got_error *
876 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
877 struct got_object_idset *idset, struct got_object_id *tree_id,
878 const char *dpath, time_t mtime, struct got_repository *repo,
879 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
881 const struct got_error *err;
882 struct got_tree_object *tree;
886 err = got_object_open_as_tree(&tree, repo, tree_id);
890 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
891 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
892 struct got_object_id *id = got_tree_entry_get_id(e);
893 mode_t mode = got_tree_entry_get_mode(e);
896 err = (*cancel_cb)(cancel_arg);
901 if (got_object_tree_entry_is_submodule(e) ||
902 got_object_idset_contains(idset, id))
905 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
906 got_tree_entry_get_name(e)) == -1) {
907 err = got_error_from_errno("asprintf");
912 struct got_object_qid *qid;
913 err = got_object_qid_alloc(&qid, id);
916 STAILQ_INSERT_TAIL(ids, qid, entry);
917 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
918 err = add_object(want_meta, idset, id, p,
919 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo);
927 got_object_tree_close(tree);
932 static const struct got_error *
933 load_tree(int want_meta, struct got_object_idset *idset,
934 struct got_object_id *tree_id, const char *dpath, time_t mtime,
935 int loose_obj_only, struct got_repository *repo,
936 got_cancel_cb cancel_cb, void *cancel_arg)
938 const struct got_error *err = NULL;
939 struct got_object_id_queue tree_ids;
940 struct got_object_qid *qid;
942 if (got_object_idset_contains(idset, tree_id))
945 err = got_object_qid_alloc(&qid, tree_id);
949 STAILQ_INIT(&tree_ids);
950 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
952 while (!STAILQ_EMPTY(&tree_ids)) {
954 err = (*cancel_cb)(cancel_arg);
959 qid = STAILQ_FIRST(&tree_ids);
960 STAILQ_REMOVE_HEAD(&tree_ids, entry);
962 if (got_object_idset_contains(idset, qid->id)) {
963 got_object_qid_free(qid);
967 err = add_object(want_meta, idset, qid->id, dpath,
968 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo);
970 got_object_qid_free(qid);
974 err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
975 dpath, mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
976 got_object_qid_free(qid);
981 got_object_id_queue_free(&tree_ids);
985 static const struct got_error *
986 load_commit(int want_meta, struct got_object_idset *idset,
987 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
988 got_cancel_cb cancel_cb, void *cancel_arg)
990 const struct got_error *err;
991 struct got_commit_object *commit;
993 if (got_object_idset_contains(idset, id))
996 if (loose_obj_only) {
998 err = search_packidx(&is_packed, id, repo);
1005 err = got_object_open_as_commit(&commit, repo, id);
1009 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1010 got_object_commit_get_committer_time(commit),
1011 loose_obj_only, repo);
1015 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1016 "", got_object_commit_get_committer_time(commit),
1017 loose_obj_only, repo, cancel_cb, cancel_arg);
1019 got_object_commit_close(commit);
1023 static const struct got_error *
1024 load_tag(int want_meta, struct got_object_idset *idset,
1025 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1026 got_cancel_cb cancel_cb, void *cancel_arg)
1028 const struct got_error *err;
1029 struct got_tag_object *tag = NULL;
1031 if (got_object_idset_contains(idset, id))
1034 if (loose_obj_only) {
1036 err = search_packidx(&is_packed, id, repo);
1043 err = got_object_open_as_tag(&tag, repo, id);
1047 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1048 got_object_tag_get_tagger_time(tag),
1049 loose_obj_only, repo);
1053 switch (got_object_tag_get_object_type(tag)) {
1054 case GOT_OBJ_TYPE_COMMIT:
1055 err = load_commit(want_meta, idset,
1056 got_object_tag_get_object_id(tag), repo,
1057 loose_obj_only, cancel_cb, cancel_arg);
1059 case GOT_OBJ_TYPE_TREE:
1060 err = load_tree(want_meta, idset,
1061 got_object_tag_get_object_id(tag), "",
1062 got_object_tag_get_tagger_time(tag),
1063 loose_obj_only, repo, cancel_cb, cancel_arg);
1070 got_object_tag_close(tag);
1074 enum findtwixt_color {
1079 static const int findtwixt_colors[] = {
1085 static const struct got_error *
1086 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1087 int color, struct got_repository *repo)
1089 const struct got_error *err;
1090 struct got_object_qid *qid;
1092 err = got_object_qid_alloc(&qid, id);
1096 STAILQ_INSERT_TAIL(ids, qid, entry);
1097 qid->data = (void *)&findtwixt_colors[color];
1101 static const struct got_error *
1102 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1103 struct got_object_id *id, struct got_repository *repo,
1104 got_cancel_cb cancel_cb, void *cancel_arg)
1106 const struct got_error *err = NULL;
1107 struct got_commit_object *commit;
1108 const struct got_object_id_queue *parents;
1109 struct got_object_id_queue ids;
1110 struct got_object_qid *qid;
1114 err = got_object_qid_alloc(&qid, id);
1117 STAILQ_INSERT_HEAD(&ids, qid, entry);
1119 while (!STAILQ_EMPTY(&ids)) {
1121 err = (*cancel_cb)(cancel_arg);
1126 qid = STAILQ_FIRST(&ids);
1127 STAILQ_REMOVE_HEAD(&ids, entry);
1129 if (got_object_idset_contains(drop, qid->id)) {
1130 got_object_qid_free(qid);
1134 err = got_object_idset_add(drop, qid->id,
1135 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1137 got_object_qid_free(qid);
1141 if (!got_object_idset_contains(keep, qid->id)) {
1142 got_object_qid_free(qid);
1146 err = got_object_open_as_commit(&commit, repo, qid->id);
1147 got_object_qid_free(qid);
1151 parents = got_object_commit_get_parent_ids(commit);
1153 err = got_object_id_queue_copy(parents, &ids);
1155 got_object_commit_close(commit);
1159 got_object_commit_close(commit);
1162 got_object_id_queue_free(&ids);
1166 struct append_id_arg {
1167 struct got_object_id **array;
1171 static const struct got_error *
1172 append_id(struct got_object_id *id, void *data, void *arg)
1174 struct append_id_arg *a = arg;
1176 a->array[a->idx] = got_object_id_dup(id);
1177 if (a->array[a->idx] == NULL)
1178 return got_error_from_errno("got_object_id_dup");
1184 static const struct got_error *
1185 findtwixt(struct got_object_id ***res, int *nres,
1186 struct got_object_id **head, int nhead,
1187 struct got_object_id **tail, int ntail,
1188 struct got_repository *repo,
1189 got_cancel_cb cancel_cb, void *cancel_arg)
1191 const struct got_error *err = NULL;
1192 struct got_object_id_queue ids;
1193 struct got_object_idset *keep, *drop;
1194 struct got_object_qid *qid;
1195 int i, ncolor, nkeep, obj_type;
1201 keep = got_object_idset_alloc();
1203 return got_error_from_errno("got_object_idset_alloc");
1205 drop = got_object_idset_alloc();
1207 err = got_error_from_errno("got_object_idset_alloc");
1211 for (i = 0; i < nhead; i++) {
1212 struct got_object_id *id = head[i];
1215 err = got_object_get_type(&obj_type, repo, id);
1218 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1220 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1224 for (i = 0; i < ntail; i++) {
1225 struct got_object_id *id = tail[i];
1228 err = got_object_get_type(&obj_type, repo, id);
1231 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1233 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1238 while (!STAILQ_EMPTY(&ids)) {
1240 qid = STAILQ_FIRST(&ids);
1241 qcolor = *((int *)qid->data);
1243 if (got_object_idset_contains(drop, qid->id))
1244 ncolor = COLOR_DROP;
1245 else if (got_object_idset_contains(keep, qid->id))
1246 ncolor = COLOR_KEEP;
1248 ncolor = COLOR_BLANK;
1250 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1251 qcolor == COLOR_KEEP)) {
1252 STAILQ_REMOVE_HEAD(&ids, entry);
1253 got_object_qid_free(qid);
1257 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1258 err = drop_commit(keep, drop, qid->id, repo,
1259 cancel_cb, cancel_arg);
1262 } else if (ncolor == COLOR_BLANK) {
1263 struct got_commit_object *commit;
1264 struct got_object_id *id;
1265 const struct got_object_id_queue *parents;
1266 struct got_object_qid *pid;
1268 id = got_object_id_dup(qid->id);
1270 err = got_error_from_errno("got_object_id_dup");
1273 if (qcolor == COLOR_KEEP)
1274 err = got_object_idset_add(keep, id,
1275 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1277 err = got_object_idset_add(drop, id,
1278 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1284 err = got_object_open_as_commit(&commit, repo, id);
1289 parents = got_object_commit_get_parent_ids(commit);
1291 STAILQ_FOREACH(pid, parents, entry) {
1292 err = queue_commit_id(&ids, pid->id,
1300 got_object_commit_close(commit);
1303 /* should not happen */
1304 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1305 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1309 STAILQ_REMOVE_HEAD(&ids, entry);
1310 got_object_qid_free(qid);
1313 nkeep = got_object_idset_num_elements(keep);
1315 struct append_id_arg arg;
1316 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1317 if (arg.array == NULL) {
1318 err = got_error_from_errno("calloc");
1322 err = got_object_idset_for_each(keep, append_id, &arg);
1331 got_object_idset_free(keep);
1332 got_object_idset_free(drop);
1333 got_object_id_queue_free(&ids);
1337 static const struct got_error *
1338 load_object_ids(struct got_object_idset *idset,
1339 struct got_object_id **theirs, int ntheirs,
1340 struct got_object_id **ours, int nours, struct got_repository *repo,
1341 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
1343 const struct got_error *err = NULL;
1344 struct got_object_id **ids = NULL;
1345 int i, nobj = 0, obj_type;
1347 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1348 cancel_cb, cancel_arg);
1349 if (err || nobj == 0)
1352 for (i = 0; i < ntheirs; i++) {
1353 struct got_object_id *id = theirs[i];
1356 err = got_object_get_type(&obj_type, repo, id);
1359 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1361 err = load_commit(0, idset, id, repo,
1362 loose_obj_only, cancel_cb, cancel_arg);
1367 for (i = 0; i < ntheirs; i++) {
1368 struct got_object_id *id = theirs[i];
1369 struct got_pack_meta *m;
1372 m = got_object_idset_get(idset, id);
1374 err = got_object_get_type(&obj_type, repo, id);
1378 obj_type = m->obj_type;
1379 if (obj_type != GOT_OBJ_TYPE_TAG)
1381 err = load_tag(0, idset, id, repo,
1382 loose_obj_only, cancel_cb, cancel_arg);
1387 for (i = 0; i < nobj; i++) {
1388 err = load_commit(1, idset, ids[i], repo,
1389 loose_obj_only, cancel_cb, cancel_arg);
1394 for (i = 0; i < nours; i++) {
1395 struct got_object_id *id = ours[i];
1396 struct got_pack_meta *m;
1399 m = got_object_idset_get(idset, id);
1401 err = got_object_get_type(&obj_type, repo, id);
1405 obj_type = m->obj_type;
1406 if (obj_type != GOT_OBJ_TYPE_TAG)
1408 err = load_tag(1, idset, id, repo,
1409 loose_obj_only, cancel_cb, cancel_arg);
1414 for (i = 0; i < nobj; i++) {
1421 const struct got_error *
1422 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1426 SHA1Update(ctx, buf, len);
1427 n = fwrite(buf, 1, len, f);
1429 return got_ferror(f, GOT_ERR_IO);
1434 putbe32(char *b, uint32_t n)
1443 write_order_cmp(const void *pa, const void *pb)
1445 struct got_pack_meta *a, *b, *ahd, *bhd;
1447 a = *(struct got_pack_meta **)pa;
1448 b = *(struct got_pack_meta **)pb;
1449 ahd = (a->head == NULL) ? a : a->head;
1450 bhd = (b->head == NULL) ? b : b->head;
1451 if (ahd->mtime != bhd->mtime)
1452 return bhd->mtime - ahd->mtime;
1454 return (uintptr_t)bhd - (uintptr_t)ahd;
1455 if (a->nchain != b->nchain)
1456 return a->nchain - b->nchain;
1457 return a->mtime - b->mtime;
1461 reuse_write_order_cmp(const void *pa, const void *pb)
1463 struct got_pack_meta *a, *b;
1465 a = *(struct got_pack_meta **)pa;
1466 b = *(struct got_pack_meta **)pb;
1468 if (a->reused_delta_offset < b->reused_delta_offset)
1470 if (a->reused_delta_offset > b->reused_delta_offset)
1475 static const struct got_error *
1476 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1482 hdr[0] = obj_type << 4;
1483 hdr[0] |= len & 0xf;
1485 for (i = 1; len != 0; i++){
1487 return got_error(GOT_ERR_NO_SPACE);
1488 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1489 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1490 len >>= GOT_DELTA_SIZE_SHIFT;
1498 packoff(char *hdr, off_t off)
1503 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1504 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1505 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1506 GOT_DELTA_SIZE_MORE;
1511 hdr[j++] = rbuf[--i];
1515 static const struct got_error *
1516 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1517 struct got_pack_meta *m)
1519 const struct got_error *err;
1523 if (m->prev->off != 0) {
1524 err = packhdr(&nh, buf, sizeof(buf),
1525 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1528 nh += packoff(buf + nh, m->off - m->prev->off);
1529 err = hwrite(packfile, buf, nh, ctx);
1532 *packfile_size += nh;
1534 err = packhdr(&nh, buf, sizeof(buf),
1535 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1538 err = hwrite(packfile, buf, nh, ctx);
1541 *packfile_size += nh;
1542 err = hwrite(packfile, m->prev->id.sha1,
1543 sizeof(m->prev->id.sha1), ctx);
1546 *packfile_size += sizeof(m->prev->id.sha1);
1552 static const struct got_error *
1553 write_packed_object(off_t *packfile_size, FILE *packfile,
1554 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1555 SHA1_CTX *ctx, struct got_repository *repo)
1557 const struct got_error *err = NULL;
1558 struct got_deflate_checksum csum;
1561 struct got_raw_object *raw = NULL;
1564 csum.output_sha1 = ctx;
1565 csum.output_crc = NULL;
1567 m->off = ftello(packfile);
1568 if (m->delta_len == 0) {
1569 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1572 err = packhdr(&nh, buf, sizeof(buf),
1573 m->obj_type, raw->size);
1576 err = hwrite(packfile, buf, nh, ctx);
1579 *packfile_size += nh;
1580 if (raw->f == NULL) {
1581 err = got_deflate_to_file_mmap(&outlen,
1582 raw->data + raw->hdrlen, 0, raw->size,
1587 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1589 err = got_error_from_errno("fseeko");
1592 err = got_deflate_to_file(&outlen, raw->f,
1593 raw->size, packfile, &csum);
1597 *packfile_size += outlen;
1598 got_object_raw_close(raw);
1600 } else if (m->delta_buf) {
1601 err = deltahdr(packfile_size, ctx, packfile, m);
1604 err = got_deflate_to_file_mmap(&outlen,
1605 m->delta_buf, 0, m->delta_len, packfile, &csum);
1608 *packfile_size += outlen;
1610 m->delta_buf = NULL;
1612 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1614 err = got_error_from_errno("fseeko");
1617 err = deltahdr(packfile_size, ctx, packfile, m);
1620 err = got_deflate_to_file(&outlen, delta_cache,
1621 m->delta_len, packfile, &csum);
1624 *packfile_size += outlen;
1628 got_object_raw_close(raw);
1632 static const struct got_error *
1633 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1634 struct got_pack_meta **deltify, int ndeltify,
1635 struct got_pack_meta **reuse, int nreuse,
1636 int nours, struct got_repository *repo,
1637 got_pack_progress_cb progress_cb, void *progress_arg,
1638 struct got_ratelimit *rl,
1639 got_cancel_cb cancel_cb, void *cancel_arg)
1641 const struct got_error *err = NULL;
1644 struct got_pack_meta *m;
1647 off_t packfile_size = 0;
1652 err = hwrite(packfile, "PACK", 4, &ctx);
1655 putbe32(buf, GOT_PACKFILE_VERSION);
1656 err = hwrite(packfile, buf, 4, &ctx);
1659 putbe32(buf, ndeltify + nreuse);
1660 err = hwrite(packfile, buf, 4, &ctx);
1664 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1666 for (i = 0; i < ndeltify; i++) {
1667 err = report_progress(progress_cb, progress_arg, rl,
1668 packfile_size, nours, ndeltify + nreuse,
1669 ndeltify + nreuse, i);
1673 err = write_packed_object(&packfile_size, packfile,
1674 delta_cache, m, &outfd, &ctx, repo);
1679 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1680 reuse_write_order_cmp);
1681 for (i = 0; i < nreuse; i++) {
1682 err = report_progress(progress_cb, progress_arg, rl,
1683 packfile_size, nours, ndeltify + nreuse,
1684 ndeltify + nreuse, ndeltify + i);
1688 err = write_packed_object(&packfile_size, packfile,
1689 delta_cache, m, &outfd, &ctx, repo);
1694 SHA1Final(pack_sha1, &ctx);
1695 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1696 if (n != SHA1_DIGEST_LENGTH)
1697 err = got_ferror(packfile, GOT_ERR_IO);
1698 packfile_size += SHA1_DIGEST_LENGTH;
1699 packfile_size += sizeof(struct got_packfile_hdr);
1701 err = progress_cb(progress_arg, packfile_size, nours,
1702 ndeltify + nreuse, ndeltify + nreuse,
1708 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1709 err = got_error_from_errno("close");
1713 static const struct got_error *
1714 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1716 struct got_object_idset *idset = arg;
1718 if (got_object_idset_get_element_data(entry) == NULL)
1719 got_object_idset_remove_element(idset, entry);
1724 static const struct got_error *
1725 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1727 struct got_object_idset *idset = arg;
1728 struct got_pack_meta *m;
1730 m = got_object_idset_get_element_data(entry);
1731 if (m->have_reused_delta)
1732 got_object_idset_remove_element(idset, entry);
1737 static const struct got_error *
1738 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1740 struct got_pack_meta *m = data;
1741 struct got_pack_metavec *v = arg;
1743 return add_meta(m, v);
1746 const struct got_error *
1747 got_pack_create(uint8_t *packsha1, FILE *packfile,
1748 struct got_object_id **theirs, int ntheirs,
1749 struct got_object_id **ours, int nours,
1750 struct got_repository *repo, int loose_obj_only, int allow_empty,
1751 got_pack_progress_cb progress_cb, void *progress_arg,
1752 got_cancel_cb cancel_cb, void *cancel_arg)
1754 const struct got_error *err;
1755 int delta_cache_fd = -1;
1756 FILE *delta_cache = NULL;
1757 struct got_object_idset *idset;
1758 struct got_ratelimit rl;
1759 struct got_pack_metavec deltify, reuse;
1761 memset(&deltify, 0, sizeof(deltify));
1762 memset(&reuse, 0, sizeof(reuse));
1764 got_ratelimit_init(&rl, 0, 500);
1766 idset = got_object_idset_alloc();
1768 return got_error_from_errno("got_object_idset_alloc");
1770 err = load_object_ids(idset, theirs, ntheirs, ours, nours,
1771 repo, loose_obj_only, cancel_cb, cancel_arg);
1775 err = got_object_idset_for_each_element(idset,
1776 remove_unused_object, idset);
1781 err = progress_cb(progress_arg, 0L, nours,
1782 got_object_idset_num_elements(idset), 0, 0);
1787 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1788 err = got_error(GOT_ERR_CANNOT_PACK);
1792 delta_cache_fd = got_opentempfd();
1793 if (delta_cache_fd == -1) {
1794 err = got_error_from_errno("got_opentemp");
1799 reuse.meta = calloc(reuse.metasz,
1800 sizeof(struct got_pack_meta *));
1801 if (reuse.meta == NULL) {
1802 err = got_error_from_errno("calloc");
1806 err = search_deltas(&reuse, idset, delta_cache_fd, nours, repo,
1807 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1810 if (reuse.nmeta > 0) {
1811 err = got_object_idset_for_each_element(idset,
1812 remove_reused_object, idset);
1817 delta_cache = fdopen(delta_cache_fd, "a+");
1818 if (delta_cache == NULL) {
1819 err = got_error_from_errno("fdopen");
1822 delta_cache_fd = -1;
1824 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1825 err = got_error_from_errno("fseeko");
1829 deltify.meta = calloc(got_object_idset_num_elements(idset),
1830 sizeof(struct got_pack_meta *));
1831 if (deltify.meta == NULL) {
1832 err = got_error_from_errno("calloc");
1835 deltify.metasz = got_object_idset_num_elements(idset);
1837 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1840 if (deltify.nmeta > 0) {
1841 err = pick_deltas(deltify.meta, deltify.nmeta, nours,
1842 reuse.nmeta, delta_cache, repo,
1843 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1846 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1847 err = got_error_from_errno("fseeko");
1852 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1853 deltify.nmeta, reuse.meta, reuse.nmeta, nours, repo,
1854 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1858 free_nmeta(deltify.meta, deltify.nmeta);
1859 free_nmeta(reuse.meta, reuse.nmeta);
1860 got_object_idset_free(idset);
1861 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1862 err = got_error_from_errno("close");
1863 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1864 err = got_error_from_errno("fclose");