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>
25 #include <endian.h>
26 #include <stdint.h>
27 #include <imsg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sha1.h>
32 #include <time.h>
33 #include <unistd.h>
34 #include <limits.h>
35 #include <zlib.h>
37 #include "got_error.h"
38 #include "got_cancel.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_reference.h"
42 #include "got_repository_admin.h"
43 #include "got_opentemp.h"
45 #include "got_lib_deltify.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_idset.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_deflate.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_repository.h"
54 #include "got_lib_ratelimit.h"
55 #include "got_lib_inflate.h"
57 #ifndef MIN
58 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 #endif
61 #ifndef MAX
62 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
63 #endif
65 #ifndef nitems
66 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
67 #endif
69 struct got_pack_meta {
70 struct got_object_id id;
71 char *path;
72 int obj_type;
73 off_t size;
74 time_t mtime;
76 /* The best delta we picked */
77 struct got_pack_meta *head;
78 struct got_pack_meta *prev;
79 unsigned char *delta_buf; /* if encoded in memory (compressed) */
80 off_t delta_offset; /* offset in delta cache file (compressed) */
81 off_t delta_len; /* encoded delta length */
82 off_t delta_compressed_len; /* encoded+compressed delta length */
83 int nchain;
85 int have_reused_delta;
86 off_t reused_delta_offset; /* offset of delta in reused pack file */
87 struct got_object_id *base_obj_id;
89 /* Only used for delta window */
90 struct got_delta_table *dtab;
92 /* Only used for writing offset deltas */
93 off_t off;
94 };
96 struct got_pack_metavec {
97 struct got_pack_meta **meta;
98 int nmeta;
99 int metasz;
100 };
102 static const struct got_error *
103 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
104 const char *path, int obj_type, time_t mtime)
106 const struct got_error *err = NULL;
107 struct got_pack_meta *m;
109 *new = NULL;
111 m = calloc(1, sizeof(*m));
112 if (m == NULL)
113 return got_error_from_errno("calloc");
115 memcpy(&m->id, id, sizeof(m->id));
117 m->path = strdup(path);
118 if (m->path == NULL) {
119 err = got_error_from_errno("strdup");
120 free(m);
121 return err;
124 m->obj_type = obj_type;
125 m->mtime = mtime;
126 *new = m;
127 return NULL;
130 static void
131 clear_meta(struct got_pack_meta *meta)
133 if (meta == NULL)
134 return;
135 free(meta->path);
136 meta->path = NULL;
137 free(meta->delta_buf);
138 meta->delta_buf = NULL;
139 free(meta->base_obj_id);
140 meta->base_obj_id = NULL;
143 static void
144 free_nmeta(struct got_pack_meta **meta, int nmeta)
146 int i;
148 for (i = 0; i < nmeta; i++)
149 clear_meta(meta[i]);
150 free(meta);
153 static int
154 delta_order_cmp(const void *pa, const void *pb)
156 struct got_pack_meta *a, *b;
157 int cmp;
159 a = *(struct got_pack_meta **)pa;
160 b = *(struct got_pack_meta **)pb;
162 if (a->obj_type != b->obj_type)
163 return a->obj_type - b->obj_type;
164 cmp = strcmp(a->path, b->path);
165 if (cmp != 0)
166 return cmp;
167 if (a->mtime < b->mtime)
168 return -1;
169 if (a->mtime > b->mtime)
170 return 1;
171 return got_object_id_cmp(&a->id, &b->id);
174 static off_t
175 delta_size(struct got_delta_instruction *deltas, int ndeltas)
177 int i;
178 off_t size = 32;
179 for (i = 0; i < ndeltas; i++) {
180 if (deltas[i].copy)
181 size += GOT_DELTA_SIZE_SHIFT;
182 else
183 size += deltas[i].len + 1;
185 return size;
188 static const struct got_error *
189 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
191 char *n;
193 if (*len + nseg >= *sz) {
194 while (*len + nseg >= *sz)
195 *sz += *sz / 2;
196 n = realloc(*p, *sz);
197 if (n == NULL)
198 return got_error_from_errno("realloc");
199 *p = n;
201 memcpy(*p + *len, seg, nseg);
202 *len += nseg;
203 return NULL;
206 static const struct got_error *
207 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
208 struct got_delta_instruction *deltas, int ndeltas,
209 off_t delta_size, off_t base_size)
211 const struct got_error *err;
212 unsigned char buf[16], *bp;
213 int i, j;
214 size_t len = 0, compressed_len;
215 off_t bufsize = delta_size;
216 off_t n;
217 struct got_delta_instruction *d;
218 uint8_t *delta_buf;
220 delta_buf = malloc(bufsize);
221 if (delta_buf == NULL)
222 return got_error_from_errno("malloc");
224 /* base object size */
225 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
226 n = base_size >> GOT_DELTA_SIZE_SHIFT;
227 for (i = 1; n > 0; i++) {
228 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
229 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
230 n >>= GOT_DELTA_SIZE_SHIFT;
232 err = append(&delta_buf, &len, &bufsize, buf, i);
233 if (err)
234 goto done;
236 /* target object size */
237 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
238 n = o->size >> GOT_DELTA_SIZE_SHIFT;
239 for (i = 1; n > 0; i++) {
240 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
241 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
242 n >>= GOT_DELTA_SIZE_SHIFT;
244 err = append(&delta_buf, &len, &bufsize, buf, i);
245 if (err)
246 goto done;
248 for (j = 0; j < ndeltas; j++) {
249 d = &deltas[j];
250 if (d->copy) {
251 n = d->offset;
252 bp = &buf[1];
253 buf[0] = GOT_DELTA_BASE_COPY;
254 for (i = 0; i < 4; i++) {
255 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
256 buf[0] |= 1 << i;
257 *bp++ = n & 0xff;
258 n >>= 8;
259 if (n == 0)
260 break;
263 n = d->len;
264 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
265 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
266 for (i = 0; i < 3 && n > 0; i++) {
267 buf[0] |= 1 << (i + 4);
268 *bp++ = n & 0xff;
269 n >>= 8;
272 err = append(&delta_buf, &len, &bufsize,
273 buf, bp - buf);
274 if (err)
275 goto done;
276 } else if (o->f == NULL) {
277 n = 0;
278 while (n != d->len) {
279 buf[0] = (d->len - n < 127) ? d->len - n : 127;
280 err = append(&delta_buf, &len, &bufsize,
281 buf, 1);
282 if (err)
283 goto done;
284 err = append(&delta_buf, &len, &bufsize,
285 o->data + o->hdrlen + d->offset + n,
286 buf[0]);
287 if (err)
288 goto done;
289 n += buf[0];
291 } else {
292 char content[128];
293 size_t r;
294 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
295 err = got_error_from_errno("fseeko");
296 goto done;
298 n = 0;
299 while (n != d->len) {
300 buf[0] = (d->len - n < 127) ? d->len - n : 127;
301 err = append(&delta_buf, &len, &bufsize,
302 buf, 1);
303 if (err)
304 goto done;
305 r = fread(content, 1, buf[0], o->f);
306 if (r != buf[0]) {
307 err = got_ferror(o->f, GOT_ERR_IO);
308 goto done;
310 err = append(&delta_buf, &len, &bufsize,
311 content, buf[0]);
312 if (err)
313 goto done;
314 n += buf[0];
319 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
320 NULL, NULL, delta_buf, 0, len);
321 if (err)
322 goto done;
324 m->delta_len = len;
325 m->delta_compressed_len = compressed_len;
326 done:
327 free(delta_buf);
328 return err;
331 static const struct got_error *
332 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
333 struct got_delta_instruction *deltas, int ndeltas,
334 off_t base_size, FILE *f)
336 const struct got_error *err;
337 unsigned char buf[16], *bp;
338 int i, j;
339 off_t n;
340 struct got_deflate_buf zb;
341 struct got_delta_instruction *d;
342 off_t delta_len = 0, compressed_len = 0;
344 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
345 if (err)
346 return err;
348 /* base object size */
349 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
350 n = base_size >> GOT_DELTA_SIZE_SHIFT;
351 for (i = 1; n > 0; i++) {
352 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
353 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
354 n >>= GOT_DELTA_SIZE_SHIFT;
357 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
358 buf, 0, i, f, NULL);
359 if (err)
360 goto done;
361 delta_len += i;
363 /* target object size */
364 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
365 n = o->size >> GOT_DELTA_SIZE_SHIFT;
366 for (i = 1; n > 0; i++) {
367 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
368 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
369 n >>= GOT_DELTA_SIZE_SHIFT;
372 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
373 buf, 0, i, f, NULL);
374 if (err)
375 goto done;
376 delta_len += i;
378 for (j = 0; j < ndeltas; j++) {
379 d = &deltas[j];
380 if (d->copy) {
381 n = d->offset;
382 bp = &buf[1];
383 buf[0] = GOT_DELTA_BASE_COPY;
384 for (i = 0; i < 4; i++) {
385 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
386 buf[0] |= 1 << i;
387 *bp++ = n & 0xff;
388 n >>= 8;
389 if (n == 0)
390 break;
392 n = d->len;
393 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
394 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
395 for (i = 0; i < 3 && n > 0; i++) {
396 buf[0] |= 1 << (i + 4);
397 *bp++ = n & 0xff;
398 n >>= 8;
401 err = got_deflate_append_to_file_mmap(&zb,
402 &compressed_len, buf, 0, bp - buf, f, NULL);
403 if (err)
404 goto done;
405 delta_len += (bp - buf);
406 } else if (o->f == NULL) {
407 n = 0;
408 while (n != d->len) {
409 buf[0] = (d->len - n < 127) ? d->len - n : 127;
410 err = got_deflate_append_to_file_mmap(&zb,
411 &compressed_len, buf, 0, 1, f, NULL);
412 if (err)
413 goto done;
414 delta_len++;
415 err = got_deflate_append_to_file_mmap(&zb,
416 &compressed_len,
417 o->data + o->hdrlen + d->offset + n, 0,
418 buf[0], f, NULL);
419 if (err)
420 goto done;
421 delta_len += buf[0];
422 n += buf[0];
424 } else {
425 char content[128];
426 size_t r;
427 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
428 err = got_error_from_errno("fseeko");
429 goto done;
431 n = 0;
432 while (n != d->len) {
433 buf[0] = (d->len - n < 127) ? d->len - n : 127;
434 err = got_deflate_append_to_file_mmap(&zb,
435 &compressed_len, buf, 0, 1, f, NULL);
436 if (err)
437 goto done;
438 delta_len++;
439 r = fread(content, 1, buf[0], o->f);
440 if (r != buf[0]) {
441 err = got_ferror(o->f, GOT_ERR_IO);
442 goto done;
444 err = got_deflate_append_to_file_mmap(&zb,
445 &compressed_len, content, 0, buf[0], f,
446 NULL);
447 if (err)
448 goto done;
449 delta_len += buf[0];
450 n += buf[0];
455 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
456 if (err)
457 goto done;
459 /* sanity check */
460 if (compressed_len != ftello(f) - m->delta_offset) {
461 err = got_error(GOT_ERR_COMPRESSION);
462 goto done;
465 m->delta_len = delta_len;
466 m->delta_compressed_len = compressed_len;
467 done:
468 got_deflate_end(&zb);
469 return err;
472 static const struct got_error *
473 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
474 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
475 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
476 int nobj_written)
478 const struct got_error *err;
479 int elapsed;
481 if (progress_cb == NULL)
482 return NULL;
484 err = got_ratelimit_check(&elapsed, rl);
485 if (err || !elapsed)
486 return err;
488 return progress_cb(progress_arg, ncolored, nfound, ntrees,
489 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
492 static const struct got_error *
493 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
495 if (v->nmeta == v->metasz){
496 size_t newsize = 2 * v->metasz;
497 struct got_pack_meta **new;
498 new = reallocarray(v->meta, newsize, sizeof(*new));
499 if (new == NULL)
500 return got_error_from_errno("reallocarray");
501 v->meta = new;
502 v->metasz = newsize;
505 v->meta[v->nmeta++] = m;
506 return NULL;
509 static const struct got_error *
510 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
511 struct got_object_idset *idset, struct got_pack *pack,
512 struct got_packidx *packidx, int delta_cache_fd,
513 struct got_repository *repo)
515 const struct got_error *err = NULL;
516 struct got_pack_meta *base = NULL;
517 struct got_object_id *base_obj_id = NULL;
518 off_t delta_len = 0, delta_compressed_len = 0;
519 off_t delta_offset = 0, delta_cache_offset = 0;
520 uint64_t base_size, result_size;
522 if (m->have_reused_delta)
523 return NULL;
525 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
526 &delta_compressed_len, &delta_offset, &delta_cache_offset,
527 &base_obj_id, delta_cache_fd, packidx, idx, &m->id, repo);
528 if (err)
529 return err;
531 if (delta_offset + delta_len < delta_offset)
532 return got_error(GOT_ERR_BAD_PACKFILE);
534 base = got_object_idset_get(idset, base_obj_id);
535 if (base == NULL)
536 goto done;
538 m->delta_len = delta_len;
539 m->delta_compressed_len = delta_compressed_len;
540 m->delta_offset = delta_cache_offset;
541 m->prev = base;
542 m->size = result_size;
543 m->have_reused_delta = 1;
544 m->reused_delta_offset = delta_offset;
545 m->base_obj_id = base_obj_id;
546 base_obj_id = NULL;
547 err = add_meta(m, v);
548 done:
549 free(base_obj_id);
550 return err;
553 static const struct got_error *
554 find_pack_for_reuse(struct got_packidx **best_packidx,
555 struct got_repository *repo)
557 const struct got_error *err = NULL;
558 struct got_pathlist_entry *pe;
559 const char *best_packidx_path = NULL;
560 int nobj_max = 0;
562 *best_packidx = NULL;
564 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
565 const char *path_packidx = pe->path;
566 struct got_packidx *packidx;
567 int nobj;
569 err = got_repo_get_packidx(&packidx, path_packidx, repo);
570 if (err)
571 break;
573 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
574 if (nobj > nobj_max) {
575 best_packidx_path = path_packidx;
576 nobj_max = nobj;
580 if (best_packidx_path) {
581 err = got_repo_get_packidx(best_packidx, best_packidx_path,
582 repo);
585 return err;
588 struct search_deltas_arg {
589 struct got_packidx *packidx;
590 struct got_pack *pack;
591 struct got_object_idset *idset;
592 struct got_pack_metavec *v;
593 int delta_cache_fd;
594 struct got_repository *repo;
595 got_pack_progress_cb progress_cb;
596 void *progress_arg;
597 struct got_ratelimit *rl;
598 got_cancel_cb cancel_cb;
599 void *cancel_arg;
600 int ncolored;
601 int nfound;
602 int ntrees;
603 int ncommits;
604 };
606 static const struct got_error *
607 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
609 const struct got_error *err;
610 struct got_pack_meta *m = data;
611 struct search_deltas_arg *a = arg;
612 int obj_idx;
613 struct got_object *obj = NULL;
615 if (a->cancel_cb) {
616 err = (*a->cancel_cb)(a->cancel_arg);
617 if (err)
618 return err;
621 if (!got_repo_check_packidx_bloom_filter(a->repo,
622 a->packidx->path_packidx, id))
623 return NULL;
625 obj_idx = got_packidx_get_object_idx(a->packidx, id);
626 if (obj_idx == -1)
627 return NULL;
629 /* TODO:
630 * Opening and closing an object just to check its flags
631 * is a bit expensive. We could have an imsg which requests
632 * plain type/size information for an object without doing
633 * work such as traversing the object's entire delta chain
634 * to find the base object type, and other such info which
635 * we don't really need here.
636 */
637 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
638 a->packidx, obj_idx, a->repo);
639 if (err)
640 return err;
642 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
643 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
644 a->delta_cache_fd, a->repo);
645 if (err)
646 goto done;
647 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
648 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
649 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
651 done:
652 got_object_close(obj);
653 return err;
656 static const struct got_error *
657 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
658 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
659 struct got_repository *repo,
660 got_pack_progress_cb progress_cb, void *progress_arg,
661 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
663 const struct got_error *err = NULL;
664 char *path_packfile = NULL;
665 struct got_packidx *packidx;
666 struct got_pack *pack;
667 struct search_deltas_arg sda;
669 err = find_pack_for_reuse(&packidx, repo);
670 if (err)
671 return err;
673 if (packidx == NULL)
674 return NULL;
676 err = got_packidx_get_packfile_path(&path_packfile,
677 packidx->path_packidx);
678 if (err)
679 return err;
681 pack = got_repo_get_cached_pack(repo, path_packfile);
682 if (pack == NULL) {
683 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
684 if (err)
685 goto done;
688 sda.packidx = packidx;
689 sda.pack = pack;
690 sda.idset = idset;
691 sda.v = v;
692 sda.delta_cache_fd = delta_cache_fd;
693 sda.repo = repo;
694 sda.progress_cb = progress_cb;
695 sda.progress_arg = progress_arg;
696 sda.rl = rl;
697 sda.cancel_cb = cancel_cb;
698 sda.cancel_arg = cancel_arg;
699 sda.ncolored = ncolored;
700 sda.nfound = nfound;
701 sda.ntrees = ntrees;
702 sda.ncommits = ncommits;
703 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
704 done:
705 free(path_packfile);
706 return err;
709 static const struct got_error *
710 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
711 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
712 struct got_repository *repo,
713 got_pack_progress_cb progress_cb, void *progress_arg,
714 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
716 const struct got_error *err = NULL;
717 struct got_pack_meta *m = NULL, *base = NULL;
718 struct got_raw_object *raw = NULL, *base_raw = NULL;
719 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
720 int i, j, ndeltas, best_ndeltas;
721 off_t size, best_size;
722 const int max_base_candidates = 3;
723 size_t delta_memsize = 0;
724 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
725 int outfd = -1;
727 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
728 for (i = 0; i < nmeta; i++) {
729 if (cancel_cb) {
730 err = (*cancel_cb)(cancel_arg);
731 if (err)
732 break;
734 err = report_progress(progress_cb, progress_arg, rl,
735 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
736 nreused + i, 0);
737 if (err)
738 goto done;
739 m = meta[i];
741 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
742 m->obj_type == GOT_OBJ_TYPE_TAG)
743 continue;
745 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
746 if (err)
747 goto done;
748 m->size = raw->size;
750 if (raw->f == NULL) {
751 err = got_deltify_init_mem(&m->dtab, raw->data,
752 raw->hdrlen, raw->size + raw->hdrlen);
753 } else {
754 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
755 raw->size + raw->hdrlen);
757 if (err)
758 goto done;
760 if (i > max_base_candidates) {
761 struct got_pack_meta *n = NULL;
762 n = meta[i - (max_base_candidates + 1)];
763 got_deltify_free(n->dtab);
764 n->dtab = NULL;
767 best_size = raw->size;
768 best_ndeltas = 0;
769 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
770 if (cancel_cb) {
771 err = (*cancel_cb)(cancel_arg);
772 if (err)
773 goto done;
775 base = meta[j];
776 /* long chains make unpacking slow, avoid such bases */
777 if (base->nchain >= 128 ||
778 base->obj_type != m->obj_type)
779 continue;
781 err = got_object_raw_open(&base_raw, &outfd, repo,
782 &base->id);
783 if (err)
784 goto done;
786 if (raw->f == NULL && base_raw->f == NULL) {
787 err = got_deltify_mem_mem(&deltas, &ndeltas,
788 raw->data, raw->hdrlen,
789 raw->size + raw->hdrlen,
790 base->dtab, base_raw->data,
791 base_raw->hdrlen,
792 base_raw->size + base_raw->hdrlen);
793 } else if (raw->f == NULL) {
794 err = got_deltify_mem_file(&deltas, &ndeltas,
795 raw->data, raw->hdrlen,
796 raw->size + raw->hdrlen,
797 base->dtab, base_raw->f,
798 base_raw->hdrlen,
799 base_raw->size + base_raw->hdrlen);
800 } else if (base_raw->f == NULL) {
801 err = got_deltify_file_mem(&deltas, &ndeltas,
802 raw->f, raw->hdrlen,
803 raw->size + raw->hdrlen,
804 base->dtab, base_raw->data,
805 base_raw->hdrlen,
806 base_raw->size + base_raw->hdrlen);
807 } else {
808 err = got_deltify(&deltas, &ndeltas,
809 raw->f, raw->hdrlen,
810 raw->size + raw->hdrlen,
811 base->dtab, base_raw->f, base_raw->hdrlen,
812 base_raw->size + base_raw->hdrlen);
814 got_object_raw_close(base_raw);
815 base_raw = NULL;
816 if (err)
817 goto done;
819 size = delta_size(deltas, ndeltas);
820 if (size + 32 < best_size){
821 /*
822 * if we already picked a best delta,
823 * replace it.
824 */
825 best_size = size;
826 free(best_deltas);
827 best_deltas = deltas;
828 best_ndeltas = ndeltas;
829 deltas = NULL;
830 m->nchain = base->nchain + 1;
831 m->prev = base;
832 m->head = base->head;
833 if (m->head == NULL)
834 m->head = base;
835 } else {
836 free(deltas);
837 deltas = NULL;
838 ndeltas = 0;
842 if (best_ndeltas > 0) {
843 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
844 delta_memsize + best_size <= max_delta_memsize) {
845 delta_memsize += best_size;
846 err = encode_delta_in_mem(m, raw, best_deltas,
847 best_ndeltas, best_size, m->prev->size);
848 } else {
849 m->delta_offset = ftello(delta_cache);
850 err = encode_delta(m, raw, best_deltas,
851 best_ndeltas, m->prev->size, delta_cache);
853 free(best_deltas);
854 best_deltas = NULL;
855 best_ndeltas = 0;
856 if (err)
857 goto done;
860 got_object_raw_close(raw);
861 raw = NULL;
863 done:
864 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
865 got_deltify_free(meta[i]->dtab);
866 meta[i]->dtab = NULL;
868 if (raw)
869 got_object_raw_close(raw);
870 if (base_raw)
871 got_object_raw_close(base_raw);
872 if (outfd != -1 && close(outfd) == -1 && err == NULL)
873 err = got_error_from_errno("close");
874 free(deltas);
875 free(best_deltas);
876 return err;
879 static const struct got_error *
880 search_packidx(int *found, struct got_object_id *id,
881 struct got_repository *repo)
883 const struct got_error *err = NULL;
884 struct got_packidx *packidx = NULL;
885 int idx;
887 *found = 0;
889 err = got_repo_search_packidx(&packidx, &idx, repo, id);
890 if (err == NULL)
891 *found = 1; /* object is already packed */
892 else if (err->code == GOT_ERR_NO_OBJ)
893 err = NULL;
894 return err;
897 static const struct got_error *
898 add_object(int want_meta, struct got_object_idset *idset,
899 struct got_object_id *id, const char *path, int obj_type,
900 time_t mtime, int loose_obj_only, struct got_repository *repo,
901 int *ncolored, int *nfound, int *ntrees,
902 got_pack_progress_cb progress_cb, void *progress_arg,
903 struct got_ratelimit *rl)
905 const struct got_error *err;
906 struct got_pack_meta *m = NULL;
908 if (loose_obj_only) {
909 int is_packed;
910 err = search_packidx(&is_packed, id, repo);
911 if (err)
912 return err;
913 if (is_packed && want_meta)
914 return NULL;
917 if (want_meta) {
918 err = alloc_meta(&m, id, path, obj_type, mtime);
919 if (err)
920 return err;
922 (*nfound)++;
923 err = report_progress(progress_cb, progress_arg, rl,
924 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
925 if (err) {
926 clear_meta(m);
927 free(m);
928 return err;
932 err = got_object_idset_add(idset, id, m);
933 if (err) {
934 clear_meta(m);
935 free(m);
937 return err;
940 static const struct got_error *
941 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
942 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
943 struct got_object_id *tree_id,
944 const char *dpath, time_t mtime, struct got_repository *repo,
945 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
946 got_pack_progress_cb progress_cb, void *progress_arg,
947 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
949 const struct got_error *err;
950 struct got_tree_object *tree;
951 char *p = NULL;
952 int i;
954 err = got_object_open_as_tree(&tree, repo, tree_id);
955 if (err)
956 return err;
958 (*ntrees)++;
959 err = report_progress(progress_cb, progress_arg, rl,
960 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
961 if (err)
962 return err;
964 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
965 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
966 struct got_object_id *id = got_tree_entry_get_id(e);
967 mode_t mode = got_tree_entry_get_mode(e);
969 if (cancel_cb) {
970 err = (*cancel_cb)(cancel_arg);
971 if (err)
972 break;
975 if (got_object_tree_entry_is_submodule(e) ||
976 got_object_idset_contains(idset, id) ||
977 got_object_idset_contains(idset_exclude, id))
978 continue;
980 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
981 got_tree_entry_get_name(e)) == -1) {
982 err = got_error_from_errno("asprintf");
983 break;
986 if (S_ISDIR(mode)) {
987 struct got_object_qid *qid;
988 err = got_object_qid_alloc(&qid, id);
989 if (err)
990 break;
991 STAILQ_INSERT_TAIL(ids, qid, entry);
992 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
993 err = add_object(want_meta,
994 want_meta ? idset : idset_exclude, id, p,
995 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
996 ncolored, nfound, ntrees,
997 progress_cb, progress_arg, rl);
998 if (err)
999 break;
1001 free(p);
1002 p = NULL;
1005 got_object_tree_close(tree);
1006 free(p);
1007 return err;
1010 static const struct got_error *
1011 load_tree(int want_meta, struct got_object_idset *idset,
1012 struct got_object_idset *idset_exclude,
1013 struct got_object_id *tree_id, const char *dpath, time_t mtime,
1014 struct got_repository *repo, int loose_obj_only,
1015 int *ncolored, int *nfound, int *ntrees,
1016 got_pack_progress_cb progress_cb, void *progress_arg,
1017 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1019 const struct got_error *err = NULL;
1020 struct got_object_id_queue tree_ids;
1021 struct got_object_qid *qid;
1023 if (got_object_idset_contains(idset, tree_id) ||
1024 got_object_idset_contains(idset_exclude, tree_id))
1025 return NULL;
1027 err = got_object_qid_alloc(&qid, tree_id);
1028 if (err)
1029 return err;
1031 STAILQ_INIT(&tree_ids);
1032 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1034 while (!STAILQ_EMPTY(&tree_ids)) {
1035 if (cancel_cb) {
1036 err = (*cancel_cb)(cancel_arg);
1037 if (err)
1038 break;
1041 qid = STAILQ_FIRST(&tree_ids);
1042 STAILQ_REMOVE_HEAD(&tree_ids, entry);
1044 if (got_object_idset_contains(idset, &qid->id) ||
1045 got_object_idset_contains(idset_exclude, &qid->id)) {
1046 got_object_qid_free(qid);
1047 continue;
1050 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1051 &qid->id, dpath, GOT_OBJ_TYPE_TREE,
1052 mtime, loose_obj_only, repo,
1053 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1054 if (err) {
1055 got_object_qid_free(qid);
1056 break;
1059 err = load_tree_entries(&tree_ids, want_meta, idset,
1060 idset_exclude, &qid->id,
1061 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1062 ntrees, progress_cb, progress_arg, rl,
1063 cancel_cb, cancel_arg);
1064 got_object_qid_free(qid);
1065 if (err)
1066 break;
1069 got_object_id_queue_free(&tree_ids);
1070 return err;
1073 static const struct got_error *
1074 load_commit(int want_meta, struct got_object_idset *idset,
1075 struct got_object_idset *idset_exclude,
1076 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1077 int *ncolored, int *nfound, int *ntrees,
1078 got_pack_progress_cb progress_cb, void *progress_arg,
1079 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1081 const struct got_error *err;
1082 struct got_commit_object *commit;
1084 if (got_object_idset_contains(idset, id) ||
1085 got_object_idset_contains(idset_exclude, id))
1086 return NULL;
1088 if (loose_obj_only) {
1089 int is_packed;
1090 err = search_packidx(&is_packed, id, repo);
1091 if (err)
1092 return err;
1093 if (is_packed && want_meta)
1094 return NULL;
1097 err = got_object_open_as_commit(&commit, repo, id);
1098 if (err)
1099 return err;
1101 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1102 id, "", GOT_OBJ_TYPE_COMMIT,
1103 got_object_commit_get_committer_time(commit),
1104 loose_obj_only, repo,
1105 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1106 if (err)
1107 goto done;
1109 err = load_tree(want_meta, idset, idset_exclude,
1110 got_object_commit_get_tree_id(commit),
1111 "", got_object_commit_get_committer_time(commit),
1112 repo, loose_obj_only, ncolored, nfound, ntrees,
1113 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1114 done:
1115 got_object_commit_close(commit);
1116 return err;
1119 static const struct got_error *
1120 load_tag(int want_meta, struct got_object_idset *idset,
1121 struct got_object_idset *idset_exclude,
1122 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1123 int *ncolored, int *nfound, int *ntrees,
1124 got_pack_progress_cb progress_cb, void *progress_arg,
1125 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1127 const struct got_error *err;
1128 struct got_tag_object *tag = NULL;
1130 if (got_object_idset_contains(idset, id) ||
1131 got_object_idset_contains(idset_exclude, id))
1132 return NULL;
1134 if (loose_obj_only) {
1135 int is_packed;
1136 err = search_packidx(&is_packed, id, repo);
1137 if (err)
1138 return err;
1139 if (is_packed && want_meta)
1140 return NULL;
1143 err = got_object_open_as_tag(&tag, repo, id);
1144 if (err)
1145 return err;
1147 err = add_object(want_meta, want_meta ? idset : idset_exclude,
1148 id, "", GOT_OBJ_TYPE_TAG,
1149 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1150 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1151 if (err)
1152 goto done;
1154 switch (got_object_tag_get_object_type(tag)) {
1155 case GOT_OBJ_TYPE_COMMIT:
1156 err = load_commit(want_meta, idset, idset_exclude,
1157 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1158 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1159 cancel_cb, cancel_arg);
1160 break;
1161 case GOT_OBJ_TYPE_TREE:
1162 err = load_tree(want_meta, idset, idset_exclude,
1163 got_object_tag_get_object_id(tag), "",
1164 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1165 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1166 cancel_cb, cancel_arg);
1167 break;
1168 default:
1169 break;
1172 done:
1173 got_object_tag_close(tag);
1174 return err;
1177 enum findtwixt_color {
1178 COLOR_KEEP = 0,
1179 COLOR_DROP,
1180 COLOR_BLANK,
1181 COLOR_SKIP,
1184 static const int findtwixt_colors[] = {
1185 COLOR_KEEP,
1186 COLOR_DROP,
1187 COLOR_BLANK,
1188 COLOR_SKIP,
1191 static const struct got_error *
1192 paint_commit(struct got_object_qid *qid, int color)
1194 if (color < 0 || color >= nitems(findtwixt_colors))
1195 return got_error(GOT_ERR_RANGE);
1197 qid->data = (void *)&findtwixt_colors[color];
1198 return NULL;
1201 static const struct got_error *
1202 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1203 int color, struct got_repository *repo)
1205 const struct got_error *err;
1206 struct got_object_qid *qid;
1208 err = got_object_qid_alloc(&qid, id);
1209 if (err)
1210 return err;
1212 STAILQ_INSERT_TAIL(ids, qid, entry);
1213 return paint_commit(qid, color);
1216 struct append_id_arg {
1217 struct got_object_id **array;
1218 int idx;
1219 struct got_object_idset *drop;
1220 struct got_object_idset *skip;
1223 static const struct got_error *
1224 append_id(struct got_object_id *id, void *data, void *arg)
1226 struct append_id_arg *a = arg;
1228 if (got_object_idset_contains(a->skip, id) ||
1229 got_object_idset_contains(a->drop, id))
1230 return NULL;
1232 a->array[++a->idx] = got_object_id_dup(id);
1233 if (a->array[a->idx] == NULL)
1234 return got_error_from_errno("got_object_id_dup");
1236 return NULL;
1239 static const struct got_error *
1240 queue_commit_or_tag_id(struct got_object_id *id, int color,
1241 struct got_object_id_queue *ids, struct got_repository *repo)
1243 const struct got_error *err;
1244 struct got_tag_object *tag = NULL;
1245 int obj_type;
1247 err = got_object_get_type(&obj_type, repo, id);
1248 if (err)
1249 return err;
1251 if (obj_type == GOT_OBJ_TYPE_TAG) {
1252 err = got_object_open_as_tag(&tag, repo, id);
1253 if (err)
1254 return err;
1255 obj_type = got_object_tag_get_object_type(tag);
1256 id = got_object_tag_get_object_id(tag);
1259 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1260 err = queue_commit_id(ids, id, color, repo);
1261 if (err)
1262 goto done;
1264 done:
1265 if (tag)
1266 got_object_tag_close(tag);
1267 return err;
1270 static const struct got_error *
1271 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1272 struct got_object_idset *keep, struct got_object_idset *drop,
1273 struct got_object_idset *skip, struct got_repository *repo,
1274 got_pack_progress_cb progress_cb, void *progress_arg,
1275 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1277 const struct got_error *err = NULL;
1278 struct got_commit_object *commit = NULL;
1279 const struct got_object_id_queue *parents;
1280 struct got_object_qid *qid;
1281 int nqueued = nids, nskip = 0;
1283 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1284 int color;
1286 if (cancel_cb) {
1287 err = cancel_cb(cancel_arg);
1288 if (err)
1289 break;
1292 qid = STAILQ_FIRST(ids);
1293 STAILQ_REMOVE_HEAD(ids, entry);
1294 nqueued--;
1295 color = *((int *)qid->data);
1296 if (color == COLOR_SKIP)
1297 nskip--;
1299 if (got_object_idset_contains(skip, &qid->id)) {
1300 got_object_qid_free(qid);
1301 continue;
1304 switch (color) {
1305 case COLOR_KEEP:
1306 if (got_object_idset_contains(keep, &qid->id)) {
1307 got_object_qid_free(qid);
1308 continue;
1310 if (got_object_idset_contains(drop, &qid->id)) {
1311 err = paint_commit(qid, COLOR_SKIP);
1312 if (err)
1313 goto done;
1314 nskip++;
1315 } else
1316 (*ncolored)++;
1317 err = got_object_idset_add(keep, &qid->id, NULL);
1318 if (err)
1319 goto done;
1320 break;
1321 case COLOR_DROP:
1322 if (got_object_idset_contains(drop, &qid->id)) {
1323 got_object_qid_free(qid);
1324 continue;
1326 if (got_object_idset_contains(keep, &qid->id)) {
1327 err = paint_commit(qid, COLOR_SKIP);
1328 if (err)
1329 goto done;
1330 nskip++;
1331 } else
1332 (*ncolored)++;
1333 err = got_object_idset_add(drop, &qid->id, NULL);
1334 if (err)
1335 goto done;
1336 break;
1337 case COLOR_SKIP:
1338 if (!got_object_idset_contains(skip, &qid->id)) {
1339 err = got_object_idset_add(skip, &qid->id,
1340 NULL);
1341 if (err)
1342 goto done;
1344 break;
1345 default:
1346 /* should not happen */
1347 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1348 "%s invalid commit color %d", __func__, color);
1349 goto done;
1352 err = report_progress(progress_cb, progress_arg, rl,
1353 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1354 if (err)
1355 break;
1358 err = got_object_open_as_commit(&commit, repo, &qid->id);
1359 if (err)
1360 break;
1362 parents = got_object_commit_get_parent_ids(commit);
1363 if (parents) {
1364 struct got_object_qid *pid;
1365 color = *((int *)qid->data);
1366 STAILQ_FOREACH(pid, parents, entry) {
1367 err = queue_commit_id(ids, &pid->id, color,
1368 repo);
1369 if (err)
1370 break;
1371 nqueued++;
1372 if (color == COLOR_SKIP)
1373 nskip++;
1377 got_object_commit_close(commit);
1378 commit = NULL;
1379 got_object_qid_free(qid);
1381 done:
1382 if (commit)
1383 got_object_commit_close(commit);
1384 return err;
1387 static const struct got_error *
1388 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1389 struct got_object_id **head, int nhead,
1390 struct got_object_id **tail, int ntail,
1391 struct got_repository *repo,
1392 got_pack_progress_cb progress_cb, void *progress_arg,
1393 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1395 const struct got_error *err = NULL;
1396 struct got_object_id_queue ids;
1397 struct got_object_idset *keep, *drop, *skip = NULL;
1398 int i, nkeep;
1400 STAILQ_INIT(&ids);
1401 *res = NULL;
1402 *nres = 0;
1403 *ncolored = 0;
1405 keep = got_object_idset_alloc();
1406 if (keep == NULL)
1407 return got_error_from_errno("got_object_idset_alloc");
1409 drop = got_object_idset_alloc();
1410 if (drop == NULL) {
1411 err = got_error_from_errno("got_object_idset_alloc");
1412 goto done;
1415 skip = got_object_idset_alloc();
1416 if (skip == NULL) {
1417 err = got_error_from_errno("got_object_idset_alloc");
1418 goto done;
1421 for (i = 0; i < nhead; i++) {
1422 struct got_object_id *id = head[i];
1423 if (id == NULL)
1424 continue;
1425 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1426 if (err)
1427 goto done;
1430 for (i = 0; i < ntail; i++) {
1431 struct got_object_id *id = tail[i];
1432 if (id == NULL)
1433 continue;
1434 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1435 if (err)
1436 goto done;
1439 err = paint_commits(ncolored, &ids, nhead + ntail,
1440 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1441 cancel_cb, cancel_arg);
1442 if (err)
1443 goto done;
1445 nkeep = got_object_idset_num_elements(keep);
1446 if (nkeep > 0) {
1447 struct append_id_arg arg;
1448 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1449 if (arg.array == NULL) {
1450 err = got_error_from_errno("calloc");
1451 goto done;
1453 arg.idx = -1;
1454 arg.skip = skip;
1455 arg.drop = drop;
1456 err = got_object_idset_for_each(keep, append_id, &arg);
1457 if (err) {
1458 free(arg.array);
1459 goto done;
1461 *res = arg.array;
1462 *nres = arg.idx + 1;
1464 done:
1465 got_object_idset_free(keep);
1466 got_object_idset_free(drop);
1467 if (skip)
1468 got_object_idset_free(skip);
1469 got_object_id_queue_free(&ids);
1470 return err;
1473 static const struct got_error *
1474 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1475 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1476 struct got_object_id **ours, int nours, struct got_repository *repo,
1477 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1478 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1480 const struct got_error *err = NULL;
1481 struct got_object_id **ids = NULL;
1482 int i, nobj = 0, obj_type;
1483 struct got_object_idset *idset_exclude;
1485 idset_exclude = got_object_idset_alloc();
1486 if (idset_exclude == NULL)
1487 return got_error_from_errno("got_object_idset_alloc");
1489 *ncolored = 0;
1490 *nfound = 0;
1491 *ntrees = 0;
1493 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1494 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1495 if (err || nobj == 0)
1496 goto done;
1498 for (i = 0; i < ntheirs; i++) {
1499 struct got_object_id *id = theirs[i];
1500 if (id == NULL)
1501 continue;
1502 err = got_object_get_type(&obj_type, repo, id);
1503 if (err)
1504 return err;
1505 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1506 err = load_commit(0, idset, idset_exclude, id, repo,
1507 loose_obj_only, ncolored, nfound, ntrees,
1508 progress_cb, progress_arg, rl,
1509 cancel_cb, cancel_arg);
1510 if (err)
1511 goto done;
1512 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1513 err = load_tag(0, idset, idset_exclude, id, repo,
1514 loose_obj_only, ncolored, nfound, ntrees,
1515 progress_cb, progress_arg, rl,
1516 cancel_cb, cancel_arg);
1517 if (err)
1518 goto done;
1522 for (i = 0; i < nobj; i++) {
1523 err = load_commit(1, idset, idset_exclude,
1524 ids[i], repo, loose_obj_only, ncolored, nfound, ntrees,
1525 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1526 if (err)
1527 goto done;
1530 for (i = 0; i < nours; i++) {
1531 struct got_object_id *id = ours[i];
1532 struct got_pack_meta *m;
1533 if (id == NULL)
1534 continue;
1535 m = got_object_idset_get(idset, id);
1536 if (m == NULL) {
1537 err = got_object_get_type(&obj_type, repo, id);
1538 if (err)
1539 goto done;
1540 } else
1541 obj_type = m->obj_type;
1542 if (obj_type != GOT_OBJ_TYPE_TAG)
1543 continue;
1544 err = load_tag(1, idset, idset_exclude, id, repo,
1545 loose_obj_only, ncolored, nfound, ntrees,
1546 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1547 if (err)
1548 goto done;
1550 done:
1551 for (i = 0; i < nobj; i++) {
1552 free(ids[i]);
1554 free(ids);
1555 got_object_idset_free(idset_exclude);
1556 return err;
1559 const struct got_error *
1560 hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
1562 size_t n;
1564 SHA1Update(ctx, buf, len);
1565 n = fwrite(buf, 1, len, f);
1566 if (n != len)
1567 return got_ferror(f, GOT_ERR_IO);
1568 return NULL;
1571 const struct got_error *
1572 hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1574 unsigned char buf[65536];
1575 off_t remain = len;
1576 size_t n;
1578 while (remain > 0) {
1579 size_t copylen = MIN(sizeof(buf), remain);
1580 n = fread(buf, 1, copylen, fsrc);
1581 if (n != copylen)
1582 return got_ferror(fsrc, GOT_ERR_IO);
1583 SHA1Update(ctx, buf, copylen);
1584 n = fwrite(buf, 1, copylen, fdst);
1585 if (n != copylen)
1586 return got_ferror(fdst, GOT_ERR_IO);
1587 remain -= copylen;
1590 return NULL;
1593 static void
1594 putbe32(char *b, uint32_t n)
1596 b[0] = n >> 24;
1597 b[1] = n >> 16;
1598 b[2] = n >> 8;
1599 b[3] = n >> 0;
1602 static int
1603 write_order_cmp(const void *pa, const void *pb)
1605 struct got_pack_meta *a, *b, *ahd, *bhd;
1607 a = *(struct got_pack_meta **)pa;
1608 b = *(struct got_pack_meta **)pb;
1609 ahd = (a->head == NULL) ? a : a->head;
1610 bhd = (b->head == NULL) ? b : b->head;
1611 if (bhd->mtime < ahd->mtime)
1612 return -1;
1613 if (bhd->mtime > ahd->mtime)
1614 return 1;
1615 if (bhd < ahd)
1616 return -1;
1617 if (bhd > ahd)
1618 return 1;
1619 if (a->nchain != b->nchain)
1620 return a->nchain - b->nchain;
1621 if (a->mtime < b->mtime)
1622 return -1;
1623 if (a->mtime > b->mtime)
1624 return 1;
1625 return got_object_id_cmp(&a->id, &b->id);
1628 static int
1629 reuse_write_order_cmp(const void *pa, const void *pb)
1631 struct got_pack_meta *a, *b;
1633 a = *(struct got_pack_meta **)pa;
1634 b = *(struct got_pack_meta **)pb;
1636 if (a->reused_delta_offset < b->reused_delta_offset)
1637 return -1;
1638 if (a->reused_delta_offset > b->reused_delta_offset)
1639 return 1;
1640 return 0;
1643 static const struct got_error *
1644 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1646 size_t i;
1648 *hdrlen = 0;
1650 hdr[0] = obj_type << 4;
1651 hdr[0] |= len & 0xf;
1652 len >>= 4;
1653 for (i = 1; len != 0; i++){
1654 if (i >= bufsize)
1655 return got_error(GOT_ERR_NO_SPACE);
1656 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1657 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1658 len >>= GOT_DELTA_SIZE_SHIFT;
1661 *hdrlen = i;
1662 return NULL;
1665 static int
1666 packoff(char *hdr, off_t off)
1668 int i, j;
1669 char rbuf[8];
1671 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1672 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1673 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1674 GOT_DELTA_SIZE_MORE;
1677 j = 0;
1678 while (i > 0)
1679 hdr[j++] = rbuf[--i];
1680 return j;
1683 static const struct got_error *
1684 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1685 struct got_pack_meta *m)
1687 const struct got_error *err;
1688 char buf[32];
1689 int nh;
1691 if (m->prev->off != 0) {
1692 err = packhdr(&nh, buf, sizeof(buf),
1693 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1694 if (err)
1695 return err;
1696 nh += packoff(buf + nh, m->off - m->prev->off);
1697 err = hwrite(packfile, buf, nh, ctx);
1698 if (err)
1699 return err;
1700 *packfile_size += nh;
1701 } else {
1702 err = packhdr(&nh, buf, sizeof(buf),
1703 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1704 if (err)
1705 return err;
1706 err = hwrite(packfile, buf, nh, ctx);
1707 if (err)
1708 return err;
1709 *packfile_size += nh;
1710 err = hwrite(packfile, m->prev->id.sha1,
1711 sizeof(m->prev->id.sha1), ctx);
1712 if (err)
1713 return err;
1714 *packfile_size += sizeof(m->prev->id.sha1);
1717 return NULL;
1720 static const struct got_error *
1721 write_packed_object(off_t *packfile_size, FILE *packfile,
1722 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1723 SHA1_CTX *ctx, struct got_repository *repo)
1725 const struct got_error *err = NULL;
1726 struct got_deflate_checksum csum;
1727 char buf[32];
1728 int nh;
1729 struct got_raw_object *raw = NULL;
1730 off_t outlen;
1732 csum.output_sha1 = ctx;
1733 csum.output_crc = NULL;
1735 m->off = ftello(packfile);
1736 if (m->delta_len == 0) {
1737 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1738 if (err)
1739 goto done;
1740 err = packhdr(&nh, buf, sizeof(buf),
1741 m->obj_type, raw->size);
1742 if (err)
1743 goto done;
1744 err = hwrite(packfile, buf, nh, ctx);
1745 if (err)
1746 goto done;
1747 *packfile_size += nh;
1748 if (raw->f == NULL) {
1749 err = got_deflate_to_file_mmap(&outlen,
1750 raw->data + raw->hdrlen, 0, raw->size,
1751 packfile, &csum);
1752 if (err)
1753 goto done;
1754 } else {
1755 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1756 == -1) {
1757 err = got_error_from_errno("fseeko");
1758 goto done;
1760 err = got_deflate_to_file(&outlen, raw->f,
1761 raw->size, packfile, &csum);
1762 if (err)
1763 goto done;
1765 *packfile_size += outlen;
1766 got_object_raw_close(raw);
1767 raw = NULL;
1768 } else if (m->delta_buf) {
1769 err = deltahdr(packfile_size, ctx, packfile, m);
1770 if (err)
1771 goto done;
1772 err = hwrite(packfile, m->delta_buf,
1773 m->delta_compressed_len, ctx);
1774 if (err)
1775 goto done;
1776 *packfile_size += m->delta_compressed_len;
1777 free(m->delta_buf);
1778 m->delta_buf = NULL;
1779 } else {
1780 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1781 == -1) {
1782 err = got_error_from_errno("fseeko");
1783 goto done;
1785 err = deltahdr(packfile_size, ctx, packfile, m);
1786 if (err)
1787 goto done;
1788 err = hcopy(delta_cache, packfile,
1789 m->delta_compressed_len, ctx);
1790 if (err)
1791 goto done;
1792 *packfile_size += m->delta_compressed_len;
1794 done:
1795 if (raw)
1796 got_object_raw_close(raw);
1797 return err;
1800 static const struct got_error *
1801 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1802 struct got_pack_meta **deltify, int ndeltify,
1803 struct got_pack_meta **reuse, int nreuse,
1804 int ncolored, int nfound, int ntrees, int nours,
1805 struct got_repository *repo,
1806 got_pack_progress_cb progress_cb, void *progress_arg,
1807 struct got_ratelimit *rl,
1808 got_cancel_cb cancel_cb, void *cancel_arg)
1810 const struct got_error *err = NULL;
1811 int i;
1812 SHA1_CTX ctx;
1813 struct got_pack_meta *m;
1814 char buf[32];
1815 size_t n;
1816 off_t packfile_size = 0;
1817 int outfd = -1;
1819 SHA1Init(&ctx);
1821 err = hwrite(packfile, "PACK", 4, &ctx);
1822 if (err)
1823 return err;
1824 putbe32(buf, GOT_PACKFILE_VERSION);
1825 err = hwrite(packfile, buf, 4, &ctx);
1826 if (err)
1827 goto done;
1828 putbe32(buf, ndeltify + nreuse);
1829 err = hwrite(packfile, buf, 4, &ctx);
1830 if (err)
1831 goto done;
1833 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1834 write_order_cmp);
1835 for (i = 0; i < ndeltify; i++) {
1836 err = report_progress(progress_cb, progress_arg, rl,
1837 ncolored, nfound, ntrees, packfile_size, nours,
1838 ndeltify + nreuse, ndeltify + nreuse, i);
1839 if (err)
1840 goto done;
1841 m = deltify[i];
1842 err = write_packed_object(&packfile_size, packfile,
1843 delta_cache, m, &outfd, &ctx, repo);
1844 if (err)
1845 goto done;
1848 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1849 reuse_write_order_cmp);
1850 for (i = 0; i < nreuse; i++) {
1851 err = report_progress(progress_cb, progress_arg, rl,
1852 ncolored, nfound, ntrees, packfile_size, nours,
1853 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1854 if (err)
1855 goto done;
1856 m = reuse[i];
1857 err = write_packed_object(&packfile_size, packfile,
1858 delta_cache, m, &outfd, &ctx, repo);
1859 if (err)
1860 goto done;
1863 SHA1Final(pack_sha1, &ctx);
1864 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1865 if (n != SHA1_DIGEST_LENGTH)
1866 err = got_ferror(packfile, GOT_ERR_IO);
1867 packfile_size += SHA1_DIGEST_LENGTH;
1868 packfile_size += sizeof(struct got_packfile_hdr);
1869 if (progress_cb) {
1870 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1871 packfile_size, nours, ndeltify + nreuse,
1872 ndeltify + nreuse, ndeltify + nreuse);
1873 if (err)
1874 goto done;
1876 done:
1877 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1878 err = got_error_from_errno("close");
1879 return err;
1882 static const struct got_error *
1883 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1885 struct got_pack_meta *m = data;
1886 struct got_pack_metavec *v = arg;
1888 if (m->have_reused_delta)
1889 return NULL;
1891 return add_meta(m, v);
1894 const struct got_error *
1895 got_pack_create(uint8_t *packsha1, FILE *packfile,
1896 struct got_object_id **theirs, int ntheirs,
1897 struct got_object_id **ours, int nours,
1898 struct got_repository *repo, int loose_obj_only, int allow_empty,
1899 got_pack_progress_cb progress_cb, void *progress_arg,
1900 got_cancel_cb cancel_cb, void *cancel_arg)
1902 const struct got_error *err;
1903 int delta_cache_fd = -1;
1904 FILE *delta_cache = NULL;
1905 struct got_object_idset *idset;
1906 struct got_ratelimit rl;
1907 struct got_pack_metavec deltify, reuse;
1908 int ncolored = 0, nfound = 0, ntrees = 0;
1909 size_t ndeltify;
1911 memset(&deltify, 0, sizeof(deltify));
1912 memset(&reuse, 0, sizeof(reuse));
1914 got_ratelimit_init(&rl, 0, 500);
1916 idset = got_object_idset_alloc();
1917 if (idset == NULL)
1918 return got_error_from_errno("got_object_idset_alloc");
1920 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1921 ntheirs, ours, nours, repo, loose_obj_only,
1922 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1923 if (err)
1924 return err;
1926 if (progress_cb) {
1927 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1928 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1929 if (err)
1930 goto done;
1933 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1934 err = got_error(GOT_ERR_CANNOT_PACK);
1935 goto done;
1938 delta_cache_fd = got_opentempfd();
1939 if (delta_cache_fd == -1) {
1940 err = got_error_from_errno("got_opentemp");
1941 goto done;
1944 reuse.metasz = 64;
1945 reuse.meta = calloc(reuse.metasz,
1946 sizeof(struct got_pack_meta *));
1947 if (reuse.meta == NULL) {
1948 err = got_error_from_errno("calloc");
1949 goto done;
1952 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1953 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1954 cancel_cb, cancel_arg);
1955 if (err)
1956 goto done;
1958 delta_cache = fdopen(delta_cache_fd, "a+");
1959 if (delta_cache == NULL) {
1960 err = got_error_from_errno("fdopen");
1961 goto done;
1963 delta_cache_fd = -1;
1965 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1966 err = got_error_from_errno("fseeko");
1967 goto done;
1970 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1971 if (ndeltify > 0) {
1972 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1973 if (deltify.meta == NULL) {
1974 err = got_error_from_errno("calloc");
1975 goto done;
1977 deltify.metasz = ndeltify;
1979 err = got_object_idset_for_each(idset, add_meta_idset_cb,
1980 &deltify);
1981 if (err)
1982 goto done;
1983 if (deltify.nmeta > 0) {
1984 err = pick_deltas(deltify.meta, deltify.nmeta,
1985 ncolored, nfound, ntrees, nours, reuse.nmeta,
1986 delta_cache, repo, progress_cb, progress_arg, &rl,
1987 cancel_cb, cancel_arg);
1988 if (err)
1989 goto done;
1993 if (fflush(delta_cache) == EOF) {
1994 err = got_error_from_errno("fflush");
1995 goto done;
1997 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1998 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1999 nours, repo, progress_cb, progress_arg, &rl,
2000 cancel_cb, cancel_arg);
2001 if (err)
2002 goto done;
2003 done:
2004 free_nmeta(deltify.meta, deltify.nmeta);
2005 free_nmeta(reuse.meta, reuse.nmeta);
2006 got_object_idset_free(idset);
2007 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2008 err = got_error_from_errno("close");
2009 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2010 err = got_error_from_errno("fclose");
2011 return err;