portable: remove sockaddr-linux code
[got-portable.git] / lib / pack_create.c
blob5bb15337dc19cabc0e5b03479522f28b680ceb62
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_admin.h"
46 #include "got_lib_deltify.h"
47 #include "got_lib_delta.h"
48 #include "got_lib_hash.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"
58 #include "got_lib_poll.h"
60 #include "murmurhash2.h"
62 #ifndef MIN
63 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 #endif
66 #ifndef MAX
67 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 #endif
70 #ifndef nitems
71 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 #endif
74 static const struct got_error *
75 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
76 const char *path, int obj_type, time_t mtime, uint32_t seed)
78 struct got_pack_meta *m;
80 *new = NULL;
82 m = calloc(1, sizeof(*m));
83 if (m == NULL)
84 return got_error_from_errno("calloc");
86 memcpy(&m->id, id, sizeof(m->id));
88 m->path_hash = murmurhash2(path, strlen(path), seed);
89 m->obj_type = obj_type;
90 m->mtime = mtime;
91 *new = m;
92 return NULL;
95 static void
96 clear_meta(struct got_pack_meta *meta)
98 if (meta == NULL)
99 return;
100 meta->path_hash = 0;
101 free(meta->delta_buf);
102 meta->delta_buf = NULL;
103 free(meta->base_obj_id);
104 meta->base_obj_id = NULL;
105 meta->reused_delta_offset = 0;
108 static void
109 free_nmeta(struct got_pack_meta **meta, int nmeta)
111 int i;
113 for (i = 0; i < nmeta; i++)
114 clear_meta(meta[i]);
115 free(meta);
118 static int
119 delta_order_cmp(const void *pa, const void *pb)
121 struct got_pack_meta *a, *b;
123 a = *(struct got_pack_meta **)pa;
124 b = *(struct got_pack_meta **)pb;
126 if (a->obj_type != b->obj_type)
127 return a->obj_type - b->obj_type;
128 if (a->path_hash < b->path_hash)
129 return -1;
130 if (a->path_hash > b->path_hash)
131 return 1;
132 if (a->mtime < b->mtime)
133 return -1;
134 if (a->mtime > b->mtime)
135 return 1;
136 return got_object_id_cmp(&a->id, &b->id);
139 static off_t
140 delta_size(struct got_delta_instruction *deltas, int ndeltas)
142 int i;
143 off_t size = 32;
144 for (i = 0; i < ndeltas; i++) {
145 if (deltas[i].copy)
146 size += GOT_DELTA_SIZE_SHIFT;
147 else
148 size += deltas[i].len + 1;
150 return size;
153 static const struct got_error *
154 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
156 char *n;
158 if (*len + nseg >= *sz) {
159 while (*len + nseg >= *sz)
160 *sz += *sz / 2;
161 n = realloc(*p, *sz);
162 if (n == NULL)
163 return got_error_from_errno("realloc");
164 *p = n;
166 memcpy(*p + *len, seg, nseg);
167 *len += nseg;
168 return NULL;
171 static const struct got_error *
172 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
173 struct got_delta_instruction *deltas, int ndeltas,
174 off_t delta_size, off_t base_size)
176 const struct got_error *err;
177 unsigned char buf[16], *bp;
178 int i, j;
179 size_t len = 0, compressed_len;
180 off_t bufsize = delta_size;
181 off_t n;
182 struct got_delta_instruction *d;
183 uint8_t *delta_buf;
185 delta_buf = malloc(bufsize);
186 if (delta_buf == NULL)
187 return got_error_from_errno("malloc");
189 /* base object size */
190 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
191 n = base_size >> GOT_DELTA_SIZE_SHIFT;
192 for (i = 1; n > 0; i++) {
193 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
194 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
195 n >>= GOT_DELTA_SIZE_SHIFT;
197 err = append(&delta_buf, &len, &bufsize, buf, i);
198 if (err)
199 goto done;
201 /* target object size */
202 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
203 n = o->size >> GOT_DELTA_SIZE_SHIFT;
204 for (i = 1; n > 0; i++) {
205 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
206 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
207 n >>= GOT_DELTA_SIZE_SHIFT;
209 err = append(&delta_buf, &len, &bufsize, buf, i);
210 if (err)
211 goto done;
213 for (j = 0; j < ndeltas; j++) {
214 d = &deltas[j];
215 if (d->copy) {
216 n = d->offset;
217 bp = &buf[1];
218 buf[0] = GOT_DELTA_BASE_COPY;
219 for (i = 0; i < 4; i++) {
220 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
221 buf[0] |= 1 << i;
222 *bp++ = n & 0xff;
223 n >>= 8;
224 if (n == 0)
225 break;
228 n = d->len;
229 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
230 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
231 for (i = 0; i < 3 && n > 0; i++) {
232 buf[0] |= 1 << (i + 4);
233 *bp++ = n & 0xff;
234 n >>= 8;
237 err = append(&delta_buf, &len, &bufsize,
238 buf, bp - buf);
239 if (err)
240 goto done;
241 } else if (o->f == NULL) {
242 n = 0;
243 while (n != d->len) {
244 buf[0] = (d->len - n < 127) ? d->len - n : 127;
245 err = append(&delta_buf, &len, &bufsize,
246 buf, 1);
247 if (err)
248 goto done;
249 err = append(&delta_buf, &len, &bufsize,
250 o->data + o->hdrlen + d->offset + n,
251 buf[0]);
252 if (err)
253 goto done;
254 n += buf[0];
256 } else {
257 char content[128];
258 size_t r;
259 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
260 err = got_error_from_errno("fseeko");
261 goto done;
263 n = 0;
264 while (n != d->len) {
265 buf[0] = (d->len - n < 127) ? d->len - n : 127;
266 err = append(&delta_buf, &len, &bufsize,
267 buf, 1);
268 if (err)
269 goto done;
270 r = fread(content, 1, buf[0], o->f);
271 if (r != buf[0]) {
272 err = got_ferror(o->f, GOT_ERR_IO);
273 goto done;
275 err = append(&delta_buf, &len, &bufsize,
276 content, buf[0]);
277 if (err)
278 goto done;
279 n += buf[0];
284 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
285 NULL, NULL, delta_buf, 0, len);
286 if (err)
287 goto done;
289 m->delta_len = len;
290 m->delta_compressed_len = compressed_len;
291 done:
292 free(delta_buf);
293 return err;
296 static const struct got_error *
297 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
298 struct got_delta_instruction *deltas, int ndeltas,
299 off_t base_size, FILE *f)
301 const struct got_error *err;
302 unsigned char buf[16], *bp;
303 int i, j;
304 off_t n;
305 struct got_deflate_buf zb;
306 struct got_delta_instruction *d;
307 off_t delta_len = 0, compressed_len = 0;
309 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
310 if (err)
311 return err;
313 /* base object size */
314 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
315 n = base_size >> GOT_DELTA_SIZE_SHIFT;
316 for (i = 1; n > 0; i++) {
317 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
318 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
319 n >>= GOT_DELTA_SIZE_SHIFT;
322 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
323 buf, 0, i, f, NULL);
324 if (err)
325 goto done;
326 delta_len += i;
328 /* target object size */
329 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
330 n = o->size >> GOT_DELTA_SIZE_SHIFT;
331 for (i = 1; n > 0; i++) {
332 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
333 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
334 n >>= GOT_DELTA_SIZE_SHIFT;
337 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
338 buf, 0, i, f, NULL);
339 if (err)
340 goto done;
341 delta_len += i;
343 for (j = 0; j < ndeltas; j++) {
344 d = &deltas[j];
345 if (d->copy) {
346 n = d->offset;
347 bp = &buf[1];
348 buf[0] = GOT_DELTA_BASE_COPY;
349 for (i = 0; i < 4; i++) {
350 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
351 buf[0] |= 1 << i;
352 *bp++ = n & 0xff;
353 n >>= 8;
354 if (n == 0)
355 break;
357 n = d->len;
358 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
359 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
360 for (i = 0; i < 3 && n > 0; i++) {
361 buf[0] |= 1 << (i + 4);
362 *bp++ = n & 0xff;
363 n >>= 8;
366 err = got_deflate_append_to_file_mmap(&zb,
367 &compressed_len, buf, 0, bp - buf, f, NULL);
368 if (err)
369 goto done;
370 delta_len += (bp - buf);
371 } else if (o->f == NULL) {
372 n = 0;
373 while (n != d->len) {
374 buf[0] = (d->len - n < 127) ? d->len - n : 127;
375 err = got_deflate_append_to_file_mmap(&zb,
376 &compressed_len, buf, 0, 1, f, NULL);
377 if (err)
378 goto done;
379 delta_len++;
380 err = got_deflate_append_to_file_mmap(&zb,
381 &compressed_len,
382 o->data + o->hdrlen + d->offset + n, 0,
383 buf[0], f, NULL);
384 if (err)
385 goto done;
386 delta_len += buf[0];
387 n += buf[0];
389 } else {
390 char content[128];
391 size_t r;
392 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
393 err = got_error_from_errno("fseeko");
394 goto done;
396 n = 0;
397 while (n != d->len) {
398 buf[0] = (d->len - n < 127) ? d->len - n : 127;
399 err = got_deflate_append_to_file_mmap(&zb,
400 &compressed_len, buf, 0, 1, f, NULL);
401 if (err)
402 goto done;
403 delta_len++;
404 r = fread(content, 1, buf[0], o->f);
405 if (r != buf[0]) {
406 err = got_ferror(o->f, GOT_ERR_IO);
407 goto done;
409 err = got_deflate_append_to_file_mmap(&zb,
410 &compressed_len, content, 0, buf[0], f,
411 NULL);
412 if (err)
413 goto done;
414 delta_len += buf[0];
415 n += buf[0];
420 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
421 if (err)
422 goto done;
424 /* sanity check */
425 if (compressed_len != ftello(f) - m->delta_offset) {
426 err = got_error(GOT_ERR_COMPRESSION);
427 goto done;
430 m->delta_len = delta_len;
431 m->delta_compressed_len = compressed_len;
432 done:
433 got_deflate_end(&zb);
434 return err;
437 const struct got_error *
438 got_pack_report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
439 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
440 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
441 int nobj_written)
443 const struct got_error *err;
444 int elapsed;
446 if (progress_cb == NULL)
447 return NULL;
449 err = got_ratelimit_check(&elapsed, rl);
450 if (err || !elapsed)
451 return err;
453 return progress_cb(progress_arg, ncolored, nfound, ntrees,
454 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
457 const struct got_error *
458 got_pack_add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
460 if (v->nmeta == v->metasz){
461 size_t newsize = 2 * v->metasz;
462 struct got_pack_meta **new;
463 new = reallocarray(v->meta, newsize, sizeof(*new));
464 if (new == NULL)
465 return got_error_from_errno("reallocarray");
466 v->meta = new;
467 v->metasz = newsize;
470 v->meta[v->nmeta++] = m;
471 return NULL;
474 const struct got_error *
475 got_pack_find_pack_for_reuse(struct got_packidx **best_packidx,
476 struct got_repository *repo)
478 const struct got_error *err = NULL;
479 struct got_pathlist_entry *pe;
480 const char *best_packidx_path = NULL;
481 int nobj_max = 0;
483 *best_packidx = NULL;
485 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
486 const char *path_packidx = pe->path;
487 struct got_packidx *packidx;
488 int nobj;
490 err = got_repo_get_packidx(&packidx, path_packidx, repo);
491 if (err)
492 break;
494 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
495 if (nobj > nobj_max) {
496 best_packidx_path = path_packidx;
497 nobj_max = nobj;
501 if (best_packidx_path) {
502 err = got_repo_get_packidx(best_packidx, best_packidx_path,
503 repo);
506 return err;
509 const struct got_error *
510 got_pack_cache_pack_for_packidx(struct got_pack **pack,
511 struct got_packidx *packidx, struct got_repository *repo)
513 const struct got_error *err;
514 char *path_packfile = NULL;
516 err = got_packidx_get_packfile_path(&path_packfile,
517 packidx->path_packidx);
518 if (err)
519 return err;
521 *pack = got_repo_get_cached_pack(repo, path_packfile);
522 if (*pack == NULL) {
523 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
524 if (err)
525 goto done;
527 done:
528 free(path_packfile);
529 return err;
532 static const struct got_error *
533 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
534 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
535 struct got_repository *repo,
536 got_pack_progress_cb progress_cb, void *progress_arg,
537 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
539 const struct got_error *err = NULL;
540 struct got_pack_meta *m = NULL, *base = NULL;
541 struct got_raw_object *raw = NULL, *base_raw = NULL;
542 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
543 int i, j, ndeltas, best_ndeltas;
544 off_t size, best_size;
545 const int max_base_candidates = 3;
546 size_t delta_memsize = 0;
547 const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
548 int outfd = -1;
549 uint32_t delta_seed;
551 delta_seed = arc4random();
553 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
554 for (i = 0; i < nmeta; i++) {
555 if (cancel_cb) {
556 err = (*cancel_cb)(cancel_arg);
557 if (err)
558 break;
560 err = got_pack_report_progress(progress_cb, progress_arg, rl,
561 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
562 nreused + i, 0);
563 if (err)
564 goto done;
565 m = meta[i];
567 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
568 m->obj_type == GOT_OBJ_TYPE_TAG)
569 continue;
571 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
572 if (err)
573 goto done;
574 m->size = raw->size;
576 if (raw->f == NULL) {
577 err = got_deltify_init_mem(&m->dtab, raw->data,
578 raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
579 } else {
580 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
581 raw->size + raw->hdrlen, delta_seed);
583 if (err)
584 goto done;
586 if (i > max_base_candidates) {
587 struct got_pack_meta *n = NULL;
588 n = meta[i - (max_base_candidates + 1)];
589 got_deltify_free(n->dtab);
590 n->dtab = NULL;
593 best_size = raw->size;
594 best_ndeltas = 0;
595 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
596 if (cancel_cb) {
597 err = (*cancel_cb)(cancel_arg);
598 if (err)
599 goto done;
601 base = meta[j];
602 /* long chains make unpacking slow, avoid such bases */
603 if (base->nchain >= 128 ||
604 base->obj_type != m->obj_type)
605 continue;
607 err = got_object_raw_open(&base_raw, &outfd, repo,
608 &base->id);
609 if (err)
610 goto done;
612 if (raw->f == NULL && base_raw->f == NULL) {
613 err = got_deltify_mem_mem(&deltas, &ndeltas,
614 raw->data, raw->hdrlen,
615 raw->size + raw->hdrlen, delta_seed,
616 base->dtab, base_raw->data,
617 base_raw->hdrlen,
618 base_raw->size + base_raw->hdrlen);
619 } else if (raw->f == NULL) {
620 err = got_deltify_mem_file(&deltas, &ndeltas,
621 raw->data, raw->hdrlen,
622 raw->size + raw->hdrlen, delta_seed,
623 base->dtab, base_raw->f,
624 base_raw->hdrlen,
625 base_raw->size + base_raw->hdrlen);
626 } else if (base_raw->f == NULL) {
627 err = got_deltify_file_mem(&deltas, &ndeltas,
628 raw->f, raw->hdrlen,
629 raw->size + raw->hdrlen, delta_seed,
630 base->dtab, base_raw->data,
631 base_raw->hdrlen,
632 base_raw->size + base_raw->hdrlen);
633 } else {
634 err = got_deltify(&deltas, &ndeltas,
635 raw->f, raw->hdrlen,
636 raw->size + raw->hdrlen, delta_seed,
637 base->dtab, base_raw->f, base_raw->hdrlen,
638 base_raw->size + base_raw->hdrlen);
640 got_object_raw_close(base_raw);
641 base_raw = NULL;
642 if (err)
643 goto done;
645 size = delta_size(deltas, ndeltas);
646 if (size + 32 < best_size){
648 * if we already picked a best delta,
649 * replace it.
651 best_size = size;
652 free(best_deltas);
653 best_deltas = deltas;
654 best_ndeltas = ndeltas;
655 deltas = NULL;
656 m->nchain = base->nchain + 1;
657 m->prev = base;
658 m->head = base->head;
659 if (m->head == NULL)
660 m->head = base;
661 } else {
662 free(deltas);
663 deltas = NULL;
664 ndeltas = 0;
668 if (best_ndeltas > 0) {
669 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
670 delta_memsize + best_size <= max_delta_memsize) {
671 delta_memsize += best_size;
672 err = encode_delta_in_mem(m, raw, best_deltas,
673 best_ndeltas, best_size, m->prev->size);
674 } else {
675 m->delta_offset = ftello(delta_cache);
676 err = encode_delta(m, raw, best_deltas,
677 best_ndeltas, m->prev->size, delta_cache);
679 free(best_deltas);
680 best_deltas = NULL;
681 best_ndeltas = 0;
682 if (err)
683 goto done;
686 got_object_raw_close(raw);
687 raw = NULL;
689 done:
690 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
691 got_deltify_free(meta[i]->dtab);
692 meta[i]->dtab = NULL;
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 queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1087 struct got_object_id_queue *ids, struct got_repository *repo)
1089 const struct got_error *err;
1090 struct got_tag_object *tag = NULL;
1091 int obj_type;
1093 err = got_object_get_type(&obj_type, repo, id);
1094 if (err)
1095 return err;
1097 if (obj_type == GOT_OBJ_TYPE_TAG) {
1098 err = got_object_open_as_tag(&tag, repo, id);
1099 if (err)
1100 return err;
1101 obj_type = got_object_tag_get_object_type(tag);
1102 id = got_object_tag_get_object_id(tag);
1105 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1106 err = got_pack_queue_commit_id(ids, id, color, repo);
1107 if (err)
1108 goto done;
1110 done:
1111 if (tag)
1112 got_object_tag_close(tag);
1113 return err;
1116 const struct got_error *
1117 got_pack_find_pack_for_commit_painting(struct got_packidx **best_packidx,
1118 struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1120 const struct got_error *err = NULL;
1121 struct got_pathlist_entry *pe;
1122 const char *best_packidx_path = NULL;
1123 int nobj_max = 0;
1124 int ncommits_max = 0;
1126 *best_packidx = NULL;
1129 * Find the largest pack which contains at least some of the
1130 * commits we are interested in.
1132 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1133 const char *path_packidx = pe->path;
1134 struct got_packidx *packidx;
1135 int nobj, idx, ncommits = 0;
1136 struct got_object_qid *qid;
1138 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1139 if (err)
1140 break;
1142 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1143 if (nobj <= nobj_max)
1144 continue;
1146 STAILQ_FOREACH(qid, ids, entry) {
1147 idx = got_packidx_get_object_idx(packidx, &qid->id);
1148 if (idx != -1)
1149 ncommits++;
1151 if (ncommits > ncommits_max) {
1152 best_packidx_path = path_packidx;
1153 nobj_max = nobj;
1154 ncommits_max = ncommits;
1158 if (best_packidx_path && err == NULL) {
1159 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1160 repo);
1163 return err;
1166 static const struct got_error *
1167 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1168 struct got_object_id **head, int nhead,
1169 struct got_object_id **tail, int ntail,
1170 struct got_repository *repo,
1171 got_pack_progress_cb progress_cb, void *progress_arg,
1172 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1174 const struct got_error *err = NULL;
1175 struct got_object_id_queue ids;
1176 struct got_object_idset *keep, *drop, *skip = NULL;
1177 int i, nkeep;
1179 STAILQ_INIT(&ids);
1180 *res = NULL;
1181 *nres = 0;
1182 *ncolored = 0;
1184 keep = got_object_idset_alloc();
1185 if (keep == NULL)
1186 return got_error_from_errno("got_object_idset_alloc");
1188 drop = got_object_idset_alloc();
1189 if (drop == NULL) {
1190 err = got_error_from_errno("got_object_idset_alloc");
1191 goto done;
1194 skip = got_object_idset_alloc();
1195 if (skip == NULL) {
1196 err = got_error_from_errno("got_object_idset_alloc");
1197 goto done;
1200 for (i = 0; i < nhead; i++) {
1201 struct got_object_id *id = head[i];
1202 if (id == NULL)
1203 continue;
1204 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1205 if (err)
1206 goto done;
1209 for (i = 0; i < ntail; i++) {
1210 struct got_object_id *id = tail[i];
1211 if (id == NULL)
1212 continue;
1213 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1214 if (err)
1215 goto done;
1218 err = got_pack_paint_commits(ncolored, &ids, nhead + ntail,
1219 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1220 cancel_cb, cancel_arg);
1221 if (err)
1222 goto done;
1224 nkeep = got_object_idset_num_elements(keep);
1225 if (nkeep > 0) {
1226 struct append_id_arg arg;
1227 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1228 if (arg.array == NULL) {
1229 err = got_error_from_errno("calloc");
1230 goto done;
1232 arg.idx = -1;
1233 arg.skip = skip;
1234 arg.drop = drop;
1235 err = got_object_idset_for_each(keep, append_id, &arg);
1236 if (err) {
1237 free(arg.array);
1238 goto done;
1240 *res = arg.array;
1241 *nres = arg.idx + 1;
1243 done:
1244 got_object_idset_free(keep);
1245 got_object_idset_free(drop);
1246 if (skip)
1247 got_object_idset_free(skip);
1248 got_object_id_queue_free(&ids);
1249 return err;
1252 static const struct got_error *
1253 find_pack_for_enumeration(struct got_packidx **best_packidx,
1254 struct got_object_id **ids, int nids, struct got_repository *repo)
1256 const struct got_error *err = NULL;
1257 struct got_pathlist_entry *pe;
1258 const char *best_packidx_path = NULL;
1259 int nobj_max = 0;
1260 int ncommits_max = 0;
1262 *best_packidx = NULL;
1265 * Find the largest pack which contains at least some of the
1266 * commits and tags we are interested in.
1268 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1269 const char *path_packidx = pe->path;
1270 struct got_packidx *packidx;
1271 int nobj, i, idx, ncommits = 0;
1273 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1274 if (err)
1275 break;
1277 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1278 if (nobj <= nobj_max)
1279 continue;
1281 for (i = 0; i < nids; i++) {
1282 idx = got_packidx_get_object_idx(packidx, ids[i]);
1283 if (idx != -1)
1284 ncommits++;
1286 if (ncommits > ncommits_max) {
1287 best_packidx_path = path_packidx;
1288 nobj_max = nobj;
1289 ncommits_max = ncommits;
1293 if (best_packidx_path && err == NULL) {
1294 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1295 repo);
1298 return err;
1301 static const struct got_error *
1302 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1303 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1304 struct got_object_id **ours, int nours, struct got_repository *repo,
1305 uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1306 void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1307 void *cancel_arg)
1309 const struct got_error *err = NULL;
1310 struct got_object_id **ids = NULL;
1311 struct got_packidx *packidx = NULL;
1312 int i, nobj = 0, obj_type, found_all_objects = 0;
1313 struct got_object_idset *idset_exclude;
1315 idset_exclude = got_object_idset_alloc();
1316 if (idset_exclude == NULL)
1317 return got_error_from_errno("got_object_idset_alloc");
1319 *ncolored = 0;
1320 *nfound = 0;
1321 *ntrees = 0;
1323 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1324 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1325 if (err)
1326 goto done;
1328 err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
1329 if (err)
1330 goto done;
1331 if (packidx) {
1332 err = got_pack_load_packed_object_ids(&found_all_objects,
1333 theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
1334 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1335 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1336 if (err)
1337 goto done;
1340 for (i = 0; i < ntheirs; i++) {
1341 struct got_object_id *id = theirs[i];
1342 if (id == NULL)
1343 continue;
1344 err = got_object_get_type(&obj_type, repo, id);
1345 if (err)
1346 return err;
1347 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1348 if (!found_all_objects) {
1349 err = load_commit(0, idset, idset_exclude,
1350 id, repo, seed, loose_obj_only,
1351 ncolored, nfound, ntrees,
1352 progress_cb, progress_arg, rl,
1353 cancel_cb, cancel_arg);
1354 if (err)
1355 goto done;
1357 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1358 err = load_tag(0, idset, idset_exclude, id, repo,
1359 seed, loose_obj_only, ncolored, nfound, ntrees,
1360 progress_cb, progress_arg, rl,
1361 cancel_cb, cancel_arg);
1362 if (err)
1363 goto done;
1367 found_all_objects = 0;
1368 err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
1369 if (err)
1370 goto done;
1371 if (packidx) {
1372 err = got_pack_load_packed_object_ids(&found_all_objects, ids,
1373 nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
1374 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1375 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1376 if (err)
1377 goto done;
1380 if (!found_all_objects) {
1381 for (i = 0; i < nobj; i++) {
1382 err = load_commit(1, idset, idset_exclude, ids[i],
1383 repo, seed, loose_obj_only, ncolored, nfound,
1384 ntrees, progress_cb, progress_arg, rl,
1385 cancel_cb, cancel_arg);
1386 if (err)
1387 goto done;
1391 for (i = 0; i < nours; i++) {
1392 struct got_object_id *id = ours[i];
1393 struct got_pack_meta *m;
1394 if (id == NULL)
1395 continue;
1396 m = got_object_idset_get(idset, id);
1397 if (m == NULL) {
1398 err = got_object_get_type(&obj_type, repo, id);
1399 if (err)
1400 goto done;
1401 } else
1402 obj_type = m->obj_type;
1403 if (obj_type != GOT_OBJ_TYPE_TAG)
1404 continue;
1405 err = load_tag(1, idset, idset_exclude, id, repo,
1406 seed, loose_obj_only, ncolored, nfound, ntrees,
1407 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1408 if (err)
1409 goto done;
1411 done:
1412 for (i = 0; i < nobj; i++) {
1413 free(ids[i]);
1415 free(ids);
1416 got_object_idset_free(idset_exclude);
1417 return err;
1420 static const struct got_error *
1421 hwrite(int fd, const void *buf, off_t len, struct got_hash *ctx)
1423 got_hash_update(ctx, buf, len);
1424 return got_poll_write_full(fd, buf, len);
1427 static const struct got_error *
1428 hcopy(FILE *fsrc, int fd_dst, off_t len, struct got_hash *ctx)
1430 const struct got_error *err;
1431 unsigned char buf[65536];
1432 off_t remain = len;
1433 size_t n;
1435 while (remain > 0) {
1436 size_t copylen = MIN(sizeof(buf), remain);
1437 n = fread(buf, 1, copylen, fsrc);
1438 if (n != copylen)
1439 return got_ferror(fsrc, GOT_ERR_IO);
1440 got_hash_update(ctx, buf, copylen);
1441 err = got_poll_write_full(fd_dst, buf, copylen);
1442 if (err)
1443 return err;
1444 remain -= copylen;
1447 return NULL;
1450 static const struct got_error *
1451 hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1452 int fd, off_t len, struct got_hash *ctx)
1454 if (src_offset + len > src_size)
1455 return got_error(GOT_ERR_RANGE);
1457 got_hash_update(ctx, src + src_offset, len);
1458 return got_poll_write_full(fd, src + src_offset, len);
1461 static void
1462 putbe32(char *b, uint32_t n)
1464 b[0] = n >> 24;
1465 b[1] = n >> 16;
1466 b[2] = n >> 8;
1467 b[3] = n >> 0;
1470 static int
1471 write_order_cmp(const void *pa, const void *pb)
1473 struct got_pack_meta *a, *b, *ahd, *bhd;
1475 a = *(struct got_pack_meta **)pa;
1476 b = *(struct got_pack_meta **)pb;
1477 ahd = (a->head == NULL) ? a : a->head;
1478 bhd = (b->head == NULL) ? b : b->head;
1479 if (bhd->mtime < ahd->mtime)
1480 return -1;
1481 if (bhd->mtime > ahd->mtime)
1482 return 1;
1483 if (bhd < ahd)
1484 return -1;
1485 if (bhd > ahd)
1486 return 1;
1487 if (a->nchain != b->nchain)
1488 return a->nchain - b->nchain;
1489 if (a->mtime < b->mtime)
1490 return -1;
1491 if (a->mtime > b->mtime)
1492 return 1;
1493 return got_object_id_cmp(&a->id, &b->id);
1496 static int
1497 reuse_write_order_cmp(const void *pa, const void *pb)
1499 struct got_pack_meta *a, *b;
1501 a = *(struct got_pack_meta **)pa;
1502 b = *(struct got_pack_meta **)pb;
1504 if (a->reused_delta_offset < b->reused_delta_offset)
1505 return -1;
1506 if (a->reused_delta_offset > b->reused_delta_offset)
1507 return 1;
1508 return 0;
1511 static const struct got_error *
1512 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1514 size_t i;
1516 *hdrlen = 0;
1518 hdr[0] = obj_type << 4;
1519 hdr[0] |= len & 0xf;
1520 len >>= 4;
1521 for (i = 1; len != 0; i++){
1522 if (i >= bufsize)
1523 return got_error(GOT_ERR_NO_SPACE);
1524 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1525 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1526 len >>= GOT_DELTA_SIZE_SHIFT;
1529 *hdrlen = i;
1530 return NULL;
1533 static int
1534 packoff(char *hdr, off_t off)
1536 int i, j;
1537 char rbuf[8];
1539 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1540 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1541 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1542 GOT_DELTA_SIZE_MORE;
1545 j = 0;
1546 while (i > 0)
1547 hdr[j++] = rbuf[--i];
1548 return j;
1551 static const struct got_error *
1552 deltahdr(off_t *packfile_size, struct got_hash *ctx, int packfd,
1553 int force_refdelta, struct got_pack_meta *m)
1555 const struct got_error *err;
1556 char buf[32];
1557 int nh;
1559 if (m->prev->off != 0 && !force_refdelta) {
1560 err = packhdr(&nh, buf, sizeof(buf),
1561 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1562 if (err)
1563 return err;
1564 nh += packoff(buf + nh, m->off - m->prev->off);
1565 err = hwrite(packfd, buf, nh, ctx);
1566 if (err)
1567 return err;
1568 *packfile_size += nh;
1569 } else {
1570 err = packhdr(&nh, buf, sizeof(buf),
1571 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1572 if (err)
1573 return err;
1574 err = hwrite(packfd, buf, nh, ctx);
1575 if (err)
1576 return err;
1577 *packfile_size += nh;
1578 err = hwrite(packfd, m->prev->id.sha1,
1579 sizeof(m->prev->id.sha1), ctx);
1580 if (err)
1581 return err;
1582 *packfile_size += sizeof(m->prev->id.sha1);
1585 return NULL;
1588 static const struct got_error *
1589 write_packed_object(off_t *packfile_size, int packfd,
1590 FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1591 struct got_pack_meta *m, int *outfd, struct got_hash *ctx,
1592 struct got_repository *repo, int force_refdelta)
1594 const struct got_error *err = NULL;
1595 struct got_deflate_checksum csum;
1596 char buf[32];
1597 int nh;
1598 struct got_raw_object *raw = NULL;
1599 off_t outlen, delta_offset;
1601 memset(&csum, 0, sizeof(csum));
1602 csum.output_ctx = ctx;
1604 if (m->reused_delta_offset)
1605 delta_offset = m->reused_delta_offset;
1606 else
1607 delta_offset = m->delta_offset;
1609 m->off = *packfile_size;
1610 if (m->delta_len == 0) {
1611 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1612 if (err)
1613 goto done;
1614 err = packhdr(&nh, buf, sizeof(buf),
1615 m->obj_type, raw->size);
1616 if (err)
1617 goto done;
1618 err = hwrite(packfd, buf, nh, ctx);
1619 if (err)
1620 goto done;
1621 *packfile_size += nh;
1622 if (raw->f == NULL) {
1623 err = got_deflate_to_fd_mmap(&outlen,
1624 raw->data + raw->hdrlen, 0, raw->size,
1625 packfd, &csum);
1626 if (err)
1627 goto done;
1628 } else {
1629 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1630 == -1) {
1631 err = got_error_from_errno("fseeko");
1632 goto done;
1634 err = got_deflate_to_fd(&outlen, raw->f,
1635 raw->size, packfd, &csum);
1636 if (err)
1637 goto done;
1639 *packfile_size += outlen;
1640 got_object_raw_close(raw);
1641 raw = NULL;
1642 } else if (m->delta_buf) {
1643 err = deltahdr(packfile_size, ctx, packfd, force_refdelta, m);
1644 if (err)
1645 goto done;
1646 err = hwrite(packfd, m->delta_buf,
1647 m->delta_compressed_len, ctx);
1648 if (err)
1649 goto done;
1650 *packfile_size += m->delta_compressed_len;
1651 free(m->delta_buf);
1652 m->delta_buf = NULL;
1653 } else if (delta_cache_map) {
1654 err = deltahdr(packfile_size, ctx, packfd, force_refdelta, m);
1655 if (err)
1656 goto done;
1657 err = hcopy_mmap(delta_cache_map, delta_offset,
1658 delta_cache_size, packfd, m->delta_compressed_len,
1659 ctx);
1660 if (err)
1661 goto done;
1662 *packfile_size += m->delta_compressed_len;
1663 } else {
1664 if (fseeko(delta_cache, delta_offset, SEEK_SET) == -1) {
1665 err = got_error_from_errno("fseeko");
1666 goto done;
1668 err = deltahdr(packfile_size, ctx, packfd, force_refdelta, m);
1669 if (err)
1670 goto done;
1671 err = hcopy(delta_cache, packfd,
1672 m->delta_compressed_len, ctx);
1673 if (err)
1674 goto done;
1675 *packfile_size += m->delta_compressed_len;
1677 done:
1678 if (raw)
1679 got_object_raw_close(raw);
1680 return err;
1683 static const struct got_error *
1684 genpack(uint8_t *pack_sha1, int packfd, struct got_pack *reuse_pack,
1685 FILE *delta_cache, struct got_pack_meta **deltify, int ndeltify,
1686 struct got_pack_meta **reuse, int nreuse,
1687 int ncolored, int nfound, int ntrees, int nours,
1688 struct got_repository *repo, int force_refdelta,
1689 got_pack_progress_cb progress_cb, void *progress_arg,
1690 struct got_ratelimit *rl,
1691 got_cancel_cb cancel_cb, void *cancel_arg)
1693 const struct got_error *err = NULL;
1694 int i;
1695 struct got_hash ctx;
1696 struct got_pack_meta *m;
1697 char buf[32];
1698 off_t packfile_size = 0;
1699 int outfd = -1;
1700 int delta_cache_fd = -1;
1701 uint8_t *delta_cache_map = NULL;
1702 size_t delta_cache_size = 0;
1703 FILE *packfile = NULL;
1705 got_hash_init(&ctx, GOT_HASH_SHA1);
1707 #ifndef GOT_PACK_NO_MMAP
1708 delta_cache_fd = dup(fileno(delta_cache));
1709 if (delta_cache_fd != -1) {
1710 struct stat sb;
1711 if (fstat(delta_cache_fd, &sb) == -1) {
1712 err = got_error_from_errno("fstat");
1713 goto done;
1715 if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1716 delta_cache_map = mmap(NULL, sb.st_size,
1717 PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1718 if (delta_cache_map == MAP_FAILED) {
1719 if (errno != ENOMEM) {
1720 err = got_error_from_errno("mmap");
1721 goto done;
1723 delta_cache_map = NULL; /* fallback on stdio */
1724 } else
1725 delta_cache_size = (size_t)sb.st_size;
1728 #endif
1729 err = hwrite(packfd, "PACK", 4, &ctx);
1730 if (err)
1731 goto done;
1732 putbe32(buf, GOT_PACKFILE_VERSION);
1733 err = hwrite(packfd, buf, 4, &ctx);
1734 if (err)
1735 goto done;
1736 putbe32(buf, ndeltify + nreuse);
1737 err = hwrite(packfd, buf, 4, &ctx);
1738 if (err)
1739 goto done;
1741 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1742 write_order_cmp);
1743 for (i = 0; i < ndeltify; i++) {
1744 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1745 ncolored, nfound, ntrees, packfile_size, nours,
1746 ndeltify + nreuse, ndeltify + nreuse, i);
1747 if (err)
1748 goto done;
1749 m = deltify[i];
1750 err = write_packed_object(&packfile_size, packfd,
1751 delta_cache, delta_cache_map, delta_cache_size,
1752 m, &outfd, &ctx, repo, force_refdelta);
1753 if (err)
1754 goto done;
1757 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1758 reuse_write_order_cmp);
1759 if (nreuse > 0 && reuse_pack->map == NULL) {
1760 int fd = dup(reuse_pack->fd);
1761 if (fd == -1) {
1762 err = got_error_from_errno("dup");
1763 goto done;
1765 packfile = fdopen(fd, "r");
1766 if (packfile == NULL) {
1767 err = got_error_from_errno("fdopen");
1768 close(fd);
1769 goto done;
1772 for (i = 0; i < nreuse; i++) {
1773 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1774 ncolored, nfound, ntrees, packfile_size, nours,
1775 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1776 if (err)
1777 goto done;
1778 m = reuse[i];
1779 err = write_packed_object(&packfile_size, packfd,
1780 packfile, reuse_pack->map, reuse_pack->filesize,
1781 m, &outfd, &ctx, repo, force_refdelta);
1782 if (err)
1783 goto done;
1786 got_hash_final(&ctx, pack_sha1);
1787 err = got_poll_write_full(packfd, pack_sha1, SHA1_DIGEST_LENGTH);
1788 if (err)
1789 goto done;
1790 packfile_size += SHA1_DIGEST_LENGTH;
1791 packfile_size += sizeof(struct got_packfile_hdr);
1792 if (progress_cb) {
1793 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1794 packfile_size, nours, ndeltify + nreuse,
1795 ndeltify + nreuse, ndeltify + nreuse);
1796 if (err)
1797 goto done;
1799 done:
1800 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1801 err = got_error_from_errno("close");
1802 if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1803 err = got_error_from_errno("munmap");
1804 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1805 err = got_error_from_errno("close");
1806 if (packfile && fclose(packfile) == EOF && err == NULL)
1807 err = got_error_from_errno("fclose");
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 int force_refdelta, 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 struct got_object_idset *idset;
1833 struct got_packidx *reuse_packidx = NULL;
1834 struct got_pack *reuse_pack = NULL;
1835 struct got_pack_metavec deltify, reuse;
1836 int ncolored = 0, nfound = 0, ntrees = 0;
1837 size_t ndeltify;
1838 uint32_t seed;
1840 seed = arc4random();
1842 memset(&deltify, 0, sizeof(deltify));
1843 memset(&reuse, 0, sizeof(reuse));
1845 idset = got_object_idset_alloc();
1846 if (idset == NULL)
1847 return got_error_from_errno("got_object_idset_alloc");
1849 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1850 ntheirs, ours, nours, repo, seed, loose_obj_only,
1851 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1852 if (err)
1853 goto done;
1855 if (progress_cb) {
1856 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1857 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1858 if (err)
1859 goto done;
1862 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1863 err = got_error(GOT_ERR_CANNOT_PACK);
1864 goto done;
1867 reuse.metasz = 64;
1868 reuse.meta = calloc(reuse.metasz,
1869 sizeof(struct got_pack_meta *));
1870 if (reuse.meta == NULL) {
1871 err = got_error_from_errno("calloc");
1872 goto done;
1875 err = got_pack_search_deltas(&reuse_packidx, &reuse_pack,
1876 &reuse, idset, ncolored, nfound, ntrees, nours,
1877 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1878 if (err)
1879 goto done;
1881 if (reuse_packidx && reuse_pack) {
1882 err = got_repo_pin_pack(repo, reuse_packidx, reuse_pack);
1883 if (err)
1884 goto done;
1887 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1888 err = got_error_from_errno("fseeko");
1889 goto done;
1892 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1893 if (ndeltify > 0) {
1894 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1895 if (deltify.meta == NULL) {
1896 err = got_error_from_errno("calloc");
1897 goto done;
1899 deltify.metasz = ndeltify;
1901 err = got_object_idset_for_each(idset, add_meta_idset_cb,
1902 &deltify);
1903 if (err)
1904 goto done;
1905 if (deltify.nmeta > 0) {
1906 err = pick_deltas(deltify.meta, deltify.nmeta,
1907 ncolored, nfound, ntrees, nours, reuse.nmeta,
1908 delta_cache, repo, progress_cb, progress_arg, rl,
1909 cancel_cb, cancel_arg);
1910 if (err)
1911 goto done;
1915 if (fflush(delta_cache) == EOF) {
1916 err = got_error_from_errno("fflush");
1917 goto done;
1920 if (progress_cb) {
1922 * Report a 1-byte packfile write to indicate we are about
1923 * to start sending packfile data. gotd(8) needs this.
1925 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1926 1 /* packfile_size */, nours,
1927 got_object_idset_num_elements(idset),
1928 deltify.nmeta + reuse.nmeta, 0);
1929 if (err)
1930 goto done;
1933 /* Pinned pack may have moved to different cache slot. */
1934 reuse_pack = got_repo_get_pinned_pack(repo);
1936 err = genpack(packsha1, packfd, reuse_pack, delta_cache, deltify.meta,
1937 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1938 nours, repo, force_refdelta, progress_cb, progress_arg, rl,
1939 cancel_cb, cancel_arg);
1940 if (err)
1941 goto done;
1942 done:
1943 free_nmeta(deltify.meta, deltify.nmeta);
1944 free_nmeta(reuse.meta, reuse.nmeta);
1945 got_object_idset_free(idset);
1946 got_repo_unpin_pack(repo);
1947 return err;