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;
61 /* The best delta we picked */
62 struct got_pack_meta *head;
63 struct got_pack_meta *prev;
64 struct got_delta_instruction *deltas;
68 /* Only used for delta window */
69 struct got_delta_table *dtab;
71 /* Only used for writing offset deltas */
75 struct got_pack_metavec {
76 struct got_pack_meta **meta;
81 static const struct got_error *
82 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
83 const char *path, int obj_type, time_t mtime)
85 const struct got_error *err = NULL;
86 struct got_pack_meta *m;
90 m = calloc(1, sizeof(*m));
92 return got_error_from_errno("calloc");
94 memcpy(&m->id, id, sizeof(m->id));
96 m->path = strdup(path);
97 if (m->path == NULL) {
98 err = got_error_from_errno("strdup");
103 m->obj_type = obj_type;
110 clear_meta(struct got_pack_meta *meta)
121 free_nmeta(struct got_pack_meta **meta, int nmeta)
125 for (i = 0; i < nmeta; i++)
131 delta_order_cmp(const void *pa, const void *pb)
133 struct got_pack_meta *a, *b;
136 a = *(struct got_pack_meta **)pa;
137 b = *(struct got_pack_meta **)pb;
139 if (a->obj_type != b->obj_type)
140 return a->obj_type - b->obj_type;
141 cmp = strcmp(a->path, b->path);
144 if (a->mtime != b->mtime)
145 return a->mtime - b->mtime;
146 return got_object_id_cmp(&a->id, &b->id);
150 delta_size(struct got_delta_instruction *deltas, int ndeltas)
153 for (i = 0; i < ndeltas; i++) {
155 size += GOT_DELTA_SIZE_SHIFT;
157 size += deltas[i].len + 1;
163 static const struct got_error *
164 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
165 struct got_repository *repo,
166 got_pack_progress_cb progress_cb, void *progress_arg,
167 got_cancel_cb cancel_cb, void *cancel_arg)
169 const struct got_error *err = NULL;
170 struct got_pack_meta *m = NULL, *base = NULL;
171 struct got_raw_object *raw = NULL, *base_raw = NULL;
172 struct got_delta_instruction *deltas;
173 int i, j, size, ndeltas, best;
174 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, repo, &m->id, 8192);
200 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
201 raw->size + raw->hdrlen);
205 if (i > max_base_candidates) {
206 struct got_pack_meta *n = NULL;
207 n = meta[i - (max_base_candidates + 1)];
208 got_deltify_free(n->dtab);
213 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
215 err = (*cancel_cb)(cancel_arg);
220 /* long chains make unpacking slow, avoid such bases */
221 if (base->nchain >= 128 ||
222 base->obj_type != m->obj_type)
225 err = got_object_raw_open(&base_raw, repo, &base->id,
229 err = got_deltify(&deltas, &ndeltas,
230 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
231 base->dtab, base_raw->f, base_raw->hdrlen,
232 base_raw->size + base_raw->hdrlen);
233 got_object_raw_close(base_raw);
238 size = delta_size(deltas, ndeltas);
239 if (size + 32 < best){
241 * if we already picked a best delta,
247 m->ndeltas = ndeltas;
248 m->nchain = base->nchain + 1;
250 m->head = base->head;
260 got_object_raw_close(raw);
264 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
265 got_deltify_free(meta[i]->dtab);
266 meta[i]->dtab = NULL;
269 got_object_raw_close(raw);
271 got_object_raw_close(base_raw);
275 static const struct got_error *
276 search_packidx(int *found, struct got_object_id *id,
277 struct got_repository *repo)
279 const struct got_error *err = NULL;
280 struct got_packidx *packidx = NULL;
285 err = got_repo_search_packidx(&packidx, &idx, repo, id);
287 *found = 1; /* object is already packed */
288 else if (err->code == GOT_ERR_NO_OBJ)
293 static const int obj_types[] = {
299 GOT_OBJ_TYPE_OFFSET_DELTA,
300 GOT_OBJ_TYPE_REF_DELTA
303 static const struct got_error *
304 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
305 struct got_object_id *id, const char *path, int obj_type,
306 time_t mtime, int loose_obj_only, struct got_repository *repo)
308 const struct got_error *err;
309 struct got_pack_meta *m;
311 if (loose_obj_only) {
313 err = search_packidx(&is_packed, id, repo);
320 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
327 err = alloc_meta(&m, id, path, obj_type, mtime);
331 if (v->nmeta == v->metasz){
332 size_t newsize = 2 * v->metasz;
333 struct got_pack_meta **new;
334 new = reallocarray(v->meta, newsize, sizeof(*new));
336 err = got_error_from_errno("reallocarray");
347 v->meta[v->nmeta++] = m;
352 static const struct got_error *
353 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
354 struct got_object_idset *idset, struct got_object_id *tree_id,
355 const char *dpath, time_t mtime, struct got_repository *repo,
356 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
358 const struct got_error *err;
359 struct got_tree_object *tree;
363 err = got_object_open_as_tree(&tree, repo, tree_id);
367 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
368 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
369 struct got_object_id *id = got_tree_entry_get_id(e);
370 mode_t mode = got_tree_entry_get_mode(e);
373 err = (*cancel_cb)(cancel_arg);
378 if (got_object_tree_entry_is_submodule(e) ||
379 got_object_idset_contains(idset, id))
382 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
383 got_tree_entry_get_name(e)) == -1) {
384 err = got_error_from_errno("asprintf");
389 struct got_object_qid *qid;
390 err = got_object_qid_alloc(&qid, id);
393 STAILQ_INSERT_TAIL(ids, qid, entry);
394 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
395 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
396 mtime, loose_obj_only, repo);
404 got_object_tree_close(tree);
409 static const struct got_error *
410 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
411 struct got_object_id *tree_id, const char *dpath, time_t mtime,
412 int loose_obj_only, struct got_repository *repo,
413 got_cancel_cb cancel_cb, void *cancel_arg)
415 const struct got_error *err = NULL;
416 struct got_object_id_queue tree_ids;
417 struct got_object_qid *qid;
419 if (got_object_idset_contains(idset, tree_id))
422 err = got_object_qid_alloc(&qid, tree_id);
426 STAILQ_INIT(&tree_ids);
427 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
429 while (!STAILQ_EMPTY(&tree_ids)) {
431 err = (*cancel_cb)(cancel_arg);
436 qid = STAILQ_FIRST(&tree_ids);
437 STAILQ_REMOVE_HEAD(&tree_ids, entry);
439 if (got_object_idset_contains(idset, qid->id)) {
440 got_object_qid_free(qid);
444 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
445 mtime, loose_obj_only, repo);
447 got_object_qid_free(qid);
451 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
452 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
453 got_object_qid_free(qid);
458 got_object_id_queue_free(&tree_ids);
462 static const struct got_error *
463 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
464 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
465 got_cancel_cb cancel_cb, void *cancel_arg)
467 const struct got_error *err;
468 struct got_commit_object *commit;
470 if (got_object_idset_contains(idset, id))
473 if (loose_obj_only) {
475 err = search_packidx(&is_packed, id, repo);
482 err = got_object_open_as_commit(&commit, repo, id);
486 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
487 got_object_commit_get_committer_time(commit),
488 loose_obj_only, repo);
492 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
493 "", got_object_commit_get_committer_time(commit),
494 loose_obj_only, repo, cancel_cb, cancel_arg);
496 got_object_commit_close(commit);
500 static const struct got_error *
501 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
502 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
503 got_cancel_cb cancel_cb, void *cancel_arg)
505 const struct got_error *err;
506 struct got_tag_object *tag = NULL;
508 if (got_object_idset_contains(idset, id))
511 if (loose_obj_only) {
513 err = search_packidx(&is_packed, id, repo);
520 err = got_object_open_as_tag(&tag, repo, id);
524 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
525 got_object_tag_get_tagger_time(tag),
526 loose_obj_only, repo);
530 switch (got_object_tag_get_object_type(tag)) {
531 case GOT_OBJ_TYPE_COMMIT:
532 err = load_commit(v, idset,
533 got_object_tag_get_object_id(tag), repo,
534 loose_obj_only, cancel_cb, cancel_arg);
536 case GOT_OBJ_TYPE_TREE:
537 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
538 "", got_object_tag_get_tagger_time(tag),
539 loose_obj_only, repo, cancel_cb, cancel_arg);
546 got_object_tag_close(tag);
550 enum findtwixt_color {
555 static const int findtwixt_colors[] = {
561 static const struct got_error *
562 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
563 int color, struct got_repository *repo)
565 const struct got_error *err;
566 struct got_object_qid *qid;
568 err = got_object_qid_alloc(&qid, id);
572 STAILQ_INSERT_TAIL(ids, qid, entry);
573 qid->data = (void *)&findtwixt_colors[color];
577 static const struct got_error *
578 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
579 struct got_object_id *id, struct got_repository *repo,
580 got_cancel_cb cancel_cb, void *cancel_arg)
582 const struct got_error *err = NULL;
583 struct got_commit_object *commit;
584 const struct got_object_id_queue *parents;
585 struct got_object_id_queue ids;
586 struct got_object_qid *qid;
590 err = got_object_qid_alloc(&qid, id);
593 STAILQ_INSERT_HEAD(&ids, qid, entry);
595 while (!STAILQ_EMPTY(&ids)) {
597 err = (*cancel_cb)(cancel_arg);
602 qid = STAILQ_FIRST(&ids);
603 STAILQ_REMOVE_HEAD(&ids, entry);
605 if (got_object_idset_contains(drop, qid->id)) {
606 got_object_qid_free(qid);
610 err = got_object_idset_add(drop, qid->id,
611 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
613 got_object_qid_free(qid);
617 if (!got_object_idset_contains(keep, qid->id)) {
618 got_object_qid_free(qid);
622 err = got_object_open_as_commit(&commit, repo, qid->id);
623 got_object_qid_free(qid);
627 parents = got_object_commit_get_parent_ids(commit);
629 err = got_object_id_queue_copy(parents, &ids);
631 got_object_commit_close(commit);
635 got_object_commit_close(commit);
638 got_object_id_queue_free(&ids);
642 struct append_id_arg {
643 struct got_object_id **array;
647 static const struct got_error *
648 append_id(struct got_object_id *id, void *data, void *arg)
650 struct append_id_arg *a = arg;
652 a->array[a->idx] = got_object_id_dup(id);
653 if (a->array[a->idx] == NULL)
654 return got_error_from_errno("got_object_id_dup");
660 static const struct got_error *
661 findtwixt(struct got_object_id ***res, int *nres,
662 struct got_object_id **head, int nhead,
663 struct got_object_id **tail, int ntail,
664 struct got_repository *repo,
665 got_cancel_cb cancel_cb, void *cancel_arg)
667 const struct got_error *err = NULL;
668 struct got_object_id_queue ids;
669 struct got_object_idset *keep, *drop;
670 struct got_object_qid *qid;
671 int i, ncolor, nkeep, obj_type;
677 keep = got_object_idset_alloc();
679 return got_error_from_errno("got_object_idset_alloc");
681 drop = got_object_idset_alloc();
683 err = got_error_from_errno("got_object_idset_alloc");
687 for (i = 0; i < nhead; i++) {
688 struct got_object_id *id = head[i];
691 err = got_object_get_type(&obj_type, repo, id);
694 if (obj_type != GOT_OBJ_TYPE_COMMIT)
696 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
700 for (i = 0; i < ntail; i++) {
701 struct got_object_id *id = tail[i];
704 err = got_object_get_type(&obj_type, repo, id);
707 if (obj_type != GOT_OBJ_TYPE_COMMIT)
709 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
714 while (!STAILQ_EMPTY(&ids)) {
716 qid = STAILQ_FIRST(&ids);
717 qcolor = *((int *)qid->data);
719 if (got_object_idset_contains(drop, qid->id))
721 else if (got_object_idset_contains(keep, qid->id))
724 ncolor = COLOR_BLANK;
726 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
727 qcolor == COLOR_KEEP)) {
728 STAILQ_REMOVE_HEAD(&ids, entry);
729 got_object_qid_free(qid);
733 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
734 err = drop_commit(keep, drop, qid->id, repo,
735 cancel_cb, cancel_arg);
738 } else if (ncolor == COLOR_BLANK) {
739 struct got_commit_object *commit;
740 struct got_object_id *id;
741 const struct got_object_id_queue *parents;
742 struct got_object_qid *pid;
744 id = got_object_id_dup(qid->id);
746 err = got_error_from_errno("got_object_id_dup");
749 if (qcolor == COLOR_KEEP)
750 err = got_object_idset_add(keep, id,
751 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
753 err = got_object_idset_add(drop, id,
754 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
760 err = got_object_open_as_commit(&commit, repo, id);
765 parents = got_object_commit_get_parent_ids(commit);
767 STAILQ_FOREACH(pid, parents, entry) {
768 err = queue_commit_id(&ids, pid->id,
776 got_object_commit_close(commit);
779 /* should not happen */
780 err = got_error_fmt(GOT_ERR_NOT_IMPL,
781 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
785 STAILQ_REMOVE_HEAD(&ids, entry);
786 got_object_qid_free(qid);
789 nkeep = got_object_idset_num_elements(keep);
791 struct append_id_arg arg;
792 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
793 if (arg.array == NULL) {
794 err = got_error_from_errno("calloc");
798 err = got_object_idset_for_each(keep, append_id, &arg);
807 got_object_idset_free(keep);
808 got_object_idset_free(drop);
809 got_object_id_queue_free(&ids);
813 static const struct got_error *
814 read_meta(struct got_pack_meta ***meta, int *nmeta,
815 struct got_object_id **theirs, int ntheirs,
816 struct got_object_id **ours, int nours, struct got_repository *repo,
817 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
818 got_cancel_cb cancel_cb, void *cancel_arg)
820 const struct got_error *err = NULL;
821 struct got_object_id **ids = NULL;
822 struct got_object_idset *idset;
823 int i, nobj = 0, obj_type;
824 struct got_pack_metavec v;
829 idset = got_object_idset_alloc();
831 return got_error_from_errno("got_object_idset_alloc");
835 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
836 if (v.meta == NULL) {
837 err = got_error_from_errno("calloc");
841 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
842 cancel_cb, cancel_arg);
843 if (err || nobj == 0)
846 for (i = 0; i < ntheirs; i++) {
847 struct got_object_id *id = theirs[i];
850 err = got_object_get_type(&obj_type, repo, id);
853 if (obj_type != GOT_OBJ_TYPE_COMMIT)
855 err = load_commit(NULL, idset, id, repo,
856 loose_obj_only, cancel_cb, cancel_arg);
860 err = progress_cb(progress_arg, 0L, nours,
867 for (i = 0; i < ntheirs; i++) {
868 struct got_object_id *id = theirs[i];
872 cached_type = got_object_idset_get(idset, id);
873 if (cached_type == NULL) {
874 err = got_object_get_type(&obj_type, repo, id);
878 obj_type = *cached_type;
879 if (obj_type != GOT_OBJ_TYPE_TAG)
881 err = load_tag(NULL, idset, id, repo,
882 loose_obj_only, cancel_cb, cancel_arg);
886 err = progress_cb(progress_arg, 0L, nours,
893 for (i = 0; i < nobj; i++) {
894 err = load_commit(&v, idset, ids[i], repo,
895 loose_obj_only, cancel_cb, cancel_arg);
899 err = progress_cb(progress_arg, 0L, nours,
906 for (i = 0; i < nours; i++) {
907 struct got_object_id *id = ours[i];
911 cached_type = got_object_idset_get(idset, id);
912 if (cached_type == NULL) {
913 err = got_object_get_type(&obj_type, repo, id);
917 obj_type = *cached_type;
918 if (obj_type != GOT_OBJ_TYPE_TAG)
920 err = load_tag(&v, idset, id, repo,
921 loose_obj_only, cancel_cb, cancel_arg);
925 err = progress_cb(progress_arg, 0L, nours,
933 for (i = 0; i < nobj; i++) {
937 got_object_idset_free(idset);
947 const struct got_error *
948 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
952 SHA1Update(ctx, buf, len);
953 n = fwrite(buf, 1, len, f);
955 return got_ferror(f, GOT_ERR_IO);
960 putbe32(char *b, uint32_t n)
969 write_order_cmp(const void *pa, const void *pb)
971 struct got_pack_meta *a, *b, *ahd, *bhd;
973 a = *(struct got_pack_meta **)pa;
974 b = *(struct got_pack_meta **)pb;
975 ahd = (a->head == NULL) ? a : a->head;
976 bhd = (b->head == NULL) ? b : b->head;
977 if (ahd->mtime != bhd->mtime)
978 return bhd->mtime - ahd->mtime;
980 return (uintptr_t)bhd - (uintptr_t)ahd;
981 if (a->nchain != b->nchain)
982 return a->nchain - b->nchain;
983 return a->mtime - b->mtime;
986 static const struct got_error *
987 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
993 hdr[0] = obj_type << 4;
996 for (i = 1; len != 0; i++){
998 return got_error(GOT_ERR_NO_SPACE);
999 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1000 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1001 len >>= GOT_DELTA_SIZE_SHIFT;
1008 static const struct got_error *
1009 encodedelta(struct got_pack_meta *m, struct got_raw_object *o,
1010 off_t base_size, FILE *f)
1012 unsigned char buf[16], *bp;
1016 struct got_delta_instruction *d;
1018 /* base object size */
1019 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1020 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1021 for (i = 1; n > 0; i++) {
1022 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1023 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1024 n >>= GOT_DELTA_SIZE_SHIFT;
1026 w = fwrite(buf, 1, i, f);
1028 return got_ferror(f, GOT_ERR_IO);
1030 /* target object size */
1031 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1032 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1033 for (i = 1; n > 0; i++) {
1034 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1035 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1036 n >>= GOT_DELTA_SIZE_SHIFT;
1038 w = fwrite(buf, 1, i, f);
1040 return got_ferror(f, GOT_ERR_IO);
1042 for (j = 0; j < m->ndeltas; j++) {
1047 buf[0] = GOT_DELTA_BASE_COPY;
1048 for (i = 0; i < 4; i++) {
1049 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1058 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1059 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1060 for (i = 0; i < 3 && n > 0; i++) {
1061 buf[0] |= 1 << (i + 4);
1066 w = fwrite(buf, 1, bp - buf, f);
1068 return got_ferror(f, GOT_ERR_IO);
1072 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1073 return got_error_from_errno("fseeko");
1075 while (n != d->len) {
1076 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1077 w = fwrite(buf, 1, 1, f);
1079 return got_ferror(f, GOT_ERR_IO);
1080 r = fread(content, 1, buf[0], o->f);
1082 return got_ferror(o->f, GOT_ERR_IO);
1083 w = fwrite(content, 1, buf[0], f);
1085 return got_ferror(f, GOT_ERR_IO);
1095 packoff(char *hdr, off_t off)
1100 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1101 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1102 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1103 GOT_DELTA_SIZE_MORE;
1108 hdr[j++] = rbuf[--i];
1112 static const struct got_error *
1113 genpack(uint8_t *pack_sha1, FILE *packfile,
1114 struct got_pack_meta **meta, int nmeta, int nours,
1115 int use_offset_deltas, struct got_repository *repo,
1116 got_pack_progress_cb progress_cb, void *progress_arg,
1117 got_cancel_cb cancel_cb, void *cancel_arg)
1119 const struct got_error *err = NULL;
1123 struct got_pack_meta *m;
1124 struct got_raw_object *raw = NULL, *base_raw = NULL;
1125 FILE *delta_file = NULL;
1128 struct got_deflate_checksum csum;
1129 off_t packfile_size = 0;
1132 csum.output_sha1 = &ctx;
1133 csum.output_crc = NULL;
1135 err = hwrite(packfile, "PACK", 4, &ctx);
1138 putbe32(buf, GOT_PACKFILE_VERSION);
1139 err = hwrite(packfile, buf, 4, &ctx);
1142 putbe32(buf, nmeta);
1143 err = hwrite(packfile, buf, 4, &ctx);
1146 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1147 for (i = 0; i < nmeta; i++) {
1149 err = progress_cb(progress_arg, packfile_size, nours,
1155 m->off = ftello(packfile);
1156 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1159 if (m->deltas == NULL) {
1160 err = packhdr(&nh, buf, sizeof(buf),
1161 m->obj_type, raw->size);
1164 err = hwrite(packfile, buf, nh, &ctx);
1167 packfile_size += nh;
1168 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1169 err = got_error_from_errno("fseeko");
1172 err = got_deflate_to_file(&outlen, raw->f, packfile,
1176 packfile_size += outlen;
1178 if (delta_file == NULL) {
1179 delta_file = got_opentemp();
1180 if (delta_file == NULL) {
1181 err = got_error_from_errno(
1186 if (ftruncate(fileno(delta_file), 0L) == -1) {
1187 err = got_error_from_errno("ftruncate");
1190 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1191 err = got_error_from_errno("fseeko");
1194 err = got_object_raw_open(&base_raw, repo,
1195 &m->prev->id, 8192);
1198 err = encodedelta(m, raw, base_raw->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 got_object_raw_close(base_raw);
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);
1257 got_object_raw_close(base_raw);
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);