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>
32 #if defined(__FreeBSD__)
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, int ncolored, int nfound, int ntrees,
412 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
415 const struct got_error *err;
418 if (progress_cb == NULL)
421 err = got_ratelimit_check(&elapsed, rl);
425 return progress_cb(progress_arg, ncolored, nfound, ntrees,
426 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
429 static const struct got_error *
430 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
432 if (v->nmeta == v->metasz){
433 size_t newsize = 2 * v->metasz;
434 struct got_pack_meta **new;
435 new = reallocarray(v->meta, newsize, sizeof(*new));
437 return got_error_from_errno("reallocarray");
442 v->meta[v->nmeta++] = m;
446 static const struct got_error *
447 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
448 struct got_object_idset *idset, struct got_pack *pack,
449 struct got_packidx *packidx, int delta_cache_fd,
450 struct got_repository *repo)
452 const struct got_error *err = NULL;
453 struct got_pack_meta *base = NULL;
454 struct got_object_id *base_obj_id = NULL;
455 off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
456 uint64_t base_size, result_size;
458 if (m->have_reused_delta)
461 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
462 &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
463 packidx, idx, &m->id, repo);
467 if (delta_offset + delta_len < delta_offset)
468 return got_error(GOT_ERR_BAD_PACKFILE);
470 base = got_object_idset_get(idset, base_obj_id);
474 m->delta_len = delta_len;
475 m->delta_offset = delta_cache_offset;
477 m->size = result_size;
478 m->have_reused_delta = 1;
479 m->reused_delta_offset = delta_offset;
480 m->base_obj_id = base_obj_id;
482 err = add_meta(m, v);
488 static const struct got_error *
489 find_pack_for_reuse(struct got_packidx **best_packidx,
490 struct got_repository *repo)
492 const struct got_error *err = NULL;
493 struct got_pathlist_entry *pe;
494 const char *best_packidx_path = NULL;
497 *best_packidx = NULL;
499 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
500 const char *path_packidx = pe->path;
501 struct got_packidx *packidx;
504 err = got_repo_get_packidx(&packidx, path_packidx, repo);
508 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
509 if (nobj > nobj_max) {
510 best_packidx_path = path_packidx;
515 if (best_packidx_path) {
516 err = got_repo_get_packidx(best_packidx, best_packidx_path,
523 struct search_deltas_arg {
524 struct got_packidx *packidx;
525 struct got_pack *pack;
526 struct got_object_idset *idset;
527 struct got_pack_metavec *v;
529 struct got_repository *repo;
530 got_pack_progress_cb progress_cb;
532 struct got_ratelimit *rl;
533 got_cancel_cb cancel_cb;
541 static const struct got_error *
542 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
544 const struct got_error *err;
545 struct got_pack_meta *m = data;
546 struct search_deltas_arg *a = arg;
548 struct got_object *obj = NULL;
551 err = (*a->cancel_cb)(a->cancel_arg);
556 if (!got_repo_check_packidx_bloom_filter(a->repo,
557 a->packidx->path_packidx, id))
560 obj_idx = got_packidx_get_object_idx(a->packidx, id);
565 * Opening and closing an object just to check its flags
566 * is a bit expensive. We could have an imsg which requests
567 * plain type/size information for an object without doing
568 * work such as traversing the object's entire delta chain
569 * to find the base object type, and other such info which
570 * we don't really need here.
572 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
573 a->packidx, obj_idx, a->repo);
577 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
578 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
579 a->delta_cache_fd, a->repo);
582 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
583 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
584 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
587 got_object_close(obj);
591 static const struct got_error *
592 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
593 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
594 struct got_repository *repo,
595 got_pack_progress_cb progress_cb, void *progress_arg,
596 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
598 const struct got_error *err = NULL;
599 char *path_packfile = NULL;
600 struct got_packidx *packidx;
601 struct got_pack *pack;
602 struct search_deltas_arg sda;
604 err = find_pack_for_reuse(&packidx, repo);
611 err = got_packidx_get_packfile_path(&path_packfile,
612 packidx->path_packidx);
616 pack = got_repo_get_cached_pack(repo, path_packfile);
618 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
623 sda.packidx = packidx;
627 sda.delta_cache_fd = delta_cache_fd;
629 sda.progress_cb = progress_cb;
630 sda.progress_arg = progress_arg;
632 sda.cancel_cb = cancel_cb;
633 sda.cancel_arg = cancel_arg;
634 sda.ncolored = ncolored;
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 ncolored,
646 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
647 struct got_repository *repo,
648 got_pack_progress_cb progress_cb, void *progress_arg,
649 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
651 const struct got_error *err = NULL;
652 struct got_pack_meta *m = NULL, *base = NULL;
653 struct got_raw_object *raw = NULL, *base_raw = NULL;
654 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
655 int i, j, ndeltas, best_ndeltas;
656 off_t size, best_size;
657 const int max_base_candidates = 3;
658 size_t delta_memsize = 0;
659 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
662 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
663 for (i = 0; i < nmeta; i++) {
665 err = (*cancel_cb)(cancel_arg);
669 err = report_progress(progress_cb, progress_arg, rl,
670 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
676 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
677 m->obj_type == GOT_OBJ_TYPE_TAG)
680 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
685 if (raw->f == NULL) {
686 err = got_deltify_init_mem(&m->dtab, raw->data,
687 raw->hdrlen, raw->size + raw->hdrlen);
689 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
690 raw->size + raw->hdrlen);
695 if (i > max_base_candidates) {
696 struct got_pack_meta *n = NULL;
697 n = meta[i - (max_base_candidates + 1)];
698 got_deltify_free(n->dtab);
702 best_size = raw->size;
704 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
706 err = (*cancel_cb)(cancel_arg);
711 /* long chains make unpacking slow, avoid such bases */
712 if (base->nchain >= 128 ||
713 base->obj_type != m->obj_type)
716 err = got_object_raw_open(&base_raw, &outfd, repo,
721 if (raw->f == NULL && base_raw->f == NULL) {
722 err = got_deltify_mem_mem(&deltas, &ndeltas,
723 raw->data, raw->hdrlen,
724 raw->size + raw->hdrlen,
725 base->dtab, base_raw->data,
727 base_raw->size + base_raw->hdrlen);
728 } else if (raw->f == NULL) {
729 err = got_deltify_mem_file(&deltas, &ndeltas,
730 raw->data, raw->hdrlen,
731 raw->size + raw->hdrlen,
732 base->dtab, base_raw->f,
734 base_raw->size + base_raw->hdrlen);
735 } else if (base_raw->f == NULL) {
736 err = got_deltify_file_mem(&deltas, &ndeltas,
738 raw->size + raw->hdrlen,
739 base->dtab, base_raw->data,
741 base_raw->size + base_raw->hdrlen);
743 err = got_deltify(&deltas, &ndeltas,
745 raw->size + raw->hdrlen,
746 base->dtab, base_raw->f, base_raw->hdrlen,
747 base_raw->size + base_raw->hdrlen);
749 got_object_raw_close(base_raw);
754 size = delta_size(deltas, ndeltas);
755 if (size + 32 < best_size){
757 * if we already picked a best delta,
762 best_deltas = deltas;
763 best_ndeltas = ndeltas;
765 m->nchain = base->nchain + 1;
767 m->head = base->head;
777 if (best_ndeltas > 0) {
778 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
779 delta_memsize + best_size <= max_delta_memsize) {
780 delta_memsize += best_size;
781 err = encode_delta_in_mem(m, raw, best_deltas,
782 best_ndeltas, best_size, m->prev->size);
784 m->delta_offset = ftello(delta_cache);
787 * Storing compressed delta data in the delta
788 * cache file would probably be more efficient
789 * than writing uncompressed delta data here
790 * and compressing it while writing the pack
791 * file. This would also allow for reusing
792 * deltas in their compressed form.
794 err = encode_delta(m, raw, best_deltas,
795 best_ndeltas, m->prev->size, delta_cache);
804 got_object_raw_close(raw);
808 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
809 got_deltify_free(meta[i]->dtab);
810 meta[i]->dtab = NULL;
813 got_object_raw_close(raw);
815 got_object_raw_close(base_raw);
816 if (outfd != -1 && close(outfd) == -1 && err == NULL)
817 err = got_error_from_errno("close");
823 static const struct got_error *
824 search_packidx(int *found, struct got_object_id *id,
825 struct got_repository *repo)
827 const struct got_error *err = NULL;
828 struct got_packidx *packidx = NULL;
833 err = got_repo_search_packidx(&packidx, &idx, repo, id);
835 *found = 1; /* object is already packed */
836 else if (err->code == GOT_ERR_NO_OBJ)
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,
845 int *ncolored, int *nfound, int *ntrees,
846 got_pack_progress_cb progress_cb, void *progress_arg,
847 struct got_ratelimit *rl)
849 const struct got_error *err;
850 struct got_pack_meta *m = NULL;
852 if (loose_obj_only) {
854 err = search_packidx(&is_packed, id, repo);
857 if (is_packed && want_meta)
862 err = alloc_meta(&m, id, path, obj_type, mtime);
867 err = report_progress(progress_cb, progress_arg, rl,
868 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
876 err = got_object_idset_add(idset, id, m);
884 static const struct got_error *
885 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
886 struct got_object_idset *idset, struct got_object_id *tree_id,
887 const char *dpath, time_t mtime, struct got_repository *repo,
888 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
889 got_pack_progress_cb progress_cb, void *progress_arg,
890 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
892 const struct got_error *err;
893 struct got_tree_object *tree;
897 err = got_object_open_as_tree(&tree, repo, tree_id);
902 err = report_progress(progress_cb, progress_arg, rl,
903 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
907 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
908 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
909 struct got_object_id *id = got_tree_entry_get_id(e);
910 mode_t mode = got_tree_entry_get_mode(e);
913 err = (*cancel_cb)(cancel_arg);
918 if (got_object_tree_entry_is_submodule(e) ||
919 got_object_idset_contains(idset, id))
922 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
923 got_tree_entry_get_name(e)) == -1) {
924 err = got_error_from_errno("asprintf");
929 struct got_object_qid *qid;
930 err = got_object_qid_alloc(&qid, id);
933 STAILQ_INSERT_TAIL(ids, qid, entry);
934 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
935 err = add_object(want_meta, idset, id, p,
936 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
937 ncolored, nfound, ntrees,
938 progress_cb, progress_arg, rl);
946 got_object_tree_close(tree);
951 static const struct got_error *
952 load_tree(int want_meta, struct got_object_idset *idset,
953 struct got_object_id *tree_id, const char *dpath, time_t mtime,
954 struct got_repository *repo, int loose_obj_only,
955 int *ncolored, int *nfound, int *ntrees,
956 got_pack_progress_cb progress_cb, void *progress_arg,
957 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
959 const struct got_error *err = NULL;
960 struct got_object_id_queue tree_ids;
961 struct got_object_qid *qid;
963 if (got_object_idset_contains(idset, tree_id))
966 err = got_object_qid_alloc(&qid, tree_id);
970 STAILQ_INIT(&tree_ids);
971 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
973 while (!STAILQ_EMPTY(&tree_ids)) {
975 err = (*cancel_cb)(cancel_arg);
980 qid = STAILQ_FIRST(&tree_ids);
981 STAILQ_REMOVE_HEAD(&tree_ids, entry);
983 if (got_object_idset_contains(idset, qid->id)) {
984 got_object_qid_free(qid);
988 err = add_object(want_meta, idset, qid->id, dpath,
989 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo,
990 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
992 got_object_qid_free(qid);
996 err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
997 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
998 ntrees, progress_cb, progress_arg, rl,
999 cancel_cb, cancel_arg);
1000 got_object_qid_free(qid);
1005 got_object_id_queue_free(&tree_ids);
1009 static const struct got_error *
1010 load_commit(int want_meta, struct got_object_idset *idset,
1011 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1012 int *ncolored, int *nfound, int *ntrees,
1013 got_pack_progress_cb progress_cb, void *progress_arg,
1014 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1016 const struct got_error *err;
1017 struct got_commit_object *commit;
1019 if (got_object_idset_contains(idset, id))
1022 if (loose_obj_only) {
1024 err = search_packidx(&is_packed, id, repo);
1027 if (is_packed && want_meta)
1031 err = got_object_open_as_commit(&commit, repo, id);
1035 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1036 got_object_commit_get_committer_time(commit),
1037 loose_obj_only, repo,
1038 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1042 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1043 "", got_object_commit_get_committer_time(commit),
1044 repo, loose_obj_only, ncolored, nfound, ntrees,
1045 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1047 got_object_commit_close(commit);
1051 static const struct got_error *
1052 load_tag(int want_meta, struct got_object_idset *idset,
1053 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1054 int *ncolored, int *nfound, int *ntrees,
1055 got_pack_progress_cb progress_cb, void *progress_arg,
1056 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1058 const struct got_error *err;
1059 struct got_tag_object *tag = NULL;
1061 if (got_object_idset_contains(idset, id))
1064 if (loose_obj_only) {
1066 err = search_packidx(&is_packed, id, repo);
1069 if (is_packed && want_meta)
1073 err = got_object_open_as_tag(&tag, repo, id);
1077 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1078 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1079 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1083 switch (got_object_tag_get_object_type(tag)) {
1084 case GOT_OBJ_TYPE_COMMIT:
1085 err = load_commit(want_meta, idset,
1086 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1087 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1088 cancel_cb, cancel_arg);
1090 case GOT_OBJ_TYPE_TREE:
1091 err = load_tree(want_meta, idset,
1092 got_object_tag_get_object_id(tag), "",
1093 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1094 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1095 cancel_cb, cancel_arg);
1102 got_object_tag_close(tag);
1106 enum findtwixt_color {
1111 static const int findtwixt_colors[] = {
1117 static const struct got_error *
1118 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1119 int color, struct got_repository *repo)
1121 const struct got_error *err;
1122 struct got_object_qid *qid;
1124 err = got_object_qid_alloc(&qid, id);
1128 STAILQ_INSERT_TAIL(ids, qid, entry);
1129 qid->data = (void *)&findtwixt_colors[color];
1133 static const struct got_error *
1134 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1135 struct got_object_id *id, struct got_repository *repo,
1136 got_cancel_cb cancel_cb, void *cancel_arg)
1138 const struct got_error *err = NULL;
1139 struct got_commit_object *commit;
1140 const struct got_object_id_queue *parents;
1141 struct got_object_id_queue ids;
1142 struct got_object_qid *qid;
1146 err = got_object_qid_alloc(&qid, id);
1149 STAILQ_INSERT_HEAD(&ids, qid, entry);
1151 while (!STAILQ_EMPTY(&ids)) {
1153 err = (*cancel_cb)(cancel_arg);
1158 qid = STAILQ_FIRST(&ids);
1159 STAILQ_REMOVE_HEAD(&ids, entry);
1161 if (got_object_idset_contains(drop, qid->id)) {
1162 got_object_qid_free(qid);
1166 err = got_object_idset_add(drop, qid->id, NULL);
1168 got_object_qid_free(qid);
1172 if (!got_object_idset_contains(keep, qid->id)) {
1173 got_object_qid_free(qid);
1177 err = got_object_open_as_commit(&commit, repo, qid->id);
1178 got_object_qid_free(qid);
1182 parents = got_object_commit_get_parent_ids(commit);
1184 err = got_object_id_queue_copy(parents, &ids);
1186 got_object_commit_close(commit);
1190 got_object_commit_close(commit);
1193 got_object_id_queue_free(&ids);
1197 struct append_id_arg {
1198 struct got_object_id **array;
1202 static const struct got_error *
1203 append_id(struct got_object_id *id, void *data, void *arg)
1205 struct append_id_arg *a = arg;
1207 a->array[a->idx] = got_object_id_dup(id);
1208 if (a->array[a->idx] == NULL)
1209 return got_error_from_errno("got_object_id_dup");
1215 static const struct got_error *
1216 queue_commit_or_tag_id(struct got_object_id *id, int color,
1217 struct got_object_id_queue *ids, struct got_repository *repo)
1219 const struct got_error *err;
1220 struct got_tag_object *tag = NULL;
1223 err = got_object_get_type(&obj_type, repo, id);
1227 if (obj_type == GOT_OBJ_TYPE_TAG) {
1228 err = got_object_open_as_tag(&tag, repo, id);
1231 obj_type = got_object_tag_get_object_type(tag);
1232 id = got_object_tag_get_object_id(tag);
1235 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1236 err = queue_commit_id(ids, id, color, repo);
1242 got_object_tag_close(tag);
1246 static const struct got_error *
1247 color_commits(int *ncolored, struct got_object_id_queue *ids,
1248 struct got_object_idset *keep, struct got_object_idset *drop,
1249 struct got_repository *repo,
1250 got_pack_progress_cb progress_cb, void *progress_arg,
1251 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1253 const struct got_error *err = NULL;
1254 struct got_commit_object *commit = NULL;
1255 struct got_object_qid *qid;
1257 while (!STAILQ_EMPTY(ids)) {
1261 err = cancel_cb(cancel_arg);
1266 qid = STAILQ_FIRST(ids);
1267 qcolor = *((int *)qid->data);
1269 if (got_object_idset_contains(drop, qid->id))
1270 ncolor = COLOR_DROP;
1271 else if (got_object_idset_contains(keep, qid->id))
1272 ncolor = COLOR_KEEP;
1274 ncolor = COLOR_BLANK;
1277 err = report_progress(progress_cb, progress_arg, rl,
1278 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1282 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1283 qcolor == COLOR_KEEP)) {
1284 STAILQ_REMOVE_HEAD(ids, entry);
1285 got_object_qid_free(qid);
1289 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1290 err = drop_commit(keep, drop, qid->id, repo,
1291 cancel_cb, cancel_arg);
1294 } else if (ncolor == COLOR_BLANK) {
1295 struct got_commit_object *commit;
1296 const struct got_object_id_queue *parents;
1297 struct got_object_qid *pid;
1299 if (qcolor == COLOR_KEEP)
1300 err = got_object_idset_add(keep, qid->id, NULL);
1302 err = got_object_idset_add(drop, qid->id, NULL);
1306 err = got_object_open_as_commit(&commit, repo, qid->id);
1310 parents = got_object_commit_get_parent_ids(commit);
1312 STAILQ_FOREACH(pid, parents, entry) {
1313 err = queue_commit_id(ids, pid->id,
1319 got_object_commit_close(commit);
1322 /* should not happen */
1323 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1324 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1328 STAILQ_REMOVE_HEAD(ids, entry);
1329 got_object_qid_free(qid);
1333 got_object_commit_close(commit);
1337 static const struct got_error *
1338 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1339 struct got_object_id **head, int nhead,
1340 struct got_object_id **tail, int ntail,
1341 struct got_repository *repo,
1342 got_pack_progress_cb progress_cb, void *progress_arg,
1343 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1345 const struct got_error *err = NULL;
1346 struct got_object_id_queue ids;
1347 struct got_object_idset *keep, *drop;
1355 keep = got_object_idset_alloc();
1357 return got_error_from_errno("got_object_idset_alloc");
1359 drop = got_object_idset_alloc();
1361 err = got_error_from_errno("got_object_idset_alloc");
1365 for (i = 0; i < nhead; i++) {
1366 struct got_object_id *id = head[i];
1369 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1374 for (i = 0; i < ntail; i++) {
1375 struct got_object_id *id = tail[i];
1378 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1383 err = color_commits(ncolored, &ids, keep, drop, repo,
1384 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1388 nkeep = got_object_idset_num_elements(keep);
1390 struct append_id_arg arg;
1391 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1392 if (arg.array == NULL) {
1393 err = got_error_from_errno("calloc");
1397 err = got_object_idset_for_each(keep, append_id, &arg);
1406 got_object_idset_free(keep);
1407 got_object_idset_free(drop);
1408 got_object_id_queue_free(&ids);
1412 static const struct got_error *
1413 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1414 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1415 struct got_object_id **ours, int nours, struct got_repository *repo,
1416 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1417 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1419 const struct got_error *err = NULL;
1420 struct got_object_id **ids = NULL;
1421 int i, nobj = 0, obj_type;
1427 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1428 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1429 if (err || nobj == 0)
1432 for (i = 0; i < ntheirs; i++) {
1433 struct got_object_id *id = theirs[i];
1436 err = got_object_get_type(&obj_type, repo, id);
1439 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1440 err = load_commit(0, idset, id, repo,
1441 loose_obj_only, ncolored, nfound, ntrees,
1442 progress_cb, progress_arg, rl,
1443 cancel_cb, cancel_arg);
1446 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1447 err = load_tag(0, idset, id, repo,
1448 loose_obj_only, ncolored, nfound, ntrees,
1449 progress_cb, progress_arg, rl,
1450 cancel_cb, cancel_arg);
1456 for (i = 0; i < nobj; i++) {
1457 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1458 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1459 cancel_cb, cancel_arg);
1464 for (i = 0; i < nours; i++) {
1465 struct got_object_id *id = ours[i];
1466 struct got_pack_meta *m;
1469 m = got_object_idset_get(idset, id);
1471 err = got_object_get_type(&obj_type, repo, id);
1475 obj_type = m->obj_type;
1476 if (obj_type != GOT_OBJ_TYPE_TAG)
1478 err = load_tag(1, idset, id, repo, loose_obj_only,
1479 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1480 cancel_cb, cancel_arg);
1485 for (i = 0; i < nobj; i++) {
1492 const struct got_error *
1493 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1497 SHA1Update(ctx, buf, len);
1498 n = fwrite(buf, 1, len, f);
1500 return got_ferror(f, GOT_ERR_IO);
1505 putbe32(char *b, uint32_t n)
1514 write_order_cmp(const void *pa, const void *pb)
1516 struct got_pack_meta *a, *b, *ahd, *bhd;
1518 a = *(struct got_pack_meta **)pa;
1519 b = *(struct got_pack_meta **)pb;
1520 ahd = (a->head == NULL) ? a : a->head;
1521 bhd = (b->head == NULL) ? b : b->head;
1522 if (ahd->mtime != bhd->mtime)
1523 return bhd->mtime - ahd->mtime;
1525 return (uintptr_t)bhd - (uintptr_t)ahd;
1526 if (a->nchain != b->nchain)
1527 return a->nchain - b->nchain;
1528 return a->mtime - b->mtime;
1532 reuse_write_order_cmp(const void *pa, const void *pb)
1534 struct got_pack_meta *a, *b;
1536 a = *(struct got_pack_meta **)pa;
1537 b = *(struct got_pack_meta **)pb;
1539 if (a->reused_delta_offset < b->reused_delta_offset)
1541 if (a->reused_delta_offset > b->reused_delta_offset)
1546 static const struct got_error *
1547 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1553 hdr[0] = obj_type << 4;
1554 hdr[0] |= len & 0xf;
1556 for (i = 1; len != 0; i++){
1558 return got_error(GOT_ERR_NO_SPACE);
1559 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1560 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1561 len >>= GOT_DELTA_SIZE_SHIFT;
1569 packoff(char *hdr, off_t off)
1574 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1575 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1576 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1577 GOT_DELTA_SIZE_MORE;
1582 hdr[j++] = rbuf[--i];
1586 static const struct got_error *
1587 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1588 struct got_pack_meta *m)
1590 const struct got_error *err;
1594 if (m->prev->off != 0) {
1595 err = packhdr(&nh, buf, sizeof(buf),
1596 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1599 nh += packoff(buf + nh, m->off - m->prev->off);
1600 err = hwrite(packfile, buf, nh, ctx);
1603 *packfile_size += nh;
1605 err = packhdr(&nh, buf, sizeof(buf),
1606 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1609 err = hwrite(packfile, buf, nh, ctx);
1612 *packfile_size += nh;
1613 err = hwrite(packfile, m->prev->id.sha1,
1614 sizeof(m->prev->id.sha1), ctx);
1617 *packfile_size += sizeof(m->prev->id.sha1);
1623 static const struct got_error *
1624 write_packed_object(off_t *packfile_size, FILE *packfile,
1625 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1626 SHA1_CTX *ctx, struct got_repository *repo)
1628 const struct got_error *err = NULL;
1629 struct got_deflate_checksum csum;
1632 struct got_raw_object *raw = NULL;
1635 csum.output_sha1 = ctx;
1636 csum.output_crc = NULL;
1638 m->off = ftello(packfile);
1639 if (m->delta_len == 0) {
1640 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1643 err = packhdr(&nh, buf, sizeof(buf),
1644 m->obj_type, raw->size);
1647 err = hwrite(packfile, buf, nh, ctx);
1650 *packfile_size += nh;
1651 if (raw->f == NULL) {
1652 err = got_deflate_to_file_mmap(&outlen,
1653 raw->data + raw->hdrlen, 0, raw->size,
1658 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1660 err = got_error_from_errno("fseeko");
1663 err = got_deflate_to_file(&outlen, raw->f,
1664 raw->size, packfile, &csum);
1668 *packfile_size += outlen;
1669 got_object_raw_close(raw);
1671 } else if (m->delta_buf) {
1672 err = deltahdr(packfile_size, ctx, packfile, m);
1675 err = got_deflate_to_file_mmap(&outlen,
1676 m->delta_buf, 0, m->delta_len, packfile, &csum);
1679 *packfile_size += outlen;
1681 m->delta_buf = NULL;
1683 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1685 err = got_error_from_errno("fseeko");
1688 err = deltahdr(packfile_size, ctx, packfile, m);
1691 err = got_deflate_to_file(&outlen, delta_cache,
1692 m->delta_len, packfile, &csum);
1695 *packfile_size += outlen;
1699 got_object_raw_close(raw);
1703 static const struct got_error *
1704 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1705 struct got_pack_meta **deltify, int ndeltify,
1706 struct got_pack_meta **reuse, int nreuse,
1707 int ncolored, int nfound, int ntrees, int nours,
1708 struct got_repository *repo,
1709 got_pack_progress_cb progress_cb, void *progress_arg,
1710 struct got_ratelimit *rl,
1711 got_cancel_cb cancel_cb, void *cancel_arg)
1713 const struct got_error *err = NULL;
1716 struct got_pack_meta *m;
1719 off_t packfile_size = 0;
1724 err = hwrite(packfile, "PACK", 4, &ctx);
1727 putbe32(buf, GOT_PACKFILE_VERSION);
1728 err = hwrite(packfile, buf, 4, &ctx);
1731 putbe32(buf, ndeltify + nreuse);
1732 err = hwrite(packfile, buf, 4, &ctx);
1736 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1738 for (i = 0; i < ndeltify; i++) {
1739 err = report_progress(progress_cb, progress_arg, rl,
1740 ncolored, nfound, ntrees, packfile_size, nours,
1741 ndeltify + nreuse, ndeltify + nreuse, i);
1745 err = write_packed_object(&packfile_size, packfile,
1746 delta_cache, m, &outfd, &ctx, repo);
1751 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1752 reuse_write_order_cmp);
1753 for (i = 0; i < nreuse; i++) {
1754 err = report_progress(progress_cb, progress_arg, rl,
1755 ncolored, nfound, ntrees, packfile_size, nours,
1756 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1760 err = write_packed_object(&packfile_size, packfile,
1761 delta_cache, m, &outfd, &ctx, repo);
1766 SHA1Final(pack_sha1, &ctx);
1767 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1768 if (n != SHA1_DIGEST_LENGTH)
1769 err = got_ferror(packfile, GOT_ERR_IO);
1770 packfile_size += SHA1_DIGEST_LENGTH;
1771 packfile_size += sizeof(struct got_packfile_hdr);
1773 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1774 packfile_size, nours, ndeltify + nreuse,
1775 ndeltify + nreuse, ndeltify + nreuse);
1780 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1781 err = got_error_from_errno("close");
1785 static const struct got_error *
1786 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1788 struct got_object_idset *idset = arg;
1790 if (got_object_idset_get_element_data(entry) == NULL)
1791 got_object_idset_remove_element(idset, entry);
1796 static const struct got_error *
1797 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1799 struct got_object_idset *idset = arg;
1800 struct got_pack_meta *m;
1802 m = got_object_idset_get_element_data(entry);
1803 if (m->have_reused_delta)
1804 got_object_idset_remove_element(idset, entry);
1809 static const struct got_error *
1810 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1812 struct got_pack_meta *m = data;
1813 struct got_pack_metavec *v = arg;
1815 return add_meta(m, v);
1818 const struct got_error *
1819 got_pack_create(uint8_t *packsha1, FILE *packfile,
1820 struct got_object_id **theirs, int ntheirs,
1821 struct got_object_id **ours, int nours,
1822 struct got_repository *repo, int loose_obj_only, int allow_empty,
1823 got_pack_progress_cb progress_cb, void *progress_arg,
1824 got_cancel_cb cancel_cb, void *cancel_arg)
1826 const struct got_error *err;
1827 int delta_cache_fd = -1;
1828 FILE *delta_cache = NULL;
1829 struct got_object_idset *idset;
1830 struct got_ratelimit rl;
1831 struct got_pack_metavec deltify, reuse;
1832 int ncolored = 0, nfound = 0, ntrees = 0;
1834 memset(&deltify, 0, sizeof(deltify));
1835 memset(&reuse, 0, sizeof(reuse));
1837 got_ratelimit_init(&rl, 0, 500);
1839 idset = got_object_idset_alloc();
1841 return got_error_from_errno("got_object_idset_alloc");
1843 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1844 ntheirs, ours, nours, repo, loose_obj_only,
1845 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1849 err = got_object_idset_for_each_element(idset,
1850 remove_unused_object, idset);
1855 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1856 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1861 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1862 err = got_error(GOT_ERR_CANNOT_PACK);
1866 delta_cache_fd = got_opentempfd();
1867 if (delta_cache_fd == -1) {
1868 err = got_error_from_errno("got_opentemp");
1873 reuse.meta = calloc(reuse.metasz,
1874 sizeof(struct got_pack_meta *));
1875 if (reuse.meta == NULL) {
1876 err = got_error_from_errno("calloc");
1880 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1881 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1882 cancel_cb, cancel_arg);
1885 if (reuse.nmeta > 0) {
1886 err = got_object_idset_for_each_element(idset,
1887 remove_reused_object, idset);
1892 delta_cache = fdopen(delta_cache_fd, "a+");
1893 if (delta_cache == NULL) {
1894 err = got_error_from_errno("fdopen");
1897 delta_cache_fd = -1;
1899 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1900 err = got_error_from_errno("fseeko");
1904 deltify.meta = calloc(got_object_idset_num_elements(idset),
1905 sizeof(struct got_pack_meta *));
1906 if (deltify.meta == NULL) {
1907 err = got_error_from_errno("calloc");
1910 deltify.metasz = got_object_idset_num_elements(idset);
1912 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1915 if (deltify.nmeta > 0) {
1916 err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1917 nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1918 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1921 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1922 err = got_error_from_errno("fseeko");
1927 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1928 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1929 nours, repo, progress_cb, progress_arg, &rl,
1930 cancel_cb, cancel_arg);
1934 free_nmeta(deltify.meta, deltify.nmeta);
1935 free_nmeta(reuse.meta, reuse.nmeta);
1936 got_object_idset_free(idset);
1937 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1938 err = got_error_from_errno("close");
1939 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1940 err = got_error_from_errno("fclose");