portable: add back sys/queue.h
[got-portable.git] / lib / pack_create.c
blobf7d23f876f92b546a95aaf832e5265e12021f2cd
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 <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/uio.h>
21 #include <sys/stat.h>
22 #include <sys/time.h>
23 #include <sys/mman.h>
25 #include <errno.h>
26 #include <stdint.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <time.h>
31 #include <unistd.h>
32 #include <limits.h>
33 #include <zlib.h>
35 #if defined(__FreeBSD__)
36 #include <unistd.h>
37 #endif
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"
45 #include "got_opentemp.h"
47 #include "got_lib_deltify.h"
48 #include "got_lib_delta.h"
49 #include "got_lib_object.h"
50 #include "got_lib_object_idset.h"
51 #include "got_lib_object_cache.h"
52 #include "got_lib_deflate.h"
53 #include "got_lib_pack.h"
54 #include "got_lib_pack_create.h"
55 #include "got_lib_privsep.h"
56 #include "got_lib_repository.h"
57 #include "got_lib_ratelimit.h"
58 #include "got_lib_inflate.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 struct got_pack_meta {
75 struct got_object_id id;
76 uint32_t path_hash;
77 int obj_type;
78 off_t size;
79 time_t mtime;
81 /* The best delta we picked */
82 struct got_pack_meta *head;
83 struct got_pack_meta *prev;
84 unsigned char *delta_buf; /* if encoded in memory (compressed) */
85 off_t delta_offset; /* offset in delta cache file (compressed) */
86 off_t delta_len; /* encoded delta length */
87 off_t delta_compressed_len; /* encoded+compressed delta length */
88 int nchain;
90 off_t reused_delta_offset; /* offset of delta in reused pack file */
91 struct got_object_id *base_obj_id;
93 /* Only used for delta window */
94 struct got_delta_table *dtab;
96 /* Only used for writing offset deltas */
97 off_t off;
100 struct got_pack_metavec {
101 struct got_pack_meta **meta;
102 int nmeta;
103 int metasz;
106 static const struct got_error *
107 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
108 const char *path, int obj_type, time_t mtime, uint32_t seed)
110 struct got_pack_meta *m;
112 *new = NULL;
114 m = calloc(1, sizeof(*m));
115 if (m == NULL)
116 return got_error_from_errno("calloc");
118 memcpy(&m->id, id, sizeof(m->id));
120 m->path_hash = murmurhash2(path, strlen(path), seed);
121 m->obj_type = obj_type;
122 m->mtime = mtime;
123 *new = m;
124 return NULL;
127 static void
128 clear_meta(struct got_pack_meta *meta)
130 if (meta == NULL)
131 return;
132 meta->path_hash = 0;
133 free(meta->delta_buf);
134 meta->delta_buf = NULL;
135 free(meta->base_obj_id);
136 meta->base_obj_id = NULL;
137 meta->reused_delta_offset = 0;
140 static void
141 free_nmeta(struct got_pack_meta **meta, int nmeta)
143 int i;
145 for (i = 0; i < nmeta; i++)
146 clear_meta(meta[i]);
147 free(meta);
150 static int
151 delta_order_cmp(const void *pa, const void *pb)
153 struct got_pack_meta *a, *b;
155 a = *(struct got_pack_meta **)pa;
156 b = *(struct got_pack_meta **)pb;
158 if (a->obj_type != b->obj_type)
159 return a->obj_type - b->obj_type;
160 if (a->path_hash < b->path_hash)
161 return -1;
162 if (a->path_hash > b->path_hash)
163 return 1;
164 if (a->mtime < b->mtime)
165 return -1;
166 if (a->mtime > b->mtime)
167 return 1;
168 return got_object_id_cmp(&a->id, &b->id);
171 static off_t
172 delta_size(struct got_delta_instruction *deltas, int ndeltas)
174 int i;
175 off_t size = 32;
176 for (i = 0; i < ndeltas; i++) {
177 if (deltas[i].copy)
178 size += GOT_DELTA_SIZE_SHIFT;
179 else
180 size += deltas[i].len + 1;
182 return size;
185 static const struct got_error *
186 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
188 char *n;
190 if (*len + nseg >= *sz) {
191 while (*len + nseg >= *sz)
192 *sz += *sz / 2;
193 n = realloc(*p, *sz);
194 if (n == NULL)
195 return got_error_from_errno("realloc");
196 *p = n;
198 memcpy(*p + *len, seg, nseg);
199 *len += nseg;
200 return NULL;
203 static const struct got_error *
204 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
205 struct got_delta_instruction *deltas, int ndeltas,
206 off_t delta_size, off_t base_size)
208 const struct got_error *err;
209 unsigned char buf[16], *bp;
210 int i, j;
211 size_t len = 0, compressed_len;
212 off_t bufsize = delta_size;
213 off_t n;
214 struct got_delta_instruction *d;
215 uint8_t *delta_buf;
217 delta_buf = malloc(bufsize);
218 if (delta_buf == NULL)
219 return got_error_from_errno("malloc");
221 /* base object size */
222 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
223 n = base_size >> GOT_DELTA_SIZE_SHIFT;
224 for (i = 1; n > 0; i++) {
225 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
226 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
227 n >>= GOT_DELTA_SIZE_SHIFT;
229 err = append(&delta_buf, &len, &bufsize, buf, i);
230 if (err)
231 goto done;
233 /* target object size */
234 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
235 n = o->size >> GOT_DELTA_SIZE_SHIFT;
236 for (i = 1; n > 0; i++) {
237 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
238 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
239 n >>= GOT_DELTA_SIZE_SHIFT;
241 err = append(&delta_buf, &len, &bufsize, buf, i);
242 if (err)
243 goto done;
245 for (j = 0; j < ndeltas; j++) {
246 d = &deltas[j];
247 if (d->copy) {
248 n = d->offset;
249 bp = &buf[1];
250 buf[0] = GOT_DELTA_BASE_COPY;
251 for (i = 0; i < 4; i++) {
252 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
253 buf[0] |= 1 << i;
254 *bp++ = n & 0xff;
255 n >>= 8;
256 if (n == 0)
257 break;
260 n = d->len;
261 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
262 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
263 for (i = 0; i < 3 && n > 0; i++) {
264 buf[0] |= 1 << (i + 4);
265 *bp++ = n & 0xff;
266 n >>= 8;
269 err = append(&delta_buf, &len, &bufsize,
270 buf, bp - buf);
271 if (err)
272 goto done;
273 } else if (o->f == NULL) {
274 n = 0;
275 while (n != d->len) {
276 buf[0] = (d->len - n < 127) ? d->len - n : 127;
277 err = append(&delta_buf, &len, &bufsize,
278 buf, 1);
279 if (err)
280 goto done;
281 err = append(&delta_buf, &len, &bufsize,
282 o->data + o->hdrlen + d->offset + n,
283 buf[0]);
284 if (err)
285 goto done;
286 n += buf[0];
288 } else {
289 char content[128];
290 size_t r;
291 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
292 err = got_error_from_errno("fseeko");
293 goto done;
295 n = 0;
296 while (n != d->len) {
297 buf[0] = (d->len - n < 127) ? d->len - n : 127;
298 err = append(&delta_buf, &len, &bufsize,
299 buf, 1);
300 if (err)
301 goto done;
302 r = fread(content, 1, buf[0], o->f);
303 if (r != buf[0]) {
304 err = got_ferror(o->f, GOT_ERR_IO);
305 goto done;
307 err = append(&delta_buf, &len, &bufsize,
308 content, buf[0]);
309 if (err)
310 goto done;
311 n += buf[0];
316 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
317 NULL, NULL, delta_buf, 0, len);
318 if (err)
319 goto done;
321 m->delta_len = len;
322 m->delta_compressed_len = compressed_len;
323 done:
324 free(delta_buf);
325 return err;
328 static const struct got_error *
329 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
330 struct got_delta_instruction *deltas, int ndeltas,
331 off_t base_size, FILE *f)
333 const struct got_error *err;
334 unsigned char buf[16], *bp;
335 int i, j;
336 off_t n;
337 struct got_deflate_buf zb;
338 struct got_delta_instruction *d;
339 off_t delta_len = 0, compressed_len = 0;
341 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
342 if (err)
343 return err;
345 /* base object size */
346 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
347 n = base_size >> GOT_DELTA_SIZE_SHIFT;
348 for (i = 1; n > 0; i++) {
349 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
350 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
351 n >>= GOT_DELTA_SIZE_SHIFT;
354 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
355 buf, 0, i, f, NULL);
356 if (err)
357 goto done;
358 delta_len += i;
360 /* target object size */
361 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
362 n = o->size >> GOT_DELTA_SIZE_SHIFT;
363 for (i = 1; n > 0; i++) {
364 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
365 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
366 n >>= GOT_DELTA_SIZE_SHIFT;
369 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
370 buf, 0, i, f, NULL);
371 if (err)
372 goto done;
373 delta_len += i;
375 for (j = 0; j < ndeltas; j++) {
376 d = &deltas[j];
377 if (d->copy) {
378 n = d->offset;
379 bp = &buf[1];
380 buf[0] = GOT_DELTA_BASE_COPY;
381 for (i = 0; i < 4; i++) {
382 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
383 buf[0] |= 1 << i;
384 *bp++ = n & 0xff;
385 n >>= 8;
386 if (n == 0)
387 break;
389 n = d->len;
390 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
391 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
392 for (i = 0; i < 3 && n > 0; i++) {
393 buf[0] |= 1 << (i + 4);
394 *bp++ = n & 0xff;
395 n >>= 8;
398 err = got_deflate_append_to_file_mmap(&zb,
399 &compressed_len, buf, 0, bp - buf, f, NULL);
400 if (err)
401 goto done;
402 delta_len += (bp - buf);
403 } else if (o->f == NULL) {
404 n = 0;
405 while (n != d->len) {
406 buf[0] = (d->len - n < 127) ? d->len - n : 127;
407 err = got_deflate_append_to_file_mmap(&zb,
408 &compressed_len, buf, 0, 1, f, NULL);
409 if (err)
410 goto done;
411 delta_len++;
412 err = got_deflate_append_to_file_mmap(&zb,
413 &compressed_len,
414 o->data + o->hdrlen + d->offset + n, 0,
415 buf[0], f, NULL);
416 if (err)
417 goto done;
418 delta_len += buf[0];
419 n += buf[0];
421 } else {
422 char content[128];
423 size_t r;
424 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
425 err = got_error_from_errno("fseeko");
426 goto done;
428 n = 0;
429 while (n != d->len) {
430 buf[0] = (d->len - n < 127) ? d->len - n : 127;
431 err = got_deflate_append_to_file_mmap(&zb,
432 &compressed_len, buf, 0, 1, f, NULL);
433 if (err)
434 goto done;
435 delta_len++;
436 r = fread(content, 1, buf[0], o->f);
437 if (r != buf[0]) {
438 err = got_ferror(o->f, GOT_ERR_IO);
439 goto done;
441 err = got_deflate_append_to_file_mmap(&zb,
442 &compressed_len, content, 0, buf[0], f,
443 NULL);
444 if (err)
445 goto done;
446 delta_len += buf[0];
447 n += buf[0];
452 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
453 if (err)
454 goto done;
456 /* sanity check */
457 if (compressed_len != ftello(f) - m->delta_offset) {
458 err = got_error(GOT_ERR_COMPRESSION);
459 goto done;
462 m->delta_len = delta_len;
463 m->delta_compressed_len = compressed_len;
464 done:
465 got_deflate_end(&zb);
466 return err;
469 static const struct got_error *
470 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
471 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
472 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
473 int nobj_written)
475 const struct got_error *err;
476 int elapsed;
478 if (progress_cb == NULL)
479 return NULL;
481 err = got_ratelimit_check(&elapsed, rl);
482 if (err || !elapsed)
483 return err;
485 return progress_cb(progress_arg, ncolored, nfound, ntrees,
486 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
489 static const struct got_error *
490 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
492 if (v->nmeta == v->metasz){
493 size_t newsize = 2 * v->metasz;
494 struct got_pack_meta **new;
495 new = reallocarray(v->meta, newsize, sizeof(*new));
496 if (new == NULL)
497 return got_error_from_errno("reallocarray");
498 v->meta = new;
499 v->metasz = newsize;
502 v->meta[v->nmeta++] = m;
503 return NULL;
506 static const struct got_error *
507 find_pack_for_reuse(struct got_packidx **best_packidx,
508 struct got_repository *repo)
510 const struct got_error *err = NULL;
511 struct got_pathlist_entry *pe;
512 const char *best_packidx_path = NULL;
513 int nobj_max = 0;
515 *best_packidx = NULL;
517 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
518 const char *path_packidx = pe->path;
519 struct got_packidx *packidx;
520 int nobj;
522 err = got_repo_get_packidx(&packidx, path_packidx, repo);
523 if (err)
524 break;
526 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
527 if (nobj > nobj_max) {
528 best_packidx_path = path_packidx;
529 nobj_max = nobj;
533 if (best_packidx_path) {
534 err = got_repo_get_packidx(best_packidx, best_packidx_path,
535 repo);
538 return err;
541 struct send_id_arg {
542 struct imsgbuf *ibuf;
543 struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
544 size_t nids;
547 static const struct got_error *
548 send_id(struct got_object_id *id, void *data, void *arg)
550 const struct got_error *err = NULL;
551 struct send_id_arg *a = arg;
553 a->ids[a->nids++] = id;
555 if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
556 err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
557 if (err)
558 return err;
559 a->nids = 0;
562 return NULL;
565 static const struct got_error *
566 send_idset(struct imsgbuf *ibuf, struct got_object_idset *idset)
568 const struct got_error *err;
569 struct send_id_arg sia;
571 memset(&sia, 0, sizeof(sia));
572 sia.ibuf = ibuf;
573 err = got_object_idset_for_each(idset, send_id, &sia);
574 if (err)
575 return err;
577 if (sia.nids > 0) {
578 err = got_privsep_send_object_idlist(ibuf, sia.ids, sia.nids);
579 if (err)
580 return err;
583 return got_privsep_send_object_idlist_done(ibuf);
587 static const struct got_error *
588 recv_reused_delta(struct got_imsg_reused_delta *delta,
589 struct got_object_idset *idset, struct got_pack_metavec *v)
591 struct got_pack_meta *m, *base;
593 if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
594 delta->delta_offset +
595 delta->delta_compressed_size < delta->delta_offset)
596 return got_error(GOT_ERR_BAD_PACKFILE);
598 m = got_object_idset_get(idset, &delta->id);
599 if (m == NULL)
600 return got_error(GOT_ERR_NO_OBJ);
602 base = got_object_idset_get(idset, &delta->base_id);
603 if (base == NULL)
604 return got_error(GOT_ERR_NO_OBJ);
606 m->delta_len = delta->delta_size;
607 m->delta_compressed_len = delta->delta_compressed_size;
608 m->delta_offset = delta->delta_out_offset;
609 m->prev = base;
610 m->size = delta->result_size;
611 m->reused_delta_offset = delta->delta_offset;
612 m->base_obj_id = got_object_id_dup(&delta->base_id);
613 if (m->base_obj_id == NULL)
614 return got_error_from_errno("got_object_id_dup");
616 return add_meta(m, v);
619 static const struct got_error *
620 cache_pack_for_packidx(struct got_pack **pack, struct got_packidx *packidx,
621 struct got_repository *repo)
623 const struct got_error *err;
624 char *path_packfile = NULL;
626 err = got_packidx_get_packfile_path(&path_packfile,
627 packidx->path_packidx);
628 if (err)
629 return err;
631 *pack = got_repo_get_cached_pack(repo, path_packfile);
632 if (*pack == NULL) {
633 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
634 if (err)
635 goto done;
637 if ((*pack)->privsep_child == NULL) {
638 err = got_pack_start_privsep_child(*pack, packidx);
639 if (err)
640 goto done;
642 done:
643 free(path_packfile);
644 return err;
647 static const struct got_error *
648 prepare_delta_reuse(struct got_pack *pack, struct got_packidx *packidx,
649 int delta_outfd, struct got_repository *repo)
651 const struct got_error *err = NULL;
653 if (!pack->child_has_delta_outfd) {
654 int outfd_child;
655 outfd_child = dup(delta_outfd);
656 if (outfd_child == -1) {
657 err = got_error_from_errno("dup");
658 goto done;
660 err = got_privsep_send_raw_delta_outfd(
661 pack->privsep_child->ibuf, outfd_child);
662 if (err)
663 goto done;
664 pack->child_has_delta_outfd = 1;
667 err = got_privsep_send_delta_reuse_req(pack->privsep_child->ibuf);
668 done:
669 return err;
673 static const struct got_error *
674 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
675 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
676 struct got_repository *repo,
677 got_pack_progress_cb progress_cb, void *progress_arg,
678 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
680 const struct got_error *err = NULL;
681 struct got_packidx *packidx;
682 struct got_pack *pack;
683 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
684 size_t ndeltas, i;
686 err = find_pack_for_reuse(&packidx, repo);
687 if (err)
688 return err;
690 if (packidx == NULL)
691 return NULL;
693 err = cache_pack_for_packidx(&pack, packidx, repo);
694 if (err)
695 return err;
697 err = prepare_delta_reuse(pack, packidx, delta_cache_fd, repo);
698 if (err)
699 return err;
701 err = send_idset(pack->privsep_child->ibuf, idset);
702 if (err)
703 return err;
705 for (;;) {
706 int done = 0;
708 if (cancel_cb) {
709 err = (*cancel_cb)(cancel_arg);
710 if (err)
711 break;
714 err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
715 pack->privsep_child->ibuf);
716 if (err || done)
717 break;
719 for (i = 0; i < ndeltas; i++) {
720 struct got_imsg_reused_delta *delta = &deltas[i];
721 err = recv_reused_delta(delta, idset, v);
722 if (err)
723 goto done;
726 err = report_progress(progress_cb, progress_arg, rl,
727 ncolored, nfound, ntrees, 0L, ncommits,
728 got_object_idset_num_elements(idset), v->nmeta, 0);
729 if (err)
730 break;
732 done:
733 return err;
736 static const struct got_error *
737 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
738 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
739 struct got_repository *repo,
740 got_pack_progress_cb progress_cb, void *progress_arg,
741 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
743 const struct got_error *err = NULL;
744 struct got_pack_meta *m = NULL, *base = NULL;
745 struct got_raw_object *raw = NULL, *base_raw = NULL;
746 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
747 int i, j, ndeltas, best_ndeltas;
748 off_t size, best_size;
749 const int max_base_candidates = 3;
750 size_t delta_memsize = 0;
751 const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
752 int outfd = -1;
753 uint32_t delta_seed;
755 delta_seed = arc4random();
757 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
758 for (i = 0; i < nmeta; i++) {
759 if (cancel_cb) {
760 err = (*cancel_cb)(cancel_arg);
761 if (err)
762 break;
764 err = report_progress(progress_cb, progress_arg, rl,
765 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
766 nreused + i, 0);
767 if (err)
768 goto done;
769 m = meta[i];
771 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
772 m->obj_type == GOT_OBJ_TYPE_TAG)
773 continue;
775 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
776 if (err)
777 goto done;
778 m->size = raw->size;
780 if (raw->f == NULL) {
781 err = got_deltify_init_mem(&m->dtab, raw->data,
782 raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
783 } else {
784 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
785 raw->size + raw->hdrlen, delta_seed);
787 if (err)
788 goto done;
790 if (i > max_base_candidates) {
791 struct got_pack_meta *n = NULL;
792 n = meta[i - (max_base_candidates + 1)];
793 got_deltify_free(n->dtab);
794 n->dtab = NULL;
797 best_size = raw->size;
798 best_ndeltas = 0;
799 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
800 if (cancel_cb) {
801 err = (*cancel_cb)(cancel_arg);
802 if (err)
803 goto done;
805 base = meta[j];
806 /* long chains make unpacking slow, avoid such bases */
807 if (base->nchain >= 128 ||
808 base->obj_type != m->obj_type)
809 continue;
811 err = got_object_raw_open(&base_raw, &outfd, repo,
812 &base->id);
813 if (err)
814 goto done;
816 if (raw->f == NULL && base_raw->f == NULL) {
817 err = got_deltify_mem_mem(&deltas, &ndeltas,
818 raw->data, raw->hdrlen,
819 raw->size + raw->hdrlen, delta_seed,
820 base->dtab, base_raw->data,
821 base_raw->hdrlen,
822 base_raw->size + base_raw->hdrlen);
823 } else if (raw->f == NULL) {
824 err = got_deltify_mem_file(&deltas, &ndeltas,
825 raw->data, raw->hdrlen,
826 raw->size + raw->hdrlen, delta_seed,
827 base->dtab, base_raw->f,
828 base_raw->hdrlen,
829 base_raw->size + base_raw->hdrlen);
830 } else if (base_raw->f == NULL) {
831 err = got_deltify_file_mem(&deltas, &ndeltas,
832 raw->f, raw->hdrlen,
833 raw->size + raw->hdrlen, delta_seed,
834 base->dtab, base_raw->data,
835 base_raw->hdrlen,
836 base_raw->size + base_raw->hdrlen);
837 } else {
838 err = got_deltify(&deltas, &ndeltas,
839 raw->f, raw->hdrlen,
840 raw->size + raw->hdrlen, delta_seed,
841 base->dtab, base_raw->f, base_raw->hdrlen,
842 base_raw->size + base_raw->hdrlen);
844 got_object_raw_close(base_raw);
845 base_raw = NULL;
846 if (err)
847 goto done;
849 size = delta_size(deltas, ndeltas);
850 if (size + 32 < best_size){
852 * if we already picked a best delta,
853 * replace it.
855 best_size = size;
856 free(best_deltas);
857 best_deltas = deltas;
858 best_ndeltas = ndeltas;
859 deltas = NULL;
860 m->nchain = base->nchain + 1;
861 m->prev = base;
862 m->head = base->head;
863 if (m->head == NULL)
864 m->head = base;
865 } else {
866 free(deltas);
867 deltas = NULL;
868 ndeltas = 0;
872 if (best_ndeltas > 0) {
873 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
874 delta_memsize + best_size <= max_delta_memsize) {
875 delta_memsize += best_size;
876 err = encode_delta_in_mem(m, raw, best_deltas,
877 best_ndeltas, best_size, m->prev->size);
878 } else {
879 m->delta_offset = ftello(delta_cache);
880 err = encode_delta(m, raw, best_deltas,
881 best_ndeltas, m->prev->size, delta_cache);
883 free(best_deltas);
884 best_deltas = NULL;
885 best_ndeltas = 0;
886 if (err)
887 goto done;
890 got_object_raw_close(raw);
891 raw = NULL;
893 done:
894 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
895 got_deltify_free(meta[i]->dtab);
896 meta[i]->dtab = NULL;
898 if (raw)
899 got_object_raw_close(raw);
900 if (base_raw)
901 got_object_raw_close(base_raw);
902 if (outfd != -1 && close(outfd) == -1 && err == NULL)
903 err = got_error_from_errno("close");
904 free(deltas);
905 free(best_deltas);
906 return err;
909 static const struct got_error *
910 search_packidx(int *found, struct got_object_id *id,
911 struct got_repository *repo)
913 const struct got_error *err = NULL;
914 struct got_packidx *packidx = NULL;
915 int idx;
917 *found = 0;
919 err = got_repo_search_packidx(&packidx, &idx, repo, id);
920 if (err == NULL)
921 *found = 1; /* object is already packed */
922 else if (err->code == GOT_ERR_NO_OBJ)
923 err = NULL;
924 return err;
927 static const struct got_error *
928 add_object(int want_meta, struct got_object_idset *idset,
929 struct got_object_id *id, const char *path, int obj_type,
930 time_t mtime, uint32_t seed, int loose_obj_only,
931 struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
932 got_pack_progress_cb progress_cb, void *progress_arg,
933 struct got_ratelimit *rl)
935 const struct got_error *err;
936 struct got_pack_meta *m = NULL;
938 if (loose_obj_only) {
939 int is_packed;
940 err = search_packidx(&is_packed, id, repo);
941 if (err)
942 return err;
943 if (is_packed && want_meta)
944 return NULL;
947 if (want_meta) {
948 err = alloc_meta(&m, id, path, obj_type, mtime, seed);
949 if (err)
950 return err;
952 (*nfound)++;
953 err = report_progress(progress_cb, progress_arg, rl,
954 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
955 if (err) {
956 clear_meta(m);
957 free(m);
958 return err;
962 err = got_object_idset_add(idset, id, m);
963 if (err) {
964 clear_meta(m);
965 free(m);
967 return err;
970 static const struct got_error *
971 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
972 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
973 struct got_tree_object *tree,
974 const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
975 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
976 got_pack_progress_cb progress_cb, void *progress_arg,
977 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
979 const struct got_error *err;
980 char *p = NULL;
981 int i;
983 (*ntrees)++;
984 err = report_progress(progress_cb, progress_arg, rl,
985 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
986 if (err)
987 return err;
989 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
990 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
991 struct got_object_id *id = got_tree_entry_get_id(e);
992 mode_t mode = got_tree_entry_get_mode(e);
994 if (cancel_cb) {
995 err = (*cancel_cb)(cancel_arg);
996 if (err)
997 break;
1000 if (got_object_tree_entry_is_submodule(e) ||
1001 got_object_idset_contains(idset, id) ||
1002 got_object_idset_contains(idset_exclude, id))
1003 continue;
1006 * If got-read-pack is crawling trees for us then
1007 * we are only here to collect blob IDs.
1009 if (ids == NULL && S_ISDIR(mode))
1010 continue;
1012 if (asprintf(&p, "%s%s%s", dpath,
1013 got_path_is_root_dir(dpath) ? "" : "/",
1014 got_tree_entry_get_name(e)) == -1) {
1015 err = got_error_from_errno("asprintf");
1016 break;
1019 if (S_ISDIR(mode)) {
1020 struct got_object_qid *qid;
1021 err = got_object_qid_alloc(&qid, id);
1022 if (err)
1023 break;
1024 qid->data = p;
1025 p = NULL;
1026 STAILQ_INSERT_TAIL(ids, qid, entry);
1027 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
1028 err = add_object(want_meta,
1029 want_meta ? idset : idset_exclude, id, p,
1030 GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
1031 repo, ncolored, nfound, ntrees,
1032 progress_cb, progress_arg, rl);
1033 if (err)
1034 break;
1035 free(p);
1036 p = NULL;
1037 } else {
1038 free(p);
1039 p = NULL;
1043 free(p);
1044 return err;
1047 static const struct got_error *
1048 load_tree(int want_meta, struct got_object_idset *idset,
1049 struct got_object_idset *idset_exclude,
1050 struct got_object_id *tree_id, const char *dpath, time_t mtime,
1051 uint32_t seed, struct got_repository *repo, int loose_obj_only,
1052 int *ncolored, int *nfound, int *ntrees,
1053 got_pack_progress_cb progress_cb, void *progress_arg,
1054 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1056 const struct got_error *err = NULL;
1057 struct got_object_id_queue tree_ids;
1058 struct got_object_qid *qid;
1059 struct got_tree_object *tree = NULL;
1061 if (got_object_idset_contains(idset, tree_id) ||
1062 got_object_idset_contains(idset_exclude, tree_id))
1063 return NULL;
1065 err = got_object_qid_alloc(&qid, tree_id);
1066 if (err)
1067 return err;
1068 qid->data = strdup(dpath);
1069 if (qid->data == NULL) {
1070 err = got_error_from_errno("strdup");
1071 got_object_qid_free(qid);
1072 return err;
1075 STAILQ_INIT(&tree_ids);
1076 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1078 while (!STAILQ_EMPTY(&tree_ids)) {
1079 const char *path;
1080 if (cancel_cb) {
1081 err = (*cancel_cb)(cancel_arg);
1082 if (err)
1083 break;
1086 qid = STAILQ_FIRST(&tree_ids);
1087 STAILQ_REMOVE_HEAD(&tree_ids, entry);
1088 path = qid->data;
1090 if (got_object_idset_contains(idset, &qid->id) ||
1091 got_object_idset_contains(idset_exclude, &qid->id)) {
1092 free(qid->data);
1093 got_object_qid_free(qid);
1094 continue;
1097 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1098 &qid->id, path, GOT_OBJ_TYPE_TREE,
1099 mtime, seed, loose_obj_only, repo,
1100 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1101 if (err) {
1102 free(qid->data);
1103 got_object_qid_free(qid);
1104 break;
1107 err = got_object_open_as_tree(&tree, repo, &qid->id);
1108 if (err) {
1109 free(qid->data);
1110 got_object_qid_free(qid);
1111 break;
1114 err = load_tree_entries(&tree_ids, want_meta, idset,
1115 idset_exclude, tree, path, mtime, seed, repo,
1116 loose_obj_only, ncolored, nfound, ntrees,
1117 progress_cb, progress_arg, rl,
1118 cancel_cb, cancel_arg);
1119 free(qid->data);
1120 got_object_qid_free(qid);
1121 if (err)
1122 break;
1124 got_object_tree_close(tree);
1125 tree = NULL;
1128 STAILQ_FOREACH(qid, &tree_ids, entry)
1129 free(qid->data);
1130 got_object_id_queue_free(&tree_ids);
1131 if (tree)
1132 got_object_tree_close(tree);
1133 return err;
1136 static const struct got_error *
1137 load_commit(int want_meta, struct got_object_idset *idset,
1138 struct got_object_idset *idset_exclude,
1139 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1140 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1141 got_pack_progress_cb progress_cb, void *progress_arg,
1142 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1144 const struct got_error *err;
1145 struct got_commit_object *commit;
1147 if (got_object_idset_contains(idset, id) ||
1148 got_object_idset_contains(idset_exclude, id))
1149 return NULL;
1151 if (loose_obj_only) {
1152 int is_packed;
1153 err = search_packidx(&is_packed, id, repo);
1154 if (err)
1155 return err;
1156 if (is_packed && want_meta)
1157 return NULL;
1160 err = got_object_open_as_commit(&commit, repo, id);
1161 if (err)
1162 return err;
1164 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1165 id, "", GOT_OBJ_TYPE_COMMIT,
1166 got_object_commit_get_committer_time(commit), seed,
1167 loose_obj_only, repo,
1168 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1169 if (err)
1170 goto done;
1172 err = load_tree(want_meta, idset, idset_exclude,
1173 got_object_commit_get_tree_id(commit),
1174 "", got_object_commit_get_committer_time(commit), seed,
1175 repo, loose_obj_only, ncolored, nfound, ntrees,
1176 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1177 done:
1178 got_object_commit_close(commit);
1179 return err;
1182 static const struct got_error *
1183 load_tag(int want_meta, struct got_object_idset *idset,
1184 struct got_object_idset *idset_exclude,
1185 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1186 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1187 got_pack_progress_cb progress_cb, void *progress_arg,
1188 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1190 const struct got_error *err;
1191 struct got_tag_object *tag = NULL;
1193 if (got_object_idset_contains(idset, id) ||
1194 got_object_idset_contains(idset_exclude, id))
1195 return NULL;
1197 if (loose_obj_only) {
1198 int is_packed;
1199 err = search_packidx(&is_packed, id, repo);
1200 if (err)
1201 return err;
1202 if (is_packed && want_meta)
1203 return NULL;
1206 err = got_object_open_as_tag(&tag, repo, id);
1207 if (err)
1208 return err;
1210 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1211 id, "", GOT_OBJ_TYPE_TAG,
1212 got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1213 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1214 if (err)
1215 goto done;
1217 switch (got_object_tag_get_object_type(tag)) {
1218 case GOT_OBJ_TYPE_COMMIT:
1219 err = load_commit(want_meta, idset, idset_exclude,
1220 got_object_tag_get_object_id(tag), repo, seed,
1221 loose_obj_only, ncolored, nfound, ntrees,
1222 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1223 break;
1224 case GOT_OBJ_TYPE_TREE:
1225 err = load_tree(want_meta, idset, idset_exclude,
1226 got_object_tag_get_object_id(tag), "",
1227 got_object_tag_get_tagger_time(tag), seed, repo,
1228 loose_obj_only, ncolored, nfound, ntrees,
1229 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1230 break;
1231 default:
1232 break;
1235 done:
1236 got_object_tag_close(tag);
1237 return err;
1240 enum findtwixt_color {
1241 COLOR_KEEP = 0,
1242 COLOR_DROP,
1243 COLOR_SKIP,
1244 COLOR_MAX,
1247 static const struct got_error *
1248 paint_commit(struct got_object_qid *qid, intptr_t color)
1250 if (color < 0 || color >= COLOR_MAX)
1251 return got_error(GOT_ERR_RANGE);
1253 qid->data = (void *)color;
1254 return NULL;
1257 static const struct got_error *
1258 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1259 intptr_t color, struct got_repository *repo)
1261 const struct got_error *err;
1262 struct got_object_qid *qid;
1264 err = got_object_qid_alloc(&qid, id);
1265 if (err)
1266 return err;
1268 STAILQ_INSERT_TAIL(ids, qid, entry);
1269 return paint_commit(qid, color);
1272 struct append_id_arg {
1273 struct got_object_id **array;
1274 int idx;
1275 struct got_object_idset *drop;
1276 struct got_object_idset *skip;
1279 static const struct got_error *
1280 append_id(struct got_object_id *id, void *data, void *arg)
1282 struct append_id_arg *a = arg;
1284 if (got_object_idset_contains(a->skip, id) ||
1285 got_object_idset_contains(a->drop, id))
1286 return NULL;
1288 a->array[++a->idx] = got_object_id_dup(id);
1289 if (a->array[a->idx] == NULL)
1290 return got_error_from_errno("got_object_id_dup");
1292 return NULL;
1295 static const struct got_error *
1296 queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1297 struct got_object_id_queue *ids, struct got_repository *repo)
1299 const struct got_error *err;
1300 struct got_tag_object *tag = NULL;
1301 int obj_type;
1303 err = got_object_get_type(&obj_type, repo, id);
1304 if (err)
1305 return err;
1307 if (obj_type == GOT_OBJ_TYPE_TAG) {
1308 err = got_object_open_as_tag(&tag, repo, id);
1309 if (err)
1310 return err;
1311 obj_type = got_object_tag_get_object_type(tag);
1312 id = got_object_tag_get_object_id(tag);
1315 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1316 err = queue_commit_id(ids, id, color, repo);
1317 if (err)
1318 goto done;
1320 done:
1321 if (tag)
1322 got_object_tag_close(tag);
1323 return err;
1326 struct recv_painted_commit_arg {
1327 int *ncolored;
1328 int *nqueued;
1329 int *nskip;
1330 struct got_object_id_queue *ids;
1331 struct got_object_idset *keep;
1332 struct got_object_idset *drop;
1333 struct got_object_idset *skip;
1334 got_pack_progress_cb progress_cb;
1335 void *progress_arg;
1336 struct got_ratelimit *rl;
1337 got_cancel_cb cancel_cb;
1338 void *cancel_arg;
1341 static const struct got_error *
1342 recv_painted_commit(void *arg, struct got_object_id *id, intptr_t color)
1344 const struct got_error *err = NULL;
1345 struct recv_painted_commit_arg *a = arg;
1346 struct got_object_qid *qid, *tmp;
1348 if (a->cancel_cb) {
1349 err = a->cancel_cb(a->cancel_arg);
1350 if (err)
1351 return err;
1354 switch (color) {
1355 case COLOR_KEEP:
1356 err = got_object_idset_add(a->keep, id, NULL);
1357 if (err)
1358 return err;
1359 (*a->ncolored)++;
1360 break;
1361 case COLOR_DROP:
1362 err = got_object_idset_add(a->drop, id, NULL);
1363 if (err)
1364 return err;
1365 (*a->ncolored)++;
1366 break;
1367 case COLOR_SKIP:
1368 err = got_object_idset_add(a->skip, id, NULL);
1369 if (err)
1370 return err;
1371 break;
1372 default:
1373 /* should not happen */
1374 return got_error_fmt(GOT_ERR_NOT_IMPL,
1375 "%s invalid commit color %d", __func__, color);
1378 STAILQ_FOREACH_SAFE(qid, a->ids, entry, tmp) {
1379 if (got_object_id_cmp(&qid->id, id) != 0)
1380 continue;
1381 STAILQ_REMOVE(a->ids, qid, got_object_qid, entry);
1382 color = (intptr_t)qid->data;
1383 got_object_qid_free(qid);
1384 (*a->nqueued)--;
1385 if (color == COLOR_SKIP)
1386 (*a->nskip)--;
1387 break;
1390 return report_progress(a->progress_cb, a->progress_arg, a->rl,
1391 *a->ncolored, 0, 0, 0L, 0, 0, 0, 0);
1394 static const struct got_error *
1395 paint_packed_commits(struct got_pack *pack, struct got_object_id *id,
1396 int idx, intptr_t color, int *ncolored, int *nqueued, int *nskip,
1397 struct got_object_id_queue *ids,
1398 struct got_object_idset *keep, struct got_object_idset *drop,
1399 struct got_object_idset *skip, struct got_repository *repo,
1400 got_pack_progress_cb progress_cb, void *progress_arg,
1401 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1403 const struct got_error *err = NULL;
1404 struct got_object_id_queue next_ids;
1405 struct got_object_qid *qid, *tmp;
1406 struct recv_painted_commit_arg arg;
1408 STAILQ_INIT(&next_ids);
1410 err = got_privsep_send_painting_request(pack->privsep_child->ibuf,
1411 idx, id, color);
1412 if (err)
1413 return err;
1415 arg.ncolored = ncolored;
1416 arg.nqueued = nqueued;
1417 arg.nskip = nskip;
1418 arg.ids = ids;
1419 arg.keep = keep;
1420 arg.drop = drop;
1421 arg.skip = skip;
1422 arg.progress_cb = progress_cb;
1423 arg.progress_arg = progress_arg;
1424 arg.rl = rl;
1425 arg.cancel_cb = cancel_cb;
1426 arg.cancel_arg = cancel_arg;
1427 err = got_privsep_recv_painted_commits(&next_ids,
1428 recv_painted_commit, &arg, pack->privsep_child->ibuf);
1429 if (err)
1430 return err;
1432 STAILQ_FOREACH_SAFE(qid, &next_ids, entry, tmp) {
1433 struct got_object_qid *old_id;
1434 intptr_t qcolor, ocolor;
1435 STAILQ_FOREACH(old_id, ids, entry) {
1436 if (got_object_id_cmp(&qid->id, &old_id->id))
1437 continue;
1438 qcolor = (intptr_t)qid->data;
1439 ocolor = (intptr_t)old_id->data;
1440 STAILQ_REMOVE(&next_ids, qid, got_object_qid, entry);
1441 got_object_qid_free(qid);
1442 qid = NULL;
1443 if (qcolor != ocolor) {
1444 paint_commit(old_id, qcolor);
1445 if (ocolor == COLOR_SKIP)
1446 (*nskip)--;
1447 else if (qcolor == COLOR_SKIP)
1448 (*nskip)++;
1450 break;
1453 while (!STAILQ_EMPTY(&next_ids)) {
1454 qid = STAILQ_FIRST(&next_ids);
1455 STAILQ_REMOVE_HEAD(&next_ids, entry);
1456 paint_commit(qid, color);
1457 STAILQ_INSERT_TAIL(ids, qid, entry);
1458 (*nqueued)++;
1459 if (color == COLOR_SKIP)
1460 (*nskip)++;
1463 return err;
1466 static const struct got_error *
1467 find_pack_for_commit_painting(struct got_packidx **best_packidx,
1468 struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1470 const struct got_error *err = NULL;
1471 struct got_pathlist_entry *pe;
1472 const char *best_packidx_path = NULL;
1473 int nobj_max = 0;
1474 int ncommits_max = 0;
1476 *best_packidx = NULL;
1479 * Find the largest pack which contains at least some of the
1480 * commits we are interested in.
1482 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1483 const char *path_packidx = pe->path;
1484 struct got_packidx *packidx;
1485 int nobj, idx, ncommits = 0;
1486 struct got_object_qid *qid;
1488 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1489 if (err)
1490 break;
1492 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1493 if (nobj <= nobj_max)
1494 continue;
1496 STAILQ_FOREACH(qid, ids, entry) {
1497 idx = got_packidx_get_object_idx(packidx, &qid->id);
1498 if (idx != -1)
1499 ncommits++;
1501 if (ncommits > ncommits_max) {
1502 best_packidx_path = path_packidx;
1503 nobj_max = nobj;
1504 ncommits_max = ncommits;
1508 if (best_packidx_path && err == NULL) {
1509 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1510 repo);
1513 return err;
1516 static const struct got_error *
1517 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1518 struct got_object_idset *keep, struct got_object_idset *drop,
1519 struct got_object_idset *skip, struct got_repository *repo,
1520 got_pack_progress_cb progress_cb, void *progress_arg,
1521 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1523 const struct got_error *err = NULL;
1524 struct got_commit_object *commit = NULL;
1525 struct got_packidx *packidx = NULL;
1526 struct got_pack *pack = NULL;
1527 const struct got_object_id_queue *parents;
1528 struct got_object_qid *qid = NULL;
1529 int nqueued = nids, nskip = 0;
1530 int idx;
1532 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1533 intptr_t color;
1535 if (cancel_cb) {
1536 err = cancel_cb(cancel_arg);
1537 if (err)
1538 break;
1541 qid = STAILQ_FIRST(ids);
1542 STAILQ_REMOVE_HEAD(ids, entry);
1543 nqueued--;
1544 color = (intptr_t)qid->data;
1545 if (color == COLOR_SKIP)
1546 nskip--;
1548 if (got_object_idset_contains(skip, &qid->id)) {
1549 got_object_qid_free(qid);
1550 qid = NULL;
1551 continue;
1553 if (color == COLOR_KEEP &&
1554 got_object_idset_contains(keep, &qid->id)) {
1555 got_object_qid_free(qid);
1556 qid = NULL;
1557 continue;
1559 if (color == COLOR_DROP &&
1560 got_object_idset_contains(drop, &qid->id)) {
1561 got_object_qid_free(qid);
1562 qid = NULL;
1563 continue;
1566 /* Pinned pack may have moved to different cache slot. */
1567 pack = got_repo_get_pinned_pack(repo);
1569 if (packidx && pack) {
1570 idx = got_packidx_get_object_idx(packidx, &qid->id);
1571 if (idx != -1) {
1572 err = paint_packed_commits(pack, &qid->id,
1573 idx, color, ncolored, &nqueued, &nskip,
1574 ids, keep, drop, skip, repo,
1575 progress_cb, progress_arg, rl,
1576 cancel_cb, cancel_arg);
1577 if (err)
1578 break;
1579 got_object_qid_free(qid);
1580 qid = NULL;
1581 continue;
1585 switch (color) {
1586 case COLOR_KEEP:
1587 if (got_object_idset_contains(drop, &qid->id)) {
1588 err = paint_commit(qid, COLOR_SKIP);
1589 if (err)
1590 goto done;
1591 } else
1592 (*ncolored)++;
1593 err = got_object_idset_add(keep, &qid->id, NULL);
1594 if (err)
1595 goto done;
1596 break;
1597 case COLOR_DROP:
1598 if (got_object_idset_contains(keep, &qid->id)) {
1599 err = paint_commit(qid, COLOR_SKIP);
1600 if (err)
1601 goto done;
1602 } else
1603 (*ncolored)++;
1604 err = got_object_idset_add(drop, &qid->id, NULL);
1605 if (err)
1606 goto done;
1607 break;
1608 case COLOR_SKIP:
1609 if (!got_object_idset_contains(skip, &qid->id)) {
1610 err = got_object_idset_add(skip, &qid->id,
1611 NULL);
1612 if (err)
1613 goto done;
1615 break;
1616 default:
1617 /* should not happen */
1618 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1619 "%s invalid commit color %d", __func__, color);
1620 goto done;
1623 err = report_progress(progress_cb, progress_arg, rl,
1624 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1625 if (err)
1626 break;
1628 err = got_object_open_as_commit(&commit, repo, &qid->id);
1629 if (err)
1630 break;
1632 parents = got_object_commit_get_parent_ids(commit);
1633 if (parents) {
1634 struct got_object_qid *pid;
1635 color = (intptr_t)qid->data;
1636 STAILQ_FOREACH(pid, parents, entry) {
1637 err = queue_commit_id(ids, &pid->id,
1638 color, repo);
1639 if (err)
1640 break;
1641 nqueued++;
1642 if (color == COLOR_SKIP)
1643 nskip++;
1647 if (pack == NULL && (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
1648 if (packidx == NULL) {
1649 err = find_pack_for_commit_painting(&packidx,
1650 ids, nqueued, repo);
1651 if (err)
1652 goto done;
1654 if (packidx != NULL) {
1655 err = cache_pack_for_packidx(&pack, packidx,
1656 repo);
1657 if (err)
1658 goto done;
1659 err = got_privsep_init_commit_painting(
1660 pack->privsep_child->ibuf);
1661 if (err)
1662 goto done;
1663 err = send_idset(pack->privsep_child->ibuf,
1664 keep);
1665 if (err)
1666 goto done;
1667 err = send_idset(pack->privsep_child->ibuf, drop);
1668 if (err)
1669 goto done;
1670 err = send_idset(pack->privsep_child->ibuf, skip);
1671 if (err)
1672 goto done;
1673 err = got_repo_pin_pack(repo, packidx, pack);
1674 if (err)
1675 goto done;
1679 got_object_commit_close(commit);
1680 commit = NULL;
1682 got_object_qid_free(qid);
1683 qid = NULL;
1685 done:
1686 if (pack) {
1687 const struct got_error *pack_err;
1688 pack_err = got_privsep_send_painting_commits_done(
1689 pack->privsep_child->ibuf);
1690 if (err == NULL)
1691 err = pack_err;
1693 if (commit)
1694 got_object_commit_close(commit);
1695 got_object_qid_free(qid);
1696 got_repo_unpin_pack(repo);
1697 return err;
1700 static const struct got_error *
1701 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1702 struct got_object_id **head, int nhead,
1703 struct got_object_id **tail, int ntail,
1704 struct got_repository *repo,
1705 got_pack_progress_cb progress_cb, void *progress_arg,
1706 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1708 const struct got_error *err = NULL;
1709 struct got_object_id_queue ids;
1710 struct got_object_idset *keep, *drop, *skip = NULL;
1711 int i, nkeep;
1713 STAILQ_INIT(&ids);
1714 *res = NULL;
1715 *nres = 0;
1716 *ncolored = 0;
1718 keep = got_object_idset_alloc();
1719 if (keep == NULL)
1720 return got_error_from_errno("got_object_idset_alloc");
1722 drop = got_object_idset_alloc();
1723 if (drop == NULL) {
1724 err = got_error_from_errno("got_object_idset_alloc");
1725 goto done;
1728 skip = got_object_idset_alloc();
1729 if (skip == NULL) {
1730 err = got_error_from_errno("got_object_idset_alloc");
1731 goto done;
1734 for (i = 0; i < nhead; i++) {
1735 struct got_object_id *id = head[i];
1736 if (id == NULL)
1737 continue;
1738 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1739 if (err)
1740 goto done;
1743 for (i = 0; i < ntail; i++) {
1744 struct got_object_id *id = tail[i];
1745 if (id == NULL)
1746 continue;
1747 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1748 if (err)
1749 goto done;
1752 err = paint_commits(ncolored, &ids, nhead + ntail,
1753 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1754 cancel_cb, cancel_arg);
1755 if (err)
1756 goto done;
1758 nkeep = got_object_idset_num_elements(keep);
1759 if (nkeep > 0) {
1760 struct append_id_arg arg;
1761 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1762 if (arg.array == NULL) {
1763 err = got_error_from_errno("calloc");
1764 goto done;
1766 arg.idx = -1;
1767 arg.skip = skip;
1768 arg.drop = drop;
1769 err = got_object_idset_for_each(keep, append_id, &arg);
1770 if (err) {
1771 free(arg.array);
1772 goto done;
1774 *res = arg.array;
1775 *nres = arg.idx + 1;
1777 done:
1778 got_object_idset_free(keep);
1779 got_object_idset_free(drop);
1780 if (skip)
1781 got_object_idset_free(skip);
1782 got_object_id_queue_free(&ids);
1783 return err;
1786 struct load_packed_obj_arg {
1787 /* output parameters: */
1788 struct got_object_id *id;
1789 char *dpath;
1790 time_t mtime;
1792 /* input parameters: */
1793 uint32_t seed;
1794 int want_meta;
1795 struct got_object_idset *idset;
1796 struct got_object_idset *idset_exclude;
1797 int loose_obj_only;
1798 int *ncolored;
1799 int *nfound;
1800 int *ntrees;
1801 got_pack_progress_cb progress_cb;
1802 void *progress_arg;
1803 struct got_ratelimit *rl;
1804 got_cancel_cb cancel_cb;
1805 void *cancel_arg;
1808 static const struct got_error *
1809 load_packed_commit_id(void *arg, time_t mtime, struct got_object_id *id,
1810 struct got_repository *repo)
1812 struct load_packed_obj_arg *a = arg;
1814 if (got_object_idset_contains(a->idset, id) ||
1815 got_object_idset_contains(a->idset_exclude, id))
1816 return NULL;
1818 return add_object(a->want_meta,
1819 a->want_meta ? a->idset : a->idset_exclude,
1820 id, "", GOT_OBJ_TYPE_COMMIT, mtime, a->seed, a->loose_obj_only,
1821 repo, a->ncolored, a->nfound, a->ntrees,
1822 a->progress_cb, a->progress_arg, a->rl);
1825 static const struct got_error *
1826 load_packed_tree_ids(void *arg, struct got_tree_object *tree, time_t mtime,
1827 struct got_object_id *id, const char *dpath, struct got_repository *repo)
1829 const struct got_error *err;
1830 struct load_packed_obj_arg *a = arg;
1831 const char *relpath;
1834 * When we receive a tree's ID and path but not the tree itself,
1835 * this tree object was not found in the pack file. This is the
1836 * last time we are being called for this optimized traversal.
1837 * Return from here and switch to loading objects the slow way.
1839 if (tree == NULL) {
1840 free(a->id);
1841 a->id = got_object_id_dup(id);
1842 if (a->id == NULL) {
1843 err = got_error_from_errno("got_object_id_dup");
1844 free(a->dpath);
1845 a->dpath = NULL;
1846 return err;
1849 free(a->dpath);
1850 a->dpath = strdup(dpath);
1851 if (a->dpath == NULL) {
1852 err = got_error_from_errno("strdup");
1853 free(a->id);
1854 a->id = NULL;
1855 return err;
1858 a->mtime = mtime;
1859 return NULL;
1862 if (got_object_idset_contains(a->idset, id) ||
1863 got_object_idset_contains(a->idset_exclude, id))
1864 return NULL;
1866 relpath = dpath;
1867 while (relpath[0] == '/')
1868 relpath++;
1870 err = add_object(a->want_meta,
1871 a->want_meta ? a->idset : a->idset_exclude,
1872 id, relpath, GOT_OBJ_TYPE_TREE, mtime, a->seed,
1873 a->loose_obj_only, repo, a->ncolored, a->nfound, a->ntrees,
1874 a->progress_cb, a->progress_arg, a->rl);
1875 if (err)
1876 return err;
1878 return load_tree_entries(NULL, a->want_meta, a->idset,
1879 a->idset_exclude, tree, dpath, mtime, a->seed, repo,
1880 a->loose_obj_only, a->ncolored, a->nfound, a->ntrees,
1881 a->progress_cb, a->progress_arg, a->rl,
1882 a->cancel_cb, a->cancel_arg);
1885 static const struct got_error *
1886 load_packed_object_ids(int *found_all_objects,
1887 struct got_object_id **ours, int nours,
1888 struct got_object_id **theirs, int ntheirs,
1889 int want_meta, uint32_t seed, struct got_object_idset *idset,
1890 struct got_object_idset *idset_exclude, int loose_obj_only,
1891 struct got_repository *repo, struct got_packidx *packidx,
1892 int *ncolored, int *nfound, int *ntrees,
1893 got_pack_progress_cb progress_cb, void *progress_arg,
1894 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1896 const struct got_error *err = NULL;
1897 struct load_packed_obj_arg lpa;
1899 memset(&lpa, 0, sizeof(lpa));
1900 lpa.seed = seed;
1901 lpa.want_meta = want_meta;
1902 lpa.idset = idset;
1903 lpa.idset_exclude = idset_exclude;
1904 lpa.loose_obj_only = loose_obj_only;
1905 lpa.ncolored = ncolored;
1906 lpa.nfound = nfound;
1907 lpa.ntrees = ntrees;
1908 lpa.progress_cb = progress_cb;
1909 lpa.progress_arg = progress_arg;
1910 lpa.rl = rl;
1911 lpa.cancel_cb = cancel_cb;
1912 lpa.cancel_arg = cancel_arg;
1914 /* Attempt to load objects via got-read-pack, as far as possible. */
1915 err = got_object_enumerate(found_all_objects, load_packed_commit_id,
1916 load_packed_tree_ids, &lpa, ours, nours, theirs, ntheirs,
1917 packidx, repo);
1918 if (err)
1919 return err;
1921 if (lpa.id == NULL)
1922 return NULL;
1925 * An incomplete tree hierarchy was present in the pack file
1926 * and caused loading to be aborted.
1927 * Continue loading trees the slow way.
1929 err = load_tree(want_meta, idset, idset_exclude,
1930 lpa.id, lpa.dpath, lpa.mtime, seed, repo, loose_obj_only,
1931 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1932 cancel_cb, cancel_arg);
1933 free(lpa.id);
1934 free(lpa.dpath);
1935 return err;
1938 static const struct got_error *
1939 find_pack_for_enumeration(struct got_packidx **best_packidx,
1940 struct got_object_id **ids, int nids, struct got_repository *repo)
1942 const struct got_error *err = NULL;
1943 struct got_pathlist_entry *pe;
1944 const char *best_packidx_path = NULL;
1945 int nobj_max = 0;
1946 int ncommits_max = 0;
1948 *best_packidx = NULL;
1951 * Find the largest pack which contains at least some of the
1952 * commits and tags we are interested in.
1954 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1955 const char *path_packidx = pe->path;
1956 struct got_packidx *packidx;
1957 int nobj, i, idx, ncommits = 0;
1959 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1960 if (err)
1961 break;
1963 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1964 if (nobj <= nobj_max)
1965 continue;
1967 for (i = 0; i < nids; i++) {
1968 idx = got_packidx_get_object_idx(packidx, ids[i]);
1969 if (idx != -1)
1970 ncommits++;
1972 if (ncommits > ncommits_max) {
1973 best_packidx_path = path_packidx;
1974 nobj_max = nobj;
1975 ncommits_max = ncommits;
1979 if (best_packidx_path && err == NULL) {
1980 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1981 repo);
1984 return err;
1987 static const struct got_error *
1988 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1989 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1990 struct got_object_id **ours, int nours, struct got_repository *repo,
1991 uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1992 void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1993 void *cancel_arg)
1995 const struct got_error *err = NULL;
1996 struct got_object_id **ids = NULL;
1997 struct got_packidx *packidx = NULL;
1998 int i, nobj = 0, obj_type, found_all_objects = 0;
1999 struct got_object_idset *idset_exclude;
2001 idset_exclude = got_object_idset_alloc();
2002 if (idset_exclude == NULL)
2003 return got_error_from_errno("got_object_idset_alloc");
2005 *ncolored = 0;
2006 *nfound = 0;
2007 *ntrees = 0;
2009 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
2010 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2011 if (err)
2012 goto done;
2014 err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
2015 if (err)
2016 goto done;
2017 if (packidx) {
2018 err = load_packed_object_ids(&found_all_objects,
2019 theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
2020 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
2021 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2022 if (err)
2023 goto done;
2026 for (i = 0; i < ntheirs; i++) {
2027 struct got_object_id *id = theirs[i];
2028 if (id == NULL)
2029 continue;
2030 err = got_object_get_type(&obj_type, repo, id);
2031 if (err)
2032 return err;
2033 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
2034 if (!found_all_objects) {
2035 err = load_commit(0, idset, idset_exclude,
2036 id, repo, seed, loose_obj_only,
2037 ncolored, nfound, ntrees,
2038 progress_cb, progress_arg, rl,
2039 cancel_cb, cancel_arg);
2040 if (err)
2041 goto done;
2043 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
2044 err = load_tag(0, idset, idset_exclude, id, repo,
2045 seed, loose_obj_only, ncolored, nfound, ntrees,
2046 progress_cb, progress_arg, rl,
2047 cancel_cb, cancel_arg);
2048 if (err)
2049 goto done;
2053 found_all_objects = 0;
2054 err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
2055 if (err)
2056 goto done;
2057 if (packidx) {
2058 err = load_packed_object_ids(&found_all_objects, ids,
2059 nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
2060 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
2061 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2062 if (err)
2063 goto done;
2066 if (!found_all_objects) {
2067 for (i = 0; i < nobj; i++) {
2068 err = load_commit(1, idset, idset_exclude, ids[i],
2069 repo, seed, loose_obj_only, ncolored, nfound,
2070 ntrees, progress_cb, progress_arg, rl,
2071 cancel_cb, cancel_arg);
2072 if (err)
2073 goto done;
2077 for (i = 0; i < nours; i++) {
2078 struct got_object_id *id = ours[i];
2079 struct got_pack_meta *m;
2080 if (id == NULL)
2081 continue;
2082 m = got_object_idset_get(idset, id);
2083 if (m == NULL) {
2084 err = got_object_get_type(&obj_type, repo, id);
2085 if (err)
2086 goto done;
2087 } else
2088 obj_type = m->obj_type;
2089 if (obj_type != GOT_OBJ_TYPE_TAG)
2090 continue;
2091 err = load_tag(1, idset, idset_exclude, id, repo,
2092 seed, loose_obj_only, ncolored, nfound, ntrees,
2093 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2094 if (err)
2095 goto done;
2097 done:
2098 for (i = 0; i < nobj; i++) {
2099 free(ids[i]);
2101 free(ids);
2102 got_object_idset_free(idset_exclude);
2103 return err;
2106 static const struct got_error *
2107 hwrite(FILE *f, const void *buf, off_t len, SHA1_CTX *ctx)
2109 size_t n;
2111 SHA1Update(ctx, buf, len);
2112 n = fwrite(buf, 1, len, f);
2113 if (n != len)
2114 return got_ferror(f, GOT_ERR_IO);
2115 return NULL;
2118 static const struct got_error *
2119 hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
2121 unsigned char buf[65536];
2122 off_t remain = len;
2123 size_t n;
2125 while (remain > 0) {
2126 size_t copylen = MIN(sizeof(buf), remain);
2127 n = fread(buf, 1, copylen, fsrc);
2128 if (n != copylen)
2129 return got_ferror(fsrc, GOT_ERR_IO);
2130 SHA1Update(ctx, buf, copylen);
2131 n = fwrite(buf, 1, copylen, fdst);
2132 if (n != copylen)
2133 return got_ferror(fdst, GOT_ERR_IO);
2134 remain -= copylen;
2137 return NULL;
2140 static const struct got_error *
2141 hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
2142 FILE *fdst, off_t len, SHA1_CTX *ctx)
2144 size_t n;
2146 if (src_offset + len > src_size)
2147 return got_error(GOT_ERR_RANGE);
2149 SHA1Update(ctx, src + src_offset, len);
2150 n = fwrite(src + src_offset, 1, len, fdst);
2151 if (n != len)
2152 return got_ferror(fdst, GOT_ERR_IO);
2154 return NULL;
2157 static void
2158 putbe32(char *b, uint32_t n)
2160 b[0] = n >> 24;
2161 b[1] = n >> 16;
2162 b[2] = n >> 8;
2163 b[3] = n >> 0;
2166 static int
2167 write_order_cmp(const void *pa, const void *pb)
2169 struct got_pack_meta *a, *b, *ahd, *bhd;
2171 a = *(struct got_pack_meta **)pa;
2172 b = *(struct got_pack_meta **)pb;
2173 ahd = (a->head == NULL) ? a : a->head;
2174 bhd = (b->head == NULL) ? b : b->head;
2175 if (bhd->mtime < ahd->mtime)
2176 return -1;
2177 if (bhd->mtime > ahd->mtime)
2178 return 1;
2179 if (bhd < ahd)
2180 return -1;
2181 if (bhd > ahd)
2182 return 1;
2183 if (a->nchain != b->nchain)
2184 return a->nchain - b->nchain;
2185 if (a->mtime < b->mtime)
2186 return -1;
2187 if (a->mtime > b->mtime)
2188 return 1;
2189 return got_object_id_cmp(&a->id, &b->id);
2192 static int
2193 reuse_write_order_cmp(const void *pa, const void *pb)
2195 struct got_pack_meta *a, *b;
2197 a = *(struct got_pack_meta **)pa;
2198 b = *(struct got_pack_meta **)pb;
2200 if (a->reused_delta_offset < b->reused_delta_offset)
2201 return -1;
2202 if (a->reused_delta_offset > b->reused_delta_offset)
2203 return 1;
2204 return 0;
2207 static const struct got_error *
2208 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
2210 size_t i;
2212 *hdrlen = 0;
2214 hdr[0] = obj_type << 4;
2215 hdr[0] |= len & 0xf;
2216 len >>= 4;
2217 for (i = 1; len != 0; i++){
2218 if (i >= bufsize)
2219 return got_error(GOT_ERR_NO_SPACE);
2220 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
2221 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
2222 len >>= GOT_DELTA_SIZE_SHIFT;
2225 *hdrlen = i;
2226 return NULL;
2229 static int
2230 packoff(char *hdr, off_t off)
2232 int i, j;
2233 char rbuf[8];
2235 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
2236 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
2237 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
2238 GOT_DELTA_SIZE_MORE;
2241 j = 0;
2242 while (i > 0)
2243 hdr[j++] = rbuf[--i];
2244 return j;
2247 static const struct got_error *
2248 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
2249 struct got_pack_meta *m)
2251 const struct got_error *err;
2252 char buf[32];
2253 int nh;
2255 if (m->prev->off != 0) {
2256 err = packhdr(&nh, buf, sizeof(buf),
2257 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
2258 if (err)
2259 return err;
2260 nh += packoff(buf + nh, m->off - m->prev->off);
2261 err = hwrite(packfile, buf, nh, ctx);
2262 if (err)
2263 return err;
2264 *packfile_size += nh;
2265 } else {
2266 err = packhdr(&nh, buf, sizeof(buf),
2267 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
2268 if (err)
2269 return err;
2270 err = hwrite(packfile, buf, nh, ctx);
2271 if (err)
2272 return err;
2273 *packfile_size += nh;
2274 err = hwrite(packfile, m->prev->id.sha1,
2275 sizeof(m->prev->id.sha1), ctx);
2276 if (err)
2277 return err;
2278 *packfile_size += sizeof(m->prev->id.sha1);
2281 return NULL;
2284 static const struct got_error *
2285 write_packed_object(off_t *packfile_size, FILE *packfile,
2286 FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
2287 struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
2288 struct got_repository *repo)
2290 const struct got_error *err = NULL;
2291 struct got_deflate_checksum csum;
2292 char buf[32];
2293 int nh;
2294 struct got_raw_object *raw = NULL;
2295 off_t outlen;
2297 csum.output_sha1 = ctx;
2298 csum.output_crc = NULL;
2300 m->off = ftello(packfile);
2301 if (m->delta_len == 0) {
2302 err = got_object_raw_open(&raw, outfd, repo, &m->id);
2303 if (err)
2304 goto done;
2305 err = packhdr(&nh, buf, sizeof(buf),
2306 m->obj_type, raw->size);
2307 if (err)
2308 goto done;
2309 err = hwrite(packfile, buf, nh, ctx);
2310 if (err)
2311 goto done;
2312 *packfile_size += nh;
2313 if (raw->f == NULL) {
2314 err = got_deflate_to_file_mmap(&outlen,
2315 raw->data + raw->hdrlen, 0, raw->size,
2316 packfile, &csum);
2317 if (err)
2318 goto done;
2319 } else {
2320 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
2321 == -1) {
2322 err = got_error_from_errno("fseeko");
2323 goto done;
2325 err = got_deflate_to_file(&outlen, raw->f,
2326 raw->size, packfile, &csum);
2327 if (err)
2328 goto done;
2330 *packfile_size += outlen;
2331 got_object_raw_close(raw);
2332 raw = NULL;
2333 } else if (m->delta_buf) {
2334 err = deltahdr(packfile_size, ctx, packfile, m);
2335 if (err)
2336 goto done;
2337 err = hwrite(packfile, m->delta_buf,
2338 m->delta_compressed_len, ctx);
2339 if (err)
2340 goto done;
2341 *packfile_size += m->delta_compressed_len;
2342 free(m->delta_buf);
2343 m->delta_buf = NULL;
2344 } else if (delta_cache_map) {
2345 err = deltahdr(packfile_size, ctx, packfile, m);
2346 if (err)
2347 goto done;
2348 err = hcopy_mmap(delta_cache_map, m->delta_offset,
2349 delta_cache_size, packfile, m->delta_compressed_len,
2350 ctx);
2351 if (err)
2352 goto done;
2353 *packfile_size += m->delta_compressed_len;
2354 } else {
2355 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
2356 == -1) {
2357 err = got_error_from_errno("fseeko");
2358 goto done;
2360 err = deltahdr(packfile_size, ctx, packfile, m);
2361 if (err)
2362 goto done;
2363 err = hcopy(delta_cache, packfile,
2364 m->delta_compressed_len, ctx);
2365 if (err)
2366 goto done;
2367 *packfile_size += m->delta_compressed_len;
2369 done:
2370 if (raw)
2371 got_object_raw_close(raw);
2372 return err;
2375 static const struct got_error *
2376 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
2377 struct got_pack_meta **deltify, int ndeltify,
2378 struct got_pack_meta **reuse, int nreuse,
2379 int ncolored, int nfound, int ntrees, int nours,
2380 struct got_repository *repo,
2381 got_pack_progress_cb progress_cb, void *progress_arg,
2382 struct got_ratelimit *rl,
2383 got_cancel_cb cancel_cb, void *cancel_arg)
2385 const struct got_error *err = NULL;
2386 int i;
2387 SHA1_CTX ctx;
2388 struct got_pack_meta *m;
2389 char buf[32];
2390 size_t n;
2391 off_t packfile_size = 0;
2392 int outfd = -1;
2393 int delta_cache_fd = -1;
2394 uint8_t *delta_cache_map = NULL;
2395 size_t delta_cache_size = 0;
2397 SHA1Init(&ctx);
2399 #ifndef GOT_PACK_NO_MMAP
2400 delta_cache_fd = dup(fileno(delta_cache));
2401 if (delta_cache_fd != -1) {
2402 struct stat sb;
2403 if (fstat(delta_cache_fd, &sb) == -1) {
2404 err = got_error_from_errno("fstat");
2405 goto done;
2407 if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
2408 delta_cache_map = mmap(NULL, sb.st_size,
2409 PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
2410 if (delta_cache_map == MAP_FAILED) {
2411 if (errno != ENOMEM) {
2412 err = got_error_from_errno("mmap");
2413 goto done;
2415 delta_cache_map = NULL; /* fallback on stdio */
2416 } else
2417 delta_cache_size = (size_t)sb.st_size;
2420 #endif
2421 err = hwrite(packfile, "PACK", 4, &ctx);
2422 if (err)
2423 goto done;
2424 putbe32(buf, GOT_PACKFILE_VERSION);
2425 err = hwrite(packfile, buf, 4, &ctx);
2426 if (err)
2427 goto done;
2428 putbe32(buf, ndeltify + nreuse);
2429 err = hwrite(packfile, buf, 4, &ctx);
2430 if (err)
2431 goto done;
2433 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
2434 write_order_cmp);
2435 for (i = 0; i < ndeltify; i++) {
2436 err = report_progress(progress_cb, progress_arg, rl,
2437 ncolored, nfound, ntrees, packfile_size, nours,
2438 ndeltify + nreuse, ndeltify + nreuse, i);
2439 if (err)
2440 goto done;
2441 m = deltify[i];
2442 err = write_packed_object(&packfile_size, packfile,
2443 delta_cache, delta_cache_map, delta_cache_size,
2444 m, &outfd, &ctx, repo);
2445 if (err)
2446 goto done;
2449 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
2450 reuse_write_order_cmp);
2451 for (i = 0; i < nreuse; i++) {
2452 err = report_progress(progress_cb, progress_arg, rl,
2453 ncolored, nfound, ntrees, packfile_size, nours,
2454 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
2455 if (err)
2456 goto done;
2457 m = reuse[i];
2458 err = write_packed_object(&packfile_size, packfile,
2459 delta_cache, delta_cache_map, delta_cache_size,
2460 m, &outfd, &ctx, repo);
2461 if (err)
2462 goto done;
2465 SHA1Final(pack_sha1, &ctx);
2466 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
2467 if (n != SHA1_DIGEST_LENGTH)
2468 err = got_ferror(packfile, GOT_ERR_IO);
2469 packfile_size += SHA1_DIGEST_LENGTH;
2470 packfile_size += sizeof(struct got_packfile_hdr);
2471 if (progress_cb) {
2472 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2473 packfile_size, nours, ndeltify + nreuse,
2474 ndeltify + nreuse, ndeltify + nreuse);
2475 if (err)
2476 goto done;
2478 done:
2479 if (outfd != -1 && close(outfd) == -1 && err == NULL)
2480 err = got_error_from_errno("close");
2481 if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
2482 err = got_error_from_errno("munmap");
2483 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2484 err = got_error_from_errno("close");
2485 return err;
2488 static const struct got_error *
2489 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
2491 struct got_pack_meta *m = data;
2492 struct got_pack_metavec *v = arg;
2494 if (m->reused_delta_offset != 0)
2495 return NULL;
2497 return add_meta(m, v);
2500 const struct got_error *
2501 got_pack_create(uint8_t *packsha1, FILE *packfile,
2502 struct got_object_id **theirs, int ntheirs,
2503 struct got_object_id **ours, int nours,
2504 struct got_repository *repo, int loose_obj_only, int allow_empty,
2505 got_pack_progress_cb progress_cb, void *progress_arg,
2506 got_cancel_cb cancel_cb, void *cancel_arg)
2508 const struct got_error *err;
2509 int delta_cache_fd = -1;
2510 FILE *delta_cache = NULL;
2511 struct got_object_idset *idset;
2512 struct got_ratelimit rl;
2513 struct got_pack_metavec deltify, reuse;
2514 int ncolored = 0, nfound = 0, ntrees = 0;
2515 size_t ndeltify;
2516 uint32_t seed;
2518 seed = arc4random();
2520 memset(&deltify, 0, sizeof(deltify));
2521 memset(&reuse, 0, sizeof(reuse));
2523 got_ratelimit_init(&rl, 0, 500);
2525 idset = got_object_idset_alloc();
2526 if (idset == NULL)
2527 return got_error_from_errno("got_object_idset_alloc");
2529 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
2530 ntheirs, ours, nours, repo, seed, loose_obj_only,
2531 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
2532 if (err)
2533 goto done;
2535 if (progress_cb) {
2536 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2537 0L, nours, got_object_idset_num_elements(idset), 0, 0);
2538 if (err)
2539 goto done;
2542 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
2543 err = got_error(GOT_ERR_CANNOT_PACK);
2544 goto done;
2547 delta_cache_fd = got_opentempfd();
2548 if (delta_cache_fd == -1) {
2549 err = got_error_from_errno("got_opentemp");
2550 goto done;
2553 reuse.metasz = 64;
2554 reuse.meta = calloc(reuse.metasz,
2555 sizeof(struct got_pack_meta *));
2556 if (reuse.meta == NULL) {
2557 err = got_error_from_errno("calloc");
2558 goto done;
2561 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
2562 ntrees, nours, repo, progress_cb, progress_arg, &rl,
2563 cancel_cb, cancel_arg);
2564 if (err)
2565 goto done;
2567 delta_cache = fdopen(delta_cache_fd, "a+");
2568 if (delta_cache == NULL) {
2569 err = got_error_from_errno("fdopen");
2570 goto done;
2572 delta_cache_fd = -1;
2574 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
2575 err = got_error_from_errno("fseeko");
2576 goto done;
2579 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
2580 if (ndeltify > 0) {
2581 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
2582 if (deltify.meta == NULL) {
2583 err = got_error_from_errno("calloc");
2584 goto done;
2586 deltify.metasz = ndeltify;
2588 err = got_object_idset_for_each(idset, add_meta_idset_cb,
2589 &deltify);
2590 if (err)
2591 goto done;
2592 if (deltify.nmeta > 0) {
2593 err = pick_deltas(deltify.meta, deltify.nmeta,
2594 ncolored, nfound, ntrees, nours, reuse.nmeta,
2595 delta_cache, repo, progress_cb, progress_arg, &rl,
2596 cancel_cb, cancel_arg);
2597 if (err)
2598 goto done;
2602 if (fflush(delta_cache) == EOF) {
2603 err = got_error_from_errno("fflush");
2604 goto done;
2606 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
2607 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
2608 nours, repo, progress_cb, progress_arg, &rl,
2609 cancel_cb, cancel_arg);
2610 if (err)
2611 goto done;
2612 done:
2613 free_nmeta(deltify.meta, deltify.nmeta);
2614 free_nmeta(reuse.meta, reuse.nmeta);
2615 got_object_idset_free(idset);
2616 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2617 err = got_error_from_errno("close");
2618 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2619 err = got_error_from_errno("fclose");
2620 return err;