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"
39 #include "got_opentemp.h"
41 #include "got_lib_deltify.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_idset.h"
45 #include "got_lib_object_cache.h"
46 #include "got_lib_deflate.h"
47 #include "got_lib_pack.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_repository.h"
52 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
55 struct got_pack_meta {
56 struct got_object_id id;
62 /* The best delta we picked */
63 struct got_pack_meta *head;
64 struct got_pack_meta *prev;
65 struct got_delta_instruction *deltas;
69 /* Only used for delta window */
70 struct got_delta_table *dtab;
72 /* Only used for writing offset deltas */
76 struct got_pack_metavec {
77 struct got_pack_meta **meta;
82 static const struct got_error *
83 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
84 const char *path, int obj_type, time_t mtime)
86 const struct got_error *err = NULL;
87 struct got_pack_meta *m;
91 m = calloc(1, sizeof(*m));
93 return got_error_from_errno("calloc");
95 memcpy(&m->id, id, sizeof(m->id));
97 m->path = strdup(path);
98 if (m->path == NULL) {
99 err = got_error_from_errno("strdup");
104 m->obj_type = obj_type;
111 clear_meta(struct got_pack_meta *meta)
122 free_nmeta(struct got_pack_meta **meta, int nmeta)
126 for (i = 0; i < nmeta; i++)
132 delta_order_cmp(const void *pa, const void *pb)
134 struct got_pack_meta *a, *b;
137 a = *(struct got_pack_meta **)pa;
138 b = *(struct got_pack_meta **)pb;
140 if (a->obj_type != b->obj_type)
141 return a->obj_type - b->obj_type;
142 cmp = strcmp(a->path, b->path);
145 if (a->mtime != b->mtime)
146 return a->mtime - b->mtime;
147 return got_object_id_cmp(&a->id, &b->id);
151 delta_size(struct got_delta_instruction *deltas, int ndeltas)
154 for (i = 0; i < ndeltas; i++) {
156 size += GOT_DELTA_SIZE_SHIFT;
158 size += deltas[i].len + 1;
164 static const struct got_error *
165 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
166 struct got_repository *repo,
167 got_pack_progress_cb progress_cb, void *progress_arg,
168 got_cancel_cb cancel_cb, void *cancel_arg)
170 const struct got_error *err = NULL;
171 struct got_pack_meta *m = NULL, *base = NULL;
172 struct got_raw_object *raw = NULL, *base_raw = NULL;
173 struct got_delta_instruction *deltas;
174 int i, j, size, ndeltas, best;
175 const int max_base_candidates = 10;
178 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
179 for (i = 0; i < nmeta; i++) {
181 err = (*cancel_cb)(cancel_arg);
186 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
194 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
195 m->obj_type == GOT_OBJ_TYPE_TAG)
198 err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
203 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
204 raw->size + raw->hdrlen);
208 if (i > max_base_candidates) {
209 struct got_pack_meta *n = NULL;
210 n = meta[i - (max_base_candidates + 1)];
211 got_deltify_free(n->dtab);
216 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
218 err = (*cancel_cb)(cancel_arg);
223 /* long chains make unpacking slow, avoid such bases */
224 if (base->nchain >= 128 ||
225 base->obj_type != m->obj_type)
228 err = got_object_raw_open(&base_raw, &outfd, repo,
232 err = got_deltify(&deltas, &ndeltas,
233 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
234 base->dtab, base_raw->f, base_raw->hdrlen,
235 base_raw->size + base_raw->hdrlen);
236 got_object_raw_close(base_raw);
241 size = delta_size(deltas, ndeltas);
242 if (size + 32 < best){
244 * if we already picked a best delta,
250 m->ndeltas = ndeltas;
251 m->nchain = base->nchain + 1;
253 m->head = base->head;
263 got_object_raw_close(raw);
267 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
268 got_deltify_free(meta[i]->dtab);
269 meta[i]->dtab = NULL;
272 got_object_raw_close(raw);
274 got_object_raw_close(base_raw);
275 if (outfd != -1 && close(outfd) == -1 && err == NULL)
276 err = got_error_from_errno("close");
280 static const struct got_error *
281 search_packidx(int *found, struct got_object_id *id,
282 struct got_repository *repo)
284 const struct got_error *err = NULL;
285 struct got_packidx *packidx = NULL;
290 err = got_repo_search_packidx(&packidx, &idx, repo, id);
292 *found = 1; /* object is already packed */
293 else if (err->code == GOT_ERR_NO_OBJ)
298 static const int obj_types[] = {
304 GOT_OBJ_TYPE_OFFSET_DELTA,
305 GOT_OBJ_TYPE_REF_DELTA
308 static const struct got_error *
309 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
310 struct got_object_id *id, const char *path, int obj_type,
311 time_t mtime, int loose_obj_only, struct got_repository *repo)
313 const struct got_error *err;
314 struct got_pack_meta *m;
316 if (loose_obj_only) {
318 err = search_packidx(&is_packed, id, repo);
325 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
332 err = alloc_meta(&m, id, path, obj_type, mtime);
336 if (v->nmeta == v->metasz){
337 size_t newsize = 2 * v->metasz;
338 struct got_pack_meta **new;
339 new = reallocarray(v->meta, newsize, sizeof(*new));
341 err = got_error_from_errno("reallocarray");
352 v->meta[v->nmeta++] = m;
357 static const struct got_error *
358 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
359 struct got_object_idset *idset, struct got_object_id *tree_id,
360 const char *dpath, time_t mtime, struct got_repository *repo,
361 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
363 const struct got_error *err;
364 struct got_tree_object *tree;
368 err = got_object_open_as_tree(&tree, repo, tree_id);
372 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
373 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
374 struct got_object_id *id = got_tree_entry_get_id(e);
375 mode_t mode = got_tree_entry_get_mode(e);
378 err = (*cancel_cb)(cancel_arg);
383 if (got_object_tree_entry_is_submodule(e) ||
384 got_object_idset_contains(idset, id))
387 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
388 got_tree_entry_get_name(e)) == -1) {
389 err = got_error_from_errno("asprintf");
394 struct got_object_qid *qid;
395 err = got_object_qid_alloc(&qid, id);
398 STAILQ_INSERT_TAIL(ids, qid, entry);
399 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
400 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
401 mtime, loose_obj_only, repo);
409 got_object_tree_close(tree);
414 static const struct got_error *
415 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
416 struct got_object_id *tree_id, const char *dpath, time_t mtime,
417 int loose_obj_only, struct got_repository *repo,
418 got_cancel_cb cancel_cb, void *cancel_arg)
420 const struct got_error *err = NULL;
421 struct got_object_id_queue tree_ids;
422 struct got_object_qid *qid;
424 if (got_object_idset_contains(idset, tree_id))
427 err = got_object_qid_alloc(&qid, tree_id);
431 STAILQ_INIT(&tree_ids);
432 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
434 while (!STAILQ_EMPTY(&tree_ids)) {
436 err = (*cancel_cb)(cancel_arg);
441 qid = STAILQ_FIRST(&tree_ids);
442 STAILQ_REMOVE_HEAD(&tree_ids, entry);
444 if (got_object_idset_contains(idset, qid->id)) {
445 got_object_qid_free(qid);
449 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
450 mtime, loose_obj_only, repo);
452 got_object_qid_free(qid);
456 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
457 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
458 got_object_qid_free(qid);
463 got_object_id_queue_free(&tree_ids);
467 static const struct got_error *
468 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
469 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
470 got_cancel_cb cancel_cb, void *cancel_arg)
472 const struct got_error *err;
473 struct got_commit_object *commit;
475 if (got_object_idset_contains(idset, id))
478 if (loose_obj_only) {
480 err = search_packidx(&is_packed, id, repo);
487 err = got_object_open_as_commit(&commit, repo, id);
491 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
492 got_object_commit_get_committer_time(commit),
493 loose_obj_only, repo);
497 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
498 "", got_object_commit_get_committer_time(commit),
499 loose_obj_only, repo, cancel_cb, cancel_arg);
501 got_object_commit_close(commit);
505 static const struct got_error *
506 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
507 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
508 got_cancel_cb cancel_cb, void *cancel_arg)
510 const struct got_error *err;
511 struct got_tag_object *tag = NULL;
513 if (got_object_idset_contains(idset, id))
516 if (loose_obj_only) {
518 err = search_packidx(&is_packed, id, repo);
525 err = got_object_open_as_tag(&tag, repo, id);
529 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
530 got_object_tag_get_tagger_time(tag),
531 loose_obj_only, repo);
535 switch (got_object_tag_get_object_type(tag)) {
536 case GOT_OBJ_TYPE_COMMIT:
537 err = load_commit(v, idset,
538 got_object_tag_get_object_id(tag), repo,
539 loose_obj_only, cancel_cb, cancel_arg);
541 case GOT_OBJ_TYPE_TREE:
542 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
543 "", got_object_tag_get_tagger_time(tag),
544 loose_obj_only, repo, cancel_cb, cancel_arg);
551 got_object_tag_close(tag);
555 enum findtwixt_color {
560 static const int findtwixt_colors[] = {
566 static const struct got_error *
567 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
568 int color, struct got_repository *repo)
570 const struct got_error *err;
571 struct got_object_qid *qid;
573 err = got_object_qid_alloc(&qid, id);
577 STAILQ_INSERT_TAIL(ids, qid, entry);
578 qid->data = (void *)&findtwixt_colors[color];
582 static const struct got_error *
583 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
584 struct got_object_id *id, struct got_repository *repo,
585 got_cancel_cb cancel_cb, void *cancel_arg)
587 const struct got_error *err = NULL;
588 struct got_commit_object *commit;
589 const struct got_object_id_queue *parents;
590 struct got_object_id_queue ids;
591 struct got_object_qid *qid;
595 err = got_object_qid_alloc(&qid, id);
598 STAILQ_INSERT_HEAD(&ids, qid, entry);
600 while (!STAILQ_EMPTY(&ids)) {
602 err = (*cancel_cb)(cancel_arg);
607 qid = STAILQ_FIRST(&ids);
608 STAILQ_REMOVE_HEAD(&ids, entry);
610 if (got_object_idset_contains(drop, qid->id)) {
611 got_object_qid_free(qid);
615 err = got_object_idset_add(drop, qid->id,
616 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
618 got_object_qid_free(qid);
622 if (!got_object_idset_contains(keep, qid->id)) {
623 got_object_qid_free(qid);
627 err = got_object_open_as_commit(&commit, repo, qid->id);
628 got_object_qid_free(qid);
632 parents = got_object_commit_get_parent_ids(commit);
634 err = got_object_id_queue_copy(parents, &ids);
636 got_object_commit_close(commit);
640 got_object_commit_close(commit);
643 got_object_id_queue_free(&ids);
647 struct append_id_arg {
648 struct got_object_id **array;
652 static const struct got_error *
653 append_id(struct got_object_id *id, void *data, void *arg)
655 struct append_id_arg *a = arg;
657 a->array[a->idx] = got_object_id_dup(id);
658 if (a->array[a->idx] == NULL)
659 return got_error_from_errno("got_object_id_dup");
665 static const struct got_error *
666 findtwixt(struct got_object_id ***res, int *nres,
667 struct got_object_id **head, int nhead,
668 struct got_object_id **tail, int ntail,
669 struct got_repository *repo,
670 got_cancel_cb cancel_cb, void *cancel_arg)
672 const struct got_error *err = NULL;
673 struct got_object_id_queue ids;
674 struct got_object_idset *keep, *drop;
675 struct got_object_qid *qid;
676 int i, ncolor, nkeep, obj_type;
682 keep = got_object_idset_alloc();
684 return got_error_from_errno("got_object_idset_alloc");
686 drop = got_object_idset_alloc();
688 err = got_error_from_errno("got_object_idset_alloc");
692 for (i = 0; i < nhead; i++) {
693 struct got_object_id *id = head[i];
696 err = got_object_get_type(&obj_type, repo, id);
699 if (obj_type != GOT_OBJ_TYPE_COMMIT)
701 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
705 for (i = 0; i < ntail; i++) {
706 struct got_object_id *id = tail[i];
709 err = got_object_get_type(&obj_type, repo, id);
712 if (obj_type != GOT_OBJ_TYPE_COMMIT)
714 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
719 while (!STAILQ_EMPTY(&ids)) {
721 qid = STAILQ_FIRST(&ids);
722 qcolor = *((int *)qid->data);
724 if (got_object_idset_contains(drop, qid->id))
726 else if (got_object_idset_contains(keep, qid->id))
729 ncolor = COLOR_BLANK;
731 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
732 qcolor == COLOR_KEEP)) {
733 STAILQ_REMOVE_HEAD(&ids, entry);
734 got_object_qid_free(qid);
738 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
739 err = drop_commit(keep, drop, qid->id, repo,
740 cancel_cb, cancel_arg);
743 } else if (ncolor == COLOR_BLANK) {
744 struct got_commit_object *commit;
745 struct got_object_id *id;
746 const struct got_object_id_queue *parents;
747 struct got_object_qid *pid;
749 id = got_object_id_dup(qid->id);
751 err = got_error_from_errno("got_object_id_dup");
754 if (qcolor == COLOR_KEEP)
755 err = got_object_idset_add(keep, id,
756 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
758 err = got_object_idset_add(drop, id,
759 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
765 err = got_object_open_as_commit(&commit, repo, id);
770 parents = got_object_commit_get_parent_ids(commit);
772 STAILQ_FOREACH(pid, parents, entry) {
773 err = queue_commit_id(&ids, pid->id,
781 got_object_commit_close(commit);
784 /* should not happen */
785 err = got_error_fmt(GOT_ERR_NOT_IMPL,
786 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
790 STAILQ_REMOVE_HEAD(&ids, entry);
791 got_object_qid_free(qid);
794 nkeep = got_object_idset_num_elements(keep);
796 struct append_id_arg arg;
797 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
798 if (arg.array == NULL) {
799 err = got_error_from_errno("calloc");
803 err = got_object_idset_for_each(keep, append_id, &arg);
812 got_object_idset_free(keep);
813 got_object_idset_free(drop);
814 got_object_id_queue_free(&ids);
818 static const struct got_error *
819 read_meta(struct got_pack_meta ***meta, int *nmeta,
820 struct got_object_id **theirs, int ntheirs,
821 struct got_object_id **ours, int nours, struct got_repository *repo,
822 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
823 got_cancel_cb cancel_cb, void *cancel_arg)
825 const struct got_error *err = NULL;
826 struct got_object_id **ids = NULL;
827 struct got_object_idset *idset;
828 int i, nobj = 0, obj_type;
829 struct got_pack_metavec v;
834 idset = got_object_idset_alloc();
836 return got_error_from_errno("got_object_idset_alloc");
840 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
841 if (v.meta == NULL) {
842 err = got_error_from_errno("calloc");
846 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
847 cancel_cb, cancel_arg);
848 if (err || nobj == 0)
851 for (i = 0; i < ntheirs; i++) {
852 struct got_object_id *id = theirs[i];
855 err = got_object_get_type(&obj_type, repo, id);
858 if (obj_type != GOT_OBJ_TYPE_COMMIT)
860 err = load_commit(NULL, idset, id, repo,
861 loose_obj_only, cancel_cb, cancel_arg);
865 err = progress_cb(progress_arg, 0L, nours,
872 for (i = 0; i < ntheirs; i++) {
873 struct got_object_id *id = theirs[i];
877 cached_type = got_object_idset_get(idset, id);
878 if (cached_type == NULL) {
879 err = got_object_get_type(&obj_type, repo, id);
883 obj_type = *cached_type;
884 if (obj_type != GOT_OBJ_TYPE_TAG)
886 err = load_tag(NULL, idset, id, repo,
887 loose_obj_only, cancel_cb, cancel_arg);
891 err = progress_cb(progress_arg, 0L, nours,
898 for (i = 0; i < nobj; i++) {
899 err = load_commit(&v, idset, ids[i], repo,
900 loose_obj_only, cancel_cb, cancel_arg);
904 err = progress_cb(progress_arg, 0L, nours,
911 for (i = 0; i < nours; i++) {
912 struct got_object_id *id = ours[i];
916 cached_type = got_object_idset_get(idset, id);
917 if (cached_type == NULL) {
918 err = got_object_get_type(&obj_type, repo, id);
922 obj_type = *cached_type;
923 if (obj_type != GOT_OBJ_TYPE_TAG)
925 err = load_tag(&v, idset, id, repo,
926 loose_obj_only, cancel_cb, cancel_arg);
930 err = progress_cb(progress_arg, 0L, nours,
938 for (i = 0; i < nobj; i++) {
942 got_object_idset_free(idset);
952 const struct got_error *
953 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
957 SHA1Update(ctx, buf, len);
958 n = fwrite(buf, 1, len, f);
960 return got_ferror(f, GOT_ERR_IO);
965 putbe32(char *b, uint32_t n)
974 write_order_cmp(const void *pa, const void *pb)
976 struct got_pack_meta *a, *b, *ahd, *bhd;
978 a = *(struct got_pack_meta **)pa;
979 b = *(struct got_pack_meta **)pb;
980 ahd = (a->head == NULL) ? a : a->head;
981 bhd = (b->head == NULL) ? b : b->head;
982 if (ahd->mtime != bhd->mtime)
983 return bhd->mtime - ahd->mtime;
985 return (uintptr_t)bhd - (uintptr_t)ahd;
986 if (a->nchain != b->nchain)
987 return a->nchain - b->nchain;
988 return a->mtime - b->mtime;
991 static const struct got_error *
992 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
998 hdr[0] = obj_type << 4;
1001 for (i = 1; len != 0; i++){
1003 return got_error(GOT_ERR_NO_SPACE);
1004 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1005 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1006 len >>= GOT_DELTA_SIZE_SHIFT;
1013 static const struct got_error *
1014 encodedelta(struct got_pack_meta *m, struct got_raw_object *o,
1015 off_t base_size, FILE *f)
1017 unsigned char buf[16], *bp;
1021 struct got_delta_instruction *d;
1023 /* base object size */
1024 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1025 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1026 for (i = 1; n > 0; i++) {
1027 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1028 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1029 n >>= GOT_DELTA_SIZE_SHIFT;
1031 w = fwrite(buf, 1, i, f);
1033 return got_ferror(f, GOT_ERR_IO);
1035 /* target object size */
1036 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1037 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1038 for (i = 1; n > 0; i++) {
1039 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1040 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1041 n >>= GOT_DELTA_SIZE_SHIFT;
1043 w = fwrite(buf, 1, i, f);
1045 return got_ferror(f, GOT_ERR_IO);
1047 for (j = 0; j < m->ndeltas; j++) {
1052 buf[0] = GOT_DELTA_BASE_COPY;
1053 for (i = 0; i < 4; i++) {
1054 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1063 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1064 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1065 for (i = 0; i < 3 && n > 0; i++) {
1066 buf[0] |= 1 << (i + 4);
1071 w = fwrite(buf, 1, bp - buf, f);
1073 return got_ferror(f, GOT_ERR_IO);
1077 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1078 return got_error_from_errno("fseeko");
1080 while (n != d->len) {
1081 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1082 w = fwrite(buf, 1, 1, f);
1084 return got_ferror(f, GOT_ERR_IO);
1085 r = fread(content, 1, buf[0], o->f);
1087 return got_ferror(o->f, GOT_ERR_IO);
1088 w = fwrite(content, 1, buf[0], f);
1090 return got_ferror(f, GOT_ERR_IO);
1100 packoff(char *hdr, off_t off)
1105 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1106 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1107 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1108 GOT_DELTA_SIZE_MORE;
1113 hdr[j++] = rbuf[--i];
1117 static const struct got_error *
1118 genpack(uint8_t *pack_sha1, FILE *packfile,
1119 struct got_pack_meta **meta, int nmeta, int nours,
1120 int use_offset_deltas, struct got_repository *repo,
1121 got_pack_progress_cb progress_cb, void *progress_arg,
1122 got_cancel_cb cancel_cb, void *cancel_arg)
1124 const struct got_error *err = NULL;
1128 struct got_pack_meta *m;
1129 struct got_raw_object *raw = NULL;
1130 FILE *delta_file = NULL;
1133 struct got_deflate_checksum csum;
1134 off_t packfile_size = 0;
1138 csum.output_sha1 = &ctx;
1139 csum.output_crc = NULL;
1141 err = hwrite(packfile, "PACK", 4, &ctx);
1144 putbe32(buf, GOT_PACKFILE_VERSION);
1145 err = hwrite(packfile, buf, 4, &ctx);
1148 putbe32(buf, nmeta);
1149 err = hwrite(packfile, buf, 4, &ctx);
1152 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1153 for (i = 0; i < nmeta; i++) {
1155 err = progress_cb(progress_arg, packfile_size, nours,
1161 m->off = ftello(packfile);
1162 err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
1165 if (m->deltas == NULL) {
1166 err = packhdr(&nh, buf, sizeof(buf),
1167 m->obj_type, raw->size);
1170 err = hwrite(packfile, buf, nh, &ctx);
1173 packfile_size += nh;
1174 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1175 err = got_error_from_errno("fseeko");
1178 err = got_deflate_to_file(&outlen, raw->f, packfile,
1182 packfile_size += outlen;
1184 if (delta_file == NULL) {
1185 delta_file = got_opentemp();
1186 if (delta_file == NULL) {
1187 err = got_error_from_errno(
1192 if (ftruncate(fileno(delta_file), 0L) == -1) {
1193 err = got_error_from_errno("ftruncate");
1196 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1197 err = got_error_from_errno("fseeko");
1200 err = encodedelta(m, raw, m->prev->size, delta_file);
1203 nd = ftello(delta_file);
1204 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1205 err = got_error_from_errno("fseeko");
1208 if (use_offset_deltas && m->prev->off != 0) {
1209 err = packhdr(&nh, buf, sizeof(buf),
1210 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1213 nh += packoff(buf + nh,
1214 m->off - m->prev->off);
1215 err = hwrite(packfile, buf, nh, &ctx);
1218 packfile_size += nh;
1220 err = packhdr(&nh, buf, sizeof(buf),
1221 GOT_OBJ_TYPE_REF_DELTA, nd);
1222 err = hwrite(packfile, buf, nh, &ctx);
1225 packfile_size += nh;
1226 err = hwrite(packfile, m->prev->id.sha1,
1227 sizeof(m->prev->id.sha1), &ctx);
1228 packfile_size += sizeof(m->prev->id.sha1);
1232 err = got_deflate_to_file(&outlen, delta_file,
1236 packfile_size += outlen;
1238 got_object_raw_close(raw);
1241 SHA1Final(pack_sha1, &ctx);
1242 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1243 if (n != SHA1_DIGEST_LENGTH)
1244 err = got_ferror(packfile, GOT_ERR_IO);
1245 packfile_size += SHA1_DIGEST_LENGTH;
1246 packfile_size += sizeof(struct got_packfile_hdr);
1247 err = progress_cb(progress_arg, packfile_size, nours,
1248 nmeta, nmeta, nmeta);
1252 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1253 err = got_error_from_errno("fclose");
1255 got_object_raw_close(raw);
1256 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1257 err = got_error_from_errno("close");
1261 const struct got_error *
1262 got_pack_create(uint8_t *packsha1, FILE *packfile,
1263 struct got_object_id **theirs, int ntheirs,
1264 struct got_object_id **ours, int nours,
1265 struct got_repository *repo, int loose_obj_only, int allow_empty,
1266 got_pack_progress_cb progress_cb, void *progress_arg,
1267 got_cancel_cb cancel_cb, void *cancel_arg)
1269 const struct got_error *err;
1270 struct got_pack_meta **meta;
1273 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1274 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1278 if (nmeta == 0 && !allow_empty) {
1279 err = got_error(GOT_ERR_CANNOT_PACK);
1283 err = pick_deltas(meta, nmeta, nours, repo,
1284 progress_cb, progress_arg, cancel_cb, cancel_arg);
1289 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1290 progress_cb, progress_arg, cancel_cb, cancel_arg);
1294 free_nmeta(meta, nmeta);