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/uio.h>
21 #include <sys/stat.h>
23 #include <stdint.h>
24 #include <imsg.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sha1.h>
29 #include <limits.h>
30 #include <zlib.h>
32 #include "got_error.h"
33 #include "got_cancel.h"
34 #include "got_object.h"
35 #include "got_reference.h"
36 #include "got_repository_admin.h"
38 #include "got_lib_deltify.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_idset.h"
42 #include "got_lib_object_cache.h"
43 #include "got_lib_deflate.h"
44 #include "got_lib_pack.h"
45 #include "got_lib_privsep.h"
46 #include "got_lib_repository.h"
48 #ifndef MAX
49 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
50 #endif
52 struct got_pack_meta {
53 struct got_object_id id;
54 char *path;
55 int obj_type;
56 time_t mtime;
58 /* The best delta we picked */
59 struct got_pack_meta *head;
60 struct got_pack_meta *prev;
61 struct got_delta_instruction *deltas;
62 int ndeltas;
63 int nchain;
65 /* Only used for delta window */
66 struct got_delta_table *dtab;
68 /* Only used for writing offset deltas */
69 off_t off;
70 };
72 struct got_pack_metavec {
73 struct got_pack_meta **meta;
74 int nmeta;
75 int metasz;
76 };
78 static const struct got_error *
79 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
80 const char *path, int obj_type, time_t mtime)
81 {
82 const struct got_error *err = NULL;
83 struct got_pack_meta *m;
85 *new = NULL;
87 m = calloc(1, sizeof(*m));
88 if (m == NULL)
89 return got_error_from_errno("malloc");
91 memcpy(&m->id, id, sizeof(m->id));
93 m->path = strdup(path);
94 if (m->path == NULL) {
95 err = got_error_from_errno("strdup");
96 free(m);
97 return err;
98 }
100 m->obj_type = obj_type;
101 m->mtime = mtime;
102 *new = m;
103 return NULL;
106 static void
107 clear_meta(struct got_pack_meta *meta)
109 if (meta == NULL)
110 return;
111 free(meta->deltas);
112 meta->deltas = NULL;
113 free(meta->path);
114 meta->path = NULL;
117 static void
118 free_nmeta(struct got_pack_meta **meta, int nmeta)
120 int i;
122 for (i = 0; i < nmeta; i++)
123 clear_meta(meta[i]);
124 free(meta);
127 static int
128 delta_order_cmp(const void *pa, const void *pb)
130 struct got_pack_meta *a, *b;
131 int cmp;
133 a = *(struct got_pack_meta **)pa;
134 b = *(struct got_pack_meta **)pb;
136 if (a->obj_type != b->obj_type)
137 return a->obj_type - b->obj_type;
138 cmp = strcmp(a->path, b->path);
139 if (cmp != 0)
140 return cmp;
141 if (a->mtime != b->mtime)
142 return a->mtime - b->mtime;
143 return got_object_id_cmp(&a->id, &b->id);
146 static int
147 delta_size(struct got_delta_instruction *deltas, int ndeltas)
149 int i, size = 32;
150 for (i = 0; i < ndeltas; i++) {
151 if (deltas[i].copy)
152 size += GOT_DELTA_SIZE_SHIFT;
153 else
154 size += deltas[i].len + 1;
156 return size;
160 static const struct got_error *
161 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
162 struct got_repository *repo,
163 got_pack_progress_cb progress_cb, void *progress_arg,
164 got_cancel_cb cancel_cb, void *cancel_arg)
166 const struct got_error *err = NULL;
167 struct got_pack_meta *m = NULL, *base = NULL;
168 struct got_raw_object *raw = NULL, *base_raw = NULL;
169 struct got_delta_instruction *deltas;
170 int i, j, size, ndeltas, best;
171 const int max_base_candidates = 10;
173 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
174 for (i = 0; i < nmeta; i++) {
175 if (cancel_cb) {
176 err = (*cancel_cb)(cancel_arg);
177 if (err)
178 break;
180 if (progress_cb) {
181 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
182 if (err)
183 goto done;
185 m = meta[i];
186 m->deltas = NULL;
187 m->ndeltas = 0;
189 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
190 m->obj_type == GOT_OBJ_TYPE_TAG)
191 continue;
193 err = got_object_raw_open(&raw, repo, &m->id, 8192);
194 if (err)
195 goto done;
197 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
198 raw->size + raw->hdrlen);
199 if (err)
200 goto done;
202 if (i > max_base_candidates) {
203 struct got_pack_meta *n = NULL;
204 n = meta[i - (max_base_candidates + 1)];
205 got_deltify_free(n->dtab);
206 n->dtab = NULL;
209 best = raw->size;
210 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
211 if (cancel_cb) {
212 err = (*cancel_cb)(cancel_arg);
213 if (err)
214 goto done;
216 base = meta[j];
217 /* long chains make unpacking slow, avoid such bases */
218 if (base->nchain >= 128 ||
219 base->obj_type != m->obj_type)
220 continue;
222 err = got_object_raw_open(&base_raw, repo, &base->id,
223 8192);
224 if (err)
225 goto done;
226 err = got_deltify(&deltas, &ndeltas,
227 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
228 base->dtab, base_raw->f, base_raw->hdrlen,
229 base_raw->size + base_raw->hdrlen);
230 got_object_raw_close(base_raw);
231 base_raw = NULL;
232 if (err)
233 goto done;
235 size = delta_size(deltas, ndeltas);
236 if (size + 32 < best){
237 /*
238 * if we already picked a best delta,
239 * replace it.
240 */
241 free(m->deltas);
242 best = size;
243 m->deltas = deltas;
244 m->ndeltas = ndeltas;
245 m->nchain = base->nchain + 1;
246 m->prev = base;
247 m->head = base->head;
248 if (m->head == NULL)
249 m->head = base;
250 } else {
251 free(deltas);
252 deltas = NULL;
253 ndeltas = 0;
257 got_object_raw_close(raw);
258 raw = NULL;
260 done:
261 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
262 got_deltify_free(meta[i]->dtab);
263 meta[i]->dtab = NULL;
265 if (raw)
266 got_object_raw_close(raw);
267 if (base_raw)
268 got_object_raw_close(base_raw);
269 return err;
272 static const struct got_error *
273 search_packidx(int *found, struct got_object_id *id,
274 struct got_repository *repo)
276 const struct got_error *err = NULL;
277 struct got_packidx *packidx = NULL;
278 int idx;
280 *found = 0;
282 err = got_repo_search_packidx(&packidx, &idx, repo, id);
283 if (err == NULL)
284 *found = 1; /* object is already packed */
285 else if (err->code == GOT_ERR_NO_OBJ)
286 err = NULL;
287 return err;
290 static const int obj_types[] = {
291 GOT_OBJ_TYPE_ANY,
292 GOT_OBJ_TYPE_COMMIT,
293 GOT_OBJ_TYPE_TREE,
294 GOT_OBJ_TYPE_BLOB,
295 GOT_OBJ_TYPE_TAG,
296 GOT_OBJ_TYPE_OFFSET_DELTA,
297 GOT_OBJ_TYPE_REF_DELTA
298 };
300 static const struct got_error *
301 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
302 struct got_object_id *id, const char *path, int obj_type,
303 time_t mtime, int loose_obj_only, struct got_repository *repo)
305 const struct got_error *err;
306 struct got_pack_meta *m;
308 if (loose_obj_only) {
309 int is_packed;
310 err = search_packidx(&is_packed, id, repo);
311 if (err)
312 return err;
313 if (is_packed)
314 return NULL;
317 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
318 if (err)
319 return err;
321 if (v == NULL)
322 return NULL;
324 err = alloc_meta(&m, id, path, obj_type, mtime);
325 if (err)
326 goto done;
328 if (v->nmeta == v->metasz){
329 size_t newsize = 2 * v->metasz;
330 struct got_pack_meta **new;
331 new = reallocarray(v->meta, newsize, sizeof(*new));
332 if (new == NULL) {
333 err = got_error_from_errno("reallocarray");
334 goto done;
336 v->meta = new;
337 v->metasz = newsize;
339 done:
340 if (err) {
341 clear_meta(m);
342 free(m);
343 } else
344 v->meta[v->nmeta++] = m;
346 return err;
349 static const struct got_error *
350 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
351 struct got_object_idset *idset, struct got_object_id *tree_id,
352 const char *dpath, time_t mtime, struct got_repository *repo,
353 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
355 const struct got_error *err;
356 struct got_tree_object *tree;
357 char *p = NULL;
358 int i;
360 err = got_object_open_as_tree(&tree, repo, tree_id);
361 if (err)
362 return err;
364 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
365 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
366 struct got_object_id *id = got_tree_entry_get_id(e);
367 mode_t mode = got_tree_entry_get_mode(e);
369 if (cancel_cb) {
370 err = (*cancel_cb)(cancel_arg);
371 if (err)
372 break;
375 if (got_object_tree_entry_is_submodule(e) ||
376 got_object_idset_contains(idset, id))
377 continue;
379 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
380 got_tree_entry_get_name(e)) == -1) {
381 err = got_error_from_errno("asprintf");
382 break;
385 if (S_ISDIR(mode)) {
386 struct got_object_qid *qid;
387 err = got_object_qid_alloc(&qid, id);
388 if (err)
389 break;
390 STAILQ_INSERT_TAIL(ids, qid, entry);
391 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
392 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
393 mtime, loose_obj_only, repo);
394 if (err)
395 break;
397 free(p);
398 p = NULL;
401 got_object_tree_close(tree);
402 free(p);
403 return err;
406 static const struct got_error *
407 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
408 struct got_object_id *tree_id, const char *dpath, time_t mtime,
409 int loose_obj_only, struct got_repository *repo,
410 got_cancel_cb cancel_cb, void *cancel_arg)
412 const struct got_error *err = NULL;
413 struct got_object_id_queue tree_ids;
414 struct got_object_qid *qid;
416 if (got_object_idset_contains(idset, tree_id))
417 return NULL;
419 err = got_object_qid_alloc(&qid, tree_id);
420 if (err)
421 return err;
423 STAILQ_INIT(&tree_ids);
424 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
426 while (!STAILQ_EMPTY(&tree_ids)) {
427 if (cancel_cb) {
428 err = (*cancel_cb)(cancel_arg);
429 if (err)
430 break;
433 qid = STAILQ_FIRST(&tree_ids);
434 STAILQ_REMOVE_HEAD(&tree_ids, entry);
436 if (got_object_idset_contains(idset, qid->id)) {
437 got_object_qid_free(qid);
438 continue;
441 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
442 mtime, loose_obj_only, repo);
443 if (err) {
444 got_object_qid_free(qid);
445 break;
448 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
449 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
450 got_object_qid_free(qid);
451 if (err)
452 break;
455 got_object_id_queue_free(&tree_ids);
456 return err;
459 static const struct got_error *
460 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
461 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
462 got_cancel_cb cancel_cb, void *cancel_arg)
464 const struct got_error *err;
465 struct got_commit_object *commit;
467 if (got_object_idset_contains(idset, id))
468 return NULL;
470 if (loose_obj_only) {
471 int is_packed;
472 err = search_packidx(&is_packed, id, repo);
473 if (err)
474 return err;
475 if (is_packed)
476 return NULL;
479 err = got_object_open_as_commit(&commit, repo, id);
480 if (err)
481 return err;
483 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
484 got_object_commit_get_committer_time(commit),
485 loose_obj_only, repo);
486 if (err)
487 goto done;
489 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
490 "", got_object_commit_get_committer_time(commit),
491 loose_obj_only, repo, cancel_cb, cancel_arg);
492 done:
493 got_object_commit_close(commit);
494 return err;
497 static const struct got_error *
498 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
499 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
500 got_cancel_cb cancel_cb, void *cancel_arg)
502 const struct got_error *err;
503 struct got_tag_object *tag = NULL;
505 if (got_object_idset_contains(idset, id))
506 return NULL;
508 if (loose_obj_only) {
509 int is_packed;
510 err = search_packidx(&is_packed, id, repo);
511 if (err)
512 return err;
513 if (is_packed)
514 return NULL;
517 err = got_object_open_as_tag(&tag, repo, id);
518 if (err)
519 return err;
521 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
522 got_object_tag_get_tagger_time(tag),
523 loose_obj_only, repo);
524 if (err)
525 goto done;
527 switch (got_object_tag_get_object_type(tag)) {
528 case GOT_OBJ_TYPE_COMMIT:
529 err = load_commit(v, idset,
530 got_object_tag_get_object_id(tag), repo,
531 loose_obj_only, cancel_cb, cancel_arg);
532 break;
533 case GOT_OBJ_TYPE_TREE:
534 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
535 "", got_object_tag_get_tagger_time(tag),
536 loose_obj_only, repo, cancel_cb, cancel_arg);
537 break;
538 default:
539 break;
542 done:
543 got_object_tag_close(tag);
544 return err;
547 enum findtwixt_color {
548 COLOR_KEEP = 0,
549 COLOR_DROP,
550 COLOR_BLANK,
551 };
552 static const int findtwixt_colors[] = {
553 COLOR_KEEP,
554 COLOR_DROP,
555 COLOR_BLANK
556 };
558 static const struct got_error *
559 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
560 int color, struct got_repository *repo)
562 const struct got_error *err;
563 struct got_object_qid *qid;
565 err = got_object_qid_alloc(&qid, id);
566 if (err)
567 return err;
569 STAILQ_INSERT_TAIL(ids, qid, entry);
570 qid->data = (void *)&findtwixt_colors[color];
571 return NULL;
574 static const struct got_error *
575 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
576 struct got_object_id *id, struct got_repository *repo,
577 got_cancel_cb cancel_cb, void *cancel_arg)
579 const struct got_error *err = NULL;
580 struct got_commit_object *commit;
581 const struct got_object_id_queue *parents;
582 struct got_object_id_queue ids;
583 struct got_object_qid *qid;
585 STAILQ_INIT(&ids);
587 err = got_object_qid_alloc(&qid, id);
588 if (err)
589 return err;
590 STAILQ_INSERT_HEAD(&ids, qid, entry);
592 while (!STAILQ_EMPTY(&ids)) {
593 if (cancel_cb) {
594 err = (*cancel_cb)(cancel_arg);
595 if (err)
596 break;
599 qid = STAILQ_FIRST(&ids);
600 STAILQ_REMOVE_HEAD(&ids, entry);
602 if (got_object_idset_contains(drop, qid->id)) {
603 got_object_qid_free(qid);
604 continue;
607 err = got_object_idset_add(drop, qid->id,
608 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
609 if (err) {
610 got_object_qid_free(qid);
611 break;
614 if (!got_object_idset_contains(keep, qid->id)) {
615 got_object_qid_free(qid);
616 continue;
619 err = got_object_open_as_commit(&commit, repo, qid->id);
620 got_object_qid_free(qid);
621 if (err)
622 break;
624 parents = got_object_commit_get_parent_ids(commit);
625 if (parents) {
626 err = got_object_id_queue_copy(parents, &ids);
627 if (err) {
628 got_object_commit_close(commit);
629 break;
632 got_object_commit_close(commit);
635 got_object_id_queue_free(&ids);
636 return err;
639 struct append_id_arg {
640 struct got_object_id **array;
641 int idx;
642 };
644 static const struct got_error *
645 append_id(struct got_object_id *id, void *data, void *arg)
647 struct append_id_arg *a = arg;
649 a->array[a->idx] = got_object_id_dup(id);
650 if (a->array[a->idx] == NULL)
651 return got_error_from_errno("got_object_id_dup");
653 a->idx++;
654 return NULL;
657 static const struct got_error *
658 findtwixt(struct got_object_id ***res, int *nres,
659 struct got_object_id **head, int nhead,
660 struct got_object_id **tail, int ntail,
661 struct got_repository *repo,
662 got_cancel_cb cancel_cb, void *cancel_arg)
664 const struct got_error *err = NULL;
665 struct got_object_id_queue ids;
666 struct got_object_idset *keep, *drop;
667 struct got_object_qid *qid;
668 int i, ncolor, nkeep, obj_type;
670 STAILQ_INIT(&ids);
671 *res = NULL;
672 *nres = 0;
674 keep = got_object_idset_alloc();
675 if (keep == NULL)
676 return got_error_from_errno("got_object_idset_alloc");
678 drop = got_object_idset_alloc();
679 if (drop == NULL) {
680 err = got_error_from_errno("got_object_idset_alloc");
681 goto done;
684 for (i = 0; i < nhead; i++) {
685 struct got_object_id *id = head[i];
686 if (id == NULL)
687 continue;
688 err = got_object_get_type(&obj_type, repo, id);
689 if (err)
690 return err;
691 if (obj_type != GOT_OBJ_TYPE_COMMIT)
692 continue;
693 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
694 if (err)
695 goto done;
697 for (i = 0; i < ntail; i++) {
698 struct got_object_id *id = tail[i];
699 if (id == NULL)
700 continue;
701 err = got_object_get_type(&obj_type, repo, id);
702 if (err)
703 return err;
704 if (obj_type != GOT_OBJ_TYPE_COMMIT)
705 continue;
706 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
707 if (err)
708 goto done;
711 while (!STAILQ_EMPTY(&ids)) {
712 int qcolor;
713 qid = STAILQ_FIRST(&ids);
714 qcolor = *((int *)qid->data);
716 if (got_object_idset_contains(drop, qid->id))
717 ncolor = COLOR_DROP;
718 else if (got_object_idset_contains(keep, qid->id))
719 ncolor = COLOR_KEEP;
720 else
721 ncolor = COLOR_BLANK;
723 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
724 qcolor == COLOR_KEEP)) {
725 STAILQ_REMOVE_HEAD(&ids, entry);
726 got_object_qid_free(qid);
727 continue;
730 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
731 err = drop_commit(keep, drop, qid->id, repo,
732 cancel_cb, cancel_arg);
733 if (err)
734 goto done;
735 } else if (ncolor == COLOR_BLANK) {
736 struct got_commit_object *commit;
737 struct got_object_id *id;
738 const struct got_object_id_queue *parents;
739 struct got_object_qid *pid;
741 id = got_object_id_dup(qid->id);
742 if (id == NULL) {
743 err = got_error_from_errno("got_object_id_dup");
744 goto done;
746 if (qcolor == COLOR_KEEP)
747 err = got_object_idset_add(keep, id,
748 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
749 else
750 err = got_object_idset_add(drop, id,
751 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
752 if (err) {
753 free(id);
754 goto done;
757 err = got_object_open_as_commit(&commit, repo, id);
758 if (err) {
759 free(id);
760 goto done;
762 parents = got_object_commit_get_parent_ids(commit);
763 if (parents) {
764 STAILQ_FOREACH(pid, parents, entry) {
765 err = queue_commit_id(&ids, pid->id,
766 qcolor, repo);
767 if (err) {
768 free(id);
769 goto done;
773 got_object_commit_close(commit);
774 commit = NULL;
775 } else {
776 /* should not happen */
777 err = got_error_fmt(GOT_ERR_NOT_IMPL,
778 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
779 goto done;
782 STAILQ_REMOVE_HEAD(&ids, entry);
783 got_object_qid_free(qid);
786 nkeep = got_object_idset_num_elements(keep);
787 if (nkeep > 0) {
788 struct append_id_arg arg;
789 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
790 if (arg.array == NULL) {
791 err = got_error_from_errno("calloc");
792 goto done;
794 arg.idx = 0;
795 err = got_object_idset_for_each(keep, append_id, &arg);
796 if (err) {
797 free(arg.array);
798 goto done;
800 *res = arg.array;
801 *nres = nkeep;
803 done:
804 got_object_idset_free(keep);
805 got_object_idset_free(drop);
806 got_object_id_queue_free(&ids);
807 return err;
810 static const struct got_error *
811 read_meta(struct got_pack_meta ***meta, int *nmeta,
812 struct got_object_id **theirs, int ntheirs,
813 struct got_object_id **ours, int nours, struct got_repository *repo,
814 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
815 got_cancel_cb cancel_cb, void *cancel_arg)
817 const struct got_error *err = NULL;
818 struct got_object_id **ids = NULL;
819 struct got_object_idset *idset;
820 int i, nobj = 0, obj_type;
821 struct got_pack_metavec v;
823 *meta = NULL;
824 *nmeta = 0;
826 idset = got_object_idset_alloc();
827 if (idset == NULL)
828 return got_error_from_errno("got_object_idset_alloc");
830 v.nmeta = 0;
831 v.metasz = 64;
832 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
833 if (v.meta == NULL) {
834 err = got_error_from_errno("reallocarray");
835 goto done;
838 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
839 cancel_cb, cancel_arg);
840 if (err || nobj == 0)
841 goto done;
843 for (i = 0; i < ntheirs; i++) {
844 struct got_object_id *id = theirs[i];
845 if (id == NULL)
846 continue;
847 err = got_object_get_type(&obj_type, repo, id);
848 if (err)
849 return err;
850 if (obj_type != GOT_OBJ_TYPE_COMMIT)
851 continue;
852 err = load_commit(NULL, idset, id, repo,
853 loose_obj_only, cancel_cb, cancel_arg);
854 if (err)
855 goto done;
856 if (progress_cb) {
857 err = progress_cb(progress_arg, 0L, nours,
858 v.nmeta, 0, 0);
859 if (err)
860 goto done;
864 for (i = 0; i < ntheirs; i++) {
865 struct got_object_id *id = theirs[i];
866 int *cached_type;
867 if (id == NULL)
868 continue;
869 cached_type = got_object_idset_get(idset, id);
870 if (cached_type == NULL) {
871 err = got_object_get_type(&obj_type, repo, id);
872 if (err)
873 goto done;
874 } else
875 obj_type = *cached_type;
876 if (obj_type != GOT_OBJ_TYPE_TAG)
877 continue;
878 err = load_tag(NULL, idset, id, repo,
879 loose_obj_only, cancel_cb, cancel_arg);
880 if (err)
881 goto done;
882 if (progress_cb) {
883 err = progress_cb(progress_arg, 0L, nours,
884 v.nmeta, 0, 0);
885 if (err)
886 goto done;
890 for (i = 0; i < nobj; i++) {
891 err = load_commit(&v, idset, ids[i], repo,
892 loose_obj_only, cancel_cb, cancel_arg);
893 if (err)
894 goto done;
895 if (progress_cb) {
896 err = progress_cb(progress_arg, 0L, nours,
897 v.nmeta, 0, 0);
898 if (err)
899 goto done;
903 for (i = 0; i < nours; i++) {
904 struct got_object_id *id = ours[i];
905 int *cached_type;
906 if (id == NULL)
907 continue;
908 cached_type = got_object_idset_get(idset, id);
909 if (cached_type == NULL) {
910 err = got_object_get_type(&obj_type, repo, id);
911 if (err)
912 goto done;
913 } else
914 obj_type = *cached_type;
915 if (obj_type != GOT_OBJ_TYPE_TAG)
916 continue;
917 err = load_tag(&v, idset, id, repo,
918 loose_obj_only, cancel_cb, cancel_arg);
919 if (err)
920 goto done;
921 if (progress_cb) {
922 err = progress_cb(progress_arg, 0L, nours,
923 v.nmeta, 0, 0);
924 if (err)
925 goto done;
929 done:
930 for (i = 0; i < nobj; i++) {
931 free(ids[i]);
933 free(ids);
934 got_object_idset_free(idset);
935 if (err == NULL) {
936 *meta = v.meta;
937 *nmeta = v.nmeta;
938 } else
939 free(v.meta);
941 return err;
944 const struct got_error *
945 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
947 size_t n;
949 SHA1Update(ctx, buf, len);
950 n = fwrite(buf, 1, len, f);
951 if (n != len)
952 return got_ferror(f, GOT_ERR_IO);
953 return NULL;
956 static void
957 putbe32(char *b, uint32_t n)
959 b[0] = n >> 24;
960 b[1] = n >> 16;
961 b[2] = n >> 8;
962 b[3] = n >> 0;
965 static int
966 write_order_cmp(const void *pa, const void *pb)
968 struct got_pack_meta *a, *b, *ahd, *bhd;
970 a = *(struct got_pack_meta **)pa;
971 b = *(struct got_pack_meta **)pb;
972 ahd = (a->head == NULL) ? a : a->head;
973 bhd = (b->head == NULL) ? b : b->head;
974 if (ahd->mtime != bhd->mtime)
975 return bhd->mtime - ahd->mtime;
976 if (ahd != bhd)
977 return (uintptr_t)bhd - (uintptr_t)ahd;
978 if (a->nchain != b->nchain)
979 return a->nchain - b->nchain;
980 return a->mtime - b->mtime;
983 static const struct got_error *
984 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
986 size_t i;
988 *hdrlen = 0;
990 hdr[0] = obj_type << 4;
991 hdr[0] |= len & 0xf;
992 len >>= 4;
993 for (i = 1; len != 0; i++){
994 if (i >= bufsize)
995 return got_error(GOT_ERR_NO_SPACE);
996 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
997 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
998 len >>= GOT_DELTA_SIZE_SHIFT;
1001 *hdrlen = i;
1002 return NULL;
1005 static const struct got_error *
1006 append(char **p, int *len, int *sz, void *seg, int nseg)
1008 char *n;
1010 if (*len + nseg >= *sz) {
1011 while (*len + nseg >= *sz)
1012 *sz += *sz / 2;
1013 n = realloc(*p, *sz);
1014 if (n == NULL)
1015 return got_error_from_errno("realloc");
1016 *p = n;
1018 memcpy(*p + *len, seg, nseg);
1019 *len += nseg;
1020 return NULL;
1024 static const struct got_error *
1025 encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1026 off_t base_size, char **pp)
1028 const struct got_error *err = NULL;
1029 char *p;
1030 unsigned char buf[16], *bp;
1031 int len, sz, i, j;
1032 off_t n;
1033 struct got_delta_instruction *d;
1035 *pp = NULL;
1036 *nd = 0;
1038 sz = 128;
1039 len = 0;
1040 p = malloc(sz);
1041 if (p == NULL)
1042 return got_error_from_errno("malloc");
1044 /* base object size */
1045 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1046 n = base_size >> GOT_DELTA_SIZE_SHIFT;
1047 for (i = 1; n > 0; i++) {
1048 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1049 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1050 n >>= GOT_DELTA_SIZE_SHIFT;
1052 err = append(&p, &len, &sz, buf, i);
1053 if (err)
1054 return err;
1056 /* target object size */
1057 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1058 n = o->size >> GOT_DELTA_SIZE_SHIFT;
1059 for (i = 1; n > 0; i++) {
1060 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1061 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1062 n >>= GOT_DELTA_SIZE_SHIFT;
1064 err = append(&p, &len, &sz, buf, i);
1065 if (err)
1066 return err;
1067 for (j = 0; j < m->ndeltas; j++) {
1068 d = &m->deltas[j];
1069 if (d->copy) {
1070 n = d->offset;
1071 bp = &buf[1];
1072 buf[0] = GOT_DELTA_BASE_COPY;
1073 for (i = 0; i < 4; i++) {
1074 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1075 buf[0] |= 1 << i;
1076 *bp++ = n & 0xff;
1077 n >>= 8;
1078 if (n == 0)
1079 break;
1082 n = d->len;
1083 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1084 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1085 for (i = 0; i < 3 && n > 0; i++) {
1086 buf[0] |= 1 << (i + 4);
1087 *bp++ = n & 0xff;
1088 n >>= 8;
1091 err = append(&p, &len, &sz, buf, bp - buf);
1092 if (err)
1093 return err;
1094 } else {
1095 char content[128];
1096 size_t r;
1097 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1098 return got_error_from_errno("fseeko");
1099 n = 0;
1100 while (n != d->len) {
1101 buf[0] = (d->len - n < 127) ? d->len - n : 127;
1102 err = append(&p, &len, &sz, buf, 1);
1103 if (err)
1104 return err;
1105 r = fread(content, 1, buf[0], o->f);
1106 if (r != buf[0])
1107 return got_ferror(o->f, GOT_ERR_IO);
1108 err = append(&p, &len, &sz, content, buf[0]);
1109 if (err)
1110 return err;
1111 n += buf[0];
1115 *pp = p;
1116 *nd = len;
1117 return NULL;
1120 static int
1121 packoff(char *hdr, off_t off)
1123 int i, j;
1124 char rbuf[8];
1126 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1127 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1128 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1129 GOT_DELTA_SIZE_MORE;
1132 j = 0;
1133 while (i > 0)
1134 hdr[j++] = rbuf[--i];
1135 return j;
1138 static const struct got_error *
1139 genpack(uint8_t *pack_sha1, FILE *packfile,
1140 struct got_pack_meta **meta, int nmeta, int nours,
1141 int use_offset_deltas, struct got_repository *repo,
1142 got_pack_progress_cb progress_cb, void *progress_arg,
1143 got_cancel_cb cancel_cb, void *cancel_arg)
1145 const struct got_error *err = NULL;
1146 int i, nh, nd;
1147 SHA1_CTX ctx;
1148 struct got_pack_meta *m;
1149 struct got_raw_object *raw;
1150 char *p = NULL, buf[32];
1151 size_t outlen, n;
1152 struct got_deflate_checksum csum;
1153 off_t packfile_size = 0;
1155 SHA1Init(&ctx);
1156 csum.output_sha1 = &ctx;
1157 csum.output_crc = NULL;
1159 err = hwrite(packfile, "PACK", 4, &ctx);
1160 if (err)
1161 return err;
1162 putbe32(buf, GOT_PACKFILE_VERSION);
1163 err = hwrite(packfile, buf, 4, &ctx);
1164 if (err)
1165 goto done;
1166 putbe32(buf, nmeta);
1167 err = hwrite(packfile, buf, 4, &ctx);
1168 if (err)
1169 goto done;
1170 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1171 for (i = 0; i < nmeta; i++) {
1172 if (progress_cb) {
1173 err = progress_cb(progress_arg, packfile_size, nours,
1174 nmeta, nmeta, i);
1175 if (err)
1176 goto done;
1178 m = meta[i];
1179 m->off = ftello(packfile);
1180 err = got_object_raw_open(&raw, repo, &m->id, 8192);
1181 if (err)
1182 goto done;
1183 if (m->deltas == NULL) {
1184 err = packhdr(&nh, buf, sizeof(buf),
1185 m->obj_type, raw->size);
1186 if (err)
1187 goto done;
1188 err = hwrite(packfile, buf, nh, &ctx);
1189 if (err)
1190 goto done;
1191 packfile_size += nh;
1192 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1193 err = got_error_from_errno("fseeko");
1194 goto done;
1196 err = got_deflate_to_file(&outlen, raw->f, packfile,
1197 &csum);
1198 if (err)
1199 goto done;
1200 packfile_size += outlen;
1201 } else {
1202 FILE *delta_file;
1203 struct got_raw_object *base_raw;
1204 err = got_object_raw_open(&base_raw, repo,
1205 &m->prev->id, 8192);
1206 if (err)
1207 goto done;
1208 err = encodedelta(&nd, m, raw, base_raw->size, &p);
1209 if (err)
1210 goto done;
1211 got_object_raw_close(base_raw);
1212 if (use_offset_deltas && m->prev->off != 0) {
1213 err = packhdr(&nh, buf, sizeof(buf),
1214 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1215 if (err)
1216 goto done;
1217 nh += packoff(buf + nh,
1218 m->off - m->prev->off);
1219 err = hwrite(packfile, buf, nh, &ctx);
1220 if (err)
1221 goto done;
1222 packfile_size += nh;
1223 } else {
1224 err = packhdr(&nh, buf, sizeof(buf),
1225 GOT_OBJ_TYPE_REF_DELTA, nd);
1226 err = hwrite(packfile, buf, nh, &ctx);
1227 if (err)
1228 goto done;
1229 packfile_size += nh;
1230 err = hwrite(packfile, m->prev->id.sha1,
1231 sizeof(m->prev->id.sha1), &ctx);
1232 packfile_size += sizeof(m->prev->id.sha1);
1233 if (err)
1234 goto done;
1236 /* XXX need got_deflate_from_mem() */
1237 delta_file = fmemopen(p, nd, "r");
1238 if (delta_file == NULL) {
1239 err = got_error_from_errno("fmemopen");
1240 goto done;
1242 err = got_deflate_to_file(&outlen, delta_file,
1243 packfile, &csum);
1244 fclose(delta_file);
1245 if (err)
1246 goto done;
1247 packfile_size += outlen;
1248 free(p);
1249 p = NULL;
1251 got_object_raw_close(raw);
1252 raw = NULL;
1254 SHA1Final(pack_sha1, &ctx);
1255 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1256 if (n != SHA1_DIGEST_LENGTH)
1257 err = got_ferror(packfile, GOT_ERR_IO);
1258 packfile_size += SHA1_DIGEST_LENGTH;
1259 packfile_size += sizeof(struct got_packfile_hdr);
1260 err = progress_cb(progress_arg, packfile_size, nours,
1261 nmeta, nmeta, nmeta);
1262 if (err)
1263 goto done;
1264 done:
1265 free(p);
1266 return err;
1269 const struct got_error *
1270 got_pack_create(uint8_t *packsha1, FILE *packfile,
1271 struct got_object_id **theirs, int ntheirs,
1272 struct got_object_id **ours, int nours,
1273 struct got_repository *repo, int loose_obj_only, int allow_empty,
1274 got_pack_progress_cb progress_cb, void *progress_arg,
1275 got_cancel_cb cancel_cb, void *cancel_arg)
1277 const struct got_error *err;
1278 struct got_pack_meta **meta;
1279 int nmeta;
1281 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1282 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1283 if (err)
1284 return err;
1286 if (nmeta == 0 && !allow_empty) {
1287 err = got_error(GOT_ERR_CANNOT_PACK);
1288 goto done;
1290 if (nmeta > 0) {
1291 err = pick_deltas(meta, nmeta, nours, repo,
1292 progress_cb, progress_arg, cancel_cb, cancel_arg);
1293 if (err)
1294 goto done;
1297 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1298 progress_cb, progress_arg, cancel_cb, cancel_arg);
1299 if (err)
1300 goto done;
1301 done:
1302 free_nmeta(meta, nmeta);
1303 return err;