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>
29 #include "got_error.h"
30 #include "got_cancel.h"
31 #include "got_object.h"
32 #include "got_reference.h"
33 #include "got_repository_admin.h"
35 #include "got_lib_deltify.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_object_idset.h"
39 #include "got_lib_object_cache.h"
40 #include "got_lib_deflate.h"
41 #include "got_lib_pack.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_repository.h"
46 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
49 struct got_pack_meta {
50 struct got_object_id id;
55 /* The best delta we picked */
56 struct got_pack_meta *head;
57 struct got_pack_meta *prev;
58 struct got_delta_instruction *deltas;
62 /* Only used for delta window */
63 struct got_delta_table *dtab;
65 /* Only used for writing offset deltas */
69 struct got_pack_metavec {
70 struct got_pack_meta **meta;
75 static const struct got_error *
76 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
77 const char *path, int obj_type, time_t mtime)
79 const struct got_error *err = NULL;
80 struct got_pack_meta *m;
84 m = calloc(1, sizeof(*m));
86 return got_error_from_errno("malloc");
88 memcpy(&m->id, id, sizeof(m->id));
90 m->path = strdup(path);
91 if (m->path == NULL) {
92 err = got_error_from_errno("strdup");
97 m->obj_type = obj_type;
104 clear_meta(struct got_pack_meta *meta)
115 free_nmeta(struct got_pack_meta **meta, int nmeta)
119 for (i = 0; i < nmeta; i++)
125 delta_order_cmp(const void *pa, const void *pb)
127 struct got_pack_meta *a, *b;
130 a = *(struct got_pack_meta **)pa;
131 b = *(struct got_pack_meta **)pb;
133 if (a->obj_type != b->obj_type)
134 return a->obj_type - b->obj_type;
135 cmp = strcmp(a->path, b->path);
138 if (a->mtime != b->mtime)
139 return a->mtime - b->mtime;
140 return got_object_id_cmp(&a->id, &b->id);
144 delta_size(struct got_delta_instruction *deltas, int ndeltas)
147 for (i = 0; i < ndeltas; i++) {
149 size += GOT_DELTA_SIZE_SHIFT;
151 size += deltas[i].len + 1;
157 static const struct got_error *
158 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
159 struct got_repository *repo,
160 got_pack_progress_cb progress_cb, void *progress_arg,
161 got_cancel_cb cancel_cb, void *cancel_arg)
163 const struct got_error *err = NULL;
164 struct got_pack_meta *m = NULL, *base = NULL;
165 struct got_raw_object *raw = NULL, *base_raw = NULL;
166 struct got_delta_instruction *deltas;
167 int i, j, size, ndeltas, best;
168 const int max_base_candidates = 10;
170 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
171 for (i = 0; i < nmeta; i++) {
173 err = (*cancel_cb)(cancel_arg);
178 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
186 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
187 m->obj_type == GOT_OBJ_TYPE_TAG)
190 err = got_object_raw_open(&raw, repo, &m->id, 8192);
194 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
195 raw->size + raw->hdrlen);
199 if (i > max_base_candidates) {
200 struct got_pack_meta *n = NULL;
201 n = meta[i - (max_base_candidates + 1)];
202 got_deltify_free(n->dtab);
207 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
209 err = (*cancel_cb)(cancel_arg);
214 /* long chains make unpacking slow, avoid such bases */
215 if (base->nchain >= 128 ||
216 base->obj_type != m->obj_type)
219 err = got_object_raw_open(&base_raw, repo, &base->id,
223 err = got_deltify(&deltas, &ndeltas,
224 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
225 base->dtab, base_raw->f, base_raw->hdrlen,
226 base_raw->size + base_raw->hdrlen);
227 got_object_raw_close(base_raw);
232 size = delta_size(deltas, ndeltas);
233 if (size + 32 < best){
235 * if we already picked a best delta,
241 m->ndeltas = ndeltas;
242 m->nchain = base->nchain + 1;
244 m->head = base->head;
254 got_object_raw_close(raw);
258 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
259 got_deltify_free(meta[i]->dtab);
260 meta[i]->dtab = NULL;
263 got_object_raw_close(raw);
265 got_object_raw_close(base_raw);
269 static const struct got_error *
270 search_packidx(int *found, struct got_object_id *id,
271 struct got_repository *repo)
273 const struct got_error *err = NULL;
274 struct got_packidx *packidx = NULL;
279 err = got_repo_search_packidx(&packidx, &idx, repo, id);
281 *found = 1; /* object is already packed */
282 else if (err->code == GOT_ERR_NO_OBJ)
287 static const int obj_types[] = {
293 GOT_OBJ_TYPE_OFFSET_DELTA,
294 GOT_OBJ_TYPE_REF_DELTA
297 static const struct got_error *
298 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
299 struct got_object_id *id, const char *path, int obj_type,
300 time_t mtime, int loose_obj_only, struct got_repository *repo)
302 const struct got_error *err;
303 struct got_pack_meta *m;
305 if (loose_obj_only) {
307 err = search_packidx(&is_packed, id, repo);
314 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
321 err = alloc_meta(&m, id, path, obj_type, mtime);
325 if (v->nmeta == v->metasz){
326 size_t newsize = 2 * v->metasz;
327 struct got_pack_meta **new;
328 new = reallocarray(v->meta, newsize, sizeof(*new));
330 err = got_error_from_errno("reallocarray");
341 v->meta[v->nmeta++] = m;
346 static const struct got_error *
347 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
348 struct got_object_idset *idset, struct got_object_id *tree_id,
349 const char *dpath, time_t mtime, struct got_repository *repo,
350 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
352 const struct got_error *err;
353 struct got_tree_object *tree;
357 err = got_object_open_as_tree(&tree, repo, tree_id);
361 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
362 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
363 struct got_object_id *id = got_tree_entry_get_id(e);
364 mode_t mode = got_tree_entry_get_mode(e);
367 err = (*cancel_cb)(cancel_arg);
372 if (got_object_tree_entry_is_submodule(e) ||
373 got_object_idset_contains(idset, id))
376 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
377 got_tree_entry_get_name(e)) == -1) {
378 err = got_error_from_errno("asprintf");
383 struct got_object_qid *qid;
384 err = got_object_qid_alloc(&qid, id);
387 STAILQ_INSERT_TAIL(ids, qid, entry);
388 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
389 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
390 mtime, loose_obj_only, repo);
398 got_object_tree_close(tree);
403 static const struct got_error *
404 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
405 struct got_object_id *tree_id, const char *dpath, time_t mtime,
406 int loose_obj_only, struct got_repository *repo,
407 got_cancel_cb cancel_cb, void *cancel_arg)
409 const struct got_error *err = NULL;
410 struct got_object_id_queue tree_ids;
411 struct got_object_qid *qid;
413 if (got_object_idset_contains(idset, tree_id))
416 err = got_object_qid_alloc(&qid, tree_id);
420 STAILQ_INIT(&tree_ids);
421 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
423 while (!STAILQ_EMPTY(&tree_ids)) {
425 err = (*cancel_cb)(cancel_arg);
430 qid = STAILQ_FIRST(&tree_ids);
431 STAILQ_REMOVE_HEAD(&tree_ids, entry);
433 if (got_object_idset_contains(idset, qid->id)) {
434 got_object_qid_free(qid);
438 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
439 mtime, loose_obj_only, repo);
441 got_object_qid_free(qid);
445 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
446 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
447 got_object_qid_free(qid);
452 got_object_id_queue_free(&tree_ids);
456 static const struct got_error *
457 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
458 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
459 got_cancel_cb cancel_cb, void *cancel_arg)
461 const struct got_error *err;
462 struct got_commit_object *commit;
464 if (got_object_idset_contains(idset, id))
467 if (loose_obj_only) {
469 err = search_packidx(&is_packed, id, repo);
476 err = got_object_open_as_commit(&commit, repo, id);
480 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
481 got_object_commit_get_committer_time(commit),
482 loose_obj_only, repo);
486 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
487 "", got_object_commit_get_committer_time(commit),
488 loose_obj_only, repo, cancel_cb, cancel_arg);
490 got_object_commit_close(commit);
494 static const struct got_error *
495 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
496 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
497 got_cancel_cb cancel_cb, void *cancel_arg)
499 const struct got_error *err;
500 struct got_tag_object *tag = NULL;
502 if (got_object_idset_contains(idset, id))
505 if (loose_obj_only) {
507 err = search_packidx(&is_packed, id, repo);
514 err = got_object_open_as_tag(&tag, repo, id);
518 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
519 got_object_tag_get_tagger_time(tag),
520 loose_obj_only, repo);
524 switch (got_object_tag_get_object_type(tag)) {
525 case GOT_OBJ_TYPE_COMMIT:
526 err = load_commit(v, idset,
527 got_object_tag_get_object_id(tag), repo,
528 loose_obj_only, cancel_cb, cancel_arg);
530 case GOT_OBJ_TYPE_TREE:
531 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
532 "", got_object_tag_get_tagger_time(tag),
533 loose_obj_only, repo, cancel_cb, cancel_arg);
540 got_object_tag_close(tag);
544 enum findtwixt_color {
549 static const int findtwixt_colors[] = {
555 static const struct got_error *
556 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
557 int color, struct got_repository *repo)
559 const struct got_error *err;
560 struct got_object_qid *qid;
562 err = got_object_qid_alloc(&qid, id);
566 STAILQ_INSERT_TAIL(ids, qid, entry);
567 qid->data = (void *)&findtwixt_colors[color];
571 static const struct got_error *
572 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
573 struct got_object_id *id, struct got_repository *repo,
574 got_cancel_cb cancel_cb, void *cancel_arg)
576 const struct got_error *err = NULL;
577 struct got_commit_object *commit;
578 const struct got_object_id_queue *parents;
579 struct got_object_id_queue ids;
580 struct got_object_qid *qid;
584 err = got_object_qid_alloc(&qid, id);
587 STAILQ_INSERT_HEAD(&ids, qid, entry);
589 while (!STAILQ_EMPTY(&ids)) {
591 err = (*cancel_cb)(cancel_arg);
596 qid = STAILQ_FIRST(&ids);
597 STAILQ_REMOVE_HEAD(&ids, entry);
599 if (got_object_idset_contains(drop, qid->id)) {
600 got_object_qid_free(qid);
604 err = got_object_idset_add(drop, qid->id,
605 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
607 got_object_qid_free(qid);
611 if (!got_object_idset_contains(keep, qid->id)) {
612 got_object_qid_free(qid);
616 err = got_object_open_as_commit(&commit, repo, qid->id);
617 got_object_qid_free(qid);
621 parents = got_object_commit_get_parent_ids(commit);
623 err = got_object_id_queue_copy(parents, &ids);
625 got_object_commit_close(commit);
629 got_object_commit_close(commit);
632 got_object_id_queue_free(&ids);
636 struct append_id_arg {
637 struct got_object_id **array;
641 static const struct got_error *
642 append_id(struct got_object_id *id, void *data, void *arg)
644 struct append_id_arg *a = arg;
646 a->array[a->idx] = got_object_id_dup(id);
647 if (a->array[a->idx] == NULL)
648 return got_error_from_errno("got_object_id_dup");
654 static const struct got_error *
655 findtwixt(struct got_object_id ***res, int *nres,
656 struct got_object_id **head, int nhead,
657 struct got_object_id **tail, int ntail,
658 struct got_repository *repo,
659 got_cancel_cb cancel_cb, void *cancel_arg)
661 const struct got_error *err = NULL;
662 struct got_object_id_queue ids;
663 struct got_object_idset *keep, *drop;
664 struct got_object_qid *qid;
665 int i, ncolor, nkeep, obj_type;
671 keep = got_object_idset_alloc();
673 return got_error_from_errno("got_object_idset_alloc");
675 drop = got_object_idset_alloc();
677 err = got_error_from_errno("got_object_idset_alloc");
681 for (i = 0; i < nhead; i++) {
682 struct got_object_id *id = head[i];
685 err = got_object_get_type(&obj_type, repo, id);
688 if (obj_type != GOT_OBJ_TYPE_COMMIT)
690 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
694 for (i = 0; i < ntail; i++) {
695 struct got_object_id *id = tail[i];
698 err = got_object_get_type(&obj_type, repo, id);
701 if (obj_type != GOT_OBJ_TYPE_COMMIT)
703 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
708 while (!STAILQ_EMPTY(&ids)) {
710 qid = STAILQ_FIRST(&ids);
711 qcolor = *((int *)qid->data);
713 if (got_object_idset_contains(drop, qid->id))
715 else if (got_object_idset_contains(keep, qid->id))
718 ncolor = COLOR_BLANK;
720 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
721 qcolor == COLOR_KEEP)) {
722 STAILQ_REMOVE_HEAD(&ids, entry);
723 got_object_qid_free(qid);
727 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
728 err = drop_commit(keep, drop, qid->id, repo,
729 cancel_cb, cancel_arg);
732 } else if (ncolor == COLOR_BLANK) {
733 struct got_commit_object *commit;
734 struct got_object_id *id;
735 const struct got_object_id_queue *parents;
736 struct got_object_qid *pid;
738 id = got_object_id_dup(qid->id);
740 err = got_error_from_errno("got_object_id_dup");
743 if (qcolor == COLOR_KEEP)
744 err = got_object_idset_add(keep, id,
745 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
747 err = got_object_idset_add(drop, id,
748 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
754 err = got_object_open_as_commit(&commit, repo, id);
759 parents = got_object_commit_get_parent_ids(commit);
761 STAILQ_FOREACH(pid, parents, entry) {
762 err = queue_commit_id(&ids, pid->id,
770 got_object_commit_close(commit);
773 /* should not happen */
774 err = got_error_fmt(GOT_ERR_NOT_IMPL,
775 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
779 STAILQ_REMOVE_HEAD(&ids, entry);
780 got_object_qid_free(qid);
783 nkeep = got_object_idset_num_elements(keep);
785 struct append_id_arg arg;
786 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
787 if (arg.array == NULL) {
788 err = got_error_from_errno("calloc");
792 err = got_object_idset_for_each(keep, append_id, &arg);
801 got_object_idset_free(keep);
802 got_object_idset_free(drop);
803 got_object_id_queue_free(&ids);
807 static const struct got_error *
808 read_meta(struct got_pack_meta ***meta, int *nmeta,
809 struct got_object_id **theirs, int ntheirs,
810 struct got_object_id **ours, int nours, struct got_repository *repo,
811 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
812 got_cancel_cb cancel_cb, void *cancel_arg)
814 const struct got_error *err = NULL;
815 struct got_object_id **ids = NULL;
816 struct got_object_idset *idset;
817 int i, nobj = 0, obj_type;
818 struct got_pack_metavec v;
823 idset = got_object_idset_alloc();
825 return got_error_from_errno("got_object_idset_alloc");
829 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
830 if (v.meta == NULL) {
831 err = got_error_from_errno("reallocarray");
835 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
836 cancel_cb, cancel_arg);
837 if (err || nobj == 0)
840 for (i = 0; i < ntheirs; i++) {
841 struct got_object_id *id = theirs[i];
844 err = got_object_get_type(&obj_type, repo, id);
847 if (obj_type != GOT_OBJ_TYPE_COMMIT)
849 err = load_commit(NULL, idset, id, repo,
850 loose_obj_only, cancel_cb, cancel_arg);
854 err = progress_cb(progress_arg, 0L, nours,
861 for (i = 0; i < ntheirs; i++) {
862 struct got_object_id *id = theirs[i];
866 cached_type = got_object_idset_get(idset, id);
867 if (cached_type == NULL) {
868 err = got_object_get_type(&obj_type, repo, id);
872 obj_type = *cached_type;
873 if (obj_type != GOT_OBJ_TYPE_TAG)
875 err = load_tag(NULL, idset, id, repo,
876 loose_obj_only, cancel_cb, cancel_arg);
880 err = progress_cb(progress_arg, 0L, nours,
887 for (i = 0; i < nobj; i++) {
888 err = load_commit(&v, idset, ids[i], repo,
889 loose_obj_only, cancel_cb, cancel_arg);
893 err = progress_cb(progress_arg, 0L, nours,
900 for (i = 0; i < nours; i++) {
901 struct got_object_id *id = ours[i];
905 cached_type = got_object_idset_get(idset, id);
906 if (cached_type == NULL) {
907 err = got_object_get_type(&obj_type, repo, id);
911 obj_type = *cached_type;
912 if (obj_type != GOT_OBJ_TYPE_TAG)
914 err = load_tag(&v, idset, id, repo,
915 loose_obj_only, cancel_cb, cancel_arg);
919 err = progress_cb(progress_arg, 0L, nours,
927 for (i = 0; i < nobj; i++) {
931 got_object_idset_free(idset);
941 const struct got_error *
942 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
946 SHA1Update(ctx, buf, len);
947 n = fwrite(buf, 1, len, f);
949 return got_ferror(f, GOT_ERR_IO);
954 putbe32(char *b, uint32_t n)
963 write_order_cmp(const void *pa, const void *pb)
965 struct got_pack_meta *a, *b, *ahd, *bhd;
967 a = *(struct got_pack_meta **)pa;
968 b = *(struct got_pack_meta **)pb;
969 ahd = (a->head == NULL) ? a : a->head;
970 bhd = (b->head == NULL) ? b : b->head;
971 if (ahd->mtime != bhd->mtime)
972 return bhd->mtime - ahd->mtime;
974 return (uintptr_t)bhd - (uintptr_t)ahd;
975 if (a->nchain != b->nchain)
976 return a->nchain - b->nchain;
977 return a->mtime - b->mtime;
980 static const struct got_error *
981 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
987 hdr[0] = obj_type << 4;
990 for (i = 1; len != 0; i++){
992 return got_error(GOT_ERR_NO_SPACE);
993 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
994 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
995 len >>= GOT_DELTA_SIZE_SHIFT;
1002 static const struct got_error *
1003 append(char **p, int *len, int *sz, void *seg, int nseg)
1007 if (*len + nseg >= *sz) {
1008 while (*len + nseg >= *sz)
1010 n = realloc(*p, *sz);
1012 return got_error_from_errno("realloc");
1015 memcpy(*p + *len, seg, nseg);
1021 static const struct got_error *
1022 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1023 off_t base_size, char **pp)
1025 const struct got_error *err = NULL;
1027 unsigned char buf[16], *bp;
1030 struct got_delta_instruction *d;
1039 return got_error_from_errno("malloc");
1041 /* base object size */
1042 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1043 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1044 for (i = 1; n > 0; i++) {
1045 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1046 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1047 n >>= GOT_DELTA_SIZE_SHIFT;
1049 err = append(&p, &len, &sz, buf, i);
1053 /* target object size */
1054 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1055 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1056 for (i = 1; n > 0; i++) {
1057 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1058 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1059 n >>= GOT_DELTA_SIZE_SHIFT;
1061 err = append(&p, &len, &sz, buf, i);
1064 for (j = 0; j < m->ndeltas; j++) {
1069 buf[0] = GOT_DELTA_BASE_COPY;
1070 for (i = 0; i < 4; i++) {
1071 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1080 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1081 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1082 for (i = 0; i < 3 && n > 0; i++) {
1083 buf[0] |= 1 << (i + 4);
1088 err = append(&p, &len, &sz, buf, bp - buf);
1094 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1095 return got_error_from_errno("fseeko");
1097 while (n != d->len) {
1098 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1099 err = append(&p, &len, &sz, buf, 1);
1102 r = fread(content, 1, buf[0], o->f);
1104 return got_ferror(o->f, GOT_ERR_IO);
1105 err = append(&p, &len, &sz, content, buf[0]);
1118 packoff(char *hdr, off_t off)
1123 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1124 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1125 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1126 GOT_DELTA_SIZE_MORE;
1131 hdr[j++] = rbuf[--i];
1135 static const struct got_error *
1136 genpack(uint8_t *pack_sha1, FILE *packfile,
1137 struct got_pack_meta **meta, int nmeta, int nours,
1138 int use_offset_deltas, struct got_repository *repo,
1139 got_pack_progress_cb progress_cb, void *progress_arg,
1140 got_cancel_cb cancel_cb, void *cancel_arg)
1142 const struct got_error *err = NULL;
1145 struct got_pack_meta *m;
1146 struct got_raw_object *raw;
1147 char *p = NULL, buf[32];
1149 struct got_deflate_checksum csum;
1150 off_t packfile_size = 0;
1153 csum.output_sha1 = &ctx;
1154 csum.output_crc = NULL;
1156 err = hwrite(packfile, "PACK", 4, &ctx);
1159 putbe32(buf, GOT_PACKFILE_VERSION);
1160 err = hwrite(packfile, buf, 4, &ctx);
1163 putbe32(buf, nmeta);
1164 err = hwrite(packfile, buf, 4, &ctx);
1167 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1168 for (i = 0; i < nmeta; i++) {
1170 err = progress_cb(progress_arg, packfile_size, nours,
1176 m->off = ftello(packfile);
1177 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1180 if (m->deltas == NULL) {
1181 err = packhdr(&nh, buf, sizeof(buf),
1182 m->obj_type, raw->size);
1185 err = hwrite(packfile, buf, nh, &ctx);
1188 packfile_size += nh;
1189 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1190 err = got_error_from_errno("fseeko");
1193 err = got_deflate_to_file(&outlen, raw->f, packfile,
1197 packfile_size += outlen;
1200 struct got_raw_object *base_raw;
1201 err = got_object_raw_open(&base_raw, repo,
1202 &m->prev->id, 8192);
1205 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1208 got_object_raw_close(base_raw);
1209 if (use_offset_deltas && m->prev->off != 0) {
1210 err = packhdr(&nh, buf, sizeof(buf),
1211 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1214 nh += packoff(buf + nh,
1215 m->off - m->prev->off);
1216 err = hwrite(packfile, buf, nh, &ctx);
1219 packfile_size += nh;
1221 err = packhdr(&nh, buf, sizeof(buf),
1222 GOT_OBJ_TYPE_REF_DELTA, nd);
1223 err = hwrite(packfile, buf, nh, &ctx);
1226 packfile_size += nh;
1227 err = hwrite(packfile, m->prev->id.sha1,
1228 sizeof(m->prev->id.sha1), &ctx);
1229 packfile_size += sizeof(m->prev->id.sha1);
1233 /* XXX need got_deflate_from_mem() */
1234 delta_file = fmemopen(p, nd, "r");
1235 if (delta_file == NULL) {
1236 err = got_error_from_errno("fmemopen");
1239 err = got_deflate_to_file(&outlen, delta_file,
1244 packfile_size += outlen;
1248 got_object_raw_close(raw);
1251 SHA1Final(pack_sha1, &ctx);
1252 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1253 if (n != SHA1_DIGEST_LENGTH)
1254 err = got_ferror(packfile, GOT_ERR_IO);
1255 packfile_size += SHA1_DIGEST_LENGTH;
1256 packfile_size += sizeof(struct got_packfile_hdr);
1257 err = progress_cb(progress_arg, packfile_size, nours,
1258 nmeta, nmeta, nmeta);
1266 const struct got_error *
1267 got_pack_create(uint8_t *packsha1, FILE *packfile,
1268 struct got_object_id **theirs, int ntheirs,
1269 struct got_object_id **ours, int nours,
1270 struct got_repository *repo, int loose_obj_only, int allow_empty,
1271 got_pack_progress_cb progress_cb, void *progress_arg,
1272 got_cancel_cb cancel_cb, void *cancel_arg)
1274 const struct got_error *err;
1275 struct got_pack_meta **meta;
1278 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1279 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1283 if (nmeta == 0 && !allow_empty) {
1284 err = got_error(GOT_ERR_CANNOT_PACK);
1288 err = pick_deltas(meta, nmeta, nours, repo,
1289 progress_cb, progress_arg, cancel_cb, cancel_arg);
1294 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1295 progress_cb, progress_arg, cancel_cb, cancel_arg);
1299 free_nmeta(meta, nmeta);