2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
33 #include "got_error.h"
34 #include "got_cancel.h"
35 #include "got_object.h"
37 #include "got_reference.h"
38 #include "got_repository_admin.h"
39 #include "got_opentemp.h"
41 #include "got_lib_deltify.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_idset.h"
45 #include "got_lib_object_cache.h"
46 #include "got_lib_deflate.h"
47 #include "got_lib_pack.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_repository.h"
52 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
55 struct got_pack_meta {
56 struct got_object_id id;
62 /* The best delta we picked */
63 struct got_pack_meta *head;
64 struct got_pack_meta *prev;
65 struct got_delta_instruction *deltas;
69 /* Only used for delta window */
70 struct got_delta_table *dtab;
72 /* Only used for writing offset deltas */
76 struct got_pack_metavec {
77 struct got_pack_meta **meta;
82 static const struct got_error *
83 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
84 const char *path, int obj_type, time_t mtime)
86 const struct got_error *err = NULL;
87 struct got_pack_meta *m;
91 m = calloc(1, sizeof(*m));
93 return got_error_from_errno("calloc");
95 memcpy(&m->id, id, sizeof(m->id));
97 m->path = strdup(path);
98 if (m->path == NULL) {
99 err = got_error_from_errno("strdup");
104 m->obj_type = obj_type;
111 clear_meta(struct got_pack_meta *meta)
122 free_nmeta(struct got_pack_meta **meta, int nmeta)
126 for (i = 0; i < nmeta; i++)
132 delta_order_cmp(const void *pa, const void *pb)
134 struct got_pack_meta *a, *b;
137 a = *(struct got_pack_meta **)pa;
138 b = *(struct got_pack_meta **)pb;
140 if (a->obj_type != b->obj_type)
141 return a->obj_type - b->obj_type;
142 cmp = strcmp(a->path, b->path);
145 if (a->mtime != b->mtime)
146 return a->mtime - b->mtime;
147 return got_object_id_cmp(&a->id, &b->id);
151 delta_size(struct got_delta_instruction *deltas, int ndeltas)
154 for (i = 0; i < ndeltas; i++) {
156 size += GOT_DELTA_SIZE_SHIFT;
158 size += deltas[i].len + 1;
164 static const struct got_error *
165 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
166 struct got_repository *repo,
167 got_pack_progress_cb progress_cb, void *progress_arg,
168 got_cancel_cb cancel_cb, void *cancel_arg)
170 const struct got_error *err = NULL;
171 struct got_pack_meta *m = NULL, *base = NULL;
172 struct got_raw_object *raw = NULL, *base_raw = NULL;
173 struct got_delta_instruction *deltas;
174 int i, j, size, ndeltas, best;
175 const int max_base_candidates = 10;
178 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
179 for (i = 0; i < nmeta; i++) {
181 err = (*cancel_cb)(cancel_arg);
186 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
194 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
195 m->obj_type == GOT_OBJ_TYPE_TAG)
199 outfd = got_opentempfd();
201 err = got_error_from_errno("got_opentempfd");
205 err = got_object_raw_open(&raw, outfd, repo, &m->id, 8192);
208 if (raw->data == NULL)
209 outfd = -1; /* outfd is now raw->f */
212 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
213 raw->size + raw->hdrlen);
217 if (i > max_base_candidates) {
218 struct got_pack_meta *n = NULL;
219 n = meta[i - (max_base_candidates + 1)];
220 got_deltify_free(n->dtab);
225 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
227 err = (*cancel_cb)(cancel_arg);
232 /* long chains make unpacking slow, avoid such bases */
233 if (base->nchain >= 128 ||
234 base->obj_type != m->obj_type)
238 outfd = got_opentempfd();
240 err = got_error_from_errno(
245 err = got_object_raw_open(&base_raw, outfd, repo,
249 if (base_raw->data == NULL)
250 outfd = -1; /* outfd is now base_raw->f */
251 err = got_deltify(&deltas, &ndeltas,
252 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
253 base->dtab, base_raw->f, base_raw->hdrlen,
254 base_raw->size + base_raw->hdrlen);
255 got_object_raw_close(base_raw);
260 size = delta_size(deltas, ndeltas);
261 if (size + 32 < best){
263 * if we already picked a best delta,
269 m->ndeltas = ndeltas;
270 m->nchain = base->nchain + 1;
272 m->head = base->head;
282 got_object_raw_close(raw);
286 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
287 got_deltify_free(meta[i]->dtab);
288 meta[i]->dtab = NULL;
291 got_object_raw_close(raw);
293 got_object_raw_close(base_raw);
294 if (outfd != -1 && close(outfd) == -1 && err == NULL)
295 err = got_error_from_errno("close");
299 static const struct got_error *
300 search_packidx(int *found, struct got_object_id *id,
301 struct got_repository *repo)
303 const struct got_error *err = NULL;
304 struct got_packidx *packidx = NULL;
309 err = got_repo_search_packidx(&packidx, &idx, repo, id);
311 *found = 1; /* object is already packed */
312 else if (err->code == GOT_ERR_NO_OBJ)
317 static const int obj_types[] = {
323 GOT_OBJ_TYPE_OFFSET_DELTA,
324 GOT_OBJ_TYPE_REF_DELTA
327 static const struct got_error *
328 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
329 struct got_object_id *id, const char *path, int obj_type,
330 time_t mtime, int loose_obj_only, struct got_repository *repo)
332 const struct got_error *err;
333 struct got_pack_meta *m;
335 if (loose_obj_only) {
337 err = search_packidx(&is_packed, id, repo);
344 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
351 err = alloc_meta(&m, id, path, obj_type, mtime);
355 if (v->nmeta == v->metasz){
356 size_t newsize = 2 * v->metasz;
357 struct got_pack_meta **new;
358 new = reallocarray(v->meta, newsize, sizeof(*new));
360 err = got_error_from_errno("reallocarray");
371 v->meta[v->nmeta++] = m;
376 static const struct got_error *
377 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
378 struct got_object_idset *idset, struct got_object_id *tree_id,
379 const char *dpath, time_t mtime, struct got_repository *repo,
380 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
382 const struct got_error *err;
383 struct got_tree_object *tree;
387 err = got_object_open_as_tree(&tree, repo, tree_id);
391 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
392 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
393 struct got_object_id *id = got_tree_entry_get_id(e);
394 mode_t mode = got_tree_entry_get_mode(e);
397 err = (*cancel_cb)(cancel_arg);
402 if (got_object_tree_entry_is_submodule(e) ||
403 got_object_idset_contains(idset, id))
406 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
407 got_tree_entry_get_name(e)) == -1) {
408 err = got_error_from_errno("asprintf");
413 struct got_object_qid *qid;
414 err = got_object_qid_alloc(&qid, id);
417 STAILQ_INSERT_TAIL(ids, qid, entry);
418 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
419 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
420 mtime, loose_obj_only, repo);
428 got_object_tree_close(tree);
433 static const struct got_error *
434 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
435 struct got_object_id *tree_id, const char *dpath, time_t mtime,
436 int loose_obj_only, struct got_repository *repo,
437 got_cancel_cb cancel_cb, void *cancel_arg)
439 const struct got_error *err = NULL;
440 struct got_object_id_queue tree_ids;
441 struct got_object_qid *qid;
443 if (got_object_idset_contains(idset, tree_id))
446 err = got_object_qid_alloc(&qid, tree_id);
450 STAILQ_INIT(&tree_ids);
451 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
453 while (!STAILQ_EMPTY(&tree_ids)) {
455 err = (*cancel_cb)(cancel_arg);
460 qid = STAILQ_FIRST(&tree_ids);
461 STAILQ_REMOVE_HEAD(&tree_ids, entry);
463 if (got_object_idset_contains(idset, qid->id)) {
464 got_object_qid_free(qid);
468 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
469 mtime, loose_obj_only, repo);
471 got_object_qid_free(qid);
475 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
476 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
477 got_object_qid_free(qid);
482 got_object_id_queue_free(&tree_ids);
486 static const struct got_error *
487 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
488 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
489 got_cancel_cb cancel_cb, void *cancel_arg)
491 const struct got_error *err;
492 struct got_commit_object *commit;
494 if (got_object_idset_contains(idset, id))
497 if (loose_obj_only) {
499 err = search_packidx(&is_packed, id, repo);
506 err = got_object_open_as_commit(&commit, repo, id);
510 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
511 got_object_commit_get_committer_time(commit),
512 loose_obj_only, repo);
516 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
517 "", got_object_commit_get_committer_time(commit),
518 loose_obj_only, repo, cancel_cb, cancel_arg);
520 got_object_commit_close(commit);
524 static const struct got_error *
525 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
526 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
527 got_cancel_cb cancel_cb, void *cancel_arg)
529 const struct got_error *err;
530 struct got_tag_object *tag = NULL;
532 if (got_object_idset_contains(idset, id))
535 if (loose_obj_only) {
537 err = search_packidx(&is_packed, id, repo);
544 err = got_object_open_as_tag(&tag, repo, id);
548 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
549 got_object_tag_get_tagger_time(tag),
550 loose_obj_only, repo);
554 switch (got_object_tag_get_object_type(tag)) {
555 case GOT_OBJ_TYPE_COMMIT:
556 err = load_commit(v, idset,
557 got_object_tag_get_object_id(tag), repo,
558 loose_obj_only, cancel_cb, cancel_arg);
560 case GOT_OBJ_TYPE_TREE:
561 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
562 "", got_object_tag_get_tagger_time(tag),
563 loose_obj_only, repo, cancel_cb, cancel_arg);
570 got_object_tag_close(tag);
574 enum findtwixt_color {
579 static const int findtwixt_colors[] = {
585 static const struct got_error *
586 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
587 int color, struct got_repository *repo)
589 const struct got_error *err;
590 struct got_object_qid *qid;
592 err = got_object_qid_alloc(&qid, id);
596 STAILQ_INSERT_TAIL(ids, qid, entry);
597 qid->data = (void *)&findtwixt_colors[color];
601 static const struct got_error *
602 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
603 struct got_object_id *id, struct got_repository *repo,
604 got_cancel_cb cancel_cb, void *cancel_arg)
606 const struct got_error *err = NULL;
607 struct got_commit_object *commit;
608 const struct got_object_id_queue *parents;
609 struct got_object_id_queue ids;
610 struct got_object_qid *qid;
614 err = got_object_qid_alloc(&qid, id);
617 STAILQ_INSERT_HEAD(&ids, qid, entry);
619 while (!STAILQ_EMPTY(&ids)) {
621 err = (*cancel_cb)(cancel_arg);
626 qid = STAILQ_FIRST(&ids);
627 STAILQ_REMOVE_HEAD(&ids, entry);
629 if (got_object_idset_contains(drop, qid->id)) {
630 got_object_qid_free(qid);
634 err = got_object_idset_add(drop, qid->id,
635 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
637 got_object_qid_free(qid);
641 if (!got_object_idset_contains(keep, qid->id)) {
642 got_object_qid_free(qid);
646 err = got_object_open_as_commit(&commit, repo, qid->id);
647 got_object_qid_free(qid);
651 parents = got_object_commit_get_parent_ids(commit);
653 err = got_object_id_queue_copy(parents, &ids);
655 got_object_commit_close(commit);
659 got_object_commit_close(commit);
662 got_object_id_queue_free(&ids);
666 struct append_id_arg {
667 struct got_object_id **array;
671 static const struct got_error *
672 append_id(struct got_object_id *id, void *data, void *arg)
674 struct append_id_arg *a = arg;
676 a->array[a->idx] = got_object_id_dup(id);
677 if (a->array[a->idx] == NULL)
678 return got_error_from_errno("got_object_id_dup");
684 static const struct got_error *
685 findtwixt(struct got_object_id ***res, int *nres,
686 struct got_object_id **head, int nhead,
687 struct got_object_id **tail, int ntail,
688 struct got_repository *repo,
689 got_cancel_cb cancel_cb, void *cancel_arg)
691 const struct got_error *err = NULL;
692 struct got_object_id_queue ids;
693 struct got_object_idset *keep, *drop;
694 struct got_object_qid *qid;
695 int i, ncolor, nkeep, obj_type;
701 keep = got_object_idset_alloc();
703 return got_error_from_errno("got_object_idset_alloc");
705 drop = got_object_idset_alloc();
707 err = got_error_from_errno("got_object_idset_alloc");
711 for (i = 0; i < nhead; i++) {
712 struct got_object_id *id = head[i];
715 err = got_object_get_type(&obj_type, repo, id);
718 if (obj_type != GOT_OBJ_TYPE_COMMIT)
720 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
724 for (i = 0; i < ntail; i++) {
725 struct got_object_id *id = tail[i];
728 err = got_object_get_type(&obj_type, repo, id);
731 if (obj_type != GOT_OBJ_TYPE_COMMIT)
733 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
738 while (!STAILQ_EMPTY(&ids)) {
740 qid = STAILQ_FIRST(&ids);
741 qcolor = *((int *)qid->data);
743 if (got_object_idset_contains(drop, qid->id))
745 else if (got_object_idset_contains(keep, qid->id))
748 ncolor = COLOR_BLANK;
750 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
751 qcolor == COLOR_KEEP)) {
752 STAILQ_REMOVE_HEAD(&ids, entry);
753 got_object_qid_free(qid);
757 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
758 err = drop_commit(keep, drop, qid->id, repo,
759 cancel_cb, cancel_arg);
762 } else if (ncolor == COLOR_BLANK) {
763 struct got_commit_object *commit;
764 struct got_object_id *id;
765 const struct got_object_id_queue *parents;
766 struct got_object_qid *pid;
768 id = got_object_id_dup(qid->id);
770 err = got_error_from_errno("got_object_id_dup");
773 if (qcolor == COLOR_KEEP)
774 err = got_object_idset_add(keep, id,
775 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
777 err = got_object_idset_add(drop, id,
778 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
784 err = got_object_open_as_commit(&commit, repo, id);
789 parents = got_object_commit_get_parent_ids(commit);
791 STAILQ_FOREACH(pid, parents, entry) {
792 err = queue_commit_id(&ids, pid->id,
800 got_object_commit_close(commit);
803 /* should not happen */
804 err = got_error_fmt(GOT_ERR_NOT_IMPL,
805 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
809 STAILQ_REMOVE_HEAD(&ids, entry);
810 got_object_qid_free(qid);
813 nkeep = got_object_idset_num_elements(keep);
815 struct append_id_arg arg;
816 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
817 if (arg.array == NULL) {
818 err = got_error_from_errno("calloc");
822 err = got_object_idset_for_each(keep, append_id, &arg);
831 got_object_idset_free(keep);
832 got_object_idset_free(drop);
833 got_object_id_queue_free(&ids);
837 static const struct got_error *
838 read_meta(struct got_pack_meta ***meta, int *nmeta,
839 struct got_object_id **theirs, int ntheirs,
840 struct got_object_id **ours, int nours, struct got_repository *repo,
841 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
842 got_cancel_cb cancel_cb, void *cancel_arg)
844 const struct got_error *err = NULL;
845 struct got_object_id **ids = NULL;
846 struct got_object_idset *idset;
847 int i, nobj = 0, obj_type;
848 struct got_pack_metavec v;
853 idset = got_object_idset_alloc();
855 return got_error_from_errno("got_object_idset_alloc");
859 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
860 if (v.meta == NULL) {
861 err = got_error_from_errno("calloc");
865 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
866 cancel_cb, cancel_arg);
867 if (err || nobj == 0)
870 for (i = 0; i < ntheirs; i++) {
871 struct got_object_id *id = theirs[i];
874 err = got_object_get_type(&obj_type, repo, id);
877 if (obj_type != GOT_OBJ_TYPE_COMMIT)
879 err = load_commit(NULL, idset, id, repo,
880 loose_obj_only, cancel_cb, cancel_arg);
884 err = progress_cb(progress_arg, 0L, nours,
891 for (i = 0; i < ntheirs; i++) {
892 struct got_object_id *id = theirs[i];
896 cached_type = got_object_idset_get(idset, id);
897 if (cached_type == NULL) {
898 err = got_object_get_type(&obj_type, repo, id);
902 obj_type = *cached_type;
903 if (obj_type != GOT_OBJ_TYPE_TAG)
905 err = load_tag(NULL, idset, id, repo,
906 loose_obj_only, cancel_cb, cancel_arg);
910 err = progress_cb(progress_arg, 0L, nours,
917 for (i = 0; i < nobj; i++) {
918 err = load_commit(&v, idset, ids[i], repo,
919 loose_obj_only, cancel_cb, cancel_arg);
923 err = progress_cb(progress_arg, 0L, nours,
930 for (i = 0; i < nours; i++) {
931 struct got_object_id *id = ours[i];
935 cached_type = got_object_idset_get(idset, id);
936 if (cached_type == NULL) {
937 err = got_object_get_type(&obj_type, repo, id);
941 obj_type = *cached_type;
942 if (obj_type != GOT_OBJ_TYPE_TAG)
944 err = load_tag(&v, idset, id, repo,
945 loose_obj_only, cancel_cb, cancel_arg);
949 err = progress_cb(progress_arg, 0L, nours,
957 for (i = 0; i < nobj; i++) {
961 got_object_idset_free(idset);
971 const struct got_error *
972 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
976 SHA1Update(ctx, buf, len);
977 n = fwrite(buf, 1, len, f);
979 return got_ferror(f, GOT_ERR_IO);
984 putbe32(char *b, uint32_t n)
993 write_order_cmp(const void *pa, const void *pb)
995 struct got_pack_meta *a, *b, *ahd, *bhd;
997 a = *(struct got_pack_meta **)pa;
998 b = *(struct got_pack_meta **)pb;
999 ahd = (a->head == NULL) ? a : a->head;
1000 bhd = (b->head == NULL) ? b : b->head;
1001 if (ahd->mtime != bhd->mtime)
1002 return bhd->mtime - ahd->mtime;
1004 return (uintptr_t)bhd - (uintptr_t)ahd;
1005 if (a->nchain != b->nchain)
1006 return a->nchain - b->nchain;
1007 return a->mtime - b->mtime;
1010 static const struct got_error *
1011 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1017 hdr[0] = obj_type << 4;
1018 hdr[0] |= len & 0xf;
1020 for (i = 1; len != 0; i++){
1022 return got_error(GOT_ERR_NO_SPACE);
1023 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1024 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1025 len >>= GOT_DELTA_SIZE_SHIFT;
1032 static const struct got_error *
1033 encodedelta(struct got_pack_meta *m, struct got_raw_object *o,
1034 off_t base_size, FILE *f)
1036 unsigned char buf[16], *bp;
1040 struct got_delta_instruction *d;
1042 /* base object size */
1043 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1044 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1045 for (i = 1; n > 0; i++) {
1046 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1047 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1048 n >>= GOT_DELTA_SIZE_SHIFT;
1050 w = fwrite(buf, 1, i, f);
1052 return got_ferror(f, GOT_ERR_IO);
1054 /* target object size */
1055 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1056 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1057 for (i = 1; n > 0; i++) {
1058 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1059 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1060 n >>= GOT_DELTA_SIZE_SHIFT;
1062 w = fwrite(buf, 1, i, f);
1064 return got_ferror(f, GOT_ERR_IO);
1066 for (j = 0; j < m->ndeltas; j++) {
1071 buf[0] = GOT_DELTA_BASE_COPY;
1072 for (i = 0; i < 4; i++) {
1073 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1082 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1083 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1084 for (i = 0; i < 3 && n > 0; i++) {
1085 buf[0] |= 1 << (i + 4);
1090 w = fwrite(buf, 1, bp - buf, f);
1092 return got_ferror(f, GOT_ERR_IO);
1096 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1097 return got_error_from_errno("fseeko");
1099 while (n != d->len) {
1100 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1101 w = fwrite(buf, 1, 1, f);
1103 return got_ferror(f, GOT_ERR_IO);
1104 r = fread(content, 1, buf[0], o->f);
1106 return got_ferror(o->f, GOT_ERR_IO);
1107 w = fwrite(content, 1, buf[0], f);
1109 return got_ferror(f, GOT_ERR_IO);
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,
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;
1147 struct got_pack_meta *m;
1148 struct got_raw_object *raw = NULL;
1149 FILE *delta_file = NULL;
1152 struct got_deflate_checksum csum;
1153 off_t packfile_size = 0;
1157 csum.output_sha1 = &ctx;
1158 csum.output_crc = NULL;
1160 err = hwrite(packfile, "PACK", 4, &ctx);
1163 putbe32(buf, GOT_PACKFILE_VERSION);
1164 err = hwrite(packfile, buf, 4, &ctx);
1167 putbe32(buf, nmeta);
1168 err = hwrite(packfile, buf, 4, &ctx);
1171 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1172 for (i = 0; i < nmeta; i++) {
1174 err = progress_cb(progress_arg, packfile_size, nours,
1180 m->off = ftello(packfile);
1182 outfd = got_opentempfd();
1184 err = got_error_from_errno("got_opentempfd");
1188 err = got_object_raw_open(&raw, outfd, repo, &m->id, 8192);
1191 if (raw->data == NULL)
1192 outfd = -1; /* outfd is now raw->f */
1193 if (m->deltas == NULL) {
1194 err = packhdr(&nh, buf, sizeof(buf),
1195 m->obj_type, raw->size);
1198 err = hwrite(packfile, buf, nh, &ctx);
1201 packfile_size += nh;
1202 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1203 err = got_error_from_errno("fseeko");
1206 err = got_deflate_to_file(&outlen, raw->f, packfile,
1210 packfile_size += outlen;
1212 if (delta_file == NULL) {
1213 delta_file = got_opentemp();
1214 if (delta_file == NULL) {
1215 err = got_error_from_errno(
1220 if (ftruncate(fileno(delta_file), 0L) == -1) {
1221 err = got_error_from_errno("ftruncate");
1224 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1225 err = got_error_from_errno("fseeko");
1228 err = encodedelta(m, raw, m->prev->size, delta_file);
1231 nd = ftello(delta_file);
1232 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1233 err = got_error_from_errno("fseeko");
1236 if (use_offset_deltas && m->prev->off != 0) {
1237 err = packhdr(&nh, buf, sizeof(buf),
1238 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1241 nh += packoff(buf + nh,
1242 m->off - m->prev->off);
1243 err = hwrite(packfile, buf, nh, &ctx);
1246 packfile_size += nh;
1248 err = packhdr(&nh, buf, sizeof(buf),
1249 GOT_OBJ_TYPE_REF_DELTA, nd);
1250 err = hwrite(packfile, buf, nh, &ctx);
1253 packfile_size += nh;
1254 err = hwrite(packfile, m->prev->id.sha1,
1255 sizeof(m->prev->id.sha1), &ctx);
1256 packfile_size += sizeof(m->prev->id.sha1);
1260 err = got_deflate_to_file(&outlen, delta_file,
1264 packfile_size += outlen;
1266 got_object_raw_close(raw);
1269 SHA1Final(pack_sha1, &ctx);
1270 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1271 if (n != SHA1_DIGEST_LENGTH)
1272 err = got_ferror(packfile, GOT_ERR_IO);
1273 packfile_size += SHA1_DIGEST_LENGTH;
1274 packfile_size += sizeof(struct got_packfile_hdr);
1275 err = progress_cb(progress_arg, packfile_size, nours,
1276 nmeta, nmeta, nmeta);
1280 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1281 err = got_error_from_errno("fclose");
1283 got_object_raw_close(raw);
1284 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1285 err = got_error_from_errno("close");
1289 const struct got_error *
1290 got_pack_create(uint8_t *packsha1, FILE *packfile,
1291 struct got_object_id **theirs, int ntheirs,
1292 struct got_object_id **ours, int nours,
1293 struct got_repository *repo, int loose_obj_only, int allow_empty,
1294 got_pack_progress_cb progress_cb, void *progress_arg,
1295 got_cancel_cb cancel_cb, void *cancel_arg)
1297 const struct got_error *err;
1298 struct got_pack_meta **meta;
1301 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1302 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1306 if (nmeta == 0 && !allow_empty) {
1307 err = got_error(GOT_ERR_CANNOT_PACK);
1311 err = pick_deltas(meta, nmeta, nours, repo,
1312 progress_cb, progress_arg, cancel_cb, cancel_arg);
1317 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1318 progress_cb, progress_arg, cancel_cb, cancel_arg);
1322 free_nmeta(meta, nmeta);