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/queue.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 struct got_error *
289 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
290 struct got_object_id *id, const char *path, int obj_type,
291 time_t mtime, int loose_obj_only, struct got_repository *repo)
293 const struct got_error *err;
294 struct got_pack_meta *m;
296 if (loose_obj_only) {
298 err = search_packidx(&is_packed, id, repo);
305 err = got_object_idset_add(idset, id, NULL);
312 err = alloc_meta(&m, id, path, obj_type, mtime);
316 if (v->nmeta == v->metasz){
317 size_t newsize = 2 * v->metasz;
318 struct got_pack_meta **new;
319 new = reallocarray(v->meta, newsize, sizeof(*new));
321 err = got_error_from_errno("reallocarray");
332 v->meta[v->nmeta++] = m;
337 static const struct got_error *
338 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
339 struct got_object_idset *idset, struct got_object_id *tree_id,
340 const char *dpath, time_t mtime, struct got_repository *repo,
341 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
343 const struct got_error *err;
344 struct got_tree_object *tree;
348 err = got_object_open_as_tree(&tree, repo, tree_id);
352 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
353 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
354 struct got_object_id *id = got_tree_entry_get_id(e);
355 mode_t mode = got_tree_entry_get_mode(e);
358 err = (*cancel_cb)(cancel_arg);
363 if (got_object_tree_entry_is_symlink(e) ||
364 got_object_tree_entry_is_submodule(e) ||
365 got_object_idset_contains(idset, id))
368 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
369 got_tree_entry_get_name(e)) == -1) {
370 err = got_error_from_errno("asprintf");
375 struct got_object_qid *qid;
376 err = got_object_qid_alloc(&qid, id);
379 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
380 } else if (S_ISREG(mode)) {
381 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
382 mtime, loose_obj_only, repo);
390 got_object_tree_close(tree);
395 static const struct got_error *
396 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
397 struct got_object_id *tree_id, const char *dpath, time_t mtime,
398 int loose_obj_only, struct got_repository *repo,
399 got_cancel_cb cancel_cb, void *cancel_arg)
401 const struct got_error *err = NULL;
402 struct got_object_id_queue tree_ids;
403 struct got_object_qid *qid;
405 if (got_object_idset_contains(idset, tree_id))
408 err = got_object_qid_alloc(&qid, tree_id);
412 SIMPLEQ_INIT(&tree_ids);
413 SIMPLEQ_INSERT_TAIL(&tree_ids, qid, entry);
415 while (!SIMPLEQ_EMPTY(&tree_ids)) {
417 err = (*cancel_cb)(cancel_arg);
422 qid = SIMPLEQ_FIRST(&tree_ids);
423 SIMPLEQ_REMOVE_HEAD(&tree_ids, entry);
425 if (got_object_idset_contains(idset, qid->id)) {
426 got_object_qid_free(qid);
430 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
431 mtime, loose_obj_only, repo);
433 got_object_qid_free(qid);
437 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
438 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
439 got_object_qid_free(qid);
444 got_object_id_queue_free(&tree_ids);
448 static const struct got_error *
449 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
450 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
451 got_cancel_cb cancel_cb, void *cancel_arg)
453 const struct got_error *err;
454 struct got_commit_object *commit;
456 if (got_object_idset_contains(idset, id))
459 if (loose_obj_only) {
461 err = search_packidx(&is_packed, id, repo);
468 err = got_object_open_as_commit(&commit, repo, id);
472 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
473 got_object_commit_get_committer_time(commit),
474 loose_obj_only, repo);
478 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
479 "", got_object_commit_get_committer_time(commit),
480 loose_obj_only, repo, cancel_cb, cancel_arg);
482 got_object_commit_close(commit);
486 static const struct got_error *
487 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
488 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
489 got_cancel_cb cancel_cb, void *cancel_arg)
491 const struct got_error *err;
492 struct got_tag_object *tag = NULL;
494 if (got_object_idset_contains(idset, id))
497 if (loose_obj_only) {
499 err = search_packidx(&is_packed, id, repo);
506 err = got_object_open_as_tag(&tag, repo, id);
510 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
511 got_object_tag_get_tagger_time(tag),
512 loose_obj_only, repo);
516 switch (got_object_tag_get_object_type(tag)) {
517 case GOT_OBJ_TYPE_COMMIT:
518 err = load_commit(NULL, idset,
519 got_object_tag_get_object_id(tag), repo,
520 loose_obj_only, cancel_cb, cancel_arg);
522 case GOT_OBJ_TYPE_TREE:
523 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
524 "", got_object_tag_get_tagger_time(tag),
525 loose_obj_only, repo, cancel_cb, cancel_arg);
532 got_object_tag_close(tag);
536 enum findtwixt_color {
541 static const int findtwixt_colors[] = {
547 static const struct got_error *
548 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
549 int color, struct got_repository *repo)
551 const struct got_error *err;
552 struct got_object_qid *qid;
554 err = got_object_qid_alloc(&qid, id);
558 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
559 qid->data = (void *)&findtwixt_colors[color];
563 static const struct got_error *
564 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
565 struct got_object_id *id, struct got_repository *repo,
566 got_cancel_cb cancel_cb, void *cancel_arg)
568 const struct got_error *err = NULL;
569 struct got_commit_object *commit;
570 const struct got_object_id_queue *parents;
571 struct got_object_id_queue ids;
572 struct got_object_qid *qid;
576 err = got_object_qid_alloc(&qid, id);
579 SIMPLEQ_INSERT_HEAD(&ids, qid, entry);
581 while (!SIMPLEQ_EMPTY(&ids)) {
583 err = (*cancel_cb)(cancel_arg);
588 qid = SIMPLEQ_FIRST(&ids);
589 SIMPLEQ_REMOVE_HEAD(&ids, entry);
591 if (got_object_idset_contains(drop, qid->id)) {
592 got_object_qid_free(qid);
596 err = got_object_idset_add(drop, qid->id, NULL);
598 got_object_qid_free(qid);
602 if (!got_object_idset_contains(keep, qid->id)) {
603 got_object_qid_free(qid);
607 err = got_object_open_as_commit(&commit, repo, qid->id);
608 got_object_qid_free(qid);
612 parents = got_object_commit_get_parent_ids(commit);
614 err = got_object_id_queue_copy(parents, &ids);
616 got_object_commit_close(commit);
620 got_object_commit_close(commit);
623 got_object_id_queue_free(&ids);
627 struct append_id_arg {
628 struct got_object_id **array;
632 static const struct got_error *
633 append_id(struct got_object_id *id, void *data, void *arg)
635 struct append_id_arg *a = arg;
637 a->array[a->idx] = got_object_id_dup(id);
638 if (a->array[a->idx] == NULL)
639 return got_error_from_errno("got_object_id_dup");
645 static const struct got_error *
646 findtwixt(struct got_object_id ***res, int *nres,
647 struct got_object_id **head, int nhead,
648 struct got_object_id **tail, int ntail,
649 struct got_repository *repo,
650 got_cancel_cb cancel_cb, void *cancel_arg)
652 const struct got_error *err = NULL;
653 struct got_object_id_queue ids;
654 struct got_object_idset *keep, *drop;
655 struct got_object_qid *qid;
656 int i, ncolor, nkeep, obj_type;
662 keep = got_object_idset_alloc();
664 return got_error_from_errno("got_object_idset_alloc");
666 drop = got_object_idset_alloc();
668 err = got_error_from_errno("got_object_idset_alloc");
672 for (i = 0; i < nhead; i++) {
673 struct got_object_id *id = head[i];
676 err = got_object_get_type(&obj_type, repo, id);
679 if (obj_type != GOT_OBJ_TYPE_COMMIT)
681 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
685 for (i = 0; i < ntail; i++) {
686 struct got_object_id *id = tail[i];
689 err = got_object_get_type(&obj_type, repo, id);
692 if (obj_type != GOT_OBJ_TYPE_COMMIT)
694 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
699 while (!SIMPLEQ_EMPTY(&ids)) {
701 qid = SIMPLEQ_FIRST(&ids);
702 qcolor = *((int *)qid->data);
704 if (got_object_idset_contains(drop, qid->id))
706 else if (got_object_idset_contains(keep, qid->id))
709 ncolor = COLOR_BLANK;
711 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
712 qcolor == COLOR_KEEP)) {
713 SIMPLEQ_REMOVE_HEAD(&ids, entry);
714 got_object_qid_free(qid);
718 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
719 err = drop_commit(keep, drop, qid->id, repo,
720 cancel_cb, cancel_arg);
723 } else if (ncolor == COLOR_BLANK) {
724 struct got_commit_object *commit;
725 struct got_object_id *id;
726 const struct got_object_id_queue *parents;
727 struct got_object_qid *pid;
729 id = got_object_id_dup(qid->id);
731 err = got_error_from_errno("got_object_id_dup");
734 if (qcolor == COLOR_KEEP)
735 err = got_object_idset_add(keep, id, NULL);
737 err = got_object_idset_add(drop, id, NULL);
743 err = got_object_open_as_commit(&commit, repo, id);
748 parents = got_object_commit_get_parent_ids(commit);
750 SIMPLEQ_FOREACH(pid, parents, entry) {
751 err = queue_commit_id(&ids, pid->id,
759 got_object_commit_close(commit);
762 /* should not happen */
763 err = got_error_fmt(GOT_ERR_NOT_IMPL,
764 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
768 SIMPLEQ_REMOVE_HEAD(&ids, entry);
769 got_object_qid_free(qid);
772 nkeep = got_object_idset_num_elements(keep);
774 struct append_id_arg arg;
775 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
776 if (arg.array == NULL) {
777 err = got_error_from_errno("calloc");
781 err = got_object_idset_for_each(keep, append_id, &arg);
790 got_object_idset_free(keep);
791 got_object_idset_free(drop);
792 got_object_id_queue_free(&ids);
796 static const struct got_error *
797 read_meta(struct got_pack_meta ***meta, int *nmeta,
798 struct got_object_id **theirs, int ntheirs,
799 struct got_object_id **ours, int nours, struct got_repository *repo,
800 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
801 got_cancel_cb cancel_cb, void *cancel_arg)
803 const struct got_error *err = NULL;
804 struct got_object_id **ids = NULL;
805 struct got_object_idset *idset;
806 int i, nobj = 0, obj_type;
807 struct got_pack_metavec v;
812 idset = got_object_idset_alloc();
814 return got_error_from_errno("got_object_idset_alloc");
818 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
819 if (v.meta == NULL) {
820 err = got_error_from_errno("reallocarray");
824 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
825 cancel_cb, cancel_arg);
826 if (err || nobj == 0)
829 for (i = 0; i < ntheirs; i++) {
830 struct got_object_id *id = theirs[i];
833 err = got_object_get_type(&obj_type, repo, id);
836 if (obj_type != GOT_OBJ_TYPE_COMMIT)
838 err = load_commit(NULL, idset, id, repo,
839 loose_obj_only, cancel_cb, cancel_arg);
843 err = progress_cb(progress_arg, 0L, nours,
850 for (i = 0; i < nobj; i++) {
851 err = load_commit(&v, idset, ids[i], 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 = ours[i];
867 err = got_object_get_type(&obj_type, repo, id);
870 if (obj_type != GOT_OBJ_TYPE_TAG)
872 err = load_tag(NULL, idset, id, repo,
873 loose_obj_only, cancel_cb, cancel_arg);
877 err = progress_cb(progress_arg, 0L, nours,
884 for (i = 0; i < nours; i++) {
885 struct got_object_id *id = ours[i];
888 err = got_object_get_type(&obj_type, repo, id);
891 if (obj_type != GOT_OBJ_TYPE_TAG)
893 err = load_tag(&v, idset, id, repo,
894 loose_obj_only, cancel_cb, cancel_arg);
898 err = progress_cb(progress_arg, 0L, nours,
906 for (i = 0; i < nobj; i++) {
910 got_object_idset_free(idset);
920 const struct got_error *
921 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
925 SHA1Update(ctx, buf, len);
926 n = fwrite(buf, 1, len, f);
928 return got_ferror(f, GOT_ERR_IO);
933 putbe32(char *b, uint32_t n)
942 write_order_cmp(const void *pa, const void *pb)
944 struct got_pack_meta *a, *b, *ahd, *bhd;
946 a = *(struct got_pack_meta **)pa;
947 b = *(struct got_pack_meta **)pb;
948 ahd = (a->head == NULL) ? a : a->head;
949 bhd = (b->head == NULL) ? b : b->head;
950 if (ahd->mtime != bhd->mtime)
951 return bhd->mtime - ahd->mtime;
953 return (uintptr_t)bhd - (uintptr_t)ahd;
954 if (a->nchain != b->nchain)
955 return a->nchain - b->nchain;
956 return a->mtime - b->mtime;
959 static const struct got_error *
960 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
966 hdr[0] = obj_type << 4;
969 for (i = 1; len != 0; i++){
971 return got_error(GOT_ERR_NO_SPACE);
972 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
973 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
974 len >>= GOT_DELTA_SIZE_SHIFT;
981 static const struct got_error *
982 append(char **p, int *len, int *sz, void *seg, int nseg)
986 if (*len + nseg >= *sz) {
987 while (*len + nseg >= *sz)
989 n = realloc(*p, *sz);
991 return got_error_from_errno("realloc");
994 memcpy(*p + *len, seg, nseg);
1000 static const struct got_error *
1001 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1002 off_t base_size, char **pp)
1004 const struct got_error *err = NULL;
1006 unsigned char buf[16], *bp;
1009 struct got_delta_instruction *d;
1018 return got_error_from_errno("malloc");
1020 /* base object size */
1021 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1022 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1023 for (i = 1; n > 0; i++) {
1024 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1025 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1026 n >>= GOT_DELTA_SIZE_SHIFT;
1028 err = append(&p, &len, &sz, buf, i);
1032 /* target object size */
1033 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1034 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1035 for (i = 1; n > 0; i++) {
1036 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1037 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1038 n >>= GOT_DELTA_SIZE_SHIFT;
1040 err = append(&p, &len, &sz, buf, i);
1043 for (j = 0; j < m->ndeltas; j++) {
1048 buf[0] = GOT_DELTA_BASE_COPY;
1049 for (i = 0; i < 4; i++) {
1050 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1059 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1060 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1061 for (i = 0; i < 3 && n > 0; i++) {
1062 buf[0] |= 1 << (i + 4);
1067 err = append(&p, &len, &sz, buf, bp - buf);
1073 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1074 return got_error_from_errno("fseeko");
1076 while (n != d->len) {
1077 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1078 err = append(&p, &len, &sz, buf, 1);
1081 r = fread(content, 1, buf[0], o->f);
1083 return got_ferror(o->f, GOT_ERR_IO);
1084 err = append(&p, &len, &sz, content, buf[0]);
1097 packoff(char *hdr, off_t off)
1102 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1103 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1104 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1105 GOT_DELTA_SIZE_MORE;
1110 hdr[j++] = rbuf[--i];
1114 static const struct got_error *
1115 genpack(uint8_t *pack_sha1, FILE *packfile,
1116 struct got_pack_meta **meta, int nmeta, int nours,
1117 int use_offset_deltas, struct got_repository *repo,
1118 got_pack_progress_cb progress_cb, void *progress_arg,
1119 got_cancel_cb cancel_cb, void *cancel_arg)
1121 const struct got_error *err = NULL;
1124 struct got_pack_meta *m;
1125 struct got_raw_object *raw;
1126 char *p = NULL, buf[32];
1128 struct got_deflate_checksum csum;
1129 off_t packfile_size = 0;
1132 csum.output_sha1 = &ctx;
1133 csum.output_crc = NULL;
1135 err = hwrite(packfile, "PACK", 4, &ctx);
1138 putbe32(buf, GOT_PACKFILE_VERSION);
1139 err = hwrite(packfile, buf, 4, &ctx);
1142 putbe32(buf, nmeta);
1143 err = hwrite(packfile, buf, 4, &ctx);
1146 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1147 for (i = 0; i < nmeta; i++) {
1149 err = progress_cb(progress_arg, packfile_size, nours,
1155 m->off = ftello(packfile);
1156 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1159 if (m->deltas == NULL) {
1160 err = packhdr(&nh, buf, sizeof(buf),
1161 m->obj_type, raw->size);
1164 err = hwrite(packfile, buf, nh, &ctx);
1167 packfile_size += nh;
1168 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1169 err = got_error_from_errno("fseeko");
1172 err = got_deflate_to_file(&outlen, raw->f, packfile,
1176 packfile_size += outlen;
1179 struct got_raw_object *base_raw;
1180 err = got_object_raw_open(&base_raw, repo,
1181 &m->prev->id, 8192);
1184 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1187 got_object_raw_close(base_raw);
1188 if (use_offset_deltas && m->prev->off != 0) {
1189 err = packhdr(&nh, buf, sizeof(buf),
1190 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1193 nh += packoff(buf + nh,
1194 m->off - m->prev->off);
1195 err = hwrite(packfile, buf, nh, &ctx);
1198 packfile_size += nh;
1200 err = packhdr(&nh, buf, sizeof(buf),
1201 GOT_OBJ_TYPE_REF_DELTA, nd);
1202 err = hwrite(packfile, buf, nh, &ctx);
1205 packfile_size += nh;
1206 err = hwrite(packfile, m->prev->id.sha1,
1207 sizeof(m->prev->id.sha1), &ctx);
1208 packfile_size += sizeof(m->prev->id.sha1);
1212 /* XXX need got_deflate_from_mem() */
1213 delta_file = fmemopen(p, nd, "r");
1214 if (delta_file == NULL) {
1215 err = got_error_from_errno("fmemopen");
1218 err = got_deflate_to_file(&outlen, delta_file,
1223 packfile_size += outlen;
1227 got_object_raw_close(raw);
1230 SHA1Final(pack_sha1, &ctx);
1231 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1232 if (n != SHA1_DIGEST_LENGTH)
1233 err = got_ferror(packfile, GOT_ERR_IO);
1234 packfile_size += SHA1_DIGEST_LENGTH;
1235 packfile_size += 16; /* pack file header */
1236 err = progress_cb(progress_arg, packfile_size, nours,
1237 nmeta, nmeta, nmeta);
1245 const struct got_error *
1246 got_pack_create(uint8_t *packsha1, FILE *packfile,
1247 struct got_object_id **theirs, int ntheirs,
1248 struct got_object_id **ours, int nours,
1249 struct got_repository *repo, int loose_obj_only,
1250 got_pack_progress_cb progress_cb, void *progress_arg,
1251 got_cancel_cb cancel_cb, void *cancel_arg)
1253 const struct got_error *err;
1254 struct got_pack_meta **meta;
1257 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1258 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1263 err = got_error(GOT_ERR_CANNOT_PACK);
1267 err = pick_deltas(meta, nmeta, nours, repo,
1268 progress_cb, progress_arg, cancel_cb, cancel_arg);
1272 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1273 progress_cb, progress_arg, cancel_cb, cancel_arg);
1277 free_nmeta(meta, nmeta);