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 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
511 struct got_object_idset *idset, struct got_pack *pack,
512 struct got_packidx *packidx, int delta_cache_fd,
513 struct got_repository *repo)
515 const struct got_error *err = NULL;
516 struct got_pack_meta *base = NULL;
517 struct got_object_id *base_obj_id = NULL;
518 off_t delta_len = 0, delta_compressed_len = 0;
519 off_t delta_offset = 0, delta_cache_offset = 0;
520 uint64_t base_size, result_size;
522 if (m->have_reused_delta)
525 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
526 &delta_compressed_len, &delta_offset, &delta_cache_offset,
527 &base_obj_id, delta_cache_fd, packidx, idx, &m->id, repo);
531 if (delta_offset + delta_len < delta_offset)
532 return got_error(GOT_ERR_BAD_PACKFILE);
534 base = got_object_idset_get(idset, base_obj_id);
538 m->delta_len = delta_len;
539 m->delta_compressed_len = delta_compressed_len;
540 m->delta_offset = delta_cache_offset;
542 m->size = result_size;
543 m->have_reused_delta = 1;
544 m->reused_delta_offset = delta_offset;
545 m->base_obj_id = base_obj_id;
547 err = add_meta(m, v);
553 static const struct got_error *
554 find_pack_for_reuse(struct got_packidx **best_packidx,
555 struct got_repository *repo)
557 const struct got_error *err = NULL;
558 struct got_pathlist_entry *pe;
559 const char *best_packidx_path = NULL;
562 *best_packidx = NULL;
564 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
565 const char *path_packidx = pe->path;
566 struct got_packidx *packidx;
569 err = got_repo_get_packidx(&packidx, path_packidx, repo);
573 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
574 if (nobj > nobj_max) {
575 best_packidx_path = path_packidx;
580 if (best_packidx_path) {
581 err = got_repo_get_packidx(best_packidx, best_packidx_path,
588 struct search_deltas_arg {
589 struct got_packidx *packidx;
590 struct got_pack *pack;
591 struct got_object_idset *idset;
592 struct got_pack_metavec *v;
594 struct got_repository *repo;
595 got_pack_progress_cb progress_cb;
597 struct got_ratelimit *rl;
598 got_cancel_cb cancel_cb;
606 static const struct got_error *
607 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
609 const struct got_error *err;
610 struct got_pack_meta *m = data;
611 struct search_deltas_arg *a = arg;
613 struct got_object *obj = NULL;
616 err = (*a->cancel_cb)(a->cancel_arg);
621 if (!got_repo_check_packidx_bloom_filter(a->repo,
622 a->packidx->path_packidx, id))
625 obj_idx = got_packidx_get_object_idx(a->packidx, id);
630 * Opening and closing an object just to check its flags
631 * is a bit expensive. We could have an imsg which requests
632 * plain type/size information for an object without doing
633 * work such as traversing the object's entire delta chain
634 * to find the base object type, and other such info which
635 * we don't really need here.
637 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
638 a->packidx, obj_idx, a->repo);
642 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
643 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
644 a->delta_cache_fd, a->repo);
647 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
648 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
649 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
652 got_object_close(obj);
656 static const struct got_error *
657 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
658 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
659 struct got_repository *repo,
660 got_pack_progress_cb progress_cb, void *progress_arg,
661 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
663 const struct got_error *err = NULL;
664 char *path_packfile = NULL;
665 struct got_packidx *packidx;
666 struct got_pack *pack;
667 struct search_deltas_arg sda;
669 err = find_pack_for_reuse(&packidx, repo);
676 err = got_packidx_get_packfile_path(&path_packfile,
677 packidx->path_packidx);
681 pack = got_repo_get_cached_pack(repo, path_packfile);
683 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
688 sda.packidx = packidx;
692 sda.delta_cache_fd = delta_cache_fd;
694 sda.progress_cb = progress_cb;
695 sda.progress_arg = progress_arg;
697 sda.cancel_cb = cancel_cb;
698 sda.cancel_arg = cancel_arg;
699 sda.ncolored = ncolored;
702 sda.ncommits = ncommits;
703 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
709 static const struct got_error *
710 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
711 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
712 struct got_repository *repo,
713 got_pack_progress_cb progress_cb, void *progress_arg,
714 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
716 const struct got_error *err = NULL;
717 struct got_pack_meta *m = NULL, *base = NULL;
718 struct got_raw_object *raw = NULL, *base_raw = NULL;
719 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
720 int i, j, ndeltas, best_ndeltas;
721 off_t size, best_size;
722 const int max_base_candidates = 3;
723 size_t delta_memsize = 0;
724 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
727 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
728 for (i = 0; i < nmeta; i++) {
730 err = (*cancel_cb)(cancel_arg);
734 err = report_progress(progress_cb, progress_arg, rl,
735 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
741 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
742 m->obj_type == GOT_OBJ_TYPE_TAG)
745 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
750 if (raw->f == NULL) {
751 err = got_deltify_init_mem(&m->dtab, raw->data,
752 raw->hdrlen, raw->size + raw->hdrlen);
754 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
755 raw->size + raw->hdrlen);
760 if (i > max_base_candidates) {
761 struct got_pack_meta *n = NULL;
762 n = meta[i - (max_base_candidates + 1)];
763 got_deltify_free(n->dtab);
767 best_size = raw->size;
769 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
771 err = (*cancel_cb)(cancel_arg);
776 /* long chains make unpacking slow, avoid such bases */
777 if (base->nchain >= 128 ||
778 base->obj_type != m->obj_type)
781 err = got_object_raw_open(&base_raw, &outfd, repo,
786 if (raw->f == NULL && base_raw->f == NULL) {
787 err = got_deltify_mem_mem(&deltas, &ndeltas,
788 raw->data, raw->hdrlen,
789 raw->size + raw->hdrlen,
790 base->dtab, base_raw->data,
792 base_raw->size + base_raw->hdrlen);
793 } else if (raw->f == NULL) {
794 err = got_deltify_mem_file(&deltas, &ndeltas,
795 raw->data, raw->hdrlen,
796 raw->size + raw->hdrlen,
797 base->dtab, base_raw->f,
799 base_raw->size + base_raw->hdrlen);
800 } else if (base_raw->f == NULL) {
801 err = got_deltify_file_mem(&deltas, &ndeltas,
803 raw->size + raw->hdrlen,
804 base->dtab, base_raw->data,
806 base_raw->size + base_raw->hdrlen);
808 err = got_deltify(&deltas, &ndeltas,
810 raw->size + raw->hdrlen,
811 base->dtab, base_raw->f, base_raw->hdrlen,
812 base_raw->size + base_raw->hdrlen);
814 got_object_raw_close(base_raw);
819 size = delta_size(deltas, ndeltas);
820 if (size + 32 < best_size){
822 * if we already picked a best delta,
827 best_deltas = deltas;
828 best_ndeltas = ndeltas;
830 m->nchain = base->nchain + 1;
832 m->head = base->head;
842 if (best_ndeltas > 0) {
843 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
844 delta_memsize + best_size <= max_delta_memsize) {
845 delta_memsize += best_size;
846 err = encode_delta_in_mem(m, raw, best_deltas,
847 best_ndeltas, best_size, m->prev->size);
849 m->delta_offset = ftello(delta_cache);
850 err = encode_delta(m, raw, best_deltas,
851 best_ndeltas, m->prev->size, delta_cache);
860 got_object_raw_close(raw);
864 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
865 got_deltify_free(meta[i]->dtab);
866 meta[i]->dtab = NULL;
869 got_object_raw_close(raw);
871 got_object_raw_close(base_raw);
872 if (outfd != -1 && close(outfd) == -1 && err == NULL)
873 err = got_error_from_errno("close");
879 static const struct got_error *
880 search_packidx(int *found, struct got_object_id *id,
881 struct got_repository *repo)
883 const struct got_error *err = NULL;
884 struct got_packidx *packidx = NULL;
889 err = got_repo_search_packidx(&packidx, &idx, repo, id);
891 *found = 1; /* object is already packed */
892 else if (err->code == GOT_ERR_NO_OBJ)
897 static const struct got_error *
898 add_object(int want_meta, struct got_object_idset *idset,
899 struct got_object_id *id, const char *path, int obj_type,
900 time_t mtime, int loose_obj_only, struct got_repository *repo,
901 int *ncolored, int *nfound, int *ntrees,
902 got_pack_progress_cb progress_cb, void *progress_arg,
903 struct got_ratelimit *rl)
905 const struct got_error *err;
906 struct got_pack_meta *m = NULL;
908 if (loose_obj_only) {
910 err = search_packidx(&is_packed, id, repo);
913 if (is_packed && want_meta)
918 err = alloc_meta(&m, id, path, obj_type, mtime);
923 err = report_progress(progress_cb, progress_arg, rl,
924 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
932 err = got_object_idset_add(idset, id, m);
940 static const struct got_error *
941 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
942 struct got_object_idset *idset, struct got_object_id *tree_id,
943 const char *dpath, time_t mtime, struct got_repository *repo,
944 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
945 got_pack_progress_cb progress_cb, void *progress_arg,
946 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
948 const struct got_error *err;
949 struct got_tree_object *tree;
953 err = got_object_open_as_tree(&tree, repo, tree_id);
958 err = report_progress(progress_cb, progress_arg, rl,
959 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
963 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
964 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
965 struct got_object_id *id = got_tree_entry_get_id(e);
966 mode_t mode = got_tree_entry_get_mode(e);
969 err = (*cancel_cb)(cancel_arg);
974 if (got_object_tree_entry_is_submodule(e) ||
975 got_object_idset_contains(idset, id))
978 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
979 got_tree_entry_get_name(e)) == -1) {
980 err = got_error_from_errno("asprintf");
985 struct got_object_qid *qid;
986 err = got_object_qid_alloc(&qid, id);
989 STAILQ_INSERT_TAIL(ids, qid, entry);
990 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
991 err = add_object(want_meta, idset, id, p,
992 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
993 ncolored, nfound, ntrees,
994 progress_cb, progress_arg, rl);
1002 got_object_tree_close(tree);
1007 static const struct got_error *
1008 load_tree(int want_meta, struct got_object_idset *idset,
1009 struct got_object_id *tree_id, const char *dpath, time_t mtime,
1010 struct got_repository *repo, int loose_obj_only,
1011 int *ncolored, int *nfound, int *ntrees,
1012 got_pack_progress_cb progress_cb, void *progress_arg,
1013 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1015 const struct got_error *err = NULL;
1016 struct got_object_id_queue tree_ids;
1017 struct got_object_qid *qid;
1019 if (got_object_idset_contains(idset, tree_id))
1022 err = got_object_qid_alloc(&qid, tree_id);
1026 STAILQ_INIT(&tree_ids);
1027 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1029 while (!STAILQ_EMPTY(&tree_ids)) {
1031 err = (*cancel_cb)(cancel_arg);
1036 qid = STAILQ_FIRST(&tree_ids);
1037 STAILQ_REMOVE_HEAD(&tree_ids, entry);
1039 if (got_object_idset_contains(idset, &qid->id)) {
1040 got_object_qid_free(qid);
1044 err = add_object(want_meta, idset, &qid->id, dpath,
1045 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo,
1046 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1048 got_object_qid_free(qid);
1052 err = load_tree_entries(&tree_ids, want_meta, idset, &qid->id,
1053 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1054 ntrees, progress_cb, progress_arg, rl,
1055 cancel_cb, cancel_arg);
1056 got_object_qid_free(qid);
1061 got_object_id_queue_free(&tree_ids);
1065 static const struct got_error *
1066 load_commit(int want_meta, struct got_object_idset *idset,
1067 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1068 int *ncolored, int *nfound, int *ntrees,
1069 got_pack_progress_cb progress_cb, void *progress_arg,
1070 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1072 const struct got_error *err;
1073 struct got_commit_object *commit;
1075 if (got_object_idset_contains(idset, id))
1078 if (loose_obj_only) {
1080 err = search_packidx(&is_packed, id, repo);
1083 if (is_packed && want_meta)
1087 err = got_object_open_as_commit(&commit, repo, id);
1091 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1092 got_object_commit_get_committer_time(commit),
1093 loose_obj_only, repo,
1094 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1098 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1099 "", got_object_commit_get_committer_time(commit),
1100 repo, loose_obj_only, ncolored, nfound, ntrees,
1101 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1103 got_object_commit_close(commit);
1107 static const struct got_error *
1108 load_tag(int want_meta, struct got_object_idset *idset,
1109 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1110 int *ncolored, int *nfound, int *ntrees,
1111 got_pack_progress_cb progress_cb, void *progress_arg,
1112 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1114 const struct got_error *err;
1115 struct got_tag_object *tag = NULL;
1117 if (got_object_idset_contains(idset, id))
1120 if (loose_obj_only) {
1122 err = search_packidx(&is_packed, id, repo);
1125 if (is_packed && want_meta)
1129 err = got_object_open_as_tag(&tag, repo, id);
1133 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1134 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1135 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1139 switch (got_object_tag_get_object_type(tag)) {
1140 case GOT_OBJ_TYPE_COMMIT:
1141 err = load_commit(want_meta, idset,
1142 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1143 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1144 cancel_cb, cancel_arg);
1146 case GOT_OBJ_TYPE_TREE:
1147 err = load_tree(want_meta, idset,
1148 got_object_tag_get_object_id(tag), "",
1149 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1150 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1151 cancel_cb, cancel_arg);
1158 got_object_tag_close(tag);
1162 enum findtwixt_color {
1169 static const int findtwixt_colors[] = {
1176 static const struct got_error *
1177 paint_commit(struct got_object_qid *qid, int color)
1179 if (color < 0 || color >= nitems(findtwixt_colors))
1180 return got_error(GOT_ERR_RANGE);
1182 qid->data = (void *)&findtwixt_colors[color];
1186 static const struct got_error *
1187 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1188 int color, struct got_repository *repo)
1190 const struct got_error *err;
1191 struct got_object_qid *qid;
1193 err = got_object_qid_alloc(&qid, id);
1197 STAILQ_INSERT_TAIL(ids, qid, entry);
1198 return paint_commit(qid, color);
1201 struct append_id_arg {
1202 struct got_object_id **array;
1204 struct got_object_idset *drop;
1205 struct got_object_idset *skip;
1208 static const struct got_error *
1209 append_id(struct got_object_id *id, void *data, void *arg)
1211 struct append_id_arg *a = arg;
1213 if (got_object_idset_contains(a->skip, id) ||
1214 got_object_idset_contains(a->drop, id))
1217 a->array[++a->idx] = got_object_id_dup(id);
1218 if (a->array[a->idx] == NULL)
1219 return got_error_from_errno("got_object_id_dup");
1224 static const struct got_error *
1225 queue_commit_or_tag_id(struct got_object_id *id, int color,
1226 struct got_object_id_queue *ids, struct got_repository *repo)
1228 const struct got_error *err;
1229 struct got_tag_object *tag = NULL;
1232 err = got_object_get_type(&obj_type, repo, id);
1236 if (obj_type == GOT_OBJ_TYPE_TAG) {
1237 err = got_object_open_as_tag(&tag, repo, id);
1240 obj_type = got_object_tag_get_object_type(tag);
1241 id = got_object_tag_get_object_id(tag);
1244 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1245 err = queue_commit_id(ids, id, color, repo);
1251 got_object_tag_close(tag);
1255 static const struct got_error *
1256 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1257 struct got_object_idset *keep, struct got_object_idset *drop,
1258 struct got_object_idset *skip, struct got_repository *repo,
1259 got_pack_progress_cb progress_cb, void *progress_arg,
1260 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1262 const struct got_error *err = NULL;
1263 struct got_commit_object *commit = NULL;
1264 const struct got_object_id_queue *parents;
1265 struct got_object_qid *qid;
1266 int nqueued = nids, nskip = 0;
1268 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1272 err = cancel_cb(cancel_arg);
1277 qid = STAILQ_FIRST(ids);
1278 STAILQ_REMOVE_HEAD(ids, entry);
1280 color = *((int *)qid->data);
1281 if (color == COLOR_SKIP)
1284 if (got_object_idset_contains(skip, &qid->id)) {
1285 got_object_qid_free(qid);
1291 if (got_object_idset_contains(keep, &qid->id)) {
1292 got_object_qid_free(qid);
1295 if (got_object_idset_contains(drop, &qid->id)) {
1296 err = paint_commit(qid, COLOR_SKIP);
1302 err = got_object_idset_add(keep, &qid->id, NULL);
1307 if (got_object_idset_contains(drop, &qid->id)) {
1308 got_object_qid_free(qid);
1311 if (got_object_idset_contains(keep, &qid->id)) {
1312 err = paint_commit(qid, COLOR_SKIP);
1318 err = got_object_idset_add(drop, &qid->id, NULL);
1323 if (!got_object_idset_contains(skip, &qid->id)) {
1324 err = got_object_idset_add(skip, &qid->id,
1331 /* should not happen */
1332 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1333 "%s invalid commit color %d", __func__, color);
1337 err = report_progress(progress_cb, progress_arg, rl,
1338 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1343 err = got_object_open_as_commit(&commit, repo, &qid->id);
1347 parents = got_object_commit_get_parent_ids(commit);
1349 struct got_object_qid *pid;
1350 color = *((int *)qid->data);
1351 STAILQ_FOREACH(pid, parents, entry) {
1352 err = queue_commit_id(ids, &pid->id, color,
1357 if (color == COLOR_SKIP)
1362 got_object_commit_close(commit);
1364 got_object_qid_free(qid);
1368 got_object_commit_close(commit);
1372 static const struct got_error *
1373 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1374 struct got_object_id **head, int nhead,
1375 struct got_object_id **tail, int ntail,
1376 struct got_repository *repo,
1377 got_pack_progress_cb progress_cb, void *progress_arg,
1378 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1380 const struct got_error *err = NULL;
1381 struct got_object_id_queue ids;
1382 struct got_object_idset *keep, *drop, *skip = NULL;
1390 keep = got_object_idset_alloc();
1392 return got_error_from_errno("got_object_idset_alloc");
1394 drop = got_object_idset_alloc();
1396 err = got_error_from_errno("got_object_idset_alloc");
1400 skip = got_object_idset_alloc();
1402 err = got_error_from_errno("got_object_idset_alloc");
1406 for (i = 0; i < nhead; i++) {
1407 struct got_object_id *id = head[i];
1410 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1415 for (i = 0; i < ntail; i++) {
1416 struct got_object_id *id = tail[i];
1419 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1424 err = paint_commits(ncolored, &ids, nhead + ntail,
1425 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1426 cancel_cb, cancel_arg);
1430 nkeep = got_object_idset_num_elements(keep);
1432 struct append_id_arg arg;
1433 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1434 if (arg.array == NULL) {
1435 err = got_error_from_errno("calloc");
1441 err = got_object_idset_for_each(keep, append_id, &arg);
1447 *nres = arg.idx + 1;
1450 got_object_idset_free(keep);
1451 got_object_idset_free(drop);
1453 got_object_idset_free(skip);
1454 got_object_id_queue_free(&ids);
1458 static const struct got_error *
1459 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1460 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1461 struct got_object_id **ours, int nours, struct got_repository *repo,
1462 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1463 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1465 const struct got_error *err = NULL;
1466 struct got_object_id **ids = NULL;
1467 int i, nobj = 0, obj_type;
1473 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1474 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1475 if (err || nobj == 0)
1478 for (i = 0; i < ntheirs; i++) {
1479 struct got_object_id *id = theirs[i];
1482 err = got_object_get_type(&obj_type, repo, id);
1485 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1486 err = load_commit(0, idset, id, repo,
1487 loose_obj_only, ncolored, nfound, ntrees,
1488 progress_cb, progress_arg, rl,
1489 cancel_cb, cancel_arg);
1492 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1493 err = load_tag(0, idset, id, repo,
1494 loose_obj_only, ncolored, nfound, ntrees,
1495 progress_cb, progress_arg, rl,
1496 cancel_cb, cancel_arg);
1502 for (i = 0; i < nobj; i++) {
1503 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1504 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1505 cancel_cb, cancel_arg);
1510 for (i = 0; i < nours; i++) {
1511 struct got_object_id *id = ours[i];
1512 struct got_pack_meta *m;
1515 m = got_object_idset_get(idset, id);
1517 err = got_object_get_type(&obj_type, repo, id);
1521 obj_type = m->obj_type;
1522 if (obj_type != GOT_OBJ_TYPE_TAG)
1524 err = load_tag(1, idset, id, repo, loose_obj_only,
1525 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1526 cancel_cb, cancel_arg);
1531 for (i = 0; i < nobj; i++) {
1538 const struct got_error *
1539 hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
1543 SHA1Update(ctx, buf, len);
1544 n = fwrite(buf, 1, len, f);
1546 return got_ferror(f, GOT_ERR_IO);
1550 const struct got_error *
1551 hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1553 unsigned char buf[65536];
1557 while (remain > 0) {
1558 size_t copylen = MIN(sizeof(buf), remain);
1559 n = fread(buf, 1, copylen, fsrc);
1561 return got_ferror(fsrc, GOT_ERR_IO);
1562 SHA1Update(ctx, buf, copylen);
1563 n = fwrite(buf, 1, copylen, fdst);
1565 return got_ferror(fdst, GOT_ERR_IO);
1573 putbe32(char *b, uint32_t n)
1582 write_order_cmp(const void *pa, const void *pb)
1584 struct got_pack_meta *a, *b, *ahd, *bhd;
1586 a = *(struct got_pack_meta **)pa;
1587 b = *(struct got_pack_meta **)pb;
1588 ahd = (a->head == NULL) ? a : a->head;
1589 bhd = (b->head == NULL) ? b : b->head;
1590 if (bhd->mtime < ahd->mtime)
1592 if (bhd->mtime > ahd->mtime)
1598 if (a->nchain != b->nchain)
1599 return a->nchain - b->nchain;
1600 if (a->mtime < b->mtime)
1602 if (a->mtime > b->mtime)
1604 return got_object_id_cmp(&a->id, &b->id);
1608 reuse_write_order_cmp(const void *pa, const void *pb)
1610 struct got_pack_meta *a, *b;
1612 a = *(struct got_pack_meta **)pa;
1613 b = *(struct got_pack_meta **)pb;
1615 if (a->reused_delta_offset < b->reused_delta_offset)
1617 if (a->reused_delta_offset > b->reused_delta_offset)
1622 static const struct got_error *
1623 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1629 hdr[0] = obj_type << 4;
1630 hdr[0] |= len & 0xf;
1632 for (i = 1; len != 0; i++){
1634 return got_error(GOT_ERR_NO_SPACE);
1635 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1636 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1637 len >>= GOT_DELTA_SIZE_SHIFT;
1645 packoff(char *hdr, off_t off)
1650 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1651 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1652 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1653 GOT_DELTA_SIZE_MORE;
1658 hdr[j++] = rbuf[--i];
1662 static const struct got_error *
1663 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1664 struct got_pack_meta *m)
1666 const struct got_error *err;
1670 if (m->prev->off != 0) {
1671 err = packhdr(&nh, buf, sizeof(buf),
1672 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1675 nh += packoff(buf + nh, m->off - m->prev->off);
1676 err = hwrite(packfile, buf, nh, ctx);
1679 *packfile_size += nh;
1681 err = packhdr(&nh, buf, sizeof(buf),
1682 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1685 err = hwrite(packfile, buf, nh, ctx);
1688 *packfile_size += nh;
1689 err = hwrite(packfile, m->prev->id.sha1,
1690 sizeof(m->prev->id.sha1), ctx);
1693 *packfile_size += sizeof(m->prev->id.sha1);
1699 static const struct got_error *
1700 write_packed_object(off_t *packfile_size, FILE *packfile,
1701 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1702 SHA1_CTX *ctx, struct got_repository *repo)
1704 const struct got_error *err = NULL;
1705 struct got_deflate_checksum csum;
1708 struct got_raw_object *raw = NULL;
1711 csum.output_sha1 = ctx;
1712 csum.output_crc = NULL;
1714 m->off = ftello(packfile);
1715 if (m->delta_len == 0) {
1716 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1719 err = packhdr(&nh, buf, sizeof(buf),
1720 m->obj_type, raw->size);
1723 err = hwrite(packfile, buf, nh, ctx);
1726 *packfile_size += nh;
1727 if (raw->f == NULL) {
1728 err = got_deflate_to_file_mmap(&outlen,
1729 raw->data + raw->hdrlen, 0, raw->size,
1734 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1736 err = got_error_from_errno("fseeko");
1739 err = got_deflate_to_file(&outlen, raw->f,
1740 raw->size, packfile, &csum);
1744 *packfile_size += outlen;
1745 got_object_raw_close(raw);
1747 } else if (m->delta_buf) {
1748 err = deltahdr(packfile_size, ctx, packfile, m);
1751 err = hwrite(packfile, m->delta_buf,
1752 m->delta_compressed_len, ctx);
1755 *packfile_size += m->delta_compressed_len;
1757 m->delta_buf = NULL;
1759 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1761 err = got_error_from_errno("fseeko");
1764 err = deltahdr(packfile_size, ctx, packfile, m);
1767 err = hcopy(delta_cache, packfile,
1768 m->delta_compressed_len, ctx);
1771 *packfile_size += m->delta_compressed_len;
1775 got_object_raw_close(raw);
1779 static const struct got_error *
1780 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1781 struct got_pack_meta **deltify, int ndeltify,
1782 struct got_pack_meta **reuse, int nreuse,
1783 int ncolored, int nfound, int ntrees, int nours,
1784 struct got_repository *repo,
1785 got_pack_progress_cb progress_cb, void *progress_arg,
1786 struct got_ratelimit *rl,
1787 got_cancel_cb cancel_cb, void *cancel_arg)
1789 const struct got_error *err = NULL;
1792 struct got_pack_meta *m;
1795 off_t packfile_size = 0;
1800 err = hwrite(packfile, "PACK", 4, &ctx);
1803 putbe32(buf, GOT_PACKFILE_VERSION);
1804 err = hwrite(packfile, buf, 4, &ctx);
1807 putbe32(buf, ndeltify + nreuse);
1808 err = hwrite(packfile, buf, 4, &ctx);
1812 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1814 for (i = 0; i < ndeltify; i++) {
1815 err = report_progress(progress_cb, progress_arg, rl,
1816 ncolored, nfound, ntrees, packfile_size, nours,
1817 ndeltify + nreuse, ndeltify + nreuse, i);
1821 err = write_packed_object(&packfile_size, packfile,
1822 delta_cache, m, &outfd, &ctx, repo);
1827 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1828 reuse_write_order_cmp);
1829 for (i = 0; i < nreuse; i++) {
1830 err = report_progress(progress_cb, progress_arg, rl,
1831 ncolored, nfound, ntrees, packfile_size, nours,
1832 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1836 err = write_packed_object(&packfile_size, packfile,
1837 delta_cache, m, &outfd, &ctx, repo);
1842 SHA1Final(pack_sha1, &ctx);
1843 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1844 if (n != SHA1_DIGEST_LENGTH)
1845 err = got_ferror(packfile, GOT_ERR_IO);
1846 packfile_size += SHA1_DIGEST_LENGTH;
1847 packfile_size += sizeof(struct got_packfile_hdr);
1849 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1850 packfile_size, nours, ndeltify + nreuse,
1851 ndeltify + nreuse, ndeltify + nreuse);
1856 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1857 err = got_error_from_errno("close");
1861 static const struct got_error *
1862 remove_unused_object(struct got_object_id *id, void *data, void *arg)
1864 struct got_object_idset *idset = arg;
1867 got_object_idset_remove(NULL, idset, id);
1872 static const struct got_error *
1873 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1875 struct got_pack_meta *m = data;
1876 struct got_pack_metavec *v = arg;
1878 if (m->have_reused_delta)
1881 return add_meta(m, v);
1884 const struct got_error *
1885 got_pack_create(uint8_t *packsha1, FILE *packfile,
1886 struct got_object_id **theirs, int ntheirs,
1887 struct got_object_id **ours, int nours,
1888 struct got_repository *repo, int loose_obj_only, int allow_empty,
1889 got_pack_progress_cb progress_cb, void *progress_arg,
1890 got_cancel_cb cancel_cb, void *cancel_arg)
1892 const struct got_error *err;
1893 int delta_cache_fd = -1;
1894 FILE *delta_cache = NULL;
1895 struct got_object_idset *idset;
1896 struct got_ratelimit rl;
1897 struct got_pack_metavec deltify, reuse;
1898 int ncolored = 0, nfound = 0, ntrees = 0;
1901 memset(&deltify, 0, sizeof(deltify));
1902 memset(&reuse, 0, sizeof(reuse));
1904 got_ratelimit_init(&rl, 0, 500);
1906 idset = got_object_idset_alloc();
1908 return got_error_from_errno("got_object_idset_alloc");
1910 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1911 ntheirs, ours, nours, repo, loose_obj_only,
1912 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1916 err = got_object_idset_for_each(idset, remove_unused_object, idset);
1921 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1922 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1927 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1928 err = got_error(GOT_ERR_CANNOT_PACK);
1932 delta_cache_fd = got_opentempfd();
1933 if (delta_cache_fd == -1) {
1934 err = got_error_from_errno("got_opentemp");
1939 reuse.meta = calloc(reuse.metasz,
1940 sizeof(struct got_pack_meta *));
1941 if (reuse.meta == NULL) {
1942 err = got_error_from_errno("calloc");
1946 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1947 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1948 cancel_cb, cancel_arg);
1952 delta_cache = fdopen(delta_cache_fd, "a+");
1953 if (delta_cache == NULL) {
1954 err = got_error_from_errno("fdopen");
1957 delta_cache_fd = -1;
1959 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1960 err = got_error_from_errno("fseeko");
1964 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1966 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1967 if (deltify.meta == NULL) {
1968 err = got_error_from_errno("calloc");
1971 deltify.metasz = ndeltify;
1973 err = got_object_idset_for_each(idset, add_meta_idset_cb,
1977 if (deltify.nmeta > 0) {
1978 err = pick_deltas(deltify.meta, deltify.nmeta,
1979 ncolored, nfound, ntrees, nours, reuse.nmeta,
1980 delta_cache, repo, progress_cb, progress_arg, &rl,
1981 cancel_cb, cancel_arg);
1987 if (fflush(delta_cache) == EOF) {
1988 err = got_error_from_errno("fflush");
1991 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1992 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1993 nours, repo, progress_cb, progress_arg, &rl,
1994 cancel_cb, cancel_arg);
1998 free_nmeta(deltify.meta, deltify.nmeta);
1999 free_nmeta(reuse.meta, reuse.nmeta);
2000 got_object_idset_free(idset);
2001 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2002 err = got_error_from_errno("close");
2003 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2004 err = got_error_from_errno("fclose");