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>
35 #include "got_error.h"
36 #include "got_cancel.h"
37 #include "got_object.h"
39 #include "got_reference.h"
40 #include "got_repository_admin.h"
41 #include "got_opentemp.h"
43 #include "got_lib_deltify.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_deflate.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_ratelimit.h"
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 struct got_pack_meta {
63 struct got_object_id id;
69 /* The best delta we picked */
70 struct got_pack_meta *head;
71 struct got_pack_meta *prev;
72 off_t delta_offset; /* offset in delta cache file */
73 off_t delta_len; /* length in delta cache file */
76 /* Only used for delta window */
77 struct got_delta_table *dtab;
79 /* Only used for writing offset deltas */
83 struct got_pack_metavec {
84 struct got_pack_meta **meta;
89 static const struct got_error *
90 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
91 const char *path, int obj_type, time_t mtime)
93 const struct got_error *err = NULL;
94 struct got_pack_meta *m;
98 m = calloc(1, sizeof(*m));
100 return got_error_from_errno("calloc");
102 memcpy(&m->id, id, sizeof(m->id));
104 m->path = strdup(path);
105 if (m->path == NULL) {
106 err = got_error_from_errno("strdup");
111 m->obj_type = obj_type;
118 clear_meta(struct got_pack_meta *meta)
127 free_nmeta(struct got_pack_meta **meta, int nmeta)
131 for (i = 0; i < nmeta; i++)
137 delta_order_cmp(const void *pa, const void *pb)
139 struct got_pack_meta *a, *b;
142 a = *(struct got_pack_meta **)pa;
143 b = *(struct got_pack_meta **)pb;
145 if (a->obj_type != b->obj_type)
146 return a->obj_type - b->obj_type;
147 cmp = strcmp(a->path, b->path);
150 if (a->mtime != b->mtime)
151 return a->mtime - b->mtime;
152 return got_object_id_cmp(&a->id, &b->id);
156 delta_size(struct got_delta_instruction *deltas, int ndeltas)
159 for (i = 0; i < ndeltas; i++) {
161 size += GOT_DELTA_SIZE_SHIFT;
163 size += deltas[i].len + 1;
168 static const struct got_error *
169 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
170 struct got_delta_instruction *deltas, int ndeltas,
171 off_t base_size, FILE *f)
173 unsigned char buf[16], *bp;
177 struct got_delta_instruction *d;
179 /* base object size */
180 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
181 n = base_size >> GOT_DELTA_SIZE_SHIFT;
182 for (i = 1; n > 0; i++) {
183 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
184 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
185 n >>= GOT_DELTA_SIZE_SHIFT;
187 w = fwrite(buf, 1, i, f);
189 return got_ferror(f, GOT_ERR_IO);
191 /* target object size */
192 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
193 n = o->size >> GOT_DELTA_SIZE_SHIFT;
194 for (i = 1; n > 0; i++) {
195 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
196 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
197 n >>= GOT_DELTA_SIZE_SHIFT;
199 w = fwrite(buf, 1, i, f);
201 return got_ferror(f, GOT_ERR_IO);
203 for (j = 0; j < ndeltas; j++) {
208 buf[0] = GOT_DELTA_BASE_COPY;
209 for (i = 0; i < 4; i++) {
210 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
219 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
220 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
221 for (i = 0; i < 3 && n > 0; i++) {
222 buf[0] |= 1 << (i + 4);
227 w = fwrite(buf, 1, bp - buf, f);
229 return got_ferror(f, GOT_ERR_IO);
230 } else if (o->f == NULL) {
232 while (n != d->len) {
233 buf[0] = (d->len - n < 127) ? d->len - n : 127;
234 w = fwrite(buf, 1, 1, f);
236 return got_ferror(f, GOT_ERR_IO);
237 w = fwrite(o->data + o->hdrlen + d->offset + n,
240 return got_ferror(f, GOT_ERR_IO);
246 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
247 return got_error_from_errno("fseeko");
249 while (n != d->len) {
250 buf[0] = (d->len - n < 127) ? d->len - n : 127;
251 w = fwrite(buf, 1, 1, f);
253 return got_ferror(f, GOT_ERR_IO);
254 r = fread(content, 1, buf[0], o->f);
256 return got_ferror(o->f, GOT_ERR_IO);
257 w = fwrite(content, 1, buf[0], f);
259 return got_ferror(f, GOT_ERR_IO);
268 static const struct got_error *
269 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
270 struct got_ratelimit *rl, off_t packfile_size, int ncommits,
271 int nobj_total, int obj_deltify, int nobj_written)
273 const struct got_error *err;
276 if (progress_cb == NULL)
279 err = got_ratelimit_check(&elapsed, rl);
283 return progress_cb(progress_arg, packfile_size, ncommits,
284 nobj_total, obj_deltify, nobj_written);
287 static const struct got_error *
288 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
289 FILE *delta_cache, struct got_repository *repo,
290 got_pack_progress_cb progress_cb, void *progress_arg,
291 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
293 const struct got_error *err = NULL;
294 struct got_pack_meta *m = NULL, *base = NULL;
295 struct got_raw_object *raw = NULL, *base_raw = NULL;
296 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
297 int i, j, size, best_size, ndeltas, best_ndeltas;
298 const int max_base_candidates = 3;
301 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
302 for (i = 0; i < nmeta; i++) {
304 err = (*cancel_cb)(cancel_arg);
308 err = report_progress(progress_cb, progress_arg, rl,
309 0L, nours, nmeta, i, 0);
314 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
315 m->obj_type == GOT_OBJ_TYPE_TAG)
318 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
323 if (raw->f == NULL) {
324 err = got_deltify_init_mem(&m->dtab, raw->data,
325 raw->hdrlen, raw->size + raw->hdrlen);
327 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
328 raw->size + raw->hdrlen);
333 if (i > max_base_candidates) {
334 struct got_pack_meta *n = NULL;
335 n = meta[i - (max_base_candidates + 1)];
336 got_deltify_free(n->dtab);
340 best_size = raw->size;
342 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
344 err = (*cancel_cb)(cancel_arg);
349 /* long chains make unpacking slow, avoid such bases */
350 if (base->nchain >= 128 ||
351 base->obj_type != m->obj_type)
354 err = got_object_raw_open(&base_raw, &outfd, repo,
358 if (raw->f == NULL && base_raw->f == NULL) {
359 err = got_deltify_mem_mem(&deltas, &ndeltas,
360 raw->data, raw->hdrlen,
361 raw->size + raw->hdrlen,
362 base->dtab, base_raw->data,
364 base_raw->size + base_raw->hdrlen);
365 } else if (raw->f == NULL) {
366 err = got_deltify_mem_file(&deltas, &ndeltas,
367 raw->data, raw->hdrlen,
368 raw->size + raw->hdrlen,
369 base->dtab, base_raw->f,
371 base_raw->size + base_raw->hdrlen);
372 } else if (base_raw->f == NULL) {
373 err = got_deltify_file_mem(&deltas, &ndeltas,
375 raw->size + raw->hdrlen,
376 base->dtab, base_raw->data,
378 base_raw->size + base_raw->hdrlen);
380 err = got_deltify(&deltas, &ndeltas,
382 raw->size + raw->hdrlen,
383 base->dtab, base_raw->f, base_raw->hdrlen,
384 base_raw->size + base_raw->hdrlen);
386 got_object_raw_close(base_raw);
391 size = delta_size(deltas, ndeltas);
392 if (size + 32 < best_size){
394 * if we already picked a best delta,
399 best_deltas = deltas;
400 best_ndeltas = ndeltas;
402 m->nchain = base->nchain + 1;
404 m->head = base->head;
414 if (best_ndeltas > 0) {
415 m->delta_offset = ftello(delta_cache);
416 err = encode_delta(m, raw, best_deltas,
417 best_ndeltas, m->prev->size, delta_cache);
423 m->delta_len = ftello(delta_cache) - m->delta_offset;
426 got_object_raw_close(raw);
430 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
431 got_deltify_free(meta[i]->dtab);
432 meta[i]->dtab = NULL;
435 got_object_raw_close(raw);
437 got_object_raw_close(base_raw);
438 if (outfd != -1 && close(outfd) == -1 && err == NULL)
439 err = got_error_from_errno("close");
445 static const struct got_error *
446 search_packidx(int *found, struct got_object_id *id,
447 struct got_repository *repo)
449 const struct got_error *err = NULL;
450 struct got_packidx *packidx = NULL;
455 err = got_repo_search_packidx(&packidx, &idx, repo, id);
457 *found = 1; /* object is already packed */
458 else if (err->code == GOT_ERR_NO_OBJ)
463 static const int obj_types[] = {
469 GOT_OBJ_TYPE_OFFSET_DELTA,
470 GOT_OBJ_TYPE_REF_DELTA
473 static const struct got_error *
474 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
475 struct got_object_id *id, const char *path, int obj_type,
476 time_t mtime, int loose_obj_only, struct got_repository *repo)
478 const struct got_error *err;
479 struct got_pack_meta *m;
481 if (loose_obj_only) {
483 err = search_packidx(&is_packed, id, repo);
490 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
497 err = alloc_meta(&m, id, path, obj_type, mtime);
501 if (v->nmeta == v->metasz){
502 size_t newsize = 2 * v->metasz;
503 struct got_pack_meta **new;
504 new = reallocarray(v->meta, newsize, sizeof(*new));
506 err = got_error_from_errno("reallocarray");
517 v->meta[v->nmeta++] = m;
522 static const struct got_error *
523 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
524 struct got_object_idset *idset, struct got_object_id *tree_id,
525 const char *dpath, time_t mtime, struct got_repository *repo,
526 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
528 const struct got_error *err;
529 struct got_tree_object *tree;
533 err = got_object_open_as_tree(&tree, repo, tree_id);
537 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
538 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
539 struct got_object_id *id = got_tree_entry_get_id(e);
540 mode_t mode = got_tree_entry_get_mode(e);
543 err = (*cancel_cb)(cancel_arg);
548 if (got_object_tree_entry_is_submodule(e) ||
549 got_object_idset_contains(idset, id))
552 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
553 got_tree_entry_get_name(e)) == -1) {
554 err = got_error_from_errno("asprintf");
559 struct got_object_qid *qid;
560 err = got_object_qid_alloc(&qid, id);
563 STAILQ_INSERT_TAIL(ids, qid, entry);
564 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
565 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
566 mtime, loose_obj_only, repo);
574 got_object_tree_close(tree);
579 static const struct got_error *
580 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
581 struct got_object_id *tree_id, const char *dpath, time_t mtime,
582 int loose_obj_only, struct got_repository *repo,
583 got_cancel_cb cancel_cb, void *cancel_arg)
585 const struct got_error *err = NULL;
586 struct got_object_id_queue tree_ids;
587 struct got_object_qid *qid;
589 if (got_object_idset_contains(idset, tree_id))
592 err = got_object_qid_alloc(&qid, tree_id);
596 STAILQ_INIT(&tree_ids);
597 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
599 while (!STAILQ_EMPTY(&tree_ids)) {
601 err = (*cancel_cb)(cancel_arg);
606 qid = STAILQ_FIRST(&tree_ids);
607 STAILQ_REMOVE_HEAD(&tree_ids, entry);
609 if (got_object_idset_contains(idset, qid->id)) {
610 got_object_qid_free(qid);
614 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
615 mtime, loose_obj_only, repo);
617 got_object_qid_free(qid);
621 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
622 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
623 got_object_qid_free(qid);
628 got_object_id_queue_free(&tree_ids);
632 static const struct got_error *
633 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
634 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
635 got_cancel_cb cancel_cb, void *cancel_arg)
637 const struct got_error *err;
638 struct got_commit_object *commit;
640 if (got_object_idset_contains(idset, id))
643 if (loose_obj_only) {
645 err = search_packidx(&is_packed, id, repo);
652 err = got_object_open_as_commit(&commit, repo, id);
656 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
657 got_object_commit_get_committer_time(commit),
658 loose_obj_only, repo);
662 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
663 "", got_object_commit_get_committer_time(commit),
664 loose_obj_only, repo, cancel_cb, cancel_arg);
666 got_object_commit_close(commit);
670 static const struct got_error *
671 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
672 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
673 got_cancel_cb cancel_cb, void *cancel_arg)
675 const struct got_error *err;
676 struct got_tag_object *tag = NULL;
678 if (got_object_idset_contains(idset, id))
681 if (loose_obj_only) {
683 err = search_packidx(&is_packed, id, repo);
690 err = got_object_open_as_tag(&tag, repo, id);
694 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
695 got_object_tag_get_tagger_time(tag),
696 loose_obj_only, repo);
700 switch (got_object_tag_get_object_type(tag)) {
701 case GOT_OBJ_TYPE_COMMIT:
702 err = load_commit(v, idset,
703 got_object_tag_get_object_id(tag), repo,
704 loose_obj_only, cancel_cb, cancel_arg);
706 case GOT_OBJ_TYPE_TREE:
707 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
708 "", got_object_tag_get_tagger_time(tag),
709 loose_obj_only, repo, cancel_cb, cancel_arg);
716 got_object_tag_close(tag);
720 enum findtwixt_color {
725 static const int findtwixt_colors[] = {
731 static const struct got_error *
732 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
733 int color, struct got_repository *repo)
735 const struct got_error *err;
736 struct got_object_qid *qid;
738 err = got_object_qid_alloc(&qid, id);
742 STAILQ_INSERT_TAIL(ids, qid, entry);
743 qid->data = (void *)&findtwixt_colors[color];
747 static const struct got_error *
748 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
749 struct got_object_id *id, struct got_repository *repo,
750 got_cancel_cb cancel_cb, void *cancel_arg)
752 const struct got_error *err = NULL;
753 struct got_commit_object *commit;
754 const struct got_object_id_queue *parents;
755 struct got_object_id_queue ids;
756 struct got_object_qid *qid;
760 err = got_object_qid_alloc(&qid, id);
763 STAILQ_INSERT_HEAD(&ids, qid, entry);
765 while (!STAILQ_EMPTY(&ids)) {
767 err = (*cancel_cb)(cancel_arg);
772 qid = STAILQ_FIRST(&ids);
773 STAILQ_REMOVE_HEAD(&ids, entry);
775 if (got_object_idset_contains(drop, qid->id)) {
776 got_object_qid_free(qid);
780 err = got_object_idset_add(drop, qid->id,
781 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
783 got_object_qid_free(qid);
787 if (!got_object_idset_contains(keep, qid->id)) {
788 got_object_qid_free(qid);
792 err = got_object_open_as_commit(&commit, repo, qid->id);
793 got_object_qid_free(qid);
797 parents = got_object_commit_get_parent_ids(commit);
799 err = got_object_id_queue_copy(parents, &ids);
801 got_object_commit_close(commit);
805 got_object_commit_close(commit);
808 got_object_id_queue_free(&ids);
812 struct append_id_arg {
813 struct got_object_id **array;
817 static const struct got_error *
818 append_id(struct got_object_id *id, void *data, void *arg)
820 struct append_id_arg *a = arg;
822 a->array[a->idx] = got_object_id_dup(id);
823 if (a->array[a->idx] == NULL)
824 return got_error_from_errno("got_object_id_dup");
830 static const struct got_error *
831 findtwixt(struct got_object_id ***res, int *nres,
832 struct got_object_id **head, int nhead,
833 struct got_object_id **tail, int ntail,
834 struct got_repository *repo,
835 got_cancel_cb cancel_cb, void *cancel_arg)
837 const struct got_error *err = NULL;
838 struct got_object_id_queue ids;
839 struct got_object_idset *keep, *drop;
840 struct got_object_qid *qid;
841 int i, ncolor, nkeep, obj_type;
847 keep = got_object_idset_alloc();
849 return got_error_from_errno("got_object_idset_alloc");
851 drop = got_object_idset_alloc();
853 err = got_error_from_errno("got_object_idset_alloc");
857 for (i = 0; i < nhead; i++) {
858 struct got_object_id *id = head[i];
861 err = got_object_get_type(&obj_type, repo, id);
864 if (obj_type != GOT_OBJ_TYPE_COMMIT)
866 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
870 for (i = 0; i < ntail; i++) {
871 struct got_object_id *id = tail[i];
874 err = got_object_get_type(&obj_type, repo, id);
877 if (obj_type != GOT_OBJ_TYPE_COMMIT)
879 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
884 while (!STAILQ_EMPTY(&ids)) {
886 qid = STAILQ_FIRST(&ids);
887 qcolor = *((int *)qid->data);
889 if (got_object_idset_contains(drop, qid->id))
891 else if (got_object_idset_contains(keep, qid->id))
894 ncolor = COLOR_BLANK;
896 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
897 qcolor == COLOR_KEEP)) {
898 STAILQ_REMOVE_HEAD(&ids, entry);
899 got_object_qid_free(qid);
903 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
904 err = drop_commit(keep, drop, qid->id, repo,
905 cancel_cb, cancel_arg);
908 } else if (ncolor == COLOR_BLANK) {
909 struct got_commit_object *commit;
910 struct got_object_id *id;
911 const struct got_object_id_queue *parents;
912 struct got_object_qid *pid;
914 id = got_object_id_dup(qid->id);
916 err = got_error_from_errno("got_object_id_dup");
919 if (qcolor == COLOR_KEEP)
920 err = got_object_idset_add(keep, id,
921 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
923 err = got_object_idset_add(drop, id,
924 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
930 err = got_object_open_as_commit(&commit, repo, id);
935 parents = got_object_commit_get_parent_ids(commit);
937 STAILQ_FOREACH(pid, parents, entry) {
938 err = queue_commit_id(&ids, pid->id,
946 got_object_commit_close(commit);
949 /* should not happen */
950 err = got_error_fmt(GOT_ERR_NOT_IMPL,
951 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
955 STAILQ_REMOVE_HEAD(&ids, entry);
956 got_object_qid_free(qid);
959 nkeep = got_object_idset_num_elements(keep);
961 struct append_id_arg arg;
962 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
963 if (arg.array == NULL) {
964 err = got_error_from_errno("calloc");
968 err = got_object_idset_for_each(keep, append_id, &arg);
977 got_object_idset_free(keep);
978 got_object_idset_free(drop);
979 got_object_id_queue_free(&ids);
983 static const struct got_error *
984 read_meta(struct got_pack_meta ***meta, int *nmeta,
985 struct got_object_id **theirs, int ntheirs,
986 struct got_object_id **ours, int nours, struct got_repository *repo,
987 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
988 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
990 const struct got_error *err = NULL;
991 struct got_object_id **ids = NULL;
992 struct got_object_idset *idset;
993 int i, nobj = 0, obj_type;
994 struct got_pack_metavec v;
999 idset = got_object_idset_alloc();
1001 return got_error_from_errno("got_object_idset_alloc");
1005 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
1006 if (v.meta == NULL) {
1007 err = got_error_from_errno("calloc");
1011 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1012 cancel_cb, cancel_arg);
1013 if (err || nobj == 0)
1016 for (i = 0; i < ntheirs; i++) {
1017 struct got_object_id *id = theirs[i];
1020 err = got_object_get_type(&obj_type, repo, id);
1023 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1025 err = load_commit(NULL, idset, id, repo,
1026 loose_obj_only, cancel_cb, cancel_arg);
1029 err = report_progress(progress_cb, progress_arg, rl,
1030 0L, nours, v.nmeta, 0, 0);
1035 for (i = 0; i < ntheirs; i++) {
1036 struct got_object_id *id = theirs[i];
1040 cached_type = got_object_idset_get(idset, id);
1041 if (cached_type == NULL) {
1042 err = got_object_get_type(&obj_type, repo, id);
1046 obj_type = *cached_type;
1047 if (obj_type != GOT_OBJ_TYPE_TAG)
1049 err = load_tag(NULL, idset, id, repo,
1050 loose_obj_only, cancel_cb, cancel_arg);
1053 err = report_progress(progress_cb, progress_arg, rl,
1054 0L, nours, v.nmeta, 0, 0);
1059 for (i = 0; i < nobj; i++) {
1060 err = load_commit(&v, idset, ids[i], repo,
1061 loose_obj_only, cancel_cb, cancel_arg);
1066 err = report_progress(progress_cb, progress_arg, rl,
1067 0L, nours, v.nmeta, 0, 0);
1072 for (i = 0; i < nours; i++) {
1073 struct got_object_id *id = ours[i];
1077 cached_type = got_object_idset_get(idset, id);
1078 if (cached_type == NULL) {
1079 err = got_object_get_type(&obj_type, repo, id);
1083 obj_type = *cached_type;
1084 if (obj_type != GOT_OBJ_TYPE_TAG)
1086 err = load_tag(&v, idset, id, repo,
1087 loose_obj_only, cancel_cb, cancel_arg);
1090 err = report_progress(progress_cb, progress_arg, rl,
1091 0L, nours, v.nmeta, 0, 0);
1097 err = progress_cb(progress_arg, 0L, nours, v.nmeta, 0, 0);
1102 for (i = 0; i < nobj; i++) {
1106 got_object_idset_free(idset);
1116 const struct got_error *
1117 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1121 SHA1Update(ctx, buf, len);
1122 n = fwrite(buf, 1, len, f);
1124 return got_ferror(f, GOT_ERR_IO);
1129 putbe32(char *b, uint32_t n)
1138 write_order_cmp(const void *pa, const void *pb)
1140 struct got_pack_meta *a, *b, *ahd, *bhd;
1142 a = *(struct got_pack_meta **)pa;
1143 b = *(struct got_pack_meta **)pb;
1144 ahd = (a->head == NULL) ? a : a->head;
1145 bhd = (b->head == NULL) ? b : b->head;
1146 if (ahd->mtime != bhd->mtime)
1147 return bhd->mtime - ahd->mtime;
1149 return (uintptr_t)bhd - (uintptr_t)ahd;
1150 if (a->nchain != b->nchain)
1151 return a->nchain - b->nchain;
1152 return a->mtime - b->mtime;
1155 static const struct got_error *
1156 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1162 hdr[0] = obj_type << 4;
1163 hdr[0] |= len & 0xf;
1165 for (i = 1; len != 0; i++){
1167 return got_error(GOT_ERR_NO_SPACE);
1168 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1169 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1170 len >>= GOT_DELTA_SIZE_SHIFT;
1178 packoff(char *hdr, off_t off)
1183 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1184 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1185 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1186 GOT_DELTA_SIZE_MORE;
1191 hdr[j++] = rbuf[--i];
1195 static const struct got_error *
1196 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1197 struct got_pack_meta **meta, int nmeta, int nours,
1198 int use_offset_deltas, struct got_repository *repo,
1199 got_pack_progress_cb progress_cb, void *progress_arg,
1200 struct got_ratelimit *rl,
1201 got_cancel_cb cancel_cb, void *cancel_arg)
1203 const struct got_error *err = NULL;
1206 struct got_pack_meta *m;
1207 struct got_raw_object *raw = NULL;
1208 FILE *delta_file = NULL;
1211 struct got_deflate_checksum csum;
1212 off_t packfile_size = 0;
1216 csum.output_sha1 = &ctx;
1217 csum.output_crc = NULL;
1219 err = hwrite(packfile, "PACK", 4, &ctx);
1222 putbe32(buf, GOT_PACKFILE_VERSION);
1223 err = hwrite(packfile, buf, 4, &ctx);
1226 putbe32(buf, nmeta);
1227 err = hwrite(packfile, buf, 4, &ctx);
1230 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1231 for (i = 0; i < nmeta; i++) {
1232 err = report_progress(progress_cb, progress_arg, rl,
1233 packfile_size, nours, nmeta, nmeta, i);
1237 m->off = ftello(packfile);
1238 if (m->delta_len == 0) {
1239 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1242 err = packhdr(&nh, buf, sizeof(buf),
1243 m->obj_type, raw->size);
1246 err = hwrite(packfile, buf, nh, &ctx);
1249 packfile_size += nh;
1250 if (raw->f == NULL) {
1251 err = got_deflate_to_file_mmap(&outlen,
1252 raw->data + raw->hdrlen, 0, raw->size,
1257 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1259 err = got_error_from_errno("fseeko");
1262 err = got_deflate_to_file(&outlen, raw->f,
1267 packfile_size += outlen;
1268 got_object_raw_close(raw);
1272 if (delta_file == NULL) {
1273 delta_file = got_opentemp();
1274 if (delta_file == NULL) {
1275 err = got_error_from_errno(
1280 if (ftruncate(fileno(delta_file), 0L) == -1) {
1281 err = got_error_from_errno("ftruncate");
1284 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1285 err = got_error_from_errno("fseeko");
1288 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1290 err = got_error_from_errno("fseeko");
1293 remain = m->delta_len;
1294 while (remain > 0) {
1295 char delta_buf[8192];
1297 n = MIN(remain, sizeof(delta_buf));
1298 r = fread(delta_buf, 1, n, delta_cache);
1300 err = got_ferror(delta_cache,
1304 w = fwrite(delta_buf, 1, n, delta_file);
1306 err = got_ferror(delta_file,
1312 if (use_offset_deltas && m->prev->off != 0) {
1313 err = packhdr(&nh, buf, sizeof(buf),
1314 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1317 nh += packoff(buf + nh,
1318 m->off - m->prev->off);
1319 err = hwrite(packfile, buf, nh, &ctx);
1322 packfile_size += nh;
1324 err = packhdr(&nh, buf, sizeof(buf),
1325 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1326 err = hwrite(packfile, buf, nh, &ctx);
1329 packfile_size += nh;
1330 err = hwrite(packfile, m->prev->id.sha1,
1331 sizeof(m->prev->id.sha1), &ctx);
1332 packfile_size += sizeof(m->prev->id.sha1);
1336 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1337 err = got_error_from_errno("fseeko");
1340 err = got_deflate_to_file(&outlen, delta_file,
1344 packfile_size += outlen;
1347 SHA1Final(pack_sha1, &ctx);
1348 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1349 if (n != SHA1_DIGEST_LENGTH)
1350 err = got_ferror(packfile, GOT_ERR_IO);
1351 packfile_size += SHA1_DIGEST_LENGTH;
1352 packfile_size += sizeof(struct got_packfile_hdr);
1354 err = progress_cb(progress_arg, packfile_size, nours,
1355 nmeta, nmeta, nmeta);
1360 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1361 err = got_error_from_errno("fclose");
1363 got_object_raw_close(raw);
1364 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1365 err = got_error_from_errno("close");
1369 const struct got_error *
1370 got_pack_create(uint8_t *packsha1, FILE *packfile,
1371 struct got_object_id **theirs, int ntheirs,
1372 struct got_object_id **ours, int nours,
1373 struct got_repository *repo, int loose_obj_only, int allow_empty,
1374 got_pack_progress_cb progress_cb, void *progress_arg,
1375 got_cancel_cb cancel_cb, void *cancel_arg)
1377 const struct got_error *err;
1378 struct got_pack_meta **meta;
1380 FILE *delta_cache = NULL;
1381 struct got_ratelimit rl;
1383 got_ratelimit_init(&rl, 0, 500);
1385 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1386 loose_obj_only, progress_cb, progress_arg, &rl,
1387 cancel_cb, cancel_arg);
1391 if (nmeta == 0 && !allow_empty) {
1392 err = got_error(GOT_ERR_CANNOT_PACK);
1396 delta_cache = got_opentemp();
1397 if (delta_cache == NULL) {
1398 err = got_error_from_errno("got_opentemp");
1403 err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1404 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1407 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1408 err = got_error_from_errno("fseeko");
1413 err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1414 repo, progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1418 free_nmeta(meta, nmeta);
1419 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1420 err = got_error_from_errno("fclose");