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/uio.h>
20 #include <sys/stat.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <limits.h>
27 #include <zlib.h>
29 #include "got_error.h"
30 #include "got_cancel.h"
31 #include "got_object.h"
32 #include "got_reference.h"
33 #include "got_repository_admin.h"
35 #include "got_lib_deltify.h"
36 #include "got_lib_delta.h"
37 #include "got_lib_object.h"
38 #include "got_lib_object_idset.h"
39 #include "got_lib_object_cache.h"
40 #include "got_lib_deflate.h"
41 #include "got_lib_pack.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_repository.h"
45 #ifndef MAX
46 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
47 #endif
49 struct got_pack_meta {
50 struct got_object_id id;
51 char *path;
52 int obj_type;
53 time_t mtime;
55 /* The best delta we picked */
56 struct got_pack_meta *head;
57 struct got_pack_meta *prev;
58 struct got_delta_instruction *deltas;
59 int ndeltas;
60 int nchain;
62 /* Only used for delta window */
63 struct got_delta_table *dtab;
65 /* Only used for writing offset deltas */
66 off_t off;
67 };
69 struct got_pack_metavec {
70 struct got_pack_meta **meta;
71 int nmeta;
72 int metasz;
73 };
75 static const struct got_error *
76 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
77 const char *path, int obj_type, time_t mtime)
78 {
79 const struct got_error *err = NULL;
80 struct got_pack_meta *m;
82 *new = NULL;
84 m = calloc(1, sizeof(*m));
85 if (m == NULL)
86 return got_error_from_errno("malloc");
88 memcpy(&m->id, id, sizeof(m->id));
90 m->path = strdup(path);
91 if (m->path == NULL) {
92 err = got_error_from_errno("strdup");
93 free(m);
94 return err;
95 }
97 m->obj_type = obj_type;
98 m->mtime = mtime;
99 *new = m;
100 return NULL;
103 static void
104 clear_meta(struct got_pack_meta *meta)
106 if (meta == NULL)
107 return;
108 free(meta->deltas);
109 meta->deltas = NULL;
110 free(meta->path);
111 meta->path = NULL;
114 static void
115 free_nmeta(struct got_pack_meta **meta, int nmeta)
117 int i;
119 for (i = 0; i < nmeta; i++)
120 clear_meta(meta[i]);
121 free(meta);
124 static int
125 delta_order_cmp(const void *pa, const void *pb)
127 struct got_pack_meta *a, *b;
128 int cmp;
130 a = *(struct got_pack_meta **)pa;
131 b = *(struct got_pack_meta **)pb;
133 if (a->obj_type != b->obj_type)
134 return a->obj_type - b->obj_type;
135 cmp = strcmp(a->path, b->path);
136 if (cmp != 0)
137 return cmp;
138 if (a->mtime != b->mtime)
139 return a->mtime - b->mtime;
140 return got_object_id_cmp(&a->id, &b->id);
143 static int
144 delta_size(struct got_delta_instruction *deltas, int ndeltas)
146 int i, size = 32;
147 for (i = 0; i < ndeltas; i++) {
148 if (deltas[i].copy)
149 size += GOT_DELTA_SIZE_SHIFT;
150 else
151 size += deltas[i].len + 1;
153 return size;
157 static const struct got_error *
158 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
159 struct got_repository *repo,
160 got_pack_progress_cb progress_cb, void *progress_arg,
161 got_cancel_cb cancel_cb, void *cancel_arg)
163 const struct got_error *err = NULL;
164 struct got_pack_meta *m = NULL, *base = NULL;
165 struct got_raw_object *raw = NULL, *base_raw = NULL;
166 struct got_delta_instruction *deltas;
167 int i, j, size, ndeltas, best;
168 const int max_base_candidates = 10;
170 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
171 for (i = 0; i < nmeta; i++) {
172 if (cancel_cb) {
173 err = (*cancel_cb)(cancel_arg);
174 if (err)
175 break;
177 if (progress_cb) {
178 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
179 if (err)
180 goto done;
182 m = meta[i];
183 m->deltas = NULL;
184 m->ndeltas = 0;
186 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
187 m->obj_type == GOT_OBJ_TYPE_TAG)
188 continue;
190 err = got_object_raw_open(&raw, repo, &m->id, 8192);
191 if (err)
192 goto done;
194 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
195 raw->size + raw->hdrlen);
196 if (err)
197 goto done;
199 if (i > max_base_candidates) {
200 struct got_pack_meta *n = NULL;
201 n = meta[i - (max_base_candidates + 1)];
202 got_deltify_free(n->dtab);
203 n->dtab = NULL;
206 best = raw->size;
207 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
208 if (cancel_cb) {
209 err = (*cancel_cb)(cancel_arg);
210 if (err)
211 goto done;
213 base = meta[j];
214 /* long chains make unpacking slow, avoid such bases */
215 if (base->nchain >= 128 ||
216 base->obj_type != m->obj_type)
217 continue;
219 err = got_object_raw_open(&base_raw, repo, &base->id,
220 8192);
221 if (err)
222 goto done;
223 err = got_deltify(&deltas, &ndeltas,
224 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
225 base->dtab, base_raw->f, base_raw->hdrlen,
226 base_raw->size + base_raw->hdrlen);
227 got_object_raw_close(base_raw);
228 base_raw = NULL;
229 if (err)
230 goto done;
232 size = delta_size(deltas, ndeltas);
233 if (size + 32 < best){
234 /*
235 * if we already picked a best delta,
236 * replace it.
237 */
238 free(m->deltas);
239 best = size;
240 m->deltas = deltas;
241 m->ndeltas = ndeltas;
242 m->nchain = base->nchain + 1;
243 m->prev = base;
244 m->head = base->head;
245 if (m->head == NULL)
246 m->head = base;
247 } else {
248 free(deltas);
249 deltas = NULL;
250 ndeltas = 0;
254 got_object_raw_close(raw);
255 raw = NULL;
257 done:
258 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
259 got_deltify_free(meta[i]->dtab);
260 meta[i]->dtab = NULL;
262 if (raw)
263 got_object_raw_close(raw);
264 if (base_raw)
265 got_object_raw_close(base_raw);
266 return err;
269 static const struct got_error *
270 search_packidx(int *found, struct got_object_id *id,
271 struct got_repository *repo)
273 const struct got_error *err = NULL;
274 struct got_packidx *packidx = NULL;
275 int idx;
277 *found = 0;
279 err = got_repo_search_packidx(&packidx, &idx, repo, id);
280 if (err == NULL)
281 *found = 1; /* object is already packed */
282 else if (err->code == GOT_ERR_NO_OBJ)
283 err = NULL;
284 return err;
287 static const int obj_types[] = {
288 GOT_OBJ_TYPE_ANY,
289 GOT_OBJ_TYPE_COMMIT,
290 GOT_OBJ_TYPE_TREE,
291 GOT_OBJ_TYPE_BLOB,
292 GOT_OBJ_TYPE_TAG,
293 GOT_OBJ_TYPE_OFFSET_DELTA,
294 GOT_OBJ_TYPE_REF_DELTA
295 };
297 static const struct got_error *
298 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
299 struct got_object_id *id, const char *path, int obj_type,
300 time_t mtime, int loose_obj_only, struct got_repository *repo)
302 const struct got_error *err;
303 struct got_pack_meta *m;
305 if (loose_obj_only) {
306 int is_packed;
307 err = search_packidx(&is_packed, id, repo);
308 if (err)
309 return err;
310 if (is_packed)
311 return NULL;
314 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
315 if (err)
316 return err;
318 if (v == NULL)
319 return NULL;
321 err = alloc_meta(&m, id, path, obj_type, mtime);
322 if (err)
323 goto done;
325 if (v->nmeta == v->metasz){
326 size_t newsize = 2 * v->metasz;
327 struct got_pack_meta **new;
328 new = reallocarray(v->meta, newsize, sizeof(*new));
329 if (new == NULL) {
330 err = got_error_from_errno("reallocarray");
331 goto done;
333 v->meta = new;
334 v->metasz = newsize;
336 done:
337 if (err) {
338 clear_meta(m);
339 free(m);
340 } else
341 v->meta[v->nmeta++] = m;
343 return err;
346 static const struct got_error *
347 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
348 struct got_object_idset *idset, struct got_object_id *tree_id,
349 const char *dpath, time_t mtime, struct got_repository *repo,
350 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
352 const struct got_error *err;
353 struct got_tree_object *tree;
354 char *p = NULL;
355 int i;
357 err = got_object_open_as_tree(&tree, repo, tree_id);
358 if (err)
359 return err;
361 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
362 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
363 struct got_object_id *id = got_tree_entry_get_id(e);
364 mode_t mode = got_tree_entry_get_mode(e);
366 if (cancel_cb) {
367 err = (*cancel_cb)(cancel_arg);
368 if (err)
369 break;
372 if (got_object_tree_entry_is_submodule(e) ||
373 got_object_idset_contains(idset, id))
374 continue;
376 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
377 got_tree_entry_get_name(e)) == -1) {
378 err = got_error_from_errno("asprintf");
379 break;
382 if (S_ISDIR(mode)) {
383 struct got_object_qid *qid;
384 err = got_object_qid_alloc(&qid, id);
385 if (err)
386 break;
387 STAILQ_INSERT_TAIL(ids, qid, entry);
388 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
389 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
390 mtime, loose_obj_only, repo);
391 if (err)
392 break;
394 free(p);
395 p = NULL;
398 got_object_tree_close(tree);
399 free(p);
400 return err;
403 static const struct got_error *
404 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
405 struct got_object_id *tree_id, const char *dpath, time_t mtime,
406 int loose_obj_only, struct got_repository *repo,
407 got_cancel_cb cancel_cb, void *cancel_arg)
409 const struct got_error *err = NULL;
410 struct got_object_id_queue tree_ids;
411 struct got_object_qid *qid;
413 if (got_object_idset_contains(idset, tree_id))
414 return NULL;
416 err = got_object_qid_alloc(&qid, tree_id);
417 if (err)
418 return err;
420 STAILQ_INIT(&tree_ids);
421 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
423 while (!STAILQ_EMPTY(&tree_ids)) {
424 if (cancel_cb) {
425 err = (*cancel_cb)(cancel_arg);
426 if (err)
427 break;
430 qid = STAILQ_FIRST(&tree_ids);
431 STAILQ_REMOVE_HEAD(&tree_ids, entry);
433 if (got_object_idset_contains(idset, qid->id)) {
434 got_object_qid_free(qid);
435 continue;
438 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
439 mtime, loose_obj_only, repo);
440 if (err) {
441 got_object_qid_free(qid);
442 break;
445 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
446 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
447 got_object_qid_free(qid);
448 if (err)
449 break;
452 got_object_id_queue_free(&tree_ids);
453 return err;
456 static const struct got_error *
457 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
458 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
459 got_cancel_cb cancel_cb, void *cancel_arg)
461 const struct got_error *err;
462 struct got_commit_object *commit;
464 if (got_object_idset_contains(idset, id))
465 return NULL;
467 if (loose_obj_only) {
468 int is_packed;
469 err = search_packidx(&is_packed, id, repo);
470 if (err)
471 return err;
472 if (is_packed)
473 return NULL;
476 err = got_object_open_as_commit(&commit, repo, id);
477 if (err)
478 return err;
480 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
481 got_object_commit_get_committer_time(commit),
482 loose_obj_only, repo);
483 if (err)
484 goto done;
486 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
487 "", got_object_commit_get_committer_time(commit),
488 loose_obj_only, repo, cancel_cb, cancel_arg);
489 done:
490 got_object_commit_close(commit);
491 return err;
494 static const struct got_error *
495 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
496 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
497 got_cancel_cb cancel_cb, void *cancel_arg)
499 const struct got_error *err;
500 struct got_tag_object *tag = NULL;
502 if (got_object_idset_contains(idset, id))
503 return NULL;
505 if (loose_obj_only) {
506 int is_packed;
507 err = search_packidx(&is_packed, id, repo);
508 if (err)
509 return err;
510 if (is_packed)
511 return NULL;
514 err = got_object_open_as_tag(&tag, repo, id);
515 if (err)
516 return err;
518 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
519 got_object_tag_get_tagger_time(tag),
520 loose_obj_only, repo);
521 if (err)
522 goto done;
524 switch (got_object_tag_get_object_type(tag)) {
525 case GOT_OBJ_TYPE_COMMIT:
526 err = load_commit(v, idset,
527 got_object_tag_get_object_id(tag), repo,
528 loose_obj_only, cancel_cb, cancel_arg);
529 break;
530 case GOT_OBJ_TYPE_TREE:
531 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
532 "", got_object_tag_get_tagger_time(tag),
533 loose_obj_only, repo, cancel_cb, cancel_arg);
534 break;
535 default:
536 break;
539 done:
540 got_object_tag_close(tag);
541 return err;
544 enum findtwixt_color {
545 COLOR_KEEP = 0,
546 COLOR_DROP,
547 COLOR_BLANK,
548 };
549 static const int findtwixt_colors[] = {
550 COLOR_KEEP,
551 COLOR_DROP,
552 COLOR_BLANK
553 };
555 static const struct got_error *
556 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
557 int color, struct got_repository *repo)
559 const struct got_error *err;
560 struct got_object_qid *qid;
562 err = got_object_qid_alloc(&qid, id);
563 if (err)
564 return err;
566 STAILQ_INSERT_TAIL(ids, qid, entry);
567 qid->data = (void *)&findtwixt_colors[color];
568 return NULL;
571 static const struct got_error *
572 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
573 struct got_object_id *id, struct got_repository *repo,
574 got_cancel_cb cancel_cb, void *cancel_arg)
576 const struct got_error *err = NULL;
577 struct got_commit_object *commit;
578 const struct got_object_id_queue *parents;
579 struct got_object_id_queue ids;
580 struct got_object_qid *qid;
582 STAILQ_INIT(&ids);
584 err = got_object_qid_alloc(&qid, id);
585 if (err)
586 return err;
587 STAILQ_INSERT_HEAD(&ids, qid, entry);
589 while (!STAILQ_EMPTY(&ids)) {
590 if (cancel_cb) {
591 err = (*cancel_cb)(cancel_arg);
592 if (err)
593 break;
596 qid = STAILQ_FIRST(&ids);
597 STAILQ_REMOVE_HEAD(&ids, entry);
599 if (got_object_idset_contains(drop, qid->id)) {
600 got_object_qid_free(qid);
601 continue;
604 err = got_object_idset_add(drop, qid->id,
605 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
606 if (err) {
607 got_object_qid_free(qid);
608 break;
611 if (!got_object_idset_contains(keep, qid->id)) {
612 got_object_qid_free(qid);
613 continue;
616 err = got_object_open_as_commit(&commit, repo, qid->id);
617 got_object_qid_free(qid);
618 if (err)
619 break;
621 parents = got_object_commit_get_parent_ids(commit);
622 if (parents) {
623 err = got_object_id_queue_copy(parents, &ids);
624 if (err) {
625 got_object_commit_close(commit);
626 break;
629 got_object_commit_close(commit);
632 got_object_id_queue_free(&ids);
633 return err;
636 struct append_id_arg {
637 struct got_object_id **array;
638 int idx;
639 };
641 static const struct got_error *
642 append_id(struct got_object_id *id, void *data, void *arg)
644 struct append_id_arg *a = arg;
646 a->array[a->idx] = got_object_id_dup(id);
647 if (a->array[a->idx] == NULL)
648 return got_error_from_errno("got_object_id_dup");
650 a->idx++;
651 return NULL;
654 static const struct got_error *
655 findtwixt(struct got_object_id ***res, int *nres,
656 struct got_object_id **head, int nhead,
657 struct got_object_id **tail, int ntail,
658 struct got_repository *repo,
659 got_cancel_cb cancel_cb, void *cancel_arg)
661 const struct got_error *err = NULL;
662 struct got_object_id_queue ids;
663 struct got_object_idset *keep, *drop;
664 struct got_object_qid *qid;
665 int i, ncolor, nkeep, obj_type;
667 STAILQ_INIT(&ids);
668 *res = NULL;
669 *nres = 0;
671 keep = got_object_idset_alloc();
672 if (keep == NULL)
673 return got_error_from_errno("got_object_idset_alloc");
675 drop = got_object_idset_alloc();
676 if (drop == NULL) {
677 err = got_error_from_errno("got_object_idset_alloc");
678 goto done;
681 for (i = 0; i < nhead; i++) {
682 struct got_object_id *id = head[i];
683 if (id == NULL)
684 continue;
685 err = got_object_get_type(&obj_type, repo, id);
686 if (err)
687 return err;
688 if (obj_type != GOT_OBJ_TYPE_COMMIT)
689 continue;
690 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
691 if (err)
692 goto done;
694 for (i = 0; i < ntail; i++) {
695 struct got_object_id *id = tail[i];
696 if (id == NULL)
697 continue;
698 err = got_object_get_type(&obj_type, repo, id);
699 if (err)
700 return err;
701 if (obj_type != GOT_OBJ_TYPE_COMMIT)
702 continue;
703 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
704 if (err)
705 goto done;
708 while (!STAILQ_EMPTY(&ids)) {
709 int qcolor;
710 qid = STAILQ_FIRST(&ids);
711 qcolor = *((int *)qid->data);
713 if (got_object_idset_contains(drop, qid->id))
714 ncolor = COLOR_DROP;
715 else if (got_object_idset_contains(keep, qid->id))
716 ncolor = COLOR_KEEP;
717 else
718 ncolor = COLOR_BLANK;
720 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
721 qcolor == COLOR_KEEP)) {
722 STAILQ_REMOVE_HEAD(&ids, entry);
723 got_object_qid_free(qid);
724 continue;
727 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
728 err = drop_commit(keep, drop, qid->id, repo,
729 cancel_cb, cancel_arg);
730 if (err)
731 goto done;
732 } else if (ncolor == COLOR_BLANK) {
733 struct got_commit_object *commit;
734 struct got_object_id *id;
735 const struct got_object_id_queue *parents;
736 struct got_object_qid *pid;
738 id = got_object_id_dup(qid->id);
739 if (id == NULL) {
740 err = got_error_from_errno("got_object_id_dup");
741 goto done;
743 if (qcolor == COLOR_KEEP)
744 err = got_object_idset_add(keep, id,
745 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
746 else
747 err = got_object_idset_add(drop, id,
748 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
749 if (err) {
750 free(id);
751 goto done;
754 err = got_object_open_as_commit(&commit, repo, id);
755 if (err) {
756 free(id);
757 goto done;
759 parents = got_object_commit_get_parent_ids(commit);
760 if (parents) {
761 STAILQ_FOREACH(pid, parents, entry) {
762 err = queue_commit_id(&ids, pid->id,
763 qcolor, repo);
764 if (err) {
765 free(id);
766 goto done;
770 got_object_commit_close(commit);
771 commit = NULL;
772 } else {
773 /* should not happen */
774 err = got_error_fmt(GOT_ERR_NOT_IMPL,
775 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
776 goto done;
779 STAILQ_REMOVE_HEAD(&ids, entry);
780 got_object_qid_free(qid);
783 nkeep = got_object_idset_num_elements(keep);
784 if (nkeep > 0) {
785 struct append_id_arg arg;
786 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
787 if (arg.array == NULL) {
788 err = got_error_from_errno("calloc");
789 goto done;
791 arg.idx = 0;
792 err = got_object_idset_for_each(keep, append_id, &arg);
793 if (err) {
794 free(arg.array);
795 goto done;
797 *res = arg.array;
798 *nres = nkeep;
800 done:
801 got_object_idset_free(keep);
802 got_object_idset_free(drop);
803 got_object_id_queue_free(&ids);
804 return err;
807 static const struct got_error *
808 read_meta(struct got_pack_meta ***meta, int *nmeta,
809 struct got_object_id **theirs, int ntheirs,
810 struct got_object_id **ours, int nours, struct got_repository *repo,
811 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
812 got_cancel_cb cancel_cb, void *cancel_arg)
814 const struct got_error *err = NULL;
815 struct got_object_id **ids = NULL;
816 struct got_object_idset *idset;
817 int i, nobj = 0, obj_type;
818 struct got_pack_metavec v;
820 *meta = NULL;
821 *nmeta = 0;
823 idset = got_object_idset_alloc();
824 if (idset == NULL)
825 return got_error_from_errno("got_object_idset_alloc");
827 v.nmeta = 0;
828 v.metasz = 64;
829 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
830 if (v.meta == NULL) {
831 err = got_error_from_errno("reallocarray");
832 goto done;
835 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
836 cancel_cb, cancel_arg);
837 if (err || nobj == 0)
838 goto done;
840 for (i = 0; i < ntheirs; i++) {
841 struct got_object_id *id = theirs[i];
842 if (id == NULL)
843 continue;
844 err = got_object_get_type(&obj_type, repo, id);
845 if (err)
846 return err;
847 if (obj_type != GOT_OBJ_TYPE_COMMIT)
848 continue;
849 err = load_commit(NULL, idset, id, repo,
850 loose_obj_only, cancel_cb, cancel_arg);
851 if (err)
852 goto done;
853 if (progress_cb) {
854 err = progress_cb(progress_arg, 0L, nours,
855 v.nmeta, 0, 0);
856 if (err)
857 goto done;
861 for (i = 0; i < ntheirs; i++) {
862 struct got_object_id *id = theirs[i];
863 int *cached_type;
864 if (id == NULL)
865 continue;
866 cached_type = got_object_idset_get(idset, id);
867 if (cached_type == NULL) {
868 err = got_object_get_type(&obj_type, repo, id);
869 if (err)
870 goto done;
871 } else
872 obj_type = *cached_type;
873 if (obj_type != GOT_OBJ_TYPE_TAG)
874 continue;
875 err = load_tag(NULL, idset, id, repo,
876 loose_obj_only, cancel_cb, cancel_arg);
877 if (err)
878 goto done;
879 if (progress_cb) {
880 err = progress_cb(progress_arg, 0L, nours,
881 v.nmeta, 0, 0);
882 if (err)
883 goto done;
887 for (i = 0; i < nobj; i++) {
888 err = load_commit(&v, idset, ids[i], repo,
889 loose_obj_only, cancel_cb, cancel_arg);
890 if (err)
891 goto done;
892 if (progress_cb) {
893 err = progress_cb(progress_arg, 0L, nours,
894 v.nmeta, 0, 0);
895 if (err)
896 goto done;
900 for (i = 0; i < nours; i++) {
901 struct got_object_id *id = ours[i];
902 int *cached_type;
903 if (id == NULL)
904 continue;
905 cached_type = got_object_idset_get(idset, id);
906 if (cached_type == NULL) {
907 err = got_object_get_type(&obj_type, repo, id);
908 if (err)
909 goto done;
910 } else
911 obj_type = *cached_type;
912 if (obj_type != GOT_OBJ_TYPE_TAG)
913 continue;
914 err = load_tag(&v, idset, id, repo,
915 loose_obj_only, cancel_cb, cancel_arg);
916 if (err)
917 goto done;
918 if (progress_cb) {
919 err = progress_cb(progress_arg, 0L, nours,
920 v.nmeta, 0, 0);
921 if (err)
922 goto done;
926 done:
927 for (i = 0; i < nobj; i++) {
928 free(ids[i]);
930 free(ids);
931 got_object_idset_free(idset);
932 if (err == NULL) {
933 *meta = v.meta;
934 *nmeta = v.nmeta;
935 } else
936 free(v.meta);
938 return err;
941 const struct got_error *
942 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
944 size_t n;
946 SHA1Update(ctx, buf, len);
947 n = fwrite(buf, 1, len, f);
948 if (n != len)
949 return got_ferror(f, GOT_ERR_IO);
950 return NULL;
953 static void
954 putbe32(char *b, uint32_t n)
956 b[0] = n >> 24;
957 b[1] = n >> 16;
958 b[2] = n >> 8;
959 b[3] = n >> 0;
962 static int
963 write_order_cmp(const void *pa, const void *pb)
965 struct got_pack_meta *a, *b, *ahd, *bhd;
967 a = *(struct got_pack_meta **)pa;
968 b = *(struct got_pack_meta **)pb;
969 ahd = (a->head == NULL) ? a : a->head;
970 bhd = (b->head == NULL) ? b : b->head;
971 if (ahd->mtime != bhd->mtime)
972 return bhd->mtime - ahd->mtime;
973 if (ahd != bhd)
974 return (uintptr_t)bhd - (uintptr_t)ahd;
975 if (a->nchain != b->nchain)
976 return a->nchain - b->nchain;
977 return a->mtime - b->mtime;
980 static const struct got_error *
981 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
983 size_t i;
985 *hdrlen = 0;
987 hdr[0] = obj_type << 4;
988 hdr[0] |= len & 0xf;
989 len >>= 4;
990 for (i = 1; len != 0; i++){
991 if (i >= bufsize)
992 return got_error(GOT_ERR_NO_SPACE);
993 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
994 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
995 len >>= GOT_DELTA_SIZE_SHIFT;
998 *hdrlen = i;
999 return NULL;
1002 static const struct got_error *
1003 append(char **p, int *len, int *sz, void *seg, int nseg)
1005 char *n;
1007 if (*len + nseg >= *sz) {
1008 while (*len + nseg >= *sz)
1009 *sz += *sz / 2;
1010 n = realloc(*p, *sz);
1011 if (n == NULL)
1012 return got_error_from_errno("realloc");
1013 *p = n;
1015 memcpy(*p + *len, seg, nseg);
1016 *len += nseg;
1017 return NULL;
1021 static const struct got_error *
1022 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1023 off_t base_size, char **pp)
1025 const struct got_error *err = NULL;
1026 char *p;
1027 unsigned char buf[16], *bp;
1028 int len, sz, i, j;
1029 off_t n;
1030 struct got_delta_instruction *d;
1032 *pp = NULL;
1033 *nd = 0;
1035 sz = 128;
1036 len = 0;
1037 p = malloc(sz);
1038 if (p == NULL)
1039 return got_error_from_errno("malloc");
1041 /* base object size */
1042 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1043 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1044 for (i = 1; n > 0; i++) {
1045 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1046 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1047 n >>= GOT_DELTA_SIZE_SHIFT;
1049 err = append(&p, &len, &sz, buf, i);
1050 if (err)
1051 return err;
1053 /* target object size */
1054 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1055 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1056 for (i = 1; n > 0; i++) {
1057 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1058 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1059 n >>= GOT_DELTA_SIZE_SHIFT;
1061 err = append(&p, &len, &sz, buf, i);
1062 if (err)
1063 return err;
1064 for (j = 0; j < m->ndeltas; j++) {
1065 d = &m->deltas[j];
1066 if (d->copy) {
1067 n = d->offset;
1068 bp = &buf[1];
1069 buf[0] = GOT_DELTA_BASE_COPY;
1070 for (i = 0; i < 4; i++) {
1071 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1072 buf[0] |= 1 << i;
1073 *bp++ = n & 0xff;
1074 n >>= 8;
1075 if (n == 0)
1076 break;
1079 n = d->len;
1080 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1081 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1082 for (i = 0; i < 3 && n > 0; i++) {
1083 buf[0] |= 1 << (i + 4);
1084 *bp++ = n & 0xff;
1085 n >>= 8;
1088 err = append(&p, &len, &sz, buf, bp - buf);
1089 if (err)
1090 return err;
1091 } else {
1092 char content[128];
1093 size_t r;
1094 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1095 return got_error_from_errno("fseeko");
1096 n = 0;
1097 while (n != d->len) {
1098 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1099 err = append(&p, &len, &sz, buf, 1);
1100 if (err)
1101 return err;
1102 r = fread(content, 1, buf[0], o->f);
1103 if (r != buf[0])
1104 return got_ferror(o->f, GOT_ERR_IO);
1105 err = append(&p, &len, &sz, content, buf[0]);
1106 if (err)
1107 return err;
1108 n += buf[0];
1112 *pp = p;
1113 *nd = len;
1114 return NULL;
1117 static int
1118 packoff(char *hdr, off_t off)
1120 int i, j;
1121 char rbuf[8];
1123 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1124 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1125 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1126 GOT_DELTA_SIZE_MORE;
1129 j = 0;
1130 while (i > 0)
1131 hdr[j++] = rbuf[--i];
1132 return j;
1135 static const struct got_error *
1136 genpack(uint8_t *pack_sha1, FILE *packfile,
1137 struct got_pack_meta **meta, int nmeta, int nours,
1138 int use_offset_deltas, struct got_repository *repo,
1139 got_pack_progress_cb progress_cb, void *progress_arg,
1140 got_cancel_cb cancel_cb, void *cancel_arg)
1142 const struct got_error *err = NULL;
1143 int i, nh, nd;
1144 SHA1_CTX ctx;
1145 struct got_pack_meta *m;
1146 struct got_raw_object *raw;
1147 char *p = NULL, buf[32];
1148 size_t outlen, n;
1149 struct got_deflate_checksum csum;
1150 off_t packfile_size = 0;
1152 SHA1Init(&ctx);
1153 csum.output_sha1 = &ctx;
1154 csum.output_crc = NULL;
1156 err = hwrite(packfile, "PACK", 4, &ctx);
1157 if (err)
1158 return err;
1159 putbe32(buf, GOT_PACKFILE_VERSION);
1160 err = hwrite(packfile, buf, 4, &ctx);
1161 if (err)
1162 goto done;
1163 putbe32(buf, nmeta);
1164 err = hwrite(packfile, buf, 4, &ctx);
1165 if (err)
1166 goto done;
1167 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1168 for (i = 0; i < nmeta; i++) {
1169 if (progress_cb) {
1170 err = progress_cb(progress_arg, packfile_size, nours,
1171 nmeta, nmeta, i);
1172 if (err)
1173 goto done;
1175 m = meta[i];
1176 m->off = ftello(packfile);
1177 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1178 if (err)
1179 goto done;
1180 if (m->deltas == NULL) {
1181 err = packhdr(&nh, buf, sizeof(buf),
1182 m->obj_type, raw->size);
1183 if (err)
1184 goto done;
1185 err = hwrite(packfile, buf, nh, &ctx);
1186 if (err)
1187 goto done;
1188 packfile_size += nh;
1189 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1190 err = got_error_from_errno("fseeko");
1191 goto done;
1193 err = got_deflate_to_file(&outlen, raw->f, packfile,
1194 &csum);
1195 if (err)
1196 goto done;
1197 packfile_size += outlen;
1198 } else {
1199 FILE *delta_file;
1200 struct got_raw_object *base_raw;
1201 err = got_object_raw_open(&base_raw, repo,
1202 &m->prev->id, 8192);
1203 if (err)
1204 goto done;
1205 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1206 if (err)
1207 goto done;
1208 got_object_raw_close(base_raw);
1209 if (use_offset_deltas && m->prev->off != 0) {
1210 err = packhdr(&nh, buf, sizeof(buf),
1211 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1212 if (err)
1213 goto done;
1214 nh += packoff(buf + nh,
1215 m->off - m->prev->off);
1216 err = hwrite(packfile, buf, nh, &ctx);
1217 if (err)
1218 goto done;
1219 packfile_size += nh;
1220 } else {
1221 err = packhdr(&nh, buf, sizeof(buf),
1222 GOT_OBJ_TYPE_REF_DELTA, nd);
1223 err = hwrite(packfile, buf, nh, &ctx);
1224 if (err)
1225 goto done;
1226 packfile_size += nh;
1227 err = hwrite(packfile, m->prev->id.sha1,
1228 sizeof(m->prev->id.sha1), &ctx);
1229 packfile_size += sizeof(m->prev->id.sha1);
1230 if (err)
1231 goto done;
1233 /* XXX need got_deflate_from_mem() */
1234 delta_file = fmemopen(p, nd, "r");
1235 if (delta_file == NULL) {
1236 err = got_error_from_errno("fmemopen");
1237 goto done;
1239 err = got_deflate_to_file(&outlen, delta_file,
1240 packfile, &csum);
1241 fclose(delta_file);
1242 if (err)
1243 goto done;
1244 packfile_size += outlen;
1245 free(p);
1246 p = NULL;
1248 got_object_raw_close(raw);
1249 raw = NULL;
1251 SHA1Final(pack_sha1, &ctx);
1252 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1253 if (n != SHA1_DIGEST_LENGTH)
1254 err = got_ferror(packfile, GOT_ERR_IO);
1255 packfile_size += SHA1_DIGEST_LENGTH;
1256 packfile_size += sizeof(struct got_packfile_hdr);
1257 err = progress_cb(progress_arg, packfile_size, nours,
1258 nmeta, nmeta, nmeta);
1259 if (err)
1260 goto done;
1261 done:
1262 free(p);
1263 return err;
1266 const struct got_error *
1267 got_pack_create(uint8_t *packsha1, FILE *packfile,
1268 struct got_object_id **theirs, int ntheirs,
1269 struct got_object_id **ours, int nours,
1270 struct got_repository *repo, int loose_obj_only, int allow_empty,
1271 got_pack_progress_cb progress_cb, void *progress_arg,
1272 got_cancel_cb cancel_cb, void *cancel_arg)
1274 const struct got_error *err;
1275 struct got_pack_meta **meta;
1276 int nmeta;
1278 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1279 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1280 if (err)
1281 return err;
1283 if (nmeta == 0 && !allow_empty) {
1284 err = got_error(GOT_ERR_CANNOT_PACK);
1285 goto done;
1287 if (nmeta > 0) {
1288 err = pick_deltas(meta, nmeta, nours, repo,
1289 progress_cb, progress_arg, cancel_cb, cancel_arg);
1290 if (err)
1291 goto done;
1294 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1295 progress_cb, progress_arg, cancel_cb, cancel_arg);
1296 if (err)
1297 goto done;
1298 done:
1299 free_nmeta(meta, nmeta);
1300 return err;