Blob


1 /*
2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
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.
8 *
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.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/stat.h>
24 #include <stdint.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <zlib.h>
31 #include "got_error.h"
32 #include "got_cancel.h"
33 #include "got_object.h"
34 #include "got_path.h"
35 #include "got_reference.h"
36 #include "got_repository_admin.h"
37 #include "got_opentemp.h"
39 #include "got_lib_deltify.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_idset.h"
43 #include "got_lib_object_cache.h"
44 #include "got_lib_deflate.h"
45 #include "got_lib_pack.h"
46 #include "got_lib_privsep.h"
47 #include "got_lib_repository.h"
49 #ifndef MAX
50 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
51 #endif
53 struct got_pack_meta {
54 struct got_object_id id;
55 char *path;
56 int obj_type;
57 off_t size;
58 time_t mtime;
60 /* The best delta we picked */
61 struct got_pack_meta *head;
62 struct got_pack_meta *prev;
63 struct got_delta_instruction *deltas;
64 int ndeltas;
65 int nchain;
67 /* Only used for delta window */
68 struct got_delta_table *dtab;
70 /* Only used for writing offset deltas */
71 off_t off;
72 };
74 struct got_pack_metavec {
75 struct got_pack_meta **meta;
76 int nmeta;
77 int metasz;
78 };
80 static const struct got_error *
81 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
82 const char *path, int obj_type, time_t mtime)
83 {
84 const struct got_error *err = NULL;
85 struct got_pack_meta *m;
87 *new = NULL;
89 m = calloc(1, sizeof(*m));
90 if (m == NULL)
91 return got_error_from_errno("calloc");
93 memcpy(&m->id, id, sizeof(m->id));
95 m->path = strdup(path);
96 if (m->path == NULL) {
97 err = got_error_from_errno("strdup");
98 free(m);
99 return err;
102 m->obj_type = obj_type;
103 m->mtime = mtime;
104 *new = m;
105 return NULL;
108 static void
109 clear_meta(struct got_pack_meta *meta)
111 if (meta == NULL)
112 return;
113 free(meta->deltas);
114 meta->deltas = NULL;
115 free(meta->path);
116 meta->path = NULL;
119 static void
120 free_nmeta(struct got_pack_meta **meta, int nmeta)
122 int i;
124 for (i = 0; i < nmeta; i++)
125 clear_meta(meta[i]);
126 free(meta);
129 static int
130 delta_order_cmp(const void *pa, const void *pb)
132 struct got_pack_meta *a, *b;
133 int cmp;
135 a = *(struct got_pack_meta **)pa;
136 b = *(struct got_pack_meta **)pb;
138 if (a->obj_type != b->obj_type)
139 return a->obj_type - b->obj_type;
140 cmp = strcmp(a->path, b->path);
141 if (cmp != 0)
142 return cmp;
143 if (a->mtime != b->mtime)
144 return a->mtime - b->mtime;
145 return got_object_id_cmp(&a->id, &b->id);
148 static int
149 delta_size(struct got_delta_instruction *deltas, int ndeltas)
151 int i, size = 32;
152 for (i = 0; i < ndeltas; i++) {
153 if (deltas[i].copy)
154 size += GOT_DELTA_SIZE_SHIFT;
155 else
156 size += deltas[i].len + 1;
158 return size;
162 static const struct got_error *
163 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
164 struct got_repository *repo,
165 got_pack_progress_cb progress_cb, void *progress_arg,
166 got_cancel_cb cancel_cb, void *cancel_arg)
168 const struct got_error *err = NULL;
169 struct got_pack_meta *m = NULL, *base = NULL;
170 struct got_raw_object *raw = NULL, *base_raw = NULL;
171 struct got_delta_instruction *deltas;
172 int i, j, size, ndeltas, best;
173 const int max_base_candidates = 10;
174 int outfd = -1;
176 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
177 for (i = 0; i < nmeta; i++) {
178 if (cancel_cb) {
179 err = (*cancel_cb)(cancel_arg);
180 if (err)
181 break;
183 if (progress_cb) {
184 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
185 if (err)
186 goto done;
188 m = meta[i];
189 m->deltas = NULL;
190 m->ndeltas = 0;
192 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
193 m->obj_type == GOT_OBJ_TYPE_TAG)
194 continue;
196 err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
197 if (err)
198 goto done;
199 m->size = raw->size;
201 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
202 raw->size + raw->hdrlen);
203 if (err)
204 goto done;
206 if (i > max_base_candidates) {
207 struct got_pack_meta *n = NULL;
208 n = meta[i - (max_base_candidates + 1)];
209 got_deltify_free(n->dtab);
210 n->dtab = NULL;
213 best = raw->size;
214 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
215 if (cancel_cb) {
216 err = (*cancel_cb)(cancel_arg);
217 if (err)
218 goto done;
220 base = meta[j];
221 /* long chains make unpacking slow, avoid such bases */
222 if (base->nchain >= 128 ||
223 base->obj_type != m->obj_type)
224 continue;
226 err = got_object_raw_open(&base_raw, &outfd, repo,
227 &base->id, 8192);
228 if (err)
229 goto done;
230 err = got_deltify(&deltas, &ndeltas,
231 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
232 base->dtab, base_raw->f, base_raw->hdrlen,
233 base_raw->size + base_raw->hdrlen);
234 got_object_raw_close(base_raw);
235 base_raw = NULL;
236 if (err)
237 goto done;
239 size = delta_size(deltas, ndeltas);
240 if (size + 32 < best){
241 /*
242 * if we already picked a best delta,
243 * replace it.
244 */
245 free(m->deltas);
246 best = size;
247 m->deltas = deltas;
248 m->ndeltas = ndeltas;
249 m->nchain = base->nchain + 1;
250 m->prev = base;
251 m->head = base->head;
252 if (m->head == NULL)
253 m->head = base;
254 } else {
255 free(deltas);
256 deltas = NULL;
257 ndeltas = 0;
261 got_object_raw_close(raw);
262 raw = NULL;
264 done:
265 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
266 got_deltify_free(meta[i]->dtab);
267 meta[i]->dtab = NULL;
269 if (raw)
270 got_object_raw_close(raw);
271 if (base_raw)
272 got_object_raw_close(base_raw);
273 if (outfd != -1 && close(outfd) == -1 && err == NULL)
274 err = got_error_from_errno("close");
275 return err;
278 static const struct got_error *
279 search_packidx(int *found, struct got_object_id *id,
280 struct got_repository *repo)
282 const struct got_error *err = NULL;
283 struct got_packidx *packidx = NULL;
284 int idx;
286 *found = 0;
288 err = got_repo_search_packidx(&packidx, &idx, repo, id);
289 if (err == NULL)
290 *found = 1; /* object is already packed */
291 else if (err->code == GOT_ERR_NO_OBJ)
292 err = NULL;
293 return err;
296 static const int obj_types[] = {
297 GOT_OBJ_TYPE_ANY,
298 GOT_OBJ_TYPE_COMMIT,
299 GOT_OBJ_TYPE_TREE,
300 GOT_OBJ_TYPE_BLOB,
301 GOT_OBJ_TYPE_TAG,
302 GOT_OBJ_TYPE_OFFSET_DELTA,
303 GOT_OBJ_TYPE_REF_DELTA
304 };
306 static const struct got_error *
307 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
308 struct got_object_id *id, const char *path, int obj_type,
309 time_t mtime, int loose_obj_only, struct got_repository *repo)
311 const struct got_error *err;
312 struct got_pack_meta *m;
314 if (loose_obj_only) {
315 int is_packed;
316 err = search_packidx(&is_packed, id, repo);
317 if (err)
318 return err;
319 if (is_packed)
320 return NULL;
323 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
324 if (err)
325 return err;
327 if (v == NULL)
328 return NULL;
330 err = alloc_meta(&m, id, path, obj_type, mtime);
331 if (err)
332 goto done;
334 if (v->nmeta == v->metasz){
335 size_t newsize = 2 * v->metasz;
336 struct got_pack_meta **new;
337 new = reallocarray(v->meta, newsize, sizeof(*new));
338 if (new == NULL) {
339 err = got_error_from_errno("reallocarray");
340 goto done;
342 v->meta = new;
343 v->metasz = newsize;
345 done:
346 if (err) {
347 clear_meta(m);
348 free(m);
349 } else
350 v->meta[v->nmeta++] = m;
352 return err;
355 static const struct got_error *
356 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
357 struct got_object_idset *idset, struct got_object_id *tree_id,
358 const char *dpath, time_t mtime, struct got_repository *repo,
359 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
361 const struct got_error *err;
362 struct got_tree_object *tree;
363 char *p = NULL;
364 int i;
366 err = got_object_open_as_tree(&tree, repo, tree_id);
367 if (err)
368 return err;
370 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
371 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
372 struct got_object_id *id = got_tree_entry_get_id(e);
373 mode_t mode = got_tree_entry_get_mode(e);
375 if (cancel_cb) {
376 err = (*cancel_cb)(cancel_arg);
377 if (err)
378 break;
381 if (got_object_tree_entry_is_submodule(e) ||
382 got_object_idset_contains(idset, id))
383 continue;
385 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
386 got_tree_entry_get_name(e)) == -1) {
387 err = got_error_from_errno("asprintf");
388 break;
391 if (S_ISDIR(mode)) {
392 struct got_object_qid *qid;
393 err = got_object_qid_alloc(&qid, id);
394 if (err)
395 break;
396 STAILQ_INSERT_TAIL(ids, qid, entry);
397 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
398 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
399 mtime, loose_obj_only, repo);
400 if (err)
401 break;
403 free(p);
404 p = NULL;
407 got_object_tree_close(tree);
408 free(p);
409 return err;
412 static const struct got_error *
413 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
414 struct got_object_id *tree_id, const char *dpath, time_t mtime,
415 int loose_obj_only, struct got_repository *repo,
416 got_cancel_cb cancel_cb, void *cancel_arg)
418 const struct got_error *err = NULL;
419 struct got_object_id_queue tree_ids;
420 struct got_object_qid *qid;
422 if (got_object_idset_contains(idset, tree_id))
423 return NULL;
425 err = got_object_qid_alloc(&qid, tree_id);
426 if (err)
427 return err;
429 STAILQ_INIT(&tree_ids);
430 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
432 while (!STAILQ_EMPTY(&tree_ids)) {
433 if (cancel_cb) {
434 err = (*cancel_cb)(cancel_arg);
435 if (err)
436 break;
439 qid = STAILQ_FIRST(&tree_ids);
440 STAILQ_REMOVE_HEAD(&tree_ids, entry);
442 if (got_object_idset_contains(idset, qid->id)) {
443 got_object_qid_free(qid);
444 continue;
447 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
448 mtime, loose_obj_only, repo);
449 if (err) {
450 got_object_qid_free(qid);
451 break;
454 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
455 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
456 got_object_qid_free(qid);
457 if (err)
458 break;
461 got_object_id_queue_free(&tree_ids);
462 return err;
465 static const struct got_error *
466 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
467 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
468 got_cancel_cb cancel_cb, void *cancel_arg)
470 const struct got_error *err;
471 struct got_commit_object *commit;
473 if (got_object_idset_contains(idset, id))
474 return NULL;
476 if (loose_obj_only) {
477 int is_packed;
478 err = search_packidx(&is_packed, id, repo);
479 if (err)
480 return err;
481 if (is_packed)
482 return NULL;
485 err = got_object_open_as_commit(&commit, repo, id);
486 if (err)
487 return err;
489 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
490 got_object_commit_get_committer_time(commit),
491 loose_obj_only, repo);
492 if (err)
493 goto done;
495 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
496 "", got_object_commit_get_committer_time(commit),
497 loose_obj_only, repo, cancel_cb, cancel_arg);
498 done:
499 got_object_commit_close(commit);
500 return err;
503 static const struct got_error *
504 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
505 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
506 got_cancel_cb cancel_cb, void *cancel_arg)
508 const struct got_error *err;
509 struct got_tag_object *tag = NULL;
511 if (got_object_idset_contains(idset, id))
512 return NULL;
514 if (loose_obj_only) {
515 int is_packed;
516 err = search_packidx(&is_packed, id, repo);
517 if (err)
518 return err;
519 if (is_packed)
520 return NULL;
523 err = got_object_open_as_tag(&tag, repo, id);
524 if (err)
525 return err;
527 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
528 got_object_tag_get_tagger_time(tag),
529 loose_obj_only, repo);
530 if (err)
531 goto done;
533 switch (got_object_tag_get_object_type(tag)) {
534 case GOT_OBJ_TYPE_COMMIT:
535 err = load_commit(v, idset,
536 got_object_tag_get_object_id(tag), repo,
537 loose_obj_only, cancel_cb, cancel_arg);
538 break;
539 case GOT_OBJ_TYPE_TREE:
540 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
541 "", got_object_tag_get_tagger_time(tag),
542 loose_obj_only, repo, cancel_cb, cancel_arg);
543 break;
544 default:
545 break;
548 done:
549 got_object_tag_close(tag);
550 return err;
553 enum findtwixt_color {
554 COLOR_KEEP = 0,
555 COLOR_DROP,
556 COLOR_BLANK,
557 };
558 static const int findtwixt_colors[] = {
559 COLOR_KEEP,
560 COLOR_DROP,
561 COLOR_BLANK
562 };
564 static const struct got_error *
565 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
566 int color, struct got_repository *repo)
568 const struct got_error *err;
569 struct got_object_qid *qid;
571 err = got_object_qid_alloc(&qid, id);
572 if (err)
573 return err;
575 STAILQ_INSERT_TAIL(ids, qid, entry);
576 qid->data = (void *)&findtwixt_colors[color];
577 return NULL;
580 static const struct got_error *
581 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
582 struct got_object_id *id, struct got_repository *repo,
583 got_cancel_cb cancel_cb, void *cancel_arg)
585 const struct got_error *err = NULL;
586 struct got_commit_object *commit;
587 const struct got_object_id_queue *parents;
588 struct got_object_id_queue ids;
589 struct got_object_qid *qid;
591 STAILQ_INIT(&ids);
593 err = got_object_qid_alloc(&qid, id);
594 if (err)
595 return err;
596 STAILQ_INSERT_HEAD(&ids, qid, entry);
598 while (!STAILQ_EMPTY(&ids)) {
599 if (cancel_cb) {
600 err = (*cancel_cb)(cancel_arg);
601 if (err)
602 break;
605 qid = STAILQ_FIRST(&ids);
606 STAILQ_REMOVE_HEAD(&ids, entry);
608 if (got_object_idset_contains(drop, qid->id)) {
609 got_object_qid_free(qid);
610 continue;
613 err = got_object_idset_add(drop, qid->id,
614 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
615 if (err) {
616 got_object_qid_free(qid);
617 break;
620 if (!got_object_idset_contains(keep, qid->id)) {
621 got_object_qid_free(qid);
622 continue;
625 err = got_object_open_as_commit(&commit, repo, qid->id);
626 got_object_qid_free(qid);
627 if (err)
628 break;
630 parents = got_object_commit_get_parent_ids(commit);
631 if (parents) {
632 err = got_object_id_queue_copy(parents, &ids);
633 if (err) {
634 got_object_commit_close(commit);
635 break;
638 got_object_commit_close(commit);
641 got_object_id_queue_free(&ids);
642 return err;
645 struct append_id_arg {
646 struct got_object_id **array;
647 int idx;
648 };
650 static const struct got_error *
651 append_id(struct got_object_id *id, void *data, void *arg)
653 struct append_id_arg *a = arg;
655 a->array[a->idx] = got_object_id_dup(id);
656 if (a->array[a->idx] == NULL)
657 return got_error_from_errno("got_object_id_dup");
659 a->idx++;
660 return NULL;
663 static const struct got_error *
664 findtwixt(struct got_object_id ***res, int *nres,
665 struct got_object_id **head, int nhead,
666 struct got_object_id **tail, int ntail,
667 struct got_repository *repo,
668 got_cancel_cb cancel_cb, void *cancel_arg)
670 const struct got_error *err = NULL;
671 struct got_object_id_queue ids;
672 struct got_object_idset *keep, *drop;
673 struct got_object_qid *qid;
674 int i, ncolor, nkeep, obj_type;
676 STAILQ_INIT(&ids);
677 *res = NULL;
678 *nres = 0;
680 keep = got_object_idset_alloc();
681 if (keep == NULL)
682 return got_error_from_errno("got_object_idset_alloc");
684 drop = got_object_idset_alloc();
685 if (drop == NULL) {
686 err = got_error_from_errno("got_object_idset_alloc");
687 goto done;
690 for (i = 0; i < nhead; i++) {
691 struct got_object_id *id = head[i];
692 if (id == NULL)
693 continue;
694 err = got_object_get_type(&obj_type, repo, id);
695 if (err)
696 return err;
697 if (obj_type != GOT_OBJ_TYPE_COMMIT)
698 continue;
699 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
700 if (err)
701 goto done;
703 for (i = 0; i < ntail; i++) {
704 struct got_object_id *id = tail[i];
705 if (id == NULL)
706 continue;
707 err = got_object_get_type(&obj_type, repo, id);
708 if (err)
709 return err;
710 if (obj_type != GOT_OBJ_TYPE_COMMIT)
711 continue;
712 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
713 if (err)
714 goto done;
717 while (!STAILQ_EMPTY(&ids)) {
718 int qcolor;
719 qid = STAILQ_FIRST(&ids);
720 qcolor = *((int *)qid->data);
722 if (got_object_idset_contains(drop, qid->id))
723 ncolor = COLOR_DROP;
724 else if (got_object_idset_contains(keep, qid->id))
725 ncolor = COLOR_KEEP;
726 else
727 ncolor = COLOR_BLANK;
729 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
730 qcolor == COLOR_KEEP)) {
731 STAILQ_REMOVE_HEAD(&ids, entry);
732 got_object_qid_free(qid);
733 continue;
736 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
737 err = drop_commit(keep, drop, qid->id, repo,
738 cancel_cb, cancel_arg);
739 if (err)
740 goto done;
741 } else if (ncolor == COLOR_BLANK) {
742 struct got_commit_object *commit;
743 struct got_object_id *id;
744 const struct got_object_id_queue *parents;
745 struct got_object_qid *pid;
747 id = got_object_id_dup(qid->id);
748 if (id == NULL) {
749 err = got_error_from_errno("got_object_id_dup");
750 goto done;
752 if (qcolor == COLOR_KEEP)
753 err = got_object_idset_add(keep, id,
754 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
755 else
756 err = got_object_idset_add(drop, id,
757 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
758 if (err) {
759 free(id);
760 goto done;
763 err = got_object_open_as_commit(&commit, repo, id);
764 if (err) {
765 free(id);
766 goto done;
768 parents = got_object_commit_get_parent_ids(commit);
769 if (parents) {
770 STAILQ_FOREACH(pid, parents, entry) {
771 err = queue_commit_id(&ids, pid->id,
772 qcolor, repo);
773 if (err) {
774 free(id);
775 goto done;
779 got_object_commit_close(commit);
780 commit = NULL;
781 } else {
782 /* should not happen */
783 err = got_error_fmt(GOT_ERR_NOT_IMPL,
784 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
785 goto done;
788 STAILQ_REMOVE_HEAD(&ids, entry);
789 got_object_qid_free(qid);
792 nkeep = got_object_idset_num_elements(keep);
793 if (nkeep > 0) {
794 struct append_id_arg arg;
795 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
796 if (arg.array == NULL) {
797 err = got_error_from_errno("calloc");
798 goto done;
800 arg.idx = 0;
801 err = got_object_idset_for_each(keep, append_id, &arg);
802 if (err) {
803 free(arg.array);
804 goto done;
806 *res = arg.array;
807 *nres = nkeep;
809 done:
810 got_object_idset_free(keep);
811 got_object_idset_free(drop);
812 got_object_id_queue_free(&ids);
813 return err;
816 static const struct got_error *
817 read_meta(struct got_pack_meta ***meta, int *nmeta,
818 struct got_object_id **theirs, int ntheirs,
819 struct got_object_id **ours, int nours, struct got_repository *repo,
820 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
821 got_cancel_cb cancel_cb, void *cancel_arg)
823 const struct got_error *err = NULL;
824 struct got_object_id **ids = NULL;
825 struct got_object_idset *idset;
826 int i, nobj = 0, obj_type;
827 struct got_pack_metavec v;
829 *meta = NULL;
830 *nmeta = 0;
832 idset = got_object_idset_alloc();
833 if (idset == NULL)
834 return got_error_from_errno("got_object_idset_alloc");
836 v.nmeta = 0;
837 v.metasz = 64;
838 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
839 if (v.meta == NULL) {
840 err = got_error_from_errno("calloc");
841 goto done;
844 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
845 cancel_cb, cancel_arg);
846 if (err || nobj == 0)
847 goto done;
849 for (i = 0; i < ntheirs; i++) {
850 struct got_object_id *id = theirs[i];
851 if (id == NULL)
852 continue;
853 err = got_object_get_type(&obj_type, repo, id);
854 if (err)
855 return err;
856 if (obj_type != GOT_OBJ_TYPE_COMMIT)
857 continue;
858 err = load_commit(NULL, idset, id, repo,
859 loose_obj_only, cancel_cb, cancel_arg);
860 if (err)
861 goto done;
862 if (progress_cb) {
863 err = progress_cb(progress_arg, 0L, nours,
864 v.nmeta, 0, 0);
865 if (err)
866 goto done;
870 for (i = 0; i < ntheirs; i++) {
871 struct got_object_id *id = theirs[i];
872 int *cached_type;
873 if (id == NULL)
874 continue;
875 cached_type = got_object_idset_get(idset, id);
876 if (cached_type == NULL) {
877 err = got_object_get_type(&obj_type, repo, id);
878 if (err)
879 goto done;
880 } else
881 obj_type = *cached_type;
882 if (obj_type != GOT_OBJ_TYPE_TAG)
883 continue;
884 err = load_tag(NULL, idset, id, repo,
885 loose_obj_only, cancel_cb, cancel_arg);
886 if (err)
887 goto done;
888 if (progress_cb) {
889 err = progress_cb(progress_arg, 0L, nours,
890 v.nmeta, 0, 0);
891 if (err)
892 goto done;
896 for (i = 0; i < nobj; i++) {
897 err = load_commit(&v, idset, ids[i], repo,
898 loose_obj_only, cancel_cb, cancel_arg);
899 if (err)
900 goto done;
901 if (progress_cb) {
902 err = progress_cb(progress_arg, 0L, nours,
903 v.nmeta, 0, 0);
904 if (err)
905 goto done;
909 for (i = 0; i < nours; i++) {
910 struct got_object_id *id = ours[i];
911 int *cached_type;
912 if (id == NULL)
913 continue;
914 cached_type = got_object_idset_get(idset, id);
915 if (cached_type == NULL) {
916 err = got_object_get_type(&obj_type, repo, id);
917 if (err)
918 goto done;
919 } else
920 obj_type = *cached_type;
921 if (obj_type != GOT_OBJ_TYPE_TAG)
922 continue;
923 err = load_tag(&v, idset, id, repo,
924 loose_obj_only, cancel_cb, cancel_arg);
925 if (err)
926 goto done;
927 if (progress_cb) {
928 err = progress_cb(progress_arg, 0L, nours,
929 v.nmeta, 0, 0);
930 if (err)
931 goto done;
935 done:
936 for (i = 0; i < nobj; i++) {
937 free(ids[i]);
939 free(ids);
940 got_object_idset_free(idset);
941 if (err == NULL) {
942 *meta = v.meta;
943 *nmeta = v.nmeta;
944 } else
945 free(v.meta);
947 return err;
950 const struct got_error *
951 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
953 size_t n;
955 SHA1Update(ctx, buf, len);
956 n = fwrite(buf, 1, len, f);
957 if (n != len)
958 return got_ferror(f, GOT_ERR_IO);
959 return NULL;
962 static void
963 putbe32(char *b, uint32_t n)
965 b[0] = n >> 24;
966 b[1] = n >> 16;
967 b[2] = n >> 8;
968 b[3] = n >> 0;
971 static int
972 write_order_cmp(const void *pa, const void *pb)
974 struct got_pack_meta *a, *b, *ahd, *bhd;
976 a = *(struct got_pack_meta **)pa;
977 b = *(struct got_pack_meta **)pb;
978 ahd = (a->head == NULL) ? a : a->head;
979 bhd = (b->head == NULL) ? b : b->head;
980 if (ahd->mtime != bhd->mtime)
981 return bhd->mtime - ahd->mtime;
982 if (ahd != bhd)
983 return (uintptr_t)bhd - (uintptr_t)ahd;
984 if (a->nchain != b->nchain)
985 return a->nchain - b->nchain;
986 return a->mtime - b->mtime;
989 static const struct got_error *
990 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
992 size_t i;
994 *hdrlen = 0;
996 hdr[0] = obj_type << 4;
997 hdr[0] |= len & 0xf;
998 len >>= 4;
999 for (i = 1; len != 0; i++){
1000 if (i >= bufsize)
1001 return got_error(GOT_ERR_NO_SPACE);
1002 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1003 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1004 len >>= GOT_DELTA_SIZE_SHIFT;
1007 *hdrlen = i;
1008 return NULL;
1011 static const struct got_error *
1012 encodedelta(struct got_pack_meta *m, struct got_raw_object *o,
1013 off_t base_size, FILE *f)
1015 unsigned char buf[16], *bp;
1016 int i, j;
1017 off_t n;
1018 size_t w;
1019 struct got_delta_instruction *d;
1021 /* base object size */
1022 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1023 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1024 for (i = 1; n > 0; i++) {
1025 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1026 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1027 n >>= GOT_DELTA_SIZE_SHIFT;
1029 w = fwrite(buf, 1, i, f);
1030 if (w != i)
1031 return got_ferror(f, GOT_ERR_IO);
1033 /* target object size */
1034 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1035 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1036 for (i = 1; n > 0; i++) {
1037 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1038 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1039 n >>= GOT_DELTA_SIZE_SHIFT;
1041 w = fwrite(buf, 1, i, f);
1042 if (w != i)
1043 return got_ferror(f, GOT_ERR_IO);
1045 for (j = 0; j < m->ndeltas; j++) {
1046 d = &m->deltas[j];
1047 if (d->copy) {
1048 n = d->offset;
1049 bp = &buf[1];
1050 buf[0] = GOT_DELTA_BASE_COPY;
1051 for (i = 0; i < 4; i++) {
1052 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1053 buf[0] |= 1 << i;
1054 *bp++ = n & 0xff;
1055 n >>= 8;
1056 if (n == 0)
1057 break;
1060 n = d->len;
1061 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1062 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1063 for (i = 0; i < 3 && n > 0; i++) {
1064 buf[0] |= 1 << (i + 4);
1065 *bp++ = n & 0xff;
1066 n >>= 8;
1069 w = fwrite(buf, 1, bp - buf, f);
1070 if (w != bp - buf)
1071 return got_ferror(f, GOT_ERR_IO);
1072 } else {
1073 char content[128];
1074 size_t r;
1075 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1076 return got_error_from_errno("fseeko");
1077 n = 0;
1078 while (n != d->len) {
1079 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1080 w = fwrite(buf, 1, 1, f);
1081 if (w != 1)
1082 return got_ferror(f, GOT_ERR_IO);
1083 r = fread(content, 1, buf[0], o->f);
1084 if (r != buf[0])
1085 return got_ferror(o->f, GOT_ERR_IO);
1086 w = fwrite(content, 1, buf[0], f);
1087 if (w != buf[0])
1088 return got_ferror(f, GOT_ERR_IO);
1089 n += buf[0];
1094 return NULL;
1097 static int
1098 packoff(char *hdr, off_t off)
1100 int i, j;
1101 char rbuf[8];
1103 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1104 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1105 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1106 GOT_DELTA_SIZE_MORE;
1109 j = 0;
1110 while (i > 0)
1111 hdr[j++] = rbuf[--i];
1112 return j;
1115 static const struct got_error *
1116 genpack(uint8_t *pack_sha1, FILE *packfile,
1117 struct got_pack_meta **meta, int nmeta, int nours,
1118 int use_offset_deltas, struct got_repository *repo,
1119 got_pack_progress_cb progress_cb, void *progress_arg,
1120 got_cancel_cb cancel_cb, void *cancel_arg)
1122 const struct got_error *err = NULL;
1123 int i, nh;
1124 off_t nd;
1125 SHA1_CTX ctx;
1126 struct got_pack_meta *m;
1127 struct got_raw_object *raw = NULL;
1128 FILE *delta_file = NULL;
1129 char buf[32];
1130 size_t outlen, n;
1131 struct got_deflate_checksum csum;
1132 off_t packfile_size = 0;
1133 int outfd = -1;
1135 SHA1Init(&ctx);
1136 csum.output_sha1 = &ctx;
1137 csum.output_crc = NULL;
1139 err = hwrite(packfile, "PACK", 4, &ctx);
1140 if (err)
1141 return err;
1142 putbe32(buf, GOT_PACKFILE_VERSION);
1143 err = hwrite(packfile, buf, 4, &ctx);
1144 if (err)
1145 goto done;
1146 putbe32(buf, nmeta);
1147 err = hwrite(packfile, buf, 4, &ctx);
1148 if (err)
1149 goto done;
1150 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1151 for (i = 0; i < nmeta; i++) {
1152 if (progress_cb) {
1153 err = progress_cb(progress_arg, packfile_size, nours,
1154 nmeta, nmeta, i);
1155 if (err)
1156 goto done;
1158 m = meta[i];
1159 m->off = ftello(packfile);
1160 err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
1161 if (err)
1162 goto done;
1163 if (m->deltas == NULL) {
1164 err = packhdr(&nh, buf, sizeof(buf),
1165 m->obj_type, raw->size);
1166 if (err)
1167 goto done;
1168 err = hwrite(packfile, buf, nh, &ctx);
1169 if (err)
1170 goto done;
1171 packfile_size += nh;
1172 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1173 err = got_error_from_errno("fseeko");
1174 goto done;
1176 err = got_deflate_to_file(&outlen, raw->f, packfile,
1177 &csum);
1178 if (err)
1179 goto done;
1180 packfile_size += outlen;
1181 } else {
1182 if (delta_file == NULL) {
1183 delta_file = got_opentemp();
1184 if (delta_file == NULL) {
1185 err = got_error_from_errno(
1186 "got_opentemp");
1187 goto done;
1190 if (ftruncate(fileno(delta_file), 0L) == -1) {
1191 err = got_error_from_errno("ftruncate");
1192 goto done;
1194 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1195 err = got_error_from_errno("fseeko");
1196 goto done;
1198 err = encodedelta(m, raw, m->prev->size, delta_file);
1199 if (err)
1200 goto done;
1201 nd = ftello(delta_file);
1202 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1203 err = got_error_from_errno("fseeko");
1204 goto done;
1206 if (use_offset_deltas && m->prev->off != 0) {
1207 err = packhdr(&nh, buf, sizeof(buf),
1208 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1209 if (err)
1210 goto done;
1211 nh += packoff(buf + nh,
1212 m->off - m->prev->off);
1213 err = hwrite(packfile, buf, nh, &ctx);
1214 if (err)
1215 goto done;
1216 packfile_size += nh;
1217 } else {
1218 err = packhdr(&nh, buf, sizeof(buf),
1219 GOT_OBJ_TYPE_REF_DELTA, nd);
1220 err = hwrite(packfile, buf, nh, &ctx);
1221 if (err)
1222 goto done;
1223 packfile_size += nh;
1224 err = hwrite(packfile, m->prev->id.sha1,
1225 sizeof(m->prev->id.sha1), &ctx);
1226 packfile_size += sizeof(m->prev->id.sha1);
1227 if (err)
1228 goto done;
1230 err = got_deflate_to_file(&outlen, delta_file,
1231 packfile, &csum);
1232 if (err)
1233 goto done;
1234 packfile_size += outlen;
1236 got_object_raw_close(raw);
1237 raw = NULL;
1239 SHA1Final(pack_sha1, &ctx);
1240 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1241 if (n != SHA1_DIGEST_LENGTH)
1242 err = got_ferror(packfile, GOT_ERR_IO);
1243 packfile_size += SHA1_DIGEST_LENGTH;
1244 packfile_size += sizeof(struct got_packfile_hdr);
1245 err = progress_cb(progress_arg, packfile_size, nours,
1246 nmeta, nmeta, nmeta);
1247 if (err)
1248 goto done;
1249 done:
1250 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1251 err = got_error_from_errno("fclose");
1252 if (raw)
1253 got_object_raw_close(raw);
1254 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1255 err = got_error_from_errno("close");
1256 return err;
1259 const struct got_error *
1260 got_pack_create(uint8_t *packsha1, FILE *packfile,
1261 struct got_object_id **theirs, int ntheirs,
1262 struct got_object_id **ours, int nours,
1263 struct got_repository *repo, int loose_obj_only, int allow_empty,
1264 got_pack_progress_cb progress_cb, void *progress_arg,
1265 got_cancel_cb cancel_cb, void *cancel_arg)
1267 const struct got_error *err;
1268 struct got_pack_meta **meta;
1269 int nmeta;
1271 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1272 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1273 if (err)
1274 return err;
1276 if (nmeta == 0 && !allow_empty) {
1277 err = got_error(GOT_ERR_CANNOT_PACK);
1278 goto done;
1280 if (nmeta > 0) {
1281 err = pick_deltas(meta, nmeta, nours, repo,
1282 progress_cb, progress_arg, cancel_cb, cancel_arg);
1283 if (err)
1284 goto done;
1287 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1288 progress_cb, progress_arg, cancel_cb, cancel_arg);
1289 if (err)
1290 goto done;
1291 done:
1292 free_nmeta(meta, nmeta);
1293 return err;