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>
37 #include "got_error.h"
38 #include "got_cancel.h"
39 #include "got_object.h"
41 #include "got_reference.h"
42 #include "got_repository_admin.h"
43 #include "got_opentemp.h"
45 #include "got_lib_deltify.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_idset.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_deflate.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_repository.h"
54 #include "got_lib_ratelimit.h"
55 #include "got_lib_inflate.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 struct got_pack_meta {
70 struct got_object_id id;
76 /* The best delta we picked */
77 struct got_pack_meta *head;
78 struct got_pack_meta *prev;
79 unsigned char *delta_buf; /* if encoded in memory (compressed) */
80 off_t delta_offset; /* offset in delta cache file (compressed) */
81 off_t delta_len; /* encoded delta length */
82 off_t delta_compressed_len; /* encoded+compressed delta length */
85 int have_reused_delta;
86 off_t reused_delta_offset; /* offset of delta in reused pack file */
87 struct got_object_id *base_obj_id;
89 /* Only used for delta window */
90 struct got_delta_table *dtab;
92 /* Only used for writing offset deltas */
96 struct got_pack_metavec {
97 struct got_pack_meta **meta;
102 static const struct got_error *
103 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
104 const char *path, int obj_type, time_t mtime)
106 const struct got_error *err = NULL;
107 struct got_pack_meta *m;
111 m = calloc(1, sizeof(*m));
113 return got_error_from_errno("calloc");
115 memcpy(&m->id, id, sizeof(m->id));
117 m->path = strdup(path);
118 if (m->path == NULL) {
119 err = got_error_from_errno("strdup");
124 m->obj_type = obj_type;
131 clear_meta(struct got_pack_meta *meta)
137 free(meta->delta_buf);
138 meta->delta_buf = NULL;
139 free(meta->base_obj_id);
140 meta->base_obj_id = NULL;
144 free_nmeta(struct got_pack_meta **meta, int nmeta)
148 for (i = 0; i < nmeta; i++)
154 delta_order_cmp(const void *pa, const void *pb)
156 struct got_pack_meta *a, *b;
159 a = *(struct got_pack_meta **)pa;
160 b = *(struct got_pack_meta **)pb;
162 if (a->obj_type != b->obj_type)
163 return a->obj_type - b->obj_type;
164 cmp = strcmp(a->path, b->path);
167 if (a->mtime < b->mtime)
169 if (a->mtime > b->mtime)
171 return got_object_id_cmp(&a->id, &b->id);
175 delta_size(struct got_delta_instruction *deltas, int ndeltas)
179 for (i = 0; i < ndeltas; i++) {
181 size += GOT_DELTA_SIZE_SHIFT;
183 size += deltas[i].len + 1;
188 static const struct got_error *
189 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
193 if (*len + nseg >= *sz) {
194 while (*len + nseg >= *sz)
196 n = realloc(*p, *sz);
198 return got_error_from_errno("realloc");
201 memcpy(*p + *len, seg, nseg);
206 static const struct got_error *
207 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
208 struct got_delta_instruction *deltas, int ndeltas,
209 off_t delta_size, off_t base_size)
211 const struct got_error *err;
212 unsigned char buf[16], *bp;
214 size_t len = 0, compressed_len;
215 off_t bufsize = delta_size;
217 struct got_delta_instruction *d;
220 delta_buf = malloc(bufsize);
221 if (delta_buf == NULL)
222 return got_error_from_errno("malloc");
224 /* base object size */
225 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
226 n = base_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(&delta_buf, &len, &bufsize, buf, i);
236 /* target object size */
237 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
238 n = o->size >> GOT_DELTA_SIZE_SHIFT;
239 for (i = 1; n > 0; i++) {
240 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
241 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
242 n >>= GOT_DELTA_SIZE_SHIFT;
244 err = append(&delta_buf, &len, &bufsize, buf, i);
248 for (j = 0; j < ndeltas; j++) {
253 buf[0] = GOT_DELTA_BASE_COPY;
254 for (i = 0; i < 4; i++) {
255 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
264 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
265 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
266 for (i = 0; i < 3 && n > 0; i++) {
267 buf[0] |= 1 << (i + 4);
272 err = append(&delta_buf, &len, &bufsize,
276 } else if (o->f == NULL) {
278 while (n != d->len) {
279 buf[0] = (d->len - n < 127) ? d->len - n : 127;
280 err = append(&delta_buf, &len, &bufsize,
284 err = append(&delta_buf, &len, &bufsize,
285 o->data + o->hdrlen + d->offset + n,
294 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
295 err = got_error_from_errno("fseeko");
299 while (n != d->len) {
300 buf[0] = (d->len - n < 127) ? d->len - n : 127;
301 err = append(&delta_buf, &len, &bufsize,
305 r = fread(content, 1, buf[0], o->f);
307 err = got_ferror(o->f, GOT_ERR_IO);
310 err = append(&delta_buf, &len, &bufsize,
319 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
320 NULL, NULL, delta_buf, 0, len);
325 m->delta_compressed_len = compressed_len;
331 static const struct got_error *
332 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
333 struct got_delta_instruction *deltas, int ndeltas,
334 off_t base_size, FILE *f)
336 const struct got_error *err;
337 unsigned char buf[16], *bp;
340 struct got_deflate_buf zb;
341 struct got_delta_instruction *d;
342 off_t delta_len = 0, compressed_len = 0;
344 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
348 /* base object size */
349 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
350 n = base_size >> GOT_DELTA_SIZE_SHIFT;
351 for (i = 1; n > 0; i++) {
352 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
353 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
354 n >>= GOT_DELTA_SIZE_SHIFT;
357 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
363 /* target object size */
364 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
365 n = o->size >> GOT_DELTA_SIZE_SHIFT;
366 for (i = 1; n > 0; i++) {
367 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
368 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
369 n >>= GOT_DELTA_SIZE_SHIFT;
372 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
378 for (j = 0; j < ndeltas; j++) {
383 buf[0] = GOT_DELTA_BASE_COPY;
384 for (i = 0; i < 4; i++) {
385 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
393 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
394 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
395 for (i = 0; i < 3 && n > 0; i++) {
396 buf[0] |= 1 << (i + 4);
401 err = got_deflate_append_to_file_mmap(&zb,
402 &compressed_len, buf, 0, bp - buf, f, NULL);
405 delta_len += (bp - buf);
406 } else if (o->f == NULL) {
408 while (n != d->len) {
409 buf[0] = (d->len - n < 127) ? d->len - n : 127;
410 err = got_deflate_append_to_file_mmap(&zb,
411 &compressed_len, buf, 0, 1, f, NULL);
415 err = got_deflate_append_to_file_mmap(&zb,
417 o->data + o->hdrlen + d->offset + n, 0,
427 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
428 err = got_error_from_errno("fseeko");
432 while (n != d->len) {
433 buf[0] = (d->len - n < 127) ? d->len - n : 127;
434 err = got_deflate_append_to_file_mmap(&zb,
435 &compressed_len, buf, 0, 1, f, NULL);
439 r = fread(content, 1, buf[0], o->f);
441 err = got_ferror(o->f, GOT_ERR_IO);
444 err = got_deflate_append_to_file_mmap(&zb,
445 &compressed_len, content, 0, buf[0], f,
455 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
460 if (compressed_len != ftello(f) - m->delta_offset) {
461 err = got_error(GOT_ERR_COMPRESSION);
465 m->delta_len = delta_len;
466 m->delta_compressed_len = compressed_len;
468 got_deflate_end(&zb);
472 static const struct got_error *
473 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
474 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
475 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
478 const struct got_error *err;
481 if (progress_cb == NULL)
484 err = got_ratelimit_check(&elapsed, rl);
488 return progress_cb(progress_arg, ncolored, nfound, ntrees,
489 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
492 static const struct got_error *
493 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
495 if (v->nmeta == v->metasz){
496 size_t newsize = 2 * v->metasz;
497 struct got_pack_meta **new;
498 new = reallocarray(v->meta, newsize, sizeof(*new));
500 return got_error_from_errno("reallocarray");
505 v->meta[v->nmeta++] = m;
509 static const struct got_error *
510 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
511 struct got_object_idset *idset, struct got_pack *pack,
512 struct got_packidx *packidx, int delta_cache_fd,
513 struct got_repository *repo)
515 const struct got_error *err = NULL;
516 struct got_pack_meta *base = NULL;
517 struct got_object_id *base_obj_id = NULL;
518 off_t delta_len = 0, delta_compressed_len = 0;
519 off_t delta_offset = 0, delta_cache_offset = 0;
520 uint64_t base_size, result_size;
522 if (m->have_reused_delta)
525 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
526 &delta_compressed_len, &delta_offset, &delta_cache_offset,
527 &base_obj_id, delta_cache_fd, packidx, idx, &m->id, repo);
531 if (delta_offset + delta_len < delta_offset)
532 return got_error(GOT_ERR_BAD_PACKFILE);
534 base = got_object_idset_get(idset, base_obj_id);
538 m->delta_len = delta_len;
539 m->delta_compressed_len = delta_compressed_len;
540 m->delta_offset = delta_cache_offset;
542 m->size = result_size;
543 m->have_reused_delta = 1;
544 m->reused_delta_offset = delta_offset;
545 m->base_obj_id = base_obj_id;
547 err = add_meta(m, v);
553 static const struct got_error *
554 find_pack_for_reuse(struct got_packidx **best_packidx,
555 struct got_repository *repo)
557 const struct got_error *err = NULL;
558 struct got_pathlist_entry *pe;
559 const char *best_packidx_path = NULL;
562 *best_packidx = NULL;
564 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
565 const char *path_packidx = pe->path;
566 struct got_packidx *packidx;
569 err = got_repo_get_packidx(&packidx, path_packidx, repo);
573 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
574 if (nobj > nobj_max) {
575 best_packidx_path = path_packidx;
580 if (best_packidx_path) {
581 err = got_repo_get_packidx(best_packidx, best_packidx_path,
588 struct search_deltas_arg {
589 struct got_packidx *packidx;
590 struct got_pack *pack;
591 struct got_object_idset *idset;
592 struct got_pack_metavec *v;
594 struct got_repository *repo;
595 got_pack_progress_cb progress_cb;
597 struct got_ratelimit *rl;
598 got_cancel_cb cancel_cb;
606 static const struct got_error *
607 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
609 const struct got_error *err;
610 struct got_pack_meta *m = data;
611 struct search_deltas_arg *a = arg;
613 struct got_object *obj = NULL;
616 err = (*a->cancel_cb)(a->cancel_arg);
621 if (!got_repo_check_packidx_bloom_filter(a->repo,
622 a->packidx->path_packidx, id))
625 obj_idx = got_packidx_get_object_idx(a->packidx, id);
630 * Opening and closing an object just to check its flags
631 * is a bit expensive. We could have an imsg which requests
632 * plain type/size information for an object without doing
633 * work such as traversing the object's entire delta chain
634 * to find the base object type, and other such info which
635 * we don't really need here.
637 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
638 a->packidx, obj_idx, a->repo);
642 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
643 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
644 a->delta_cache_fd, a->repo);
647 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
648 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
649 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
652 got_object_close(obj);
656 static const struct got_error *
657 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
658 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
659 struct got_repository *repo,
660 got_pack_progress_cb progress_cb, void *progress_arg,
661 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
663 const struct got_error *err = NULL;
664 char *path_packfile = NULL;
665 struct got_packidx *packidx;
666 struct got_pack *pack;
667 struct search_deltas_arg sda;
669 err = find_pack_for_reuse(&packidx, repo);
676 err = got_packidx_get_packfile_path(&path_packfile,
677 packidx->path_packidx);
681 pack = got_repo_get_cached_pack(repo, path_packfile);
683 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
688 sda.packidx = packidx;
692 sda.delta_cache_fd = delta_cache_fd;
694 sda.progress_cb = progress_cb;
695 sda.progress_arg = progress_arg;
697 sda.cancel_cb = cancel_cb;
698 sda.cancel_arg = cancel_arg;
699 sda.ncolored = ncolored;
702 sda.ncommits = ncommits;
703 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
709 static const struct got_error *
710 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
711 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
712 struct got_repository *repo,
713 got_pack_progress_cb progress_cb, void *progress_arg,
714 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
716 const struct got_error *err = NULL;
717 struct got_pack_meta *m = NULL, *base = NULL;
718 struct got_raw_object *raw = NULL, *base_raw = NULL;
719 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
720 int i, j, ndeltas, best_ndeltas;
721 off_t size, best_size;
722 const int max_base_candidates = 3;
723 size_t delta_memsize = 0;
724 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
727 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
728 for (i = 0; i < nmeta; i++) {
730 err = (*cancel_cb)(cancel_arg);
734 err = report_progress(progress_cb, progress_arg, rl,
735 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
741 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
742 m->obj_type == GOT_OBJ_TYPE_TAG)
745 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
750 if (raw->f == NULL) {
751 err = got_deltify_init_mem(&m->dtab, raw->data,
752 raw->hdrlen, raw->size + raw->hdrlen);
754 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
755 raw->size + raw->hdrlen);
760 if (i > max_base_candidates) {
761 struct got_pack_meta *n = NULL;
762 n = meta[i - (max_base_candidates + 1)];
763 got_deltify_free(n->dtab);
767 best_size = raw->size;
769 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
771 err = (*cancel_cb)(cancel_arg);
776 /* long chains make unpacking slow, avoid such bases */
777 if (base->nchain >= 128 ||
778 base->obj_type != m->obj_type)
781 err = got_object_raw_open(&base_raw, &outfd, repo,
786 if (raw->f == NULL && base_raw->f == NULL) {
787 err = got_deltify_mem_mem(&deltas, &ndeltas,
788 raw->data, raw->hdrlen,
789 raw->size + raw->hdrlen,
790 base->dtab, base_raw->data,
792 base_raw->size + base_raw->hdrlen);
793 } else if (raw->f == NULL) {
794 err = got_deltify_mem_file(&deltas, &ndeltas,
795 raw->data, raw->hdrlen,
796 raw->size + raw->hdrlen,
797 base->dtab, base_raw->f,
799 base_raw->size + base_raw->hdrlen);
800 } else if (base_raw->f == NULL) {
801 err = got_deltify_file_mem(&deltas, &ndeltas,
803 raw->size + raw->hdrlen,
804 base->dtab, base_raw->data,
806 base_raw->size + base_raw->hdrlen);
808 err = got_deltify(&deltas, &ndeltas,
810 raw->size + raw->hdrlen,
811 base->dtab, base_raw->f, base_raw->hdrlen,
812 base_raw->size + base_raw->hdrlen);
814 got_object_raw_close(base_raw);
819 size = delta_size(deltas, ndeltas);
820 if (size + 32 < best_size){
822 * if we already picked a best delta,
827 best_deltas = deltas;
828 best_ndeltas = ndeltas;
830 m->nchain = base->nchain + 1;
832 m->head = base->head;
842 if (best_ndeltas > 0) {
843 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
844 delta_memsize + best_size <= max_delta_memsize) {
845 delta_memsize += best_size;
846 err = encode_delta_in_mem(m, raw, best_deltas,
847 best_ndeltas, best_size, m->prev->size);
849 m->delta_offset = ftello(delta_cache);
850 err = encode_delta(m, raw, best_deltas,
851 best_ndeltas, m->prev->size, delta_cache);
860 got_object_raw_close(raw);
864 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
865 got_deltify_free(meta[i]->dtab);
866 meta[i]->dtab = NULL;
869 got_object_raw_close(raw);
871 got_object_raw_close(base_raw);
872 if (outfd != -1 && close(outfd) == -1 && err == NULL)
873 err = got_error_from_errno("close");
879 static const struct got_error *
880 search_packidx(int *found, struct got_object_id *id,
881 struct got_repository *repo)
883 const struct got_error *err = NULL;
884 struct got_packidx *packidx = NULL;
889 err = got_repo_search_packidx(&packidx, &idx, repo, id);
891 *found = 1; /* object is already packed */
892 else if (err->code == GOT_ERR_NO_OBJ)
897 static const struct got_error *
898 add_object(int want_meta, struct got_object_idset *idset,
899 struct got_object_id *id, const char *path, int obj_type,
900 time_t mtime, int loose_obj_only, struct got_repository *repo,
901 int *ncolored, int *nfound, int *ntrees,
902 got_pack_progress_cb progress_cb, void *progress_arg,
903 struct got_ratelimit *rl)
905 const struct got_error *err;
906 struct got_pack_meta *m = NULL;
908 if (loose_obj_only) {
910 err = search_packidx(&is_packed, id, repo);
913 if (is_packed && want_meta)
918 err = alloc_meta(&m, id, path, obj_type, mtime);
923 err = report_progress(progress_cb, progress_arg, rl,
924 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
932 err = got_object_idset_add(idset, id, m);
940 static const struct got_error *
941 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
942 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
943 struct got_object_id *tree_id,
944 const char *dpath, time_t mtime, struct got_repository *repo,
945 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
946 got_pack_progress_cb progress_cb, void *progress_arg,
947 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
949 const struct got_error *err;
950 struct got_tree_object *tree;
954 err = got_object_open_as_tree(&tree, repo, tree_id);
959 err = report_progress(progress_cb, progress_arg, rl,
960 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
964 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
965 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
966 struct got_object_id *id = got_tree_entry_get_id(e);
967 mode_t mode = got_tree_entry_get_mode(e);
970 err = (*cancel_cb)(cancel_arg);
975 if (got_object_tree_entry_is_submodule(e) ||
976 got_object_idset_contains(idset, id) ||
977 got_object_idset_contains(idset_exclude, id))
980 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
981 got_tree_entry_get_name(e)) == -1) {
982 err = got_error_from_errno("asprintf");
987 struct got_object_qid *qid;
988 err = got_object_qid_alloc(&qid, id);
991 STAILQ_INSERT_TAIL(ids, qid, entry);
992 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
993 err = add_object(want_meta,
994 want_meta ? idset : idset_exclude, id, p,
995 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
996 ncolored, nfound, ntrees,
997 progress_cb, progress_arg, rl);
1005 got_object_tree_close(tree);
1010 static const struct got_error *
1011 load_tree(int want_meta, struct got_object_idset *idset,
1012 struct got_object_idset *idset_exclude,
1013 struct got_object_id *tree_id, const char *dpath, time_t mtime,
1014 struct got_repository *repo, int loose_obj_only,
1015 int *ncolored, int *nfound, int *ntrees,
1016 got_pack_progress_cb progress_cb, void *progress_arg,
1017 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1019 const struct got_error *err = NULL;
1020 struct got_object_id_queue tree_ids;
1021 struct got_object_qid *qid;
1023 if (got_object_idset_contains(idset, tree_id) ||
1024 got_object_idset_contains(idset_exclude, tree_id))
1027 err = got_object_qid_alloc(&qid, tree_id);
1031 STAILQ_INIT(&tree_ids);
1032 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1034 while (!STAILQ_EMPTY(&tree_ids)) {
1036 err = (*cancel_cb)(cancel_arg);
1041 qid = STAILQ_FIRST(&tree_ids);
1042 STAILQ_REMOVE_HEAD(&tree_ids, entry);
1044 if (got_object_idset_contains(idset, &qid->id) ||
1045 got_object_idset_contains(idset_exclude, &qid->id)) {
1046 got_object_qid_free(qid);
1050 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1051 &qid->id, dpath, GOT_OBJ_TYPE_TREE,
1052 mtime, loose_obj_only, repo,
1053 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1055 got_object_qid_free(qid);
1059 err = load_tree_entries(&tree_ids, want_meta, idset,
1060 idset_exclude, &qid->id,
1061 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1062 ntrees, progress_cb, progress_arg, rl,
1063 cancel_cb, cancel_arg);
1064 got_object_qid_free(qid);
1069 got_object_id_queue_free(&tree_ids);
1073 static const struct got_error *
1074 load_commit(int want_meta, struct got_object_idset *idset,
1075 struct got_object_idset *idset_exclude,
1076 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1077 int *ncolored, int *nfound, int *ntrees,
1078 got_pack_progress_cb progress_cb, void *progress_arg,
1079 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1081 const struct got_error *err;
1082 struct got_commit_object *commit;
1084 if (got_object_idset_contains(idset, id) ||
1085 got_object_idset_contains(idset_exclude, id))
1088 if (loose_obj_only) {
1090 err = search_packidx(&is_packed, id, repo);
1093 if (is_packed && want_meta)
1097 err = got_object_open_as_commit(&commit, repo, id);
1101 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1102 id, "", GOT_OBJ_TYPE_COMMIT,
1103 got_object_commit_get_committer_time(commit),
1104 loose_obj_only, repo,
1105 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1109 err = load_tree(want_meta, idset, idset_exclude,
1110 got_object_commit_get_tree_id(commit),
1111 "", got_object_commit_get_committer_time(commit),
1112 repo, loose_obj_only, ncolored, nfound, ntrees,
1113 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1115 got_object_commit_close(commit);
1119 static const struct got_error *
1120 load_tag(int want_meta, struct got_object_idset *idset,
1121 struct got_object_idset *idset_exclude,
1122 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1123 int *ncolored, int *nfound, int *ntrees,
1124 got_pack_progress_cb progress_cb, void *progress_arg,
1125 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1127 const struct got_error *err;
1128 struct got_tag_object *tag = NULL;
1130 if (got_object_idset_contains(idset, id) ||
1131 got_object_idset_contains(idset_exclude, id))
1134 if (loose_obj_only) {
1136 err = search_packidx(&is_packed, id, repo);
1139 if (is_packed && want_meta)
1143 err = got_object_open_as_tag(&tag, repo, id);
1147 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1148 id, "", GOT_OBJ_TYPE_TAG,
1149 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1150 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1154 switch (got_object_tag_get_object_type(tag)) {
1155 case GOT_OBJ_TYPE_COMMIT:
1156 err = load_commit(want_meta, idset, idset_exclude,
1157 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1158 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1159 cancel_cb, cancel_arg);
1161 case GOT_OBJ_TYPE_TREE:
1162 err = load_tree(want_meta, idset, idset_exclude,
1163 got_object_tag_get_object_id(tag), "",
1164 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1165 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1166 cancel_cb, cancel_arg);
1173 got_object_tag_close(tag);
1177 enum findtwixt_color {
1184 static const int findtwixt_colors[] = {
1191 static const struct got_error *
1192 paint_commit(struct got_object_qid *qid, int color)
1194 if (color < 0 || color >= nitems(findtwixt_colors))
1195 return got_error(GOT_ERR_RANGE);
1197 qid->data = (void *)&findtwixt_colors[color];
1201 static const struct got_error *
1202 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1203 int color, struct got_repository *repo)
1205 const struct got_error *err;
1206 struct got_object_qid *qid;
1208 err = got_object_qid_alloc(&qid, id);
1212 STAILQ_INSERT_TAIL(ids, qid, entry);
1213 return paint_commit(qid, color);
1216 struct append_id_arg {
1217 struct got_object_id **array;
1219 struct got_object_idset *drop;
1220 struct got_object_idset *skip;
1223 static const struct got_error *
1224 append_id(struct got_object_id *id, void *data, void *arg)
1226 struct append_id_arg *a = arg;
1228 if (got_object_idset_contains(a->skip, id) ||
1229 got_object_idset_contains(a->drop, id))
1232 a->array[++a->idx] = got_object_id_dup(id);
1233 if (a->array[a->idx] == NULL)
1234 return got_error_from_errno("got_object_id_dup");
1239 static const struct got_error *
1240 queue_commit_or_tag_id(struct got_object_id *id, int color,
1241 struct got_object_id_queue *ids, struct got_repository *repo)
1243 const struct got_error *err;
1244 struct got_tag_object *tag = NULL;
1247 err = got_object_get_type(&obj_type, repo, id);
1251 if (obj_type == GOT_OBJ_TYPE_TAG) {
1252 err = got_object_open_as_tag(&tag, repo, id);
1255 obj_type = got_object_tag_get_object_type(tag);
1256 id = got_object_tag_get_object_id(tag);
1259 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1260 err = queue_commit_id(ids, id, color, repo);
1266 got_object_tag_close(tag);
1270 static const struct got_error *
1271 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1272 struct got_object_idset *keep, struct got_object_idset *drop,
1273 struct got_object_idset *skip, struct got_repository *repo,
1274 got_pack_progress_cb progress_cb, void *progress_arg,
1275 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1277 const struct got_error *err = NULL;
1278 struct got_commit_object *commit = NULL;
1279 const struct got_object_id_queue *parents;
1280 struct got_object_qid *qid;
1281 int nqueued = nids, nskip = 0;
1283 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1287 err = cancel_cb(cancel_arg);
1292 qid = STAILQ_FIRST(ids);
1293 STAILQ_REMOVE_HEAD(ids, entry);
1295 color = *((int *)qid->data);
1296 if (color == COLOR_SKIP)
1299 if (got_object_idset_contains(skip, &qid->id)) {
1300 got_object_qid_free(qid);
1306 if (got_object_idset_contains(keep, &qid->id)) {
1307 got_object_qid_free(qid);
1310 if (got_object_idset_contains(drop, &qid->id)) {
1311 err = paint_commit(qid, COLOR_SKIP);
1317 err = got_object_idset_add(keep, &qid->id, NULL);
1322 if (got_object_idset_contains(drop, &qid->id)) {
1323 got_object_qid_free(qid);
1326 if (got_object_idset_contains(keep, &qid->id)) {
1327 err = paint_commit(qid, COLOR_SKIP);
1333 err = got_object_idset_add(drop, &qid->id, NULL);
1338 if (!got_object_idset_contains(skip, &qid->id)) {
1339 err = got_object_idset_add(skip, &qid->id,
1346 /* should not happen */
1347 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1348 "%s invalid commit color %d", __func__, color);
1352 err = report_progress(progress_cb, progress_arg, rl,
1353 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1358 err = got_object_open_as_commit(&commit, repo, &qid->id);
1362 parents = got_object_commit_get_parent_ids(commit);
1364 struct got_object_qid *pid;
1365 color = *((int *)qid->data);
1366 STAILQ_FOREACH(pid, parents, entry) {
1367 err = queue_commit_id(ids, &pid->id, color,
1372 if (color == COLOR_SKIP)
1377 got_object_commit_close(commit);
1379 got_object_qid_free(qid);
1383 got_object_commit_close(commit);
1387 static const struct got_error *
1388 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1389 struct got_object_id **head, int nhead,
1390 struct got_object_id **tail, int ntail,
1391 struct got_repository *repo,
1392 got_pack_progress_cb progress_cb, void *progress_arg,
1393 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1395 const struct got_error *err = NULL;
1396 struct got_object_id_queue ids;
1397 struct got_object_idset *keep, *drop, *skip = NULL;
1405 keep = got_object_idset_alloc();
1407 return got_error_from_errno("got_object_idset_alloc");
1409 drop = got_object_idset_alloc();
1411 err = got_error_from_errno("got_object_idset_alloc");
1415 skip = got_object_idset_alloc();
1417 err = got_error_from_errno("got_object_idset_alloc");
1421 for (i = 0; i < nhead; i++) {
1422 struct got_object_id *id = head[i];
1425 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1430 for (i = 0; i < ntail; i++) {
1431 struct got_object_id *id = tail[i];
1434 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1439 err = paint_commits(ncolored, &ids, nhead + ntail,
1440 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1441 cancel_cb, cancel_arg);
1445 nkeep = got_object_idset_num_elements(keep);
1447 struct append_id_arg arg;
1448 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1449 if (arg.array == NULL) {
1450 err = got_error_from_errno("calloc");
1456 err = got_object_idset_for_each(keep, append_id, &arg);
1462 *nres = arg.idx + 1;
1465 got_object_idset_free(keep);
1466 got_object_idset_free(drop);
1468 got_object_idset_free(skip);
1469 got_object_id_queue_free(&ids);
1473 static const struct got_error *
1474 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1475 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1476 struct got_object_id **ours, int nours, struct got_repository *repo,
1477 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1478 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1480 const struct got_error *err = NULL;
1481 struct got_object_id **ids = NULL;
1482 int i, nobj = 0, obj_type;
1483 struct got_object_idset *idset_exclude;
1485 idset_exclude = got_object_idset_alloc();
1486 if (idset_exclude == NULL)
1487 return got_error_from_errno("got_object_idset_alloc");
1493 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1494 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1495 if (err || nobj == 0)
1498 for (i = 0; i < ntheirs; i++) {
1499 struct got_object_id *id = theirs[i];
1502 err = got_object_get_type(&obj_type, repo, id);
1505 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1506 err = load_commit(0, idset, idset_exclude, id, repo,
1507 loose_obj_only, ncolored, nfound, ntrees,
1508 progress_cb, progress_arg, rl,
1509 cancel_cb, cancel_arg);
1512 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1513 err = load_tag(0, idset, idset_exclude, id, repo,
1514 loose_obj_only, ncolored, nfound, ntrees,
1515 progress_cb, progress_arg, rl,
1516 cancel_cb, cancel_arg);
1522 for (i = 0; i < nobj; i++) {
1523 err = load_commit(1, idset, idset_exclude,
1524 ids[i], repo, loose_obj_only, ncolored, nfound, ntrees,
1525 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1530 for (i = 0; i < nours; i++) {
1531 struct got_object_id *id = ours[i];
1532 struct got_pack_meta *m;
1535 m = got_object_idset_get(idset, id);
1537 err = got_object_get_type(&obj_type, repo, id);
1541 obj_type = m->obj_type;
1542 if (obj_type != GOT_OBJ_TYPE_TAG)
1544 err = load_tag(1, idset, idset_exclude, id, repo,
1545 loose_obj_only, ncolored, nfound, ntrees,
1546 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1551 for (i = 0; i < nobj; i++) {
1555 got_object_idset_free(idset_exclude);
1559 const struct got_error *
1560 hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
1564 SHA1Update(ctx, buf, len);
1565 n = fwrite(buf, 1, len, f);
1567 return got_ferror(f, GOT_ERR_IO);
1571 const struct got_error *
1572 hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1574 unsigned char buf[65536];
1578 while (remain > 0) {
1579 size_t copylen = MIN(sizeof(buf), remain);
1580 n = fread(buf, 1, copylen, fsrc);
1582 return got_ferror(fsrc, GOT_ERR_IO);
1583 SHA1Update(ctx, buf, copylen);
1584 n = fwrite(buf, 1, copylen, fdst);
1586 return got_ferror(fdst, GOT_ERR_IO);
1594 putbe32(char *b, uint32_t n)
1603 write_order_cmp(const void *pa, const void *pb)
1605 struct got_pack_meta *a, *b, *ahd, *bhd;
1607 a = *(struct got_pack_meta **)pa;
1608 b = *(struct got_pack_meta **)pb;
1609 ahd = (a->head == NULL) ? a : a->head;
1610 bhd = (b->head == NULL) ? b : b->head;
1611 if (bhd->mtime < ahd->mtime)
1613 if (bhd->mtime > ahd->mtime)
1619 if (a->nchain != b->nchain)
1620 return a->nchain - b->nchain;
1621 if (a->mtime < b->mtime)
1623 if (a->mtime > b->mtime)
1625 return got_object_id_cmp(&a->id, &b->id);
1629 reuse_write_order_cmp(const void *pa, const void *pb)
1631 struct got_pack_meta *a, *b;
1633 a = *(struct got_pack_meta **)pa;
1634 b = *(struct got_pack_meta **)pb;
1636 if (a->reused_delta_offset < b->reused_delta_offset)
1638 if (a->reused_delta_offset > b->reused_delta_offset)
1643 static const struct got_error *
1644 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1650 hdr[0] = obj_type << 4;
1651 hdr[0] |= len & 0xf;
1653 for (i = 1; len != 0; i++){
1655 return got_error(GOT_ERR_NO_SPACE);
1656 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1657 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1658 len >>= GOT_DELTA_SIZE_SHIFT;
1666 packoff(char *hdr, off_t off)
1671 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1672 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1673 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1674 GOT_DELTA_SIZE_MORE;
1679 hdr[j++] = rbuf[--i];
1683 static const struct got_error *
1684 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1685 struct got_pack_meta *m)
1687 const struct got_error *err;
1691 if (m->prev->off != 0) {
1692 err = packhdr(&nh, buf, sizeof(buf),
1693 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1696 nh += packoff(buf + nh, m->off - m->prev->off);
1697 err = hwrite(packfile, buf, nh, ctx);
1700 *packfile_size += nh;
1702 err = packhdr(&nh, buf, sizeof(buf),
1703 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1706 err = hwrite(packfile, buf, nh, ctx);
1709 *packfile_size += nh;
1710 err = hwrite(packfile, m->prev->id.sha1,
1711 sizeof(m->prev->id.sha1), ctx);
1714 *packfile_size += sizeof(m->prev->id.sha1);
1720 static const struct got_error *
1721 write_packed_object(off_t *packfile_size, FILE *packfile,
1722 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1723 SHA1_CTX *ctx, struct got_repository *repo)
1725 const struct got_error *err = NULL;
1726 struct got_deflate_checksum csum;
1729 struct got_raw_object *raw = NULL;
1732 csum.output_sha1 = ctx;
1733 csum.output_crc = NULL;
1735 m->off = ftello(packfile);
1736 if (m->delta_len == 0) {
1737 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1740 err = packhdr(&nh, buf, sizeof(buf),
1741 m->obj_type, raw->size);
1744 err = hwrite(packfile, buf, nh, ctx);
1747 *packfile_size += nh;
1748 if (raw->f == NULL) {
1749 err = got_deflate_to_file_mmap(&outlen,
1750 raw->data + raw->hdrlen, 0, raw->size,
1755 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1757 err = got_error_from_errno("fseeko");
1760 err = got_deflate_to_file(&outlen, raw->f,
1761 raw->size, packfile, &csum);
1765 *packfile_size += outlen;
1766 got_object_raw_close(raw);
1768 } else if (m->delta_buf) {
1769 err = deltahdr(packfile_size, ctx, packfile, m);
1772 err = hwrite(packfile, m->delta_buf,
1773 m->delta_compressed_len, ctx);
1776 *packfile_size += m->delta_compressed_len;
1778 m->delta_buf = NULL;
1780 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1782 err = got_error_from_errno("fseeko");
1785 err = deltahdr(packfile_size, ctx, packfile, m);
1788 err = hcopy(delta_cache, packfile,
1789 m->delta_compressed_len, ctx);
1792 *packfile_size += m->delta_compressed_len;
1796 got_object_raw_close(raw);
1800 static const struct got_error *
1801 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1802 struct got_pack_meta **deltify, int ndeltify,
1803 struct got_pack_meta **reuse, int nreuse,
1804 int ncolored, int nfound, int ntrees, int nours,
1805 struct got_repository *repo,
1806 got_pack_progress_cb progress_cb, void *progress_arg,
1807 struct got_ratelimit *rl,
1808 got_cancel_cb cancel_cb, void *cancel_arg)
1810 const struct got_error *err = NULL;
1813 struct got_pack_meta *m;
1816 off_t packfile_size = 0;
1821 err = hwrite(packfile, "PACK", 4, &ctx);
1824 putbe32(buf, GOT_PACKFILE_VERSION);
1825 err = hwrite(packfile, buf, 4, &ctx);
1828 putbe32(buf, ndeltify + nreuse);
1829 err = hwrite(packfile, buf, 4, &ctx);
1833 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1835 for (i = 0; i < ndeltify; i++) {
1836 err = report_progress(progress_cb, progress_arg, rl,
1837 ncolored, nfound, ntrees, packfile_size, nours,
1838 ndeltify + nreuse, ndeltify + nreuse, i);
1842 err = write_packed_object(&packfile_size, packfile,
1843 delta_cache, m, &outfd, &ctx, repo);
1848 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1849 reuse_write_order_cmp);
1850 for (i = 0; i < nreuse; i++) {
1851 err = report_progress(progress_cb, progress_arg, rl,
1852 ncolored, nfound, ntrees, packfile_size, nours,
1853 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1857 err = write_packed_object(&packfile_size, packfile,
1858 delta_cache, m, &outfd, &ctx, repo);
1863 SHA1Final(pack_sha1, &ctx);
1864 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1865 if (n != SHA1_DIGEST_LENGTH)
1866 err = got_ferror(packfile, GOT_ERR_IO);
1867 packfile_size += SHA1_DIGEST_LENGTH;
1868 packfile_size += sizeof(struct got_packfile_hdr);
1870 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1871 packfile_size, nours, ndeltify + nreuse,
1872 ndeltify + nreuse, ndeltify + nreuse);
1877 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1878 err = got_error_from_errno("close");
1882 static const struct got_error *
1883 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1885 struct got_pack_meta *m = data;
1886 struct got_pack_metavec *v = arg;
1888 if (m->have_reused_delta)
1891 return add_meta(m, v);
1894 const struct got_error *
1895 got_pack_create(uint8_t *packsha1, FILE *packfile,
1896 struct got_object_id **theirs, int ntheirs,
1897 struct got_object_id **ours, int nours,
1898 struct got_repository *repo, int loose_obj_only, int allow_empty,
1899 got_pack_progress_cb progress_cb, void *progress_arg,
1900 got_cancel_cb cancel_cb, void *cancel_arg)
1902 const struct got_error *err;
1903 int delta_cache_fd = -1;
1904 FILE *delta_cache = NULL;
1905 struct got_object_idset *idset;
1906 struct got_ratelimit rl;
1907 struct got_pack_metavec deltify, reuse;
1908 int ncolored = 0, nfound = 0, ntrees = 0;
1911 memset(&deltify, 0, sizeof(deltify));
1912 memset(&reuse, 0, sizeof(reuse));
1914 got_ratelimit_init(&rl, 0, 500);
1916 idset = got_object_idset_alloc();
1918 return got_error_from_errno("got_object_idset_alloc");
1920 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1921 ntheirs, ours, nours, repo, loose_obj_only,
1922 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1927 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1928 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1933 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1934 err = got_error(GOT_ERR_CANNOT_PACK);
1938 delta_cache_fd = got_opentempfd();
1939 if (delta_cache_fd == -1) {
1940 err = got_error_from_errno("got_opentemp");
1945 reuse.meta = calloc(reuse.metasz,
1946 sizeof(struct got_pack_meta *));
1947 if (reuse.meta == NULL) {
1948 err = got_error_from_errno("calloc");
1952 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1953 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1954 cancel_cb, cancel_arg);
1958 delta_cache = fdopen(delta_cache_fd, "a+");
1959 if (delta_cache == NULL) {
1960 err = got_error_from_errno("fdopen");
1963 delta_cache_fd = -1;
1965 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1966 err = got_error_from_errno("fseeko");
1970 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1972 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1973 if (deltify.meta == NULL) {
1974 err = got_error_from_errno("calloc");
1977 deltify.metasz = ndeltify;
1979 err = got_object_idset_for_each(idset, add_meta_idset_cb,
1983 if (deltify.nmeta > 0) {
1984 err = pick_deltas(deltify.meta, deltify.nmeta,
1985 ncolored, nfound, ntrees, nours, reuse.nmeta,
1986 delta_cache, repo, progress_cb, progress_arg, &rl,
1987 cancel_cb, cancel_arg);
1993 if (fflush(delta_cache) == EOF) {
1994 err = got_error_from_errno("fflush");
1997 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1998 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1999 nours, repo, progress_cb, progress_arg, &rl,
2000 cancel_cb, cancel_arg);
2004 free_nmeta(deltify.meta, deltify.nmeta);
2005 free_nmeta(reuse.meta, reuse.nmeta);
2006 got_object_idset_free(idset);
2007 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2008 err = got_error_from_errno("close");
2009 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2010 err = got_error_from_errno("fclose");