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>
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"
45 #include "got_opentemp.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_pack.h"
54 #include "got_lib_privsep.h"
55 #include "got_lib_repository.h"
56 #include "got_lib_ratelimit.h"
57 #include "got_lib_inflate.h"
60 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 struct got_pack_meta {
72 struct got_object_id id;
78 /* The best delta we picked */
79 struct got_pack_meta *head;
80 struct got_pack_meta *prev;
81 unsigned char *delta_buf; /* if encoded in memory (compressed) */
82 off_t delta_offset; /* offset in delta cache file (compressed) */
83 off_t delta_len; /* encoded delta length */
84 off_t delta_compressed_len; /* encoded+compressed delta length */
87 int have_reused_delta;
88 off_t reused_delta_offset; /* offset of delta in reused pack file */
89 struct got_object_id *base_obj_id;
91 /* Only used for delta window */
92 struct got_delta_table *dtab;
94 /* Only used for writing offset deltas */
98 struct got_pack_metavec {
99 struct got_pack_meta **meta;
104 static const struct got_error *
105 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
106 const char *path, int obj_type, time_t mtime)
108 const struct got_error *err = NULL;
109 struct got_pack_meta *m;
113 m = calloc(1, sizeof(*m));
115 return got_error_from_errno("calloc");
117 memcpy(&m->id, id, sizeof(m->id));
119 m->path = strdup(path);
120 if (m->path == NULL) {
121 err = got_error_from_errno("strdup");
126 m->obj_type = obj_type;
133 clear_meta(struct got_pack_meta *meta)
139 free(meta->delta_buf);
140 meta->delta_buf = NULL;
141 free(meta->base_obj_id);
142 meta->base_obj_id = NULL;
146 free_nmeta(struct got_pack_meta **meta, int nmeta)
150 for (i = 0; i < nmeta; i++)
156 delta_order_cmp(const void *pa, const void *pb)
158 struct got_pack_meta *a, *b;
161 a = *(struct got_pack_meta **)pa;
162 b = *(struct got_pack_meta **)pb;
164 if (a->obj_type != b->obj_type)
165 return a->obj_type - b->obj_type;
166 cmp = strcmp(a->path, b->path);
169 if (a->mtime < b->mtime)
171 if (a->mtime > b->mtime)
173 return got_object_id_cmp(&a->id, &b->id);
177 delta_size(struct got_delta_instruction *deltas, int ndeltas)
181 for (i = 0; i < ndeltas; i++) {
183 size += GOT_DELTA_SIZE_SHIFT;
185 size += deltas[i].len + 1;
190 static const struct got_error *
191 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
195 if (*len + nseg >= *sz) {
196 while (*len + nseg >= *sz)
198 n = realloc(*p, *sz);
200 return got_error_from_errno("realloc");
203 memcpy(*p + *len, seg, nseg);
208 static const struct got_error *
209 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
210 struct got_delta_instruction *deltas, int ndeltas,
211 off_t delta_size, off_t base_size)
213 const struct got_error *err;
214 unsigned char buf[16], *bp;
216 size_t len = 0, compressed_len;
217 off_t bufsize = delta_size;
219 struct got_delta_instruction *d;
222 delta_buf = malloc(bufsize);
223 if (delta_buf == NULL)
224 return got_error_from_errno("malloc");
226 /* base object size */
227 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
228 n = base_size >> GOT_DELTA_SIZE_SHIFT;
229 for (i = 1; n > 0; i++) {
230 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
231 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
232 n >>= GOT_DELTA_SIZE_SHIFT;
234 err = append(&delta_buf, &len, &bufsize, buf, i);
238 /* target object size */
239 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
240 n = o->size >> GOT_DELTA_SIZE_SHIFT;
241 for (i = 1; n > 0; i++) {
242 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
243 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
244 n >>= GOT_DELTA_SIZE_SHIFT;
246 err = append(&delta_buf, &len, &bufsize, buf, i);
250 for (j = 0; j < ndeltas; j++) {
255 buf[0] = GOT_DELTA_BASE_COPY;
256 for (i = 0; i < 4; i++) {
257 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
266 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
267 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
268 for (i = 0; i < 3 && n > 0; i++) {
269 buf[0] |= 1 << (i + 4);
274 err = append(&delta_buf, &len, &bufsize,
278 } else if (o->f == NULL) {
280 while (n != d->len) {
281 buf[0] = (d->len - n < 127) ? d->len - n : 127;
282 err = append(&delta_buf, &len, &bufsize,
286 err = append(&delta_buf, &len, &bufsize,
287 o->data + o->hdrlen + d->offset + n,
296 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
297 err = got_error_from_errno("fseeko");
301 while (n != d->len) {
302 buf[0] = (d->len - n < 127) ? d->len - n : 127;
303 err = append(&delta_buf, &len, &bufsize,
307 r = fread(content, 1, buf[0], o->f);
309 err = got_ferror(o->f, GOT_ERR_IO);
312 err = append(&delta_buf, &len, &bufsize,
321 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
322 NULL, NULL, delta_buf, 0, len);
327 m->delta_compressed_len = compressed_len;
333 static const struct got_error *
334 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
335 struct got_delta_instruction *deltas, int ndeltas,
336 off_t base_size, FILE *f)
338 const struct got_error *err;
339 unsigned char buf[16], *bp;
342 struct got_deflate_buf zb;
343 struct got_delta_instruction *d;
344 off_t delta_len = 0, compressed_len = 0;
346 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
350 /* base object size */
351 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
352 n = base_size >> GOT_DELTA_SIZE_SHIFT;
353 for (i = 1; n > 0; i++) {
354 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
355 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
356 n >>= GOT_DELTA_SIZE_SHIFT;
359 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
365 /* target object size */
366 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
367 n = o->size >> GOT_DELTA_SIZE_SHIFT;
368 for (i = 1; n > 0; i++) {
369 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
370 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
371 n >>= GOT_DELTA_SIZE_SHIFT;
374 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
380 for (j = 0; j < ndeltas; j++) {
385 buf[0] = GOT_DELTA_BASE_COPY;
386 for (i = 0; i < 4; i++) {
387 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
395 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
396 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
397 for (i = 0; i < 3 && n > 0; i++) {
398 buf[0] |= 1 << (i + 4);
403 err = got_deflate_append_to_file_mmap(&zb,
404 &compressed_len, buf, 0, bp - buf, f, NULL);
407 delta_len += (bp - buf);
408 } else if (o->f == NULL) {
410 while (n != d->len) {
411 buf[0] = (d->len - n < 127) ? d->len - n : 127;
412 err = got_deflate_append_to_file_mmap(&zb,
413 &compressed_len, buf, 0, 1, f, NULL);
417 err = got_deflate_append_to_file_mmap(&zb,
419 o->data + o->hdrlen + d->offset + n, 0,
429 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
430 err = got_error_from_errno("fseeko");
434 while (n != d->len) {
435 buf[0] = (d->len - n < 127) ? d->len - n : 127;
436 err = got_deflate_append_to_file_mmap(&zb,
437 &compressed_len, buf, 0, 1, f, NULL);
441 r = fread(content, 1, buf[0], o->f);
443 err = got_ferror(o->f, GOT_ERR_IO);
446 err = got_deflate_append_to_file_mmap(&zb,
447 &compressed_len, content, 0, buf[0], f,
457 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
462 if (compressed_len != ftello(f) - m->delta_offset) {
463 err = got_error(GOT_ERR_COMPRESSION);
467 m->delta_len = delta_len;
468 m->delta_compressed_len = compressed_len;
470 got_deflate_end(&zb);
474 static const struct got_error *
475 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
476 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
477 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
480 const struct got_error *err;
483 if (progress_cb == NULL)
486 err = got_ratelimit_check(&elapsed, rl);
490 return progress_cb(progress_arg, ncolored, nfound, ntrees,
491 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
494 static const struct got_error *
495 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
497 if (v->nmeta == v->metasz){
498 size_t newsize = 2 * v->metasz;
499 struct got_pack_meta **new;
500 new = reallocarray(v->meta, newsize, sizeof(*new));
502 return got_error_from_errno("reallocarray");
507 v->meta[v->nmeta++] = m;
511 static const struct got_error *
512 find_pack_for_reuse(struct got_packidx **best_packidx,
513 struct got_repository *repo)
515 const struct got_error *err = NULL;
516 struct got_pathlist_entry *pe;
517 const char *best_packidx_path = NULL;
520 *best_packidx = NULL;
522 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
523 const char *path_packidx = pe->path;
524 struct got_packidx *packidx;
527 err = got_repo_get_packidx(&packidx, path_packidx, repo);
531 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
532 if (nobj > nobj_max) {
533 best_packidx_path = path_packidx;
538 if (best_packidx_path) {
539 err = got_repo_get_packidx(best_packidx, best_packidx_path,
547 struct imsgbuf *ibuf;
548 struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
552 static const struct got_error *
553 send_id(struct got_object_id *id, void *data, void *arg)
555 const struct got_error *err = NULL;
556 struct send_id_arg *a = arg;
558 a->ids[a->nids++] = id;
560 if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
561 err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
570 static const struct got_error *
571 recv_reused_delta(struct got_imsg_reused_delta *delta,
572 struct got_object_idset *idset, struct got_pack_metavec *v)
574 struct got_pack_meta *m, *base;
576 if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
577 delta->delta_offset +
578 delta->delta_compressed_size < delta->delta_offset)
579 return got_error(GOT_ERR_BAD_PACKFILE);
581 m = got_object_idset_get(idset, &delta->id);
583 return got_error(GOT_ERR_NO_OBJ);
585 base = got_object_idset_get(idset, &delta->base_id);
587 return got_error(GOT_ERR_NO_OBJ);
589 m->delta_len = delta->delta_size;
590 m->delta_compressed_len = delta->delta_compressed_size;
591 m->delta_offset = delta->delta_out_offset;
593 m->size = delta->result_size;
594 m->have_reused_delta = 1;
595 m->reused_delta_offset = delta->delta_offset;
596 m->base_obj_id = got_object_id_dup(&delta->base_id);
597 if (m->base_obj_id == NULL)
598 return got_error_from_errno("got_object_id_dup");
600 return add_meta(m, v);
603 static const struct got_error *
604 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
605 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
606 struct got_repository *repo,
607 got_pack_progress_cb progress_cb, void *progress_arg,
608 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
610 const struct got_error *err = NULL;
611 struct got_packidx *packidx;
612 struct got_pack *pack;
613 struct send_id_arg sia;
614 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
617 err = find_pack_for_reuse(&packidx, repo);
624 err = got_object_prepare_delta_reuse(&pack, packidx,
625 delta_cache_fd, repo);
629 memset(&sia, 0, sizeof(sia));
630 sia.ibuf = pack->privsep_child->ibuf;
631 err = got_object_idset_for_each(idset, send_id, &sia);
635 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
640 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
648 err = (*cancel_cb)(cancel_arg);
653 err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
654 pack->privsep_child->ibuf);
658 for (i = 0; i < ndeltas; i++) {
659 struct got_imsg_reused_delta *delta = &deltas[i];
660 err = recv_reused_delta(delta, idset, v);
665 err = report_progress(progress_cb, progress_arg, rl,
666 ncolored, nfound, ntrees, 0L, ncommits,
667 got_object_idset_num_elements(idset), v->nmeta, 0);
675 static const struct got_error *
676 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
677 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
678 struct got_repository *repo,
679 got_pack_progress_cb progress_cb, void *progress_arg,
680 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
682 const struct got_error *err = NULL;
683 struct got_pack_meta *m = NULL, *base = NULL;
684 struct got_raw_object *raw = NULL, *base_raw = NULL;
685 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
686 int i, j, ndeltas, best_ndeltas;
687 off_t size, best_size;
688 const int max_base_candidates = 3;
689 size_t delta_memsize = 0;
690 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
693 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
694 for (i = 0; i < nmeta; i++) {
696 err = (*cancel_cb)(cancel_arg);
700 err = report_progress(progress_cb, progress_arg, rl,
701 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
707 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
708 m->obj_type == GOT_OBJ_TYPE_TAG)
711 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
716 if (raw->f == NULL) {
717 err = got_deltify_init_mem(&m->dtab, raw->data,
718 raw->hdrlen, raw->size + raw->hdrlen);
720 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
721 raw->size + raw->hdrlen);
726 if (i > max_base_candidates) {
727 struct got_pack_meta *n = NULL;
728 n = meta[i - (max_base_candidates + 1)];
729 got_deltify_free(n->dtab);
733 best_size = raw->size;
735 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
737 err = (*cancel_cb)(cancel_arg);
742 /* long chains make unpacking slow, avoid such bases */
743 if (base->nchain >= 128 ||
744 base->obj_type != m->obj_type)
747 err = got_object_raw_open(&base_raw, &outfd, repo,
752 if (raw->f == NULL && base_raw->f == NULL) {
753 err = got_deltify_mem_mem(&deltas, &ndeltas,
754 raw->data, raw->hdrlen,
755 raw->size + raw->hdrlen,
756 base->dtab, base_raw->data,
758 base_raw->size + base_raw->hdrlen);
759 } else if (raw->f == NULL) {
760 err = got_deltify_mem_file(&deltas, &ndeltas,
761 raw->data, raw->hdrlen,
762 raw->size + raw->hdrlen,
763 base->dtab, base_raw->f,
765 base_raw->size + base_raw->hdrlen);
766 } else if (base_raw->f == NULL) {
767 err = got_deltify_file_mem(&deltas, &ndeltas,
769 raw->size + raw->hdrlen,
770 base->dtab, base_raw->data,
772 base_raw->size + base_raw->hdrlen);
774 err = got_deltify(&deltas, &ndeltas,
776 raw->size + raw->hdrlen,
777 base->dtab, base_raw->f, base_raw->hdrlen,
778 base_raw->size + base_raw->hdrlen);
780 got_object_raw_close(base_raw);
785 size = delta_size(deltas, ndeltas);
786 if (size + 32 < best_size){
788 * if we already picked a best delta,
793 best_deltas = deltas;
794 best_ndeltas = ndeltas;
796 m->nchain = base->nchain + 1;
798 m->head = base->head;
808 if (best_ndeltas > 0) {
809 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
810 delta_memsize + best_size <= max_delta_memsize) {
811 delta_memsize += best_size;
812 err = encode_delta_in_mem(m, raw, best_deltas,
813 best_ndeltas, best_size, m->prev->size);
815 m->delta_offset = ftello(delta_cache);
816 err = encode_delta(m, raw, best_deltas,
817 best_ndeltas, m->prev->size, delta_cache);
826 got_object_raw_close(raw);
830 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
831 got_deltify_free(meta[i]->dtab);
832 meta[i]->dtab = NULL;
835 got_object_raw_close(raw);
837 got_object_raw_close(base_raw);
838 if (outfd != -1 && close(outfd) == -1 && err == NULL)
839 err = got_error_from_errno("close");
845 static const struct got_error *
846 search_packidx(int *found, struct got_object_id *id,
847 struct got_repository *repo)
849 const struct got_error *err = NULL;
850 struct got_packidx *packidx = NULL;
855 err = got_repo_search_packidx(&packidx, &idx, repo, id);
857 *found = 1; /* object is already packed */
858 else if (err->code == GOT_ERR_NO_OBJ)
863 static const struct got_error *
864 add_object(int want_meta, struct got_object_idset *idset,
865 struct got_object_id *id, const char *path, int obj_type,
866 time_t mtime, int loose_obj_only, struct got_repository *repo,
867 int *ncolored, int *nfound, int *ntrees,
868 got_pack_progress_cb progress_cb, void *progress_arg,
869 struct got_ratelimit *rl)
871 const struct got_error *err;
872 struct got_pack_meta *m = NULL;
874 if (loose_obj_only) {
876 err = search_packidx(&is_packed, id, repo);
879 if (is_packed && want_meta)
884 err = alloc_meta(&m, id, path, obj_type, mtime);
889 err = report_progress(progress_cb, progress_arg, rl,
890 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
898 err = got_object_idset_add(idset, id, m);
906 static const struct got_error *
907 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
908 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
909 struct got_object_id *tree_id,
910 const char *dpath, time_t mtime, struct got_repository *repo,
911 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
912 got_pack_progress_cb progress_cb, void *progress_arg,
913 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
915 const struct got_error *err;
916 struct got_tree_object *tree;
920 err = got_object_open_as_tree(&tree, repo, tree_id);
925 err = report_progress(progress_cb, progress_arg, rl,
926 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
930 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
931 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
932 struct got_object_id *id = got_tree_entry_get_id(e);
933 mode_t mode = got_tree_entry_get_mode(e);
936 err = (*cancel_cb)(cancel_arg);
941 if (got_object_tree_entry_is_submodule(e) ||
942 got_object_idset_contains(idset, id) ||
943 got_object_idset_contains(idset_exclude, id))
946 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
947 got_tree_entry_get_name(e)) == -1) {
948 err = got_error_from_errno("asprintf");
953 struct got_object_qid *qid;
954 err = got_object_qid_alloc(&qid, id);
959 STAILQ_INSERT_TAIL(ids, qid, entry);
960 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
961 err = add_object(want_meta,
962 want_meta ? idset : idset_exclude, id, p,
963 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
964 ncolored, nfound, ntrees,
965 progress_cb, progress_arg, rl);
976 got_object_tree_close(tree);
981 static const struct got_error *
982 load_tree(int want_meta, struct got_object_idset *idset,
983 struct got_object_idset *idset_exclude,
984 struct got_object_id *tree_id, const char *dpath, time_t mtime,
985 struct got_repository *repo, int loose_obj_only,
986 int *ncolored, int *nfound, int *ntrees,
987 got_pack_progress_cb progress_cb, void *progress_arg,
988 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
990 const struct got_error *err = NULL;
991 struct got_object_id_queue tree_ids;
992 struct got_object_qid *qid;
994 if (got_object_idset_contains(idset, tree_id) ||
995 got_object_idset_contains(idset_exclude, tree_id))
998 err = got_object_qid_alloc(&qid, tree_id);
1001 qid->data = strdup(dpath);
1002 if (qid->data == NULL) {
1003 err = got_error_from_errno("strdup");
1004 got_object_qid_free(qid);
1008 STAILQ_INIT(&tree_ids);
1009 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1011 while (!STAILQ_EMPTY(&tree_ids)) {
1014 err = (*cancel_cb)(cancel_arg);
1019 qid = STAILQ_FIRST(&tree_ids);
1020 STAILQ_REMOVE_HEAD(&tree_ids, entry);
1023 if (got_object_idset_contains(idset, &qid->id) ||
1024 got_object_idset_contains(idset_exclude, &qid->id)) {
1026 got_object_qid_free(qid);
1030 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1031 &qid->id, path, GOT_OBJ_TYPE_TREE,
1032 mtime, loose_obj_only, repo,
1033 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1036 got_object_qid_free(qid);
1040 err = load_tree_entries(&tree_ids, want_meta, idset,
1041 idset_exclude, &qid->id,
1042 path, mtime, repo, loose_obj_only, ncolored, nfound,
1043 ntrees, progress_cb, progress_arg, rl,
1044 cancel_cb, cancel_arg);
1046 got_object_qid_free(qid);
1051 STAILQ_FOREACH(qid, &tree_ids, entry)
1053 got_object_id_queue_free(&tree_ids);
1057 static const struct got_error *
1058 load_commit(int want_meta, struct got_object_idset *idset,
1059 struct got_object_idset *idset_exclude,
1060 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1061 int *ncolored, int *nfound, int *ntrees,
1062 got_pack_progress_cb progress_cb, void *progress_arg,
1063 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1065 const struct got_error *err;
1066 struct got_commit_object *commit;
1068 if (got_object_idset_contains(idset, id) ||
1069 got_object_idset_contains(idset_exclude, id))
1072 if (loose_obj_only) {
1074 err = search_packidx(&is_packed, id, repo);
1077 if (is_packed && want_meta)
1081 err = got_object_open_as_commit(&commit, repo, id);
1085 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1086 id, "", GOT_OBJ_TYPE_COMMIT,
1087 got_object_commit_get_committer_time(commit),
1088 loose_obj_only, repo,
1089 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1093 err = load_tree(want_meta, idset, idset_exclude,
1094 got_object_commit_get_tree_id(commit),
1095 "", got_object_commit_get_committer_time(commit),
1096 repo, loose_obj_only, ncolored, nfound, ntrees,
1097 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1099 got_object_commit_close(commit);
1103 static const struct got_error *
1104 load_tag(int want_meta, struct got_object_idset *idset,
1105 struct got_object_idset *idset_exclude,
1106 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1107 int *ncolored, int *nfound, int *ntrees,
1108 got_pack_progress_cb progress_cb, void *progress_arg,
1109 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1111 const struct got_error *err;
1112 struct got_tag_object *tag = NULL;
1114 if (got_object_idset_contains(idset, id) ||
1115 got_object_idset_contains(idset_exclude, id))
1118 if (loose_obj_only) {
1120 err = search_packidx(&is_packed, id, repo);
1123 if (is_packed && want_meta)
1127 err = got_object_open_as_tag(&tag, repo, id);
1131 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1132 id, "", GOT_OBJ_TYPE_TAG,
1133 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1134 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1138 switch (got_object_tag_get_object_type(tag)) {
1139 case GOT_OBJ_TYPE_COMMIT:
1140 err = load_commit(want_meta, idset, idset_exclude,
1141 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1142 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1143 cancel_cb, cancel_arg);
1145 case GOT_OBJ_TYPE_TREE:
1146 err = load_tree(want_meta, idset, idset_exclude,
1147 got_object_tag_get_object_id(tag), "",
1148 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1149 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1150 cancel_cb, cancel_arg);
1157 got_object_tag_close(tag);
1161 enum findtwixt_color {
1168 static const int findtwixt_colors[] = {
1175 static const struct got_error *
1176 paint_commit(struct got_object_qid *qid, int color)
1178 if (color < 0 || color >= nitems(findtwixt_colors))
1179 return got_error(GOT_ERR_RANGE);
1181 qid->data = (void *)&findtwixt_colors[color];
1185 static const struct got_error *
1186 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1187 int color, struct got_repository *repo)
1189 const struct got_error *err;
1190 struct got_object_qid *qid;
1192 err = got_object_qid_alloc(&qid, id);
1196 STAILQ_INSERT_TAIL(ids, qid, entry);
1197 return paint_commit(qid, color);
1200 struct append_id_arg {
1201 struct got_object_id **array;
1203 struct got_object_idset *drop;
1204 struct got_object_idset *skip;
1207 static const struct got_error *
1208 append_id(struct got_object_id *id, void *data, void *arg)
1210 struct append_id_arg *a = arg;
1212 if (got_object_idset_contains(a->skip, id) ||
1213 got_object_idset_contains(a->drop, id))
1216 a->array[++a->idx] = got_object_id_dup(id);
1217 if (a->array[a->idx] == NULL)
1218 return got_error_from_errno("got_object_id_dup");
1223 static const struct got_error *
1224 queue_commit_or_tag_id(struct got_object_id *id, int color,
1225 struct got_object_id_queue *ids, struct got_repository *repo)
1227 const struct got_error *err;
1228 struct got_tag_object *tag = NULL;
1231 err = got_object_get_type(&obj_type, repo, id);
1235 if (obj_type == GOT_OBJ_TYPE_TAG) {
1236 err = got_object_open_as_tag(&tag, repo, id);
1239 obj_type = got_object_tag_get_object_type(tag);
1240 id = got_object_tag_get_object_id(tag);
1243 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1244 err = queue_commit_id(ids, id, color, repo);
1250 got_object_tag_close(tag);
1254 static const struct got_error *
1255 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1256 struct got_object_idset *keep, struct got_object_idset *drop,
1257 struct got_object_idset *skip, struct got_repository *repo,
1258 got_pack_progress_cb progress_cb, void *progress_arg,
1259 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1261 const struct got_error *err = NULL;
1262 struct got_commit_object *commit = NULL;
1263 const struct got_object_id_queue *parents;
1264 struct got_object_qid *qid;
1265 int nqueued = nids, nskip = 0;
1267 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1271 err = cancel_cb(cancel_arg);
1276 qid = STAILQ_FIRST(ids);
1277 STAILQ_REMOVE_HEAD(ids, entry);
1279 color = *((int *)qid->data);
1280 if (color == COLOR_SKIP)
1283 if (got_object_idset_contains(skip, &qid->id)) {
1284 got_object_qid_free(qid);
1290 if (got_object_idset_contains(keep, &qid->id)) {
1291 got_object_qid_free(qid);
1294 if (got_object_idset_contains(drop, &qid->id)) {
1295 err = paint_commit(qid, COLOR_SKIP);
1301 err = got_object_idset_add(keep, &qid->id, NULL);
1306 if (got_object_idset_contains(drop, &qid->id)) {
1307 got_object_qid_free(qid);
1310 if (got_object_idset_contains(keep, &qid->id)) {
1311 err = paint_commit(qid, COLOR_SKIP);
1317 err = got_object_idset_add(drop, &qid->id, NULL);
1322 if (!got_object_idset_contains(skip, &qid->id)) {
1323 err = got_object_idset_add(skip, &qid->id,
1330 /* should not happen */
1331 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1332 "%s invalid commit color %d", __func__, color);
1336 err = report_progress(progress_cb, progress_arg, rl,
1337 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1342 err = got_object_open_as_commit(&commit, repo, &qid->id);
1346 parents = got_object_commit_get_parent_ids(commit);
1348 struct got_object_qid *pid;
1349 color = *((int *)qid->data);
1350 STAILQ_FOREACH(pid, parents, entry) {
1351 err = queue_commit_id(ids, &pid->id, color,
1356 if (color == COLOR_SKIP)
1361 got_object_commit_close(commit);
1363 got_object_qid_free(qid);
1367 got_object_commit_close(commit);
1371 static const struct got_error *
1372 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1373 struct got_object_id **head, int nhead,
1374 struct got_object_id **tail, int ntail,
1375 struct got_repository *repo,
1376 got_pack_progress_cb progress_cb, void *progress_arg,
1377 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1379 const struct got_error *err = NULL;
1380 struct got_object_id_queue ids;
1381 struct got_object_idset *keep, *drop, *skip = NULL;
1389 keep = got_object_idset_alloc();
1391 return got_error_from_errno("got_object_idset_alloc");
1393 drop = got_object_idset_alloc();
1395 err = got_error_from_errno("got_object_idset_alloc");
1399 skip = got_object_idset_alloc();
1401 err = got_error_from_errno("got_object_idset_alloc");
1405 for (i = 0; i < nhead; i++) {
1406 struct got_object_id *id = head[i];
1409 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1414 for (i = 0; i < ntail; i++) {
1415 struct got_object_id *id = tail[i];
1418 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1423 err = paint_commits(ncolored, &ids, nhead + ntail,
1424 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1425 cancel_cb, cancel_arg);
1429 nkeep = got_object_idset_num_elements(keep);
1431 struct append_id_arg arg;
1432 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1433 if (arg.array == NULL) {
1434 err = got_error_from_errno("calloc");
1440 err = got_object_idset_for_each(keep, append_id, &arg);
1446 *nres = arg.idx + 1;
1449 got_object_idset_free(keep);
1450 got_object_idset_free(drop);
1452 got_object_idset_free(skip);
1453 got_object_id_queue_free(&ids);
1457 static const struct got_error *
1458 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1459 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1460 struct got_object_id **ours, int nours, struct got_repository *repo,
1461 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1462 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1464 const struct got_error *err = NULL;
1465 struct got_object_id **ids = NULL;
1466 int i, nobj = 0, obj_type;
1467 struct got_object_idset *idset_exclude;
1469 idset_exclude = got_object_idset_alloc();
1470 if (idset_exclude == NULL)
1471 return got_error_from_errno("got_object_idset_alloc");
1477 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1478 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1482 for (i = 0; i < ntheirs; i++) {
1483 struct got_object_id *id = theirs[i];
1486 err = got_object_get_type(&obj_type, repo, id);
1489 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1490 err = load_commit(0, idset, idset_exclude, id, repo,
1491 loose_obj_only, ncolored, nfound, ntrees,
1492 progress_cb, progress_arg, rl,
1493 cancel_cb, cancel_arg);
1496 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1497 err = load_tag(0, idset, idset_exclude, id, repo,
1498 loose_obj_only, ncolored, nfound, ntrees,
1499 progress_cb, progress_arg, rl,
1500 cancel_cb, cancel_arg);
1506 for (i = 0; i < nobj; i++) {
1507 err = load_commit(1, idset, idset_exclude,
1508 ids[i], repo, loose_obj_only, ncolored, nfound, ntrees,
1509 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1514 for (i = 0; i < nours; i++) {
1515 struct got_object_id *id = ours[i];
1516 struct got_pack_meta *m;
1519 m = got_object_idset_get(idset, id);
1521 err = got_object_get_type(&obj_type, repo, id);
1525 obj_type = m->obj_type;
1526 if (obj_type != GOT_OBJ_TYPE_TAG)
1528 err = load_tag(1, idset, idset_exclude, id, repo,
1529 loose_obj_only, ncolored, nfound, ntrees,
1530 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1535 for (i = 0; i < nobj; i++) {
1539 got_object_idset_free(idset_exclude);
1543 const struct got_error *
1544 hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
1548 SHA1Update(ctx, buf, len);
1549 n = fwrite(buf, 1, len, f);
1551 return got_ferror(f, GOT_ERR_IO);
1555 const struct got_error *
1556 hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1558 unsigned char buf[65536];
1562 while (remain > 0) {
1563 size_t copylen = MIN(sizeof(buf), remain);
1564 n = fread(buf, 1, copylen, fsrc);
1566 return got_ferror(fsrc, GOT_ERR_IO);
1567 SHA1Update(ctx, buf, copylen);
1568 n = fwrite(buf, 1, copylen, fdst);
1570 return got_ferror(fdst, GOT_ERR_IO);
1577 const struct got_error *
1578 hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1579 FILE *fdst, off_t len, SHA1_CTX *ctx)
1583 if (src_offset + len > src_size)
1584 return got_error(GOT_ERR_RANGE);
1586 SHA1Update(ctx, src + src_offset, len);
1587 n = fwrite(src + src_offset, 1, len, fdst);
1589 return got_ferror(fdst, GOT_ERR_IO);
1595 putbe32(char *b, uint32_t n)
1604 write_order_cmp(const void *pa, const void *pb)
1606 struct got_pack_meta *a, *b, *ahd, *bhd;
1608 a = *(struct got_pack_meta **)pa;
1609 b = *(struct got_pack_meta **)pb;
1610 ahd = (a->head == NULL) ? a : a->head;
1611 bhd = (b->head == NULL) ? b : b->head;
1612 if (bhd->mtime < ahd->mtime)
1614 if (bhd->mtime > ahd->mtime)
1620 if (a->nchain != b->nchain)
1621 return a->nchain - b->nchain;
1622 if (a->mtime < b->mtime)
1624 if (a->mtime > b->mtime)
1626 return got_object_id_cmp(&a->id, &b->id);
1630 reuse_write_order_cmp(const void *pa, const void *pb)
1632 struct got_pack_meta *a, *b;
1634 a = *(struct got_pack_meta **)pa;
1635 b = *(struct got_pack_meta **)pb;
1637 if (a->reused_delta_offset < b->reused_delta_offset)
1639 if (a->reused_delta_offset > b->reused_delta_offset)
1644 static const struct got_error *
1645 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1651 hdr[0] = obj_type << 4;
1652 hdr[0] |= len & 0xf;
1654 for (i = 1; len != 0; i++){
1656 return got_error(GOT_ERR_NO_SPACE);
1657 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1658 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1659 len >>= GOT_DELTA_SIZE_SHIFT;
1667 packoff(char *hdr, off_t off)
1672 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1673 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1674 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1675 GOT_DELTA_SIZE_MORE;
1680 hdr[j++] = rbuf[--i];
1684 static const struct got_error *
1685 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1686 struct got_pack_meta *m)
1688 const struct got_error *err;
1692 if (m->prev->off != 0) {
1693 err = packhdr(&nh, buf, sizeof(buf),
1694 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1697 nh += packoff(buf + nh, m->off - m->prev->off);
1698 err = hwrite(packfile, buf, nh, ctx);
1701 *packfile_size += nh;
1703 err = packhdr(&nh, buf, sizeof(buf),
1704 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1707 err = hwrite(packfile, buf, nh, ctx);
1710 *packfile_size += nh;
1711 err = hwrite(packfile, m->prev->id.sha1,
1712 sizeof(m->prev->id.sha1), ctx);
1715 *packfile_size += sizeof(m->prev->id.sha1);
1721 static const struct got_error *
1722 write_packed_object(off_t *packfile_size, FILE *packfile,
1723 FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1724 struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
1725 struct got_repository *repo)
1727 const struct got_error *err = NULL;
1728 struct got_deflate_checksum csum;
1731 struct got_raw_object *raw = NULL;
1734 csum.output_sha1 = ctx;
1735 csum.output_crc = NULL;
1737 m->off = ftello(packfile);
1738 if (m->delta_len == 0) {
1739 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1742 err = packhdr(&nh, buf, sizeof(buf),
1743 m->obj_type, raw->size);
1746 err = hwrite(packfile, buf, nh, ctx);
1749 *packfile_size += nh;
1750 if (raw->f == NULL) {
1751 err = got_deflate_to_file_mmap(&outlen,
1752 raw->data + raw->hdrlen, 0, raw->size,
1757 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1759 err = got_error_from_errno("fseeko");
1762 err = got_deflate_to_file(&outlen, raw->f,
1763 raw->size, packfile, &csum);
1767 *packfile_size += outlen;
1768 got_object_raw_close(raw);
1770 } else if (m->delta_buf) {
1771 err = deltahdr(packfile_size, ctx, packfile, m);
1774 err = hwrite(packfile, m->delta_buf,
1775 m->delta_compressed_len, ctx);
1778 *packfile_size += m->delta_compressed_len;
1780 m->delta_buf = NULL;
1781 } else if (delta_cache_map) {
1782 err = deltahdr(packfile_size, ctx, packfile, m);
1785 err = hcopy_mmap(delta_cache_map, m->delta_offset,
1786 delta_cache_size, packfile, m->delta_compressed_len,
1790 *packfile_size += m->delta_compressed_len;
1792 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1794 err = got_error_from_errno("fseeko");
1797 err = deltahdr(packfile_size, ctx, packfile, m);
1800 err = hcopy(delta_cache, packfile,
1801 m->delta_compressed_len, ctx);
1804 *packfile_size += m->delta_compressed_len;
1808 got_object_raw_close(raw);
1812 static const struct got_error *
1813 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1814 struct got_pack_meta **deltify, int ndeltify,
1815 struct got_pack_meta **reuse, int nreuse,
1816 int ncolored, int nfound, int ntrees, int nours,
1817 struct got_repository *repo,
1818 got_pack_progress_cb progress_cb, void *progress_arg,
1819 struct got_ratelimit *rl,
1820 got_cancel_cb cancel_cb, void *cancel_arg)
1822 const struct got_error *err = NULL;
1825 struct got_pack_meta *m;
1828 off_t packfile_size = 0;
1830 int delta_cache_fd = -1;
1831 uint8_t *delta_cache_map = NULL;
1832 size_t delta_cache_size = 0;
1836 #ifndef GOT_PACK_NO_MMAP
1837 delta_cache_fd = dup(fileno(delta_cache));
1838 if (delta_cache_fd != -1) {
1840 if (fstat(delta_cache_fd, &sb) == -1) {
1841 err = got_error_from_errno("fstat");
1844 if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1845 delta_cache_map = mmap(NULL, sb.st_size,
1846 PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1847 if (delta_cache_map == MAP_FAILED) {
1848 if (errno != ENOMEM) {
1849 err = got_error_from_errno("mmap");
1852 delta_cache_map = NULL; /* fallback on stdio */
1854 delta_cache_size = (size_t)sb.st_size;
1858 err = hwrite(packfile, "PACK", 4, &ctx);
1861 putbe32(buf, GOT_PACKFILE_VERSION);
1862 err = hwrite(packfile, buf, 4, &ctx);
1865 putbe32(buf, ndeltify + nreuse);
1866 err = hwrite(packfile, buf, 4, &ctx);
1870 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1872 for (i = 0; i < ndeltify; i++) {
1873 err = report_progress(progress_cb, progress_arg, rl,
1874 ncolored, nfound, ntrees, packfile_size, nours,
1875 ndeltify + nreuse, ndeltify + nreuse, i);
1879 err = write_packed_object(&packfile_size, packfile,
1880 delta_cache, delta_cache_map, delta_cache_size,
1881 m, &outfd, &ctx, repo);
1886 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1887 reuse_write_order_cmp);
1888 for (i = 0; i < nreuse; i++) {
1889 err = report_progress(progress_cb, progress_arg, rl,
1890 ncolored, nfound, ntrees, packfile_size, nours,
1891 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1895 err = write_packed_object(&packfile_size, packfile,
1896 delta_cache, delta_cache_map, delta_cache_size,
1897 m, &outfd, &ctx, repo);
1902 SHA1Final(pack_sha1, &ctx);
1903 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1904 if (n != SHA1_DIGEST_LENGTH)
1905 err = got_ferror(packfile, GOT_ERR_IO);
1906 packfile_size += SHA1_DIGEST_LENGTH;
1907 packfile_size += sizeof(struct got_packfile_hdr);
1909 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1910 packfile_size, nours, ndeltify + nreuse,
1911 ndeltify + nreuse, ndeltify + nreuse);
1916 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1917 err = got_error_from_errno("close");
1918 if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1919 err = got_error_from_errno("munmap");
1920 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1921 err = got_error_from_errno("close");
1925 static const struct got_error *
1926 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1928 struct got_pack_meta *m = data;
1929 struct got_pack_metavec *v = arg;
1931 if (m->have_reused_delta)
1934 return add_meta(m, v);
1937 const struct got_error *
1938 got_pack_create(uint8_t *packsha1, FILE *packfile,
1939 struct got_object_id **theirs, int ntheirs,
1940 struct got_object_id **ours, int nours,
1941 struct got_repository *repo, int loose_obj_only, int allow_empty,
1942 got_pack_progress_cb progress_cb, void *progress_arg,
1943 got_cancel_cb cancel_cb, void *cancel_arg)
1945 const struct got_error *err;
1946 int delta_cache_fd = -1;
1947 FILE *delta_cache = NULL;
1948 struct got_object_idset *idset;
1949 struct got_ratelimit rl;
1950 struct got_pack_metavec deltify, reuse;
1951 int ncolored = 0, nfound = 0, ntrees = 0;
1954 memset(&deltify, 0, sizeof(deltify));
1955 memset(&reuse, 0, sizeof(reuse));
1957 got_ratelimit_init(&rl, 0, 500);
1959 idset = got_object_idset_alloc();
1961 return got_error_from_errno("got_object_idset_alloc");
1963 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1964 ntheirs, ours, nours, repo, loose_obj_only,
1965 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1970 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1971 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1976 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1977 err = got_error(GOT_ERR_CANNOT_PACK);
1981 delta_cache_fd = got_opentempfd();
1982 if (delta_cache_fd == -1) {
1983 err = got_error_from_errno("got_opentemp");
1988 reuse.meta = calloc(reuse.metasz,
1989 sizeof(struct got_pack_meta *));
1990 if (reuse.meta == NULL) {
1991 err = got_error_from_errno("calloc");
1995 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1996 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1997 cancel_cb, cancel_arg);
2001 delta_cache = fdopen(delta_cache_fd, "a+");
2002 if (delta_cache == NULL) {
2003 err = got_error_from_errno("fdopen");
2006 delta_cache_fd = -1;
2008 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
2009 err = got_error_from_errno("fseeko");
2013 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
2015 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
2016 if (deltify.meta == NULL) {
2017 err = got_error_from_errno("calloc");
2020 deltify.metasz = ndeltify;
2022 err = got_object_idset_for_each(idset, add_meta_idset_cb,
2026 if (deltify.nmeta > 0) {
2027 err = pick_deltas(deltify.meta, deltify.nmeta,
2028 ncolored, nfound, ntrees, nours, reuse.nmeta,
2029 delta_cache, repo, progress_cb, progress_arg, &rl,
2030 cancel_cb, cancel_arg);
2036 if (fflush(delta_cache) == EOF) {
2037 err = got_error_from_errno("fflush");
2040 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
2041 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
2042 nours, repo, progress_cb, progress_arg, &rl,
2043 cancel_cb, cancel_arg);
2047 free_nmeta(deltify.meta, deltify.nmeta);
2048 free_nmeta(reuse.meta, reuse.nmeta);
2049 got_object_idset_free(idset);
2050 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2051 err = got_error_from_errno("close");
2052 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2053 err = got_error_from_errno("fclose");