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>
34 #if defined(__FreeBSD__)
38 #include "got_error.h"
39 #include "got_cancel.h"
40 #include "got_object.h"
42 #include "got_reference.h"
43 #include "got_repository_admin.h"
44 #include "got_opentemp.h"
46 #include "got_lib_deltify.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_object.h"
49 #include "got_lib_object_idset.h"
50 #include "got_lib_object_cache.h"
51 #include "got_lib_deflate.h"
52 #include "got_lib_pack.h"
53 #include "got_lib_pack_create.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"
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 struct got_pack_meta {
74 struct got_object_id id;
80 /* The best delta we picked */
81 struct got_pack_meta *head;
82 struct got_pack_meta *prev;
83 unsigned char *delta_buf; /* if encoded in memory (compressed) */
84 off_t delta_offset; /* offset in delta cache file (compressed) */
85 off_t delta_len; /* encoded delta length */
86 off_t delta_compressed_len; /* encoded+compressed delta length */
89 off_t reused_delta_offset; /* offset of delta in reused pack file */
90 struct got_object_id *base_obj_id;
92 /* Only used for delta window */
93 struct got_delta_table *dtab;
95 /* Only used for writing offset deltas */
99 struct got_pack_metavec {
100 struct got_pack_meta **meta;
105 static const struct got_error *
106 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
107 const char *path, int obj_type, time_t mtime, uint32_t seed)
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_hash = murmurhash2(path, strlen(path), seed);
120 m->obj_type = obj_type;
127 clear_meta(struct got_pack_meta *meta)
132 free(meta->delta_buf);
133 meta->delta_buf = NULL;
134 free(meta->base_obj_id);
135 meta->base_obj_id = NULL;
136 meta->reused_delta_offset = 0;
140 free_nmeta(struct got_pack_meta **meta, int nmeta)
144 for (i = 0; i < nmeta; i++)
150 delta_order_cmp(const void *pa, const void *pb)
152 struct got_pack_meta *a, *b;
154 a = *(struct got_pack_meta **)pa;
155 b = *(struct got_pack_meta **)pb;
157 if (a->obj_type != b->obj_type)
158 return a->obj_type - b->obj_type;
159 if (a->path_hash < b->path_hash)
161 if (a->path_hash > b->path_hash)
163 if (a->mtime < b->mtime)
165 if (a->mtime > b->mtime)
167 return got_object_id_cmp(&a->id, &b->id);
171 delta_size(struct got_delta_instruction *deltas, int ndeltas)
175 for (i = 0; i < ndeltas; i++) {
177 size += GOT_DELTA_SIZE_SHIFT;
179 size += deltas[i].len + 1;
184 static const struct got_error *
185 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
189 if (*len + nseg >= *sz) {
190 while (*len + nseg >= *sz)
192 n = realloc(*p, *sz);
194 return got_error_from_errno("realloc");
197 memcpy(*p + *len, seg, nseg);
202 static const struct got_error *
203 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
204 struct got_delta_instruction *deltas, int ndeltas,
205 off_t delta_size, off_t base_size)
207 const struct got_error *err;
208 unsigned char buf[16], *bp;
210 size_t len = 0, compressed_len;
211 off_t bufsize = delta_size;
213 struct got_delta_instruction *d;
216 delta_buf = malloc(bufsize);
217 if (delta_buf == NULL)
218 return got_error_from_errno("malloc");
220 /* base object size */
221 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
222 n = base_size >> GOT_DELTA_SIZE_SHIFT;
223 for (i = 1; n > 0; i++) {
224 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
225 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
226 n >>= GOT_DELTA_SIZE_SHIFT;
228 err = append(&delta_buf, &len, &bufsize, buf, i);
232 /* target object size */
233 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
234 n = o->size >> GOT_DELTA_SIZE_SHIFT;
235 for (i = 1; n > 0; i++) {
236 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
237 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
238 n >>= GOT_DELTA_SIZE_SHIFT;
240 err = append(&delta_buf, &len, &bufsize, buf, i);
244 for (j = 0; j < ndeltas; j++) {
249 buf[0] = GOT_DELTA_BASE_COPY;
250 for (i = 0; i < 4; i++) {
251 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
260 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
261 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
262 for (i = 0; i < 3 && n > 0; i++) {
263 buf[0] |= 1 << (i + 4);
268 err = append(&delta_buf, &len, &bufsize,
272 } else if (o->f == NULL) {
274 while (n != d->len) {
275 buf[0] = (d->len - n < 127) ? d->len - n : 127;
276 err = append(&delta_buf, &len, &bufsize,
280 err = append(&delta_buf, &len, &bufsize,
281 o->data + o->hdrlen + d->offset + n,
290 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
291 err = got_error_from_errno("fseeko");
295 while (n != d->len) {
296 buf[0] = (d->len - n < 127) ? d->len - n : 127;
297 err = append(&delta_buf, &len, &bufsize,
301 r = fread(content, 1, buf[0], o->f);
303 err = got_ferror(o->f, GOT_ERR_IO);
306 err = append(&delta_buf, &len, &bufsize,
315 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
316 NULL, NULL, delta_buf, 0, len);
321 m->delta_compressed_len = compressed_len;
327 static const struct got_error *
328 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
329 struct got_delta_instruction *deltas, int ndeltas,
330 off_t base_size, FILE *f)
332 const struct got_error *err;
333 unsigned char buf[16], *bp;
336 struct got_deflate_buf zb;
337 struct got_delta_instruction *d;
338 off_t delta_len = 0, compressed_len = 0;
340 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
344 /* base object size */
345 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
346 n = base_size >> GOT_DELTA_SIZE_SHIFT;
347 for (i = 1; n > 0; i++) {
348 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
349 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
350 n >>= GOT_DELTA_SIZE_SHIFT;
353 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
359 /* target object size */
360 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
361 n = o->size >> GOT_DELTA_SIZE_SHIFT;
362 for (i = 1; n > 0; i++) {
363 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
364 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
365 n >>= GOT_DELTA_SIZE_SHIFT;
368 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
374 for (j = 0; j < ndeltas; j++) {
379 buf[0] = GOT_DELTA_BASE_COPY;
380 for (i = 0; i < 4; i++) {
381 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
389 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
390 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
391 for (i = 0; i < 3 && n > 0; i++) {
392 buf[0] |= 1 << (i + 4);
397 err = got_deflate_append_to_file_mmap(&zb,
398 &compressed_len, buf, 0, bp - buf, f, NULL);
401 delta_len += (bp - buf);
402 } else if (o->f == NULL) {
404 while (n != d->len) {
405 buf[0] = (d->len - n < 127) ? d->len - n : 127;
406 err = got_deflate_append_to_file_mmap(&zb,
407 &compressed_len, buf, 0, 1, f, NULL);
411 err = got_deflate_append_to_file_mmap(&zb,
413 o->data + o->hdrlen + d->offset + n, 0,
423 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
424 err = got_error_from_errno("fseeko");
428 while (n != d->len) {
429 buf[0] = (d->len - n < 127) ? d->len - n : 127;
430 err = got_deflate_append_to_file_mmap(&zb,
431 &compressed_len, buf, 0, 1, f, NULL);
435 r = fread(content, 1, buf[0], o->f);
437 err = got_ferror(o->f, GOT_ERR_IO);
440 err = got_deflate_append_to_file_mmap(&zb,
441 &compressed_len, content, 0, buf[0], f,
451 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
456 if (compressed_len != ftello(f) - m->delta_offset) {
457 err = got_error(GOT_ERR_COMPRESSION);
461 m->delta_len = delta_len;
462 m->delta_compressed_len = compressed_len;
464 got_deflate_end(&zb);
468 static const struct got_error *
469 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
470 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
471 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
474 const struct got_error *err;
477 if (progress_cb == NULL)
480 err = got_ratelimit_check(&elapsed, rl);
484 return progress_cb(progress_arg, ncolored, nfound, ntrees,
485 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
488 static const struct got_error *
489 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
491 if (v->nmeta == v->metasz){
492 size_t newsize = 2 * v->metasz;
493 struct got_pack_meta **new;
494 new = reallocarray(v->meta, newsize, sizeof(*new));
496 return got_error_from_errno("reallocarray");
501 v->meta[v->nmeta++] = m;
505 static const struct got_error *
506 find_pack_for_reuse(struct got_packidx **best_packidx,
507 struct got_repository *repo)
509 const struct got_error *err = NULL;
510 struct got_pathlist_entry *pe;
511 const char *best_packidx_path = NULL;
514 *best_packidx = NULL;
516 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
517 const char *path_packidx = pe->path;
518 struct got_packidx *packidx;
521 err = got_repo_get_packidx(&packidx, path_packidx, repo);
525 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
526 if (nobj > nobj_max) {
527 best_packidx_path = path_packidx;
532 if (best_packidx_path) {
533 err = got_repo_get_packidx(best_packidx, best_packidx_path,
541 struct imsgbuf *ibuf;
542 struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
546 static const struct got_error *
547 send_id(struct got_object_id *id, void *data, void *arg)
549 const struct got_error *err = NULL;
550 struct send_id_arg *a = arg;
552 a->ids[a->nids++] = id;
554 if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
555 err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
564 static const struct got_error *
565 recv_reused_delta(struct got_imsg_reused_delta *delta,
566 struct got_object_idset *idset, struct got_pack_metavec *v)
568 struct got_pack_meta *m, *base;
570 if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
571 delta->delta_offset +
572 delta->delta_compressed_size < delta->delta_offset)
573 return got_error(GOT_ERR_BAD_PACKFILE);
575 m = got_object_idset_get(idset, &delta->id);
577 return got_error(GOT_ERR_NO_OBJ);
579 base = got_object_idset_get(idset, &delta->base_id);
581 return got_error(GOT_ERR_NO_OBJ);
583 m->delta_len = delta->delta_size;
584 m->delta_compressed_len = delta->delta_compressed_size;
585 m->delta_offset = delta->delta_out_offset;
587 m->size = delta->result_size;
588 m->reused_delta_offset = delta->delta_offset;
589 m->base_obj_id = got_object_id_dup(&delta->base_id);
590 if (m->base_obj_id == NULL)
591 return got_error_from_errno("got_object_id_dup");
593 return add_meta(m, v);
596 static const struct got_error *
597 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
598 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
599 struct got_repository *repo,
600 got_pack_progress_cb progress_cb, void *progress_arg,
601 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
603 const struct got_error *err = NULL;
604 struct got_packidx *packidx;
605 struct got_pack *pack;
606 struct send_id_arg sia;
607 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
610 err = find_pack_for_reuse(&packidx, repo);
617 err = got_object_prepare_delta_reuse(&pack, packidx,
618 delta_cache_fd, repo);
622 memset(&sia, 0, sizeof(sia));
623 sia.ibuf = pack->privsep_child->ibuf;
624 err = got_object_idset_for_each(idset, send_id, &sia);
628 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
633 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
641 err = (*cancel_cb)(cancel_arg);
646 err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
647 pack->privsep_child->ibuf);
651 for (i = 0; i < ndeltas; i++) {
652 struct got_imsg_reused_delta *delta = &deltas[i];
653 err = recv_reused_delta(delta, idset, v);
658 err = report_progress(progress_cb, progress_arg, rl,
659 ncolored, nfound, ntrees, 0L, ncommits,
660 got_object_idset_num_elements(idset), v->nmeta, 0);
668 static const struct got_error *
669 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
670 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
671 struct got_repository *repo,
672 got_pack_progress_cb progress_cb, void *progress_arg,
673 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
675 const struct got_error *err = NULL;
676 struct got_pack_meta *m = NULL, *base = NULL;
677 struct got_raw_object *raw = NULL, *base_raw = NULL;
678 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
679 int i, j, ndeltas, best_ndeltas;
680 off_t size, best_size;
681 const int max_base_candidates = 3;
682 size_t delta_memsize = 0;
683 const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
687 delta_seed = arc4random();
689 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
690 for (i = 0; i < nmeta; i++) {
692 err = (*cancel_cb)(cancel_arg);
696 err = report_progress(progress_cb, progress_arg, rl,
697 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
703 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
704 m->obj_type == GOT_OBJ_TYPE_TAG)
707 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
712 if (raw->f == NULL) {
713 err = got_deltify_init_mem(&m->dtab, raw->data,
714 raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
716 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
717 raw->size + raw->hdrlen, delta_seed);
722 if (i > max_base_candidates) {
723 struct got_pack_meta *n = NULL;
724 n = meta[i - (max_base_candidates + 1)];
725 got_deltify_free(n->dtab);
729 best_size = raw->size;
731 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
733 err = (*cancel_cb)(cancel_arg);
738 /* long chains make unpacking slow, avoid such bases */
739 if (base->nchain >= 128 ||
740 base->obj_type != m->obj_type)
743 err = got_object_raw_open(&base_raw, &outfd, repo,
748 if (raw->f == NULL && base_raw->f == NULL) {
749 err = got_deltify_mem_mem(&deltas, &ndeltas,
750 raw->data, raw->hdrlen,
751 raw->size + raw->hdrlen, delta_seed,
752 base->dtab, base_raw->data,
754 base_raw->size + base_raw->hdrlen);
755 } else if (raw->f == NULL) {
756 err = got_deltify_mem_file(&deltas, &ndeltas,
757 raw->data, raw->hdrlen,
758 raw->size + raw->hdrlen, delta_seed,
759 base->dtab, base_raw->f,
761 base_raw->size + base_raw->hdrlen);
762 } else if (base_raw->f == NULL) {
763 err = got_deltify_file_mem(&deltas, &ndeltas,
765 raw->size + raw->hdrlen, delta_seed,
766 base->dtab, base_raw->data,
768 base_raw->size + base_raw->hdrlen);
770 err = got_deltify(&deltas, &ndeltas,
772 raw->size + raw->hdrlen, delta_seed,
773 base->dtab, base_raw->f, base_raw->hdrlen,
774 base_raw->size + base_raw->hdrlen);
776 got_object_raw_close(base_raw);
781 size = delta_size(deltas, ndeltas);
782 if (size + 32 < best_size){
784 * if we already picked a best delta,
789 best_deltas = deltas;
790 best_ndeltas = ndeltas;
792 m->nchain = base->nchain + 1;
794 m->head = base->head;
804 if (best_ndeltas > 0) {
805 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
806 delta_memsize + best_size <= max_delta_memsize) {
807 delta_memsize += best_size;
808 err = encode_delta_in_mem(m, raw, best_deltas,
809 best_ndeltas, best_size, m->prev->size);
811 m->delta_offset = ftello(delta_cache);
812 err = encode_delta(m, raw, best_deltas,
813 best_ndeltas, m->prev->size, delta_cache);
822 got_object_raw_close(raw);
826 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
827 got_deltify_free(meta[i]->dtab);
828 meta[i]->dtab = NULL;
831 got_object_raw_close(raw);
833 got_object_raw_close(base_raw);
834 if (outfd != -1 && close(outfd) == -1 && err == NULL)
835 err = got_error_from_errno("close");
841 static const struct got_error *
842 search_packidx(int *found, struct got_object_id *id,
843 struct got_repository *repo)
845 const struct got_error *err = NULL;
846 struct got_packidx *packidx = NULL;
851 err = got_repo_search_packidx(&packidx, &idx, repo, id);
853 *found = 1; /* object is already packed */
854 else if (err->code == GOT_ERR_NO_OBJ)
859 static const struct got_error *
860 add_object(int want_meta, struct got_object_idset *idset,
861 struct got_object_id *id, const char *path, int obj_type,
862 time_t mtime, uint32_t seed, int loose_obj_only,
863 struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
864 got_pack_progress_cb progress_cb, void *progress_arg,
865 struct got_ratelimit *rl)
867 const struct got_error *err;
868 struct got_pack_meta *m = NULL;
870 if (loose_obj_only) {
872 err = search_packidx(&is_packed, id, repo);
875 if (is_packed && want_meta)
880 err = alloc_meta(&m, id, path, obj_type, mtime, seed);
885 err = report_progress(progress_cb, progress_arg, rl,
886 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
894 err = got_object_idset_add(idset, id, m);
902 static const struct got_error *
903 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
904 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
905 struct got_object_id *tree_id,
906 const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
907 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
908 got_pack_progress_cb progress_cb, void *progress_arg,
909 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
911 const struct got_error *err;
912 struct got_tree_object *tree;
916 err = got_object_open_as_tree(&tree, repo, tree_id);
921 err = report_progress(progress_cb, progress_arg, rl,
922 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
926 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
927 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
928 struct got_object_id *id = got_tree_entry_get_id(e);
929 mode_t mode = got_tree_entry_get_mode(e);
932 err = (*cancel_cb)(cancel_arg);
937 if (got_object_tree_entry_is_submodule(e) ||
938 got_object_idset_contains(idset, id) ||
939 got_object_idset_contains(idset_exclude, id))
942 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
943 got_tree_entry_get_name(e)) == -1) {
944 err = got_error_from_errno("asprintf");
949 struct got_object_qid *qid;
950 err = got_object_qid_alloc(&qid, id);
955 STAILQ_INSERT_TAIL(ids, qid, entry);
956 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
957 err = add_object(want_meta,
958 want_meta ? idset : idset_exclude, id, p,
959 GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
960 repo, ncolored, nfound, ntrees,
961 progress_cb, progress_arg, rl);
972 got_object_tree_close(tree);
977 static const struct got_error *
978 load_tree(int want_meta, struct got_object_idset *idset,
979 struct got_object_idset *idset_exclude,
980 struct got_object_id *tree_id, const char *dpath, time_t mtime,
981 uint32_t seed, struct got_repository *repo, int loose_obj_only,
982 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 = NULL;
987 struct got_object_id_queue tree_ids;
988 struct got_object_qid *qid;
990 if (got_object_idset_contains(idset, tree_id) ||
991 got_object_idset_contains(idset_exclude, tree_id))
994 err = got_object_qid_alloc(&qid, tree_id);
997 qid->data = strdup(dpath);
998 if (qid->data == NULL) {
999 err = got_error_from_errno("strdup");
1000 got_object_qid_free(qid);
1004 STAILQ_INIT(&tree_ids);
1005 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1007 while (!STAILQ_EMPTY(&tree_ids)) {
1010 err = (*cancel_cb)(cancel_arg);
1015 qid = STAILQ_FIRST(&tree_ids);
1016 STAILQ_REMOVE_HEAD(&tree_ids, entry);
1019 if (got_object_idset_contains(idset, &qid->id) ||
1020 got_object_idset_contains(idset_exclude, &qid->id)) {
1022 got_object_qid_free(qid);
1026 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1027 &qid->id, path, GOT_OBJ_TYPE_TREE,
1028 mtime, seed, loose_obj_only, repo,
1029 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1032 got_object_qid_free(qid);
1036 err = load_tree_entries(&tree_ids, want_meta, idset,
1037 idset_exclude, &qid->id,
1038 path, mtime, seed, repo, loose_obj_only, ncolored, nfound,
1039 ntrees, progress_cb, progress_arg, rl,
1040 cancel_cb, cancel_arg);
1042 got_object_qid_free(qid);
1047 STAILQ_FOREACH(qid, &tree_ids, entry)
1049 got_object_id_queue_free(&tree_ids);
1053 static const struct got_error *
1054 load_commit(int want_meta, struct got_object_idset *idset,
1055 struct got_object_idset *idset_exclude,
1056 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1057 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1058 got_pack_progress_cb progress_cb, void *progress_arg,
1059 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1061 const struct got_error *err;
1062 struct got_commit_object *commit;
1064 if (got_object_idset_contains(idset, id) ||
1065 got_object_idset_contains(idset_exclude, id))
1068 if (loose_obj_only) {
1070 err = search_packidx(&is_packed, id, repo);
1073 if (is_packed && want_meta)
1077 err = got_object_open_as_commit(&commit, repo, id);
1081 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1082 id, "", GOT_OBJ_TYPE_COMMIT,
1083 got_object_commit_get_committer_time(commit), seed,
1084 loose_obj_only, repo,
1085 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1089 err = load_tree(want_meta, idset, idset_exclude,
1090 got_object_commit_get_tree_id(commit),
1091 "", got_object_commit_get_committer_time(commit), seed,
1092 repo, loose_obj_only, ncolored, nfound, ntrees,
1093 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1095 got_object_commit_close(commit);
1099 static const struct got_error *
1100 load_tag(int want_meta, struct got_object_idset *idset,
1101 struct got_object_idset *idset_exclude,
1102 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1103 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1104 got_pack_progress_cb progress_cb, void *progress_arg,
1105 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1107 const struct got_error *err;
1108 struct got_tag_object *tag = NULL;
1110 if (got_object_idset_contains(idset, id) ||
1111 got_object_idset_contains(idset_exclude, id))
1114 if (loose_obj_only) {
1116 err = search_packidx(&is_packed, id, repo);
1119 if (is_packed && want_meta)
1123 err = got_object_open_as_tag(&tag, repo, id);
1127 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1128 id, "", GOT_OBJ_TYPE_TAG,
1129 got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1130 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1134 switch (got_object_tag_get_object_type(tag)) {
1135 case GOT_OBJ_TYPE_COMMIT:
1136 err = load_commit(want_meta, idset, idset_exclude,
1137 got_object_tag_get_object_id(tag), repo, seed,
1138 loose_obj_only, ncolored, nfound, ntrees,
1139 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1141 case GOT_OBJ_TYPE_TREE:
1142 err = load_tree(want_meta, idset, idset_exclude,
1143 got_object_tag_get_object_id(tag), "",
1144 got_object_tag_get_tagger_time(tag), seed, repo,
1145 loose_obj_only, ncolored, nfound, ntrees,
1146 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1153 got_object_tag_close(tag);
1157 enum findtwixt_color {
1164 static const int findtwixt_colors[] = {
1171 static const struct got_error *
1172 paint_commit(struct got_object_qid *qid, int color)
1174 if (color < 0 || color >= nitems(findtwixt_colors))
1175 return got_error(GOT_ERR_RANGE);
1177 qid->data = (void *)&findtwixt_colors[color];
1181 static const struct got_error *
1182 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1183 int color, struct got_repository *repo)
1185 const struct got_error *err;
1186 struct got_object_qid *qid;
1188 err = got_object_qid_alloc(&qid, id);
1192 STAILQ_INSERT_TAIL(ids, qid, entry);
1193 return paint_commit(qid, color);
1196 struct append_id_arg {
1197 struct got_object_id **array;
1199 struct got_object_idset *drop;
1200 struct got_object_idset *skip;
1203 static const struct got_error *
1204 append_id(struct got_object_id *id, void *data, void *arg)
1206 struct append_id_arg *a = arg;
1208 if (got_object_idset_contains(a->skip, id) ||
1209 got_object_idset_contains(a->drop, id))
1212 a->array[++a->idx] = got_object_id_dup(id);
1213 if (a->array[a->idx] == NULL)
1214 return got_error_from_errno("got_object_id_dup");
1219 static const struct got_error *
1220 queue_commit_or_tag_id(struct got_object_id *id, int color,
1221 struct got_object_id_queue *ids, struct got_repository *repo)
1223 const struct got_error *err;
1224 struct got_tag_object *tag = NULL;
1227 err = got_object_get_type(&obj_type, repo, id);
1231 if (obj_type == GOT_OBJ_TYPE_TAG) {
1232 err = got_object_open_as_tag(&tag, repo, id);
1235 obj_type = got_object_tag_get_object_type(tag);
1236 id = got_object_tag_get_object_id(tag);
1239 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1240 err = queue_commit_id(ids, id, color, repo);
1246 got_object_tag_close(tag);
1250 static const struct got_error *
1251 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1252 struct got_object_idset *keep, struct got_object_idset *drop,
1253 struct got_object_idset *skip, struct got_repository *repo,
1254 got_pack_progress_cb progress_cb, void *progress_arg,
1255 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1257 const struct got_error *err = NULL;
1258 struct got_commit_object *commit = NULL;
1259 const struct got_object_id_queue *parents;
1260 struct got_object_qid *qid;
1261 int nqueued = nids, nskip = 0;
1263 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1267 err = cancel_cb(cancel_arg);
1272 qid = STAILQ_FIRST(ids);
1273 STAILQ_REMOVE_HEAD(ids, entry);
1275 color = *((int *)qid->data);
1276 if (color == COLOR_SKIP)
1279 if (got_object_idset_contains(skip, &qid->id)) {
1280 got_object_qid_free(qid);
1286 if (got_object_idset_contains(keep, &qid->id)) {
1287 got_object_qid_free(qid);
1290 if (got_object_idset_contains(drop, &qid->id)) {
1291 err = paint_commit(qid, COLOR_SKIP);
1296 err = got_object_idset_add(keep, &qid->id, NULL);
1301 if (got_object_idset_contains(drop, &qid->id)) {
1302 got_object_qid_free(qid);
1305 if (got_object_idset_contains(keep, &qid->id)) {
1306 err = paint_commit(qid, COLOR_SKIP);
1311 err = got_object_idset_add(drop, &qid->id, NULL);
1316 if (!got_object_idset_contains(skip, &qid->id)) {
1317 err = got_object_idset_add(skip, &qid->id,
1324 /* should not happen */
1325 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1326 "%s invalid commit color %d", __func__, color);
1330 err = report_progress(progress_cb, progress_arg, rl,
1331 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1336 err = got_object_open_as_commit(&commit, repo, &qid->id);
1340 parents = got_object_commit_get_parent_ids(commit);
1342 struct got_object_qid *pid;
1343 color = *((int *)qid->data);
1344 STAILQ_FOREACH(pid, parents, entry) {
1345 err = queue_commit_id(ids, &pid->id, color,
1350 if (color == COLOR_SKIP)
1355 got_object_commit_close(commit);
1357 got_object_qid_free(qid);
1361 got_object_commit_close(commit);
1365 static const struct got_error *
1366 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1367 struct got_object_id **head, int nhead,
1368 struct got_object_id **tail, int ntail,
1369 struct got_repository *repo,
1370 got_pack_progress_cb progress_cb, void *progress_arg,
1371 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1373 const struct got_error *err = NULL;
1374 struct got_object_id_queue ids;
1375 struct got_object_idset *keep, *drop, *skip = NULL;
1383 keep = got_object_idset_alloc();
1385 return got_error_from_errno("got_object_idset_alloc");
1387 drop = got_object_idset_alloc();
1389 err = got_error_from_errno("got_object_idset_alloc");
1393 skip = got_object_idset_alloc();
1395 err = got_error_from_errno("got_object_idset_alloc");
1399 for (i = 0; i < nhead; i++) {
1400 struct got_object_id *id = head[i];
1403 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1408 for (i = 0; i < ntail; i++) {
1409 struct got_object_id *id = tail[i];
1412 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1417 err = paint_commits(ncolored, &ids, nhead + ntail,
1418 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1419 cancel_cb, cancel_arg);
1423 nkeep = got_object_idset_num_elements(keep);
1425 struct append_id_arg arg;
1426 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1427 if (arg.array == NULL) {
1428 err = got_error_from_errno("calloc");
1434 err = got_object_idset_for_each(keep, append_id, &arg);
1440 *nres = arg.idx + 1;
1443 got_object_idset_free(keep);
1444 got_object_idset_free(drop);
1446 got_object_idset_free(skip);
1447 got_object_id_queue_free(&ids);
1451 static const struct got_error *
1452 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1453 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1454 struct got_object_id **ours, int nours, struct got_repository *repo,
1455 uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1456 void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1459 const struct got_error *err = NULL;
1460 struct got_object_id **ids = NULL;
1461 int i, nobj = 0, obj_type;
1462 struct got_object_idset *idset_exclude;
1464 idset_exclude = got_object_idset_alloc();
1465 if (idset_exclude == NULL)
1466 return got_error_from_errno("got_object_idset_alloc");
1472 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1473 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1477 for (i = 0; i < ntheirs; i++) {
1478 struct got_object_id *id = theirs[i];
1481 err = got_object_get_type(&obj_type, repo, id);
1484 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1485 err = load_commit(0, idset, idset_exclude, id, repo,
1486 seed, loose_obj_only, ncolored, nfound, ntrees,
1487 progress_cb, progress_arg, rl,
1488 cancel_cb, cancel_arg);
1491 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1492 err = load_tag(0, idset, idset_exclude, id, repo,
1493 seed, loose_obj_only, ncolored, nfound, ntrees,
1494 progress_cb, progress_arg, rl,
1495 cancel_cb, cancel_arg);
1501 for (i = 0; i < nobj; i++) {
1502 err = load_commit(1, idset, idset_exclude, ids[i], repo,
1503 seed, loose_obj_only, ncolored, nfound, ntrees,
1504 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1509 for (i = 0; i < nours; i++) {
1510 struct got_object_id *id = ours[i];
1511 struct got_pack_meta *m;
1514 m = got_object_idset_get(idset, id);
1516 err = got_object_get_type(&obj_type, repo, id);
1520 obj_type = m->obj_type;
1521 if (obj_type != GOT_OBJ_TYPE_TAG)
1523 err = load_tag(1, idset, idset_exclude, id, repo,
1524 seed, loose_obj_only, ncolored, nfound, ntrees,
1525 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1530 for (i = 0; i < nobj; i++) {
1534 got_object_idset_free(idset_exclude);
1538 const struct got_error *
1539 hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
1543 SHA1Update(ctx, buf, len);
1544 n = fwrite(buf, 1, len, f);
1546 return got_ferror(f, GOT_ERR_IO);
1550 const struct got_error *
1551 hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1553 unsigned char buf[65536];
1557 while (remain > 0) {
1558 size_t copylen = MIN(sizeof(buf), remain);
1559 n = fread(buf, 1, copylen, fsrc);
1561 return got_ferror(fsrc, GOT_ERR_IO);
1562 SHA1Update(ctx, buf, copylen);
1563 n = fwrite(buf, 1, copylen, fdst);
1565 return got_ferror(fdst, GOT_ERR_IO);
1572 const struct got_error *
1573 hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1574 FILE *fdst, off_t len, SHA1_CTX *ctx)
1578 if (src_offset + len > src_size)
1579 return got_error(GOT_ERR_RANGE);
1581 SHA1Update(ctx, src + src_offset, len);
1582 n = fwrite(src + src_offset, 1, len, fdst);
1584 return got_ferror(fdst, GOT_ERR_IO);
1590 putbe32(char *b, uint32_t n)
1599 write_order_cmp(const void *pa, const void *pb)
1601 struct got_pack_meta *a, *b, *ahd, *bhd;
1603 a = *(struct got_pack_meta **)pa;
1604 b = *(struct got_pack_meta **)pb;
1605 ahd = (a->head == NULL) ? a : a->head;
1606 bhd = (b->head == NULL) ? b : b->head;
1607 if (bhd->mtime < ahd->mtime)
1609 if (bhd->mtime > ahd->mtime)
1615 if (a->nchain != b->nchain)
1616 return a->nchain - b->nchain;
1617 if (a->mtime < b->mtime)
1619 if (a->mtime > b->mtime)
1621 return got_object_id_cmp(&a->id, &b->id);
1625 reuse_write_order_cmp(const void *pa, const void *pb)
1627 struct got_pack_meta *a, *b;
1629 a = *(struct got_pack_meta **)pa;
1630 b = *(struct got_pack_meta **)pb;
1632 if (a->reused_delta_offset < b->reused_delta_offset)
1634 if (a->reused_delta_offset > b->reused_delta_offset)
1639 static const struct got_error *
1640 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1646 hdr[0] = obj_type << 4;
1647 hdr[0] |= len & 0xf;
1649 for (i = 1; len != 0; i++){
1651 return got_error(GOT_ERR_NO_SPACE);
1652 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1653 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1654 len >>= GOT_DELTA_SIZE_SHIFT;
1662 packoff(char *hdr, off_t off)
1667 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1668 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1669 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1670 GOT_DELTA_SIZE_MORE;
1675 hdr[j++] = rbuf[--i];
1679 static const struct got_error *
1680 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1681 struct got_pack_meta *m)
1683 const struct got_error *err;
1687 if (m->prev->off != 0) {
1688 err = packhdr(&nh, buf, sizeof(buf),
1689 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1692 nh += packoff(buf + nh, m->off - m->prev->off);
1693 err = hwrite(packfile, buf, nh, ctx);
1696 *packfile_size += nh;
1698 err = packhdr(&nh, buf, sizeof(buf),
1699 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1702 err = hwrite(packfile, buf, nh, ctx);
1705 *packfile_size += nh;
1706 err = hwrite(packfile, m->prev->id.sha1,
1707 sizeof(m->prev->id.sha1), ctx);
1710 *packfile_size += sizeof(m->prev->id.sha1);
1716 static const struct got_error *
1717 write_packed_object(off_t *packfile_size, FILE *packfile,
1718 FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1719 struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
1720 struct got_repository *repo)
1722 const struct got_error *err = NULL;
1723 struct got_deflate_checksum csum;
1726 struct got_raw_object *raw = NULL;
1729 csum.output_sha1 = ctx;
1730 csum.output_crc = NULL;
1732 m->off = ftello(packfile);
1733 if (m->delta_len == 0) {
1734 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1737 err = packhdr(&nh, buf, sizeof(buf),
1738 m->obj_type, raw->size);
1741 err = hwrite(packfile, buf, nh, ctx);
1744 *packfile_size += nh;
1745 if (raw->f == NULL) {
1746 err = got_deflate_to_file_mmap(&outlen,
1747 raw->data + raw->hdrlen, 0, raw->size,
1752 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1754 err = got_error_from_errno("fseeko");
1757 err = got_deflate_to_file(&outlen, raw->f,
1758 raw->size, packfile, &csum);
1762 *packfile_size += outlen;
1763 got_object_raw_close(raw);
1765 } else if (m->delta_buf) {
1766 err = deltahdr(packfile_size, ctx, packfile, m);
1769 err = hwrite(packfile, m->delta_buf,
1770 m->delta_compressed_len, ctx);
1773 *packfile_size += m->delta_compressed_len;
1775 m->delta_buf = NULL;
1776 } else if (delta_cache_map) {
1777 err = deltahdr(packfile_size, ctx, packfile, m);
1780 err = hcopy_mmap(delta_cache_map, m->delta_offset,
1781 delta_cache_size, packfile, m->delta_compressed_len,
1785 *packfile_size += m->delta_compressed_len;
1787 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1789 err = got_error_from_errno("fseeko");
1792 err = deltahdr(packfile_size, ctx, packfile, m);
1795 err = hcopy(delta_cache, packfile,
1796 m->delta_compressed_len, ctx);
1799 *packfile_size += m->delta_compressed_len;
1803 got_object_raw_close(raw);
1807 static const struct got_error *
1808 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1809 struct got_pack_meta **deltify, int ndeltify,
1810 struct got_pack_meta **reuse, int nreuse,
1811 int ncolored, int nfound, int ntrees, int nours,
1812 struct got_repository *repo,
1813 got_pack_progress_cb progress_cb, void *progress_arg,
1814 struct got_ratelimit *rl,
1815 got_cancel_cb cancel_cb, void *cancel_arg)
1817 const struct got_error *err = NULL;
1820 struct got_pack_meta *m;
1823 off_t packfile_size = 0;
1825 int delta_cache_fd = -1;
1826 uint8_t *delta_cache_map = NULL;
1827 size_t delta_cache_size = 0;
1831 #ifndef GOT_PACK_NO_MMAP
1832 delta_cache_fd = dup(fileno(delta_cache));
1833 if (delta_cache_fd != -1) {
1835 if (fstat(delta_cache_fd, &sb) == -1) {
1836 err = got_error_from_errno("fstat");
1839 if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1840 delta_cache_map = mmap(NULL, sb.st_size,
1841 PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1842 if (delta_cache_map == MAP_FAILED) {
1843 if (errno != ENOMEM) {
1844 err = got_error_from_errno("mmap");
1847 delta_cache_map = NULL; /* fallback on stdio */
1849 delta_cache_size = (size_t)sb.st_size;
1853 err = hwrite(packfile, "PACK", 4, &ctx);
1856 putbe32(buf, GOT_PACKFILE_VERSION);
1857 err = hwrite(packfile, buf, 4, &ctx);
1860 putbe32(buf, ndeltify + nreuse);
1861 err = hwrite(packfile, buf, 4, &ctx);
1865 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1867 for (i = 0; i < ndeltify; i++) {
1868 err = report_progress(progress_cb, progress_arg, rl,
1869 ncolored, nfound, ntrees, packfile_size, nours,
1870 ndeltify + nreuse, ndeltify + nreuse, i);
1874 err = write_packed_object(&packfile_size, packfile,
1875 delta_cache, delta_cache_map, delta_cache_size,
1876 m, &outfd, &ctx, repo);
1881 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1882 reuse_write_order_cmp);
1883 for (i = 0; i < nreuse; i++) {
1884 err = report_progress(progress_cb, progress_arg, rl,
1885 ncolored, nfound, ntrees, packfile_size, nours,
1886 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1890 err = write_packed_object(&packfile_size, packfile,
1891 delta_cache, delta_cache_map, delta_cache_size,
1892 m, &outfd, &ctx, repo);
1897 SHA1Final(pack_sha1, &ctx);
1898 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1899 if (n != SHA1_DIGEST_LENGTH)
1900 err = got_ferror(packfile, GOT_ERR_IO);
1901 packfile_size += SHA1_DIGEST_LENGTH;
1902 packfile_size += sizeof(struct got_packfile_hdr);
1904 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1905 packfile_size, nours, ndeltify + nreuse,
1906 ndeltify + nreuse, ndeltify + nreuse);
1911 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1912 err = got_error_from_errno("close");
1913 if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1914 err = got_error_from_errno("munmap");
1915 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1916 err = got_error_from_errno("close");
1920 static const struct got_error *
1921 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1923 struct got_pack_meta *m = data;
1924 struct got_pack_metavec *v = arg;
1926 if (m->reused_delta_offset != 0)
1929 return add_meta(m, v);
1932 const struct got_error *
1933 got_pack_create(uint8_t *packsha1, FILE *packfile,
1934 struct got_object_id **theirs, int ntheirs,
1935 struct got_object_id **ours, int nours,
1936 struct got_repository *repo, int loose_obj_only, int allow_empty,
1937 got_pack_progress_cb progress_cb, void *progress_arg,
1938 got_cancel_cb cancel_cb, void *cancel_arg)
1940 const struct got_error *err;
1941 int delta_cache_fd = -1;
1942 FILE *delta_cache = NULL;
1943 struct got_object_idset *idset;
1944 struct got_ratelimit rl;
1945 struct got_pack_metavec deltify, reuse;
1946 int ncolored = 0, nfound = 0, ntrees = 0;
1950 seed = arc4random();
1952 memset(&deltify, 0, sizeof(deltify));
1953 memset(&reuse, 0, sizeof(reuse));
1955 got_ratelimit_init(&rl, 0, 500);
1957 idset = got_object_idset_alloc();
1959 return got_error_from_errno("got_object_idset_alloc");
1961 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1962 ntheirs, ours, nours, repo, seed, loose_obj_only,
1963 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1968 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1969 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1974 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1975 err = got_error(GOT_ERR_CANNOT_PACK);
1979 delta_cache_fd = got_opentempfd();
1980 if (delta_cache_fd == -1) {
1981 err = got_error_from_errno("got_opentemp");
1986 reuse.meta = calloc(reuse.metasz,
1987 sizeof(struct got_pack_meta *));
1988 if (reuse.meta == NULL) {
1989 err = got_error_from_errno("calloc");
1993 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1994 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1995 cancel_cb, cancel_arg);
1999 delta_cache = fdopen(delta_cache_fd, "a+");
2000 if (delta_cache == NULL) {
2001 err = got_error_from_errno("fdopen");
2004 delta_cache_fd = -1;
2006 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
2007 err = got_error_from_errno("fseeko");
2011 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
2013 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
2014 if (deltify.meta == NULL) {
2015 err = got_error_from_errno("calloc");
2018 deltify.metasz = ndeltify;
2020 err = got_object_idset_for_each(idset, add_meta_idset_cb,
2024 if (deltify.nmeta > 0) {
2025 err = pick_deltas(deltify.meta, deltify.nmeta,
2026 ncolored, nfound, ntrees, nours, reuse.nmeta,
2027 delta_cache, repo, progress_cb, progress_arg, &rl,
2028 cancel_cb, cancel_arg);
2034 if (fflush(delta_cache) == EOF) {
2035 err = got_error_from_errno("fflush");
2038 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
2039 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
2040 nours, repo, progress_cb, progress_arg, &rl,
2041 cancel_cb, cancel_arg);
2045 free_nmeta(deltify.meta, deltify.nmeta);
2046 free_nmeta(reuse.meta, reuse.nmeta);
2047 got_object_idset_free(idset);
2048 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2049 err = got_error_from_errno("close");
2050 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2051 err = got_error_from_errno("fclose");