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>
31 #if defined(__FreeBSD__)
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);
233 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
234 return got_error_from_errno("fseeko");
236 while (n != d->len) {
237 buf[0] = (d->len - n < 127) ? d->len - n : 127;
238 w = fwrite(buf, 1, 1, f);
240 return got_ferror(f, GOT_ERR_IO);
241 r = fread(content, 1, buf[0], o->f);
243 return got_ferror(o->f, GOT_ERR_IO);
244 w = fwrite(content, 1, buf[0], f);
246 return got_ferror(f, GOT_ERR_IO);
255 static const struct got_error *
256 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
257 struct got_ratelimit *rl, off_t packfile_size, int ncommits,
258 int nobj_total, int obj_deltify, int nobj_written)
260 const struct got_error *err;
263 if (progress_cb == NULL)
266 err = got_ratelimit_check(&elapsed, rl);
270 return progress_cb(progress_arg, packfile_size, ncommits,
271 nobj_total, obj_deltify, nobj_written);
274 static const struct got_error *
275 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
276 FILE *delta_cache, struct got_repository *repo,
277 got_pack_progress_cb progress_cb, void *progress_arg,
278 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
280 const struct got_error *err = NULL;
281 struct got_pack_meta *m = NULL, *base = NULL;
282 struct got_raw_object *raw = NULL, *base_raw = NULL;
283 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
284 int i, j, size, best_size, ndeltas, best_ndeltas;
285 const int max_base_candidates = 3;
288 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
289 for (i = 0; i < nmeta; i++) {
291 err = (*cancel_cb)(cancel_arg);
295 err = report_progress(progress_cb, progress_arg, rl,
296 0L, nours, nmeta, i, 0);
301 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
302 m->obj_type == GOT_OBJ_TYPE_TAG)
305 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
310 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
311 raw->size + raw->hdrlen);
315 if (i > max_base_candidates) {
316 struct got_pack_meta *n = NULL;
317 n = meta[i - (max_base_candidates + 1)];
318 got_deltify_free(n->dtab);
322 best_size = raw->size;
324 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
326 err = (*cancel_cb)(cancel_arg);
331 /* long chains make unpacking slow, avoid such bases */
332 if (base->nchain >= 128 ||
333 base->obj_type != m->obj_type)
336 err = got_object_raw_open(&base_raw, &outfd, repo,
340 err = got_deltify(&deltas, &ndeltas,
341 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
342 base->dtab, base_raw->f, base_raw->hdrlen,
343 base_raw->size + base_raw->hdrlen);
344 got_object_raw_close(base_raw);
349 size = delta_size(deltas, ndeltas);
350 if (size + 32 < best_size){
352 * if we already picked a best delta,
357 best_deltas = deltas;
358 best_ndeltas = ndeltas;
360 m->nchain = base->nchain + 1;
362 m->head = base->head;
372 if (best_ndeltas > 0) {
373 m->delta_offset = ftello(delta_cache);
374 err = encode_delta(m, raw, best_deltas,
375 best_ndeltas, m->prev->size, delta_cache);
381 m->delta_len = ftello(delta_cache) - m->delta_offset;
384 got_object_raw_close(raw);
388 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
389 got_deltify_free(meta[i]->dtab);
390 meta[i]->dtab = NULL;
393 got_object_raw_close(raw);
395 got_object_raw_close(base_raw);
396 if (outfd != -1 && close(outfd) == -1 && err == NULL)
397 err = got_error_from_errno("close");
403 static const struct got_error *
404 search_packidx(int *found, struct got_object_id *id,
405 struct got_repository *repo)
407 const struct got_error *err = NULL;
408 struct got_packidx *packidx = NULL;
413 err = got_repo_search_packidx(&packidx, &idx, repo, id);
415 *found = 1; /* object is already packed */
416 else if (err->code == GOT_ERR_NO_OBJ)
421 static const int obj_types[] = {
427 GOT_OBJ_TYPE_OFFSET_DELTA,
428 GOT_OBJ_TYPE_REF_DELTA
431 static const struct got_error *
432 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
433 struct got_object_id *id, const char *path, int obj_type,
434 time_t mtime, int loose_obj_only, struct got_repository *repo)
436 const struct got_error *err;
437 struct got_pack_meta *m;
439 if (loose_obj_only) {
441 err = search_packidx(&is_packed, id, repo);
448 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
455 err = alloc_meta(&m, id, path, obj_type, mtime);
459 if (v->nmeta == v->metasz){
460 size_t newsize = 2 * v->metasz;
461 struct got_pack_meta **new;
462 new = reallocarray(v->meta, newsize, sizeof(*new));
464 err = got_error_from_errno("reallocarray");
475 v->meta[v->nmeta++] = m;
480 static const struct got_error *
481 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
482 struct got_object_idset *idset, struct got_object_id *tree_id,
483 const char *dpath, time_t mtime, struct got_repository *repo,
484 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
486 const struct got_error *err;
487 struct got_tree_object *tree;
491 err = got_object_open_as_tree(&tree, repo, tree_id);
495 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
496 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
497 struct got_object_id *id = got_tree_entry_get_id(e);
498 mode_t mode = got_tree_entry_get_mode(e);
501 err = (*cancel_cb)(cancel_arg);
506 if (got_object_tree_entry_is_submodule(e) ||
507 got_object_idset_contains(idset, id))
510 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
511 got_tree_entry_get_name(e)) == -1) {
512 err = got_error_from_errno("asprintf");
517 struct got_object_qid *qid;
518 err = got_object_qid_alloc(&qid, id);
521 STAILQ_INSERT_TAIL(ids, qid, entry);
522 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
523 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
524 mtime, loose_obj_only, repo);
532 got_object_tree_close(tree);
537 static const struct got_error *
538 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
539 struct got_object_id *tree_id, const char *dpath, time_t mtime,
540 int loose_obj_only, struct got_repository *repo,
541 got_cancel_cb cancel_cb, void *cancel_arg)
543 const struct got_error *err = NULL;
544 struct got_object_id_queue tree_ids;
545 struct got_object_qid *qid;
547 if (got_object_idset_contains(idset, tree_id))
550 err = got_object_qid_alloc(&qid, tree_id);
554 STAILQ_INIT(&tree_ids);
555 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
557 while (!STAILQ_EMPTY(&tree_ids)) {
559 err = (*cancel_cb)(cancel_arg);
564 qid = STAILQ_FIRST(&tree_ids);
565 STAILQ_REMOVE_HEAD(&tree_ids, entry);
567 if (got_object_idset_contains(idset, qid->id)) {
568 got_object_qid_free(qid);
572 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
573 mtime, loose_obj_only, repo);
575 got_object_qid_free(qid);
579 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
580 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
581 got_object_qid_free(qid);
586 got_object_id_queue_free(&tree_ids);
590 static const struct got_error *
591 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
592 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
593 got_cancel_cb cancel_cb, void *cancel_arg)
595 const struct got_error *err;
596 struct got_commit_object *commit;
598 if (got_object_idset_contains(idset, id))
601 if (loose_obj_only) {
603 err = search_packidx(&is_packed, id, repo);
610 err = got_object_open_as_commit(&commit, repo, id);
614 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
615 got_object_commit_get_committer_time(commit),
616 loose_obj_only, repo);
620 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
621 "", got_object_commit_get_committer_time(commit),
622 loose_obj_only, repo, cancel_cb, cancel_arg);
624 got_object_commit_close(commit);
628 static const struct got_error *
629 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
630 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
631 got_cancel_cb cancel_cb, void *cancel_arg)
633 const struct got_error *err;
634 struct got_tag_object *tag = NULL;
636 if (got_object_idset_contains(idset, id))
639 if (loose_obj_only) {
641 err = search_packidx(&is_packed, id, repo);
648 err = got_object_open_as_tag(&tag, repo, id);
652 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
653 got_object_tag_get_tagger_time(tag),
654 loose_obj_only, repo);
658 switch (got_object_tag_get_object_type(tag)) {
659 case GOT_OBJ_TYPE_COMMIT:
660 err = load_commit(v, idset,
661 got_object_tag_get_object_id(tag), repo,
662 loose_obj_only, cancel_cb, cancel_arg);
664 case GOT_OBJ_TYPE_TREE:
665 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
666 "", got_object_tag_get_tagger_time(tag),
667 loose_obj_only, repo, cancel_cb, cancel_arg);
674 got_object_tag_close(tag);
678 enum findtwixt_color {
683 static const int findtwixt_colors[] = {
689 static const struct got_error *
690 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
691 int color, struct got_repository *repo)
693 const struct got_error *err;
694 struct got_object_qid *qid;
696 err = got_object_qid_alloc(&qid, id);
700 STAILQ_INSERT_TAIL(ids, qid, entry);
701 qid->data = (void *)&findtwixt_colors[color];
705 static const struct got_error *
706 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
707 struct got_object_id *id, struct got_repository *repo,
708 got_cancel_cb cancel_cb, void *cancel_arg)
710 const struct got_error *err = NULL;
711 struct got_commit_object *commit;
712 const struct got_object_id_queue *parents;
713 struct got_object_id_queue ids;
714 struct got_object_qid *qid;
718 err = got_object_qid_alloc(&qid, id);
721 STAILQ_INSERT_HEAD(&ids, qid, entry);
723 while (!STAILQ_EMPTY(&ids)) {
725 err = (*cancel_cb)(cancel_arg);
730 qid = STAILQ_FIRST(&ids);
731 STAILQ_REMOVE_HEAD(&ids, entry);
733 if (got_object_idset_contains(drop, qid->id)) {
734 got_object_qid_free(qid);
738 err = got_object_idset_add(drop, qid->id,
739 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
741 got_object_qid_free(qid);
745 if (!got_object_idset_contains(keep, qid->id)) {
746 got_object_qid_free(qid);
750 err = got_object_open_as_commit(&commit, repo, qid->id);
751 got_object_qid_free(qid);
755 parents = got_object_commit_get_parent_ids(commit);
757 err = got_object_id_queue_copy(parents, &ids);
759 got_object_commit_close(commit);
763 got_object_commit_close(commit);
766 got_object_id_queue_free(&ids);
770 struct append_id_arg {
771 struct got_object_id **array;
775 static const struct got_error *
776 append_id(struct got_object_id *id, void *data, void *arg)
778 struct append_id_arg *a = arg;
780 a->array[a->idx] = got_object_id_dup(id);
781 if (a->array[a->idx] == NULL)
782 return got_error_from_errno("got_object_id_dup");
788 static const struct got_error *
789 findtwixt(struct got_object_id ***res, int *nres,
790 struct got_object_id **head, int nhead,
791 struct got_object_id **tail, int ntail,
792 struct got_repository *repo,
793 got_cancel_cb cancel_cb, void *cancel_arg)
795 const struct got_error *err = NULL;
796 struct got_object_id_queue ids;
797 struct got_object_idset *keep, *drop;
798 struct got_object_qid *qid;
799 int i, ncolor, nkeep, obj_type;
805 keep = got_object_idset_alloc();
807 return got_error_from_errno("got_object_idset_alloc");
809 drop = got_object_idset_alloc();
811 err = got_error_from_errno("got_object_idset_alloc");
815 for (i = 0; i < nhead; i++) {
816 struct got_object_id *id = head[i];
819 err = got_object_get_type(&obj_type, repo, id);
822 if (obj_type != GOT_OBJ_TYPE_COMMIT)
824 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
828 for (i = 0; i < ntail; i++) {
829 struct got_object_id *id = tail[i];
832 err = got_object_get_type(&obj_type, repo, id);
835 if (obj_type != GOT_OBJ_TYPE_COMMIT)
837 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
842 while (!STAILQ_EMPTY(&ids)) {
844 qid = STAILQ_FIRST(&ids);
845 qcolor = *((int *)qid->data);
847 if (got_object_idset_contains(drop, qid->id))
849 else if (got_object_idset_contains(keep, qid->id))
852 ncolor = COLOR_BLANK;
854 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
855 qcolor == COLOR_KEEP)) {
856 STAILQ_REMOVE_HEAD(&ids, entry);
857 got_object_qid_free(qid);
861 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
862 err = drop_commit(keep, drop, qid->id, repo,
863 cancel_cb, cancel_arg);
866 } else if (ncolor == COLOR_BLANK) {
867 struct got_commit_object *commit;
868 struct got_object_id *id;
869 const struct got_object_id_queue *parents;
870 struct got_object_qid *pid;
872 id = got_object_id_dup(qid->id);
874 err = got_error_from_errno("got_object_id_dup");
877 if (qcolor == COLOR_KEEP)
878 err = got_object_idset_add(keep, id,
879 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
881 err = got_object_idset_add(drop, id,
882 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
888 err = got_object_open_as_commit(&commit, repo, id);
893 parents = got_object_commit_get_parent_ids(commit);
895 STAILQ_FOREACH(pid, parents, entry) {
896 err = queue_commit_id(&ids, pid->id,
904 got_object_commit_close(commit);
907 /* should not happen */
908 err = got_error_fmt(GOT_ERR_NOT_IMPL,
909 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
913 STAILQ_REMOVE_HEAD(&ids, entry);
914 got_object_qid_free(qid);
917 nkeep = got_object_idset_num_elements(keep);
919 struct append_id_arg arg;
920 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
921 if (arg.array == NULL) {
922 err = got_error_from_errno("calloc");
926 err = got_object_idset_for_each(keep, append_id, &arg);
935 got_object_idset_free(keep);
936 got_object_idset_free(drop);
937 got_object_id_queue_free(&ids);
941 static const struct got_error *
942 read_meta(struct got_pack_meta ***meta, int *nmeta,
943 struct got_object_id **theirs, int ntheirs,
944 struct got_object_id **ours, int nours, struct got_repository *repo,
945 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
946 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
948 const struct got_error *err = NULL;
949 struct got_object_id **ids = NULL;
950 struct got_object_idset *idset;
951 int i, nobj = 0, obj_type;
952 struct got_pack_metavec v;
957 idset = got_object_idset_alloc();
959 return got_error_from_errno("got_object_idset_alloc");
963 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
964 if (v.meta == NULL) {
965 err = got_error_from_errno("calloc");
969 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
970 cancel_cb, cancel_arg);
971 if (err || nobj == 0)
974 for (i = 0; i < ntheirs; i++) {
975 struct got_object_id *id = theirs[i];
978 err = got_object_get_type(&obj_type, repo, id);
981 if (obj_type != GOT_OBJ_TYPE_COMMIT)
983 err = load_commit(NULL, idset, id, repo,
984 loose_obj_only, cancel_cb, cancel_arg);
987 err = report_progress(progress_cb, progress_arg, rl,
988 0L, nours, v.nmeta, 0, 0);
993 for (i = 0; i < ntheirs; i++) {
994 struct got_object_id *id = theirs[i];
998 cached_type = got_object_idset_get(idset, id);
999 if (cached_type == NULL) {
1000 err = got_object_get_type(&obj_type, repo, id);
1004 obj_type = *cached_type;
1005 if (obj_type != GOT_OBJ_TYPE_TAG)
1007 err = load_tag(NULL, idset, id, repo,
1008 loose_obj_only, cancel_cb, cancel_arg);
1011 err = report_progress(progress_cb, progress_arg, rl,
1012 0L, nours, v.nmeta, 0, 0);
1017 for (i = 0; i < nobj; i++) {
1018 err = load_commit(&v, idset, ids[i], repo,
1019 loose_obj_only, cancel_cb, cancel_arg);
1024 err = report_progress(progress_cb, progress_arg, rl,
1025 0L, nours, v.nmeta, 0, 0);
1030 for (i = 0; i < nours; i++) {
1031 struct got_object_id *id = ours[i];
1035 cached_type = got_object_idset_get(idset, id);
1036 if (cached_type == NULL) {
1037 err = got_object_get_type(&obj_type, repo, id);
1041 obj_type = *cached_type;
1042 if (obj_type != GOT_OBJ_TYPE_TAG)
1044 err = load_tag(&v, idset, id, repo,
1045 loose_obj_only, cancel_cb, cancel_arg);
1048 err = report_progress(progress_cb, progress_arg, rl,
1049 0L, nours, v.nmeta, 0, 0);
1055 err = progress_cb(progress_arg, 0L, nours, v.nmeta, 0, 0);
1060 for (i = 0; i < nobj; i++) {
1064 got_object_idset_free(idset);
1074 const struct got_error *
1075 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1079 SHA1Update(ctx, buf, len);
1080 n = fwrite(buf, 1, len, f);
1082 return got_ferror(f, GOT_ERR_IO);
1087 putbe32(char *b, uint32_t n)
1096 write_order_cmp(const void *pa, const void *pb)
1098 struct got_pack_meta *a, *b, *ahd, *bhd;
1100 a = *(struct got_pack_meta **)pa;
1101 b = *(struct got_pack_meta **)pb;
1102 ahd = (a->head == NULL) ? a : a->head;
1103 bhd = (b->head == NULL) ? b : b->head;
1104 if (ahd->mtime != bhd->mtime)
1105 return bhd->mtime - ahd->mtime;
1107 return (uintptr_t)bhd - (uintptr_t)ahd;
1108 if (a->nchain != b->nchain)
1109 return a->nchain - b->nchain;
1110 return a->mtime - b->mtime;
1113 static const struct got_error *
1114 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1120 hdr[0] = obj_type << 4;
1121 hdr[0] |= len & 0xf;
1123 for (i = 1; len != 0; i++){
1125 return got_error(GOT_ERR_NO_SPACE);
1126 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1127 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1128 len >>= GOT_DELTA_SIZE_SHIFT;
1136 packoff(char *hdr, off_t off)
1141 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1142 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1143 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1144 GOT_DELTA_SIZE_MORE;
1149 hdr[j++] = rbuf[--i];
1153 static const struct got_error *
1154 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1155 struct got_pack_meta **meta, int nmeta, int nours,
1156 int use_offset_deltas, struct got_repository *repo,
1157 got_pack_progress_cb progress_cb, void *progress_arg,
1158 struct got_ratelimit *rl,
1159 got_cancel_cb cancel_cb, void *cancel_arg)
1161 const struct got_error *err = NULL;
1164 struct got_pack_meta *m;
1165 struct got_raw_object *raw = NULL;
1166 FILE *delta_file = NULL;
1169 struct got_deflate_checksum csum;
1170 off_t packfile_size = 0;
1174 csum.output_sha1 = &ctx;
1175 csum.output_crc = NULL;
1177 err = hwrite(packfile, "PACK", 4, &ctx);
1180 putbe32(buf, GOT_PACKFILE_VERSION);
1181 err = hwrite(packfile, buf, 4, &ctx);
1184 putbe32(buf, nmeta);
1185 err = hwrite(packfile, buf, 4, &ctx);
1188 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1189 for (i = 0; i < nmeta; i++) {
1190 err = report_progress(progress_cb, progress_arg, rl,
1191 packfile_size, nours, nmeta, nmeta, i);
1195 m->off = ftello(packfile);
1196 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1199 if (m->delta_len == 0) {
1200 err = packhdr(&nh, buf, sizeof(buf),
1201 m->obj_type, raw->size);
1204 err = hwrite(packfile, buf, nh, &ctx);
1207 packfile_size += nh;
1208 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1209 err = got_error_from_errno("fseeko");
1212 err = got_deflate_to_file(&outlen, raw->f, packfile,
1216 packfile_size += outlen;
1219 if (delta_file == NULL) {
1220 delta_file = got_opentemp();
1221 if (delta_file == NULL) {
1222 err = got_error_from_errno(
1227 if (ftruncate(fileno(delta_file), 0L) == -1) {
1228 err = got_error_from_errno("ftruncate");
1231 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1232 err = got_error_from_errno("fseeko");
1235 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1237 err = got_error_from_errno("fseeko");
1240 remain = m->delta_len;
1241 while (remain > 0) {
1242 char delta_buf[8192];
1244 n = MIN(remain, sizeof(delta_buf));
1245 r = fread(delta_buf, 1, n, delta_cache);
1247 err = got_ferror(delta_cache,
1251 w = fwrite(delta_buf, 1, n, delta_file);
1253 err = got_ferror(delta_file,
1259 if (use_offset_deltas && m->prev->off != 0) {
1260 err = packhdr(&nh, buf, sizeof(buf),
1261 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1264 nh += packoff(buf + nh,
1265 m->off - m->prev->off);
1266 err = hwrite(packfile, buf, nh, &ctx);
1269 packfile_size += nh;
1271 err = packhdr(&nh, buf, sizeof(buf),
1272 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1273 err = hwrite(packfile, buf, nh, &ctx);
1276 packfile_size += nh;
1277 err = hwrite(packfile, m->prev->id.sha1,
1278 sizeof(m->prev->id.sha1), &ctx);
1279 packfile_size += sizeof(m->prev->id.sha1);
1283 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1284 err = got_error_from_errno("fseeko");
1287 err = got_deflate_to_file(&outlen, delta_file,
1291 packfile_size += outlen;
1293 got_object_raw_close(raw);
1296 SHA1Final(pack_sha1, &ctx);
1297 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1298 if (n != SHA1_DIGEST_LENGTH)
1299 err = got_ferror(packfile, GOT_ERR_IO);
1300 packfile_size += SHA1_DIGEST_LENGTH;
1301 packfile_size += sizeof(struct got_packfile_hdr);
1303 err = progress_cb(progress_arg, packfile_size, nours,
1304 nmeta, nmeta, nmeta);
1309 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1310 err = got_error_from_errno("fclose");
1312 got_object_raw_close(raw);
1313 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1314 err = got_error_from_errno("close");
1318 const struct got_error *
1319 got_pack_create(uint8_t *packsha1, FILE *packfile,
1320 struct got_object_id **theirs, int ntheirs,
1321 struct got_object_id **ours, int nours,
1322 struct got_repository *repo, int loose_obj_only, int allow_empty,
1323 got_pack_progress_cb progress_cb, void *progress_arg,
1324 got_cancel_cb cancel_cb, void *cancel_arg)
1326 const struct got_error *err;
1327 struct got_pack_meta **meta;
1329 FILE *delta_cache = NULL;
1330 struct got_ratelimit rl;
1332 got_ratelimit_init(&rl, 0, 500);
1334 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1335 loose_obj_only, progress_cb, progress_arg, &rl,
1336 cancel_cb, cancel_arg);
1340 if (nmeta == 0 && !allow_empty) {
1341 err = got_error(GOT_ERR_CANNOT_PACK);
1345 delta_cache = got_opentemp();
1346 if (delta_cache == NULL) {
1347 err = got_error_from_errno("got_opentemp");
1352 err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1353 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1356 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1357 err = got_error_from_errno("fseeko");
1362 err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1363 repo, progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1367 free_nmeta(meta, nmeta);
1368 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1369 err = got_error_from_errno("fclose");