Blob


1 /*
2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/stat.h>
24 #include <stdint.h>
25 #include <imsg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sha1.h>
30 #include <limits.h>
31 #include <zlib.h>
33 #include "got_error.h"
34 #include "got_cancel.h"
35 #include "got_object.h"
36 #include "got_path.h"
37 #include "got_reference.h"
38 #include "got_repository_admin.h"
40 #include "got_lib_deltify.h"
41 #include "got_lib_delta.h"
42 #include "got_lib_object.h"
43 #include "got_lib_object_idset.h"
44 #include "got_lib_object_cache.h"
45 #include "got_lib_deflate.h"
46 #include "got_lib_pack.h"
47 #include "got_lib_privsep.h"
48 #include "got_lib_repository.h"
50 #ifndef MAX
51 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
52 #endif
54 struct got_pack_meta {
55 struct got_object_id id;
56 char *path;
57 int obj_type;
58 time_t mtime;
60 /* The best delta we picked */
61 struct got_pack_meta *head;
62 struct got_pack_meta *prev;
63 struct got_delta_instruction *deltas;
64 int ndeltas;
65 int nchain;
67 /* Only used for delta window */
68 struct got_delta_table *dtab;
70 /* Only used for writing offset deltas */
71 off_t off;
72 };
74 struct got_pack_metavec {
75 struct got_pack_meta **meta;
76 int nmeta;
77 int metasz;
78 };
80 static const struct got_error *
81 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
82 const char *path, int obj_type, time_t mtime)
83 {
84 const struct got_error *err = NULL;
85 struct got_pack_meta *m;
87 *new = NULL;
89 m = calloc(1, sizeof(*m));
90 if (m == NULL)
91 return got_error_from_errno("calloc");
93 memcpy(&m->id, id, sizeof(m->id));
95 m->path = strdup(path);
96 if (m->path == NULL) {
97 err = got_error_from_errno("strdup");
98 free(m);
99 return err;
102 m->obj_type = obj_type;
103 m->mtime = mtime;
104 *new = m;
105 return NULL;
108 static void
109 clear_meta(struct got_pack_meta *meta)
111 if (meta == NULL)
112 return;
113 free(meta->deltas);
114 meta->deltas = NULL;
115 free(meta->path);
116 meta->path = NULL;
119 static void
120 free_nmeta(struct got_pack_meta **meta, int nmeta)
122 int i;
124 for (i = 0; i < nmeta; i++)
125 clear_meta(meta[i]);
126 free(meta);
129 static int
130 delta_order_cmp(const void *pa, const void *pb)
132 struct got_pack_meta *a, *b;
133 int cmp;
135 a = *(struct got_pack_meta **)pa;
136 b = *(struct got_pack_meta **)pb;
138 if (a->obj_type != b->obj_type)
139 return a->obj_type - b->obj_type;
140 cmp = strcmp(a->path, b->path);
141 if (cmp != 0)
142 return cmp;
143 if (a->mtime != b->mtime)
144 return a->mtime - b->mtime;
145 return got_object_id_cmp(&a->id, &b->id);
148 static int
149 delta_size(struct got_delta_instruction *deltas, int ndeltas)
151 int i, size = 32;
152 for (i = 0; i < ndeltas; i++) {
153 if (deltas[i].copy)
154 size += GOT_DELTA_SIZE_SHIFT;
155 else
156 size += deltas[i].len + 1;
158 return size;
162 static const struct got_error *
163 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
164 struct got_repository *repo,
165 got_pack_progress_cb progress_cb, void *progress_arg,
166 got_cancel_cb cancel_cb, void *cancel_arg)
168 const struct got_error *err = NULL;
169 struct got_pack_meta *m = NULL, *base = NULL;
170 struct got_raw_object *raw = NULL, *base_raw = NULL;
171 struct got_delta_instruction *deltas;
172 int i, j, size, ndeltas, best;
173 const int max_base_candidates = 10;
175 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
176 for (i = 0; i < nmeta; i++) {
177 if (cancel_cb) {
178 err = (*cancel_cb)(cancel_arg);
179 if (err)
180 break;
182 if (progress_cb) {
183 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
184 if (err)
185 goto done;
187 m = meta[i];
188 m->deltas = NULL;
189 m->ndeltas = 0;
191 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
192 m->obj_type == GOT_OBJ_TYPE_TAG)
193 continue;
195 err = got_object_raw_open(&raw, repo, &m->id, 8192);
196 if (err)
197 goto done;
199 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
200 raw->size + raw->hdrlen);
201 if (err)
202 goto done;
204 if (i > max_base_candidates) {
205 struct got_pack_meta *n = NULL;
206 n = meta[i - (max_base_candidates + 1)];
207 got_deltify_free(n->dtab);
208 n->dtab = NULL;
211 best = raw->size;
212 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
213 if (cancel_cb) {
214 err = (*cancel_cb)(cancel_arg);
215 if (err)
216 goto done;
218 base = meta[j];
219 /* long chains make unpacking slow, avoid such bases */
220 if (base->nchain >= 128 ||
221 base->obj_type != m->obj_type)
222 continue;
224 err = got_object_raw_open(&base_raw, repo, &base->id,
225 8192);
226 if (err)
227 goto done;
228 err = got_deltify(&deltas, &ndeltas,
229 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
230 base->dtab, base_raw->f, base_raw->hdrlen,
231 base_raw->size + base_raw->hdrlen);
232 got_object_raw_close(base_raw);
233 base_raw = NULL;
234 if (err)
235 goto done;
237 size = delta_size(deltas, ndeltas);
238 if (size + 32 < best){
239 /*
240 * if we already picked a best delta,
241 * replace it.
242 */
243 free(m->deltas);
244 best = size;
245 m->deltas = deltas;
246 m->ndeltas = ndeltas;
247 m->nchain = base->nchain + 1;
248 m->prev = base;
249 m->head = base->head;
250 if (m->head == NULL)
251 m->head = base;
252 } else {
253 free(deltas);
254 deltas = NULL;
255 ndeltas = 0;
259 got_object_raw_close(raw);
260 raw = NULL;
262 done:
263 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
264 got_deltify_free(meta[i]->dtab);
265 meta[i]->dtab = NULL;
267 if (raw)
268 got_object_raw_close(raw);
269 if (base_raw)
270 got_object_raw_close(base_raw);
271 return err;
274 static const struct got_error *
275 search_packidx(int *found, struct got_object_id *id,
276 struct got_repository *repo)
278 const struct got_error *err = NULL;
279 struct got_packidx *packidx = NULL;
280 int idx;
282 *found = 0;
284 err = got_repo_search_packidx(&packidx, &idx, repo, id);
285 if (err == NULL)
286 *found = 1; /* object is already packed */
287 else if (err->code == GOT_ERR_NO_OBJ)
288 err = NULL;
289 return err;
292 static const int obj_types[] = {
293 GOT_OBJ_TYPE_ANY,
294 GOT_OBJ_TYPE_COMMIT,
295 GOT_OBJ_TYPE_TREE,
296 GOT_OBJ_TYPE_BLOB,
297 GOT_OBJ_TYPE_TAG,
298 GOT_OBJ_TYPE_OFFSET_DELTA,
299 GOT_OBJ_TYPE_REF_DELTA
300 };
302 static const struct got_error *
303 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
304 struct got_object_id *id, const char *path, int obj_type,
305 time_t mtime, int loose_obj_only, struct got_repository *repo)
307 const struct got_error *err;
308 struct got_pack_meta *m;
310 if (loose_obj_only) {
311 int is_packed;
312 err = search_packidx(&is_packed, id, repo);
313 if (err)
314 return err;
315 if (is_packed)
316 return NULL;
319 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
320 if (err)
321 return err;
323 if (v == NULL)
324 return NULL;
326 err = alloc_meta(&m, id, path, obj_type, mtime);
327 if (err)
328 goto done;
330 if (v->nmeta == v->metasz){
331 size_t newsize = 2 * v->metasz;
332 struct got_pack_meta **new;
333 new = reallocarray(v->meta, newsize, sizeof(*new));
334 if (new == NULL) {
335 err = got_error_from_errno("reallocarray");
336 goto done;
338 v->meta = new;
339 v->metasz = newsize;
341 done:
342 if (err) {
343 clear_meta(m);
344 free(m);
345 } else
346 v->meta[v->nmeta++] = m;
348 return err;
351 static const struct got_error *
352 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
353 struct got_object_idset *idset, struct got_object_id *tree_id,
354 const char *dpath, time_t mtime, struct got_repository *repo,
355 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
357 const struct got_error *err;
358 struct got_tree_object *tree;
359 char *p = NULL;
360 int i;
362 err = got_object_open_as_tree(&tree, repo, tree_id);
363 if (err)
364 return err;
366 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
367 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
368 struct got_object_id *id = got_tree_entry_get_id(e);
369 mode_t mode = got_tree_entry_get_mode(e);
371 if (cancel_cb) {
372 err = (*cancel_cb)(cancel_arg);
373 if (err)
374 break;
377 if (got_object_tree_entry_is_submodule(e) ||
378 got_object_idset_contains(idset, id))
379 continue;
381 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
382 got_tree_entry_get_name(e)) == -1) {
383 err = got_error_from_errno("asprintf");
384 break;
387 if (S_ISDIR(mode)) {
388 struct got_object_qid *qid;
389 err = got_object_qid_alloc(&qid, id);
390 if (err)
391 break;
392 STAILQ_INSERT_TAIL(ids, qid, entry);
393 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
394 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
395 mtime, loose_obj_only, repo);
396 if (err)
397 break;
399 free(p);
400 p = NULL;
403 got_object_tree_close(tree);
404 free(p);
405 return err;
408 static const struct got_error *
409 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
410 struct got_object_id *tree_id, const char *dpath, time_t mtime,
411 int loose_obj_only, struct got_repository *repo,
412 got_cancel_cb cancel_cb, void *cancel_arg)
414 const struct got_error *err = NULL;
415 struct got_object_id_queue tree_ids;
416 struct got_object_qid *qid;
418 if (got_object_idset_contains(idset, tree_id))
419 return NULL;
421 err = got_object_qid_alloc(&qid, tree_id);
422 if (err)
423 return err;
425 STAILQ_INIT(&tree_ids);
426 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
428 while (!STAILQ_EMPTY(&tree_ids)) {
429 if (cancel_cb) {
430 err = (*cancel_cb)(cancel_arg);
431 if (err)
432 break;
435 qid = STAILQ_FIRST(&tree_ids);
436 STAILQ_REMOVE_HEAD(&tree_ids, entry);
438 if (got_object_idset_contains(idset, qid->id)) {
439 got_object_qid_free(qid);
440 continue;
443 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
444 mtime, loose_obj_only, repo);
445 if (err) {
446 got_object_qid_free(qid);
447 break;
450 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
451 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
452 got_object_qid_free(qid);
453 if (err)
454 break;
457 got_object_id_queue_free(&tree_ids);
458 return err;
461 static const struct got_error *
462 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
463 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
464 got_cancel_cb cancel_cb, void *cancel_arg)
466 const struct got_error *err;
467 struct got_commit_object *commit;
469 if (got_object_idset_contains(idset, id))
470 return NULL;
472 if (loose_obj_only) {
473 int is_packed;
474 err = search_packidx(&is_packed, id, repo);
475 if (err)
476 return err;
477 if (is_packed)
478 return NULL;
481 err = got_object_open_as_commit(&commit, repo, id);
482 if (err)
483 return err;
485 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
486 got_object_commit_get_committer_time(commit),
487 loose_obj_only, repo);
488 if (err)
489 goto done;
491 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
492 "", got_object_commit_get_committer_time(commit),
493 loose_obj_only, repo, cancel_cb, cancel_arg);
494 done:
495 got_object_commit_close(commit);
496 return err;
499 static const struct got_error *
500 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
501 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
502 got_cancel_cb cancel_cb, void *cancel_arg)
504 const struct got_error *err;
505 struct got_tag_object *tag = NULL;
507 if (got_object_idset_contains(idset, id))
508 return NULL;
510 if (loose_obj_only) {
511 int is_packed;
512 err = search_packidx(&is_packed, id, repo);
513 if (err)
514 return err;
515 if (is_packed)
516 return NULL;
519 err = got_object_open_as_tag(&tag, repo, id);
520 if (err)
521 return err;
523 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
524 got_object_tag_get_tagger_time(tag),
525 loose_obj_only, repo);
526 if (err)
527 goto done;
529 switch (got_object_tag_get_object_type(tag)) {
530 case GOT_OBJ_TYPE_COMMIT:
531 err = load_commit(v, idset,
532 got_object_tag_get_object_id(tag), repo,
533 loose_obj_only, cancel_cb, cancel_arg);
534 break;
535 case GOT_OBJ_TYPE_TREE:
536 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
537 "", got_object_tag_get_tagger_time(tag),
538 loose_obj_only, repo, cancel_cb, cancel_arg);
539 break;
540 default:
541 break;
544 done:
545 got_object_tag_close(tag);
546 return err;
549 enum findtwixt_color {
550 COLOR_KEEP = 0,
551 COLOR_DROP,
552 COLOR_BLANK,
553 };
554 static const int findtwixt_colors[] = {
555 COLOR_KEEP,
556 COLOR_DROP,
557 COLOR_BLANK
558 };
560 static const struct got_error *
561 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
562 int color, struct got_repository *repo)
564 const struct got_error *err;
565 struct got_object_qid *qid;
567 err = got_object_qid_alloc(&qid, id);
568 if (err)
569 return err;
571 STAILQ_INSERT_TAIL(ids, qid, entry);
572 qid->data = (void *)&findtwixt_colors[color];
573 return NULL;
576 static const struct got_error *
577 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
578 struct got_object_id *id, struct got_repository *repo,
579 got_cancel_cb cancel_cb, void *cancel_arg)
581 const struct got_error *err = NULL;
582 struct got_commit_object *commit;
583 const struct got_object_id_queue *parents;
584 struct got_object_id_queue ids;
585 struct got_object_qid *qid;
587 STAILQ_INIT(&ids);
589 err = got_object_qid_alloc(&qid, id);
590 if (err)
591 return err;
592 STAILQ_INSERT_HEAD(&ids, qid, entry);
594 while (!STAILQ_EMPTY(&ids)) {
595 if (cancel_cb) {
596 err = (*cancel_cb)(cancel_arg);
597 if (err)
598 break;
601 qid = STAILQ_FIRST(&ids);
602 STAILQ_REMOVE_HEAD(&ids, entry);
604 if (got_object_idset_contains(drop, qid->id)) {
605 got_object_qid_free(qid);
606 continue;
609 err = got_object_idset_add(drop, qid->id,
610 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
611 if (err) {
612 got_object_qid_free(qid);
613 break;
616 if (!got_object_idset_contains(keep, qid->id)) {
617 got_object_qid_free(qid);
618 continue;
621 err = got_object_open_as_commit(&commit, repo, qid->id);
622 got_object_qid_free(qid);
623 if (err)
624 break;
626 parents = got_object_commit_get_parent_ids(commit);
627 if (parents) {
628 err = got_object_id_queue_copy(parents, &ids);
629 if (err) {
630 got_object_commit_close(commit);
631 break;
634 got_object_commit_close(commit);
637 got_object_id_queue_free(&ids);
638 return err;
641 struct append_id_arg {
642 struct got_object_id **array;
643 int idx;
644 };
646 static const struct got_error *
647 append_id(struct got_object_id *id, void *data, void *arg)
649 struct append_id_arg *a = arg;
651 a->array[a->idx] = got_object_id_dup(id);
652 if (a->array[a->idx] == NULL)
653 return got_error_from_errno("got_object_id_dup");
655 a->idx++;
656 return NULL;
659 static const struct got_error *
660 findtwixt(struct got_object_id ***res, int *nres,
661 struct got_object_id **head, int nhead,
662 struct got_object_id **tail, int ntail,
663 struct got_repository *repo,
664 got_cancel_cb cancel_cb, void *cancel_arg)
666 const struct got_error *err = NULL;
667 struct got_object_id_queue ids;
668 struct got_object_idset *keep, *drop;
669 struct got_object_qid *qid;
670 int i, ncolor, nkeep, obj_type;
672 STAILQ_INIT(&ids);
673 *res = NULL;
674 *nres = 0;
676 keep = got_object_idset_alloc();
677 if (keep == NULL)
678 return got_error_from_errno("got_object_idset_alloc");
680 drop = got_object_idset_alloc();
681 if (drop == NULL) {
682 err = got_error_from_errno("got_object_idset_alloc");
683 goto done;
686 for (i = 0; i < nhead; i++) {
687 struct got_object_id *id = head[i];
688 if (id == NULL)
689 continue;
690 err = got_object_get_type(&obj_type, repo, id);
691 if (err)
692 return err;
693 if (obj_type != GOT_OBJ_TYPE_COMMIT)
694 continue;
695 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
696 if (err)
697 goto done;
699 for (i = 0; i < ntail; i++) {
700 struct got_object_id *id = tail[i];
701 if (id == NULL)
702 continue;
703 err = got_object_get_type(&obj_type, repo, id);
704 if (err)
705 return err;
706 if (obj_type != GOT_OBJ_TYPE_COMMIT)
707 continue;
708 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
709 if (err)
710 goto done;
713 while (!STAILQ_EMPTY(&ids)) {
714 int qcolor;
715 qid = STAILQ_FIRST(&ids);
716 qcolor = *((int *)qid->data);
718 if (got_object_idset_contains(drop, qid->id))
719 ncolor = COLOR_DROP;
720 else if (got_object_idset_contains(keep, qid->id))
721 ncolor = COLOR_KEEP;
722 else
723 ncolor = COLOR_BLANK;
725 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
726 qcolor == COLOR_KEEP)) {
727 STAILQ_REMOVE_HEAD(&ids, entry);
728 got_object_qid_free(qid);
729 continue;
732 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
733 err = drop_commit(keep, drop, qid->id, repo,
734 cancel_cb, cancel_arg);
735 if (err)
736 goto done;
737 } else if (ncolor == COLOR_BLANK) {
738 struct got_commit_object *commit;
739 struct got_object_id *id;
740 const struct got_object_id_queue *parents;
741 struct got_object_qid *pid;
743 id = got_object_id_dup(qid->id);
744 if (id == NULL) {
745 err = got_error_from_errno("got_object_id_dup");
746 goto done;
748 if (qcolor == COLOR_KEEP)
749 err = got_object_idset_add(keep, id,
750 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
751 else
752 err = got_object_idset_add(drop, id,
753 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
754 if (err) {
755 free(id);
756 goto done;
759 err = got_object_open_as_commit(&commit, repo, id);
760 if (err) {
761 free(id);
762 goto done;
764 parents = got_object_commit_get_parent_ids(commit);
765 if (parents) {
766 STAILQ_FOREACH(pid, parents, entry) {
767 err = queue_commit_id(&ids, pid->id,
768 qcolor, repo);
769 if (err) {
770 free(id);
771 goto done;
775 got_object_commit_close(commit);
776 commit = NULL;
777 } else {
778 /* should not happen */
779 err = got_error_fmt(GOT_ERR_NOT_IMPL,
780 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
781 goto done;
784 STAILQ_REMOVE_HEAD(&ids, entry);
785 got_object_qid_free(qid);
788 nkeep = got_object_idset_num_elements(keep);
789 if (nkeep > 0) {
790 struct append_id_arg arg;
791 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
792 if (arg.array == NULL) {
793 err = got_error_from_errno("calloc");
794 goto done;
796 arg.idx = 0;
797 err = got_object_idset_for_each(keep, append_id, &arg);
798 if (err) {
799 free(arg.array);
800 goto done;
802 *res = arg.array;
803 *nres = nkeep;
805 done:
806 got_object_idset_free(keep);
807 got_object_idset_free(drop);
808 got_object_id_queue_free(&ids);
809 return err;
812 static const struct got_error *
813 read_meta(struct got_pack_meta ***meta, int *nmeta,
814 struct got_object_id **theirs, int ntheirs,
815 struct got_object_id **ours, int nours, struct got_repository *repo,
816 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
817 got_cancel_cb cancel_cb, void *cancel_arg)
819 const struct got_error *err = NULL;
820 struct got_object_id **ids = NULL;
821 struct got_object_idset *idset;
822 int i, nobj = 0, obj_type;
823 struct got_pack_metavec v;
825 *meta = NULL;
826 *nmeta = 0;
828 idset = got_object_idset_alloc();
829 if (idset == NULL)
830 return got_error_from_errno("got_object_idset_alloc");
832 v.nmeta = 0;
833 v.metasz = 64;
834 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
835 if (v.meta == NULL) {
836 err = got_error_from_errno("calloc");
837 goto done;
840 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
841 cancel_cb, cancel_arg);
842 if (err || nobj == 0)
843 goto done;
845 for (i = 0; i < ntheirs; i++) {
846 struct got_object_id *id = theirs[i];
847 if (id == NULL)
848 continue;
849 err = got_object_get_type(&obj_type, repo, id);
850 if (err)
851 return err;
852 if (obj_type != GOT_OBJ_TYPE_COMMIT)
853 continue;
854 err = load_commit(NULL, idset, id, repo,
855 loose_obj_only, cancel_cb, cancel_arg);
856 if (err)
857 goto done;
858 if (progress_cb) {
859 err = progress_cb(progress_arg, 0L, nours,
860 v.nmeta, 0, 0);
861 if (err)
862 goto done;
866 for (i = 0; i < ntheirs; i++) {
867 struct got_object_id *id = theirs[i];
868 int *cached_type;
869 if (id == NULL)
870 continue;
871 cached_type = got_object_idset_get(idset, id);
872 if (cached_type == NULL) {
873 err = got_object_get_type(&obj_type, repo, id);
874 if (err)
875 goto done;
876 } else
877 obj_type = *cached_type;
878 if (obj_type != GOT_OBJ_TYPE_TAG)
879 continue;
880 err = load_tag(NULL, idset, id, repo,
881 loose_obj_only, cancel_cb, cancel_arg);
882 if (err)
883 goto done;
884 if (progress_cb) {
885 err = progress_cb(progress_arg, 0L, nours,
886 v.nmeta, 0, 0);
887 if (err)
888 goto done;
892 for (i = 0; i < nobj; i++) {
893 err = load_commit(&v, idset, ids[i], 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 for (i = 0; i < nours; i++) {
906 struct got_object_id *id = ours[i];
907 int *cached_type;
908 if (id == NULL)
909 continue;
910 cached_type = got_object_idset_get(idset, id);
911 if (cached_type == NULL) {
912 err = got_object_get_type(&obj_type, repo, id);
913 if (err)
914 goto done;
915 } else
916 obj_type = *cached_type;
917 if (obj_type != GOT_OBJ_TYPE_TAG)
918 continue;
919 err = load_tag(&v, idset, id, repo,
920 loose_obj_only, cancel_cb, cancel_arg);
921 if (err)
922 goto done;
923 if (progress_cb) {
924 err = progress_cb(progress_arg, 0L, nours,
925 v.nmeta, 0, 0);
926 if (err)
927 goto done;
931 done:
932 for (i = 0; i < nobj; i++) {
933 free(ids[i]);
935 free(ids);
936 got_object_idset_free(idset);
937 if (err == NULL) {
938 *meta = v.meta;
939 *nmeta = v.nmeta;
940 } else
941 free(v.meta);
943 return err;
946 const struct got_error *
947 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
949 size_t n;
951 SHA1Update(ctx, buf, len);
952 n = fwrite(buf, 1, len, f);
953 if (n != len)
954 return got_ferror(f, GOT_ERR_IO);
955 return NULL;
958 static void
959 putbe32(char *b, uint32_t n)
961 b[0] = n >> 24;
962 b[1] = n >> 16;
963 b[2] = n >> 8;
964 b[3] = n >> 0;
967 static int
968 write_order_cmp(const void *pa, const void *pb)
970 struct got_pack_meta *a, *b, *ahd, *bhd;
972 a = *(struct got_pack_meta **)pa;
973 b = *(struct got_pack_meta **)pb;
974 ahd = (a->head == NULL) ? a : a->head;
975 bhd = (b->head == NULL) ? b : b->head;
976 if (ahd->mtime != bhd->mtime)
977 return bhd->mtime - ahd->mtime;
978 if (ahd != bhd)
979 return (uintptr_t)bhd - (uintptr_t)ahd;
980 if (a->nchain != b->nchain)
981 return a->nchain - b->nchain;
982 return a->mtime - b->mtime;
985 static const struct got_error *
986 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
988 size_t i;
990 *hdrlen = 0;
992 hdr[0] = obj_type << 4;
993 hdr[0] |= len & 0xf;
994 len >>= 4;
995 for (i = 1; len != 0; i++){
996 if (i >= bufsize)
997 return got_error(GOT_ERR_NO_SPACE);
998 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
999 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1000 len >>= GOT_DELTA_SIZE_SHIFT;
1003 *hdrlen = i;
1004 return NULL;
1007 static const struct got_error *
1008 append(char **p, int *len, int *sz, void *seg, int nseg)
1010 char *n;
1012 if (*len + nseg >= *sz) {
1013 while (*len + nseg >= *sz)
1014 *sz += *sz / 2;
1015 n = realloc(*p, *sz);
1016 if (n == NULL)
1017 return got_error_from_errno("realloc");
1018 *p = n;
1020 memcpy(*p + *len, seg, nseg);
1021 *len += nseg;
1022 return NULL;
1026 static const struct got_error *
1027 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1028 off_t base_size, char **pp)
1030 const struct got_error *err = NULL;
1031 char *p;
1032 unsigned char buf[16], *bp;
1033 int len, sz, i, j;
1034 off_t n;
1035 struct got_delta_instruction *d;
1037 *pp = NULL;
1038 *nd = 0;
1040 sz = 128;
1041 len = 0;
1042 p = malloc(sz);
1043 if (p == NULL)
1044 return got_error_from_errno("malloc");
1046 /* base object size */
1047 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1048 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1049 for (i = 1; n > 0; i++) {
1050 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1051 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1052 n >>= GOT_DELTA_SIZE_SHIFT;
1054 err = append(&p, &len, &sz, buf, i);
1055 if (err)
1056 return err;
1058 /* target object size */
1059 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1060 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1061 for (i = 1; n > 0; i++) {
1062 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1063 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1064 n >>= GOT_DELTA_SIZE_SHIFT;
1066 err = append(&p, &len, &sz, buf, i);
1067 if (err)
1068 return err;
1069 for (j = 0; j < m->ndeltas; j++) {
1070 d = &m->deltas[j];
1071 if (d->copy) {
1072 n = d->offset;
1073 bp = &buf[1];
1074 buf[0] = GOT_DELTA_BASE_COPY;
1075 for (i = 0; i < 4; i++) {
1076 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1077 buf[0] |= 1 << i;
1078 *bp++ = n & 0xff;
1079 n >>= 8;
1080 if (n == 0)
1081 break;
1084 n = d->len;
1085 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1086 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1087 for (i = 0; i < 3 && n > 0; i++) {
1088 buf[0] |= 1 << (i + 4);
1089 *bp++ = n & 0xff;
1090 n >>= 8;
1093 err = append(&p, &len, &sz, buf, bp - buf);
1094 if (err)
1095 return err;
1096 } else {
1097 char content[128];
1098 size_t r;
1099 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1100 return got_error_from_errno("fseeko");
1101 n = 0;
1102 while (n != d->len) {
1103 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1104 err = append(&p, &len, &sz, buf, 1);
1105 if (err)
1106 return err;
1107 r = fread(content, 1, buf[0], o->f);
1108 if (r != buf[0])
1109 return got_ferror(o->f, GOT_ERR_IO);
1110 err = append(&p, &len, &sz, content, buf[0]);
1111 if (err)
1112 return err;
1113 n += buf[0];
1117 *pp = p;
1118 *nd = len;
1119 return NULL;
1122 static int
1123 packoff(char *hdr, off_t off)
1125 int i, j;
1126 char rbuf[8];
1128 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1129 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1130 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1131 GOT_DELTA_SIZE_MORE;
1134 j = 0;
1135 while (i > 0)
1136 hdr[j++] = rbuf[--i];
1137 return j;
1140 static const struct got_error *
1141 genpack(uint8_t *pack_sha1, FILE *packfile,
1142 struct got_pack_meta **meta, int nmeta, int nours,
1143 int use_offset_deltas, struct got_repository *repo,
1144 got_pack_progress_cb progress_cb, void *progress_arg,
1145 got_cancel_cb cancel_cb, void *cancel_arg)
1147 const struct got_error *err = NULL;
1148 int i, nh, nd;
1149 SHA1_CTX ctx;
1150 struct got_pack_meta *m;
1151 struct got_raw_object *raw;
1152 char *p = NULL, buf[32];
1153 size_t outlen, n;
1154 struct got_deflate_checksum csum;
1155 off_t packfile_size = 0;
1157 SHA1Init(&ctx);
1158 csum.output_sha1 = &ctx;
1159 csum.output_crc = NULL;
1161 err = hwrite(packfile, "PACK", 4, &ctx);
1162 if (err)
1163 return err;
1164 putbe32(buf, GOT_PACKFILE_VERSION);
1165 err = hwrite(packfile, buf, 4, &ctx);
1166 if (err)
1167 goto done;
1168 putbe32(buf, nmeta);
1169 err = hwrite(packfile, buf, 4, &ctx);
1170 if (err)
1171 goto done;
1172 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1173 for (i = 0; i < nmeta; i++) {
1174 if (progress_cb) {
1175 err = progress_cb(progress_arg, packfile_size, nours,
1176 nmeta, nmeta, i);
1177 if (err)
1178 goto done;
1180 m = meta[i];
1181 m->off = ftello(packfile);
1182 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1183 if (err)
1184 goto done;
1185 if (m->deltas == NULL) {
1186 err = packhdr(&nh, buf, sizeof(buf),
1187 m->obj_type, raw->size);
1188 if (err)
1189 goto done;
1190 err = hwrite(packfile, buf, nh, &ctx);
1191 if (err)
1192 goto done;
1193 packfile_size += nh;
1194 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1195 err = got_error_from_errno("fseeko");
1196 goto done;
1198 err = got_deflate_to_file(&outlen, raw->f, packfile,
1199 &csum);
1200 if (err)
1201 goto done;
1202 packfile_size += outlen;
1203 } else {
1204 FILE *delta_file;
1205 struct got_raw_object *base_raw;
1206 err = got_object_raw_open(&base_raw, repo,
1207 &m->prev->id, 8192);
1208 if (err)
1209 goto done;
1210 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1211 if (err)
1212 goto done;
1213 got_object_raw_close(base_raw);
1214 if (use_offset_deltas && m->prev->off != 0) {
1215 err = packhdr(&nh, buf, sizeof(buf),
1216 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1217 if (err)
1218 goto done;
1219 nh += packoff(buf + nh,
1220 m->off - m->prev->off);
1221 err = hwrite(packfile, buf, nh, &ctx);
1222 if (err)
1223 goto done;
1224 packfile_size += nh;
1225 } else {
1226 err = packhdr(&nh, buf, sizeof(buf),
1227 GOT_OBJ_TYPE_REF_DELTA, nd);
1228 err = hwrite(packfile, buf, nh, &ctx);
1229 if (err)
1230 goto done;
1231 packfile_size += nh;
1232 err = hwrite(packfile, m->prev->id.sha1,
1233 sizeof(m->prev->id.sha1), &ctx);
1234 packfile_size += sizeof(m->prev->id.sha1);
1235 if (err)
1236 goto done;
1238 /* XXX need got_deflate_from_mem() */
1239 delta_file = fmemopen(p, nd, "r");
1240 if (delta_file == NULL) {
1241 err = got_error_from_errno("fmemopen");
1242 goto done;
1244 err = got_deflate_to_file(&outlen, delta_file,
1245 packfile, &csum);
1246 fclose(delta_file);
1247 if (err)
1248 goto done;
1249 packfile_size += outlen;
1250 free(p);
1251 p = NULL;
1253 got_object_raw_close(raw);
1254 raw = NULL;
1256 SHA1Final(pack_sha1, &ctx);
1257 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1258 if (n != SHA1_DIGEST_LENGTH)
1259 err = got_ferror(packfile, GOT_ERR_IO);
1260 packfile_size += SHA1_DIGEST_LENGTH;
1261 packfile_size += sizeof(struct got_packfile_hdr);
1262 err = progress_cb(progress_arg, packfile_size, nours,
1263 nmeta, nmeta, nmeta);
1264 if (err)
1265 goto done;
1266 done:
1267 free(p);
1268 return err;
1271 const struct got_error *
1272 got_pack_create(uint8_t *packsha1, FILE *packfile,
1273 struct got_object_id **theirs, int ntheirs,
1274 struct got_object_id **ours, int nours,
1275 struct got_repository *repo, int loose_obj_only, int allow_empty,
1276 got_pack_progress_cb progress_cb, void *progress_arg,
1277 got_cancel_cb cancel_cb, void *cancel_arg)
1279 const struct got_error *err;
1280 struct got_pack_meta **meta;
1281 int nmeta;
1283 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1284 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1285 if (err)
1286 return err;
1288 if (nmeta == 0 && !allow_empty) {
1289 err = got_error(GOT_ERR_CANNOT_PACK);
1290 goto done;
1292 if (nmeta > 0) {
1293 err = pick_deltas(meta, nmeta, nours, repo,
1294 progress_cb, progress_arg, cancel_cb, cancel_arg);
1295 if (err)
1296 goto done;
1299 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1300 progress_cb, progress_arg, cancel_cb, cancel_arg);
1301 if (err)
1302 goto done;
1303 done:
1304 free_nmeta(meta, nmeta);
1305 return err;