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 MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
59 struct got_pack_meta {
60 struct got_object_id id;
66 /* The best delta we picked */
67 struct got_pack_meta *head;
68 struct got_pack_meta *prev;
69 off_t delta_offset; /* offset in delta cache file */
70 off_t delta_len; /* length in delta cache file */
73 /* Only used for delta window */
74 struct got_delta_table *dtab;
76 /* Only used for writing offset deltas */
80 struct got_pack_metavec {
81 struct got_pack_meta **meta;
86 static const struct got_error *
87 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
88 const char *path, int obj_type, time_t mtime)
90 const struct got_error *err = NULL;
91 struct got_pack_meta *m;
95 m = calloc(1, sizeof(*m));
97 return got_error_from_errno("calloc");
99 memcpy(&m->id, id, sizeof(m->id));
101 m->path = strdup(path);
102 if (m->path == NULL) {
103 err = got_error_from_errno("strdup");
108 m->obj_type = obj_type;
115 clear_meta(struct got_pack_meta *meta)
124 free_nmeta(struct got_pack_meta **meta, int nmeta)
128 for (i = 0; i < nmeta; i++)
134 delta_order_cmp(const void *pa, const void *pb)
136 struct got_pack_meta *a, *b;
139 a = *(struct got_pack_meta **)pa;
140 b = *(struct got_pack_meta **)pb;
142 if (a->obj_type != b->obj_type)
143 return a->obj_type - b->obj_type;
144 cmp = strcmp(a->path, b->path);
147 if (a->mtime != b->mtime)
148 return a->mtime - b->mtime;
149 return got_object_id_cmp(&a->id, &b->id);
153 delta_size(struct got_delta_instruction *deltas, int ndeltas)
156 for (i = 0; i < ndeltas; i++) {
158 size += GOT_DELTA_SIZE_SHIFT;
160 size += deltas[i].len + 1;
165 static const struct got_error *
166 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
167 struct got_delta_instruction *deltas, int ndeltas,
168 off_t base_size, FILE *f)
170 unsigned char buf[16], *bp;
174 struct got_delta_instruction *d;
176 /* base object size */
177 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
178 n = base_size >> GOT_DELTA_SIZE_SHIFT;
179 for (i = 1; n > 0; i++) {
180 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
181 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
182 n >>= GOT_DELTA_SIZE_SHIFT;
184 w = fwrite(buf, 1, i, f);
186 return got_ferror(f, GOT_ERR_IO);
188 /* target object size */
189 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
190 n = o->size >> GOT_DELTA_SIZE_SHIFT;
191 for (i = 1; n > 0; i++) {
192 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
193 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
194 n >>= GOT_DELTA_SIZE_SHIFT;
196 w = fwrite(buf, 1, i, f);
198 return got_ferror(f, GOT_ERR_IO);
200 for (j = 0; j < ndeltas; j++) {
205 buf[0] = GOT_DELTA_BASE_COPY;
206 for (i = 0; i < 4; i++) {
207 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
216 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
217 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
218 for (i = 0; i < 3 && n > 0; i++) {
219 buf[0] |= 1 << (i + 4);
224 w = fwrite(buf, 1, bp - buf, f);
226 return got_ferror(f, GOT_ERR_IO);
230 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
231 return got_error_from_errno("fseeko");
233 while (n != d->len) {
234 buf[0] = (d->len - n < 127) ? d->len - n : 127;
235 w = fwrite(buf, 1, 1, f);
237 return got_ferror(f, GOT_ERR_IO);
238 r = fread(content, 1, buf[0], o->f);
240 return got_ferror(o->f, GOT_ERR_IO);
241 w = fwrite(content, 1, buf[0], f);
243 return got_ferror(f, GOT_ERR_IO);
253 static const struct got_error *
254 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
255 FILE *delta_cache, struct got_repository *repo,
256 got_pack_progress_cb progress_cb, void *progress_arg,
257 got_cancel_cb cancel_cb, void *cancel_arg)
259 const struct got_error *err = NULL;
260 struct got_pack_meta *m = NULL, *base = NULL;
261 struct got_raw_object *raw = NULL, *base_raw = NULL;
262 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
263 int i, j, size, best_size, ndeltas, best_ndeltas;
264 const int max_base_candidates = 3;
267 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
268 for (i = 0; i < nmeta; i++) {
270 err = (*cancel_cb)(cancel_arg);
275 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
281 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
282 m->obj_type == GOT_OBJ_TYPE_TAG)
285 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
290 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
291 raw->size + raw->hdrlen);
295 if (i > max_base_candidates) {
296 struct got_pack_meta *n = NULL;
297 n = meta[i - (max_base_candidates + 1)];
298 got_deltify_free(n->dtab);
302 best_size = raw->size;
304 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
306 err = (*cancel_cb)(cancel_arg);
311 /* long chains make unpacking slow, avoid such bases */
312 if (base->nchain >= 128 ||
313 base->obj_type != m->obj_type)
316 err = got_object_raw_open(&base_raw, &outfd, repo,
320 err = got_deltify(&deltas, &ndeltas,
321 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
322 base->dtab, base_raw->f, base_raw->hdrlen,
323 base_raw->size + base_raw->hdrlen);
324 got_object_raw_close(base_raw);
329 size = delta_size(deltas, ndeltas);
330 if (size + 32 < best_size){
332 * if we already picked a best delta,
337 best_deltas = deltas;
338 best_ndeltas = ndeltas;
340 m->nchain = base->nchain + 1;
342 m->head = base->head;
352 if (best_ndeltas > 0) {
353 m->delta_offset = ftello(delta_cache);
354 err = encode_delta(m, raw, best_deltas,
355 best_ndeltas, m->prev->size, delta_cache);
361 m->delta_len = ftello(delta_cache) - m->delta_offset;
364 got_object_raw_close(raw);
368 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
369 got_deltify_free(meta[i]->dtab);
370 meta[i]->dtab = NULL;
373 got_object_raw_close(raw);
375 got_object_raw_close(base_raw);
376 if (outfd != -1 && close(outfd) == -1 && err == NULL)
377 err = got_error_from_errno("close");
383 static const struct got_error *
384 search_packidx(int *found, struct got_object_id *id,
385 struct got_repository *repo)
387 const struct got_error *err = NULL;
388 struct got_packidx *packidx = NULL;
393 err = got_repo_search_packidx(&packidx, &idx, repo, id);
395 *found = 1; /* object is already packed */
396 else if (err->code == GOT_ERR_NO_OBJ)
401 static const int obj_types[] = {
407 GOT_OBJ_TYPE_OFFSET_DELTA,
408 GOT_OBJ_TYPE_REF_DELTA
411 static const struct got_error *
412 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
413 struct got_object_id *id, const char *path, int obj_type,
414 time_t mtime, int loose_obj_only, struct got_repository *repo)
416 const struct got_error *err;
417 struct got_pack_meta *m;
419 if (loose_obj_only) {
421 err = search_packidx(&is_packed, id, repo);
428 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
435 err = alloc_meta(&m, id, path, obj_type, mtime);
439 if (v->nmeta == v->metasz){
440 size_t newsize = 2 * v->metasz;
441 struct got_pack_meta **new;
442 new = reallocarray(v->meta, newsize, sizeof(*new));
444 err = got_error_from_errno("reallocarray");
455 v->meta[v->nmeta++] = m;
460 static const struct got_error *
461 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
462 struct got_object_idset *idset, struct got_object_id *tree_id,
463 const char *dpath, time_t mtime, struct got_repository *repo,
464 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
466 const struct got_error *err;
467 struct got_tree_object *tree;
471 err = got_object_open_as_tree(&tree, repo, tree_id);
475 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
476 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
477 struct got_object_id *id = got_tree_entry_get_id(e);
478 mode_t mode = got_tree_entry_get_mode(e);
481 err = (*cancel_cb)(cancel_arg);
486 if (got_object_tree_entry_is_submodule(e) ||
487 got_object_idset_contains(idset, id))
490 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
491 got_tree_entry_get_name(e)) == -1) {
492 err = got_error_from_errno("asprintf");
497 struct got_object_qid *qid;
498 err = got_object_qid_alloc(&qid, id);
501 STAILQ_INSERT_TAIL(ids, qid, entry);
502 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
503 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
504 mtime, loose_obj_only, repo);
512 got_object_tree_close(tree);
517 static const struct got_error *
518 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
519 struct got_object_id *tree_id, const char *dpath, time_t mtime,
520 int loose_obj_only, struct got_repository *repo,
521 got_cancel_cb cancel_cb, void *cancel_arg)
523 const struct got_error *err = NULL;
524 struct got_object_id_queue tree_ids;
525 struct got_object_qid *qid;
527 if (got_object_idset_contains(idset, tree_id))
530 err = got_object_qid_alloc(&qid, tree_id);
534 STAILQ_INIT(&tree_ids);
535 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
537 while (!STAILQ_EMPTY(&tree_ids)) {
539 err = (*cancel_cb)(cancel_arg);
544 qid = STAILQ_FIRST(&tree_ids);
545 STAILQ_REMOVE_HEAD(&tree_ids, entry);
547 if (got_object_idset_contains(idset, qid->id)) {
548 got_object_qid_free(qid);
552 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
553 mtime, loose_obj_only, repo);
555 got_object_qid_free(qid);
559 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
560 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
561 got_object_qid_free(qid);
566 got_object_id_queue_free(&tree_ids);
570 static const struct got_error *
571 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
572 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
573 got_cancel_cb cancel_cb, void *cancel_arg)
575 const struct got_error *err;
576 struct got_commit_object *commit;
578 if (got_object_idset_contains(idset, id))
581 if (loose_obj_only) {
583 err = search_packidx(&is_packed, id, repo);
590 err = got_object_open_as_commit(&commit, repo, id);
594 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
595 got_object_commit_get_committer_time(commit),
596 loose_obj_only, repo);
600 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
601 "", got_object_commit_get_committer_time(commit),
602 loose_obj_only, repo, cancel_cb, cancel_arg);
604 got_object_commit_close(commit);
608 static const struct got_error *
609 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
610 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
611 got_cancel_cb cancel_cb, void *cancel_arg)
613 const struct got_error *err;
614 struct got_tag_object *tag = NULL;
616 if (got_object_idset_contains(idset, id))
619 if (loose_obj_only) {
621 err = search_packidx(&is_packed, id, repo);
628 err = got_object_open_as_tag(&tag, repo, id);
632 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
633 got_object_tag_get_tagger_time(tag),
634 loose_obj_only, repo);
638 switch (got_object_tag_get_object_type(tag)) {
639 case GOT_OBJ_TYPE_COMMIT:
640 err = load_commit(v, idset,
641 got_object_tag_get_object_id(tag), repo,
642 loose_obj_only, cancel_cb, cancel_arg);
644 case GOT_OBJ_TYPE_TREE:
645 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
646 "", got_object_tag_get_tagger_time(tag),
647 loose_obj_only, repo, cancel_cb, cancel_arg);
654 got_object_tag_close(tag);
658 enum findtwixt_color {
663 static const int findtwixt_colors[] = {
669 static const struct got_error *
670 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
671 int color, struct got_repository *repo)
673 const struct got_error *err;
674 struct got_object_qid *qid;
676 err = got_object_qid_alloc(&qid, id);
680 STAILQ_INSERT_TAIL(ids, qid, entry);
681 qid->data = (void *)&findtwixt_colors[color];
685 static const struct got_error *
686 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
687 struct got_object_id *id, struct got_repository *repo,
688 got_cancel_cb cancel_cb, void *cancel_arg)
690 const struct got_error *err = NULL;
691 struct got_commit_object *commit;
692 const struct got_object_id_queue *parents;
693 struct got_object_id_queue ids;
694 struct got_object_qid *qid;
698 err = got_object_qid_alloc(&qid, id);
701 STAILQ_INSERT_HEAD(&ids, qid, entry);
703 while (!STAILQ_EMPTY(&ids)) {
705 err = (*cancel_cb)(cancel_arg);
710 qid = STAILQ_FIRST(&ids);
711 STAILQ_REMOVE_HEAD(&ids, entry);
713 if (got_object_idset_contains(drop, qid->id)) {
714 got_object_qid_free(qid);
718 err = got_object_idset_add(drop, qid->id,
719 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
721 got_object_qid_free(qid);
725 if (!got_object_idset_contains(keep, qid->id)) {
726 got_object_qid_free(qid);
730 err = got_object_open_as_commit(&commit, repo, qid->id);
731 got_object_qid_free(qid);
735 parents = got_object_commit_get_parent_ids(commit);
737 err = got_object_id_queue_copy(parents, &ids);
739 got_object_commit_close(commit);
743 got_object_commit_close(commit);
746 got_object_id_queue_free(&ids);
750 struct append_id_arg {
751 struct got_object_id **array;
755 static const struct got_error *
756 append_id(struct got_object_id *id, void *data, void *arg)
758 struct append_id_arg *a = arg;
760 a->array[a->idx] = got_object_id_dup(id);
761 if (a->array[a->idx] == NULL)
762 return got_error_from_errno("got_object_id_dup");
768 static const struct got_error *
769 findtwixt(struct got_object_id ***res, int *nres,
770 struct got_object_id **head, int nhead,
771 struct got_object_id **tail, int ntail,
772 struct got_repository *repo,
773 got_cancel_cb cancel_cb, void *cancel_arg)
775 const struct got_error *err = NULL;
776 struct got_object_id_queue ids;
777 struct got_object_idset *keep, *drop;
778 struct got_object_qid *qid;
779 int i, ncolor, nkeep, obj_type;
785 keep = got_object_idset_alloc();
787 return got_error_from_errno("got_object_idset_alloc");
789 drop = got_object_idset_alloc();
791 err = got_error_from_errno("got_object_idset_alloc");
795 for (i = 0; i < nhead; i++) {
796 struct got_object_id *id = head[i];
799 err = got_object_get_type(&obj_type, repo, id);
802 if (obj_type != GOT_OBJ_TYPE_COMMIT)
804 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
808 for (i = 0; i < ntail; i++) {
809 struct got_object_id *id = tail[i];
812 err = got_object_get_type(&obj_type, repo, id);
815 if (obj_type != GOT_OBJ_TYPE_COMMIT)
817 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
822 while (!STAILQ_EMPTY(&ids)) {
824 qid = STAILQ_FIRST(&ids);
825 qcolor = *((int *)qid->data);
827 if (got_object_idset_contains(drop, qid->id))
829 else if (got_object_idset_contains(keep, qid->id))
832 ncolor = COLOR_BLANK;
834 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
835 qcolor == COLOR_KEEP)) {
836 STAILQ_REMOVE_HEAD(&ids, entry);
837 got_object_qid_free(qid);
841 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
842 err = drop_commit(keep, drop, qid->id, repo,
843 cancel_cb, cancel_arg);
846 } else if (ncolor == COLOR_BLANK) {
847 struct got_commit_object *commit;
848 struct got_object_id *id;
849 const struct got_object_id_queue *parents;
850 struct got_object_qid *pid;
852 id = got_object_id_dup(qid->id);
854 err = got_error_from_errno("got_object_id_dup");
857 if (qcolor == COLOR_KEEP)
858 err = got_object_idset_add(keep, id,
859 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
861 err = got_object_idset_add(drop, id,
862 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
868 err = got_object_open_as_commit(&commit, repo, id);
873 parents = got_object_commit_get_parent_ids(commit);
875 STAILQ_FOREACH(pid, parents, entry) {
876 err = queue_commit_id(&ids, pid->id,
884 got_object_commit_close(commit);
887 /* should not happen */
888 err = got_error_fmt(GOT_ERR_NOT_IMPL,
889 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
893 STAILQ_REMOVE_HEAD(&ids, entry);
894 got_object_qid_free(qid);
897 nkeep = got_object_idset_num_elements(keep);
899 struct append_id_arg arg;
900 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
901 if (arg.array == NULL) {
902 err = got_error_from_errno("calloc");
906 err = got_object_idset_for_each(keep, append_id, &arg);
915 got_object_idset_free(keep);
916 got_object_idset_free(drop);
917 got_object_id_queue_free(&ids);
921 static const struct got_error *
922 read_meta(struct got_pack_meta ***meta, int *nmeta,
923 struct got_object_id **theirs, int ntheirs,
924 struct got_object_id **ours, int nours, struct got_repository *repo,
925 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
926 got_cancel_cb cancel_cb, void *cancel_arg)
928 const struct got_error *err = NULL;
929 struct got_object_id **ids = NULL;
930 struct got_object_idset *idset;
931 int i, nobj = 0, obj_type;
932 struct got_pack_metavec v;
937 idset = got_object_idset_alloc();
939 return got_error_from_errno("got_object_idset_alloc");
943 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
944 if (v.meta == NULL) {
945 err = got_error_from_errno("calloc");
949 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
950 cancel_cb, cancel_arg);
951 if (err || nobj == 0)
954 for (i = 0; i < ntheirs; i++) {
955 struct got_object_id *id = theirs[i];
958 err = got_object_get_type(&obj_type, repo, id);
961 if (obj_type != GOT_OBJ_TYPE_COMMIT)
963 err = load_commit(NULL, idset, id, repo,
964 loose_obj_only, cancel_cb, cancel_arg);
968 err = progress_cb(progress_arg, 0L, nours,
975 for (i = 0; i < ntheirs; i++) {
976 struct got_object_id *id = theirs[i];
980 cached_type = got_object_idset_get(idset, id);
981 if (cached_type == NULL) {
982 err = got_object_get_type(&obj_type, repo, id);
986 obj_type = *cached_type;
987 if (obj_type != GOT_OBJ_TYPE_TAG)
989 err = load_tag(NULL, idset, id, repo,
990 loose_obj_only, cancel_cb, cancel_arg);
994 err = progress_cb(progress_arg, 0L, nours,
1001 for (i = 0; i < nobj; i++) {
1002 err = load_commit(&v, idset, ids[i], repo,
1003 loose_obj_only, cancel_cb, cancel_arg);
1007 err = progress_cb(progress_arg, 0L, nours,
1014 for (i = 0; i < nours; i++) {
1015 struct got_object_id *id = ours[i];
1019 cached_type = got_object_idset_get(idset, id);
1020 if (cached_type == NULL) {
1021 err = got_object_get_type(&obj_type, repo, id);
1025 obj_type = *cached_type;
1026 if (obj_type != GOT_OBJ_TYPE_TAG)
1028 err = load_tag(&v, idset, id, repo,
1029 loose_obj_only, cancel_cb, cancel_arg);
1033 err = progress_cb(progress_arg, 0L, nours,
1041 for (i = 0; i < nobj; i++) {
1045 got_object_idset_free(idset);
1055 const struct got_error *
1056 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1060 SHA1Update(ctx, buf, len);
1061 n = fwrite(buf, 1, len, f);
1063 return got_ferror(f, GOT_ERR_IO);
1068 putbe32(char *b, uint32_t n)
1077 write_order_cmp(const void *pa, const void *pb)
1079 struct got_pack_meta *a, *b, *ahd, *bhd;
1081 a = *(struct got_pack_meta **)pa;
1082 b = *(struct got_pack_meta **)pb;
1083 ahd = (a->head == NULL) ? a : a->head;
1084 bhd = (b->head == NULL) ? b : b->head;
1085 if (ahd->mtime != bhd->mtime)
1086 return bhd->mtime - ahd->mtime;
1088 return (uintptr_t)bhd - (uintptr_t)ahd;
1089 if (a->nchain != b->nchain)
1090 return a->nchain - b->nchain;
1091 return a->mtime - b->mtime;
1094 static const struct got_error *
1095 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1101 hdr[0] = obj_type << 4;
1102 hdr[0] |= len & 0xf;
1104 for (i = 1; len != 0; i++){
1106 return got_error(GOT_ERR_NO_SPACE);
1107 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1108 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1109 len >>= GOT_DELTA_SIZE_SHIFT;
1117 packoff(char *hdr, off_t off)
1122 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1123 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1124 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1125 GOT_DELTA_SIZE_MORE;
1130 hdr[j++] = rbuf[--i];
1134 static const struct got_error *
1135 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1136 struct got_pack_meta **meta, int nmeta, int nours,
1137 int use_offset_deltas, struct got_repository *repo,
1138 got_pack_progress_cb progress_cb, void *progress_arg,
1139 got_cancel_cb cancel_cb, void *cancel_arg)
1141 const struct got_error *err = NULL;
1144 struct got_pack_meta *m;
1145 struct got_raw_object *raw = NULL;
1146 FILE *delta_file = NULL;
1149 struct got_deflate_checksum csum;
1150 off_t packfile_size = 0;
1154 csum.output_sha1 = &ctx;
1155 csum.output_crc = NULL;
1157 err = hwrite(packfile, "PACK", 4, &ctx);
1160 putbe32(buf, GOT_PACKFILE_VERSION);
1161 err = hwrite(packfile, buf, 4, &ctx);
1164 putbe32(buf, nmeta);
1165 err = hwrite(packfile, buf, 4, &ctx);
1168 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1169 for (i = 0; i < nmeta; i++) {
1171 err = progress_cb(progress_arg, packfile_size, nours,
1177 m->off = ftello(packfile);
1178 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1181 if (m->delta_len == 0) {
1182 err = packhdr(&nh, buf, sizeof(buf),
1183 m->obj_type, raw->size);
1186 err = hwrite(packfile, buf, nh, &ctx);
1189 packfile_size += nh;
1190 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1191 err = got_error_from_errno("fseeko");
1194 err = got_deflate_to_file(&outlen, raw->f, packfile,
1198 packfile_size += outlen;
1201 if (delta_file == NULL) {
1202 delta_file = got_opentemp();
1203 if (delta_file == NULL) {
1204 err = got_error_from_errno(
1209 if (ftruncate(fileno(delta_file), 0L) == -1) {
1210 err = got_error_from_errno("ftruncate");
1213 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1214 err = got_error_from_errno("fseeko");
1217 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1219 err = got_error_from_errno("fseeko");
1222 remain = m->delta_len;
1223 while (remain > 0) {
1224 char delta_buf[8192];
1226 n = MIN(remain, sizeof(delta_buf));
1227 r = fread(delta_buf, 1, n, delta_cache);
1229 err = got_ferror(delta_cache,
1233 w = fwrite(delta_buf, 1, n, delta_file);
1235 err = got_ferror(delta_file,
1241 if (use_offset_deltas && m->prev->off != 0) {
1242 err = packhdr(&nh, buf, sizeof(buf),
1243 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1246 nh += packoff(buf + nh,
1247 m->off - m->prev->off);
1248 err = hwrite(packfile, buf, nh, &ctx);
1251 packfile_size += nh;
1253 err = packhdr(&nh, buf, sizeof(buf),
1254 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1255 err = hwrite(packfile, buf, nh, &ctx);
1258 packfile_size += nh;
1259 err = hwrite(packfile, m->prev->id.sha1,
1260 sizeof(m->prev->id.sha1), &ctx);
1261 packfile_size += sizeof(m->prev->id.sha1);
1265 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1266 err = got_error_from_errno("fseeko");
1269 err = got_deflate_to_file(&outlen, delta_file,
1273 packfile_size += outlen;
1275 got_object_raw_close(raw);
1278 SHA1Final(pack_sha1, &ctx);
1279 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1280 if (n != SHA1_DIGEST_LENGTH)
1281 err = got_ferror(packfile, GOT_ERR_IO);
1282 packfile_size += SHA1_DIGEST_LENGTH;
1283 packfile_size += sizeof(struct got_packfile_hdr);
1284 err = progress_cb(progress_arg, packfile_size, nours,
1285 nmeta, nmeta, nmeta);
1289 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1290 err = got_error_from_errno("fclose");
1292 got_object_raw_close(raw);
1293 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1294 err = got_error_from_errno("close");
1298 const struct got_error *
1299 got_pack_create(uint8_t *packsha1, FILE *packfile,
1300 struct got_object_id **theirs, int ntheirs,
1301 struct got_object_id **ours, int nours,
1302 struct got_repository *repo, int loose_obj_only, int allow_empty,
1303 got_pack_progress_cb progress_cb, void *progress_arg,
1304 got_cancel_cb cancel_cb, void *cancel_arg)
1306 const struct got_error *err;
1307 struct got_pack_meta **meta;
1309 FILE *delta_cache = NULL;
1311 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1312 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1316 if (nmeta == 0 && !allow_empty) {
1317 err = got_error(GOT_ERR_CANNOT_PACK);
1321 delta_cache = got_opentemp();
1322 if (delta_cache == NULL) {
1323 err = got_error_from_errno("got_opentemp");
1328 err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1329 progress_cb, progress_arg, cancel_cb, cancel_arg);
1332 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1333 err = got_error_from_errno("fseeko");
1338 err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1339 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1343 free_nmeta(meta, nmeta);
1344 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1345 err = got_error_from_errno("fclose");