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 <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 int obj_types[] = {
289 GOT_OBJ_TYPE_ANY,
290 GOT_OBJ_TYPE_COMMIT,
291 GOT_OBJ_TYPE_TREE,
292 GOT_OBJ_TYPE_BLOB,
293 GOT_OBJ_TYPE_TAG,
294 GOT_OBJ_TYPE_OFFSET_DELTA,
295 GOT_OBJ_TYPE_REF_DELTA
296 };
298 static const struct got_error *
299 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
300 struct got_object_id *id, const char *path, int obj_type,
301 time_t mtime, int loose_obj_only, struct got_repository *repo)
303 const struct got_error *err;
304 struct got_pack_meta *m;
306 if (loose_obj_only) {
307 int is_packed;
308 err = search_packidx(&is_packed, id, repo);
309 if (err)
310 return err;
311 if (is_packed)
312 return NULL;
315 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
316 if (err)
317 return err;
319 if (v == NULL)
320 return NULL;
322 err = alloc_meta(&m, id, path, obj_type, mtime);
323 if (err)
324 goto done;
326 if (v->nmeta == v->metasz){
327 size_t newsize = 2 * v->metasz;
328 struct got_pack_meta **new;
329 new = reallocarray(v->meta, newsize, sizeof(*new));
330 if (new == NULL) {
331 err = got_error_from_errno("reallocarray");
332 goto done;
334 v->meta = new;
335 v->metasz = newsize;
337 done:
338 if (err) {
339 clear_meta(m);
340 free(m);
341 } else
342 v->meta[v->nmeta++] = m;
344 return err;
347 static const struct got_error *
348 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
349 struct got_object_idset *idset, struct got_object_id *tree_id,
350 const char *dpath, time_t mtime, struct got_repository *repo,
351 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
353 const struct got_error *err;
354 struct got_tree_object *tree;
355 char *p = NULL;
356 int i;
358 err = got_object_open_as_tree(&tree, repo, tree_id);
359 if (err)
360 return err;
362 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
363 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
364 struct got_object_id *id = got_tree_entry_get_id(e);
365 mode_t mode = got_tree_entry_get_mode(e);
367 if (cancel_cb) {
368 err = (*cancel_cb)(cancel_arg);
369 if (err)
370 break;
373 if (got_object_tree_entry_is_symlink(e) ||
374 got_object_tree_entry_is_submodule(e) ||
375 got_object_idset_contains(idset, id))
376 continue;
378 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
379 got_tree_entry_get_name(e)) == -1) {
380 err = got_error_from_errno("asprintf");
381 break;
384 if (S_ISDIR(mode)) {
385 struct got_object_qid *qid;
386 err = got_object_qid_alloc(&qid, id);
387 if (err)
388 break;
389 STAILQ_INSERT_TAIL(ids, qid, entry);
390 } else if (S_ISREG(mode)) {
391 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
392 mtime, loose_obj_only, repo);
393 if (err)
394 break;
396 free(p);
397 p = NULL;
400 got_object_tree_close(tree);
401 free(p);
402 return err;
405 static const struct got_error *
406 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
407 struct got_object_id *tree_id, const char *dpath, time_t mtime,
408 int loose_obj_only, struct got_repository *repo,
409 got_cancel_cb cancel_cb, void *cancel_arg)
411 const struct got_error *err = NULL;
412 struct got_object_id_queue tree_ids;
413 struct got_object_qid *qid;
415 if (got_object_idset_contains(idset, tree_id))
416 return NULL;
418 err = got_object_qid_alloc(&qid, tree_id);
419 if (err)
420 return err;
422 STAILQ_INIT(&tree_ids);
423 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
425 while (!STAILQ_EMPTY(&tree_ids)) {
426 if (cancel_cb) {
427 err = (*cancel_cb)(cancel_arg);
428 if (err)
429 break;
432 qid = STAILQ_FIRST(&tree_ids);
433 STAILQ_REMOVE_HEAD(&tree_ids, entry);
435 if (got_object_idset_contains(idset, qid->id)) {
436 got_object_qid_free(qid);
437 continue;
440 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
441 mtime, loose_obj_only, repo);
442 if (err) {
443 got_object_qid_free(qid);
444 break;
447 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
448 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
449 got_object_qid_free(qid);
450 if (err)
451 break;
454 got_object_id_queue_free(&tree_ids);
455 return err;
458 static const struct got_error *
459 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
460 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
461 got_cancel_cb cancel_cb, void *cancel_arg)
463 const struct got_error *err;
464 struct got_commit_object *commit;
466 if (got_object_idset_contains(idset, id))
467 return NULL;
469 if (loose_obj_only) {
470 int is_packed;
471 err = search_packidx(&is_packed, id, repo);
472 if (err)
473 return err;
474 if (is_packed)
475 return NULL;
478 err = got_object_open_as_commit(&commit, repo, id);
479 if (err)
480 return err;
482 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
483 got_object_commit_get_committer_time(commit),
484 loose_obj_only, repo);
485 if (err)
486 goto done;
488 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
489 "", got_object_commit_get_committer_time(commit),
490 loose_obj_only, repo, cancel_cb, cancel_arg);
491 done:
492 got_object_commit_close(commit);
493 return err;
496 static const struct got_error *
497 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
498 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
499 got_cancel_cb cancel_cb, void *cancel_arg)
501 const struct got_error *err;
502 struct got_tag_object *tag = NULL;
504 if (got_object_idset_contains(idset, id))
505 return NULL;
507 if (loose_obj_only) {
508 int is_packed;
509 err = search_packidx(&is_packed, id, repo);
510 if (err)
511 return err;
512 if (is_packed)
513 return NULL;
516 err = got_object_open_as_tag(&tag, repo, id);
517 if (err)
518 return err;
520 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
521 got_object_tag_get_tagger_time(tag),
522 loose_obj_only, repo);
523 if (err)
524 goto done;
526 switch (got_object_tag_get_object_type(tag)) {
527 case GOT_OBJ_TYPE_COMMIT:
528 err = load_commit(v, idset,
529 got_object_tag_get_object_id(tag), repo,
530 loose_obj_only, cancel_cb, cancel_arg);
531 break;
532 case GOT_OBJ_TYPE_TREE:
533 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
534 "", got_object_tag_get_tagger_time(tag),
535 loose_obj_only, repo, cancel_cb, cancel_arg);
536 break;
537 default:
538 break;
541 done:
542 got_object_tag_close(tag);
543 return err;
546 enum findtwixt_color {
547 COLOR_KEEP = 0,
548 COLOR_DROP,
549 COLOR_BLANK,
550 };
551 static const int findtwixt_colors[] = {
552 COLOR_KEEP,
553 COLOR_DROP,
554 COLOR_BLANK
555 };
557 static const struct got_error *
558 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
559 int color, struct got_repository *repo)
561 const struct got_error *err;
562 struct got_object_qid *qid;
564 err = got_object_qid_alloc(&qid, id);
565 if (err)
566 return err;
568 STAILQ_INSERT_TAIL(ids, qid, entry);
569 qid->data = (void *)&findtwixt_colors[color];
570 return NULL;
573 static const struct got_error *
574 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
575 struct got_object_id *id, struct got_repository *repo,
576 got_cancel_cb cancel_cb, void *cancel_arg)
578 const struct got_error *err = NULL;
579 struct got_commit_object *commit;
580 const struct got_object_id_queue *parents;
581 struct got_object_id_queue ids;
582 struct got_object_qid *qid;
584 STAILQ_INIT(&ids);
586 err = got_object_qid_alloc(&qid, id);
587 if (err)
588 return err;
589 STAILQ_INSERT_HEAD(&ids, qid, entry);
591 while (!STAILQ_EMPTY(&ids)) {
592 if (cancel_cb) {
593 err = (*cancel_cb)(cancel_arg);
594 if (err)
595 break;
598 qid = STAILQ_FIRST(&ids);
599 STAILQ_REMOVE_HEAD(&ids, entry);
601 if (got_object_idset_contains(drop, qid->id)) {
602 got_object_qid_free(qid);
603 continue;
606 err = got_object_idset_add(drop, qid->id,
607 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
608 if (err) {
609 got_object_qid_free(qid);
610 break;
613 if (!got_object_idset_contains(keep, qid->id)) {
614 got_object_qid_free(qid);
615 continue;
618 err = got_object_open_as_commit(&commit, repo, qid->id);
619 got_object_qid_free(qid);
620 if (err)
621 break;
623 parents = got_object_commit_get_parent_ids(commit);
624 if (parents) {
625 err = got_object_id_queue_copy(parents, &ids);
626 if (err) {
627 got_object_commit_close(commit);
628 break;
631 got_object_commit_close(commit);
634 got_object_id_queue_free(&ids);
635 return err;
638 struct append_id_arg {
639 struct got_object_id **array;
640 int idx;
641 };
643 static const struct got_error *
644 append_id(struct got_object_id *id, void *data, void *arg)
646 struct append_id_arg *a = arg;
648 a->array[a->idx] = got_object_id_dup(id);
649 if (a->array[a->idx] == NULL)
650 return got_error_from_errno("got_object_id_dup");
652 a->idx++;
653 return NULL;
656 static const struct got_error *
657 findtwixt(struct got_object_id ***res, int *nres,
658 struct got_object_id **head, int nhead,
659 struct got_object_id **tail, int ntail,
660 struct got_repository *repo,
661 got_cancel_cb cancel_cb, void *cancel_arg)
663 const struct got_error *err = NULL;
664 struct got_object_id_queue ids;
665 struct got_object_idset *keep, *drop;
666 struct got_object_qid *qid;
667 int i, ncolor, nkeep, obj_type;
669 STAILQ_INIT(&ids);
670 *res = NULL;
671 *nres = 0;
673 keep = got_object_idset_alloc();
674 if (keep == NULL)
675 return got_error_from_errno("got_object_idset_alloc");
677 drop = got_object_idset_alloc();
678 if (drop == NULL) {
679 err = got_error_from_errno("got_object_idset_alloc");
680 goto done;
683 for (i = 0; i < nhead; i++) {
684 struct got_object_id *id = head[i];
685 if (id == NULL)
686 continue;
687 err = got_object_get_type(&obj_type, repo, id);
688 if (err)
689 return err;
690 if (obj_type != GOT_OBJ_TYPE_COMMIT)
691 continue;
692 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
693 if (err)
694 goto done;
696 for (i = 0; i < ntail; i++) {
697 struct got_object_id *id = tail[i];
698 if (id == NULL)
699 continue;
700 err = got_object_get_type(&obj_type, repo, id);
701 if (err)
702 return err;
703 if (obj_type != GOT_OBJ_TYPE_COMMIT)
704 continue;
705 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
706 if (err)
707 goto done;
710 while (!STAILQ_EMPTY(&ids)) {
711 int qcolor;
712 qid = STAILQ_FIRST(&ids);
713 qcolor = *((int *)qid->data);
715 if (got_object_idset_contains(drop, qid->id))
716 ncolor = COLOR_DROP;
717 else if (got_object_idset_contains(keep, qid->id))
718 ncolor = COLOR_KEEP;
719 else
720 ncolor = COLOR_BLANK;
722 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
723 qcolor == COLOR_KEEP)) {
724 STAILQ_REMOVE_HEAD(&ids, entry);
725 got_object_qid_free(qid);
726 continue;
729 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
730 err = drop_commit(keep, drop, qid->id, repo,
731 cancel_cb, cancel_arg);
732 if (err)
733 goto done;
734 } else if (ncolor == COLOR_BLANK) {
735 struct got_commit_object *commit;
736 struct got_object_id *id;
737 const struct got_object_id_queue *parents;
738 struct got_object_qid *pid;
740 id = got_object_id_dup(qid->id);
741 if (id == NULL) {
742 err = got_error_from_errno("got_object_id_dup");
743 goto done;
745 if (qcolor == COLOR_KEEP)
746 err = got_object_idset_add(keep, id,
747 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
748 else
749 err = got_object_idset_add(drop, id,
750 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
751 if (err) {
752 free(id);
753 goto done;
756 err = got_object_open_as_commit(&commit, repo, id);
757 if (err) {
758 free(id);
759 goto done;
761 parents = got_object_commit_get_parent_ids(commit);
762 if (parents) {
763 STAILQ_FOREACH(pid, parents, entry) {
764 err = queue_commit_id(&ids, pid->id,
765 qcolor, repo);
766 if (err) {
767 free(id);
768 goto done;
772 got_object_commit_close(commit);
773 commit = NULL;
774 } else {
775 /* should not happen */
776 err = got_error_fmt(GOT_ERR_NOT_IMPL,
777 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
778 goto done;
781 STAILQ_REMOVE_HEAD(&ids, entry);
782 got_object_qid_free(qid);
785 nkeep = got_object_idset_num_elements(keep);
786 if (nkeep > 0) {
787 struct append_id_arg arg;
788 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
789 if (arg.array == NULL) {
790 err = got_error_from_errno("calloc");
791 goto done;
793 arg.idx = 0;
794 err = got_object_idset_for_each(keep, append_id, &arg);
795 if (err) {
796 free(arg.array);
797 goto done;
799 *res = arg.array;
800 *nres = nkeep;
802 done:
803 got_object_idset_free(keep);
804 got_object_idset_free(drop);
805 got_object_id_queue_free(&ids);
806 return err;
809 static const struct got_error *
810 read_meta(struct got_pack_meta ***meta, int *nmeta,
811 struct got_object_id **theirs, int ntheirs,
812 struct got_object_id **ours, int nours, struct got_repository *repo,
813 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
814 got_cancel_cb cancel_cb, void *cancel_arg)
816 const struct got_error *err = NULL;
817 struct got_object_id **ids = NULL;
818 struct got_object_idset *idset;
819 int i, nobj = 0, obj_type;
820 struct got_pack_metavec v;
822 *meta = NULL;
823 *nmeta = 0;
825 idset = got_object_idset_alloc();
826 if (idset == NULL)
827 return got_error_from_errno("got_object_idset_alloc");
829 v.nmeta = 0;
830 v.metasz = 64;
831 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
832 if (v.meta == NULL) {
833 err = got_error_from_errno("reallocarray");
834 goto done;
837 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
838 cancel_cb, cancel_arg);
839 if (err || nobj == 0)
840 goto done;
842 for (i = 0; i < ntheirs; i++) {
843 struct got_object_id *id = theirs[i];
844 if (id == NULL)
845 continue;
846 err = got_object_get_type(&obj_type, repo, id);
847 if (err)
848 return err;
849 if (obj_type != GOT_OBJ_TYPE_COMMIT)
850 continue;
851 err = load_commit(NULL, idset, id, 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 = theirs[i];
865 int *cached_type;
866 if (id == NULL)
867 continue;
868 cached_type = got_object_idset_get(idset, id);
869 if (cached_type == NULL) {
870 err = got_object_get_type(&obj_type, repo, id);
871 if (err)
872 goto done;
873 } else
874 obj_type = *cached_type;
875 if (obj_type != GOT_OBJ_TYPE_TAG)
876 continue;
877 err = load_tag(NULL, idset, id, repo,
878 loose_obj_only, cancel_cb, cancel_arg);
879 if (err)
880 goto done;
881 if (progress_cb) {
882 err = progress_cb(progress_arg, 0L, nours,
883 v.nmeta, 0, 0);
884 if (err)
885 goto done;
889 for (i = 0; i < nobj; i++) {
890 err = load_commit(&v, idset, ids[i], repo,
891 loose_obj_only, cancel_cb, cancel_arg);
892 if (err)
893 goto done;
894 if (progress_cb) {
895 err = progress_cb(progress_arg, 0L, nours,
896 v.nmeta, 0, 0);
897 if (err)
898 goto done;
902 for (i = 0; i < nours; i++) {
903 struct got_object_id *id = ours[i];
904 int *cached_type;
905 if (id == NULL)
906 continue;
907 cached_type = got_object_idset_get(idset, id);
908 if (cached_type == NULL) {
909 err = got_object_get_type(&obj_type, repo, id);
910 if (err)
911 goto done;
912 } else
913 obj_type = *cached_type;
914 if (obj_type != GOT_OBJ_TYPE_TAG)
915 continue;
916 err = load_tag(&v, idset, id, repo,
917 loose_obj_only, cancel_cb, cancel_arg);
918 if (err)
919 goto done;
920 if (progress_cb) {
921 err = progress_cb(progress_arg, 0L, nours,
922 v.nmeta, 0, 0);
923 if (err)
924 goto done;
928 done:
929 for (i = 0; i < nobj; i++) {
930 free(ids[i]);
932 free(ids);
933 got_object_idset_free(idset);
934 if (err == NULL) {
935 *meta = v.meta;
936 *nmeta = v.nmeta;
937 } else
938 free(v.meta);
940 return err;
943 const struct got_error *
944 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
946 size_t n;
948 SHA1Update(ctx, buf, len);
949 n = fwrite(buf, 1, len, f);
950 if (n != len)
951 return got_ferror(f, GOT_ERR_IO);
952 return NULL;
955 static void
956 putbe32(char *b, uint32_t n)
958 b[0] = n >> 24;
959 b[1] = n >> 16;
960 b[2] = n >> 8;
961 b[3] = n >> 0;
964 static int
965 write_order_cmp(const void *pa, const void *pb)
967 struct got_pack_meta *a, *b, *ahd, *bhd;
969 a = *(struct got_pack_meta **)pa;
970 b = *(struct got_pack_meta **)pb;
971 ahd = (a->head == NULL) ? a : a->head;
972 bhd = (b->head == NULL) ? b : b->head;
973 if (ahd->mtime != bhd->mtime)
974 return bhd->mtime - ahd->mtime;
975 if (ahd != bhd)
976 return (uintptr_t)bhd - (uintptr_t)ahd;
977 if (a->nchain != b->nchain)
978 return a->nchain - b->nchain;
979 return a->mtime - b->mtime;
982 static const struct got_error *
983 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
985 size_t i;
987 *hdrlen = 0;
989 hdr[0] = obj_type << 4;
990 hdr[0] |= len & 0xf;
991 len >>= 4;
992 for (i = 1; len != 0; i++){
993 if (i >= bufsize)
994 return got_error(GOT_ERR_NO_SPACE);
995 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
996 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
997 len >>= GOT_DELTA_SIZE_SHIFT;
1000 *hdrlen = i;
1001 return NULL;
1004 static const struct got_error *
1005 append(char **p, int *len, int *sz, void *seg, int nseg)
1007 char *n;
1009 if (*len + nseg >= *sz) {
1010 while (*len + nseg >= *sz)
1011 *sz += *sz / 2;
1012 n = realloc(*p, *sz);
1013 if (n == NULL)
1014 return got_error_from_errno("realloc");
1015 *p = n;
1017 memcpy(*p + *len, seg, nseg);
1018 *len += nseg;
1019 return NULL;
1023 static const struct got_error *
1024 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1025 off_t base_size, char **pp)
1027 const struct got_error *err = NULL;
1028 char *p;
1029 unsigned char buf[16], *bp;
1030 int len, sz, i, j;
1031 off_t n;
1032 struct got_delta_instruction *d;
1034 *pp = NULL;
1035 *nd = 0;
1037 sz = 128;
1038 len = 0;
1039 p = malloc(sz);
1040 if (p == NULL)
1041 return got_error_from_errno("malloc");
1043 /* base object size */
1044 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1045 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1046 for (i = 1; n > 0; i++) {
1047 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1048 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1049 n >>= GOT_DELTA_SIZE_SHIFT;
1051 err = append(&p, &len, &sz, buf, i);
1052 if (err)
1053 return err;
1055 /* target object size */
1056 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1057 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1058 for (i = 1; n > 0; i++) {
1059 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1060 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1061 n >>= GOT_DELTA_SIZE_SHIFT;
1063 err = append(&p, &len, &sz, buf, i);
1064 if (err)
1065 return err;
1066 for (j = 0; j < m->ndeltas; j++) {
1067 d = &m->deltas[j];
1068 if (d->copy) {
1069 n = d->offset;
1070 bp = &buf[1];
1071 buf[0] = GOT_DELTA_BASE_COPY;
1072 for (i = 0; i < 4; i++) {
1073 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1074 buf[0] |= 1 << i;
1075 *bp++ = n & 0xff;
1076 n >>= 8;
1077 if (n == 0)
1078 break;
1081 n = d->len;
1082 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1083 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1084 for (i = 0; i < 3 && n > 0; i++) {
1085 buf[0] |= 1 << (i + 4);
1086 *bp++ = n & 0xff;
1087 n >>= 8;
1090 err = append(&p, &len, &sz, buf, bp - buf);
1091 if (err)
1092 return err;
1093 } else {
1094 char content[128];
1095 size_t r;
1096 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1097 return got_error_from_errno("fseeko");
1098 n = 0;
1099 while (n != d->len) {
1100 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1101 err = append(&p, &len, &sz, buf, 1);
1102 if (err)
1103 return err;
1104 r = fread(content, 1, buf[0], o->f);
1105 if (r != buf[0])
1106 return got_ferror(o->f, GOT_ERR_IO);
1107 err = append(&p, &len, &sz, content, buf[0]);
1108 if (err)
1109 return err;
1110 n += buf[0];
1114 *pp = p;
1115 *nd = len;
1116 return NULL;
1119 static int
1120 packoff(char *hdr, off_t off)
1122 int i, j;
1123 char rbuf[8];
1125 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1126 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1127 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1128 GOT_DELTA_SIZE_MORE;
1131 j = 0;
1132 while (i > 0)
1133 hdr[j++] = rbuf[--i];
1134 return j;
1137 static const struct got_error *
1138 genpack(uint8_t *pack_sha1, FILE *packfile,
1139 struct got_pack_meta **meta, int nmeta, int nours,
1140 int use_offset_deltas, struct got_repository *repo,
1141 got_pack_progress_cb progress_cb, void *progress_arg,
1142 got_cancel_cb cancel_cb, void *cancel_arg)
1144 const struct got_error *err = NULL;
1145 int i, nh, nd;
1146 SHA1_CTX ctx;
1147 struct got_pack_meta *m;
1148 struct got_raw_object *raw;
1149 char *p = NULL, buf[32];
1150 size_t outlen, n;
1151 struct got_deflate_checksum csum;
1152 off_t packfile_size = 0;
1154 SHA1Init(&ctx);
1155 csum.output_sha1 = &ctx;
1156 csum.output_crc = NULL;
1158 err = hwrite(packfile, "PACK", 4, &ctx);
1159 if (err)
1160 return err;
1161 putbe32(buf, GOT_PACKFILE_VERSION);
1162 err = hwrite(packfile, buf, 4, &ctx);
1163 if (err)
1164 goto done;
1165 putbe32(buf, nmeta);
1166 err = hwrite(packfile, buf, 4, &ctx);
1167 if (err)
1168 goto done;
1169 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1170 for (i = 0; i < nmeta; i++) {
1171 if (progress_cb) {
1172 err = progress_cb(progress_arg, packfile_size, nours,
1173 nmeta, nmeta, i);
1174 if (err)
1175 goto done;
1177 m = meta[i];
1178 m->off = ftello(packfile);
1179 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1180 if (err)
1181 goto done;
1182 if (m->deltas == NULL) {
1183 err = packhdr(&nh, buf, sizeof(buf),
1184 m->obj_type, raw->size);
1185 if (err)
1186 goto done;
1187 err = hwrite(packfile, buf, nh, &ctx);
1188 if (err)
1189 goto done;
1190 packfile_size += nh;
1191 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1192 err = got_error_from_errno("fseeko");
1193 goto done;
1195 err = got_deflate_to_file(&outlen, raw->f, packfile,
1196 &csum);
1197 if (err)
1198 goto done;
1199 packfile_size += outlen;
1200 } else {
1201 FILE *delta_file;
1202 struct got_raw_object *base_raw;
1203 err = got_object_raw_open(&base_raw, repo,
1204 &m->prev->id, 8192);
1205 if (err)
1206 goto done;
1207 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1208 if (err)
1209 goto done;
1210 got_object_raw_close(base_raw);
1211 if (use_offset_deltas && m->prev->off != 0) {
1212 err = packhdr(&nh, buf, sizeof(buf),
1213 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1214 if (err)
1215 goto done;
1216 nh += packoff(buf + nh,
1217 m->off - m->prev->off);
1218 err = hwrite(packfile, buf, nh, &ctx);
1219 if (err)
1220 goto done;
1221 packfile_size += nh;
1222 } else {
1223 err = packhdr(&nh, buf, sizeof(buf),
1224 GOT_OBJ_TYPE_REF_DELTA, nd);
1225 err = hwrite(packfile, buf, nh, &ctx);
1226 if (err)
1227 goto done;
1228 packfile_size += nh;
1229 err = hwrite(packfile, m->prev->id.sha1,
1230 sizeof(m->prev->id.sha1), &ctx);
1231 packfile_size += sizeof(m->prev->id.sha1);
1232 if (err)
1233 goto done;
1235 /* XXX need got_deflate_from_mem() */
1236 delta_file = fmemopen(p, nd, "r");
1237 if (delta_file == NULL) {
1238 err = got_error_from_errno("fmemopen");
1239 goto done;
1241 err = got_deflate_to_file(&outlen, delta_file,
1242 packfile, &csum);
1243 fclose(delta_file);
1244 if (err)
1245 goto done;
1246 packfile_size += outlen;
1247 free(p);
1248 p = NULL;
1250 got_object_raw_close(raw);
1251 raw = NULL;
1253 SHA1Final(pack_sha1, &ctx);
1254 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1255 if (n != SHA1_DIGEST_LENGTH)
1256 err = got_ferror(packfile, GOT_ERR_IO);
1257 packfile_size += SHA1_DIGEST_LENGTH;
1258 packfile_size += sizeof(struct got_packfile_hdr);
1259 err = progress_cb(progress_arg, packfile_size, nours,
1260 nmeta, nmeta, nmeta);
1261 if (err)
1262 goto done;
1263 done:
1264 free(p);
1265 return err;
1268 const struct got_error *
1269 got_pack_create(uint8_t *packsha1, FILE *packfile,
1270 struct got_object_id **theirs, int ntheirs,
1271 struct got_object_id **ours, int nours,
1272 struct got_repository *repo, int loose_obj_only, int allow_empty,
1273 got_pack_progress_cb progress_cb, void *progress_arg,
1274 got_cancel_cb cancel_cb, void *cancel_arg)
1276 const struct got_error *err;
1277 struct got_pack_meta **meta;
1278 int nmeta;
1280 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1281 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1282 if (err)
1283 return err;
1285 if (nmeta == 0 && !allow_empty) {
1286 err = got_error(GOT_ERR_CANNOT_PACK);
1287 goto done;
1289 if (nmeta > 0) {
1290 err = pick_deltas(meta, nmeta, nours, repo,
1291 progress_cb, progress_arg, cancel_cb, cancel_arg);
1292 if (err)
1293 goto done;
1296 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1297 progress_cb, progress_arg, cancel_cb, cancel_arg);
1298 if (err)
1299 goto done;
1300 done:
1301 free_nmeta(meta, nmeta);
1302 return err;