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 #include "got_error.h"
32 #include "got_cancel.h"
33 #include "got_object.h"
35 #include "got_reference.h"
36 #include "got_repository_admin.h"
37 #include "got_opentemp.h"
39 #include "got_lib_deltify.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_idset.h"
43 #include "got_lib_object_cache.h"
44 #include "got_lib_deflate.h"
45 #include "got_lib_pack.h"
46 #include "got_lib_privsep.h"
47 #include "got_lib_repository.h"
50 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
53 struct got_pack_meta {
54 struct got_object_id id;
60 /* The best delta we picked */
61 struct got_pack_meta *head;
62 struct got_pack_meta *prev;
63 struct got_delta_instruction *deltas;
67 /* Only used for delta window */
68 struct got_delta_table *dtab;
70 /* Only used for writing offset deltas */
74 struct got_pack_metavec {
75 struct got_pack_meta **meta;
80 static const struct got_error *
81 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
82 const char *path, int obj_type, time_t mtime)
84 const struct got_error *err = NULL;
85 struct got_pack_meta *m;
89 m = calloc(1, sizeof(*m));
91 return got_error_from_errno("calloc");
93 memcpy(&m->id, id, sizeof(m->id));
95 m->path = strdup(path);
96 if (m->path == NULL) {
97 err = got_error_from_errno("strdup");
102 m->obj_type = obj_type;
109 clear_meta(struct got_pack_meta *meta)
120 free_nmeta(struct got_pack_meta **meta, int nmeta)
124 for (i = 0; i < nmeta; i++)
130 delta_order_cmp(const void *pa, const void *pb)
132 struct got_pack_meta *a, *b;
135 a = *(struct got_pack_meta **)pa;
136 b = *(struct got_pack_meta **)pb;
138 if (a->obj_type != b->obj_type)
139 return a->obj_type - b->obj_type;
140 cmp = strcmp(a->path, b->path);
143 if (a->mtime != b->mtime)
144 return a->mtime - b->mtime;
145 return got_object_id_cmp(&a->id, &b->id);
149 delta_size(struct got_delta_instruction *deltas, int ndeltas)
152 for (i = 0; i < ndeltas; i++) {
154 size += GOT_DELTA_SIZE_SHIFT;
156 size += deltas[i].len + 1;
162 static const struct got_error *
163 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
164 struct got_repository *repo,
165 got_pack_progress_cb progress_cb, void *progress_arg,
166 got_cancel_cb cancel_cb, void *cancel_arg)
168 const struct got_error *err = NULL;
169 struct got_pack_meta *m = NULL, *base = NULL;
170 struct got_raw_object *raw = NULL, *base_raw = NULL;
171 struct got_delta_instruction *deltas;
172 int i, j, size, ndeltas, best;
173 const int max_base_candidates = 10;
176 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
177 for (i = 0; i < nmeta; i++) {
179 err = (*cancel_cb)(cancel_arg);
184 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
192 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
193 m->obj_type == GOT_OBJ_TYPE_TAG)
196 err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
201 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
202 raw->size + raw->hdrlen);
206 if (i > max_base_candidates) {
207 struct got_pack_meta *n = NULL;
208 n = meta[i - (max_base_candidates + 1)];
209 got_deltify_free(n->dtab);
214 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
216 err = (*cancel_cb)(cancel_arg);
221 /* long chains make unpacking slow, avoid such bases */
222 if (base->nchain >= 128 ||
223 base->obj_type != m->obj_type)
226 err = got_object_raw_open(&base_raw, &outfd, repo,
230 err = got_deltify(&deltas, &ndeltas,
231 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
232 base->dtab, base_raw->f, base_raw->hdrlen,
233 base_raw->size + base_raw->hdrlen);
234 got_object_raw_close(base_raw);
239 size = delta_size(deltas, ndeltas);
240 if (size + 32 < best){
242 * if we already picked a best delta,
248 m->ndeltas = ndeltas;
249 m->nchain = base->nchain + 1;
251 m->head = base->head;
261 got_object_raw_close(raw);
265 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
266 got_deltify_free(meta[i]->dtab);
267 meta[i]->dtab = NULL;
270 got_object_raw_close(raw);
272 got_object_raw_close(base_raw);
273 if (outfd != -1 && close(outfd) == -1 && err == NULL)
274 err = got_error_from_errno("close");
278 static const struct got_error *
279 search_packidx(int *found, struct got_object_id *id,
280 struct got_repository *repo)
282 const struct got_error *err = NULL;
283 struct got_packidx *packidx = NULL;
288 err = got_repo_search_packidx(&packidx, &idx, repo, id);
290 *found = 1; /* object is already packed */
291 else if (err->code == GOT_ERR_NO_OBJ)
296 static const int obj_types[] = {
302 GOT_OBJ_TYPE_OFFSET_DELTA,
303 GOT_OBJ_TYPE_REF_DELTA
306 static const struct got_error *
307 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
308 struct got_object_id *id, const char *path, int obj_type,
309 time_t mtime, int loose_obj_only, struct got_repository *repo)
311 const struct got_error *err;
312 struct got_pack_meta *m;
314 if (loose_obj_only) {
316 err = search_packidx(&is_packed, id, repo);
323 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
330 err = alloc_meta(&m, id, path, obj_type, mtime);
334 if (v->nmeta == v->metasz){
335 size_t newsize = 2 * v->metasz;
336 struct got_pack_meta **new;
337 new = reallocarray(v->meta, newsize, sizeof(*new));
339 err = got_error_from_errno("reallocarray");
350 v->meta[v->nmeta++] = m;
355 static const struct got_error *
356 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
357 struct got_object_idset *idset, struct got_object_id *tree_id,
358 const char *dpath, time_t mtime, struct got_repository *repo,
359 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
361 const struct got_error *err;
362 struct got_tree_object *tree;
366 err = got_object_open_as_tree(&tree, repo, tree_id);
370 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
371 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
372 struct got_object_id *id = got_tree_entry_get_id(e);
373 mode_t mode = got_tree_entry_get_mode(e);
376 err = (*cancel_cb)(cancel_arg);
381 if (got_object_tree_entry_is_submodule(e) ||
382 got_object_idset_contains(idset, id))
385 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
386 got_tree_entry_get_name(e)) == -1) {
387 err = got_error_from_errno("asprintf");
392 struct got_object_qid *qid;
393 err = got_object_qid_alloc(&qid, id);
396 STAILQ_INSERT_TAIL(ids, qid, entry);
397 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
398 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
399 mtime, loose_obj_only, repo);
407 got_object_tree_close(tree);
412 static const struct got_error *
413 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
414 struct got_object_id *tree_id, const char *dpath, time_t mtime,
415 int loose_obj_only, struct got_repository *repo,
416 got_cancel_cb cancel_cb, void *cancel_arg)
418 const struct got_error *err = NULL;
419 struct got_object_id_queue tree_ids;
420 struct got_object_qid *qid;
422 if (got_object_idset_contains(idset, tree_id))
425 err = got_object_qid_alloc(&qid, tree_id);
429 STAILQ_INIT(&tree_ids);
430 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
432 while (!STAILQ_EMPTY(&tree_ids)) {
434 err = (*cancel_cb)(cancel_arg);
439 qid = STAILQ_FIRST(&tree_ids);
440 STAILQ_REMOVE_HEAD(&tree_ids, entry);
442 if (got_object_idset_contains(idset, qid->id)) {
443 got_object_qid_free(qid);
447 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
448 mtime, loose_obj_only, repo);
450 got_object_qid_free(qid);
454 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
455 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
456 got_object_qid_free(qid);
461 got_object_id_queue_free(&tree_ids);
465 static const struct got_error *
466 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
467 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
468 got_cancel_cb cancel_cb, void *cancel_arg)
470 const struct got_error *err;
471 struct got_commit_object *commit;
473 if (got_object_idset_contains(idset, id))
476 if (loose_obj_only) {
478 err = search_packidx(&is_packed, id, repo);
485 err = got_object_open_as_commit(&commit, repo, id);
489 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
490 got_object_commit_get_committer_time(commit),
491 loose_obj_only, repo);
495 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
496 "", got_object_commit_get_committer_time(commit),
497 loose_obj_only, repo, cancel_cb, cancel_arg);
499 got_object_commit_close(commit);
503 static const struct got_error *
504 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
505 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
506 got_cancel_cb cancel_cb, void *cancel_arg)
508 const struct got_error *err;
509 struct got_tag_object *tag = NULL;
511 if (got_object_idset_contains(idset, id))
514 if (loose_obj_only) {
516 err = search_packidx(&is_packed, id, repo);
523 err = got_object_open_as_tag(&tag, repo, id);
527 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
528 got_object_tag_get_tagger_time(tag),
529 loose_obj_only, repo);
533 switch (got_object_tag_get_object_type(tag)) {
534 case GOT_OBJ_TYPE_COMMIT:
535 err = load_commit(v, idset,
536 got_object_tag_get_object_id(tag), repo,
537 loose_obj_only, cancel_cb, cancel_arg);
539 case GOT_OBJ_TYPE_TREE:
540 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
541 "", got_object_tag_get_tagger_time(tag),
542 loose_obj_only, repo, cancel_cb, cancel_arg);
549 got_object_tag_close(tag);
553 enum findtwixt_color {
558 static const int findtwixt_colors[] = {
564 static const struct got_error *
565 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
566 int color, struct got_repository *repo)
568 const struct got_error *err;
569 struct got_object_qid *qid;
571 err = got_object_qid_alloc(&qid, id);
575 STAILQ_INSERT_TAIL(ids, qid, entry);
576 qid->data = (void *)&findtwixt_colors[color];
580 static const struct got_error *
581 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
582 struct got_object_id *id, struct got_repository *repo,
583 got_cancel_cb cancel_cb, void *cancel_arg)
585 const struct got_error *err = NULL;
586 struct got_commit_object *commit;
587 const struct got_object_id_queue *parents;
588 struct got_object_id_queue ids;
589 struct got_object_qid *qid;
593 err = got_object_qid_alloc(&qid, id);
596 STAILQ_INSERT_HEAD(&ids, qid, entry);
598 while (!STAILQ_EMPTY(&ids)) {
600 err = (*cancel_cb)(cancel_arg);
605 qid = STAILQ_FIRST(&ids);
606 STAILQ_REMOVE_HEAD(&ids, entry);
608 if (got_object_idset_contains(drop, qid->id)) {
609 got_object_qid_free(qid);
613 err = got_object_idset_add(drop, qid->id,
614 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
616 got_object_qid_free(qid);
620 if (!got_object_idset_contains(keep, qid->id)) {
621 got_object_qid_free(qid);
625 err = got_object_open_as_commit(&commit, repo, qid->id);
626 got_object_qid_free(qid);
630 parents = got_object_commit_get_parent_ids(commit);
632 err = got_object_id_queue_copy(parents, &ids);
634 got_object_commit_close(commit);
638 got_object_commit_close(commit);
641 got_object_id_queue_free(&ids);
645 struct append_id_arg {
646 struct got_object_id **array;
650 static const struct got_error *
651 append_id(struct got_object_id *id, void *data, void *arg)
653 struct append_id_arg *a = arg;
655 a->array[a->idx] = got_object_id_dup(id);
656 if (a->array[a->idx] == NULL)
657 return got_error_from_errno("got_object_id_dup");
663 static const struct got_error *
664 findtwixt(struct got_object_id ***res, int *nres,
665 struct got_object_id **head, int nhead,
666 struct got_object_id **tail, int ntail,
667 struct got_repository *repo,
668 got_cancel_cb cancel_cb, void *cancel_arg)
670 const struct got_error *err = NULL;
671 struct got_object_id_queue ids;
672 struct got_object_idset *keep, *drop;
673 struct got_object_qid *qid;
674 int i, ncolor, nkeep, obj_type;
680 keep = got_object_idset_alloc();
682 return got_error_from_errno("got_object_idset_alloc");
684 drop = got_object_idset_alloc();
686 err = got_error_from_errno("got_object_idset_alloc");
690 for (i = 0; i < nhead; i++) {
691 struct got_object_id *id = head[i];
694 err = got_object_get_type(&obj_type, repo, id);
697 if (obj_type != GOT_OBJ_TYPE_COMMIT)
699 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
703 for (i = 0; i < ntail; i++) {
704 struct got_object_id *id = tail[i];
707 err = got_object_get_type(&obj_type, repo, id);
710 if (obj_type != GOT_OBJ_TYPE_COMMIT)
712 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
717 while (!STAILQ_EMPTY(&ids)) {
719 qid = STAILQ_FIRST(&ids);
720 qcolor = *((int *)qid->data);
722 if (got_object_idset_contains(drop, qid->id))
724 else if (got_object_idset_contains(keep, qid->id))
727 ncolor = COLOR_BLANK;
729 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
730 qcolor == COLOR_KEEP)) {
731 STAILQ_REMOVE_HEAD(&ids, entry);
732 got_object_qid_free(qid);
736 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
737 err = drop_commit(keep, drop, qid->id, repo,
738 cancel_cb, cancel_arg);
741 } else if (ncolor == COLOR_BLANK) {
742 struct got_commit_object *commit;
743 struct got_object_id *id;
744 const struct got_object_id_queue *parents;
745 struct got_object_qid *pid;
747 id = got_object_id_dup(qid->id);
749 err = got_error_from_errno("got_object_id_dup");
752 if (qcolor == COLOR_KEEP)
753 err = got_object_idset_add(keep, id,
754 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
756 err = got_object_idset_add(drop, id,
757 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
763 err = got_object_open_as_commit(&commit, repo, id);
768 parents = got_object_commit_get_parent_ids(commit);
770 STAILQ_FOREACH(pid, parents, entry) {
771 err = queue_commit_id(&ids, pid->id,
779 got_object_commit_close(commit);
782 /* should not happen */
783 err = got_error_fmt(GOT_ERR_NOT_IMPL,
784 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
788 STAILQ_REMOVE_HEAD(&ids, entry);
789 got_object_qid_free(qid);
792 nkeep = got_object_idset_num_elements(keep);
794 struct append_id_arg arg;
795 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
796 if (arg.array == NULL) {
797 err = got_error_from_errno("calloc");
801 err = got_object_idset_for_each(keep, append_id, &arg);
810 got_object_idset_free(keep);
811 got_object_idset_free(drop);
812 got_object_id_queue_free(&ids);
816 static const struct got_error *
817 read_meta(struct got_pack_meta ***meta, int *nmeta,
818 struct got_object_id **theirs, int ntheirs,
819 struct got_object_id **ours, int nours, struct got_repository *repo,
820 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
821 got_cancel_cb cancel_cb, void *cancel_arg)
823 const struct got_error *err = NULL;
824 struct got_object_id **ids = NULL;
825 struct got_object_idset *idset;
826 int i, nobj = 0, obj_type;
827 struct got_pack_metavec v;
832 idset = got_object_idset_alloc();
834 return got_error_from_errno("got_object_idset_alloc");
838 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
839 if (v.meta == NULL) {
840 err = got_error_from_errno("calloc");
844 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
845 cancel_cb, cancel_arg);
846 if (err || nobj == 0)
849 for (i = 0; i < ntheirs; i++) {
850 struct got_object_id *id = theirs[i];
853 err = got_object_get_type(&obj_type, repo, id);
856 if (obj_type != GOT_OBJ_TYPE_COMMIT)
858 err = load_commit(NULL, idset, id, repo,
859 loose_obj_only, cancel_cb, cancel_arg);
863 err = progress_cb(progress_arg, 0L, nours,
870 for (i = 0; i < ntheirs; i++) {
871 struct got_object_id *id = theirs[i];
875 cached_type = got_object_idset_get(idset, id);
876 if (cached_type == NULL) {
877 err = got_object_get_type(&obj_type, repo, id);
881 obj_type = *cached_type;
882 if (obj_type != GOT_OBJ_TYPE_TAG)
884 err = load_tag(NULL, idset, id, repo,
885 loose_obj_only, cancel_cb, cancel_arg);
889 err = progress_cb(progress_arg, 0L, nours,
896 for (i = 0; i < nobj; i++) {
897 err = load_commit(&v, idset, ids[i], repo,
898 loose_obj_only, cancel_cb, cancel_arg);
902 err = progress_cb(progress_arg, 0L, nours,
909 for (i = 0; i < nours; i++) {
910 struct got_object_id *id = ours[i];
914 cached_type = got_object_idset_get(idset, id);
915 if (cached_type == NULL) {
916 err = got_object_get_type(&obj_type, repo, id);
920 obj_type = *cached_type;
921 if (obj_type != GOT_OBJ_TYPE_TAG)
923 err = load_tag(&v, idset, id, repo,
924 loose_obj_only, cancel_cb, cancel_arg);
928 err = progress_cb(progress_arg, 0L, nours,
936 for (i = 0; i < nobj; i++) {
940 got_object_idset_free(idset);
950 const struct got_error *
951 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
955 SHA1Update(ctx, buf, len);
956 n = fwrite(buf, 1, len, f);
958 return got_ferror(f, GOT_ERR_IO);
963 putbe32(char *b, uint32_t n)
972 write_order_cmp(const void *pa, const void *pb)
974 struct got_pack_meta *a, *b, *ahd, *bhd;
976 a = *(struct got_pack_meta **)pa;
977 b = *(struct got_pack_meta **)pb;
978 ahd = (a->head == NULL) ? a : a->head;
979 bhd = (b->head == NULL) ? b : b->head;
980 if (ahd->mtime != bhd->mtime)
981 return bhd->mtime - ahd->mtime;
983 return (uintptr_t)bhd - (uintptr_t)ahd;
984 if (a->nchain != b->nchain)
985 return a->nchain - b->nchain;
986 return a->mtime - b->mtime;
989 static const struct got_error *
990 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
996 hdr[0] = obj_type << 4;
999 for (i = 1; len != 0; i++){
1001 return got_error(GOT_ERR_NO_SPACE);
1002 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1003 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1004 len >>= GOT_DELTA_SIZE_SHIFT;
1011 static const struct got_error *
1012 encodedelta(struct got_pack_meta *m, struct got_raw_object *o,
1013 off_t base_size, FILE *f)
1015 unsigned char buf[16], *bp;
1019 struct got_delta_instruction *d;
1021 /* base object size */
1022 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1023 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1024 for (i = 1; n > 0; i++) {
1025 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1026 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1027 n >>= GOT_DELTA_SIZE_SHIFT;
1029 w = fwrite(buf, 1, i, f);
1031 return got_ferror(f, GOT_ERR_IO);
1033 /* target object size */
1034 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1035 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1036 for (i = 1; n > 0; i++) {
1037 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1038 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1039 n >>= GOT_DELTA_SIZE_SHIFT;
1041 w = fwrite(buf, 1, i, f);
1043 return got_ferror(f, GOT_ERR_IO);
1045 for (j = 0; j < m->ndeltas; j++) {
1050 buf[0] = GOT_DELTA_BASE_COPY;
1051 for (i = 0; i < 4; i++) {
1052 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1061 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1062 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1063 for (i = 0; i < 3 && n > 0; i++) {
1064 buf[0] |= 1 << (i + 4);
1069 w = fwrite(buf, 1, bp - buf, f);
1071 return got_ferror(f, GOT_ERR_IO);
1075 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1076 return got_error_from_errno("fseeko");
1078 while (n != d->len) {
1079 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1080 w = fwrite(buf, 1, 1, f);
1082 return got_ferror(f, GOT_ERR_IO);
1083 r = fread(content, 1, buf[0], o->f);
1085 return got_ferror(o->f, GOT_ERR_IO);
1086 w = fwrite(content, 1, buf[0], f);
1088 return got_ferror(f, GOT_ERR_IO);
1098 packoff(char *hdr, off_t off)
1103 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1104 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1105 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1106 GOT_DELTA_SIZE_MORE;
1111 hdr[j++] = rbuf[--i];
1115 static const struct got_error *
1116 genpack(uint8_t *pack_sha1, FILE *packfile,
1117 struct got_pack_meta **meta, int nmeta, int nours,
1118 int use_offset_deltas, struct got_repository *repo,
1119 got_pack_progress_cb progress_cb, void *progress_arg,
1120 got_cancel_cb cancel_cb, void *cancel_arg)
1122 const struct got_error *err = NULL;
1126 struct got_pack_meta *m;
1127 struct got_raw_object *raw = NULL;
1128 FILE *delta_file = NULL;
1131 struct got_deflate_checksum csum;
1132 off_t packfile_size = 0;
1136 csum.output_sha1 = &ctx;
1137 csum.output_crc = NULL;
1139 err = hwrite(packfile, "PACK", 4, &ctx);
1142 putbe32(buf, GOT_PACKFILE_VERSION);
1143 err = hwrite(packfile, buf, 4, &ctx);
1146 putbe32(buf, nmeta);
1147 err = hwrite(packfile, buf, 4, &ctx);
1150 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1151 for (i = 0; i < nmeta; i++) {
1153 err = progress_cb(progress_arg, packfile_size, nours,
1159 m->off = ftello(packfile);
1160 err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
1163 if (m->deltas == NULL) {
1164 err = packhdr(&nh, buf, sizeof(buf),
1165 m->obj_type, raw->size);
1168 err = hwrite(packfile, buf, nh, &ctx);
1171 packfile_size += nh;
1172 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1173 err = got_error_from_errno("fseeko");
1176 err = got_deflate_to_file(&outlen, raw->f, packfile,
1180 packfile_size += outlen;
1182 if (delta_file == NULL) {
1183 delta_file = got_opentemp();
1184 if (delta_file == NULL) {
1185 err = got_error_from_errno(
1190 if (ftruncate(fileno(delta_file), 0L) == -1) {
1191 err = got_error_from_errno("ftruncate");
1194 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1195 err = got_error_from_errno("fseeko");
1198 err = encodedelta(m, raw, m->prev->size, delta_file);
1201 nd = ftello(delta_file);
1202 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1203 err = got_error_from_errno("fseeko");
1206 if (use_offset_deltas && m->prev->off != 0) {
1207 err = packhdr(&nh, buf, sizeof(buf),
1208 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1211 nh += packoff(buf + nh,
1212 m->off - m->prev->off);
1213 err = hwrite(packfile, buf, nh, &ctx);
1216 packfile_size += nh;
1218 err = packhdr(&nh, buf, sizeof(buf),
1219 GOT_OBJ_TYPE_REF_DELTA, nd);
1220 err = hwrite(packfile, buf, nh, &ctx);
1223 packfile_size += nh;
1224 err = hwrite(packfile, m->prev->id.sha1,
1225 sizeof(m->prev->id.sha1), &ctx);
1226 packfile_size += sizeof(m->prev->id.sha1);
1230 err = got_deflate_to_file(&outlen, delta_file,
1234 packfile_size += outlen;
1236 got_object_raw_close(raw);
1239 SHA1Final(pack_sha1, &ctx);
1240 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1241 if (n != SHA1_DIGEST_LENGTH)
1242 err = got_ferror(packfile, GOT_ERR_IO);
1243 packfile_size += SHA1_DIGEST_LENGTH;
1244 packfile_size += sizeof(struct got_packfile_hdr);
1245 err = progress_cb(progress_arg, packfile_size, nours,
1246 nmeta, nmeta, nmeta);
1250 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1251 err = got_error_from_errno("fclose");
1253 got_object_raw_close(raw);
1254 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1255 err = got_error_from_errno("close");
1259 const struct got_error *
1260 got_pack_create(uint8_t *packsha1, FILE *packfile,
1261 struct got_object_id **theirs, int ntheirs,
1262 struct got_object_id **ours, int nours,
1263 struct got_repository *repo, int loose_obj_only, int allow_empty,
1264 got_pack_progress_cb progress_cb, void *progress_arg,
1265 got_cancel_cb cancel_cb, void *cancel_arg)
1267 const struct got_error *err;
1268 struct got_pack_meta **meta;
1271 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1272 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1276 if (nmeta == 0 && !allow_empty) {
1277 err = got_error(GOT_ERR_CANNOT_PACK);
1281 err = pick_deltas(meta, nmeta, nours, repo,
1282 progress_cb, progress_arg, cancel_cb, cancel_arg);
1287 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1288 progress_cb, progress_arg, cancel_cb, cancel_arg);
1292 free_nmeta(meta, nmeta);