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))
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 struct got_pack_meta {
69 struct got_object_id id;
75 /* The best delta we picked */
76 struct got_pack_meta *head;
77 struct got_pack_meta *prev;
78 unsigned char *delta_buf; /* if not encoded in delta cache file */
79 off_t delta_offset; /* offset in delta cache file */
80 off_t delta_len; /* encoded delta length */
83 int have_reused_delta;
84 off_t reused_delta_offset; /* offset of delta in reused pack file */
85 struct got_object_id *base_obj_id;
87 /* Only used for delta window */
88 struct got_delta_table *dtab;
90 /* Only used for writing offset deltas */
94 struct got_pack_metavec {
95 struct got_pack_meta **meta;
100 static const struct got_error *
101 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
102 const char *path, int obj_type, time_t mtime)
104 const struct got_error *err = NULL;
105 struct got_pack_meta *m;
109 m = calloc(1, sizeof(*m));
111 return got_error_from_errno("calloc");
113 memcpy(&m->id, id, sizeof(m->id));
115 m->path = strdup(path);
116 if (m->path == NULL) {
117 err = got_error_from_errno("strdup");
122 m->obj_type = obj_type;
129 clear_meta(struct got_pack_meta *meta)
135 free(meta->delta_buf);
136 meta->delta_buf = NULL;
137 free(meta->base_obj_id);
138 meta->base_obj_id = NULL;
142 free_nmeta(struct got_pack_meta **meta, int nmeta)
146 for (i = 0; i < nmeta; i++)
152 delta_order_cmp(const void *pa, const void *pb)
154 struct got_pack_meta *a, *b;
157 a = *(struct got_pack_meta **)pa;
158 b = *(struct got_pack_meta **)pb;
160 if (a->obj_type != b->obj_type)
161 return a->obj_type - b->obj_type;
162 cmp = strcmp(a->path, b->path);
165 if (a->mtime < b->mtime)
167 if (a->mtime > b->mtime)
169 return got_object_id_cmp(&a->id, &b->id);
173 delta_size(struct got_delta_instruction *deltas, int ndeltas)
177 for (i = 0; i < ndeltas; i++) {
179 size += GOT_DELTA_SIZE_SHIFT;
181 size += deltas[i].len + 1;
186 static const struct got_error *
187 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
191 if (*len + nseg >= *sz) {
192 while (*len + nseg >= *sz)
194 n = realloc(*p, *sz);
196 return got_error_from_errno("realloc");
199 memcpy(*p + *len, seg, nseg);
204 static const struct got_error *
205 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
206 struct got_delta_instruction *deltas, int ndeltas,
207 off_t delta_size, off_t base_size)
209 const struct got_error *err;
210 unsigned char buf[16], *bp;
214 struct got_delta_instruction *d;
216 m->delta_buf = malloc(delta_size);
217 if (m->delta_buf == NULL)
218 return got_error_from_errno("calloc");
220 /* base object size */
221 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
222 n = base_size >> GOT_DELTA_SIZE_SHIFT;
223 for (i = 1; n > 0; i++) {
224 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
225 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
226 n >>= GOT_DELTA_SIZE_SHIFT;
228 err = append(&m->delta_buf, &len, &delta_size, buf, i);
232 /* target object size */
233 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
234 n = o->size >> GOT_DELTA_SIZE_SHIFT;
235 for (i = 1; n > 0; i++) {
236 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
237 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
238 n >>= GOT_DELTA_SIZE_SHIFT;
240 err = append(&m->delta_buf, &len, &delta_size, buf, i);
244 for (j = 0; j < ndeltas; j++) {
249 buf[0] = GOT_DELTA_BASE_COPY;
250 for (i = 0; i < 4; i++) {
251 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
260 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
261 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
262 for (i = 0; i < 3 && n > 0; i++) {
263 buf[0] |= 1 << (i + 4);
268 err = append(&m->delta_buf, &len, &delta_size,
272 } else if (o->f == NULL) {
274 while (n != d->len) {
275 buf[0] = (d->len - n < 127) ? d->len - n : 127;
276 err = append(&m->delta_buf, &len, &delta_size,
280 err = append(&m->delta_buf, &len, &delta_size,
281 o->data + o->hdrlen + d->offset + n,
290 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
291 return got_error_from_errno("fseeko");
293 while (n != d->len) {
294 buf[0] = (d->len - n < 127) ? d->len - n : 127;
295 err = append(&m->delta_buf, &len, &delta_size,
299 r = fread(content, 1, buf[0], o->f);
301 return got_ferror(o->f, GOT_ERR_IO);
302 err = append(&m->delta_buf, &len, &delta_size,
315 static const struct got_error *
316 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
317 struct got_delta_instruction *deltas, int ndeltas,
318 off_t base_size, FILE *f)
320 unsigned char buf[16], *bp;
324 struct got_delta_instruction *d;
326 /* base object size */
327 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
328 n = base_size >> GOT_DELTA_SIZE_SHIFT;
329 for (i = 1; n > 0; i++) {
330 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
331 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
332 n >>= GOT_DELTA_SIZE_SHIFT;
334 w = fwrite(buf, 1, i, f);
336 return got_ferror(f, GOT_ERR_IO);
338 /* target object size */
339 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
340 n = o->size >> GOT_DELTA_SIZE_SHIFT;
341 for (i = 1; n > 0; i++) {
342 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
343 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
344 n >>= GOT_DELTA_SIZE_SHIFT;
346 w = fwrite(buf, 1, i, f);
348 return got_ferror(f, GOT_ERR_IO);
350 for (j = 0; j < ndeltas; j++) {
355 buf[0] = GOT_DELTA_BASE_COPY;
356 for (i = 0; i < 4; i++) {
357 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
366 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
367 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
368 for (i = 0; i < 3 && n > 0; i++) {
369 buf[0] |= 1 << (i + 4);
374 w = fwrite(buf, 1, bp - buf, f);
376 return got_ferror(f, GOT_ERR_IO);
377 } else if (o->f == NULL) {
379 while (n != d->len) {
380 buf[0] = (d->len - n < 127) ? d->len - n : 127;
381 w = fwrite(buf, 1, 1, f);
383 return got_ferror(f, GOT_ERR_IO);
384 w = fwrite(o->data + o->hdrlen + d->offset + n,
387 return got_ferror(f, GOT_ERR_IO);
393 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
394 return got_error_from_errno("fseeko");
396 while (n != d->len) {
397 buf[0] = (d->len - n < 127) ? d->len - n : 127;
398 w = fwrite(buf, 1, 1, f);
400 return got_ferror(f, GOT_ERR_IO);
401 r = fread(content, 1, buf[0], o->f);
403 return got_ferror(o->f, GOT_ERR_IO);
404 w = fwrite(content, 1, buf[0], f);
406 return got_ferror(f, GOT_ERR_IO);
412 m->delta_len = ftello(f) - m->delta_offset;
416 static const struct got_error *
417 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
418 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
419 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
422 const struct got_error *err;
425 if (progress_cb == NULL)
428 err = got_ratelimit_check(&elapsed, rl);
432 return progress_cb(progress_arg, ncolored, nfound, ntrees,
433 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
436 static const struct got_error *
437 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
439 if (v->nmeta == v->metasz){
440 size_t newsize = 2 * v->metasz;
441 struct got_pack_meta **new;
442 new = reallocarray(v->meta, newsize, sizeof(*new));
444 return got_error_from_errno("reallocarray");
449 v->meta[v->nmeta++] = m;
453 static const struct got_error *
454 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
455 struct got_object_idset *idset, struct got_pack *pack,
456 struct got_packidx *packidx, int delta_cache_fd,
457 struct got_repository *repo)
459 const struct got_error *err = NULL;
460 struct got_pack_meta *base = NULL;
461 struct got_object_id *base_obj_id = NULL;
462 off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
463 uint64_t base_size, result_size;
465 if (m->have_reused_delta)
468 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
469 &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
470 packidx, idx, &m->id, repo);
474 if (delta_offset + delta_len < delta_offset)
475 return got_error(GOT_ERR_BAD_PACKFILE);
477 base = got_object_idset_get(idset, base_obj_id);
481 m->delta_len = delta_len;
482 m->delta_offset = delta_cache_offset;
484 m->size = result_size;
485 m->have_reused_delta = 1;
486 m->reused_delta_offset = delta_offset;
487 m->base_obj_id = base_obj_id;
489 err = add_meta(m, v);
495 static const struct got_error *
496 find_pack_for_reuse(struct got_packidx **best_packidx,
497 struct got_repository *repo)
499 const struct got_error *err = NULL;
500 struct got_pathlist_entry *pe;
501 const char *best_packidx_path = NULL;
504 *best_packidx = NULL;
506 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
507 const char *path_packidx = pe->path;
508 struct got_packidx *packidx;
511 err = got_repo_get_packidx(&packidx, path_packidx, repo);
515 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
516 if (nobj > nobj_max) {
517 best_packidx_path = path_packidx;
522 if (best_packidx_path) {
523 err = got_repo_get_packidx(best_packidx, best_packidx_path,
530 struct search_deltas_arg {
531 struct got_packidx *packidx;
532 struct got_pack *pack;
533 struct got_object_idset *idset;
534 struct got_pack_metavec *v;
536 struct got_repository *repo;
537 got_pack_progress_cb progress_cb;
539 struct got_ratelimit *rl;
540 got_cancel_cb cancel_cb;
548 static const struct got_error *
549 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
551 const struct got_error *err;
552 struct got_pack_meta *m = data;
553 struct search_deltas_arg *a = arg;
555 struct got_object *obj = NULL;
558 err = (*a->cancel_cb)(a->cancel_arg);
563 if (!got_repo_check_packidx_bloom_filter(a->repo,
564 a->packidx->path_packidx, id))
567 obj_idx = got_packidx_get_object_idx(a->packidx, id);
572 * Opening and closing an object just to check its flags
573 * is a bit expensive. We could have an imsg which requests
574 * plain type/size information for an object without doing
575 * work such as traversing the object's entire delta chain
576 * to find the base object type, and other such info which
577 * we don't really need here.
579 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
580 a->packidx, obj_idx, a->repo);
584 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
585 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
586 a->delta_cache_fd, a->repo);
589 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
590 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
591 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
594 got_object_close(obj);
598 static const struct got_error *
599 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
600 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
601 struct got_repository *repo,
602 got_pack_progress_cb progress_cb, void *progress_arg,
603 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
605 const struct got_error *err = NULL;
606 char *path_packfile = NULL;
607 struct got_packidx *packidx;
608 struct got_pack *pack;
609 struct search_deltas_arg sda;
611 err = find_pack_for_reuse(&packidx, repo);
618 err = got_packidx_get_packfile_path(&path_packfile,
619 packidx->path_packidx);
623 pack = got_repo_get_cached_pack(repo, path_packfile);
625 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
630 sda.packidx = packidx;
634 sda.delta_cache_fd = delta_cache_fd;
636 sda.progress_cb = progress_cb;
637 sda.progress_arg = progress_arg;
639 sda.cancel_cb = cancel_cb;
640 sda.cancel_arg = cancel_arg;
641 sda.ncolored = ncolored;
644 sda.ncommits = ncommits;
645 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
651 static const struct got_error *
652 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
653 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
654 struct got_repository *repo,
655 got_pack_progress_cb progress_cb, void *progress_arg,
656 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
658 const struct got_error *err = NULL;
659 struct got_pack_meta *m = NULL, *base = NULL;
660 struct got_raw_object *raw = NULL, *base_raw = NULL;
661 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
662 int i, j, ndeltas, best_ndeltas;
663 off_t size, best_size;
664 const int max_base_candidates = 3;
665 size_t delta_memsize = 0;
666 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
669 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
670 for (i = 0; i < nmeta; i++) {
672 err = (*cancel_cb)(cancel_arg);
676 err = report_progress(progress_cb, progress_arg, rl,
677 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
683 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
684 m->obj_type == GOT_OBJ_TYPE_TAG)
687 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
692 if (raw->f == NULL) {
693 err = got_deltify_init_mem(&m->dtab, raw->data,
694 raw->hdrlen, raw->size + raw->hdrlen);
696 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
697 raw->size + raw->hdrlen);
702 if (i > max_base_candidates) {
703 struct got_pack_meta *n = NULL;
704 n = meta[i - (max_base_candidates + 1)];
705 got_deltify_free(n->dtab);
709 best_size = raw->size;
711 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
713 err = (*cancel_cb)(cancel_arg);
718 /* long chains make unpacking slow, avoid such bases */
719 if (base->nchain >= 128 ||
720 base->obj_type != m->obj_type)
723 err = got_object_raw_open(&base_raw, &outfd, repo,
728 if (raw->f == NULL && base_raw->f == NULL) {
729 err = got_deltify_mem_mem(&deltas, &ndeltas,
730 raw->data, raw->hdrlen,
731 raw->size + raw->hdrlen,
732 base->dtab, base_raw->data,
734 base_raw->size + base_raw->hdrlen);
735 } else if (raw->f == NULL) {
736 err = got_deltify_mem_file(&deltas, &ndeltas,
737 raw->data, raw->hdrlen,
738 raw->size + raw->hdrlen,
739 base->dtab, base_raw->f,
741 base_raw->size + base_raw->hdrlen);
742 } else if (base_raw->f == NULL) {
743 err = got_deltify_file_mem(&deltas, &ndeltas,
745 raw->size + raw->hdrlen,
746 base->dtab, base_raw->data,
748 base_raw->size + base_raw->hdrlen);
750 err = got_deltify(&deltas, &ndeltas,
752 raw->size + raw->hdrlen,
753 base->dtab, base_raw->f, base_raw->hdrlen,
754 base_raw->size + base_raw->hdrlen);
756 got_object_raw_close(base_raw);
761 size = delta_size(deltas, ndeltas);
762 if (size + 32 < best_size){
764 * if we already picked a best delta,
769 best_deltas = deltas;
770 best_ndeltas = ndeltas;
772 m->nchain = base->nchain + 1;
774 m->head = base->head;
784 if (best_ndeltas > 0) {
785 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
786 delta_memsize + best_size <= max_delta_memsize) {
787 delta_memsize += best_size;
788 err = encode_delta_in_mem(m, raw, best_deltas,
789 best_ndeltas, best_size, m->prev->size);
791 m->delta_offset = ftello(delta_cache);
794 * Storing compressed delta data in the delta
795 * cache file would probably be more efficient
796 * than writing uncompressed delta data here
797 * and compressing it while writing the pack
798 * file. This would also allow for reusing
799 * deltas in their compressed form.
801 err = encode_delta(m, raw, best_deltas,
802 best_ndeltas, m->prev->size, delta_cache);
811 got_object_raw_close(raw);
815 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
816 got_deltify_free(meta[i]->dtab);
817 meta[i]->dtab = NULL;
820 got_object_raw_close(raw);
822 got_object_raw_close(base_raw);
823 if (outfd != -1 && close(outfd) == -1 && err == NULL)
824 err = got_error_from_errno("close");
830 static const struct got_error *
831 search_packidx(int *found, struct got_object_id *id,
832 struct got_repository *repo)
834 const struct got_error *err = NULL;
835 struct got_packidx *packidx = NULL;
840 err = got_repo_search_packidx(&packidx, &idx, repo, id);
842 *found = 1; /* object is already packed */
843 else if (err->code == GOT_ERR_NO_OBJ)
848 static const struct got_error *
849 add_object(int want_meta, struct got_object_idset *idset,
850 struct got_object_id *id, const char *path, int obj_type,
851 time_t mtime, int loose_obj_only, struct got_repository *repo,
852 int *ncolored, int *nfound, int *ntrees,
853 got_pack_progress_cb progress_cb, void *progress_arg,
854 struct got_ratelimit *rl)
856 const struct got_error *err;
857 struct got_pack_meta *m = NULL;
859 if (loose_obj_only) {
861 err = search_packidx(&is_packed, id, repo);
864 if (is_packed && want_meta)
869 err = alloc_meta(&m, id, path, obj_type, mtime);
874 err = report_progress(progress_cb, progress_arg, rl,
875 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
883 err = got_object_idset_add(idset, id, m);
891 static const struct got_error *
892 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
893 struct got_object_idset *idset, struct got_object_id *tree_id,
894 const char *dpath, time_t mtime, struct got_repository *repo,
895 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
896 got_pack_progress_cb progress_cb, void *progress_arg,
897 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
899 const struct got_error *err;
900 struct got_tree_object *tree;
904 err = got_object_open_as_tree(&tree, repo, tree_id);
909 err = report_progress(progress_cb, progress_arg, rl,
910 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
914 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
915 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
916 struct got_object_id *id = got_tree_entry_get_id(e);
917 mode_t mode = got_tree_entry_get_mode(e);
920 err = (*cancel_cb)(cancel_arg);
925 if (got_object_tree_entry_is_submodule(e) ||
926 got_object_idset_contains(idset, id))
929 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
930 got_tree_entry_get_name(e)) == -1) {
931 err = got_error_from_errno("asprintf");
936 struct got_object_qid *qid;
937 err = got_object_qid_alloc(&qid, id);
940 STAILQ_INSERT_TAIL(ids, qid, entry);
941 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
942 err = add_object(want_meta, idset, id, p,
943 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
944 ncolored, nfound, ntrees,
945 progress_cb, progress_arg, rl);
953 got_object_tree_close(tree);
958 static const struct got_error *
959 load_tree(int want_meta, struct got_object_idset *idset,
960 struct got_object_id *tree_id, const char *dpath, time_t mtime,
961 struct got_repository *repo, int loose_obj_only,
962 int *ncolored, int *nfound, int *ntrees,
963 got_pack_progress_cb progress_cb, void *progress_arg,
964 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
966 const struct got_error *err = NULL;
967 struct got_object_id_queue tree_ids;
968 struct got_object_qid *qid;
970 if (got_object_idset_contains(idset, tree_id))
973 err = got_object_qid_alloc(&qid, tree_id);
977 STAILQ_INIT(&tree_ids);
978 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
980 while (!STAILQ_EMPTY(&tree_ids)) {
982 err = (*cancel_cb)(cancel_arg);
987 qid = STAILQ_FIRST(&tree_ids);
988 STAILQ_REMOVE_HEAD(&tree_ids, entry);
990 if (got_object_idset_contains(idset, &qid->id)) {
991 got_object_qid_free(qid);
995 err = add_object(want_meta, idset, &qid->id, dpath,
996 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo,
997 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
999 got_object_qid_free(qid);
1003 err = load_tree_entries(&tree_ids, want_meta, idset, &qid->id,
1004 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1005 ntrees, progress_cb, progress_arg, rl,
1006 cancel_cb, cancel_arg);
1007 got_object_qid_free(qid);
1012 got_object_id_queue_free(&tree_ids);
1016 static const struct got_error *
1017 load_commit(int want_meta, struct got_object_idset *idset,
1018 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1019 int *ncolored, int *nfound, int *ntrees,
1020 got_pack_progress_cb progress_cb, void *progress_arg,
1021 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1023 const struct got_error *err;
1024 struct got_commit_object *commit;
1026 if (got_object_idset_contains(idset, id))
1029 if (loose_obj_only) {
1031 err = search_packidx(&is_packed, id, repo);
1034 if (is_packed && want_meta)
1038 err = got_object_open_as_commit(&commit, repo, id);
1042 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1043 got_object_commit_get_committer_time(commit),
1044 loose_obj_only, repo,
1045 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1049 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1050 "", got_object_commit_get_committer_time(commit),
1051 repo, loose_obj_only, ncolored, nfound, ntrees,
1052 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1054 got_object_commit_close(commit);
1058 static const struct got_error *
1059 load_tag(int want_meta, struct got_object_idset *idset,
1060 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1061 int *ncolored, int *nfound, int *ntrees,
1062 got_pack_progress_cb progress_cb, void *progress_arg,
1063 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1065 const struct got_error *err;
1066 struct got_tag_object *tag = NULL;
1068 if (got_object_idset_contains(idset, id))
1071 if (loose_obj_only) {
1073 err = search_packidx(&is_packed, id, repo);
1076 if (is_packed && want_meta)
1080 err = got_object_open_as_tag(&tag, repo, id);
1084 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1085 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1086 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1090 switch (got_object_tag_get_object_type(tag)) {
1091 case GOT_OBJ_TYPE_COMMIT:
1092 err = load_commit(want_meta, idset,
1093 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1094 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1095 cancel_cb, cancel_arg);
1097 case GOT_OBJ_TYPE_TREE:
1098 err = load_tree(want_meta, idset,
1099 got_object_tag_get_object_id(tag), "",
1100 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1101 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1102 cancel_cb, cancel_arg);
1109 got_object_tag_close(tag);
1113 enum findtwixt_color {
1120 static const int findtwixt_colors[] = {
1127 static const struct got_error *
1128 paint_commit(struct got_object_qid *qid, int color)
1130 if (color < 0 || color >= nitems(findtwixt_colors))
1131 return got_error(GOT_ERR_RANGE);
1133 qid->data = (void *)&findtwixt_colors[color];
1137 static const struct got_error *
1138 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1139 int color, struct got_repository *repo)
1141 const struct got_error *err;
1142 struct got_object_qid *qid;
1144 err = got_object_qid_alloc(&qid, id);
1148 STAILQ_INSERT_TAIL(ids, qid, entry);
1149 return paint_commit(qid, color);
1152 struct append_id_arg {
1153 struct got_object_id **array;
1155 struct got_object_idset *drop;
1156 struct got_object_idset *skip;
1159 static const struct got_error *
1160 append_id(struct got_object_id *id, void *data, void *arg)
1162 struct append_id_arg *a = arg;
1164 if (got_object_idset_contains(a->skip, id) ||
1165 got_object_idset_contains(a->drop, id))
1168 a->array[++a->idx] = got_object_id_dup(id);
1169 if (a->array[a->idx] == NULL)
1170 return got_error_from_errno("got_object_id_dup");
1175 static const struct got_error *
1176 queue_commit_or_tag_id(struct got_object_id *id, int color,
1177 struct got_object_id_queue *ids, struct got_repository *repo)
1179 const struct got_error *err;
1180 struct got_tag_object *tag = NULL;
1183 err = got_object_get_type(&obj_type, repo, id);
1187 if (obj_type == GOT_OBJ_TYPE_TAG) {
1188 err = got_object_open_as_tag(&tag, repo, id);
1191 obj_type = got_object_tag_get_object_type(tag);
1192 id = got_object_tag_get_object_id(tag);
1195 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1196 err = queue_commit_id(ids, id, color, repo);
1202 got_object_tag_close(tag);
1206 static const struct got_error *
1207 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1208 struct got_object_idset *keep, struct got_object_idset *drop,
1209 struct got_object_idset *skip, struct got_repository *repo,
1210 got_pack_progress_cb progress_cb, void *progress_arg,
1211 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1213 const struct got_error *err = NULL;
1214 struct got_commit_object *commit = NULL;
1215 const struct got_object_id_queue *parents;
1216 struct got_object_qid *qid;
1217 int nqueued = nids, nskip = 0;
1219 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1223 err = cancel_cb(cancel_arg);
1228 qid = STAILQ_FIRST(ids);
1229 STAILQ_REMOVE_HEAD(ids, entry);
1231 color = *((int *)qid->data);
1232 if (color == COLOR_SKIP)
1235 if (got_object_idset_contains(skip, &qid->id)) {
1236 got_object_qid_free(qid);
1242 if (got_object_idset_contains(keep, &qid->id)) {
1243 got_object_qid_free(qid);
1246 if (got_object_idset_contains(drop, &qid->id)) {
1247 err = paint_commit(qid, COLOR_SKIP);
1253 err = got_object_idset_add(keep, &qid->id, NULL);
1258 if (got_object_idset_contains(drop, &qid->id)) {
1259 got_object_qid_free(qid);
1262 if (got_object_idset_contains(keep, &qid->id)) {
1263 err = paint_commit(qid, COLOR_SKIP);
1269 err = got_object_idset_add(drop, &qid->id, NULL);
1274 if (!got_object_idset_contains(skip, &qid->id)) {
1275 err = got_object_idset_add(skip, &qid->id,
1282 /* should not happen */
1283 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1284 "%s invalid commit color %d", __func__, color);
1288 err = report_progress(progress_cb, progress_arg, rl,
1289 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1294 err = got_object_open_as_commit(&commit, repo, &qid->id);
1298 parents = got_object_commit_get_parent_ids(commit);
1300 struct got_object_qid *pid;
1301 color = *((int *)qid->data);
1302 STAILQ_FOREACH(pid, parents, entry) {
1303 err = queue_commit_id(ids, &pid->id, color,
1308 if (color == COLOR_SKIP)
1313 got_object_commit_close(commit);
1315 got_object_qid_free(qid);
1319 got_object_commit_close(commit);
1323 static const struct got_error *
1324 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1325 struct got_object_id **head, int nhead,
1326 struct got_object_id **tail, int ntail,
1327 struct got_repository *repo,
1328 got_pack_progress_cb progress_cb, void *progress_arg,
1329 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1331 const struct got_error *err = NULL;
1332 struct got_object_id_queue ids;
1333 struct got_object_idset *keep, *drop, *skip = NULL;
1341 keep = got_object_idset_alloc();
1343 return got_error_from_errno("got_object_idset_alloc");
1345 drop = got_object_idset_alloc();
1347 err = got_error_from_errno("got_object_idset_alloc");
1351 skip = got_object_idset_alloc();
1353 err = got_error_from_errno("got_object_idset_alloc");
1357 for (i = 0; i < nhead; i++) {
1358 struct got_object_id *id = head[i];
1361 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1366 for (i = 0; i < ntail; i++) {
1367 struct got_object_id *id = tail[i];
1370 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1375 err = paint_commits(ncolored, &ids, nhead + ntail,
1376 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1377 cancel_cb, cancel_arg);
1381 nkeep = got_object_idset_num_elements(keep);
1383 struct append_id_arg arg;
1384 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1385 if (arg.array == NULL) {
1386 err = got_error_from_errno("calloc");
1392 err = got_object_idset_for_each(keep, append_id, &arg);
1398 *nres = arg.idx + 1;
1401 got_object_idset_free(keep);
1402 got_object_idset_free(drop);
1404 got_object_idset_free(skip);
1405 got_object_id_queue_free(&ids);
1409 static const struct got_error *
1410 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1411 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1412 struct got_object_id **ours, int nours, struct got_repository *repo,
1413 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1414 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1416 const struct got_error *err = NULL;
1417 struct got_object_id **ids = NULL;
1418 int i, nobj = 0, obj_type;
1424 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1425 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1426 if (err || nobj == 0)
1429 for (i = 0; i < ntheirs; i++) {
1430 struct got_object_id *id = theirs[i];
1433 err = got_object_get_type(&obj_type, repo, id);
1436 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1437 err = load_commit(0, idset, id, repo,
1438 loose_obj_only, ncolored, nfound, ntrees,
1439 progress_cb, progress_arg, rl,
1440 cancel_cb, cancel_arg);
1443 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1444 err = load_tag(0, idset, id, repo,
1445 loose_obj_only, ncolored, nfound, ntrees,
1446 progress_cb, progress_arg, rl,
1447 cancel_cb, cancel_arg);
1453 for (i = 0; i < nobj; i++) {
1454 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1455 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1456 cancel_cb, cancel_arg);
1461 for (i = 0; i < nours; i++) {
1462 struct got_object_id *id = ours[i];
1463 struct got_pack_meta *m;
1466 m = got_object_idset_get(idset, id);
1468 err = got_object_get_type(&obj_type, repo, id);
1472 obj_type = m->obj_type;
1473 if (obj_type != GOT_OBJ_TYPE_TAG)
1475 err = load_tag(1, idset, id, repo, loose_obj_only,
1476 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1477 cancel_cb, cancel_arg);
1482 for (i = 0; i < nobj; i++) {
1489 const struct got_error *
1490 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1494 SHA1Update(ctx, buf, len);
1495 n = fwrite(buf, 1, len, f);
1497 return got_ferror(f, GOT_ERR_IO);
1502 putbe32(char *b, uint32_t n)
1511 write_order_cmp(const void *pa, const void *pb)
1513 struct got_pack_meta *a, *b, *ahd, *bhd;
1515 a = *(struct got_pack_meta **)pa;
1516 b = *(struct got_pack_meta **)pb;
1517 ahd = (a->head == NULL) ? a : a->head;
1518 bhd = (b->head == NULL) ? b : b->head;
1519 if (bhd->mtime < ahd->mtime)
1521 if (bhd->mtime > ahd->mtime)
1527 if (a->nchain != b->nchain)
1528 return a->nchain - b->nchain;
1529 if (a->mtime < b->mtime)
1531 if (a->mtime > b->mtime)
1533 return got_object_id_cmp(&a->id, &b->id);
1537 reuse_write_order_cmp(const void *pa, const void *pb)
1539 struct got_pack_meta *a, *b;
1541 a = *(struct got_pack_meta **)pa;
1542 b = *(struct got_pack_meta **)pb;
1544 if (a->reused_delta_offset < b->reused_delta_offset)
1546 if (a->reused_delta_offset > b->reused_delta_offset)
1551 static const struct got_error *
1552 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1558 hdr[0] = obj_type << 4;
1559 hdr[0] |= len & 0xf;
1561 for (i = 1; len != 0; i++){
1563 return got_error(GOT_ERR_NO_SPACE);
1564 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1565 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1566 len >>= GOT_DELTA_SIZE_SHIFT;
1574 packoff(char *hdr, off_t off)
1579 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1580 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1581 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1582 GOT_DELTA_SIZE_MORE;
1587 hdr[j++] = rbuf[--i];
1591 static const struct got_error *
1592 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1593 struct got_pack_meta *m)
1595 const struct got_error *err;
1599 if (m->prev->off != 0) {
1600 err = packhdr(&nh, buf, sizeof(buf),
1601 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1604 nh += packoff(buf + nh, m->off - m->prev->off);
1605 err = hwrite(packfile, buf, nh, ctx);
1608 *packfile_size += nh;
1610 err = packhdr(&nh, buf, sizeof(buf),
1611 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1614 err = hwrite(packfile, buf, nh, ctx);
1617 *packfile_size += nh;
1618 err = hwrite(packfile, m->prev->id.sha1,
1619 sizeof(m->prev->id.sha1), ctx);
1622 *packfile_size += sizeof(m->prev->id.sha1);
1628 static const struct got_error *
1629 write_packed_object(off_t *packfile_size, FILE *packfile,
1630 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1631 SHA1_CTX *ctx, struct got_repository *repo)
1633 const struct got_error *err = NULL;
1634 struct got_deflate_checksum csum;
1637 struct got_raw_object *raw = NULL;
1640 csum.output_sha1 = ctx;
1641 csum.output_crc = NULL;
1643 m->off = ftello(packfile);
1644 if (m->delta_len == 0) {
1645 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1648 err = packhdr(&nh, buf, sizeof(buf),
1649 m->obj_type, raw->size);
1652 err = hwrite(packfile, buf, nh, ctx);
1655 *packfile_size += nh;
1656 if (raw->f == NULL) {
1657 err = got_deflate_to_file_mmap(&outlen,
1658 raw->data + raw->hdrlen, 0, raw->size,
1663 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1665 err = got_error_from_errno("fseeko");
1668 err = got_deflate_to_file(&outlen, raw->f,
1669 raw->size, packfile, &csum);
1673 *packfile_size += outlen;
1674 got_object_raw_close(raw);
1676 } else if (m->delta_buf) {
1677 err = deltahdr(packfile_size, ctx, packfile, m);
1680 err = got_deflate_to_file_mmap(&outlen,
1681 m->delta_buf, 0, m->delta_len, packfile, &csum);
1684 *packfile_size += outlen;
1686 m->delta_buf = NULL;
1688 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1690 err = got_error_from_errno("fseeko");
1693 err = deltahdr(packfile_size, ctx, packfile, m);
1696 err = got_deflate_to_file(&outlen, delta_cache,
1697 m->delta_len, packfile, &csum);
1700 *packfile_size += outlen;
1704 got_object_raw_close(raw);
1708 static const struct got_error *
1709 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1710 struct got_pack_meta **deltify, int ndeltify,
1711 struct got_pack_meta **reuse, int nreuse,
1712 int ncolored, int nfound, int ntrees, int nours,
1713 struct got_repository *repo,
1714 got_pack_progress_cb progress_cb, void *progress_arg,
1715 struct got_ratelimit *rl,
1716 got_cancel_cb cancel_cb, void *cancel_arg)
1718 const struct got_error *err = NULL;
1721 struct got_pack_meta *m;
1724 off_t packfile_size = 0;
1729 err = hwrite(packfile, "PACK", 4, &ctx);
1732 putbe32(buf, GOT_PACKFILE_VERSION);
1733 err = hwrite(packfile, buf, 4, &ctx);
1736 putbe32(buf, ndeltify + nreuse);
1737 err = hwrite(packfile, buf, 4, &ctx);
1741 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1743 for (i = 0; i < ndeltify; i++) {
1744 err = report_progress(progress_cb, progress_arg, rl,
1745 ncolored, nfound, ntrees, packfile_size, nours,
1746 ndeltify + nreuse, ndeltify + nreuse, i);
1750 err = write_packed_object(&packfile_size, packfile,
1751 delta_cache, m, &outfd, &ctx, repo);
1756 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1757 reuse_write_order_cmp);
1758 for (i = 0; i < nreuse; i++) {
1759 err = report_progress(progress_cb, progress_arg, rl,
1760 ncolored, nfound, ntrees, packfile_size, nours,
1761 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1765 err = write_packed_object(&packfile_size, packfile,
1766 delta_cache, m, &outfd, &ctx, repo);
1771 SHA1Final(pack_sha1, &ctx);
1772 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1773 if (n != SHA1_DIGEST_LENGTH)
1774 err = got_ferror(packfile, GOT_ERR_IO);
1775 packfile_size += SHA1_DIGEST_LENGTH;
1776 packfile_size += sizeof(struct got_packfile_hdr);
1778 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1779 packfile_size, nours, ndeltify + nreuse,
1780 ndeltify + nreuse, ndeltify + nreuse);
1785 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1786 err = got_error_from_errno("close");
1790 static const struct got_error *
1791 remove_unused_object(struct got_object_id *id, void *data, void *arg)
1793 struct got_object_idset *idset = arg;
1796 got_object_idset_remove(NULL, idset, id);
1801 static const struct got_error *
1802 remove_reused_object(struct got_object_id *id, void *data, void *arg)
1804 struct got_object_idset *idset = arg;
1805 struct got_pack_meta *m = data;
1807 if (m->have_reused_delta)
1808 got_object_idset_remove(NULL, idset, id);
1813 static const struct got_error *
1814 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1816 struct got_pack_meta *m = data;
1817 struct got_pack_metavec *v = arg;
1819 return add_meta(m, v);
1822 const struct got_error *
1823 got_pack_create(uint8_t *packsha1, FILE *packfile,
1824 struct got_object_id **theirs, int ntheirs,
1825 struct got_object_id **ours, int nours,
1826 struct got_repository *repo, int loose_obj_only, int allow_empty,
1827 got_pack_progress_cb progress_cb, void *progress_arg,
1828 got_cancel_cb cancel_cb, void *cancel_arg)
1830 const struct got_error *err;
1831 int delta_cache_fd = -1;
1832 FILE *delta_cache = NULL;
1833 struct got_object_idset *idset;
1834 struct got_ratelimit rl;
1835 struct got_pack_metavec deltify, reuse;
1836 int ncolored = 0, nfound = 0, ntrees = 0;
1838 memset(&deltify, 0, sizeof(deltify));
1839 memset(&reuse, 0, sizeof(reuse));
1841 got_ratelimit_init(&rl, 0, 500);
1843 idset = got_object_idset_alloc();
1845 return got_error_from_errno("got_object_idset_alloc");
1847 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1848 ntheirs, ours, nours, repo, loose_obj_only,
1849 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1853 err = got_object_idset_for_each(idset, remove_unused_object, idset);
1858 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1859 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1864 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1865 err = got_error(GOT_ERR_CANNOT_PACK);
1869 delta_cache_fd = got_opentempfd();
1870 if (delta_cache_fd == -1) {
1871 err = got_error_from_errno("got_opentemp");
1876 reuse.meta = calloc(reuse.metasz,
1877 sizeof(struct got_pack_meta *));
1878 if (reuse.meta == NULL) {
1879 err = got_error_from_errno("calloc");
1883 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1884 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1885 cancel_cb, cancel_arg);
1888 if (reuse.nmeta > 0) {
1889 err = got_object_idset_for_each(idset,
1890 remove_reused_object, idset);
1895 delta_cache = fdopen(delta_cache_fd, "a+");
1896 if (delta_cache == NULL) {
1897 err = got_error_from_errno("fdopen");
1900 delta_cache_fd = -1;
1902 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1903 err = got_error_from_errno("fseeko");
1907 deltify.meta = calloc(got_object_idset_num_elements(idset),
1908 sizeof(struct got_pack_meta *));
1909 if (deltify.meta == NULL) {
1910 err = got_error_from_errno("calloc");
1913 deltify.metasz = got_object_idset_num_elements(idset);
1915 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1918 if (deltify.nmeta > 0) {
1919 err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1920 nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1921 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1924 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1925 err = got_error_from_errno("fseeko");
1930 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1931 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1932 nours, repo, progress_cb, progress_arg, &rl,
1933 cancel_cb, cancel_arg);
1937 free_nmeta(deltify.meta, deltify.nmeta);
1938 free_nmeta(reuse.meta, reuse.nmeta);
1939 got_object_idset_free(idset);
1940 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1941 err = got_error_from_errno("close");
1942 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1943 err = got_error_from_errno("fclose");