2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
31 #if defined(__FreeBSD__)
35 #include "got_error.h"
36 #include "got_cancel.h"
37 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository_admin.h"
41 #include "got_opentemp.h"
43 #include "got_lib_deltify.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_deflate.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_ratelimit.h"
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 struct got_pack_meta {
63 struct got_object_id id;
69 /* The best delta we picked */
70 struct got_pack_meta *head;
71 struct got_pack_meta *prev;
72 unsigned char *delta_buf; /* if not encoded in delta cache file */
73 off_t delta_offset; /* offset in delta cache file */
74 off_t delta_len; /* encoded delta length */
77 int have_reused_delta;
78 off_t reused_delta_offset; /* offset of delta in reused pack file */
79 struct got_object_id *base_obj_id;
81 /* Only used for delta window */
82 struct got_delta_table *dtab;
84 /* Only used for writing offset deltas */
88 struct got_pack_metavec {
89 struct got_pack_meta **meta;
94 static const struct got_error *
95 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
96 const char *path, int obj_type, time_t mtime)
98 const struct got_error *err = NULL;
99 struct got_pack_meta *m;
103 m = calloc(1, sizeof(*m));
105 return got_error_from_errno("calloc");
107 memcpy(&m->id, id, sizeof(m->id));
109 m->path = strdup(path);
110 if (m->path == NULL) {
111 err = got_error_from_errno("strdup");
116 m->obj_type = obj_type;
123 clear_meta(struct got_pack_meta *meta)
129 free(meta->delta_buf);
130 meta->delta_buf = NULL;
131 free(meta->base_obj_id);
132 meta->base_obj_id = NULL;
136 free_nmeta(struct got_pack_meta **meta, int nmeta)
140 for (i = 0; i < nmeta; i++)
146 delta_order_cmp(const void *pa, const void *pb)
148 struct got_pack_meta *a, *b;
151 a = *(struct got_pack_meta **)pa;
152 b = *(struct got_pack_meta **)pb;
154 if (a->obj_type != b->obj_type)
155 return a->obj_type - b->obj_type;
156 cmp = strcmp(a->path, b->path);
159 if (a->mtime != b->mtime)
160 return a->mtime - b->mtime;
161 return got_object_id_cmp(&a->id, &b->id);
165 delta_size(struct got_delta_instruction *deltas, int ndeltas)
169 for (i = 0; i < ndeltas; i++) {
171 size += GOT_DELTA_SIZE_SHIFT;
173 size += deltas[i].len + 1;
178 static const struct got_error *
179 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
183 if (*len + nseg >= *sz) {
184 while (*len + nseg >= *sz)
186 n = realloc(*p, *sz);
188 return got_error_from_errno("realloc");
191 memcpy(*p + *len, seg, nseg);
196 static const struct got_error *
197 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
198 struct got_delta_instruction *deltas, int ndeltas,
199 off_t delta_size, off_t base_size)
201 const struct got_error *err;
202 unsigned char buf[16], *bp;
206 struct got_delta_instruction *d;
208 m->delta_buf = malloc(delta_size);
209 if (m->delta_buf == NULL)
210 return got_error_from_errno("calloc");
212 /* base object size */
213 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
214 n = base_size >> GOT_DELTA_SIZE_SHIFT;
215 for (i = 1; n > 0; i++) {
216 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
217 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
218 n >>= GOT_DELTA_SIZE_SHIFT;
220 err = append(&m->delta_buf, &len, &delta_size, buf, i);
224 /* target object size */
225 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
226 n = o->size >> GOT_DELTA_SIZE_SHIFT;
227 for (i = 1; n > 0; i++) {
228 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
229 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
230 n >>= GOT_DELTA_SIZE_SHIFT;
232 err = append(&m->delta_buf, &len, &delta_size, buf, i);
236 for (j = 0; j < ndeltas; j++) {
241 buf[0] = GOT_DELTA_BASE_COPY;
242 for (i = 0; i < 4; i++) {
243 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
252 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
253 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
254 for (i = 0; i < 3 && n > 0; i++) {
255 buf[0] |= 1 << (i + 4);
260 err = append(&m->delta_buf, &len, &delta_size,
264 } else if (o->f == NULL) {
266 while (n != d->len) {
267 buf[0] = (d->len - n < 127) ? d->len - n : 127;
268 err = append(&m->delta_buf, &len, &delta_size,
272 err = append(&m->delta_buf, &len, &delta_size,
273 o->data + o->hdrlen + d->offset + n,
282 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
283 return got_error_from_errno("fseeko");
285 while (n != d->len) {
286 buf[0] = (d->len - n < 127) ? d->len - n : 127;
287 err = append(&m->delta_buf, &len, &delta_size,
291 r = fread(content, 1, buf[0], o->f);
293 return got_ferror(o->f, GOT_ERR_IO);
294 err = append(&m->delta_buf, &len, &delta_size,
307 static const struct got_error *
308 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
309 struct got_delta_instruction *deltas, int ndeltas,
310 off_t base_size, FILE *f)
312 unsigned char buf[16], *bp;
316 struct got_delta_instruction *d;
318 /* base object size */
319 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
320 n = base_size >> GOT_DELTA_SIZE_SHIFT;
321 for (i = 1; n > 0; i++) {
322 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
323 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
324 n >>= GOT_DELTA_SIZE_SHIFT;
326 w = fwrite(buf, 1, i, f);
328 return got_ferror(f, GOT_ERR_IO);
330 /* target object size */
331 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
332 n = o->size >> GOT_DELTA_SIZE_SHIFT;
333 for (i = 1; n > 0; i++) {
334 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
335 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
336 n >>= GOT_DELTA_SIZE_SHIFT;
338 w = fwrite(buf, 1, i, f);
340 return got_ferror(f, GOT_ERR_IO);
342 for (j = 0; j < ndeltas; j++) {
347 buf[0] = GOT_DELTA_BASE_COPY;
348 for (i = 0; i < 4; i++) {
349 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
358 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
359 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
360 for (i = 0; i < 3 && n > 0; i++) {
361 buf[0] |= 1 << (i + 4);
366 w = fwrite(buf, 1, bp - buf, f);
368 return got_ferror(f, GOT_ERR_IO);
369 } else if (o->f == NULL) {
371 while (n != d->len) {
372 buf[0] = (d->len - n < 127) ? d->len - n : 127;
373 w = fwrite(buf, 1, 1, f);
375 return got_ferror(f, GOT_ERR_IO);
376 w = fwrite(o->data + o->hdrlen + d->offset + n,
379 return got_ferror(f, GOT_ERR_IO);
385 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
386 return got_error_from_errno("fseeko");
388 while (n != d->len) {
389 buf[0] = (d->len - n < 127) ? d->len - n : 127;
390 w = fwrite(buf, 1, 1, f);
392 return got_ferror(f, GOT_ERR_IO);
393 r = fread(content, 1, buf[0], o->f);
395 return got_ferror(o->f, GOT_ERR_IO);
396 w = fwrite(content, 1, buf[0], f);
398 return got_ferror(f, GOT_ERR_IO);
404 m->delta_len = ftello(f) - m->delta_offset;
408 static const struct got_error *
409 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
410 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
411 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
414 const struct got_error *err;
417 if (progress_cb == NULL)
420 err = got_ratelimit_check(&elapsed, rl);
424 return progress_cb(progress_arg, ncolored, nfound, ntrees,
425 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
428 static const struct got_error *
429 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
431 if (v->nmeta == v->metasz){
432 size_t newsize = 2 * v->metasz;
433 struct got_pack_meta **new;
434 new = reallocarray(v->meta, newsize, sizeof(*new));
436 return got_error_from_errno("reallocarray");
441 v->meta[v->nmeta++] = m;
445 static const struct got_error *
446 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
447 struct got_object_idset *idset, struct got_pack *pack,
448 struct got_packidx *packidx, int delta_cache_fd,
449 struct got_repository *repo)
451 const struct got_error *err = NULL;
452 struct got_pack_meta *base = NULL;
453 struct got_object_id *base_obj_id = NULL;
454 off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
455 uint64_t base_size, result_size;
457 if (m->have_reused_delta)
460 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
461 &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
462 packidx, idx, &m->id, repo);
466 if (delta_offset + delta_len < delta_offset)
467 return got_error(GOT_ERR_BAD_PACKFILE);
469 base = got_object_idset_get(idset, base_obj_id);
473 m->delta_len = delta_len;
474 m->delta_offset = delta_cache_offset;
476 m->size = result_size;
477 m->have_reused_delta = 1;
478 m->reused_delta_offset = delta_offset;
479 m->base_obj_id = base_obj_id;
481 err = add_meta(m, v);
487 static const struct got_error *
488 find_pack_for_reuse(struct got_packidx **best_packidx,
489 struct got_repository *repo)
491 const struct got_error *err = NULL;
492 struct got_pathlist_entry *pe;
493 const char *best_packidx_path = NULL;
496 *best_packidx = NULL;
498 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
499 const char *path_packidx = pe->path;
500 struct got_packidx *packidx;
503 err = got_repo_get_packidx(&packidx, path_packidx, repo);
507 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
508 if (nobj > nobj_max) {
509 best_packidx_path = path_packidx;
514 if (best_packidx_path) {
515 err = got_repo_get_packidx(best_packidx, best_packidx_path,
522 struct search_deltas_arg {
523 struct got_packidx *packidx;
524 struct got_pack *pack;
525 struct got_object_idset *idset;
526 struct got_pack_metavec *v;
528 struct got_repository *repo;
529 got_pack_progress_cb progress_cb;
531 struct got_ratelimit *rl;
532 got_cancel_cb cancel_cb;
540 static const struct got_error *
541 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
543 const struct got_error *err;
544 struct got_pack_meta *m = data;
545 struct search_deltas_arg *a = arg;
547 struct got_object *obj = NULL;
550 err = (*a->cancel_cb)(a->cancel_arg);
555 if (!got_repo_check_packidx_bloom_filter(a->repo,
556 a->packidx->path_packidx, id))
559 obj_idx = got_packidx_get_object_idx(a->packidx, id);
564 * Opening and closing an object just to check its flags
565 * is a bit expensive. We could have an imsg which requests
566 * plain type/size information for an object without doing
567 * work such as traversing the object's entire delta chain
568 * to find the base object type, and other such info which
569 * we don't really need here.
571 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
572 a->packidx, obj_idx, a->repo);
576 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
577 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
578 a->delta_cache_fd, a->repo);
581 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
582 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
583 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
586 got_object_close(obj);
590 static const struct got_error *
591 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
592 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
593 struct got_repository *repo,
594 got_pack_progress_cb progress_cb, void *progress_arg,
595 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
597 const struct got_error *err = NULL;
598 char *path_packfile = NULL;
599 struct got_packidx *packidx;
600 struct got_pack *pack;
601 struct search_deltas_arg sda;
603 err = find_pack_for_reuse(&packidx, repo);
610 err = got_packidx_get_packfile_path(&path_packfile,
611 packidx->path_packidx);
615 pack = got_repo_get_cached_pack(repo, path_packfile);
617 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
622 sda.packidx = packidx;
626 sda.delta_cache_fd = delta_cache_fd;
628 sda.progress_cb = progress_cb;
629 sda.progress_arg = progress_arg;
631 sda.cancel_cb = cancel_cb;
632 sda.cancel_arg = cancel_arg;
633 sda.ncolored = ncolored;
636 sda.ncommits = ncommits;
637 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
643 static const struct got_error *
644 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
645 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
646 struct got_repository *repo,
647 got_pack_progress_cb progress_cb, void *progress_arg,
648 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
650 const struct got_error *err = NULL;
651 struct got_pack_meta *m = NULL, *base = NULL;
652 struct got_raw_object *raw = NULL, *base_raw = NULL;
653 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
654 int i, j, ndeltas, best_ndeltas;
655 off_t size, best_size;
656 const int max_base_candidates = 3;
657 size_t delta_memsize = 0;
658 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
661 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
662 for (i = 0; i < nmeta; i++) {
664 err = (*cancel_cb)(cancel_arg);
668 err = report_progress(progress_cb, progress_arg, rl,
669 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
675 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
676 m->obj_type == GOT_OBJ_TYPE_TAG)
679 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
684 if (raw->f == NULL) {
685 err = got_deltify_init_mem(&m->dtab, raw->data,
686 raw->hdrlen, raw->size + raw->hdrlen);
688 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
689 raw->size + raw->hdrlen);
694 if (i > max_base_candidates) {
695 struct got_pack_meta *n = NULL;
696 n = meta[i - (max_base_candidates + 1)];
697 got_deltify_free(n->dtab);
701 best_size = raw->size;
703 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
705 err = (*cancel_cb)(cancel_arg);
710 /* long chains make unpacking slow, avoid such bases */
711 if (base->nchain >= 128 ||
712 base->obj_type != m->obj_type)
715 err = got_object_raw_open(&base_raw, &outfd, repo,
720 if (raw->f == NULL && base_raw->f == NULL) {
721 err = got_deltify_mem_mem(&deltas, &ndeltas,
722 raw->data, raw->hdrlen,
723 raw->size + raw->hdrlen,
724 base->dtab, base_raw->data,
726 base_raw->size + base_raw->hdrlen);
727 } else if (raw->f == NULL) {
728 err = got_deltify_mem_file(&deltas, &ndeltas,
729 raw->data, raw->hdrlen,
730 raw->size + raw->hdrlen,
731 base->dtab, base_raw->f,
733 base_raw->size + base_raw->hdrlen);
734 } else if (base_raw->f == NULL) {
735 err = got_deltify_file_mem(&deltas, &ndeltas,
737 raw->size + raw->hdrlen,
738 base->dtab, base_raw->data,
740 base_raw->size + base_raw->hdrlen);
742 err = got_deltify(&deltas, &ndeltas,
744 raw->size + raw->hdrlen,
745 base->dtab, base_raw->f, base_raw->hdrlen,
746 base_raw->size + base_raw->hdrlen);
748 got_object_raw_close(base_raw);
753 size = delta_size(deltas, ndeltas);
754 if (size + 32 < best_size){
756 * if we already picked a best delta,
761 best_deltas = deltas;
762 best_ndeltas = ndeltas;
764 m->nchain = base->nchain + 1;
766 m->head = base->head;
776 if (best_ndeltas > 0) {
777 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
778 delta_memsize + best_size <= max_delta_memsize) {
779 delta_memsize += best_size;
780 err = encode_delta_in_mem(m, raw, best_deltas,
781 best_ndeltas, best_size, m->prev->size);
783 m->delta_offset = ftello(delta_cache);
786 * Storing compressed delta data in the delta
787 * cache file would probably be more efficient
788 * than writing uncompressed delta data here
789 * and compressing it while writing the pack
790 * file. This would also allow for reusing
791 * deltas in their compressed form.
793 err = encode_delta(m, raw, best_deltas,
794 best_ndeltas, m->prev->size, delta_cache);
803 got_object_raw_close(raw);
807 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
808 got_deltify_free(meta[i]->dtab);
809 meta[i]->dtab = NULL;
812 got_object_raw_close(raw);
814 got_object_raw_close(base_raw);
815 if (outfd != -1 && close(outfd) == -1 && err == NULL)
816 err = got_error_from_errno("close");
822 static const struct got_error *
823 search_packidx(int *found, struct got_object_id *id,
824 struct got_repository *repo)
826 const struct got_error *err = NULL;
827 struct got_packidx *packidx = NULL;
832 err = got_repo_search_packidx(&packidx, &idx, repo, id);
834 *found = 1; /* object is already packed */
835 else if (err->code == GOT_ERR_NO_OBJ)
840 static const int obj_types[] = {
846 GOT_OBJ_TYPE_OFFSET_DELTA,
847 GOT_OBJ_TYPE_REF_DELTA
850 static const struct got_error *
851 add_object(int want_meta, struct got_object_idset *idset,
852 struct got_object_id *id, const char *path, int obj_type,
853 time_t mtime, int loose_obj_only, struct got_repository *repo)
855 const struct got_error *err;
856 struct got_pack_meta *m = NULL;
858 if (loose_obj_only) {
860 err = search_packidx(&is_packed, id, repo);
868 err = alloc_meta(&m, id, path, obj_type, mtime);
873 return got_object_idset_add(idset, id, m);
876 static const struct got_error *
877 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
878 struct got_object_idset *idset, struct got_object_id *tree_id,
879 const char *dpath, time_t mtime, struct got_repository *repo,
880 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
881 got_pack_progress_cb progress_cb, void *progress_arg,
882 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
884 const struct got_error *err;
885 struct got_tree_object *tree;
889 err = got_object_open_as_tree(&tree, repo, tree_id);
894 err = report_progress(progress_cb, progress_arg, rl,
895 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
899 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
900 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
901 struct got_object_id *id = got_tree_entry_get_id(e);
902 mode_t mode = got_tree_entry_get_mode(e);
905 err = (*cancel_cb)(cancel_arg);
910 if (got_object_tree_entry_is_submodule(e) ||
911 got_object_idset_contains(idset, id))
914 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
915 got_tree_entry_get_name(e)) == -1) {
916 err = got_error_from_errno("asprintf");
921 struct got_object_qid *qid;
922 err = got_object_qid_alloc(&qid, id);
925 STAILQ_INSERT_TAIL(ids, qid, entry);
926 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
927 err = add_object(want_meta, idset, id, p,
928 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo);
932 err = report_progress(progress_cb, progress_arg, rl,
933 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
941 got_object_tree_close(tree);
946 static const struct got_error *
947 load_tree(int want_meta, struct got_object_idset *idset,
948 struct got_object_id *tree_id, const char *dpath, time_t mtime,
949 struct got_repository *repo, int loose_obj_only,
950 int *ncolored, int *nfound, int *ntrees,
951 got_pack_progress_cb progress_cb, void *progress_arg,
952 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
954 const struct got_error *err = NULL;
955 struct got_object_id_queue tree_ids;
956 struct got_object_qid *qid;
958 if (got_object_idset_contains(idset, tree_id))
961 err = got_object_qid_alloc(&qid, tree_id);
965 STAILQ_INIT(&tree_ids);
966 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
968 while (!STAILQ_EMPTY(&tree_ids)) {
970 err = (*cancel_cb)(cancel_arg);
975 qid = STAILQ_FIRST(&tree_ids);
976 STAILQ_REMOVE_HEAD(&tree_ids, entry);
978 if (got_object_idset_contains(idset, qid->id)) {
979 got_object_qid_free(qid);
983 err = add_object(want_meta, idset, qid->id, dpath,
984 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo);
986 got_object_qid_free(qid);
991 err = report_progress(progress_cb, progress_arg, rl,
992 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
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);
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);
1042 err = report_progress(progress_cb, progress_arg, rl,
1043 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
1047 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1048 "", got_object_commit_get_committer_time(commit),
1049 repo, loose_obj_only, ncolored, nfound, ntrees,
1050 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1052 got_object_commit_close(commit);
1056 static const struct got_error *
1057 load_tag(int want_meta, struct got_object_idset *idset,
1058 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1059 int *ncolored, int *nfound, int *ntrees,
1060 got_pack_progress_cb progress_cb, void *progress_arg,
1061 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1063 const struct got_error *err;
1064 struct got_tag_object *tag = NULL;
1066 if (got_object_idset_contains(idset, id))
1069 if (loose_obj_only) {
1071 err = search_packidx(&is_packed, id, repo);
1078 err = got_object_open_as_tag(&tag, repo, id);
1082 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1083 got_object_tag_get_tagger_time(tag),
1084 loose_obj_only, repo);
1089 err = report_progress(progress_cb, progress_arg, rl,
1090 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
1094 switch (got_object_tag_get_object_type(tag)) {
1095 case GOT_OBJ_TYPE_COMMIT:
1096 err = load_commit(want_meta, idset,
1097 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1098 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1099 cancel_cb, cancel_arg);
1101 case GOT_OBJ_TYPE_TREE:
1102 err = load_tree(want_meta, idset,
1103 got_object_tag_get_object_id(tag), "",
1104 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1105 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1106 cancel_cb, cancel_arg);
1113 got_object_tag_close(tag);
1117 enum findtwixt_color {
1122 static const int findtwixt_colors[] = {
1128 static const struct got_error *
1129 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1130 int color, struct got_repository *repo)
1132 const struct got_error *err;
1133 struct got_object_qid *qid;
1135 err = got_object_qid_alloc(&qid, id);
1139 STAILQ_INSERT_TAIL(ids, qid, entry);
1140 qid->data = (void *)&findtwixt_colors[color];
1144 static const struct got_error *
1145 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1146 struct got_object_id *id, struct got_repository *repo,
1147 got_cancel_cb cancel_cb, void *cancel_arg)
1149 const struct got_error *err = NULL;
1150 struct got_commit_object *commit;
1151 const struct got_object_id_queue *parents;
1152 struct got_object_id_queue ids;
1153 struct got_object_qid *qid;
1157 err = got_object_qid_alloc(&qid, id);
1160 STAILQ_INSERT_HEAD(&ids, qid, entry);
1162 while (!STAILQ_EMPTY(&ids)) {
1164 err = (*cancel_cb)(cancel_arg);
1169 qid = STAILQ_FIRST(&ids);
1170 STAILQ_REMOVE_HEAD(&ids, entry);
1172 if (got_object_idset_contains(drop, qid->id)) {
1173 got_object_qid_free(qid);
1177 err = got_object_idset_add(drop, qid->id,
1178 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1180 got_object_qid_free(qid);
1184 if (!got_object_idset_contains(keep, qid->id)) {
1185 got_object_qid_free(qid);
1189 err = got_object_open_as_commit(&commit, repo, qid->id);
1190 got_object_qid_free(qid);
1194 parents = got_object_commit_get_parent_ids(commit);
1196 err = got_object_id_queue_copy(parents, &ids);
1198 got_object_commit_close(commit);
1202 got_object_commit_close(commit);
1205 got_object_id_queue_free(&ids);
1209 struct append_id_arg {
1210 struct got_object_id **array;
1214 static const struct got_error *
1215 append_id(struct got_object_id *id, void *data, void *arg)
1217 struct append_id_arg *a = arg;
1219 a->array[a->idx] = got_object_id_dup(id);
1220 if (a->array[a->idx] == NULL)
1221 return got_error_from_errno("got_object_id_dup");
1227 static const struct got_error *
1228 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1229 struct got_object_id **head, int nhead,
1230 struct got_object_id **tail, int ntail,
1231 struct got_repository *repo,
1232 got_pack_progress_cb progress_cb, void *progress_arg,
1233 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1235 const struct got_error *err = NULL;
1236 struct got_object_id_queue ids;
1237 struct got_object_idset *keep, *drop;
1238 struct got_object_qid *qid;
1239 int i, ncolor, nkeep, obj_type;
1246 keep = got_object_idset_alloc();
1248 return got_error_from_errno("got_object_idset_alloc");
1250 drop = got_object_idset_alloc();
1252 err = got_error_from_errno("got_object_idset_alloc");
1256 for (i = 0; i < nhead; i++) {
1257 struct got_object_id *id = head[i];
1260 err = got_object_get_type(&obj_type, repo, id);
1263 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1265 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1269 for (i = 0; i < ntail; i++) {
1270 struct got_object_id *id = tail[i];
1273 err = got_object_get_type(&obj_type, repo, id);
1276 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1278 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1283 while (!STAILQ_EMPTY(&ids)) {
1285 qid = STAILQ_FIRST(&ids);
1286 qcolor = *((int *)qid->data);
1288 if (got_object_idset_contains(drop, qid->id))
1289 ncolor = COLOR_DROP;
1290 else if (got_object_idset_contains(keep, qid->id))
1291 ncolor = COLOR_KEEP;
1293 ncolor = COLOR_BLANK;
1296 err = report_progress(progress_cb, progress_arg, rl,
1297 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1301 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1302 qcolor == COLOR_KEEP)) {
1303 STAILQ_REMOVE_HEAD(&ids, entry);
1304 got_object_qid_free(qid);
1308 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1309 err = drop_commit(keep, drop, qid->id, repo,
1310 cancel_cb, cancel_arg);
1313 } else if (ncolor == COLOR_BLANK) {
1314 struct got_commit_object *commit;
1315 struct got_object_id *id;
1316 const struct got_object_id_queue *parents;
1317 struct got_object_qid *pid;
1319 id = got_object_id_dup(qid->id);
1321 err = got_error_from_errno("got_object_id_dup");
1324 if (qcolor == COLOR_KEEP)
1325 err = got_object_idset_add(keep, id,
1326 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1328 err = got_object_idset_add(drop, id,
1329 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1335 err = got_object_open_as_commit(&commit, repo, id);
1340 parents = got_object_commit_get_parent_ids(commit);
1342 STAILQ_FOREACH(pid, parents, entry) {
1343 err = queue_commit_id(&ids, pid->id,
1351 got_object_commit_close(commit);
1354 /* should not happen */
1355 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1356 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1360 STAILQ_REMOVE_HEAD(&ids, entry);
1361 got_object_qid_free(qid);
1364 nkeep = got_object_idset_num_elements(keep);
1366 struct append_id_arg arg;
1367 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1368 if (arg.array == NULL) {
1369 err = got_error_from_errno("calloc");
1373 err = got_object_idset_for_each(keep, append_id, &arg);
1382 got_object_idset_free(keep);
1383 got_object_idset_free(drop);
1384 got_object_id_queue_free(&ids);
1388 static const struct got_error *
1389 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1390 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1391 struct got_object_id **ours, int nours, struct got_repository *repo,
1392 int loose_obj_only, 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 **ids = NULL;
1397 int i, nobj = 0, obj_type;
1403 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1404 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1405 if (err || nobj == 0)
1408 for (i = 0; i < ntheirs; i++) {
1409 struct got_object_id *id = theirs[i];
1412 err = got_object_get_type(&obj_type, repo, id);
1415 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1417 err = load_commit(0, idset, id, repo, loose_obj_only,
1418 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1419 cancel_cb, cancel_arg);
1424 for (i = 0; i < ntheirs; i++) {
1425 struct got_object_id *id = theirs[i];
1426 struct got_pack_meta *m;
1429 m = got_object_idset_get(idset, id);
1431 err = got_object_get_type(&obj_type, repo, id);
1435 obj_type = m->obj_type;
1436 if (obj_type != GOT_OBJ_TYPE_TAG)
1438 err = load_tag(0, idset, id, repo, loose_obj_only,
1439 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1440 cancel_cb, cancel_arg);
1445 for (i = 0; i < nobj; i++) {
1446 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1447 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1448 cancel_cb, cancel_arg);
1453 for (i = 0; i < nours; i++) {
1454 struct got_object_id *id = ours[i];
1455 struct got_pack_meta *m;
1458 m = got_object_idset_get(idset, id);
1460 err = got_object_get_type(&obj_type, repo, id);
1464 obj_type = m->obj_type;
1465 if (obj_type != GOT_OBJ_TYPE_TAG)
1467 err = load_tag(1, idset, id, repo, loose_obj_only,
1468 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1469 cancel_cb, cancel_arg);
1474 for (i = 0; i < nobj; i++) {
1481 const struct got_error *
1482 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1486 SHA1Update(ctx, buf, len);
1487 n = fwrite(buf, 1, len, f);
1489 return got_ferror(f, GOT_ERR_IO);
1494 putbe32(char *b, uint32_t n)
1503 write_order_cmp(const void *pa, const void *pb)
1505 struct got_pack_meta *a, *b, *ahd, *bhd;
1507 a = *(struct got_pack_meta **)pa;
1508 b = *(struct got_pack_meta **)pb;
1509 ahd = (a->head == NULL) ? a : a->head;
1510 bhd = (b->head == NULL) ? b : b->head;
1511 if (ahd->mtime != bhd->mtime)
1512 return bhd->mtime - ahd->mtime;
1514 return (uintptr_t)bhd - (uintptr_t)ahd;
1515 if (a->nchain != b->nchain)
1516 return a->nchain - b->nchain;
1517 return a->mtime - b->mtime;
1521 reuse_write_order_cmp(const void *pa, const void *pb)
1523 struct got_pack_meta *a, *b;
1525 a = *(struct got_pack_meta **)pa;
1526 b = *(struct got_pack_meta **)pb;
1528 if (a->reused_delta_offset < b->reused_delta_offset)
1530 if (a->reused_delta_offset > b->reused_delta_offset)
1535 static const struct got_error *
1536 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1542 hdr[0] = obj_type << 4;
1543 hdr[0] |= len & 0xf;
1545 for (i = 1; len != 0; i++){
1547 return got_error(GOT_ERR_NO_SPACE);
1548 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1549 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1550 len >>= GOT_DELTA_SIZE_SHIFT;
1558 packoff(char *hdr, off_t off)
1563 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1564 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1565 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1566 GOT_DELTA_SIZE_MORE;
1571 hdr[j++] = rbuf[--i];
1575 static const struct got_error *
1576 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1577 struct got_pack_meta *m)
1579 const struct got_error *err;
1583 if (m->prev->off != 0) {
1584 err = packhdr(&nh, buf, sizeof(buf),
1585 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1588 nh += packoff(buf + nh, m->off - m->prev->off);
1589 err = hwrite(packfile, buf, nh, ctx);
1592 *packfile_size += nh;
1594 err = packhdr(&nh, buf, sizeof(buf),
1595 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1598 err = hwrite(packfile, buf, nh, ctx);
1601 *packfile_size += nh;
1602 err = hwrite(packfile, m->prev->id.sha1,
1603 sizeof(m->prev->id.sha1), ctx);
1606 *packfile_size += sizeof(m->prev->id.sha1);
1612 static const struct got_error *
1613 write_packed_object(off_t *packfile_size, FILE *packfile,
1614 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1615 SHA1_CTX *ctx, struct got_repository *repo)
1617 const struct got_error *err = NULL;
1618 struct got_deflate_checksum csum;
1621 struct got_raw_object *raw = NULL;
1624 csum.output_sha1 = ctx;
1625 csum.output_crc = NULL;
1627 m->off = ftello(packfile);
1628 if (m->delta_len == 0) {
1629 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1632 err = packhdr(&nh, buf, sizeof(buf),
1633 m->obj_type, raw->size);
1636 err = hwrite(packfile, buf, nh, ctx);
1639 *packfile_size += nh;
1640 if (raw->f == NULL) {
1641 err = got_deflate_to_file_mmap(&outlen,
1642 raw->data + raw->hdrlen, 0, raw->size,
1647 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1649 err = got_error_from_errno("fseeko");
1652 err = got_deflate_to_file(&outlen, raw->f,
1653 raw->size, packfile, &csum);
1657 *packfile_size += outlen;
1658 got_object_raw_close(raw);
1660 } else if (m->delta_buf) {
1661 err = deltahdr(packfile_size, ctx, packfile, m);
1664 err = got_deflate_to_file_mmap(&outlen,
1665 m->delta_buf, 0, m->delta_len, packfile, &csum);
1668 *packfile_size += outlen;
1670 m->delta_buf = NULL;
1672 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1674 err = got_error_from_errno("fseeko");
1677 err = deltahdr(packfile_size, ctx, packfile, m);
1680 err = got_deflate_to_file(&outlen, delta_cache,
1681 m->delta_len, packfile, &csum);
1684 *packfile_size += outlen;
1688 got_object_raw_close(raw);
1692 static const struct got_error *
1693 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1694 struct got_pack_meta **deltify, int ndeltify,
1695 struct got_pack_meta **reuse, int nreuse,
1696 int ncolored, int nfound, int ntrees, int nours,
1697 struct got_repository *repo,
1698 got_pack_progress_cb progress_cb, void *progress_arg,
1699 struct got_ratelimit *rl,
1700 got_cancel_cb cancel_cb, void *cancel_arg)
1702 const struct got_error *err = NULL;
1705 struct got_pack_meta *m;
1708 off_t packfile_size = 0;
1713 err = hwrite(packfile, "PACK", 4, &ctx);
1716 putbe32(buf, GOT_PACKFILE_VERSION);
1717 err = hwrite(packfile, buf, 4, &ctx);
1720 putbe32(buf, ndeltify + nreuse);
1721 err = hwrite(packfile, buf, 4, &ctx);
1725 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1727 for (i = 0; i < ndeltify; i++) {
1728 err = report_progress(progress_cb, progress_arg, rl,
1729 ncolored, nfound, ntrees, packfile_size, nours,
1730 ndeltify + nreuse, ndeltify + nreuse, i);
1734 err = write_packed_object(&packfile_size, packfile,
1735 delta_cache, m, &outfd, &ctx, repo);
1740 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1741 reuse_write_order_cmp);
1742 for (i = 0; i < nreuse; i++) {
1743 err = report_progress(progress_cb, progress_arg, rl,
1744 ncolored, nfound, ntrees, packfile_size, nours,
1745 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1749 err = write_packed_object(&packfile_size, packfile,
1750 delta_cache, m, &outfd, &ctx, repo);
1755 SHA1Final(pack_sha1, &ctx);
1756 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1757 if (n != SHA1_DIGEST_LENGTH)
1758 err = got_ferror(packfile, GOT_ERR_IO);
1759 packfile_size += SHA1_DIGEST_LENGTH;
1760 packfile_size += sizeof(struct got_packfile_hdr);
1762 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1763 packfile_size, nours, ndeltify + nreuse,
1764 ndeltify + nreuse, ndeltify + nreuse);
1769 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1770 err = got_error_from_errno("close");
1774 static const struct got_error *
1775 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1777 struct got_object_idset *idset = arg;
1779 if (got_object_idset_get_element_data(entry) == NULL)
1780 got_object_idset_remove_element(idset, entry);
1785 static const struct got_error *
1786 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1788 struct got_object_idset *idset = arg;
1789 struct got_pack_meta *m;
1791 m = got_object_idset_get_element_data(entry);
1792 if (m->have_reused_delta)
1793 got_object_idset_remove_element(idset, entry);
1798 static const struct got_error *
1799 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1801 struct got_pack_meta *m = data;
1802 struct got_pack_metavec *v = arg;
1804 return add_meta(m, v);
1807 const struct got_error *
1808 got_pack_create(uint8_t *packsha1, FILE *packfile,
1809 struct got_object_id **theirs, int ntheirs,
1810 struct got_object_id **ours, int nours,
1811 struct got_repository *repo, int loose_obj_only, int allow_empty,
1812 got_pack_progress_cb progress_cb, void *progress_arg,
1813 got_cancel_cb cancel_cb, void *cancel_arg)
1815 const struct got_error *err;
1816 int delta_cache_fd = -1;
1817 FILE *delta_cache = NULL;
1818 struct got_object_idset *idset;
1819 struct got_ratelimit rl;
1820 struct got_pack_metavec deltify, reuse;
1821 int ncolored = 0, nfound = 0, ntrees = 0;
1823 memset(&deltify, 0, sizeof(deltify));
1824 memset(&reuse, 0, sizeof(reuse));
1826 got_ratelimit_init(&rl, 0, 500);
1828 idset = got_object_idset_alloc();
1830 return got_error_from_errno("got_object_idset_alloc");
1832 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1833 ntheirs, ours, nours, repo, loose_obj_only,
1834 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1838 err = got_object_idset_for_each_element(idset,
1839 remove_unused_object, idset);
1844 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1845 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1850 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1851 err = got_error(GOT_ERR_CANNOT_PACK);
1855 delta_cache_fd = got_opentempfd();
1856 if (delta_cache_fd == -1) {
1857 err = got_error_from_errno("got_opentemp");
1862 reuse.meta = calloc(reuse.metasz,
1863 sizeof(struct got_pack_meta *));
1864 if (reuse.meta == NULL) {
1865 err = got_error_from_errno("calloc");
1869 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1870 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1871 cancel_cb, cancel_arg);
1874 if (reuse.nmeta > 0) {
1875 err = got_object_idset_for_each_element(idset,
1876 remove_reused_object, idset);
1881 delta_cache = fdopen(delta_cache_fd, "a+");
1882 if (delta_cache == NULL) {
1883 err = got_error_from_errno("fdopen");
1886 delta_cache_fd = -1;
1888 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1889 err = got_error_from_errno("fseeko");
1893 deltify.meta = calloc(got_object_idset_num_elements(idset),
1894 sizeof(struct got_pack_meta *));
1895 if (deltify.meta == NULL) {
1896 err = got_error_from_errno("calloc");
1899 deltify.metasz = got_object_idset_num_elements(idset);
1901 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1904 if (deltify.nmeta > 0) {
1905 err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1906 nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1907 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1910 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1911 err = got_error_from_errno("fseeko");
1916 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1917 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1918 nours, repo, progress_cb, progress_arg, &rl,
1919 cancel_cb, cancel_arg);
1923 free_nmeta(deltify.meta, deltify.nmeta);
1924 free_nmeta(reuse.meta, reuse.nmeta);
1925 got_object_idset_free(idset);
1926 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1927 err = got_error_from_errno("close");
1928 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1929 err = got_error_from_errno("fclose");