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 "got_compat.h"
20 #include <sys/types.h>
21 #include <sys/queue.h>
39 #include "got_error.h"
40 #include "got_cancel.h"
41 #include "got_object.h"
43 #include "got_reference.h"
44 #include "got_repository_admin.h"
46 #include "got_lib_deltify.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_hash.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"
58 #include "got_lib_poll.h"
60 #include "murmurhash2.h"
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 static const struct got_error *
75 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
76 const char *path, int obj_type, time_t mtime, uint32_t seed)
78 struct got_pack_meta *m;
82 m = calloc(1, sizeof(*m));
84 return got_error_from_errno("calloc");
86 memcpy(&m->id, id, sizeof(m->id));
88 m->path_hash = murmurhash2(path, strlen(path), seed);
89 m->obj_type = obj_type;
96 clear_meta(struct got_pack_meta *meta)
101 free(meta->delta_buf);
102 meta->delta_buf = NULL;
103 free(meta->base_obj_id);
104 meta->base_obj_id = NULL;
105 meta->reused_delta_offset = 0;
109 free_nmeta(struct got_pack_meta **meta, int nmeta)
113 for (i = 0; i < nmeta; i++)
119 delta_order_cmp(const void *pa, const void *pb)
121 struct got_pack_meta *a, *b;
123 a = *(struct got_pack_meta **)pa;
124 b = *(struct got_pack_meta **)pb;
126 if (a->obj_type != b->obj_type)
127 return a->obj_type - b->obj_type;
128 if (a->path_hash < b->path_hash)
130 if (a->path_hash > b->path_hash)
132 if (a->mtime < b->mtime)
134 if (a->mtime > b->mtime)
136 return got_object_id_cmp(&a->id, &b->id);
140 delta_size(struct got_delta_instruction *deltas, int ndeltas)
144 for (i = 0; i < ndeltas; i++) {
146 size += GOT_DELTA_SIZE_SHIFT;
148 size += deltas[i].len + 1;
153 static const struct got_error *
154 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
158 if (*len + nseg >= *sz) {
159 while (*len + nseg >= *sz)
161 n = realloc(*p, *sz);
163 return got_error_from_errno("realloc");
166 memcpy(*p + *len, seg, nseg);
171 static const struct got_error *
172 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
173 struct got_delta_instruction *deltas, int ndeltas,
174 off_t delta_size, off_t base_size)
176 const struct got_error *err;
177 unsigned char buf[16], *bp;
179 size_t len = 0, compressed_len;
180 off_t bufsize = delta_size;
182 struct got_delta_instruction *d;
185 delta_buf = malloc(bufsize);
186 if (delta_buf == NULL)
187 return got_error_from_errno("malloc");
189 /* base object size */
190 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
191 n = base_size >> GOT_DELTA_SIZE_SHIFT;
192 for (i = 1; n > 0; i++) {
193 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
194 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
195 n >>= GOT_DELTA_SIZE_SHIFT;
197 err = append(&delta_buf, &len, &bufsize, buf, i);
201 /* target object size */
202 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
203 n = o->size >> GOT_DELTA_SIZE_SHIFT;
204 for (i = 1; n > 0; i++) {
205 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
206 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
207 n >>= GOT_DELTA_SIZE_SHIFT;
209 err = append(&delta_buf, &len, &bufsize, buf, i);
213 for (j = 0; j < ndeltas; j++) {
218 buf[0] = GOT_DELTA_BASE_COPY;
219 for (i = 0; i < 4; i++) {
220 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
229 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
230 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
231 for (i = 0; i < 3 && n > 0; i++) {
232 buf[0] |= 1 << (i + 4);
237 err = append(&delta_buf, &len, &bufsize,
241 } else if (o->f == NULL) {
243 while (n != d->len) {
244 buf[0] = (d->len - n < 127) ? d->len - n : 127;
245 err = append(&delta_buf, &len, &bufsize,
249 err = append(&delta_buf, &len, &bufsize,
250 o->data + o->hdrlen + d->offset + n,
259 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
260 err = got_error_from_errno("fseeko");
264 while (n != d->len) {
265 buf[0] = (d->len - n < 127) ? d->len - n : 127;
266 err = append(&delta_buf, &len, &bufsize,
270 r = fread(content, 1, buf[0], o->f);
272 err = got_ferror(o->f, GOT_ERR_IO);
275 err = append(&delta_buf, &len, &bufsize,
284 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
285 NULL, NULL, delta_buf, 0, len);
290 m->delta_compressed_len = compressed_len;
296 static const struct got_error *
297 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
298 struct got_delta_instruction *deltas, int ndeltas,
299 off_t base_size, FILE *f)
301 const struct got_error *err;
302 unsigned char buf[16], *bp;
305 struct got_deflate_buf zb;
306 struct got_delta_instruction *d;
307 off_t delta_len = 0, compressed_len = 0;
309 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
313 /* base object size */
314 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
315 n = base_size >> GOT_DELTA_SIZE_SHIFT;
316 for (i = 1; n > 0; i++) {
317 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
318 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
319 n >>= GOT_DELTA_SIZE_SHIFT;
322 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
328 /* target object size */
329 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
330 n = o->size >> GOT_DELTA_SIZE_SHIFT;
331 for (i = 1; n > 0; i++) {
332 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
333 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
334 n >>= GOT_DELTA_SIZE_SHIFT;
337 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
343 for (j = 0; j < ndeltas; j++) {
348 buf[0] = GOT_DELTA_BASE_COPY;
349 for (i = 0; i < 4; i++) {
350 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
358 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
359 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
360 for (i = 0; i < 3 && n > 0; i++) {
361 buf[0] |= 1 << (i + 4);
366 err = got_deflate_append_to_file_mmap(&zb,
367 &compressed_len, buf, 0, bp - buf, f, NULL);
370 delta_len += (bp - buf);
371 } else if (o->f == NULL) {
373 while (n != d->len) {
374 buf[0] = (d->len - n < 127) ? d->len - n : 127;
375 err = got_deflate_append_to_file_mmap(&zb,
376 &compressed_len, buf, 0, 1, f, NULL);
380 err = got_deflate_append_to_file_mmap(&zb,
382 o->data + o->hdrlen + d->offset + n, 0,
392 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
393 err = got_error_from_errno("fseeko");
397 while (n != d->len) {
398 buf[0] = (d->len - n < 127) ? d->len - n : 127;
399 err = got_deflate_append_to_file_mmap(&zb,
400 &compressed_len, buf, 0, 1, f, NULL);
404 r = fread(content, 1, buf[0], o->f);
406 err = got_ferror(o->f, GOT_ERR_IO);
409 err = got_deflate_append_to_file_mmap(&zb,
410 &compressed_len, content, 0, buf[0], f,
420 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
425 if (compressed_len != ftello(f) - m->delta_offset) {
426 err = got_error(GOT_ERR_COMPRESSION);
430 m->delta_len = delta_len;
431 m->delta_compressed_len = compressed_len;
433 got_deflate_end(&zb);
437 const struct got_error *
438 got_pack_report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
439 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
440 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
443 const struct got_error *err;
446 if (progress_cb == NULL)
449 err = got_ratelimit_check(&elapsed, rl);
453 return progress_cb(progress_arg, ncolored, nfound, ntrees,
454 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
457 const struct got_error *
458 got_pack_add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
460 if (v->nmeta == v->metasz){
461 size_t newsize = 2 * v->metasz;
462 struct got_pack_meta **new;
463 new = reallocarray(v->meta, newsize, sizeof(*new));
465 return got_error_from_errno("reallocarray");
470 v->meta[v->nmeta++] = m;
474 const struct got_error *
475 got_pack_find_pack_for_reuse(struct got_packidx **best_packidx,
476 struct got_repository *repo)
478 const struct got_error *err = NULL;
479 struct got_pathlist_entry *pe;
480 const char *best_packidx_path = NULL;
483 *best_packidx = NULL;
485 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
486 const char *path_packidx = pe->path;
487 struct got_packidx *packidx;
490 err = got_repo_get_packidx(&packidx, path_packidx, repo);
494 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
495 if (nobj > nobj_max) {
496 best_packidx_path = path_packidx;
501 if (best_packidx_path) {
502 err = got_repo_get_packidx(best_packidx, best_packidx_path,
509 const struct got_error *
510 got_pack_cache_pack_for_packidx(struct got_pack **pack,
511 struct got_packidx *packidx, struct got_repository *repo)
513 const struct got_error *err;
514 char *path_packfile = NULL;
516 err = got_packidx_get_packfile_path(&path_packfile,
517 packidx->path_packidx);
521 *pack = got_repo_get_cached_pack(repo, path_packfile);
523 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
532 static const struct got_error *
533 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
534 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
535 struct got_repository *repo,
536 got_pack_progress_cb progress_cb, void *progress_arg,
537 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
539 const struct got_error *err = NULL;
540 struct got_pack_meta *m = NULL, *base = NULL;
541 struct got_raw_object *raw = NULL, *base_raw = NULL;
542 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
543 int i, j, ndeltas, best_ndeltas;
544 off_t size, best_size;
545 const int max_base_candidates = 3;
546 size_t delta_memsize = 0;
547 const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
551 delta_seed = arc4random();
553 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
554 for (i = 0; i < nmeta; i++) {
556 err = (*cancel_cb)(cancel_arg);
560 err = got_pack_report_progress(progress_cb, progress_arg, rl,
561 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
567 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
568 m->obj_type == GOT_OBJ_TYPE_TAG)
571 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
576 if (raw->f == NULL) {
577 err = got_deltify_init_mem(&m->dtab, raw->data,
578 raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
580 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
581 raw->size + raw->hdrlen, delta_seed);
586 if (i > max_base_candidates) {
587 struct got_pack_meta *n = NULL;
588 n = meta[i - (max_base_candidates + 1)];
589 got_deltify_free(n->dtab);
593 best_size = raw->size;
595 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
597 err = (*cancel_cb)(cancel_arg);
602 /* long chains make unpacking slow, avoid such bases */
603 if (base->nchain >= 128 ||
604 base->obj_type != m->obj_type)
607 err = got_object_raw_open(&base_raw, &outfd, repo,
612 if (raw->f == NULL && base_raw->f == NULL) {
613 err = got_deltify_mem_mem(&deltas, &ndeltas,
614 raw->data, raw->hdrlen,
615 raw->size + raw->hdrlen, delta_seed,
616 base->dtab, base_raw->data,
618 base_raw->size + base_raw->hdrlen);
619 } else if (raw->f == NULL) {
620 err = got_deltify_mem_file(&deltas, &ndeltas,
621 raw->data, raw->hdrlen,
622 raw->size + raw->hdrlen, delta_seed,
623 base->dtab, base_raw->f,
625 base_raw->size + base_raw->hdrlen);
626 } else if (base_raw->f == NULL) {
627 err = got_deltify_file_mem(&deltas, &ndeltas,
629 raw->size + raw->hdrlen, delta_seed,
630 base->dtab, base_raw->data,
632 base_raw->size + base_raw->hdrlen);
634 err = got_deltify(&deltas, &ndeltas,
636 raw->size + raw->hdrlen, delta_seed,
637 base->dtab, base_raw->f, base_raw->hdrlen,
638 base_raw->size + base_raw->hdrlen);
640 got_object_raw_close(base_raw);
645 size = delta_size(deltas, ndeltas);
646 if (size + 32 < best_size){
648 * if we already picked a best delta,
653 best_deltas = deltas;
654 best_ndeltas = ndeltas;
656 m->nchain = base->nchain + 1;
658 m->head = base->head;
668 if (best_ndeltas > 0) {
669 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
670 delta_memsize + best_size <= max_delta_memsize) {
671 delta_memsize += best_size;
672 err = encode_delta_in_mem(m, raw, best_deltas,
673 best_ndeltas, best_size, m->prev->size);
675 m->delta_offset = ftello(delta_cache);
676 err = encode_delta(m, raw, best_deltas,
677 best_ndeltas, m->prev->size, delta_cache);
686 got_object_raw_close(raw);
690 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
691 got_deltify_free(meta[i]->dtab);
692 meta[i]->dtab = NULL;
695 got_object_raw_close(raw);
697 got_object_raw_close(base_raw);
698 if (outfd != -1 && close(outfd) == -1 && err == NULL)
699 err = got_error_from_errno("close");
705 static const struct got_error *
706 search_packidx(int *found, struct got_object_id *id,
707 struct got_repository *repo)
709 const struct got_error *err = NULL;
710 struct got_packidx *packidx = NULL;
715 err = got_repo_search_packidx(&packidx, &idx, repo, id);
717 *found = 1; /* object is already packed */
718 else if (err->code == GOT_ERR_NO_OBJ)
723 const struct got_error *
724 got_pack_add_object(int want_meta, struct got_object_idset *idset,
725 struct got_object_id *id, const char *path, int obj_type,
726 time_t mtime, uint32_t seed, int loose_obj_only,
727 struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
728 got_pack_progress_cb progress_cb, void *progress_arg,
729 struct got_ratelimit *rl)
731 const struct got_error *err;
732 struct got_pack_meta *m = NULL;
734 if (loose_obj_only) {
736 err = search_packidx(&is_packed, id, repo);
739 if (is_packed && want_meta)
744 err = alloc_meta(&m, id, path, obj_type, mtime, seed);
749 err = got_pack_report_progress(progress_cb, progress_arg, rl,
750 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
758 err = got_object_idset_add(idset, id, m);
766 const struct got_error *
767 got_pack_load_tree_entries(struct got_object_id_queue *ids, int want_meta,
768 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
769 struct got_tree_object *tree,
770 const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
771 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
772 got_pack_progress_cb progress_cb, void *progress_arg,
773 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
775 const struct got_error *err;
780 err = got_pack_report_progress(progress_cb, progress_arg, rl,
781 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
785 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
786 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
787 struct got_object_id *id = got_tree_entry_get_id(e);
788 mode_t mode = got_tree_entry_get_mode(e);
791 err = (*cancel_cb)(cancel_arg);
796 if (got_object_tree_entry_is_submodule(e) ||
797 got_object_idset_contains(idset, id) ||
798 got_object_idset_contains(idset_exclude, id))
802 * If got-read-pack is crawling trees for us then
803 * we are only here to collect blob IDs.
805 if (ids == NULL && S_ISDIR(mode))
808 if (asprintf(&p, "%s%s%s", dpath,
809 got_path_is_root_dir(dpath) ? "" : "/",
810 got_tree_entry_get_name(e)) == -1) {
811 err = got_error_from_errno("asprintf");
816 struct got_object_qid *qid;
817 err = got_object_qid_alloc(&qid, id);
822 STAILQ_INSERT_TAIL(ids, qid, entry);
823 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
824 err = got_pack_add_object(want_meta,
825 want_meta ? idset : idset_exclude, id, p,
826 GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
827 repo, ncolored, nfound, ntrees,
828 progress_cb, progress_arg, rl);
843 const struct got_error *
844 got_pack_load_tree(int want_meta, struct got_object_idset *idset,
845 struct got_object_idset *idset_exclude,
846 struct got_object_id *tree_id, const char *dpath, time_t mtime,
847 uint32_t seed, struct got_repository *repo, int loose_obj_only,
848 int *ncolored, int *nfound, int *ntrees,
849 got_pack_progress_cb progress_cb, void *progress_arg,
850 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
852 const struct got_error *err = NULL;
853 struct got_object_id_queue tree_ids;
854 struct got_object_qid *qid;
855 struct got_tree_object *tree = NULL;
857 if (got_object_idset_contains(idset, tree_id) ||
858 got_object_idset_contains(idset_exclude, tree_id))
861 err = got_object_qid_alloc(&qid, tree_id);
864 qid->data = strdup(dpath);
865 if (qid->data == NULL) {
866 err = got_error_from_errno("strdup");
867 got_object_qid_free(qid);
871 STAILQ_INIT(&tree_ids);
872 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
874 while (!STAILQ_EMPTY(&tree_ids)) {
877 err = (*cancel_cb)(cancel_arg);
882 qid = STAILQ_FIRST(&tree_ids);
883 STAILQ_REMOVE_HEAD(&tree_ids, entry);
886 if (got_object_idset_contains(idset, &qid->id) ||
887 got_object_idset_contains(idset_exclude, &qid->id)) {
889 got_object_qid_free(qid);
893 err = got_pack_add_object(want_meta,
894 want_meta ? idset : idset_exclude,
895 &qid->id, path, GOT_OBJ_TYPE_TREE,
896 mtime, seed, loose_obj_only, repo,
897 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
900 got_object_qid_free(qid);
904 err = got_object_open_as_tree(&tree, repo, &qid->id);
907 got_object_qid_free(qid);
911 err = got_pack_load_tree_entries(&tree_ids, want_meta, idset,
912 idset_exclude, tree, path, mtime, seed, repo,
913 loose_obj_only, ncolored, nfound, ntrees,
914 progress_cb, progress_arg, rl,
915 cancel_cb, cancel_arg);
917 got_object_qid_free(qid);
921 got_object_tree_close(tree);
925 STAILQ_FOREACH(qid, &tree_ids, entry)
927 got_object_id_queue_free(&tree_ids);
929 got_object_tree_close(tree);
933 static const struct got_error *
934 load_commit(int want_meta, struct got_object_idset *idset,
935 struct got_object_idset *idset_exclude,
936 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
937 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
938 got_pack_progress_cb progress_cb, void *progress_arg,
939 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
941 const struct got_error *err;
942 struct got_commit_object *commit;
944 if (got_object_idset_contains(idset, id) ||
945 got_object_idset_contains(idset_exclude, id))
948 if (loose_obj_only) {
950 err = search_packidx(&is_packed, id, repo);
953 if (is_packed && want_meta)
957 err = got_object_open_as_commit(&commit, repo, id);
961 err = got_pack_add_object(want_meta,
962 want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_COMMIT,
963 got_object_commit_get_committer_time(commit), seed,
964 loose_obj_only, repo,
965 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
969 err = got_pack_load_tree(want_meta, idset, idset_exclude,
970 got_object_commit_get_tree_id(commit),
971 "", got_object_commit_get_committer_time(commit), seed,
972 repo, loose_obj_only, ncolored, nfound, ntrees,
973 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
975 got_object_commit_close(commit);
979 static const struct got_error *
980 load_tag(int want_meta, struct got_object_idset *idset,
981 struct got_object_idset *idset_exclude,
982 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
983 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
984 got_pack_progress_cb progress_cb, void *progress_arg,
985 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
987 const struct got_error *err;
988 struct got_tag_object *tag = NULL;
990 if (got_object_idset_contains(idset, id) ||
991 got_object_idset_contains(idset_exclude, id))
994 if (loose_obj_only) {
996 err = search_packidx(&is_packed, id, repo);
999 if (is_packed && want_meta)
1003 err = got_object_open_as_tag(&tag, repo, id);
1007 err = got_pack_add_object(want_meta,
1008 want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_TAG,
1009 got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1010 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1014 switch (got_object_tag_get_object_type(tag)) {
1015 case GOT_OBJ_TYPE_COMMIT:
1016 err = load_commit(want_meta, idset, idset_exclude,
1017 got_object_tag_get_object_id(tag), repo, seed,
1018 loose_obj_only, ncolored, nfound, ntrees,
1019 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1021 case GOT_OBJ_TYPE_TREE:
1022 err = got_pack_load_tree(want_meta, idset, idset_exclude,
1023 got_object_tag_get_object_id(tag), "",
1024 got_object_tag_get_tagger_time(tag), seed, repo,
1025 loose_obj_only, ncolored, nfound, ntrees,
1026 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1033 got_object_tag_close(tag);
1037 const struct got_error *
1038 got_pack_paint_commit(struct got_object_qid *qid, intptr_t color)
1040 if (color < 0 || color >= COLOR_MAX)
1041 return got_error(GOT_ERR_RANGE);
1043 qid->data = (void *)color;
1047 const struct got_error *
1048 got_pack_queue_commit_id(struct got_object_id_queue *ids,
1049 struct got_object_id *id, intptr_t color, struct got_repository *repo)
1051 const struct got_error *err;
1052 struct got_object_qid *qid;
1054 err = got_object_qid_alloc(&qid, id);
1058 STAILQ_INSERT_TAIL(ids, qid, entry);
1059 return got_pack_paint_commit(qid, color);
1062 struct append_id_arg {
1063 struct got_object_id **array;
1065 struct got_object_idset *drop;
1066 struct got_object_idset *skip;
1069 static const struct got_error *
1070 append_id(struct got_object_id *id, void *data, void *arg)
1072 struct append_id_arg *a = arg;
1074 if (got_object_idset_contains(a->skip, id) ||
1075 got_object_idset_contains(a->drop, id))
1078 a->array[++a->idx] = got_object_id_dup(id);
1079 if (a->array[a->idx] == NULL)
1080 return got_error_from_errno("got_object_id_dup");
1085 static const struct got_error *
1086 queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1087 struct got_object_id_queue *ids, struct got_repository *repo)
1089 const struct got_error *err;
1090 struct got_tag_object *tag = NULL;
1093 err = got_object_get_type(&obj_type, repo, id);
1097 if (obj_type == GOT_OBJ_TYPE_TAG) {
1098 err = got_object_open_as_tag(&tag, repo, id);
1101 obj_type = got_object_tag_get_object_type(tag);
1102 id = got_object_tag_get_object_id(tag);
1105 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1106 err = got_pack_queue_commit_id(ids, id, color, repo);
1112 got_object_tag_close(tag);
1116 const struct got_error *
1117 got_pack_find_pack_for_commit_painting(struct got_packidx **best_packidx,
1118 struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1120 const struct got_error *err = NULL;
1121 struct got_pathlist_entry *pe;
1122 const char *best_packidx_path = NULL;
1124 int ncommits_max = 0;
1126 *best_packidx = NULL;
1129 * Find the largest pack which contains at least some of the
1130 * commits we are interested in.
1132 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1133 const char *path_packidx = pe->path;
1134 struct got_packidx *packidx;
1135 int nobj, idx, ncommits = 0;
1136 struct got_object_qid *qid;
1138 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1142 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1143 if (nobj <= nobj_max)
1146 STAILQ_FOREACH(qid, ids, entry) {
1147 idx = got_packidx_get_object_idx(packidx, &qid->id);
1151 if (ncommits > ncommits_max) {
1152 best_packidx_path = path_packidx;
1154 ncommits_max = ncommits;
1158 if (best_packidx_path && err == NULL) {
1159 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1166 static const struct got_error *
1167 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1168 struct got_object_id **head, int nhead,
1169 struct got_object_id **tail, int ntail,
1170 struct got_repository *repo,
1171 got_pack_progress_cb progress_cb, void *progress_arg,
1172 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1174 const struct got_error *err = NULL;
1175 struct got_object_id_queue ids;
1176 struct got_object_idset *keep, *drop, *skip = NULL;
1184 keep = got_object_idset_alloc();
1186 return got_error_from_errno("got_object_idset_alloc");
1188 drop = got_object_idset_alloc();
1190 err = got_error_from_errno("got_object_idset_alloc");
1194 skip = got_object_idset_alloc();
1196 err = got_error_from_errno("got_object_idset_alloc");
1200 for (i = 0; i < nhead; i++) {
1201 struct got_object_id *id = head[i];
1204 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1209 for (i = 0; i < ntail; i++) {
1210 struct got_object_id *id = tail[i];
1213 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1218 err = got_pack_paint_commits(ncolored, &ids, nhead + ntail,
1219 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1220 cancel_cb, cancel_arg);
1224 nkeep = got_object_idset_num_elements(keep);
1226 struct append_id_arg arg;
1227 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1228 if (arg.array == NULL) {
1229 err = got_error_from_errno("calloc");
1235 err = got_object_idset_for_each(keep, append_id, &arg);
1241 *nres = arg.idx + 1;
1244 got_object_idset_free(keep);
1245 got_object_idset_free(drop);
1247 got_object_idset_free(skip);
1248 got_object_id_queue_free(&ids);
1252 static const struct got_error *
1253 find_pack_for_enumeration(struct got_packidx **best_packidx,
1254 struct got_object_id **ids, int nids, struct got_repository *repo)
1256 const struct got_error *err = NULL;
1257 struct got_pathlist_entry *pe;
1258 const char *best_packidx_path = NULL;
1260 int ncommits_max = 0;
1262 *best_packidx = NULL;
1265 * Find the largest pack which contains at least some of the
1266 * commits and tags we are interested in.
1268 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1269 const char *path_packidx = pe->path;
1270 struct got_packidx *packidx;
1271 int nobj, i, idx, ncommits = 0;
1273 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1277 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1278 if (nobj <= nobj_max)
1281 for (i = 0; i < nids; i++) {
1282 idx = got_packidx_get_object_idx(packidx, ids[i]);
1286 if (ncommits > ncommits_max) {
1287 best_packidx_path = path_packidx;
1289 ncommits_max = ncommits;
1293 if (best_packidx_path && err == NULL) {
1294 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1301 static const struct got_error *
1302 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1303 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1304 struct got_object_id **ours, int nours, struct got_repository *repo,
1305 uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1306 void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1309 const struct got_error *err = NULL;
1310 struct got_object_id **ids = NULL;
1311 struct got_packidx *packidx = NULL;
1312 int i, nobj = 0, obj_type, found_all_objects = 0;
1313 struct got_object_idset *idset_exclude;
1315 idset_exclude = got_object_idset_alloc();
1316 if (idset_exclude == NULL)
1317 return got_error_from_errno("got_object_idset_alloc");
1323 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1324 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1328 err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
1332 err = got_pack_load_packed_object_ids(&found_all_objects,
1333 theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
1334 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1335 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1340 for (i = 0; i < ntheirs; i++) {
1341 struct got_object_id *id = theirs[i];
1344 err = got_object_get_type(&obj_type, repo, id);
1347 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1348 if (!found_all_objects) {
1349 err = load_commit(0, idset, idset_exclude,
1350 id, repo, seed, loose_obj_only,
1351 ncolored, nfound, ntrees,
1352 progress_cb, progress_arg, rl,
1353 cancel_cb, cancel_arg);
1357 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1358 err = load_tag(0, idset, idset_exclude, id, repo,
1359 seed, loose_obj_only, ncolored, nfound, ntrees,
1360 progress_cb, progress_arg, rl,
1361 cancel_cb, cancel_arg);
1367 found_all_objects = 0;
1368 err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
1372 err = got_pack_load_packed_object_ids(&found_all_objects, ids,
1373 nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
1374 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1375 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1380 if (!found_all_objects) {
1381 for (i = 0; i < nobj; i++) {
1382 err = load_commit(1, idset, idset_exclude, ids[i],
1383 repo, seed, loose_obj_only, ncolored, nfound,
1384 ntrees, progress_cb, progress_arg, rl,
1385 cancel_cb, cancel_arg);
1391 for (i = 0; i < nours; i++) {
1392 struct got_object_id *id = ours[i];
1393 struct got_pack_meta *m;
1396 m = got_object_idset_get(idset, id);
1398 err = got_object_get_type(&obj_type, repo, id);
1402 obj_type = m->obj_type;
1403 if (obj_type != GOT_OBJ_TYPE_TAG)
1405 err = load_tag(1, idset, idset_exclude, id, repo,
1406 seed, loose_obj_only, ncolored, nfound, ntrees,
1407 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1412 for (i = 0; i < nobj; i++) {
1416 got_object_idset_free(idset_exclude);
1420 static const struct got_error *
1421 hwrite(int fd, const void *buf, off_t len, struct got_hash *ctx)
1423 got_hash_update(ctx, buf, len);
1424 return got_poll_write_full(fd, buf, len);
1427 static const struct got_error *
1428 hcopy(FILE *fsrc, int fd_dst, off_t len, struct got_hash *ctx)
1430 const struct got_error *err;
1431 unsigned char buf[65536];
1435 while (remain > 0) {
1436 size_t copylen = MIN(sizeof(buf), remain);
1437 n = fread(buf, 1, copylen, fsrc);
1439 return got_ferror(fsrc, GOT_ERR_IO);
1440 got_hash_update(ctx, buf, copylen);
1441 err = got_poll_write_full(fd_dst, buf, copylen);
1450 static const struct got_error *
1451 hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1452 int fd, off_t len, struct got_hash *ctx)
1454 if (src_offset + len > src_size)
1455 return got_error(GOT_ERR_RANGE);
1457 got_hash_update(ctx, src + src_offset, len);
1458 return got_poll_write_full(fd, src + src_offset, len);
1462 putbe32(char *b, uint32_t n)
1471 write_order_cmp(const void *pa, const void *pb)
1473 struct got_pack_meta *a, *b, *ahd, *bhd;
1475 a = *(struct got_pack_meta **)pa;
1476 b = *(struct got_pack_meta **)pb;
1477 ahd = (a->head == NULL) ? a : a->head;
1478 bhd = (b->head == NULL) ? b : b->head;
1479 if (bhd->mtime < ahd->mtime)
1481 if (bhd->mtime > ahd->mtime)
1487 if (a->nchain != b->nchain)
1488 return a->nchain - b->nchain;
1489 if (a->mtime < b->mtime)
1491 if (a->mtime > b->mtime)
1493 return got_object_id_cmp(&a->id, &b->id);
1497 reuse_write_order_cmp(const void *pa, const void *pb)
1499 struct got_pack_meta *a, *b;
1501 a = *(struct got_pack_meta **)pa;
1502 b = *(struct got_pack_meta **)pb;
1504 if (a->reused_delta_offset < b->reused_delta_offset)
1506 if (a->reused_delta_offset > b->reused_delta_offset)
1511 static const struct got_error *
1512 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1518 hdr[0] = obj_type << 4;
1519 hdr[0] |= len & 0xf;
1521 for (i = 1; len != 0; i++){
1523 return got_error(GOT_ERR_NO_SPACE);
1524 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1525 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1526 len >>= GOT_DELTA_SIZE_SHIFT;
1534 packoff(char *hdr, off_t off)
1539 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1540 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1541 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1542 GOT_DELTA_SIZE_MORE;
1547 hdr[j++] = rbuf[--i];
1551 static const struct got_error *
1552 deltahdr(off_t *packfile_size, struct got_hash *ctx, int packfd,
1553 int force_refdelta, struct got_pack_meta *m)
1555 const struct got_error *err;
1559 if (m->prev->off != 0 && !force_refdelta) {
1560 err = packhdr(&nh, buf, sizeof(buf),
1561 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1564 nh += packoff(buf + nh, m->off - m->prev->off);
1565 err = hwrite(packfd, buf, nh, ctx);
1568 *packfile_size += nh;
1570 err = packhdr(&nh, buf, sizeof(buf),
1571 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1574 err = hwrite(packfd, buf, nh, ctx);
1577 *packfile_size += nh;
1578 err = hwrite(packfd, m->prev->id.sha1,
1579 sizeof(m->prev->id.sha1), ctx);
1582 *packfile_size += sizeof(m->prev->id.sha1);
1588 static const struct got_error *
1589 write_packed_object(off_t *packfile_size, int packfd,
1590 FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1591 struct got_pack_meta *m, int *outfd, struct got_hash *ctx,
1592 struct got_repository *repo, int force_refdelta)
1594 const struct got_error *err = NULL;
1595 struct got_deflate_checksum csum;
1598 struct got_raw_object *raw = NULL;
1599 off_t outlen, delta_offset;
1601 memset(&csum, 0, sizeof(csum));
1602 csum.output_ctx = ctx;
1604 if (m->reused_delta_offset)
1605 delta_offset = m->reused_delta_offset;
1607 delta_offset = m->delta_offset;
1609 m->off = *packfile_size;
1610 if (m->delta_len == 0) {
1611 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1614 err = packhdr(&nh, buf, sizeof(buf),
1615 m->obj_type, raw->size);
1618 err = hwrite(packfd, buf, nh, ctx);
1621 *packfile_size += nh;
1622 if (raw->f == NULL) {
1623 err = got_deflate_to_fd_mmap(&outlen,
1624 raw->data + raw->hdrlen, 0, raw->size,
1629 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1631 err = got_error_from_errno("fseeko");
1634 err = got_deflate_to_fd(&outlen, raw->f,
1635 raw->size, packfd, &csum);
1639 *packfile_size += outlen;
1640 got_object_raw_close(raw);
1642 } else if (m->delta_buf) {
1643 err = deltahdr(packfile_size, ctx, packfd, force_refdelta, m);
1646 err = hwrite(packfd, m->delta_buf,
1647 m->delta_compressed_len, ctx);
1650 *packfile_size += m->delta_compressed_len;
1652 m->delta_buf = NULL;
1653 } else if (delta_cache_map) {
1654 err = deltahdr(packfile_size, ctx, packfd, force_refdelta, m);
1657 err = hcopy_mmap(delta_cache_map, delta_offset,
1658 delta_cache_size, packfd, m->delta_compressed_len,
1662 *packfile_size += m->delta_compressed_len;
1664 if (fseeko(delta_cache, delta_offset, SEEK_SET) == -1) {
1665 err = got_error_from_errno("fseeko");
1668 err = deltahdr(packfile_size, ctx, packfd, force_refdelta, m);
1671 err = hcopy(delta_cache, packfd,
1672 m->delta_compressed_len, ctx);
1675 *packfile_size += m->delta_compressed_len;
1679 got_object_raw_close(raw);
1683 static const struct got_error *
1684 genpack(uint8_t *pack_sha1, int packfd, struct got_pack *reuse_pack,
1685 FILE *delta_cache, struct got_pack_meta **deltify, int ndeltify,
1686 struct got_pack_meta **reuse, int nreuse,
1687 int ncolored, int nfound, int ntrees, int nours,
1688 struct got_repository *repo, int force_refdelta,
1689 got_pack_progress_cb progress_cb, void *progress_arg,
1690 struct got_ratelimit *rl,
1691 got_cancel_cb cancel_cb, void *cancel_arg)
1693 const struct got_error *err = NULL;
1695 struct got_hash ctx;
1696 struct got_pack_meta *m;
1698 off_t packfile_size = 0;
1700 int delta_cache_fd = -1;
1701 uint8_t *delta_cache_map = NULL;
1702 size_t delta_cache_size = 0;
1703 FILE *packfile = NULL;
1705 got_hash_init(&ctx, GOT_HASH_SHA1);
1707 #ifndef GOT_PACK_NO_MMAP
1708 delta_cache_fd = dup(fileno(delta_cache));
1709 if (delta_cache_fd != -1) {
1711 if (fstat(delta_cache_fd, &sb) == -1) {
1712 err = got_error_from_errno("fstat");
1715 if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1716 delta_cache_map = mmap(NULL, sb.st_size,
1717 PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1718 if (delta_cache_map == MAP_FAILED) {
1719 if (errno != ENOMEM) {
1720 err = got_error_from_errno("mmap");
1723 delta_cache_map = NULL; /* fallback on stdio */
1725 delta_cache_size = (size_t)sb.st_size;
1729 err = hwrite(packfd, "PACK", 4, &ctx);
1732 putbe32(buf, GOT_PACKFILE_VERSION);
1733 err = hwrite(packfd, buf, 4, &ctx);
1736 putbe32(buf, ndeltify + nreuse);
1737 err = hwrite(packfd, buf, 4, &ctx);
1741 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1743 for (i = 0; i < ndeltify; i++) {
1744 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1745 ncolored, nfound, ntrees, packfile_size, nours,
1746 ndeltify + nreuse, ndeltify + nreuse, i);
1750 err = write_packed_object(&packfile_size, packfd,
1751 delta_cache, delta_cache_map, delta_cache_size,
1752 m, &outfd, &ctx, repo, force_refdelta);
1757 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1758 reuse_write_order_cmp);
1759 if (nreuse > 0 && reuse_pack->map == NULL) {
1760 int fd = dup(reuse_pack->fd);
1762 err = got_error_from_errno("dup");
1765 packfile = fdopen(fd, "r");
1766 if (packfile == NULL) {
1767 err = got_error_from_errno("fdopen");
1772 for (i = 0; i < nreuse; i++) {
1773 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1774 ncolored, nfound, ntrees, packfile_size, nours,
1775 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1779 err = write_packed_object(&packfile_size, packfd,
1780 packfile, reuse_pack->map, reuse_pack->filesize,
1781 m, &outfd, &ctx, repo, force_refdelta);
1786 got_hash_final(&ctx, pack_sha1);
1787 err = got_poll_write_full(packfd, pack_sha1, SHA1_DIGEST_LENGTH);
1790 packfile_size += SHA1_DIGEST_LENGTH;
1791 packfile_size += sizeof(struct got_packfile_hdr);
1793 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1794 packfile_size, nours, ndeltify + nreuse,
1795 ndeltify + nreuse, ndeltify + nreuse);
1800 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1801 err = got_error_from_errno("close");
1802 if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1803 err = got_error_from_errno("munmap");
1804 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1805 err = got_error_from_errno("close");
1806 if (packfile && fclose(packfile) == EOF && err == NULL)
1807 err = got_error_from_errno("fclose");
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 int force_refdelta, 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 struct got_object_idset *idset;
1833 struct got_packidx *reuse_packidx = NULL;
1834 struct got_pack *reuse_pack = NULL;
1835 struct got_pack_metavec deltify, reuse;
1836 int ncolored = 0, nfound = 0, ntrees = 0;
1840 seed = arc4random();
1842 memset(&deltify, 0, sizeof(deltify));
1843 memset(&reuse, 0, sizeof(reuse));
1845 idset = got_object_idset_alloc();
1847 return got_error_from_errno("got_object_idset_alloc");
1849 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1850 ntheirs, ours, nours, repo, seed, loose_obj_only,
1851 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1856 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1857 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1862 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1863 err = got_error(GOT_ERR_CANNOT_PACK);
1868 reuse.meta = calloc(reuse.metasz,
1869 sizeof(struct got_pack_meta *));
1870 if (reuse.meta == NULL) {
1871 err = got_error_from_errno("calloc");
1875 err = got_pack_search_deltas(&reuse_packidx, &reuse_pack,
1876 &reuse, idset, ncolored, nfound, ntrees, nours,
1877 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1881 if (reuse_packidx && reuse_pack) {
1882 err = got_repo_pin_pack(repo, reuse_packidx, reuse_pack);
1887 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1888 err = got_error_from_errno("fseeko");
1892 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1894 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1895 if (deltify.meta == NULL) {
1896 err = got_error_from_errno("calloc");
1899 deltify.metasz = ndeltify;
1901 err = got_object_idset_for_each(idset, add_meta_idset_cb,
1905 if (deltify.nmeta > 0) {
1906 err = pick_deltas(deltify.meta, deltify.nmeta,
1907 ncolored, nfound, ntrees, nours, reuse.nmeta,
1908 delta_cache, repo, progress_cb, progress_arg, rl,
1909 cancel_cb, cancel_arg);
1915 if (fflush(delta_cache) == EOF) {
1916 err = got_error_from_errno("fflush");
1922 * Report a 1-byte packfile write to indicate we are about
1923 * to start sending packfile data. gotd(8) needs this.
1925 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1926 1 /* packfile_size */, nours,
1927 got_object_idset_num_elements(idset),
1928 deltify.nmeta + reuse.nmeta, 0);
1933 /* Pinned pack may have moved to different cache slot. */
1934 reuse_pack = got_repo_get_pinned_pack(repo);
1936 err = genpack(packsha1, packfd, reuse_pack, delta_cache, deltify.meta,
1937 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1938 nours, repo, force_refdelta, progress_cb, progress_arg, rl,
1939 cancel_cb, cancel_arg);
1943 free_nmeta(deltify.meta, deltify.nmeta);
1944 free_nmeta(reuse.meta, reuse.nmeta);
1945 got_object_idset_free(idset);
1946 got_repo_unpin_pack(repo);