free gotwebd servers in gotwebd_shutdown()
[got-portable.git] / lib / pack_create.c
blobfc1ab25021b651e94754143be5b647d2ba14ebbe
1 /*
2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
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.
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.
18 #include "got_compat.h"
20 #include <sys/types.h>
21 #include <sys/queue.h>
22 #include <sys/uio.h>
23 #include <sys/stat.h>
24 #include <sys/time.h>
25 #include <sys/mman.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <inttypes.h>
30 #include <poll.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <time.h>
35 #include <unistd.h>
36 #include <limits.h>
37 #include <zlib.h>
39 #include "got_error.h"
40 #include "got_cancel.h"
41 #include "got_object.h"
42 #include "got_path.h"
43 #include "got_reference.h"
44 #include "got_repository.h"
45 #include "got_repository_admin.h"
47 #include "got_lib_deltify.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_hash.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_idset.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_deflate.h"
54 #include "got_lib_ratelimit.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_pack_create.h"
57 #include "got_lib_repository.h"
58 #include "got_lib_inflate.h"
59 #include "got_lib_poll.h"
61 #include "murmurhash2.h"
63 #ifndef MIN
64 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #endif
67 #ifndef MAX
68 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
69 #endif
71 #ifndef nitems
72 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 #endif
75 static const struct got_error *
76 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
77 const char *path, int obj_type, time_t mtime, uint32_t seed)
79 struct got_pack_meta *m;
81 *new = NULL;
83 m = calloc(1, sizeof(*m));
84 if (m == NULL)
85 return got_error_from_errno("calloc");
87 memcpy(&m->id, id, sizeof(m->id));
89 m->path_hash = murmurhash2(path, strlen(path), seed);
90 m->obj_type = obj_type;
91 m->mtime = mtime;
92 *new = m;
93 return NULL;
96 static void
97 clear_meta(struct got_pack_meta *meta)
99 if (meta == NULL)
100 return;
101 meta->path_hash = 0;
102 free(meta->delta_buf);
103 meta->delta_buf = NULL;
104 free(meta->base_obj_id);
105 meta->base_obj_id = NULL;
106 meta->reused_delta_offset = 0;
107 got_deltify_free(meta->dtab);
108 meta->dtab = NULL;
111 static void
112 free_nmeta(struct got_pack_meta **meta, int nmeta)
114 int i;
116 for (i = 0; i < nmeta; i++)
117 clear_meta(meta[i]);
119 free(meta);
122 static int
123 delta_order_cmp(const void *pa, const void *pb)
125 struct got_pack_meta *a, *b;
127 a = *(struct got_pack_meta **)pa;
128 b = *(struct got_pack_meta **)pb;
130 if (a->obj_type != b->obj_type)
131 return a->obj_type - b->obj_type;
132 if (a->path_hash < b->path_hash)
133 return -1;
134 if (a->path_hash > b->path_hash)
135 return 1;
136 if (a->mtime < b->mtime)
137 return -1;
138 if (a->mtime > b->mtime)
139 return 1;
140 return got_object_id_cmp(&a->id, &b->id);
143 static off_t
144 delta_size(struct got_delta_instruction *deltas, int ndeltas)
146 int i;
147 off_t size = 32;
148 for (i = 0; i < ndeltas; i++) {
149 if (deltas[i].copy)
150 size += GOT_DELTA_SIZE_SHIFT;
151 else
152 size += deltas[i].len + 1;
154 return size;
157 static const struct got_error *
158 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
160 char *n;
162 if (*len + nseg >= *sz) {
163 while (*len + nseg >= *sz)
164 *sz += *sz / 2;
165 n = realloc(*p, *sz);
166 if (n == NULL)
167 return got_error_from_errno("realloc");
168 *p = n;
170 memcpy(*p + *len, seg, nseg);
171 *len += nseg;
172 return NULL;
175 static const struct got_error *
176 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
177 struct got_delta_instruction *deltas, int ndeltas,
178 off_t delta_size, off_t base_size)
180 const struct got_error *err;
181 unsigned char buf[16], *bp;
182 int i, j;
183 size_t len = 0, compressed_len;
184 off_t bufsize = delta_size;
185 off_t n;
186 struct got_delta_instruction *d;
187 uint8_t *delta_buf;
189 delta_buf = malloc(bufsize);
190 if (delta_buf == NULL)
191 return got_error_from_errno("malloc");
193 /* base object size */
194 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
195 n = base_size >> GOT_DELTA_SIZE_SHIFT;
196 for (i = 1; n > 0; i++) {
197 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
198 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
199 n >>= GOT_DELTA_SIZE_SHIFT;
201 err = append(&delta_buf, &len, &bufsize, buf, i);
202 if (err)
203 goto done;
205 /* target object size */
206 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
207 n = o->size >> GOT_DELTA_SIZE_SHIFT;
208 for (i = 1; n > 0; i++) {
209 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
210 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
211 n >>= GOT_DELTA_SIZE_SHIFT;
213 err = append(&delta_buf, &len, &bufsize, buf, i);
214 if (err)
215 goto done;
217 for (j = 0; j < ndeltas; j++) {
218 d = &deltas[j];
219 if (d->copy) {
220 n = d->offset;
221 bp = &buf[1];
222 buf[0] = GOT_DELTA_BASE_COPY;
223 for (i = 0; i < 4; i++) {
224 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
225 buf[0] |= 1 << i;
226 *bp++ = n & 0xff;
227 n >>= 8;
228 if (n == 0)
229 break;
232 n = d->len;
233 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
234 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
235 for (i = 0; i < 3 && n > 0; i++) {
236 buf[0] |= 1 << (i + 4);
237 *bp++ = n & 0xff;
238 n >>= 8;
241 err = append(&delta_buf, &len, &bufsize,
242 buf, bp - buf);
243 if (err)
244 goto done;
245 } else if (o->f == NULL) {
246 n = 0;
247 while (n != d->len) {
248 buf[0] = (d->len - n < 127) ? d->len - n : 127;
249 err = append(&delta_buf, &len, &bufsize,
250 buf, 1);
251 if (err)
252 goto done;
253 err = append(&delta_buf, &len, &bufsize,
254 o->data + o->hdrlen + d->offset + n,
255 buf[0]);
256 if (err)
257 goto done;
258 n += buf[0];
260 } else {
261 char content[128];
262 size_t r;
263 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
264 err = got_error_from_errno("fseeko");
265 goto done;
267 n = 0;
268 while (n != d->len) {
269 buf[0] = (d->len - n < 127) ? d->len - n : 127;
270 err = append(&delta_buf, &len, &bufsize,
271 buf, 1);
272 if (err)
273 goto done;
274 r = fread(content, 1, buf[0], o->f);
275 if (r != buf[0]) {
276 err = got_ferror(o->f, GOT_ERR_IO);
277 goto done;
279 err = append(&delta_buf, &len, &bufsize,
280 content, buf[0]);
281 if (err)
282 goto done;
283 n += buf[0];
288 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
289 NULL, NULL, delta_buf, 0, len);
290 if (err)
291 goto done;
293 m->delta_len = len;
294 m->delta_compressed_len = compressed_len;
295 done:
296 free(delta_buf);
297 return err;
300 static const struct got_error *
301 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
302 struct got_delta_instruction *deltas, int ndeltas,
303 off_t base_size, FILE *f)
305 const struct got_error *err;
306 unsigned char buf[16], *bp;
307 int i, j;
308 off_t n;
309 struct got_deflate_buf zb;
310 struct got_delta_instruction *d;
311 off_t delta_len = 0, compressed_len = 0;
313 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
314 if (err)
315 return err;
317 /* base object size */
318 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
319 n = base_size >> GOT_DELTA_SIZE_SHIFT;
320 for (i = 1; n > 0; i++) {
321 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
322 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
323 n >>= GOT_DELTA_SIZE_SHIFT;
326 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
327 buf, 0, i, f, NULL);
328 if (err)
329 goto done;
330 delta_len += i;
332 /* target object size */
333 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
334 n = o->size >> GOT_DELTA_SIZE_SHIFT;
335 for (i = 1; n > 0; i++) {
336 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
337 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
338 n >>= GOT_DELTA_SIZE_SHIFT;
341 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
342 buf, 0, i, f, NULL);
343 if (err)
344 goto done;
345 delta_len += i;
347 for (j = 0; j < ndeltas; j++) {
348 d = &deltas[j];
349 if (d->copy) {
350 n = d->offset;
351 bp = &buf[1];
352 buf[0] = GOT_DELTA_BASE_COPY;
353 for (i = 0; i < 4; i++) {
354 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
355 buf[0] |= 1 << i;
356 *bp++ = n & 0xff;
357 n >>= 8;
358 if (n == 0)
359 break;
361 n = d->len;
362 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
363 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
364 for (i = 0; i < 3 && n > 0; i++) {
365 buf[0] |= 1 << (i + 4);
366 *bp++ = n & 0xff;
367 n >>= 8;
370 err = got_deflate_append_to_file_mmap(&zb,
371 &compressed_len, buf, 0, bp - buf, f, NULL);
372 if (err)
373 goto done;
374 delta_len += (bp - buf);
375 } else if (o->f == NULL) {
376 n = 0;
377 while (n != d->len) {
378 buf[0] = (d->len - n < 127) ? d->len - n : 127;
379 err = got_deflate_append_to_file_mmap(&zb,
380 &compressed_len, buf, 0, 1, f, NULL);
381 if (err)
382 goto done;
383 delta_len++;
384 err = got_deflate_append_to_file_mmap(&zb,
385 &compressed_len,
386 o->data + o->hdrlen + d->offset + n, 0,
387 buf[0], f, NULL);
388 if (err)
389 goto done;
390 delta_len += buf[0];
391 n += buf[0];
393 } else {
394 char content[128];
395 size_t r;
396 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
397 err = got_error_from_errno("fseeko");
398 goto done;
400 n = 0;
401 while (n != d->len) {
402 buf[0] = (d->len - n < 127) ? d->len - n : 127;
403 err = got_deflate_append_to_file_mmap(&zb,
404 &compressed_len, buf, 0, 1, f, NULL);
405 if (err)
406 goto done;
407 delta_len++;
408 r = fread(content, 1, buf[0], o->f);
409 if (r != buf[0]) {
410 err = got_ferror(o->f, GOT_ERR_IO);
411 goto done;
413 err = got_deflate_append_to_file_mmap(&zb,
414 &compressed_len, content, 0, buf[0], f,
415 NULL);
416 if (err)
417 goto done;
418 delta_len += buf[0];
419 n += buf[0];
424 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
425 if (err)
426 goto done;
428 /* sanity check */
429 if (compressed_len != ftello(f) - m->delta_offset) {
430 err = got_error(GOT_ERR_COMPRESSION);
431 goto done;
434 m->delta_len = delta_len;
435 m->delta_compressed_len = compressed_len;
436 done:
437 got_deflate_end(&zb);
438 return err;
441 const struct got_error *
442 got_pack_report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
443 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
444 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
445 int nobj_written)
447 const struct got_error *err;
448 int elapsed;
450 if (progress_cb == NULL)
451 return NULL;
453 err = got_ratelimit_check(&elapsed, rl);
454 if (err || !elapsed)
455 return err;
457 return progress_cb(progress_arg, ncolored, nfound, ntrees,
458 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
461 const struct got_error *
462 got_pack_add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
464 if (v->nmeta == v->metasz){
465 size_t newsize = 2 * v->metasz;
466 struct got_pack_meta **new;
467 new = reallocarray(v->meta, newsize, sizeof(*new));
468 if (new == NULL)
469 return got_error_from_errno("reallocarray");
470 v->meta = new;
471 v->metasz = newsize;
474 v->meta[v->nmeta++] = m;
475 return NULL;
478 const struct got_error *
479 got_pack_find_pack_for_reuse(struct got_packidx **best_packidx,
480 struct got_repository *repo)
482 const struct got_error *err = NULL;
483 struct got_pathlist_entry *pe;
484 const char *best_packidx_path = NULL;
485 int nobj_max = 0;
487 *best_packidx = NULL;
489 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
490 const char *path_packidx = pe->path;
491 struct got_packidx *packidx;
492 int nobj;
494 err = got_repo_get_packidx(&packidx, path_packidx, repo);
495 if (err)
496 break;
498 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
499 if (nobj > nobj_max) {
500 best_packidx_path = path_packidx;
501 nobj_max = nobj;
505 if (best_packidx_path) {
506 err = got_repo_get_packidx(best_packidx, best_packidx_path,
507 repo);
510 return err;
513 const struct got_error *
514 got_pack_cache_pack_for_packidx(struct got_pack **pack,
515 struct got_packidx *packidx, struct got_repository *repo)
517 const struct got_error *err;
518 char *path_packfile = NULL;
520 err = got_packidx_get_packfile_path(&path_packfile,
521 packidx->path_packidx);
522 if (err)
523 return err;
525 *pack = got_repo_get_cached_pack(repo, path_packfile);
526 if (*pack == NULL) {
527 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
528 if (err)
529 goto done;
531 done:
532 free(path_packfile);
533 return err;
536 static const struct got_error *
537 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
538 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
539 struct got_repository *repo,
540 got_pack_progress_cb progress_cb, void *progress_arg,
541 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
543 const struct got_error *err = NULL;
544 struct got_pack_meta *m = NULL, *base = NULL;
545 struct got_raw_object *raw = NULL, *base_raw = NULL;
546 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
547 int i, j, ndeltas, best_ndeltas;
548 off_t size, best_size;
549 const int max_base_candidates = 3;
550 size_t delta_memsize = 0;
551 const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
552 int outfd = -1;
553 uint32_t delta_seed;
555 delta_seed = arc4random();
557 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
558 for (i = 0; i < nmeta; i++) {
559 if (cancel_cb) {
560 err = (*cancel_cb)(cancel_arg);
561 if (err)
562 break;
564 err = got_pack_report_progress(progress_cb, progress_arg, rl,
565 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
566 nreused + i, 0);
567 if (err)
568 goto done;
569 m = meta[i];
571 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
572 m->obj_type == GOT_OBJ_TYPE_TAG)
573 continue;
575 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
576 if (err)
577 goto done;
578 m->size = raw->size;
580 if (raw->f == NULL) {
581 err = got_deltify_init_mem(&m->dtab, raw->data,
582 raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
583 } else {
584 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
585 raw->size + raw->hdrlen, delta_seed);
587 if (err)
588 goto done;
590 if (i > max_base_candidates) {
591 struct got_pack_meta *n = NULL;
592 n = meta[i - (max_base_candidates + 1)];
593 got_deltify_free(n->dtab);
594 n->dtab = NULL;
597 best_size = raw->size;
598 best_ndeltas = 0;
599 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
600 if (cancel_cb) {
601 err = (*cancel_cb)(cancel_arg);
602 if (err)
603 goto done;
605 base = meta[j];
606 /* long chains make unpacking slow, avoid such bases */
607 if (base->nchain >= 128 ||
608 base->obj_type != m->obj_type)
609 continue;
611 err = got_object_raw_open(&base_raw, &outfd, repo,
612 &base->id);
613 if (err)
614 goto done;
616 if (raw->f == NULL && base_raw->f == NULL) {
617 err = got_deltify_mem_mem(&deltas, &ndeltas,
618 raw->data, raw->hdrlen,
619 raw->size + raw->hdrlen, delta_seed,
620 base->dtab, base_raw->data,
621 base_raw->hdrlen,
622 base_raw->size + base_raw->hdrlen);
623 } else if (raw->f == NULL) {
624 err = got_deltify_mem_file(&deltas, &ndeltas,
625 raw->data, raw->hdrlen,
626 raw->size + raw->hdrlen, delta_seed,
627 base->dtab, base_raw->f,
628 base_raw->hdrlen,
629 base_raw->size + base_raw->hdrlen);
630 } else if (base_raw->f == NULL) {
631 err = got_deltify_file_mem(&deltas, &ndeltas,
632 raw->f, raw->hdrlen,
633 raw->size + raw->hdrlen, delta_seed,
634 base->dtab, base_raw->data,
635 base_raw->hdrlen,
636 base_raw->size + base_raw->hdrlen);
637 } else {
638 err = got_deltify(&deltas, &ndeltas,
639 raw->f, raw->hdrlen,
640 raw->size + raw->hdrlen, delta_seed,
641 base->dtab, base_raw->f, base_raw->hdrlen,
642 base_raw->size + base_raw->hdrlen);
644 got_object_raw_close(base_raw);
645 base_raw = NULL;
646 if (err)
647 goto done;
649 size = delta_size(deltas, ndeltas);
650 if (size + 32 < best_size){
652 * if we already picked a best delta,
653 * replace it.
655 best_size = size;
656 free(best_deltas);
657 best_deltas = deltas;
658 best_ndeltas = ndeltas;
659 deltas = NULL;
660 m->nchain = base->nchain + 1;
661 m->prev = base;
662 m->head = base->head;
663 if (m->head == NULL)
664 m->head = base;
665 } else {
666 free(deltas);
667 deltas = NULL;
668 ndeltas = 0;
672 if (best_ndeltas > 0) {
673 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
674 delta_memsize + best_size <= max_delta_memsize) {
675 delta_memsize += best_size;
676 err = encode_delta_in_mem(m, raw, best_deltas,
677 best_ndeltas, best_size, m->prev->size);
678 } else {
679 m->delta_offset = ftello(delta_cache);
680 err = encode_delta(m, raw, best_deltas,
681 best_ndeltas, m->prev->size, delta_cache);
683 free(best_deltas);
684 best_deltas = NULL;
685 best_ndeltas = 0;
686 if (err)
687 goto done;
690 got_object_raw_close(raw);
691 raw = NULL;
693 done:
694 if (raw)
695 got_object_raw_close(raw);
696 if (base_raw)
697 got_object_raw_close(base_raw);
698 if (outfd != -1 && close(outfd) == -1 && err == NULL)
699 err = got_error_from_errno("close");
700 free(deltas);
701 free(best_deltas);
702 return err;
705 static const struct got_error *
706 search_packidx(int *found, struct got_object_id *id,
707 struct got_repository *repo)
709 const struct got_error *err = NULL;
710 struct got_packidx *packidx = NULL;
711 int idx;
713 *found = 0;
715 err = got_repo_search_packidx(&packidx, &idx, repo, id);
716 if (err == NULL)
717 *found = 1; /* object is already packed */
718 else if (err->code == GOT_ERR_NO_OBJ)
719 err = NULL;
720 return err;
723 const struct got_error *
724 got_pack_add_object(int want_meta, struct got_object_idset *idset,
725 struct got_object_id *id, const char *path, int obj_type,
726 time_t mtime, uint32_t seed, int loose_obj_only,
727 struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
728 got_pack_progress_cb progress_cb, void *progress_arg,
729 struct got_ratelimit *rl)
731 const struct got_error *err;
732 struct got_pack_meta *m = NULL;
734 if (loose_obj_only) {
735 int is_packed;
736 err = search_packidx(&is_packed, id, repo);
737 if (err)
738 return err;
739 if (is_packed && want_meta)
740 return NULL;
743 if (want_meta) {
744 err = alloc_meta(&m, id, path, obj_type, mtime, seed);
745 if (err)
746 return err;
748 (*nfound)++;
749 err = got_pack_report_progress(progress_cb, progress_arg, rl,
750 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
751 if (err) {
752 clear_meta(m);
753 free(m);
754 return err;
758 err = got_object_idset_add(idset, id, m);
759 if (err) {
760 clear_meta(m);
761 free(m);
763 return err;
766 const struct got_error *
767 got_pack_load_tree_entries(struct got_object_id_queue *ids, int want_meta,
768 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
769 struct got_tree_object *tree,
770 const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
771 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
772 got_pack_progress_cb progress_cb, void *progress_arg,
773 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
775 const struct got_error *err;
776 char *p = NULL;
777 int i;
779 (*ntrees)++;
780 err = got_pack_report_progress(progress_cb, progress_arg, rl,
781 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
782 if (err)
783 return err;
785 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
786 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
787 struct got_object_id *id = got_tree_entry_get_id(e);
788 mode_t mode = got_tree_entry_get_mode(e);
790 if (cancel_cb) {
791 err = (*cancel_cb)(cancel_arg);
792 if (err)
793 break;
796 if (got_object_tree_entry_is_submodule(e) ||
797 got_object_idset_contains(idset, id) ||
798 got_object_idset_contains(idset_exclude, id))
799 continue;
802 * If got-read-pack is crawling trees for us then
803 * we are only here to collect blob IDs.
805 if (ids == NULL && S_ISDIR(mode))
806 continue;
808 if (asprintf(&p, "%s%s%s", dpath,
809 got_path_is_root_dir(dpath) ? "" : "/",
810 got_tree_entry_get_name(e)) == -1) {
811 err = got_error_from_errno("asprintf");
812 break;
815 if (S_ISDIR(mode)) {
816 struct got_object_qid *qid;
817 err = got_object_qid_alloc(&qid, id);
818 if (err)
819 break;
820 qid->data = p;
821 p = NULL;
822 STAILQ_INSERT_TAIL(ids, qid, entry);
823 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
824 err = got_pack_add_object(want_meta,
825 want_meta ? idset : idset_exclude, id, p,
826 GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
827 repo, ncolored, nfound, ntrees,
828 progress_cb, progress_arg, rl);
829 if (err)
830 break;
831 free(p);
832 p = NULL;
833 } else {
834 free(p);
835 p = NULL;
839 free(p);
840 return err;
843 const struct got_error *
844 got_pack_load_tree(int want_meta, struct got_object_idset *idset,
845 struct got_object_idset *idset_exclude,
846 struct got_object_id *tree_id, const char *dpath, time_t mtime,
847 uint32_t seed, struct got_repository *repo, int loose_obj_only,
848 int *ncolored, int *nfound, int *ntrees,
849 got_pack_progress_cb progress_cb, void *progress_arg,
850 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
852 const struct got_error *err = NULL;
853 struct got_object_id_queue tree_ids;
854 struct got_object_qid *qid;
855 struct got_tree_object *tree = NULL;
857 if (got_object_idset_contains(idset, tree_id) ||
858 got_object_idset_contains(idset_exclude, tree_id))
859 return NULL;
861 err = got_object_qid_alloc(&qid, tree_id);
862 if (err)
863 return err;
864 qid->data = strdup(dpath);
865 if (qid->data == NULL) {
866 err = got_error_from_errno("strdup");
867 got_object_qid_free(qid);
868 return err;
871 STAILQ_INIT(&tree_ids);
872 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
874 while (!STAILQ_EMPTY(&tree_ids)) {
875 const char *path;
876 if (cancel_cb) {
877 err = (*cancel_cb)(cancel_arg);
878 if (err)
879 break;
882 qid = STAILQ_FIRST(&tree_ids);
883 STAILQ_REMOVE_HEAD(&tree_ids, entry);
884 path = qid->data;
886 if (got_object_idset_contains(idset, &qid->id) ||
887 got_object_idset_contains(idset_exclude, &qid->id)) {
888 free(qid->data);
889 got_object_qid_free(qid);
890 continue;
893 err = got_pack_add_object(want_meta,
894 want_meta ? idset : idset_exclude,
895 &qid->id, path, GOT_OBJ_TYPE_TREE,
896 mtime, seed, loose_obj_only, repo,
897 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
898 if (err) {
899 free(qid->data);
900 got_object_qid_free(qid);
901 break;
904 err = got_object_open_as_tree(&tree, repo, &qid->id);
905 if (err) {
906 free(qid->data);
907 got_object_qid_free(qid);
908 break;
911 err = got_pack_load_tree_entries(&tree_ids, want_meta, idset,
912 idset_exclude, tree, path, mtime, seed, repo,
913 loose_obj_only, ncolored, nfound, ntrees,
914 progress_cb, progress_arg, rl,
915 cancel_cb, cancel_arg);
916 free(qid->data);
917 got_object_qid_free(qid);
918 if (err)
919 break;
921 got_object_tree_close(tree);
922 tree = NULL;
925 STAILQ_FOREACH(qid, &tree_ids, entry)
926 free(qid->data);
927 got_object_id_queue_free(&tree_ids);
928 if (tree)
929 got_object_tree_close(tree);
930 return err;
933 static const struct got_error *
934 load_commit(int want_meta, struct got_object_idset *idset,
935 struct got_object_idset *idset_exclude,
936 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
937 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
938 got_pack_progress_cb progress_cb, void *progress_arg,
939 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
941 const struct got_error *err;
942 struct got_commit_object *commit;
944 if (got_object_idset_contains(idset, id) ||
945 got_object_idset_contains(idset_exclude, id))
946 return NULL;
948 if (loose_obj_only) {
949 int is_packed;
950 err = search_packidx(&is_packed, id, repo);
951 if (err)
952 return err;
953 if (is_packed && want_meta)
954 return NULL;
957 err = got_object_open_as_commit(&commit, repo, id);
958 if (err)
959 return err;
961 err = got_pack_add_object(want_meta,
962 want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_COMMIT,
963 got_object_commit_get_committer_time(commit), seed,
964 loose_obj_only, repo,
965 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
966 if (err)
967 goto done;
969 err = got_pack_load_tree(want_meta, idset, idset_exclude,
970 got_object_commit_get_tree_id(commit),
971 "", got_object_commit_get_committer_time(commit), seed,
972 repo, loose_obj_only, ncolored, nfound, ntrees,
973 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
974 done:
975 got_object_commit_close(commit);
976 return err;
979 static const struct got_error *
980 load_tag(int want_meta, struct got_object_idset *idset,
981 struct got_object_idset *idset_exclude,
982 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
983 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
984 got_pack_progress_cb progress_cb, void *progress_arg,
985 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
987 const struct got_error *err;
988 struct got_tag_object *tag = NULL;
990 if (got_object_idset_contains(idset, id) ||
991 got_object_idset_contains(idset_exclude, id))
992 return NULL;
994 if (loose_obj_only) {
995 int is_packed;
996 err = search_packidx(&is_packed, id, repo);
997 if (err)
998 return err;
999 if (is_packed && want_meta)
1000 return NULL;
1003 err = got_object_open_as_tag(&tag, repo, id);
1004 if (err)
1005 return err;
1007 err = got_pack_add_object(want_meta,
1008 want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_TAG,
1009 got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1010 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1011 if (err)
1012 goto done;
1014 switch (got_object_tag_get_object_type(tag)) {
1015 case GOT_OBJ_TYPE_COMMIT:
1016 err = load_commit(want_meta, idset, idset_exclude,
1017 got_object_tag_get_object_id(tag), repo, seed,
1018 loose_obj_only, ncolored, nfound, ntrees,
1019 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1020 break;
1021 case GOT_OBJ_TYPE_TREE:
1022 err = got_pack_load_tree(want_meta, idset, idset_exclude,
1023 got_object_tag_get_object_id(tag), "",
1024 got_object_tag_get_tagger_time(tag), seed, repo,
1025 loose_obj_only, ncolored, nfound, ntrees,
1026 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1027 break;
1028 default:
1029 break;
1032 done:
1033 got_object_tag_close(tag);
1034 return err;
1037 const struct got_error *
1038 got_pack_paint_commit(struct got_object_qid *qid, intptr_t color)
1040 if (color < 0 || color >= COLOR_MAX)
1041 return got_error(GOT_ERR_RANGE);
1043 qid->data = (void *)color;
1044 return NULL;
1047 const struct got_error *
1048 got_pack_queue_commit_id(struct got_object_id_queue *ids,
1049 struct got_object_id *id, intptr_t color, struct got_repository *repo)
1051 const struct got_error *err;
1052 struct got_object_qid *qid;
1054 err = got_object_qid_alloc(&qid, id);
1055 if (err)
1056 return err;
1058 STAILQ_INSERT_TAIL(ids, qid, entry);
1059 return got_pack_paint_commit(qid, color);
1062 struct append_id_arg {
1063 struct got_object_id **array;
1064 int idx;
1065 struct got_object_idset *drop;
1066 struct got_object_idset *skip;
1069 static const struct got_error *
1070 append_id(struct got_object_id *id, void *data, void *arg)
1072 struct append_id_arg *a = arg;
1074 if (got_object_idset_contains(a->skip, id) ||
1075 got_object_idset_contains(a->drop, id))
1076 return NULL;
1078 a->array[++a->idx] = got_object_id_dup(id);
1079 if (a->array[a->idx] == NULL)
1080 return got_error_from_errno("got_object_id_dup");
1082 return NULL;
1085 static const struct got_error *
1086 free_meta(struct got_object_id *id, void *data, void *arg)
1088 struct got_pack_meta *meta = data;
1090 clear_meta(meta);
1091 free(meta);
1092 return NULL;
1095 static const struct got_error *
1096 queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1097 struct got_object_id_queue *ids, struct got_repository *repo)
1099 const struct got_error *err;
1100 struct got_tag_object *tag = NULL;
1101 int obj_type;
1103 err = got_object_get_type(&obj_type, repo, id);
1104 if (err)
1105 return err;
1107 if (obj_type == GOT_OBJ_TYPE_TAG) {
1108 err = got_object_open_as_tag(&tag, repo, id);
1109 if (err)
1110 return err;
1111 obj_type = got_object_tag_get_object_type(tag);
1112 id = got_object_tag_get_object_id(tag);
1115 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1116 err = got_pack_queue_commit_id(ids, id, color, repo);
1117 if (err)
1118 goto done;
1120 done:
1121 if (tag)
1122 got_object_tag_close(tag);
1123 return err;
1126 const struct got_error *
1127 got_pack_find_pack_for_commit_painting(struct got_packidx **best_packidx,
1128 struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1130 const struct got_error *err = NULL;
1131 struct got_pathlist_entry *pe;
1132 const char *best_packidx_path = NULL;
1133 int nobj_max = 0;
1134 int ncommits_max = 0;
1136 *best_packidx = NULL;
1139 * Find the largest pack which contains at least some of the
1140 * commits we are interested in.
1142 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1143 const char *path_packidx = pe->path;
1144 struct got_packidx *packidx;
1145 int nobj, idx, ncommits = 0;
1146 struct got_object_qid *qid;
1148 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1149 if (err)
1150 break;
1152 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1153 if (nobj <= nobj_max)
1154 continue;
1156 STAILQ_FOREACH(qid, ids, entry) {
1157 idx = got_packidx_get_object_idx(packidx, &qid->id);
1158 if (idx != -1)
1159 ncommits++;
1161 if (ncommits > ncommits_max) {
1162 best_packidx_path = path_packidx;
1163 nobj_max = nobj;
1164 ncommits_max = ncommits;
1168 if (best_packidx_path && err == NULL) {
1169 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1170 repo);
1173 return err;
1176 static const struct got_error *
1177 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1178 struct got_object_id **head, int nhead,
1179 struct got_object_id **tail, int ntail,
1180 struct got_repository *repo,
1181 got_pack_progress_cb progress_cb, void *progress_arg,
1182 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1184 const struct got_error *err = NULL;
1185 struct got_object_id_queue ids;
1186 struct got_object_idset *keep, *drop, *skip = NULL;
1187 int i, nkeep;
1189 STAILQ_INIT(&ids);
1190 *res = NULL;
1191 *nres = 0;
1192 *ncolored = 0;
1194 keep = got_object_idset_alloc();
1195 if (keep == NULL)
1196 return got_error_from_errno("got_object_idset_alloc");
1198 drop = got_object_idset_alloc();
1199 if (drop == NULL) {
1200 err = got_error_from_errno("got_object_idset_alloc");
1201 goto done;
1204 skip = got_object_idset_alloc();
1205 if (skip == NULL) {
1206 err = got_error_from_errno("got_object_idset_alloc");
1207 goto done;
1210 for (i = 0; i < nhead; i++) {
1211 struct got_object_id *id = head[i];
1212 if (id == NULL)
1213 continue;
1214 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1215 if (err)
1216 goto done;
1219 for (i = 0; i < ntail; i++) {
1220 struct got_object_id *id = tail[i];
1221 if (id == NULL)
1222 continue;
1223 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1224 if (err)
1225 goto done;
1228 err = got_pack_paint_commits(ncolored, &ids, nhead + ntail,
1229 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1230 cancel_cb, cancel_arg);
1231 if (err)
1232 goto done;
1234 nkeep = got_object_idset_num_elements(keep);
1235 if (nkeep > 0) {
1236 struct append_id_arg arg;
1237 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1238 if (arg.array == NULL) {
1239 err = got_error_from_errno("calloc");
1240 goto done;
1242 arg.idx = -1;
1243 arg.skip = skip;
1244 arg.drop = drop;
1245 err = got_object_idset_for_each(keep, append_id, &arg);
1246 if (err) {
1247 free(arg.array);
1248 goto done;
1250 *res = arg.array;
1251 *nres = arg.idx + 1;
1253 done:
1254 got_object_idset_free(keep);
1255 got_object_idset_free(drop);
1256 if (skip)
1257 got_object_idset_free(skip);
1258 got_object_id_queue_free(&ids);
1259 return err;
1262 static const struct got_error *
1263 find_pack_for_enumeration(struct got_packidx **best_packidx,
1264 struct got_object_id **ids, int nids, struct got_repository *repo)
1266 const struct got_error *err = NULL;
1267 struct got_pathlist_entry *pe;
1268 const char *best_packidx_path = NULL;
1269 int nobj_max = 0;
1270 int ncommits_max = 0;
1272 *best_packidx = NULL;
1275 * Find the largest pack which contains at least some of the
1276 * commits and tags we are interested in.
1278 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1279 const char *path_packidx = pe->path;
1280 struct got_packidx *packidx;
1281 int nobj, i, idx, ncommits = 0;
1283 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1284 if (err)
1285 break;
1287 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1288 if (nobj <= nobj_max)
1289 continue;
1291 for (i = 0; i < nids; i++) {
1292 idx = got_packidx_get_object_idx(packidx, ids[i]);
1293 if (idx != -1)
1294 ncommits++;
1296 if (ncommits > ncommits_max) {
1297 best_packidx_path = path_packidx;
1298 nobj_max = nobj;
1299 ncommits_max = ncommits;
1303 if (best_packidx_path && err == NULL) {
1304 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1305 repo);
1308 return err;
1311 static const struct got_error *
1312 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1313 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1314 struct got_object_id **ours, int nours, struct got_repository *repo,
1315 uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1316 void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1317 void *cancel_arg)
1319 const struct got_error *err = NULL;
1320 struct got_object_id **ids = NULL;
1321 struct got_packidx *packidx = NULL;
1322 int i, nobj = 0, obj_type, found_all_objects = 0;
1323 struct got_object_idset *idset_exclude;
1325 idset_exclude = got_object_idset_alloc();
1326 if (idset_exclude == NULL)
1327 return got_error_from_errno("got_object_idset_alloc");
1329 *ncolored = 0;
1330 *nfound = 0;
1331 *ntrees = 0;
1333 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1334 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1335 if (err)
1336 goto done;
1338 err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
1339 if (err)
1340 goto done;
1341 if (packidx) {
1342 err = got_pack_load_packed_object_ids(&found_all_objects,
1343 theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
1344 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1345 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1346 if (err)
1347 goto done;
1350 for (i = 0; i < ntheirs; i++) {
1351 struct got_object_id *id = theirs[i];
1352 if (id == NULL)
1353 continue;
1354 err = got_object_get_type(&obj_type, repo, id);
1355 if (err)
1356 return err;
1357 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1358 if (!found_all_objects) {
1359 err = load_commit(0, idset, idset_exclude,
1360 id, repo, seed, loose_obj_only,
1361 ncolored, nfound, ntrees,
1362 progress_cb, progress_arg, rl,
1363 cancel_cb, cancel_arg);
1364 if (err)
1365 goto done;
1367 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1368 err = load_tag(0, idset, idset_exclude, id, repo,
1369 seed, loose_obj_only, ncolored, nfound, ntrees,
1370 progress_cb, progress_arg, rl,
1371 cancel_cb, cancel_arg);
1372 if (err)
1373 goto done;
1377 found_all_objects = 0;
1378 err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
1379 if (err)
1380 goto done;
1381 if (packidx) {
1382 err = got_pack_load_packed_object_ids(&found_all_objects, ids,
1383 nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
1384 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1385 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1386 if (err)
1387 goto done;
1390 if (!found_all_objects) {
1391 for (i = 0; i < nobj; i++) {
1392 err = load_commit(1, idset, idset_exclude, ids[i],
1393 repo, seed, loose_obj_only, ncolored, nfound,
1394 ntrees, progress_cb, progress_arg, rl,
1395 cancel_cb, cancel_arg);
1396 if (err)
1397 goto done;
1401 for (i = 0; i < nours; i++) {
1402 struct got_object_id *id = ours[i];
1403 struct got_pack_meta *m;
1404 if (id == NULL)
1405 continue;
1406 m = got_object_idset_get(idset, id);
1407 if (m == NULL) {
1408 err = got_object_get_type(&obj_type, repo, id);
1409 if (err)
1410 goto done;
1411 } else
1412 obj_type = m->obj_type;
1413 if (obj_type != GOT_OBJ_TYPE_TAG)
1414 continue;
1415 err = load_tag(1, idset, idset_exclude, id, repo,
1416 seed, loose_obj_only, ncolored, nfound, ntrees,
1417 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1418 if (err)
1419 goto done;
1421 done:
1422 for (i = 0; i < nobj; i++) {
1423 free(ids[i]);
1425 free(ids);
1426 got_object_idset_free(idset_exclude);
1427 return err;
1430 static const struct got_error *
1431 hwrite(int fd, const void *buf, off_t len, struct got_hash *ctx)
1433 got_hash_update(ctx, buf, len);
1434 return got_poll_write_full(fd, buf, len);
1437 static const struct got_error *
1438 hcopy(FILE *fsrc, int fd_dst, off_t len, struct got_hash *ctx)
1440 const struct got_error *err;
1441 unsigned char buf[65536];
1442 off_t remain = len;
1443 size_t n;
1445 while (remain > 0) {
1446 size_t copylen = MIN(sizeof(buf), remain);
1447 n = fread(buf, 1, copylen, fsrc);
1448 if (n != copylen)
1449 return got_ferror(fsrc, GOT_ERR_IO);
1450 got_hash_update(ctx, buf, copylen);
1451 err = got_poll_write_full(fd_dst, buf, copylen);
1452 if (err)
1453 return err;
1454 remain -= copylen;
1457 return NULL;
1460 static const struct got_error *
1461 hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1462 int fd, off_t len, struct got_hash *ctx)
1464 if (src_offset + len > src_size)
1465 return got_error(GOT_ERR_RANGE);
1467 got_hash_update(ctx, src + src_offset, len);
1468 return got_poll_write_full(fd, src + src_offset, len);
1471 static void
1472 putbe32(char *b, uint32_t n)
1474 b[0] = n >> 24;
1475 b[1] = n >> 16;
1476 b[2] = n >> 8;
1477 b[3] = n >> 0;
1480 static int
1481 write_order_cmp(const void *pa, const void *pb)
1483 struct got_pack_meta *a, *b, *ahd, *bhd;
1485 a = *(struct got_pack_meta **)pa;
1486 b = *(struct got_pack_meta **)pb;
1487 ahd = (a->head == NULL) ? a : a->head;
1488 bhd = (b->head == NULL) ? b : b->head;
1489 if (bhd->mtime < ahd->mtime)
1490 return -1;
1491 if (bhd->mtime > ahd->mtime)
1492 return 1;
1493 if (bhd < ahd)
1494 return -1;
1495 if (bhd > ahd)
1496 return 1;
1497 if (a->nchain != b->nchain)
1498 return a->nchain - b->nchain;
1499 if (a->mtime < b->mtime)
1500 return -1;
1501 if (a->mtime > b->mtime)
1502 return 1;
1503 return got_object_id_cmp(&a->id, &b->id);
1506 static int
1507 reuse_write_order_cmp(const void *pa, const void *pb)
1509 struct got_pack_meta *a, *b;
1511 a = *(struct got_pack_meta **)pa;
1512 b = *(struct got_pack_meta **)pb;
1514 if (a->reused_delta_offset < b->reused_delta_offset)
1515 return -1;
1516 if (a->reused_delta_offset > b->reused_delta_offset)
1517 return 1;
1518 return 0;
1521 static const struct got_error *
1522 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1524 size_t i;
1526 *hdrlen = 0;
1528 hdr[0] = obj_type << 4;
1529 hdr[0] |= len & 0xf;
1530 len >>= 4;
1531 for (i = 1; len != 0; i++){
1532 if (i >= bufsize)
1533 return got_error(GOT_ERR_NO_SPACE);
1534 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1535 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1536 len >>= GOT_DELTA_SIZE_SHIFT;
1539 *hdrlen = i;
1540 return NULL;
1543 static int
1544 packoff(char *hdr, off_t off)
1546 int i, j;
1547 char rbuf[8];
1549 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1550 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1551 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1552 GOT_DELTA_SIZE_MORE;
1555 j = 0;
1556 while (i > 0)
1557 hdr[j++] = rbuf[--i];
1558 return j;
1561 static const struct got_error *
1562 deltahdr(off_t *packfile_size, struct got_hash *ctx, int packfd,
1563 int force_refdelta, struct got_pack_meta *m)
1565 const struct got_error *err;
1566 char buf[32];
1567 int nh;
1568 size_t digest_len = got_hash_digest_length(m->prev->id.algo);
1570 if (m->prev->off != 0 && !force_refdelta) {
1571 err = packhdr(&nh, buf, sizeof(buf),
1572 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1573 if (err)
1574 return err;
1575 nh += packoff(buf + nh, m->off - m->prev->off);
1576 err = hwrite(packfd, buf, nh, ctx);
1577 if (err)
1578 return err;
1579 *packfile_size += nh;
1580 } else {
1581 err = packhdr(&nh, buf, sizeof(buf),
1582 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1583 if (err)
1584 return err;
1585 err = hwrite(packfd, buf, nh, ctx);
1586 if (err)
1587 return err;
1588 *packfile_size += nh;
1589 err = hwrite(packfd, m->prev->id.hash, digest_len, ctx);
1590 if (err)
1591 return err;
1592 *packfile_size += digest_len;
1595 return NULL;
1598 static const struct got_error *
1599 write_packed_object(off_t *packfile_size, int packfd,
1600 FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1601 struct got_pack_meta *m, int *outfd, struct got_hash *ctx,
1602 struct got_repository *repo, int force_refdelta)
1604 const struct got_error *err = NULL;
1605 struct got_deflate_checksum csum;
1606 char buf[32];
1607 int nh;
1608 struct got_raw_object *raw = NULL;
1609 off_t outlen, delta_offset;
1611 memset(&csum, 0, sizeof(csum));
1612 csum.output_ctx = ctx;
1614 if (m->reused_delta_offset)
1615 delta_offset = m->reused_delta_offset;
1616 else
1617 delta_offset = m->delta_offset;
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, force_refdelta, 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, force_refdelta, m);
1665 if (err)
1666 goto done;
1667 err = hcopy_mmap(delta_cache_map, 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, delta_offset, SEEK_SET) == -1) {
1675 err = got_error_from_errno("fseeko");
1676 goto done;
1678 err = deltahdr(packfile_size, ctx, packfd, force_refdelta, m);
1679 if (err)
1680 goto done;
1681 err = hcopy(delta_cache, packfd,
1682 m->delta_compressed_len, ctx);
1683 if (err)
1684 goto done;
1685 *packfile_size += m->delta_compressed_len;
1687 done:
1688 if (raw)
1689 got_object_raw_close(raw);
1690 return err;
1693 static const struct got_error *
1694 genpack(struct got_object_id *pack_hash, int packfd,
1695 struct got_pack *reuse_pack, 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, int force_refdelta,
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 struct got_hash ctx;
1707 struct got_pack_meta *m;
1708 char buf[32];
1709 off_t packfile_size = 0;
1710 int outfd = -1;
1711 int delta_cache_fd = -1;
1712 uint8_t *delta_cache_map = NULL;
1713 size_t delta_cache_size = 0;
1714 FILE *packfile = NULL;
1715 enum got_hash_algorithm algo;
1716 size_t digest_len;
1718 algo = got_repo_get_object_format(repo);
1719 digest_len = got_hash_digest_length(algo);
1720 got_hash_init(&ctx, algo);
1722 memset(pack_hash, 0, sizeof(*pack_hash));
1723 pack_hash->algo = algo;
1725 #ifndef GOT_PACK_NO_MMAP
1726 delta_cache_fd = dup(fileno(delta_cache));
1727 if (delta_cache_fd != -1) {
1728 struct stat sb;
1729 if (fstat(delta_cache_fd, &sb) == -1) {
1730 err = got_error_from_errno("fstat");
1731 goto done;
1733 if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1734 delta_cache_map = mmap(NULL, sb.st_size,
1735 PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1736 if (delta_cache_map == MAP_FAILED) {
1737 if (errno != ENOMEM) {
1738 err = got_error_from_errno("mmap");
1739 goto done;
1741 delta_cache_map = NULL; /* fallback on stdio */
1742 } else
1743 delta_cache_size = (size_t)sb.st_size;
1746 #endif
1747 err = hwrite(packfd, "PACK", 4, &ctx);
1748 if (err)
1749 goto done;
1750 putbe32(buf, GOT_PACKFILE_VERSION);
1751 err = hwrite(packfd, buf, 4, &ctx);
1752 if (err)
1753 goto done;
1754 putbe32(buf, ndeltify + nreuse);
1755 err = hwrite(packfd, buf, 4, &ctx);
1756 if (err)
1757 goto done;
1759 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1760 write_order_cmp);
1761 for (i = 0; i < ndeltify; i++) {
1762 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1763 ncolored, nfound, ntrees, packfile_size, nours,
1764 ndeltify + nreuse, ndeltify + nreuse, i);
1765 if (err)
1766 goto done;
1767 m = deltify[i];
1768 err = write_packed_object(&packfile_size, packfd,
1769 delta_cache, delta_cache_map, delta_cache_size,
1770 m, &outfd, &ctx, repo, force_refdelta);
1771 if (err)
1772 goto done;
1775 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1776 reuse_write_order_cmp);
1777 if (nreuse > 0 && reuse_pack->map == NULL) {
1778 int fd = dup(reuse_pack->fd);
1779 if (fd == -1) {
1780 err = got_error_from_errno("dup");
1781 goto done;
1783 packfile = fdopen(fd, "r");
1784 if (packfile == NULL) {
1785 err = got_error_from_errno("fdopen");
1786 close(fd);
1787 goto done;
1790 for (i = 0; i < nreuse; i++) {
1791 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1792 ncolored, nfound, ntrees, packfile_size, nours,
1793 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1794 if (err)
1795 goto done;
1796 m = reuse[i];
1797 err = write_packed_object(&packfile_size, packfd,
1798 packfile, reuse_pack->map, reuse_pack->filesize,
1799 m, &outfd, &ctx, repo, force_refdelta);
1800 if (err)
1801 goto done;
1804 got_hash_final_object_id(&ctx, pack_hash);
1805 err = got_poll_write_full(packfd, pack_hash->hash, digest_len);
1806 if (err)
1807 goto done;
1808 packfile_size += digest_len;
1809 packfile_size += sizeof(struct got_packfile_hdr);
1810 if (progress_cb) {
1811 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1812 packfile_size, nours, ndeltify + nreuse,
1813 ndeltify + nreuse, ndeltify + nreuse);
1814 if (err)
1815 goto done;
1817 done:
1818 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1819 err = got_error_from_errno("close");
1820 if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1821 err = got_error_from_errno("munmap");
1822 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1823 err = got_error_from_errno("close");
1824 if (packfile && fclose(packfile) == EOF && err == NULL)
1825 err = got_error_from_errno("fclose");
1826 return err;
1829 static const struct got_error *
1830 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1832 struct got_pack_meta *m = data;
1833 struct got_pack_metavec *v = arg;
1835 if (m->reused_delta_offset != 0)
1836 return NULL;
1838 return got_pack_add_meta(m, v);
1841 const struct got_error *
1842 got_pack_create(struct got_object_id *packhash, int packfd, FILE *delta_cache,
1843 struct got_object_id **theirs, int ntheirs,
1844 struct got_object_id **ours, int nours,
1845 struct got_repository *repo, int loose_obj_only, int allow_empty,
1846 int force_refdelta, got_pack_progress_cb progress_cb, void *progress_arg,
1847 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1849 const struct got_error *err;
1850 struct got_object_idset *idset;
1851 struct got_packidx *reuse_packidx = NULL;
1852 struct got_pack *reuse_pack = NULL;
1853 struct got_pack_metavec deltify, reuse;
1854 int ncolored = 0, nfound = 0, ntrees = 0;
1855 size_t ndeltify;
1856 uint32_t seed;
1858 seed = arc4random();
1860 memset(&deltify, 0, sizeof(deltify));
1861 memset(&reuse, 0, sizeof(reuse));
1863 idset = got_object_idset_alloc();
1864 if (idset == NULL)
1865 return got_error_from_errno("got_object_idset_alloc");
1867 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1868 ntheirs, ours, nours, repo, seed, loose_obj_only,
1869 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1870 if (err)
1871 goto done;
1873 if (progress_cb) {
1874 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1875 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1876 if (err)
1877 goto done;
1880 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1881 err = got_error(GOT_ERR_CANNOT_PACK);
1882 goto done;
1885 reuse.metasz = 64;
1886 reuse.meta = calloc(reuse.metasz,
1887 sizeof(struct got_pack_meta *));
1888 if (reuse.meta == NULL) {
1889 err = got_error_from_errno("calloc");
1890 goto done;
1893 err = got_pack_search_deltas(&reuse_packidx, &reuse_pack,
1894 &reuse, idset, ncolored, nfound, ntrees, nours,
1895 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1896 if (err)
1897 goto done;
1899 if (reuse_packidx && reuse_pack) {
1900 err = got_repo_pin_pack(repo, reuse_packidx, reuse_pack);
1901 if (err)
1902 goto done;
1905 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1906 err = got_error_from_errno("fseeko");
1907 goto done;
1910 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1911 if (ndeltify > 0) {
1912 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1913 if (deltify.meta == NULL) {
1914 err = got_error_from_errno("calloc");
1915 goto done;
1917 deltify.metasz = ndeltify;
1919 err = got_object_idset_for_each(idset, add_meta_idset_cb,
1920 &deltify);
1921 if (err)
1922 goto done;
1923 if (deltify.nmeta > 0) {
1924 err = pick_deltas(deltify.meta, deltify.nmeta,
1925 ncolored, nfound, ntrees, nours, reuse.nmeta,
1926 delta_cache, repo, progress_cb, progress_arg, rl,
1927 cancel_cb, cancel_arg);
1928 if (err)
1929 goto done;
1933 if (fflush(delta_cache) == EOF) {
1934 err = got_error_from_errno("fflush");
1935 goto done;
1938 if (progress_cb) {
1940 * Report a 1-byte packfile write to indicate we are about
1941 * to start sending packfile data. gotd(8) needs this.
1943 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1944 1 /* packfile_size */, nours,
1945 got_object_idset_num_elements(idset),
1946 deltify.nmeta + reuse.nmeta, 0);
1947 if (err)
1948 goto done;
1951 /* Pinned pack may have moved to different cache slot. */
1952 reuse_pack = got_repo_get_pinned_pack(repo);
1954 err = genpack(packhash, packfd, reuse_pack, delta_cache, deltify.meta,
1955 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1956 nours, repo, force_refdelta, progress_cb, progress_arg, rl,
1957 cancel_cb, cancel_arg);
1958 if (err)
1959 goto done;
1960 done:
1961 free_nmeta(deltify.meta, deltify.nmeta);
1962 free_nmeta(reuse.meta, reuse.nmeta);
1963 got_object_idset_for_each(idset, free_meta, NULL);
1964 got_object_idset_free(idset);
1965 got_repo_unpin_pack(repo);
1966 return err;