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>
30 #include "got_error.h"
31 #include "got_cancel.h"
32 #include "got_object.h"
33 #include "got_reference.h"
34 #include "got_repository_admin.h"
36 #include "got_lib_deltify.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_idset.h"
40 #include "got_lib_object_cache.h"
41 #include "got_lib_deflate.h"
42 #include "got_lib_pack.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_repository.h"
47 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
50 struct got_pack_meta {
51 struct got_object_id id;
56 /* The best delta we picked */
57 struct got_pack_meta *head;
58 struct got_pack_meta *prev;
59 struct got_delta_instruction *deltas;
63 /* Only used for delta window */
64 struct got_delta_table *dtab;
66 /* Only used for writing offset deltas */
70 struct got_pack_metavec {
71 struct got_pack_meta **meta;
76 static const struct got_error *
77 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
78 const char *path, int obj_type, time_t mtime)
80 const struct got_error *err = NULL;
81 struct got_pack_meta *m;
85 m = calloc(1, sizeof(*m));
87 return got_error_from_errno("malloc");
89 memcpy(&m->id, id, sizeof(m->id));
91 m->path = strdup(path);
92 if (m->path == NULL) {
93 err = got_error_from_errno("strdup");
98 m->obj_type = obj_type;
105 clear_meta(struct got_pack_meta *meta)
116 free_nmeta(struct got_pack_meta **meta, int nmeta)
120 for (i = 0; i < nmeta; i++)
126 delta_order_cmp(const void *pa, const void *pb)
128 struct got_pack_meta *a, *b;
131 a = *(struct got_pack_meta **)pa;
132 b = *(struct got_pack_meta **)pb;
134 if (a->obj_type != b->obj_type)
135 return a->obj_type - b->obj_type;
136 cmp = strcmp(a->path, b->path);
139 if (a->mtime != b->mtime)
140 return a->mtime - b->mtime;
141 return got_object_id_cmp(&a->id, &b->id);
145 delta_size(struct got_delta_instruction *deltas, int ndeltas)
148 for (i = 0; i < ndeltas; i++) {
150 size += GOT_DELTA_SIZE_SHIFT;
152 size += deltas[i].len + 1;
158 static const struct got_error *
159 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
160 struct got_repository *repo,
161 got_pack_progress_cb progress_cb, void *progress_arg,
162 got_cancel_cb cancel_cb, void *cancel_arg)
164 const struct got_error *err = NULL;
165 struct got_pack_meta *m = NULL, *base = NULL;
166 struct got_raw_object *raw = NULL, *base_raw = NULL;
167 struct got_delta_instruction *deltas;
168 int i, j, size, ndeltas, best;
169 const int max_base_candidates = 10;
171 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
172 for (i = 0; i < nmeta; i++) {
174 err = (*cancel_cb)(cancel_arg);
179 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
187 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
188 m->obj_type == GOT_OBJ_TYPE_TAG)
191 err = got_object_raw_open(&raw, repo, &m->id, 8192);
195 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
196 raw->size + raw->hdrlen);
200 if (i > max_base_candidates) {
201 struct got_pack_meta *n = NULL;
202 n = meta[i - (max_base_candidates + 1)];
203 got_deltify_free(n->dtab);
208 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
210 err = (*cancel_cb)(cancel_arg);
215 /* long chains make unpacking slow, avoid such bases */
216 if (base->nchain >= 128 ||
217 base->obj_type != m->obj_type)
220 err = got_object_raw_open(&base_raw, repo, &base->id,
224 err = got_deltify(&deltas, &ndeltas,
225 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
226 base->dtab, base_raw->f, base_raw->hdrlen,
227 base_raw->size + base_raw->hdrlen);
228 got_object_raw_close(base_raw);
233 size = delta_size(deltas, ndeltas);
234 if (size + 32 < best){
236 * if we already picked a best delta,
242 m->ndeltas = ndeltas;
243 m->nchain = base->nchain + 1;
245 m->head = base->head;
255 got_object_raw_close(raw);
259 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
260 got_deltify_free(meta[i]->dtab);
261 meta[i]->dtab = NULL;
264 got_object_raw_close(raw);
266 got_object_raw_close(base_raw);
270 static const struct got_error *
271 search_packidx(int *found, struct got_object_id *id,
272 struct got_repository *repo)
274 const struct got_error *err = NULL;
275 struct got_packidx *packidx = NULL;
280 err = got_repo_search_packidx(&packidx, &idx, repo, id);
282 *found = 1; /* object is already packed */
283 else if (err->code == GOT_ERR_NO_OBJ)
288 static const int obj_types[] = {
294 GOT_OBJ_TYPE_OFFSET_DELTA,
295 GOT_OBJ_TYPE_REF_DELTA
298 static const struct got_error *
299 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
300 struct got_object_id *id, const char *path, int obj_type,
301 time_t mtime, int loose_obj_only, struct got_repository *repo)
303 const struct got_error *err;
304 struct got_pack_meta *m;
306 if (loose_obj_only) {
308 err = search_packidx(&is_packed, id, repo);
315 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
322 err = alloc_meta(&m, id, path, obj_type, mtime);
326 if (v->nmeta == v->metasz){
327 size_t newsize = 2 * v->metasz;
328 struct got_pack_meta **new;
329 new = reallocarray(v->meta, newsize, sizeof(*new));
331 err = got_error_from_errno("reallocarray");
342 v->meta[v->nmeta++] = m;
347 static const struct got_error *
348 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
349 struct got_object_idset *idset, struct got_object_id *tree_id,
350 const char *dpath, time_t mtime, struct got_repository *repo,
351 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
353 const struct got_error *err;
354 struct got_tree_object *tree;
358 err = got_object_open_as_tree(&tree, repo, tree_id);
362 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
363 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
364 struct got_object_id *id = got_tree_entry_get_id(e);
365 mode_t mode = got_tree_entry_get_mode(e);
368 err = (*cancel_cb)(cancel_arg);
373 if (got_object_tree_entry_is_symlink(e) ||
374 got_object_tree_entry_is_submodule(e) ||
375 got_object_idset_contains(idset, id))
378 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
379 got_tree_entry_get_name(e)) == -1) {
380 err = got_error_from_errno("asprintf");
385 struct got_object_qid *qid;
386 err = got_object_qid_alloc(&qid, id);
389 STAILQ_INSERT_TAIL(ids, qid, entry);
390 } else if (S_ISREG(mode)) {
391 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
392 mtime, loose_obj_only, repo);
400 got_object_tree_close(tree);
405 static const struct got_error *
406 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
407 struct got_object_id *tree_id, const char *dpath, time_t mtime,
408 int loose_obj_only, struct got_repository *repo,
409 got_cancel_cb cancel_cb, void *cancel_arg)
411 const struct got_error *err = NULL;
412 struct got_object_id_queue tree_ids;
413 struct got_object_qid *qid;
415 if (got_object_idset_contains(idset, tree_id))
418 err = got_object_qid_alloc(&qid, tree_id);
422 STAILQ_INIT(&tree_ids);
423 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
425 while (!STAILQ_EMPTY(&tree_ids)) {
427 err = (*cancel_cb)(cancel_arg);
432 qid = STAILQ_FIRST(&tree_ids);
433 STAILQ_REMOVE_HEAD(&tree_ids, entry);
435 if (got_object_idset_contains(idset, qid->id)) {
436 got_object_qid_free(qid);
440 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
441 mtime, loose_obj_only, repo);
443 got_object_qid_free(qid);
447 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
448 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
449 got_object_qid_free(qid);
454 got_object_id_queue_free(&tree_ids);
458 static const struct got_error *
459 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
460 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
461 got_cancel_cb cancel_cb, void *cancel_arg)
463 const struct got_error *err;
464 struct got_commit_object *commit;
466 if (got_object_idset_contains(idset, id))
469 if (loose_obj_only) {
471 err = search_packidx(&is_packed, id, repo);
478 err = got_object_open_as_commit(&commit, repo, id);
482 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
483 got_object_commit_get_committer_time(commit),
484 loose_obj_only, repo);
488 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
489 "", got_object_commit_get_committer_time(commit),
490 loose_obj_only, repo, cancel_cb, cancel_arg);
492 got_object_commit_close(commit);
496 static const struct got_error *
497 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
498 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
499 got_cancel_cb cancel_cb, void *cancel_arg)
501 const struct got_error *err;
502 struct got_tag_object *tag = NULL;
504 if (got_object_idset_contains(idset, id))
507 if (loose_obj_only) {
509 err = search_packidx(&is_packed, id, repo);
516 err = got_object_open_as_tag(&tag, repo, id);
520 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
521 got_object_tag_get_tagger_time(tag),
522 loose_obj_only, repo);
526 switch (got_object_tag_get_object_type(tag)) {
527 case GOT_OBJ_TYPE_COMMIT:
528 err = load_commit(v, idset,
529 got_object_tag_get_object_id(tag), repo,
530 loose_obj_only, cancel_cb, cancel_arg);
532 case GOT_OBJ_TYPE_TREE:
533 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
534 "", got_object_tag_get_tagger_time(tag),
535 loose_obj_only, repo, cancel_cb, cancel_arg);
542 got_object_tag_close(tag);
546 enum findtwixt_color {
551 static const int findtwixt_colors[] = {
557 static const struct got_error *
558 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
559 int color, struct got_repository *repo)
561 const struct got_error *err;
562 struct got_object_qid *qid;
564 err = got_object_qid_alloc(&qid, id);
568 STAILQ_INSERT_TAIL(ids, qid, entry);
569 qid->data = (void *)&findtwixt_colors[color];
573 static const struct got_error *
574 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
575 struct got_object_id *id, struct got_repository *repo,
576 got_cancel_cb cancel_cb, void *cancel_arg)
578 const struct got_error *err = NULL;
579 struct got_commit_object *commit;
580 const struct got_object_id_queue *parents;
581 struct got_object_id_queue ids;
582 struct got_object_qid *qid;
586 err = got_object_qid_alloc(&qid, id);
589 STAILQ_INSERT_HEAD(&ids, qid, entry);
591 while (!STAILQ_EMPTY(&ids)) {
593 err = (*cancel_cb)(cancel_arg);
598 qid = STAILQ_FIRST(&ids);
599 STAILQ_REMOVE_HEAD(&ids, entry);
601 if (got_object_idset_contains(drop, qid->id)) {
602 got_object_qid_free(qid);
606 err = got_object_idset_add(drop, qid->id,
607 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
609 got_object_qid_free(qid);
613 if (!got_object_idset_contains(keep, qid->id)) {
614 got_object_qid_free(qid);
618 err = got_object_open_as_commit(&commit, repo, qid->id);
619 got_object_qid_free(qid);
623 parents = got_object_commit_get_parent_ids(commit);
625 err = got_object_id_queue_copy(parents, &ids);
627 got_object_commit_close(commit);
631 got_object_commit_close(commit);
634 got_object_id_queue_free(&ids);
638 struct append_id_arg {
639 struct got_object_id **array;
643 static const struct got_error *
644 append_id(struct got_object_id *id, void *data, void *arg)
646 struct append_id_arg *a = arg;
648 a->array[a->idx] = got_object_id_dup(id);
649 if (a->array[a->idx] == NULL)
650 return got_error_from_errno("got_object_id_dup");
656 static const struct got_error *
657 findtwixt(struct got_object_id ***res, int *nres,
658 struct got_object_id **head, int nhead,
659 struct got_object_id **tail, int ntail,
660 struct got_repository *repo,
661 got_cancel_cb cancel_cb, void *cancel_arg)
663 const struct got_error *err = NULL;
664 struct got_object_id_queue ids;
665 struct got_object_idset *keep, *drop;
666 struct got_object_qid *qid;
667 int i, ncolor, nkeep, obj_type;
673 keep = got_object_idset_alloc();
675 return got_error_from_errno("got_object_idset_alloc");
677 drop = got_object_idset_alloc();
679 err = got_error_from_errno("got_object_idset_alloc");
683 for (i = 0; i < nhead; i++) {
684 struct got_object_id *id = head[i];
687 err = got_object_get_type(&obj_type, repo, id);
690 if (obj_type != GOT_OBJ_TYPE_COMMIT)
692 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
696 for (i = 0; i < ntail; i++) {
697 struct got_object_id *id = tail[i];
700 err = got_object_get_type(&obj_type, repo, id);
703 if (obj_type != GOT_OBJ_TYPE_COMMIT)
705 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
710 while (!STAILQ_EMPTY(&ids)) {
712 qid = STAILQ_FIRST(&ids);
713 qcolor = *((int *)qid->data);
715 if (got_object_idset_contains(drop, qid->id))
717 else if (got_object_idset_contains(keep, qid->id))
720 ncolor = COLOR_BLANK;
722 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
723 qcolor == COLOR_KEEP)) {
724 STAILQ_REMOVE_HEAD(&ids, entry);
725 got_object_qid_free(qid);
729 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
730 err = drop_commit(keep, drop, qid->id, repo,
731 cancel_cb, cancel_arg);
734 } else if (ncolor == COLOR_BLANK) {
735 struct got_commit_object *commit;
736 struct got_object_id *id;
737 const struct got_object_id_queue *parents;
738 struct got_object_qid *pid;
740 id = got_object_id_dup(qid->id);
742 err = got_error_from_errno("got_object_id_dup");
745 if (qcolor == COLOR_KEEP)
746 err = got_object_idset_add(keep, id,
747 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
749 err = got_object_idset_add(drop, id,
750 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
756 err = got_object_open_as_commit(&commit, repo, id);
761 parents = got_object_commit_get_parent_ids(commit);
763 STAILQ_FOREACH(pid, parents, entry) {
764 err = queue_commit_id(&ids, pid->id,
772 got_object_commit_close(commit);
775 /* should not happen */
776 err = got_error_fmt(GOT_ERR_NOT_IMPL,
777 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
781 STAILQ_REMOVE_HEAD(&ids, entry);
782 got_object_qid_free(qid);
785 nkeep = got_object_idset_num_elements(keep);
787 struct append_id_arg arg;
788 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
789 if (arg.array == NULL) {
790 err = got_error_from_errno("calloc");
794 err = got_object_idset_for_each(keep, append_id, &arg);
803 got_object_idset_free(keep);
804 got_object_idset_free(drop);
805 got_object_id_queue_free(&ids);
809 static const struct got_error *
810 read_meta(struct got_pack_meta ***meta, int *nmeta,
811 struct got_object_id **theirs, int ntheirs,
812 struct got_object_id **ours, int nours, struct got_repository *repo,
813 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
814 got_cancel_cb cancel_cb, void *cancel_arg)
816 const struct got_error *err = NULL;
817 struct got_object_id **ids = NULL;
818 struct got_object_idset *idset;
819 int i, nobj = 0, obj_type;
820 struct got_pack_metavec v;
825 idset = got_object_idset_alloc();
827 return got_error_from_errno("got_object_idset_alloc");
831 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
832 if (v.meta == NULL) {
833 err = got_error_from_errno("reallocarray");
837 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
838 cancel_cb, cancel_arg);
839 if (err || nobj == 0)
842 for (i = 0; i < ntheirs; i++) {
843 struct got_object_id *id = theirs[i];
846 err = got_object_get_type(&obj_type, repo, id);
849 if (obj_type != GOT_OBJ_TYPE_COMMIT)
851 err = load_commit(NULL, idset, id, repo,
852 loose_obj_only, cancel_cb, cancel_arg);
856 err = progress_cb(progress_arg, 0L, nours,
863 for (i = 0; i < ntheirs; i++) {
864 struct got_object_id *id = theirs[i];
868 cached_type = got_object_idset_get(idset, id);
869 if (cached_type == NULL) {
870 err = got_object_get_type(&obj_type, repo, id);
874 obj_type = *cached_type;
875 if (obj_type != GOT_OBJ_TYPE_TAG)
877 err = load_tag(NULL, idset, id, repo,
878 loose_obj_only, cancel_cb, cancel_arg);
882 err = progress_cb(progress_arg, 0L, nours,
889 for (i = 0; i < nobj; i++) {
890 err = load_commit(&v, idset, ids[i], repo,
891 loose_obj_only, cancel_cb, cancel_arg);
895 err = progress_cb(progress_arg, 0L, nours,
902 for (i = 0; i < nours; i++) {
903 struct got_object_id *id = ours[i];
907 cached_type = got_object_idset_get(idset, id);
908 if (cached_type == NULL) {
909 err = got_object_get_type(&obj_type, repo, id);
913 obj_type = *cached_type;
914 if (obj_type != GOT_OBJ_TYPE_TAG)
916 err = load_tag(&v, idset, id, repo,
917 loose_obj_only, cancel_cb, cancel_arg);
921 err = progress_cb(progress_arg, 0L, nours,
929 for (i = 0; i < nobj; i++) {
933 got_object_idset_free(idset);
943 const struct got_error *
944 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
948 SHA1Update(ctx, buf, len);
949 n = fwrite(buf, 1, len, f);
951 return got_ferror(f, GOT_ERR_IO);
956 putbe32(char *b, uint32_t n)
965 write_order_cmp(const void *pa, const void *pb)
967 struct got_pack_meta *a, *b, *ahd, *bhd;
969 a = *(struct got_pack_meta **)pa;
970 b = *(struct got_pack_meta **)pb;
971 ahd = (a->head == NULL) ? a : a->head;
972 bhd = (b->head == NULL) ? b : b->head;
973 if (ahd->mtime != bhd->mtime)
974 return bhd->mtime - ahd->mtime;
976 return (uintptr_t)bhd - (uintptr_t)ahd;
977 if (a->nchain != b->nchain)
978 return a->nchain - b->nchain;
979 return a->mtime - b->mtime;
982 static const struct got_error *
983 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
989 hdr[0] = obj_type << 4;
992 for (i = 1; len != 0; i++){
994 return got_error(GOT_ERR_NO_SPACE);
995 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
996 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
997 len >>= GOT_DELTA_SIZE_SHIFT;
1004 static const struct got_error *
1005 append(char **p, int *len, int *sz, void *seg, int nseg)
1009 if (*len + nseg >= *sz) {
1010 while (*len + nseg >= *sz)
1012 n = realloc(*p, *sz);
1014 return got_error_from_errno("realloc");
1017 memcpy(*p + *len, seg, nseg);
1023 static const struct got_error *
1024 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1025 off_t base_size, char **pp)
1027 const struct got_error *err = NULL;
1029 unsigned char buf[16], *bp;
1032 struct got_delta_instruction *d;
1041 return got_error_from_errno("malloc");
1043 /* base object size */
1044 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1045 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1046 for (i = 1; n > 0; i++) {
1047 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1048 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1049 n >>= GOT_DELTA_SIZE_SHIFT;
1051 err = append(&p, &len, &sz, buf, i);
1055 /* target object size */
1056 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1057 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1058 for (i = 1; n > 0; i++) {
1059 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1060 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1061 n >>= GOT_DELTA_SIZE_SHIFT;
1063 err = append(&p, &len, &sz, buf, i);
1066 for (j = 0; j < m->ndeltas; j++) {
1071 buf[0] = GOT_DELTA_BASE_COPY;
1072 for (i = 0; i < 4; i++) {
1073 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1082 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1083 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1084 for (i = 0; i < 3 && n > 0; i++) {
1085 buf[0] |= 1 << (i + 4);
1090 err = append(&p, &len, &sz, buf, bp - buf);
1096 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1097 return got_error_from_errno("fseeko");
1099 while (n != d->len) {
1100 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1101 err = append(&p, &len, &sz, buf, 1);
1104 r = fread(content, 1, buf[0], o->f);
1106 return got_ferror(o->f, GOT_ERR_IO);
1107 err = append(&p, &len, &sz, content, buf[0]);
1120 packoff(char *hdr, off_t off)
1125 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1126 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1127 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1128 GOT_DELTA_SIZE_MORE;
1133 hdr[j++] = rbuf[--i];
1137 static const struct got_error *
1138 genpack(uint8_t *pack_sha1, FILE *packfile,
1139 struct got_pack_meta **meta, int nmeta, int nours,
1140 int use_offset_deltas, struct got_repository *repo,
1141 got_pack_progress_cb progress_cb, void *progress_arg,
1142 got_cancel_cb cancel_cb, void *cancel_arg)
1144 const struct got_error *err = NULL;
1147 struct got_pack_meta *m;
1148 struct got_raw_object *raw;
1149 char *p = NULL, buf[32];
1151 struct got_deflate_checksum csum;
1152 off_t packfile_size = 0;
1155 csum.output_sha1 = &ctx;
1156 csum.output_crc = NULL;
1158 err = hwrite(packfile, "PACK", 4, &ctx);
1161 putbe32(buf, GOT_PACKFILE_VERSION);
1162 err = hwrite(packfile, buf, 4, &ctx);
1165 putbe32(buf, nmeta);
1166 err = hwrite(packfile, buf, 4, &ctx);
1169 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1170 for (i = 0; i < nmeta; i++) {
1172 err = progress_cb(progress_arg, packfile_size, nours,
1178 m->off = ftello(packfile);
1179 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1182 if (m->deltas == NULL) {
1183 err = packhdr(&nh, buf, sizeof(buf),
1184 m->obj_type, raw->size);
1187 err = hwrite(packfile, buf, nh, &ctx);
1190 packfile_size += nh;
1191 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1192 err = got_error_from_errno("fseeko");
1195 err = got_deflate_to_file(&outlen, raw->f, packfile,
1199 packfile_size += outlen;
1202 struct got_raw_object *base_raw;
1203 err = got_object_raw_open(&base_raw, repo,
1204 &m->prev->id, 8192);
1207 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1210 got_object_raw_close(base_raw);
1211 if (use_offset_deltas && m->prev->off != 0) {
1212 err = packhdr(&nh, buf, sizeof(buf),
1213 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1216 nh += packoff(buf + nh,
1217 m->off - m->prev->off);
1218 err = hwrite(packfile, buf, nh, &ctx);
1221 packfile_size += nh;
1223 err = packhdr(&nh, buf, sizeof(buf),
1224 GOT_OBJ_TYPE_REF_DELTA, nd);
1225 err = hwrite(packfile, buf, nh, &ctx);
1228 packfile_size += nh;
1229 err = hwrite(packfile, m->prev->id.sha1,
1230 sizeof(m->prev->id.sha1), &ctx);
1231 packfile_size += sizeof(m->prev->id.sha1);
1235 /* XXX need got_deflate_from_mem() */
1236 delta_file = fmemopen(p, nd, "r");
1237 if (delta_file == NULL) {
1238 err = got_error_from_errno("fmemopen");
1241 err = got_deflate_to_file(&outlen, delta_file,
1246 packfile_size += outlen;
1250 got_object_raw_close(raw);
1253 SHA1Final(pack_sha1, &ctx);
1254 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1255 if (n != SHA1_DIGEST_LENGTH)
1256 err = got_ferror(packfile, GOT_ERR_IO);
1257 packfile_size += SHA1_DIGEST_LENGTH;
1258 packfile_size += sizeof(struct got_packfile_hdr);
1259 err = progress_cb(progress_arg, packfile_size, nours,
1260 nmeta, nmeta, nmeta);
1268 const struct got_error *
1269 got_pack_create(uint8_t *packsha1, FILE *packfile,
1270 struct got_object_id **theirs, int ntheirs,
1271 struct got_object_id **ours, int nours,
1272 struct got_repository *repo, int loose_obj_only, int allow_empty,
1273 got_pack_progress_cb progress_cb, void *progress_arg,
1274 got_cancel_cb cancel_cb, void *cancel_arg)
1276 const struct got_error *err;
1277 struct got_pack_meta **meta;
1280 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1281 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1285 if (nmeta == 0 && !allow_empty) {
1286 err = got_error(GOT_ERR_CANNOT_PACK);
1290 err = pick_deltas(meta, nmeta, nours, repo,
1291 progress_cb, progress_arg, cancel_cb, cancel_arg);
1296 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1297 progress_cb, progress_arg, cancel_cb, cancel_arg);
1301 free_nmeta(meta, nmeta);