2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
32 #if defined(__FreeBSD__)
36 #include "got_error.h"
37 #include "got_cancel.h"
38 #include "got_object.h"
40 #include "got_reference.h"
41 #include "got_repository_admin.h"
42 #include "got_opentemp.h"
44 #include "got_lib_deltify.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_idset.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_deflate.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_ratelimit.h"
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
60 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 struct got_pack_meta {
68 struct got_object_id id;
74 /* The best delta we picked */
75 struct got_pack_meta *head;
76 struct got_pack_meta *prev;
77 unsigned char *delta_buf; /* if not encoded in delta cache file */
78 off_t delta_offset; /* offset in delta cache file */
79 off_t delta_len; /* encoded delta length */
82 int have_reused_delta;
83 off_t reused_delta_offset; /* offset of delta in reused pack file */
84 struct got_object_id *base_obj_id;
86 /* Only used for delta window */
87 struct got_delta_table *dtab;
89 /* Only used for writing offset deltas */
93 struct got_pack_metavec {
94 struct got_pack_meta **meta;
99 static const struct got_error *
100 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
101 const char *path, int obj_type, time_t mtime)
103 const struct got_error *err = NULL;
104 struct got_pack_meta *m;
108 m = calloc(1, sizeof(*m));
110 return got_error_from_errno("calloc");
112 memcpy(&m->id, id, sizeof(m->id));
114 m->path = strdup(path);
115 if (m->path == NULL) {
116 err = got_error_from_errno("strdup");
121 m->obj_type = obj_type;
128 clear_meta(struct got_pack_meta *meta)
134 free(meta->delta_buf);
135 meta->delta_buf = NULL;
136 free(meta->base_obj_id);
137 meta->base_obj_id = NULL;
141 free_nmeta(struct got_pack_meta **meta, int nmeta)
145 for (i = 0; i < nmeta; i++)
151 delta_order_cmp(const void *pa, const void *pb)
153 struct got_pack_meta *a, *b;
156 a = *(struct got_pack_meta **)pa;
157 b = *(struct got_pack_meta **)pb;
159 if (a->obj_type != b->obj_type)
160 return a->obj_type - b->obj_type;
161 cmp = strcmp(a->path, b->path);
164 if (a->mtime < b->mtime)
166 if (a->mtime > b->mtime)
168 return got_object_id_cmp(&a->id, &b->id);
172 delta_size(struct got_delta_instruction *deltas, int ndeltas)
176 for (i = 0; i < ndeltas; i++) {
178 size += GOT_DELTA_SIZE_SHIFT;
180 size += deltas[i].len + 1;
185 static const struct got_error *
186 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
190 if (*len + nseg >= *sz) {
191 while (*len + nseg >= *sz)
193 n = realloc(*p, *sz);
195 return got_error_from_errno("realloc");
198 memcpy(*p + *len, seg, nseg);
203 static const struct got_error *
204 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
205 struct got_delta_instruction *deltas, int ndeltas,
206 off_t delta_size, off_t base_size)
208 const struct got_error *err;
209 unsigned char buf[16], *bp;
213 struct got_delta_instruction *d;
215 m->delta_buf = malloc(delta_size);
216 if (m->delta_buf == NULL)
217 return got_error_from_errno("calloc");
219 /* base object size */
220 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
221 n = base_size >> GOT_DELTA_SIZE_SHIFT;
222 for (i = 1; n > 0; i++) {
223 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
224 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
225 n >>= GOT_DELTA_SIZE_SHIFT;
227 err = append(&m->delta_buf, &len, &delta_size, buf, i);
231 /* target object size */
232 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
233 n = o->size >> GOT_DELTA_SIZE_SHIFT;
234 for (i = 1; n > 0; i++) {
235 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
236 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
237 n >>= GOT_DELTA_SIZE_SHIFT;
239 err = append(&m->delta_buf, &len, &delta_size, buf, i);
243 for (j = 0; j < ndeltas; j++) {
248 buf[0] = GOT_DELTA_BASE_COPY;
249 for (i = 0; i < 4; i++) {
250 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
259 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
260 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
261 for (i = 0; i < 3 && n > 0; i++) {
262 buf[0] |= 1 << (i + 4);
267 err = append(&m->delta_buf, &len, &delta_size,
271 } else if (o->f == NULL) {
273 while (n != d->len) {
274 buf[0] = (d->len - n < 127) ? d->len - n : 127;
275 err = append(&m->delta_buf, &len, &delta_size,
279 err = append(&m->delta_buf, &len, &delta_size,
280 o->data + o->hdrlen + d->offset + n,
289 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
290 return got_error_from_errno("fseeko");
292 while (n != d->len) {
293 buf[0] = (d->len - n < 127) ? d->len - n : 127;
294 err = append(&m->delta_buf, &len, &delta_size,
298 r = fread(content, 1, buf[0], o->f);
300 return got_ferror(o->f, GOT_ERR_IO);
301 err = append(&m->delta_buf, &len, &delta_size,
314 static const struct got_error *
315 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
316 struct got_delta_instruction *deltas, int ndeltas,
317 off_t base_size, FILE *f)
319 unsigned char buf[16], *bp;
323 struct got_delta_instruction *d;
325 /* base object size */
326 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
327 n = base_size >> GOT_DELTA_SIZE_SHIFT;
328 for (i = 1; n > 0; i++) {
329 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
330 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
331 n >>= GOT_DELTA_SIZE_SHIFT;
333 w = fwrite(buf, 1, i, f);
335 return got_ferror(f, GOT_ERR_IO);
337 /* target object size */
338 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
339 n = o->size >> GOT_DELTA_SIZE_SHIFT;
340 for (i = 1; n > 0; i++) {
341 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
342 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
343 n >>= GOT_DELTA_SIZE_SHIFT;
345 w = fwrite(buf, 1, i, f);
347 return got_ferror(f, GOT_ERR_IO);
349 for (j = 0; j < ndeltas; j++) {
354 buf[0] = GOT_DELTA_BASE_COPY;
355 for (i = 0; i < 4; i++) {
356 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
365 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
366 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
367 for (i = 0; i < 3 && n > 0; i++) {
368 buf[0] |= 1 << (i + 4);
373 w = fwrite(buf, 1, bp - buf, f);
375 return got_ferror(f, GOT_ERR_IO);
376 } else if (o->f == NULL) {
378 while (n != d->len) {
379 buf[0] = (d->len - n < 127) ? d->len - n : 127;
380 w = fwrite(buf, 1, 1, f);
382 return got_ferror(f, GOT_ERR_IO);
383 w = fwrite(o->data + o->hdrlen + d->offset + n,
386 return got_ferror(f, GOT_ERR_IO);
392 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
393 return got_error_from_errno("fseeko");
395 while (n != d->len) {
396 buf[0] = (d->len - n < 127) ? d->len - n : 127;
397 w = fwrite(buf, 1, 1, f);
399 return got_ferror(f, GOT_ERR_IO);
400 r = fread(content, 1, buf[0], o->f);
402 return got_ferror(o->f, GOT_ERR_IO);
403 w = fwrite(content, 1, buf[0], f);
405 return got_ferror(f, GOT_ERR_IO);
411 m->delta_len = ftello(f) - m->delta_offset;
415 static const struct got_error *
416 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
417 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
418 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
421 const struct got_error *err;
424 if (progress_cb == NULL)
427 err = got_ratelimit_check(&elapsed, rl);
431 return progress_cb(progress_arg, ncolored, nfound, ntrees,
432 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
435 static const struct got_error *
436 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
438 if (v->nmeta == v->metasz){
439 size_t newsize = 2 * v->metasz;
440 struct got_pack_meta **new;
441 new = reallocarray(v->meta, newsize, sizeof(*new));
443 return got_error_from_errno("reallocarray");
448 v->meta[v->nmeta++] = m;
452 static const struct got_error *
453 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
454 struct got_object_idset *idset, struct got_pack *pack,
455 struct got_packidx *packidx, int delta_cache_fd,
456 struct got_repository *repo)
458 const struct got_error *err = NULL;
459 struct got_pack_meta *base = NULL;
460 struct got_object_id *base_obj_id = NULL;
461 off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
462 uint64_t base_size, result_size;
464 if (m->have_reused_delta)
467 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
468 &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
469 packidx, idx, &m->id, repo);
473 if (delta_offset + delta_len < delta_offset)
474 return got_error(GOT_ERR_BAD_PACKFILE);
476 base = got_object_idset_get(idset, base_obj_id);
480 m->delta_len = delta_len;
481 m->delta_offset = delta_cache_offset;
483 m->size = result_size;
484 m->have_reused_delta = 1;
485 m->reused_delta_offset = delta_offset;
486 m->base_obj_id = base_obj_id;
488 err = add_meta(m, v);
494 static const struct got_error *
495 find_pack_for_reuse(struct got_packidx **best_packidx,
496 struct got_repository *repo)
498 const struct got_error *err = NULL;
499 struct got_pathlist_entry *pe;
500 const char *best_packidx_path = NULL;
503 *best_packidx = NULL;
505 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
506 const char *path_packidx = pe->path;
507 struct got_packidx *packidx;
510 err = got_repo_get_packidx(&packidx, path_packidx, repo);
514 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
515 if (nobj > nobj_max) {
516 best_packidx_path = path_packidx;
521 if (best_packidx_path) {
522 err = got_repo_get_packidx(best_packidx, best_packidx_path,
529 struct search_deltas_arg {
530 struct got_packidx *packidx;
531 struct got_pack *pack;
532 struct got_object_idset *idset;
533 struct got_pack_metavec *v;
535 struct got_repository *repo;
536 got_pack_progress_cb progress_cb;
538 struct got_ratelimit *rl;
539 got_cancel_cb cancel_cb;
547 static const struct got_error *
548 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
550 const struct got_error *err;
551 struct got_pack_meta *m = data;
552 struct search_deltas_arg *a = arg;
554 struct got_object *obj = NULL;
557 err = (*a->cancel_cb)(a->cancel_arg);
562 if (!got_repo_check_packidx_bloom_filter(a->repo,
563 a->packidx->path_packidx, id))
566 obj_idx = got_packidx_get_object_idx(a->packidx, id);
571 * Opening and closing an object just to check its flags
572 * is a bit expensive. We could have an imsg which requests
573 * plain type/size information for an object without doing
574 * work such as traversing the object's entire delta chain
575 * to find the base object type, and other such info which
576 * we don't really need here.
578 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
579 a->packidx, obj_idx, a->repo);
583 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
584 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
585 a->delta_cache_fd, a->repo);
588 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
589 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
590 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
593 got_object_close(obj);
597 static const struct got_error *
598 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
599 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
600 struct got_repository *repo,
601 got_pack_progress_cb progress_cb, void *progress_arg,
602 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
604 const struct got_error *err = NULL;
605 char *path_packfile = NULL;
606 struct got_packidx *packidx;
607 struct got_pack *pack;
608 struct search_deltas_arg sda;
610 err = find_pack_for_reuse(&packidx, repo);
617 err = got_packidx_get_packfile_path(&path_packfile,
618 packidx->path_packidx);
622 pack = got_repo_get_cached_pack(repo, path_packfile);
624 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
629 sda.packidx = packidx;
633 sda.delta_cache_fd = delta_cache_fd;
635 sda.progress_cb = progress_cb;
636 sda.progress_arg = progress_arg;
638 sda.cancel_cb = cancel_cb;
639 sda.cancel_arg = cancel_arg;
640 sda.ncolored = ncolored;
643 sda.ncommits = ncommits;
644 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
650 static const struct got_error *
651 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
652 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
653 struct got_repository *repo,
654 got_pack_progress_cb progress_cb, void *progress_arg,
655 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
657 const struct got_error *err = NULL;
658 struct got_pack_meta *m = NULL, *base = NULL;
659 struct got_raw_object *raw = NULL, *base_raw = NULL;
660 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
661 int i, j, ndeltas, best_ndeltas;
662 off_t size, best_size;
663 const int max_base_candidates = 3;
664 size_t delta_memsize = 0;
665 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
668 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
669 for (i = 0; i < nmeta; i++) {
671 err = (*cancel_cb)(cancel_arg);
675 err = report_progress(progress_cb, progress_arg, rl,
676 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
682 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
683 m->obj_type == GOT_OBJ_TYPE_TAG)
686 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
691 if (raw->f == NULL) {
692 err = got_deltify_init_mem(&m->dtab, raw->data,
693 raw->hdrlen, raw->size + raw->hdrlen);
695 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
696 raw->size + raw->hdrlen);
701 if (i > max_base_candidates) {
702 struct got_pack_meta *n = NULL;
703 n = meta[i - (max_base_candidates + 1)];
704 got_deltify_free(n->dtab);
708 best_size = raw->size;
710 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
712 err = (*cancel_cb)(cancel_arg);
717 /* long chains make unpacking slow, avoid such bases */
718 if (base->nchain >= 128 ||
719 base->obj_type != m->obj_type)
722 err = got_object_raw_open(&base_raw, &outfd, repo,
727 if (raw->f == NULL && base_raw->f == NULL) {
728 err = got_deltify_mem_mem(&deltas, &ndeltas,
729 raw->data, raw->hdrlen,
730 raw->size + raw->hdrlen,
731 base->dtab, base_raw->data,
733 base_raw->size + base_raw->hdrlen);
734 } else if (raw->f == NULL) {
735 err = got_deltify_mem_file(&deltas, &ndeltas,
736 raw->data, raw->hdrlen,
737 raw->size + raw->hdrlen,
738 base->dtab, base_raw->f,
740 base_raw->size + base_raw->hdrlen);
741 } else if (base_raw->f == NULL) {
742 err = got_deltify_file_mem(&deltas, &ndeltas,
744 raw->size + raw->hdrlen,
745 base->dtab, base_raw->data,
747 base_raw->size + base_raw->hdrlen);
749 err = got_deltify(&deltas, &ndeltas,
751 raw->size + raw->hdrlen,
752 base->dtab, base_raw->f, base_raw->hdrlen,
753 base_raw->size + base_raw->hdrlen);
755 got_object_raw_close(base_raw);
760 size = delta_size(deltas, ndeltas);
761 if (size + 32 < best_size){
763 * if we already picked a best delta,
768 best_deltas = deltas;
769 best_ndeltas = ndeltas;
771 m->nchain = base->nchain + 1;
773 m->head = base->head;
783 if (best_ndeltas > 0) {
784 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
785 delta_memsize + best_size <= max_delta_memsize) {
786 delta_memsize += best_size;
787 err = encode_delta_in_mem(m, raw, best_deltas,
788 best_ndeltas, best_size, m->prev->size);
790 m->delta_offset = ftello(delta_cache);
793 * Storing compressed delta data in the delta
794 * cache file would probably be more efficient
795 * than writing uncompressed delta data here
796 * and compressing it while writing the pack
797 * file. This would also allow for reusing
798 * deltas in their compressed form.
800 err = encode_delta(m, raw, best_deltas,
801 best_ndeltas, m->prev->size, delta_cache);
810 got_object_raw_close(raw);
814 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
815 got_deltify_free(meta[i]->dtab);
816 meta[i]->dtab = NULL;
819 got_object_raw_close(raw);
821 got_object_raw_close(base_raw);
822 if (outfd != -1 && close(outfd) == -1 && err == NULL)
823 err = got_error_from_errno("close");
829 static const struct got_error *
830 search_packidx(int *found, struct got_object_id *id,
831 struct got_repository *repo)
833 const struct got_error *err = NULL;
834 struct got_packidx *packidx = NULL;
839 err = got_repo_search_packidx(&packidx, &idx, repo, id);
841 *found = 1; /* object is already packed */
842 else if (err->code == GOT_ERR_NO_OBJ)
847 static const struct got_error *
848 add_object(int want_meta, struct got_object_idset *idset,
849 struct got_object_id *id, const char *path, int obj_type,
850 time_t mtime, int loose_obj_only, struct got_repository *repo,
851 int *ncolored, int *nfound, int *ntrees,
852 got_pack_progress_cb progress_cb, void *progress_arg,
853 struct got_ratelimit *rl)
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);
863 if (is_packed && want_meta)
868 err = alloc_meta(&m, id, path, obj_type, mtime);
873 err = report_progress(progress_cb, progress_arg, rl,
874 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
882 err = got_object_idset_add(idset, id, m);
890 static const struct got_error *
891 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
892 struct got_object_idset *idset, struct got_object_id *tree_id,
893 const char *dpath, time_t mtime, struct got_repository *repo,
894 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
895 got_pack_progress_cb progress_cb, void *progress_arg,
896 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
898 const struct got_error *err;
899 struct got_tree_object *tree;
903 err = got_object_open_as_tree(&tree, repo, tree_id);
908 err = report_progress(progress_cb, progress_arg, rl,
909 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
913 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
914 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
915 struct got_object_id *id = got_tree_entry_get_id(e);
916 mode_t mode = got_tree_entry_get_mode(e);
919 err = (*cancel_cb)(cancel_arg);
924 if (got_object_tree_entry_is_submodule(e) ||
925 got_object_idset_contains(idset, id))
928 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
929 got_tree_entry_get_name(e)) == -1) {
930 err = got_error_from_errno("asprintf");
935 struct got_object_qid *qid;
936 err = got_object_qid_alloc(&qid, id);
939 STAILQ_INSERT_TAIL(ids, qid, entry);
940 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
941 err = add_object(want_meta, idset, id, p,
942 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
943 ncolored, nfound, ntrees,
944 progress_cb, progress_arg, rl);
952 got_object_tree_close(tree);
957 static const struct got_error *
958 load_tree(int want_meta, struct got_object_idset *idset,
959 struct got_object_id *tree_id, const char *dpath, time_t mtime,
960 struct got_repository *repo, int loose_obj_only,
961 int *ncolored, int *nfound, int *ntrees,
962 got_pack_progress_cb progress_cb, void *progress_arg,
963 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
965 const struct got_error *err = NULL;
966 struct got_object_id_queue tree_ids;
967 struct got_object_qid *qid;
969 if (got_object_idset_contains(idset, tree_id))
972 err = got_object_qid_alloc(&qid, tree_id);
976 STAILQ_INIT(&tree_ids);
977 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
979 while (!STAILQ_EMPTY(&tree_ids)) {
981 err = (*cancel_cb)(cancel_arg);
986 qid = STAILQ_FIRST(&tree_ids);
987 STAILQ_REMOVE_HEAD(&tree_ids, entry);
989 if (got_object_idset_contains(idset, &qid->id)) {
990 got_object_qid_free(qid);
994 err = add_object(want_meta, idset, &qid->id, dpath,
995 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo,
996 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
998 got_object_qid_free(qid);
1002 err = load_tree_entries(&tree_ids, want_meta, idset, &qid->id,
1003 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1004 ntrees, progress_cb, progress_arg, rl,
1005 cancel_cb, cancel_arg);
1006 got_object_qid_free(qid);
1011 got_object_id_queue_free(&tree_ids);
1015 static const struct got_error *
1016 load_commit(int want_meta, struct got_object_idset *idset,
1017 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1018 int *ncolored, int *nfound, int *ntrees,
1019 got_pack_progress_cb progress_cb, void *progress_arg,
1020 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1022 const struct got_error *err;
1023 struct got_commit_object *commit;
1025 if (got_object_idset_contains(idset, id))
1028 if (loose_obj_only) {
1030 err = search_packidx(&is_packed, id, repo);
1033 if (is_packed && want_meta)
1037 err = got_object_open_as_commit(&commit, repo, id);
1041 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1042 got_object_commit_get_committer_time(commit),
1043 loose_obj_only, repo,
1044 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1048 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1049 "", got_object_commit_get_committer_time(commit),
1050 repo, loose_obj_only, ncolored, nfound, ntrees,
1051 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1053 got_object_commit_close(commit);
1057 static const struct got_error *
1058 load_tag(int want_meta, struct got_object_idset *idset,
1059 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1060 int *ncolored, int *nfound, int *ntrees,
1061 got_pack_progress_cb progress_cb, void *progress_arg,
1062 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1064 const struct got_error *err;
1065 struct got_tag_object *tag = NULL;
1067 if (got_object_idset_contains(idset, id))
1070 if (loose_obj_only) {
1072 err = search_packidx(&is_packed, id, repo);
1075 if (is_packed && want_meta)
1079 err = got_object_open_as_tag(&tag, repo, id);
1083 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1084 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1085 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1089 switch (got_object_tag_get_object_type(tag)) {
1090 case GOT_OBJ_TYPE_COMMIT:
1091 err = load_commit(want_meta, idset,
1092 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1093 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1094 cancel_cb, cancel_arg);
1096 case GOT_OBJ_TYPE_TREE:
1097 err = load_tree(want_meta, idset,
1098 got_object_tag_get_object_id(tag), "",
1099 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1100 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1101 cancel_cb, cancel_arg);
1108 got_object_tag_close(tag);
1112 enum findtwixt_color {
1119 static const int findtwixt_colors[] = {
1126 static const struct got_error *
1127 paint_commit(struct got_object_qid *qid, int color)
1129 if (color < 0 || color >= nitems(findtwixt_colors))
1130 return got_error(GOT_ERR_RANGE);
1132 qid->data = (void *)&findtwixt_colors[color];
1136 static const struct got_error *
1137 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1138 int color, struct got_repository *repo)
1140 const struct got_error *err;
1141 struct got_object_qid *qid;
1143 err = got_object_qid_alloc(&qid, id);
1147 STAILQ_INSERT_TAIL(ids, qid, entry);
1148 return paint_commit(qid, color);
1151 struct append_id_arg {
1152 struct got_object_id **array;
1154 struct got_object_idset *drop;
1155 struct got_object_idset *skip;
1158 static const struct got_error *
1159 append_id(struct got_object_id *id, void *data, void *arg)
1161 struct append_id_arg *a = arg;
1163 if (got_object_idset_contains(a->skip, id) ||
1164 got_object_idset_contains(a->drop, id))
1167 a->array[++a->idx] = got_object_id_dup(id);
1168 if (a->array[a->idx] == NULL)
1169 return got_error_from_errno("got_object_id_dup");
1174 static const struct got_error *
1175 queue_commit_or_tag_id(struct got_object_id *id, int color,
1176 struct got_object_id_queue *ids, struct got_repository *repo)
1178 const struct got_error *err;
1179 struct got_tag_object *tag = NULL;
1182 err = got_object_get_type(&obj_type, repo, id);
1186 if (obj_type == GOT_OBJ_TYPE_TAG) {
1187 err = got_object_open_as_tag(&tag, repo, id);
1190 obj_type = got_object_tag_get_object_type(tag);
1191 id = got_object_tag_get_object_id(tag);
1194 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1195 err = queue_commit_id(ids, id, color, repo);
1201 got_object_tag_close(tag);
1205 static const struct got_error *
1206 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1207 struct got_object_idset *keep, struct got_object_idset *drop,
1208 struct got_object_idset *skip, struct got_repository *repo,
1209 got_pack_progress_cb progress_cb, void *progress_arg,
1210 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1212 const struct got_error *err = NULL;
1213 struct got_commit_object *commit = NULL;
1214 const struct got_object_id_queue *parents;
1215 struct got_object_qid *qid;
1216 int nqueued = nids, nskip = 0;
1218 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1222 err = cancel_cb(cancel_arg);
1227 qid = STAILQ_FIRST(ids);
1228 STAILQ_REMOVE_HEAD(ids, entry);
1230 color = *((int *)qid->data);
1231 if (color == COLOR_SKIP)
1234 if (got_object_idset_contains(skip, &qid->id)) {
1235 got_object_qid_free(qid);
1241 if (got_object_idset_contains(keep, &qid->id)) {
1242 got_object_qid_free(qid);
1245 if (got_object_idset_contains(drop, &qid->id)) {
1246 err = paint_commit(qid, COLOR_SKIP);
1252 err = got_object_idset_add(keep, &qid->id, NULL);
1257 if (got_object_idset_contains(drop, &qid->id)) {
1258 got_object_qid_free(qid);
1261 if (got_object_idset_contains(keep, &qid->id)) {
1262 err = paint_commit(qid, COLOR_SKIP);
1268 err = got_object_idset_add(drop, &qid->id, NULL);
1273 if (!got_object_idset_contains(skip, &qid->id)) {
1274 err = got_object_idset_add(skip, &qid->id,
1281 /* should not happen */
1282 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1283 "%s invalid commit color %d", __func__, color);
1287 err = report_progress(progress_cb, progress_arg, rl,
1288 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1293 err = got_object_open_as_commit(&commit, repo, &qid->id);
1297 parents = got_object_commit_get_parent_ids(commit);
1299 struct got_object_qid *pid;
1300 color = *((int *)qid->data);
1301 STAILQ_FOREACH(pid, parents, entry) {
1302 err = queue_commit_id(ids, &pid->id, color,
1307 if (color == COLOR_SKIP)
1312 got_object_commit_close(commit);
1314 got_object_qid_free(qid);
1318 got_object_commit_close(commit);
1322 static const struct got_error *
1323 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1324 struct got_object_id **head, int nhead,
1325 struct got_object_id **tail, int ntail,
1326 struct got_repository *repo,
1327 got_pack_progress_cb progress_cb, void *progress_arg,
1328 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1330 const struct got_error *err = NULL;
1331 struct got_object_id_queue ids;
1332 struct got_object_idset *keep, *drop, *skip = NULL;
1340 keep = got_object_idset_alloc();
1342 return got_error_from_errno("got_object_idset_alloc");
1344 drop = got_object_idset_alloc();
1346 err = got_error_from_errno("got_object_idset_alloc");
1350 skip = got_object_idset_alloc();
1352 err = got_error_from_errno("got_object_idset_alloc");
1356 for (i = 0; i < nhead; i++) {
1357 struct got_object_id *id = head[i];
1360 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1365 for (i = 0; i < ntail; i++) {
1366 struct got_object_id *id = tail[i];
1369 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1374 err = paint_commits(ncolored, &ids, nhead + ntail,
1375 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1376 cancel_cb, cancel_arg);
1380 nkeep = got_object_idset_num_elements(keep);
1382 struct append_id_arg arg;
1383 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1384 if (arg.array == NULL) {
1385 err = got_error_from_errno("calloc");
1391 err = got_object_idset_for_each(keep, append_id, &arg);
1397 *nres = arg.idx + 1;
1400 got_object_idset_free(keep);
1401 got_object_idset_free(drop);
1403 got_object_idset_free(skip);
1404 got_object_id_queue_free(&ids);
1408 static const struct got_error *
1409 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1410 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1411 struct got_object_id **ours, int nours, struct got_repository *repo,
1412 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1413 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1415 const struct got_error *err = NULL;
1416 struct got_object_id **ids = NULL;
1417 int i, nobj = 0, obj_type;
1423 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1424 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1425 if (err || nobj == 0)
1428 for (i = 0; i < ntheirs; i++) {
1429 struct got_object_id *id = theirs[i];
1432 err = got_object_get_type(&obj_type, repo, id);
1435 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1436 err = load_commit(0, idset, id, repo,
1437 loose_obj_only, ncolored, nfound, ntrees,
1438 progress_cb, progress_arg, rl,
1439 cancel_cb, cancel_arg);
1442 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1443 err = load_tag(0, idset, id, repo,
1444 loose_obj_only, ncolored, nfound, ntrees,
1445 progress_cb, progress_arg, rl,
1446 cancel_cb, cancel_arg);
1452 for (i = 0; i < nobj; i++) {
1453 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1454 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1455 cancel_cb, cancel_arg);
1460 for (i = 0; i < nours; i++) {
1461 struct got_object_id *id = ours[i];
1462 struct got_pack_meta *m;
1465 m = got_object_idset_get(idset, id);
1467 err = got_object_get_type(&obj_type, repo, id);
1471 obj_type = m->obj_type;
1472 if (obj_type != GOT_OBJ_TYPE_TAG)
1474 err = load_tag(1, idset, id, repo, loose_obj_only,
1475 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1476 cancel_cb, cancel_arg);
1481 for (i = 0; i < nobj; i++) {
1488 const struct got_error *
1489 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1493 SHA1Update(ctx, buf, len);
1494 n = fwrite(buf, 1, len, f);
1496 return got_ferror(f, GOT_ERR_IO);
1501 putbe32(char *b, uint32_t n)
1510 write_order_cmp(const void *pa, const void *pb)
1512 struct got_pack_meta *a, *b, *ahd, *bhd;
1514 a = *(struct got_pack_meta **)pa;
1515 b = *(struct got_pack_meta **)pb;
1516 ahd = (a->head == NULL) ? a : a->head;
1517 bhd = (b->head == NULL) ? b : b->head;
1518 if (bhd->mtime < ahd->mtime)
1520 if (bhd->mtime > ahd->mtime)
1526 if (a->nchain != b->nchain)
1527 return a->nchain - b->nchain;
1528 if (a->mtime < b->mtime)
1530 if (a->mtime > b->mtime)
1532 return got_object_id_cmp(&a->id, &b->id);
1536 reuse_write_order_cmp(const void *pa, const void *pb)
1538 struct got_pack_meta *a, *b;
1540 a = *(struct got_pack_meta **)pa;
1541 b = *(struct got_pack_meta **)pb;
1543 if (a->reused_delta_offset < b->reused_delta_offset)
1545 if (a->reused_delta_offset > b->reused_delta_offset)
1550 static const struct got_error *
1551 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1557 hdr[0] = obj_type << 4;
1558 hdr[0] |= len & 0xf;
1560 for (i = 1; len != 0; i++){
1562 return got_error(GOT_ERR_NO_SPACE);
1563 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1564 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1565 len >>= GOT_DELTA_SIZE_SHIFT;
1573 packoff(char *hdr, off_t off)
1578 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1579 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1580 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1581 GOT_DELTA_SIZE_MORE;
1586 hdr[j++] = rbuf[--i];
1590 static const struct got_error *
1591 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1592 struct got_pack_meta *m)
1594 const struct got_error *err;
1598 if (m->prev->off != 0) {
1599 err = packhdr(&nh, buf, sizeof(buf),
1600 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1603 nh += packoff(buf + nh, m->off - m->prev->off);
1604 err = hwrite(packfile, buf, nh, ctx);
1607 *packfile_size += nh;
1609 err = packhdr(&nh, buf, sizeof(buf),
1610 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1613 err = hwrite(packfile, buf, nh, ctx);
1616 *packfile_size += nh;
1617 err = hwrite(packfile, m->prev->id.sha1,
1618 sizeof(m->prev->id.sha1), ctx);
1621 *packfile_size += sizeof(m->prev->id.sha1);
1627 static const struct got_error *
1628 write_packed_object(off_t *packfile_size, FILE *packfile,
1629 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1630 SHA1_CTX *ctx, struct got_repository *repo)
1632 const struct got_error *err = NULL;
1633 struct got_deflate_checksum csum;
1636 struct got_raw_object *raw = NULL;
1639 csum.output_sha1 = ctx;
1640 csum.output_crc = NULL;
1642 m->off = ftello(packfile);
1643 if (m->delta_len == 0) {
1644 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1647 err = packhdr(&nh, buf, sizeof(buf),
1648 m->obj_type, raw->size);
1651 err = hwrite(packfile, buf, nh, ctx);
1654 *packfile_size += nh;
1655 if (raw->f == NULL) {
1656 err = got_deflate_to_file_mmap(&outlen,
1657 raw->data + raw->hdrlen, 0, raw->size,
1662 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1664 err = got_error_from_errno("fseeko");
1667 err = got_deflate_to_file(&outlen, raw->f,
1668 raw->size, packfile, &csum);
1672 *packfile_size += outlen;
1673 got_object_raw_close(raw);
1675 } else if (m->delta_buf) {
1676 err = deltahdr(packfile_size, ctx, packfile, m);
1679 err = got_deflate_to_file_mmap(&outlen,
1680 m->delta_buf, 0, m->delta_len, packfile, &csum);
1683 *packfile_size += outlen;
1685 m->delta_buf = NULL;
1687 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1689 err = got_error_from_errno("fseeko");
1692 err = deltahdr(packfile_size, ctx, packfile, m);
1695 err = got_deflate_to_file(&outlen, delta_cache,
1696 m->delta_len, packfile, &csum);
1699 *packfile_size += outlen;
1703 got_object_raw_close(raw);
1707 static const struct got_error *
1708 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1709 struct got_pack_meta **deltify, int ndeltify,
1710 struct got_pack_meta **reuse, int nreuse,
1711 int ncolored, int nfound, int ntrees, int nours,
1712 struct got_repository *repo,
1713 got_pack_progress_cb progress_cb, void *progress_arg,
1714 struct got_ratelimit *rl,
1715 got_cancel_cb cancel_cb, void *cancel_arg)
1717 const struct got_error *err = NULL;
1720 struct got_pack_meta *m;
1723 off_t packfile_size = 0;
1728 err = hwrite(packfile, "PACK", 4, &ctx);
1731 putbe32(buf, GOT_PACKFILE_VERSION);
1732 err = hwrite(packfile, buf, 4, &ctx);
1735 putbe32(buf, ndeltify + nreuse);
1736 err = hwrite(packfile, buf, 4, &ctx);
1740 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1742 for (i = 0; i < ndeltify; i++) {
1743 err = report_progress(progress_cb, progress_arg, rl,
1744 ncolored, nfound, ntrees, packfile_size, nours,
1745 ndeltify + nreuse, ndeltify + nreuse, i);
1749 err = write_packed_object(&packfile_size, packfile,
1750 delta_cache, m, &outfd, &ctx, repo);
1755 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1756 reuse_write_order_cmp);
1757 for (i = 0; i < nreuse; i++) {
1758 err = report_progress(progress_cb, progress_arg, rl,
1759 ncolored, nfound, ntrees, packfile_size, nours,
1760 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1764 err = write_packed_object(&packfile_size, packfile,
1765 delta_cache, m, &outfd, &ctx, repo);
1770 SHA1Final(pack_sha1, &ctx);
1771 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1772 if (n != SHA1_DIGEST_LENGTH)
1773 err = got_ferror(packfile, GOT_ERR_IO);
1774 packfile_size += SHA1_DIGEST_LENGTH;
1775 packfile_size += sizeof(struct got_packfile_hdr);
1777 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1778 packfile_size, nours, ndeltify + nreuse,
1779 ndeltify + nreuse, ndeltify + nreuse);
1784 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1785 err = got_error_from_errno("close");
1789 static const struct got_error *
1790 remove_unused_object(struct got_object_id *id, void *data, void *arg)
1792 struct got_object_idset *idset = arg;
1795 got_object_idset_remove(NULL, idset, id);
1800 static const struct got_error *
1801 remove_reused_object(struct got_object_id *id, void *data, void *arg)
1803 struct got_object_idset *idset = arg;
1804 struct got_pack_meta *m = data;
1806 if (m->have_reused_delta)
1807 got_object_idset_remove(NULL, idset, id);
1812 static const struct got_error *
1813 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1815 struct got_pack_meta *m = data;
1816 struct got_pack_metavec *v = arg;
1818 return add_meta(m, v);
1821 const struct got_error *
1822 got_pack_create(uint8_t *packsha1, FILE *packfile,
1823 struct got_object_id **theirs, int ntheirs,
1824 struct got_object_id **ours, int nours,
1825 struct got_repository *repo, int loose_obj_only, int allow_empty,
1826 got_pack_progress_cb progress_cb, void *progress_arg,
1827 got_cancel_cb cancel_cb, void *cancel_arg)
1829 const struct got_error *err;
1830 int delta_cache_fd = -1;
1831 FILE *delta_cache = NULL;
1832 struct got_object_idset *idset;
1833 struct got_ratelimit rl;
1834 struct got_pack_metavec deltify, reuse;
1835 int ncolored = 0, nfound = 0, ntrees = 0;
1837 memset(&deltify, 0, sizeof(deltify));
1838 memset(&reuse, 0, sizeof(reuse));
1840 got_ratelimit_init(&rl, 0, 500);
1842 idset = got_object_idset_alloc();
1844 return got_error_from_errno("got_object_idset_alloc");
1846 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1847 ntheirs, ours, nours, repo, loose_obj_only,
1848 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1852 err = got_object_idset_for_each(idset, remove_unused_object, idset);
1857 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1858 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1863 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1864 err = got_error(GOT_ERR_CANNOT_PACK);
1868 delta_cache_fd = got_opentempfd();
1869 if (delta_cache_fd == -1) {
1870 err = got_error_from_errno("got_opentemp");
1875 reuse.meta = calloc(reuse.metasz,
1876 sizeof(struct got_pack_meta *));
1877 if (reuse.meta == NULL) {
1878 err = got_error_from_errno("calloc");
1882 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1883 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1884 cancel_cb, cancel_arg);
1887 if (reuse.nmeta > 0) {
1888 err = got_object_idset_for_each(idset,
1889 remove_reused_object, idset);
1894 delta_cache = fdopen(delta_cache_fd, "a+");
1895 if (delta_cache == NULL) {
1896 err = got_error_from_errno("fdopen");
1899 delta_cache_fd = -1;
1901 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1902 err = got_error_from_errno("fseeko");
1906 deltify.meta = calloc(got_object_idset_num_elements(idset),
1907 sizeof(struct got_pack_meta *));
1908 if (deltify.meta == NULL) {
1909 err = got_error_from_errno("calloc");
1912 deltify.metasz = got_object_idset_num_elements(idset);
1914 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1917 if (deltify.nmeta > 0) {
1918 err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1919 nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1920 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1923 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1924 err = got_error_from_errno("fseeko");
1929 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1930 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1931 nours, repo, progress_cb, progress_arg, &rl,
1932 cancel_cb, cancel_arg);
1936 free_nmeta(deltify.meta, deltify.nmeta);
1937 free_nmeta(reuse.meta, reuse.nmeta);
1938 got_object_idset_free(idset);
1939 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1940 err = got_error_from_errno("close");
1941 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1942 err = got_error_from_errno("fclose");