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/uio.h>
21 #include <sys/stat.h>
23 #include <stdint.h>
24 #include <imsg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sha1.h>
29 #include <limits.h>
30 #include <zlib.h>
32 #include "got_error.h"
33 #include "got_cancel.h"
34 #include "got_object.h"
35 #include "got_reference.h"
36 #include "got_repository_admin.h"
38 #include "got_lib_deltify.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_idset.h"
42 #include "got_lib_object_cache.h"
43 #include "got_lib_deflate.h"
44 #include "got_lib_pack.h"
45 #include "got_lib_privsep.h"
46 #include "got_lib_repository.h"
48 #ifndef MAX
49 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
50 #endif
52 struct got_pack_meta {
53 struct got_object_id id;
54 char *path;
55 int obj_type;
56 time_t mtime;
58 /* The best delta we picked */
59 struct got_pack_meta *head;
60 struct got_pack_meta *prev;
61 struct got_delta_instruction *deltas;
62 int ndeltas;
63 int nchain;
65 /* Only used for delta window */
66 struct got_delta_table *dtab;
68 /* Only used for writing offset deltas */
69 off_t off;
70 };
72 struct got_pack_metavec {
73 struct got_pack_meta **meta;
74 int nmeta;
75 int metasz;
76 };
78 static const struct got_error *
79 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
80 const char *path, int obj_type, time_t mtime)
81 {
82 const struct got_error *err = NULL;
83 struct got_pack_meta *m;
85 *new = NULL;
87 m = calloc(1, sizeof(*m));
88 if (m == NULL)
89 return got_error_from_errno("malloc");
91 memcpy(&m->id, id, sizeof(m->id));
93 m->path = strdup(path);
94 if (m->path == NULL) {
95 err = got_error_from_errno("strdup");
96 free(m);
97 return err;
98 }
100 m->obj_type = obj_type;
101 m->mtime = mtime;
102 *new = m;
103 return NULL;
106 static void
107 clear_meta(struct got_pack_meta *meta)
109 if (meta == NULL)
110 return;
111 free(meta->deltas);
112 meta->deltas = NULL;
113 free(meta->path);
114 meta->path = NULL;
117 static void
118 free_nmeta(struct got_pack_meta **meta, int nmeta)
120 int i;
122 for (i = 0; i < nmeta; i++)
123 clear_meta(meta[i]);
124 free(meta);
127 static int
128 delta_order_cmp(const void *pa, const void *pb)
130 struct got_pack_meta *a, *b;
131 int cmp;
133 a = *(struct got_pack_meta **)pa;
134 b = *(struct got_pack_meta **)pb;
136 if (a->obj_type != b->obj_type)
137 return a->obj_type - b->obj_type;
138 cmp = strcmp(a->path, b->path);
139 if (cmp != 0)
140 return cmp;
141 if (a->mtime != b->mtime)
142 return a->mtime - b->mtime;
143 return got_object_id_cmp(&a->id, &b->id);
146 static int
147 delta_size(struct got_delta_instruction *deltas, int ndeltas)
149 int i, size = 32;
150 for (i = 0; i < ndeltas; i++) {
151 if (deltas[i].copy)
152 size += GOT_DELTA_SIZE_SHIFT;
153 else
154 size += deltas[i].len + 1;
156 return size;
160 static const struct got_error *
161 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
162 struct got_repository *repo,
163 got_pack_progress_cb progress_cb, void *progress_arg,
164 got_cancel_cb cancel_cb, void *cancel_arg)
166 const struct got_error *err = NULL;
167 struct got_pack_meta *m = NULL, *base = NULL;
168 struct got_raw_object *raw = NULL, *base_raw = NULL;
169 struct got_delta_instruction *deltas;
170 int i, j, size, ndeltas, best;
171 const int max_base_candidates = 10;
173 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
174 for (i = 0; i < nmeta; i++) {
175 if (cancel_cb) {
176 err = (*cancel_cb)(cancel_arg);
177 if (err)
178 break;
180 if (progress_cb) {
181 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
182 if (err)
183 goto done;
185 m = meta[i];
186 m->deltas = NULL;
187 m->ndeltas = 0;
189 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
190 m->obj_type == GOT_OBJ_TYPE_TAG)
191 continue;
193 err = got_object_raw_open(&raw, repo, &m->id, 8192);
194 if (err)
195 goto done;
197 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
198 raw->size + raw->hdrlen);
199 if (err)
200 goto done;
202 if (i > max_base_candidates) {
203 struct got_pack_meta *n = NULL;
204 n = meta[i - (max_base_candidates + 1)];
205 got_deltify_free(n->dtab);
206 n->dtab = NULL;
209 best = raw->size;
210 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
211 if (cancel_cb) {
212 err = (*cancel_cb)(cancel_arg);
213 if (err)
214 goto done;
216 base = meta[j];
217 /* long chains make unpacking slow, avoid such bases */
218 if (base->nchain >= 128 ||
219 base->obj_type != m->obj_type)
220 continue;
222 err = got_object_raw_open(&base_raw, repo, &base->id,
223 8192);
224 if (err)
225 goto done;
226 err = got_deltify(&deltas, &ndeltas,
227 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
228 base->dtab, base_raw->f, base_raw->hdrlen,
229 base_raw->size + base_raw->hdrlen);
230 got_object_raw_close(base_raw);
231 base_raw = NULL;
232 if (err)
233 goto done;
235 size = delta_size(deltas, ndeltas);
236 if (size + 32 < best){
237 /*
238 * if we already picked a best delta,
239 * replace it.
240 */
241 free(m->deltas);
242 best = size;
243 m->deltas = deltas;
244 m->ndeltas = ndeltas;
245 m->nchain = base->nchain + 1;
246 m->prev = base;
247 m->head = base->head;
248 if (m->head == NULL)
249 m->head = base;
250 } else {
251 free(deltas);
252 deltas = NULL;
253 ndeltas = 0;
257 got_object_raw_close(raw);
258 raw = NULL;
260 done:
261 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
262 got_deltify_free(meta[i]->dtab);
263 meta[i]->dtab = NULL;
265 if (raw)
266 got_object_raw_close(raw);
267 if (base_raw)
268 got_object_raw_close(base_raw);
269 return err;
272 static const struct got_error *
273 search_packidx(int *found, struct got_object_id *id,
274 struct got_repository *repo)
276 const struct got_error *err = NULL;
277 struct got_packidx *packidx = NULL;
278 int idx;
280 *found = 0;
282 err = got_repo_search_packidx(&packidx, &idx, repo, id);
283 if (err == NULL)
284 *found = 1; /* object is already packed */
285 else if (err->code == GOT_ERR_NO_OBJ)
286 err = NULL;
287 return err;
290 static const struct got_error *
291 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
292 struct got_object_id *id, const char *path, int obj_type,
293 time_t mtime, int loose_obj_only, struct got_repository *repo)
295 const struct got_error *err;
296 struct got_pack_meta *m;
298 if (loose_obj_only) {
299 int is_packed;
300 err = search_packidx(&is_packed, id, repo);
301 if (err)
302 return err;
303 if (is_packed)
304 return NULL;
307 err = got_object_idset_add(idset, id, NULL);
308 if (err)
309 return err;
311 if (v == NULL)
312 return NULL;
314 err = alloc_meta(&m, id, path, obj_type, mtime);
315 if (err)
316 goto done;
318 if (v->nmeta == v->metasz){
319 size_t newsize = 2 * v->metasz;
320 struct got_pack_meta **new;
321 new = reallocarray(v->meta, newsize, sizeof(*new));
322 if (new == NULL) {
323 err = got_error_from_errno("reallocarray");
324 goto done;
326 v->meta = new;
327 v->metasz = newsize;
329 done:
330 if (err) {
331 clear_meta(m);
332 free(m);
333 } else
334 v->meta[v->nmeta++] = m;
336 return err;
339 static const struct got_error *
340 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
341 struct got_object_idset *idset, struct got_object_id *tree_id,
342 const char *dpath, time_t mtime, struct got_repository *repo,
343 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
345 const struct got_error *err;
346 struct got_tree_object *tree;
347 char *p = NULL;
348 int i;
350 err = got_object_open_as_tree(&tree, repo, tree_id);
351 if (err)
352 return err;
354 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
355 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
356 struct got_object_id *id = got_tree_entry_get_id(e);
357 mode_t mode = got_tree_entry_get_mode(e);
359 if (cancel_cb) {
360 err = (*cancel_cb)(cancel_arg);
361 if (err)
362 break;
365 if (got_object_tree_entry_is_symlink(e) ||
366 got_object_tree_entry_is_submodule(e) ||
367 got_object_idset_contains(idset, id))
368 continue;
370 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
371 got_tree_entry_get_name(e)) == -1) {
372 err = got_error_from_errno("asprintf");
373 break;
376 if (S_ISDIR(mode)) {
377 struct got_object_qid *qid;
378 err = got_object_qid_alloc(&qid, id);
379 if (err)
380 break;
381 STAILQ_INSERT_TAIL(ids, qid, entry);
382 } else if (S_ISREG(mode)) {
383 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
384 mtime, loose_obj_only, repo);
385 if (err)
386 break;
388 free(p);
389 p = NULL;
392 got_object_tree_close(tree);
393 free(p);
394 return err;
397 static const struct got_error *
398 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
399 struct got_object_id *tree_id, const char *dpath, time_t mtime,
400 int loose_obj_only, struct got_repository *repo,
401 got_cancel_cb cancel_cb, void *cancel_arg)
403 const struct got_error *err = NULL;
404 struct got_object_id_queue tree_ids;
405 struct got_object_qid *qid;
407 if (got_object_idset_contains(idset, tree_id))
408 return NULL;
410 err = got_object_qid_alloc(&qid, tree_id);
411 if (err)
412 return err;
414 STAILQ_INIT(&tree_ids);
415 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
417 while (!STAILQ_EMPTY(&tree_ids)) {
418 if (cancel_cb) {
419 err = (*cancel_cb)(cancel_arg);
420 if (err)
421 break;
424 qid = STAILQ_FIRST(&tree_ids);
425 STAILQ_REMOVE_HEAD(&tree_ids, entry);
427 if (got_object_idset_contains(idset, qid->id)) {
428 got_object_qid_free(qid);
429 continue;
432 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
433 mtime, loose_obj_only, repo);
434 if (err) {
435 got_object_qid_free(qid);
436 break;
439 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
440 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
441 got_object_qid_free(qid);
442 if (err)
443 break;
446 got_object_id_queue_free(&tree_ids);
447 return err;
450 static const struct got_error *
451 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
452 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
453 got_cancel_cb cancel_cb, void *cancel_arg)
455 const struct got_error *err;
456 struct got_commit_object *commit;
458 if (got_object_idset_contains(idset, id))
459 return NULL;
461 if (loose_obj_only) {
462 int is_packed;
463 err = search_packidx(&is_packed, id, repo);
464 if (err)
465 return err;
466 if (is_packed)
467 return NULL;
470 err = got_object_open_as_commit(&commit, repo, id);
471 if (err)
472 return err;
474 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
475 got_object_commit_get_committer_time(commit),
476 loose_obj_only, repo);
477 if (err)
478 goto done;
480 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
481 "", got_object_commit_get_committer_time(commit),
482 loose_obj_only, repo, cancel_cb, cancel_arg);
483 done:
484 got_object_commit_close(commit);
485 return err;
488 static const struct got_error *
489 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
490 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
491 got_cancel_cb cancel_cb, void *cancel_arg)
493 const struct got_error *err;
494 struct got_tag_object *tag = NULL;
496 if (got_object_idset_contains(idset, id))
497 return NULL;
499 if (loose_obj_only) {
500 int is_packed;
501 err = search_packidx(&is_packed, id, repo);
502 if (err)
503 return err;
504 if (is_packed)
505 return NULL;
508 err = got_object_open_as_tag(&tag, repo, id);
509 if (err)
510 return err;
512 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
513 got_object_tag_get_tagger_time(tag),
514 loose_obj_only, repo);
515 if (err)
516 goto done;
518 switch (got_object_tag_get_object_type(tag)) {
519 case GOT_OBJ_TYPE_COMMIT:
520 err = load_commit(NULL, idset,
521 got_object_tag_get_object_id(tag), repo,
522 loose_obj_only, cancel_cb, cancel_arg);
523 break;
524 case GOT_OBJ_TYPE_TREE:
525 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
526 "", got_object_tag_get_tagger_time(tag),
527 loose_obj_only, repo, cancel_cb, cancel_arg);
528 break;
529 default:
530 break;
533 done:
534 got_object_tag_close(tag);
535 return err;
538 enum findtwixt_color {
539 COLOR_KEEP = 0,
540 COLOR_DROP,
541 COLOR_BLANK,
542 };
543 static const int findtwixt_colors[] = {
544 COLOR_KEEP,
545 COLOR_DROP,
546 COLOR_BLANK
547 };
549 static const struct got_error *
550 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
551 int color, struct got_repository *repo)
553 const struct got_error *err;
554 struct got_object_qid *qid;
556 err = got_object_qid_alloc(&qid, id);
557 if (err)
558 return err;
560 STAILQ_INSERT_TAIL(ids, qid, entry);
561 qid->data = (void *)&findtwixt_colors[color];
562 return NULL;
565 static const struct got_error *
566 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
567 struct got_object_id *id, struct got_repository *repo,
568 got_cancel_cb cancel_cb, void *cancel_arg)
570 const struct got_error *err = NULL;
571 struct got_commit_object *commit;
572 const struct got_object_id_queue *parents;
573 struct got_object_id_queue ids;
574 struct got_object_qid *qid;
576 STAILQ_INIT(&ids);
578 err = got_object_qid_alloc(&qid, id);
579 if (err)
580 return err;
581 STAILQ_INSERT_HEAD(&ids, qid, entry);
583 while (!STAILQ_EMPTY(&ids)) {
584 if (cancel_cb) {
585 err = (*cancel_cb)(cancel_arg);
586 if (err)
587 break;
590 qid = STAILQ_FIRST(&ids);
591 STAILQ_REMOVE_HEAD(&ids, entry);
593 if (got_object_idset_contains(drop, qid->id)) {
594 got_object_qid_free(qid);
595 continue;
598 err = got_object_idset_add(drop, qid->id, NULL);
599 if (err) {
600 got_object_qid_free(qid);
601 break;
604 if (!got_object_idset_contains(keep, qid->id)) {
605 got_object_qid_free(qid);
606 continue;
609 err = got_object_open_as_commit(&commit, repo, qid->id);
610 got_object_qid_free(qid);
611 if (err)
612 break;
614 parents = got_object_commit_get_parent_ids(commit);
615 if (parents) {
616 err = got_object_id_queue_copy(parents, &ids);
617 if (err) {
618 got_object_commit_close(commit);
619 break;
622 got_object_commit_close(commit);
625 got_object_id_queue_free(&ids);
626 return err;
629 struct append_id_arg {
630 struct got_object_id **array;
631 int idx;
632 };
634 static const struct got_error *
635 append_id(struct got_object_id *id, void *data, void *arg)
637 struct append_id_arg *a = arg;
639 a->array[a->idx] = got_object_id_dup(id);
640 if (a->array[a->idx] == NULL)
641 return got_error_from_errno("got_object_id_dup");
643 a->idx++;
644 return NULL;
647 static const struct got_error *
648 findtwixt(struct got_object_id ***res, int *nres,
649 struct got_object_id **head, int nhead,
650 struct got_object_id **tail, int ntail,
651 struct got_repository *repo,
652 got_cancel_cb cancel_cb, void *cancel_arg)
654 const struct got_error *err = NULL;
655 struct got_object_id_queue ids;
656 struct got_object_idset *keep, *drop;
657 struct got_object_qid *qid;
658 int i, ncolor, nkeep, obj_type;
660 STAILQ_INIT(&ids);
661 *res = NULL;
662 *nres = 0;
664 keep = got_object_idset_alloc();
665 if (keep == NULL)
666 return got_error_from_errno("got_object_idset_alloc");
668 drop = got_object_idset_alloc();
669 if (drop == NULL) {
670 err = got_error_from_errno("got_object_idset_alloc");
671 goto done;
674 for (i = 0; i < nhead; i++) {
675 struct got_object_id *id = head[i];
676 if (id == NULL)
677 continue;
678 err = got_object_get_type(&obj_type, repo, id);
679 if (err)
680 return err;
681 if (obj_type != GOT_OBJ_TYPE_COMMIT)
682 continue;
683 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
684 if (err)
685 goto done;
687 for (i = 0; i < ntail; i++) {
688 struct got_object_id *id = tail[i];
689 if (id == NULL)
690 continue;
691 err = got_object_get_type(&obj_type, repo, id);
692 if (err)
693 return err;
694 if (obj_type != GOT_OBJ_TYPE_COMMIT)
695 continue;
696 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
697 if (err)
698 goto done;
701 while (!STAILQ_EMPTY(&ids)) {
702 int qcolor;
703 qid = STAILQ_FIRST(&ids);
704 qcolor = *((int *)qid->data);
706 if (got_object_idset_contains(drop, qid->id))
707 ncolor = COLOR_DROP;
708 else if (got_object_idset_contains(keep, qid->id))
709 ncolor = COLOR_KEEP;
710 else
711 ncolor = COLOR_BLANK;
713 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
714 qcolor == COLOR_KEEP)) {
715 STAILQ_REMOVE_HEAD(&ids, entry);
716 got_object_qid_free(qid);
717 continue;
720 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
721 err = drop_commit(keep, drop, qid->id, repo,
722 cancel_cb, cancel_arg);
723 if (err)
724 goto done;
725 } else if (ncolor == COLOR_BLANK) {
726 struct got_commit_object *commit;
727 struct got_object_id *id;
728 const struct got_object_id_queue *parents;
729 struct got_object_qid *pid;
731 id = got_object_id_dup(qid->id);
732 if (id == NULL) {
733 err = got_error_from_errno("got_object_id_dup");
734 goto done;
736 if (qcolor == COLOR_KEEP)
737 err = got_object_idset_add(keep, id, NULL);
738 else
739 err = got_object_idset_add(drop, id, NULL);
740 if (err) {
741 free(id);
742 goto done;
745 err = got_object_open_as_commit(&commit, repo, id);
746 if (err) {
747 free(id);
748 goto done;
750 parents = got_object_commit_get_parent_ids(commit);
751 if (parents) {
752 STAILQ_FOREACH(pid, parents, entry) {
753 err = queue_commit_id(&ids, pid->id,
754 qcolor, repo);
755 if (err) {
756 free(id);
757 goto done;
761 got_object_commit_close(commit);
762 commit = NULL;
763 } else {
764 /* should not happen */
765 err = got_error_fmt(GOT_ERR_NOT_IMPL,
766 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
767 goto done;
770 STAILQ_REMOVE_HEAD(&ids, entry);
771 got_object_qid_free(qid);
774 nkeep = got_object_idset_num_elements(keep);
775 if (nkeep > 0) {
776 struct append_id_arg arg;
777 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
778 if (arg.array == NULL) {
779 err = got_error_from_errno("calloc");
780 goto done;
782 arg.idx = 0;
783 err = got_object_idset_for_each(keep, append_id, &arg);
784 if (err) {
785 free(arg.array);
786 goto done;
788 *res = arg.array;
789 *nres = nkeep;
791 done:
792 got_object_idset_free(keep);
793 got_object_idset_free(drop);
794 got_object_id_queue_free(&ids);
795 return err;
798 static const struct got_error *
799 read_meta(struct got_pack_meta ***meta, int *nmeta,
800 struct got_object_id **theirs, int ntheirs,
801 struct got_object_id **ours, int nours, struct got_repository *repo,
802 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
803 got_cancel_cb cancel_cb, void *cancel_arg)
805 const struct got_error *err = NULL;
806 struct got_object_id **ids = NULL;
807 struct got_object_idset *idset;
808 int i, nobj = 0, obj_type;
809 struct got_pack_metavec v;
811 *meta = NULL;
812 *nmeta = 0;
814 idset = got_object_idset_alloc();
815 if (idset == NULL)
816 return got_error_from_errno("got_object_idset_alloc");
818 v.nmeta = 0;
819 v.metasz = 64;
820 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
821 if (v.meta == NULL) {
822 err = got_error_from_errno("reallocarray");
823 goto done;
826 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
827 cancel_cb, cancel_arg);
828 if (err || nobj == 0)
829 goto done;
831 for (i = 0; i < ntheirs; i++) {
832 struct got_object_id *id = theirs[i];
833 if (id == NULL)
834 continue;
835 err = got_object_get_type(&obj_type, repo, id);
836 if (err)
837 return err;
838 if (obj_type != GOT_OBJ_TYPE_COMMIT)
839 continue;
840 err = load_commit(NULL, idset, id, repo,
841 loose_obj_only, cancel_cb, cancel_arg);
842 if (err)
843 goto done;
844 if (progress_cb) {
845 err = progress_cb(progress_arg, 0L, nours,
846 v.nmeta, 0, 0);
847 if (err)
848 goto done;
852 for (i = 0; i < nobj; i++) {
853 err = load_commit(&v, idset, ids[i], repo,
854 loose_obj_only, cancel_cb, cancel_arg);
855 if (err)
856 goto done;
857 if (progress_cb) {
858 err = progress_cb(progress_arg, 0L, nours,
859 v.nmeta, 0, 0);
860 if (err)
861 goto done;
865 for (i = 0; i < ntheirs; i++) {
866 struct got_object_id *id = ours[i];
867 if (id == NULL)
868 continue;
869 err = got_object_get_type(&obj_type, repo, id);
870 if (err)
871 return err;
872 if (obj_type != GOT_OBJ_TYPE_TAG)
873 continue;
874 err = load_tag(NULL, idset, id, repo,
875 loose_obj_only, cancel_cb, cancel_arg);
876 if (err)
877 goto done;
878 if (progress_cb) {
879 err = progress_cb(progress_arg, 0L, nours,
880 v.nmeta, 0, 0);
881 if (err)
882 goto done;
886 for (i = 0; i < nours; i++) {
887 struct got_object_id *id = ours[i];
888 if (id == NULL)
889 continue;
890 err = got_object_get_type(&obj_type, repo, id);
891 if (err)
892 return err;
893 if (obj_type != GOT_OBJ_TYPE_TAG)
894 continue;
895 err = load_tag(&v, idset, id, repo,
896 loose_obj_only, cancel_cb, cancel_arg);
897 if (err)
898 goto done;
899 if (progress_cb) {
900 err = progress_cb(progress_arg, 0L, nours,
901 v.nmeta, 0, 0);
902 if (err)
903 goto done;
907 done:
908 for (i = 0; i < nobj; i++) {
909 free(ids[i]);
911 free(ids);
912 got_object_idset_free(idset);
913 if (err == NULL) {
914 *meta = v.meta;
915 *nmeta = v.nmeta;
916 } else
917 free(v.meta);
919 return err;
922 const struct got_error *
923 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
925 size_t n;
927 SHA1Update(ctx, buf, len);
928 n = fwrite(buf, 1, len, f);
929 if (n != len)
930 return got_ferror(f, GOT_ERR_IO);
931 return NULL;
934 static void
935 putbe32(char *b, uint32_t n)
937 b[0] = n >> 24;
938 b[1] = n >> 16;
939 b[2] = n >> 8;
940 b[3] = n >> 0;
943 static int
944 write_order_cmp(const void *pa, const void *pb)
946 struct got_pack_meta *a, *b, *ahd, *bhd;
948 a = *(struct got_pack_meta **)pa;
949 b = *(struct got_pack_meta **)pb;
950 ahd = (a->head == NULL) ? a : a->head;
951 bhd = (b->head == NULL) ? b : b->head;
952 if (ahd->mtime != bhd->mtime)
953 return bhd->mtime - ahd->mtime;
954 if (ahd != bhd)
955 return (uintptr_t)bhd - (uintptr_t)ahd;
956 if (a->nchain != b->nchain)
957 return a->nchain - b->nchain;
958 return a->mtime - b->mtime;
961 static const struct got_error *
962 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
964 size_t i;
966 *hdrlen = 0;
968 hdr[0] = obj_type << 4;
969 hdr[0] |= len & 0xf;
970 len >>= 4;
971 for (i = 1; len != 0; i++){
972 if (i >= bufsize)
973 return got_error(GOT_ERR_NO_SPACE);
974 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
975 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
976 len >>= GOT_DELTA_SIZE_SHIFT;
979 *hdrlen = i;
980 return NULL;
983 static const struct got_error *
984 append(char **p, int *len, int *sz, void *seg, int nseg)
986 char *n;
988 if (*len + nseg >= *sz) {
989 while (*len + nseg >= *sz)
990 *sz += *sz / 2;
991 n = realloc(*p, *sz);
992 if (n == NULL)
993 return got_error_from_errno("realloc");
994 *p = n;
996 memcpy(*p + *len, seg, nseg);
997 *len += nseg;
998 return NULL;
1002 static const struct got_error *
1003 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1004 off_t base_size, char **pp)
1006 const struct got_error *err = NULL;
1007 char *p;
1008 unsigned char buf[16], *bp;
1009 int len, sz, i, j;
1010 off_t n;
1011 struct got_delta_instruction *d;
1013 *pp = NULL;
1014 *nd = 0;
1016 sz = 128;
1017 len = 0;
1018 p = malloc(sz);
1019 if (p == NULL)
1020 return got_error_from_errno("malloc");
1022 /* base object size */
1023 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1024 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1025 for (i = 1; n > 0; i++) {
1026 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1027 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1028 n >>= GOT_DELTA_SIZE_SHIFT;
1030 err = append(&p, &len, &sz, buf, i);
1031 if (err)
1032 return err;
1034 /* target object size */
1035 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1036 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1037 for (i = 1; n > 0; i++) {
1038 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1039 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1040 n >>= GOT_DELTA_SIZE_SHIFT;
1042 err = append(&p, &len, &sz, buf, i);
1043 if (err)
1044 return err;
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 err = append(&p, &len, &sz, buf, bp - buf);
1070 if (err)
1071 return err;
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 err = append(&p, &len, &sz, buf, 1);
1081 if (err)
1082 return err;
1083 r = fread(content, 1, buf[0], o->f);
1084 if (r != buf[0])
1085 return got_ferror(o->f, GOT_ERR_IO);
1086 err = append(&p, &len, &sz, content, buf[0]);
1087 if (err)
1088 return err;
1089 n += buf[0];
1093 *pp = p;
1094 *nd = len;
1095 return NULL;
1098 static int
1099 packoff(char *hdr, off_t off)
1101 int i, j;
1102 char rbuf[8];
1104 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1105 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1106 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1107 GOT_DELTA_SIZE_MORE;
1110 j = 0;
1111 while (i > 0)
1112 hdr[j++] = rbuf[--i];
1113 return j;
1116 static const struct got_error *
1117 genpack(uint8_t *pack_sha1, FILE *packfile,
1118 struct got_pack_meta **meta, int nmeta, int nours,
1119 int use_offset_deltas, struct got_repository *repo,
1120 got_pack_progress_cb progress_cb, void *progress_arg,
1121 got_cancel_cb cancel_cb, void *cancel_arg)
1123 const struct got_error *err = NULL;
1124 int i, nh, nd;
1125 SHA1_CTX ctx;
1126 struct got_pack_meta *m;
1127 struct got_raw_object *raw;
1128 char *p = NULL, buf[32];
1129 size_t outlen, n;
1130 struct got_deflate_checksum csum;
1131 off_t packfile_size = 0;
1133 SHA1Init(&ctx);
1134 csum.output_sha1 = &ctx;
1135 csum.output_crc = NULL;
1137 err = hwrite(packfile, "PACK", 4, &ctx);
1138 if (err)
1139 return err;
1140 putbe32(buf, GOT_PACKFILE_VERSION);
1141 err = hwrite(packfile, buf, 4, &ctx);
1142 if (err)
1143 goto done;
1144 putbe32(buf, nmeta);
1145 err = hwrite(packfile, buf, 4, &ctx);
1146 if (err)
1147 goto done;
1148 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1149 for (i = 0; i < nmeta; i++) {
1150 if (progress_cb) {
1151 err = progress_cb(progress_arg, packfile_size, nours,
1152 nmeta, nmeta, i);
1153 if (err)
1154 goto done;
1156 m = meta[i];
1157 m->off = ftello(packfile);
1158 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1159 if (err)
1160 goto done;
1161 if (m->deltas == NULL) {
1162 err = packhdr(&nh, buf, sizeof(buf),
1163 m->obj_type, raw->size);
1164 if (err)
1165 goto done;
1166 err = hwrite(packfile, buf, nh, &ctx);
1167 if (err)
1168 goto done;
1169 packfile_size += nh;
1170 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1171 err = got_error_from_errno("fseeko");
1172 goto done;
1174 err = got_deflate_to_file(&outlen, raw->f, packfile,
1175 &csum);
1176 if (err)
1177 goto done;
1178 packfile_size += outlen;
1179 } else {
1180 FILE *delta_file;
1181 struct got_raw_object *base_raw;
1182 err = got_object_raw_open(&base_raw, repo,
1183 &m->prev->id, 8192);
1184 if (err)
1185 goto done;
1186 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1187 if (err)
1188 goto done;
1189 got_object_raw_close(base_raw);
1190 if (use_offset_deltas && m->prev->off != 0) {
1191 err = packhdr(&nh, buf, sizeof(buf),
1192 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1193 if (err)
1194 goto done;
1195 nh += packoff(buf + nh,
1196 m->off - m->prev->off);
1197 err = hwrite(packfile, buf, nh, &ctx);
1198 if (err)
1199 goto done;
1200 packfile_size += nh;
1201 } else {
1202 err = packhdr(&nh, buf, sizeof(buf),
1203 GOT_OBJ_TYPE_REF_DELTA, nd);
1204 err = hwrite(packfile, buf, nh, &ctx);
1205 if (err)
1206 goto done;
1207 packfile_size += nh;
1208 err = hwrite(packfile, m->prev->id.sha1,
1209 sizeof(m->prev->id.sha1), &ctx);
1210 packfile_size += sizeof(m->prev->id.sha1);
1211 if (err)
1212 goto done;
1214 /* XXX need got_deflate_from_mem() */
1215 delta_file = fmemopen(p, nd, "r");
1216 if (delta_file == NULL) {
1217 err = got_error_from_errno("fmemopen");
1218 goto done;
1220 err = got_deflate_to_file(&outlen, delta_file,
1221 packfile, &csum);
1222 fclose(delta_file);
1223 if (err)
1224 goto done;
1225 packfile_size += outlen;
1226 free(p);
1227 p = NULL;
1229 got_object_raw_close(raw);
1230 raw = NULL;
1232 SHA1Final(pack_sha1, &ctx);
1233 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1234 if (n != SHA1_DIGEST_LENGTH)
1235 err = got_ferror(packfile, GOT_ERR_IO);
1236 packfile_size += SHA1_DIGEST_LENGTH;
1237 packfile_size += 16; /* pack file header */
1238 err = progress_cb(progress_arg, packfile_size, nours,
1239 nmeta, nmeta, nmeta);
1240 if (err)
1241 goto done;
1242 done:
1243 free(p);
1244 return err;
1247 const struct got_error *
1248 got_pack_create(uint8_t *packsha1, FILE *packfile,
1249 struct got_object_id **theirs, int ntheirs,
1250 struct got_object_id **ours, int nours,
1251 struct got_repository *repo, int loose_obj_only,
1252 got_pack_progress_cb progress_cb, void *progress_arg,
1253 got_cancel_cb cancel_cb, void *cancel_arg)
1255 const struct got_error *err;
1256 struct got_pack_meta **meta;
1257 int nmeta;
1259 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1260 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1261 if (err)
1262 return err;
1264 if (nmeta == 0) {
1265 err = got_error(GOT_ERR_CANNOT_PACK);
1266 goto done;
1269 err = pick_deltas(meta, nmeta, nours, repo,
1270 progress_cb, progress_arg, cancel_cb, cancel_arg);
1271 if (err)
1272 goto done;
1274 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1275 progress_cb, progress_arg, cancel_cb, cancel_arg);
1276 if (err)
1277 goto done;
1278 done:
1279 free_nmeta(meta, nmeta);
1280 return err;