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 {
231 char content[128];
232 size_t r;
233 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
234 return got_error_from_errno("fseeko");
235 n = 0;
236 while (n != d->len) {
237 buf[0] = (d->len - n < 127) ? d->len - n : 127;
238 w = fwrite(buf, 1, 1, f);
239 if (w != 1)
240 return got_ferror(f, GOT_ERR_IO);
241 r = fread(content, 1, buf[0], o->f);
242 if (r != buf[0])
243 return got_ferror(o->f, GOT_ERR_IO);
244 w = fwrite(content, 1, buf[0], f);
245 if (w != buf[0])
246 return got_ferror(f, GOT_ERR_IO);
247 n += buf[0];
252 return NULL;
255 static const struct got_error *
256 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
257 struct got_ratelimit *rl, off_t packfile_size, int ncommits,
258 int nobj_total, int obj_deltify, int nobj_written)
260 const struct got_error *err;
261 int elapsed;
263 if (progress_cb == NULL)
264 return NULL;
266 err = got_ratelimit_check(&elapsed, rl);
267 if (err || !elapsed)
268 return err;
270 return progress_cb(progress_arg, packfile_size, ncommits,
271 nobj_total, obj_deltify, nobj_written);
274 static const struct got_error *
275 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
276 FILE *delta_cache, struct got_repository *repo,
277 got_pack_progress_cb progress_cb, void *progress_arg,
278 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
280 const struct got_error *err = NULL;
281 struct got_pack_meta *m = NULL, *base = NULL;
282 struct got_raw_object *raw = NULL, *base_raw = NULL;
283 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
284 int i, j, size, best_size, ndeltas, best_ndeltas;
285 const int max_base_candidates = 3;
286 int outfd = -1;
288 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
289 for (i = 0; i < nmeta; i++) {
290 if (cancel_cb) {
291 err = (*cancel_cb)(cancel_arg);
292 if (err)
293 break;
295 err = report_progress(progress_cb, progress_arg, rl,
296 0L, nours, nmeta, i, 0);
297 if (err)
298 goto done;
299 m = meta[i];
301 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
302 m->obj_type == GOT_OBJ_TYPE_TAG)
303 continue;
305 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
306 if (err)
307 goto done;
308 m->size = raw->size;
310 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
311 raw->size + raw->hdrlen);
312 if (err)
313 goto done;
315 if (i > max_base_candidates) {
316 struct got_pack_meta *n = NULL;
317 n = meta[i - (max_base_candidates + 1)];
318 got_deltify_free(n->dtab);
319 n->dtab = NULL;
322 best_size = raw->size;
323 best_ndeltas = 0;
324 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
325 if (cancel_cb) {
326 err = (*cancel_cb)(cancel_arg);
327 if (err)
328 goto done;
330 base = meta[j];
331 /* long chains make unpacking slow, avoid such bases */
332 if (base->nchain >= 128 ||
333 base->obj_type != m->obj_type)
334 continue;
336 err = got_object_raw_open(&base_raw, &outfd, repo,
337 &base->id);
338 if (err)
339 goto done;
340 err = got_deltify(&deltas, &ndeltas,
341 raw->f, raw->hdrlen, raw->size + raw->hdrlen,
342 base->dtab, base_raw->f, base_raw->hdrlen,
343 base_raw->size + base_raw->hdrlen);
344 got_object_raw_close(base_raw);
345 base_raw = NULL;
346 if (err)
347 goto done;
349 size = delta_size(deltas, ndeltas);
350 if (size + 32 < best_size){
351 /*
352 * if we already picked a best delta,
353 * replace it.
354 */
355 best_size = size;
356 free(best_deltas);
357 best_deltas = deltas;
358 best_ndeltas = ndeltas;
359 deltas = NULL;
360 m->nchain = base->nchain + 1;
361 m->prev = base;
362 m->head = base->head;
363 if (m->head == NULL)
364 m->head = base;
365 } else {
366 free(deltas);
367 deltas = NULL;
368 ndeltas = 0;
372 if (best_ndeltas > 0) {
373 m->delta_offset = ftello(delta_cache);
374 err = encode_delta(m, raw, best_deltas,
375 best_ndeltas, m->prev->size, delta_cache);
376 free(best_deltas);
377 best_deltas = NULL;
378 best_ndeltas = 0;
379 if (err)
380 goto done;
381 m->delta_len = ftello(delta_cache) - m->delta_offset;
384 got_object_raw_close(raw);
385 raw = NULL;
387 done:
388 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
389 got_deltify_free(meta[i]->dtab);
390 meta[i]->dtab = NULL;
392 if (raw)
393 got_object_raw_close(raw);
394 if (base_raw)
395 got_object_raw_close(base_raw);
396 if (outfd != -1 && close(outfd) == -1 && err == NULL)
397 err = got_error_from_errno("close");
398 free(deltas);
399 free(best_deltas);
400 return err;
403 static const struct got_error *
404 search_packidx(int *found, struct got_object_id *id,
405 struct got_repository *repo)
407 const struct got_error *err = NULL;
408 struct got_packidx *packidx = NULL;
409 int idx;
411 *found = 0;
413 err = got_repo_search_packidx(&packidx, &idx, repo, id);
414 if (err == NULL)
415 *found = 1; /* object is already packed */
416 else if (err->code == GOT_ERR_NO_OBJ)
417 err = NULL;
418 return err;
421 static const int obj_types[] = {
422 GOT_OBJ_TYPE_ANY,
423 GOT_OBJ_TYPE_COMMIT,
424 GOT_OBJ_TYPE_TREE,
425 GOT_OBJ_TYPE_BLOB,
426 GOT_OBJ_TYPE_TAG,
427 GOT_OBJ_TYPE_OFFSET_DELTA,
428 GOT_OBJ_TYPE_REF_DELTA
429 };
431 static const struct got_error *
432 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
433 struct got_object_id *id, const char *path, int obj_type,
434 time_t mtime, int loose_obj_only, struct got_repository *repo)
436 const struct got_error *err;
437 struct got_pack_meta *m;
439 if (loose_obj_only) {
440 int is_packed;
441 err = search_packidx(&is_packed, id, repo);
442 if (err)
443 return err;
444 if (is_packed)
445 return NULL;
448 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
449 if (err)
450 return err;
452 if (v == NULL)
453 return NULL;
455 err = alloc_meta(&m, id, path, obj_type, mtime);
456 if (err)
457 goto done;
459 if (v->nmeta == v->metasz){
460 size_t newsize = 2 * v->metasz;
461 struct got_pack_meta **new;
462 new = reallocarray(v->meta, newsize, sizeof(*new));
463 if (new == NULL) {
464 err = got_error_from_errno("reallocarray");
465 goto done;
467 v->meta = new;
468 v->metasz = newsize;
470 done:
471 if (err) {
472 clear_meta(m);
473 free(m);
474 } else
475 v->meta[v->nmeta++] = m;
477 return err;
480 static const struct got_error *
481 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
482 struct got_object_idset *idset, struct got_object_id *tree_id,
483 const char *dpath, time_t mtime, struct got_repository *repo,
484 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
486 const struct got_error *err;
487 struct got_tree_object *tree;
488 char *p = NULL;
489 int i;
491 err = got_object_open_as_tree(&tree, repo, tree_id);
492 if (err)
493 return err;
495 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
496 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
497 struct got_object_id *id = got_tree_entry_get_id(e);
498 mode_t mode = got_tree_entry_get_mode(e);
500 if (cancel_cb) {
501 err = (*cancel_cb)(cancel_arg);
502 if (err)
503 break;
506 if (got_object_tree_entry_is_submodule(e) ||
507 got_object_idset_contains(idset, id))
508 continue;
510 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
511 got_tree_entry_get_name(e)) == -1) {
512 err = got_error_from_errno("asprintf");
513 break;
516 if (S_ISDIR(mode)) {
517 struct got_object_qid *qid;
518 err = got_object_qid_alloc(&qid, id);
519 if (err)
520 break;
521 STAILQ_INSERT_TAIL(ids, qid, entry);
522 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
523 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
524 mtime, loose_obj_only, repo);
525 if (err)
526 break;
528 free(p);
529 p = NULL;
532 got_object_tree_close(tree);
533 free(p);
534 return err;
537 static const struct got_error *
538 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
539 struct got_object_id *tree_id, const char *dpath, time_t mtime,
540 int loose_obj_only, struct got_repository *repo,
541 got_cancel_cb cancel_cb, void *cancel_arg)
543 const struct got_error *err = NULL;
544 struct got_object_id_queue tree_ids;
545 struct got_object_qid *qid;
547 if (got_object_idset_contains(idset, tree_id))
548 return NULL;
550 err = got_object_qid_alloc(&qid, tree_id);
551 if (err)
552 return err;
554 STAILQ_INIT(&tree_ids);
555 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
557 while (!STAILQ_EMPTY(&tree_ids)) {
558 if (cancel_cb) {
559 err = (*cancel_cb)(cancel_arg);
560 if (err)
561 break;
564 qid = STAILQ_FIRST(&tree_ids);
565 STAILQ_REMOVE_HEAD(&tree_ids, entry);
567 if (got_object_idset_contains(idset, qid->id)) {
568 got_object_qid_free(qid);
569 continue;
572 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
573 mtime, loose_obj_only, repo);
574 if (err) {
575 got_object_qid_free(qid);
576 break;
579 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
580 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
581 got_object_qid_free(qid);
582 if (err)
583 break;
586 got_object_id_queue_free(&tree_ids);
587 return err;
590 static const struct got_error *
591 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
592 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
593 got_cancel_cb cancel_cb, void *cancel_arg)
595 const struct got_error *err;
596 struct got_commit_object *commit;
598 if (got_object_idset_contains(idset, id))
599 return NULL;
601 if (loose_obj_only) {
602 int is_packed;
603 err = search_packidx(&is_packed, id, repo);
604 if (err)
605 return err;
606 if (is_packed)
607 return NULL;
610 err = got_object_open_as_commit(&commit, repo, id);
611 if (err)
612 return err;
614 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
615 got_object_commit_get_committer_time(commit),
616 loose_obj_only, repo);
617 if (err)
618 goto done;
620 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
621 "", got_object_commit_get_committer_time(commit),
622 loose_obj_only, repo, cancel_cb, cancel_arg);
623 done:
624 got_object_commit_close(commit);
625 return err;
628 static const struct got_error *
629 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
630 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
631 got_cancel_cb cancel_cb, void *cancel_arg)
633 const struct got_error *err;
634 struct got_tag_object *tag = NULL;
636 if (got_object_idset_contains(idset, id))
637 return NULL;
639 if (loose_obj_only) {
640 int is_packed;
641 err = search_packidx(&is_packed, id, repo);
642 if (err)
643 return err;
644 if (is_packed)
645 return NULL;
648 err = got_object_open_as_tag(&tag, repo, id);
649 if (err)
650 return err;
652 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
653 got_object_tag_get_tagger_time(tag),
654 loose_obj_only, repo);
655 if (err)
656 goto done;
658 switch (got_object_tag_get_object_type(tag)) {
659 case GOT_OBJ_TYPE_COMMIT:
660 err = load_commit(v, idset,
661 got_object_tag_get_object_id(tag), repo,
662 loose_obj_only, cancel_cb, cancel_arg);
663 break;
664 case GOT_OBJ_TYPE_TREE:
665 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
666 "", got_object_tag_get_tagger_time(tag),
667 loose_obj_only, repo, cancel_cb, cancel_arg);
668 break;
669 default:
670 break;
673 done:
674 got_object_tag_close(tag);
675 return err;
678 enum findtwixt_color {
679 COLOR_KEEP = 0,
680 COLOR_DROP,
681 COLOR_BLANK,
682 };
683 static const int findtwixt_colors[] = {
684 COLOR_KEEP,
685 COLOR_DROP,
686 COLOR_BLANK
687 };
689 static const struct got_error *
690 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
691 int color, struct got_repository *repo)
693 const struct got_error *err;
694 struct got_object_qid *qid;
696 err = got_object_qid_alloc(&qid, id);
697 if (err)
698 return err;
700 STAILQ_INSERT_TAIL(ids, qid, entry);
701 qid->data = (void *)&findtwixt_colors[color];
702 return NULL;
705 static const struct got_error *
706 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
707 struct got_object_id *id, struct got_repository *repo,
708 got_cancel_cb cancel_cb, void *cancel_arg)
710 const struct got_error *err = NULL;
711 struct got_commit_object *commit;
712 const struct got_object_id_queue *parents;
713 struct got_object_id_queue ids;
714 struct got_object_qid *qid;
716 STAILQ_INIT(&ids);
718 err = got_object_qid_alloc(&qid, id);
719 if (err)
720 return err;
721 STAILQ_INSERT_HEAD(&ids, qid, entry);
723 while (!STAILQ_EMPTY(&ids)) {
724 if (cancel_cb) {
725 err = (*cancel_cb)(cancel_arg);
726 if (err)
727 break;
730 qid = STAILQ_FIRST(&ids);
731 STAILQ_REMOVE_HEAD(&ids, entry);
733 if (got_object_idset_contains(drop, qid->id)) {
734 got_object_qid_free(qid);
735 continue;
738 err = got_object_idset_add(drop, qid->id,
739 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
740 if (err) {
741 got_object_qid_free(qid);
742 break;
745 if (!got_object_idset_contains(keep, qid->id)) {
746 got_object_qid_free(qid);
747 continue;
750 err = got_object_open_as_commit(&commit, repo, qid->id);
751 got_object_qid_free(qid);
752 if (err)
753 break;
755 parents = got_object_commit_get_parent_ids(commit);
756 if (parents) {
757 err = got_object_id_queue_copy(parents, &ids);
758 if (err) {
759 got_object_commit_close(commit);
760 break;
763 got_object_commit_close(commit);
766 got_object_id_queue_free(&ids);
767 return err;
770 struct append_id_arg {
771 struct got_object_id **array;
772 int idx;
773 };
775 static const struct got_error *
776 append_id(struct got_object_id *id, void *data, void *arg)
778 struct append_id_arg *a = arg;
780 a->array[a->idx] = got_object_id_dup(id);
781 if (a->array[a->idx] == NULL)
782 return got_error_from_errno("got_object_id_dup");
784 a->idx++;
785 return NULL;
788 static const struct got_error *
789 findtwixt(struct got_object_id ***res, int *nres,
790 struct got_object_id **head, int nhead,
791 struct got_object_id **tail, int ntail,
792 struct got_repository *repo,
793 got_cancel_cb cancel_cb, void *cancel_arg)
795 const struct got_error *err = NULL;
796 struct got_object_id_queue ids;
797 struct got_object_idset *keep, *drop;
798 struct got_object_qid *qid;
799 int i, ncolor, nkeep, obj_type;
801 STAILQ_INIT(&ids);
802 *res = NULL;
803 *nres = 0;
805 keep = got_object_idset_alloc();
806 if (keep == NULL)
807 return got_error_from_errno("got_object_idset_alloc");
809 drop = got_object_idset_alloc();
810 if (drop == NULL) {
811 err = got_error_from_errno("got_object_idset_alloc");
812 goto done;
815 for (i = 0; i < nhead; i++) {
816 struct got_object_id *id = head[i];
817 if (id == NULL)
818 continue;
819 err = got_object_get_type(&obj_type, repo, id);
820 if (err)
821 return err;
822 if (obj_type != GOT_OBJ_TYPE_COMMIT)
823 continue;
824 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
825 if (err)
826 goto done;
828 for (i = 0; i < ntail; i++) {
829 struct got_object_id *id = tail[i];
830 if (id == NULL)
831 continue;
832 err = got_object_get_type(&obj_type, repo, id);
833 if (err)
834 return err;
835 if (obj_type != GOT_OBJ_TYPE_COMMIT)
836 continue;
837 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
838 if (err)
839 goto done;
842 while (!STAILQ_EMPTY(&ids)) {
843 int qcolor;
844 qid = STAILQ_FIRST(&ids);
845 qcolor = *((int *)qid->data);
847 if (got_object_idset_contains(drop, qid->id))
848 ncolor = COLOR_DROP;
849 else if (got_object_idset_contains(keep, qid->id))
850 ncolor = COLOR_KEEP;
851 else
852 ncolor = COLOR_BLANK;
854 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
855 qcolor == COLOR_KEEP)) {
856 STAILQ_REMOVE_HEAD(&ids, entry);
857 got_object_qid_free(qid);
858 continue;
861 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
862 err = drop_commit(keep, drop, qid->id, repo,
863 cancel_cb, cancel_arg);
864 if (err)
865 goto done;
866 } else if (ncolor == COLOR_BLANK) {
867 struct got_commit_object *commit;
868 struct got_object_id *id;
869 const struct got_object_id_queue *parents;
870 struct got_object_qid *pid;
872 id = got_object_id_dup(qid->id);
873 if (id == NULL) {
874 err = got_error_from_errno("got_object_id_dup");
875 goto done;
877 if (qcolor == COLOR_KEEP)
878 err = got_object_idset_add(keep, id,
879 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
880 else
881 err = got_object_idset_add(drop, id,
882 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
883 if (err) {
884 free(id);
885 goto done;
888 err = got_object_open_as_commit(&commit, repo, id);
889 if (err) {
890 free(id);
891 goto done;
893 parents = got_object_commit_get_parent_ids(commit);
894 if (parents) {
895 STAILQ_FOREACH(pid, parents, entry) {
896 err = queue_commit_id(&ids, pid->id,
897 qcolor, repo);
898 if (err) {
899 free(id);
900 goto done;
904 got_object_commit_close(commit);
905 commit = NULL;
906 } else {
907 /* should not happen */
908 err = got_error_fmt(GOT_ERR_NOT_IMPL,
909 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
910 goto done;
913 STAILQ_REMOVE_HEAD(&ids, entry);
914 got_object_qid_free(qid);
917 nkeep = got_object_idset_num_elements(keep);
918 if (nkeep > 0) {
919 struct append_id_arg arg;
920 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
921 if (arg.array == NULL) {
922 err = got_error_from_errno("calloc");
923 goto done;
925 arg.idx = 0;
926 err = got_object_idset_for_each(keep, append_id, &arg);
927 if (err) {
928 free(arg.array);
929 goto done;
931 *res = arg.array;
932 *nres = nkeep;
934 done:
935 got_object_idset_free(keep);
936 got_object_idset_free(drop);
937 got_object_id_queue_free(&ids);
938 return err;
941 static const struct got_error *
942 read_meta(struct got_pack_meta ***meta, int *nmeta,
943 struct got_object_id **theirs, int ntheirs,
944 struct got_object_id **ours, int nours, struct got_repository *repo,
945 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
946 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
948 const struct got_error *err = NULL;
949 struct got_object_id **ids = NULL;
950 struct got_object_idset *idset;
951 int i, nobj = 0, obj_type;
952 struct got_pack_metavec v;
954 *meta = NULL;
955 *nmeta = 0;
957 idset = got_object_idset_alloc();
958 if (idset == NULL)
959 return got_error_from_errno("got_object_idset_alloc");
961 v.nmeta = 0;
962 v.metasz = 64;
963 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
964 if (v.meta == NULL) {
965 err = got_error_from_errno("calloc");
966 goto done;
969 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
970 cancel_cb, cancel_arg);
971 if (err || nobj == 0)
972 goto done;
974 for (i = 0; i < ntheirs; i++) {
975 struct got_object_id *id = theirs[i];
976 if (id == NULL)
977 continue;
978 err = got_object_get_type(&obj_type, repo, id);
979 if (err)
980 return err;
981 if (obj_type != GOT_OBJ_TYPE_COMMIT)
982 continue;
983 err = load_commit(NULL, idset, id, repo,
984 loose_obj_only, cancel_cb, cancel_arg);
985 if (err)
986 goto done;
987 err = report_progress(progress_cb, progress_arg, rl,
988 0L, nours, v.nmeta, 0, 0);
989 if (err)
990 goto done;
993 for (i = 0; i < ntheirs; i++) {
994 struct got_object_id *id = theirs[i];
995 int *cached_type;
996 if (id == NULL)
997 continue;
998 cached_type = got_object_idset_get(idset, id);
999 if (cached_type == NULL) {
1000 err = got_object_get_type(&obj_type, repo, id);
1001 if (err)
1002 goto done;
1003 } else
1004 obj_type = *cached_type;
1005 if (obj_type != GOT_OBJ_TYPE_TAG)
1006 continue;
1007 err = load_tag(NULL, idset, id, repo,
1008 loose_obj_only, cancel_cb, cancel_arg);
1009 if (err)
1010 goto done;
1011 err = report_progress(progress_cb, progress_arg, rl,
1012 0L, nours, v.nmeta, 0, 0);
1013 if (err)
1014 goto done;
1017 for (i = 0; i < nobj; i++) {
1018 err = load_commit(&v, idset, ids[i], repo,
1019 loose_obj_only, cancel_cb, cancel_arg);
1020 if (err)
1021 goto done;
1022 if (err)
1023 goto done;
1024 err = report_progress(progress_cb, progress_arg, rl,
1025 0L, nours, v.nmeta, 0, 0);
1026 if (err)
1027 goto done;
1030 for (i = 0; i < nours; i++) {
1031 struct got_object_id *id = ours[i];
1032 int *cached_type;
1033 if (id == NULL)
1034 continue;
1035 cached_type = got_object_idset_get(idset, id);
1036 if (cached_type == NULL) {
1037 err = got_object_get_type(&obj_type, repo, id);
1038 if (err)
1039 goto done;
1040 } else
1041 obj_type = *cached_type;
1042 if (obj_type != GOT_OBJ_TYPE_TAG)
1043 continue;
1044 err = load_tag(&v, idset, id, repo,
1045 loose_obj_only, cancel_cb, cancel_arg);
1046 if (err)
1047 goto done;
1048 err = report_progress(progress_cb, progress_arg, rl,
1049 0L, nours, v.nmeta, 0, 0);
1050 if (err)
1051 goto done;
1054 if (progress_cb) {
1055 err = progress_cb(progress_arg, 0L, nours, v.nmeta, 0, 0);
1056 if (err)
1057 goto done;
1059 done:
1060 for (i = 0; i < nobj; i++) {
1061 free(ids[i]);
1063 free(ids);
1064 got_object_idset_free(idset);
1065 if (err == NULL) {
1066 *meta = v.meta;
1067 *nmeta = v.nmeta;
1068 } else
1069 free(v.meta);
1071 return err;
1074 const struct got_error *
1075 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1077 size_t n;
1079 SHA1Update(ctx, buf, len);
1080 n = fwrite(buf, 1, len, f);
1081 if (n != len)
1082 return got_ferror(f, GOT_ERR_IO);
1083 return NULL;
1086 static void
1087 putbe32(char *b, uint32_t n)
1089 b[0] = n >> 24;
1090 b[1] = n >> 16;
1091 b[2] = n >> 8;
1092 b[3] = n >> 0;
1095 static int
1096 write_order_cmp(const void *pa, const void *pb)
1098 struct got_pack_meta *a, *b, *ahd, *bhd;
1100 a = *(struct got_pack_meta **)pa;
1101 b = *(struct got_pack_meta **)pb;
1102 ahd = (a->head == NULL) ? a : a->head;
1103 bhd = (b->head == NULL) ? b : b->head;
1104 if (ahd->mtime != bhd->mtime)
1105 return bhd->mtime - ahd->mtime;
1106 if (ahd != bhd)
1107 return (uintptr_t)bhd - (uintptr_t)ahd;
1108 if (a->nchain != b->nchain)
1109 return a->nchain - b->nchain;
1110 return a->mtime - b->mtime;
1113 static const struct got_error *
1114 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1116 size_t i;
1118 *hdrlen = 0;
1120 hdr[0] = obj_type << 4;
1121 hdr[0] |= len & 0xf;
1122 len >>= 4;
1123 for (i = 1; len != 0; i++){
1124 if (i >= bufsize)
1125 return got_error(GOT_ERR_NO_SPACE);
1126 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1127 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1128 len >>= GOT_DELTA_SIZE_SHIFT;
1131 *hdrlen = i;
1132 return NULL;
1135 static int
1136 packoff(char *hdr, off_t off)
1138 int i, j;
1139 char rbuf[8];
1141 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1142 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1143 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1144 GOT_DELTA_SIZE_MORE;
1147 j = 0;
1148 while (i > 0)
1149 hdr[j++] = rbuf[--i];
1150 return j;
1153 static const struct got_error *
1154 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1155 struct got_pack_meta **meta, int nmeta, int nours,
1156 int use_offset_deltas, struct got_repository *repo,
1157 got_pack_progress_cb progress_cb, void *progress_arg,
1158 struct got_ratelimit *rl,
1159 got_cancel_cb cancel_cb, void *cancel_arg)
1161 const struct got_error *err = NULL;
1162 int i, nh;
1163 SHA1_CTX ctx;
1164 struct got_pack_meta *m;
1165 struct got_raw_object *raw = NULL;
1166 FILE *delta_file = NULL;
1167 char buf[32];
1168 size_t outlen, n;
1169 struct got_deflate_checksum csum;
1170 off_t packfile_size = 0;
1171 int outfd = -1;
1173 SHA1Init(&ctx);
1174 csum.output_sha1 = &ctx;
1175 csum.output_crc = NULL;
1177 err = hwrite(packfile, "PACK", 4, &ctx);
1178 if (err)
1179 return err;
1180 putbe32(buf, GOT_PACKFILE_VERSION);
1181 err = hwrite(packfile, buf, 4, &ctx);
1182 if (err)
1183 goto done;
1184 putbe32(buf, nmeta);
1185 err = hwrite(packfile, buf, 4, &ctx);
1186 if (err)
1187 goto done;
1188 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1189 for (i = 0; i < nmeta; i++) {
1190 err = report_progress(progress_cb, progress_arg, rl,
1191 packfile_size, nours, nmeta, nmeta, i);
1192 if (err)
1193 goto done;
1194 m = meta[i];
1195 m->off = ftello(packfile);
1196 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1197 if (err)
1198 goto done;
1199 if (m->delta_len == 0) {
1200 err = packhdr(&nh, buf, sizeof(buf),
1201 m->obj_type, raw->size);
1202 if (err)
1203 goto done;
1204 err = hwrite(packfile, buf, nh, &ctx);
1205 if (err)
1206 goto done;
1207 packfile_size += nh;
1208 if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1209 err = got_error_from_errno("fseeko");
1210 goto done;
1212 err = got_deflate_to_file(&outlen, raw->f, packfile,
1213 &csum);
1214 if (err)
1215 goto done;
1216 packfile_size += outlen;
1217 } else {
1218 off_t remain;
1219 if (delta_file == NULL) {
1220 delta_file = got_opentemp();
1221 if (delta_file == NULL) {
1222 err = got_error_from_errno(
1223 "got_opentemp");
1224 goto done;
1227 if (ftruncate(fileno(delta_file), 0L) == -1) {
1228 err = got_error_from_errno("ftruncate");
1229 goto done;
1231 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1232 err = got_error_from_errno("fseeko");
1233 goto done;
1235 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1236 == -1) {
1237 err = got_error_from_errno("fseeko");
1238 goto done;
1240 remain = m->delta_len;
1241 while (remain > 0) {
1242 char delta_buf[8192];
1243 size_t r, w, n;
1244 n = MIN(remain, sizeof(delta_buf));
1245 r = fread(delta_buf, 1, n, delta_cache);
1246 if (r != n) {
1247 err = got_ferror(delta_cache,
1248 GOT_ERR_IO);
1249 goto done;
1251 w = fwrite(delta_buf, 1, n, delta_file);
1252 if (w != n) {
1253 err = got_ferror(delta_file,
1254 GOT_ERR_IO);
1255 goto done;
1257 remain -= n;
1259 if (use_offset_deltas && m->prev->off != 0) {
1260 err = packhdr(&nh, buf, sizeof(buf),
1261 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1262 if (err)
1263 goto done;
1264 nh += packoff(buf + nh,
1265 m->off - m->prev->off);
1266 err = hwrite(packfile, buf, nh, &ctx);
1267 if (err)
1268 goto done;
1269 packfile_size += nh;
1270 } else {
1271 err = packhdr(&nh, buf, sizeof(buf),
1272 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1273 err = hwrite(packfile, buf, nh, &ctx);
1274 if (err)
1275 goto done;
1276 packfile_size += nh;
1277 err = hwrite(packfile, m->prev->id.sha1,
1278 sizeof(m->prev->id.sha1), &ctx);
1279 packfile_size += sizeof(m->prev->id.sha1);
1280 if (err)
1281 goto done;
1283 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1284 err = got_error_from_errno("fseeko");
1285 goto done;
1287 err = got_deflate_to_file(&outlen, delta_file,
1288 packfile, &csum);
1289 if (err)
1290 goto done;
1291 packfile_size += outlen;
1293 got_object_raw_close(raw);
1294 raw = NULL;
1296 SHA1Final(pack_sha1, &ctx);
1297 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1298 if (n != SHA1_DIGEST_LENGTH)
1299 err = got_ferror(packfile, GOT_ERR_IO);
1300 packfile_size += SHA1_DIGEST_LENGTH;
1301 packfile_size += sizeof(struct got_packfile_hdr);
1302 if (progress_cb) {
1303 err = progress_cb(progress_arg, packfile_size, nours,
1304 nmeta, nmeta, nmeta);
1305 if (err)
1306 goto done;
1308 done:
1309 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1310 err = got_error_from_errno("fclose");
1311 if (raw)
1312 got_object_raw_close(raw);
1313 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1314 err = got_error_from_errno("close");
1315 return err;
1318 const struct got_error *
1319 got_pack_create(uint8_t *packsha1, FILE *packfile,
1320 struct got_object_id **theirs, int ntheirs,
1321 struct got_object_id **ours, int nours,
1322 struct got_repository *repo, int loose_obj_only, int allow_empty,
1323 got_pack_progress_cb progress_cb, void *progress_arg,
1324 got_cancel_cb cancel_cb, void *cancel_arg)
1326 const struct got_error *err;
1327 struct got_pack_meta **meta;
1328 int nmeta;
1329 FILE *delta_cache = NULL;
1330 struct got_ratelimit rl;
1332 got_ratelimit_init(&rl, 0, 500);
1334 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1335 loose_obj_only, progress_cb, progress_arg, &rl,
1336 cancel_cb, cancel_arg);
1337 if (err)
1338 return err;
1340 if (nmeta == 0 && !allow_empty) {
1341 err = got_error(GOT_ERR_CANNOT_PACK);
1342 goto done;
1345 delta_cache = got_opentemp();
1346 if (delta_cache == NULL) {
1347 err = got_error_from_errno("got_opentemp");
1348 goto done;
1351 if (nmeta > 0) {
1352 err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1353 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1354 if (err)
1355 goto done;
1356 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1357 err = got_error_from_errno("fseeko");
1358 goto done;
1362 err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1363 repo, progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1364 if (err)
1365 goto done;
1366 done:
1367 free_nmeta(meta, nmeta);
1368 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1369 err = got_error_from_errno("fclose");
1370 return err;