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>
32 #include "got_error.h"
33 #include "got_cancel.h"
34 #include "got_object.h"
35 #include "got_reference.h"
36 #include "got_repository_admin.h"
38 #include "got_lib_deltify.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_idset.h"
42 #include "got_lib_object_cache.h"
43 #include "got_lib_deflate.h"
44 #include "got_lib_pack.h"
45 #include "got_lib_privsep.h"
46 #include "got_lib_repository.h"
49 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
52 struct got_pack_meta {
53 struct got_object_id id;
58 /* The best delta we picked */
59 struct got_pack_meta *head;
60 struct got_pack_meta *prev;
61 struct got_delta_instruction *deltas;
65 /* Only used for delta window */
66 struct got_delta_table *dtab;
68 /* Only used for writing offset deltas */
72 struct got_pack_metavec {
73 struct got_pack_meta **meta;
78 static const struct got_error *
79 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
80 const char *path, int obj_type, time_t mtime)
82 const struct got_error *err = NULL;
83 struct got_pack_meta *m;
87 m = calloc(1, sizeof(*m));
89 return got_error_from_errno("malloc");
91 memcpy(&m->id, id, sizeof(m->id));
93 m->path = strdup(path);
94 if (m->path == NULL) {
95 err = got_error_from_errno("strdup");
100 m->obj_type = obj_type;
107 clear_meta(struct got_pack_meta *meta)
118 free_nmeta(struct got_pack_meta **meta, int nmeta)
122 for (i = 0; i < nmeta; i++)
128 delta_order_cmp(const void *pa, const void *pb)
130 struct got_pack_meta *a, *b;
133 a = *(struct got_pack_meta **)pa;
134 b = *(struct got_pack_meta **)pb;
136 if (a->obj_type != b->obj_type)
137 return a->obj_type - b->obj_type;
138 cmp = strcmp(a->path, b->path);
141 if (a->mtime != b->mtime)
142 return a->mtime - b->mtime;
143 return got_object_id_cmp(&a->id, &b->id);
147 delta_size(struct got_delta_instruction *deltas, int ndeltas)
150 for (i = 0; i < ndeltas; i++) {
152 size += GOT_DELTA_SIZE_SHIFT;
154 size += deltas[i].len + 1;
160 static const struct got_error *
161 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
162 struct got_repository *repo,
163 got_pack_progress_cb progress_cb, void *progress_arg,
164 got_cancel_cb cancel_cb, void *cancel_arg)
166 const struct got_error *err = NULL;
167 struct got_pack_meta *m = NULL, *base = NULL;
168 struct got_raw_object *raw = NULL, *base_raw = NULL;
169 struct got_delta_instruction *deltas;
170 int i, j, size, ndeltas, best;
171 const int max_base_candidates = 10;
173 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
174 for (i = 0; i < nmeta; i++) {
176 err = (*cancel_cb)(cancel_arg);
181 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
189 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
190 m->obj_type == GOT_OBJ_TYPE_TAG)
193 err = got_object_raw_open(&raw, repo, &m->id, 8192);
197 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
198 raw->size + raw->hdrlen);
202 if (i > max_base_candidates) {
203 struct got_pack_meta *n = NULL;
204 n = meta[i - (max_base_candidates + 1)];
205 got_deltify_free(n->dtab);
210 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
212 err = (*cancel_cb)(cancel_arg);
217 /* long chains make unpacking slow, avoid such bases */
218 if (base->nchain >= 128 ||
219 base->obj_type != m->obj_type)
222 err = got_object_raw_open(&base_raw, repo, &base->id,
226 err = got_deltify(&deltas, &ndeltas,
227 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
228 base->dtab, base_raw->f, base_raw->hdrlen,
229 base_raw->size + base_raw->hdrlen);
230 got_object_raw_close(base_raw);
235 size = delta_size(deltas, ndeltas);
236 if (size + 32 < best){
238 * if we already picked a best delta,
244 m->ndeltas = ndeltas;
245 m->nchain = base->nchain + 1;
247 m->head = base->head;
257 got_object_raw_close(raw);
261 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
262 got_deltify_free(meta[i]->dtab);
263 meta[i]->dtab = NULL;
266 got_object_raw_close(raw);
268 got_object_raw_close(base_raw);
272 static const struct got_error *
273 search_packidx(int *found, struct got_object_id *id,
274 struct got_repository *repo)
276 const struct got_error *err = NULL;
277 struct got_packidx *packidx = NULL;
282 err = got_repo_search_packidx(&packidx, &idx, repo, id);
284 *found = 1; /* object is already packed */
285 else if (err->code == GOT_ERR_NO_OBJ)
290 static const struct got_error *
291 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
292 struct got_object_id *id, const char *path, int obj_type,
293 time_t mtime, int loose_obj_only, struct got_repository *repo)
295 const struct got_error *err;
296 struct got_pack_meta *m;
298 if (loose_obj_only) {
300 err = search_packidx(&is_packed, id, repo);
307 err = got_object_idset_add(idset, id, NULL);
314 err = alloc_meta(&m, id, path, obj_type, mtime);
318 if (v->nmeta == v->metasz){
319 size_t newsize = 2 * v->metasz;
320 struct got_pack_meta **new;
321 new = reallocarray(v->meta, newsize, sizeof(*new));
323 err = got_error_from_errno("reallocarray");
334 v->meta[v->nmeta++] = m;
339 static const struct got_error *
340 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
341 struct got_object_idset *idset, struct got_object_id *tree_id,
342 const char *dpath, time_t mtime, struct got_repository *repo,
343 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
345 const struct got_error *err;
346 struct got_tree_object *tree;
350 err = got_object_open_as_tree(&tree, repo, tree_id);
354 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
355 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
356 struct got_object_id *id = got_tree_entry_get_id(e);
357 mode_t mode = got_tree_entry_get_mode(e);
360 err = (*cancel_cb)(cancel_arg);
365 if (got_object_tree_entry_is_symlink(e) ||
366 got_object_tree_entry_is_submodule(e) ||
367 got_object_idset_contains(idset, id))
370 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
371 got_tree_entry_get_name(e)) == -1) {
372 err = got_error_from_errno("asprintf");
377 struct got_object_qid *qid;
378 err = got_object_qid_alloc(&qid, id);
381 STAILQ_INSERT_TAIL(ids, qid, entry);
382 } else if (S_ISREG(mode)) {
383 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
384 mtime, loose_obj_only, repo);
392 got_object_tree_close(tree);
397 static const struct got_error *
398 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
399 struct got_object_id *tree_id, const char *dpath, time_t mtime,
400 int loose_obj_only, struct got_repository *repo,
401 got_cancel_cb cancel_cb, void *cancel_arg)
403 const struct got_error *err = NULL;
404 struct got_object_id_queue tree_ids;
405 struct got_object_qid *qid;
407 if (got_object_idset_contains(idset, tree_id))
410 err = got_object_qid_alloc(&qid, tree_id);
414 STAILQ_INIT(&tree_ids);
415 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
417 while (!STAILQ_EMPTY(&tree_ids)) {
419 err = (*cancel_cb)(cancel_arg);
424 qid = STAILQ_FIRST(&tree_ids);
425 STAILQ_REMOVE_HEAD(&tree_ids, entry);
427 if (got_object_idset_contains(idset, qid->id)) {
428 got_object_qid_free(qid);
432 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
433 mtime, loose_obj_only, repo);
435 got_object_qid_free(qid);
439 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
440 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
441 got_object_qid_free(qid);
446 got_object_id_queue_free(&tree_ids);
450 static const struct got_error *
451 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
452 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
453 got_cancel_cb cancel_cb, void *cancel_arg)
455 const struct got_error *err;
456 struct got_commit_object *commit;
458 if (got_object_idset_contains(idset, id))
461 if (loose_obj_only) {
463 err = search_packidx(&is_packed, id, repo);
470 err = got_object_open_as_commit(&commit, repo, id);
474 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
475 got_object_commit_get_committer_time(commit),
476 loose_obj_only, repo);
480 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
481 "", got_object_commit_get_committer_time(commit),
482 loose_obj_only, repo, cancel_cb, cancel_arg);
484 got_object_commit_close(commit);
488 static const struct got_error *
489 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
490 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
491 got_cancel_cb cancel_cb, void *cancel_arg)
493 const struct got_error *err;
494 struct got_tag_object *tag = NULL;
496 if (got_object_idset_contains(idset, id))
499 if (loose_obj_only) {
501 err = search_packidx(&is_packed, id, repo);
508 err = got_object_open_as_tag(&tag, repo, id);
512 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
513 got_object_tag_get_tagger_time(tag),
514 loose_obj_only, repo);
518 switch (got_object_tag_get_object_type(tag)) {
519 case GOT_OBJ_TYPE_COMMIT:
520 err = load_commit(NULL, idset,
521 got_object_tag_get_object_id(tag), repo,
522 loose_obj_only, cancel_cb, cancel_arg);
524 case GOT_OBJ_TYPE_TREE:
525 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
526 "", got_object_tag_get_tagger_time(tag),
527 loose_obj_only, repo, cancel_cb, cancel_arg);
534 got_object_tag_close(tag);
538 enum findtwixt_color {
543 static const int findtwixt_colors[] = {
549 static const struct got_error *
550 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
551 int color, struct got_repository *repo)
553 const struct got_error *err;
554 struct got_object_qid *qid;
556 err = got_object_qid_alloc(&qid, id);
560 STAILQ_INSERT_TAIL(ids, qid, entry);
561 qid->data = (void *)&findtwixt_colors[color];
565 static const struct got_error *
566 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
567 struct got_object_id *id, struct got_repository *repo,
568 got_cancel_cb cancel_cb, void *cancel_arg)
570 const struct got_error *err = NULL;
571 struct got_commit_object *commit;
572 const struct got_object_id_queue *parents;
573 struct got_object_id_queue ids;
574 struct got_object_qid *qid;
578 err = got_object_qid_alloc(&qid, id);
581 STAILQ_INSERT_HEAD(&ids, qid, entry);
583 while (!STAILQ_EMPTY(&ids)) {
585 err = (*cancel_cb)(cancel_arg);
590 qid = STAILQ_FIRST(&ids);
591 STAILQ_REMOVE_HEAD(&ids, entry);
593 if (got_object_idset_contains(drop, qid->id)) {
594 got_object_qid_free(qid);
598 err = got_object_idset_add(drop, qid->id, NULL);
600 got_object_qid_free(qid);
604 if (!got_object_idset_contains(keep, qid->id)) {
605 got_object_qid_free(qid);
609 err = got_object_open_as_commit(&commit, repo, qid->id);
610 got_object_qid_free(qid);
614 parents = got_object_commit_get_parent_ids(commit);
616 err = got_object_id_queue_copy(parents, &ids);
618 got_object_commit_close(commit);
622 got_object_commit_close(commit);
625 got_object_id_queue_free(&ids);
629 struct append_id_arg {
630 struct got_object_id **array;
634 static const struct got_error *
635 append_id(struct got_object_id *id, void *data, void *arg)
637 struct append_id_arg *a = arg;
639 a->array[a->idx] = got_object_id_dup(id);
640 if (a->array[a->idx] == NULL)
641 return got_error_from_errno("got_object_id_dup");
647 static const struct got_error *
648 findtwixt(struct got_object_id ***res, int *nres,
649 struct got_object_id **head, int nhead,
650 struct got_object_id **tail, int ntail,
651 struct got_repository *repo,
652 got_cancel_cb cancel_cb, void *cancel_arg)
654 const struct got_error *err = NULL;
655 struct got_object_id_queue ids;
656 struct got_object_idset *keep, *drop;
657 struct got_object_qid *qid;
658 int i, ncolor, nkeep, obj_type;
664 keep = got_object_idset_alloc();
666 return got_error_from_errno("got_object_idset_alloc");
668 drop = got_object_idset_alloc();
670 err = got_error_from_errno("got_object_idset_alloc");
674 for (i = 0; i < nhead; i++) {
675 struct got_object_id *id = head[i];
678 err = got_object_get_type(&obj_type, repo, id);
681 if (obj_type != GOT_OBJ_TYPE_COMMIT)
683 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
687 for (i = 0; i < ntail; i++) {
688 struct got_object_id *id = tail[i];
691 err = got_object_get_type(&obj_type, repo, id);
694 if (obj_type != GOT_OBJ_TYPE_COMMIT)
696 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
701 while (!STAILQ_EMPTY(&ids)) {
703 qid = STAILQ_FIRST(&ids);
704 qcolor = *((int *)qid->data);
706 if (got_object_idset_contains(drop, qid->id))
708 else if (got_object_idset_contains(keep, qid->id))
711 ncolor = COLOR_BLANK;
713 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
714 qcolor == COLOR_KEEP)) {
715 STAILQ_REMOVE_HEAD(&ids, entry);
716 got_object_qid_free(qid);
720 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
721 err = drop_commit(keep, drop, qid->id, repo,
722 cancel_cb, cancel_arg);
725 } else if (ncolor == COLOR_BLANK) {
726 struct got_commit_object *commit;
727 struct got_object_id *id;
728 const struct got_object_id_queue *parents;
729 struct got_object_qid *pid;
731 id = got_object_id_dup(qid->id);
733 err = got_error_from_errno("got_object_id_dup");
736 if (qcolor == COLOR_KEEP)
737 err = got_object_idset_add(keep, id, NULL);
739 err = got_object_idset_add(drop, id, NULL);
745 err = got_object_open_as_commit(&commit, repo, id);
750 parents = got_object_commit_get_parent_ids(commit);
752 STAILQ_FOREACH(pid, parents, entry) {
753 err = queue_commit_id(&ids, pid->id,
761 got_object_commit_close(commit);
764 /* should not happen */
765 err = got_error_fmt(GOT_ERR_NOT_IMPL,
766 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
770 STAILQ_REMOVE_HEAD(&ids, entry);
771 got_object_qid_free(qid);
774 nkeep = got_object_idset_num_elements(keep);
776 struct append_id_arg arg;
777 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
778 if (arg.array == NULL) {
779 err = got_error_from_errno("calloc");
783 err = got_object_idset_for_each(keep, append_id, &arg);
792 got_object_idset_free(keep);
793 got_object_idset_free(drop);
794 got_object_id_queue_free(&ids);
798 static const struct got_error *
799 read_meta(struct got_pack_meta ***meta, int *nmeta,
800 struct got_object_id **theirs, int ntheirs,
801 struct got_object_id **ours, int nours, struct got_repository *repo,
802 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
803 got_cancel_cb cancel_cb, void *cancel_arg)
805 const struct got_error *err = NULL;
806 struct got_object_id **ids = NULL;
807 struct got_object_idset *idset;
808 int i, nobj = 0, obj_type;
809 struct got_pack_metavec v;
814 idset = got_object_idset_alloc();
816 return got_error_from_errno("got_object_idset_alloc");
820 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
821 if (v.meta == NULL) {
822 err = got_error_from_errno("reallocarray");
826 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
827 cancel_cb, cancel_arg);
828 if (err || nobj == 0)
831 for (i = 0; i < ntheirs; i++) {
832 struct got_object_id *id = theirs[i];
835 err = got_object_get_type(&obj_type, repo, id);
838 if (obj_type != GOT_OBJ_TYPE_COMMIT)
840 err = load_commit(NULL, idset, id, repo,
841 loose_obj_only, cancel_cb, cancel_arg);
845 err = progress_cb(progress_arg, 0L, nours,
852 for (i = 0; i < nobj; i++) {
853 err = load_commit(&v, idset, ids[i], repo,
854 loose_obj_only, cancel_cb, cancel_arg);
858 err = progress_cb(progress_arg, 0L, nours,
865 for (i = 0; i < ntheirs; i++) {
866 struct got_object_id *id = theirs[i];
869 err = got_object_get_type(&obj_type, repo, id);
872 if (obj_type != GOT_OBJ_TYPE_TAG)
874 err = load_tag(NULL, idset, id, repo,
875 loose_obj_only, cancel_cb, cancel_arg);
879 err = progress_cb(progress_arg, 0L, nours,
886 for (i = 0; i < nours; i++) {
887 struct got_object_id *id = ours[i];
890 err = got_object_get_type(&obj_type, repo, id);
893 if (obj_type != GOT_OBJ_TYPE_TAG)
895 err = load_tag(&v, idset, id, repo,
896 loose_obj_only, cancel_cb, cancel_arg);
900 err = progress_cb(progress_arg, 0L, nours,
908 for (i = 0; i < nobj; i++) {
912 got_object_idset_free(idset);
922 const struct got_error *
923 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
927 SHA1Update(ctx, buf, len);
928 n = fwrite(buf, 1, len, f);
930 return got_ferror(f, GOT_ERR_IO);
935 putbe32(char *b, uint32_t n)
944 write_order_cmp(const void *pa, const void *pb)
946 struct got_pack_meta *a, *b, *ahd, *bhd;
948 a = *(struct got_pack_meta **)pa;
949 b = *(struct got_pack_meta **)pb;
950 ahd = (a->head == NULL) ? a : a->head;
951 bhd = (b->head == NULL) ? b : b->head;
952 if (ahd->mtime != bhd->mtime)
953 return bhd->mtime - ahd->mtime;
955 return (uintptr_t)bhd - (uintptr_t)ahd;
956 if (a->nchain != b->nchain)
957 return a->nchain - b->nchain;
958 return a->mtime - b->mtime;
961 static const struct got_error *
962 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
968 hdr[0] = obj_type << 4;
971 for (i = 1; len != 0; i++){
973 return got_error(GOT_ERR_NO_SPACE);
974 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
975 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
976 len >>= GOT_DELTA_SIZE_SHIFT;
983 static const struct got_error *
984 append(char **p, int *len, int *sz, void *seg, int nseg)
988 if (*len + nseg >= *sz) {
989 while (*len + nseg >= *sz)
991 n = realloc(*p, *sz);
993 return got_error_from_errno("realloc");
996 memcpy(*p + *len, seg, nseg);
1002 static const struct got_error *
1003 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1004 off_t base_size, char **pp)
1006 const struct got_error *err = NULL;
1008 unsigned char buf[16], *bp;
1011 struct got_delta_instruction *d;
1020 return got_error_from_errno("malloc");
1022 /* base object size */
1023 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1024 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1025 for (i = 1; n > 0; i++) {
1026 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1027 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1028 n >>= GOT_DELTA_SIZE_SHIFT;
1030 err = append(&p, &len, &sz, buf, i);
1034 /* target object size */
1035 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1036 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1037 for (i = 1; n > 0; i++) {
1038 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1039 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1040 n >>= GOT_DELTA_SIZE_SHIFT;
1042 err = append(&p, &len, &sz, buf, i);
1045 for (j = 0; j < m->ndeltas; j++) {
1050 buf[0] = GOT_DELTA_BASE_COPY;
1051 for (i = 0; i < 4; i++) {
1052 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1061 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1062 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1063 for (i = 0; i < 3 && n > 0; i++) {
1064 buf[0] |= 1 << (i + 4);
1069 err = append(&p, &len, &sz, buf, bp - buf);
1075 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1076 return got_error_from_errno("fseeko");
1078 while (n != d->len) {
1079 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1080 err = append(&p, &len, &sz, buf, 1);
1083 r = fread(content, 1, buf[0], o->f);
1085 return got_ferror(o->f, GOT_ERR_IO);
1086 err = append(&p, &len, &sz, content, buf[0]);
1099 packoff(char *hdr, off_t off)
1104 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1105 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1106 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1107 GOT_DELTA_SIZE_MORE;
1112 hdr[j++] = rbuf[--i];
1116 static const struct got_error *
1117 genpack(uint8_t *pack_sha1, FILE *packfile,
1118 struct got_pack_meta **meta, int nmeta, int nours,
1119 int use_offset_deltas, struct got_repository *repo,
1120 got_pack_progress_cb progress_cb, void *progress_arg,
1121 got_cancel_cb cancel_cb, void *cancel_arg)
1123 const struct got_error *err = NULL;
1126 struct got_pack_meta *m;
1127 struct got_raw_object *raw;
1128 char *p = NULL, buf[32];
1130 struct got_deflate_checksum csum;
1131 off_t packfile_size = 0;
1134 csum.output_sha1 = &ctx;
1135 csum.output_crc = NULL;
1137 err = hwrite(packfile, "PACK", 4, &ctx);
1140 putbe32(buf, GOT_PACKFILE_VERSION);
1141 err = hwrite(packfile, buf, 4, &ctx);
1144 putbe32(buf, nmeta);
1145 err = hwrite(packfile, buf, 4, &ctx);
1148 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1149 for (i = 0; i < nmeta; i++) {
1151 err = progress_cb(progress_arg, packfile_size, nours,
1157 m->off = ftello(packfile);
1158 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1161 if (m->deltas == NULL) {
1162 err = packhdr(&nh, buf, sizeof(buf),
1163 m->obj_type, raw->size);
1166 err = hwrite(packfile, buf, nh, &ctx);
1169 packfile_size += nh;
1170 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1171 err = got_error_from_errno("fseeko");
1174 err = got_deflate_to_file(&outlen, raw->f, packfile,
1178 packfile_size += outlen;
1181 struct got_raw_object *base_raw;
1182 err = got_object_raw_open(&base_raw, repo,
1183 &m->prev->id, 8192);
1186 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1189 got_object_raw_close(base_raw);
1190 if (use_offset_deltas && m->prev->off != 0) {
1191 err = packhdr(&nh, buf, sizeof(buf),
1192 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1195 nh += packoff(buf + nh,
1196 m->off - m->prev->off);
1197 err = hwrite(packfile, buf, nh, &ctx);
1200 packfile_size += nh;
1202 err = packhdr(&nh, buf, sizeof(buf),
1203 GOT_OBJ_TYPE_REF_DELTA, nd);
1204 err = hwrite(packfile, buf, nh, &ctx);
1207 packfile_size += nh;
1208 err = hwrite(packfile, m->prev->id.sha1,
1209 sizeof(m->prev->id.sha1), &ctx);
1210 packfile_size += sizeof(m->prev->id.sha1);
1214 /* XXX need got_deflate_from_mem() */
1215 delta_file = fmemopen(p, nd, "r");
1216 if (delta_file == NULL) {
1217 err = got_error_from_errno("fmemopen");
1220 err = got_deflate_to_file(&outlen, delta_file,
1225 packfile_size += outlen;
1229 got_object_raw_close(raw);
1232 SHA1Final(pack_sha1, &ctx);
1233 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1234 if (n != SHA1_DIGEST_LENGTH)
1235 err = got_ferror(packfile, GOT_ERR_IO);
1236 packfile_size += SHA1_DIGEST_LENGTH;
1237 packfile_size += 16; /* pack file header */
1238 err = progress_cb(progress_arg, packfile_size, nours,
1239 nmeta, nmeta, nmeta);
1247 const struct got_error *
1248 got_pack_create(uint8_t *packsha1, FILE *packfile,
1249 struct got_object_id **theirs, int ntheirs,
1250 struct got_object_id **ours, int nours,
1251 struct got_repository *repo, int loose_obj_only,
1252 got_pack_progress_cb progress_cb, void *progress_arg,
1253 got_cancel_cb cancel_cb, void *cancel_arg)
1255 const struct got_error *err;
1256 struct got_pack_meta **meta;
1259 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1260 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1265 err = got_error(GOT_ERR_CANNOT_PACK);
1269 err = pick_deltas(meta, nmeta, nours, repo,
1270 progress_cb, progress_arg, cancel_cb, cancel_arg);
1274 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1275 progress_cb, progress_arg, cancel_cb, cancel_arg);
1279 free_nmeta(meta, nmeta);