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/queue.h>
19 #include <sys/stat.h>
21 #include <inttypes.h>
22 #include <imsg.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sha1.h>
27 #include <limits.h>
28 #include <zlib.h>
30 #include "got_error.h"
31 #include "got_cancel.h"
32 #include "got_object.h"
33 #include "got_reference.h"
34 #include "got_repository_admin.h"
36 #include "got_lib_deltify.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_object.h"
39 #include "got_lib_object_idset.h"
40 #include "got_lib_object_cache.h"
41 #include "got_lib_deflate.h"
42 #include "got_lib_pack.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_repository.h"
46 #ifndef MAX
47 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
48 #endif
50 struct got_pack_meta {
51 struct got_object_id id;
52 char *path;
53 int obj_type;
54 time_t mtime;
56 /* The best delta we picked */
57 struct got_pack_meta *head;
58 struct got_pack_meta *prev;
59 struct got_delta_instruction *deltas;
60 int ndeltas;
61 int nchain;
63 /* Only used for delta window */
64 struct got_delta_table *dtab;
66 /* Only used for writing offset deltas */
67 off_t off;
68 };
70 struct got_pack_metavec {
71 struct got_pack_meta **meta;
72 int nmeta;
73 int metasz;
74 };
76 static const struct got_error *
77 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
78 const char *path, int obj_type, time_t mtime)
79 {
80 const struct got_error *err = NULL;
81 struct got_pack_meta *m;
83 *new = NULL;
85 m = calloc(1, sizeof(*m));
86 if (m == NULL)
87 return got_error_from_errno("malloc");
89 memcpy(&m->id, id, sizeof(m->id));
91 m->path = strdup(path);
92 if (m->path == NULL) {
93 err = got_error_from_errno("strdup");
94 free(m);
95 return err;
96 }
98 m->obj_type = obj_type;
99 m->mtime = mtime;
100 *new = m;
101 return NULL;
104 static void
105 clear_meta(struct got_pack_meta *meta)
107 if (meta == NULL)
108 return;
109 free(meta->deltas);
110 meta->deltas = NULL;
111 free(meta->path);
112 meta->path = NULL;
115 static void
116 free_nmeta(struct got_pack_meta **meta, int nmeta)
118 int i;
120 for (i = 0; i < nmeta; i++)
121 clear_meta(meta[i]);
122 free(meta);
125 static int
126 delta_order_cmp(const void *pa, const void *pb)
128 struct got_pack_meta *a, *b;
129 int cmp;
131 a = *(struct got_pack_meta **)pa;
132 b = *(struct got_pack_meta **)pb;
134 if (a->obj_type != b->obj_type)
135 return a->obj_type - b->obj_type;
136 cmp = strcmp(a->path, b->path);
137 if (cmp != 0)
138 return cmp;
139 if (a->mtime != b->mtime)
140 return a->mtime - b->mtime;
141 return got_object_id_cmp(&a->id, &b->id);
144 static int
145 delta_size(struct got_delta_instruction *deltas, int ndeltas)
147 int i, size = 32;
148 for (i = 0; i < ndeltas; i++) {
149 if (deltas[i].copy)
150 size += GOT_DELTA_SIZE_SHIFT;
151 else
152 size += deltas[i].len + 1;
154 return size;
158 static const struct got_error *
159 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
160 struct got_repository *repo,
161 got_pack_progress_cb progress_cb, void *progress_arg,
162 got_cancel_cb cancel_cb, void *cancel_arg)
164 const struct got_error *err = NULL;
165 struct got_pack_meta *m = NULL, *base = NULL;
166 struct got_raw_object *raw = NULL, *base_raw = NULL;
167 struct got_delta_instruction *deltas;
168 int i, j, size, ndeltas, best;
169 const int max_base_candidates = 10;
171 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
172 for (i = 0; i < nmeta; i++) {
173 if (cancel_cb) {
174 err = (*cancel_cb)(cancel_arg);
175 if (err)
176 break;
178 if (progress_cb) {
179 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
180 if (err)
181 goto done;
183 m = meta[i];
184 m->deltas = NULL;
185 m->ndeltas = 0;
187 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
188 m->obj_type == GOT_OBJ_TYPE_TAG)
189 continue;
191 err = got_object_raw_open(&raw, repo, &m->id, 8192);
192 if (err)
193 goto done;
195 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
196 raw->size + raw->hdrlen);
197 if (err)
198 goto done;
200 if (i > max_base_candidates) {
201 struct got_pack_meta *n = NULL;
202 n = meta[i - (max_base_candidates + 1)];
203 got_deltify_free(n->dtab);
204 n->dtab = NULL;
207 best = raw->size;
208 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
209 if (cancel_cb) {
210 err = (*cancel_cb)(cancel_arg);
211 if (err)
212 goto done;
214 base = meta[j];
215 /* long chains make unpacking slow, avoid such bases */
216 if (base->nchain >= 128 ||
217 base->obj_type != m->obj_type)
218 continue;
220 err = got_object_raw_open(&base_raw, repo, &base->id,
221 8192);
222 if (err)
223 goto done;
224 err = got_deltify(&deltas, &ndeltas,
225 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
226 base->dtab, base_raw->f, base_raw->hdrlen,
227 base_raw->size + base_raw->hdrlen);
228 got_object_raw_close(base_raw);
229 base_raw = NULL;
230 if (err)
231 goto done;
233 size = delta_size(deltas, ndeltas);
234 if (size + 32 < best){
235 /*
236 * if we already picked a best delta,
237 * replace it.
238 */
239 free(m->deltas);
240 best = size;
241 m->deltas = deltas;
242 m->ndeltas = ndeltas;
243 m->nchain = base->nchain + 1;
244 m->prev = base;
245 m->head = base->head;
246 if (m->head == NULL)
247 m->head = base;
248 } else {
249 free(deltas);
250 deltas = NULL;
251 ndeltas = 0;
255 got_object_raw_close(raw);
256 raw = NULL;
258 done:
259 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
260 got_deltify_free(meta[i]->dtab);
261 meta[i]->dtab = NULL;
263 if (raw)
264 got_object_raw_close(raw);
265 if (base_raw)
266 got_object_raw_close(base_raw);
267 return err;
270 static const struct got_error *
271 search_packidx(int *found, struct got_object_id *id,
272 struct got_repository *repo)
274 const struct got_error *err = NULL;
275 struct got_packidx *packidx = NULL;
276 int idx;
278 *found = 0;
280 err = got_repo_search_packidx(&packidx, &idx, repo, id);
281 if (err == NULL)
282 *found = 1; /* object is already packed */
283 else if (err->code == GOT_ERR_NO_OBJ)
284 err = NULL;
285 return err;
288 static const struct got_error *
289 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
290 struct got_object_id *id, const char *path, int obj_type,
291 time_t mtime, int loose_obj_only, struct got_repository *repo)
293 const struct got_error *err;
294 struct got_pack_meta *m;
296 if (loose_obj_only) {
297 int is_packed;
298 err = search_packidx(&is_packed, id, repo);
299 if (err)
300 return err;
301 if (is_packed)
302 return NULL;
305 err = got_object_idset_add(idset, id, NULL);
306 if (err)
307 return err;
309 if (v == NULL)
310 return NULL;
312 err = alloc_meta(&m, id, path, obj_type, mtime);
313 if (err)
314 goto done;
316 if (v->nmeta == v->metasz){
317 size_t newsize = 2 * v->metasz;
318 struct got_pack_meta **new;
319 new = reallocarray(v->meta, newsize, sizeof(*new));
320 if (new == NULL) {
321 err = got_error_from_errno("reallocarray");
322 goto done;
324 v->meta = new;
325 v->metasz = newsize;
327 done:
328 if (err) {
329 clear_meta(m);
330 free(m);
331 } else
332 v->meta[v->nmeta++] = m;
334 return err;
337 static const struct got_error *
338 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
339 struct got_object_idset *idset, struct got_object_id *tree_id,
340 const char *dpath, time_t mtime, struct got_repository *repo,
341 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
343 const struct got_error *err;
344 struct got_tree_object *tree;
345 char *p = NULL;
346 int i;
348 err = got_object_open_as_tree(&tree, repo, tree_id);
349 if (err)
350 return err;
352 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
353 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
354 struct got_object_id *id = got_tree_entry_get_id(e);
355 mode_t mode = got_tree_entry_get_mode(e);
357 if (cancel_cb) {
358 err = (*cancel_cb)(cancel_arg);
359 if (err)
360 break;
363 if (got_object_tree_entry_is_symlink(e) ||
364 got_object_tree_entry_is_submodule(e) ||
365 got_object_idset_contains(idset, id))
366 continue;
368 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
369 got_tree_entry_get_name(e)) == -1) {
370 err = got_error_from_errno("asprintf");
371 break;
374 if (S_ISDIR(mode)) {
375 struct got_object_qid *qid;
376 err = got_object_qid_alloc(&qid, id);
377 if (err)
378 break;
379 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
380 } else if (S_ISREG(mode)) {
381 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
382 mtime, loose_obj_only, repo);
383 if (err)
384 break;
386 free(p);
387 p = NULL;
390 got_object_tree_close(tree);
391 free(p);
392 return err;
395 static const struct got_error *
396 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
397 struct got_object_id *tree_id, const char *dpath, time_t mtime,
398 int loose_obj_only, struct got_repository *repo,
399 got_cancel_cb cancel_cb, void *cancel_arg)
401 const struct got_error *err = NULL;
402 struct got_object_id_queue tree_ids;
403 struct got_object_qid *qid;
405 if (got_object_idset_contains(idset, tree_id))
406 return NULL;
408 err = got_object_qid_alloc(&qid, tree_id);
409 if (err)
410 return err;
412 SIMPLEQ_INIT(&tree_ids);
413 SIMPLEQ_INSERT_TAIL(&tree_ids, qid, entry);
415 while (!SIMPLEQ_EMPTY(&tree_ids)) {
416 if (cancel_cb) {
417 err = (*cancel_cb)(cancel_arg);
418 if (err)
419 break;
422 qid = SIMPLEQ_FIRST(&tree_ids);
423 SIMPLEQ_REMOVE_HEAD(&tree_ids, entry);
425 if (got_object_idset_contains(idset, qid->id)) {
426 got_object_qid_free(qid);
427 continue;
430 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
431 mtime, loose_obj_only, repo);
432 if (err) {
433 got_object_qid_free(qid);
434 break;
437 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
438 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
439 got_object_qid_free(qid);
440 if (err)
441 break;
444 got_object_id_queue_free(&tree_ids);
445 return err;
448 static const struct got_error *
449 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
450 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
451 got_cancel_cb cancel_cb, void *cancel_arg)
453 const struct got_error *err;
454 struct got_commit_object *commit;
456 if (got_object_idset_contains(idset, id))
457 return NULL;
459 if (loose_obj_only) {
460 int is_packed;
461 err = search_packidx(&is_packed, id, repo);
462 if (err)
463 return err;
464 if (is_packed)
465 return NULL;
468 err = got_object_open_as_commit(&commit, repo, id);
469 if (err)
470 return err;
472 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
473 got_object_commit_get_committer_time(commit),
474 loose_obj_only, repo);
475 if (err)
476 goto done;
478 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
479 "", got_object_commit_get_committer_time(commit),
480 loose_obj_only, repo, cancel_cb, cancel_arg);
481 done:
482 got_object_commit_close(commit);
483 return err;
486 static const struct got_error *
487 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
488 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
489 got_cancel_cb cancel_cb, void *cancel_arg)
491 const struct got_error *err;
492 struct got_tag_object *tag = NULL;
494 if (got_object_idset_contains(idset, id))
495 return NULL;
497 if (loose_obj_only) {
498 int is_packed;
499 err = search_packidx(&is_packed, id, repo);
500 if (err)
501 return err;
502 if (is_packed)
503 return NULL;
506 err = got_object_open_as_tag(&tag, repo, id);
507 if (err)
508 return err;
510 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
511 got_object_tag_get_tagger_time(tag),
512 loose_obj_only, repo);
513 if (err)
514 goto done;
516 switch (got_object_tag_get_object_type(tag)) {
517 case GOT_OBJ_TYPE_COMMIT:
518 err = load_commit(NULL, idset,
519 got_object_tag_get_object_id(tag), repo,
520 loose_obj_only, cancel_cb, cancel_arg);
521 break;
522 case GOT_OBJ_TYPE_TREE:
523 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
524 "", got_object_tag_get_tagger_time(tag),
525 loose_obj_only, repo, cancel_cb, cancel_arg);
526 break;
527 default:
528 break;
531 done:
532 got_object_tag_close(tag);
533 return err;
536 enum findtwixt_color {
537 COLOR_KEEP = 0,
538 COLOR_DROP,
539 COLOR_BLANK,
540 };
541 static const int findtwixt_colors[] = {
542 COLOR_KEEP,
543 COLOR_DROP,
544 COLOR_BLANK
545 };
547 static const struct got_error *
548 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
549 int color, struct got_repository *repo)
551 const struct got_error *err;
552 struct got_object_qid *qid;
554 err = got_object_qid_alloc(&qid, id);
555 if (err)
556 return err;
558 SIMPLEQ_INSERT_TAIL(ids, qid, entry);
559 qid->data = (void *)&findtwixt_colors[color];
560 return NULL;
563 static const struct got_error *
564 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
565 struct got_object_id *id, struct got_repository *repo,
566 got_cancel_cb cancel_cb, void *cancel_arg)
568 const struct got_error *err = NULL;
569 struct got_commit_object *commit;
570 const struct got_object_id_queue *parents;
571 struct got_object_id_queue ids;
572 struct got_object_qid *qid;
574 SIMPLEQ_INIT(&ids);
576 err = got_object_qid_alloc(&qid, id);
577 if (err)
578 return err;
579 SIMPLEQ_INSERT_HEAD(&ids, qid, entry);
581 while (!SIMPLEQ_EMPTY(&ids)) {
582 if (cancel_cb) {
583 err = (*cancel_cb)(cancel_arg);
584 if (err)
585 break;
588 qid = SIMPLEQ_FIRST(&ids);
589 SIMPLEQ_REMOVE_HEAD(&ids, entry);
591 if (got_object_idset_contains(drop, qid->id)) {
592 got_object_qid_free(qid);
593 continue;
596 err = got_object_idset_add(drop, qid->id, NULL);
597 if (err) {
598 got_object_qid_free(qid);
599 break;
602 if (!got_object_idset_contains(keep, qid->id)) {
603 got_object_qid_free(qid);
604 continue;
607 err = got_object_open_as_commit(&commit, repo, qid->id);
608 got_object_qid_free(qid);
609 if (err)
610 break;
612 parents = got_object_commit_get_parent_ids(commit);
613 if (parents) {
614 err = got_object_id_queue_copy(parents, &ids);
615 if (err) {
616 got_object_commit_close(commit);
617 break;
620 got_object_commit_close(commit);
623 got_object_id_queue_free(&ids);
624 return err;
627 struct append_id_arg {
628 struct got_object_id **array;
629 int idx;
630 };
632 static const struct got_error *
633 append_id(struct got_object_id *id, void *data, void *arg)
635 struct append_id_arg *a = arg;
637 a->array[a->idx] = got_object_id_dup(id);
638 if (a->array[a->idx] == NULL)
639 return got_error_from_errno("got_object_id_dup");
641 a->idx++;
642 return NULL;
645 static const struct got_error *
646 findtwixt(struct got_object_id ***res, int *nres,
647 struct got_object_id **head, int nhead,
648 struct got_object_id **tail, int ntail,
649 struct got_repository *repo,
650 got_cancel_cb cancel_cb, void *cancel_arg)
652 const struct got_error *err = NULL;
653 struct got_object_id_queue ids;
654 struct got_object_idset *keep, *drop;
655 struct got_object_qid *qid;
656 int i, ncolor, nkeep, obj_type;
658 SIMPLEQ_INIT(&ids);
659 *res = NULL;
660 *nres = 0;
662 keep = got_object_idset_alloc();
663 if (keep == NULL)
664 return got_error_from_errno("got_object_idset_alloc");
666 drop = got_object_idset_alloc();
667 if (drop == NULL) {
668 err = got_error_from_errno("got_object_idset_alloc");
669 goto done;
672 for (i = 0; i < nhead; i++) {
673 struct got_object_id *id = head[i];
674 if (id == NULL)
675 continue;
676 err = got_object_get_type(&obj_type, repo, id);
677 if (err)
678 return err;
679 if (obj_type != GOT_OBJ_TYPE_COMMIT)
680 continue;
681 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
682 if (err)
683 goto done;
685 for (i = 0; i < ntail; i++) {
686 struct got_object_id *id = tail[i];
687 if (id == NULL)
688 continue;
689 err = got_object_get_type(&obj_type, repo, id);
690 if (err)
691 return err;
692 if (obj_type != GOT_OBJ_TYPE_COMMIT)
693 continue;
694 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
695 if (err)
696 goto done;
699 while (!SIMPLEQ_EMPTY(&ids)) {
700 int qcolor;
701 qid = SIMPLEQ_FIRST(&ids);
702 qcolor = *((int *)qid->data);
704 if (got_object_idset_contains(drop, qid->id))
705 ncolor = COLOR_DROP;
706 else if (got_object_idset_contains(keep, qid->id))
707 ncolor = COLOR_KEEP;
708 else
709 ncolor = COLOR_BLANK;
711 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
712 qcolor == COLOR_KEEP)) {
713 SIMPLEQ_REMOVE_HEAD(&ids, entry);
714 got_object_qid_free(qid);
715 continue;
718 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
719 err = drop_commit(keep, drop, qid->id, repo,
720 cancel_cb, cancel_arg);
721 if (err)
722 goto done;
723 } else if (ncolor == COLOR_BLANK) {
724 struct got_commit_object *commit;
725 struct got_object_id *id;
726 const struct got_object_id_queue *parents;
727 struct got_object_qid *pid;
729 id = got_object_id_dup(qid->id);
730 if (id == NULL) {
731 err = got_error_from_errno("got_object_id_dup");
732 goto done;
734 if (qcolor == COLOR_KEEP)
735 err = got_object_idset_add(keep, id, NULL);
736 else
737 err = got_object_idset_add(drop, id, NULL);
738 if (err) {
739 free(id);
740 goto done;
743 err = got_object_open_as_commit(&commit, repo, id);
744 if (err) {
745 free(id);
746 goto done;
748 parents = got_object_commit_get_parent_ids(commit);
749 if (parents) {
750 SIMPLEQ_FOREACH(pid, parents, entry) {
751 err = queue_commit_id(&ids, pid->id,
752 qcolor, repo);
753 if (err) {
754 free(id);
755 goto done;
759 got_object_commit_close(commit);
760 commit = NULL;
761 } else {
762 /* should not happen */
763 err = got_error_fmt(GOT_ERR_NOT_IMPL,
764 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
765 goto done;
768 SIMPLEQ_REMOVE_HEAD(&ids, entry);
769 got_object_qid_free(qid);
772 nkeep = got_object_idset_num_elements(keep);
773 if (nkeep > 0) {
774 struct append_id_arg arg;
775 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
776 if (arg.array == NULL) {
777 err = got_error_from_errno("calloc");
778 goto done;
780 arg.idx = 0;
781 err = got_object_idset_for_each(keep, append_id, &arg);
782 if (err) {
783 free(arg.array);
784 goto done;
786 *res = arg.array;
787 *nres = nkeep;
789 done:
790 got_object_idset_free(keep);
791 got_object_idset_free(drop);
792 got_object_id_queue_free(&ids);
793 return err;
796 static const struct got_error *
797 read_meta(struct got_pack_meta ***meta, int *nmeta,
798 struct got_object_id **theirs, int ntheirs,
799 struct got_object_id **ours, int nours, struct got_repository *repo,
800 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
801 got_cancel_cb cancel_cb, void *cancel_arg)
803 const struct got_error *err = NULL;
804 struct got_object_id **ids = NULL;
805 struct got_object_idset *idset;
806 int i, nobj = 0, obj_type;
807 struct got_pack_metavec v;
809 *meta = NULL;
810 *nmeta = 0;
812 idset = got_object_idset_alloc();
813 if (idset == NULL)
814 return got_error_from_errno("got_object_idset_alloc");
816 v.nmeta = 0;
817 v.metasz = 64;
818 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
819 if (v.meta == NULL) {
820 err = got_error_from_errno("reallocarray");
821 goto done;
824 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
825 cancel_cb, cancel_arg);
826 if (err || nobj == 0)
827 goto done;
829 for (i = 0; i < ntheirs; i++) {
830 struct got_object_id *id = theirs[i];
831 if (id == NULL)
832 continue;
833 err = got_object_get_type(&obj_type, repo, id);
834 if (err)
835 return err;
836 if (obj_type != GOT_OBJ_TYPE_COMMIT)
837 continue;
838 err = load_commit(NULL, idset, id, repo,
839 loose_obj_only, cancel_cb, cancel_arg);
840 if (err)
841 goto done;
842 if (progress_cb) {
843 err = progress_cb(progress_arg, 0L, nours,
844 v.nmeta, 0, 0);
845 if (err)
846 goto done;
850 for (i = 0; i < nobj; i++) {
851 err = load_commit(&v, idset, ids[i], repo,
852 loose_obj_only, cancel_cb, cancel_arg);
853 if (err)
854 goto done;
855 if (progress_cb) {
856 err = progress_cb(progress_arg, 0L, nours,
857 v.nmeta, 0, 0);
858 if (err)
859 goto done;
863 for (i = 0; i < ntheirs; i++) {
864 struct got_object_id *id = ours[i];
865 if (id == NULL)
866 continue;
867 err = got_object_get_type(&obj_type, repo, id);
868 if (err)
869 return err;
870 if (obj_type != GOT_OBJ_TYPE_TAG)
871 continue;
872 err = load_tag(NULL, idset, id, repo,
873 loose_obj_only, cancel_cb, cancel_arg);
874 if (err)
875 goto done;
876 if (progress_cb) {
877 err = progress_cb(progress_arg, 0L, nours,
878 v.nmeta, 0, 0);
879 if (err)
880 goto done;
884 for (i = 0; i < nours; i++) {
885 struct got_object_id *id = ours[i];
886 if (id == NULL)
887 continue;
888 err = got_object_get_type(&obj_type, repo, id);
889 if (err)
890 return err;
891 if (obj_type != GOT_OBJ_TYPE_TAG)
892 continue;
893 err = load_tag(&v, idset, id, repo,
894 loose_obj_only, cancel_cb, cancel_arg);
895 if (err)
896 goto done;
897 if (progress_cb) {
898 err = progress_cb(progress_arg, 0L, nours,
899 v.nmeta, 0, 0);
900 if (err)
901 goto done;
905 done:
906 for (i = 0; i < nobj; i++) {
907 free(ids[i]);
909 free(ids);
910 got_object_idset_free(idset);
911 if (err == NULL) {
912 *meta = v.meta;
913 *nmeta = v.nmeta;
914 } else
915 free(v.meta);
917 return err;
920 const struct got_error *
921 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
923 size_t n;
925 SHA1Update(ctx, buf, len);
926 n = fwrite(buf, 1, len, f);
927 if (n != len)
928 return got_ferror(f, GOT_ERR_IO);
929 return NULL;
932 static void
933 putbe32(char *b, uint32_t n)
935 b[0] = n >> 24;
936 b[1] = n >> 16;
937 b[2] = n >> 8;
938 b[3] = n >> 0;
941 static int
942 write_order_cmp(const void *pa, const void *pb)
944 struct got_pack_meta *a, *b, *ahd, *bhd;
946 a = *(struct got_pack_meta **)pa;
947 b = *(struct got_pack_meta **)pb;
948 ahd = (a->head == NULL) ? a : a->head;
949 bhd = (b->head == NULL) ? b : b->head;
950 if (ahd->mtime != bhd->mtime)
951 return bhd->mtime - ahd->mtime;
952 if (ahd != bhd)
953 return (uintptr_t)bhd - (uintptr_t)ahd;
954 if (a->nchain != b->nchain)
955 return a->nchain - b->nchain;
956 return a->mtime - b->mtime;
959 static const struct got_error *
960 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
962 size_t i;
964 *hdrlen = 0;
966 hdr[0] = obj_type << 4;
967 hdr[0] |= len & 0xf;
968 len >>= 4;
969 for (i = 1; len != 0; i++){
970 if (i >= bufsize)
971 return got_error(GOT_ERR_NO_SPACE);
972 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
973 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
974 len >>= GOT_DELTA_SIZE_SHIFT;
977 *hdrlen = i;
978 return NULL;
981 static const struct got_error *
982 append(char **p, int *len, int *sz, void *seg, int nseg)
984 char *n;
986 if (*len + nseg >= *sz) {
987 while (*len + nseg >= *sz)
988 *sz += *sz / 2;
989 n = realloc(*p, *sz);
990 if (n == NULL)
991 return got_error_from_errno("realloc");
992 *p = n;
994 memcpy(*p + *len, seg, nseg);
995 *len += nseg;
996 return NULL;
1000 static const struct got_error *
1001 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1002 off_t base_size, char **pp)
1004 const struct got_error *err = NULL;
1005 char *p;
1006 unsigned char buf[16], *bp;
1007 int len, sz, i, j;
1008 off_t n;
1009 struct got_delta_instruction *d;
1011 *pp = NULL;
1012 *nd = 0;
1014 sz = 128;
1015 len = 0;
1016 p = malloc(sz);
1017 if (p == NULL)
1018 return got_error_from_errno("malloc");
1020 /* base object size */
1021 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1022 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1023 for (i = 1; n > 0; i++) {
1024 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1025 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1026 n >>= GOT_DELTA_SIZE_SHIFT;
1028 err = append(&p, &len, &sz, buf, i);
1029 if (err)
1030 return err;
1032 /* target object size */
1033 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1034 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1035 for (i = 1; n > 0; i++) {
1036 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1037 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1038 n >>= GOT_DELTA_SIZE_SHIFT;
1040 err = append(&p, &len, &sz, buf, i);
1041 if (err)
1042 return err;
1043 for (j = 0; j < m->ndeltas; j++) {
1044 d = &m->deltas[j];
1045 if (d->copy) {
1046 n = d->offset;
1047 bp = &buf[1];
1048 buf[0] = GOT_DELTA_BASE_COPY;
1049 for (i = 0; i < 4; i++) {
1050 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1051 buf[0] |= 1 << i;
1052 *bp++ = n & 0xff;
1053 n >>= 8;
1054 if (n == 0)
1055 break;
1058 n = d->len;
1059 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1060 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1061 for (i = 0; i < 3 && n > 0; i++) {
1062 buf[0] |= 1 << (i + 4);
1063 *bp++ = n & 0xff;
1064 n >>= 8;
1067 err = append(&p, &len, &sz, buf, bp - buf);
1068 if (err)
1069 return err;
1070 } else {
1071 char content[128];
1072 size_t r;
1073 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1074 return got_error_from_errno("fseeko");
1075 n = 0;
1076 while (n != d->len) {
1077 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1078 err = append(&p, &len, &sz, buf, 1);
1079 if (err)
1080 return err;
1081 r = fread(content, 1, buf[0], o->f);
1082 if (r != buf[0])
1083 return got_ferror(o->f, GOT_ERR_IO);
1084 err = append(&p, &len, &sz, content, buf[0]);
1085 if (err)
1086 return err;
1087 n += buf[0];
1091 *pp = p;
1092 *nd = len;
1093 return NULL;
1096 static int
1097 packoff(char *hdr, off_t off)
1099 int i, j;
1100 char rbuf[8];
1102 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1103 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1104 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1105 GOT_DELTA_SIZE_MORE;
1108 j = 0;
1109 while (i > 0)
1110 hdr[j++] = rbuf[--i];
1111 return j;
1114 static const struct got_error *
1115 genpack(uint8_t *pack_sha1, FILE *packfile,
1116 struct got_pack_meta **meta, int nmeta, int nours,
1117 int use_offset_deltas, struct got_repository *repo,
1118 got_pack_progress_cb progress_cb, void *progress_arg,
1119 got_cancel_cb cancel_cb, void *cancel_arg)
1121 const struct got_error *err = NULL;
1122 int i, nh, nd;
1123 SHA1_CTX ctx;
1124 struct got_pack_meta *m;
1125 struct got_raw_object *raw;
1126 char *p = NULL, buf[32];
1127 size_t outlen, n;
1128 struct got_deflate_checksum csum;
1129 off_t packfile_size = 0;
1131 SHA1Init(&ctx);
1132 csum.output_sha1 = &ctx;
1133 csum.output_crc = NULL;
1135 err = hwrite(packfile, "PACK", 4, &ctx);
1136 if (err)
1137 return err;
1138 putbe32(buf, GOT_PACKFILE_VERSION);
1139 err = hwrite(packfile, buf, 4, &ctx);
1140 if (err)
1141 goto done;
1142 putbe32(buf, nmeta);
1143 err = hwrite(packfile, buf, 4, &ctx);
1144 if (err)
1145 goto done;
1146 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1147 for (i = 0; i < nmeta; i++) {
1148 if (progress_cb) {
1149 err = progress_cb(progress_arg, packfile_size, nours,
1150 nmeta, nmeta, i);
1151 if (err)
1152 goto done;
1154 m = meta[i];
1155 m->off = ftello(packfile);
1156 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1157 if (err)
1158 goto done;
1159 if (m->deltas == NULL) {
1160 err = packhdr(&nh, buf, sizeof(buf),
1161 m->obj_type, raw->size);
1162 if (err)
1163 goto done;
1164 err = hwrite(packfile, buf, nh, &ctx);
1165 if (err)
1166 goto done;
1167 packfile_size += nh;
1168 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1169 err = got_error_from_errno("fseeko");
1170 goto done;
1172 err = got_deflate_to_file(&outlen, raw->f, packfile,
1173 &csum);
1174 if (err)
1175 goto done;
1176 packfile_size += outlen;
1177 } else {
1178 FILE *delta_file;
1179 struct got_raw_object *base_raw;
1180 err = got_object_raw_open(&base_raw, repo,
1181 &m->prev->id, 8192);
1182 if (err)
1183 goto done;
1184 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1185 if (err)
1186 goto done;
1187 got_object_raw_close(base_raw);
1188 if (use_offset_deltas && m->prev->off != 0) {
1189 err = packhdr(&nh, buf, sizeof(buf),
1190 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1191 if (err)
1192 goto done;
1193 nh += packoff(buf + nh,
1194 m->off - m->prev->off);
1195 err = hwrite(packfile, buf, nh, &ctx);
1196 if (err)
1197 goto done;
1198 packfile_size += nh;
1199 } else {
1200 err = packhdr(&nh, buf, sizeof(buf),
1201 GOT_OBJ_TYPE_REF_DELTA, nd);
1202 err = hwrite(packfile, buf, nh, &ctx);
1203 if (err)
1204 goto done;
1205 packfile_size += nh;
1206 err = hwrite(packfile, m->prev->id.sha1,
1207 sizeof(m->prev->id.sha1), &ctx);
1208 packfile_size += sizeof(m->prev->id.sha1);
1209 if (err)
1210 goto done;
1212 /* XXX need got_deflate_from_mem() */
1213 delta_file = fmemopen(p, nd, "r");
1214 if (delta_file == NULL) {
1215 err = got_error_from_errno("fmemopen");
1216 goto done;
1218 err = got_deflate_to_file(&outlen, delta_file,
1219 packfile, &csum);
1220 fclose(delta_file);
1221 if (err)
1222 goto done;
1223 packfile_size += outlen;
1224 free(p);
1225 p = NULL;
1227 got_object_raw_close(raw);
1228 raw = NULL;
1230 SHA1Final(pack_sha1, &ctx);
1231 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1232 if (n != SHA1_DIGEST_LENGTH)
1233 err = got_ferror(packfile, GOT_ERR_IO);
1234 packfile_size += SHA1_DIGEST_LENGTH;
1235 packfile_size += 16; /* pack file header */
1236 err = progress_cb(progress_arg, packfile_size, nours,
1237 nmeta, nmeta, nmeta);
1238 if (err)
1239 goto done;
1240 done:
1241 free(p);
1242 return err;
1245 const struct got_error *
1246 got_pack_create(uint8_t *packsha1, FILE *packfile,
1247 struct got_object_id **theirs, int ntheirs,
1248 struct got_object_id **ours, int nours,
1249 struct got_repository *repo, int loose_obj_only,
1250 got_pack_progress_cb progress_cb, void *progress_arg,
1251 got_cancel_cb cancel_cb, void *cancel_arg)
1253 const struct got_error *err;
1254 struct got_pack_meta **meta;
1255 int nmeta;
1257 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1258 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1259 if (err)
1260 return err;
1262 if (nmeta == 0) {
1263 err = got_error(GOT_ERR_CANNOT_PACK);
1264 goto done;
1267 err = pick_deltas(meta, nmeta, nours, repo,
1268 progress_cb, progress_arg, cancel_cb, cancel_arg);
1269 if (err)
1270 goto done;
1272 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1273 progress_cb, progress_arg, cancel_cb, cancel_arg);
1274 if (err)
1275 goto done;
1276 done:
1277 free_nmeta(meta, nmeta);
1278 return err;