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 <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <limits.h>
29 #include <zlib.h>
31 #include "got_error.h"
32 #include "got_cancel.h"
33 #include "got_object.h"
34 #include "got_path.h"
35 #include "got_reference.h"
36 #include "got_repository_admin.h"
37 #include "got_opentemp.h"
39 #include "got_lib_deltify.h"
40 #include "got_lib_delta.h"
41 #include "got_lib_object.h"
42 #include "got_lib_object_idset.h"
43 #include "got_lib_object_cache.h"
44 #include "got_lib_deflate.h"
45 #include "got_lib_pack.h"
46 #include "got_lib_privsep.h"
47 #include "got_lib_repository.h"
49 #ifndef MIN
50 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
51 #endif
53 #ifndef MAX
54 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
55 #endif
57 struct got_pack_meta {
58 struct got_object_id id;
59 char *path;
60 int obj_type;
61 off_t size;
62 time_t mtime;
64 /* The best delta we picked */
65 struct got_pack_meta *head;
66 struct got_pack_meta *prev;
67 off_t delta_offset; /* offset in delta cache file */
68 off_t delta_len; /* length in delta cache file */
69 int nchain;
71 /* Only used for delta window */
72 struct got_delta_table *dtab;
74 /* Only used for writing offset deltas */
75 off_t off;
76 };
78 struct got_pack_metavec {
79 struct got_pack_meta **meta;
80 int nmeta;
81 int metasz;
82 };
84 static const struct got_error *
85 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
86 const char *path, int obj_type, time_t mtime)
87 {
88 const struct got_error *err = NULL;
89 struct got_pack_meta *m;
91 *new = NULL;
93 m = calloc(1, sizeof(*m));
94 if (m == NULL)
95 return got_error_from_errno("calloc");
97 memcpy(&m->id, id, sizeof(m->id));
99 m->path = strdup(path);
100 if (m->path == NULL) {
101 err = got_error_from_errno("strdup");
102 free(m);
103 return err;
106 m->obj_type = obj_type;
107 m->mtime = mtime;
108 *new = m;
109 return NULL;
112 static void
113 clear_meta(struct got_pack_meta *meta)
115 if (meta == NULL)
116 return;
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;
163 static const struct got_error *
164 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
165 struct got_delta_instruction *deltas, int ndeltas,
166 off_t base_size, FILE *f);
168 static const struct got_error *
169 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
170 FILE *delta_cache, struct got_repository *repo,
171 got_pack_progress_cb progress_cb, void *progress_arg,
172 got_cancel_cb cancel_cb, void *cancel_arg)
174 const struct got_error *err = NULL;
175 struct got_pack_meta *m = NULL, *base = NULL;
176 struct got_raw_object *raw = NULL, *base_raw = NULL;
177 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
178 int i, j, size, best_size, ndeltas, best_ndeltas;
179 const int max_base_candidates = 10;
180 int outfd = -1;
182 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
183 for (i = 0; i < nmeta; i++) {
184 if (cancel_cb) {
185 err = (*cancel_cb)(cancel_arg);
186 if (err)
187 break;
189 if (progress_cb) {
190 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
191 if (err)
192 goto done;
194 m = meta[i];
196 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
197 m->obj_type == GOT_OBJ_TYPE_TAG)
198 continue;
200 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
201 if (err)
202 goto done;
203 m->size = raw->size;
205 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
206 raw->size + raw->hdrlen);
207 if (err)
208 goto done;
210 if (i > max_base_candidates) {
211 struct got_pack_meta *n = NULL;
212 n = meta[i - (max_base_candidates + 1)];
213 got_deltify_free(n->dtab);
214 n->dtab = NULL;
217 best_size = raw->size;
218 best_ndeltas = 0;
219 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
220 if (cancel_cb) {
221 err = (*cancel_cb)(cancel_arg);
222 if (err)
223 goto done;
225 base = meta[j];
226 /* long chains make unpacking slow, avoid such bases */
227 if (base->nchain >= 32 ||
228 base->obj_type != m->obj_type)
229 continue;
231 err = got_object_raw_open(&base_raw, &outfd, repo,
232 &base->id);
233 if (err)
234 goto done;
235 err = got_deltify(&deltas, &ndeltas,
236 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
237 base->dtab, base_raw->f, base_raw->hdrlen,
238 base_raw->size + base_raw->hdrlen);
239 got_object_raw_close(base_raw);
240 base_raw = NULL;
241 if (err)
242 goto done;
244 size = delta_size(deltas, ndeltas);
245 if (size + 32 < best_size){
246 /*
247 * if we already picked a best delta,
248 * replace it.
249 */
250 best_size = size;
251 free(best_deltas);
252 best_deltas = deltas;
253 best_ndeltas = ndeltas;
254 deltas = NULL;
255 m->nchain = base->nchain + 1;
256 m->prev = base;
257 m->head = base->head;
258 if (m->head == NULL)
259 m->head = base;
260 } else {
261 free(deltas);
262 deltas = NULL;
263 ndeltas = 0;
267 if (best_ndeltas > 0) {
268 m->delta_offset = ftello(delta_cache);
269 err = encode_delta(m, raw, best_deltas,
270 best_ndeltas, m->prev->size, delta_cache);
271 free(best_deltas);
272 best_deltas = NULL;
273 best_ndeltas = 0;
274 if (err)
275 goto done;
276 m->delta_len = ftello(delta_cache) - m->delta_offset;
279 got_object_raw_close(raw);
280 raw = NULL;
282 done:
283 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
284 got_deltify_free(meta[i]->dtab);
285 meta[i]->dtab = NULL;
287 if (raw)
288 got_object_raw_close(raw);
289 if (base_raw)
290 got_object_raw_close(base_raw);
291 if (outfd != -1 && close(outfd) == -1 && err == NULL)
292 err = got_error_from_errno("close");
293 free(deltas);
294 free(best_deltas);
295 return err;
298 static const struct got_error *
299 search_packidx(int *found, struct got_object_id *id,
300 struct got_repository *repo)
302 const struct got_error *err = NULL;
303 struct got_packidx *packidx = NULL;
304 int idx;
306 *found = 0;
308 err = got_repo_search_packidx(&packidx, &idx, repo, id);
309 if (err == NULL)
310 *found = 1; /* object is already packed */
311 else if (err->code == GOT_ERR_NO_OBJ)
312 err = NULL;
313 return err;
316 static const int obj_types[] = {
317 GOT_OBJ_TYPE_ANY,
318 GOT_OBJ_TYPE_COMMIT,
319 GOT_OBJ_TYPE_TREE,
320 GOT_OBJ_TYPE_BLOB,
321 GOT_OBJ_TYPE_TAG,
322 GOT_OBJ_TYPE_OFFSET_DELTA,
323 GOT_OBJ_TYPE_REF_DELTA
324 };
326 static const struct got_error *
327 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
328 struct got_object_id *id, const char *path, int obj_type,
329 time_t mtime, int loose_obj_only, struct got_repository *repo)
331 const struct got_error *err;
332 struct got_pack_meta *m;
334 if (loose_obj_only) {
335 int is_packed;
336 err = search_packidx(&is_packed, id, repo);
337 if (err)
338 return err;
339 if (is_packed)
340 return NULL;
343 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
344 if (err)
345 return err;
347 if (v == NULL)
348 return NULL;
350 err = alloc_meta(&m, id, path, obj_type, mtime);
351 if (err)
352 goto done;
354 if (v->nmeta == v->metasz){
355 size_t newsize = 2 * v->metasz;
356 struct got_pack_meta **new;
357 new = reallocarray(v->meta, newsize, sizeof(*new));
358 if (new == NULL) {
359 err = got_error_from_errno("reallocarray");
360 goto done;
362 v->meta = new;
363 v->metasz = newsize;
365 done:
366 if (err) {
367 clear_meta(m);
368 free(m);
369 } else
370 v->meta[v->nmeta++] = m;
372 return err;
375 static const struct got_error *
376 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
377 struct got_object_idset *idset, struct got_object_id *tree_id,
378 const char *dpath, time_t mtime, struct got_repository *repo,
379 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
381 const struct got_error *err;
382 struct got_tree_object *tree;
383 char *p = NULL;
384 int i;
386 err = got_object_open_as_tree(&tree, repo, tree_id);
387 if (err)
388 return err;
390 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
391 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
392 struct got_object_id *id = got_tree_entry_get_id(e);
393 mode_t mode = got_tree_entry_get_mode(e);
395 if (cancel_cb) {
396 err = (*cancel_cb)(cancel_arg);
397 if (err)
398 break;
401 if (got_object_tree_entry_is_submodule(e) ||
402 got_object_idset_contains(idset, id))
403 continue;
405 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
406 got_tree_entry_get_name(e)) == -1) {
407 err = got_error_from_errno("asprintf");
408 break;
411 if (S_ISDIR(mode)) {
412 struct got_object_qid *qid;
413 err = got_object_qid_alloc(&qid, id);
414 if (err)
415 break;
416 STAILQ_INSERT_TAIL(ids, qid, entry);
417 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
418 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
419 mtime, loose_obj_only, repo);
420 if (err)
421 break;
423 free(p);
424 p = NULL;
427 got_object_tree_close(tree);
428 free(p);
429 return err;
432 static const struct got_error *
433 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
434 struct got_object_id *tree_id, const char *dpath, time_t mtime,
435 int loose_obj_only, struct got_repository *repo,
436 got_cancel_cb cancel_cb, void *cancel_arg)
438 const struct got_error *err = NULL;
439 struct got_object_id_queue tree_ids;
440 struct got_object_qid *qid;
442 if (got_object_idset_contains(idset, tree_id))
443 return NULL;
445 err = got_object_qid_alloc(&qid, tree_id);
446 if (err)
447 return err;
449 STAILQ_INIT(&tree_ids);
450 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
452 while (!STAILQ_EMPTY(&tree_ids)) {
453 if (cancel_cb) {
454 err = (*cancel_cb)(cancel_arg);
455 if (err)
456 break;
459 qid = STAILQ_FIRST(&tree_ids);
460 STAILQ_REMOVE_HEAD(&tree_ids, entry);
462 if (got_object_idset_contains(idset, qid->id)) {
463 got_object_qid_free(qid);
464 continue;
467 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
468 mtime, loose_obj_only, repo);
469 if (err) {
470 got_object_qid_free(qid);
471 break;
474 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
475 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
476 got_object_qid_free(qid);
477 if (err)
478 break;
481 got_object_id_queue_free(&tree_ids);
482 return err;
485 static const struct got_error *
486 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
487 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
488 got_cancel_cb cancel_cb, void *cancel_arg)
490 const struct got_error *err;
491 struct got_commit_object *commit;
493 if (got_object_idset_contains(idset, id))
494 return NULL;
496 if (loose_obj_only) {
497 int is_packed;
498 err = search_packidx(&is_packed, id, repo);
499 if (err)
500 return err;
501 if (is_packed)
502 return NULL;
505 err = got_object_open_as_commit(&commit, repo, id);
506 if (err)
507 return err;
509 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
510 got_object_commit_get_committer_time(commit),
511 loose_obj_only, repo);
512 if (err)
513 goto done;
515 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
516 "", got_object_commit_get_committer_time(commit),
517 loose_obj_only, repo, cancel_cb, cancel_arg);
518 done:
519 got_object_commit_close(commit);
520 return err;
523 static const struct got_error *
524 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
525 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
526 got_cancel_cb cancel_cb, void *cancel_arg)
528 const struct got_error *err;
529 struct got_tag_object *tag = NULL;
531 if (got_object_idset_contains(idset, id))
532 return NULL;
534 if (loose_obj_only) {
535 int is_packed;
536 err = search_packidx(&is_packed, id, repo);
537 if (err)
538 return err;
539 if (is_packed)
540 return NULL;
543 err = got_object_open_as_tag(&tag, repo, id);
544 if (err)
545 return err;
547 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
548 got_object_tag_get_tagger_time(tag),
549 loose_obj_only, repo);
550 if (err)
551 goto done;
553 switch (got_object_tag_get_object_type(tag)) {
554 case GOT_OBJ_TYPE_COMMIT:
555 err = load_commit(v, idset,
556 got_object_tag_get_object_id(tag), repo,
557 loose_obj_only, cancel_cb, cancel_arg);
558 break;
559 case GOT_OBJ_TYPE_TREE:
560 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
561 "", got_object_tag_get_tagger_time(tag),
562 loose_obj_only, repo, cancel_cb, cancel_arg);
563 break;
564 default:
565 break;
568 done:
569 got_object_tag_close(tag);
570 return err;
573 enum findtwixt_color {
574 COLOR_KEEP = 0,
575 COLOR_DROP,
576 COLOR_BLANK,
577 };
578 static const int findtwixt_colors[] = {
579 COLOR_KEEP,
580 COLOR_DROP,
581 COLOR_BLANK
582 };
584 static const struct got_error *
585 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
586 int color, struct got_repository *repo)
588 const struct got_error *err;
589 struct got_object_qid *qid;
591 err = got_object_qid_alloc(&qid, id);
592 if (err)
593 return err;
595 STAILQ_INSERT_TAIL(ids, qid, entry);
596 qid->data = (void *)&findtwixt_colors[color];
597 return NULL;
600 static const struct got_error *
601 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
602 struct got_object_id *id, struct got_repository *repo,
603 got_cancel_cb cancel_cb, void *cancel_arg)
605 const struct got_error *err = NULL;
606 struct got_commit_object *commit;
607 const struct got_object_id_queue *parents;
608 struct got_object_id_queue ids;
609 struct got_object_qid *qid;
611 STAILQ_INIT(&ids);
613 err = got_object_qid_alloc(&qid, id);
614 if (err)
615 return err;
616 STAILQ_INSERT_HEAD(&ids, qid, entry);
618 while (!STAILQ_EMPTY(&ids)) {
619 if (cancel_cb) {
620 err = (*cancel_cb)(cancel_arg);
621 if (err)
622 break;
625 qid = STAILQ_FIRST(&ids);
626 STAILQ_REMOVE_HEAD(&ids, entry);
628 if (got_object_idset_contains(drop, qid->id)) {
629 got_object_qid_free(qid);
630 continue;
633 err = got_object_idset_add(drop, qid->id,
634 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
635 if (err) {
636 got_object_qid_free(qid);
637 break;
640 if (!got_object_idset_contains(keep, qid->id)) {
641 got_object_qid_free(qid);
642 continue;
645 err = got_object_open_as_commit(&commit, repo, qid->id);
646 got_object_qid_free(qid);
647 if (err)
648 break;
650 parents = got_object_commit_get_parent_ids(commit);
651 if (parents) {
652 err = got_object_id_queue_copy(parents, &ids);
653 if (err) {
654 got_object_commit_close(commit);
655 break;
658 got_object_commit_close(commit);
661 got_object_id_queue_free(&ids);
662 return err;
665 struct append_id_arg {
666 struct got_object_id **array;
667 int idx;
668 };
670 static const struct got_error *
671 append_id(struct got_object_id *id, void *data, void *arg)
673 struct append_id_arg *a = arg;
675 a->array[a->idx] = got_object_id_dup(id);
676 if (a->array[a->idx] == NULL)
677 return got_error_from_errno("got_object_id_dup");
679 a->idx++;
680 return NULL;
683 static const struct got_error *
684 findtwixt(struct got_object_id ***res, int *nres,
685 struct got_object_id **head, int nhead,
686 struct got_object_id **tail, int ntail,
687 struct got_repository *repo,
688 got_cancel_cb cancel_cb, void *cancel_arg)
690 const struct got_error *err = NULL;
691 struct got_object_id_queue ids;
692 struct got_object_idset *keep, *drop;
693 struct got_object_qid *qid;
694 int i, ncolor, nkeep, obj_type;
696 STAILQ_INIT(&ids);
697 *res = NULL;
698 *nres = 0;
700 keep = got_object_idset_alloc();
701 if (keep == NULL)
702 return got_error_from_errno("got_object_idset_alloc");
704 drop = got_object_idset_alloc();
705 if (drop == NULL) {
706 err = got_error_from_errno("got_object_idset_alloc");
707 goto done;
710 for (i = 0; i < nhead; i++) {
711 struct got_object_id *id = head[i];
712 if (id == NULL)
713 continue;
714 err = got_object_get_type(&obj_type, repo, id);
715 if (err)
716 return err;
717 if (obj_type != GOT_OBJ_TYPE_COMMIT)
718 continue;
719 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
720 if (err)
721 goto done;
723 for (i = 0; i < ntail; i++) {
724 struct got_object_id *id = tail[i];
725 if (id == NULL)
726 continue;
727 err = got_object_get_type(&obj_type, repo, id);
728 if (err)
729 return err;
730 if (obj_type != GOT_OBJ_TYPE_COMMIT)
731 continue;
732 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
733 if (err)
734 goto done;
737 while (!STAILQ_EMPTY(&ids)) {
738 int qcolor;
739 qid = STAILQ_FIRST(&ids);
740 qcolor = *((int *)qid->data);
742 if (got_object_idset_contains(drop, qid->id))
743 ncolor = COLOR_DROP;
744 else if (got_object_idset_contains(keep, qid->id))
745 ncolor = COLOR_KEEP;
746 else
747 ncolor = COLOR_BLANK;
749 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
750 qcolor == COLOR_KEEP)) {
751 STAILQ_REMOVE_HEAD(&ids, entry);
752 got_object_qid_free(qid);
753 continue;
756 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
757 err = drop_commit(keep, drop, qid->id, repo,
758 cancel_cb, cancel_arg);
759 if (err)
760 goto done;
761 } else if (ncolor == COLOR_BLANK) {
762 struct got_commit_object *commit;
763 struct got_object_id *id;
764 const struct got_object_id_queue *parents;
765 struct got_object_qid *pid;
767 id = got_object_id_dup(qid->id);
768 if (id == NULL) {
769 err = got_error_from_errno("got_object_id_dup");
770 goto done;
772 if (qcolor == COLOR_KEEP)
773 err = got_object_idset_add(keep, id,
774 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
775 else
776 err = got_object_idset_add(drop, id,
777 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
778 if (err) {
779 free(id);
780 goto done;
783 err = got_object_open_as_commit(&commit, repo, id);
784 if (err) {
785 free(id);
786 goto done;
788 parents = got_object_commit_get_parent_ids(commit);
789 if (parents) {
790 STAILQ_FOREACH(pid, parents, entry) {
791 err = queue_commit_id(&ids, pid->id,
792 qcolor, repo);
793 if (err) {
794 free(id);
795 goto done;
799 got_object_commit_close(commit);
800 commit = NULL;
801 } else {
802 /* should not happen */
803 err = got_error_fmt(GOT_ERR_NOT_IMPL,
804 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
805 goto done;
808 STAILQ_REMOVE_HEAD(&ids, entry);
809 got_object_qid_free(qid);
812 nkeep = got_object_idset_num_elements(keep);
813 if (nkeep > 0) {
814 struct append_id_arg arg;
815 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
816 if (arg.array == NULL) {
817 err = got_error_from_errno("calloc");
818 goto done;
820 arg.idx = 0;
821 err = got_object_idset_for_each(keep, append_id, &arg);
822 if (err) {
823 free(arg.array);
824 goto done;
826 *res = arg.array;
827 *nres = nkeep;
829 done:
830 got_object_idset_free(keep);
831 got_object_idset_free(drop);
832 got_object_id_queue_free(&ids);
833 return err;
836 static const struct got_error *
837 read_meta(struct got_pack_meta ***meta, int *nmeta,
838 struct got_object_id **theirs, int ntheirs,
839 struct got_object_id **ours, int nours, struct got_repository *repo,
840 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
841 got_cancel_cb cancel_cb, void *cancel_arg)
843 const struct got_error *err = NULL;
844 struct got_object_id **ids = NULL;
845 struct got_object_idset *idset;
846 int i, nobj = 0, obj_type;
847 struct got_pack_metavec v;
849 *meta = NULL;
850 *nmeta = 0;
852 idset = got_object_idset_alloc();
853 if (idset == NULL)
854 return got_error_from_errno("got_object_idset_alloc");
856 v.nmeta = 0;
857 v.metasz = 64;
858 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
859 if (v.meta == NULL) {
860 err = got_error_from_errno("calloc");
861 goto done;
864 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
865 cancel_cb, cancel_arg);
866 if (err || nobj == 0)
867 goto done;
869 for (i = 0; i < ntheirs; i++) {
870 struct got_object_id *id = theirs[i];
871 if (id == NULL)
872 continue;
873 err = got_object_get_type(&obj_type, repo, id);
874 if (err)
875 return err;
876 if (obj_type != GOT_OBJ_TYPE_COMMIT)
877 continue;
878 err = load_commit(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 < ntheirs; i++) {
891 struct got_object_id *id = theirs[i];
892 int *cached_type;
893 if (id == NULL)
894 continue;
895 cached_type = got_object_idset_get(idset, id);
896 if (cached_type == NULL) {
897 err = got_object_get_type(&obj_type, repo, id);
898 if (err)
899 goto done;
900 } else
901 obj_type = *cached_type;
902 if (obj_type != GOT_OBJ_TYPE_TAG)
903 continue;
904 err = load_tag(NULL, idset, id, repo,
905 loose_obj_only, cancel_cb, cancel_arg);
906 if (err)
907 goto done;
908 if (progress_cb) {
909 err = progress_cb(progress_arg, 0L, nours,
910 v.nmeta, 0, 0);
911 if (err)
912 goto done;
916 for (i = 0; i < nobj; i++) {
917 err = load_commit(&v, idset, ids[i], 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 for (i = 0; i < nours; i++) {
930 struct got_object_id *id = ours[i];
931 int *cached_type;
932 if (id == NULL)
933 continue;
934 cached_type = got_object_idset_get(idset, id);
935 if (cached_type == NULL) {
936 err = got_object_get_type(&obj_type, repo, id);
937 if (err)
938 goto done;
939 } else
940 obj_type = *cached_type;
941 if (obj_type != GOT_OBJ_TYPE_TAG)
942 continue;
943 err = load_tag(&v, idset, id, repo,
944 loose_obj_only, cancel_cb, cancel_arg);
945 if (err)
946 goto done;
947 if (progress_cb) {
948 err = progress_cb(progress_arg, 0L, nours,
949 v.nmeta, 0, 0);
950 if (err)
951 goto done;
955 done:
956 for (i = 0; i < nobj; i++) {
957 free(ids[i]);
959 free(ids);
960 got_object_idset_free(idset);
961 if (err == NULL) {
962 *meta = v.meta;
963 *nmeta = v.nmeta;
964 } else
965 free(v.meta);
967 return err;
970 const struct got_error *
971 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
973 size_t n;
975 SHA1Update(ctx, buf, len);
976 n = fwrite(buf, 1, len, f);
977 if (n != len)
978 return got_ferror(f, GOT_ERR_IO);
979 return NULL;
982 static void
983 putbe32(char *b, uint32_t n)
985 b[0] = n >> 24;
986 b[1] = n >> 16;
987 b[2] = n >> 8;
988 b[3] = n >> 0;
991 static int
992 write_order_cmp(const void *pa, const void *pb)
994 struct got_pack_meta *a, *b, *ahd, *bhd;
996 a = *(struct got_pack_meta **)pa;
997 b = *(struct got_pack_meta **)pb;
998 ahd = (a->head == NULL) ? a : a->head;
999 bhd = (b->head == NULL) ? b : b->head;
1000 if (ahd->mtime != bhd->mtime)
1001 return bhd->mtime - ahd->mtime;
1002 if (ahd != bhd)
1003 return (uintptr_t)bhd - (uintptr_t)ahd;
1004 if (a->nchain != b->nchain)
1005 return a->nchain - b->nchain;
1006 return a->mtime - b->mtime;
1009 static const struct got_error *
1010 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1012 size_t i;
1014 *hdrlen = 0;
1016 hdr[0] = obj_type << 4;
1017 hdr[0] |= len & 0xf;
1018 len >>= 4;
1019 for (i = 1; len != 0; i++){
1020 if (i >= bufsize)
1021 return got_error(GOT_ERR_NO_SPACE);
1022 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1023 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1024 len >>= GOT_DELTA_SIZE_SHIFT;
1027 *hdrlen = i;
1028 return NULL;
1031 static const struct got_error *
1032 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
1033 struct got_delta_instruction *deltas, int ndeltas,
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 < ndeltas; j++) {
1067 d = &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, FILE *delta_cache,
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 SHA1_CTX ctx;
1146 struct got_pack_meta *m;
1147 struct got_raw_object *raw = NULL;
1148 FILE *delta_file = NULL;
1149 char buf[32];
1150 size_t outlen, n;
1151 struct got_deflate_checksum csum;
1152 off_t packfile_size = 0;
1153 int outfd = -1;
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, &outfd, repo, &m->id);
1181 if (err)
1182 goto done;
1183 if (m->delta_len == 0) {
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 off_t remain;
1203 if (delta_file == NULL) {
1204 delta_file = got_opentemp();
1205 if (delta_file == NULL) {
1206 err = got_error_from_errno(
1207 "got_opentemp");
1208 goto done;
1211 if (ftruncate(fileno(delta_file), 0L) == -1) {
1212 err = got_error_from_errno("ftruncate");
1213 goto done;
1215 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1216 err = got_error_from_errno("fseeko");
1217 goto done;
1219 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1220 == -1) {
1221 err = got_error_from_errno("fseeko");
1222 goto done;
1224 remain = m->delta_len;
1225 while (remain > 0) {
1226 char delta_buf[8192];
1227 size_t r, w, n;
1228 n = MIN(remain, sizeof(delta_buf));
1229 r = fread(delta_buf, 1, n, delta_cache);
1230 if (r != n) {
1231 err = got_ferror(delta_cache,
1232 GOT_ERR_IO);
1233 goto done;
1235 w = fwrite(delta_buf, 1, n, delta_file);
1236 if (w != n) {
1237 err = got_ferror(delta_file,
1238 GOT_ERR_IO);
1239 goto done;
1241 remain -= n;
1243 if (use_offset_deltas && m->prev->off != 0) {
1244 err = packhdr(&nh, buf, sizeof(buf),
1245 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1246 if (err)
1247 goto done;
1248 nh += packoff(buf + nh,
1249 m->off - m->prev->off);
1250 err = hwrite(packfile, buf, nh, &ctx);
1251 if (err)
1252 goto done;
1253 packfile_size += nh;
1254 } else {
1255 err = packhdr(&nh, buf, sizeof(buf),
1256 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1257 err = hwrite(packfile, buf, nh, &ctx);
1258 if (err)
1259 goto done;
1260 packfile_size += nh;
1261 err = hwrite(packfile, m->prev->id.sha1,
1262 sizeof(m->prev->id.sha1), &ctx);
1263 packfile_size += sizeof(m->prev->id.sha1);
1264 if (err)
1265 goto done;
1267 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1268 err = got_error_from_errno("fseeko");
1269 goto done;
1271 err = got_deflate_to_file(&outlen, delta_file,
1272 packfile, &csum);
1273 if (err)
1274 goto done;
1275 packfile_size += outlen;
1277 got_object_raw_close(raw);
1278 raw = NULL;
1280 SHA1Final(pack_sha1, &ctx);
1281 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1282 if (n != SHA1_DIGEST_LENGTH)
1283 err = got_ferror(packfile, GOT_ERR_IO);
1284 packfile_size += SHA1_DIGEST_LENGTH;
1285 packfile_size += sizeof(struct got_packfile_hdr);
1286 err = progress_cb(progress_arg, packfile_size, nours,
1287 nmeta, nmeta, nmeta);
1288 if (err)
1289 goto done;
1290 done:
1291 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1292 err = got_error_from_errno("fclose");
1293 if (raw)
1294 got_object_raw_close(raw);
1295 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1296 err = got_error_from_errno("close");
1297 return err;
1300 const struct got_error *
1301 got_pack_create(uint8_t *packsha1, FILE *packfile,
1302 struct got_object_id **theirs, int ntheirs,
1303 struct got_object_id **ours, int nours,
1304 struct got_repository *repo, int loose_obj_only, int allow_empty,
1305 got_pack_progress_cb progress_cb, void *progress_arg,
1306 got_cancel_cb cancel_cb, void *cancel_arg)
1308 const struct got_error *err;
1309 struct got_pack_meta **meta;
1310 int nmeta;
1311 FILE *delta_cache = NULL;
1313 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1314 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1315 if (err)
1316 return err;
1318 if (nmeta == 0 && !allow_empty) {
1319 err = got_error(GOT_ERR_CANNOT_PACK);
1320 goto done;
1323 delta_cache = got_opentemp();
1324 if (delta_cache == NULL) {
1325 err = got_error_from_errno("got_opentemp");
1326 goto done;
1329 if (nmeta > 0) {
1330 err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1331 progress_cb, progress_arg, cancel_cb, cancel_arg);
1332 if (err)
1333 goto done;
1334 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1335 err = got_error_from_errno("fseeko");
1336 goto done;
1340 err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1341 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1342 if (err)
1343 goto done;
1344 done:
1345 free_nmeta(meta, nmeta);
1346 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1347 err = got_error_from_errno("fclose");
1348 return err;