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_submodule(e) ||
374 got_object_idset_contains(idset, id))
377 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
378 got_tree_entry_get_name(e)) == -1) {
379 err = got_error_from_errno("asprintf");
384 struct got_object_qid *qid;
385 err = got_object_qid_alloc(&qid, id);
388 STAILQ_INSERT_TAIL(ids, qid, entry);
389 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
390 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
391 mtime, loose_obj_only, repo);
399 got_object_tree_close(tree);
404 static const struct got_error *
405 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
406 struct got_object_id *tree_id, const char *dpath, time_t mtime,
407 int loose_obj_only, struct got_repository *repo,
408 got_cancel_cb cancel_cb, void *cancel_arg)
410 const struct got_error *err = NULL;
411 struct got_object_id_queue tree_ids;
412 struct got_object_qid *qid;
414 if (got_object_idset_contains(idset, tree_id))
417 err = got_object_qid_alloc(&qid, tree_id);
421 STAILQ_INIT(&tree_ids);
422 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
424 while (!STAILQ_EMPTY(&tree_ids)) {
426 err = (*cancel_cb)(cancel_arg);
431 qid = STAILQ_FIRST(&tree_ids);
432 STAILQ_REMOVE_HEAD(&tree_ids, entry);
434 if (got_object_idset_contains(idset, qid->id)) {
435 got_object_qid_free(qid);
439 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
440 mtime, loose_obj_only, repo);
442 got_object_qid_free(qid);
446 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
447 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
448 got_object_qid_free(qid);
453 got_object_id_queue_free(&tree_ids);
457 static const struct got_error *
458 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
459 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
460 got_cancel_cb cancel_cb, void *cancel_arg)
462 const struct got_error *err;
463 struct got_commit_object *commit;
465 if (got_object_idset_contains(idset, id))
468 if (loose_obj_only) {
470 err = search_packidx(&is_packed, id, repo);
477 err = got_object_open_as_commit(&commit, repo, id);
481 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
482 got_object_commit_get_committer_time(commit),
483 loose_obj_only, repo);
487 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
488 "", got_object_commit_get_committer_time(commit),
489 loose_obj_only, repo, cancel_cb, cancel_arg);
491 got_object_commit_close(commit);
495 static const struct got_error *
496 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
497 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
498 got_cancel_cb cancel_cb, void *cancel_arg)
500 const struct got_error *err;
501 struct got_tag_object *tag = NULL;
503 if (got_object_idset_contains(idset, id))
506 if (loose_obj_only) {
508 err = search_packidx(&is_packed, id, repo);
515 err = got_object_open_as_tag(&tag, repo, id);
519 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
520 got_object_tag_get_tagger_time(tag),
521 loose_obj_only, repo);
525 switch (got_object_tag_get_object_type(tag)) {
526 case GOT_OBJ_TYPE_COMMIT:
527 err = load_commit(v, idset,
528 got_object_tag_get_object_id(tag), repo,
529 loose_obj_only, cancel_cb, cancel_arg);
531 case GOT_OBJ_TYPE_TREE:
532 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
533 "", got_object_tag_get_tagger_time(tag),
534 loose_obj_only, repo, cancel_cb, cancel_arg);
541 got_object_tag_close(tag);
545 enum findtwixt_color {
550 static const int findtwixt_colors[] = {
556 static const struct got_error *
557 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
558 int color, struct got_repository *repo)
560 const struct got_error *err;
561 struct got_object_qid *qid;
563 err = got_object_qid_alloc(&qid, id);
567 STAILQ_INSERT_TAIL(ids, qid, entry);
568 qid->data = (void *)&findtwixt_colors[color];
572 static const struct got_error *
573 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
574 struct got_object_id *id, struct got_repository *repo,
575 got_cancel_cb cancel_cb, void *cancel_arg)
577 const struct got_error *err = NULL;
578 struct got_commit_object *commit;
579 const struct got_object_id_queue *parents;
580 struct got_object_id_queue ids;
581 struct got_object_qid *qid;
585 err = got_object_qid_alloc(&qid, id);
588 STAILQ_INSERT_HEAD(&ids, qid, entry);
590 while (!STAILQ_EMPTY(&ids)) {
592 err = (*cancel_cb)(cancel_arg);
597 qid = STAILQ_FIRST(&ids);
598 STAILQ_REMOVE_HEAD(&ids, entry);
600 if (got_object_idset_contains(drop, qid->id)) {
601 got_object_qid_free(qid);
605 err = got_object_idset_add(drop, qid->id,
606 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
608 got_object_qid_free(qid);
612 if (!got_object_idset_contains(keep, qid->id)) {
613 got_object_qid_free(qid);
617 err = got_object_open_as_commit(&commit, repo, qid->id);
618 got_object_qid_free(qid);
622 parents = got_object_commit_get_parent_ids(commit);
624 err = got_object_id_queue_copy(parents, &ids);
626 got_object_commit_close(commit);
630 got_object_commit_close(commit);
633 got_object_id_queue_free(&ids);
637 struct append_id_arg {
638 struct got_object_id **array;
642 static const struct got_error *
643 append_id(struct got_object_id *id, void *data, void *arg)
645 struct append_id_arg *a = arg;
647 a->array[a->idx] = got_object_id_dup(id);
648 if (a->array[a->idx] == NULL)
649 return got_error_from_errno("got_object_id_dup");
655 static const struct got_error *
656 findtwixt(struct got_object_id ***res, int *nres,
657 struct got_object_id **head, int nhead,
658 struct got_object_id **tail, int ntail,
659 struct got_repository *repo,
660 got_cancel_cb cancel_cb, void *cancel_arg)
662 const struct got_error *err = NULL;
663 struct got_object_id_queue ids;
664 struct got_object_idset *keep, *drop;
665 struct got_object_qid *qid;
666 int i, ncolor, nkeep, obj_type;
672 keep = got_object_idset_alloc();
674 return got_error_from_errno("got_object_idset_alloc");
676 drop = got_object_idset_alloc();
678 err = got_error_from_errno("got_object_idset_alloc");
682 for (i = 0; i < nhead; i++) {
683 struct got_object_id *id = head[i];
686 err = got_object_get_type(&obj_type, repo, id);
689 if (obj_type != GOT_OBJ_TYPE_COMMIT)
691 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
695 for (i = 0; i < ntail; i++) {
696 struct got_object_id *id = tail[i];
699 err = got_object_get_type(&obj_type, repo, id);
702 if (obj_type != GOT_OBJ_TYPE_COMMIT)
704 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
709 while (!STAILQ_EMPTY(&ids)) {
711 qid = STAILQ_FIRST(&ids);
712 qcolor = *((int *)qid->data);
714 if (got_object_idset_contains(drop, qid->id))
716 else if (got_object_idset_contains(keep, qid->id))
719 ncolor = COLOR_BLANK;
721 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
722 qcolor == COLOR_KEEP)) {
723 STAILQ_REMOVE_HEAD(&ids, entry);
724 got_object_qid_free(qid);
728 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
729 err = drop_commit(keep, drop, qid->id, repo,
730 cancel_cb, cancel_arg);
733 } else if (ncolor == COLOR_BLANK) {
734 struct got_commit_object *commit;
735 struct got_object_id *id;
736 const struct got_object_id_queue *parents;
737 struct got_object_qid *pid;
739 id = got_object_id_dup(qid->id);
741 err = got_error_from_errno("got_object_id_dup");
744 if (qcolor == COLOR_KEEP)
745 err = got_object_idset_add(keep, id,
746 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
748 err = got_object_idset_add(drop, id,
749 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
755 err = got_object_open_as_commit(&commit, repo, id);
760 parents = got_object_commit_get_parent_ids(commit);
762 STAILQ_FOREACH(pid, parents, entry) {
763 err = queue_commit_id(&ids, pid->id,
771 got_object_commit_close(commit);
774 /* should not happen */
775 err = got_error_fmt(GOT_ERR_NOT_IMPL,
776 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
780 STAILQ_REMOVE_HEAD(&ids, entry);
781 got_object_qid_free(qid);
784 nkeep = got_object_idset_num_elements(keep);
786 struct append_id_arg arg;
787 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
788 if (arg.array == NULL) {
789 err = got_error_from_errno("calloc");
793 err = got_object_idset_for_each(keep, append_id, &arg);
802 got_object_idset_free(keep);
803 got_object_idset_free(drop);
804 got_object_id_queue_free(&ids);
808 static const struct got_error *
809 read_meta(struct got_pack_meta ***meta, int *nmeta,
810 struct got_object_id **theirs, int ntheirs,
811 struct got_object_id **ours, int nours, struct got_repository *repo,
812 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
813 got_cancel_cb cancel_cb, void *cancel_arg)
815 const struct got_error *err = NULL;
816 struct got_object_id **ids = NULL;
817 struct got_object_idset *idset;
818 int i, nobj = 0, obj_type;
819 struct got_pack_metavec v;
824 idset = got_object_idset_alloc();
826 return got_error_from_errno("got_object_idset_alloc");
830 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
831 if (v.meta == NULL) {
832 err = got_error_from_errno("reallocarray");
836 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
837 cancel_cb, cancel_arg);
838 if (err || nobj == 0)
841 for (i = 0; i < ntheirs; i++) {
842 struct got_object_id *id = theirs[i];
845 err = got_object_get_type(&obj_type, repo, id);
848 if (obj_type != GOT_OBJ_TYPE_COMMIT)
850 err = load_commit(NULL, idset, id, repo,
851 loose_obj_only, cancel_cb, cancel_arg);
855 err = progress_cb(progress_arg, 0L, nours,
862 for (i = 0; i < ntheirs; i++) {
863 struct got_object_id *id = theirs[i];
867 cached_type = got_object_idset_get(idset, id);
868 if (cached_type == NULL) {
869 err = got_object_get_type(&obj_type, repo, id);
873 obj_type = *cached_type;
874 if (obj_type != GOT_OBJ_TYPE_TAG)
876 err = load_tag(NULL, idset, id, repo,
877 loose_obj_only, cancel_cb, cancel_arg);
881 err = progress_cb(progress_arg, 0L, nours,
888 for (i = 0; i < nobj; i++) {
889 err = load_commit(&v, idset, ids[i], repo,
890 loose_obj_only, cancel_cb, cancel_arg);
894 err = progress_cb(progress_arg, 0L, nours,
901 for (i = 0; i < nours; i++) {
902 struct got_object_id *id = ours[i];
906 cached_type = got_object_idset_get(idset, id);
907 if (cached_type == NULL) {
908 err = got_object_get_type(&obj_type, repo, id);
912 obj_type = *cached_type;
913 if (obj_type != GOT_OBJ_TYPE_TAG)
915 err = load_tag(&v, idset, id, repo,
916 loose_obj_only, cancel_cb, cancel_arg);
920 err = progress_cb(progress_arg, 0L, nours,
928 for (i = 0; i < nobj; i++) {
932 got_object_idset_free(idset);
942 const struct got_error *
943 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
947 SHA1Update(ctx, buf, len);
948 n = fwrite(buf, 1, len, f);
950 return got_ferror(f, GOT_ERR_IO);
955 putbe32(char *b, uint32_t n)
964 write_order_cmp(const void *pa, const void *pb)
966 struct got_pack_meta *a, *b, *ahd, *bhd;
968 a = *(struct got_pack_meta **)pa;
969 b = *(struct got_pack_meta **)pb;
970 ahd = (a->head == NULL) ? a : a->head;
971 bhd = (b->head == NULL) ? b : b->head;
972 if (ahd->mtime != bhd->mtime)
973 return bhd->mtime - ahd->mtime;
975 return (uintptr_t)bhd - (uintptr_t)ahd;
976 if (a->nchain != b->nchain)
977 return a->nchain - b->nchain;
978 return a->mtime - b->mtime;
981 static const struct got_error *
982 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
988 hdr[0] = obj_type << 4;
991 for (i = 1; len != 0; i++){
993 return got_error(GOT_ERR_NO_SPACE);
994 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
995 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
996 len >>= GOT_DELTA_SIZE_SHIFT;
1003 static const struct got_error *
1004 append(char **p, int *len, int *sz, void *seg, int nseg)
1008 if (*len + nseg >= *sz) {
1009 while (*len + nseg >= *sz)
1011 n = realloc(*p, *sz);
1013 return got_error_from_errno("realloc");
1016 memcpy(*p + *len, seg, nseg);
1022 static const struct got_error *
1023 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1024 off_t base_size, char **pp)
1026 const struct got_error *err = NULL;
1028 unsigned char buf[16], *bp;
1031 struct got_delta_instruction *d;
1040 return got_error_from_errno("malloc");
1042 /* base object size */
1043 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1044 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1045 for (i = 1; n > 0; i++) {
1046 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1047 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1048 n >>= GOT_DELTA_SIZE_SHIFT;
1050 err = append(&p, &len, &sz, buf, i);
1054 /* target object size */
1055 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1056 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1057 for (i = 1; n > 0; i++) {
1058 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1059 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1060 n >>= GOT_DELTA_SIZE_SHIFT;
1062 err = append(&p, &len, &sz, buf, i);
1065 for (j = 0; j < m->ndeltas; j++) {
1070 buf[0] = GOT_DELTA_BASE_COPY;
1071 for (i = 0; i < 4; i++) {
1072 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1081 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1082 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1083 for (i = 0; i < 3 && n > 0; i++) {
1084 buf[0] |= 1 << (i + 4);
1089 err = append(&p, &len, &sz, buf, bp - buf);
1095 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1096 return got_error_from_errno("fseeko");
1098 while (n != d->len) {
1099 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1100 err = append(&p, &len, &sz, buf, 1);
1103 r = fread(content, 1, buf[0], o->f);
1105 return got_ferror(o->f, GOT_ERR_IO);
1106 err = append(&p, &len, &sz, content, buf[0]);
1119 packoff(char *hdr, off_t off)
1124 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1125 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1126 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1127 GOT_DELTA_SIZE_MORE;
1132 hdr[j++] = rbuf[--i];
1136 static const struct got_error *
1137 genpack(uint8_t *pack_sha1, FILE *packfile,
1138 struct got_pack_meta **meta, int nmeta, int nours,
1139 int use_offset_deltas, struct got_repository *repo,
1140 got_pack_progress_cb progress_cb, void *progress_arg,
1141 got_cancel_cb cancel_cb, void *cancel_arg)
1143 const struct got_error *err = NULL;
1146 struct got_pack_meta *m;
1147 struct got_raw_object *raw;
1148 char *p = NULL, buf[32];
1150 struct got_deflate_checksum csum;
1151 off_t packfile_size = 0;
1154 csum.output_sha1 = &ctx;
1155 csum.output_crc = NULL;
1157 err = hwrite(packfile, "PACK", 4, &ctx);
1160 putbe32(buf, GOT_PACKFILE_VERSION);
1161 err = hwrite(packfile, buf, 4, &ctx);
1164 putbe32(buf, nmeta);
1165 err = hwrite(packfile, buf, 4, &ctx);
1168 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1169 for (i = 0; i < nmeta; i++) {
1171 err = progress_cb(progress_arg, packfile_size, nours,
1177 m->off = ftello(packfile);
1178 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1181 if (m->deltas == NULL) {
1182 err = packhdr(&nh, buf, sizeof(buf),
1183 m->obj_type, raw->size);
1186 err = hwrite(packfile, buf, nh, &ctx);
1189 packfile_size += nh;
1190 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1191 err = got_error_from_errno("fseeko");
1194 err = got_deflate_to_file(&outlen, raw->f, packfile,
1198 packfile_size += outlen;
1201 struct got_raw_object *base_raw;
1202 err = got_object_raw_open(&base_raw, repo,
1203 &m->prev->id, 8192);
1206 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1209 got_object_raw_close(base_raw);
1210 if (use_offset_deltas && m->prev->off != 0) {
1211 err = packhdr(&nh, buf, sizeof(buf),
1212 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1215 nh += packoff(buf + nh,
1216 m->off - m->prev->off);
1217 err = hwrite(packfile, buf, nh, &ctx);
1220 packfile_size += nh;
1222 err = packhdr(&nh, buf, sizeof(buf),
1223 GOT_OBJ_TYPE_REF_DELTA, nd);
1224 err = hwrite(packfile, buf, nh, &ctx);
1227 packfile_size += nh;
1228 err = hwrite(packfile, m->prev->id.sha1,
1229 sizeof(m->prev->id.sha1), &ctx);
1230 packfile_size += sizeof(m->prev->id.sha1);
1234 /* XXX need got_deflate_from_mem() */
1235 delta_file = fmemopen(p, nd, "r");
1236 if (delta_file == NULL) {
1237 err = got_error_from_errno("fmemopen");
1240 err = got_deflate_to_file(&outlen, delta_file,
1245 packfile_size += outlen;
1249 got_object_raw_close(raw);
1252 SHA1Final(pack_sha1, &ctx);
1253 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1254 if (n != SHA1_DIGEST_LENGTH)
1255 err = got_ferror(packfile, GOT_ERR_IO);
1256 packfile_size += SHA1_DIGEST_LENGTH;
1257 packfile_size += sizeof(struct got_packfile_hdr);
1258 err = progress_cb(progress_arg, packfile_size, nours,
1259 nmeta, nmeta, nmeta);
1267 const struct got_error *
1268 got_pack_create(uint8_t *packsha1, FILE *packfile,
1269 struct got_object_id **theirs, int ntheirs,
1270 struct got_object_id **ours, int nours,
1271 struct got_repository *repo, int loose_obj_only, int allow_empty,
1272 got_pack_progress_cb progress_cb, void *progress_arg,
1273 got_cancel_cb cancel_cb, void *cancel_arg)
1275 const struct got_error *err;
1276 struct got_pack_meta **meta;
1279 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1280 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1284 if (nmeta == 0 && !allow_empty) {
1285 err = got_error(GOT_ERR_CANNOT_PACK);
1289 err = pick_deltas(meta, nmeta, nours, repo,
1290 progress_cb, progress_arg, cancel_cb, cancel_arg);
1295 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1296 progress_cb, progress_arg, cancel_cb, cancel_arg);
1300 free_nmeta(meta, nmeta);