Blob


1 /*
2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/uio.h>
20 #include <sys/stat.h>
22 #include <stdint.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <limits.h>
27 #include <zlib.h>
29 #if defined(__FreeBSD__)
30 #include <unistd.h>
31 #endif
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 MIN
52 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
53 #endif
55 #ifndef MAX
56 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
57 #endif
59 struct got_pack_meta {
60 struct got_object_id id;
61 char *path;
62 int obj_type;
63 off_t size;
64 time_t mtime;
66 /* The best delta we picked */
67 struct got_pack_meta *head;
68 struct got_pack_meta *prev;
69 off_t delta_offset; /* offset in delta cache file */
70 off_t delta_len; /* length in delta cache file */
71 int nchain;
73 /* Only used for delta window */
74 struct got_delta_table *dtab;
76 /* Only used for writing offset deltas */
77 off_t off;
78 };
80 struct got_pack_metavec {
81 struct got_pack_meta **meta;
82 int nmeta;
83 int metasz;
84 };
86 static const struct got_error *
87 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
88 const char *path, int obj_type, time_t mtime)
89 {
90 const struct got_error *err = NULL;
91 struct got_pack_meta *m;
93 *new = NULL;
95 m = calloc(1, sizeof(*m));
96 if (m == NULL)
97 return got_error_from_errno("calloc");
99 memcpy(&m->id, id, sizeof(m->id));
101 m->path = strdup(path);
102 if (m->path == NULL) {
103 err = got_error_from_errno("strdup");
104 free(m);
105 return err;
108 m->obj_type = obj_type;
109 m->mtime = mtime;
110 *new = m;
111 return NULL;
114 static void
115 clear_meta(struct got_pack_meta *meta)
117 if (meta == NULL)
118 return;
119 free(meta->path);
120 meta->path = NULL;
123 static void
124 free_nmeta(struct got_pack_meta **meta, int nmeta)
126 int i;
128 for (i = 0; i < nmeta; i++)
129 clear_meta(meta[i]);
130 free(meta);
133 static int
134 delta_order_cmp(const void *pa, const void *pb)
136 struct got_pack_meta *a, *b;
137 int cmp;
139 a = *(struct got_pack_meta **)pa;
140 b = *(struct got_pack_meta **)pb;
142 if (a->obj_type != b->obj_type)
143 return a->obj_type - b->obj_type;
144 cmp = strcmp(a->path, b->path);
145 if (cmp != 0)
146 return cmp;
147 if (a->mtime != b->mtime)
148 return a->mtime - b->mtime;
149 return got_object_id_cmp(&a->id, &b->id);
152 static int
153 delta_size(struct got_delta_instruction *deltas, int ndeltas)
155 int i, size = 32;
156 for (i = 0; i < ndeltas; i++) {
157 if (deltas[i].copy)
158 size += GOT_DELTA_SIZE_SHIFT;
159 else
160 size += deltas[i].len + 1;
162 return size;
165 static const struct got_error *
166 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
167 struct got_delta_instruction *deltas, int ndeltas,
168 off_t base_size, FILE *f)
170 unsigned char buf[16], *bp;
171 int i, j;
172 off_t n;
173 size_t w;
174 struct got_delta_instruction *d;
176 /* base object size */
177 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
178 n = base_size >> GOT_DELTA_SIZE_SHIFT;
179 for (i = 1; n > 0; i++) {
180 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
181 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
182 n >>= GOT_DELTA_SIZE_SHIFT;
184 w = fwrite(buf, 1, i, f);
185 if (w != i)
186 return got_ferror(f, GOT_ERR_IO);
188 /* target object size */
189 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
190 n = o->size >> GOT_DELTA_SIZE_SHIFT;
191 for (i = 1; n > 0; i++) {
192 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
193 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
194 n >>= GOT_DELTA_SIZE_SHIFT;
196 w = fwrite(buf, 1, i, f);
197 if (w != i)
198 return got_ferror(f, GOT_ERR_IO);
200 for (j = 0; j < ndeltas; j++) {
201 d = &deltas[j];
202 if (d->copy) {
203 n = d->offset;
204 bp = &buf[1];
205 buf[0] = GOT_DELTA_BASE_COPY;
206 for (i = 0; i < 4; i++) {
207 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
208 buf[0] |= 1 << i;
209 *bp++ = n & 0xff;
210 n >>= 8;
211 if (n == 0)
212 break;
215 n = d->len;
216 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
217 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
218 for (i = 0; i < 3 && n > 0; i++) {
219 buf[0] |= 1 << (i + 4);
220 *bp++ = n & 0xff;
221 n >>= 8;
224 w = fwrite(buf, 1, bp - buf, f);
225 if (w != bp - buf)
226 return got_ferror(f, GOT_ERR_IO);
227 } else {
228 char content[128];
229 size_t r;
230 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
231 return got_error_from_errno("fseeko");
232 n = 0;
233 while (n != d->len) {
234 buf[0] = (d->len - n < 127) ? d->len - n : 127;
235 w = fwrite(buf, 1, 1, f);
236 if (w != 1)
237 return got_ferror(f, GOT_ERR_IO);
238 r = fread(content, 1, buf[0], o->f);
239 if (r != buf[0])
240 return got_ferror(o->f, GOT_ERR_IO);
241 w = fwrite(content, 1, buf[0], f);
242 if (w != buf[0])
243 return got_ferror(f, GOT_ERR_IO);
244 n += buf[0];
249 return NULL;
253 static const struct got_error *
254 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
255 FILE *delta_cache, struct got_repository *repo,
256 got_pack_progress_cb progress_cb, void *progress_arg,
257 got_cancel_cb cancel_cb, void *cancel_arg)
259 const struct got_error *err = NULL;
260 struct got_pack_meta *m = NULL, *base = NULL;
261 struct got_raw_object *raw = NULL, *base_raw = NULL;
262 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
263 int i, j, size, best_size, ndeltas, best_ndeltas;
264 const int max_base_candidates = 10;
265 int outfd = -1;
267 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
268 for (i = 0; i < nmeta; i++) {
269 if (cancel_cb) {
270 err = (*cancel_cb)(cancel_arg);
271 if (err)
272 break;
274 if (progress_cb) {
275 err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
276 if (err)
277 goto done;
279 m = meta[i];
281 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
282 m->obj_type == GOT_OBJ_TYPE_TAG)
283 continue;
285 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
286 if (err)
287 goto done;
288 m->size = raw->size;
290 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
291 raw->size + raw->hdrlen);
292 if (err)
293 goto done;
295 if (i > max_base_candidates) {
296 struct got_pack_meta *n = NULL;
297 n = meta[i - (max_base_candidates + 1)];
298 got_deltify_free(n->dtab);
299 n->dtab = NULL;
302 best_size = raw->size;
303 best_ndeltas = 0;
304 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
305 if (cancel_cb) {
306 err = (*cancel_cb)(cancel_arg);
307 if (err)
308 goto done;
310 base = meta[j];
311 /* long chains make unpacking slow, avoid such bases */
312 if (base->nchain >= 32 ||
313 base->obj_type != m->obj_type)
314 continue;
316 err = got_object_raw_open(&base_raw, &outfd, repo,
317 &base->id);
318 if (err)
319 goto done;
320 err = got_deltify(&deltas, &ndeltas,
321 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
322 base->dtab, base_raw->f, base_raw->hdrlen,
323 base_raw->size + base_raw->hdrlen);
324 got_object_raw_close(base_raw);
325 base_raw = NULL;
326 if (err)
327 goto done;
329 size = delta_size(deltas, ndeltas);
330 if (size + 32 < best_size){
331 /*
332 * if we already picked a best delta,
333 * replace it.
334 */
335 best_size = size;
336 free(best_deltas);
337 best_deltas = deltas;
338 best_ndeltas = ndeltas;
339 deltas = NULL;
340 m->nchain = base->nchain + 1;
341 m->prev = base;
342 m->head = base->head;
343 if (m->head == NULL)
344 m->head = base;
345 } else {
346 free(deltas);
347 deltas = NULL;
348 ndeltas = 0;
352 if (best_ndeltas > 0) {
353 m->delta_offset = ftello(delta_cache);
354 err = encode_delta(m, raw, best_deltas,
355 best_ndeltas, m->prev->size, delta_cache);
356 free(best_deltas);
357 best_deltas = NULL;
358 best_ndeltas = 0;
359 if (err)
360 goto done;
361 m->delta_len = ftello(delta_cache) - m->delta_offset;
364 got_object_raw_close(raw);
365 raw = NULL;
367 done:
368 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
369 got_deltify_free(meta[i]->dtab);
370 meta[i]->dtab = NULL;
372 if (raw)
373 got_object_raw_close(raw);
374 if (base_raw)
375 got_object_raw_close(base_raw);
376 if (outfd != -1 && close(outfd) == -1 && err == NULL)
377 err = got_error_from_errno("close");
378 free(deltas);
379 free(best_deltas);
380 return err;
383 static const struct got_error *
384 search_packidx(int *found, struct got_object_id *id,
385 struct got_repository *repo)
387 const struct got_error *err = NULL;
388 struct got_packidx *packidx = NULL;
389 int idx;
391 *found = 0;
393 err = got_repo_search_packidx(&packidx, &idx, repo, id);
394 if (err == NULL)
395 *found = 1; /* object is already packed */
396 else if (err->code == GOT_ERR_NO_OBJ)
397 err = NULL;
398 return err;
401 static const int obj_types[] = {
402 GOT_OBJ_TYPE_ANY,
403 GOT_OBJ_TYPE_COMMIT,
404 GOT_OBJ_TYPE_TREE,
405 GOT_OBJ_TYPE_BLOB,
406 GOT_OBJ_TYPE_TAG,
407 GOT_OBJ_TYPE_OFFSET_DELTA,
408 GOT_OBJ_TYPE_REF_DELTA
409 };
411 static const struct got_error *
412 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
413 struct got_object_id *id, const char *path, int obj_type,
414 time_t mtime, int loose_obj_only, struct got_repository *repo)
416 const struct got_error *err;
417 struct got_pack_meta *m;
419 if (loose_obj_only) {
420 int is_packed;
421 err = search_packidx(&is_packed, id, repo);
422 if (err)
423 return err;
424 if (is_packed)
425 return NULL;
428 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
429 if (err)
430 return err;
432 if (v == NULL)
433 return NULL;
435 err = alloc_meta(&m, id, path, obj_type, mtime);
436 if (err)
437 goto done;
439 if (v->nmeta == v->metasz){
440 size_t newsize = 2 * v->metasz;
441 struct got_pack_meta **new;
442 new = reallocarray(v->meta, newsize, sizeof(*new));
443 if (new == NULL) {
444 err = got_error_from_errno("reallocarray");
445 goto done;
447 v->meta = new;
448 v->metasz = newsize;
450 done:
451 if (err) {
452 clear_meta(m);
453 free(m);
454 } else
455 v->meta[v->nmeta++] = m;
457 return err;
460 static const struct got_error *
461 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
462 struct got_object_idset *idset, struct got_object_id *tree_id,
463 const char *dpath, time_t mtime, struct got_repository *repo,
464 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
466 const struct got_error *err;
467 struct got_tree_object *tree;
468 char *p = NULL;
469 int i;
471 err = got_object_open_as_tree(&tree, repo, tree_id);
472 if (err)
473 return err;
475 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
476 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
477 struct got_object_id *id = got_tree_entry_get_id(e);
478 mode_t mode = got_tree_entry_get_mode(e);
480 if (cancel_cb) {
481 err = (*cancel_cb)(cancel_arg);
482 if (err)
483 break;
486 if (got_object_tree_entry_is_submodule(e) ||
487 got_object_idset_contains(idset, id))
488 continue;
490 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
491 got_tree_entry_get_name(e)) == -1) {
492 err = got_error_from_errno("asprintf");
493 break;
496 if (S_ISDIR(mode)) {
497 struct got_object_qid *qid;
498 err = got_object_qid_alloc(&qid, id);
499 if (err)
500 break;
501 STAILQ_INSERT_TAIL(ids, qid, entry);
502 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
503 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
504 mtime, loose_obj_only, repo);
505 if (err)
506 break;
508 free(p);
509 p = NULL;
512 got_object_tree_close(tree);
513 free(p);
514 return err;
517 static const struct got_error *
518 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
519 struct got_object_id *tree_id, const char *dpath, time_t mtime,
520 int loose_obj_only, struct got_repository *repo,
521 got_cancel_cb cancel_cb, void *cancel_arg)
523 const struct got_error *err = NULL;
524 struct got_object_id_queue tree_ids;
525 struct got_object_qid *qid;
527 if (got_object_idset_contains(idset, tree_id))
528 return NULL;
530 err = got_object_qid_alloc(&qid, tree_id);
531 if (err)
532 return err;
534 STAILQ_INIT(&tree_ids);
535 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
537 while (!STAILQ_EMPTY(&tree_ids)) {
538 if (cancel_cb) {
539 err = (*cancel_cb)(cancel_arg);
540 if (err)
541 break;
544 qid = STAILQ_FIRST(&tree_ids);
545 STAILQ_REMOVE_HEAD(&tree_ids, entry);
547 if (got_object_idset_contains(idset, qid->id)) {
548 got_object_qid_free(qid);
549 continue;
552 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
553 mtime, loose_obj_only, repo);
554 if (err) {
555 got_object_qid_free(qid);
556 break;
559 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
560 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
561 got_object_qid_free(qid);
562 if (err)
563 break;
566 got_object_id_queue_free(&tree_ids);
567 return err;
570 static const struct got_error *
571 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
572 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
573 got_cancel_cb cancel_cb, void *cancel_arg)
575 const struct got_error *err;
576 struct got_commit_object *commit;
578 if (got_object_idset_contains(idset, id))
579 return NULL;
581 if (loose_obj_only) {
582 int is_packed;
583 err = search_packidx(&is_packed, id, repo);
584 if (err)
585 return err;
586 if (is_packed)
587 return NULL;
590 err = got_object_open_as_commit(&commit, repo, id);
591 if (err)
592 return err;
594 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
595 got_object_commit_get_committer_time(commit),
596 loose_obj_only, repo);
597 if (err)
598 goto done;
600 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
601 "", got_object_commit_get_committer_time(commit),
602 loose_obj_only, repo, cancel_cb, cancel_arg);
603 done:
604 got_object_commit_close(commit);
605 return err;
608 static const struct got_error *
609 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
610 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
611 got_cancel_cb cancel_cb, void *cancel_arg)
613 const struct got_error *err;
614 struct got_tag_object *tag = NULL;
616 if (got_object_idset_contains(idset, id))
617 return NULL;
619 if (loose_obj_only) {
620 int is_packed;
621 err = search_packidx(&is_packed, id, repo);
622 if (err)
623 return err;
624 if (is_packed)
625 return NULL;
628 err = got_object_open_as_tag(&tag, repo, id);
629 if (err)
630 return err;
632 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
633 got_object_tag_get_tagger_time(tag),
634 loose_obj_only, repo);
635 if (err)
636 goto done;
638 switch (got_object_tag_get_object_type(tag)) {
639 case GOT_OBJ_TYPE_COMMIT:
640 err = load_commit(v, idset,
641 got_object_tag_get_object_id(tag), repo,
642 loose_obj_only, cancel_cb, cancel_arg);
643 break;
644 case GOT_OBJ_TYPE_TREE:
645 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
646 "", got_object_tag_get_tagger_time(tag),
647 loose_obj_only, repo, cancel_cb, cancel_arg);
648 break;
649 default:
650 break;
653 done:
654 got_object_tag_close(tag);
655 return err;
658 enum findtwixt_color {
659 COLOR_KEEP = 0,
660 COLOR_DROP,
661 COLOR_BLANK,
662 };
663 static const int findtwixt_colors[] = {
664 COLOR_KEEP,
665 COLOR_DROP,
666 COLOR_BLANK
667 };
669 static const struct got_error *
670 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
671 int color, struct got_repository *repo)
673 const struct got_error *err;
674 struct got_object_qid *qid;
676 err = got_object_qid_alloc(&qid, id);
677 if (err)
678 return err;
680 STAILQ_INSERT_TAIL(ids, qid, entry);
681 qid->data = (void *)&findtwixt_colors[color];
682 return NULL;
685 static const struct got_error *
686 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
687 struct got_object_id *id, struct got_repository *repo,
688 got_cancel_cb cancel_cb, void *cancel_arg)
690 const struct got_error *err = NULL;
691 struct got_commit_object *commit;
692 const struct got_object_id_queue *parents;
693 struct got_object_id_queue ids;
694 struct got_object_qid *qid;
696 STAILQ_INIT(&ids);
698 err = got_object_qid_alloc(&qid, id);
699 if (err)
700 return err;
701 STAILQ_INSERT_HEAD(&ids, qid, entry);
703 while (!STAILQ_EMPTY(&ids)) {
704 if (cancel_cb) {
705 err = (*cancel_cb)(cancel_arg);
706 if (err)
707 break;
710 qid = STAILQ_FIRST(&ids);
711 STAILQ_REMOVE_HEAD(&ids, entry);
713 if (got_object_idset_contains(drop, qid->id)) {
714 got_object_qid_free(qid);
715 continue;
718 err = got_object_idset_add(drop, qid->id,
719 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
720 if (err) {
721 got_object_qid_free(qid);
722 break;
725 if (!got_object_idset_contains(keep, qid->id)) {
726 got_object_qid_free(qid);
727 continue;
730 err = got_object_open_as_commit(&commit, repo, qid->id);
731 got_object_qid_free(qid);
732 if (err)
733 break;
735 parents = got_object_commit_get_parent_ids(commit);
736 if (parents) {
737 err = got_object_id_queue_copy(parents, &ids);
738 if (err) {
739 got_object_commit_close(commit);
740 break;
743 got_object_commit_close(commit);
746 got_object_id_queue_free(&ids);
747 return err;
750 struct append_id_arg {
751 struct got_object_id **array;
752 int idx;
753 };
755 static const struct got_error *
756 append_id(struct got_object_id *id, void *data, void *arg)
758 struct append_id_arg *a = arg;
760 a->array[a->idx] = got_object_id_dup(id);
761 if (a->array[a->idx] == NULL)
762 return got_error_from_errno("got_object_id_dup");
764 a->idx++;
765 return NULL;
768 static const struct got_error *
769 findtwixt(struct got_object_id ***res, int *nres,
770 struct got_object_id **head, int nhead,
771 struct got_object_id **tail, int ntail,
772 struct got_repository *repo,
773 got_cancel_cb cancel_cb, void *cancel_arg)
775 const struct got_error *err = NULL;
776 struct got_object_id_queue ids;
777 struct got_object_idset *keep, *drop;
778 struct got_object_qid *qid;
779 int i, ncolor, nkeep, obj_type;
781 STAILQ_INIT(&ids);
782 *res = NULL;
783 *nres = 0;
785 keep = got_object_idset_alloc();
786 if (keep == NULL)
787 return got_error_from_errno("got_object_idset_alloc");
789 drop = got_object_idset_alloc();
790 if (drop == NULL) {
791 err = got_error_from_errno("got_object_idset_alloc");
792 goto done;
795 for (i = 0; i < nhead; i++) {
796 struct got_object_id *id = head[i];
797 if (id == NULL)
798 continue;
799 err = got_object_get_type(&obj_type, repo, id);
800 if (err)
801 return err;
802 if (obj_type != GOT_OBJ_TYPE_COMMIT)
803 continue;
804 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
805 if (err)
806 goto done;
808 for (i = 0; i < ntail; i++) {
809 struct got_object_id *id = tail[i];
810 if (id == NULL)
811 continue;
812 err = got_object_get_type(&obj_type, repo, id);
813 if (err)
814 return err;
815 if (obj_type != GOT_OBJ_TYPE_COMMIT)
816 continue;
817 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
818 if (err)
819 goto done;
822 while (!STAILQ_EMPTY(&ids)) {
823 int qcolor;
824 qid = STAILQ_FIRST(&ids);
825 qcolor = *((int *)qid->data);
827 if (got_object_idset_contains(drop, qid->id))
828 ncolor = COLOR_DROP;
829 else if (got_object_idset_contains(keep, qid->id))
830 ncolor = COLOR_KEEP;
831 else
832 ncolor = COLOR_BLANK;
834 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
835 qcolor == COLOR_KEEP)) {
836 STAILQ_REMOVE_HEAD(&ids, entry);
837 got_object_qid_free(qid);
838 continue;
841 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
842 err = drop_commit(keep, drop, qid->id, repo,
843 cancel_cb, cancel_arg);
844 if (err)
845 goto done;
846 } else if (ncolor == COLOR_BLANK) {
847 struct got_commit_object *commit;
848 struct got_object_id *id;
849 const struct got_object_id_queue *parents;
850 struct got_object_qid *pid;
852 id = got_object_id_dup(qid->id);
853 if (id == NULL) {
854 err = got_error_from_errno("got_object_id_dup");
855 goto done;
857 if (qcolor == COLOR_KEEP)
858 err = got_object_idset_add(keep, id,
859 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
860 else
861 err = got_object_idset_add(drop, id,
862 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
863 if (err) {
864 free(id);
865 goto done;
868 err = got_object_open_as_commit(&commit, repo, id);
869 if (err) {
870 free(id);
871 goto done;
873 parents = got_object_commit_get_parent_ids(commit);
874 if (parents) {
875 STAILQ_FOREACH(pid, parents, entry) {
876 err = queue_commit_id(&ids, pid->id,
877 qcolor, repo);
878 if (err) {
879 free(id);
880 goto done;
884 got_object_commit_close(commit);
885 commit = NULL;
886 } else {
887 /* should not happen */
888 err = got_error_fmt(GOT_ERR_NOT_IMPL,
889 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
890 goto done;
893 STAILQ_REMOVE_HEAD(&ids, entry);
894 got_object_qid_free(qid);
897 nkeep = got_object_idset_num_elements(keep);
898 if (nkeep > 0) {
899 struct append_id_arg arg;
900 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
901 if (arg.array == NULL) {
902 err = got_error_from_errno("calloc");
903 goto done;
905 arg.idx = 0;
906 err = got_object_idset_for_each(keep, append_id, &arg);
907 if (err) {
908 free(arg.array);
909 goto done;
911 *res = arg.array;
912 *nres = nkeep;
914 done:
915 got_object_idset_free(keep);
916 got_object_idset_free(drop);
917 got_object_id_queue_free(&ids);
918 return err;
921 static const struct got_error *
922 read_meta(struct got_pack_meta ***meta, int *nmeta,
923 struct got_object_id **theirs, int ntheirs,
924 struct got_object_id **ours, int nours, struct got_repository *repo,
925 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
926 got_cancel_cb cancel_cb, void *cancel_arg)
928 const struct got_error *err = NULL;
929 struct got_object_id **ids = NULL;
930 struct got_object_idset *idset;
931 int i, nobj = 0, obj_type;
932 struct got_pack_metavec v;
934 *meta = NULL;
935 *nmeta = 0;
937 idset = got_object_idset_alloc();
938 if (idset == NULL)
939 return got_error_from_errno("got_object_idset_alloc");
941 v.nmeta = 0;
942 v.metasz = 64;
943 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
944 if (v.meta == NULL) {
945 err = got_error_from_errno("calloc");
946 goto done;
949 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
950 cancel_cb, cancel_arg);
951 if (err || nobj == 0)
952 goto done;
954 for (i = 0; i < ntheirs; i++) {
955 struct got_object_id *id = theirs[i];
956 if (id == NULL)
957 continue;
958 err = got_object_get_type(&obj_type, repo, id);
959 if (err)
960 return err;
961 if (obj_type != GOT_OBJ_TYPE_COMMIT)
962 continue;
963 err = load_commit(NULL, idset, id, repo,
964 loose_obj_only, cancel_cb, cancel_arg);
965 if (err)
966 goto done;
967 if (progress_cb) {
968 err = progress_cb(progress_arg, 0L, nours,
969 v.nmeta, 0, 0);
970 if (err)
971 goto done;
975 for (i = 0; i < ntheirs; i++) {
976 struct got_object_id *id = theirs[i];
977 int *cached_type;
978 if (id == NULL)
979 continue;
980 cached_type = got_object_idset_get(idset, id);
981 if (cached_type == NULL) {
982 err = got_object_get_type(&obj_type, repo, id);
983 if (err)
984 goto done;
985 } else
986 obj_type = *cached_type;
987 if (obj_type != GOT_OBJ_TYPE_TAG)
988 continue;
989 err = load_tag(NULL, idset, id, repo,
990 loose_obj_only, cancel_cb, cancel_arg);
991 if (err)
992 goto done;
993 if (progress_cb) {
994 err = progress_cb(progress_arg, 0L, nours,
995 v.nmeta, 0, 0);
996 if (err)
997 goto done;
1001 for (i = 0; i < nobj; i++) {
1002 err = load_commit(&v, idset, ids[i], repo,
1003 loose_obj_only, cancel_cb, cancel_arg);
1004 if (err)
1005 goto done;
1006 if (progress_cb) {
1007 err = progress_cb(progress_arg, 0L, nours,
1008 v.nmeta, 0, 0);
1009 if (err)
1010 goto done;
1014 for (i = 0; i < nours; i++) {
1015 struct got_object_id *id = ours[i];
1016 int *cached_type;
1017 if (id == NULL)
1018 continue;
1019 cached_type = got_object_idset_get(idset, id);
1020 if (cached_type == NULL) {
1021 err = got_object_get_type(&obj_type, repo, id);
1022 if (err)
1023 goto done;
1024 } else
1025 obj_type = *cached_type;
1026 if (obj_type != GOT_OBJ_TYPE_TAG)
1027 continue;
1028 err = load_tag(&v, idset, id, repo,
1029 loose_obj_only, cancel_cb, cancel_arg);
1030 if (err)
1031 goto done;
1032 if (progress_cb) {
1033 err = progress_cb(progress_arg, 0L, nours,
1034 v.nmeta, 0, 0);
1035 if (err)
1036 goto done;
1040 done:
1041 for (i = 0; i < nobj; i++) {
1042 free(ids[i]);
1044 free(ids);
1045 got_object_idset_free(idset);
1046 if (err == NULL) {
1047 *meta = v.meta;
1048 *nmeta = v.nmeta;
1049 } else
1050 free(v.meta);
1052 return err;
1055 const struct got_error *
1056 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1058 size_t n;
1060 SHA1Update(ctx, buf, len);
1061 n = fwrite(buf, 1, len, f);
1062 if (n != len)
1063 return got_ferror(f, GOT_ERR_IO);
1064 return NULL;
1067 static void
1068 putbe32(char *b, uint32_t n)
1070 b[0] = n >> 24;
1071 b[1] = n >> 16;
1072 b[2] = n >> 8;
1073 b[3] = n >> 0;
1076 static int
1077 write_order_cmp(const void *pa, const void *pb)
1079 struct got_pack_meta *a, *b, *ahd, *bhd;
1081 a = *(struct got_pack_meta **)pa;
1082 b = *(struct got_pack_meta **)pb;
1083 ahd = (a->head == NULL) ? a : a->head;
1084 bhd = (b->head == NULL) ? b : b->head;
1085 if (ahd->mtime != bhd->mtime)
1086 return bhd->mtime - ahd->mtime;
1087 if (ahd != bhd)
1088 return (uintptr_t)bhd - (uintptr_t)ahd;
1089 if (a->nchain != b->nchain)
1090 return a->nchain - b->nchain;
1091 return a->mtime - b->mtime;
1094 static const struct got_error *
1095 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1097 size_t i;
1099 *hdrlen = 0;
1101 hdr[0] = obj_type << 4;
1102 hdr[0] |= len & 0xf;
1103 len >>= 4;
1104 for (i = 1; len != 0; i++){
1105 if (i >= bufsize)
1106 return got_error(GOT_ERR_NO_SPACE);
1107 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1108 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1109 len >>= GOT_DELTA_SIZE_SHIFT;
1112 *hdrlen = i;
1113 return NULL;
1116 static int
1117 packoff(char *hdr, off_t off)
1119 int i, j;
1120 char rbuf[8];
1122 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1123 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1124 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1125 GOT_DELTA_SIZE_MORE;
1128 j = 0;
1129 while (i > 0)
1130 hdr[j++] = rbuf[--i];
1131 return j;
1134 static const struct got_error *
1135 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1136 struct got_pack_meta **meta, int nmeta, int nours,
1137 int use_offset_deltas, struct got_repository *repo,
1138 got_pack_progress_cb progress_cb, void *progress_arg,
1139 got_cancel_cb cancel_cb, void *cancel_arg)
1141 const struct got_error *err = NULL;
1142 int i, nh;
1143 SHA1_CTX ctx;
1144 struct got_pack_meta *m;
1145 struct got_raw_object *raw = NULL;
1146 FILE *delta_file = NULL;
1147 char buf[32];
1148 size_t outlen, n;
1149 struct got_deflate_checksum csum;
1150 off_t packfile_size = 0;
1151 int outfd = -1;
1153 SHA1Init(&ctx);
1154 csum.output_sha1 = &ctx;
1155 csum.output_crc = NULL;
1157 err = hwrite(packfile, "PACK", 4, &ctx);
1158 if (err)
1159 return err;
1160 putbe32(buf, GOT_PACKFILE_VERSION);
1161 err = hwrite(packfile, buf, 4, &ctx);
1162 if (err)
1163 goto done;
1164 putbe32(buf, nmeta);
1165 err = hwrite(packfile, buf, 4, &ctx);
1166 if (err)
1167 goto done;
1168 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1169 for (i = 0; i < nmeta; i++) {
1170 if (progress_cb) {
1171 err = progress_cb(progress_arg, packfile_size, nours,
1172 nmeta, nmeta, i);
1173 if (err)
1174 goto done;
1176 m = meta[i];
1177 m->off = ftello(packfile);
1178 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1179 if (err)
1180 goto done;
1181 if (m->delta_len == 0) {
1182 err = packhdr(&nh, buf, sizeof(buf),
1183 m->obj_type, raw->size);
1184 if (err)
1185 goto done;
1186 err = hwrite(packfile, buf, nh, &ctx);
1187 if (err)
1188 goto done;
1189 packfile_size += nh;
1190 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1191 err = got_error_from_errno("fseeko");
1192 goto done;
1194 err = got_deflate_to_file(&outlen, raw->f, packfile,
1195 &csum);
1196 if (err)
1197 goto done;
1198 packfile_size += outlen;
1199 } else {
1200 off_t remain;
1201 if (delta_file == NULL) {
1202 delta_file = got_opentemp();
1203 if (delta_file == NULL) {
1204 err = got_error_from_errno(
1205 "got_opentemp");
1206 goto done;
1209 if (ftruncate(fileno(delta_file), 0L) == -1) {
1210 err = got_error_from_errno("ftruncate");
1211 goto done;
1213 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1214 err = got_error_from_errno("fseeko");
1215 goto done;
1217 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1218 == -1) {
1219 err = got_error_from_errno("fseeko");
1220 goto done;
1222 remain = m->delta_len;
1223 while (remain > 0) {
1224 char delta_buf[8192];
1225 size_t r, w, n;
1226 n = MIN(remain, sizeof(delta_buf));
1227 r = fread(delta_buf, 1, n, delta_cache);
1228 if (r != n) {
1229 err = got_ferror(delta_cache,
1230 GOT_ERR_IO);
1231 goto done;
1233 w = fwrite(delta_buf, 1, n, delta_file);
1234 if (w != n) {
1235 err = got_ferror(delta_file,
1236 GOT_ERR_IO);
1237 goto done;
1239 remain -= n;
1241 if (use_offset_deltas && m->prev->off != 0) {
1242 err = packhdr(&nh, buf, sizeof(buf),
1243 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1244 if (err)
1245 goto done;
1246 nh += packoff(buf + nh,
1247 m->off - m->prev->off);
1248 err = hwrite(packfile, buf, nh, &ctx);
1249 if (err)
1250 goto done;
1251 packfile_size += nh;
1252 } else {
1253 err = packhdr(&nh, buf, sizeof(buf),
1254 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1255 err = hwrite(packfile, buf, nh, &ctx);
1256 if (err)
1257 goto done;
1258 packfile_size += nh;
1259 err = hwrite(packfile, m->prev->id.sha1,
1260 sizeof(m->prev->id.sha1), &ctx);
1261 packfile_size += sizeof(m->prev->id.sha1);
1262 if (err)
1263 goto done;
1265 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1266 err = got_error_from_errno("fseeko");
1267 goto done;
1269 err = got_deflate_to_file(&outlen, delta_file,
1270 packfile, &csum);
1271 if (err)
1272 goto done;
1273 packfile_size += outlen;
1275 got_object_raw_close(raw);
1276 raw = NULL;
1278 SHA1Final(pack_sha1, &ctx);
1279 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1280 if (n != SHA1_DIGEST_LENGTH)
1281 err = got_ferror(packfile, GOT_ERR_IO);
1282 packfile_size += SHA1_DIGEST_LENGTH;
1283 packfile_size += sizeof(struct got_packfile_hdr);
1284 err = progress_cb(progress_arg, packfile_size, nours,
1285 nmeta, nmeta, nmeta);
1286 if (err)
1287 goto done;
1288 done:
1289 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1290 err = got_error_from_errno("fclose");
1291 if (raw)
1292 got_object_raw_close(raw);
1293 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1294 err = got_error_from_errno("close");
1295 return err;
1298 const struct got_error *
1299 got_pack_create(uint8_t *packsha1, FILE *packfile,
1300 struct got_object_id **theirs, int ntheirs,
1301 struct got_object_id **ours, int nours,
1302 struct got_repository *repo, int loose_obj_only, int allow_empty,
1303 got_pack_progress_cb progress_cb, void *progress_arg,
1304 got_cancel_cb cancel_cb, void *cancel_arg)
1306 const struct got_error *err;
1307 struct got_pack_meta **meta;
1308 int nmeta;
1309 FILE *delta_cache = NULL;
1311 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1312 loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1313 if (err)
1314 return err;
1316 if (nmeta == 0 && !allow_empty) {
1317 err = got_error(GOT_ERR_CANNOT_PACK);
1318 goto done;
1321 delta_cache = got_opentemp();
1322 if (delta_cache == NULL) {
1323 err = got_error_from_errno("got_opentemp");
1324 goto done;
1327 if (nmeta > 0) {
1328 err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1329 progress_cb, progress_arg, cancel_cb, cancel_arg);
1330 if (err)
1331 goto done;
1332 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1333 err = got_error_from_errno("fseeko");
1334 goto done;
1338 err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1339 repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1340 if (err)
1341 goto done;
1342 done:
1343 free_nmeta(meta, nmeta);
1344 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1345 err = got_error_from_errno("fclose");
1346 return err;