Blob


1 /*
2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
24 #include <sys/mman.h>
26 #include <endian.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <imsg.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sha1.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"
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;
98 };
100 struct got_pack_metavec {
101 struct got_pack_meta **meta;
102 int nmeta;
103 int metasz;
104 };
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;
545 };
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 recv_reused_delta(struct got_imsg_reused_delta *delta,
567 struct got_object_idset *idset, struct got_pack_metavec *v)
569 struct got_pack_meta *m, *base;
571 if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
572 delta->delta_offset +
573 delta->delta_compressed_size < delta->delta_offset)
574 return got_error(GOT_ERR_BAD_PACKFILE);
576 m = got_object_idset_get(idset, &delta->id);
577 if (m == NULL)
578 return got_error(GOT_ERR_NO_OBJ);
580 base = got_object_idset_get(idset, &delta->base_id);
581 if (base == NULL)
582 return got_error(GOT_ERR_NO_OBJ);
584 m->delta_len = delta->delta_size;
585 m->delta_compressed_len = delta->delta_compressed_size;
586 m->delta_offset = delta->delta_out_offset;
587 m->prev = base;
588 m->size = delta->result_size;
589 m->reused_delta_offset = delta->delta_offset;
590 m->base_obj_id = got_object_id_dup(&delta->base_id);
591 if (m->base_obj_id == NULL)
592 return got_error_from_errno("got_object_id_dup");
594 return add_meta(m, v);
597 static const struct got_error *
598 prepare_delta_reuse(struct got_pack **pack, struct got_packidx *packidx,
599 int delta_outfd, struct got_repository *repo)
601 const struct got_error *err = NULL;
602 char *path_packfile = NULL;
604 err = got_packidx_get_packfile_path(&path_packfile,
605 packidx->path_packidx);
606 if (err)
607 return err;
609 *pack = got_repo_get_cached_pack(repo, path_packfile);
610 if (*pack == NULL) {
611 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
612 if (err)
613 goto done;
615 if ((*pack)->privsep_child == NULL) {
616 err = got_pack_start_privsep_child(*pack, packidx);
617 if (err)
618 goto done;
621 if (!(*pack)->child_has_delta_outfd) {
622 int outfd_child;
623 outfd_child = dup(delta_outfd);
624 if (outfd_child == -1) {
625 err = got_error_from_errno("dup");
626 goto done;
628 err = got_privsep_send_raw_delta_outfd(
629 (*pack)->privsep_child->ibuf, outfd_child);
630 if (err)
631 goto done;
632 (*pack)->child_has_delta_outfd = 1;
635 err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
636 done:
637 free(path_packfile);
638 return err;
642 static const struct got_error *
643 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
644 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
645 struct got_repository *repo,
646 got_pack_progress_cb progress_cb, void *progress_arg,
647 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
649 const struct got_error *err = NULL;
650 struct got_packidx *packidx;
651 struct got_pack *pack;
652 struct send_id_arg sia;
653 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
654 size_t ndeltas, i;
656 err = find_pack_for_reuse(&packidx, repo);
657 if (err)
658 return err;
660 if (packidx == NULL)
661 return NULL;
663 err = prepare_delta_reuse(&pack, packidx, delta_cache_fd, repo);
664 if (err)
665 return err;
667 memset(&sia, 0, sizeof(sia));
668 sia.ibuf = pack->privsep_child->ibuf;
669 err = got_object_idset_for_each(idset, send_id, &sia);
670 if (err)
671 return err;
672 if (sia.nids > 0) {
673 err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
674 sia.ids, sia.nids);
675 if (err)
676 return err;
678 err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
679 if (err)
680 return err;
682 for (;;) {
683 int done = 0;
685 if (cancel_cb) {
686 err = (*cancel_cb)(cancel_arg);
687 if (err)
688 break;
691 err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
692 pack->privsep_child->ibuf);
693 if (err || done)
694 break;
696 for (i = 0; i < ndeltas; i++) {
697 struct got_imsg_reused_delta *delta = &deltas[i];
698 err = recv_reused_delta(delta, idset, v);
699 if (err)
700 goto done;
703 err = report_progress(progress_cb, progress_arg, rl,
704 ncolored, nfound, ntrees, 0L, ncommits,
705 got_object_idset_num_elements(idset), v->nmeta, 0);
706 if (err)
707 break;
709 done:
710 return err;
713 static const struct got_error *
714 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
715 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
716 struct got_repository *repo,
717 got_pack_progress_cb progress_cb, void *progress_arg,
718 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
720 const struct got_error *err = NULL;
721 struct got_pack_meta *m = NULL, *base = NULL;
722 struct got_raw_object *raw = NULL, *base_raw = NULL;
723 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
724 int i, j, ndeltas, best_ndeltas;
725 off_t size, best_size;
726 const int max_base_candidates = 3;
727 size_t delta_memsize = 0;
728 const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
729 int outfd = -1;
730 uint32_t delta_seed;
732 delta_seed = arc4random();
734 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
735 for (i = 0; i < nmeta; i++) {
736 if (cancel_cb) {
737 err = (*cancel_cb)(cancel_arg);
738 if (err)
739 break;
741 err = report_progress(progress_cb, progress_arg, rl,
742 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
743 nreused + i, 0);
744 if (err)
745 goto done;
746 m = meta[i];
748 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
749 m->obj_type == GOT_OBJ_TYPE_TAG)
750 continue;
752 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
753 if (err)
754 goto done;
755 m->size = raw->size;
757 if (raw->f == NULL) {
758 err = got_deltify_init_mem(&m->dtab, raw->data,
759 raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
760 } else {
761 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
762 raw->size + raw->hdrlen, delta_seed);
764 if (err)
765 goto done;
767 if (i > max_base_candidates) {
768 struct got_pack_meta *n = NULL;
769 n = meta[i - (max_base_candidates + 1)];
770 got_deltify_free(n->dtab);
771 n->dtab = NULL;
774 best_size = raw->size;
775 best_ndeltas = 0;
776 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
777 if (cancel_cb) {
778 err = (*cancel_cb)(cancel_arg);
779 if (err)
780 goto done;
782 base = meta[j];
783 /* long chains make unpacking slow, avoid such bases */
784 if (base->nchain >= 128 ||
785 base->obj_type != m->obj_type)
786 continue;
788 err = got_object_raw_open(&base_raw, &outfd, repo,
789 &base->id);
790 if (err)
791 goto done;
793 if (raw->f == NULL && base_raw->f == NULL) {
794 err = got_deltify_mem_mem(&deltas, &ndeltas,
795 raw->data, raw->hdrlen,
796 raw->size + raw->hdrlen, delta_seed,
797 base->dtab, base_raw->data,
798 base_raw->hdrlen,
799 base_raw->size + base_raw->hdrlen);
800 } else if (raw->f == NULL) {
801 err = got_deltify_mem_file(&deltas, &ndeltas,
802 raw->data, raw->hdrlen,
803 raw->size + raw->hdrlen, delta_seed,
804 base->dtab, base_raw->f,
805 base_raw->hdrlen,
806 base_raw->size + base_raw->hdrlen);
807 } else if (base_raw->f == NULL) {
808 err = got_deltify_file_mem(&deltas, &ndeltas,
809 raw->f, raw->hdrlen,
810 raw->size + raw->hdrlen, delta_seed,
811 base->dtab, base_raw->data,
812 base_raw->hdrlen,
813 base_raw->size + base_raw->hdrlen);
814 } else {
815 err = got_deltify(&deltas, &ndeltas,
816 raw->f, raw->hdrlen,
817 raw->size + raw->hdrlen, delta_seed,
818 base->dtab, base_raw->f, base_raw->hdrlen,
819 base_raw->size + base_raw->hdrlen);
821 got_object_raw_close(base_raw);
822 base_raw = NULL;
823 if (err)
824 goto done;
826 size = delta_size(deltas, ndeltas);
827 if (size + 32 < best_size){
828 /*
829 * if we already picked a best delta,
830 * replace it.
831 */
832 best_size = size;
833 free(best_deltas);
834 best_deltas = deltas;
835 best_ndeltas = ndeltas;
836 deltas = NULL;
837 m->nchain = base->nchain + 1;
838 m->prev = base;
839 m->head = base->head;
840 if (m->head == NULL)
841 m->head = base;
842 } else {
843 free(deltas);
844 deltas = NULL;
845 ndeltas = 0;
849 if (best_ndeltas > 0) {
850 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
851 delta_memsize + best_size <= max_delta_memsize) {
852 delta_memsize += best_size;
853 err = encode_delta_in_mem(m, raw, best_deltas,
854 best_ndeltas, best_size, m->prev->size);
855 } else {
856 m->delta_offset = ftello(delta_cache);
857 err = encode_delta(m, raw, best_deltas,
858 best_ndeltas, m->prev->size, delta_cache);
860 free(best_deltas);
861 best_deltas = NULL;
862 best_ndeltas = 0;
863 if (err)
864 goto done;
867 got_object_raw_close(raw);
868 raw = NULL;
870 done:
871 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
872 got_deltify_free(meta[i]->dtab);
873 meta[i]->dtab = NULL;
875 if (raw)
876 got_object_raw_close(raw);
877 if (base_raw)
878 got_object_raw_close(base_raw);
879 if (outfd != -1 && close(outfd) == -1 && err == NULL)
880 err = got_error_from_errno("close");
881 free(deltas);
882 free(best_deltas);
883 return err;
886 static const struct got_error *
887 search_packidx(int *found, struct got_object_id *id,
888 struct got_repository *repo)
890 const struct got_error *err = NULL;
891 struct got_packidx *packidx = NULL;
892 int idx;
894 *found = 0;
896 err = got_repo_search_packidx(&packidx, &idx, repo, id);
897 if (err == NULL)
898 *found = 1; /* object is already packed */
899 else if (err->code == GOT_ERR_NO_OBJ)
900 err = NULL;
901 return err;
904 static const struct got_error *
905 add_object(int want_meta, struct got_object_idset *idset,
906 struct got_object_id *id, const char *path, int obj_type,
907 time_t mtime, uint32_t seed, int loose_obj_only,
908 struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
909 got_pack_progress_cb progress_cb, void *progress_arg,
910 struct got_ratelimit *rl)
912 const struct got_error *err;
913 struct got_pack_meta *m = NULL;
915 if (loose_obj_only) {
916 int is_packed;
917 err = search_packidx(&is_packed, id, repo);
918 if (err)
919 return err;
920 if (is_packed && want_meta)
921 return NULL;
924 if (want_meta) {
925 err = alloc_meta(&m, id, path, obj_type, mtime, seed);
926 if (err)
927 return err;
929 (*nfound)++;
930 err = report_progress(progress_cb, progress_arg, rl,
931 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
932 if (err) {
933 clear_meta(m);
934 free(m);
935 return err;
939 err = got_object_idset_add(idset, id, m);
940 if (err) {
941 clear_meta(m);
942 free(m);
944 return err;
947 static const struct got_error *
948 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
949 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
950 struct got_tree_object *tree,
951 const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
952 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
953 got_pack_progress_cb progress_cb, void *progress_arg,
954 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
956 const struct got_error *err;
957 char *p = NULL;
958 int i;
960 (*ntrees)++;
961 err = report_progress(progress_cb, progress_arg, rl,
962 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
963 if (err)
964 return err;
966 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
967 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
968 struct got_object_id *id = got_tree_entry_get_id(e);
969 mode_t mode = got_tree_entry_get_mode(e);
971 if (cancel_cb) {
972 err = (*cancel_cb)(cancel_arg);
973 if (err)
974 break;
977 if (got_object_tree_entry_is_submodule(e) ||
978 got_object_idset_contains(idset, id) ||
979 got_object_idset_contains(idset_exclude, id))
980 continue;
982 /*
983 * If got-read-pack is crawling trees for us then
984 * we are only here to collect blob IDs.
985 */
986 if (ids == NULL && S_ISDIR(mode))
987 continue;
989 if (asprintf(&p, "%s%s%s", dpath,
990 got_path_is_root_dir(dpath) ? "" : "/",
991 got_tree_entry_get_name(e)) == -1) {
992 err = got_error_from_errno("asprintf");
993 break;
996 if (S_ISDIR(mode)) {
997 struct got_object_qid *qid;
998 err = got_object_qid_alloc(&qid, id);
999 if (err)
1000 break;
1001 qid->data = p;
1002 p = NULL;
1003 STAILQ_INSERT_TAIL(ids, qid, entry);
1004 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
1005 err = add_object(want_meta,
1006 want_meta ? idset : idset_exclude, id, p,
1007 GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
1008 repo, ncolored, nfound, ntrees,
1009 progress_cb, progress_arg, rl);
1010 if (err)
1011 break;
1012 free(p);
1013 p = NULL;
1014 } else {
1015 free(p);
1016 p = NULL;
1020 free(p);
1021 return err;
1024 static const struct got_error *
1025 load_tree(int want_meta, struct got_object_idset *idset,
1026 struct got_object_idset *idset_exclude,
1027 struct got_object_id *tree_id, const char *dpath, time_t mtime,
1028 uint32_t seed, struct got_repository *repo, int loose_obj_only,
1029 int *ncolored, int *nfound, int *ntrees,
1030 got_pack_progress_cb progress_cb, void *progress_arg,
1031 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1033 const struct got_error *err = NULL;
1034 struct got_object_id_queue tree_ids;
1035 struct got_object_qid *qid;
1036 struct got_tree_object *tree = NULL;
1038 if (got_object_idset_contains(idset, tree_id) ||
1039 got_object_idset_contains(idset_exclude, tree_id))
1040 return NULL;
1042 err = got_object_qid_alloc(&qid, tree_id);
1043 if (err)
1044 return err;
1045 qid->data = strdup(dpath);
1046 if (qid->data == NULL) {
1047 err = got_error_from_errno("strdup");
1048 got_object_qid_free(qid);
1049 return err;
1052 STAILQ_INIT(&tree_ids);
1053 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1055 while (!STAILQ_EMPTY(&tree_ids)) {
1056 const char *path;
1057 if (cancel_cb) {
1058 err = (*cancel_cb)(cancel_arg);
1059 if (err)
1060 break;
1063 qid = STAILQ_FIRST(&tree_ids);
1064 STAILQ_REMOVE_HEAD(&tree_ids, entry);
1065 path = qid->data;
1067 if (got_object_idset_contains(idset, &qid->id) ||
1068 got_object_idset_contains(idset_exclude, &qid->id)) {
1069 free(qid->data);
1070 got_object_qid_free(qid);
1071 continue;
1074 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1075 &qid->id, path, GOT_OBJ_TYPE_TREE,
1076 mtime, seed, loose_obj_only, repo,
1077 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1078 if (err) {
1079 free(qid->data);
1080 got_object_qid_free(qid);
1081 break;
1084 err = got_object_open_as_tree(&tree, repo, &qid->id);
1085 if (err) {
1086 free(qid->data);
1087 got_object_qid_free(qid);
1088 break;
1091 err = load_tree_entries(&tree_ids, want_meta, idset,
1092 idset_exclude, tree, path, mtime, seed, repo,
1093 loose_obj_only, ncolored, nfound, ntrees,
1094 progress_cb, progress_arg, rl,
1095 cancel_cb, cancel_arg);
1096 free(qid->data);
1097 got_object_qid_free(qid);
1098 if (err)
1099 break;
1101 got_object_tree_close(tree);
1102 tree = NULL;
1105 STAILQ_FOREACH(qid, &tree_ids, entry)
1106 free(qid->data);
1107 got_object_id_queue_free(&tree_ids);
1108 if (tree)
1109 got_object_tree_close(tree);
1110 return err;
1113 static const struct got_error *
1114 load_commit(int want_meta, struct got_object_idset *idset,
1115 struct got_object_idset *idset_exclude,
1116 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1117 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1118 got_pack_progress_cb progress_cb, void *progress_arg,
1119 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1121 const struct got_error *err;
1122 struct got_commit_object *commit;
1124 if (got_object_idset_contains(idset, id) ||
1125 got_object_idset_contains(idset_exclude, id))
1126 return NULL;
1128 if (loose_obj_only) {
1129 int is_packed;
1130 err = search_packidx(&is_packed, id, repo);
1131 if (err)
1132 return err;
1133 if (is_packed && want_meta)
1134 return NULL;
1137 err = got_object_open_as_commit(&commit, repo, id);
1138 if (err)
1139 return err;
1141 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1142 id, "", GOT_OBJ_TYPE_COMMIT,
1143 got_object_commit_get_committer_time(commit), seed,
1144 loose_obj_only, repo,
1145 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1146 if (err)
1147 goto done;
1149 err = load_tree(want_meta, idset, idset_exclude,
1150 got_object_commit_get_tree_id(commit),
1151 "", got_object_commit_get_committer_time(commit), seed,
1152 repo, loose_obj_only, ncolored, nfound, ntrees,
1153 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1154 done:
1155 got_object_commit_close(commit);
1156 return err;
1159 static const struct got_error *
1160 load_tag(int want_meta, struct got_object_idset *idset,
1161 struct got_object_idset *idset_exclude,
1162 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1163 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1164 got_pack_progress_cb progress_cb, void *progress_arg,
1165 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1167 const struct got_error *err;
1168 struct got_tag_object *tag = NULL;
1170 if (got_object_idset_contains(idset, id) ||
1171 got_object_idset_contains(idset_exclude, id))
1172 return NULL;
1174 if (loose_obj_only) {
1175 int is_packed;
1176 err = search_packidx(&is_packed, id, repo);
1177 if (err)
1178 return err;
1179 if (is_packed && want_meta)
1180 return NULL;
1183 err = got_object_open_as_tag(&tag, repo, id);
1184 if (err)
1185 return err;
1187 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1188 id, "", GOT_OBJ_TYPE_TAG,
1189 got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1190 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1191 if (err)
1192 goto done;
1194 switch (got_object_tag_get_object_type(tag)) {
1195 case GOT_OBJ_TYPE_COMMIT:
1196 err = load_commit(want_meta, idset, idset_exclude,
1197 got_object_tag_get_object_id(tag), repo, seed,
1198 loose_obj_only, ncolored, nfound, ntrees,
1199 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1200 break;
1201 case GOT_OBJ_TYPE_TREE:
1202 err = load_tree(want_meta, idset, idset_exclude,
1203 got_object_tag_get_object_id(tag), "",
1204 got_object_tag_get_tagger_time(tag), seed, repo,
1205 loose_obj_only, ncolored, nfound, ntrees,
1206 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1207 break;
1208 default:
1209 break;
1212 done:
1213 got_object_tag_close(tag);
1214 return err;
1217 enum findtwixt_color {
1218 COLOR_KEEP = 0,
1219 COLOR_DROP,
1220 COLOR_BLANK,
1221 COLOR_SKIP,
1224 static const int findtwixt_colors[] = {
1225 COLOR_KEEP,
1226 COLOR_DROP,
1227 COLOR_BLANK,
1228 COLOR_SKIP,
1231 static const struct got_error *
1232 paint_commit(struct got_object_qid *qid, int color)
1234 if (color < 0 || color >= nitems(findtwixt_colors))
1235 return got_error(GOT_ERR_RANGE);
1237 qid->data = (void *)&findtwixt_colors[color];
1238 return NULL;
1241 static const struct got_error *
1242 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1243 int color, struct got_repository *repo)
1245 const struct got_error *err;
1246 struct got_object_qid *qid;
1248 err = got_object_qid_alloc(&qid, id);
1249 if (err)
1250 return err;
1252 STAILQ_INSERT_TAIL(ids, qid, entry);
1253 return paint_commit(qid, color);
1256 struct append_id_arg {
1257 struct got_object_id **array;
1258 int idx;
1259 struct got_object_idset *drop;
1260 struct got_object_idset *skip;
1263 static const struct got_error *
1264 append_id(struct got_object_id *id, void *data, void *arg)
1266 struct append_id_arg *a = arg;
1268 if (got_object_idset_contains(a->skip, id) ||
1269 got_object_idset_contains(a->drop, id))
1270 return NULL;
1272 a->array[++a->idx] = got_object_id_dup(id);
1273 if (a->array[a->idx] == NULL)
1274 return got_error_from_errno("got_object_id_dup");
1276 return NULL;
1279 static const struct got_error *
1280 queue_commit_or_tag_id(struct got_object_id *id, int color,
1281 struct got_object_id_queue *ids, struct got_repository *repo)
1283 const struct got_error *err;
1284 struct got_tag_object *tag = NULL;
1285 int obj_type;
1287 err = got_object_get_type(&obj_type, repo, id);
1288 if (err)
1289 return err;
1291 if (obj_type == GOT_OBJ_TYPE_TAG) {
1292 err = got_object_open_as_tag(&tag, repo, id);
1293 if (err)
1294 return err;
1295 obj_type = got_object_tag_get_object_type(tag);
1296 id = got_object_tag_get_object_id(tag);
1299 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1300 err = queue_commit_id(ids, id, color, repo);
1301 if (err)
1302 goto done;
1304 done:
1305 if (tag)
1306 got_object_tag_close(tag);
1307 return err;
1310 static const struct got_error *
1311 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1312 struct got_object_idset *keep, struct got_object_idset *drop,
1313 struct got_object_idset *skip, struct got_repository *repo,
1314 got_pack_progress_cb progress_cb, void *progress_arg,
1315 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1317 const struct got_error *err = NULL;
1318 struct got_commit_object *commit = NULL;
1319 const struct got_object_id_queue *parents;
1320 struct got_object_qid *qid;
1321 int nqueued = nids, nskip = 0;
1323 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1324 int color;
1326 if (cancel_cb) {
1327 err = cancel_cb(cancel_arg);
1328 if (err)
1329 break;
1332 qid = STAILQ_FIRST(ids);
1333 STAILQ_REMOVE_HEAD(ids, entry);
1334 nqueued--;
1335 color = *((int *)qid->data);
1336 if (color == COLOR_SKIP)
1337 nskip--;
1339 if (got_object_idset_contains(skip, &qid->id)) {
1340 got_object_qid_free(qid);
1341 continue;
1344 switch (color) {
1345 case COLOR_KEEP:
1346 if (got_object_idset_contains(keep, &qid->id)) {
1347 got_object_qid_free(qid);
1348 continue;
1350 if (got_object_idset_contains(drop, &qid->id)) {
1351 err = paint_commit(qid, COLOR_SKIP);
1352 if (err)
1353 goto done;
1354 } else
1355 (*ncolored)++;
1356 err = got_object_idset_add(keep, &qid->id, NULL);
1357 if (err)
1358 goto done;
1359 break;
1360 case COLOR_DROP:
1361 if (got_object_idset_contains(drop, &qid->id)) {
1362 got_object_qid_free(qid);
1363 continue;
1365 if (got_object_idset_contains(keep, &qid->id)) {
1366 err = paint_commit(qid, COLOR_SKIP);
1367 if (err)
1368 goto done;
1369 } else
1370 (*ncolored)++;
1371 err = got_object_idset_add(drop, &qid->id, NULL);
1372 if (err)
1373 goto done;
1374 break;
1375 case COLOR_SKIP:
1376 if (!got_object_idset_contains(skip, &qid->id)) {
1377 err = got_object_idset_add(skip, &qid->id,
1378 NULL);
1379 if (err)
1380 goto done;
1382 break;
1383 default:
1384 /* should not happen */
1385 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1386 "%s invalid commit color %d", __func__, color);
1387 goto done;
1390 err = report_progress(progress_cb, progress_arg, rl,
1391 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1392 if (err)
1393 break;
1396 err = got_object_open_as_commit(&commit, repo, &qid->id);
1397 if (err)
1398 break;
1400 parents = got_object_commit_get_parent_ids(commit);
1401 if (parents) {
1402 struct got_object_qid *pid;
1403 color = *((int *)qid->data);
1404 STAILQ_FOREACH(pid, parents, entry) {
1405 err = queue_commit_id(ids, &pid->id, color,
1406 repo);
1407 if (err)
1408 break;
1409 nqueued++;
1410 if (color == COLOR_SKIP)
1411 nskip++;
1415 got_object_commit_close(commit);
1416 commit = NULL;
1417 got_object_qid_free(qid);
1419 done:
1420 if (commit)
1421 got_object_commit_close(commit);
1422 return err;
1425 static const struct got_error *
1426 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1427 struct got_object_id **head, int nhead,
1428 struct got_object_id **tail, int ntail,
1429 struct got_repository *repo,
1430 got_pack_progress_cb progress_cb, void *progress_arg,
1431 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1433 const struct got_error *err = NULL;
1434 struct got_object_id_queue ids;
1435 struct got_object_idset *keep, *drop, *skip = NULL;
1436 int i, nkeep;
1438 STAILQ_INIT(&ids);
1439 *res = NULL;
1440 *nres = 0;
1441 *ncolored = 0;
1443 keep = got_object_idset_alloc();
1444 if (keep == NULL)
1445 return got_error_from_errno("got_object_idset_alloc");
1447 drop = got_object_idset_alloc();
1448 if (drop == NULL) {
1449 err = got_error_from_errno("got_object_idset_alloc");
1450 goto done;
1453 skip = got_object_idset_alloc();
1454 if (skip == NULL) {
1455 err = got_error_from_errno("got_object_idset_alloc");
1456 goto done;
1459 for (i = 0; i < nhead; i++) {
1460 struct got_object_id *id = head[i];
1461 if (id == NULL)
1462 continue;
1463 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1464 if (err)
1465 goto done;
1468 for (i = 0; i < ntail; i++) {
1469 struct got_object_id *id = tail[i];
1470 if (id == NULL)
1471 continue;
1472 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1473 if (err)
1474 goto done;
1477 err = paint_commits(ncolored, &ids, nhead + ntail,
1478 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1479 cancel_cb, cancel_arg);
1480 if (err)
1481 goto done;
1483 nkeep = got_object_idset_num_elements(keep);
1484 if (nkeep > 0) {
1485 struct append_id_arg arg;
1486 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1487 if (arg.array == NULL) {
1488 err = got_error_from_errno("calloc");
1489 goto done;
1491 arg.idx = -1;
1492 arg.skip = skip;
1493 arg.drop = drop;
1494 err = got_object_idset_for_each(keep, append_id, &arg);
1495 if (err) {
1496 free(arg.array);
1497 goto done;
1499 *res = arg.array;
1500 *nres = arg.idx + 1;
1502 done:
1503 got_object_idset_free(keep);
1504 got_object_idset_free(drop);
1505 if (skip)
1506 got_object_idset_free(skip);
1507 got_object_id_queue_free(&ids);
1508 return err;
1511 struct load_packed_obj_arg {
1512 /* output parameters: */
1513 struct got_object_id *id;
1514 char *dpath;
1515 time_t mtime;
1517 /* input parameters: */
1518 uint32_t seed;
1519 int want_meta;
1520 struct got_object_idset *idset;
1521 struct got_object_idset *idset_exclude;
1522 int loose_obj_only;
1523 int *ncolored;
1524 int *nfound;
1525 int *ntrees;
1526 got_pack_progress_cb progress_cb;
1527 void *progress_arg;
1528 struct got_ratelimit *rl;
1529 got_cancel_cb cancel_cb;
1530 void *cancel_arg;
1533 static const struct got_error *
1534 load_packed_commit_id(void *arg, time_t mtime, struct got_object_id *id,
1535 struct got_repository *repo)
1537 struct load_packed_obj_arg *a = arg;
1539 if (got_object_idset_contains(a->idset, id) ||
1540 got_object_idset_contains(a->idset_exclude, id))
1541 return NULL;
1543 return add_object(a->want_meta,
1544 a->want_meta ? a->idset : a->idset_exclude,
1545 id, "", GOT_OBJ_TYPE_COMMIT, mtime, a->seed, a->loose_obj_only,
1546 repo, a->ncolored, a->nfound, a->ntrees,
1547 a->progress_cb, a->progress_arg, a->rl);
1550 static const struct got_error *
1551 load_packed_tree_ids(void *arg, struct got_tree_object *tree, time_t mtime,
1552 struct got_object_id *id, const char *dpath, struct got_repository *repo)
1554 const struct got_error *err;
1555 struct load_packed_obj_arg *a = arg;
1556 const char *relpath;
1559 * When we receive a tree's ID and path but not the tree itself,
1560 * this tree object was not found in the pack file. This is the
1561 * last time we are being called for this optimized traversal.
1562 * Return from here and switch to loading objects the slow way.
1564 if (tree == NULL) {
1565 free(a->id);
1566 a->id = got_object_id_dup(id);
1567 if (a->id == NULL) {
1568 err = got_error_from_errno("got_object_id_dup");
1569 free(a->dpath);
1570 a->dpath = NULL;
1571 return err;
1574 free(a->dpath);
1575 a->dpath = strdup(dpath);
1576 if (a->dpath == NULL) {
1577 err = got_error_from_errno("strdup");
1578 free(a->id);
1579 a->id = NULL;
1580 return err;
1583 a->mtime = mtime;
1584 return NULL;
1587 if (got_object_idset_contains(a->idset, id) ||
1588 got_object_idset_contains(a->idset_exclude, id))
1589 return NULL;
1591 relpath = dpath;
1592 while (relpath[0] == '/')
1593 relpath++;
1595 err = add_object(a->want_meta,
1596 a->want_meta ? a->idset : a->idset_exclude,
1597 id, relpath, GOT_OBJ_TYPE_TREE, mtime, a->seed,
1598 a->loose_obj_only, repo, a->ncolored, a->nfound, a->ntrees,
1599 a->progress_cb, a->progress_arg, a->rl);
1600 if (err)
1601 return err;
1603 return load_tree_entries(NULL, a->want_meta, a->idset,
1604 a->idset_exclude, tree, dpath, mtime, a->seed, repo,
1605 a->loose_obj_only, a->ncolored, a->nfound, a->ntrees,
1606 a->progress_cb, a->progress_arg, a->rl,
1607 a->cancel_cb, a->cancel_arg);
1610 static const struct got_error *
1611 load_packed_object_ids(int *found_all_objects,
1612 struct got_object_id **ours, int nours,
1613 struct got_object_id **theirs, int ntheirs,
1614 int want_meta, uint32_t seed, struct got_object_idset *idset,
1615 struct got_object_idset *idset_exclude, int loose_obj_only,
1616 struct got_repository *repo, struct got_packidx *packidx,
1617 int *ncolored, int *nfound, int *ntrees,
1618 got_pack_progress_cb progress_cb, void *progress_arg,
1619 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1621 const struct got_error *err = NULL;
1622 struct load_packed_obj_arg lpa;
1624 memset(&lpa, 0, sizeof(lpa));
1625 lpa.seed = seed;
1626 lpa.want_meta = want_meta;
1627 lpa.idset = idset;
1628 lpa.idset_exclude = idset_exclude;
1629 lpa.loose_obj_only = loose_obj_only;
1630 lpa.ncolored = ncolored;
1631 lpa.nfound = nfound;
1632 lpa.ntrees = ntrees;
1633 lpa.progress_cb = progress_cb;
1634 lpa.progress_arg = progress_arg;
1635 lpa.rl = rl;
1636 lpa.cancel_cb = cancel_cb;
1637 lpa.cancel_arg = cancel_arg;
1639 /* Attempt to load objects via got-read-pack, as far as possible. */
1640 err = got_object_enumerate(found_all_objects, load_packed_commit_id,
1641 load_packed_tree_ids, &lpa, ours, nours, theirs, ntheirs,
1642 packidx, repo);
1643 if (err)
1644 return err;
1646 if (lpa.id == NULL)
1647 return NULL;
1650 * An incomplete tree hierarchy was present in the pack file
1651 * and caused loading to be aborted.
1652 * Continue loading trees the slow way.
1654 err = load_tree(want_meta, idset, idset_exclude,
1655 lpa.id, lpa.dpath, lpa.mtime, seed, repo, loose_obj_only,
1656 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1657 cancel_cb, cancel_arg);
1658 free(lpa.id);
1659 free(lpa.dpath);
1660 return err;
1663 static const struct got_error *
1664 find_pack_for_enumeration(struct got_packidx **best_packidx,
1665 struct got_object_id **ids, int nids, struct got_repository *repo)
1667 const struct got_error *err = NULL;
1668 struct got_pathlist_entry *pe;
1669 const char *best_packidx_path = NULL;
1670 int nobj_max = 0;
1671 int ncommits_max = 0;
1673 *best_packidx = NULL;
1676 * Find the largest pack which contains at least some of the
1677 * commits and tags we are interested in.
1679 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1680 const char *path_packidx = pe->path;
1681 struct got_packidx *packidx;
1682 int nobj, i, idx, ncommits = 0;
1684 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1685 if (err)
1686 break;
1688 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1689 if (nobj <= nobj_max)
1690 continue;
1692 for (i = 0; i < nids; i++) {
1693 idx = got_packidx_get_object_idx(packidx, ids[i]);
1694 if (idx != -1)
1695 ncommits++;
1697 if (ncommits > ncommits_max) {
1698 best_packidx_path = path_packidx;
1699 nobj_max = nobj;
1700 ncommits_max = ncommits;
1704 if (best_packidx_path && err == NULL) {
1705 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1706 repo);
1709 return err;
1712 static const struct got_error *
1713 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1714 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1715 struct got_object_id **ours, int nours, struct got_repository *repo,
1716 uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1717 void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1718 void *cancel_arg)
1720 const struct got_error *err = NULL;
1721 struct got_object_id **ids = NULL;
1722 struct got_packidx *packidx = NULL;
1723 int i, nobj = 0, obj_type, found_all_objects = 0;
1724 struct got_object_idset *idset_exclude;
1726 idset_exclude = got_object_idset_alloc();
1727 if (idset_exclude == NULL)
1728 return got_error_from_errno("got_object_idset_alloc");
1730 *ncolored = 0;
1731 *nfound = 0;
1732 *ntrees = 0;
1734 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1735 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1736 if (err)
1737 goto done;
1739 err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
1740 if (err)
1741 goto done;
1742 if (packidx) {
1743 err = load_packed_object_ids(&found_all_objects,
1744 theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
1745 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1746 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1747 if (err)
1748 goto done;
1751 for (i = 0; i < ntheirs; i++) {
1752 struct got_object_id *id = theirs[i];
1753 if (id == NULL)
1754 continue;
1755 err = got_object_get_type(&obj_type, repo, id);
1756 if (err)
1757 return err;
1758 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1759 if (!found_all_objects) {
1760 err = load_commit(0, idset, idset_exclude,
1761 id, repo, seed, loose_obj_only,
1762 ncolored, nfound, ntrees,
1763 progress_cb, progress_arg, rl,
1764 cancel_cb, cancel_arg);
1765 if (err)
1766 goto done;
1768 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1769 err = load_tag(0, idset, idset_exclude, id, repo,
1770 seed, loose_obj_only, ncolored, nfound, ntrees,
1771 progress_cb, progress_arg, rl,
1772 cancel_cb, cancel_arg);
1773 if (err)
1774 goto done;
1778 found_all_objects = 0;
1779 err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
1780 if (err)
1781 goto done;
1782 if (packidx) {
1783 err = load_packed_object_ids(&found_all_objects, ids,
1784 nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
1785 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1786 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1787 if (err)
1788 goto done;
1791 if (!found_all_objects) {
1792 for (i = 0; i < nobj; i++) {
1793 err = load_commit(1, idset, idset_exclude, ids[i],
1794 repo, seed, loose_obj_only, ncolored, nfound,
1795 ntrees, progress_cb, progress_arg, rl,
1796 cancel_cb, cancel_arg);
1797 if (err)
1798 goto done;
1802 for (i = 0; i < nours; i++) {
1803 struct got_object_id *id = ours[i];
1804 struct got_pack_meta *m;
1805 if (id == NULL)
1806 continue;
1807 m = got_object_idset_get(idset, id);
1808 if (m == NULL) {
1809 err = got_object_get_type(&obj_type, repo, id);
1810 if (err)
1811 goto done;
1812 } else
1813 obj_type = m->obj_type;
1814 if (obj_type != GOT_OBJ_TYPE_TAG)
1815 continue;
1816 err = load_tag(1, idset, idset_exclude, id, repo,
1817 seed, loose_obj_only, ncolored, nfound, ntrees,
1818 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1819 if (err)
1820 goto done;
1822 done:
1823 for (i = 0; i < nobj; i++) {
1824 free(ids[i]);
1826 free(ids);
1827 got_object_idset_free(idset_exclude);
1828 return err;
1831 static const struct got_error *
1832 hwrite(FILE *f, const void *buf, off_t len, SHA1_CTX *ctx)
1834 size_t n;
1836 SHA1Update(ctx, buf, len);
1837 n = fwrite(buf, 1, len, f);
1838 if (n != len)
1839 return got_ferror(f, GOT_ERR_IO);
1840 return NULL;
1843 static const struct got_error *
1844 hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1846 unsigned char buf[65536];
1847 off_t remain = len;
1848 size_t n;
1850 while (remain > 0) {
1851 size_t copylen = MIN(sizeof(buf), remain);
1852 n = fread(buf, 1, copylen, fsrc);
1853 if (n != copylen)
1854 return got_ferror(fsrc, GOT_ERR_IO);
1855 SHA1Update(ctx, buf, copylen);
1856 n = fwrite(buf, 1, copylen, fdst);
1857 if (n != copylen)
1858 return got_ferror(fdst, GOT_ERR_IO);
1859 remain -= copylen;
1862 return NULL;
1865 static const struct got_error *
1866 hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1867 FILE *fdst, off_t len, SHA1_CTX *ctx)
1869 size_t n;
1871 if (src_offset + len > src_size)
1872 return got_error(GOT_ERR_RANGE);
1874 SHA1Update(ctx, src + src_offset, len);
1875 n = fwrite(src + src_offset, 1, len, fdst);
1876 if (n != len)
1877 return got_ferror(fdst, GOT_ERR_IO);
1879 return NULL;
1882 static void
1883 putbe32(char *b, uint32_t n)
1885 b[0] = n >> 24;
1886 b[1] = n >> 16;
1887 b[2] = n >> 8;
1888 b[3] = n >> 0;
1891 static int
1892 write_order_cmp(const void *pa, const void *pb)
1894 struct got_pack_meta *a, *b, *ahd, *bhd;
1896 a = *(struct got_pack_meta **)pa;
1897 b = *(struct got_pack_meta **)pb;
1898 ahd = (a->head == NULL) ? a : a->head;
1899 bhd = (b->head == NULL) ? b : b->head;
1900 if (bhd->mtime < ahd->mtime)
1901 return -1;
1902 if (bhd->mtime > ahd->mtime)
1903 return 1;
1904 if (bhd < ahd)
1905 return -1;
1906 if (bhd > ahd)
1907 return 1;
1908 if (a->nchain != b->nchain)
1909 return a->nchain - b->nchain;
1910 if (a->mtime < b->mtime)
1911 return -1;
1912 if (a->mtime > b->mtime)
1913 return 1;
1914 return got_object_id_cmp(&a->id, &b->id);
1917 static int
1918 reuse_write_order_cmp(const void *pa, const void *pb)
1920 struct got_pack_meta *a, *b;
1922 a = *(struct got_pack_meta **)pa;
1923 b = *(struct got_pack_meta **)pb;
1925 if (a->reused_delta_offset < b->reused_delta_offset)
1926 return -1;
1927 if (a->reused_delta_offset > b->reused_delta_offset)
1928 return 1;
1929 return 0;
1932 static const struct got_error *
1933 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1935 size_t i;
1937 *hdrlen = 0;
1939 hdr[0] = obj_type << 4;
1940 hdr[0] |= len & 0xf;
1941 len >>= 4;
1942 for (i = 1; len != 0; i++){
1943 if (i >= bufsize)
1944 return got_error(GOT_ERR_NO_SPACE);
1945 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1946 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1947 len >>= GOT_DELTA_SIZE_SHIFT;
1950 *hdrlen = i;
1951 return NULL;
1954 static int
1955 packoff(char *hdr, off_t off)
1957 int i, j;
1958 char rbuf[8];
1960 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1961 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1962 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1963 GOT_DELTA_SIZE_MORE;
1966 j = 0;
1967 while (i > 0)
1968 hdr[j++] = rbuf[--i];
1969 return j;
1972 static const struct got_error *
1973 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1974 struct got_pack_meta *m)
1976 const struct got_error *err;
1977 char buf[32];
1978 int nh;
1980 if (m->prev->off != 0) {
1981 err = packhdr(&nh, buf, sizeof(buf),
1982 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1983 if (err)
1984 return err;
1985 nh += packoff(buf + nh, m->off - m->prev->off);
1986 err = hwrite(packfile, buf, nh, ctx);
1987 if (err)
1988 return err;
1989 *packfile_size += nh;
1990 } else {
1991 err = packhdr(&nh, buf, sizeof(buf),
1992 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1993 if (err)
1994 return err;
1995 err = hwrite(packfile, buf, nh, ctx);
1996 if (err)
1997 return err;
1998 *packfile_size += nh;
1999 err = hwrite(packfile, m->prev->id.sha1,
2000 sizeof(m->prev->id.sha1), ctx);
2001 if (err)
2002 return err;
2003 *packfile_size += sizeof(m->prev->id.sha1);
2006 return NULL;
2009 static const struct got_error *
2010 write_packed_object(off_t *packfile_size, FILE *packfile,
2011 FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
2012 struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
2013 struct got_repository *repo)
2015 const struct got_error *err = NULL;
2016 struct got_deflate_checksum csum;
2017 char buf[32];
2018 int nh;
2019 struct got_raw_object *raw = NULL;
2020 off_t outlen;
2022 csum.output_sha1 = ctx;
2023 csum.output_crc = NULL;
2025 m->off = ftello(packfile);
2026 if (m->delta_len == 0) {
2027 err = got_object_raw_open(&raw, outfd, repo, &m->id);
2028 if (err)
2029 goto done;
2030 err = packhdr(&nh, buf, sizeof(buf),
2031 m->obj_type, raw->size);
2032 if (err)
2033 goto done;
2034 err = hwrite(packfile, buf, nh, ctx);
2035 if (err)
2036 goto done;
2037 *packfile_size += nh;
2038 if (raw->f == NULL) {
2039 err = got_deflate_to_file_mmap(&outlen,
2040 raw->data + raw->hdrlen, 0, raw->size,
2041 packfile, &csum);
2042 if (err)
2043 goto done;
2044 } else {
2045 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
2046 == -1) {
2047 err = got_error_from_errno("fseeko");
2048 goto done;
2050 err = got_deflate_to_file(&outlen, raw->f,
2051 raw->size, packfile, &csum);
2052 if (err)
2053 goto done;
2055 *packfile_size += outlen;
2056 got_object_raw_close(raw);
2057 raw = NULL;
2058 } else if (m->delta_buf) {
2059 err = deltahdr(packfile_size, ctx, packfile, m);
2060 if (err)
2061 goto done;
2062 err = hwrite(packfile, m->delta_buf,
2063 m->delta_compressed_len, ctx);
2064 if (err)
2065 goto done;
2066 *packfile_size += m->delta_compressed_len;
2067 free(m->delta_buf);
2068 m->delta_buf = NULL;
2069 } else if (delta_cache_map) {
2070 err = deltahdr(packfile_size, ctx, packfile, m);
2071 if (err)
2072 goto done;
2073 err = hcopy_mmap(delta_cache_map, m->delta_offset,
2074 delta_cache_size, packfile, m->delta_compressed_len,
2075 ctx);
2076 if (err)
2077 goto done;
2078 *packfile_size += m->delta_compressed_len;
2079 } else {
2080 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
2081 == -1) {
2082 err = got_error_from_errno("fseeko");
2083 goto done;
2085 err = deltahdr(packfile_size, ctx, packfile, m);
2086 if (err)
2087 goto done;
2088 err = hcopy(delta_cache, packfile,
2089 m->delta_compressed_len, ctx);
2090 if (err)
2091 goto done;
2092 *packfile_size += m->delta_compressed_len;
2094 done:
2095 if (raw)
2096 got_object_raw_close(raw);
2097 return err;
2100 static const struct got_error *
2101 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
2102 struct got_pack_meta **deltify, int ndeltify,
2103 struct got_pack_meta **reuse, int nreuse,
2104 int ncolored, int nfound, int ntrees, int nours,
2105 struct got_repository *repo,
2106 got_pack_progress_cb progress_cb, void *progress_arg,
2107 struct got_ratelimit *rl,
2108 got_cancel_cb cancel_cb, void *cancel_arg)
2110 const struct got_error *err = NULL;
2111 int i;
2112 SHA1_CTX ctx;
2113 struct got_pack_meta *m;
2114 char buf[32];
2115 size_t n;
2116 off_t packfile_size = 0;
2117 int outfd = -1;
2118 int delta_cache_fd = -1;
2119 uint8_t *delta_cache_map = NULL;
2120 size_t delta_cache_size = 0;
2122 SHA1Init(&ctx);
2124 #ifndef GOT_PACK_NO_MMAP
2125 delta_cache_fd = dup(fileno(delta_cache));
2126 if (delta_cache_fd != -1) {
2127 struct stat sb;
2128 if (fstat(delta_cache_fd, &sb) == -1) {
2129 err = got_error_from_errno("fstat");
2130 goto done;
2132 if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
2133 delta_cache_map = mmap(NULL, sb.st_size,
2134 PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
2135 if (delta_cache_map == MAP_FAILED) {
2136 if (errno != ENOMEM) {
2137 err = got_error_from_errno("mmap");
2138 goto done;
2140 delta_cache_map = NULL; /* fallback on stdio */
2141 } else
2142 delta_cache_size = (size_t)sb.st_size;
2145 #endif
2146 err = hwrite(packfile, "PACK", 4, &ctx);
2147 if (err)
2148 goto done;
2149 putbe32(buf, GOT_PACKFILE_VERSION);
2150 err = hwrite(packfile, buf, 4, &ctx);
2151 if (err)
2152 goto done;
2153 putbe32(buf, ndeltify + nreuse);
2154 err = hwrite(packfile, buf, 4, &ctx);
2155 if (err)
2156 goto done;
2158 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
2159 write_order_cmp);
2160 for (i = 0; i < ndeltify; i++) {
2161 err = report_progress(progress_cb, progress_arg, rl,
2162 ncolored, nfound, ntrees, packfile_size, nours,
2163 ndeltify + nreuse, ndeltify + nreuse, i);
2164 if (err)
2165 goto done;
2166 m = deltify[i];
2167 err = write_packed_object(&packfile_size, packfile,
2168 delta_cache, delta_cache_map, delta_cache_size,
2169 m, &outfd, &ctx, repo);
2170 if (err)
2171 goto done;
2174 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
2175 reuse_write_order_cmp);
2176 for (i = 0; i < nreuse; i++) {
2177 err = report_progress(progress_cb, progress_arg, rl,
2178 ncolored, nfound, ntrees, packfile_size, nours,
2179 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
2180 if (err)
2181 goto done;
2182 m = reuse[i];
2183 err = write_packed_object(&packfile_size, packfile,
2184 delta_cache, delta_cache_map, delta_cache_size,
2185 m, &outfd, &ctx, repo);
2186 if (err)
2187 goto done;
2190 SHA1Final(pack_sha1, &ctx);
2191 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
2192 if (n != SHA1_DIGEST_LENGTH)
2193 err = got_ferror(packfile, GOT_ERR_IO);
2194 packfile_size += SHA1_DIGEST_LENGTH;
2195 packfile_size += sizeof(struct got_packfile_hdr);
2196 if (progress_cb) {
2197 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2198 packfile_size, nours, ndeltify + nreuse,
2199 ndeltify + nreuse, ndeltify + nreuse);
2200 if (err)
2201 goto done;
2203 done:
2204 if (outfd != -1 && close(outfd) == -1 && err == NULL)
2205 err = got_error_from_errno("close");
2206 if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
2207 err = got_error_from_errno("munmap");
2208 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2209 err = got_error_from_errno("close");
2210 return err;
2213 static const struct got_error *
2214 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
2216 struct got_pack_meta *m = data;
2217 struct got_pack_metavec *v = arg;
2219 if (m->reused_delta_offset != 0)
2220 return NULL;
2222 return add_meta(m, v);
2225 const struct got_error *
2226 got_pack_create(uint8_t *packsha1, FILE *packfile,
2227 struct got_object_id **theirs, int ntheirs,
2228 struct got_object_id **ours, int nours,
2229 struct got_repository *repo, int loose_obj_only, int allow_empty,
2230 got_pack_progress_cb progress_cb, void *progress_arg,
2231 got_cancel_cb cancel_cb, void *cancel_arg)
2233 const struct got_error *err;
2234 int delta_cache_fd = -1;
2235 FILE *delta_cache = NULL;
2236 struct got_object_idset *idset;
2237 struct got_ratelimit rl;
2238 struct got_pack_metavec deltify, reuse;
2239 int ncolored = 0, nfound = 0, ntrees = 0;
2240 size_t ndeltify;
2241 uint32_t seed;
2243 seed = arc4random();
2245 memset(&deltify, 0, sizeof(deltify));
2246 memset(&reuse, 0, sizeof(reuse));
2248 got_ratelimit_init(&rl, 0, 500);
2250 idset = got_object_idset_alloc();
2251 if (idset == NULL)
2252 return got_error_from_errno("got_object_idset_alloc");
2254 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
2255 ntheirs, ours, nours, repo, seed, loose_obj_only,
2256 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
2257 if (err)
2258 goto done;
2260 if (progress_cb) {
2261 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2262 0L, nours, got_object_idset_num_elements(idset), 0, 0);
2263 if (err)
2264 goto done;
2267 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
2268 err = got_error(GOT_ERR_CANNOT_PACK);
2269 goto done;
2272 delta_cache_fd = got_opentempfd();
2273 if (delta_cache_fd == -1) {
2274 err = got_error_from_errno("got_opentemp");
2275 goto done;
2278 reuse.metasz = 64;
2279 reuse.meta = calloc(reuse.metasz,
2280 sizeof(struct got_pack_meta *));
2281 if (reuse.meta == NULL) {
2282 err = got_error_from_errno("calloc");
2283 goto done;
2286 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
2287 ntrees, nours, repo, progress_cb, progress_arg, &rl,
2288 cancel_cb, cancel_arg);
2289 if (err)
2290 goto done;
2292 delta_cache = fdopen(delta_cache_fd, "a+");
2293 if (delta_cache == NULL) {
2294 err = got_error_from_errno("fdopen");
2295 goto done;
2297 delta_cache_fd = -1;
2299 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
2300 err = got_error_from_errno("fseeko");
2301 goto done;
2304 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
2305 if (ndeltify > 0) {
2306 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
2307 if (deltify.meta == NULL) {
2308 err = got_error_from_errno("calloc");
2309 goto done;
2311 deltify.metasz = ndeltify;
2313 err = got_object_idset_for_each(idset, add_meta_idset_cb,
2314 &deltify);
2315 if (err)
2316 goto done;
2317 if (deltify.nmeta > 0) {
2318 err = pick_deltas(deltify.meta, deltify.nmeta,
2319 ncolored, nfound, ntrees, nours, reuse.nmeta,
2320 delta_cache, repo, progress_cb, progress_arg, &rl,
2321 cancel_cb, cancel_arg);
2322 if (err)
2323 goto done;
2327 if (fflush(delta_cache) == EOF) {
2328 err = got_error_from_errno("fflush");
2329 goto done;
2331 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
2332 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
2333 nours, repo, progress_cb, progress_arg, &rl,
2334 cancel_cb, cancel_arg);
2335 if (err)
2336 goto done;
2337 done:
2338 free_nmeta(deltify.meta, deltify.nmeta);
2339 free_nmeta(reuse.meta, reuse.nmeta);
2340 got_object_idset_free(idset);
2341 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2342 err = got_error_from_errno("close");
2343 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2344 err = got_error_from_errno("fclose");
2345 return err;