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>
19 #include <sys/queue.h>
36 #include "got_error.h"
37 #include "got_cancel.h"
38 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository_admin.h"
42 #include "got_opentemp.h"
44 #include "got_lib_deltify.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_idset.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_deflate.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_ratelimit.h"
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
63 struct got_pack_meta {
64 struct got_object_id id;
70 /* The best delta we picked */
71 struct got_pack_meta *head;
72 struct got_pack_meta *prev;
73 unsigned char *delta_buf; /* if not encoded in delta cache file */
74 off_t delta_offset; /* offset in delta cache file */
75 off_t delta_len; /* encoded delta length */
78 int have_reused_delta;
79 off_t reused_delta_offset; /* offset of delta in reused pack file */
80 struct got_object_id *base_obj_id;
82 /* Only used for delta window */
83 struct got_delta_table *dtab;
85 /* Only used for writing offset deltas */
89 struct got_pack_metavec {
90 struct got_pack_meta **meta;
95 static const struct got_error *
96 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
97 const char *path, int obj_type, time_t mtime)
99 const struct got_error *err = NULL;
100 struct got_pack_meta *m;
104 m = calloc(1, sizeof(*m));
106 return got_error_from_errno("calloc");
108 memcpy(&m->id, id, sizeof(m->id));
110 m->path = strdup(path);
111 if (m->path == NULL) {
112 err = got_error_from_errno("strdup");
117 m->obj_type = obj_type;
124 clear_meta(struct got_pack_meta *meta)
130 free(meta->delta_buf);
131 meta->delta_buf = NULL;
132 free(meta->base_obj_id);
133 meta->base_obj_id = NULL;
137 free_nmeta(struct got_pack_meta **meta, int nmeta)
141 for (i = 0; i < nmeta; i++)
147 delta_order_cmp(const void *pa, const void *pb)
149 struct got_pack_meta *a, *b;
152 a = *(struct got_pack_meta **)pa;
153 b = *(struct got_pack_meta **)pb;
155 if (a->obj_type != b->obj_type)
156 return a->obj_type - b->obj_type;
157 cmp = strcmp(a->path, b->path);
160 if (a->mtime != b->mtime)
161 return a->mtime - b->mtime;
162 return got_object_id_cmp(&a->id, &b->id);
166 delta_size(struct got_delta_instruction *deltas, int ndeltas)
170 for (i = 0; i < ndeltas; i++) {
172 size += GOT_DELTA_SIZE_SHIFT;
174 size += deltas[i].len + 1;
179 static const struct got_error *
180 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
184 if (*len + nseg >= *sz) {
185 while (*len + nseg >= *sz)
187 n = realloc(*p, *sz);
189 return got_error_from_errno("realloc");
192 memcpy(*p + *len, seg, nseg);
197 static const struct got_error *
198 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
199 struct got_delta_instruction *deltas, int ndeltas,
200 off_t delta_size, off_t base_size)
202 const struct got_error *err;
203 unsigned char buf[16], *bp;
207 struct got_delta_instruction *d;
209 m->delta_buf = malloc(delta_size);
210 if (m->delta_buf == NULL)
211 return got_error_from_errno("calloc");
213 /* base object size */
214 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
215 n = base_size >> GOT_DELTA_SIZE_SHIFT;
216 for (i = 1; n > 0; i++) {
217 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
218 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
219 n >>= GOT_DELTA_SIZE_SHIFT;
221 err = append(&m->delta_buf, &len, &delta_size, buf, i);
225 /* target object size */
226 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
227 n = o->size >> GOT_DELTA_SIZE_SHIFT;
228 for (i = 1; n > 0; i++) {
229 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
230 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
231 n >>= GOT_DELTA_SIZE_SHIFT;
233 err = append(&m->delta_buf, &len, &delta_size, buf, i);
237 for (j = 0; j < ndeltas; j++) {
242 buf[0] = GOT_DELTA_BASE_COPY;
243 for (i = 0; i < 4; i++) {
244 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
253 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
254 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
255 for (i = 0; i < 3 && n > 0; i++) {
256 buf[0] |= 1 << (i + 4);
261 err = append(&m->delta_buf, &len, &delta_size,
265 } else if (o->f == NULL) {
267 while (n != d->len) {
268 buf[0] = (d->len - n < 127) ? d->len - n : 127;
269 err = append(&m->delta_buf, &len, &delta_size,
273 err = append(&m->delta_buf, &len, &delta_size,
274 o->data + o->hdrlen + d->offset + n,
283 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
284 return got_error_from_errno("fseeko");
286 while (n != d->len) {
287 buf[0] = (d->len - n < 127) ? d->len - n : 127;
288 err = append(&m->delta_buf, &len, &delta_size,
292 r = fread(content, 1, buf[0], o->f);
294 return got_ferror(o->f, GOT_ERR_IO);
295 err = append(&m->delta_buf, &len, &delta_size,
308 static const struct got_error *
309 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
310 struct got_delta_instruction *deltas, int ndeltas,
311 off_t base_size, FILE *f)
313 unsigned char buf[16], *bp;
317 struct got_delta_instruction *d;
319 /* base object size */
320 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
321 n = base_size >> GOT_DELTA_SIZE_SHIFT;
322 for (i = 1; n > 0; i++) {
323 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
324 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
325 n >>= GOT_DELTA_SIZE_SHIFT;
327 w = fwrite(buf, 1, i, f);
329 return got_ferror(f, GOT_ERR_IO);
331 /* target object size */
332 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
333 n = o->size >> GOT_DELTA_SIZE_SHIFT;
334 for (i = 1; n > 0; i++) {
335 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
336 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
337 n >>= GOT_DELTA_SIZE_SHIFT;
339 w = fwrite(buf, 1, i, f);
341 return got_ferror(f, GOT_ERR_IO);
343 for (j = 0; j < ndeltas; j++) {
348 buf[0] = GOT_DELTA_BASE_COPY;
349 for (i = 0; i < 4; i++) {
350 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
359 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
360 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
361 for (i = 0; i < 3 && n > 0; i++) {
362 buf[0] |= 1 << (i + 4);
367 w = fwrite(buf, 1, bp - buf, f);
369 return got_ferror(f, GOT_ERR_IO);
370 } else if (o->f == NULL) {
372 while (n != d->len) {
373 buf[0] = (d->len - n < 127) ? d->len - n : 127;
374 w = fwrite(buf, 1, 1, f);
376 return got_ferror(f, GOT_ERR_IO);
377 w = fwrite(o->data + o->hdrlen + d->offset + n,
380 return got_ferror(f, GOT_ERR_IO);
386 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
387 return got_error_from_errno("fseeko");
389 while (n != d->len) {
390 buf[0] = (d->len - n < 127) ? d->len - n : 127;
391 w = fwrite(buf, 1, 1, f);
393 return got_ferror(f, GOT_ERR_IO);
394 r = fread(content, 1, buf[0], o->f);
396 return got_ferror(o->f, GOT_ERR_IO);
397 w = fwrite(content, 1, buf[0], f);
399 return got_ferror(f, GOT_ERR_IO);
405 m->delta_len = ftello(f) - m->delta_offset;
409 static const struct got_error *
410 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
411 struct got_ratelimit *rl, off_t packfile_size, int ncommits,
412 int nobj_total, int obj_deltify, int nobj_written)
414 const struct got_error *err;
417 if (progress_cb == NULL)
420 err = got_ratelimit_check(&elapsed, rl);
424 return progress_cb(progress_arg, packfile_size, ncommits,
425 nobj_total, obj_deltify, nobj_written);
428 static const struct got_error *
429 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
431 if (v->nmeta == v->metasz){
432 size_t newsize = 2 * v->metasz;
433 struct got_pack_meta **new;
434 new = reallocarray(v->meta, newsize, sizeof(*new));
436 return got_error_from_errno("reallocarray");
441 v->meta[v->nmeta++] = m;
445 static const struct got_error *
446 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
447 struct got_object_idset *idset, struct got_pack *pack,
448 struct got_packidx *packidx, int delta_cache_fd,
449 struct got_repository *repo)
451 const struct got_error *err = NULL;
452 struct got_pack_meta *base = NULL;
453 struct got_object_id *base_obj_id = NULL;
454 off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
455 uint64_t base_size, result_size;
457 if (m->have_reused_delta)
460 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
461 &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
462 packidx, idx, &m->id, repo);
466 if (delta_offset + delta_len < delta_offset)
467 return got_error(GOT_ERR_BAD_PACKFILE);
469 base = got_object_idset_get(idset, base_obj_id);
473 m->delta_len = delta_len;
474 m->delta_offset = delta_cache_offset;
476 m->size = result_size;
477 m->have_reused_delta = 1;
478 m->reused_delta_offset = delta_offset;
479 m->base_obj_id = base_obj_id;
481 err = add_meta(m, v);
487 static const struct got_error *
488 find_pack_for_reuse(struct got_packidx **best_packidx,
489 struct got_repository *repo)
491 const struct got_error *err = NULL;
492 struct got_pathlist_entry *pe;
493 const char *best_packidx_path = NULL;
496 *best_packidx = NULL;
498 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
499 const char *path_packidx = pe->path;
500 struct got_packidx *packidx;
503 err = got_repo_get_packidx(&packidx, path_packidx, repo);
507 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
508 if (nobj > nobj_max) {
509 best_packidx_path = path_packidx;
514 if (best_packidx_path) {
515 err = got_repo_get_packidx(best_packidx, best_packidx_path,
522 struct search_deltas_arg {
523 struct got_packidx *packidx;
524 struct got_pack *pack;
525 struct got_object_idset *idset;
526 struct got_pack_metavec *v;
528 struct got_repository *repo;
529 got_pack_progress_cb progress_cb;
531 struct got_ratelimit *rl;
532 got_cancel_cb cancel_cb;
537 static const struct got_error *
538 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
540 const struct got_error *err;
541 struct got_pack_meta *m = data;
542 struct search_deltas_arg *a = arg;
544 struct got_object *obj = NULL;
547 err = (*a->cancel_cb)(a->cancel_arg);
552 if (!got_repo_check_packidx_bloom_filter(a->repo,
553 a->packidx->path_packidx, id))
556 obj_idx = got_packidx_get_object_idx(a->packidx, id);
561 * Opening and closing an object just to check its flags
562 * is a bit expensive. We could have an imsg which requests
563 * plain type/size information for an object without doing
564 * work such as traversing the object's entire delta chain
565 * to find the base object type, and other such info which
566 * we don't really need here.
568 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
569 a->packidx, obj_idx, a->repo);
573 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
574 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
575 a->delta_cache_fd, a->repo);
578 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
579 0L, a->ncommits, got_object_idset_num_elements(a->idset),
583 got_object_close(obj);
587 static const struct got_error *
588 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
589 int delta_cache_fd, int ncommits, struct got_repository *repo,
590 got_pack_progress_cb progress_cb, void *progress_arg,
591 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
593 const struct got_error *err = NULL;
594 char *path_packfile = NULL;
595 struct got_packidx *packidx;
596 struct got_pack *pack;
597 struct search_deltas_arg sda;
599 err = find_pack_for_reuse(&packidx, repo);
606 err = got_packidx_get_packfile_path(&path_packfile,
607 packidx->path_packidx);
611 pack = got_repo_get_cached_pack(repo, path_packfile);
613 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
618 sda.packidx = packidx;
622 sda.delta_cache_fd = delta_cache_fd;
624 sda.progress_cb = progress_cb;
625 sda.progress_arg = progress_arg;
627 sda.cancel_cb = cancel_cb;
628 sda.cancel_arg = cancel_arg;
629 sda.ncommits = ncommits;
630 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
636 static const struct got_error *
637 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncommits,
638 int nreused, FILE *delta_cache, struct got_repository *repo,
639 got_pack_progress_cb progress_cb, void *progress_arg,
640 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
642 const struct got_error *err = NULL;
643 struct got_pack_meta *m = NULL, *base = NULL;
644 struct got_raw_object *raw = NULL, *base_raw = NULL;
645 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
646 int i, j, ndeltas, best_ndeltas;
647 off_t size, best_size;
648 const int max_base_candidates = 3;
649 size_t delta_memsize = 0;
650 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
653 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
654 for (i = 0; i < nmeta; i++) {
656 err = (*cancel_cb)(cancel_arg);
660 err = report_progress(progress_cb, progress_arg, rl,
661 0L, ncommits, nreused + nmeta, nreused + i, 0);
666 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
667 m->obj_type == GOT_OBJ_TYPE_TAG)
670 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
675 if (raw->f == NULL) {
676 err = got_deltify_init_mem(&m->dtab, raw->data,
677 raw->hdrlen, raw->size + raw->hdrlen);
679 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
680 raw->size + raw->hdrlen);
685 if (i > max_base_candidates) {
686 struct got_pack_meta *n = NULL;
687 n = meta[i - (max_base_candidates + 1)];
688 got_deltify_free(n->dtab);
692 best_size = raw->size;
694 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
696 err = (*cancel_cb)(cancel_arg);
701 /* long chains make unpacking slow, avoid such bases */
702 if (base->nchain >= 128 ||
703 base->obj_type != m->obj_type)
706 err = got_object_raw_open(&base_raw, &outfd, repo,
711 if (raw->f == NULL && base_raw->f == NULL) {
712 err = got_deltify_mem_mem(&deltas, &ndeltas,
713 raw->data, raw->hdrlen,
714 raw->size + raw->hdrlen,
715 base->dtab, base_raw->data,
717 base_raw->size + base_raw->hdrlen);
718 } else if (raw->f == NULL) {
719 err = got_deltify_mem_file(&deltas, &ndeltas,
720 raw->data, raw->hdrlen,
721 raw->size + raw->hdrlen,
722 base->dtab, base_raw->f,
724 base_raw->size + base_raw->hdrlen);
725 } else if (base_raw->f == NULL) {
726 err = got_deltify_file_mem(&deltas, &ndeltas,
728 raw->size + raw->hdrlen,
729 base->dtab, base_raw->data,
731 base_raw->size + base_raw->hdrlen);
733 err = got_deltify(&deltas, &ndeltas,
735 raw->size + raw->hdrlen,
736 base->dtab, base_raw->f, base_raw->hdrlen,
737 base_raw->size + base_raw->hdrlen);
739 got_object_raw_close(base_raw);
744 size = delta_size(deltas, ndeltas);
745 if (size + 32 < best_size){
747 * if we already picked a best delta,
752 best_deltas = deltas;
753 best_ndeltas = ndeltas;
755 m->nchain = base->nchain + 1;
757 m->head = base->head;
767 if (best_ndeltas > 0) {
768 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
769 delta_memsize + best_size <= max_delta_memsize) {
770 delta_memsize += best_size;
771 err = encode_delta_in_mem(m, raw, best_deltas,
772 best_ndeltas, best_size, m->prev->size);
774 m->delta_offset = ftello(delta_cache);
777 * Storing compressed delta data in the delta
778 * cache file would probably be more efficient
779 * than writing uncompressed delta data here
780 * and compressing it while writing the pack
781 * file. This would also allow for reusing
782 * deltas in their compressed form.
784 err = encode_delta(m, raw, best_deltas,
785 best_ndeltas, m->prev->size, delta_cache);
794 got_object_raw_close(raw);
798 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
799 got_deltify_free(meta[i]->dtab);
800 meta[i]->dtab = NULL;
803 got_object_raw_close(raw);
805 got_object_raw_close(base_raw);
806 if (outfd != -1 && close(outfd) == -1 && err == NULL)
807 err = got_error_from_errno("close");
813 static const struct got_error *
814 search_packidx(int *found, struct got_object_id *id,
815 struct got_repository *repo)
817 const struct got_error *err = NULL;
818 struct got_packidx *packidx = NULL;
823 err = got_repo_search_packidx(&packidx, &idx, repo, id);
825 *found = 1; /* object is already packed */
826 else if (err->code == GOT_ERR_NO_OBJ)
831 static const int obj_types[] = {
837 GOT_OBJ_TYPE_OFFSET_DELTA,
838 GOT_OBJ_TYPE_REF_DELTA
841 static const struct got_error *
842 add_object(int want_meta, struct got_object_idset *idset,
843 struct got_object_id *id, const char *path, int obj_type,
844 time_t mtime, int loose_obj_only, struct got_repository *repo)
846 const struct got_error *err;
847 struct got_pack_meta *m = NULL;
849 if (loose_obj_only) {
851 err = search_packidx(&is_packed, id, repo);
859 err = alloc_meta(&m, id, path, obj_type, mtime);
864 return got_object_idset_add(idset, id, m);
867 static const struct got_error *
868 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
869 struct got_object_idset *idset, struct got_object_id *tree_id,
870 const char *dpath, time_t mtime, struct got_repository *repo,
871 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
873 const struct got_error *err;
874 struct got_tree_object *tree;
878 err = got_object_open_as_tree(&tree, repo, tree_id);
882 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
883 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
884 struct got_object_id *id = got_tree_entry_get_id(e);
885 mode_t mode = got_tree_entry_get_mode(e);
888 err = (*cancel_cb)(cancel_arg);
893 if (got_object_tree_entry_is_submodule(e) ||
894 got_object_idset_contains(idset, id))
897 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
898 got_tree_entry_get_name(e)) == -1) {
899 err = got_error_from_errno("asprintf");
904 struct got_object_qid *qid;
905 err = got_object_qid_alloc(&qid, id);
908 STAILQ_INSERT_TAIL(ids, qid, entry);
909 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
910 err = add_object(want_meta, idset, id, p,
911 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo);
919 got_object_tree_close(tree);
924 static const struct got_error *
925 load_tree(int want_meta, struct got_object_idset *idset,
926 struct got_object_id *tree_id, const char *dpath, time_t mtime,
927 int loose_obj_only, struct got_repository *repo,
928 got_cancel_cb cancel_cb, void *cancel_arg)
930 const struct got_error *err = NULL;
931 struct got_object_id_queue tree_ids;
932 struct got_object_qid *qid;
934 if (got_object_idset_contains(idset, tree_id))
937 err = got_object_qid_alloc(&qid, tree_id);
941 STAILQ_INIT(&tree_ids);
942 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
944 while (!STAILQ_EMPTY(&tree_ids)) {
946 err = (*cancel_cb)(cancel_arg);
951 qid = STAILQ_FIRST(&tree_ids);
952 STAILQ_REMOVE_HEAD(&tree_ids, entry);
954 if (got_object_idset_contains(idset, qid->id)) {
955 got_object_qid_free(qid);
959 err = add_object(want_meta, idset, qid->id, dpath,
960 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo);
962 got_object_qid_free(qid);
966 err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
967 dpath, mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
968 got_object_qid_free(qid);
973 got_object_id_queue_free(&tree_ids);
977 static const struct got_error *
978 load_commit(int want_meta, struct got_object_idset *idset,
979 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
980 got_cancel_cb cancel_cb, void *cancel_arg)
982 const struct got_error *err;
983 struct got_commit_object *commit;
985 if (got_object_idset_contains(idset, id))
988 if (loose_obj_only) {
990 err = search_packidx(&is_packed, id, repo);
997 err = got_object_open_as_commit(&commit, repo, id);
1001 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1002 got_object_commit_get_committer_time(commit),
1003 loose_obj_only, repo);
1007 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1008 "", got_object_commit_get_committer_time(commit),
1009 loose_obj_only, repo, cancel_cb, cancel_arg);
1011 got_object_commit_close(commit);
1015 static const struct got_error *
1016 load_tag(int want_meta, struct got_object_idset *idset,
1017 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1018 got_cancel_cb cancel_cb, void *cancel_arg)
1020 const struct got_error *err;
1021 struct got_tag_object *tag = NULL;
1023 if (got_object_idset_contains(idset, id))
1026 if (loose_obj_only) {
1028 err = search_packidx(&is_packed, id, repo);
1035 err = got_object_open_as_tag(&tag, repo, id);
1039 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1040 got_object_tag_get_tagger_time(tag),
1041 loose_obj_only, repo);
1045 switch (got_object_tag_get_object_type(tag)) {
1046 case GOT_OBJ_TYPE_COMMIT:
1047 err = load_commit(want_meta, idset,
1048 got_object_tag_get_object_id(tag), repo,
1049 loose_obj_only, cancel_cb, cancel_arg);
1051 case GOT_OBJ_TYPE_TREE:
1052 err = load_tree(want_meta, idset,
1053 got_object_tag_get_object_id(tag), "",
1054 got_object_tag_get_tagger_time(tag),
1055 loose_obj_only, repo, cancel_cb, cancel_arg);
1062 got_object_tag_close(tag);
1066 enum findtwixt_color {
1071 static const int findtwixt_colors[] = {
1077 static const struct got_error *
1078 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1079 int color, struct got_repository *repo)
1081 const struct got_error *err;
1082 struct got_object_qid *qid;
1084 err = got_object_qid_alloc(&qid, id);
1088 STAILQ_INSERT_TAIL(ids, qid, entry);
1089 qid->data = (void *)&findtwixt_colors[color];
1093 static const struct got_error *
1094 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1095 struct got_object_id *id, struct got_repository *repo,
1096 got_cancel_cb cancel_cb, void *cancel_arg)
1098 const struct got_error *err = NULL;
1099 struct got_commit_object *commit;
1100 const struct got_object_id_queue *parents;
1101 struct got_object_id_queue ids;
1102 struct got_object_qid *qid;
1106 err = got_object_qid_alloc(&qid, id);
1109 STAILQ_INSERT_HEAD(&ids, qid, entry);
1111 while (!STAILQ_EMPTY(&ids)) {
1113 err = (*cancel_cb)(cancel_arg);
1118 qid = STAILQ_FIRST(&ids);
1119 STAILQ_REMOVE_HEAD(&ids, entry);
1121 if (got_object_idset_contains(drop, qid->id)) {
1122 got_object_qid_free(qid);
1126 err = got_object_idset_add(drop, qid->id,
1127 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1129 got_object_qid_free(qid);
1133 if (!got_object_idset_contains(keep, qid->id)) {
1134 got_object_qid_free(qid);
1138 err = got_object_open_as_commit(&commit, repo, qid->id);
1139 got_object_qid_free(qid);
1143 parents = got_object_commit_get_parent_ids(commit);
1145 err = got_object_id_queue_copy(parents, &ids);
1147 got_object_commit_close(commit);
1151 got_object_commit_close(commit);
1154 got_object_id_queue_free(&ids);
1158 struct append_id_arg {
1159 struct got_object_id **array;
1163 static const struct got_error *
1164 append_id(struct got_object_id *id, void *data, void *arg)
1166 struct append_id_arg *a = arg;
1168 a->array[a->idx] = got_object_id_dup(id);
1169 if (a->array[a->idx] == NULL)
1170 return got_error_from_errno("got_object_id_dup");
1176 static const struct got_error *
1177 findtwixt(struct got_object_id ***res, int *nres,
1178 struct got_object_id **head, int nhead,
1179 struct got_object_id **tail, int ntail,
1180 struct got_repository *repo,
1181 got_cancel_cb cancel_cb, void *cancel_arg)
1183 const struct got_error *err = NULL;
1184 struct got_object_id_queue ids;
1185 struct got_object_idset *keep, *drop;
1186 struct got_object_qid *qid;
1187 int i, ncolor, nkeep, obj_type;
1193 keep = got_object_idset_alloc();
1195 return got_error_from_errno("got_object_idset_alloc");
1197 drop = got_object_idset_alloc();
1199 err = got_error_from_errno("got_object_idset_alloc");
1203 for (i = 0; i < nhead; i++) {
1204 struct got_object_id *id = head[i];
1207 err = got_object_get_type(&obj_type, repo, id);
1210 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1212 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1216 for (i = 0; i < ntail; i++) {
1217 struct got_object_id *id = tail[i];
1220 err = got_object_get_type(&obj_type, repo, id);
1223 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1225 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1230 while (!STAILQ_EMPTY(&ids)) {
1232 qid = STAILQ_FIRST(&ids);
1233 qcolor = *((int *)qid->data);
1235 if (got_object_idset_contains(drop, qid->id))
1236 ncolor = COLOR_DROP;
1237 else if (got_object_idset_contains(keep, qid->id))
1238 ncolor = COLOR_KEEP;
1240 ncolor = COLOR_BLANK;
1242 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1243 qcolor == COLOR_KEEP)) {
1244 STAILQ_REMOVE_HEAD(&ids, entry);
1245 got_object_qid_free(qid);
1249 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1250 err = drop_commit(keep, drop, qid->id, repo,
1251 cancel_cb, cancel_arg);
1254 } else if (ncolor == COLOR_BLANK) {
1255 struct got_commit_object *commit;
1256 struct got_object_id *id;
1257 const struct got_object_id_queue *parents;
1258 struct got_object_qid *pid;
1260 id = got_object_id_dup(qid->id);
1262 err = got_error_from_errno("got_object_id_dup");
1265 if (qcolor == COLOR_KEEP)
1266 err = got_object_idset_add(keep, id,
1267 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1269 err = got_object_idset_add(drop, id,
1270 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1276 err = got_object_open_as_commit(&commit, repo, id);
1281 parents = got_object_commit_get_parent_ids(commit);
1283 STAILQ_FOREACH(pid, parents, entry) {
1284 err = queue_commit_id(&ids, pid->id,
1292 got_object_commit_close(commit);
1295 /* should not happen */
1296 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1297 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1301 STAILQ_REMOVE_HEAD(&ids, entry);
1302 got_object_qid_free(qid);
1305 nkeep = got_object_idset_num_elements(keep);
1307 struct append_id_arg arg;
1308 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1309 if (arg.array == NULL) {
1310 err = got_error_from_errno("calloc");
1314 err = got_object_idset_for_each(keep, append_id, &arg);
1323 got_object_idset_free(keep);
1324 got_object_idset_free(drop);
1325 got_object_id_queue_free(&ids);
1329 static const struct got_error *
1330 load_object_ids(struct got_object_idset *idset,
1331 struct got_object_id **theirs, int ntheirs,
1332 struct got_object_id **ours, int nours, struct got_repository *repo,
1333 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
1335 const struct got_error *err = NULL;
1336 struct got_object_id **ids = NULL;
1337 int i, nobj = 0, obj_type;
1339 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1340 cancel_cb, cancel_arg);
1341 if (err || nobj == 0)
1344 for (i = 0; i < ntheirs; i++) {
1345 struct got_object_id *id = theirs[i];
1348 err = got_object_get_type(&obj_type, repo, id);
1351 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1353 err = load_commit(0, idset, id, repo,
1354 loose_obj_only, cancel_cb, cancel_arg);
1359 for (i = 0; i < ntheirs; i++) {
1360 struct got_object_id *id = theirs[i];
1361 struct got_pack_meta *m;
1364 m = got_object_idset_get(idset, id);
1366 err = got_object_get_type(&obj_type, repo, id);
1370 obj_type = m->obj_type;
1371 if (obj_type != GOT_OBJ_TYPE_TAG)
1373 err = load_tag(0, idset, id, repo,
1374 loose_obj_only, cancel_cb, cancel_arg);
1379 for (i = 0; i < nobj; i++) {
1380 err = load_commit(1, idset, ids[i], repo,
1381 loose_obj_only, cancel_cb, cancel_arg);
1386 for (i = 0; i < nours; i++) {
1387 struct got_object_id *id = ours[i];
1388 struct got_pack_meta *m;
1391 m = got_object_idset_get(idset, id);
1393 err = got_object_get_type(&obj_type, repo, id);
1397 obj_type = m->obj_type;
1398 if (obj_type != GOT_OBJ_TYPE_TAG)
1400 err = load_tag(1, idset, id, repo,
1401 loose_obj_only, cancel_cb, cancel_arg);
1406 for (i = 0; i < nobj; i++) {
1413 const struct got_error *
1414 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1418 SHA1Update(ctx, buf, len);
1419 n = fwrite(buf, 1, len, f);
1421 return got_ferror(f, GOT_ERR_IO);
1426 putbe32(char *b, uint32_t n)
1435 write_order_cmp(const void *pa, const void *pb)
1437 struct got_pack_meta *a, *b, *ahd, *bhd;
1439 a = *(struct got_pack_meta **)pa;
1440 b = *(struct got_pack_meta **)pb;
1441 ahd = (a->head == NULL) ? a : a->head;
1442 bhd = (b->head == NULL) ? b : b->head;
1443 if (ahd->mtime != bhd->mtime)
1444 return bhd->mtime - ahd->mtime;
1446 return (uintptr_t)bhd - (uintptr_t)ahd;
1447 if (a->nchain != b->nchain)
1448 return a->nchain - b->nchain;
1449 return a->mtime - b->mtime;
1453 reuse_write_order_cmp(const void *pa, const void *pb)
1455 struct got_pack_meta *a, *b;
1457 a = *(struct got_pack_meta **)pa;
1458 b = *(struct got_pack_meta **)pb;
1460 if (a->reused_delta_offset < b->reused_delta_offset)
1462 if (a->reused_delta_offset > b->reused_delta_offset)
1467 static const struct got_error *
1468 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1474 hdr[0] = obj_type << 4;
1475 hdr[0] |= len & 0xf;
1477 for (i = 1; len != 0; i++){
1479 return got_error(GOT_ERR_NO_SPACE);
1480 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1481 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1482 len >>= GOT_DELTA_SIZE_SHIFT;
1490 packoff(char *hdr, off_t off)
1495 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1496 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1497 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1498 GOT_DELTA_SIZE_MORE;
1503 hdr[j++] = rbuf[--i];
1507 static const struct got_error *
1508 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1509 struct got_pack_meta *m)
1511 const struct got_error *err;
1515 if (m->prev->off != 0) {
1516 err = packhdr(&nh, buf, sizeof(buf),
1517 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1520 nh += packoff(buf + nh, m->off - m->prev->off);
1521 err = hwrite(packfile, buf, nh, ctx);
1524 *packfile_size += nh;
1526 err = packhdr(&nh, buf, sizeof(buf),
1527 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1530 err = hwrite(packfile, buf, nh, ctx);
1533 *packfile_size += nh;
1534 err = hwrite(packfile, m->prev->id.sha1,
1535 sizeof(m->prev->id.sha1), ctx);
1538 *packfile_size += sizeof(m->prev->id.sha1);
1544 static const struct got_error *
1545 write_packed_object(off_t *packfile_size, FILE *packfile,
1546 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1547 SHA1_CTX *ctx, struct got_repository *repo)
1549 const struct got_error *err = NULL;
1550 struct got_deflate_checksum csum;
1553 struct got_raw_object *raw = NULL;
1556 csum.output_sha1 = ctx;
1557 csum.output_crc = NULL;
1559 m->off = ftello(packfile);
1560 if (m->delta_len == 0) {
1561 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1564 err = packhdr(&nh, buf, sizeof(buf),
1565 m->obj_type, raw->size);
1568 err = hwrite(packfile, buf, nh, ctx);
1571 *packfile_size += nh;
1572 if (raw->f == NULL) {
1573 err = got_deflate_to_file_mmap(&outlen,
1574 raw->data + raw->hdrlen, 0, raw->size,
1579 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1581 err = got_error_from_errno("fseeko");
1584 err = got_deflate_to_file(&outlen, raw->f,
1585 raw->size, packfile, &csum);
1589 *packfile_size += outlen;
1590 got_object_raw_close(raw);
1592 } else if (m->delta_buf) {
1593 err = deltahdr(packfile_size, ctx, packfile, m);
1596 err = got_deflate_to_file_mmap(&outlen,
1597 m->delta_buf, 0, m->delta_len, packfile, &csum);
1600 *packfile_size += outlen;
1602 m->delta_buf = NULL;
1604 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1606 err = got_error_from_errno("fseeko");
1609 err = deltahdr(packfile_size, ctx, packfile, m);
1612 err = got_deflate_to_file(&outlen, delta_cache,
1613 m->delta_len, packfile, &csum);
1616 *packfile_size += outlen;
1620 got_object_raw_close(raw);
1624 static const struct got_error *
1625 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1626 struct got_pack_meta **deltify, int ndeltify,
1627 struct got_pack_meta **reuse, int nreuse,
1628 int nours, struct got_repository *repo,
1629 got_pack_progress_cb progress_cb, void *progress_arg,
1630 struct got_ratelimit *rl,
1631 got_cancel_cb cancel_cb, void *cancel_arg)
1633 const struct got_error *err = NULL;
1636 struct got_pack_meta *m;
1639 off_t packfile_size = 0;
1644 err = hwrite(packfile, "PACK", 4, &ctx);
1647 putbe32(buf, GOT_PACKFILE_VERSION);
1648 err = hwrite(packfile, buf, 4, &ctx);
1651 putbe32(buf, ndeltify + nreuse);
1652 err = hwrite(packfile, buf, 4, &ctx);
1656 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1658 for (i = 0; i < ndeltify; i++) {
1659 err = report_progress(progress_cb, progress_arg, rl,
1660 packfile_size, nours, ndeltify + nreuse,
1661 ndeltify + nreuse, i);
1665 err = write_packed_object(&packfile_size, packfile,
1666 delta_cache, m, &outfd, &ctx, repo);
1671 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1672 reuse_write_order_cmp);
1673 for (i = 0; i < nreuse; i++) {
1674 err = report_progress(progress_cb, progress_arg, rl,
1675 packfile_size, nours, ndeltify + nreuse,
1676 ndeltify + nreuse, ndeltify + i);
1680 err = write_packed_object(&packfile_size, packfile,
1681 delta_cache, m, &outfd, &ctx, repo);
1686 SHA1Final(pack_sha1, &ctx);
1687 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1688 if (n != SHA1_DIGEST_LENGTH)
1689 err = got_ferror(packfile, GOT_ERR_IO);
1690 packfile_size += SHA1_DIGEST_LENGTH;
1691 packfile_size += sizeof(struct got_packfile_hdr);
1693 err = progress_cb(progress_arg, packfile_size, nours,
1694 ndeltify + nreuse, ndeltify + nreuse,
1700 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1701 err = got_error_from_errno("close");
1705 static const struct got_error *
1706 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1708 struct got_object_idset *idset = arg;
1710 if (got_object_idset_get_element_data(entry) == NULL)
1711 got_object_idset_remove_element(idset, entry);
1716 static const struct got_error *
1717 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1719 struct got_object_idset *idset = arg;
1720 struct got_pack_meta *m;
1722 m = got_object_idset_get_element_data(entry);
1723 if (m->have_reused_delta)
1724 got_object_idset_remove_element(idset, entry);
1729 static const struct got_error *
1730 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1732 struct got_pack_meta *m = data;
1733 struct got_pack_metavec *v = arg;
1735 return add_meta(m, v);
1738 const struct got_error *
1739 got_pack_create(uint8_t *packsha1, FILE *packfile,
1740 struct got_object_id **theirs, int ntheirs,
1741 struct got_object_id **ours, int nours,
1742 struct got_repository *repo, int loose_obj_only, int allow_empty,
1743 got_pack_progress_cb progress_cb, void *progress_arg,
1744 got_cancel_cb cancel_cb, void *cancel_arg)
1746 const struct got_error *err;
1747 int delta_cache_fd = -1;
1748 FILE *delta_cache = NULL;
1749 struct got_object_idset *idset;
1750 struct got_ratelimit rl;
1751 struct got_pack_metavec deltify, reuse;
1753 memset(&deltify, 0, sizeof(deltify));
1754 memset(&reuse, 0, sizeof(reuse));
1756 got_ratelimit_init(&rl, 0, 500);
1758 idset = got_object_idset_alloc();
1760 return got_error_from_errno("got_object_idset_alloc");
1762 err = load_object_ids(idset, theirs, ntheirs, ours, nours,
1763 repo, loose_obj_only, cancel_cb, cancel_arg);
1767 err = got_object_idset_for_each_element(idset,
1768 remove_unused_object, idset);
1773 err = progress_cb(progress_arg, 0L, nours,
1774 got_object_idset_num_elements(idset), 0, 0);
1779 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1780 err = got_error(GOT_ERR_CANNOT_PACK);
1784 delta_cache_fd = got_opentempfd();
1785 if (delta_cache_fd == -1) {
1786 err = got_error_from_errno("got_opentemp");
1791 reuse.meta = calloc(reuse.metasz,
1792 sizeof(struct got_pack_meta *));
1793 if (reuse.meta == NULL) {
1794 err = got_error_from_errno("calloc");
1798 err = search_deltas(&reuse, idset, delta_cache_fd, nours, repo,
1799 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1802 if (reuse.nmeta > 0) {
1803 err = got_object_idset_for_each_element(idset,
1804 remove_reused_object, idset);
1809 delta_cache = fdopen(delta_cache_fd, "a+");
1810 if (delta_cache == NULL) {
1811 err = got_error_from_errno("fdopen");
1814 delta_cache_fd = -1;
1816 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1817 err = got_error_from_errno("fseeko");
1821 deltify.meta = calloc(got_object_idset_num_elements(idset),
1822 sizeof(struct got_pack_meta *));
1823 if (deltify.meta == NULL) {
1824 err = got_error_from_errno("calloc");
1827 deltify.metasz = got_object_idset_num_elements(idset);
1829 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1832 if (deltify.nmeta > 0) {
1833 err = pick_deltas(deltify.meta, deltify.nmeta, nours,
1834 reuse.nmeta, delta_cache, repo,
1835 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1838 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1839 err = got_error_from_errno("fseeko");
1844 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1845 deltify.nmeta, reuse.meta, reuse.nmeta, nours, repo,
1846 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1850 free_nmeta(deltify.meta, deltify.nmeta);
1851 free_nmeta(reuse.meta, reuse.nmeta);
1852 got_object_idset_free(idset);
1853 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1854 err = got_error_from_errno("close");
1855 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1856 err = got_error_from_errno("fclose");