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>
35 #include "got_error.h"
36 #include "got_cancel.h"
37 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository_admin.h"
41 #include "got_opentemp.h"
43 #include "got_lib_deltify.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_deflate.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_ratelimit.h"
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 struct got_pack_meta {
63 struct got_object_id id;
69 /* The best delta we picked */
70 struct got_pack_meta *head;
71 struct got_pack_meta *prev;
72 unsigned char *delta_buf; /* if not encoded in delta cache file */
73 off_t delta_offset; /* offset in delta cache file */
74 off_t delta_len; /* encoded delta length */
77 /* Only used for delta window */
78 struct got_delta_table *dtab;
80 /* Only used for writing offset deltas */
84 struct got_pack_metavec {
85 struct got_pack_meta **meta;
90 static const struct got_error *
91 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
92 const char *path, int obj_type, time_t mtime)
94 const struct got_error *err = NULL;
95 struct got_pack_meta *m;
99 m = calloc(1, sizeof(*m));
101 return got_error_from_errno("calloc");
103 memcpy(&m->id, id, sizeof(m->id));
105 m->path = strdup(path);
106 if (m->path == NULL) {
107 err = got_error_from_errno("strdup");
112 m->obj_type = obj_type;
119 clear_meta(struct got_pack_meta *meta)
125 free(meta->delta_buf);
126 meta->delta_buf = NULL;
130 free_nmeta(struct got_pack_meta **meta, int nmeta)
134 for (i = 0; i < nmeta; i++)
140 delta_order_cmp(const void *pa, const void *pb)
142 struct got_pack_meta *a, *b;
145 a = *(struct got_pack_meta **)pa;
146 b = *(struct got_pack_meta **)pb;
148 if (a->obj_type != b->obj_type)
149 return a->obj_type - b->obj_type;
150 cmp = strcmp(a->path, b->path);
153 if (a->mtime != b->mtime)
154 return a->mtime - b->mtime;
155 return got_object_id_cmp(&a->id, &b->id);
159 delta_size(struct got_delta_instruction *deltas, int ndeltas)
163 for (i = 0; i < ndeltas; i++) {
165 size += GOT_DELTA_SIZE_SHIFT;
167 size += deltas[i].len + 1;
172 static const struct got_error *
173 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
177 if (*len + nseg >= *sz) {
178 while (*len + nseg >= *sz)
180 n = realloc(*p, *sz);
182 return got_error_from_errno("realloc");
185 memcpy(*p + *len, seg, nseg);
190 static const struct got_error *
191 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
192 struct got_delta_instruction *deltas, int ndeltas,
193 off_t delta_size, off_t base_size)
195 const struct got_error *err;
196 unsigned char buf[16], *bp;
200 struct got_delta_instruction *d;
202 m->delta_buf = malloc(delta_size);
203 if (m->delta_buf == NULL)
204 return got_error_from_errno("calloc");
206 /* base object size */
207 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
208 n = base_size >> GOT_DELTA_SIZE_SHIFT;
209 for (i = 1; n > 0; i++) {
210 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
211 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
212 n >>= GOT_DELTA_SIZE_SHIFT;
214 err = append(&m->delta_buf, &len, &delta_size, buf, i);
218 /* target object size */
219 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
220 n = o->size >> GOT_DELTA_SIZE_SHIFT;
221 for (i = 1; n > 0; i++) {
222 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
223 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
224 n >>= GOT_DELTA_SIZE_SHIFT;
226 err = append(&m->delta_buf, &len, &delta_size, buf, i);
230 for (j = 0; j < ndeltas; j++) {
235 buf[0] = GOT_DELTA_BASE_COPY;
236 for (i = 0; i < 4; i++) {
237 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
246 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
247 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
248 for (i = 0; i < 3 && n > 0; i++) {
249 buf[0] |= 1 << (i + 4);
254 err = append(&m->delta_buf, &len, &delta_size,
258 } else if (o->f == NULL) {
260 while (n != d->len) {
261 buf[0] = (d->len - n < 127) ? d->len - n : 127;
262 err = append(&m->delta_buf, &len, &delta_size,
266 err = append(&m->delta_buf, &len, &delta_size,
267 o->data + o->hdrlen + d->offset + n,
276 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
277 return got_error_from_errno("fseeko");
279 while (n != d->len) {
280 buf[0] = (d->len - n < 127) ? d->len - n : 127;
281 err = append(&m->delta_buf, &len, &delta_size,
285 r = fread(content, 1, buf[0], o->f);
287 return got_ferror(o->f, GOT_ERR_IO);
288 err = append(&m->delta_buf, &len, &delta_size,
301 static const struct got_error *
302 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
303 struct got_delta_instruction *deltas, int ndeltas,
304 off_t base_size, FILE *f)
306 unsigned char buf[16], *bp;
310 struct got_delta_instruction *d;
312 /* base object size */
313 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
314 n = base_size >> GOT_DELTA_SIZE_SHIFT;
315 for (i = 1; n > 0; i++) {
316 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
317 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
318 n >>= GOT_DELTA_SIZE_SHIFT;
320 w = fwrite(buf, 1, i, f);
322 return got_ferror(f, GOT_ERR_IO);
324 /* target object size */
325 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
326 n = o->size >> GOT_DELTA_SIZE_SHIFT;
327 for (i = 1; n > 0; i++) {
328 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
329 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
330 n >>= GOT_DELTA_SIZE_SHIFT;
332 w = fwrite(buf, 1, i, f);
334 return got_ferror(f, GOT_ERR_IO);
336 for (j = 0; j < ndeltas; j++) {
341 buf[0] = GOT_DELTA_BASE_COPY;
342 for (i = 0; i < 4; i++) {
343 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
352 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
353 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
354 for (i = 0; i < 3 && n > 0; i++) {
355 buf[0] |= 1 << (i + 4);
360 w = fwrite(buf, 1, bp - buf, f);
362 return got_ferror(f, GOT_ERR_IO);
363 } else if (o->f == NULL) {
365 while (n != d->len) {
366 buf[0] = (d->len - n < 127) ? d->len - n : 127;
367 w = fwrite(buf, 1, 1, f);
369 return got_ferror(f, GOT_ERR_IO);
370 w = fwrite(o->data + o->hdrlen + d->offset + n,
373 return got_ferror(f, GOT_ERR_IO);
379 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
380 return got_error_from_errno("fseeko");
382 while (n != d->len) {
383 buf[0] = (d->len - n < 127) ? d->len - n : 127;
384 w = fwrite(buf, 1, 1, f);
386 return got_ferror(f, GOT_ERR_IO);
387 r = fread(content, 1, buf[0], o->f);
389 return got_ferror(o->f, GOT_ERR_IO);
390 w = fwrite(content, 1, buf[0], f);
392 return got_ferror(f, GOT_ERR_IO);
398 m->delta_len = ftello(f) - m->delta_offset;
402 static const struct got_error *
403 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
404 struct got_ratelimit *rl, off_t packfile_size, int ncommits,
405 int nobj_total, int obj_deltify, int nobj_written)
407 const struct got_error *err;
410 if (progress_cb == NULL)
413 err = got_ratelimit_check(&elapsed, rl);
417 return progress_cb(progress_arg, packfile_size, ncommits,
418 nobj_total, obj_deltify, nobj_written);
421 static const struct got_error *
422 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
423 FILE *delta_cache, struct got_repository *repo,
424 got_pack_progress_cb progress_cb, void *progress_arg,
425 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
427 const struct got_error *err = NULL;
428 struct got_pack_meta *m = NULL, *base = NULL;
429 struct got_raw_object *raw = NULL, *base_raw = NULL;
430 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
431 int i, j, ndeltas, best_ndeltas;
432 off_t size, best_size;
433 const int max_base_candidates = 3;
436 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
437 for (i = 0; i < nmeta; i++) {
439 err = (*cancel_cb)(cancel_arg);
443 err = report_progress(progress_cb, progress_arg, rl,
444 0L, nours, nmeta, i, 0);
449 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
450 m->obj_type == GOT_OBJ_TYPE_TAG)
453 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
458 if (raw->f == NULL) {
459 err = got_deltify_init_mem(&m->dtab, raw->data,
460 raw->hdrlen, raw->size + raw->hdrlen);
462 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
463 raw->size + raw->hdrlen);
468 if (i > max_base_candidates) {
469 struct got_pack_meta *n = NULL;
470 n = meta[i - (max_base_candidates + 1)];
471 got_deltify_free(n->dtab);
475 best_size = raw->size;
477 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
479 err = (*cancel_cb)(cancel_arg);
484 /* long chains make unpacking slow, avoid such bases */
485 if (base->nchain >= 128 ||
486 base->obj_type != m->obj_type)
489 err = got_object_raw_open(&base_raw, &outfd, repo,
493 if (raw->f == NULL && base_raw->f == NULL) {
494 err = got_deltify_mem_mem(&deltas, &ndeltas,
495 raw->data, raw->hdrlen,
496 raw->size + raw->hdrlen,
497 base->dtab, base_raw->data,
499 base_raw->size + base_raw->hdrlen);
500 } else if (raw->f == NULL) {
501 err = got_deltify_mem_file(&deltas, &ndeltas,
502 raw->data, raw->hdrlen,
503 raw->size + raw->hdrlen,
504 base->dtab, base_raw->f,
506 base_raw->size + base_raw->hdrlen);
507 } else if (base_raw->f == NULL) {
508 err = got_deltify_file_mem(&deltas, &ndeltas,
510 raw->size + raw->hdrlen,
511 base->dtab, base_raw->data,
513 base_raw->size + base_raw->hdrlen);
515 err = got_deltify(&deltas, &ndeltas,
517 raw->size + raw->hdrlen,
518 base->dtab, base_raw->f, base_raw->hdrlen,
519 base_raw->size + base_raw->hdrlen);
521 got_object_raw_close(base_raw);
526 size = delta_size(deltas, ndeltas);
527 if (size + 32 < best_size){
529 * if we already picked a best delta,
534 best_deltas = deltas;
535 best_ndeltas = ndeltas;
537 m->nchain = base->nchain + 1;
539 m->head = base->head;
549 if (best_ndeltas > 0) {
550 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
551 err = encode_delta_in_mem(m, raw, best_deltas,
552 best_ndeltas, best_size, m->prev->size);
554 m->delta_offset = ftello(delta_cache);
555 err = encode_delta(m, raw, best_deltas,
556 best_ndeltas, m->prev->size, delta_cache);
565 got_object_raw_close(raw);
569 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
570 got_deltify_free(meta[i]->dtab);
571 meta[i]->dtab = NULL;
574 got_object_raw_close(raw);
576 got_object_raw_close(base_raw);
577 if (outfd != -1 && close(outfd) == -1 && err == NULL)
578 err = got_error_from_errno("close");
584 static const struct got_error *
585 search_packidx(int *found, struct got_object_id *id,
586 struct got_repository *repo)
588 const struct got_error *err = NULL;
589 struct got_packidx *packidx = NULL;
594 err = got_repo_search_packidx(&packidx, &idx, repo, id);
596 *found = 1; /* object is already packed */
597 else if (err->code == GOT_ERR_NO_OBJ)
602 static const int obj_types[] = {
608 GOT_OBJ_TYPE_OFFSET_DELTA,
609 GOT_OBJ_TYPE_REF_DELTA
612 static const struct got_error *
613 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
614 struct got_object_id *id, const char *path, int obj_type,
615 time_t mtime, int loose_obj_only, struct got_repository *repo)
617 const struct got_error *err;
618 struct got_pack_meta *m;
620 if (loose_obj_only) {
622 err = search_packidx(&is_packed, id, repo);
629 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
636 err = alloc_meta(&m, id, path, obj_type, mtime);
640 if (v->nmeta == v->metasz){
641 size_t newsize = 2 * v->metasz;
642 struct got_pack_meta **new;
643 new = reallocarray(v->meta, newsize, sizeof(*new));
645 err = got_error_from_errno("reallocarray");
656 v->meta[v->nmeta++] = m;
661 static const struct got_error *
662 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
663 struct got_object_idset *idset, struct got_object_id *tree_id,
664 const char *dpath, time_t mtime, struct got_repository *repo,
665 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
667 const struct got_error *err;
668 struct got_tree_object *tree;
672 err = got_object_open_as_tree(&tree, repo, tree_id);
676 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
677 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
678 struct got_object_id *id = got_tree_entry_get_id(e);
679 mode_t mode = got_tree_entry_get_mode(e);
682 err = (*cancel_cb)(cancel_arg);
687 if (got_object_tree_entry_is_submodule(e) ||
688 got_object_idset_contains(idset, id))
691 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
692 got_tree_entry_get_name(e)) == -1) {
693 err = got_error_from_errno("asprintf");
698 struct got_object_qid *qid;
699 err = got_object_qid_alloc(&qid, id);
702 STAILQ_INSERT_TAIL(ids, qid, entry);
703 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
704 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
705 mtime, loose_obj_only, repo);
713 got_object_tree_close(tree);
718 static const struct got_error *
719 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
720 struct got_object_id *tree_id, const char *dpath, time_t mtime,
721 int loose_obj_only, struct got_repository *repo,
722 got_cancel_cb cancel_cb, void *cancel_arg)
724 const struct got_error *err = NULL;
725 struct got_object_id_queue tree_ids;
726 struct got_object_qid *qid;
728 if (got_object_idset_contains(idset, tree_id))
731 err = got_object_qid_alloc(&qid, tree_id);
735 STAILQ_INIT(&tree_ids);
736 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
738 while (!STAILQ_EMPTY(&tree_ids)) {
740 err = (*cancel_cb)(cancel_arg);
745 qid = STAILQ_FIRST(&tree_ids);
746 STAILQ_REMOVE_HEAD(&tree_ids, entry);
748 if (got_object_idset_contains(idset, qid->id)) {
749 got_object_qid_free(qid);
753 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
754 mtime, loose_obj_only, repo);
756 got_object_qid_free(qid);
760 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
761 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
762 got_object_qid_free(qid);
767 got_object_id_queue_free(&tree_ids);
771 static const struct got_error *
772 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
773 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
774 got_cancel_cb cancel_cb, void *cancel_arg)
776 const struct got_error *err;
777 struct got_commit_object *commit;
779 if (got_object_idset_contains(idset, id))
782 if (loose_obj_only) {
784 err = search_packidx(&is_packed, id, repo);
791 err = got_object_open_as_commit(&commit, repo, id);
795 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
796 got_object_commit_get_committer_time(commit),
797 loose_obj_only, repo);
801 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
802 "", got_object_commit_get_committer_time(commit),
803 loose_obj_only, repo, cancel_cb, cancel_arg);
805 got_object_commit_close(commit);
809 static const struct got_error *
810 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
811 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
812 got_cancel_cb cancel_cb, void *cancel_arg)
814 const struct got_error *err;
815 struct got_tag_object *tag = NULL;
817 if (got_object_idset_contains(idset, id))
820 if (loose_obj_only) {
822 err = search_packidx(&is_packed, id, repo);
829 err = got_object_open_as_tag(&tag, repo, id);
833 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
834 got_object_tag_get_tagger_time(tag),
835 loose_obj_only, repo);
839 switch (got_object_tag_get_object_type(tag)) {
840 case GOT_OBJ_TYPE_COMMIT:
841 err = load_commit(v, idset,
842 got_object_tag_get_object_id(tag), repo,
843 loose_obj_only, cancel_cb, cancel_arg);
845 case GOT_OBJ_TYPE_TREE:
846 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
847 "", got_object_tag_get_tagger_time(tag),
848 loose_obj_only, repo, cancel_cb, cancel_arg);
855 got_object_tag_close(tag);
859 enum findtwixt_color {
864 static const int findtwixt_colors[] = {
870 static const struct got_error *
871 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
872 int color, struct got_repository *repo)
874 const struct got_error *err;
875 struct got_object_qid *qid;
877 err = got_object_qid_alloc(&qid, id);
881 STAILQ_INSERT_TAIL(ids, qid, entry);
882 qid->data = (void *)&findtwixt_colors[color];
886 static const struct got_error *
887 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
888 struct got_object_id *id, struct got_repository *repo,
889 got_cancel_cb cancel_cb, void *cancel_arg)
891 const struct got_error *err = NULL;
892 struct got_commit_object *commit;
893 const struct got_object_id_queue *parents;
894 struct got_object_id_queue ids;
895 struct got_object_qid *qid;
899 err = got_object_qid_alloc(&qid, id);
902 STAILQ_INSERT_HEAD(&ids, qid, entry);
904 while (!STAILQ_EMPTY(&ids)) {
906 err = (*cancel_cb)(cancel_arg);
911 qid = STAILQ_FIRST(&ids);
912 STAILQ_REMOVE_HEAD(&ids, entry);
914 if (got_object_idset_contains(drop, qid->id)) {
915 got_object_qid_free(qid);
919 err = got_object_idset_add(drop, qid->id,
920 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
922 got_object_qid_free(qid);
926 if (!got_object_idset_contains(keep, qid->id)) {
927 got_object_qid_free(qid);
931 err = got_object_open_as_commit(&commit, repo, qid->id);
932 got_object_qid_free(qid);
936 parents = got_object_commit_get_parent_ids(commit);
938 err = got_object_id_queue_copy(parents, &ids);
940 got_object_commit_close(commit);
944 got_object_commit_close(commit);
947 got_object_id_queue_free(&ids);
951 struct append_id_arg {
952 struct got_object_id **array;
956 static const struct got_error *
957 append_id(struct got_object_id *id, void *data, void *arg)
959 struct append_id_arg *a = arg;
961 a->array[a->idx] = got_object_id_dup(id);
962 if (a->array[a->idx] == NULL)
963 return got_error_from_errno("got_object_id_dup");
969 static const struct got_error *
970 findtwixt(struct got_object_id ***res, int *nres,
971 struct got_object_id **head, int nhead,
972 struct got_object_id **tail, int ntail,
973 struct got_repository *repo,
974 got_cancel_cb cancel_cb, void *cancel_arg)
976 const struct got_error *err = NULL;
977 struct got_object_id_queue ids;
978 struct got_object_idset *keep, *drop;
979 struct got_object_qid *qid;
980 int i, ncolor, nkeep, obj_type;
986 keep = got_object_idset_alloc();
988 return got_error_from_errno("got_object_idset_alloc");
990 drop = got_object_idset_alloc();
992 err = got_error_from_errno("got_object_idset_alloc");
996 for (i = 0; i < nhead; i++) {
997 struct got_object_id *id = head[i];
1000 err = got_object_get_type(&obj_type, repo, id);
1003 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1005 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1009 for (i = 0; i < ntail; i++) {
1010 struct got_object_id *id = tail[i];
1013 err = got_object_get_type(&obj_type, repo, id);
1016 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1018 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1023 while (!STAILQ_EMPTY(&ids)) {
1025 qid = STAILQ_FIRST(&ids);
1026 qcolor = *((int *)qid->data);
1028 if (got_object_idset_contains(drop, qid->id))
1029 ncolor = COLOR_DROP;
1030 else if (got_object_idset_contains(keep, qid->id))
1031 ncolor = COLOR_KEEP;
1033 ncolor = COLOR_BLANK;
1035 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1036 qcolor == COLOR_KEEP)) {
1037 STAILQ_REMOVE_HEAD(&ids, entry);
1038 got_object_qid_free(qid);
1042 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1043 err = drop_commit(keep, drop, qid->id, repo,
1044 cancel_cb, cancel_arg);
1047 } else if (ncolor == COLOR_BLANK) {
1048 struct got_commit_object *commit;
1049 struct got_object_id *id;
1050 const struct got_object_id_queue *parents;
1051 struct got_object_qid *pid;
1053 id = got_object_id_dup(qid->id);
1055 err = got_error_from_errno("got_object_id_dup");
1058 if (qcolor == COLOR_KEEP)
1059 err = got_object_idset_add(keep, id,
1060 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1062 err = got_object_idset_add(drop, id,
1063 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1069 err = got_object_open_as_commit(&commit, repo, id);
1074 parents = got_object_commit_get_parent_ids(commit);
1076 STAILQ_FOREACH(pid, parents, entry) {
1077 err = queue_commit_id(&ids, pid->id,
1085 got_object_commit_close(commit);
1088 /* should not happen */
1089 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1090 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1094 STAILQ_REMOVE_HEAD(&ids, entry);
1095 got_object_qid_free(qid);
1098 nkeep = got_object_idset_num_elements(keep);
1100 struct append_id_arg arg;
1101 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1102 if (arg.array == NULL) {
1103 err = got_error_from_errno("calloc");
1107 err = got_object_idset_for_each(keep, append_id, &arg);
1116 got_object_idset_free(keep);
1117 got_object_idset_free(drop);
1118 got_object_id_queue_free(&ids);
1122 static const struct got_error *
1123 read_meta(struct got_pack_meta ***meta, int *nmeta,
1124 struct got_object_id **theirs, int ntheirs,
1125 struct got_object_id **ours, int nours, struct got_repository *repo,
1126 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1127 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1129 const struct got_error *err = NULL;
1130 struct got_object_id **ids = NULL;
1131 struct got_object_idset *idset;
1132 int i, nobj = 0, obj_type;
1133 struct got_pack_metavec v;
1138 idset = got_object_idset_alloc();
1140 return got_error_from_errno("got_object_idset_alloc");
1144 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
1145 if (v.meta == NULL) {
1146 err = got_error_from_errno("calloc");
1150 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1151 cancel_cb, cancel_arg);
1152 if (err || nobj == 0)
1155 for (i = 0; i < ntheirs; i++) {
1156 struct got_object_id *id = theirs[i];
1159 err = got_object_get_type(&obj_type, repo, id);
1162 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1164 err = load_commit(NULL, idset, id, repo,
1165 loose_obj_only, cancel_cb, cancel_arg);
1168 err = report_progress(progress_cb, progress_arg, rl,
1169 0L, nours, v.nmeta, 0, 0);
1174 for (i = 0; i < ntheirs; i++) {
1175 struct got_object_id *id = theirs[i];
1179 cached_type = got_object_idset_get(idset, id);
1180 if (cached_type == NULL) {
1181 err = got_object_get_type(&obj_type, repo, id);
1185 obj_type = *cached_type;
1186 if (obj_type != GOT_OBJ_TYPE_TAG)
1188 err = load_tag(NULL, idset, id, repo,
1189 loose_obj_only, cancel_cb, cancel_arg);
1192 err = report_progress(progress_cb, progress_arg, rl,
1193 0L, nours, v.nmeta, 0, 0);
1198 for (i = 0; i < nobj; i++) {
1199 err = load_commit(&v, idset, ids[i], repo,
1200 loose_obj_only, cancel_cb, cancel_arg);
1205 err = report_progress(progress_cb, progress_arg, rl,
1206 0L, nours, v.nmeta, 0, 0);
1211 for (i = 0; i < nours; i++) {
1212 struct got_object_id *id = ours[i];
1216 cached_type = got_object_idset_get(idset, id);
1217 if (cached_type == NULL) {
1218 err = got_object_get_type(&obj_type, repo, id);
1222 obj_type = *cached_type;
1223 if (obj_type != GOT_OBJ_TYPE_TAG)
1225 err = load_tag(&v, idset, id, repo,
1226 loose_obj_only, cancel_cb, cancel_arg);
1229 err = report_progress(progress_cb, progress_arg, rl,
1230 0L, nours, v.nmeta, 0, 0);
1236 err = progress_cb(progress_arg, 0L, nours, v.nmeta, 0, 0);
1241 for (i = 0; i < nobj; i++) {
1245 got_object_idset_free(idset);
1255 const struct got_error *
1256 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1260 SHA1Update(ctx, buf, len);
1261 n = fwrite(buf, 1, len, f);
1263 return got_ferror(f, GOT_ERR_IO);
1268 putbe32(char *b, uint32_t n)
1277 write_order_cmp(const void *pa, const void *pb)
1279 struct got_pack_meta *a, *b, *ahd, *bhd;
1281 a = *(struct got_pack_meta **)pa;
1282 b = *(struct got_pack_meta **)pb;
1283 ahd = (a->head == NULL) ? a : a->head;
1284 bhd = (b->head == NULL) ? b : b->head;
1285 if (ahd->mtime != bhd->mtime)
1286 return bhd->mtime - ahd->mtime;
1288 return (uintptr_t)bhd - (uintptr_t)ahd;
1289 if (a->nchain != b->nchain)
1290 return a->nchain - b->nchain;
1291 return a->mtime - b->mtime;
1294 static const struct got_error *
1295 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1301 hdr[0] = obj_type << 4;
1302 hdr[0] |= len & 0xf;
1304 for (i = 1; len != 0; i++){
1306 return got_error(GOT_ERR_NO_SPACE);
1307 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1308 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1309 len >>= GOT_DELTA_SIZE_SHIFT;
1317 packoff(char *hdr, off_t off)
1322 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1323 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1324 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1325 GOT_DELTA_SIZE_MORE;
1330 hdr[j++] = rbuf[--i];
1334 static const struct got_error *
1335 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1336 struct got_pack_meta *m, int use_offset_deltas)
1338 const struct got_error *err;
1342 if (use_offset_deltas && m->prev->off != 0) {
1343 err = packhdr(&nh, buf, sizeof(buf),
1344 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1347 nh += packoff(buf + nh, m->off - m->prev->off);
1348 err = hwrite(packfile, buf, nh, ctx);
1351 *packfile_size += nh;
1353 err = packhdr(&nh, buf, sizeof(buf),
1354 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1357 err = hwrite(packfile, buf, nh, ctx);
1360 *packfile_size += nh;
1361 err = hwrite(packfile, m->prev->id.sha1,
1362 sizeof(m->prev->id.sha1), ctx);
1365 *packfile_size += sizeof(m->prev->id.sha1);
1371 static const struct got_error *
1372 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1373 struct got_pack_meta **meta, int nmeta, int nours,
1374 int use_offset_deltas, struct got_repository *repo,
1375 got_pack_progress_cb progress_cb, void *progress_arg,
1376 struct got_ratelimit *rl,
1377 got_cancel_cb cancel_cb, void *cancel_arg)
1379 const struct got_error *err = NULL;
1382 struct got_pack_meta *m;
1383 struct got_raw_object *raw = NULL;
1384 FILE *delta_file = NULL;
1387 struct got_deflate_checksum csum;
1388 off_t packfile_size = 0;
1392 csum.output_sha1 = &ctx;
1393 csum.output_crc = NULL;
1395 err = hwrite(packfile, "PACK", 4, &ctx);
1398 putbe32(buf, GOT_PACKFILE_VERSION);
1399 err = hwrite(packfile, buf, 4, &ctx);
1402 putbe32(buf, nmeta);
1403 err = hwrite(packfile, buf, 4, &ctx);
1406 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1407 for (i = 0; i < nmeta; i++) {
1408 err = report_progress(progress_cb, progress_arg, rl,
1409 packfile_size, nours, nmeta, nmeta, i);
1413 m->off = ftello(packfile);
1414 if (m->delta_len == 0) {
1415 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1418 err = packhdr(&nh, buf, sizeof(buf),
1419 m->obj_type, raw->size);
1422 err = hwrite(packfile, buf, nh, &ctx);
1425 packfile_size += nh;
1426 if (raw->f == NULL) {
1427 err = got_deflate_to_file_mmap(&outlen,
1428 raw->data + raw->hdrlen, 0, raw->size,
1433 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1435 err = got_error_from_errno("fseeko");
1438 err = got_deflate_to_file(&outlen, raw->f,
1443 packfile_size += outlen;
1444 got_object_raw_close(raw);
1446 } else if (m->delta_buf) {
1447 err = deltahdr(&packfile_size, &ctx, packfile,
1448 m, use_offset_deltas);
1451 err = got_deflate_to_file_mmap(&outlen,
1452 m->delta_buf, 0, m->delta_len, packfile, &csum);
1455 packfile_size += outlen;
1457 m->delta_buf = NULL;
1460 if (delta_file == NULL) {
1461 delta_file = got_opentemp();
1462 if (delta_file == NULL) {
1463 err = got_error_from_errno(
1468 if (ftruncate(fileno(delta_file), 0L) == -1) {
1469 err = got_error_from_errno("ftruncate");
1472 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1473 err = got_error_from_errno("fseeko");
1476 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1478 err = got_error_from_errno("fseeko");
1481 remain = m->delta_len;
1482 while (remain > 0) {
1483 char delta_buf[8192];
1485 n = MIN(remain, sizeof(delta_buf));
1486 r = fread(delta_buf, 1, n, delta_cache);
1488 err = got_ferror(delta_cache,
1492 w = fwrite(delta_buf, 1, n, delta_file);
1494 err = got_ferror(delta_file,
1500 err = deltahdr(&packfile_size, &ctx, packfile,
1501 m, use_offset_deltas);
1504 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1505 err = got_error_from_errno("fseeko");
1508 err = got_deflate_to_file(&outlen, delta_file,
1512 packfile_size += outlen;
1515 SHA1Final(pack_sha1, &ctx);
1516 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1517 if (n != SHA1_DIGEST_LENGTH)
1518 err = got_ferror(packfile, GOT_ERR_IO);
1519 packfile_size += SHA1_DIGEST_LENGTH;
1520 packfile_size += sizeof(struct got_packfile_hdr);
1522 err = progress_cb(progress_arg, packfile_size, nours,
1523 nmeta, nmeta, nmeta);
1528 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1529 err = got_error_from_errno("fclose");
1531 got_object_raw_close(raw);
1532 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1533 err = got_error_from_errno("close");
1537 const struct got_error *
1538 got_pack_create(uint8_t *packsha1, FILE *packfile,
1539 struct got_object_id **theirs, int ntheirs,
1540 struct got_object_id **ours, int nours,
1541 struct got_repository *repo, int loose_obj_only, int allow_empty,
1542 got_pack_progress_cb progress_cb, void *progress_arg,
1543 got_cancel_cb cancel_cb, void *cancel_arg)
1545 const struct got_error *err;
1546 struct got_pack_meta **meta;
1548 FILE *delta_cache = NULL;
1549 struct got_ratelimit rl;
1551 got_ratelimit_init(&rl, 0, 500);
1553 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1554 loose_obj_only, progress_cb, progress_arg, &rl,
1555 cancel_cb, cancel_arg);
1559 if (nmeta == 0 && !allow_empty) {
1560 err = got_error(GOT_ERR_CANNOT_PACK);
1564 delta_cache = got_opentemp();
1565 if (delta_cache == NULL) {
1566 err = got_error_from_errno("got_opentemp");
1571 err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1572 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1575 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1576 err = got_error_from_errno("fseeko");
1581 err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1582 repo, progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1586 free_nmeta(meta, nmeta);
1587 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1588 err = got_error_from_errno("fclose");