Blob


1 /*
2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/stat.h>
24 #include <stdint.h>
25 #include <imsg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sha1.h>
30 #include <limits.h>
31 #include <zlib.h>
33 #include "got_error.h"
34 #include "got_cancel.h"
35 #include "got_object.h"
36 #include "got_path.h"
37 #include "got_reference.h"
38 #include "got_repository_admin.h"
39 #include "got_opentemp.h"
41 #include "got_lib_deltify.h"
42 #include "got_lib_delta.h"
43 #include "got_lib_object.h"
44 #include "got_lib_object_idset.h"
45 #include "got_lib_object_cache.h"
46 #include "got_lib_deflate.h"
47 #include "got_lib_pack.h"
48 #include "got_lib_privsep.h"
49 #include "got_lib_repository.h"
51 #ifndef MAX
52 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
53 #endif
55 struct got_pack_meta {
56 struct got_object_id id;
57 char *path;
58 int obj_type;
59 off_t size;
60 time_t mtime;
62 /* The best delta we picked */
63 struct got_pack_meta *head;
64 struct got_pack_meta *prev;
65 struct got_delta_instruction *deltas;
66 int ndeltas;
67 int nchain;
69 /* Only used for delta window */
70 struct got_delta_table *dtab;
72 /* Only used for writing offset deltas */
73 off_t off;
74 };
76 struct got_pack_metavec {
77 struct got_pack_meta **meta;
78 int nmeta;
79 int metasz;
80 };
82 static const struct got_error *
83 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
84 const char *path, int obj_type, time_t mtime)
85 {
86 const struct got_error *err = NULL;
87 struct got_pack_meta *m;
89 *new = NULL;
91 m = calloc(1, sizeof(*m));
92 if (m == NULL)
93 return got_error_from_errno("calloc");
95 memcpy(&m->id, id, sizeof(m->id));
97 m->path = strdup(path);
98 if (m->path == NULL) {
99 err = got_error_from_errno("strdup");
100 free(m);
101 return err;
104 m->obj_type = obj_type;
105 m->mtime = mtime;
106 *new = m;
107 return NULL;
110 static void
111 clear_meta(struct got_pack_meta *meta)
113 if (meta == NULL)
114 return;
115 free(meta->deltas);
116 meta->deltas = NULL;
117 free(meta->path);
118 meta->path = NULL;
121 static void
122 free_nmeta(struct got_pack_meta **meta, int nmeta)
124 int i;
126 for (i = 0; i < nmeta; i++)
127 clear_meta(meta[i]);
128 free(meta);
131 static int
132 delta_order_cmp(const void *pa, const void *pb)
134 struct got_pack_meta *a, *b;
135 int cmp;
137 a = *(struct got_pack_meta **)pa;
138 b = *(struct got_pack_meta **)pb;
140 if (a->obj_type != b->obj_type)
141 return a->obj_type - b->obj_type;
142 cmp = strcmp(a->path, b->path);
143 if (cmp != 0)
144 return cmp;
145 if (a->mtime != b->mtime)
146 return a->mtime - b->mtime;
147 return got_object_id_cmp(&a->id, &b->id);
150 static int
151 delta_size(struct got_delta_instruction *deltas, int ndeltas)
153 int i, size = 32;
154 for (i = 0; i < ndeltas; i++) {
155 if (deltas[i].copy)
156 size += GOT_DELTA_SIZE_SHIFT;
157 else
158 size += deltas[i].len + 1;
160 return size;
164 static const struct got_error *
165 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
166 struct got_repository *repo,
167 got_pack_progress_cb progress_cb, void *progress_arg,
168 got_cancel_cb cancel_cb, void *cancel_arg)
170 const struct got_error *err = NULL;
171 struct got_pack_meta *m = NULL, *base = NULL;
172 struct got_raw_object *raw = NULL, *base_raw = NULL;
173 struct got_delta_instruction *deltas;
174 int i, j, size, ndeltas, best;
175 const int max_base_candidates = 10;
176 int outfd = -1;
178 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
179 for (i = 0; i < nmeta; i++) {
180 if (cancel_cb) {
181 err = (*cancel_cb)(cancel_arg);
182 if (err)
183 break;
185 if (progress_cb) {
186 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
187 if (err)
188 goto done;
190 m = meta[i];
191 m->deltas = NULL;
192 m->ndeltas = 0;
194 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
195 m->obj_type == GOT_OBJ_TYPE_TAG)
196 continue;
198 if (outfd == -1) {
199 outfd = got_opentempfd();
200 if (outfd == -1) {
201 err = got_error_from_errno("got_opentempfd");
202 goto done;
205 err = got_object_raw_open(&raw, outfd, repo, &m->id, 8192);
206 if (err)
207 goto done;
208 if (raw->data == NULL)
209 outfd = -1; /* outfd is now raw->f */
210 m->size = raw->size;
212 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
213 raw->size + raw->hdrlen);
214 if (err)
215 goto done;
217 if (i > max_base_candidates) {
218 struct got_pack_meta *n = NULL;
219 n = meta[i - (max_base_candidates + 1)];
220 got_deltify_free(n->dtab);
221 n->dtab = NULL;
224 best = raw->size;
225 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
226 if (cancel_cb) {
227 err = (*cancel_cb)(cancel_arg);
228 if (err)
229 goto done;
231 base = meta[j];
232 /* long chains make unpacking slow, avoid such bases */
233 if (base->nchain >= 128 ||
234 base->obj_type != m->obj_type)
235 continue;
237 if (outfd == -1) {
238 outfd = got_opentempfd();
239 if (outfd == -1) {
240 err = got_error_from_errno(
241 "got_opentempfd");
242 goto done;
245 err = got_object_raw_open(&base_raw, outfd, repo,
246 &base->id, 8192);
247 if (err)
248 goto done;
249 if (base_raw->data == NULL)
250 outfd = -1; /* outfd is now base_raw->f */
251 err = got_deltify(&deltas, &ndeltas,
252 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
253 base->dtab, base_raw->f, base_raw->hdrlen,
254 base_raw->size + base_raw->hdrlen);
255 got_object_raw_close(base_raw);
256 base_raw = NULL;
257 if (err)
258 goto done;
260 size = delta_size(deltas, ndeltas);
261 if (size + 32 < best){
262 /*
263 * if we already picked a best delta,
264 * replace it.
265 */
266 free(m->deltas);
267 best = size;
268 m->deltas = deltas;
269 m->ndeltas = ndeltas;
270 m->nchain = base->nchain + 1;
271 m->prev = base;
272 m->head = base->head;
273 if (m->head == NULL)
274 m->head = base;
275 } else {
276 free(deltas);
277 deltas = NULL;
278 ndeltas = 0;
282 got_object_raw_close(raw);
283 raw = NULL;
285 done:
286 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
287 got_deltify_free(meta[i]->dtab);
288 meta[i]->dtab = NULL;
290 if (raw)
291 got_object_raw_close(raw);
292 if (base_raw)
293 got_object_raw_close(base_raw);
294 if (outfd != -1 && close(outfd) == -1 && err == NULL)
295 err = got_error_from_errno("close");
296 return err;
299 static const struct got_error *
300 search_packidx(int *found, struct got_object_id *id,
301 struct got_repository *repo)
303 const struct got_error *err = NULL;
304 struct got_packidx *packidx = NULL;
305 int idx;
307 *found = 0;
309 err = got_repo_search_packidx(&packidx, &idx, repo, id);
310 if (err == NULL)
311 *found = 1; /* object is already packed */
312 else if (err->code == GOT_ERR_NO_OBJ)
313 err = NULL;
314 return err;
317 static const int obj_types[] = {
318 GOT_OBJ_TYPE_ANY,
319 GOT_OBJ_TYPE_COMMIT,
320 GOT_OBJ_TYPE_TREE,
321 GOT_OBJ_TYPE_BLOB,
322 GOT_OBJ_TYPE_TAG,
323 GOT_OBJ_TYPE_OFFSET_DELTA,
324 GOT_OBJ_TYPE_REF_DELTA
325 };
327 static const struct got_error *
328 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
329 struct got_object_id *id, const char *path, int obj_type,
330 time_t mtime, int loose_obj_only, struct got_repository *repo)
332 const struct got_error *err;
333 struct got_pack_meta *m;
335 if (loose_obj_only) {
336 int is_packed;
337 err = search_packidx(&is_packed, id, repo);
338 if (err)
339 return err;
340 if (is_packed)
341 return NULL;
344 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
345 if (err)
346 return err;
348 if (v == NULL)
349 return NULL;
351 err = alloc_meta(&m, id, path, obj_type, mtime);
352 if (err)
353 goto done;
355 if (v->nmeta == v->metasz){
356 size_t newsize = 2 * v->metasz;
357 struct got_pack_meta **new;
358 new = reallocarray(v->meta, newsize, sizeof(*new));
359 if (new == NULL) {
360 err = got_error_from_errno("reallocarray");
361 goto done;
363 v->meta = new;
364 v->metasz = newsize;
366 done:
367 if (err) {
368 clear_meta(m);
369 free(m);
370 } else
371 v->meta[v->nmeta++] = m;
373 return err;
376 static const struct got_error *
377 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
378 struct got_object_idset *idset, struct got_object_id *tree_id,
379 const char *dpath, time_t mtime, struct got_repository *repo,
380 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
382 const struct got_error *err;
383 struct got_tree_object *tree;
384 char *p = NULL;
385 int i;
387 err = got_object_open_as_tree(&tree, repo, tree_id);
388 if (err)
389 return err;
391 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
392 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
393 struct got_object_id *id = got_tree_entry_get_id(e);
394 mode_t mode = got_tree_entry_get_mode(e);
396 if (cancel_cb) {
397 err = (*cancel_cb)(cancel_arg);
398 if (err)
399 break;
402 if (got_object_tree_entry_is_submodule(e) ||
403 got_object_idset_contains(idset, id))
404 continue;
406 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
407 got_tree_entry_get_name(e)) == -1) {
408 err = got_error_from_errno("asprintf");
409 break;
412 if (S_ISDIR(mode)) {
413 struct got_object_qid *qid;
414 err = got_object_qid_alloc(&qid, id);
415 if (err)
416 break;
417 STAILQ_INSERT_TAIL(ids, qid, entry);
418 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
419 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
420 mtime, loose_obj_only, repo);
421 if (err)
422 break;
424 free(p);
425 p = NULL;
428 got_object_tree_close(tree);
429 free(p);
430 return err;
433 static const struct got_error *
434 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
435 struct got_object_id *tree_id, const char *dpath, time_t mtime,
436 int loose_obj_only, struct got_repository *repo,
437 got_cancel_cb cancel_cb, void *cancel_arg)
439 const struct got_error *err = NULL;
440 struct got_object_id_queue tree_ids;
441 struct got_object_qid *qid;
443 if (got_object_idset_contains(idset, tree_id))
444 return NULL;
446 err = got_object_qid_alloc(&qid, tree_id);
447 if (err)
448 return err;
450 STAILQ_INIT(&tree_ids);
451 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
453 while (!STAILQ_EMPTY(&tree_ids)) {
454 if (cancel_cb) {
455 err = (*cancel_cb)(cancel_arg);
456 if (err)
457 break;
460 qid = STAILQ_FIRST(&tree_ids);
461 STAILQ_REMOVE_HEAD(&tree_ids, entry);
463 if (got_object_idset_contains(idset, qid->id)) {
464 got_object_qid_free(qid);
465 continue;
468 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
469 mtime, loose_obj_only, repo);
470 if (err) {
471 got_object_qid_free(qid);
472 break;
475 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
476 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
477 got_object_qid_free(qid);
478 if (err)
479 break;
482 got_object_id_queue_free(&tree_ids);
483 return err;
486 static const struct got_error *
487 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
488 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
489 got_cancel_cb cancel_cb, void *cancel_arg)
491 const struct got_error *err;
492 struct got_commit_object *commit;
494 if (got_object_idset_contains(idset, id))
495 return NULL;
497 if (loose_obj_only) {
498 int is_packed;
499 err = search_packidx(&is_packed, id, repo);
500 if (err)
501 return err;
502 if (is_packed)
503 return NULL;
506 err = got_object_open_as_commit(&commit, repo, id);
507 if (err)
508 return err;
510 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
511 got_object_commit_get_committer_time(commit),
512 loose_obj_only, repo);
513 if (err)
514 goto done;
516 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
517 "", got_object_commit_get_committer_time(commit),
518 loose_obj_only, repo, cancel_cb, cancel_arg);
519 done:
520 got_object_commit_close(commit);
521 return err;
524 static const struct got_error *
525 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
526 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
527 got_cancel_cb cancel_cb, void *cancel_arg)
529 const struct got_error *err;
530 struct got_tag_object *tag = NULL;
532 if (got_object_idset_contains(idset, id))
533 return NULL;
535 if (loose_obj_only) {
536 int is_packed;
537 err = search_packidx(&is_packed, id, repo);
538 if (err)
539 return err;
540 if (is_packed)
541 return NULL;
544 err = got_object_open_as_tag(&tag, repo, id);
545 if (err)
546 return err;
548 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
549 got_object_tag_get_tagger_time(tag),
550 loose_obj_only, repo);
551 if (err)
552 goto done;
554 switch (got_object_tag_get_object_type(tag)) {
555 case GOT_OBJ_TYPE_COMMIT:
556 err = load_commit(v, idset,
557 got_object_tag_get_object_id(tag), repo,
558 loose_obj_only, cancel_cb, cancel_arg);
559 break;
560 case GOT_OBJ_TYPE_TREE:
561 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
562 "", got_object_tag_get_tagger_time(tag),
563 loose_obj_only, repo, cancel_cb, cancel_arg);
564 break;
565 default:
566 break;
569 done:
570 got_object_tag_close(tag);
571 return err;
574 enum findtwixt_color {
575 COLOR_KEEP = 0,
576 COLOR_DROP,
577 COLOR_BLANK,
578 };
579 static const int findtwixt_colors[] = {
580 COLOR_KEEP,
581 COLOR_DROP,
582 COLOR_BLANK
583 };
585 static const struct got_error *
586 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
587 int color, struct got_repository *repo)
589 const struct got_error *err;
590 struct got_object_qid *qid;
592 err = got_object_qid_alloc(&qid, id);
593 if (err)
594 return err;
596 STAILQ_INSERT_TAIL(ids, qid, entry);
597 qid->data = (void *)&findtwixt_colors[color];
598 return NULL;
601 static const struct got_error *
602 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
603 struct got_object_id *id, struct got_repository *repo,
604 got_cancel_cb cancel_cb, void *cancel_arg)
606 const struct got_error *err = NULL;
607 struct got_commit_object *commit;
608 const struct got_object_id_queue *parents;
609 struct got_object_id_queue ids;
610 struct got_object_qid *qid;
612 STAILQ_INIT(&ids);
614 err = got_object_qid_alloc(&qid, id);
615 if (err)
616 return err;
617 STAILQ_INSERT_HEAD(&ids, qid, entry);
619 while (!STAILQ_EMPTY(&ids)) {
620 if (cancel_cb) {
621 err = (*cancel_cb)(cancel_arg);
622 if (err)
623 break;
626 qid = STAILQ_FIRST(&ids);
627 STAILQ_REMOVE_HEAD(&ids, entry);
629 if (got_object_idset_contains(drop, qid->id)) {
630 got_object_qid_free(qid);
631 continue;
634 err = got_object_idset_add(drop, qid->id,
635 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
636 if (err) {
637 got_object_qid_free(qid);
638 break;
641 if (!got_object_idset_contains(keep, qid->id)) {
642 got_object_qid_free(qid);
643 continue;
646 err = got_object_open_as_commit(&commit, repo, qid->id);
647 got_object_qid_free(qid);
648 if (err)
649 break;
651 parents = got_object_commit_get_parent_ids(commit);
652 if (parents) {
653 err = got_object_id_queue_copy(parents, &ids);
654 if (err) {
655 got_object_commit_close(commit);
656 break;
659 got_object_commit_close(commit);
662 got_object_id_queue_free(&ids);
663 return err;
666 struct append_id_arg {
667 struct got_object_id **array;
668 int idx;
669 };
671 static const struct got_error *
672 append_id(struct got_object_id *id, void *data, void *arg)
674 struct append_id_arg *a = arg;
676 a->array[a->idx] = got_object_id_dup(id);
677 if (a->array[a->idx] == NULL)
678 return got_error_from_errno("got_object_id_dup");
680 a->idx++;
681 return NULL;
684 static const struct got_error *
685 findtwixt(struct got_object_id ***res, int *nres,
686 struct got_object_id **head, int nhead,
687 struct got_object_id **tail, int ntail,
688 struct got_repository *repo,
689 got_cancel_cb cancel_cb, void *cancel_arg)
691 const struct got_error *err = NULL;
692 struct got_object_id_queue ids;
693 struct got_object_idset *keep, *drop;
694 struct got_object_qid *qid;
695 int i, ncolor, nkeep, obj_type;
697 STAILQ_INIT(&ids);
698 *res = NULL;
699 *nres = 0;
701 keep = got_object_idset_alloc();
702 if (keep == NULL)
703 return got_error_from_errno("got_object_idset_alloc");
705 drop = got_object_idset_alloc();
706 if (drop == NULL) {
707 err = got_error_from_errno("got_object_idset_alloc");
708 goto done;
711 for (i = 0; i < nhead; i++) {
712 struct got_object_id *id = head[i];
713 if (id == NULL)
714 continue;
715 err = got_object_get_type(&obj_type, repo, id);
716 if (err)
717 return err;
718 if (obj_type != GOT_OBJ_TYPE_COMMIT)
719 continue;
720 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
721 if (err)
722 goto done;
724 for (i = 0; i < ntail; i++) {
725 struct got_object_id *id = tail[i];
726 if (id == NULL)
727 continue;
728 err = got_object_get_type(&obj_type, repo, id);
729 if (err)
730 return err;
731 if (obj_type != GOT_OBJ_TYPE_COMMIT)
732 continue;
733 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
734 if (err)
735 goto done;
738 while (!STAILQ_EMPTY(&ids)) {
739 int qcolor;
740 qid = STAILQ_FIRST(&ids);
741 qcolor = *((int *)qid->data);
743 if (got_object_idset_contains(drop, qid->id))
744 ncolor = COLOR_DROP;
745 else if (got_object_idset_contains(keep, qid->id))
746 ncolor = COLOR_KEEP;
747 else
748 ncolor = COLOR_BLANK;
750 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
751 qcolor == COLOR_KEEP)) {
752 STAILQ_REMOVE_HEAD(&ids, entry);
753 got_object_qid_free(qid);
754 continue;
757 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
758 err = drop_commit(keep, drop, qid->id, repo,
759 cancel_cb, cancel_arg);
760 if (err)
761 goto done;
762 } else if (ncolor == COLOR_BLANK) {
763 struct got_commit_object *commit;
764 struct got_object_id *id;
765 const struct got_object_id_queue *parents;
766 struct got_object_qid *pid;
768 id = got_object_id_dup(qid->id);
769 if (id == NULL) {
770 err = got_error_from_errno("got_object_id_dup");
771 goto done;
773 if (qcolor == COLOR_KEEP)
774 err = got_object_idset_add(keep, id,
775 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
776 else
777 err = got_object_idset_add(drop, id,
778 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
779 if (err) {
780 free(id);
781 goto done;
784 err = got_object_open_as_commit(&commit, repo, id);
785 if (err) {
786 free(id);
787 goto done;
789 parents = got_object_commit_get_parent_ids(commit);
790 if (parents) {
791 STAILQ_FOREACH(pid, parents, entry) {
792 err = queue_commit_id(&ids, pid->id,
793 qcolor, repo);
794 if (err) {
795 free(id);
796 goto done;
800 got_object_commit_close(commit);
801 commit = NULL;
802 } else {
803 /* should not happen */
804 err = got_error_fmt(GOT_ERR_NOT_IMPL,
805 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
806 goto done;
809 STAILQ_REMOVE_HEAD(&ids, entry);
810 got_object_qid_free(qid);
813 nkeep = got_object_idset_num_elements(keep);
814 if (nkeep > 0) {
815 struct append_id_arg arg;
816 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
817 if (arg.array == NULL) {
818 err = got_error_from_errno("calloc");
819 goto done;
821 arg.idx = 0;
822 err = got_object_idset_for_each(keep, append_id, &arg);
823 if (err) {
824 free(arg.array);
825 goto done;
827 *res = arg.array;
828 *nres = nkeep;
830 done:
831 got_object_idset_free(keep);
832 got_object_idset_free(drop);
833 got_object_id_queue_free(&ids);
834 return err;
837 static const struct got_error *
838 read_meta(struct got_pack_meta ***meta, int *nmeta,
839 struct got_object_id **theirs, int ntheirs,
840 struct got_object_id **ours, int nours, struct got_repository *repo,
841 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
842 got_cancel_cb cancel_cb, void *cancel_arg)
844 const struct got_error *err = NULL;
845 struct got_object_id **ids = NULL;
846 struct got_object_idset *idset;
847 int i, nobj = 0, obj_type;
848 struct got_pack_metavec v;
850 *meta = NULL;
851 *nmeta = 0;
853 idset = got_object_idset_alloc();
854 if (idset == NULL)
855 return got_error_from_errno("got_object_idset_alloc");
857 v.nmeta = 0;
858 v.metasz = 64;
859 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
860 if (v.meta == NULL) {
861 err = got_error_from_errno("calloc");
862 goto done;
865 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
866 cancel_cb, cancel_arg);
867 if (err || nobj == 0)
868 goto done;
870 for (i = 0; i < ntheirs; i++) {
871 struct got_object_id *id = theirs[i];
872 if (id == NULL)
873 continue;
874 err = got_object_get_type(&obj_type, repo, id);
875 if (err)
876 return err;
877 if (obj_type != GOT_OBJ_TYPE_COMMIT)
878 continue;
879 err = load_commit(NULL, idset, id, repo,
880 loose_obj_only, cancel_cb, cancel_arg);
881 if (err)
882 goto done;
883 if (progress_cb) {
884 err = progress_cb(progress_arg, 0L, nours,
885 v.nmeta, 0, 0);
886 if (err)
887 goto done;
891 for (i = 0; i < ntheirs; i++) {
892 struct got_object_id *id = theirs[i];
893 int *cached_type;
894 if (id == NULL)
895 continue;
896 cached_type = got_object_idset_get(idset, id);
897 if (cached_type == NULL) {
898 err = got_object_get_type(&obj_type, repo, id);
899 if (err)
900 goto done;
901 } else
902 obj_type = *cached_type;
903 if (obj_type != GOT_OBJ_TYPE_TAG)
904 continue;
905 err = load_tag(NULL, idset, id, repo,
906 loose_obj_only, cancel_cb, cancel_arg);
907 if (err)
908 goto done;
909 if (progress_cb) {
910 err = progress_cb(progress_arg, 0L, nours,
911 v.nmeta, 0, 0);
912 if (err)
913 goto done;
917 for (i = 0; i < nobj; i++) {
918 err = load_commit(&v, idset, ids[i], repo,
919 loose_obj_only, cancel_cb, cancel_arg);
920 if (err)
921 goto done;
922 if (progress_cb) {
923 err = progress_cb(progress_arg, 0L, nours,
924 v.nmeta, 0, 0);
925 if (err)
926 goto done;
930 for (i = 0; i < nours; i++) {
931 struct got_object_id *id = ours[i];
932 int *cached_type;
933 if (id == NULL)
934 continue;
935 cached_type = got_object_idset_get(idset, id);
936 if (cached_type == NULL) {
937 err = got_object_get_type(&obj_type, repo, id);
938 if (err)
939 goto done;
940 } else
941 obj_type = *cached_type;
942 if (obj_type != GOT_OBJ_TYPE_TAG)
943 continue;
944 err = load_tag(&v, idset, id, repo,
945 loose_obj_only, cancel_cb, cancel_arg);
946 if (err)
947 goto done;
948 if (progress_cb) {
949 err = progress_cb(progress_arg, 0L, nours,
950 v.nmeta, 0, 0);
951 if (err)
952 goto done;
956 done:
957 for (i = 0; i < nobj; i++) {
958 free(ids[i]);
960 free(ids);
961 got_object_idset_free(idset);
962 if (err == NULL) {
963 *meta = v.meta;
964 *nmeta = v.nmeta;
965 } else
966 free(v.meta);
968 return err;
971 const struct got_error *
972 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
974 size_t n;
976 SHA1Update(ctx, buf, len);
977 n = fwrite(buf, 1, len, f);
978 if (n != len)
979 return got_ferror(f, GOT_ERR_IO);
980 return NULL;
983 static void
984 putbe32(char *b, uint32_t n)
986 b[0] = n >> 24;
987 b[1] = n >> 16;
988 b[2] = n >> 8;
989 b[3] = n >> 0;
992 static int
993 write_order_cmp(const void *pa, const void *pb)
995 struct got_pack_meta *a, *b, *ahd, *bhd;
997 a = *(struct got_pack_meta **)pa;
998 b = *(struct got_pack_meta **)pb;
999 ahd = (a->head == NULL) ? a : a->head;
1000 bhd = (b->head == NULL) ? b : b->head;
1001 if (ahd->mtime != bhd->mtime)
1002 return bhd->mtime - ahd->mtime;
1003 if (ahd != bhd)
1004 return (uintptr_t)bhd - (uintptr_t)ahd;
1005 if (a->nchain != b->nchain)
1006 return a->nchain - b->nchain;
1007 return a->mtime - b->mtime;
1010 static const struct got_error *
1011 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1013 size_t i;
1015 *hdrlen = 0;
1017 hdr[0] = obj_type << 4;
1018 hdr[0] |= len & 0xf;
1019 len >>= 4;
1020 for (i = 1; len != 0; i++){
1021 if (i >= bufsize)
1022 return got_error(GOT_ERR_NO_SPACE);
1023 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1024 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1025 len >>= GOT_DELTA_SIZE_SHIFT;
1028 *hdrlen = i;
1029 return NULL;
1032 static const struct got_error *
1033 encodedelta(struct got_pack_meta *m, struct got_raw_object *o,
1034 off_t base_size, FILE *f)
1036 unsigned char buf[16], *bp;
1037 int i, j;
1038 off_t n;
1039 size_t w;
1040 struct got_delta_instruction *d;
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 w = fwrite(buf, 1, i, f);
1051 if (w != i)
1052 return got_ferror(f, GOT_ERR_IO);
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 w = fwrite(buf, 1, i, f);
1063 if (w != i)
1064 return got_ferror(f, GOT_ERR_IO);
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 w = fwrite(buf, 1, bp - buf, f);
1091 if (w != bp - buf)
1092 return got_ferror(f, GOT_ERR_IO);
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 w = fwrite(buf, 1, 1, f);
1102 if (w != 1)
1103 return got_ferror(f, GOT_ERR_IO);
1104 r = fread(content, 1, buf[0], o->f);
1105 if (r != buf[0])
1106 return got_ferror(o->f, GOT_ERR_IO);
1107 w = fwrite(content, 1, buf[0], f);
1108 if (w != buf[0])
1109 return got_ferror(f, GOT_ERR_IO);
1110 n += buf[0];
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;
1145 off_t nd;
1146 SHA1_CTX ctx;
1147 struct got_pack_meta *m;
1148 struct got_raw_object *raw = NULL;
1149 FILE *delta_file = NULL;
1150 char buf[32];
1151 size_t outlen, n;
1152 struct got_deflate_checksum csum;
1153 off_t packfile_size = 0;
1154 int outfd = -1;
1156 SHA1Init(&ctx);
1157 csum.output_sha1 = &ctx;
1158 csum.output_crc = NULL;
1160 err = hwrite(packfile, "PACK", 4, &ctx);
1161 if (err)
1162 return err;
1163 putbe32(buf, GOT_PACKFILE_VERSION);
1164 err = hwrite(packfile, buf, 4, &ctx);
1165 if (err)
1166 goto done;
1167 putbe32(buf, nmeta);
1168 err = hwrite(packfile, buf, 4, &ctx);
1169 if (err)
1170 goto done;
1171 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1172 for (i = 0; i < nmeta; i++) {
1173 if (progress_cb) {
1174 err = progress_cb(progress_arg, packfile_size, nours,
1175 nmeta, nmeta, i);
1176 if (err)
1177 goto done;
1179 m = meta[i];
1180 m->off = ftello(packfile);
1181 if (outfd == -1) {
1182 outfd = got_opentempfd();
1183 if (outfd == -1) {
1184 err = got_error_from_errno("got_opentempfd");
1185 goto done;
1188 err = got_object_raw_open(&raw, outfd, repo, &m->id, 8192);
1189 if (err)
1190 goto done;
1191 if (raw->data == NULL)
1192 outfd = -1; /* outfd is now raw->f */
1193 if (m->deltas == NULL) {
1194 err = packhdr(&nh, buf, sizeof(buf),
1195 m->obj_type, raw->size);
1196 if (err)
1197 goto done;
1198 err = hwrite(packfile, buf, nh, &ctx);
1199 if (err)
1200 goto done;
1201 packfile_size += nh;
1202 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1203 err = got_error_from_errno("fseeko");
1204 goto done;
1206 err = got_deflate_to_file(&outlen, raw->f, packfile,
1207 &csum);
1208 if (err)
1209 goto done;
1210 packfile_size += outlen;
1211 } else {
1212 if (delta_file == NULL) {
1213 delta_file = got_opentemp();
1214 if (delta_file == NULL) {
1215 err = got_error_from_errno(
1216 "got_opentemp");
1217 goto done;
1220 if (ftruncate(fileno(delta_file), 0L) == -1) {
1221 err = got_error_from_errno("ftruncate");
1222 goto done;
1224 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1225 err = got_error_from_errno("fseeko");
1226 goto done;
1228 err = encodedelta(m, raw, m->prev->size, delta_file);
1229 if (err)
1230 goto done;
1231 nd = ftello(delta_file);
1232 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1233 err = got_error_from_errno("fseeko");
1234 goto done;
1236 if (use_offset_deltas && m->prev->off != 0) {
1237 err = packhdr(&nh, buf, sizeof(buf),
1238 GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1239 if (err)
1240 goto done;
1241 nh += packoff(buf + nh,
1242 m->off - m->prev->off);
1243 err = hwrite(packfile, buf, nh, &ctx);
1244 if (err)
1245 goto done;
1246 packfile_size += nh;
1247 } else {
1248 err = packhdr(&nh, buf, sizeof(buf),
1249 GOT_OBJ_TYPE_REF_DELTA, nd);
1250 err = hwrite(packfile, buf, nh, &ctx);
1251 if (err)
1252 goto done;
1253 packfile_size += nh;
1254 err = hwrite(packfile, m->prev->id.sha1,
1255 sizeof(m->prev->id.sha1), &ctx);
1256 packfile_size += sizeof(m->prev->id.sha1);
1257 if (err)
1258 goto done;
1260 err = got_deflate_to_file(&outlen, delta_file,
1261 packfile, &csum);
1262 if (err)
1263 goto done;
1264 packfile_size += outlen;
1266 got_object_raw_close(raw);
1267 raw = NULL;
1269 SHA1Final(pack_sha1, &ctx);
1270 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1271 if (n != SHA1_DIGEST_LENGTH)
1272 err = got_ferror(packfile, GOT_ERR_IO);
1273 packfile_size += SHA1_DIGEST_LENGTH;
1274 packfile_size += sizeof(struct got_packfile_hdr);
1275 err = progress_cb(progress_arg, packfile_size, nours,
1276 nmeta, nmeta, nmeta);
1277 if (err)
1278 goto done;
1279 done:
1280 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1281 err = got_error_from_errno("fclose");
1282 if (raw)
1283 got_object_raw_close(raw);
1284 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1285 err = got_error_from_errno("close");
1286 return err;
1289 const struct got_error *
1290 got_pack_create(uint8_t *packsha1, FILE *packfile,
1291 struct got_object_id **theirs, int ntheirs,
1292 struct got_object_id **ours, int nours,
1293 struct got_repository *repo, int loose_obj_only, int allow_empty,
1294 got_pack_progress_cb progress_cb, void *progress_arg,
1295 got_cancel_cb cancel_cb, void *cancel_arg)
1297 const struct got_error *err;
1298 struct got_pack_meta **meta;
1299 int nmeta;
1301 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1302 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1303 if (err)
1304 return err;
1306 if (nmeta == 0 && !allow_empty) {
1307 err = got_error(GOT_ERR_CANNOT_PACK);
1308 goto done;
1310 if (nmeta > 0) {
1311 err = pick_deltas(meta, nmeta, nours, repo,
1312 progress_cb, progress_arg, cancel_cb, cancel_arg);
1313 if (err)
1314 goto done;
1317 err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1318 progress_cb, progress_arg, cancel_cb, cancel_arg);
1319 if (err)
1320 goto done;
1321 done:
1322 free_nmeta(meta, nmeta);
1323 return err;