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>
40 #include "got_error.h"
41 #include "got_cancel.h"
42 #include "got_object.h"
44 #include "got_reference.h"
45 #include "got_repository_admin.h"
47 #include "got_lib_deltify.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_idset.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_deflate.h"
53 #include "got_lib_ratelimit.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_pack_create.h"
56 #include "got_lib_repository.h"
57 #include "got_lib_inflate.h"
59 #include "murmurhash2.h"
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 static const struct got_error *
74 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
75 const char *path, int obj_type, time_t mtime, uint32_t seed)
77 struct got_pack_meta *m;
81 m = calloc(1, sizeof(*m));
83 return got_error_from_errno("calloc");
85 memcpy(&m->id, id, sizeof(m->id));
87 m->path_hash = murmurhash2(path, strlen(path), seed);
88 m->obj_type = obj_type;
95 clear_meta(struct got_pack_meta *meta)
100 free(meta->delta_buf);
101 meta->delta_buf = NULL;
102 free(meta->base_obj_id);
103 meta->base_obj_id = NULL;
104 meta->reused_delta_offset = 0;
108 free_nmeta(struct got_pack_meta **meta, int nmeta)
112 for (i = 0; i < nmeta; i++)
118 delta_order_cmp(const void *pa, const void *pb)
120 struct got_pack_meta *a, *b;
122 a = *(struct got_pack_meta **)pa;
123 b = *(struct got_pack_meta **)pb;
125 if (a->obj_type != b->obj_type)
126 return a->obj_type - b->obj_type;
127 if (a->path_hash < b->path_hash)
129 if (a->path_hash > b->path_hash)
131 if (a->mtime < b->mtime)
133 if (a->mtime > b->mtime)
135 return got_object_id_cmp(&a->id, &b->id);
139 delta_size(struct got_delta_instruction *deltas, int ndeltas)
143 for (i = 0; i < ndeltas; i++) {
145 size += GOT_DELTA_SIZE_SHIFT;
147 size += deltas[i].len + 1;
152 static const struct got_error *
153 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
157 if (*len + nseg >= *sz) {
158 while (*len + nseg >= *sz)
160 n = realloc(*p, *sz);
162 return got_error_from_errno("realloc");
165 memcpy(*p + *len, seg, nseg);
170 static const struct got_error *
171 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
172 struct got_delta_instruction *deltas, int ndeltas,
173 off_t delta_size, off_t base_size)
175 const struct got_error *err;
176 unsigned char buf[16], *bp;
178 size_t len = 0, compressed_len;
179 off_t bufsize = delta_size;
181 struct got_delta_instruction *d;
184 delta_buf = malloc(bufsize);
185 if (delta_buf == NULL)
186 return got_error_from_errno("malloc");
188 /* base object size */
189 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
190 n = base_size >> GOT_DELTA_SIZE_SHIFT;
191 for (i = 1; n > 0; i++) {
192 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
193 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
194 n >>= GOT_DELTA_SIZE_SHIFT;
196 err = append(&delta_buf, &len, &bufsize, buf, i);
200 /* target object size */
201 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
202 n = o->size >> GOT_DELTA_SIZE_SHIFT;
203 for (i = 1; n > 0; i++) {
204 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
205 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
206 n >>= GOT_DELTA_SIZE_SHIFT;
208 err = append(&delta_buf, &len, &bufsize, buf, i);
212 for (j = 0; j < ndeltas; j++) {
217 buf[0] = GOT_DELTA_BASE_COPY;
218 for (i = 0; i < 4; i++) {
219 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
228 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
229 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
230 for (i = 0; i < 3 && n > 0; i++) {
231 buf[0] |= 1 << (i + 4);
236 err = append(&delta_buf, &len, &bufsize,
240 } else if (o->f == NULL) {
242 while (n != d->len) {
243 buf[0] = (d->len - n < 127) ? d->len - n : 127;
244 err = append(&delta_buf, &len, &bufsize,
248 err = append(&delta_buf, &len, &bufsize,
249 o->data + o->hdrlen + d->offset + n,
258 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
259 err = got_error_from_errno("fseeko");
263 while (n != d->len) {
264 buf[0] = (d->len - n < 127) ? d->len - n : 127;
265 err = append(&delta_buf, &len, &bufsize,
269 r = fread(content, 1, buf[0], o->f);
271 err = got_ferror(o->f, GOT_ERR_IO);
274 err = append(&delta_buf, &len, &bufsize,
283 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
284 NULL, NULL, delta_buf, 0, len);
289 m->delta_compressed_len = compressed_len;
295 static const struct got_error *
296 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
297 struct got_delta_instruction *deltas, int ndeltas,
298 off_t base_size, FILE *f)
300 const struct got_error *err;
301 unsigned char buf[16], *bp;
304 struct got_deflate_buf zb;
305 struct got_delta_instruction *d;
306 off_t delta_len = 0, compressed_len = 0;
308 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
312 /* base object size */
313 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
314 n = base_size >> GOT_DELTA_SIZE_SHIFT;
315 for (i = 1; n > 0; i++) {
316 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
317 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
318 n >>= GOT_DELTA_SIZE_SHIFT;
321 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
327 /* target object size */
328 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
329 n = o->size >> GOT_DELTA_SIZE_SHIFT;
330 for (i = 1; n > 0; i++) {
331 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
332 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
333 n >>= GOT_DELTA_SIZE_SHIFT;
336 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
342 for (j = 0; j < ndeltas; j++) {
347 buf[0] = GOT_DELTA_BASE_COPY;
348 for (i = 0; i < 4; i++) {
349 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
357 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
358 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
359 for (i = 0; i < 3 && n > 0; i++) {
360 buf[0] |= 1 << (i + 4);
365 err = got_deflate_append_to_file_mmap(&zb,
366 &compressed_len, buf, 0, bp - buf, f, NULL);
369 delta_len += (bp - buf);
370 } else if (o->f == NULL) {
372 while (n != d->len) {
373 buf[0] = (d->len - n < 127) ? d->len - n : 127;
374 err = got_deflate_append_to_file_mmap(&zb,
375 &compressed_len, buf, 0, 1, f, NULL);
379 err = got_deflate_append_to_file_mmap(&zb,
381 o->data + o->hdrlen + d->offset + n, 0,
391 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
392 err = got_error_from_errno("fseeko");
396 while (n != d->len) {
397 buf[0] = (d->len - n < 127) ? d->len - n : 127;
398 err = got_deflate_append_to_file_mmap(&zb,
399 &compressed_len, buf, 0, 1, f, NULL);
403 r = fread(content, 1, buf[0], o->f);
405 err = got_ferror(o->f, GOT_ERR_IO);
408 err = got_deflate_append_to_file_mmap(&zb,
409 &compressed_len, content, 0, buf[0], f,
419 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
424 if (compressed_len != ftello(f) - m->delta_offset) {
425 err = got_error(GOT_ERR_COMPRESSION);
429 m->delta_len = delta_len;
430 m->delta_compressed_len = compressed_len;
432 got_deflate_end(&zb);
436 const struct got_error *
437 got_pack_report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
438 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
439 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
442 const struct got_error *err;
445 if (progress_cb == NULL)
448 err = got_ratelimit_check(&elapsed, rl);
452 return progress_cb(progress_arg, ncolored, nfound, ntrees,
453 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
456 const struct got_error *
457 got_pack_add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
459 if (v->nmeta == v->metasz){
460 size_t newsize = 2 * v->metasz;
461 struct got_pack_meta **new;
462 new = reallocarray(v->meta, newsize, sizeof(*new));
464 return got_error_from_errno("reallocarray");
469 v->meta[v->nmeta++] = m;
473 const struct got_error *
474 got_pack_find_pack_for_reuse(struct got_packidx **best_packidx,
475 struct got_repository *repo)
477 const struct got_error *err = NULL;
478 struct got_pathlist_entry *pe;
479 const char *best_packidx_path = NULL;
482 *best_packidx = NULL;
484 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
485 const char *path_packidx = pe->path;
486 struct got_packidx *packidx;
489 err = got_repo_get_packidx(&packidx, path_packidx, repo);
493 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
494 if (nobj > nobj_max) {
495 best_packidx_path = path_packidx;
500 if (best_packidx_path) {
501 err = got_repo_get_packidx(best_packidx, best_packidx_path,
508 const struct got_error *
509 got_pack_cache_pack_for_packidx(struct got_pack **pack,
510 struct got_packidx *packidx, struct got_repository *repo)
512 const struct got_error *err;
513 char *path_packfile = NULL;
515 err = got_packidx_get_packfile_path(&path_packfile,
516 packidx->path_packidx);
520 *pack = got_repo_get_cached_pack(repo, path_packfile);
522 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
531 static const struct got_error *
532 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
533 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
534 struct got_repository *repo,
535 got_pack_progress_cb progress_cb, void *progress_arg,
536 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
538 const struct got_error *err = NULL;
539 struct got_pack_meta *m = NULL, *base = NULL;
540 struct got_raw_object *raw = NULL, *base_raw = NULL;
541 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
542 int i, j, ndeltas, best_ndeltas;
543 off_t size, best_size;
544 const int max_base_candidates = 3;
545 size_t delta_memsize = 0;
546 const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
550 delta_seed = arc4random();
552 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
553 for (i = 0; i < nmeta; i++) {
555 err = (*cancel_cb)(cancel_arg);
559 err = got_pack_report_progress(progress_cb, progress_arg, rl,
560 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
566 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
567 m->obj_type == GOT_OBJ_TYPE_TAG)
570 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
575 if (raw->f == NULL) {
576 err = got_deltify_init_mem(&m->dtab, raw->data,
577 raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
579 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
580 raw->size + raw->hdrlen, delta_seed);
585 if (i > max_base_candidates) {
586 struct got_pack_meta *n = NULL;
587 n = meta[i - (max_base_candidates + 1)];
588 got_deltify_free(n->dtab);
592 best_size = raw->size;
594 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
596 err = (*cancel_cb)(cancel_arg);
601 /* long chains make unpacking slow, avoid such bases */
602 if (base->nchain >= 128 ||
603 base->obj_type != m->obj_type)
606 err = got_object_raw_open(&base_raw, &outfd, repo,
611 if (raw->f == NULL && base_raw->f == NULL) {
612 err = got_deltify_mem_mem(&deltas, &ndeltas,
613 raw->data, raw->hdrlen,
614 raw->size + raw->hdrlen, delta_seed,
615 base->dtab, base_raw->data,
617 base_raw->size + base_raw->hdrlen);
618 } else if (raw->f == NULL) {
619 err = got_deltify_mem_file(&deltas, &ndeltas,
620 raw->data, raw->hdrlen,
621 raw->size + raw->hdrlen, delta_seed,
622 base->dtab, base_raw->f,
624 base_raw->size + base_raw->hdrlen);
625 } else if (base_raw->f == NULL) {
626 err = got_deltify_file_mem(&deltas, &ndeltas,
628 raw->size + raw->hdrlen, delta_seed,
629 base->dtab, base_raw->data,
631 base_raw->size + base_raw->hdrlen);
633 err = got_deltify(&deltas, &ndeltas,
635 raw->size + raw->hdrlen, delta_seed,
636 base->dtab, base_raw->f, base_raw->hdrlen,
637 base_raw->size + base_raw->hdrlen);
639 got_object_raw_close(base_raw);
644 size = delta_size(deltas, ndeltas);
645 if (size + 32 < best_size){
647 * if we already picked a best delta,
652 best_deltas = deltas;
653 best_ndeltas = ndeltas;
655 m->nchain = base->nchain + 1;
657 m->head = base->head;
667 if (best_ndeltas > 0) {
668 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
669 delta_memsize + best_size <= max_delta_memsize) {
670 delta_memsize += best_size;
671 err = encode_delta_in_mem(m, raw, best_deltas,
672 best_ndeltas, best_size, m->prev->size);
674 m->delta_offset = ftello(delta_cache);
675 err = encode_delta(m, raw, best_deltas,
676 best_ndeltas, m->prev->size, delta_cache);
685 got_object_raw_close(raw);
689 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
690 got_deltify_free(meta[i]->dtab);
691 meta[i]->dtab = NULL;
694 got_object_raw_close(raw);
696 got_object_raw_close(base_raw);
697 if (outfd != -1 && close(outfd) == -1 && err == NULL)
698 err = got_error_from_errno("close");
704 static const struct got_error *
705 search_packidx(int *found, struct got_object_id *id,
706 struct got_repository *repo)
708 const struct got_error *err = NULL;
709 struct got_packidx *packidx = NULL;
714 err = got_repo_search_packidx(&packidx, &idx, repo, id);
716 *found = 1; /* object is already packed */
717 else if (err->code == GOT_ERR_NO_OBJ)
722 const struct got_error *
723 got_pack_add_object(int want_meta, struct got_object_idset *idset,
724 struct got_object_id *id, const char *path, int obj_type,
725 time_t mtime, uint32_t seed, int loose_obj_only,
726 struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
727 got_pack_progress_cb progress_cb, void *progress_arg,
728 struct got_ratelimit *rl)
730 const struct got_error *err;
731 struct got_pack_meta *m = NULL;
733 if (loose_obj_only) {
735 err = search_packidx(&is_packed, id, repo);
738 if (is_packed && want_meta)
743 err = alloc_meta(&m, id, path, obj_type, mtime, seed);
748 err = got_pack_report_progress(progress_cb, progress_arg, rl,
749 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
757 err = got_object_idset_add(idset, id, m);
765 const struct got_error *
766 got_pack_load_tree_entries(struct got_object_id_queue *ids, int want_meta,
767 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
768 struct got_tree_object *tree,
769 const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
770 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
771 got_pack_progress_cb progress_cb, void *progress_arg,
772 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
774 const struct got_error *err;
779 err = got_pack_report_progress(progress_cb, progress_arg, rl,
780 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
784 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
785 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
786 struct got_object_id *id = got_tree_entry_get_id(e);
787 mode_t mode = got_tree_entry_get_mode(e);
790 err = (*cancel_cb)(cancel_arg);
795 if (got_object_tree_entry_is_submodule(e) ||
796 got_object_idset_contains(idset, id) ||
797 got_object_idset_contains(idset_exclude, id))
801 * If got-read-pack is crawling trees for us then
802 * we are only here to collect blob IDs.
804 if (ids == NULL && S_ISDIR(mode))
807 if (asprintf(&p, "%s%s%s", dpath,
808 got_path_is_root_dir(dpath) ? "" : "/",
809 got_tree_entry_get_name(e)) == -1) {
810 err = got_error_from_errno("asprintf");
815 struct got_object_qid *qid;
816 err = got_object_qid_alloc(&qid, id);
821 STAILQ_INSERT_TAIL(ids, qid, entry);
822 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
823 err = got_pack_add_object(want_meta,
824 want_meta ? idset : idset_exclude, id, p,
825 GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
826 repo, ncolored, nfound, ntrees,
827 progress_cb, progress_arg, rl);
842 const struct got_error *
843 got_pack_load_tree(int want_meta, struct got_object_idset *idset,
844 struct got_object_idset *idset_exclude,
845 struct got_object_id *tree_id, const char *dpath, time_t mtime,
846 uint32_t seed, struct got_repository *repo, int loose_obj_only,
847 int *ncolored, int *nfound, int *ntrees,
848 got_pack_progress_cb progress_cb, void *progress_arg,
849 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
851 const struct got_error *err = NULL;
852 struct got_object_id_queue tree_ids;
853 struct got_object_qid *qid;
854 struct got_tree_object *tree = NULL;
856 if (got_object_idset_contains(idset, tree_id) ||
857 got_object_idset_contains(idset_exclude, tree_id))
860 err = got_object_qid_alloc(&qid, tree_id);
863 qid->data = strdup(dpath);
864 if (qid->data == NULL) {
865 err = got_error_from_errno("strdup");
866 got_object_qid_free(qid);
870 STAILQ_INIT(&tree_ids);
871 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
873 while (!STAILQ_EMPTY(&tree_ids)) {
876 err = (*cancel_cb)(cancel_arg);
881 qid = STAILQ_FIRST(&tree_ids);
882 STAILQ_REMOVE_HEAD(&tree_ids, entry);
885 if (got_object_idset_contains(idset, &qid->id) ||
886 got_object_idset_contains(idset_exclude, &qid->id)) {
888 got_object_qid_free(qid);
892 err = got_pack_add_object(want_meta,
893 want_meta ? idset : idset_exclude,
894 &qid->id, path, GOT_OBJ_TYPE_TREE,
895 mtime, seed, loose_obj_only, repo,
896 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
899 got_object_qid_free(qid);
903 err = got_object_open_as_tree(&tree, repo, &qid->id);
906 got_object_qid_free(qid);
910 err = got_pack_load_tree_entries(&tree_ids, want_meta, idset,
911 idset_exclude, tree, path, mtime, seed, repo,
912 loose_obj_only, ncolored, nfound, ntrees,
913 progress_cb, progress_arg, rl,
914 cancel_cb, cancel_arg);
916 got_object_qid_free(qid);
920 got_object_tree_close(tree);
924 STAILQ_FOREACH(qid, &tree_ids, entry)
926 got_object_id_queue_free(&tree_ids);
928 got_object_tree_close(tree);
932 static const struct got_error *
933 load_commit(int want_meta, struct got_object_idset *idset,
934 struct got_object_idset *idset_exclude,
935 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
936 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
937 got_pack_progress_cb progress_cb, void *progress_arg,
938 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
940 const struct got_error *err;
941 struct got_commit_object *commit;
943 if (got_object_idset_contains(idset, id) ||
944 got_object_idset_contains(idset_exclude, id))
947 if (loose_obj_only) {
949 err = search_packidx(&is_packed, id, repo);
952 if (is_packed && want_meta)
956 err = got_object_open_as_commit(&commit, repo, id);
960 err = got_pack_add_object(want_meta,
961 want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_COMMIT,
962 got_object_commit_get_committer_time(commit), seed,
963 loose_obj_only, repo,
964 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
968 err = got_pack_load_tree(want_meta, idset, idset_exclude,
969 got_object_commit_get_tree_id(commit),
970 "", got_object_commit_get_committer_time(commit), seed,
971 repo, loose_obj_only, ncolored, nfound, ntrees,
972 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
974 got_object_commit_close(commit);
978 static const struct got_error *
979 load_tag(int want_meta, struct got_object_idset *idset,
980 struct got_object_idset *idset_exclude,
981 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
982 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
983 got_pack_progress_cb progress_cb, void *progress_arg,
984 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
986 const struct got_error *err;
987 struct got_tag_object *tag = NULL;
989 if (got_object_idset_contains(idset, id) ||
990 got_object_idset_contains(idset_exclude, id))
993 if (loose_obj_only) {
995 err = search_packidx(&is_packed, id, repo);
998 if (is_packed && want_meta)
1002 err = got_object_open_as_tag(&tag, repo, id);
1006 err = got_pack_add_object(want_meta,
1007 want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_TAG,
1008 got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1009 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1013 switch (got_object_tag_get_object_type(tag)) {
1014 case GOT_OBJ_TYPE_COMMIT:
1015 err = load_commit(want_meta, idset, idset_exclude,
1016 got_object_tag_get_object_id(tag), repo, seed,
1017 loose_obj_only, ncolored, nfound, ntrees,
1018 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1020 case GOT_OBJ_TYPE_TREE:
1021 err = got_pack_load_tree(want_meta, idset, idset_exclude,
1022 got_object_tag_get_object_id(tag), "",
1023 got_object_tag_get_tagger_time(tag), seed, repo,
1024 loose_obj_only, ncolored, nfound, ntrees,
1025 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1032 got_object_tag_close(tag);
1036 const struct got_error *
1037 got_pack_paint_commit(struct got_object_qid *qid, intptr_t color)
1039 if (color < 0 || color >= COLOR_MAX)
1040 return got_error(GOT_ERR_RANGE);
1042 qid->data = (void *)color;
1046 const struct got_error *
1047 got_pack_queue_commit_id(struct got_object_id_queue *ids,
1048 struct got_object_id *id, intptr_t color, struct got_repository *repo)
1050 const struct got_error *err;
1051 struct got_object_qid *qid;
1053 err = got_object_qid_alloc(&qid, id);
1057 STAILQ_INSERT_TAIL(ids, qid, entry);
1058 return got_pack_paint_commit(qid, color);
1061 struct append_id_arg {
1062 struct got_object_id **array;
1064 struct got_object_idset *drop;
1065 struct got_object_idset *skip;
1068 static const struct got_error *
1069 append_id(struct got_object_id *id, void *data, void *arg)
1071 struct append_id_arg *a = arg;
1073 if (got_object_idset_contains(a->skip, id) ||
1074 got_object_idset_contains(a->drop, id))
1077 a->array[++a->idx] = got_object_id_dup(id);
1078 if (a->array[a->idx] == NULL)
1079 return got_error_from_errno("got_object_id_dup");
1084 static const struct got_error *
1085 queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1086 struct got_object_id_queue *ids, struct got_repository *repo)
1088 const struct got_error *err;
1089 struct got_tag_object *tag = NULL;
1092 err = got_object_get_type(&obj_type, repo, id);
1096 if (obj_type == GOT_OBJ_TYPE_TAG) {
1097 err = got_object_open_as_tag(&tag, repo, id);
1100 obj_type = got_object_tag_get_object_type(tag);
1101 id = got_object_tag_get_object_id(tag);
1104 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1105 err = got_pack_queue_commit_id(ids, id, color, repo);
1111 got_object_tag_close(tag);
1115 const struct got_error *
1116 got_pack_find_pack_for_commit_painting(struct got_packidx **best_packidx,
1117 struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1119 const struct got_error *err = NULL;
1120 struct got_pathlist_entry *pe;
1121 const char *best_packidx_path = NULL;
1123 int ncommits_max = 0;
1125 *best_packidx = NULL;
1128 * Find the largest pack which contains at least some of the
1129 * commits we are interested in.
1131 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1132 const char *path_packidx = pe->path;
1133 struct got_packidx *packidx;
1134 int nobj, idx, ncommits = 0;
1135 struct got_object_qid *qid;
1137 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1141 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1142 if (nobj <= nobj_max)
1145 STAILQ_FOREACH(qid, ids, entry) {
1146 idx = got_packidx_get_object_idx(packidx, &qid->id);
1150 if (ncommits > ncommits_max) {
1151 best_packidx_path = path_packidx;
1153 ncommits_max = ncommits;
1157 if (best_packidx_path && err == NULL) {
1158 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1165 static const struct got_error *
1166 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1167 struct got_object_id **head, int nhead,
1168 struct got_object_id **tail, int ntail,
1169 struct got_repository *repo,
1170 got_pack_progress_cb progress_cb, void *progress_arg,
1171 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1173 const struct got_error *err = NULL;
1174 struct got_object_id_queue ids;
1175 struct got_object_idset *keep, *drop, *skip = NULL;
1183 keep = got_object_idset_alloc();
1185 return got_error_from_errno("got_object_idset_alloc");
1187 drop = got_object_idset_alloc();
1189 err = got_error_from_errno("got_object_idset_alloc");
1193 skip = got_object_idset_alloc();
1195 err = got_error_from_errno("got_object_idset_alloc");
1199 for (i = 0; i < nhead; i++) {
1200 struct got_object_id *id = head[i];
1203 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1208 for (i = 0; i < ntail; i++) {
1209 struct got_object_id *id = tail[i];
1212 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1217 err = got_pack_paint_commits(ncolored, &ids, nhead + ntail,
1218 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1219 cancel_cb, cancel_arg);
1223 nkeep = got_object_idset_num_elements(keep);
1225 struct append_id_arg arg;
1226 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1227 if (arg.array == NULL) {
1228 err = got_error_from_errno("calloc");
1234 err = got_object_idset_for_each(keep, append_id, &arg);
1240 *nres = arg.idx + 1;
1243 got_object_idset_free(keep);
1244 got_object_idset_free(drop);
1246 got_object_idset_free(skip);
1247 got_object_id_queue_free(&ids);
1251 static const struct got_error *
1252 find_pack_for_enumeration(struct got_packidx **best_packidx,
1253 struct got_object_id **ids, int nids, struct got_repository *repo)
1255 const struct got_error *err = NULL;
1256 struct got_pathlist_entry *pe;
1257 const char *best_packidx_path = NULL;
1259 int ncommits_max = 0;
1261 *best_packidx = NULL;
1264 * Find the largest pack which contains at least some of the
1265 * commits and tags we are interested in.
1267 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1268 const char *path_packidx = pe->path;
1269 struct got_packidx *packidx;
1270 int nobj, i, idx, ncommits = 0;
1272 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1276 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1277 if (nobj <= nobj_max)
1280 for (i = 0; i < nids; i++) {
1281 idx = got_packidx_get_object_idx(packidx, ids[i]);
1285 if (ncommits > ncommits_max) {
1286 best_packidx_path = path_packidx;
1288 ncommits_max = ncommits;
1292 if (best_packidx_path && err == NULL) {
1293 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1300 static const struct got_error *
1301 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1302 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1303 struct got_object_id **ours, int nours, struct got_repository *repo,
1304 uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1305 void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1308 const struct got_error *err = NULL;
1309 struct got_object_id **ids = NULL;
1310 struct got_packidx *packidx = NULL;
1311 int i, nobj = 0, obj_type, found_all_objects = 0;
1312 struct got_object_idset *idset_exclude;
1314 idset_exclude = got_object_idset_alloc();
1315 if (idset_exclude == NULL)
1316 return got_error_from_errno("got_object_idset_alloc");
1322 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1323 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1327 err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
1331 err = got_pack_load_packed_object_ids(&found_all_objects,
1332 theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
1333 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1334 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1339 for (i = 0; i < ntheirs; i++) {
1340 struct got_object_id *id = theirs[i];
1343 err = got_object_get_type(&obj_type, repo, id);
1346 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1347 if (!found_all_objects) {
1348 err = load_commit(0, idset, idset_exclude,
1349 id, repo, seed, loose_obj_only,
1350 ncolored, nfound, ntrees,
1351 progress_cb, progress_arg, rl,
1352 cancel_cb, cancel_arg);
1356 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1357 err = load_tag(0, idset, idset_exclude, id, repo,
1358 seed, loose_obj_only, ncolored, nfound, ntrees,
1359 progress_cb, progress_arg, rl,
1360 cancel_cb, cancel_arg);
1366 found_all_objects = 0;
1367 err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
1371 err = got_pack_load_packed_object_ids(&found_all_objects, ids,
1372 nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
1373 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1374 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1379 if (!found_all_objects) {
1380 for (i = 0; i < nobj; i++) {
1381 err = load_commit(1, idset, idset_exclude, ids[i],
1382 repo, seed, loose_obj_only, ncolored, nfound,
1383 ntrees, progress_cb, progress_arg, rl,
1384 cancel_cb, cancel_arg);
1390 for (i = 0; i < nours; i++) {
1391 struct got_object_id *id = ours[i];
1392 struct got_pack_meta *m;
1395 m = got_object_idset_get(idset, id);
1397 err = got_object_get_type(&obj_type, repo, id);
1401 obj_type = m->obj_type;
1402 if (obj_type != GOT_OBJ_TYPE_TAG)
1404 err = load_tag(1, idset, idset_exclude, id, repo,
1405 seed, loose_obj_only, ncolored, nfound, ntrees,
1406 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1411 for (i = 0; i < nobj; i++) {
1415 got_object_idset_free(idset_exclude);
1419 static const struct got_error *
1420 hwrite(int fd, const void *buf, off_t len, SHA1_CTX *ctx)
1424 SHA1Update(ctx, buf, len);
1425 w = write(fd, buf, len);
1427 return got_error_from_errno("write");
1429 return got_error(GOT_ERR_IO);
1433 static const struct got_error *
1434 hcopy(FILE *fsrc, int fd_dst, off_t len, SHA1_CTX *ctx)
1436 unsigned char buf[65536];
1441 while (remain > 0) {
1442 size_t copylen = MIN(sizeof(buf), remain);
1443 n = fread(buf, 1, copylen, fsrc);
1445 return got_ferror(fsrc, GOT_ERR_IO);
1446 SHA1Update(ctx, buf, copylen);
1447 w = write(fd_dst, buf, copylen);
1449 return got_error_from_errno("write");
1450 else if (w != copylen)
1451 return got_error(GOT_ERR_IO);
1458 static const struct got_error *
1459 hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1460 int fd, off_t len, SHA1_CTX *ctx)
1464 if (src_offset + len > src_size)
1465 return got_error(GOT_ERR_RANGE);
1467 SHA1Update(ctx, src + src_offset, len);
1468 w = write(fd, src + src_offset, len);
1470 return got_error_from_errno("write");
1472 return got_error(GOT_ERR_IO);
1477 putbe32(char *b, uint32_t n)
1486 write_order_cmp(const void *pa, const void *pb)
1488 struct got_pack_meta *a, *b, *ahd, *bhd;
1490 a = *(struct got_pack_meta **)pa;
1491 b = *(struct got_pack_meta **)pb;
1492 ahd = (a->head == NULL) ? a : a->head;
1493 bhd = (b->head == NULL) ? b : b->head;
1494 if (bhd->mtime < ahd->mtime)
1496 if (bhd->mtime > ahd->mtime)
1502 if (a->nchain != b->nchain)
1503 return a->nchain - b->nchain;
1504 if (a->mtime < b->mtime)
1506 if (a->mtime > b->mtime)
1508 return got_object_id_cmp(&a->id, &b->id);
1512 reuse_write_order_cmp(const void *pa, const void *pb)
1514 struct got_pack_meta *a, *b;
1516 a = *(struct got_pack_meta **)pa;
1517 b = *(struct got_pack_meta **)pb;
1519 if (a->reused_delta_offset < b->reused_delta_offset)
1521 if (a->reused_delta_offset > b->reused_delta_offset)
1526 static const struct got_error *
1527 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1533 hdr[0] = obj_type << 4;
1534 hdr[0] |= len & 0xf;
1536 for (i = 1; len != 0; i++){
1538 return got_error(GOT_ERR_NO_SPACE);
1539 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1540 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1541 len >>= GOT_DELTA_SIZE_SHIFT;
1549 packoff(char *hdr, off_t off)
1554 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1555 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1556 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1557 GOT_DELTA_SIZE_MORE;
1562 hdr[j++] = rbuf[--i];
1566 static const struct got_error *
1567 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, int packfd,
1568 struct got_pack_meta *m)
1570 const struct got_error *err;
1574 if (m->prev->off != 0) {
1575 err = packhdr(&nh, buf, sizeof(buf),
1576 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1579 nh += packoff(buf + nh, m->off - m->prev->off);
1580 err = hwrite(packfd, buf, nh, ctx);
1583 *packfile_size += nh;
1585 err = packhdr(&nh, buf, sizeof(buf),
1586 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1589 err = hwrite(packfd, buf, nh, ctx);
1592 *packfile_size += nh;
1593 err = hwrite(packfd, m->prev->id.sha1,
1594 sizeof(m->prev->id.sha1), ctx);
1597 *packfile_size += sizeof(m->prev->id.sha1);
1603 static const struct got_error *
1604 write_packed_object(off_t *packfile_size, int packfd,
1605 FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1606 struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
1607 struct got_repository *repo)
1609 const struct got_error *err = NULL;
1610 struct got_deflate_checksum csum;
1613 struct got_raw_object *raw = NULL;
1616 csum.output_sha1 = ctx;
1617 csum.output_crc = NULL;
1619 m->off = *packfile_size;
1620 if (m->delta_len == 0) {
1621 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1624 err = packhdr(&nh, buf, sizeof(buf),
1625 m->obj_type, raw->size);
1628 err = hwrite(packfd, buf, nh, ctx);
1631 *packfile_size += nh;
1632 if (raw->f == NULL) {
1633 err = got_deflate_to_fd_mmap(&outlen,
1634 raw->data + raw->hdrlen, 0, raw->size,
1639 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1641 err = got_error_from_errno("fseeko");
1644 err = got_deflate_to_fd(&outlen, raw->f,
1645 raw->size, packfd, &csum);
1649 *packfile_size += outlen;
1650 got_object_raw_close(raw);
1652 } else if (m->delta_buf) {
1653 err = deltahdr(packfile_size, ctx, packfd, m);
1656 err = hwrite(packfd, m->delta_buf,
1657 m->delta_compressed_len, ctx);
1660 *packfile_size += m->delta_compressed_len;
1662 m->delta_buf = NULL;
1663 } else if (delta_cache_map) {
1664 err = deltahdr(packfile_size, ctx, packfd, m);
1667 err = hcopy_mmap(delta_cache_map, m->delta_offset,
1668 delta_cache_size, packfd, m->delta_compressed_len,
1672 *packfile_size += m->delta_compressed_len;
1674 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1676 err = got_error_from_errno("fseeko");
1679 err = deltahdr(packfile_size, ctx, packfd, m);
1682 err = hcopy(delta_cache, packfd,
1683 m->delta_compressed_len, ctx);
1686 *packfile_size += m->delta_compressed_len;
1690 got_object_raw_close(raw);
1694 static const struct got_error *
1695 genpack(uint8_t *pack_sha1, int packfd, FILE *delta_cache,
1696 struct got_pack_meta **deltify, int ndeltify,
1697 struct got_pack_meta **reuse, int nreuse,
1698 int ncolored, int nfound, int ntrees, int nours,
1699 struct got_repository *repo,
1700 got_pack_progress_cb progress_cb, void *progress_arg,
1701 struct got_ratelimit *rl,
1702 got_cancel_cb cancel_cb, void *cancel_arg)
1704 const struct got_error *err = NULL;
1707 struct got_pack_meta *m;
1710 off_t packfile_size = 0;
1712 int delta_cache_fd = -1;
1713 uint8_t *delta_cache_map = NULL;
1714 size_t delta_cache_size = 0;
1718 #ifndef GOT_PACK_NO_MMAP
1719 delta_cache_fd = dup(fileno(delta_cache));
1720 if (delta_cache_fd != -1) {
1722 if (fstat(delta_cache_fd, &sb) == -1) {
1723 err = got_error_from_errno("fstat");
1726 if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1727 delta_cache_map = mmap(NULL, sb.st_size,
1728 PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1729 if (delta_cache_map == MAP_FAILED) {
1730 if (errno != ENOMEM) {
1731 err = got_error_from_errno("mmap");
1734 delta_cache_map = NULL; /* fallback on stdio */
1736 delta_cache_size = (size_t)sb.st_size;
1740 err = hwrite(packfd, "PACK", 4, &ctx);
1743 putbe32(buf, GOT_PACKFILE_VERSION);
1744 err = hwrite(packfd, buf, 4, &ctx);
1747 putbe32(buf, ndeltify + nreuse);
1748 err = hwrite(packfd, buf, 4, &ctx);
1752 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1754 for (i = 0; i < ndeltify; i++) {
1755 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1756 ncolored, nfound, ntrees, packfile_size, nours,
1757 ndeltify + nreuse, ndeltify + nreuse, i);
1761 err = write_packed_object(&packfile_size, packfd,
1762 delta_cache, delta_cache_map, delta_cache_size,
1763 m, &outfd, &ctx, repo);
1768 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1769 reuse_write_order_cmp);
1770 for (i = 0; i < nreuse; i++) {
1771 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1772 ncolored, nfound, ntrees, packfile_size, nours,
1773 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1777 err = write_packed_object(&packfile_size, packfd,
1778 delta_cache, delta_cache_map, delta_cache_size,
1779 m, &outfd, &ctx, repo);
1784 SHA1Final(pack_sha1, &ctx);
1785 w = write(packfd, pack_sha1, SHA1_DIGEST_LENGTH);
1787 err = got_error_from_errno("write");
1788 else if (w != SHA1_DIGEST_LENGTH)
1789 err = got_error(GOT_ERR_IO);
1792 packfile_size += SHA1_DIGEST_LENGTH;
1793 packfile_size += sizeof(struct got_packfile_hdr);
1795 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1796 packfile_size, nours, ndeltify + nreuse,
1797 ndeltify + nreuse, ndeltify + nreuse);
1802 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1803 err = got_error_from_errno("close");
1804 if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1805 err = got_error_from_errno("munmap");
1806 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1807 err = got_error_from_errno("close");
1811 static const struct got_error *
1812 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1814 struct got_pack_meta *m = data;
1815 struct got_pack_metavec *v = arg;
1817 if (m->reused_delta_offset != 0)
1820 return got_pack_add_meta(m, v);
1823 const struct got_error *
1824 got_pack_create(uint8_t *packsha1, int packfd, FILE *delta_cache,
1825 struct got_object_id **theirs, int ntheirs,
1826 struct got_object_id **ours, int nours,
1827 struct got_repository *repo, int loose_obj_only, int allow_empty,
1828 got_pack_progress_cb progress_cb, void *progress_arg,
1829 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1831 const struct got_error *err;
1832 int delta_cache_fd = -1;
1833 struct got_object_idset *idset;
1834 struct got_pack_metavec deltify, reuse;
1835 int ncolored = 0, nfound = 0, ntrees = 0;
1839 seed = arc4random();
1841 memset(&deltify, 0, sizeof(deltify));
1842 memset(&reuse, 0, sizeof(reuse));
1844 idset = got_object_idset_alloc();
1846 return got_error_from_errno("got_object_idset_alloc");
1848 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1849 ntheirs, ours, nours, repo, seed, loose_obj_only,
1850 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1855 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1856 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1861 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1862 err = got_error(GOT_ERR_CANNOT_PACK);
1866 delta_cache_fd = dup(fileno(delta_cache));
1867 if (delta_cache_fd == -1) {
1868 err = got_error_from_errno("dup");
1873 reuse.meta = calloc(reuse.metasz,
1874 sizeof(struct got_pack_meta *));
1875 if (reuse.meta == NULL) {
1876 err = got_error_from_errno("calloc");
1880 err = got_pack_search_deltas(&reuse, idset, delta_cache_fd,
1881 ncolored, nfound, ntrees, nours,
1882 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1886 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1887 err = got_error_from_errno("fseeko");
1891 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1893 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1894 if (deltify.meta == NULL) {
1895 err = got_error_from_errno("calloc");
1898 deltify.metasz = ndeltify;
1900 err = got_object_idset_for_each(idset, add_meta_idset_cb,
1904 if (deltify.nmeta > 0) {
1905 err = pick_deltas(deltify.meta, deltify.nmeta,
1906 ncolored, nfound, ntrees, nours, reuse.nmeta,
1907 delta_cache, repo, progress_cb, progress_arg, rl,
1908 cancel_cb, cancel_arg);
1914 if (fflush(delta_cache) == EOF) {
1915 err = got_error_from_errno("fflush");
1918 err = genpack(packsha1, packfd, delta_cache, deltify.meta,
1919 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1920 nours, repo, progress_cb, progress_arg, rl,
1921 cancel_cb, cancel_arg);
1925 free_nmeta(deltify.meta, deltify.nmeta);
1926 free_nmeta(reuse.meta, reuse.nmeta);
1927 got_object_idset_free(idset);
1928 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1929 err = got_error_from_errno("close");