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"
55 #include "got_lib_inflate.h"
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 struct got_pack_meta {
70 struct got_object_id id;
76 /* The best delta we picked */
77 struct got_pack_meta *head;
78 struct got_pack_meta *prev;
79 unsigned char *delta_buf; /* if encoded in memory (compressed) */
80 off_t delta_offset; /* offset in delta cache file (compressed) */
81 off_t delta_len; /* encoded delta length */
82 off_t delta_compressed_len; /* encoded+compressed delta length */
85 int have_reused_delta;
86 off_t reused_delta_offset; /* offset of delta in reused pack file */
87 struct got_object_id *base_obj_id;
89 /* Only used for delta window */
90 struct got_delta_table *dtab;
92 /* Only used for writing offset deltas */
96 struct got_pack_metavec {
97 struct got_pack_meta **meta;
102 static const struct got_error *
103 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
104 const char *path, int obj_type, time_t mtime)
106 const struct got_error *err = NULL;
107 struct got_pack_meta *m;
111 m = calloc(1, sizeof(*m));
113 return got_error_from_errno("calloc");
115 memcpy(&m->id, id, sizeof(m->id));
117 m->path = strdup(path);
118 if (m->path == NULL) {
119 err = got_error_from_errno("strdup");
124 m->obj_type = obj_type;
131 clear_meta(struct got_pack_meta *meta)
137 free(meta->delta_buf);
138 meta->delta_buf = NULL;
139 free(meta->base_obj_id);
140 meta->base_obj_id = NULL;
144 free_nmeta(struct got_pack_meta **meta, int nmeta)
148 for (i = 0; i < nmeta; i++)
154 delta_order_cmp(const void *pa, const void *pb)
156 struct got_pack_meta *a, *b;
159 a = *(struct got_pack_meta **)pa;
160 b = *(struct got_pack_meta **)pb;
162 if (a->obj_type != b->obj_type)
163 return a->obj_type - b->obj_type;
164 cmp = strcmp(a->path, b->path);
167 if (a->mtime < b->mtime)
169 if (a->mtime > b->mtime)
171 return got_object_id_cmp(&a->id, &b->id);
175 delta_size(struct got_delta_instruction *deltas, int ndeltas)
179 for (i = 0; i < ndeltas; i++) {
181 size += GOT_DELTA_SIZE_SHIFT;
183 size += deltas[i].len + 1;
188 static const struct got_error *
189 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
193 if (*len + nseg >= *sz) {
194 while (*len + nseg >= *sz)
196 n = realloc(*p, *sz);
198 return got_error_from_errno("realloc");
201 memcpy(*p + *len, seg, nseg);
206 static const struct got_error *
207 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
208 struct got_delta_instruction *deltas, int ndeltas,
209 off_t delta_size, off_t base_size)
211 const struct got_error *err;
212 unsigned char buf[16], *bp;
214 size_t len = 0, compressed_len;
215 off_t bufsize = delta_size;
217 struct got_delta_instruction *d;
220 delta_buf = malloc(bufsize);
221 if (delta_buf == NULL)
222 return got_error_from_errno("malloc");
224 /* base object size */
225 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
226 n = base_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(&delta_buf, &len, &bufsize, buf, i);
236 /* target object size */
237 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
238 n = o->size >> GOT_DELTA_SIZE_SHIFT;
239 for (i = 1; n > 0; i++) {
240 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
241 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
242 n >>= GOT_DELTA_SIZE_SHIFT;
244 err = append(&delta_buf, &len, &bufsize, buf, i);
248 for (j = 0; j < ndeltas; j++) {
253 buf[0] = GOT_DELTA_BASE_COPY;
254 for (i = 0; i < 4; i++) {
255 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
264 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
265 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
266 for (i = 0; i < 3 && n > 0; i++) {
267 buf[0] |= 1 << (i + 4);
272 err = append(&delta_buf, &len, &bufsize,
276 } else if (o->f == NULL) {
278 while (n != d->len) {
279 buf[0] = (d->len - n < 127) ? d->len - n : 127;
280 err = append(&delta_buf, &len, &bufsize,
284 err = append(&delta_buf, &len, &bufsize,
285 o->data + o->hdrlen + d->offset + n,
294 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
295 err = got_error_from_errno("fseeko");
299 while (n != d->len) {
300 buf[0] = (d->len - n < 127) ? d->len - n : 127;
301 err = append(&delta_buf, &len, &bufsize,
305 r = fread(content, 1, buf[0], o->f);
307 err = got_ferror(o->f, GOT_ERR_IO);
310 err = append(&delta_buf, &len, &bufsize,
319 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
320 NULL, NULL, delta_buf, 0, len);
325 m->delta_compressed_len = compressed_len;
331 static const struct got_error *
332 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
333 struct got_delta_instruction *deltas, int ndeltas,
334 off_t base_size, FILE *f)
336 const struct got_error *err;
337 unsigned char buf[16], *bp;
340 struct got_deflate_buf zb;
341 struct got_delta_instruction *d;
342 off_t delta_len = 0, compressed_len = 0;
344 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
348 /* base object size */
349 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
350 n = base_size >> GOT_DELTA_SIZE_SHIFT;
351 for (i = 1; n > 0; i++) {
352 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
353 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
354 n >>= GOT_DELTA_SIZE_SHIFT;
357 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
363 /* target object size */
364 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
365 n = o->size >> GOT_DELTA_SIZE_SHIFT;
366 for (i = 1; n > 0; i++) {
367 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
368 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
369 n >>= GOT_DELTA_SIZE_SHIFT;
372 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
378 for (j = 0; j < ndeltas; j++) {
383 buf[0] = GOT_DELTA_BASE_COPY;
384 for (i = 0; i < 4; i++) {
385 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
393 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
394 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
395 for (i = 0; i < 3 && n > 0; i++) {
396 buf[0] |= 1 << (i + 4);
401 err = got_deflate_append_to_file_mmap(&zb,
402 &compressed_len, buf, 0, bp - buf, f, NULL);
405 delta_len += (bp - buf);
406 } else if (o->f == NULL) {
408 while (n != d->len) {
409 buf[0] = (d->len - n < 127) ? d->len - n : 127;
410 err = got_deflate_append_to_file_mmap(&zb,
411 &compressed_len, buf, 0, 1, f, NULL);
415 err = got_deflate_append_to_file_mmap(&zb,
417 o->data + o->hdrlen + d->offset + n, 0,
427 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
428 err = got_error_from_errno("fseeko");
432 while (n != d->len) {
433 buf[0] = (d->len - n < 127) ? d->len - n : 127;
434 err = got_deflate_append_to_file_mmap(&zb,
435 &compressed_len, buf, 0, 1, f, NULL);
439 r = fread(content, 1, buf[0], o->f);
441 err = got_ferror(o->f, GOT_ERR_IO);
444 err = got_deflate_append_to_file_mmap(&zb,
445 &compressed_len, content, 0, buf[0], f,
455 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
460 if (compressed_len != ftello(f) - m->delta_offset) {
461 err = got_error(GOT_ERR_COMPRESSION);
465 m->delta_len = delta_len;
466 m->delta_compressed_len = compressed_len;
468 got_deflate_end(&zb);
472 static const struct got_error *
473 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
474 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
475 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
478 const struct got_error *err;
481 if (progress_cb == NULL)
484 err = got_ratelimit_check(&elapsed, rl);
488 return progress_cb(progress_arg, ncolored, nfound, ntrees,
489 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
492 static const struct got_error *
493 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
495 if (v->nmeta == v->metasz){
496 size_t newsize = 2 * v->metasz;
497 struct got_pack_meta **new;
498 new = reallocarray(v->meta, newsize, sizeof(*new));
500 return got_error_from_errno("reallocarray");
505 v->meta[v->nmeta++] = m;
509 static const struct got_error *
510 find_pack_for_reuse(struct got_packidx **best_packidx,
511 struct got_repository *repo)
513 const struct got_error *err = NULL;
514 struct got_pathlist_entry *pe;
515 const char *best_packidx_path = NULL;
518 *best_packidx = NULL;
520 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
521 const char *path_packidx = pe->path;
522 struct got_packidx *packidx;
525 err = got_repo_get_packidx(&packidx, path_packidx, repo);
529 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
530 if (nobj > nobj_max) {
531 best_packidx_path = path_packidx;
536 if (best_packidx_path) {
537 err = got_repo_get_packidx(best_packidx, best_packidx_path,
545 struct imsgbuf *ibuf;
546 struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
550 static const struct got_error *
551 send_id(struct got_object_id *id, void *data, void *arg)
553 const struct got_error *err = NULL;
554 struct send_id_arg *a = arg;
556 a->ids[a->nids++] = id;
558 if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
559 err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
568 static const struct got_error *
569 recv_reused_delta(struct got_imsg_reused_delta *delta,
570 struct got_object_idset *idset, struct got_pack_metavec *v)
572 struct got_pack_meta *m, *base;
574 if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
575 delta->delta_offset +
576 delta->delta_compressed_size < delta->delta_offset)
577 return got_error(GOT_ERR_BAD_PACKFILE);
579 m = got_object_idset_get(idset, &delta->id);
581 return got_error(GOT_ERR_NO_OBJ);
583 base = got_object_idset_get(idset, &delta->base_id);
585 return got_error(GOT_ERR_NO_OBJ);
587 m->delta_len = delta->delta_size;
588 m->delta_compressed_len = delta->delta_compressed_size;
589 m->delta_offset = delta->delta_out_offset;
591 m->size = delta->result_size;
592 m->have_reused_delta = 1;
593 m->reused_delta_offset = delta->delta_offset;
594 m->base_obj_id = got_object_id_dup(&delta->base_id);
595 if (m->base_obj_id == NULL)
596 return got_error_from_errno("got_object_id_dup");
598 return add_meta(m, v);
601 static const struct got_error *
602 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
603 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
604 struct got_repository *repo,
605 got_pack_progress_cb progress_cb, void *progress_arg,
606 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
608 const struct got_error *err = NULL;
609 struct got_packidx *packidx;
610 struct got_pack *pack;
611 struct send_id_arg sia;
612 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
615 err = find_pack_for_reuse(&packidx, repo);
622 err = got_object_prepare_delta_reuse(&pack, packidx,
623 delta_cache_fd, repo);
627 memset(&sia, 0, sizeof(sia));
628 sia.ibuf = pack->privsep_child->ibuf;
629 err = got_object_idset_for_each(idset, send_id, &sia);
633 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
638 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
646 err = (*cancel_cb)(cancel_arg);
651 err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
652 pack->privsep_child->ibuf);
656 for (i = 0; i < ndeltas; i++) {
657 struct got_imsg_reused_delta *delta = &deltas[i];
658 err = recv_reused_delta(delta, idset, v);
663 err = report_progress(progress_cb, progress_arg, rl,
664 ncolored, nfound, ntrees, 0L, ncommits,
665 got_object_idset_num_elements(idset), v->nmeta, 0);
673 static const struct got_error *
674 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
675 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
676 struct got_repository *repo,
677 got_pack_progress_cb progress_cb, void *progress_arg,
678 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
680 const struct got_error *err = NULL;
681 struct got_pack_meta *m = NULL, *base = NULL;
682 struct got_raw_object *raw = NULL, *base_raw = NULL;
683 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
684 int i, j, ndeltas, best_ndeltas;
685 off_t size, best_size;
686 const int max_base_candidates = 3;
687 size_t delta_memsize = 0;
688 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
691 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
692 for (i = 0; i < nmeta; i++) {
694 err = (*cancel_cb)(cancel_arg);
698 err = report_progress(progress_cb, progress_arg, rl,
699 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
705 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
706 m->obj_type == GOT_OBJ_TYPE_TAG)
709 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
714 if (raw->f == NULL) {
715 err = got_deltify_init_mem(&m->dtab, raw->data,
716 raw->hdrlen, raw->size + raw->hdrlen);
718 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
719 raw->size + raw->hdrlen);
724 if (i > max_base_candidates) {
725 struct got_pack_meta *n = NULL;
726 n = meta[i - (max_base_candidates + 1)];
727 got_deltify_free(n->dtab);
731 best_size = raw->size;
733 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
735 err = (*cancel_cb)(cancel_arg);
740 /* long chains make unpacking slow, avoid such bases */
741 if (base->nchain >= 128 ||
742 base->obj_type != m->obj_type)
745 err = got_object_raw_open(&base_raw, &outfd, repo,
750 if (raw->f == NULL && base_raw->f == NULL) {
751 err = got_deltify_mem_mem(&deltas, &ndeltas,
752 raw->data, raw->hdrlen,
753 raw->size + raw->hdrlen,
754 base->dtab, base_raw->data,
756 base_raw->size + base_raw->hdrlen);
757 } else if (raw->f == NULL) {
758 err = got_deltify_mem_file(&deltas, &ndeltas,
759 raw->data, raw->hdrlen,
760 raw->size + raw->hdrlen,
761 base->dtab, base_raw->f,
763 base_raw->size + base_raw->hdrlen);
764 } else if (base_raw->f == NULL) {
765 err = got_deltify_file_mem(&deltas, &ndeltas,
767 raw->size + raw->hdrlen,
768 base->dtab, base_raw->data,
770 base_raw->size + base_raw->hdrlen);
772 err = got_deltify(&deltas, &ndeltas,
774 raw->size + raw->hdrlen,
775 base->dtab, base_raw->f, base_raw->hdrlen,
776 base_raw->size + base_raw->hdrlen);
778 got_object_raw_close(base_raw);
783 size = delta_size(deltas, ndeltas);
784 if (size + 32 < best_size){
786 * if we already picked a best delta,
791 best_deltas = deltas;
792 best_ndeltas = ndeltas;
794 m->nchain = base->nchain + 1;
796 m->head = base->head;
806 if (best_ndeltas > 0) {
807 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
808 delta_memsize + best_size <= max_delta_memsize) {
809 delta_memsize += best_size;
810 err = encode_delta_in_mem(m, raw, best_deltas,
811 best_ndeltas, best_size, m->prev->size);
813 m->delta_offset = ftello(delta_cache);
814 err = encode_delta(m, raw, best_deltas,
815 best_ndeltas, m->prev->size, delta_cache);
824 got_object_raw_close(raw);
828 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
829 got_deltify_free(meta[i]->dtab);
830 meta[i]->dtab = NULL;
833 got_object_raw_close(raw);
835 got_object_raw_close(base_raw);
836 if (outfd != -1 && close(outfd) == -1 && err == NULL)
837 err = got_error_from_errno("close");
843 static const struct got_error *
844 search_packidx(int *found, struct got_object_id *id,
845 struct got_repository *repo)
847 const struct got_error *err = NULL;
848 struct got_packidx *packidx = NULL;
853 err = got_repo_search_packidx(&packidx, &idx, repo, id);
855 *found = 1; /* object is already packed */
856 else if (err->code == GOT_ERR_NO_OBJ)
861 static const struct got_error *
862 add_object(int want_meta, struct got_object_idset *idset,
863 struct got_object_id *id, const char *path, int obj_type,
864 time_t mtime, int loose_obj_only, struct got_repository *repo,
865 int *ncolored, int *nfound, int *ntrees,
866 got_pack_progress_cb progress_cb, void *progress_arg,
867 struct got_ratelimit *rl)
869 const struct got_error *err;
870 struct got_pack_meta *m = NULL;
872 if (loose_obj_only) {
874 err = search_packidx(&is_packed, id, repo);
877 if (is_packed && want_meta)
882 err = alloc_meta(&m, id, path, obj_type, mtime);
887 err = report_progress(progress_cb, progress_arg, rl,
888 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
896 err = got_object_idset_add(idset, id, m);
904 static const struct got_error *
905 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
906 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
907 struct got_object_id *tree_id,
908 const char *dpath, time_t mtime, struct got_repository *repo,
909 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
910 got_pack_progress_cb progress_cb, void *progress_arg,
911 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
913 const struct got_error *err;
914 struct got_tree_object *tree;
918 err = got_object_open_as_tree(&tree, repo, tree_id);
923 err = report_progress(progress_cb, progress_arg, rl,
924 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
928 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
929 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
930 struct got_object_id *id = got_tree_entry_get_id(e);
931 mode_t mode = got_tree_entry_get_mode(e);
934 err = (*cancel_cb)(cancel_arg);
939 if (got_object_tree_entry_is_submodule(e) ||
940 got_object_idset_contains(idset, id) ||
941 got_object_idset_contains(idset_exclude, id))
944 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
945 got_tree_entry_get_name(e)) == -1) {
946 err = got_error_from_errno("asprintf");
951 struct got_object_qid *qid;
952 err = got_object_qid_alloc(&qid, id);
955 STAILQ_INSERT_TAIL(ids, qid, entry);
956 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
957 err = add_object(want_meta,
958 want_meta ? idset : idset_exclude, id, p,
959 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
960 ncolored, nfound, ntrees,
961 progress_cb, progress_arg, rl);
969 got_object_tree_close(tree);
974 static const struct got_error *
975 load_tree(int want_meta, struct got_object_idset *idset,
976 struct got_object_idset *idset_exclude,
977 struct got_object_id *tree_id, const char *dpath, time_t mtime,
978 struct got_repository *repo, int loose_obj_only,
979 int *ncolored, int *nfound, int *ntrees,
980 got_pack_progress_cb progress_cb, void *progress_arg,
981 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
983 const struct got_error *err = NULL;
984 struct got_object_id_queue tree_ids;
985 struct got_object_qid *qid;
987 if (got_object_idset_contains(idset, tree_id) ||
988 got_object_idset_contains(idset_exclude, tree_id))
991 err = got_object_qid_alloc(&qid, tree_id);
995 STAILQ_INIT(&tree_ids);
996 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
998 while (!STAILQ_EMPTY(&tree_ids)) {
1000 err = (*cancel_cb)(cancel_arg);
1005 qid = STAILQ_FIRST(&tree_ids);
1006 STAILQ_REMOVE_HEAD(&tree_ids, entry);
1008 if (got_object_idset_contains(idset, &qid->id) ||
1009 got_object_idset_contains(idset_exclude, &qid->id)) {
1010 got_object_qid_free(qid);
1014 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1015 &qid->id, dpath, GOT_OBJ_TYPE_TREE,
1016 mtime, loose_obj_only, repo,
1017 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1019 got_object_qid_free(qid);
1023 err = load_tree_entries(&tree_ids, want_meta, idset,
1024 idset_exclude, &qid->id,
1025 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1026 ntrees, progress_cb, progress_arg, rl,
1027 cancel_cb, cancel_arg);
1028 got_object_qid_free(qid);
1033 got_object_id_queue_free(&tree_ids);
1037 static const struct got_error *
1038 load_commit(int want_meta, struct got_object_idset *idset,
1039 struct got_object_idset *idset_exclude,
1040 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1041 int *ncolored, int *nfound, int *ntrees,
1042 got_pack_progress_cb progress_cb, void *progress_arg,
1043 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1045 const struct got_error *err;
1046 struct got_commit_object *commit;
1048 if (got_object_idset_contains(idset, id) ||
1049 got_object_idset_contains(idset_exclude, id))
1052 if (loose_obj_only) {
1054 err = search_packidx(&is_packed, id, repo);
1057 if (is_packed && want_meta)
1061 err = got_object_open_as_commit(&commit, repo, id);
1065 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1066 id, "", GOT_OBJ_TYPE_COMMIT,
1067 got_object_commit_get_committer_time(commit),
1068 loose_obj_only, repo,
1069 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1073 err = load_tree(want_meta, idset, idset_exclude,
1074 got_object_commit_get_tree_id(commit),
1075 "", got_object_commit_get_committer_time(commit),
1076 repo, loose_obj_only, ncolored, nfound, ntrees,
1077 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1079 got_object_commit_close(commit);
1083 static const struct got_error *
1084 load_tag(int want_meta, struct got_object_idset *idset,
1085 struct got_object_idset *idset_exclude,
1086 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1087 int *ncolored, int *nfound, int *ntrees,
1088 got_pack_progress_cb progress_cb, void *progress_arg,
1089 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1091 const struct got_error *err;
1092 struct got_tag_object *tag = NULL;
1094 if (got_object_idset_contains(idset, id) ||
1095 got_object_idset_contains(idset_exclude, id))
1098 if (loose_obj_only) {
1100 err = search_packidx(&is_packed, id, repo);
1103 if (is_packed && want_meta)
1107 err = got_object_open_as_tag(&tag, repo, id);
1111 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1112 id, "", GOT_OBJ_TYPE_TAG,
1113 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1114 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1118 switch (got_object_tag_get_object_type(tag)) {
1119 case GOT_OBJ_TYPE_COMMIT:
1120 err = load_commit(want_meta, idset, idset_exclude,
1121 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1122 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1123 cancel_cb, cancel_arg);
1125 case GOT_OBJ_TYPE_TREE:
1126 err = load_tree(want_meta, idset, idset_exclude,
1127 got_object_tag_get_object_id(tag), "",
1128 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1129 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1130 cancel_cb, cancel_arg);
1137 got_object_tag_close(tag);
1141 enum findtwixt_color {
1148 static const int findtwixt_colors[] = {
1155 static const struct got_error *
1156 paint_commit(struct got_object_qid *qid, int color)
1158 if (color < 0 || color >= nitems(findtwixt_colors))
1159 return got_error(GOT_ERR_RANGE);
1161 qid->data = (void *)&findtwixt_colors[color];
1165 static const struct got_error *
1166 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1167 int color, struct got_repository *repo)
1169 const struct got_error *err;
1170 struct got_object_qid *qid;
1172 err = got_object_qid_alloc(&qid, id);
1176 STAILQ_INSERT_TAIL(ids, qid, entry);
1177 return paint_commit(qid, color);
1180 struct append_id_arg {
1181 struct got_object_id **array;
1183 struct got_object_idset *drop;
1184 struct got_object_idset *skip;
1187 static const struct got_error *
1188 append_id(struct got_object_id *id, void *data, void *arg)
1190 struct append_id_arg *a = arg;
1192 if (got_object_idset_contains(a->skip, id) ||
1193 got_object_idset_contains(a->drop, id))
1196 a->array[++a->idx] = got_object_id_dup(id);
1197 if (a->array[a->idx] == NULL)
1198 return got_error_from_errno("got_object_id_dup");
1203 static const struct got_error *
1204 queue_commit_or_tag_id(struct got_object_id *id, int color,
1205 struct got_object_id_queue *ids, struct got_repository *repo)
1207 const struct got_error *err;
1208 struct got_tag_object *tag = NULL;
1211 err = got_object_get_type(&obj_type, repo, id);
1215 if (obj_type == GOT_OBJ_TYPE_TAG) {
1216 err = got_object_open_as_tag(&tag, repo, id);
1219 obj_type = got_object_tag_get_object_type(tag);
1220 id = got_object_tag_get_object_id(tag);
1223 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1224 err = queue_commit_id(ids, id, color, repo);
1230 got_object_tag_close(tag);
1234 static const struct got_error *
1235 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1236 struct got_object_idset *keep, struct got_object_idset *drop,
1237 struct got_object_idset *skip, struct got_repository *repo,
1238 got_pack_progress_cb progress_cb, void *progress_arg,
1239 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1241 const struct got_error *err = NULL;
1242 struct got_commit_object *commit = NULL;
1243 const struct got_object_id_queue *parents;
1244 struct got_object_qid *qid;
1245 int nqueued = nids, nskip = 0;
1247 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1251 err = cancel_cb(cancel_arg);
1256 qid = STAILQ_FIRST(ids);
1257 STAILQ_REMOVE_HEAD(ids, entry);
1259 color = *((int *)qid->data);
1260 if (color == COLOR_SKIP)
1263 if (got_object_idset_contains(skip, &qid->id)) {
1264 got_object_qid_free(qid);
1270 if (got_object_idset_contains(keep, &qid->id)) {
1271 got_object_qid_free(qid);
1274 if (got_object_idset_contains(drop, &qid->id)) {
1275 err = paint_commit(qid, COLOR_SKIP);
1281 err = got_object_idset_add(keep, &qid->id, NULL);
1286 if (got_object_idset_contains(drop, &qid->id)) {
1287 got_object_qid_free(qid);
1290 if (got_object_idset_contains(keep, &qid->id)) {
1291 err = paint_commit(qid, COLOR_SKIP);
1297 err = got_object_idset_add(drop, &qid->id, NULL);
1302 if (!got_object_idset_contains(skip, &qid->id)) {
1303 err = got_object_idset_add(skip, &qid->id,
1310 /* should not happen */
1311 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1312 "%s invalid commit color %d", __func__, color);
1316 err = report_progress(progress_cb, progress_arg, rl,
1317 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1322 err = got_object_open_as_commit(&commit, repo, &qid->id);
1326 parents = got_object_commit_get_parent_ids(commit);
1328 struct got_object_qid *pid;
1329 color = *((int *)qid->data);
1330 STAILQ_FOREACH(pid, parents, entry) {
1331 err = queue_commit_id(ids, &pid->id, color,
1336 if (color == COLOR_SKIP)
1341 got_object_commit_close(commit);
1343 got_object_qid_free(qid);
1347 got_object_commit_close(commit);
1351 static const struct got_error *
1352 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1353 struct got_object_id **head, int nhead,
1354 struct got_object_id **tail, int ntail,
1355 struct got_repository *repo,
1356 got_pack_progress_cb progress_cb, void *progress_arg,
1357 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1359 const struct got_error *err = NULL;
1360 struct got_object_id_queue ids;
1361 struct got_object_idset *keep, *drop, *skip = NULL;
1369 keep = got_object_idset_alloc();
1371 return got_error_from_errno("got_object_idset_alloc");
1373 drop = got_object_idset_alloc();
1375 err = got_error_from_errno("got_object_idset_alloc");
1379 skip = got_object_idset_alloc();
1381 err = got_error_from_errno("got_object_idset_alloc");
1385 for (i = 0; i < nhead; i++) {
1386 struct got_object_id *id = head[i];
1389 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1394 for (i = 0; i < ntail; i++) {
1395 struct got_object_id *id = tail[i];
1398 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1403 err = paint_commits(ncolored, &ids, nhead + ntail,
1404 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1405 cancel_cb, cancel_arg);
1409 nkeep = got_object_idset_num_elements(keep);
1411 struct append_id_arg arg;
1412 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1413 if (arg.array == NULL) {
1414 err = got_error_from_errno("calloc");
1420 err = got_object_idset_for_each(keep, append_id, &arg);
1426 *nres = arg.idx + 1;
1429 got_object_idset_free(keep);
1430 got_object_idset_free(drop);
1432 got_object_idset_free(skip);
1433 got_object_id_queue_free(&ids);
1437 static const struct got_error *
1438 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1439 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1440 struct got_object_id **ours, int nours, struct got_repository *repo,
1441 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1442 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1444 const struct got_error *err = NULL;
1445 struct got_object_id **ids = NULL;
1446 int i, nobj = 0, obj_type;
1447 struct got_object_idset *idset_exclude;
1449 idset_exclude = got_object_idset_alloc();
1450 if (idset_exclude == NULL)
1451 return got_error_from_errno("got_object_idset_alloc");
1457 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1458 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1459 if (err || nobj == 0)
1462 for (i = 0; i < ntheirs; i++) {
1463 struct got_object_id *id = theirs[i];
1466 err = got_object_get_type(&obj_type, repo, id);
1469 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1470 err = load_commit(0, idset, idset_exclude, id, repo,
1471 loose_obj_only, ncolored, nfound, ntrees,
1472 progress_cb, progress_arg, rl,
1473 cancel_cb, cancel_arg);
1476 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1477 err = load_tag(0, idset, idset_exclude, id, repo,
1478 loose_obj_only, ncolored, nfound, ntrees,
1479 progress_cb, progress_arg, rl,
1480 cancel_cb, cancel_arg);
1486 for (i = 0; i < nobj; i++) {
1487 err = load_commit(1, idset, idset_exclude,
1488 ids[i], repo, loose_obj_only, ncolored, nfound, ntrees,
1489 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1494 for (i = 0; i < nours; i++) {
1495 struct got_object_id *id = ours[i];
1496 struct got_pack_meta *m;
1499 m = got_object_idset_get(idset, id);
1501 err = got_object_get_type(&obj_type, repo, id);
1505 obj_type = m->obj_type;
1506 if (obj_type != GOT_OBJ_TYPE_TAG)
1508 err = load_tag(1, idset, idset_exclude, id, repo,
1509 loose_obj_only, ncolored, nfound, ntrees,
1510 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1515 for (i = 0; i < nobj; i++) {
1519 got_object_idset_free(idset_exclude);
1523 const struct got_error *
1524 hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
1528 SHA1Update(ctx, buf, len);
1529 n = fwrite(buf, 1, len, f);
1531 return got_ferror(f, GOT_ERR_IO);
1535 const struct got_error *
1536 hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1538 unsigned char buf[65536];
1542 while (remain > 0) {
1543 size_t copylen = MIN(sizeof(buf), remain);
1544 n = fread(buf, 1, copylen, fsrc);
1546 return got_ferror(fsrc, GOT_ERR_IO);
1547 SHA1Update(ctx, buf, copylen);
1548 n = fwrite(buf, 1, copylen, fdst);
1550 return got_ferror(fdst, GOT_ERR_IO);
1558 putbe32(char *b, uint32_t n)
1567 write_order_cmp(const void *pa, const void *pb)
1569 struct got_pack_meta *a, *b, *ahd, *bhd;
1571 a = *(struct got_pack_meta **)pa;
1572 b = *(struct got_pack_meta **)pb;
1573 ahd = (a->head == NULL) ? a : a->head;
1574 bhd = (b->head == NULL) ? b : b->head;
1575 if (bhd->mtime < ahd->mtime)
1577 if (bhd->mtime > ahd->mtime)
1583 if (a->nchain != b->nchain)
1584 return a->nchain - b->nchain;
1585 if (a->mtime < b->mtime)
1587 if (a->mtime > b->mtime)
1589 return got_object_id_cmp(&a->id, &b->id);
1593 reuse_write_order_cmp(const void *pa, const void *pb)
1595 struct got_pack_meta *a, *b;
1597 a = *(struct got_pack_meta **)pa;
1598 b = *(struct got_pack_meta **)pb;
1600 if (a->reused_delta_offset < b->reused_delta_offset)
1602 if (a->reused_delta_offset > b->reused_delta_offset)
1607 static const struct got_error *
1608 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1614 hdr[0] = obj_type << 4;
1615 hdr[0] |= len & 0xf;
1617 for (i = 1; len != 0; i++){
1619 return got_error(GOT_ERR_NO_SPACE);
1620 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1621 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1622 len >>= GOT_DELTA_SIZE_SHIFT;
1630 packoff(char *hdr, off_t off)
1635 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1636 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1637 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1638 GOT_DELTA_SIZE_MORE;
1643 hdr[j++] = rbuf[--i];
1647 static const struct got_error *
1648 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1649 struct got_pack_meta *m)
1651 const struct got_error *err;
1655 if (m->prev->off != 0) {
1656 err = packhdr(&nh, buf, sizeof(buf),
1657 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1660 nh += packoff(buf + nh, m->off - m->prev->off);
1661 err = hwrite(packfile, buf, nh, ctx);
1664 *packfile_size += nh;
1666 err = packhdr(&nh, buf, sizeof(buf),
1667 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1670 err = hwrite(packfile, buf, nh, ctx);
1673 *packfile_size += nh;
1674 err = hwrite(packfile, m->prev->id.sha1,
1675 sizeof(m->prev->id.sha1), ctx);
1678 *packfile_size += sizeof(m->prev->id.sha1);
1684 static const struct got_error *
1685 write_packed_object(off_t *packfile_size, FILE *packfile,
1686 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1687 SHA1_CTX *ctx, struct got_repository *repo)
1689 const struct got_error *err = NULL;
1690 struct got_deflate_checksum csum;
1693 struct got_raw_object *raw = NULL;
1696 csum.output_sha1 = ctx;
1697 csum.output_crc = NULL;
1699 m->off = ftello(packfile);
1700 if (m->delta_len == 0) {
1701 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1704 err = packhdr(&nh, buf, sizeof(buf),
1705 m->obj_type, raw->size);
1708 err = hwrite(packfile, buf, nh, ctx);
1711 *packfile_size += nh;
1712 if (raw->f == NULL) {
1713 err = got_deflate_to_file_mmap(&outlen,
1714 raw->data + raw->hdrlen, 0, raw->size,
1719 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1721 err = got_error_from_errno("fseeko");
1724 err = got_deflate_to_file(&outlen, raw->f,
1725 raw->size, packfile, &csum);
1729 *packfile_size += outlen;
1730 got_object_raw_close(raw);
1732 } else if (m->delta_buf) {
1733 err = deltahdr(packfile_size, ctx, packfile, m);
1736 err = hwrite(packfile, m->delta_buf,
1737 m->delta_compressed_len, ctx);
1740 *packfile_size += m->delta_compressed_len;
1742 m->delta_buf = NULL;
1744 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1746 err = got_error_from_errno("fseeko");
1749 err = deltahdr(packfile_size, ctx, packfile, m);
1752 err = hcopy(delta_cache, packfile,
1753 m->delta_compressed_len, ctx);
1756 *packfile_size += m->delta_compressed_len;
1760 got_object_raw_close(raw);
1764 static const struct got_error *
1765 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1766 struct got_pack_meta **deltify, int ndeltify,
1767 struct got_pack_meta **reuse, int nreuse,
1768 int ncolored, int nfound, int ntrees, int nours,
1769 struct got_repository *repo,
1770 got_pack_progress_cb progress_cb, void *progress_arg,
1771 struct got_ratelimit *rl,
1772 got_cancel_cb cancel_cb, void *cancel_arg)
1774 const struct got_error *err = NULL;
1777 struct got_pack_meta *m;
1780 off_t packfile_size = 0;
1785 err = hwrite(packfile, "PACK", 4, &ctx);
1788 putbe32(buf, GOT_PACKFILE_VERSION);
1789 err = hwrite(packfile, buf, 4, &ctx);
1792 putbe32(buf, ndeltify + nreuse);
1793 err = hwrite(packfile, buf, 4, &ctx);
1797 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1799 for (i = 0; i < ndeltify; i++) {
1800 err = report_progress(progress_cb, progress_arg, rl,
1801 ncolored, nfound, ntrees, packfile_size, nours,
1802 ndeltify + nreuse, ndeltify + nreuse, i);
1806 err = write_packed_object(&packfile_size, packfile,
1807 delta_cache, m, &outfd, &ctx, repo);
1812 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1813 reuse_write_order_cmp);
1814 for (i = 0; i < nreuse; i++) {
1815 err = report_progress(progress_cb, progress_arg, rl,
1816 ncolored, nfound, ntrees, packfile_size, nours,
1817 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1821 err = write_packed_object(&packfile_size, packfile,
1822 delta_cache, m, &outfd, &ctx, repo);
1827 SHA1Final(pack_sha1, &ctx);
1828 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1829 if (n != SHA1_DIGEST_LENGTH)
1830 err = got_ferror(packfile, GOT_ERR_IO);
1831 packfile_size += SHA1_DIGEST_LENGTH;
1832 packfile_size += sizeof(struct got_packfile_hdr);
1834 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1835 packfile_size, nours, ndeltify + nreuse,
1836 ndeltify + nreuse, ndeltify + nreuse);
1841 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1842 err = got_error_from_errno("close");
1846 static const struct got_error *
1847 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1849 struct got_pack_meta *m = data;
1850 struct got_pack_metavec *v = arg;
1852 if (m->have_reused_delta)
1855 return add_meta(m, v);
1858 const struct got_error *
1859 got_pack_create(uint8_t *packsha1, FILE *packfile,
1860 struct got_object_id **theirs, int ntheirs,
1861 struct got_object_id **ours, int nours,
1862 struct got_repository *repo, int loose_obj_only, int allow_empty,
1863 got_pack_progress_cb progress_cb, void *progress_arg,
1864 got_cancel_cb cancel_cb, void *cancel_arg)
1866 const struct got_error *err;
1867 int delta_cache_fd = -1;
1868 FILE *delta_cache = NULL;
1869 struct got_object_idset *idset;
1870 struct got_ratelimit rl;
1871 struct got_pack_metavec deltify, reuse;
1872 int ncolored = 0, nfound = 0, ntrees = 0;
1875 memset(&deltify, 0, sizeof(deltify));
1876 memset(&reuse, 0, sizeof(reuse));
1878 got_ratelimit_init(&rl, 0, 500);
1880 idset = got_object_idset_alloc();
1882 return got_error_from_errno("got_object_idset_alloc");
1884 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1885 ntheirs, ours, nours, repo, loose_obj_only,
1886 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1891 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1892 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1897 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1898 err = got_error(GOT_ERR_CANNOT_PACK);
1902 delta_cache_fd = got_opentempfd();
1903 if (delta_cache_fd == -1) {
1904 err = got_error_from_errno("got_opentemp");
1909 reuse.meta = calloc(reuse.metasz,
1910 sizeof(struct got_pack_meta *));
1911 if (reuse.meta == NULL) {
1912 err = got_error_from_errno("calloc");
1916 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1917 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1918 cancel_cb, cancel_arg);
1922 delta_cache = fdopen(delta_cache_fd, "a+");
1923 if (delta_cache == NULL) {
1924 err = got_error_from_errno("fdopen");
1927 delta_cache_fd = -1;
1929 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1930 err = got_error_from_errno("fseeko");
1934 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1936 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1937 if (deltify.meta == NULL) {
1938 err = got_error_from_errno("calloc");
1941 deltify.metasz = ndeltify;
1943 err = got_object_idset_for_each(idset, add_meta_idset_cb,
1947 if (deltify.nmeta > 0) {
1948 err = pick_deltas(deltify.meta, deltify.nmeta,
1949 ncolored, nfound, ntrees, nours, reuse.nmeta,
1950 delta_cache, repo, progress_cb, progress_arg, &rl,
1951 cancel_cb, cancel_arg);
1957 if (fflush(delta_cache) == EOF) {
1958 err = got_error_from_errno("fflush");
1961 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1962 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1963 nours, repo, progress_cb, progress_arg, &rl,
1964 cancel_cb, cancel_arg);
1968 free_nmeta(deltify.meta, deltify.nmeta);
1969 free_nmeta(reuse.meta, reuse.nmeta);
1970 got_object_idset_free(idset);
1971 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1972 err = got_error_from_errno("close");
1973 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1974 err = got_error_from_errno("fclose");