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>
24 #include <sys/mman.h>
26 #include <endian.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <imsg.h>
30 #include <inttypes.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sha1.h>
35 #include <time.h>
36 #include <unistd.h>
37 #include <limits.h>
38 #include <zlib.h>
40 #include "got_error.h"
41 #include "got_cancel.h"
42 #include "got_object.h"
43 #include "got_path.h"
44 #include "got_reference.h"
45 #include "got_repository_admin.h"
47 #include "got_lib_deltify.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_idset.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_deflate.h"
53 #include "got_lib_ratelimit.h"
54 #include "got_lib_pack.h"
55 #include "got_lib_pack_create.h"
56 #include "got_lib_repository.h"
57 #include "got_lib_inflate.h"
59 #include "murmurhash2.h"
61 #ifndef MIN
62 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
63 #endif
65 #ifndef MAX
66 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
67 #endif
69 #ifndef nitems
70 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
71 #endif
73 static const struct got_error *
74 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
75 const char *path, int obj_type, time_t mtime, uint32_t seed)
76 {
77 struct got_pack_meta *m;
79 *new = NULL;
81 m = calloc(1, sizeof(*m));
82 if (m == NULL)
83 return got_error_from_errno("calloc");
85 memcpy(&m->id, id, sizeof(m->id));
87 m->path_hash = murmurhash2(path, strlen(path), seed);
88 m->obj_type = obj_type;
89 m->mtime = mtime;
90 *new = m;
91 return NULL;
92 }
94 static void
95 clear_meta(struct got_pack_meta *meta)
96 {
97 if (meta == NULL)
98 return;
99 meta->path_hash = 0;
100 free(meta->delta_buf);
101 meta->delta_buf = NULL;
102 free(meta->base_obj_id);
103 meta->base_obj_id = NULL;
104 meta->reused_delta_offset = 0;
107 static void
108 free_nmeta(struct got_pack_meta **meta, int nmeta)
110 int i;
112 for (i = 0; i < nmeta; i++)
113 clear_meta(meta[i]);
114 free(meta);
117 static int
118 delta_order_cmp(const void *pa, const void *pb)
120 struct got_pack_meta *a, *b;
122 a = *(struct got_pack_meta **)pa;
123 b = *(struct got_pack_meta **)pb;
125 if (a->obj_type != b->obj_type)
126 return a->obj_type - b->obj_type;
127 if (a->path_hash < b->path_hash)
128 return -1;
129 if (a->path_hash > b->path_hash)
130 return 1;
131 if (a->mtime < b->mtime)
132 return -1;
133 if (a->mtime > b->mtime)
134 return 1;
135 return got_object_id_cmp(&a->id, &b->id);
138 static off_t
139 delta_size(struct got_delta_instruction *deltas, int ndeltas)
141 int i;
142 off_t size = 32;
143 for (i = 0; i < ndeltas; i++) {
144 if (deltas[i].copy)
145 size += GOT_DELTA_SIZE_SHIFT;
146 else
147 size += deltas[i].len + 1;
149 return size;
152 static const struct got_error *
153 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
155 char *n;
157 if (*len + nseg >= *sz) {
158 while (*len + nseg >= *sz)
159 *sz += *sz / 2;
160 n = realloc(*p, *sz);
161 if (n == NULL)
162 return got_error_from_errno("realloc");
163 *p = n;
165 memcpy(*p + *len, seg, nseg);
166 *len += nseg;
167 return NULL;
170 static const struct got_error *
171 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
172 struct got_delta_instruction *deltas, int ndeltas,
173 off_t delta_size, off_t base_size)
175 const struct got_error *err;
176 unsigned char buf[16], *bp;
177 int i, j;
178 size_t len = 0, compressed_len;
179 off_t bufsize = delta_size;
180 off_t n;
181 struct got_delta_instruction *d;
182 uint8_t *delta_buf;
184 delta_buf = malloc(bufsize);
185 if (delta_buf == NULL)
186 return got_error_from_errno("malloc");
188 /* base object size */
189 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
190 n = base_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 err = append(&delta_buf, &len, &bufsize, buf, i);
197 if (err)
198 goto done;
200 /* target object size */
201 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
202 n = o->size >> GOT_DELTA_SIZE_SHIFT;
203 for (i = 1; n > 0; i++) {
204 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
205 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
206 n >>= GOT_DELTA_SIZE_SHIFT;
208 err = append(&delta_buf, &len, &bufsize, buf, i);
209 if (err)
210 goto done;
212 for (j = 0; j < ndeltas; j++) {
213 d = &deltas[j];
214 if (d->copy) {
215 n = d->offset;
216 bp = &buf[1];
217 buf[0] = GOT_DELTA_BASE_COPY;
218 for (i = 0; i < 4; i++) {
219 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
220 buf[0] |= 1 << i;
221 *bp++ = n & 0xff;
222 n >>= 8;
223 if (n == 0)
224 break;
227 n = d->len;
228 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
229 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
230 for (i = 0; i < 3 && n > 0; i++) {
231 buf[0] |= 1 << (i + 4);
232 *bp++ = n & 0xff;
233 n >>= 8;
236 err = append(&delta_buf, &len, &bufsize,
237 buf, bp - buf);
238 if (err)
239 goto done;
240 } else if (o->f == NULL) {
241 n = 0;
242 while (n != d->len) {
243 buf[0] = (d->len - n < 127) ? d->len - n : 127;
244 err = append(&delta_buf, &len, &bufsize,
245 buf, 1);
246 if (err)
247 goto done;
248 err = append(&delta_buf, &len, &bufsize,
249 o->data + o->hdrlen + d->offset + n,
250 buf[0]);
251 if (err)
252 goto done;
253 n += buf[0];
255 } else {
256 char content[128];
257 size_t r;
258 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
259 err = got_error_from_errno("fseeko");
260 goto done;
262 n = 0;
263 while (n != d->len) {
264 buf[0] = (d->len - n < 127) ? d->len - n : 127;
265 err = append(&delta_buf, &len, &bufsize,
266 buf, 1);
267 if (err)
268 goto done;
269 r = fread(content, 1, buf[0], o->f);
270 if (r != buf[0]) {
271 err = got_ferror(o->f, GOT_ERR_IO);
272 goto done;
274 err = append(&delta_buf, &len, &bufsize,
275 content, buf[0]);
276 if (err)
277 goto done;
278 n += buf[0];
283 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
284 NULL, NULL, delta_buf, 0, len);
285 if (err)
286 goto done;
288 m->delta_len = len;
289 m->delta_compressed_len = compressed_len;
290 done:
291 free(delta_buf);
292 return err;
295 static const struct got_error *
296 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
297 struct got_delta_instruction *deltas, int ndeltas,
298 off_t base_size, FILE *f)
300 const struct got_error *err;
301 unsigned char buf[16], *bp;
302 int i, j;
303 off_t n;
304 struct got_deflate_buf zb;
305 struct got_delta_instruction *d;
306 off_t delta_len = 0, compressed_len = 0;
308 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
309 if (err)
310 return err;
312 /* base object size */
313 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
314 n = base_size >> GOT_DELTA_SIZE_SHIFT;
315 for (i = 1; n > 0; i++) {
316 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
317 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
318 n >>= GOT_DELTA_SIZE_SHIFT;
321 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
322 buf, 0, i, f, NULL);
323 if (err)
324 goto done;
325 delta_len += i;
327 /* target object size */
328 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
329 n = o->size >> GOT_DELTA_SIZE_SHIFT;
330 for (i = 1; n > 0; i++) {
331 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
332 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
333 n >>= GOT_DELTA_SIZE_SHIFT;
336 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
337 buf, 0, i, f, NULL);
338 if (err)
339 goto done;
340 delta_len += i;
342 for (j = 0; j < ndeltas; j++) {
343 d = &deltas[j];
344 if (d->copy) {
345 n = d->offset;
346 bp = &buf[1];
347 buf[0] = GOT_DELTA_BASE_COPY;
348 for (i = 0; i < 4; i++) {
349 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
350 buf[0] |= 1 << i;
351 *bp++ = n & 0xff;
352 n >>= 8;
353 if (n == 0)
354 break;
356 n = d->len;
357 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
358 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
359 for (i = 0; i < 3 && n > 0; i++) {
360 buf[0] |= 1 << (i + 4);
361 *bp++ = n & 0xff;
362 n >>= 8;
365 err = got_deflate_append_to_file_mmap(&zb,
366 &compressed_len, buf, 0, bp - buf, f, NULL);
367 if (err)
368 goto done;
369 delta_len += (bp - buf);
370 } else if (o->f == NULL) {
371 n = 0;
372 while (n != d->len) {
373 buf[0] = (d->len - n < 127) ? d->len - n : 127;
374 err = got_deflate_append_to_file_mmap(&zb,
375 &compressed_len, buf, 0, 1, f, NULL);
376 if (err)
377 goto done;
378 delta_len++;
379 err = got_deflate_append_to_file_mmap(&zb,
380 &compressed_len,
381 o->data + o->hdrlen + d->offset + n, 0,
382 buf[0], f, NULL);
383 if (err)
384 goto done;
385 delta_len += buf[0];
386 n += buf[0];
388 } else {
389 char content[128];
390 size_t r;
391 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
392 err = got_error_from_errno("fseeko");
393 goto done;
395 n = 0;
396 while (n != d->len) {
397 buf[0] = (d->len - n < 127) ? d->len - n : 127;
398 err = got_deflate_append_to_file_mmap(&zb,
399 &compressed_len, buf, 0, 1, f, NULL);
400 if (err)
401 goto done;
402 delta_len++;
403 r = fread(content, 1, buf[0], o->f);
404 if (r != buf[0]) {
405 err = got_ferror(o->f, GOT_ERR_IO);
406 goto done;
408 err = got_deflate_append_to_file_mmap(&zb,
409 &compressed_len, content, 0, buf[0], f,
410 NULL);
411 if (err)
412 goto done;
413 delta_len += buf[0];
414 n += buf[0];
419 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
420 if (err)
421 goto done;
423 /* sanity check */
424 if (compressed_len != ftello(f) - m->delta_offset) {
425 err = got_error(GOT_ERR_COMPRESSION);
426 goto done;
429 m->delta_len = delta_len;
430 m->delta_compressed_len = compressed_len;
431 done:
432 got_deflate_end(&zb);
433 return err;
436 const struct got_error *
437 got_pack_report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
438 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
439 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
440 int nobj_written)
442 const struct got_error *err;
443 int elapsed;
445 if (progress_cb == NULL)
446 return NULL;
448 err = got_ratelimit_check(&elapsed, rl);
449 if (err || !elapsed)
450 return err;
452 return progress_cb(progress_arg, ncolored, nfound, ntrees,
453 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
456 const struct got_error *
457 got_pack_add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
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 return got_error_from_errno("reallocarray");
465 v->meta = new;
466 v->metasz = newsize;
469 v->meta[v->nmeta++] = m;
470 return NULL;
473 const struct got_error *
474 got_pack_find_pack_for_reuse(struct got_packidx **best_packidx,
475 struct got_repository *repo)
477 const struct got_error *err = NULL;
478 struct got_pathlist_entry *pe;
479 const char *best_packidx_path = NULL;
480 int nobj_max = 0;
482 *best_packidx = NULL;
484 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
485 const char *path_packidx = pe->path;
486 struct got_packidx *packidx;
487 int nobj;
489 err = got_repo_get_packidx(&packidx, path_packidx, repo);
490 if (err)
491 break;
493 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
494 if (nobj > nobj_max) {
495 best_packidx_path = path_packidx;
496 nobj_max = nobj;
500 if (best_packidx_path) {
501 err = got_repo_get_packidx(best_packidx, best_packidx_path,
502 repo);
505 return err;
508 const struct got_error *
509 got_pack_cache_pack_for_packidx(struct got_pack **pack,
510 struct got_packidx *packidx, struct got_repository *repo)
512 const struct got_error *err;
513 char *path_packfile = NULL;
515 err = got_packidx_get_packfile_path(&path_packfile,
516 packidx->path_packidx);
517 if (err)
518 return err;
520 *pack = got_repo_get_cached_pack(repo, path_packfile);
521 if (*pack == NULL) {
522 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
523 if (err)
524 goto done;
526 done:
527 free(path_packfile);
528 return err;
531 static const struct got_error *
532 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
533 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
534 struct got_repository *repo,
535 got_pack_progress_cb progress_cb, void *progress_arg,
536 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
538 const struct got_error *err = NULL;
539 struct got_pack_meta *m = NULL, *base = NULL;
540 struct got_raw_object *raw = NULL, *base_raw = NULL;
541 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
542 int i, j, ndeltas, best_ndeltas;
543 off_t size, best_size;
544 const int max_base_candidates = 3;
545 size_t delta_memsize = 0;
546 const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
547 int outfd = -1;
548 uint32_t delta_seed;
550 delta_seed = arc4random();
552 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
553 for (i = 0; i < nmeta; i++) {
554 if (cancel_cb) {
555 err = (*cancel_cb)(cancel_arg);
556 if (err)
557 break;
559 err = got_pack_report_progress(progress_cb, progress_arg, rl,
560 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
561 nreused + i, 0);
562 if (err)
563 goto done;
564 m = meta[i];
566 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
567 m->obj_type == GOT_OBJ_TYPE_TAG)
568 continue;
570 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
571 if (err)
572 goto done;
573 m->size = raw->size;
575 if (raw->f == NULL) {
576 err = got_deltify_init_mem(&m->dtab, raw->data,
577 raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
578 } else {
579 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
580 raw->size + raw->hdrlen, delta_seed);
582 if (err)
583 goto done;
585 if (i > max_base_candidates) {
586 struct got_pack_meta *n = NULL;
587 n = meta[i - (max_base_candidates + 1)];
588 got_deltify_free(n->dtab);
589 n->dtab = NULL;
592 best_size = raw->size;
593 best_ndeltas = 0;
594 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
595 if (cancel_cb) {
596 err = (*cancel_cb)(cancel_arg);
597 if (err)
598 goto done;
600 base = meta[j];
601 /* long chains make unpacking slow, avoid such bases */
602 if (base->nchain >= 128 ||
603 base->obj_type != m->obj_type)
604 continue;
606 err = got_object_raw_open(&base_raw, &outfd, repo,
607 &base->id);
608 if (err)
609 goto done;
611 if (raw->f == NULL && base_raw->f == NULL) {
612 err = got_deltify_mem_mem(&deltas, &ndeltas,
613 raw->data, raw->hdrlen,
614 raw->size + raw->hdrlen, delta_seed,
615 base->dtab, base_raw->data,
616 base_raw->hdrlen,
617 base_raw->size + base_raw->hdrlen);
618 } else if (raw->f == NULL) {
619 err = got_deltify_mem_file(&deltas, &ndeltas,
620 raw->data, raw->hdrlen,
621 raw->size + raw->hdrlen, delta_seed,
622 base->dtab, base_raw->f,
623 base_raw->hdrlen,
624 base_raw->size + base_raw->hdrlen);
625 } else if (base_raw->f == NULL) {
626 err = got_deltify_file_mem(&deltas, &ndeltas,
627 raw->f, raw->hdrlen,
628 raw->size + raw->hdrlen, delta_seed,
629 base->dtab, base_raw->data,
630 base_raw->hdrlen,
631 base_raw->size + base_raw->hdrlen);
632 } else {
633 err = got_deltify(&deltas, &ndeltas,
634 raw->f, raw->hdrlen,
635 raw->size + raw->hdrlen, delta_seed,
636 base->dtab, base_raw->f, base_raw->hdrlen,
637 base_raw->size + base_raw->hdrlen);
639 got_object_raw_close(base_raw);
640 base_raw = NULL;
641 if (err)
642 goto done;
644 size = delta_size(deltas, ndeltas);
645 if (size + 32 < best_size){
646 /*
647 * if we already picked a best delta,
648 * replace it.
649 */
650 best_size = size;
651 free(best_deltas);
652 best_deltas = deltas;
653 best_ndeltas = ndeltas;
654 deltas = NULL;
655 m->nchain = base->nchain + 1;
656 m->prev = base;
657 m->head = base->head;
658 if (m->head == NULL)
659 m->head = base;
660 } else {
661 free(deltas);
662 deltas = NULL;
663 ndeltas = 0;
667 if (best_ndeltas > 0) {
668 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
669 delta_memsize + best_size <= max_delta_memsize) {
670 delta_memsize += best_size;
671 err = encode_delta_in_mem(m, raw, best_deltas,
672 best_ndeltas, best_size, m->prev->size);
673 } else {
674 m->delta_offset = ftello(delta_cache);
675 err = encode_delta(m, raw, best_deltas,
676 best_ndeltas, m->prev->size, delta_cache);
678 free(best_deltas);
679 best_deltas = NULL;
680 best_ndeltas = 0;
681 if (err)
682 goto done;
685 got_object_raw_close(raw);
686 raw = NULL;
688 done:
689 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
690 got_deltify_free(meta[i]->dtab);
691 meta[i]->dtab = NULL;
693 if (raw)
694 got_object_raw_close(raw);
695 if (base_raw)
696 got_object_raw_close(base_raw);
697 if (outfd != -1 && close(outfd) == -1 && err == NULL)
698 err = got_error_from_errno("close");
699 free(deltas);
700 free(best_deltas);
701 return err;
704 static const struct got_error *
705 search_packidx(int *found, struct got_object_id *id,
706 struct got_repository *repo)
708 const struct got_error *err = NULL;
709 struct got_packidx *packidx = NULL;
710 int idx;
712 *found = 0;
714 err = got_repo_search_packidx(&packidx, &idx, repo, id);
715 if (err == NULL)
716 *found = 1; /* object is already packed */
717 else if (err->code == GOT_ERR_NO_OBJ)
718 err = NULL;
719 return err;
722 const struct got_error *
723 got_pack_add_object(int want_meta, struct got_object_idset *idset,
724 struct got_object_id *id, const char *path, int obj_type,
725 time_t mtime, uint32_t seed, int loose_obj_only,
726 struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
727 got_pack_progress_cb progress_cb, void *progress_arg,
728 struct got_ratelimit *rl)
730 const struct got_error *err;
731 struct got_pack_meta *m = NULL;
733 if (loose_obj_only) {
734 int is_packed;
735 err = search_packidx(&is_packed, id, repo);
736 if (err)
737 return err;
738 if (is_packed && want_meta)
739 return NULL;
742 if (want_meta) {
743 err = alloc_meta(&m, id, path, obj_type, mtime, seed);
744 if (err)
745 return err;
747 (*nfound)++;
748 err = got_pack_report_progress(progress_cb, progress_arg, rl,
749 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
750 if (err) {
751 clear_meta(m);
752 free(m);
753 return err;
757 err = got_object_idset_add(idset, id, m);
758 if (err) {
759 clear_meta(m);
760 free(m);
762 return err;
765 const struct got_error *
766 got_pack_load_tree_entries(struct got_object_id_queue *ids, int want_meta,
767 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
768 struct got_tree_object *tree,
769 const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
770 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
771 got_pack_progress_cb progress_cb, void *progress_arg,
772 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
774 const struct got_error *err;
775 char *p = NULL;
776 int i;
778 (*ntrees)++;
779 err = got_pack_report_progress(progress_cb, progress_arg, rl,
780 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
781 if (err)
782 return err;
784 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
785 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
786 struct got_object_id *id = got_tree_entry_get_id(e);
787 mode_t mode = got_tree_entry_get_mode(e);
789 if (cancel_cb) {
790 err = (*cancel_cb)(cancel_arg);
791 if (err)
792 break;
795 if (got_object_tree_entry_is_submodule(e) ||
796 got_object_idset_contains(idset, id) ||
797 got_object_idset_contains(idset_exclude, id))
798 continue;
800 /*
801 * If got-read-pack is crawling trees for us then
802 * we are only here to collect blob IDs.
803 */
804 if (ids == NULL && S_ISDIR(mode))
805 continue;
807 if (asprintf(&p, "%s%s%s", dpath,
808 got_path_is_root_dir(dpath) ? "" : "/",
809 got_tree_entry_get_name(e)) == -1) {
810 err = got_error_from_errno("asprintf");
811 break;
814 if (S_ISDIR(mode)) {
815 struct got_object_qid *qid;
816 err = got_object_qid_alloc(&qid, id);
817 if (err)
818 break;
819 qid->data = p;
820 p = NULL;
821 STAILQ_INSERT_TAIL(ids, qid, entry);
822 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
823 err = got_pack_add_object(want_meta,
824 want_meta ? idset : idset_exclude, id, p,
825 GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
826 repo, ncolored, nfound, ntrees,
827 progress_cb, progress_arg, rl);
828 if (err)
829 break;
830 free(p);
831 p = NULL;
832 } else {
833 free(p);
834 p = NULL;
838 free(p);
839 return err;
842 const struct got_error *
843 got_pack_load_tree(int want_meta, struct got_object_idset *idset,
844 struct got_object_idset *idset_exclude,
845 struct got_object_id *tree_id, const char *dpath, time_t mtime,
846 uint32_t seed, struct got_repository *repo, int loose_obj_only,
847 int *ncolored, int *nfound, int *ntrees,
848 got_pack_progress_cb progress_cb, void *progress_arg,
849 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
851 const struct got_error *err = NULL;
852 struct got_object_id_queue tree_ids;
853 struct got_object_qid *qid;
854 struct got_tree_object *tree = NULL;
856 if (got_object_idset_contains(idset, tree_id) ||
857 got_object_idset_contains(idset_exclude, tree_id))
858 return NULL;
860 err = got_object_qid_alloc(&qid, tree_id);
861 if (err)
862 return err;
863 qid->data = strdup(dpath);
864 if (qid->data == NULL) {
865 err = got_error_from_errno("strdup");
866 got_object_qid_free(qid);
867 return err;
870 STAILQ_INIT(&tree_ids);
871 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
873 while (!STAILQ_EMPTY(&tree_ids)) {
874 const char *path;
875 if (cancel_cb) {
876 err = (*cancel_cb)(cancel_arg);
877 if (err)
878 break;
881 qid = STAILQ_FIRST(&tree_ids);
882 STAILQ_REMOVE_HEAD(&tree_ids, entry);
883 path = qid->data;
885 if (got_object_idset_contains(idset, &qid->id) ||
886 got_object_idset_contains(idset_exclude, &qid->id)) {
887 free(qid->data);
888 got_object_qid_free(qid);
889 continue;
892 err = got_pack_add_object(want_meta,
893 want_meta ? idset : idset_exclude,
894 &qid->id, path, GOT_OBJ_TYPE_TREE,
895 mtime, seed, loose_obj_only, repo,
896 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
897 if (err) {
898 free(qid->data);
899 got_object_qid_free(qid);
900 break;
903 err = got_object_open_as_tree(&tree, repo, &qid->id);
904 if (err) {
905 free(qid->data);
906 got_object_qid_free(qid);
907 break;
910 err = got_pack_load_tree_entries(&tree_ids, want_meta, idset,
911 idset_exclude, tree, path, mtime, seed, repo,
912 loose_obj_only, ncolored, nfound, ntrees,
913 progress_cb, progress_arg, rl,
914 cancel_cb, cancel_arg);
915 free(qid->data);
916 got_object_qid_free(qid);
917 if (err)
918 break;
920 got_object_tree_close(tree);
921 tree = NULL;
924 STAILQ_FOREACH(qid, &tree_ids, entry)
925 free(qid->data);
926 got_object_id_queue_free(&tree_ids);
927 if (tree)
928 got_object_tree_close(tree);
929 return err;
932 static const struct got_error *
933 load_commit(int want_meta, struct got_object_idset *idset,
934 struct got_object_idset *idset_exclude,
935 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
936 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
937 got_pack_progress_cb progress_cb, void *progress_arg,
938 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
940 const struct got_error *err;
941 struct got_commit_object *commit;
943 if (got_object_idset_contains(idset, id) ||
944 got_object_idset_contains(idset_exclude, id))
945 return NULL;
947 if (loose_obj_only) {
948 int is_packed;
949 err = search_packidx(&is_packed, id, repo);
950 if (err)
951 return err;
952 if (is_packed && want_meta)
953 return NULL;
956 err = got_object_open_as_commit(&commit, repo, id);
957 if (err)
958 return err;
960 err = got_pack_add_object(want_meta,
961 want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_COMMIT,
962 got_object_commit_get_committer_time(commit), seed,
963 loose_obj_only, repo,
964 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
965 if (err)
966 goto done;
968 err = got_pack_load_tree(want_meta, idset, idset_exclude,
969 got_object_commit_get_tree_id(commit),
970 "", got_object_commit_get_committer_time(commit), seed,
971 repo, loose_obj_only, ncolored, nfound, ntrees,
972 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
973 done:
974 got_object_commit_close(commit);
975 return err;
978 static const struct got_error *
979 load_tag(int want_meta, struct got_object_idset *idset,
980 struct got_object_idset *idset_exclude,
981 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
982 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
983 got_pack_progress_cb progress_cb, void *progress_arg,
984 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
986 const struct got_error *err;
987 struct got_tag_object *tag = NULL;
989 if (got_object_idset_contains(idset, id) ||
990 got_object_idset_contains(idset_exclude, id))
991 return NULL;
993 if (loose_obj_only) {
994 int is_packed;
995 err = search_packidx(&is_packed, id, repo);
996 if (err)
997 return err;
998 if (is_packed && want_meta)
999 return NULL;
1002 err = got_object_open_as_tag(&tag, repo, id);
1003 if (err)
1004 return err;
1006 err = got_pack_add_object(want_meta,
1007 want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_TAG,
1008 got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1009 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1010 if (err)
1011 goto done;
1013 switch (got_object_tag_get_object_type(tag)) {
1014 case GOT_OBJ_TYPE_COMMIT:
1015 err = load_commit(want_meta, idset, idset_exclude,
1016 got_object_tag_get_object_id(tag), repo, seed,
1017 loose_obj_only, ncolored, nfound, ntrees,
1018 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1019 break;
1020 case GOT_OBJ_TYPE_TREE:
1021 err = got_pack_load_tree(want_meta, idset, idset_exclude,
1022 got_object_tag_get_object_id(tag), "",
1023 got_object_tag_get_tagger_time(tag), seed, repo,
1024 loose_obj_only, ncolored, nfound, ntrees,
1025 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1026 break;
1027 default:
1028 break;
1031 done:
1032 got_object_tag_close(tag);
1033 return err;
1036 const struct got_error *
1037 got_pack_paint_commit(struct got_object_qid *qid, intptr_t color)
1039 if (color < 0 || color >= COLOR_MAX)
1040 return got_error(GOT_ERR_RANGE);
1042 qid->data = (void *)color;
1043 return NULL;
1046 const struct got_error *
1047 got_pack_queue_commit_id(struct got_object_id_queue *ids,
1048 struct got_object_id *id, intptr_t color, struct got_repository *repo)
1050 const struct got_error *err;
1051 struct got_object_qid *qid;
1053 err = got_object_qid_alloc(&qid, id);
1054 if (err)
1055 return err;
1057 STAILQ_INSERT_TAIL(ids, qid, entry);
1058 return got_pack_paint_commit(qid, color);
1061 struct append_id_arg {
1062 struct got_object_id **array;
1063 int idx;
1064 struct got_object_idset *drop;
1065 struct got_object_idset *skip;
1068 static const struct got_error *
1069 append_id(struct got_object_id *id, void *data, void *arg)
1071 struct append_id_arg *a = arg;
1073 if (got_object_idset_contains(a->skip, id) ||
1074 got_object_idset_contains(a->drop, id))
1075 return NULL;
1077 a->array[++a->idx] = got_object_id_dup(id);
1078 if (a->array[a->idx] == NULL)
1079 return got_error_from_errno("got_object_id_dup");
1081 return NULL;
1084 static const struct got_error *
1085 queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1086 struct got_object_id_queue *ids, struct got_repository *repo)
1088 const struct got_error *err;
1089 struct got_tag_object *tag = NULL;
1090 int obj_type;
1092 err = got_object_get_type(&obj_type, repo, id);
1093 if (err)
1094 return err;
1096 if (obj_type == GOT_OBJ_TYPE_TAG) {
1097 err = got_object_open_as_tag(&tag, repo, id);
1098 if (err)
1099 return err;
1100 obj_type = got_object_tag_get_object_type(tag);
1101 id = got_object_tag_get_object_id(tag);
1104 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1105 err = got_pack_queue_commit_id(ids, id, color, repo);
1106 if (err)
1107 goto done;
1109 done:
1110 if (tag)
1111 got_object_tag_close(tag);
1112 return err;
1115 const struct got_error *
1116 got_pack_find_pack_for_commit_painting(struct got_packidx **best_packidx,
1117 struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1119 const struct got_error *err = NULL;
1120 struct got_pathlist_entry *pe;
1121 const char *best_packidx_path = NULL;
1122 int nobj_max = 0;
1123 int ncommits_max = 0;
1125 *best_packidx = NULL;
1128 * Find the largest pack which contains at least some of the
1129 * commits we are interested in.
1131 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1132 const char *path_packidx = pe->path;
1133 struct got_packidx *packidx;
1134 int nobj, idx, ncommits = 0;
1135 struct got_object_qid *qid;
1137 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1138 if (err)
1139 break;
1141 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1142 if (nobj <= nobj_max)
1143 continue;
1145 STAILQ_FOREACH(qid, ids, entry) {
1146 idx = got_packidx_get_object_idx(packidx, &qid->id);
1147 if (idx != -1)
1148 ncommits++;
1150 if (ncommits > ncommits_max) {
1151 best_packidx_path = path_packidx;
1152 nobj_max = nobj;
1153 ncommits_max = ncommits;
1157 if (best_packidx_path && err == NULL) {
1158 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1159 repo);
1162 return err;
1165 static const struct got_error *
1166 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1167 struct got_object_id **head, int nhead,
1168 struct got_object_id **tail, int ntail,
1169 struct got_repository *repo,
1170 got_pack_progress_cb progress_cb, void *progress_arg,
1171 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1173 const struct got_error *err = NULL;
1174 struct got_object_id_queue ids;
1175 struct got_object_idset *keep, *drop, *skip = NULL;
1176 int i, nkeep;
1178 STAILQ_INIT(&ids);
1179 *res = NULL;
1180 *nres = 0;
1181 *ncolored = 0;
1183 keep = got_object_idset_alloc();
1184 if (keep == NULL)
1185 return got_error_from_errno("got_object_idset_alloc");
1187 drop = got_object_idset_alloc();
1188 if (drop == NULL) {
1189 err = got_error_from_errno("got_object_idset_alloc");
1190 goto done;
1193 skip = got_object_idset_alloc();
1194 if (skip == NULL) {
1195 err = got_error_from_errno("got_object_idset_alloc");
1196 goto done;
1199 for (i = 0; i < nhead; i++) {
1200 struct got_object_id *id = head[i];
1201 if (id == NULL)
1202 continue;
1203 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1204 if (err)
1205 goto done;
1208 for (i = 0; i < ntail; i++) {
1209 struct got_object_id *id = tail[i];
1210 if (id == NULL)
1211 continue;
1212 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1213 if (err)
1214 goto done;
1217 err = got_pack_paint_commits(ncolored, &ids, nhead + ntail,
1218 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1219 cancel_cb, cancel_arg);
1220 if (err)
1221 goto done;
1223 nkeep = got_object_idset_num_elements(keep);
1224 if (nkeep > 0) {
1225 struct append_id_arg arg;
1226 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1227 if (arg.array == NULL) {
1228 err = got_error_from_errno("calloc");
1229 goto done;
1231 arg.idx = -1;
1232 arg.skip = skip;
1233 arg.drop = drop;
1234 err = got_object_idset_for_each(keep, append_id, &arg);
1235 if (err) {
1236 free(arg.array);
1237 goto done;
1239 *res = arg.array;
1240 *nres = arg.idx + 1;
1242 done:
1243 got_object_idset_free(keep);
1244 got_object_idset_free(drop);
1245 if (skip)
1246 got_object_idset_free(skip);
1247 got_object_id_queue_free(&ids);
1248 return err;
1251 static const struct got_error *
1252 find_pack_for_enumeration(struct got_packidx **best_packidx,
1253 struct got_object_id **ids, int nids, struct got_repository *repo)
1255 const struct got_error *err = NULL;
1256 struct got_pathlist_entry *pe;
1257 const char *best_packidx_path = NULL;
1258 int nobj_max = 0;
1259 int ncommits_max = 0;
1261 *best_packidx = NULL;
1264 * Find the largest pack which contains at least some of the
1265 * commits and tags we are interested in.
1267 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1268 const char *path_packidx = pe->path;
1269 struct got_packidx *packidx;
1270 int nobj, i, idx, ncommits = 0;
1272 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1273 if (err)
1274 break;
1276 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1277 if (nobj <= nobj_max)
1278 continue;
1280 for (i = 0; i < nids; i++) {
1281 idx = got_packidx_get_object_idx(packidx, ids[i]);
1282 if (idx != -1)
1283 ncommits++;
1285 if (ncommits > ncommits_max) {
1286 best_packidx_path = path_packidx;
1287 nobj_max = nobj;
1288 ncommits_max = ncommits;
1292 if (best_packidx_path && err == NULL) {
1293 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1294 repo);
1297 return err;
1300 static const struct got_error *
1301 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1302 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1303 struct got_object_id **ours, int nours, struct got_repository *repo,
1304 uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1305 void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1306 void *cancel_arg)
1308 const struct got_error *err = NULL;
1309 struct got_object_id **ids = NULL;
1310 struct got_packidx *packidx = NULL;
1311 int i, nobj = 0, obj_type, found_all_objects = 0;
1312 struct got_object_idset *idset_exclude;
1314 idset_exclude = got_object_idset_alloc();
1315 if (idset_exclude == NULL)
1316 return got_error_from_errno("got_object_idset_alloc");
1318 *ncolored = 0;
1319 *nfound = 0;
1320 *ntrees = 0;
1322 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1323 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1324 if (err)
1325 goto done;
1327 err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
1328 if (err)
1329 goto done;
1330 if (packidx) {
1331 err = got_pack_load_packed_object_ids(&found_all_objects,
1332 theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
1333 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1334 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1335 if (err)
1336 goto done;
1339 for (i = 0; i < ntheirs; i++) {
1340 struct got_object_id *id = theirs[i];
1341 if (id == NULL)
1342 continue;
1343 err = got_object_get_type(&obj_type, repo, id);
1344 if (err)
1345 return err;
1346 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1347 if (!found_all_objects) {
1348 err = load_commit(0, idset, idset_exclude,
1349 id, repo, seed, loose_obj_only,
1350 ncolored, nfound, ntrees,
1351 progress_cb, progress_arg, rl,
1352 cancel_cb, cancel_arg);
1353 if (err)
1354 goto done;
1356 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1357 err = load_tag(0, idset, idset_exclude, id, repo,
1358 seed, loose_obj_only, ncolored, nfound, ntrees,
1359 progress_cb, progress_arg, rl,
1360 cancel_cb, cancel_arg);
1361 if (err)
1362 goto done;
1366 found_all_objects = 0;
1367 err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
1368 if (err)
1369 goto done;
1370 if (packidx) {
1371 err = got_pack_load_packed_object_ids(&found_all_objects, ids,
1372 nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
1373 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1374 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1375 if (err)
1376 goto done;
1379 if (!found_all_objects) {
1380 for (i = 0; i < nobj; i++) {
1381 err = load_commit(1, idset, idset_exclude, ids[i],
1382 repo, seed, loose_obj_only, ncolored, nfound,
1383 ntrees, progress_cb, progress_arg, rl,
1384 cancel_cb, cancel_arg);
1385 if (err)
1386 goto done;
1390 for (i = 0; i < nours; i++) {
1391 struct got_object_id *id = ours[i];
1392 struct got_pack_meta *m;
1393 if (id == NULL)
1394 continue;
1395 m = got_object_idset_get(idset, id);
1396 if (m == NULL) {
1397 err = got_object_get_type(&obj_type, repo, id);
1398 if (err)
1399 goto done;
1400 } else
1401 obj_type = m->obj_type;
1402 if (obj_type != GOT_OBJ_TYPE_TAG)
1403 continue;
1404 err = load_tag(1, idset, idset_exclude, id, repo,
1405 seed, loose_obj_only, ncolored, nfound, ntrees,
1406 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1407 if (err)
1408 goto done;
1410 done:
1411 for (i = 0; i < nobj; i++) {
1412 free(ids[i]);
1414 free(ids);
1415 got_object_idset_free(idset_exclude);
1416 return err;
1419 static const struct got_error *
1420 hwrite(int fd, const void *buf, off_t len, SHA1_CTX *ctx)
1422 ssize_t w;
1424 SHA1Update(ctx, buf, len);
1425 w = write(fd, buf, len);
1426 if (w == -1)
1427 return got_error_from_errno("write");
1428 else if (w != len)
1429 return got_error(GOT_ERR_IO);
1430 return NULL;
1433 static const struct got_error *
1434 hcopy(FILE *fsrc, int fd_dst, off_t len, SHA1_CTX *ctx)
1436 unsigned char buf[65536];
1437 off_t remain = len;
1438 size_t n;
1439 ssize_t w;
1441 while (remain > 0) {
1442 size_t copylen = MIN(sizeof(buf), remain);
1443 n = fread(buf, 1, copylen, fsrc);
1444 if (n != copylen)
1445 return got_ferror(fsrc, GOT_ERR_IO);
1446 SHA1Update(ctx, buf, copylen);
1447 w = write(fd_dst, buf, copylen);
1448 if (w == -1)
1449 return got_error_from_errno("write");
1450 else if (w != copylen)
1451 return got_error(GOT_ERR_IO);
1452 remain -= copylen;
1455 return NULL;
1458 static const struct got_error *
1459 hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1460 int fd, off_t len, SHA1_CTX *ctx)
1462 ssize_t w;
1464 if (src_offset + len > src_size)
1465 return got_error(GOT_ERR_RANGE);
1467 SHA1Update(ctx, src + src_offset, len);
1468 w = write(fd, src + src_offset, len);
1469 if (w == -1)
1470 return got_error_from_errno("write");
1471 else if (w != len)
1472 return got_error(GOT_ERR_IO);
1473 return NULL;
1476 static void
1477 putbe32(char *b, uint32_t n)
1479 b[0] = n >> 24;
1480 b[1] = n >> 16;
1481 b[2] = n >> 8;
1482 b[3] = n >> 0;
1485 static int
1486 write_order_cmp(const void *pa, const void *pb)
1488 struct got_pack_meta *a, *b, *ahd, *bhd;
1490 a = *(struct got_pack_meta **)pa;
1491 b = *(struct got_pack_meta **)pb;
1492 ahd = (a->head == NULL) ? a : a->head;
1493 bhd = (b->head == NULL) ? b : b->head;
1494 if (bhd->mtime < ahd->mtime)
1495 return -1;
1496 if (bhd->mtime > ahd->mtime)
1497 return 1;
1498 if (bhd < ahd)
1499 return -1;
1500 if (bhd > ahd)
1501 return 1;
1502 if (a->nchain != b->nchain)
1503 return a->nchain - b->nchain;
1504 if (a->mtime < b->mtime)
1505 return -1;
1506 if (a->mtime > b->mtime)
1507 return 1;
1508 return got_object_id_cmp(&a->id, &b->id);
1511 static int
1512 reuse_write_order_cmp(const void *pa, const void *pb)
1514 struct got_pack_meta *a, *b;
1516 a = *(struct got_pack_meta **)pa;
1517 b = *(struct got_pack_meta **)pb;
1519 if (a->reused_delta_offset < b->reused_delta_offset)
1520 return -1;
1521 if (a->reused_delta_offset > b->reused_delta_offset)
1522 return 1;
1523 return 0;
1526 static const struct got_error *
1527 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1529 size_t i;
1531 *hdrlen = 0;
1533 hdr[0] = obj_type << 4;
1534 hdr[0] |= len & 0xf;
1535 len >>= 4;
1536 for (i = 1; len != 0; i++){
1537 if (i >= bufsize)
1538 return got_error(GOT_ERR_NO_SPACE);
1539 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1540 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1541 len >>= GOT_DELTA_SIZE_SHIFT;
1544 *hdrlen = i;
1545 return NULL;
1548 static int
1549 packoff(char *hdr, off_t off)
1551 int i, j;
1552 char rbuf[8];
1554 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1555 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1556 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1557 GOT_DELTA_SIZE_MORE;
1560 j = 0;
1561 while (i > 0)
1562 hdr[j++] = rbuf[--i];
1563 return j;
1566 static const struct got_error *
1567 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, int packfd,
1568 struct got_pack_meta *m)
1570 const struct got_error *err;
1571 char buf[32];
1572 int nh;
1574 if (m->prev->off != 0) {
1575 err = packhdr(&nh, buf, sizeof(buf),
1576 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1577 if (err)
1578 return err;
1579 nh += packoff(buf + nh, m->off - m->prev->off);
1580 err = hwrite(packfd, buf, nh, ctx);
1581 if (err)
1582 return err;
1583 *packfile_size += nh;
1584 } else {
1585 err = packhdr(&nh, buf, sizeof(buf),
1586 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1587 if (err)
1588 return err;
1589 err = hwrite(packfd, buf, nh, ctx);
1590 if (err)
1591 return err;
1592 *packfile_size += nh;
1593 err = hwrite(packfd, m->prev->id.sha1,
1594 sizeof(m->prev->id.sha1), ctx);
1595 if (err)
1596 return err;
1597 *packfile_size += sizeof(m->prev->id.sha1);
1600 return NULL;
1603 static const struct got_error *
1604 write_packed_object(off_t *packfile_size, int packfd,
1605 FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1606 struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
1607 struct got_repository *repo)
1609 const struct got_error *err = NULL;
1610 struct got_deflate_checksum csum;
1611 char buf[32];
1612 int nh;
1613 struct got_raw_object *raw = NULL;
1614 off_t outlen;
1616 csum.output_sha1 = ctx;
1617 csum.output_crc = NULL;
1619 m->off = *packfile_size;
1620 if (m->delta_len == 0) {
1621 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1622 if (err)
1623 goto done;
1624 err = packhdr(&nh, buf, sizeof(buf),
1625 m->obj_type, raw->size);
1626 if (err)
1627 goto done;
1628 err = hwrite(packfd, buf, nh, ctx);
1629 if (err)
1630 goto done;
1631 *packfile_size += nh;
1632 if (raw->f == NULL) {
1633 err = got_deflate_to_fd_mmap(&outlen,
1634 raw->data + raw->hdrlen, 0, raw->size,
1635 packfd, &csum);
1636 if (err)
1637 goto done;
1638 } else {
1639 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1640 == -1) {
1641 err = got_error_from_errno("fseeko");
1642 goto done;
1644 err = got_deflate_to_fd(&outlen, raw->f,
1645 raw->size, packfd, &csum);
1646 if (err)
1647 goto done;
1649 *packfile_size += outlen;
1650 got_object_raw_close(raw);
1651 raw = NULL;
1652 } else if (m->delta_buf) {
1653 err = deltahdr(packfile_size, ctx, packfd, m);
1654 if (err)
1655 goto done;
1656 err = hwrite(packfd, m->delta_buf,
1657 m->delta_compressed_len, ctx);
1658 if (err)
1659 goto done;
1660 *packfile_size += m->delta_compressed_len;
1661 free(m->delta_buf);
1662 m->delta_buf = NULL;
1663 } else if (delta_cache_map) {
1664 err = deltahdr(packfile_size, ctx, packfd, m);
1665 if (err)
1666 goto done;
1667 err = hcopy_mmap(delta_cache_map, m->delta_offset,
1668 delta_cache_size, packfd, m->delta_compressed_len,
1669 ctx);
1670 if (err)
1671 goto done;
1672 *packfile_size += m->delta_compressed_len;
1673 } else {
1674 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1675 == -1) {
1676 err = got_error_from_errno("fseeko");
1677 goto done;
1679 err = deltahdr(packfile_size, ctx, packfd, m);
1680 if (err)
1681 goto done;
1682 err = hcopy(delta_cache, packfd,
1683 m->delta_compressed_len, ctx);
1684 if (err)
1685 goto done;
1686 *packfile_size += m->delta_compressed_len;
1688 done:
1689 if (raw)
1690 got_object_raw_close(raw);
1691 return err;
1694 static const struct got_error *
1695 genpack(uint8_t *pack_sha1, int packfd, FILE *delta_cache,
1696 struct got_pack_meta **deltify, int ndeltify,
1697 struct got_pack_meta **reuse, int nreuse,
1698 int ncolored, int nfound, int ntrees, int nours,
1699 struct got_repository *repo,
1700 got_pack_progress_cb progress_cb, void *progress_arg,
1701 struct got_ratelimit *rl,
1702 got_cancel_cb cancel_cb, void *cancel_arg)
1704 const struct got_error *err = NULL;
1705 int i;
1706 SHA1_CTX ctx;
1707 struct got_pack_meta *m;
1708 char buf[32];
1709 ssize_t w;
1710 off_t packfile_size = 0;
1711 int outfd = -1;
1712 int delta_cache_fd = -1;
1713 uint8_t *delta_cache_map = NULL;
1714 size_t delta_cache_size = 0;
1716 SHA1Init(&ctx);
1718 #ifndef GOT_PACK_NO_MMAP
1719 delta_cache_fd = dup(fileno(delta_cache));
1720 if (delta_cache_fd != -1) {
1721 struct stat sb;
1722 if (fstat(delta_cache_fd, &sb) == -1) {
1723 err = got_error_from_errno("fstat");
1724 goto done;
1726 if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1727 delta_cache_map = mmap(NULL, sb.st_size,
1728 PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1729 if (delta_cache_map == MAP_FAILED) {
1730 if (errno != ENOMEM) {
1731 err = got_error_from_errno("mmap");
1732 goto done;
1734 delta_cache_map = NULL; /* fallback on stdio */
1735 } else
1736 delta_cache_size = (size_t)sb.st_size;
1739 #endif
1740 err = hwrite(packfd, "PACK", 4, &ctx);
1741 if (err)
1742 goto done;
1743 putbe32(buf, GOT_PACKFILE_VERSION);
1744 err = hwrite(packfd, buf, 4, &ctx);
1745 if (err)
1746 goto done;
1747 putbe32(buf, ndeltify + nreuse);
1748 err = hwrite(packfd, buf, 4, &ctx);
1749 if (err)
1750 goto done;
1752 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1753 write_order_cmp);
1754 for (i = 0; i < ndeltify; i++) {
1755 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1756 ncolored, nfound, ntrees, packfile_size, nours,
1757 ndeltify + nreuse, ndeltify + nreuse, i);
1758 if (err)
1759 goto done;
1760 m = deltify[i];
1761 err = write_packed_object(&packfile_size, packfd,
1762 delta_cache, delta_cache_map, delta_cache_size,
1763 m, &outfd, &ctx, repo);
1764 if (err)
1765 goto done;
1768 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1769 reuse_write_order_cmp);
1770 for (i = 0; i < nreuse; i++) {
1771 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1772 ncolored, nfound, ntrees, packfile_size, nours,
1773 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1774 if (err)
1775 goto done;
1776 m = reuse[i];
1777 err = write_packed_object(&packfile_size, packfd,
1778 delta_cache, delta_cache_map, delta_cache_size,
1779 m, &outfd, &ctx, repo);
1780 if (err)
1781 goto done;
1784 SHA1Final(pack_sha1, &ctx);
1785 w = write(packfd, pack_sha1, SHA1_DIGEST_LENGTH);
1786 if (w == -1)
1787 err = got_error_from_errno("write");
1788 else if (w != SHA1_DIGEST_LENGTH)
1789 err = got_error(GOT_ERR_IO);
1790 if (err)
1791 goto done;
1792 packfile_size += SHA1_DIGEST_LENGTH;
1793 packfile_size += sizeof(struct got_packfile_hdr);
1794 if (progress_cb) {
1795 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1796 packfile_size, nours, ndeltify + nreuse,
1797 ndeltify + nreuse, ndeltify + nreuse);
1798 if (err)
1799 goto done;
1801 done:
1802 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1803 err = got_error_from_errno("close");
1804 if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1805 err = got_error_from_errno("munmap");
1806 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1807 err = got_error_from_errno("close");
1808 return err;
1811 static const struct got_error *
1812 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1814 struct got_pack_meta *m = data;
1815 struct got_pack_metavec *v = arg;
1817 if (m->reused_delta_offset != 0)
1818 return NULL;
1820 return got_pack_add_meta(m, v);
1823 const struct got_error *
1824 got_pack_create(uint8_t *packsha1, int packfd, FILE *delta_cache,
1825 struct got_object_id **theirs, int ntheirs,
1826 struct got_object_id **ours, int nours,
1827 struct got_repository *repo, int loose_obj_only, int allow_empty,
1828 got_pack_progress_cb progress_cb, void *progress_arg,
1829 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1831 const struct got_error *err;
1832 int delta_cache_fd = -1;
1833 struct got_object_idset *idset;
1834 struct got_pack_metavec deltify, reuse;
1835 int ncolored = 0, nfound = 0, ntrees = 0;
1836 size_t ndeltify;
1837 uint32_t seed;
1839 seed = arc4random();
1841 memset(&deltify, 0, sizeof(deltify));
1842 memset(&reuse, 0, sizeof(reuse));
1844 idset = got_object_idset_alloc();
1845 if (idset == NULL)
1846 return got_error_from_errno("got_object_idset_alloc");
1848 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1849 ntheirs, ours, nours, repo, seed, loose_obj_only,
1850 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1851 if (err)
1852 goto done;
1854 if (progress_cb) {
1855 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1856 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1857 if (err)
1858 goto done;
1861 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1862 err = got_error(GOT_ERR_CANNOT_PACK);
1863 goto done;
1866 delta_cache_fd = dup(fileno(delta_cache));
1867 if (delta_cache_fd == -1) {
1868 err = got_error_from_errno("dup");
1869 goto done;
1872 reuse.metasz = 64;
1873 reuse.meta = calloc(reuse.metasz,
1874 sizeof(struct got_pack_meta *));
1875 if (reuse.meta == NULL) {
1876 err = got_error_from_errno("calloc");
1877 goto done;
1880 err = got_pack_search_deltas(&reuse, idset, delta_cache_fd,
1881 ncolored, nfound, ntrees, nours,
1882 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1883 if (err)
1884 goto done;
1886 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1887 err = got_error_from_errno("fseeko");
1888 goto done;
1891 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1892 if (ndeltify > 0) {
1893 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1894 if (deltify.meta == NULL) {
1895 err = got_error_from_errno("calloc");
1896 goto done;
1898 deltify.metasz = ndeltify;
1900 err = got_object_idset_for_each(idset, add_meta_idset_cb,
1901 &deltify);
1902 if (err)
1903 goto done;
1904 if (deltify.nmeta > 0) {
1905 err = pick_deltas(deltify.meta, deltify.nmeta,
1906 ncolored, nfound, ntrees, nours, reuse.nmeta,
1907 delta_cache, repo, progress_cb, progress_arg, rl,
1908 cancel_cb, cancel_arg);
1909 if (err)
1910 goto done;
1914 if (fflush(delta_cache) == EOF) {
1915 err = got_error_from_errno("fflush");
1916 goto done;
1918 err = genpack(packsha1, packfd, delta_cache, deltify.meta,
1919 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1920 nours, repo, progress_cb, progress_arg, rl,
1921 cancel_cb, cancel_arg);
1922 if (err)
1923 goto done;
1924 done:
1925 free_nmeta(deltify.meta, deltify.nmeta);
1926 free_nmeta(reuse.meta, reuse.nmeta);
1927 got_object_idset_free(idset);
1928 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1929 err = got_error_from_errno("close");
1930 return err;