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>
33 #include "got_error.h"
34 #include "got_cancel.h"
35 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_repository_admin.h"
40 #include "got_lib_deltify.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_object.h"
43 #include "got_lib_object_idset.h"
44 #include "got_lib_object_cache.h"
45 #include "got_lib_deflate.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_repository.h"
51 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
54 struct got_pack_meta {
55 struct got_object_id id;
60 /* The best delta we picked */
61 struct got_pack_meta *head;
62 struct got_pack_meta *prev;
63 struct got_delta_instruction *deltas;
67 /* Only used for delta window */
68 struct got_delta_table *dtab;
70 /* Only used for writing offset deltas */
74 struct got_pack_metavec {
75 struct got_pack_meta **meta;
80 static const struct got_error *
81 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
82 const char *path, int obj_type, time_t mtime)
84 const struct got_error *err = NULL;
85 struct got_pack_meta *m;
89 m = calloc(1, sizeof(*m));
91 return got_error_from_errno("calloc");
93 memcpy(&m->id, id, sizeof(m->id));
95 m->path = strdup(path);
96 if (m->path == NULL) {
97 err = got_error_from_errno("strdup");
102 m->obj_type = obj_type;
109 clear_meta(struct got_pack_meta *meta)
120 free_nmeta(struct got_pack_meta **meta, int nmeta)
124 for (i = 0; i < nmeta; i++)
130 delta_order_cmp(const void *pa, const void *pb)
132 struct got_pack_meta *a, *b;
135 a = *(struct got_pack_meta **)pa;
136 b = *(struct got_pack_meta **)pb;
138 if (a->obj_type != b->obj_type)
139 return a->obj_type - b->obj_type;
140 cmp = strcmp(a->path, b->path);
143 if (a->mtime != b->mtime)
144 return a->mtime - b->mtime;
145 return got_object_id_cmp(&a->id, &b->id);
149 delta_size(struct got_delta_instruction *deltas, int ndeltas)
152 for (i = 0; i < ndeltas; i++) {
154 size += GOT_DELTA_SIZE_SHIFT;
156 size += deltas[i].len + 1;
162 static const struct got_error *
163 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
164 struct got_repository *repo,
165 got_pack_progress_cb progress_cb, void *progress_arg,
166 got_cancel_cb cancel_cb, void *cancel_arg)
168 const struct got_error *err = NULL;
169 struct got_pack_meta *m = NULL, *base = NULL;
170 struct got_raw_object *raw = NULL, *base_raw = NULL;
171 struct got_delta_instruction *deltas;
172 int i, j, size, ndeltas, best;
173 const int max_base_candidates = 10;
175 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
176 for (i = 0; i < nmeta; i++) {
178 err = (*cancel_cb)(cancel_arg);
183 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
191 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
192 m->obj_type == GOT_OBJ_TYPE_TAG)
195 err = got_object_raw_open(&raw, repo, &m->id, 8192);
199 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
200 raw->size + raw->hdrlen);
204 if (i > max_base_candidates) {
205 struct got_pack_meta *n = NULL;
206 n = meta[i - (max_base_candidates + 1)];
207 got_deltify_free(n->dtab);
212 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
214 err = (*cancel_cb)(cancel_arg);
219 /* long chains make unpacking slow, avoid such bases */
220 if (base->nchain >= 128 ||
221 base->obj_type != m->obj_type)
224 err = got_object_raw_open(&base_raw, repo, &base->id,
228 err = got_deltify(&deltas, &ndeltas,
229 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
230 base->dtab, base_raw->f, base_raw->hdrlen,
231 base_raw->size + base_raw->hdrlen);
232 got_object_raw_close(base_raw);
237 size = delta_size(deltas, ndeltas);
238 if (size + 32 < best){
240 * if we already picked a best delta,
246 m->ndeltas = ndeltas;
247 m->nchain = base->nchain + 1;
249 m->head = base->head;
259 got_object_raw_close(raw);
263 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
264 got_deltify_free(meta[i]->dtab);
265 meta[i]->dtab = NULL;
268 got_object_raw_close(raw);
270 got_object_raw_close(base_raw);
274 static const struct got_error *
275 search_packidx(int *found, struct got_object_id *id,
276 struct got_repository *repo)
278 const struct got_error *err = NULL;
279 struct got_packidx *packidx = NULL;
284 err = got_repo_search_packidx(&packidx, &idx, repo, id);
286 *found = 1; /* object is already packed */
287 else if (err->code == GOT_ERR_NO_OBJ)
292 static const int obj_types[] = {
298 GOT_OBJ_TYPE_OFFSET_DELTA,
299 GOT_OBJ_TYPE_REF_DELTA
302 static const struct got_error *
303 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
304 struct got_object_id *id, const char *path, int obj_type,
305 time_t mtime, int loose_obj_only, struct got_repository *repo)
307 const struct got_error *err;
308 struct got_pack_meta *m;
310 if (loose_obj_only) {
312 err = search_packidx(&is_packed, id, repo);
319 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
326 err = alloc_meta(&m, id, path, obj_type, mtime);
330 if (v->nmeta == v->metasz){
331 size_t newsize = 2 * v->metasz;
332 struct got_pack_meta **new;
333 new = reallocarray(v->meta, newsize, sizeof(*new));
335 err = got_error_from_errno("reallocarray");
346 v->meta[v->nmeta++] = m;
351 static const struct got_error *
352 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
353 struct got_object_idset *idset, struct got_object_id *tree_id,
354 const char *dpath, time_t mtime, struct got_repository *repo,
355 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
357 const struct got_error *err;
358 struct got_tree_object *tree;
362 err = got_object_open_as_tree(&tree, repo, tree_id);
366 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
367 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
368 struct got_object_id *id = got_tree_entry_get_id(e);
369 mode_t mode = got_tree_entry_get_mode(e);
372 err = (*cancel_cb)(cancel_arg);
377 if (got_object_tree_entry_is_submodule(e) ||
378 got_object_idset_contains(idset, id))
381 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
382 got_tree_entry_get_name(e)) == -1) {
383 err = got_error_from_errno("asprintf");
388 struct got_object_qid *qid;
389 err = got_object_qid_alloc(&qid, id);
392 STAILQ_INSERT_TAIL(ids, qid, entry);
393 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
394 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
395 mtime, loose_obj_only, repo);
403 got_object_tree_close(tree);
408 static const struct got_error *
409 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
410 struct got_object_id *tree_id, const char *dpath, time_t mtime,
411 int loose_obj_only, struct got_repository *repo,
412 got_cancel_cb cancel_cb, void *cancel_arg)
414 const struct got_error *err = NULL;
415 struct got_object_id_queue tree_ids;
416 struct got_object_qid *qid;
418 if (got_object_idset_contains(idset, tree_id))
421 err = got_object_qid_alloc(&qid, tree_id);
425 STAILQ_INIT(&tree_ids);
426 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
428 while (!STAILQ_EMPTY(&tree_ids)) {
430 err = (*cancel_cb)(cancel_arg);
435 qid = STAILQ_FIRST(&tree_ids);
436 STAILQ_REMOVE_HEAD(&tree_ids, entry);
438 if (got_object_idset_contains(idset, qid->id)) {
439 got_object_qid_free(qid);
443 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
444 mtime, loose_obj_only, repo);
446 got_object_qid_free(qid);
450 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
451 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
452 got_object_qid_free(qid);
457 got_object_id_queue_free(&tree_ids);
461 static const struct got_error *
462 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
463 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
464 got_cancel_cb cancel_cb, void *cancel_arg)
466 const struct got_error *err;
467 struct got_commit_object *commit;
469 if (got_object_idset_contains(idset, id))
472 if (loose_obj_only) {
474 err = search_packidx(&is_packed, id, repo);
481 err = got_object_open_as_commit(&commit, repo, id);
485 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
486 got_object_commit_get_committer_time(commit),
487 loose_obj_only, repo);
491 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
492 "", got_object_commit_get_committer_time(commit),
493 loose_obj_only, repo, cancel_cb, cancel_arg);
495 got_object_commit_close(commit);
499 static const struct got_error *
500 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
501 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
502 got_cancel_cb cancel_cb, void *cancel_arg)
504 const struct got_error *err;
505 struct got_tag_object *tag = NULL;
507 if (got_object_idset_contains(idset, id))
510 if (loose_obj_only) {
512 err = search_packidx(&is_packed, id, repo);
519 err = got_object_open_as_tag(&tag, repo, id);
523 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
524 got_object_tag_get_tagger_time(tag),
525 loose_obj_only, repo);
529 switch (got_object_tag_get_object_type(tag)) {
530 case GOT_OBJ_TYPE_COMMIT:
531 err = load_commit(v, idset,
532 got_object_tag_get_object_id(tag), repo,
533 loose_obj_only, cancel_cb, cancel_arg);
535 case GOT_OBJ_TYPE_TREE:
536 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
537 "", got_object_tag_get_tagger_time(tag),
538 loose_obj_only, repo, cancel_cb, cancel_arg);
545 got_object_tag_close(tag);
549 enum findtwixt_color {
554 static const int findtwixt_colors[] = {
560 static const struct got_error *
561 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
562 int color, struct got_repository *repo)
564 const struct got_error *err;
565 struct got_object_qid *qid;
567 err = got_object_qid_alloc(&qid, id);
571 STAILQ_INSERT_TAIL(ids, qid, entry);
572 qid->data = (void *)&findtwixt_colors[color];
576 static const struct got_error *
577 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
578 struct got_object_id *id, struct got_repository *repo,
579 got_cancel_cb cancel_cb, void *cancel_arg)
581 const struct got_error *err = NULL;
582 struct got_commit_object *commit;
583 const struct got_object_id_queue *parents;
584 struct got_object_id_queue ids;
585 struct got_object_qid *qid;
589 err = got_object_qid_alloc(&qid, id);
592 STAILQ_INSERT_HEAD(&ids, qid, entry);
594 while (!STAILQ_EMPTY(&ids)) {
596 err = (*cancel_cb)(cancel_arg);
601 qid = STAILQ_FIRST(&ids);
602 STAILQ_REMOVE_HEAD(&ids, entry);
604 if (got_object_idset_contains(drop, qid->id)) {
605 got_object_qid_free(qid);
609 err = got_object_idset_add(drop, qid->id,
610 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
612 got_object_qid_free(qid);
616 if (!got_object_idset_contains(keep, qid->id)) {
617 got_object_qid_free(qid);
621 err = got_object_open_as_commit(&commit, repo, qid->id);
622 got_object_qid_free(qid);
626 parents = got_object_commit_get_parent_ids(commit);
628 err = got_object_id_queue_copy(parents, &ids);
630 got_object_commit_close(commit);
634 got_object_commit_close(commit);
637 got_object_id_queue_free(&ids);
641 struct append_id_arg {
642 struct got_object_id **array;
646 static const struct got_error *
647 append_id(struct got_object_id *id, void *data, void *arg)
649 struct append_id_arg *a = arg;
651 a->array[a->idx] = got_object_id_dup(id);
652 if (a->array[a->idx] == NULL)
653 return got_error_from_errno("got_object_id_dup");
659 static const struct got_error *
660 findtwixt(struct got_object_id ***res, int *nres,
661 struct got_object_id **head, int nhead,
662 struct got_object_id **tail, int ntail,
663 struct got_repository *repo,
664 got_cancel_cb cancel_cb, void *cancel_arg)
666 const struct got_error *err = NULL;
667 struct got_object_id_queue ids;
668 struct got_object_idset *keep, *drop;
669 struct got_object_qid *qid;
670 int i, ncolor, nkeep, obj_type;
676 keep = got_object_idset_alloc();
678 return got_error_from_errno("got_object_idset_alloc");
680 drop = got_object_idset_alloc();
682 err = got_error_from_errno("got_object_idset_alloc");
686 for (i = 0; i < nhead; i++) {
687 struct got_object_id *id = head[i];
690 err = got_object_get_type(&obj_type, repo, id);
693 if (obj_type != GOT_OBJ_TYPE_COMMIT)
695 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
699 for (i = 0; i < ntail; i++) {
700 struct got_object_id *id = tail[i];
703 err = got_object_get_type(&obj_type, repo, id);
706 if (obj_type != GOT_OBJ_TYPE_COMMIT)
708 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
713 while (!STAILQ_EMPTY(&ids)) {
715 qid = STAILQ_FIRST(&ids);
716 qcolor = *((int *)qid->data);
718 if (got_object_idset_contains(drop, qid->id))
720 else if (got_object_idset_contains(keep, qid->id))
723 ncolor = COLOR_BLANK;
725 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
726 qcolor == COLOR_KEEP)) {
727 STAILQ_REMOVE_HEAD(&ids, entry);
728 got_object_qid_free(qid);
732 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
733 err = drop_commit(keep, drop, qid->id, repo,
734 cancel_cb, cancel_arg);
737 } else if (ncolor == COLOR_BLANK) {
738 struct got_commit_object *commit;
739 struct got_object_id *id;
740 const struct got_object_id_queue *parents;
741 struct got_object_qid *pid;
743 id = got_object_id_dup(qid->id);
745 err = got_error_from_errno("got_object_id_dup");
748 if (qcolor == COLOR_KEEP)
749 err = got_object_idset_add(keep, id,
750 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
752 err = got_object_idset_add(drop, id,
753 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
759 err = got_object_open_as_commit(&commit, repo, id);
764 parents = got_object_commit_get_parent_ids(commit);
766 STAILQ_FOREACH(pid, parents, entry) {
767 err = queue_commit_id(&ids, pid->id,
775 got_object_commit_close(commit);
778 /* should not happen */
779 err = got_error_fmt(GOT_ERR_NOT_IMPL,
780 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
784 STAILQ_REMOVE_HEAD(&ids, entry);
785 got_object_qid_free(qid);
788 nkeep = got_object_idset_num_elements(keep);
790 struct append_id_arg arg;
791 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
792 if (arg.array == NULL) {
793 err = got_error_from_errno("calloc");
797 err = got_object_idset_for_each(keep, append_id, &arg);
806 got_object_idset_free(keep);
807 got_object_idset_free(drop);
808 got_object_id_queue_free(&ids);
812 static const struct got_error *
813 read_meta(struct got_pack_meta ***meta, int *nmeta,
814 struct got_object_id **theirs, int ntheirs,
815 struct got_object_id **ours, int nours, struct got_repository *repo,
816 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
817 got_cancel_cb cancel_cb, void *cancel_arg)
819 const struct got_error *err = NULL;
820 struct got_object_id **ids = NULL;
821 struct got_object_idset *idset;
822 int i, nobj = 0, obj_type;
823 struct got_pack_metavec v;
828 idset = got_object_idset_alloc();
830 return got_error_from_errno("got_object_idset_alloc");
834 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
835 if (v.meta == NULL) {
836 err = got_error_from_errno("calloc");
840 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
841 cancel_cb, cancel_arg);
842 if (err || nobj == 0)
845 for (i = 0; i < ntheirs; i++) {
846 struct got_object_id *id = theirs[i];
849 err = got_object_get_type(&obj_type, repo, id);
852 if (obj_type != GOT_OBJ_TYPE_COMMIT)
854 err = load_commit(NULL, idset, id, repo,
855 loose_obj_only, cancel_cb, cancel_arg);
859 err = progress_cb(progress_arg, 0L, nours,
866 for (i = 0; i < ntheirs; i++) {
867 struct got_object_id *id = theirs[i];
871 cached_type = got_object_idset_get(idset, id);
872 if (cached_type == NULL) {
873 err = got_object_get_type(&obj_type, repo, id);
877 obj_type = *cached_type;
878 if (obj_type != GOT_OBJ_TYPE_TAG)
880 err = load_tag(NULL, idset, id, repo,
881 loose_obj_only, cancel_cb, cancel_arg);
885 err = progress_cb(progress_arg, 0L, nours,
892 for (i = 0; i < nobj; i++) {
893 err = load_commit(&v, idset, ids[i], repo,
894 loose_obj_only, cancel_cb, cancel_arg);
898 err = progress_cb(progress_arg, 0L, nours,
905 for (i = 0; i < nours; i++) {
906 struct got_object_id *id = ours[i];
910 cached_type = got_object_idset_get(idset, id);
911 if (cached_type == NULL) {
912 err = got_object_get_type(&obj_type, repo, id);
916 obj_type = *cached_type;
917 if (obj_type != GOT_OBJ_TYPE_TAG)
919 err = load_tag(&v, idset, id, repo,
920 loose_obj_only, cancel_cb, cancel_arg);
924 err = progress_cb(progress_arg, 0L, nours,
932 for (i = 0; i < nobj; i++) {
936 got_object_idset_free(idset);
946 const struct got_error *
947 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
951 SHA1Update(ctx, buf, len);
952 n = fwrite(buf, 1, len, f);
954 return got_ferror(f, GOT_ERR_IO);
959 putbe32(char *b, uint32_t n)
968 write_order_cmp(const void *pa, const void *pb)
970 struct got_pack_meta *a, *b, *ahd, *bhd;
972 a = *(struct got_pack_meta **)pa;
973 b = *(struct got_pack_meta **)pb;
974 ahd = (a->head == NULL) ? a : a->head;
975 bhd = (b->head == NULL) ? b : b->head;
976 if (ahd->mtime != bhd->mtime)
977 return bhd->mtime - ahd->mtime;
979 return (uintptr_t)bhd - (uintptr_t)ahd;
980 if (a->nchain != b->nchain)
981 return a->nchain - b->nchain;
982 return a->mtime - b->mtime;
985 static const struct got_error *
986 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
992 hdr[0] = obj_type << 4;
995 for (i = 1; len != 0; i++){
997 return got_error(GOT_ERR_NO_SPACE);
998 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
999 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1000 len >>= GOT_DELTA_SIZE_SHIFT;
1007 static const struct got_error *
1008 append(char **p, int *len, int *sz, void *seg, int nseg)
1012 if (*len + nseg >= *sz) {
1013 while (*len + nseg >= *sz)
1015 n = realloc(*p, *sz);
1017 return got_error_from_errno("realloc");
1020 memcpy(*p + *len, seg, nseg);
1026 static const struct got_error *
1027 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1028 off_t base_size, char **pp)
1030 const struct got_error *err = NULL;
1032 unsigned char buf[16], *bp;
1035 struct got_delta_instruction *d;
1044 return got_error_from_errno("malloc");
1046 /* base object size */
1047 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1048 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1049 for (i = 1; n > 0; i++) {
1050 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1051 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1052 n >>= GOT_DELTA_SIZE_SHIFT;
1054 err = append(&p, &len, &sz, buf, i);
1058 /* target object size */
1059 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1060 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1061 for (i = 1; n > 0; i++) {
1062 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1063 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1064 n >>= GOT_DELTA_SIZE_SHIFT;
1066 err = append(&p, &len, &sz, buf, i);
1069 for (j = 0; j < m->ndeltas; j++) {
1074 buf[0] = GOT_DELTA_BASE_COPY;
1075 for (i = 0; i < 4; i++) {
1076 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1085 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1086 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1087 for (i = 0; i < 3 && n > 0; i++) {
1088 buf[0] |= 1 << (i + 4);
1093 err = append(&p, &len, &sz, buf, bp - buf);
1099 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1100 return got_error_from_errno("fseeko");
1102 while (n != d->len) {
1103 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1104 err = append(&p, &len, &sz, buf, 1);
1107 r = fread(content, 1, buf[0], o->f);
1109 return got_ferror(o->f, GOT_ERR_IO);
1110 err = append(&p, &len, &sz, content, buf[0]);
1123 packoff(char *hdr, off_t off)
1128 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1129 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1130 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1131 GOT_DELTA_SIZE_MORE;
1136 hdr[j++] = rbuf[--i];
1140 static const struct got_error *
1141 genpack(uint8_t *pack_sha1, FILE *packfile,
1142 struct got_pack_meta **meta, int nmeta, int nours,
1143 int use_offset_deltas, struct got_repository *repo,
1144 got_pack_progress_cb progress_cb, void *progress_arg,
1145 got_cancel_cb cancel_cb, void *cancel_arg)
1147 const struct got_error *err = NULL;
1150 struct got_pack_meta *m;
1151 struct got_raw_object *raw;
1152 char *p = NULL, buf[32];
1154 struct got_deflate_checksum csum;
1155 off_t packfile_size = 0;
1158 csum.output_sha1 = &ctx;
1159 csum.output_crc = NULL;
1161 err = hwrite(packfile, "PACK", 4, &ctx);
1164 putbe32(buf, GOT_PACKFILE_VERSION);
1165 err = hwrite(packfile, buf, 4, &ctx);
1168 putbe32(buf, nmeta);
1169 err = hwrite(packfile, buf, 4, &ctx);
1172 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1173 for (i = 0; i < nmeta; i++) {
1175 err = progress_cb(progress_arg, packfile_size, nours,
1181 m->off = ftello(packfile);
1182 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1185 if (m->deltas == NULL) {
1186 err = packhdr(&nh, buf, sizeof(buf),
1187 m->obj_type, raw->size);
1190 err = hwrite(packfile, buf, nh, &ctx);
1193 packfile_size += nh;
1194 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1195 err = got_error_from_errno("fseeko");
1198 err = got_deflate_to_file(&outlen, raw->f, packfile,
1202 packfile_size += outlen;
1205 struct got_raw_object *base_raw;
1206 err = got_object_raw_open(&base_raw, repo,
1207 &m->prev->id, 8192);
1210 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1213 got_object_raw_close(base_raw);
1214 if (use_offset_deltas && m->prev->off != 0) {
1215 err = packhdr(&nh, buf, sizeof(buf),
1216 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1219 nh += packoff(buf + nh,
1220 m->off - m->prev->off);
1221 err = hwrite(packfile, buf, nh, &ctx);
1224 packfile_size += nh;
1226 err = packhdr(&nh, buf, sizeof(buf),
1227 GOT_OBJ_TYPE_REF_DELTA, nd);
1228 err = hwrite(packfile, buf, nh, &ctx);
1231 packfile_size += nh;
1232 err = hwrite(packfile, m->prev->id.sha1,
1233 sizeof(m->prev->id.sha1), &ctx);
1234 packfile_size += sizeof(m->prev->id.sha1);
1238 /* XXX need got_deflate_from_mem() */
1239 delta_file = fmemopen(p, nd, "r");
1240 if (delta_file == NULL) {
1241 err = got_error_from_errno("fmemopen");
1244 err = got_deflate_to_file(&outlen, delta_file,
1249 packfile_size += outlen;
1253 got_object_raw_close(raw);
1256 SHA1Final(pack_sha1, &ctx);
1257 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1258 if (n != SHA1_DIGEST_LENGTH)
1259 err = got_ferror(packfile, GOT_ERR_IO);
1260 packfile_size += SHA1_DIGEST_LENGTH;
1261 packfile_size += sizeof(struct got_packfile_hdr);
1262 err = progress_cb(progress_arg, packfile_size, nours,
1263 nmeta, nmeta, nmeta);
1271 const struct got_error *
1272 got_pack_create(uint8_t *packsha1, FILE *packfile,
1273 struct got_object_id **theirs, int ntheirs,
1274 struct got_object_id **ours, int nours,
1275 struct got_repository *repo, int loose_obj_only, int allow_empty,
1276 got_pack_progress_cb progress_cb, void *progress_arg,
1277 got_cancel_cb cancel_cb, void *cancel_arg)
1279 const struct got_error *err;
1280 struct got_pack_meta **meta;
1283 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1284 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1288 if (nmeta == 0 && !allow_empty) {
1289 err = got_error(GOT_ERR_CANNOT_PACK);
1293 err = pick_deltas(meta, nmeta, nours, repo,
1294 progress_cb, progress_arg, cancel_cb, cancel_arg);
1299 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1300 progress_cb, progress_arg, cancel_cb, cancel_arg);
1304 free_nmeta(meta, nmeta);