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>
31 #if defined(__FreeBSD__)
35 #include "got_error.h"
36 #include "got_cancel.h"
37 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository_admin.h"
41 #include "got_opentemp.h"
43 #include "got_lib_deltify.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_deflate.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
54 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
61 struct got_pack_meta {
62 struct got_object_id id;
68 /* The best delta we picked */
69 struct got_pack_meta *head;
70 struct got_pack_meta *prev;
71 off_t delta_offset; /* offset in delta cache file */
72 off_t delta_len; /* length in delta cache file */
75 /* Only used for delta window */
76 struct got_delta_table *dtab;
78 /* Only used for writing offset deltas */
82 struct got_pack_metavec {
83 struct got_pack_meta **meta;
88 static const struct got_error *
89 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
90 const char *path, int obj_type, time_t mtime)
92 const struct got_error *err = NULL;
93 struct got_pack_meta *m;
97 m = calloc(1, sizeof(*m));
99 return got_error_from_errno("calloc");
101 memcpy(&m->id, id, sizeof(m->id));
103 m->path = strdup(path);
104 if (m->path == NULL) {
105 err = got_error_from_errno("strdup");
110 m->obj_type = obj_type;
117 clear_meta(struct got_pack_meta *meta)
126 free_nmeta(struct got_pack_meta **meta, int nmeta)
130 for (i = 0; i < nmeta; i++)
136 delta_order_cmp(const void *pa, const void *pb)
138 struct got_pack_meta *a, *b;
141 a = *(struct got_pack_meta **)pa;
142 b = *(struct got_pack_meta **)pb;
144 if (a->obj_type != b->obj_type)
145 return a->obj_type - b->obj_type;
146 cmp = strcmp(a->path, b->path);
149 if (a->mtime != b->mtime)
150 return a->mtime - b->mtime;
151 return got_object_id_cmp(&a->id, &b->id);
155 delta_size(struct got_delta_instruction *deltas, int ndeltas)
158 for (i = 0; i < ndeltas; i++) {
160 size += GOT_DELTA_SIZE_SHIFT;
162 size += deltas[i].len + 1;
167 static const struct got_error *
168 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
169 struct got_delta_instruction *deltas, int ndeltas,
170 off_t base_size, FILE *f)
172 unsigned char buf[16], *bp;
176 struct got_delta_instruction *d;
178 /* base object size */
179 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
180 n = base_size >> GOT_DELTA_SIZE_SHIFT;
181 for (i = 1; n > 0; i++) {
182 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
183 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
184 n >>= GOT_DELTA_SIZE_SHIFT;
186 w = fwrite(buf, 1, i, f);
188 return got_ferror(f, GOT_ERR_IO);
190 /* target object size */
191 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
192 n = o->size >> GOT_DELTA_SIZE_SHIFT;
193 for (i = 1; n > 0; i++) {
194 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
195 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
196 n >>= GOT_DELTA_SIZE_SHIFT;
198 w = fwrite(buf, 1, i, f);
200 return got_ferror(f, GOT_ERR_IO);
202 for (j = 0; j < ndeltas; j++) {
207 buf[0] = GOT_DELTA_BASE_COPY;
208 for (i = 0; i < 4; i++) {
209 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
218 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
219 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
220 for (i = 0; i < 3 && n > 0; i++) {
221 buf[0] |= 1 << (i + 4);
226 w = fwrite(buf, 1, bp - buf, f);
228 return got_ferror(f, GOT_ERR_IO);
232 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
233 return got_error_from_errno("fseeko");
235 while (n != d->len) {
236 buf[0] = (d->len - n < 127) ? d->len - n : 127;
237 w = fwrite(buf, 1, 1, f);
239 return got_ferror(f, GOT_ERR_IO);
240 r = fread(content, 1, buf[0], o->f);
242 return got_ferror(o->f, GOT_ERR_IO);
243 w = fwrite(content, 1, buf[0], f);
245 return got_ferror(f, GOT_ERR_IO);
255 static const struct got_error *
256 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
257 FILE *delta_cache, struct got_repository *repo,
258 got_pack_progress_cb progress_cb, void *progress_arg,
259 got_cancel_cb cancel_cb, void *cancel_arg)
261 const struct got_error *err = NULL;
262 struct got_pack_meta *m = NULL, *base = NULL;
263 struct got_raw_object *raw = NULL, *base_raw = NULL;
264 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
265 int i, j, size, best_size, ndeltas, best_ndeltas;
266 const int max_base_candidates = 10;
269 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
270 for (i = 0; i < nmeta; i++) {
272 err = (*cancel_cb)(cancel_arg);
277 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
283 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
284 m->obj_type == GOT_OBJ_TYPE_TAG)
287 err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
292 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
293 raw->size + raw->hdrlen);
297 if (i > max_base_candidates) {
298 struct got_pack_meta *n = NULL;
299 n = meta[i - (max_base_candidates + 1)];
300 got_deltify_free(n->dtab);
304 best_size = raw->size;
306 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
308 err = (*cancel_cb)(cancel_arg);
313 /* long chains make unpacking slow, avoid such bases */
314 if (base->nchain >= 32 ||
315 base->obj_type != m->obj_type)
318 err = got_object_raw_open(&base_raw, &outfd, repo,
322 err = got_deltify(&deltas, &ndeltas,
323 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
324 base->dtab, base_raw->f, base_raw->hdrlen,
325 base_raw->size + base_raw->hdrlen);
326 got_object_raw_close(base_raw);
331 size = delta_size(deltas, ndeltas);
332 if (size + 32 < best_size){
334 * if we already picked a best delta,
339 best_deltas = deltas;
340 best_ndeltas = ndeltas;
342 m->nchain = base->nchain + 1;
344 m->head = base->head;
354 if (best_ndeltas > 0) {
355 m->delta_offset = ftello(delta_cache);
356 err = encode_delta(m, raw, best_deltas,
357 best_ndeltas, m->prev->size, delta_cache);
363 m->delta_len = ftello(delta_cache) - m->delta_offset;
366 got_object_raw_close(raw);
370 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
371 got_deltify_free(meta[i]->dtab);
372 meta[i]->dtab = NULL;
375 got_object_raw_close(raw);
377 got_object_raw_close(base_raw);
378 if (outfd != -1 && close(outfd) == -1 && err == NULL)
379 err = got_error_from_errno("close");
385 static const struct got_error *
386 search_packidx(int *found, struct got_object_id *id,
387 struct got_repository *repo)
389 const struct got_error *err = NULL;
390 struct got_packidx *packidx = NULL;
395 err = got_repo_search_packidx(&packidx, &idx, repo, id);
397 *found = 1; /* object is already packed */
398 else if (err->code == GOT_ERR_NO_OBJ)
403 static const int obj_types[] = {
409 GOT_OBJ_TYPE_OFFSET_DELTA,
410 GOT_OBJ_TYPE_REF_DELTA
413 static const struct got_error *
414 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
415 struct got_object_id *id, const char *path, int obj_type,
416 time_t mtime, int loose_obj_only, struct got_repository *repo)
418 const struct got_error *err;
419 struct got_pack_meta *m;
421 if (loose_obj_only) {
423 err = search_packidx(&is_packed, id, repo);
430 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
437 err = alloc_meta(&m, id, path, obj_type, mtime);
441 if (v->nmeta == v->metasz){
442 size_t newsize = 2 * v->metasz;
443 struct got_pack_meta **new;
444 new = reallocarray(v->meta, newsize, sizeof(*new));
446 err = got_error_from_errno("reallocarray");
457 v->meta[v->nmeta++] = m;
462 static const struct got_error *
463 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
464 struct got_object_idset *idset, struct got_object_id *tree_id,
465 const char *dpath, time_t mtime, struct got_repository *repo,
466 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
468 const struct got_error *err;
469 struct got_tree_object *tree;
473 err = got_object_open_as_tree(&tree, repo, tree_id);
477 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
478 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
479 struct got_object_id *id = got_tree_entry_get_id(e);
480 mode_t mode = got_tree_entry_get_mode(e);
483 err = (*cancel_cb)(cancel_arg);
488 if (got_object_tree_entry_is_submodule(e) ||
489 got_object_idset_contains(idset, id))
492 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
493 got_tree_entry_get_name(e)) == -1) {
494 err = got_error_from_errno("asprintf");
499 struct got_object_qid *qid;
500 err = got_object_qid_alloc(&qid, id);
503 STAILQ_INSERT_TAIL(ids, qid, entry);
504 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
505 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
506 mtime, loose_obj_only, repo);
514 got_object_tree_close(tree);
519 static const struct got_error *
520 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
521 struct got_object_id *tree_id, const char *dpath, time_t mtime,
522 int loose_obj_only, struct got_repository *repo,
523 got_cancel_cb cancel_cb, void *cancel_arg)
525 const struct got_error *err = NULL;
526 struct got_object_id_queue tree_ids;
527 struct got_object_qid *qid;
529 if (got_object_idset_contains(idset, tree_id))
532 err = got_object_qid_alloc(&qid, tree_id);
536 STAILQ_INIT(&tree_ids);
537 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
539 while (!STAILQ_EMPTY(&tree_ids)) {
541 err = (*cancel_cb)(cancel_arg);
546 qid = STAILQ_FIRST(&tree_ids);
547 STAILQ_REMOVE_HEAD(&tree_ids, entry);
549 if (got_object_idset_contains(idset, qid->id)) {
550 got_object_qid_free(qid);
554 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
555 mtime, loose_obj_only, repo);
557 got_object_qid_free(qid);
561 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
562 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
563 got_object_qid_free(qid);
568 got_object_id_queue_free(&tree_ids);
572 static const struct got_error *
573 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
574 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
575 got_cancel_cb cancel_cb, void *cancel_arg)
577 const struct got_error *err;
578 struct got_commit_object *commit;
580 if (got_object_idset_contains(idset, id))
583 if (loose_obj_only) {
585 err = search_packidx(&is_packed, id, repo);
592 err = got_object_open_as_commit(&commit, repo, id);
596 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
597 got_object_commit_get_committer_time(commit),
598 loose_obj_only, repo);
602 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
603 "", got_object_commit_get_committer_time(commit),
604 loose_obj_only, repo, cancel_cb, cancel_arg);
606 got_object_commit_close(commit);
610 static const struct got_error *
611 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
612 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
613 got_cancel_cb cancel_cb, void *cancel_arg)
615 const struct got_error *err;
616 struct got_tag_object *tag = NULL;
618 if (got_object_idset_contains(idset, id))
621 if (loose_obj_only) {
623 err = search_packidx(&is_packed, id, repo);
630 err = got_object_open_as_tag(&tag, repo, id);
634 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
635 got_object_tag_get_tagger_time(tag),
636 loose_obj_only, repo);
640 switch (got_object_tag_get_object_type(tag)) {
641 case GOT_OBJ_TYPE_COMMIT:
642 err = load_commit(v, idset,
643 got_object_tag_get_object_id(tag), repo,
644 loose_obj_only, cancel_cb, cancel_arg);
646 case GOT_OBJ_TYPE_TREE:
647 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
648 "", got_object_tag_get_tagger_time(tag),
649 loose_obj_only, repo, cancel_cb, cancel_arg);
656 got_object_tag_close(tag);
660 enum findtwixt_color {
665 static const int findtwixt_colors[] = {
671 static const struct got_error *
672 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
673 int color, struct got_repository *repo)
675 const struct got_error *err;
676 struct got_object_qid *qid;
678 err = got_object_qid_alloc(&qid, id);
682 STAILQ_INSERT_TAIL(ids, qid, entry);
683 qid->data = (void *)&findtwixt_colors[color];
687 static const struct got_error *
688 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
689 struct got_object_id *id, struct got_repository *repo,
690 got_cancel_cb cancel_cb, void *cancel_arg)
692 const struct got_error *err = NULL;
693 struct got_commit_object *commit;
694 const struct got_object_id_queue *parents;
695 struct got_object_id_queue ids;
696 struct got_object_qid *qid;
700 err = got_object_qid_alloc(&qid, id);
703 STAILQ_INSERT_HEAD(&ids, qid, entry);
705 while (!STAILQ_EMPTY(&ids)) {
707 err = (*cancel_cb)(cancel_arg);
712 qid = STAILQ_FIRST(&ids);
713 STAILQ_REMOVE_HEAD(&ids, entry);
715 if (got_object_idset_contains(drop, qid->id)) {
716 got_object_qid_free(qid);
720 err = got_object_idset_add(drop, qid->id,
721 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
723 got_object_qid_free(qid);
727 if (!got_object_idset_contains(keep, qid->id)) {
728 got_object_qid_free(qid);
732 err = got_object_open_as_commit(&commit, repo, qid->id);
733 got_object_qid_free(qid);
737 parents = got_object_commit_get_parent_ids(commit);
739 err = got_object_id_queue_copy(parents, &ids);
741 got_object_commit_close(commit);
745 got_object_commit_close(commit);
748 got_object_id_queue_free(&ids);
752 struct append_id_arg {
753 struct got_object_id **array;
757 static const struct got_error *
758 append_id(struct got_object_id *id, void *data, void *arg)
760 struct append_id_arg *a = arg;
762 a->array[a->idx] = got_object_id_dup(id);
763 if (a->array[a->idx] == NULL)
764 return got_error_from_errno("got_object_id_dup");
770 static const struct got_error *
771 findtwixt(struct got_object_id ***res, int *nres,
772 struct got_object_id **head, int nhead,
773 struct got_object_id **tail, int ntail,
774 struct got_repository *repo,
775 got_cancel_cb cancel_cb, void *cancel_arg)
777 const struct got_error *err = NULL;
778 struct got_object_id_queue ids;
779 struct got_object_idset *keep, *drop;
780 struct got_object_qid *qid;
781 int i, ncolor, nkeep, obj_type;
787 keep = got_object_idset_alloc();
789 return got_error_from_errno("got_object_idset_alloc");
791 drop = got_object_idset_alloc();
793 err = got_error_from_errno("got_object_idset_alloc");
797 for (i = 0; i < nhead; i++) {
798 struct got_object_id *id = head[i];
801 err = got_object_get_type(&obj_type, repo, id);
804 if (obj_type != GOT_OBJ_TYPE_COMMIT)
806 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
810 for (i = 0; i < ntail; i++) {
811 struct got_object_id *id = tail[i];
814 err = got_object_get_type(&obj_type, repo, id);
817 if (obj_type != GOT_OBJ_TYPE_COMMIT)
819 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
824 while (!STAILQ_EMPTY(&ids)) {
826 qid = STAILQ_FIRST(&ids);
827 qcolor = *((int *)qid->data);
829 if (got_object_idset_contains(drop, qid->id))
831 else if (got_object_idset_contains(keep, qid->id))
834 ncolor = COLOR_BLANK;
836 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
837 qcolor == COLOR_KEEP)) {
838 STAILQ_REMOVE_HEAD(&ids, entry);
839 got_object_qid_free(qid);
843 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
844 err = drop_commit(keep, drop, qid->id, repo,
845 cancel_cb, cancel_arg);
848 } else if (ncolor == COLOR_BLANK) {
849 struct got_commit_object *commit;
850 struct got_object_id *id;
851 const struct got_object_id_queue *parents;
852 struct got_object_qid *pid;
854 id = got_object_id_dup(qid->id);
856 err = got_error_from_errno("got_object_id_dup");
859 if (qcolor == COLOR_KEEP)
860 err = got_object_idset_add(keep, id,
861 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
863 err = got_object_idset_add(drop, id,
864 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
870 err = got_object_open_as_commit(&commit, repo, id);
875 parents = got_object_commit_get_parent_ids(commit);
877 STAILQ_FOREACH(pid, parents, entry) {
878 err = queue_commit_id(&ids, pid->id,
886 got_object_commit_close(commit);
889 /* should not happen */
890 err = got_error_fmt(GOT_ERR_NOT_IMPL,
891 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
895 STAILQ_REMOVE_HEAD(&ids, entry);
896 got_object_qid_free(qid);
899 nkeep = got_object_idset_num_elements(keep);
901 struct append_id_arg arg;
902 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
903 if (arg.array == NULL) {
904 err = got_error_from_errno("calloc");
908 err = got_object_idset_for_each(keep, append_id, &arg);
917 got_object_idset_free(keep);
918 got_object_idset_free(drop);
919 got_object_id_queue_free(&ids);
923 static const struct got_error *
924 read_meta(struct got_pack_meta ***meta, int *nmeta,
925 struct got_object_id **theirs, int ntheirs,
926 struct got_object_id **ours, int nours, struct got_repository *repo,
927 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
928 got_cancel_cb cancel_cb, void *cancel_arg)
930 const struct got_error *err = NULL;
931 struct got_object_id **ids = NULL;
932 struct got_object_idset *idset;
933 int i, nobj = 0, obj_type;
934 struct got_pack_metavec v;
939 idset = got_object_idset_alloc();
941 return got_error_from_errno("got_object_idset_alloc");
945 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
946 if (v.meta == NULL) {
947 err = got_error_from_errno("calloc");
951 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
952 cancel_cb, cancel_arg);
953 if (err || nobj == 0)
956 for (i = 0; i < ntheirs; i++) {
957 struct got_object_id *id = theirs[i];
960 err = got_object_get_type(&obj_type, repo, id);
963 if (obj_type != GOT_OBJ_TYPE_COMMIT)
965 err = load_commit(NULL, idset, id, repo,
966 loose_obj_only, cancel_cb, cancel_arg);
970 err = progress_cb(progress_arg, 0L, nours,
977 for (i = 0; i < ntheirs; i++) {
978 struct got_object_id *id = theirs[i];
982 cached_type = got_object_idset_get(idset, id);
983 if (cached_type == NULL) {
984 err = got_object_get_type(&obj_type, repo, id);
988 obj_type = *cached_type;
989 if (obj_type != GOT_OBJ_TYPE_TAG)
991 err = load_tag(NULL, idset, id, repo,
992 loose_obj_only, cancel_cb, cancel_arg);
996 err = progress_cb(progress_arg, 0L, nours,
1003 for (i = 0; i < nobj; i++) {
1004 err = load_commit(&v, idset, ids[i], repo,
1005 loose_obj_only, cancel_cb, cancel_arg);
1009 err = progress_cb(progress_arg, 0L, nours,
1016 for (i = 0; i < nours; i++) {
1017 struct got_object_id *id = ours[i];
1021 cached_type = got_object_idset_get(idset, id);
1022 if (cached_type == NULL) {
1023 err = got_object_get_type(&obj_type, repo, id);
1027 obj_type = *cached_type;
1028 if (obj_type != GOT_OBJ_TYPE_TAG)
1030 err = load_tag(&v, idset, id, repo,
1031 loose_obj_only, cancel_cb, cancel_arg);
1035 err = progress_cb(progress_arg, 0L, nours,
1043 for (i = 0; i < nobj; i++) {
1047 got_object_idset_free(idset);
1057 const struct got_error *
1058 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1062 SHA1Update(ctx, buf, len);
1063 n = fwrite(buf, 1, len, f);
1065 return got_ferror(f, GOT_ERR_IO);
1070 putbe32(char *b, uint32_t n)
1079 write_order_cmp(const void *pa, const void *pb)
1081 struct got_pack_meta *a, *b, *ahd, *bhd;
1083 a = *(struct got_pack_meta **)pa;
1084 b = *(struct got_pack_meta **)pb;
1085 ahd = (a->head == NULL) ? a : a->head;
1086 bhd = (b->head == NULL) ? b : b->head;
1087 if (ahd->mtime != bhd->mtime)
1088 return bhd->mtime - ahd->mtime;
1090 return (uintptr_t)bhd - (uintptr_t)ahd;
1091 if (a->nchain != b->nchain)
1092 return a->nchain - b->nchain;
1093 return a->mtime - b->mtime;
1096 static const struct got_error *
1097 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1103 hdr[0] = obj_type << 4;
1104 hdr[0] |= len & 0xf;
1106 for (i = 1; len != 0; i++){
1108 return got_error(GOT_ERR_NO_SPACE);
1109 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1110 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1111 len >>= GOT_DELTA_SIZE_SHIFT;
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, FILE *delta_cache,
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 = NULL;
1148 FILE *delta_file = NULL;
1151 struct got_deflate_checksum csum;
1152 off_t packfile_size = 0;
1156 csum.output_sha1 = &ctx;
1157 csum.output_crc = NULL;
1159 err = hwrite(packfile, "PACK", 4, &ctx);
1162 putbe32(buf, GOT_PACKFILE_VERSION);
1163 err = hwrite(packfile, buf, 4, &ctx);
1166 putbe32(buf, nmeta);
1167 err = hwrite(packfile, buf, 4, &ctx);
1170 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1171 for (i = 0; i < nmeta; i++) {
1173 err = progress_cb(progress_arg, packfile_size, nours,
1179 m->off = ftello(packfile);
1180 err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
1183 if (m->deltas == NULL) {
1184 err = packhdr(&nh, buf, sizeof(buf),
1185 m->obj_type, raw->size);
1188 err = hwrite(packfile, buf, nh, &ctx);
1191 packfile_size += nh;
1192 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1193 err = got_error_from_errno("fseeko");
1196 err = got_deflate_to_file(&outlen, raw->f, packfile,
1200 packfile_size += outlen;
1203 if (delta_file == NULL) {
1204 delta_file = got_opentemp();
1205 if (delta_file == NULL) {
1206 err = got_error_from_errno(
1211 if (ftruncate(fileno(delta_file), 0L) == -1) {
1212 err = got_error_from_errno("ftruncate");
1215 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1216 err = got_error_from_errno("fseeko");
1219 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1221 err = got_error_from_errno("fseeko");
1224 remain = m->delta_len;
1225 while (remain > 0) {
1226 char delta_buf[8192];
1228 n = MIN(remain, sizeof(delta_buf));
1229 r = fread(delta_buf, 1, n, delta_cache);
1231 err = got_ferror(delta_cache,
1235 w = fwrite(delta_buf, 1, n, delta_file);
1237 err = got_ferror(delta_file,
1243 if (use_offset_deltas && m->prev->off != 0) {
1244 err = packhdr(&nh, buf, sizeof(buf),
1245 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1248 nh += packoff(buf + nh,
1249 m->off - m->prev->off);
1250 err = hwrite(packfile, buf, nh, &ctx);
1253 packfile_size += nh;
1255 err = packhdr(&nh, buf, sizeof(buf),
1256 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1257 err = hwrite(packfile, buf, nh, &ctx);
1260 packfile_size += nh;
1261 err = hwrite(packfile, m->prev->id.sha1,
1262 sizeof(m->prev->id.sha1), &ctx);
1263 packfile_size += sizeof(m->prev->id.sha1);
1267 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1268 err = got_error_from_errno("fseeko");
1271 err = got_deflate_to_file(&outlen, delta_file,
1275 packfile_size += outlen;
1277 got_object_raw_close(raw);
1280 SHA1Final(pack_sha1, &ctx);
1281 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1282 if (n != SHA1_DIGEST_LENGTH)
1283 err = got_ferror(packfile, GOT_ERR_IO);
1284 packfile_size += SHA1_DIGEST_LENGTH;
1285 packfile_size += sizeof(struct got_packfile_hdr);
1286 err = progress_cb(progress_arg, packfile_size, nours,
1287 nmeta, nmeta, nmeta);
1291 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1292 err = got_error_from_errno("fclose");
1294 got_object_raw_close(raw);
1295 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1296 err = got_error_from_errno("close");
1300 const struct got_error *
1301 got_pack_create(uint8_t *packsha1, FILE *packfile,
1302 struct got_object_id **theirs, int ntheirs,
1303 struct got_object_id **ours, int nours,
1304 struct got_repository *repo, int loose_obj_only, int allow_empty,
1305 got_pack_progress_cb progress_cb, void *progress_arg,
1306 got_cancel_cb cancel_cb, void *cancel_arg)
1308 const struct got_error *err;
1309 struct got_pack_meta **meta;
1311 FILE *delta_cache = NULL;
1313 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1314 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1318 if (nmeta == 0 && !allow_empty) {
1319 err = got_error(GOT_ERR_CANNOT_PACK);
1323 delta_cache = got_opentemp();
1324 if (delta_cache == NULL) {
1325 err = got_error_from_errno("got_opentemp");
1330 err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1331 progress_cb, progress_arg, cancel_cb, cancel_arg);
1334 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1335 err = got_error_from_errno("fseeko");
1340 err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1341 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1345 free_nmeta(meta, nmeta);
1346 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1347 err = got_error_from_errno("fclose");