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_submodule(e) ||
374 got_object_idset_contains(idset, id))
375 continue;
377 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
378 got_tree_entry_get_name(e)) == -1) {
379 err = got_error_from_errno("asprintf");
380 break;
383 if (S_ISDIR(mode)) {
384 struct got_object_qid *qid;
385 err = got_object_qid_alloc(&qid, id);
386 if (err)
387 break;
388 STAILQ_INSERT_TAIL(ids, qid, entry);
389 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
390 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
391 mtime, loose_obj_only, repo);
392 if (err)
393 break;
395 free(p);
396 p = NULL;
399 got_object_tree_close(tree);
400 free(p);
401 return err;
404 static const struct got_error *
405 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
406 struct got_object_id *tree_id, const char *dpath, time_t mtime,
407 int loose_obj_only, struct got_repository *repo,
408 got_cancel_cb cancel_cb, void *cancel_arg)
410 const struct got_error *err = NULL;
411 struct got_object_id_queue tree_ids;
412 struct got_object_qid *qid;
414 if (got_object_idset_contains(idset, tree_id))
415 return NULL;
417 err = got_object_qid_alloc(&qid, tree_id);
418 if (err)
419 return err;
421 STAILQ_INIT(&tree_ids);
422 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
424 while (!STAILQ_EMPTY(&tree_ids)) {
425 if (cancel_cb) {
426 err = (*cancel_cb)(cancel_arg);
427 if (err)
428 break;
431 qid = STAILQ_FIRST(&tree_ids);
432 STAILQ_REMOVE_HEAD(&tree_ids, entry);
434 if (got_object_idset_contains(idset, qid->id)) {
435 got_object_qid_free(qid);
436 continue;
439 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
440 mtime, loose_obj_only, repo);
441 if (err) {
442 got_object_qid_free(qid);
443 break;
446 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
447 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
448 got_object_qid_free(qid);
449 if (err)
450 break;
453 got_object_id_queue_free(&tree_ids);
454 return err;
457 static const struct got_error *
458 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
459 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
460 got_cancel_cb cancel_cb, void *cancel_arg)
462 const struct got_error *err;
463 struct got_commit_object *commit;
465 if (got_object_idset_contains(idset, id))
466 return NULL;
468 if (loose_obj_only) {
469 int is_packed;
470 err = search_packidx(&is_packed, id, repo);
471 if (err)
472 return err;
473 if (is_packed)
474 return NULL;
477 err = got_object_open_as_commit(&commit, repo, id);
478 if (err)
479 return err;
481 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
482 got_object_commit_get_committer_time(commit),
483 loose_obj_only, repo);
484 if (err)
485 goto done;
487 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
488 "", got_object_commit_get_committer_time(commit),
489 loose_obj_only, repo, cancel_cb, cancel_arg);
490 done:
491 got_object_commit_close(commit);
492 return err;
495 static const struct got_error *
496 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
497 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
498 got_cancel_cb cancel_cb, void *cancel_arg)
500 const struct got_error *err;
501 struct got_tag_object *tag = NULL;
503 if (got_object_idset_contains(idset, id))
504 return NULL;
506 if (loose_obj_only) {
507 int is_packed;
508 err = search_packidx(&is_packed, id, repo);
509 if (err)
510 return err;
511 if (is_packed)
512 return NULL;
515 err = got_object_open_as_tag(&tag, repo, id);
516 if (err)
517 return err;
519 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
520 got_object_tag_get_tagger_time(tag),
521 loose_obj_only, repo);
522 if (err)
523 goto done;
525 switch (got_object_tag_get_object_type(tag)) {
526 case GOT_OBJ_TYPE_COMMIT:
527 err = load_commit(v, idset,
528 got_object_tag_get_object_id(tag), repo,
529 loose_obj_only, cancel_cb, cancel_arg);
530 break;
531 case GOT_OBJ_TYPE_TREE:
532 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
533 "", got_object_tag_get_tagger_time(tag),
534 loose_obj_only, repo, cancel_cb, cancel_arg);
535 break;
536 default:
537 break;
540 done:
541 got_object_tag_close(tag);
542 return err;
545 enum findtwixt_color {
546 COLOR_KEEP = 0,
547 COLOR_DROP,
548 COLOR_BLANK,
549 };
550 static const int findtwixt_colors[] = {
551 COLOR_KEEP,
552 COLOR_DROP,
553 COLOR_BLANK
554 };
556 static const struct got_error *
557 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
558 int color, struct got_repository *repo)
560 const struct got_error *err;
561 struct got_object_qid *qid;
563 err = got_object_qid_alloc(&qid, id);
564 if (err)
565 return err;
567 STAILQ_INSERT_TAIL(ids, qid, entry);
568 qid->data = (void *)&findtwixt_colors[color];
569 return NULL;
572 static const struct got_error *
573 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
574 struct got_object_id *id, struct got_repository *repo,
575 got_cancel_cb cancel_cb, void *cancel_arg)
577 const struct got_error *err = NULL;
578 struct got_commit_object *commit;
579 const struct got_object_id_queue *parents;
580 struct got_object_id_queue ids;
581 struct got_object_qid *qid;
583 STAILQ_INIT(&ids);
585 err = got_object_qid_alloc(&qid, id);
586 if (err)
587 return err;
588 STAILQ_INSERT_HEAD(&ids, qid, entry);
590 while (!STAILQ_EMPTY(&ids)) {
591 if (cancel_cb) {
592 err = (*cancel_cb)(cancel_arg);
593 if (err)
594 break;
597 qid = STAILQ_FIRST(&ids);
598 STAILQ_REMOVE_HEAD(&ids, entry);
600 if (got_object_idset_contains(drop, qid->id)) {
601 got_object_qid_free(qid);
602 continue;
605 err = got_object_idset_add(drop, qid->id,
606 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
607 if (err) {
608 got_object_qid_free(qid);
609 break;
612 if (!got_object_idset_contains(keep, qid->id)) {
613 got_object_qid_free(qid);
614 continue;
617 err = got_object_open_as_commit(&commit, repo, qid->id);
618 got_object_qid_free(qid);
619 if (err)
620 break;
622 parents = got_object_commit_get_parent_ids(commit);
623 if (parents) {
624 err = got_object_id_queue_copy(parents, &ids);
625 if (err) {
626 got_object_commit_close(commit);
627 break;
630 got_object_commit_close(commit);
633 got_object_id_queue_free(&ids);
634 return err;
637 struct append_id_arg {
638 struct got_object_id **array;
639 int idx;
640 };
642 static const struct got_error *
643 append_id(struct got_object_id *id, void *data, void *arg)
645 struct append_id_arg *a = arg;
647 a->array[a->idx] = got_object_id_dup(id);
648 if (a->array[a->idx] == NULL)
649 return got_error_from_errno("got_object_id_dup");
651 a->idx++;
652 return NULL;
655 static const struct got_error *
656 findtwixt(struct got_object_id ***res, int *nres,
657 struct got_object_id **head, int nhead,
658 struct got_object_id **tail, int ntail,
659 struct got_repository *repo,
660 got_cancel_cb cancel_cb, void *cancel_arg)
662 const struct got_error *err = NULL;
663 struct got_object_id_queue ids;
664 struct got_object_idset *keep, *drop;
665 struct got_object_qid *qid;
666 int i, ncolor, nkeep, obj_type;
668 STAILQ_INIT(&ids);
669 *res = NULL;
670 *nres = 0;
672 keep = got_object_idset_alloc();
673 if (keep == NULL)
674 return got_error_from_errno("got_object_idset_alloc");
676 drop = got_object_idset_alloc();
677 if (drop == NULL) {
678 err = got_error_from_errno("got_object_idset_alloc");
679 goto done;
682 for (i = 0; i < nhead; i++) {
683 struct got_object_id *id = head[i];
684 if (id == NULL)
685 continue;
686 err = got_object_get_type(&obj_type, repo, id);
687 if (err)
688 return err;
689 if (obj_type != GOT_OBJ_TYPE_COMMIT)
690 continue;
691 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
692 if (err)
693 goto done;
695 for (i = 0; i < ntail; i++) {
696 struct got_object_id *id = tail[i];
697 if (id == NULL)
698 continue;
699 err = got_object_get_type(&obj_type, repo, id);
700 if (err)
701 return err;
702 if (obj_type != GOT_OBJ_TYPE_COMMIT)
703 continue;
704 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
705 if (err)
706 goto done;
709 while (!STAILQ_EMPTY(&ids)) {
710 int qcolor;
711 qid = STAILQ_FIRST(&ids);
712 qcolor = *((int *)qid->data);
714 if (got_object_idset_contains(drop, qid->id))
715 ncolor = COLOR_DROP;
716 else if (got_object_idset_contains(keep, qid->id))
717 ncolor = COLOR_KEEP;
718 else
719 ncolor = COLOR_BLANK;
721 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
722 qcolor == COLOR_KEEP)) {
723 STAILQ_REMOVE_HEAD(&ids, entry);
724 got_object_qid_free(qid);
725 continue;
728 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
729 err = drop_commit(keep, drop, qid->id, repo,
730 cancel_cb, cancel_arg);
731 if (err)
732 goto done;
733 } else if (ncolor == COLOR_BLANK) {
734 struct got_commit_object *commit;
735 struct got_object_id *id;
736 const struct got_object_id_queue *parents;
737 struct got_object_qid *pid;
739 id = got_object_id_dup(qid->id);
740 if (id == NULL) {
741 err = got_error_from_errno("got_object_id_dup");
742 goto done;
744 if (qcolor == COLOR_KEEP)
745 err = got_object_idset_add(keep, id,
746 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
747 else
748 err = got_object_idset_add(drop, id,
749 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
750 if (err) {
751 free(id);
752 goto done;
755 err = got_object_open_as_commit(&commit, repo, id);
756 if (err) {
757 free(id);
758 goto done;
760 parents = got_object_commit_get_parent_ids(commit);
761 if (parents) {
762 STAILQ_FOREACH(pid, parents, entry) {
763 err = queue_commit_id(&ids, pid->id,
764 qcolor, repo);
765 if (err) {
766 free(id);
767 goto done;
771 got_object_commit_close(commit);
772 commit = NULL;
773 } else {
774 /* should not happen */
775 err = got_error_fmt(GOT_ERR_NOT_IMPL,
776 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
777 goto done;
780 STAILQ_REMOVE_HEAD(&ids, entry);
781 got_object_qid_free(qid);
784 nkeep = got_object_idset_num_elements(keep);
785 if (nkeep > 0) {
786 struct append_id_arg arg;
787 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
788 if (arg.array == NULL) {
789 err = got_error_from_errno("calloc");
790 goto done;
792 arg.idx = 0;
793 err = got_object_idset_for_each(keep, append_id, &arg);
794 if (err) {
795 free(arg.array);
796 goto done;
798 *res = arg.array;
799 *nres = nkeep;
801 done:
802 got_object_idset_free(keep);
803 got_object_idset_free(drop);
804 got_object_id_queue_free(&ids);
805 return err;
808 static const struct got_error *
809 read_meta(struct got_pack_meta ***meta, int *nmeta,
810 struct got_object_id **theirs, int ntheirs,
811 struct got_object_id **ours, int nours, struct got_repository *repo,
812 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
813 got_cancel_cb cancel_cb, void *cancel_arg)
815 const struct got_error *err = NULL;
816 struct got_object_id **ids = NULL;
817 struct got_object_idset *idset;
818 int i, nobj = 0, obj_type;
819 struct got_pack_metavec v;
821 *meta = NULL;
822 *nmeta = 0;
824 idset = got_object_idset_alloc();
825 if (idset == NULL)
826 return got_error_from_errno("got_object_idset_alloc");
828 v.nmeta = 0;
829 v.metasz = 64;
830 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
831 if (v.meta == NULL) {
832 err = got_error_from_errno("reallocarray");
833 goto done;
836 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
837 cancel_cb, cancel_arg);
838 if (err || nobj == 0)
839 goto done;
841 for (i = 0; i < ntheirs; i++) {
842 struct got_object_id *id = theirs[i];
843 if (id == NULL)
844 continue;
845 err = got_object_get_type(&obj_type, repo, id);
846 if (err)
847 return err;
848 if (obj_type != GOT_OBJ_TYPE_COMMIT)
849 continue;
850 err = load_commit(NULL, idset, id, repo,
851 loose_obj_only, cancel_cb, cancel_arg);
852 if (err)
853 goto done;
854 if (progress_cb) {
855 err = progress_cb(progress_arg, 0L, nours,
856 v.nmeta, 0, 0);
857 if (err)
858 goto done;
862 for (i = 0; i < ntheirs; i++) {
863 struct got_object_id *id = theirs[i];
864 int *cached_type;
865 if (id == NULL)
866 continue;
867 cached_type = got_object_idset_get(idset, id);
868 if (cached_type == NULL) {
869 err = got_object_get_type(&obj_type, repo, id);
870 if (err)
871 goto done;
872 } else
873 obj_type = *cached_type;
874 if (obj_type != GOT_OBJ_TYPE_TAG)
875 continue;
876 err = load_tag(NULL, idset, id, repo,
877 loose_obj_only, cancel_cb, cancel_arg);
878 if (err)
879 goto done;
880 if (progress_cb) {
881 err = progress_cb(progress_arg, 0L, nours,
882 v.nmeta, 0, 0);
883 if (err)
884 goto done;
888 for (i = 0; i < nobj; i++) {
889 err = load_commit(&v, idset, ids[i], repo,
890 loose_obj_only, cancel_cb, cancel_arg);
891 if (err)
892 goto done;
893 if (progress_cb) {
894 err = progress_cb(progress_arg, 0L, nours,
895 v.nmeta, 0, 0);
896 if (err)
897 goto done;
901 for (i = 0; i < nours; i++) {
902 struct got_object_id *id = ours[i];
903 int *cached_type;
904 if (id == NULL)
905 continue;
906 cached_type = got_object_idset_get(idset, id);
907 if (cached_type == NULL) {
908 err = got_object_get_type(&obj_type, repo, id);
909 if (err)
910 goto done;
911 } else
912 obj_type = *cached_type;
913 if (obj_type != GOT_OBJ_TYPE_TAG)
914 continue;
915 err = load_tag(&v, idset, id, repo,
916 loose_obj_only, cancel_cb, cancel_arg);
917 if (err)
918 goto done;
919 if (progress_cb) {
920 err = progress_cb(progress_arg, 0L, nours,
921 v.nmeta, 0, 0);
922 if (err)
923 goto done;
927 done:
928 for (i = 0; i < nobj; i++) {
929 free(ids[i]);
931 free(ids);
932 got_object_idset_free(idset);
933 if (err == NULL) {
934 *meta = v.meta;
935 *nmeta = v.nmeta;
936 } else
937 free(v.meta);
939 return err;
942 const struct got_error *
943 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
945 size_t n;
947 SHA1Update(ctx, buf, len);
948 n = fwrite(buf, 1, len, f);
949 if (n != len)
950 return got_ferror(f, GOT_ERR_IO);
951 return NULL;
954 static void
955 putbe32(char *b, uint32_t n)
957 b[0] = n >> 24;
958 b[1] = n >> 16;
959 b[2] = n >> 8;
960 b[3] = n >> 0;
963 static int
964 write_order_cmp(const void *pa, const void *pb)
966 struct got_pack_meta *a, *b, *ahd, *bhd;
968 a = *(struct got_pack_meta **)pa;
969 b = *(struct got_pack_meta **)pb;
970 ahd = (a->head == NULL) ? a : a->head;
971 bhd = (b->head == NULL) ? b : b->head;
972 if (ahd->mtime != bhd->mtime)
973 return bhd->mtime - ahd->mtime;
974 if (ahd != bhd)
975 return (uintptr_t)bhd - (uintptr_t)ahd;
976 if (a->nchain != b->nchain)
977 return a->nchain - b->nchain;
978 return a->mtime - b->mtime;
981 static const struct got_error *
982 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
984 size_t i;
986 *hdrlen = 0;
988 hdr[0] = obj_type << 4;
989 hdr[0] |= len & 0xf;
990 len >>= 4;
991 for (i = 1; len != 0; i++){
992 if (i >= bufsize)
993 return got_error(GOT_ERR_NO_SPACE);
994 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
995 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
996 len >>= GOT_DELTA_SIZE_SHIFT;
999 *hdrlen = i;
1000 return NULL;
1003 static const struct got_error *
1004 append(char **p, int *len, int *sz, void *seg, int nseg)
1006 char *n;
1008 if (*len + nseg >= *sz) {
1009 while (*len + nseg >= *sz)
1010 *sz += *sz / 2;
1011 n = realloc(*p, *sz);
1012 if (n == NULL)
1013 return got_error_from_errno("realloc");
1014 *p = n;
1016 memcpy(*p + *len, seg, nseg);
1017 *len += nseg;
1018 return NULL;
1022 static const struct got_error *
1023 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1024 off_t base_size, char **pp)
1026 const struct got_error *err = NULL;
1027 char *p;
1028 unsigned char buf[16], *bp;
1029 int len, sz, i, j;
1030 off_t n;
1031 struct got_delta_instruction *d;
1033 *pp = NULL;
1034 *nd = 0;
1036 sz = 128;
1037 len = 0;
1038 p = malloc(sz);
1039 if (p == NULL)
1040 return got_error_from_errno("malloc");
1042 /* base object size */
1043 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1044 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1045 for (i = 1; n > 0; i++) {
1046 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1047 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1048 n >>= GOT_DELTA_SIZE_SHIFT;
1050 err = append(&p, &len, &sz, buf, i);
1051 if (err)
1052 return err;
1054 /* target object size */
1055 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1056 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1057 for (i = 1; n > 0; i++) {
1058 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1059 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1060 n >>= GOT_DELTA_SIZE_SHIFT;
1062 err = append(&p, &len, &sz, buf, i);
1063 if (err)
1064 return err;
1065 for (j = 0; j < m->ndeltas; j++) {
1066 d = &m->deltas[j];
1067 if (d->copy) {
1068 n = d->offset;
1069 bp = &buf[1];
1070 buf[0] = GOT_DELTA_BASE_COPY;
1071 for (i = 0; i < 4; i++) {
1072 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1073 buf[0] |= 1 << i;
1074 *bp++ = n & 0xff;
1075 n >>= 8;
1076 if (n == 0)
1077 break;
1080 n = d->len;
1081 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1082 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1083 for (i = 0; i < 3 && n > 0; i++) {
1084 buf[0] |= 1 << (i + 4);
1085 *bp++ = n & 0xff;
1086 n >>= 8;
1089 err = append(&p, &len, &sz, buf, bp - buf);
1090 if (err)
1091 return err;
1092 } else {
1093 char content[128];
1094 size_t r;
1095 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1096 return got_error_from_errno("fseeko");
1097 n = 0;
1098 while (n != d->len) {
1099 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1100 err = append(&p, &len, &sz, buf, 1);
1101 if (err)
1102 return err;
1103 r = fread(content, 1, buf[0], o->f);
1104 if (r != buf[0])
1105 return got_ferror(o->f, GOT_ERR_IO);
1106 err = append(&p, &len, &sz, content, buf[0]);
1107 if (err)
1108 return err;
1109 n += buf[0];
1113 *pp = p;
1114 *nd = len;
1115 return NULL;
1118 static int
1119 packoff(char *hdr, off_t off)
1121 int i, j;
1122 char rbuf[8];
1124 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1125 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1126 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1127 GOT_DELTA_SIZE_MORE;
1130 j = 0;
1131 while (i > 0)
1132 hdr[j++] = rbuf[--i];
1133 return j;
1136 static const struct got_error *
1137 genpack(uint8_t *pack_sha1, FILE *packfile,
1138 struct got_pack_meta **meta, int nmeta, int nours,
1139 int use_offset_deltas, struct got_repository *repo,
1140 got_pack_progress_cb progress_cb, void *progress_arg,
1141 got_cancel_cb cancel_cb, void *cancel_arg)
1143 const struct got_error *err = NULL;
1144 int i, nh, nd;
1145 SHA1_CTX ctx;
1146 struct got_pack_meta *m;
1147 struct got_raw_object *raw;
1148 char *p = NULL, buf[32];
1149 size_t outlen, n;
1150 struct got_deflate_checksum csum;
1151 off_t packfile_size = 0;
1153 SHA1Init(&ctx);
1154 csum.output_sha1 = &ctx;
1155 csum.output_crc = NULL;
1157 err = hwrite(packfile, "PACK", 4, &ctx);
1158 if (err)
1159 return err;
1160 putbe32(buf, GOT_PACKFILE_VERSION);
1161 err = hwrite(packfile, buf, 4, &ctx);
1162 if (err)
1163 goto done;
1164 putbe32(buf, nmeta);
1165 err = hwrite(packfile, buf, 4, &ctx);
1166 if (err)
1167 goto done;
1168 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1169 for (i = 0; i < nmeta; i++) {
1170 if (progress_cb) {
1171 err = progress_cb(progress_arg, packfile_size, nours,
1172 nmeta, nmeta, i);
1173 if (err)
1174 goto done;
1176 m = meta[i];
1177 m->off = ftello(packfile);
1178 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1179 if (err)
1180 goto done;
1181 if (m->deltas == NULL) {
1182 err = packhdr(&nh, buf, sizeof(buf),
1183 m->obj_type, raw->size);
1184 if (err)
1185 goto done;
1186 err = hwrite(packfile, buf, nh, &ctx);
1187 if (err)
1188 goto done;
1189 packfile_size += nh;
1190 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1191 err = got_error_from_errno("fseeko");
1192 goto done;
1194 err = got_deflate_to_file(&outlen, raw->f, packfile,
1195 &csum);
1196 if (err)
1197 goto done;
1198 packfile_size += outlen;
1199 } else {
1200 FILE *delta_file;
1201 struct got_raw_object *base_raw;
1202 err = got_object_raw_open(&base_raw, repo,
1203 &m->prev->id, 8192);
1204 if (err)
1205 goto done;
1206 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1207 if (err)
1208 goto done;
1209 got_object_raw_close(base_raw);
1210 if (use_offset_deltas && m->prev->off != 0) {
1211 err = packhdr(&nh, buf, sizeof(buf),
1212 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1213 if (err)
1214 goto done;
1215 nh += packoff(buf + nh,
1216 m->off - m->prev->off);
1217 err = hwrite(packfile, buf, nh, &ctx);
1218 if (err)
1219 goto done;
1220 packfile_size += nh;
1221 } else {
1222 err = packhdr(&nh, buf, sizeof(buf),
1223 GOT_OBJ_TYPE_REF_DELTA, nd);
1224 err = hwrite(packfile, buf, nh, &ctx);
1225 if (err)
1226 goto done;
1227 packfile_size += nh;
1228 err = hwrite(packfile, m->prev->id.sha1,
1229 sizeof(m->prev->id.sha1), &ctx);
1230 packfile_size += sizeof(m->prev->id.sha1);
1231 if (err)
1232 goto done;
1234 /* XXX need got_deflate_from_mem() */
1235 delta_file = fmemopen(p, nd, "r");
1236 if (delta_file == NULL) {
1237 err = got_error_from_errno("fmemopen");
1238 goto done;
1240 err = got_deflate_to_file(&outlen, delta_file,
1241 packfile, &csum);
1242 fclose(delta_file);
1243 if (err)
1244 goto done;
1245 packfile_size += outlen;
1246 free(p);
1247 p = NULL;
1249 got_object_raw_close(raw);
1250 raw = NULL;
1252 SHA1Final(pack_sha1, &ctx);
1253 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1254 if (n != SHA1_DIGEST_LENGTH)
1255 err = got_ferror(packfile, GOT_ERR_IO);
1256 packfile_size += SHA1_DIGEST_LENGTH;
1257 packfile_size += sizeof(struct got_packfile_hdr);
1258 err = progress_cb(progress_arg, packfile_size, nours,
1259 nmeta, nmeta, nmeta);
1260 if (err)
1261 goto done;
1262 done:
1263 free(p);
1264 return err;
1267 const struct got_error *
1268 got_pack_create(uint8_t *packsha1, FILE *packfile,
1269 struct got_object_id **theirs, int ntheirs,
1270 struct got_object_id **ours, int nours,
1271 struct got_repository *repo, int loose_obj_only, int allow_empty,
1272 got_pack_progress_cb progress_cb, void *progress_arg,
1273 got_cancel_cb cancel_cb, void *cancel_arg)
1275 const struct got_error *err;
1276 struct got_pack_meta **meta;
1277 int nmeta;
1279 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1280 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1281 if (err)
1282 return err;
1284 if (nmeta == 0 && !allow_empty) {
1285 err = got_error(GOT_ERR_CANNOT_PACK);
1286 goto done;
1288 if (nmeta > 0) {
1289 err = pick_deltas(meta, nmeta, nours, repo,
1290 progress_cb, progress_arg, cancel_cb, cancel_arg);
1291 if (err)
1292 goto done;
1295 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1296 progress_cb, progress_arg, cancel_cb, cancel_arg);
1297 if (err)
1298 goto done;
1299 done:
1300 free_nmeta(meta, nmeta);
1301 return err;