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>
23 #include <sys/time.h>
25 #include <stdint.h>
26 #include <imsg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sha1.h>
31 #include <time.h>
32 #include <limits.h>
33 #include <zlib.h>
35 #include "got_error.h"
36 #include "got_cancel.h"
37 #include "got_object.h"
38 #include "got_path.h"
39 #include "got_reference.h"
40 #include "got_repository_admin.h"
41 #include "got_opentemp.h"
43 #include "got_lib_deltify.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_deflate.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_ratelimit.h"
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 #ifndef MAX
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 #endif
62 struct got_pack_meta {
63 struct got_object_id id;
64 char *path;
65 int obj_type;
66 off_t size;
67 time_t mtime;
69 /* The best delta we picked */
70 struct got_pack_meta *head;
71 struct got_pack_meta *prev;
72 off_t delta_offset; /* offset in delta cache file */
73 off_t delta_len; /* length in delta cache file */
74 int nchain;
76 /* Only used for delta window */
77 struct got_delta_table *dtab;
79 /* Only used for writing offset deltas */
80 off_t off;
81 };
83 struct got_pack_metavec {
84 struct got_pack_meta **meta;
85 int nmeta;
86 int metasz;
87 };
89 static const struct got_error *
90 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
91 const char *path, int obj_type, time_t mtime)
92 {
93 const struct got_error *err = NULL;
94 struct got_pack_meta *m;
96 *new = NULL;
98 m = calloc(1, sizeof(*m));
99 if (m == NULL)
100 return got_error_from_errno("calloc");
102 memcpy(&m->id, id, sizeof(m->id));
104 m->path = strdup(path);
105 if (m->path == NULL) {
106 err = got_error_from_errno("strdup");
107 free(m);
108 return err;
111 m->obj_type = obj_type;
112 m->mtime = mtime;
113 *new = m;
114 return NULL;
117 static void
118 clear_meta(struct got_pack_meta *meta)
120 if (meta == NULL)
121 return;
122 free(meta->path);
123 meta->path = NULL;
126 static void
127 free_nmeta(struct got_pack_meta **meta, int nmeta)
129 int i;
131 for (i = 0; i < nmeta; i++)
132 clear_meta(meta[i]);
133 free(meta);
136 static int
137 delta_order_cmp(const void *pa, const void *pb)
139 struct got_pack_meta *a, *b;
140 int cmp;
142 a = *(struct got_pack_meta **)pa;
143 b = *(struct got_pack_meta **)pb;
145 if (a->obj_type != b->obj_type)
146 return a->obj_type - b->obj_type;
147 cmp = strcmp(a->path, b->path);
148 if (cmp != 0)
149 return cmp;
150 if (a->mtime != b->mtime)
151 return a->mtime - b->mtime;
152 return got_object_id_cmp(&a->id, &b->id);
155 static int
156 delta_size(struct got_delta_instruction *deltas, int ndeltas)
158 int i, size = 32;
159 for (i = 0; i < ndeltas; i++) {
160 if (deltas[i].copy)
161 size += GOT_DELTA_SIZE_SHIFT;
162 else
163 size += deltas[i].len + 1;
165 return size;
168 static const struct got_error *
169 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
170 struct got_delta_instruction *deltas, int ndeltas,
171 off_t base_size, FILE *f)
173 unsigned char buf[16], *bp;
174 int i, j;
175 off_t n;
176 size_t w;
177 struct got_delta_instruction *d;
179 /* base object size */
180 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
181 n = base_size >> GOT_DELTA_SIZE_SHIFT;
182 for (i = 1; n > 0; i++) {
183 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
184 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
185 n >>= GOT_DELTA_SIZE_SHIFT;
187 w = fwrite(buf, 1, i, f);
188 if (w != i)
189 return got_ferror(f, GOT_ERR_IO);
191 /* target object size */
192 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
193 n = o->size >> GOT_DELTA_SIZE_SHIFT;
194 for (i = 1; n > 0; i++) {
195 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
196 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
197 n >>= GOT_DELTA_SIZE_SHIFT;
199 w = fwrite(buf, 1, i, f);
200 if (w != i)
201 return got_ferror(f, GOT_ERR_IO);
203 for (j = 0; j < ndeltas; j++) {
204 d = &deltas[j];
205 if (d->copy) {
206 n = d->offset;
207 bp = &buf[1];
208 buf[0] = GOT_DELTA_BASE_COPY;
209 for (i = 0; i < 4; i++) {
210 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
211 buf[0] |= 1 << i;
212 *bp++ = n & 0xff;
213 n >>= 8;
214 if (n == 0)
215 break;
218 n = d->len;
219 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
220 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
221 for (i = 0; i < 3 && n > 0; i++) {
222 buf[0] |= 1 << (i + 4);
223 *bp++ = n & 0xff;
224 n >>= 8;
227 w = fwrite(buf, 1, bp - buf, f);
228 if (w != bp - buf)
229 return got_ferror(f, GOT_ERR_IO);
230 } else if (o->f == NULL) {
231 n = 0;
232 while (n != d->len) {
233 buf[0] = (d->len - n < 127) ? d->len - n : 127;
234 w = fwrite(buf, 1, 1, f);
235 if (w != 1)
236 return got_ferror(f, GOT_ERR_IO);
237 w = fwrite(o->data + o->hdrlen + d->offset + n,
238 1, buf[0], f);
239 if (w != buf[0])
240 return got_ferror(f, GOT_ERR_IO);
241 n += buf[0];
243 } else {
244 char content[128];
245 size_t r;
246 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
247 return got_error_from_errno("fseeko");
248 n = 0;
249 while (n != d->len) {
250 buf[0] = (d->len - n < 127) ? d->len - n : 127;
251 w = fwrite(buf, 1, 1, f);
252 if (w != 1)
253 return got_ferror(f, GOT_ERR_IO);
254 r = fread(content, 1, buf[0], o->f);
255 if (r != buf[0])
256 return got_ferror(o->f, GOT_ERR_IO);
257 w = fwrite(content, 1, buf[0], f);
258 if (w != buf[0])
259 return got_ferror(f, GOT_ERR_IO);
260 n += buf[0];
265 return NULL;
268 static const struct got_error *
269 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
270 struct got_ratelimit *rl, off_t packfile_size, int ncommits,
271 int nobj_total, int obj_deltify, int nobj_written)
273 const struct got_error *err;
274 int elapsed;
276 if (progress_cb == NULL)
277 return NULL;
279 err = got_ratelimit_check(&elapsed, rl);
280 if (err || !elapsed)
281 return err;
283 return progress_cb(progress_arg, packfile_size, ncommits,
284 nobj_total, obj_deltify, nobj_written);
287 static const struct got_error *
288 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
289 FILE *delta_cache, struct got_repository *repo,
290 got_pack_progress_cb progress_cb, void *progress_arg,
291 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
293 const struct got_error *err = NULL;
294 struct got_pack_meta *m = NULL, *base = NULL;
295 struct got_raw_object *raw = NULL, *base_raw = NULL;
296 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
297 int i, j, size, best_size, ndeltas, best_ndeltas;
298 const int max_base_candidates = 3;
299 int outfd = -1;
301 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
302 for (i = 0; i < nmeta; i++) {
303 if (cancel_cb) {
304 err = (*cancel_cb)(cancel_arg);
305 if (err)
306 break;
308 err = report_progress(progress_cb, progress_arg, rl,
309 0L, nours, nmeta, i, 0);
310 if (err)
311 goto done;
312 m = meta[i];
314 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
315 m->obj_type == GOT_OBJ_TYPE_TAG)
316 continue;
318 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
319 if (err)
320 goto done;
321 m->size = raw->size;
323 if (raw->f == NULL) {
324 err = got_deltify_init_mem(&m->dtab, raw->data,
325 raw->hdrlen, raw->size + raw->hdrlen);
326 } else {
327 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
328 raw->size + raw->hdrlen);
330 if (err)
331 goto done;
333 if (i > max_base_candidates) {
334 struct got_pack_meta *n = NULL;
335 n = meta[i - (max_base_candidates + 1)];
336 got_deltify_free(n->dtab);
337 n->dtab = NULL;
340 best_size = raw->size;
341 best_ndeltas = 0;
342 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
343 if (cancel_cb) {
344 err = (*cancel_cb)(cancel_arg);
345 if (err)
346 goto done;
348 base = meta[j];
349 /* long chains make unpacking slow, avoid such bases */
350 if (base->nchain >= 128 ||
351 base->obj_type != m->obj_type)
352 continue;
354 err = got_object_raw_open(&base_raw, &outfd, repo,
355 &base->id);
356 if (err)
357 goto done;
358 if (raw->f == NULL && base_raw->f == NULL) {
359 err = got_deltify_mem_mem(&deltas, &ndeltas,
360 raw->data, raw->hdrlen,
361 raw->size + raw->hdrlen,
362 base->dtab, base_raw->data,
363 base_raw->hdrlen,
364 base_raw->size + base_raw->hdrlen);
365 } else if (raw->f == NULL) {
366 err = got_deltify_mem_file(&deltas, &ndeltas,
367 raw->data, raw->hdrlen,
368 raw->size + raw->hdrlen,
369 base->dtab, base_raw->f,
370 base_raw->hdrlen,
371 base_raw->size + base_raw->hdrlen);
372 } else if (base_raw->f == NULL) {
373 err = got_deltify_file_mem(&deltas, &ndeltas,
374 raw->f, raw->hdrlen,
375 raw->size + raw->hdrlen,
376 base->dtab, base_raw->data,
377 base_raw->hdrlen,
378 base_raw->size + base_raw->hdrlen);
379 } else {
380 err = got_deltify(&deltas, &ndeltas,
381 raw->f, raw->hdrlen,
382 raw->size + raw->hdrlen,
383 base->dtab, base_raw->f, base_raw->hdrlen,
384 base_raw->size + base_raw->hdrlen);
386 got_object_raw_close(base_raw);
387 base_raw = NULL;
388 if (err)
389 goto done;
391 size = delta_size(deltas, ndeltas);
392 if (size + 32 < best_size){
393 /*
394 * if we already picked a best delta,
395 * replace it.
396 */
397 best_size = size;
398 free(best_deltas);
399 best_deltas = deltas;
400 best_ndeltas = ndeltas;
401 deltas = NULL;
402 m->nchain = base->nchain + 1;
403 m->prev = base;
404 m->head = base->head;
405 if (m->head == NULL)
406 m->head = base;
407 } else {
408 free(deltas);
409 deltas = NULL;
410 ndeltas = 0;
414 if (best_ndeltas > 0) {
415 m->delta_offset = ftello(delta_cache);
416 err = encode_delta(m, raw, best_deltas,
417 best_ndeltas, m->prev->size, delta_cache);
418 free(best_deltas);
419 best_deltas = NULL;
420 best_ndeltas = 0;
421 if (err)
422 goto done;
423 m->delta_len = ftello(delta_cache) - m->delta_offset;
426 got_object_raw_close(raw);
427 raw = NULL;
429 done:
430 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
431 got_deltify_free(meta[i]->dtab);
432 meta[i]->dtab = NULL;
434 if (raw)
435 got_object_raw_close(raw);
436 if (base_raw)
437 got_object_raw_close(base_raw);
438 if (outfd != -1 && close(outfd) == -1 && err == NULL)
439 err = got_error_from_errno("close");
440 free(deltas);
441 free(best_deltas);
442 return err;
445 static const struct got_error *
446 search_packidx(int *found, struct got_object_id *id,
447 struct got_repository *repo)
449 const struct got_error *err = NULL;
450 struct got_packidx *packidx = NULL;
451 int idx;
453 *found = 0;
455 err = got_repo_search_packidx(&packidx, &idx, repo, id);
456 if (err == NULL)
457 *found = 1; /* object is already packed */
458 else if (err->code == GOT_ERR_NO_OBJ)
459 err = NULL;
460 return err;
463 static const int obj_types[] = {
464 GOT_OBJ_TYPE_ANY,
465 GOT_OBJ_TYPE_COMMIT,
466 GOT_OBJ_TYPE_TREE,
467 GOT_OBJ_TYPE_BLOB,
468 GOT_OBJ_TYPE_TAG,
469 GOT_OBJ_TYPE_OFFSET_DELTA,
470 GOT_OBJ_TYPE_REF_DELTA
471 };
473 static const struct got_error *
474 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
475 struct got_object_id *id, const char *path, int obj_type,
476 time_t mtime, int loose_obj_only, struct got_repository *repo)
478 const struct got_error *err;
479 struct got_pack_meta *m;
481 if (loose_obj_only) {
482 int is_packed;
483 err = search_packidx(&is_packed, id, repo);
484 if (err)
485 return err;
486 if (is_packed)
487 return NULL;
490 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
491 if (err)
492 return err;
494 if (v == NULL)
495 return NULL;
497 err = alloc_meta(&m, id, path, obj_type, mtime);
498 if (err)
499 goto done;
501 if (v->nmeta == v->metasz){
502 size_t newsize = 2 * v->metasz;
503 struct got_pack_meta **new;
504 new = reallocarray(v->meta, newsize, sizeof(*new));
505 if (new == NULL) {
506 err = got_error_from_errno("reallocarray");
507 goto done;
509 v->meta = new;
510 v->metasz = newsize;
512 done:
513 if (err) {
514 clear_meta(m);
515 free(m);
516 } else
517 v->meta[v->nmeta++] = m;
519 return err;
522 static const struct got_error *
523 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
524 struct got_object_idset *idset, struct got_object_id *tree_id,
525 const char *dpath, time_t mtime, struct got_repository *repo,
526 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
528 const struct got_error *err;
529 struct got_tree_object *tree;
530 char *p = NULL;
531 int i;
533 err = got_object_open_as_tree(&tree, repo, tree_id);
534 if (err)
535 return err;
537 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
538 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
539 struct got_object_id *id = got_tree_entry_get_id(e);
540 mode_t mode = got_tree_entry_get_mode(e);
542 if (cancel_cb) {
543 err = (*cancel_cb)(cancel_arg);
544 if (err)
545 break;
548 if (got_object_tree_entry_is_submodule(e) ||
549 got_object_idset_contains(idset, id))
550 continue;
552 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
553 got_tree_entry_get_name(e)) == -1) {
554 err = got_error_from_errno("asprintf");
555 break;
558 if (S_ISDIR(mode)) {
559 struct got_object_qid *qid;
560 err = got_object_qid_alloc(&qid, id);
561 if (err)
562 break;
563 STAILQ_INSERT_TAIL(ids, qid, entry);
564 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
565 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
566 mtime, loose_obj_only, repo);
567 if (err)
568 break;
570 free(p);
571 p = NULL;
574 got_object_tree_close(tree);
575 free(p);
576 return err;
579 static const struct got_error *
580 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
581 struct got_object_id *tree_id, const char *dpath, time_t mtime,
582 int loose_obj_only, struct got_repository *repo,
583 got_cancel_cb cancel_cb, void *cancel_arg)
585 const struct got_error *err = NULL;
586 struct got_object_id_queue tree_ids;
587 struct got_object_qid *qid;
589 if (got_object_idset_contains(idset, tree_id))
590 return NULL;
592 err = got_object_qid_alloc(&qid, tree_id);
593 if (err)
594 return err;
596 STAILQ_INIT(&tree_ids);
597 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
599 while (!STAILQ_EMPTY(&tree_ids)) {
600 if (cancel_cb) {
601 err = (*cancel_cb)(cancel_arg);
602 if (err)
603 break;
606 qid = STAILQ_FIRST(&tree_ids);
607 STAILQ_REMOVE_HEAD(&tree_ids, entry);
609 if (got_object_idset_contains(idset, qid->id)) {
610 got_object_qid_free(qid);
611 continue;
614 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
615 mtime, loose_obj_only, repo);
616 if (err) {
617 got_object_qid_free(qid);
618 break;
621 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
622 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
623 got_object_qid_free(qid);
624 if (err)
625 break;
628 got_object_id_queue_free(&tree_ids);
629 return err;
632 static const struct got_error *
633 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
634 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
635 got_cancel_cb cancel_cb, void *cancel_arg)
637 const struct got_error *err;
638 struct got_commit_object *commit;
640 if (got_object_idset_contains(idset, id))
641 return NULL;
643 if (loose_obj_only) {
644 int is_packed;
645 err = search_packidx(&is_packed, id, repo);
646 if (err)
647 return err;
648 if (is_packed)
649 return NULL;
652 err = got_object_open_as_commit(&commit, repo, id);
653 if (err)
654 return err;
656 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
657 got_object_commit_get_committer_time(commit),
658 loose_obj_only, repo);
659 if (err)
660 goto done;
662 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
663 "", got_object_commit_get_committer_time(commit),
664 loose_obj_only, repo, cancel_cb, cancel_arg);
665 done:
666 got_object_commit_close(commit);
667 return err;
670 static const struct got_error *
671 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
672 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
673 got_cancel_cb cancel_cb, void *cancel_arg)
675 const struct got_error *err;
676 struct got_tag_object *tag = NULL;
678 if (got_object_idset_contains(idset, id))
679 return NULL;
681 if (loose_obj_only) {
682 int is_packed;
683 err = search_packidx(&is_packed, id, repo);
684 if (err)
685 return err;
686 if (is_packed)
687 return NULL;
690 err = got_object_open_as_tag(&tag, repo, id);
691 if (err)
692 return err;
694 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
695 got_object_tag_get_tagger_time(tag),
696 loose_obj_only, repo);
697 if (err)
698 goto done;
700 switch (got_object_tag_get_object_type(tag)) {
701 case GOT_OBJ_TYPE_COMMIT:
702 err = load_commit(v, idset,
703 got_object_tag_get_object_id(tag), repo,
704 loose_obj_only, cancel_cb, cancel_arg);
705 break;
706 case GOT_OBJ_TYPE_TREE:
707 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
708 "", got_object_tag_get_tagger_time(tag),
709 loose_obj_only, repo, cancel_cb, cancel_arg);
710 break;
711 default:
712 break;
715 done:
716 got_object_tag_close(tag);
717 return err;
720 enum findtwixt_color {
721 COLOR_KEEP = 0,
722 COLOR_DROP,
723 COLOR_BLANK,
724 };
725 static const int findtwixt_colors[] = {
726 COLOR_KEEP,
727 COLOR_DROP,
728 COLOR_BLANK
729 };
731 static const struct got_error *
732 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
733 int color, struct got_repository *repo)
735 const struct got_error *err;
736 struct got_object_qid *qid;
738 err = got_object_qid_alloc(&qid, id);
739 if (err)
740 return err;
742 STAILQ_INSERT_TAIL(ids, qid, entry);
743 qid->data = (void *)&findtwixt_colors[color];
744 return NULL;
747 static const struct got_error *
748 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
749 struct got_object_id *id, struct got_repository *repo,
750 got_cancel_cb cancel_cb, void *cancel_arg)
752 const struct got_error *err = NULL;
753 struct got_commit_object *commit;
754 const struct got_object_id_queue *parents;
755 struct got_object_id_queue ids;
756 struct got_object_qid *qid;
758 STAILQ_INIT(&ids);
760 err = got_object_qid_alloc(&qid, id);
761 if (err)
762 return err;
763 STAILQ_INSERT_HEAD(&ids, qid, entry);
765 while (!STAILQ_EMPTY(&ids)) {
766 if (cancel_cb) {
767 err = (*cancel_cb)(cancel_arg);
768 if (err)
769 break;
772 qid = STAILQ_FIRST(&ids);
773 STAILQ_REMOVE_HEAD(&ids, entry);
775 if (got_object_idset_contains(drop, qid->id)) {
776 got_object_qid_free(qid);
777 continue;
780 err = got_object_idset_add(drop, qid->id,
781 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
782 if (err) {
783 got_object_qid_free(qid);
784 break;
787 if (!got_object_idset_contains(keep, qid->id)) {
788 got_object_qid_free(qid);
789 continue;
792 err = got_object_open_as_commit(&commit, repo, qid->id);
793 got_object_qid_free(qid);
794 if (err)
795 break;
797 parents = got_object_commit_get_parent_ids(commit);
798 if (parents) {
799 err = got_object_id_queue_copy(parents, &ids);
800 if (err) {
801 got_object_commit_close(commit);
802 break;
805 got_object_commit_close(commit);
808 got_object_id_queue_free(&ids);
809 return err;
812 struct append_id_arg {
813 struct got_object_id **array;
814 int idx;
815 };
817 static const struct got_error *
818 append_id(struct got_object_id *id, void *data, void *arg)
820 struct append_id_arg *a = arg;
822 a->array[a->idx] = got_object_id_dup(id);
823 if (a->array[a->idx] == NULL)
824 return got_error_from_errno("got_object_id_dup");
826 a->idx++;
827 return NULL;
830 static const struct got_error *
831 findtwixt(struct got_object_id ***res, int *nres,
832 struct got_object_id **head, int nhead,
833 struct got_object_id **tail, int ntail,
834 struct got_repository *repo,
835 got_cancel_cb cancel_cb, void *cancel_arg)
837 const struct got_error *err = NULL;
838 struct got_object_id_queue ids;
839 struct got_object_idset *keep, *drop;
840 struct got_object_qid *qid;
841 int i, ncolor, nkeep, obj_type;
843 STAILQ_INIT(&ids);
844 *res = NULL;
845 *nres = 0;
847 keep = got_object_idset_alloc();
848 if (keep == NULL)
849 return got_error_from_errno("got_object_idset_alloc");
851 drop = got_object_idset_alloc();
852 if (drop == NULL) {
853 err = got_error_from_errno("got_object_idset_alloc");
854 goto done;
857 for (i = 0; i < nhead; i++) {
858 struct got_object_id *id = head[i];
859 if (id == NULL)
860 continue;
861 err = got_object_get_type(&obj_type, repo, id);
862 if (err)
863 return err;
864 if (obj_type != GOT_OBJ_TYPE_COMMIT)
865 continue;
866 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
867 if (err)
868 goto done;
870 for (i = 0; i < ntail; i++) {
871 struct got_object_id *id = tail[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 = queue_commit_id(&ids, id, COLOR_DROP, repo);
880 if (err)
881 goto done;
884 while (!STAILQ_EMPTY(&ids)) {
885 int qcolor;
886 qid = STAILQ_FIRST(&ids);
887 qcolor = *((int *)qid->data);
889 if (got_object_idset_contains(drop, qid->id))
890 ncolor = COLOR_DROP;
891 else if (got_object_idset_contains(keep, qid->id))
892 ncolor = COLOR_KEEP;
893 else
894 ncolor = COLOR_BLANK;
896 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
897 qcolor == COLOR_KEEP)) {
898 STAILQ_REMOVE_HEAD(&ids, entry);
899 got_object_qid_free(qid);
900 continue;
903 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
904 err = drop_commit(keep, drop, qid->id, repo,
905 cancel_cb, cancel_arg);
906 if (err)
907 goto done;
908 } else if (ncolor == COLOR_BLANK) {
909 struct got_commit_object *commit;
910 struct got_object_id *id;
911 const struct got_object_id_queue *parents;
912 struct got_object_qid *pid;
914 id = got_object_id_dup(qid->id);
915 if (id == NULL) {
916 err = got_error_from_errno("got_object_id_dup");
917 goto done;
919 if (qcolor == COLOR_KEEP)
920 err = got_object_idset_add(keep, id,
921 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
922 else
923 err = got_object_idset_add(drop, id,
924 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
925 if (err) {
926 free(id);
927 goto done;
930 err = got_object_open_as_commit(&commit, repo, id);
931 if (err) {
932 free(id);
933 goto done;
935 parents = got_object_commit_get_parent_ids(commit);
936 if (parents) {
937 STAILQ_FOREACH(pid, parents, entry) {
938 err = queue_commit_id(&ids, pid->id,
939 qcolor, repo);
940 if (err) {
941 free(id);
942 goto done;
946 got_object_commit_close(commit);
947 commit = NULL;
948 } else {
949 /* should not happen */
950 err = got_error_fmt(GOT_ERR_NOT_IMPL,
951 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
952 goto done;
955 STAILQ_REMOVE_HEAD(&ids, entry);
956 got_object_qid_free(qid);
959 nkeep = got_object_idset_num_elements(keep);
960 if (nkeep > 0) {
961 struct append_id_arg arg;
962 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
963 if (arg.array == NULL) {
964 err = got_error_from_errno("calloc");
965 goto done;
967 arg.idx = 0;
968 err = got_object_idset_for_each(keep, append_id, &arg);
969 if (err) {
970 free(arg.array);
971 goto done;
973 *res = arg.array;
974 *nres = nkeep;
976 done:
977 got_object_idset_free(keep);
978 got_object_idset_free(drop);
979 got_object_id_queue_free(&ids);
980 return err;
983 static const struct got_error *
984 read_meta(struct got_pack_meta ***meta, int *nmeta,
985 struct got_object_id **theirs, int ntheirs,
986 struct got_object_id **ours, int nours, struct got_repository *repo,
987 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
988 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
990 const struct got_error *err = NULL;
991 struct got_object_id **ids = NULL;
992 struct got_object_idset *idset;
993 int i, nobj = 0, obj_type;
994 struct got_pack_metavec v;
996 *meta = NULL;
997 *nmeta = 0;
999 idset = got_object_idset_alloc();
1000 if (idset == NULL)
1001 return got_error_from_errno("got_object_idset_alloc");
1003 v.nmeta = 0;
1004 v.metasz = 64;
1005 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
1006 if (v.meta == NULL) {
1007 err = got_error_from_errno("calloc");
1008 goto done;
1011 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1012 cancel_cb, cancel_arg);
1013 if (err || nobj == 0)
1014 goto done;
1016 for (i = 0; i < ntheirs; i++) {
1017 struct got_object_id *id = theirs[i];
1018 if (id == NULL)
1019 continue;
1020 err = got_object_get_type(&obj_type, repo, id);
1021 if (err)
1022 return err;
1023 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1024 continue;
1025 err = load_commit(NULL, idset, id, repo,
1026 loose_obj_only, cancel_cb, cancel_arg);
1027 if (err)
1028 goto done;
1029 err = report_progress(progress_cb, progress_arg, rl,
1030 0L, nours, v.nmeta, 0, 0);
1031 if (err)
1032 goto done;
1035 for (i = 0; i < ntheirs; i++) {
1036 struct got_object_id *id = theirs[i];
1037 int *cached_type;
1038 if (id == NULL)
1039 continue;
1040 cached_type = got_object_idset_get(idset, id);
1041 if (cached_type == NULL) {
1042 err = got_object_get_type(&obj_type, repo, id);
1043 if (err)
1044 goto done;
1045 } else
1046 obj_type = *cached_type;
1047 if (obj_type != GOT_OBJ_TYPE_TAG)
1048 continue;
1049 err = load_tag(NULL, idset, id, repo,
1050 loose_obj_only, cancel_cb, cancel_arg);
1051 if (err)
1052 goto done;
1053 err = report_progress(progress_cb, progress_arg, rl,
1054 0L, nours, v.nmeta, 0, 0);
1055 if (err)
1056 goto done;
1059 for (i = 0; i < nobj; i++) {
1060 err = load_commit(&v, idset, ids[i], repo,
1061 loose_obj_only, cancel_cb, cancel_arg);
1062 if (err)
1063 goto done;
1064 if (err)
1065 goto done;
1066 err = report_progress(progress_cb, progress_arg, rl,
1067 0L, nours, v.nmeta, 0, 0);
1068 if (err)
1069 goto done;
1072 for (i = 0; i < nours; i++) {
1073 struct got_object_id *id = ours[i];
1074 int *cached_type;
1075 if (id == NULL)
1076 continue;
1077 cached_type = got_object_idset_get(idset, id);
1078 if (cached_type == NULL) {
1079 err = got_object_get_type(&obj_type, repo, id);
1080 if (err)
1081 goto done;
1082 } else
1083 obj_type = *cached_type;
1084 if (obj_type != GOT_OBJ_TYPE_TAG)
1085 continue;
1086 err = load_tag(&v, idset, id, repo,
1087 loose_obj_only, cancel_cb, cancel_arg);
1088 if (err)
1089 goto done;
1090 err = report_progress(progress_cb, progress_arg, rl,
1091 0L, nours, v.nmeta, 0, 0);
1092 if (err)
1093 goto done;
1096 if (progress_cb) {
1097 err = progress_cb(progress_arg, 0L, nours, v.nmeta, 0, 0);
1098 if (err)
1099 goto done;
1101 done:
1102 for (i = 0; i < nobj; i++) {
1103 free(ids[i]);
1105 free(ids);
1106 got_object_idset_free(idset);
1107 if (err == NULL) {
1108 *meta = v.meta;
1109 *nmeta = v.nmeta;
1110 } else
1111 free(v.meta);
1113 return err;
1116 const struct got_error *
1117 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1119 size_t n;
1121 SHA1Update(ctx, buf, len);
1122 n = fwrite(buf, 1, len, f);
1123 if (n != len)
1124 return got_ferror(f, GOT_ERR_IO);
1125 return NULL;
1128 static void
1129 putbe32(char *b, uint32_t n)
1131 b[0] = n >> 24;
1132 b[1] = n >> 16;
1133 b[2] = n >> 8;
1134 b[3] = n >> 0;
1137 static int
1138 write_order_cmp(const void *pa, const void *pb)
1140 struct got_pack_meta *a, *b, *ahd, *bhd;
1142 a = *(struct got_pack_meta **)pa;
1143 b = *(struct got_pack_meta **)pb;
1144 ahd = (a->head == NULL) ? a : a->head;
1145 bhd = (b->head == NULL) ? b : b->head;
1146 if (ahd->mtime != bhd->mtime)
1147 return bhd->mtime - ahd->mtime;
1148 if (ahd != bhd)
1149 return (uintptr_t)bhd - (uintptr_t)ahd;
1150 if (a->nchain != b->nchain)
1151 return a->nchain - b->nchain;
1152 return a->mtime - b->mtime;
1155 static const struct got_error *
1156 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1158 size_t i;
1160 *hdrlen = 0;
1162 hdr[0] = obj_type << 4;
1163 hdr[0] |= len & 0xf;
1164 len >>= 4;
1165 for (i = 1; len != 0; i++){
1166 if (i >= bufsize)
1167 return got_error(GOT_ERR_NO_SPACE);
1168 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1169 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1170 len >>= GOT_DELTA_SIZE_SHIFT;
1173 *hdrlen = i;
1174 return NULL;
1177 static int
1178 packoff(char *hdr, off_t off)
1180 int i, j;
1181 char rbuf[8];
1183 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1184 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1185 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1186 GOT_DELTA_SIZE_MORE;
1189 j = 0;
1190 while (i > 0)
1191 hdr[j++] = rbuf[--i];
1192 return j;
1195 static const struct got_error *
1196 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1197 struct got_pack_meta **meta, int nmeta, int nours,
1198 int use_offset_deltas, struct got_repository *repo,
1199 got_pack_progress_cb progress_cb, void *progress_arg,
1200 struct got_ratelimit *rl,
1201 got_cancel_cb cancel_cb, void *cancel_arg)
1203 const struct got_error *err = NULL;
1204 int i, nh;
1205 SHA1_CTX ctx;
1206 struct got_pack_meta *m;
1207 struct got_raw_object *raw = NULL;
1208 FILE *delta_file = NULL;
1209 char buf[32];
1210 size_t outlen, n;
1211 struct got_deflate_checksum csum;
1212 off_t packfile_size = 0;
1213 int outfd = -1;
1215 SHA1Init(&ctx);
1216 csum.output_sha1 = &ctx;
1217 csum.output_crc = NULL;
1219 err = hwrite(packfile, "PACK", 4, &ctx);
1220 if (err)
1221 return err;
1222 putbe32(buf, GOT_PACKFILE_VERSION);
1223 err = hwrite(packfile, buf, 4, &ctx);
1224 if (err)
1225 goto done;
1226 putbe32(buf, nmeta);
1227 err = hwrite(packfile, buf, 4, &ctx);
1228 if (err)
1229 goto done;
1230 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1231 for (i = 0; i < nmeta; i++) {
1232 err = report_progress(progress_cb, progress_arg, rl,
1233 packfile_size, nours, nmeta, nmeta, i);
1234 if (err)
1235 goto done;
1236 m = meta[i];
1237 m->off = ftello(packfile);
1238 if (m->delta_len == 0) {
1239 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1240 if (err)
1241 goto done;
1242 err = packhdr(&nh, buf, sizeof(buf),
1243 m->obj_type, raw->size);
1244 if (err)
1245 goto done;
1246 err = hwrite(packfile, buf, nh, &ctx);
1247 if (err)
1248 goto done;
1249 packfile_size += nh;
1250 if (raw->f == NULL) {
1251 err = got_deflate_to_file_mmap(&outlen,
1252 raw->data + raw->hdrlen, 0, raw->size,
1253 packfile, &csum);
1254 if (err)
1255 goto done;
1256 } else {
1257 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1258 == -1) {
1259 err = got_error_from_errno("fseeko");
1260 goto done;
1262 err = got_deflate_to_file(&outlen, raw->f,
1263 packfile, &csum);
1264 if (err)
1265 goto done;
1267 packfile_size += outlen;
1268 got_object_raw_close(raw);
1269 raw = NULL;
1270 } else {
1271 off_t remain;
1272 if (delta_file == NULL) {
1273 delta_file = got_opentemp();
1274 if (delta_file == NULL) {
1275 err = got_error_from_errno(
1276 "got_opentemp");
1277 goto done;
1280 if (ftruncate(fileno(delta_file), 0L) == -1) {
1281 err = got_error_from_errno("ftruncate");
1282 goto done;
1284 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1285 err = got_error_from_errno("fseeko");
1286 goto done;
1288 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1289 == -1) {
1290 err = got_error_from_errno("fseeko");
1291 goto done;
1293 remain = m->delta_len;
1294 while (remain > 0) {
1295 char delta_buf[8192];
1296 size_t r, w, n;
1297 n = MIN(remain, sizeof(delta_buf));
1298 r = fread(delta_buf, 1, n, delta_cache);
1299 if (r != n) {
1300 err = got_ferror(delta_cache,
1301 GOT_ERR_IO);
1302 goto done;
1304 w = fwrite(delta_buf, 1, n, delta_file);
1305 if (w != n) {
1306 err = got_ferror(delta_file,
1307 GOT_ERR_IO);
1308 goto done;
1310 remain -= n;
1312 if (use_offset_deltas && m->prev->off != 0) {
1313 err = packhdr(&nh, buf, sizeof(buf),
1314 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1315 if (err)
1316 goto done;
1317 nh += packoff(buf + nh,
1318 m->off - m->prev->off);
1319 err = hwrite(packfile, buf, nh, &ctx);
1320 if (err)
1321 goto done;
1322 packfile_size += nh;
1323 } else {
1324 err = packhdr(&nh, buf, sizeof(buf),
1325 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1326 err = hwrite(packfile, buf, nh, &ctx);
1327 if (err)
1328 goto done;
1329 packfile_size += nh;
1330 err = hwrite(packfile, m->prev->id.sha1,
1331 sizeof(m->prev->id.sha1), &ctx);
1332 packfile_size += sizeof(m->prev->id.sha1);
1333 if (err)
1334 goto done;
1336 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1337 err = got_error_from_errno("fseeko");
1338 goto done;
1340 err = got_deflate_to_file(&outlen, delta_file,
1341 packfile, &csum);
1342 if (err)
1343 goto done;
1344 packfile_size += outlen;
1347 SHA1Final(pack_sha1, &ctx);
1348 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1349 if (n != SHA1_DIGEST_LENGTH)
1350 err = got_ferror(packfile, GOT_ERR_IO);
1351 packfile_size += SHA1_DIGEST_LENGTH;
1352 packfile_size += sizeof(struct got_packfile_hdr);
1353 if (progress_cb) {
1354 err = progress_cb(progress_arg, packfile_size, nours,
1355 nmeta, nmeta, nmeta);
1356 if (err)
1357 goto done;
1359 done:
1360 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1361 err = got_error_from_errno("fclose");
1362 if (raw)
1363 got_object_raw_close(raw);
1364 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1365 err = got_error_from_errno("close");
1366 return err;
1369 const struct got_error *
1370 got_pack_create(uint8_t *packsha1, FILE *packfile,
1371 struct got_object_id **theirs, int ntheirs,
1372 struct got_object_id **ours, int nours,
1373 struct got_repository *repo, int loose_obj_only, int allow_empty,
1374 got_pack_progress_cb progress_cb, void *progress_arg,
1375 got_cancel_cb cancel_cb, void *cancel_arg)
1377 const struct got_error *err;
1378 struct got_pack_meta **meta;
1379 int nmeta;
1380 FILE *delta_cache = NULL;
1381 struct got_ratelimit rl;
1383 got_ratelimit_init(&rl, 0, 500);
1385 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1386 loose_obj_only, progress_cb, progress_arg, &rl,
1387 cancel_cb, cancel_arg);
1388 if (err)
1389 return err;
1391 if (nmeta == 0 && !allow_empty) {
1392 err = got_error(GOT_ERR_CANNOT_PACK);
1393 goto done;
1396 delta_cache = got_opentemp();
1397 if (delta_cache == NULL) {
1398 err = got_error_from_errno("got_opentemp");
1399 goto done;
1402 if (nmeta > 0) {
1403 err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1404 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1405 if (err)
1406 goto done;
1407 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1408 err = got_error_from_errno("fseeko");
1409 goto done;
1413 err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1414 repo, progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1415 if (err)
1416 goto done;
1417 done:
1418 free_nmeta(meta, nmeta);
1419 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1420 err = got_error_from_errno("fclose");
1421 return err;