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"
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 struct got_pack_meta {
65 struct got_object_id id;
71 /* The best delta we picked */
72 struct got_pack_meta *head;
73 struct got_pack_meta *prev;
74 unsigned char *delta_buf; /* if not encoded in delta cache file */
75 off_t delta_offset; /* offset in delta cache file */
76 off_t delta_len; /* encoded delta length */
79 int have_reused_delta;
80 off_t reused_delta_offset; /* offset of delta in reused pack file */
81 struct got_object_id *base_obj_id;
83 /* Only used for delta window */
84 struct got_delta_table *dtab;
86 /* Only used for writing offset deltas */
90 struct got_pack_metavec {
91 struct got_pack_meta **meta;
96 static const struct got_error *
97 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
98 const char *path, int obj_type, time_t mtime)
100 const struct got_error *err = NULL;
101 struct got_pack_meta *m;
105 m = calloc(1, sizeof(*m));
107 return got_error_from_errno("calloc");
109 memcpy(&m->id, id, sizeof(m->id));
111 m->path = strdup(path);
112 if (m->path == NULL) {
113 err = got_error_from_errno("strdup");
118 m->obj_type = obj_type;
125 clear_meta(struct got_pack_meta *meta)
131 free(meta->delta_buf);
132 meta->delta_buf = NULL;
133 free(meta->base_obj_id);
134 meta->base_obj_id = NULL;
138 free_nmeta(struct got_pack_meta **meta, int nmeta)
142 for (i = 0; i < nmeta; i++)
148 delta_order_cmp(const void *pa, const void *pb)
150 struct got_pack_meta *a, *b;
153 a = *(struct got_pack_meta **)pa;
154 b = *(struct got_pack_meta **)pb;
156 if (a->obj_type != b->obj_type)
157 return a->obj_type - b->obj_type;
158 cmp = strcmp(a->path, b->path);
161 if (a->mtime != b->mtime)
162 return a->mtime - b->mtime;
163 return got_object_id_cmp(&a->id, &b->id);
167 delta_size(struct got_delta_instruction *deltas, int ndeltas)
171 for (i = 0; i < ndeltas; i++) {
173 size += GOT_DELTA_SIZE_SHIFT;
175 size += deltas[i].len + 1;
180 static const struct got_error *
181 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
185 if (*len + nseg >= *sz) {
186 while (*len + nseg >= *sz)
188 n = realloc(*p, *sz);
190 return got_error_from_errno("realloc");
193 memcpy(*p + *len, seg, nseg);
198 static const struct got_error *
199 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
200 struct got_delta_instruction *deltas, int ndeltas,
201 off_t delta_size, off_t base_size)
203 const struct got_error *err;
204 unsigned char buf[16], *bp;
208 struct got_delta_instruction *d;
210 m->delta_buf = malloc(delta_size);
211 if (m->delta_buf == NULL)
212 return got_error_from_errno("calloc");
214 /* base object size */
215 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
216 n = base_size >> GOT_DELTA_SIZE_SHIFT;
217 for (i = 1; n > 0; i++) {
218 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
219 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
220 n >>= GOT_DELTA_SIZE_SHIFT;
222 err = append(&m->delta_buf, &len, &delta_size, buf, i);
226 /* target object size */
227 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
228 n = o->size >> GOT_DELTA_SIZE_SHIFT;
229 for (i = 1; n > 0; i++) {
230 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
231 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
232 n >>= GOT_DELTA_SIZE_SHIFT;
234 err = append(&m->delta_buf, &len, &delta_size, buf, i);
238 for (j = 0; j < ndeltas; j++) {
243 buf[0] = GOT_DELTA_BASE_COPY;
244 for (i = 0; i < 4; i++) {
245 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
254 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
255 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
256 for (i = 0; i < 3 && n > 0; i++) {
257 buf[0] |= 1 << (i + 4);
262 err = append(&m->delta_buf, &len, &delta_size,
266 } else if (o->f == NULL) {
268 while (n != d->len) {
269 buf[0] = (d->len - n < 127) ? d->len - n : 127;
270 err = append(&m->delta_buf, &len, &delta_size,
274 err = append(&m->delta_buf, &len, &delta_size,
275 o->data + o->hdrlen + d->offset + n,
284 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
285 return got_error_from_errno("fseeko");
287 while (n != d->len) {
288 buf[0] = (d->len - n < 127) ? d->len - n : 127;
289 err = append(&m->delta_buf, &len, &delta_size,
293 r = fread(content, 1, buf[0], o->f);
295 return got_ferror(o->f, GOT_ERR_IO);
296 err = append(&m->delta_buf, &len, &delta_size,
309 static const struct got_error *
310 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
311 struct got_delta_instruction *deltas, int ndeltas,
312 off_t base_size, FILE *f)
314 unsigned char buf[16], *bp;
318 struct got_delta_instruction *d;
320 /* base object size */
321 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
322 n = base_size >> GOT_DELTA_SIZE_SHIFT;
323 for (i = 1; n > 0; i++) {
324 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
325 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
326 n >>= GOT_DELTA_SIZE_SHIFT;
328 w = fwrite(buf, 1, i, f);
330 return got_ferror(f, GOT_ERR_IO);
332 /* target object size */
333 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
334 n = o->size >> GOT_DELTA_SIZE_SHIFT;
335 for (i = 1; n > 0; i++) {
336 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
337 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
338 n >>= GOT_DELTA_SIZE_SHIFT;
340 w = fwrite(buf, 1, i, f);
342 return got_ferror(f, GOT_ERR_IO);
344 for (j = 0; j < ndeltas; j++) {
349 buf[0] = GOT_DELTA_BASE_COPY;
350 for (i = 0; i < 4; i++) {
351 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
360 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
361 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
362 for (i = 0; i < 3 && n > 0; i++) {
363 buf[0] |= 1 << (i + 4);
368 w = fwrite(buf, 1, bp - buf, f);
370 return got_ferror(f, GOT_ERR_IO);
371 } else if (o->f == NULL) {
373 while (n != d->len) {
374 buf[0] = (d->len - n < 127) ? d->len - n : 127;
375 w = fwrite(buf, 1, 1, f);
377 return got_ferror(f, GOT_ERR_IO);
378 w = fwrite(o->data + o->hdrlen + d->offset + n,
381 return got_ferror(f, GOT_ERR_IO);
387 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
388 return got_error_from_errno("fseeko");
390 while (n != d->len) {
391 buf[0] = (d->len - n < 127) ? d->len - n : 127;
392 w = fwrite(buf, 1, 1, f);
394 return got_ferror(f, GOT_ERR_IO);
395 r = fread(content, 1, buf[0], o->f);
397 return got_ferror(o->f, GOT_ERR_IO);
398 w = fwrite(content, 1, buf[0], f);
400 return got_ferror(f, GOT_ERR_IO);
406 m->delta_len = ftello(f) - m->delta_offset;
410 static const struct got_error *
411 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
412 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
413 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
416 const struct got_error *err;
419 if (progress_cb == NULL)
422 err = got_ratelimit_check(&elapsed, rl);
426 return progress_cb(progress_arg, ncolored, nfound, ntrees,
427 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
430 static const struct got_error *
431 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
433 if (v->nmeta == v->metasz){
434 size_t newsize = 2 * v->metasz;
435 struct got_pack_meta **new;
436 new = reallocarray(v->meta, newsize, sizeof(*new));
438 return got_error_from_errno("reallocarray");
443 v->meta[v->nmeta++] = m;
447 static const struct got_error *
448 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
449 struct got_object_idset *idset, struct got_pack *pack,
450 struct got_packidx *packidx, int delta_cache_fd,
451 struct got_repository *repo)
453 const struct got_error *err = NULL;
454 struct got_pack_meta *base = NULL;
455 struct got_object_id *base_obj_id = NULL;
456 off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
457 uint64_t base_size, result_size;
459 if (m->have_reused_delta)
462 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
463 &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
464 packidx, idx, &m->id, repo);
468 if (delta_offset + delta_len < delta_offset)
469 return got_error(GOT_ERR_BAD_PACKFILE);
471 base = got_object_idset_get(idset, base_obj_id);
475 m->delta_len = delta_len;
476 m->delta_offset = delta_cache_offset;
478 m->size = result_size;
479 m->have_reused_delta = 1;
480 m->reused_delta_offset = delta_offset;
481 m->base_obj_id = base_obj_id;
483 err = add_meta(m, v);
489 static const struct got_error *
490 find_pack_for_reuse(struct got_packidx **best_packidx,
491 struct got_repository *repo)
493 const struct got_error *err = NULL;
494 struct got_pathlist_entry *pe;
495 const char *best_packidx_path = NULL;
498 *best_packidx = NULL;
500 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
501 const char *path_packidx = pe->path;
502 struct got_packidx *packidx;
505 err = got_repo_get_packidx(&packidx, path_packidx, repo);
509 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
510 if (nobj > nobj_max) {
511 best_packidx_path = path_packidx;
516 if (best_packidx_path) {
517 err = got_repo_get_packidx(best_packidx, best_packidx_path,
524 struct search_deltas_arg {
525 struct got_packidx *packidx;
526 struct got_pack *pack;
527 struct got_object_idset *idset;
528 struct got_pack_metavec *v;
530 struct got_repository *repo;
531 got_pack_progress_cb progress_cb;
533 struct got_ratelimit *rl;
534 got_cancel_cb cancel_cb;
542 static const struct got_error *
543 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
545 const struct got_error *err;
546 struct got_pack_meta *m = data;
547 struct search_deltas_arg *a = arg;
549 struct got_object *obj = NULL;
552 err = (*a->cancel_cb)(a->cancel_arg);
557 if (!got_repo_check_packidx_bloom_filter(a->repo,
558 a->packidx->path_packidx, id))
561 obj_idx = got_packidx_get_object_idx(a->packidx, id);
566 * Opening and closing an object just to check its flags
567 * is a bit expensive. We could have an imsg which requests
568 * plain type/size information for an object without doing
569 * work such as traversing the object's entire delta chain
570 * to find the base object type, and other such info which
571 * we don't really need here.
573 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
574 a->packidx, obj_idx, a->repo);
578 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
579 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
580 a->delta_cache_fd, a->repo);
583 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
584 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
585 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
588 got_object_close(obj);
592 static const struct got_error *
593 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
594 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
595 struct got_repository *repo,
596 got_pack_progress_cb progress_cb, void *progress_arg,
597 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
599 const struct got_error *err = NULL;
600 char *path_packfile = NULL;
601 struct got_packidx *packidx;
602 struct got_pack *pack;
603 struct search_deltas_arg sda;
605 err = find_pack_for_reuse(&packidx, repo);
612 err = got_packidx_get_packfile_path(&path_packfile,
613 packidx->path_packidx);
617 pack = got_repo_get_cached_pack(repo, path_packfile);
619 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
624 sda.packidx = packidx;
628 sda.delta_cache_fd = delta_cache_fd;
630 sda.progress_cb = progress_cb;
631 sda.progress_arg = progress_arg;
633 sda.cancel_cb = cancel_cb;
634 sda.cancel_arg = cancel_arg;
635 sda.ncolored = ncolored;
638 sda.ncommits = ncommits;
639 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
645 static const struct got_error *
646 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
647 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
648 struct got_repository *repo,
649 got_pack_progress_cb progress_cb, void *progress_arg,
650 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
652 const struct got_error *err = NULL;
653 struct got_pack_meta *m = NULL, *base = NULL;
654 struct got_raw_object *raw = NULL, *base_raw = NULL;
655 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
656 int i, j, ndeltas, best_ndeltas;
657 off_t size, best_size;
658 const int max_base_candidates = 3;
659 size_t delta_memsize = 0;
660 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
663 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
664 for (i = 0; i < nmeta; i++) {
666 err = (*cancel_cb)(cancel_arg);
670 err = report_progress(progress_cb, progress_arg, rl,
671 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
677 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
678 m->obj_type == GOT_OBJ_TYPE_TAG)
681 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
686 if (raw->f == NULL) {
687 err = got_deltify_init_mem(&m->dtab, raw->data,
688 raw->hdrlen, raw->size + raw->hdrlen);
690 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
691 raw->size + raw->hdrlen);
696 if (i > max_base_candidates) {
697 struct got_pack_meta *n = NULL;
698 n = meta[i - (max_base_candidates + 1)];
699 got_deltify_free(n->dtab);
703 best_size = raw->size;
705 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
707 err = (*cancel_cb)(cancel_arg);
712 /* long chains make unpacking slow, avoid such bases */
713 if (base->nchain >= 128 ||
714 base->obj_type != m->obj_type)
717 err = got_object_raw_open(&base_raw, &outfd, repo,
722 if (raw->f == NULL && base_raw->f == NULL) {
723 err = got_deltify_mem_mem(&deltas, &ndeltas,
724 raw->data, raw->hdrlen,
725 raw->size + raw->hdrlen,
726 base->dtab, base_raw->data,
728 base_raw->size + base_raw->hdrlen);
729 } else if (raw->f == NULL) {
730 err = got_deltify_mem_file(&deltas, &ndeltas,
731 raw->data, raw->hdrlen,
732 raw->size + raw->hdrlen,
733 base->dtab, base_raw->f,
735 base_raw->size + base_raw->hdrlen);
736 } else if (base_raw->f == NULL) {
737 err = got_deltify_file_mem(&deltas, &ndeltas,
739 raw->size + raw->hdrlen,
740 base->dtab, base_raw->data,
742 base_raw->size + base_raw->hdrlen);
744 err = got_deltify(&deltas, &ndeltas,
746 raw->size + raw->hdrlen,
747 base->dtab, base_raw->f, base_raw->hdrlen,
748 base_raw->size + base_raw->hdrlen);
750 got_object_raw_close(base_raw);
755 size = delta_size(deltas, ndeltas);
756 if (size + 32 < best_size){
758 * if we already picked a best delta,
763 best_deltas = deltas;
764 best_ndeltas = ndeltas;
766 m->nchain = base->nchain + 1;
768 m->head = base->head;
778 if (best_ndeltas > 0) {
779 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
780 delta_memsize + best_size <= max_delta_memsize) {
781 delta_memsize += best_size;
782 err = encode_delta_in_mem(m, raw, best_deltas,
783 best_ndeltas, best_size, m->prev->size);
785 m->delta_offset = ftello(delta_cache);
788 * Storing compressed delta data in the delta
789 * cache file would probably be more efficient
790 * than writing uncompressed delta data here
791 * and compressing it while writing the pack
792 * file. This would also allow for reusing
793 * deltas in their compressed form.
795 err = encode_delta(m, raw, best_deltas,
796 best_ndeltas, m->prev->size, delta_cache);
805 got_object_raw_close(raw);
809 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
810 got_deltify_free(meta[i]->dtab);
811 meta[i]->dtab = NULL;
814 got_object_raw_close(raw);
816 got_object_raw_close(base_raw);
817 if (outfd != -1 && close(outfd) == -1 && err == NULL)
818 err = got_error_from_errno("close");
824 static const struct got_error *
825 search_packidx(int *found, struct got_object_id *id,
826 struct got_repository *repo)
828 const struct got_error *err = NULL;
829 struct got_packidx *packidx = NULL;
834 err = got_repo_search_packidx(&packidx, &idx, repo, id);
836 *found = 1; /* object is already packed */
837 else if (err->code == GOT_ERR_NO_OBJ)
842 static const struct got_error *
843 add_object(int want_meta, struct got_object_idset *idset,
844 struct got_object_id *id, const char *path, int obj_type,
845 time_t mtime, int loose_obj_only, struct got_repository *repo,
846 int *ncolored, int *nfound, int *ntrees,
847 got_pack_progress_cb progress_cb, void *progress_arg,
848 struct got_ratelimit *rl)
850 const struct got_error *err;
851 struct got_pack_meta *m = NULL;
853 if (loose_obj_only) {
855 err = search_packidx(&is_packed, id, repo);
858 if (is_packed && want_meta)
863 err = alloc_meta(&m, id, path, obj_type, mtime);
868 err = report_progress(progress_cb, progress_arg, rl,
869 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
874 return got_object_idset_add(idset, id, m);
877 static const struct got_error *
878 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
879 struct got_object_idset *idset, struct got_object_id *tree_id,
880 const char *dpath, time_t mtime, struct got_repository *repo,
881 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
882 got_pack_progress_cb progress_cb, void *progress_arg,
883 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
885 const struct got_error *err;
886 struct got_tree_object *tree;
890 err = got_object_open_as_tree(&tree, repo, tree_id);
895 err = report_progress(progress_cb, progress_arg, rl,
896 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
900 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
901 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
902 struct got_object_id *id = got_tree_entry_get_id(e);
903 mode_t mode = got_tree_entry_get_mode(e);
906 err = (*cancel_cb)(cancel_arg);
911 if (got_object_tree_entry_is_submodule(e) ||
912 got_object_idset_contains(idset, id))
915 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
916 got_tree_entry_get_name(e)) == -1) {
917 err = got_error_from_errno("asprintf");
922 struct got_object_qid *qid;
923 err = got_object_qid_alloc(&qid, id);
926 STAILQ_INSERT_TAIL(ids, qid, entry);
927 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
928 err = add_object(want_meta, idset, id, p,
929 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
930 ncolored, nfound, ntrees,
931 progress_cb, progress_arg, rl);
939 got_object_tree_close(tree);
944 static const struct got_error *
945 load_tree(int want_meta, struct got_object_idset *idset,
946 struct got_object_id *tree_id, const char *dpath, time_t mtime,
947 struct got_repository *repo, int loose_obj_only,
948 int *ncolored, int *nfound, int *ntrees,
949 got_pack_progress_cb progress_cb, void *progress_arg,
950 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
952 const struct got_error *err = NULL;
953 struct got_object_id_queue tree_ids;
954 struct got_object_qid *qid;
956 if (got_object_idset_contains(idset, tree_id))
959 err = got_object_qid_alloc(&qid, tree_id);
963 STAILQ_INIT(&tree_ids);
964 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
966 while (!STAILQ_EMPTY(&tree_ids)) {
968 err = (*cancel_cb)(cancel_arg);
973 qid = STAILQ_FIRST(&tree_ids);
974 STAILQ_REMOVE_HEAD(&tree_ids, entry);
976 if (got_object_idset_contains(idset, qid->id)) {
977 got_object_qid_free(qid);
981 err = add_object(want_meta, idset, qid->id, dpath,
982 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo,
983 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
985 got_object_qid_free(qid);
989 err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
990 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
991 ntrees, progress_cb, progress_arg, rl,
992 cancel_cb, cancel_arg);
993 got_object_qid_free(qid);
998 got_object_id_queue_free(&tree_ids);
1002 static const struct got_error *
1003 load_commit(int want_meta, struct got_object_idset *idset,
1004 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1005 int *ncolored, int *nfound, int *ntrees,
1006 got_pack_progress_cb progress_cb, void *progress_arg,
1007 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1009 const struct got_error *err;
1010 struct got_commit_object *commit;
1012 if (got_object_idset_contains(idset, id))
1015 if (loose_obj_only) {
1017 err = search_packidx(&is_packed, id, repo);
1020 if (is_packed && want_meta)
1024 err = got_object_open_as_commit(&commit, repo, id);
1028 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1029 got_object_commit_get_committer_time(commit),
1030 loose_obj_only, repo,
1031 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1035 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1036 "", got_object_commit_get_committer_time(commit),
1037 repo, loose_obj_only, ncolored, nfound, ntrees,
1038 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1040 got_object_commit_close(commit);
1044 static const struct got_error *
1045 load_tag(int want_meta, struct got_object_idset *idset,
1046 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1047 int *ncolored, int *nfound, int *ntrees,
1048 got_pack_progress_cb progress_cb, void *progress_arg,
1049 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1051 const struct got_error *err;
1052 struct got_tag_object *tag = NULL;
1054 if (got_object_idset_contains(idset, id))
1057 if (loose_obj_only) {
1059 err = search_packidx(&is_packed, id, repo);
1062 if (is_packed && want_meta)
1066 err = got_object_open_as_tag(&tag, repo, id);
1070 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1071 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1072 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1076 switch (got_object_tag_get_object_type(tag)) {
1077 case GOT_OBJ_TYPE_COMMIT:
1078 err = load_commit(want_meta, idset,
1079 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1080 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1081 cancel_cb, cancel_arg);
1083 case GOT_OBJ_TYPE_TREE:
1084 err = load_tree(want_meta, idset,
1085 got_object_tag_get_object_id(tag), "",
1086 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1087 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1088 cancel_cb, cancel_arg);
1095 got_object_tag_close(tag);
1099 enum findtwixt_color {
1104 static const int findtwixt_colors[] = {
1110 static const struct got_error *
1111 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1112 int color, struct got_repository *repo)
1114 const struct got_error *err;
1115 struct got_object_qid *qid;
1117 err = got_object_qid_alloc(&qid, id);
1121 STAILQ_INSERT_TAIL(ids, qid, entry);
1122 qid->data = (void *)&findtwixt_colors[color];
1126 static const struct got_error *
1127 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1128 struct got_object_id *id, struct got_repository *repo,
1129 got_cancel_cb cancel_cb, void *cancel_arg)
1131 const struct got_error *err = NULL;
1132 struct got_commit_object *commit;
1133 const struct got_object_id_queue *parents;
1134 struct got_object_id_queue ids;
1135 struct got_object_qid *qid;
1139 err = got_object_qid_alloc(&qid, id);
1142 STAILQ_INSERT_HEAD(&ids, qid, entry);
1144 while (!STAILQ_EMPTY(&ids)) {
1146 err = (*cancel_cb)(cancel_arg);
1151 qid = STAILQ_FIRST(&ids);
1152 STAILQ_REMOVE_HEAD(&ids, entry);
1154 if (got_object_idset_contains(drop, qid->id)) {
1155 got_object_qid_free(qid);
1159 err = got_object_idset_add(drop, qid->id, NULL);
1161 got_object_qid_free(qid);
1165 if (!got_object_idset_contains(keep, qid->id)) {
1166 got_object_qid_free(qid);
1170 err = got_object_open_as_commit(&commit, repo, qid->id);
1171 got_object_qid_free(qid);
1175 parents = got_object_commit_get_parent_ids(commit);
1177 err = got_object_id_queue_copy(parents, &ids);
1179 got_object_commit_close(commit);
1183 got_object_commit_close(commit);
1186 got_object_id_queue_free(&ids);
1190 struct append_id_arg {
1191 struct got_object_id **array;
1195 static const struct got_error *
1196 append_id(struct got_object_id *id, void *data, void *arg)
1198 struct append_id_arg *a = arg;
1200 a->array[a->idx] = got_object_id_dup(id);
1201 if (a->array[a->idx] == NULL)
1202 return got_error_from_errno("got_object_id_dup");
1208 static const struct got_error *
1209 queue_commit_or_tag_id(struct got_object_id *id, int color,
1210 struct got_object_id_queue *ids, struct got_repository *repo)
1212 const struct got_error *err;
1213 struct got_tag_object *tag = NULL;
1216 err = got_object_get_type(&obj_type, repo, id);
1220 if (obj_type == GOT_OBJ_TYPE_TAG) {
1221 err = got_object_open_as_tag(&tag, repo, id);
1224 obj_type = got_object_tag_get_object_type(tag);
1225 id = got_object_tag_get_object_id(tag);
1228 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1229 err = queue_commit_id(ids, id, color, repo);
1235 got_object_tag_close(tag);
1239 static const struct got_error *
1240 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1241 struct got_object_id **head, int nhead,
1242 struct got_object_id **tail, int ntail,
1243 struct got_repository *repo,
1244 got_pack_progress_cb progress_cb, void *progress_arg,
1245 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1247 const struct got_error *err = NULL;
1248 struct got_object_id_queue ids;
1249 struct got_object_idset *keep, *drop;
1250 struct got_object_qid *qid;
1251 int i, ncolor, nkeep;
1258 keep = got_object_idset_alloc();
1260 return got_error_from_errno("got_object_idset_alloc");
1262 drop = got_object_idset_alloc();
1264 err = got_error_from_errno("got_object_idset_alloc");
1268 for (i = 0; i < nhead; i++) {
1269 struct got_object_id *id = head[i];
1272 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1277 for (i = 0; i < ntail; i++) {
1278 struct got_object_id *id = tail[i];
1281 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1286 while (!STAILQ_EMPTY(&ids)) {
1288 qid = STAILQ_FIRST(&ids);
1289 qcolor = *((int *)qid->data);
1291 if (got_object_idset_contains(drop, qid->id))
1292 ncolor = COLOR_DROP;
1293 else if (got_object_idset_contains(keep, qid->id))
1294 ncolor = COLOR_KEEP;
1296 ncolor = COLOR_BLANK;
1299 err = report_progress(progress_cb, progress_arg, rl,
1300 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1304 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1305 qcolor == COLOR_KEEP)) {
1306 STAILQ_REMOVE_HEAD(&ids, entry);
1307 got_object_qid_free(qid);
1311 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1312 err = drop_commit(keep, drop, qid->id, repo,
1313 cancel_cb, cancel_arg);
1316 } else if (ncolor == COLOR_BLANK) {
1317 struct got_commit_object *commit;
1318 struct got_object_id *id;
1319 const struct got_object_id_queue *parents;
1320 struct got_object_qid *pid;
1322 id = got_object_id_dup(qid->id);
1324 err = got_error_from_errno("got_object_id_dup");
1327 if (qcolor == COLOR_KEEP)
1328 err = got_object_idset_add(keep, id, NULL);
1330 err = got_object_idset_add(drop, id, NULL);
1336 err = got_object_open_as_commit(&commit, repo, id);
1341 parents = got_object_commit_get_parent_ids(commit);
1343 STAILQ_FOREACH(pid, parents, entry) {
1344 err = queue_commit_id(&ids, pid->id,
1352 got_object_commit_close(commit);
1355 /* should not happen */
1356 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1357 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1361 STAILQ_REMOVE_HEAD(&ids, entry);
1362 got_object_qid_free(qid);
1365 nkeep = got_object_idset_num_elements(keep);
1367 struct append_id_arg arg;
1368 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1369 if (arg.array == NULL) {
1370 err = got_error_from_errno("calloc");
1374 err = got_object_idset_for_each(keep, append_id, &arg);
1383 got_object_idset_free(keep);
1384 got_object_idset_free(drop);
1385 got_object_id_queue_free(&ids);
1389 static const struct got_error *
1390 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1391 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1392 struct got_object_id **ours, int nours, struct got_repository *repo,
1393 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1394 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1396 const struct got_error *err = NULL;
1397 struct got_object_id **ids = NULL;
1398 int i, nobj = 0, obj_type;
1404 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1405 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1406 if (err || nobj == 0)
1409 for (i = 0; i < ntheirs; i++) {
1410 struct got_object_id *id = theirs[i];
1413 err = got_object_get_type(&obj_type, repo, id);
1416 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1417 err = load_commit(0, idset, id, repo,
1418 loose_obj_only, ncolored, nfound, ntrees,
1419 progress_cb, progress_arg, rl,
1420 cancel_cb, cancel_arg);
1423 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1424 err = load_tag(0, idset, id, repo,
1425 loose_obj_only, ncolored, nfound, ntrees,
1426 progress_cb, progress_arg, rl,
1427 cancel_cb, cancel_arg);
1433 for (i = 0; i < nobj; i++) {
1434 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1435 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1436 cancel_cb, cancel_arg);
1441 for (i = 0; i < nours; i++) {
1442 struct got_object_id *id = ours[i];
1443 struct got_pack_meta *m;
1446 m = got_object_idset_get(idset, id);
1448 err = got_object_get_type(&obj_type, repo, id);
1452 obj_type = m->obj_type;
1453 if (obj_type != GOT_OBJ_TYPE_TAG)
1455 err = load_tag(1, idset, id, repo, loose_obj_only,
1456 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1457 cancel_cb, cancel_arg);
1462 for (i = 0; i < nobj; i++) {
1469 const struct got_error *
1470 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1474 SHA1Update(ctx, buf, len);
1475 n = fwrite(buf, 1, len, f);
1477 return got_ferror(f, GOT_ERR_IO);
1482 putbe32(char *b, uint32_t n)
1491 write_order_cmp(const void *pa, const void *pb)
1493 struct got_pack_meta *a, *b, *ahd, *bhd;
1495 a = *(struct got_pack_meta **)pa;
1496 b = *(struct got_pack_meta **)pb;
1497 ahd = (a->head == NULL) ? a : a->head;
1498 bhd = (b->head == NULL) ? b : b->head;
1499 if (ahd->mtime != bhd->mtime)
1500 return bhd->mtime - ahd->mtime;
1502 return (uintptr_t)bhd - (uintptr_t)ahd;
1503 if (a->nchain != b->nchain)
1504 return a->nchain - b->nchain;
1505 return a->mtime - b->mtime;
1509 reuse_write_order_cmp(const void *pa, const void *pb)
1511 struct got_pack_meta *a, *b;
1513 a = *(struct got_pack_meta **)pa;
1514 b = *(struct got_pack_meta **)pb;
1516 if (a->reused_delta_offset < b->reused_delta_offset)
1518 if (a->reused_delta_offset > b->reused_delta_offset)
1523 static const struct got_error *
1524 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1530 hdr[0] = obj_type << 4;
1531 hdr[0] |= len & 0xf;
1533 for (i = 1; len != 0; i++){
1535 return got_error(GOT_ERR_NO_SPACE);
1536 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1537 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1538 len >>= GOT_DELTA_SIZE_SHIFT;
1546 packoff(char *hdr, off_t off)
1551 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1552 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1553 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1554 GOT_DELTA_SIZE_MORE;
1559 hdr[j++] = rbuf[--i];
1563 static const struct got_error *
1564 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1565 struct got_pack_meta *m)
1567 const struct got_error *err;
1571 if (m->prev->off != 0) {
1572 err = packhdr(&nh, buf, sizeof(buf),
1573 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1576 nh += packoff(buf + nh, m->off - m->prev->off);
1577 err = hwrite(packfile, buf, nh, ctx);
1580 *packfile_size += nh;
1582 err = packhdr(&nh, buf, sizeof(buf),
1583 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1586 err = hwrite(packfile, buf, nh, ctx);
1589 *packfile_size += nh;
1590 err = hwrite(packfile, m->prev->id.sha1,
1591 sizeof(m->prev->id.sha1), ctx);
1594 *packfile_size += sizeof(m->prev->id.sha1);
1600 static const struct got_error *
1601 write_packed_object(off_t *packfile_size, FILE *packfile,
1602 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1603 SHA1_CTX *ctx, struct got_repository *repo)
1605 const struct got_error *err = NULL;
1606 struct got_deflate_checksum csum;
1609 struct got_raw_object *raw = NULL;
1612 csum.output_sha1 = ctx;
1613 csum.output_crc = NULL;
1615 m->off = ftello(packfile);
1616 if (m->delta_len == 0) {
1617 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1620 err = packhdr(&nh, buf, sizeof(buf),
1621 m->obj_type, raw->size);
1624 err = hwrite(packfile, buf, nh, ctx);
1627 *packfile_size += nh;
1628 if (raw->f == NULL) {
1629 err = got_deflate_to_file_mmap(&outlen,
1630 raw->data + raw->hdrlen, 0, raw->size,
1635 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1637 err = got_error_from_errno("fseeko");
1640 err = got_deflate_to_file(&outlen, raw->f,
1641 raw->size, packfile, &csum);
1645 *packfile_size += outlen;
1646 got_object_raw_close(raw);
1648 } else if (m->delta_buf) {
1649 err = deltahdr(packfile_size, ctx, packfile, m);
1652 err = got_deflate_to_file_mmap(&outlen,
1653 m->delta_buf, 0, m->delta_len, packfile, &csum);
1656 *packfile_size += outlen;
1658 m->delta_buf = NULL;
1660 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1662 err = got_error_from_errno("fseeko");
1665 err = deltahdr(packfile_size, ctx, packfile, m);
1668 err = got_deflate_to_file(&outlen, delta_cache,
1669 m->delta_len, packfile, &csum);
1672 *packfile_size += outlen;
1676 got_object_raw_close(raw);
1680 static const struct got_error *
1681 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1682 struct got_pack_meta **deltify, int ndeltify,
1683 struct got_pack_meta **reuse, int nreuse,
1684 int ncolored, int nfound, int ntrees, int nours,
1685 struct got_repository *repo,
1686 got_pack_progress_cb progress_cb, void *progress_arg,
1687 struct got_ratelimit *rl,
1688 got_cancel_cb cancel_cb, void *cancel_arg)
1690 const struct got_error *err = NULL;
1693 struct got_pack_meta *m;
1696 off_t packfile_size = 0;
1701 err = hwrite(packfile, "PACK", 4, &ctx);
1704 putbe32(buf, GOT_PACKFILE_VERSION);
1705 err = hwrite(packfile, buf, 4, &ctx);
1708 putbe32(buf, ndeltify + nreuse);
1709 err = hwrite(packfile, buf, 4, &ctx);
1713 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1715 for (i = 0; i < ndeltify; i++) {
1716 err = report_progress(progress_cb, progress_arg, rl,
1717 ncolored, nfound, ntrees, packfile_size, nours,
1718 ndeltify + nreuse, ndeltify + nreuse, i);
1722 err = write_packed_object(&packfile_size, packfile,
1723 delta_cache, m, &outfd, &ctx, repo);
1728 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1729 reuse_write_order_cmp);
1730 for (i = 0; i < nreuse; i++) {
1731 err = report_progress(progress_cb, progress_arg, rl,
1732 ncolored, nfound, ntrees, packfile_size, nours,
1733 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1737 err = write_packed_object(&packfile_size, packfile,
1738 delta_cache, m, &outfd, &ctx, repo);
1743 SHA1Final(pack_sha1, &ctx);
1744 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1745 if (n != SHA1_DIGEST_LENGTH)
1746 err = got_ferror(packfile, GOT_ERR_IO);
1747 packfile_size += SHA1_DIGEST_LENGTH;
1748 packfile_size += sizeof(struct got_packfile_hdr);
1750 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1751 packfile_size, nours, ndeltify + nreuse,
1752 ndeltify + nreuse, ndeltify + nreuse);
1757 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1758 err = got_error_from_errno("close");
1762 static const struct got_error *
1763 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1765 struct got_object_idset *idset = arg;
1767 if (got_object_idset_get_element_data(entry) == NULL)
1768 got_object_idset_remove_element(idset, entry);
1773 static const struct got_error *
1774 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1776 struct got_object_idset *idset = arg;
1777 struct got_pack_meta *m;
1779 m = got_object_idset_get_element_data(entry);
1780 if (m->have_reused_delta)
1781 got_object_idset_remove_element(idset, entry);
1786 static const struct got_error *
1787 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1789 struct got_pack_meta *m = data;
1790 struct got_pack_metavec *v = arg;
1792 return add_meta(m, v);
1795 const struct got_error *
1796 got_pack_create(uint8_t *packsha1, FILE *packfile,
1797 struct got_object_id **theirs, int ntheirs,
1798 struct got_object_id **ours, int nours,
1799 struct got_repository *repo, int loose_obj_only, int allow_empty,
1800 got_pack_progress_cb progress_cb, void *progress_arg,
1801 got_cancel_cb cancel_cb, void *cancel_arg)
1803 const struct got_error *err;
1804 int delta_cache_fd = -1;
1805 FILE *delta_cache = NULL;
1806 struct got_object_idset *idset;
1807 struct got_ratelimit rl;
1808 struct got_pack_metavec deltify, reuse;
1809 int ncolored = 0, nfound = 0, ntrees = 0;
1811 memset(&deltify, 0, sizeof(deltify));
1812 memset(&reuse, 0, sizeof(reuse));
1814 got_ratelimit_init(&rl, 0, 500);
1816 idset = got_object_idset_alloc();
1818 return got_error_from_errno("got_object_idset_alloc");
1820 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1821 ntheirs, ours, nours, repo, loose_obj_only,
1822 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1826 err = got_object_idset_for_each_element(idset,
1827 remove_unused_object, idset);
1832 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1833 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1838 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1839 err = got_error(GOT_ERR_CANNOT_PACK);
1843 delta_cache_fd = got_opentempfd();
1844 if (delta_cache_fd == -1) {
1845 err = got_error_from_errno("got_opentemp");
1850 reuse.meta = calloc(reuse.metasz,
1851 sizeof(struct got_pack_meta *));
1852 if (reuse.meta == NULL) {
1853 err = got_error_from_errno("calloc");
1857 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1858 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1859 cancel_cb, cancel_arg);
1862 if (reuse.nmeta > 0) {
1863 err = got_object_idset_for_each_element(idset,
1864 remove_reused_object, idset);
1869 delta_cache = fdopen(delta_cache_fd, "a+");
1870 if (delta_cache == NULL) {
1871 err = got_error_from_errno("fdopen");
1874 delta_cache_fd = -1;
1876 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1877 err = got_error_from_errno("fseeko");
1881 deltify.meta = calloc(got_object_idset_num_elements(idset),
1882 sizeof(struct got_pack_meta *));
1883 if (deltify.meta == NULL) {
1884 err = got_error_from_errno("calloc");
1887 deltify.metasz = got_object_idset_num_elements(idset);
1889 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1892 if (deltify.nmeta > 0) {
1893 err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1894 nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1895 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1898 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1899 err = got_error_from_errno("fseeko");
1904 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1905 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1906 nours, repo, progress_cb, progress_arg, &rl,
1907 cancel_cb, cancel_arg);
1911 free_nmeta(deltify.meta, deltify.nmeta);
1912 free_nmeta(reuse.meta, reuse.nmeta);
1913 got_object_idset_free(idset);
1914 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1915 err = got_error_from_errno("close");
1916 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1917 err = got_error_from_errno("fclose");