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;
177 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
178 for (i = 0; i < nmeta; i++) {
180 err = (*cancel_cb)(cancel_arg);
185 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
193 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
194 m->obj_type == GOT_OBJ_TYPE_TAG)
197 err = got_object_raw_open(&raw, repo, &m->id, 8192);
202 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
203 raw->size + raw->hdrlen);
207 if (i > max_base_candidates) {
208 struct got_pack_meta *n = NULL;
209 n = meta[i - (max_base_candidates + 1)];
210 got_deltify_free(n->dtab);
215 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
217 err = (*cancel_cb)(cancel_arg);
222 /* long chains make unpacking slow, avoid such bases */
223 if (base->nchain >= 128 ||
224 base->obj_type != m->obj_type)
227 err = got_object_raw_open(&base_raw, repo, &base->id,
231 err = got_deltify(&deltas, &ndeltas,
232 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
233 base->dtab, base_raw->f, base_raw->hdrlen,
234 base_raw->size + base_raw->hdrlen);
235 got_object_raw_close(base_raw);
240 size = delta_size(deltas, ndeltas);
241 if (size + 32 < best){
243 * if we already picked a best delta,
249 m->ndeltas = ndeltas;
250 m->nchain = base->nchain + 1;
252 m->head = base->head;
262 got_object_raw_close(raw);
266 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
267 got_deltify_free(meta[i]->dtab);
268 meta[i]->dtab = NULL;
271 got_object_raw_close(raw);
273 got_object_raw_close(base_raw);
277 static const struct got_error *
278 search_packidx(int *found, struct got_object_id *id,
279 struct got_repository *repo)
281 const struct got_error *err = NULL;
282 struct got_packidx *packidx = NULL;
287 err = got_repo_search_packidx(&packidx, &idx, repo, id);
289 *found = 1; /* object is already packed */
290 else if (err->code == GOT_ERR_NO_OBJ)
295 static const int obj_types[] = {
301 GOT_OBJ_TYPE_OFFSET_DELTA,
302 GOT_OBJ_TYPE_REF_DELTA
305 static const struct got_error *
306 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
307 struct got_object_id *id, const char *path, int obj_type,
308 time_t mtime, int loose_obj_only, struct got_repository *repo)
310 const struct got_error *err;
311 struct got_pack_meta *m;
313 if (loose_obj_only) {
315 err = search_packidx(&is_packed, id, repo);
322 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
329 err = alloc_meta(&m, id, path, obj_type, mtime);
333 if (v->nmeta == v->metasz){
334 size_t newsize = 2 * v->metasz;
335 struct got_pack_meta **new;
336 new = reallocarray(v->meta, newsize, sizeof(*new));
338 err = got_error_from_errno("reallocarray");
349 v->meta[v->nmeta++] = m;
354 static const struct got_error *
355 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
356 struct got_object_idset *idset, struct got_object_id *tree_id,
357 const char *dpath, time_t mtime, struct got_repository *repo,
358 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
360 const struct got_error *err;
361 struct got_tree_object *tree;
365 err = got_object_open_as_tree(&tree, repo, tree_id);
369 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
370 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
371 struct got_object_id *id = got_tree_entry_get_id(e);
372 mode_t mode = got_tree_entry_get_mode(e);
375 err = (*cancel_cb)(cancel_arg);
380 if (got_object_tree_entry_is_submodule(e) ||
381 got_object_idset_contains(idset, id))
384 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
385 got_tree_entry_get_name(e)) == -1) {
386 err = got_error_from_errno("asprintf");
391 struct got_object_qid *qid;
392 err = got_object_qid_alloc(&qid, id);
395 STAILQ_INSERT_TAIL(ids, qid, entry);
396 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
397 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
398 mtime, loose_obj_only, repo);
406 got_object_tree_close(tree);
411 static const struct got_error *
412 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
413 struct got_object_id *tree_id, const char *dpath, time_t mtime,
414 int loose_obj_only, struct got_repository *repo,
415 got_cancel_cb cancel_cb, void *cancel_arg)
417 const struct got_error *err = NULL;
418 struct got_object_id_queue tree_ids;
419 struct got_object_qid *qid;
421 if (got_object_idset_contains(idset, tree_id))
424 err = got_object_qid_alloc(&qid, tree_id);
428 STAILQ_INIT(&tree_ids);
429 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
431 while (!STAILQ_EMPTY(&tree_ids)) {
433 err = (*cancel_cb)(cancel_arg);
438 qid = STAILQ_FIRST(&tree_ids);
439 STAILQ_REMOVE_HEAD(&tree_ids, entry);
441 if (got_object_idset_contains(idset, qid->id)) {
442 got_object_qid_free(qid);
446 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
447 mtime, loose_obj_only, repo);
449 got_object_qid_free(qid);
453 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
454 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
455 got_object_qid_free(qid);
460 got_object_id_queue_free(&tree_ids);
464 static const struct got_error *
465 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
466 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
467 got_cancel_cb cancel_cb, void *cancel_arg)
469 const struct got_error *err;
470 struct got_commit_object *commit;
472 if (got_object_idset_contains(idset, id))
475 if (loose_obj_only) {
477 err = search_packidx(&is_packed, id, repo);
484 err = got_object_open_as_commit(&commit, repo, id);
488 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
489 got_object_commit_get_committer_time(commit),
490 loose_obj_only, repo);
494 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
495 "", got_object_commit_get_committer_time(commit),
496 loose_obj_only, repo, cancel_cb, cancel_arg);
498 got_object_commit_close(commit);
502 static const struct got_error *
503 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
504 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
505 got_cancel_cb cancel_cb, void *cancel_arg)
507 const struct got_error *err;
508 struct got_tag_object *tag = NULL;
510 if (got_object_idset_contains(idset, id))
513 if (loose_obj_only) {
515 err = search_packidx(&is_packed, id, repo);
522 err = got_object_open_as_tag(&tag, repo, id);
526 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
527 got_object_tag_get_tagger_time(tag),
528 loose_obj_only, repo);
532 switch (got_object_tag_get_object_type(tag)) {
533 case GOT_OBJ_TYPE_COMMIT:
534 err = load_commit(v, idset,
535 got_object_tag_get_object_id(tag), repo,
536 loose_obj_only, cancel_cb, cancel_arg);
538 case GOT_OBJ_TYPE_TREE:
539 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
540 "", got_object_tag_get_tagger_time(tag),
541 loose_obj_only, repo, cancel_cb, cancel_arg);
548 got_object_tag_close(tag);
552 enum findtwixt_color {
557 static const int findtwixt_colors[] = {
563 static const struct got_error *
564 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
565 int color, struct got_repository *repo)
567 const struct got_error *err;
568 struct got_object_qid *qid;
570 err = got_object_qid_alloc(&qid, id);
574 STAILQ_INSERT_TAIL(ids, qid, entry);
575 qid->data = (void *)&findtwixt_colors[color];
579 static const struct got_error *
580 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
581 struct got_object_id *id, struct got_repository *repo,
582 got_cancel_cb cancel_cb, void *cancel_arg)
584 const struct got_error *err = NULL;
585 struct got_commit_object *commit;
586 const struct got_object_id_queue *parents;
587 struct got_object_id_queue ids;
588 struct got_object_qid *qid;
592 err = got_object_qid_alloc(&qid, id);
595 STAILQ_INSERT_HEAD(&ids, qid, entry);
597 while (!STAILQ_EMPTY(&ids)) {
599 err = (*cancel_cb)(cancel_arg);
604 qid = STAILQ_FIRST(&ids);
605 STAILQ_REMOVE_HEAD(&ids, entry);
607 if (got_object_idset_contains(drop, qid->id)) {
608 got_object_qid_free(qid);
612 err = got_object_idset_add(drop, qid->id,
613 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
615 got_object_qid_free(qid);
619 if (!got_object_idset_contains(keep, qid->id)) {
620 got_object_qid_free(qid);
624 err = got_object_open_as_commit(&commit, repo, qid->id);
625 got_object_qid_free(qid);
629 parents = got_object_commit_get_parent_ids(commit);
631 err = got_object_id_queue_copy(parents, &ids);
633 got_object_commit_close(commit);
637 got_object_commit_close(commit);
640 got_object_id_queue_free(&ids);
644 struct append_id_arg {
645 struct got_object_id **array;
649 static const struct got_error *
650 append_id(struct got_object_id *id, void *data, void *arg)
652 struct append_id_arg *a = arg;
654 a->array[a->idx] = got_object_id_dup(id);
655 if (a->array[a->idx] == NULL)
656 return got_error_from_errno("got_object_id_dup");
662 static const struct got_error *
663 findtwixt(struct got_object_id ***res, int *nres,
664 struct got_object_id **head, int nhead,
665 struct got_object_id **tail, int ntail,
666 struct got_repository *repo,
667 got_cancel_cb cancel_cb, void *cancel_arg)
669 const struct got_error *err = NULL;
670 struct got_object_id_queue ids;
671 struct got_object_idset *keep, *drop;
672 struct got_object_qid *qid;
673 int i, ncolor, nkeep, obj_type;
679 keep = got_object_idset_alloc();
681 return got_error_from_errno("got_object_idset_alloc");
683 drop = got_object_idset_alloc();
685 err = got_error_from_errno("got_object_idset_alloc");
689 for (i = 0; i < nhead; i++) {
690 struct got_object_id *id = head[i];
693 err = got_object_get_type(&obj_type, repo, id);
696 if (obj_type != GOT_OBJ_TYPE_COMMIT)
698 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
702 for (i = 0; i < ntail; i++) {
703 struct got_object_id *id = tail[i];
706 err = got_object_get_type(&obj_type, repo, id);
709 if (obj_type != GOT_OBJ_TYPE_COMMIT)
711 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
716 while (!STAILQ_EMPTY(&ids)) {
718 qid = STAILQ_FIRST(&ids);
719 qcolor = *((int *)qid->data);
721 if (got_object_idset_contains(drop, qid->id))
723 else if (got_object_idset_contains(keep, qid->id))
726 ncolor = COLOR_BLANK;
728 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
729 qcolor == COLOR_KEEP)) {
730 STAILQ_REMOVE_HEAD(&ids, entry);
731 got_object_qid_free(qid);
735 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
736 err = drop_commit(keep, drop, qid->id, repo,
737 cancel_cb, cancel_arg);
740 } else if (ncolor == COLOR_BLANK) {
741 struct got_commit_object *commit;
742 struct got_object_id *id;
743 const struct got_object_id_queue *parents;
744 struct got_object_qid *pid;
746 id = got_object_id_dup(qid->id);
748 err = got_error_from_errno("got_object_id_dup");
751 if (qcolor == COLOR_KEEP)
752 err = got_object_idset_add(keep, id,
753 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
755 err = got_object_idset_add(drop, id,
756 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
762 err = got_object_open_as_commit(&commit, repo, id);
767 parents = got_object_commit_get_parent_ids(commit);
769 STAILQ_FOREACH(pid, parents, entry) {
770 err = queue_commit_id(&ids, pid->id,
778 got_object_commit_close(commit);
781 /* should not happen */
782 err = got_error_fmt(GOT_ERR_NOT_IMPL,
783 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
787 STAILQ_REMOVE_HEAD(&ids, entry);
788 got_object_qid_free(qid);
791 nkeep = got_object_idset_num_elements(keep);
793 struct append_id_arg arg;
794 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
795 if (arg.array == NULL) {
796 err = got_error_from_errno("calloc");
800 err = got_object_idset_for_each(keep, append_id, &arg);
809 got_object_idset_free(keep);
810 got_object_idset_free(drop);
811 got_object_id_queue_free(&ids);
815 static const struct got_error *
816 read_meta(struct got_pack_meta ***meta, int *nmeta,
817 struct got_object_id **theirs, int ntheirs,
818 struct got_object_id **ours, int nours, struct got_repository *repo,
819 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
820 got_cancel_cb cancel_cb, void *cancel_arg)
822 const struct got_error *err = NULL;
823 struct got_object_id **ids = NULL;
824 struct got_object_idset *idset;
825 int i, nobj = 0, obj_type;
826 struct got_pack_metavec v;
831 idset = got_object_idset_alloc();
833 return got_error_from_errno("got_object_idset_alloc");
837 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
838 if (v.meta == NULL) {
839 err = got_error_from_errno("calloc");
843 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
844 cancel_cb, cancel_arg);
845 if (err || nobj == 0)
848 for (i = 0; i < ntheirs; i++) {
849 struct got_object_id *id = theirs[i];
852 err = got_object_get_type(&obj_type, repo, id);
855 if (obj_type != GOT_OBJ_TYPE_COMMIT)
857 err = load_commit(NULL, idset, id, repo,
858 loose_obj_only, cancel_cb, cancel_arg);
862 err = progress_cb(progress_arg, 0L, nours,
869 for (i = 0; i < ntheirs; i++) {
870 struct got_object_id *id = theirs[i];
874 cached_type = got_object_idset_get(idset, id);
875 if (cached_type == NULL) {
876 err = got_object_get_type(&obj_type, repo, id);
880 obj_type = *cached_type;
881 if (obj_type != GOT_OBJ_TYPE_TAG)
883 err = load_tag(NULL, idset, id, repo,
884 loose_obj_only, cancel_cb, cancel_arg);
888 err = progress_cb(progress_arg, 0L, nours,
895 for (i = 0; i < nobj; i++) {
896 err = load_commit(&v, idset, ids[i], repo,
897 loose_obj_only, cancel_cb, cancel_arg);
901 err = progress_cb(progress_arg, 0L, nours,
908 for (i = 0; i < nours; i++) {
909 struct got_object_id *id = ours[i];
913 cached_type = got_object_idset_get(idset, id);
914 if (cached_type == NULL) {
915 err = got_object_get_type(&obj_type, repo, id);
919 obj_type = *cached_type;
920 if (obj_type != GOT_OBJ_TYPE_TAG)
922 err = load_tag(&v, idset, id, repo,
923 loose_obj_only, cancel_cb, cancel_arg);
927 err = progress_cb(progress_arg, 0L, nours,
935 for (i = 0; i < nobj; i++) {
939 got_object_idset_free(idset);
949 const struct got_error *
950 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
954 SHA1Update(ctx, buf, len);
955 n = fwrite(buf, 1, len, f);
957 return got_ferror(f, GOT_ERR_IO);
962 putbe32(char *b, uint32_t n)
971 write_order_cmp(const void *pa, const void *pb)
973 struct got_pack_meta *a, *b, *ahd, *bhd;
975 a = *(struct got_pack_meta **)pa;
976 b = *(struct got_pack_meta **)pb;
977 ahd = (a->head == NULL) ? a : a->head;
978 bhd = (b->head == NULL) ? b : b->head;
979 if (ahd->mtime != bhd->mtime)
980 return bhd->mtime - ahd->mtime;
982 return (uintptr_t)bhd - (uintptr_t)ahd;
983 if (a->nchain != b->nchain)
984 return a->nchain - b->nchain;
985 return a->mtime - b->mtime;
988 static const struct got_error *
989 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
995 hdr[0] = obj_type << 4;
998 for (i = 1; len != 0; i++){
1000 return got_error(GOT_ERR_NO_SPACE);
1001 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1002 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1003 len >>= GOT_DELTA_SIZE_SHIFT;
1010 static const struct got_error *
1011 encodedelta(struct got_pack_meta *m, struct got_raw_object *o,
1012 off_t base_size, FILE *f)
1014 unsigned char buf[16], *bp;
1018 struct got_delta_instruction *d;
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 w = fwrite(buf, 1, i, f);
1030 return got_ferror(f, GOT_ERR_IO);
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 w = fwrite(buf, 1, i, f);
1042 return got_ferror(f, GOT_ERR_IO);
1044 for (j = 0; j < m->ndeltas; j++) {
1049 buf[0] = GOT_DELTA_BASE_COPY;
1050 for (i = 0; i < 4; i++) {
1051 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1060 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1061 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1062 for (i = 0; i < 3 && n > 0; i++) {
1063 buf[0] |= 1 << (i + 4);
1068 w = fwrite(buf, 1, bp - buf, f);
1070 return got_ferror(f, GOT_ERR_IO);
1074 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1075 return got_error_from_errno("fseeko");
1077 while (n != d->len) {
1078 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1079 w = fwrite(buf, 1, 1, f);
1081 return got_ferror(f, GOT_ERR_IO);
1082 r = fread(content, 1, buf[0], o->f);
1084 return got_ferror(o->f, GOT_ERR_IO);
1085 w = fwrite(content, 1, buf[0], f);
1087 return got_ferror(f, GOT_ERR_IO);
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;
1125 struct got_pack_meta *m;
1126 struct got_raw_object *raw = NULL;
1127 FILE *delta_file = NULL;
1130 struct got_deflate_checksum csum;
1131 off_t packfile_size = 0;
1134 csum.output_sha1 = &ctx;
1135 csum.output_crc = NULL;
1137 err = hwrite(packfile, "PACK", 4, &ctx);
1140 putbe32(buf, GOT_PACKFILE_VERSION);
1141 err = hwrite(packfile, buf, 4, &ctx);
1144 putbe32(buf, nmeta);
1145 err = hwrite(packfile, buf, 4, &ctx);
1148 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1149 for (i = 0; i < nmeta; i++) {
1151 err = progress_cb(progress_arg, packfile_size, nours,
1157 m->off = ftello(packfile);
1158 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1161 if (m->deltas == NULL) {
1162 err = packhdr(&nh, buf, sizeof(buf),
1163 m->obj_type, raw->size);
1166 err = hwrite(packfile, buf, nh, &ctx);
1169 packfile_size += nh;
1170 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1171 err = got_error_from_errno("fseeko");
1174 err = got_deflate_to_file(&outlen, raw->f, packfile,
1178 packfile_size += outlen;
1180 if (delta_file == NULL) {
1181 delta_file = got_opentemp();
1182 if (delta_file == NULL) {
1183 err = got_error_from_errno(
1188 if (ftruncate(fileno(delta_file), 0L) == -1) {
1189 err = got_error_from_errno("ftruncate");
1192 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1193 err = got_error_from_errno("fseeko");
1196 err = encodedelta(m, raw, m->prev->size, delta_file);
1199 nd = ftello(delta_file);
1200 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1201 err = got_error_from_errno("fseeko");
1204 if (use_offset_deltas && m->prev->off != 0) {
1205 err = packhdr(&nh, buf, sizeof(buf),
1206 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1209 nh += packoff(buf + nh,
1210 m->off - m->prev->off);
1211 err = hwrite(packfile, buf, nh, &ctx);
1214 packfile_size += nh;
1216 err = packhdr(&nh, buf, sizeof(buf),
1217 GOT_OBJ_TYPE_REF_DELTA, nd);
1218 err = hwrite(packfile, buf, nh, &ctx);
1221 packfile_size += nh;
1222 err = hwrite(packfile, m->prev->id.sha1,
1223 sizeof(m->prev->id.sha1), &ctx);
1224 packfile_size += sizeof(m->prev->id.sha1);
1228 err = got_deflate_to_file(&outlen, delta_file,
1232 packfile_size += outlen;
1234 got_object_raw_close(raw);
1237 SHA1Final(pack_sha1, &ctx);
1238 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1239 if (n != SHA1_DIGEST_LENGTH)
1240 err = got_ferror(packfile, GOT_ERR_IO);
1241 packfile_size += SHA1_DIGEST_LENGTH;
1242 packfile_size += sizeof(struct got_packfile_hdr);
1243 err = progress_cb(progress_arg, packfile_size, nours,
1244 nmeta, nmeta, nmeta);
1248 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1249 err = got_error_from_errno("fclose");
1251 got_object_raw_close(raw);
1255 const struct got_error *
1256 got_pack_create(uint8_t *packsha1, FILE *packfile,
1257 struct got_object_id **theirs, int ntheirs,
1258 struct got_object_id **ours, int nours,
1259 struct got_repository *repo, int loose_obj_only, int allow_empty,
1260 got_pack_progress_cb progress_cb, void *progress_arg,
1261 got_cancel_cb cancel_cb, void *cancel_arg)
1263 const struct got_error *err;
1264 struct got_pack_meta **meta;
1267 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1268 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1272 if (nmeta == 0 && !allow_empty) {
1273 err = got_error(GOT_ERR_CANNOT_PACK);
1277 err = pick_deltas(meta, nmeta, nours, repo,
1278 progress_cb, progress_arg, cancel_cb, cancel_arg);
1283 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1284 progress_cb, progress_arg, cancel_cb, cancel_arg);
1288 free_nmeta(meta, nmeta);