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_id *tree_id,
943 const char *dpath, time_t mtime, struct got_repository *repo,
944 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
945 got_pack_progress_cb progress_cb, void *progress_arg,
946 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
948 const struct got_error *err;
949 struct got_tree_object *tree;
950 char *p = NULL;
951 int i;
953 err = got_object_open_as_tree(&tree, repo, tree_id);
954 if (err)
955 return err;
957 (*ntrees)++;
958 err = report_progress(progress_cb, progress_arg, rl,
959 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
960 if (err)
961 return err;
963 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
964 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
965 struct got_object_id *id = got_tree_entry_get_id(e);
966 mode_t mode = got_tree_entry_get_mode(e);
968 if (cancel_cb) {
969 err = (*cancel_cb)(cancel_arg);
970 if (err)
971 break;
974 if (got_object_tree_entry_is_submodule(e) ||
975 got_object_idset_contains(idset, id))
976 continue;
978 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
979 got_tree_entry_get_name(e)) == -1) {
980 err = got_error_from_errno("asprintf");
981 break;
984 if (S_ISDIR(mode)) {
985 struct got_object_qid *qid;
986 err = got_object_qid_alloc(&qid, id);
987 if (err)
988 break;
989 STAILQ_INSERT_TAIL(ids, qid, entry);
990 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
991 err = add_object(want_meta, idset, id, p,
992 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
993 ncolored, nfound, ntrees,
994 progress_cb, progress_arg, rl);
995 if (err)
996 break;
998 free(p);
999 p = NULL;
1002 got_object_tree_close(tree);
1003 free(p);
1004 return err;
1007 static const struct got_error *
1008 load_tree(int want_meta, struct got_object_idset *idset,
1009 struct got_object_id *tree_id, const char *dpath, time_t mtime,
1010 struct got_repository *repo, int loose_obj_only,
1011 int *ncolored, int *nfound, int *ntrees,
1012 got_pack_progress_cb progress_cb, void *progress_arg,
1013 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1015 const struct got_error *err = NULL;
1016 struct got_object_id_queue tree_ids;
1017 struct got_object_qid *qid;
1019 if (got_object_idset_contains(idset, tree_id))
1020 return NULL;
1022 err = got_object_qid_alloc(&qid, tree_id);
1023 if (err)
1024 return err;
1026 STAILQ_INIT(&tree_ids);
1027 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1029 while (!STAILQ_EMPTY(&tree_ids)) {
1030 if (cancel_cb) {
1031 err = (*cancel_cb)(cancel_arg);
1032 if (err)
1033 break;
1036 qid = STAILQ_FIRST(&tree_ids);
1037 STAILQ_REMOVE_HEAD(&tree_ids, entry);
1039 if (got_object_idset_contains(idset, &qid->id)) {
1040 got_object_qid_free(qid);
1041 continue;
1044 err = add_object(want_meta, idset, &qid->id, dpath,
1045 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo,
1046 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1047 if (err) {
1048 got_object_qid_free(qid);
1049 break;
1052 err = load_tree_entries(&tree_ids, want_meta, idset, &qid->id,
1053 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1054 ntrees, progress_cb, progress_arg, rl,
1055 cancel_cb, cancel_arg);
1056 got_object_qid_free(qid);
1057 if (err)
1058 break;
1061 got_object_id_queue_free(&tree_ids);
1062 return err;
1065 static const struct got_error *
1066 load_commit(int want_meta, struct got_object_idset *idset,
1067 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1068 int *ncolored, int *nfound, int *ntrees,
1069 got_pack_progress_cb progress_cb, void *progress_arg,
1070 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1072 const struct got_error *err;
1073 struct got_commit_object *commit;
1075 if (got_object_idset_contains(idset, id))
1076 return NULL;
1078 if (loose_obj_only) {
1079 int is_packed;
1080 err = search_packidx(&is_packed, id, repo);
1081 if (err)
1082 return err;
1083 if (is_packed && want_meta)
1084 return NULL;
1087 err = got_object_open_as_commit(&commit, repo, id);
1088 if (err)
1089 return err;
1091 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1092 got_object_commit_get_committer_time(commit),
1093 loose_obj_only, repo,
1094 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1095 if (err)
1096 goto done;
1098 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1099 "", got_object_commit_get_committer_time(commit),
1100 repo, loose_obj_only, ncolored, nfound, ntrees,
1101 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1102 done:
1103 got_object_commit_close(commit);
1104 return err;
1107 static const struct got_error *
1108 load_tag(int want_meta, struct got_object_idset *idset,
1109 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1110 int *ncolored, int *nfound, int *ntrees,
1111 got_pack_progress_cb progress_cb, void *progress_arg,
1112 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1114 const struct got_error *err;
1115 struct got_tag_object *tag = NULL;
1117 if (got_object_idset_contains(idset, id))
1118 return NULL;
1120 if (loose_obj_only) {
1121 int is_packed;
1122 err = search_packidx(&is_packed, id, repo);
1123 if (err)
1124 return err;
1125 if (is_packed && want_meta)
1126 return NULL;
1129 err = got_object_open_as_tag(&tag, repo, id);
1130 if (err)
1131 return err;
1133 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1134 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1135 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1136 if (err)
1137 goto done;
1139 switch (got_object_tag_get_object_type(tag)) {
1140 case GOT_OBJ_TYPE_COMMIT:
1141 err = load_commit(want_meta, idset,
1142 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1143 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1144 cancel_cb, cancel_arg);
1145 break;
1146 case GOT_OBJ_TYPE_TREE:
1147 err = load_tree(want_meta, idset,
1148 got_object_tag_get_object_id(tag), "",
1149 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1150 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1151 cancel_cb, cancel_arg);
1152 break;
1153 default:
1154 break;
1157 done:
1158 got_object_tag_close(tag);
1159 return err;
1162 enum findtwixt_color {
1163 COLOR_KEEP = 0,
1164 COLOR_DROP,
1165 COLOR_BLANK,
1166 COLOR_SKIP,
1169 static const int findtwixt_colors[] = {
1170 COLOR_KEEP,
1171 COLOR_DROP,
1172 COLOR_BLANK,
1173 COLOR_SKIP,
1176 static const struct got_error *
1177 paint_commit(struct got_object_qid *qid, int color)
1179 if (color < 0 || color >= nitems(findtwixt_colors))
1180 return got_error(GOT_ERR_RANGE);
1182 qid->data = (void *)&findtwixt_colors[color];
1183 return NULL;
1186 static const struct got_error *
1187 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1188 int color, struct got_repository *repo)
1190 const struct got_error *err;
1191 struct got_object_qid *qid;
1193 err = got_object_qid_alloc(&qid, id);
1194 if (err)
1195 return err;
1197 STAILQ_INSERT_TAIL(ids, qid, entry);
1198 return paint_commit(qid, color);
1201 struct append_id_arg {
1202 struct got_object_id **array;
1203 int idx;
1204 struct got_object_idset *drop;
1205 struct got_object_idset *skip;
1208 static const struct got_error *
1209 append_id(struct got_object_id *id, void *data, void *arg)
1211 struct append_id_arg *a = arg;
1213 if (got_object_idset_contains(a->skip, id) ||
1214 got_object_idset_contains(a->drop, id))
1215 return NULL;
1217 a->array[++a->idx] = got_object_id_dup(id);
1218 if (a->array[a->idx] == NULL)
1219 return got_error_from_errno("got_object_id_dup");
1221 return NULL;
1224 static const struct got_error *
1225 queue_commit_or_tag_id(struct got_object_id *id, int color,
1226 struct got_object_id_queue *ids, struct got_repository *repo)
1228 const struct got_error *err;
1229 struct got_tag_object *tag = NULL;
1230 int obj_type;
1232 err = got_object_get_type(&obj_type, repo, id);
1233 if (err)
1234 return err;
1236 if (obj_type == GOT_OBJ_TYPE_TAG) {
1237 err = got_object_open_as_tag(&tag, repo, id);
1238 if (err)
1239 return err;
1240 obj_type = got_object_tag_get_object_type(tag);
1241 id = got_object_tag_get_object_id(tag);
1244 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1245 err = queue_commit_id(ids, id, color, repo);
1246 if (err)
1247 goto done;
1249 done:
1250 if (tag)
1251 got_object_tag_close(tag);
1252 return err;
1255 static const struct got_error *
1256 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1257 struct got_object_idset *keep, struct got_object_idset *drop,
1258 struct got_object_idset *skip, struct got_repository *repo,
1259 got_pack_progress_cb progress_cb, void *progress_arg,
1260 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1262 const struct got_error *err = NULL;
1263 struct got_commit_object *commit = NULL;
1264 const struct got_object_id_queue *parents;
1265 struct got_object_qid *qid;
1266 int nqueued = nids, nskip = 0;
1268 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1269 int color;
1271 if (cancel_cb) {
1272 err = cancel_cb(cancel_arg);
1273 if (err)
1274 break;
1277 qid = STAILQ_FIRST(ids);
1278 STAILQ_REMOVE_HEAD(ids, entry);
1279 nqueued--;
1280 color = *((int *)qid->data);
1281 if (color == COLOR_SKIP)
1282 nskip--;
1284 if (got_object_idset_contains(skip, &qid->id)) {
1285 got_object_qid_free(qid);
1286 continue;
1289 switch (color) {
1290 case COLOR_KEEP:
1291 if (got_object_idset_contains(keep, &qid->id)) {
1292 got_object_qid_free(qid);
1293 continue;
1295 if (got_object_idset_contains(drop, &qid->id)) {
1296 err = paint_commit(qid, COLOR_SKIP);
1297 if (err)
1298 goto done;
1299 nskip++;
1300 } else
1301 (*ncolored)++;
1302 err = got_object_idset_add(keep, &qid->id, NULL);
1303 if (err)
1304 goto done;
1305 break;
1306 case COLOR_DROP:
1307 if (got_object_idset_contains(drop, &qid->id)) {
1308 got_object_qid_free(qid);
1309 continue;
1311 if (got_object_idset_contains(keep, &qid->id)) {
1312 err = paint_commit(qid, COLOR_SKIP);
1313 if (err)
1314 goto done;
1315 nskip++;
1316 } else
1317 (*ncolored)++;
1318 err = got_object_idset_add(drop, &qid->id, NULL);
1319 if (err)
1320 goto done;
1321 break;
1322 case COLOR_SKIP:
1323 if (!got_object_idset_contains(skip, &qid->id)) {
1324 err = got_object_idset_add(skip, &qid->id,
1325 NULL);
1326 if (err)
1327 goto done;
1329 break;
1330 default:
1331 /* should not happen */
1332 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1333 "%s invalid commit color %d", __func__, color);
1334 goto done;
1337 err = report_progress(progress_cb, progress_arg, rl,
1338 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1339 if (err)
1340 break;
1343 err = got_object_open_as_commit(&commit, repo, &qid->id);
1344 if (err)
1345 break;
1347 parents = got_object_commit_get_parent_ids(commit);
1348 if (parents) {
1349 struct got_object_qid *pid;
1350 color = *((int *)qid->data);
1351 STAILQ_FOREACH(pid, parents, entry) {
1352 err = queue_commit_id(ids, &pid->id, color,
1353 repo);
1354 if (err)
1355 break;
1356 nqueued++;
1357 if (color == COLOR_SKIP)
1358 nskip++;
1362 got_object_commit_close(commit);
1363 commit = NULL;
1364 got_object_qid_free(qid);
1366 done:
1367 if (commit)
1368 got_object_commit_close(commit);
1369 return err;
1372 static const struct got_error *
1373 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1374 struct got_object_id **head, int nhead,
1375 struct got_object_id **tail, int ntail,
1376 struct got_repository *repo,
1377 got_pack_progress_cb progress_cb, void *progress_arg,
1378 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1380 const struct got_error *err = NULL;
1381 struct got_object_id_queue ids;
1382 struct got_object_idset *keep, *drop, *skip = NULL;
1383 int i, nkeep;
1385 STAILQ_INIT(&ids);
1386 *res = NULL;
1387 *nres = 0;
1388 *ncolored = 0;
1390 keep = got_object_idset_alloc();
1391 if (keep == NULL)
1392 return got_error_from_errno("got_object_idset_alloc");
1394 drop = got_object_idset_alloc();
1395 if (drop == NULL) {
1396 err = got_error_from_errno("got_object_idset_alloc");
1397 goto done;
1400 skip = got_object_idset_alloc();
1401 if (skip == NULL) {
1402 err = got_error_from_errno("got_object_idset_alloc");
1403 goto done;
1406 for (i = 0; i < nhead; i++) {
1407 struct got_object_id *id = head[i];
1408 if (id == NULL)
1409 continue;
1410 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1411 if (err)
1412 goto done;
1415 for (i = 0; i < ntail; i++) {
1416 struct got_object_id *id = tail[i];
1417 if (id == NULL)
1418 continue;
1419 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1420 if (err)
1421 goto done;
1424 err = paint_commits(ncolored, &ids, nhead + ntail,
1425 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1426 cancel_cb, cancel_arg);
1427 if (err)
1428 goto done;
1430 nkeep = got_object_idset_num_elements(keep);
1431 if (nkeep > 0) {
1432 struct append_id_arg arg;
1433 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1434 if (arg.array == NULL) {
1435 err = got_error_from_errno("calloc");
1436 goto done;
1438 arg.idx = -1;
1439 arg.skip = skip;
1440 arg.drop = drop;
1441 err = got_object_idset_for_each(keep, append_id, &arg);
1442 if (err) {
1443 free(arg.array);
1444 goto done;
1446 *res = arg.array;
1447 *nres = arg.idx + 1;
1449 done:
1450 got_object_idset_free(keep);
1451 got_object_idset_free(drop);
1452 if (skip)
1453 got_object_idset_free(skip);
1454 got_object_id_queue_free(&ids);
1455 return err;
1458 static const struct got_error *
1459 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1460 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1461 struct got_object_id **ours, int nours, struct got_repository *repo,
1462 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1463 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1465 const struct got_error *err = NULL;
1466 struct got_object_id **ids = NULL;
1467 int i, nobj = 0, obj_type;
1469 *ncolored = 0;
1470 *nfound = 0;
1471 *ntrees = 0;
1473 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1474 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1475 if (err || nobj == 0)
1476 goto done;
1478 for (i = 0; i < ntheirs; i++) {
1479 struct got_object_id *id = theirs[i];
1480 if (id == NULL)
1481 continue;
1482 err = got_object_get_type(&obj_type, repo, id);
1483 if (err)
1484 return err;
1485 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1486 err = load_commit(0, idset, id, repo,
1487 loose_obj_only, ncolored, nfound, ntrees,
1488 progress_cb, progress_arg, rl,
1489 cancel_cb, cancel_arg);
1490 if (err)
1491 goto done;
1492 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1493 err = load_tag(0, idset, id, repo,
1494 loose_obj_only, ncolored, nfound, ntrees,
1495 progress_cb, progress_arg, rl,
1496 cancel_cb, cancel_arg);
1497 if (err)
1498 goto done;
1502 for (i = 0; i < nobj; i++) {
1503 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1504 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1505 cancel_cb, cancel_arg);
1506 if (err)
1507 goto done;
1510 for (i = 0; i < nours; i++) {
1511 struct got_object_id *id = ours[i];
1512 struct got_pack_meta *m;
1513 if (id == NULL)
1514 continue;
1515 m = got_object_idset_get(idset, id);
1516 if (m == NULL) {
1517 err = got_object_get_type(&obj_type, repo, id);
1518 if (err)
1519 goto done;
1520 } else
1521 obj_type = m->obj_type;
1522 if (obj_type != GOT_OBJ_TYPE_TAG)
1523 continue;
1524 err = load_tag(1, idset, id, repo, loose_obj_only,
1525 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1526 cancel_cb, cancel_arg);
1527 if (err)
1528 goto done;
1530 done:
1531 for (i = 0; i < nobj; i++) {
1532 free(ids[i]);
1534 free(ids);
1535 return err;
1538 const struct got_error *
1539 hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
1541 size_t n;
1543 SHA1Update(ctx, buf, len);
1544 n = fwrite(buf, 1, len, f);
1545 if (n != len)
1546 return got_ferror(f, GOT_ERR_IO);
1547 return NULL;
1550 const struct got_error *
1551 hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1553 unsigned char buf[65536];
1554 off_t remain = len;
1555 size_t n;
1557 while (remain > 0) {
1558 size_t copylen = MIN(sizeof(buf), remain);
1559 n = fread(buf, 1, copylen, fsrc);
1560 if (n != copylen)
1561 return got_ferror(fsrc, GOT_ERR_IO);
1562 SHA1Update(ctx, buf, copylen);
1563 n = fwrite(buf, 1, copylen, fdst);
1564 if (n != copylen)
1565 return got_ferror(fdst, GOT_ERR_IO);
1566 remain -= copylen;
1569 return NULL;
1572 static void
1573 putbe32(char *b, uint32_t n)
1575 b[0] = n >> 24;
1576 b[1] = n >> 16;
1577 b[2] = n >> 8;
1578 b[3] = n >> 0;
1581 static int
1582 write_order_cmp(const void *pa, const void *pb)
1584 struct got_pack_meta *a, *b, *ahd, *bhd;
1586 a = *(struct got_pack_meta **)pa;
1587 b = *(struct got_pack_meta **)pb;
1588 ahd = (a->head == NULL) ? a : a->head;
1589 bhd = (b->head == NULL) ? b : b->head;
1590 if (bhd->mtime < ahd->mtime)
1591 return -1;
1592 if (bhd->mtime > ahd->mtime)
1593 return 1;
1594 if (bhd < ahd)
1595 return -1;
1596 if (bhd > ahd)
1597 return 1;
1598 if (a->nchain != b->nchain)
1599 return a->nchain - b->nchain;
1600 if (a->mtime < b->mtime)
1601 return -1;
1602 if (a->mtime > b->mtime)
1603 return 1;
1604 return got_object_id_cmp(&a->id, &b->id);
1607 static int
1608 reuse_write_order_cmp(const void *pa, const void *pb)
1610 struct got_pack_meta *a, *b;
1612 a = *(struct got_pack_meta **)pa;
1613 b = *(struct got_pack_meta **)pb;
1615 if (a->reused_delta_offset < b->reused_delta_offset)
1616 return -1;
1617 if (a->reused_delta_offset > b->reused_delta_offset)
1618 return 1;
1619 return 0;
1622 static const struct got_error *
1623 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1625 size_t i;
1627 *hdrlen = 0;
1629 hdr[0] = obj_type << 4;
1630 hdr[0] |= len & 0xf;
1631 len >>= 4;
1632 for (i = 1; len != 0; i++){
1633 if (i >= bufsize)
1634 return got_error(GOT_ERR_NO_SPACE);
1635 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1636 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1637 len >>= GOT_DELTA_SIZE_SHIFT;
1640 *hdrlen = i;
1641 return NULL;
1644 static int
1645 packoff(char *hdr, off_t off)
1647 int i, j;
1648 char rbuf[8];
1650 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1651 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1652 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1653 GOT_DELTA_SIZE_MORE;
1656 j = 0;
1657 while (i > 0)
1658 hdr[j++] = rbuf[--i];
1659 return j;
1662 static const struct got_error *
1663 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1664 struct got_pack_meta *m)
1666 const struct got_error *err;
1667 char buf[32];
1668 int nh;
1670 if (m->prev->off != 0) {
1671 err = packhdr(&nh, buf, sizeof(buf),
1672 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1673 if (err)
1674 return err;
1675 nh += packoff(buf + nh, m->off - m->prev->off);
1676 err = hwrite(packfile, buf, nh, ctx);
1677 if (err)
1678 return err;
1679 *packfile_size += nh;
1680 } else {
1681 err = packhdr(&nh, buf, sizeof(buf),
1682 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1683 if (err)
1684 return err;
1685 err = hwrite(packfile, buf, nh, ctx);
1686 if (err)
1687 return err;
1688 *packfile_size += nh;
1689 err = hwrite(packfile, m->prev->id.sha1,
1690 sizeof(m->prev->id.sha1), ctx);
1691 if (err)
1692 return err;
1693 *packfile_size += sizeof(m->prev->id.sha1);
1696 return NULL;
1699 static const struct got_error *
1700 write_packed_object(off_t *packfile_size, FILE *packfile,
1701 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1702 SHA1_CTX *ctx, struct got_repository *repo)
1704 const struct got_error *err = NULL;
1705 struct got_deflate_checksum csum;
1706 char buf[32];
1707 int nh;
1708 struct got_raw_object *raw = NULL;
1709 off_t outlen;
1711 csum.output_sha1 = ctx;
1712 csum.output_crc = NULL;
1714 m->off = ftello(packfile);
1715 if (m->delta_len == 0) {
1716 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1717 if (err)
1718 goto done;
1719 err = packhdr(&nh, buf, sizeof(buf),
1720 m->obj_type, raw->size);
1721 if (err)
1722 goto done;
1723 err = hwrite(packfile, buf, nh, ctx);
1724 if (err)
1725 goto done;
1726 *packfile_size += nh;
1727 if (raw->f == NULL) {
1728 err = got_deflate_to_file_mmap(&outlen,
1729 raw->data + raw->hdrlen, 0, raw->size,
1730 packfile, &csum);
1731 if (err)
1732 goto done;
1733 } else {
1734 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1735 == -1) {
1736 err = got_error_from_errno("fseeko");
1737 goto done;
1739 err = got_deflate_to_file(&outlen, raw->f,
1740 raw->size, packfile, &csum);
1741 if (err)
1742 goto done;
1744 *packfile_size += outlen;
1745 got_object_raw_close(raw);
1746 raw = NULL;
1747 } else if (m->delta_buf) {
1748 err = deltahdr(packfile_size, ctx, packfile, m);
1749 if (err)
1750 goto done;
1751 err = hwrite(packfile, m->delta_buf,
1752 m->delta_compressed_len, ctx);
1753 if (err)
1754 goto done;
1755 *packfile_size += m->delta_compressed_len;
1756 free(m->delta_buf);
1757 m->delta_buf = NULL;
1758 } else {
1759 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1760 == -1) {
1761 err = got_error_from_errno("fseeko");
1762 goto done;
1764 err = deltahdr(packfile_size, ctx, packfile, m);
1765 if (err)
1766 goto done;
1767 err = hcopy(delta_cache, packfile,
1768 m->delta_compressed_len, ctx);
1769 if (err)
1770 goto done;
1771 *packfile_size += m->delta_compressed_len;
1773 done:
1774 if (raw)
1775 got_object_raw_close(raw);
1776 return err;
1779 static const struct got_error *
1780 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1781 struct got_pack_meta **deltify, int ndeltify,
1782 struct got_pack_meta **reuse, int nreuse,
1783 int ncolored, int nfound, int ntrees, int nours,
1784 struct got_repository *repo,
1785 got_pack_progress_cb progress_cb, void *progress_arg,
1786 struct got_ratelimit *rl,
1787 got_cancel_cb cancel_cb, void *cancel_arg)
1789 const struct got_error *err = NULL;
1790 int i;
1791 SHA1_CTX ctx;
1792 struct got_pack_meta *m;
1793 char buf[32];
1794 size_t n;
1795 off_t packfile_size = 0;
1796 int outfd = -1;
1798 SHA1Init(&ctx);
1800 err = hwrite(packfile, "PACK", 4, &ctx);
1801 if (err)
1802 return err;
1803 putbe32(buf, GOT_PACKFILE_VERSION);
1804 err = hwrite(packfile, buf, 4, &ctx);
1805 if (err)
1806 goto done;
1807 putbe32(buf, ndeltify + nreuse);
1808 err = hwrite(packfile, buf, 4, &ctx);
1809 if (err)
1810 goto done;
1812 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1813 write_order_cmp);
1814 for (i = 0; i < ndeltify; i++) {
1815 err = report_progress(progress_cb, progress_arg, rl,
1816 ncolored, nfound, ntrees, packfile_size, nours,
1817 ndeltify + nreuse, ndeltify + nreuse, i);
1818 if (err)
1819 goto done;
1820 m = deltify[i];
1821 err = write_packed_object(&packfile_size, packfile,
1822 delta_cache, m, &outfd, &ctx, repo);
1823 if (err)
1824 goto done;
1827 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1828 reuse_write_order_cmp);
1829 for (i = 0; i < nreuse; i++) {
1830 err = report_progress(progress_cb, progress_arg, rl,
1831 ncolored, nfound, ntrees, packfile_size, nours,
1832 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1833 if (err)
1834 goto done;
1835 m = reuse[i];
1836 err = write_packed_object(&packfile_size, packfile,
1837 delta_cache, m, &outfd, &ctx, repo);
1838 if (err)
1839 goto done;
1842 SHA1Final(pack_sha1, &ctx);
1843 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1844 if (n != SHA1_DIGEST_LENGTH)
1845 err = got_ferror(packfile, GOT_ERR_IO);
1846 packfile_size += SHA1_DIGEST_LENGTH;
1847 packfile_size += sizeof(struct got_packfile_hdr);
1848 if (progress_cb) {
1849 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1850 packfile_size, nours, ndeltify + nreuse,
1851 ndeltify + nreuse, ndeltify + nreuse);
1852 if (err)
1853 goto done;
1855 done:
1856 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1857 err = got_error_from_errno("close");
1858 return err;
1861 static const struct got_error *
1862 remove_unused_object(struct got_object_id *id, void *data, void *arg)
1864 struct got_object_idset *idset = arg;
1866 if (data == NULL)
1867 got_object_idset_remove(NULL, idset, id);
1869 return NULL;
1872 static const struct got_error *
1873 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1875 struct got_pack_meta *m = data;
1876 struct got_pack_metavec *v = arg;
1878 if (m->have_reused_delta)
1879 return NULL;
1881 return add_meta(m, v);
1884 const struct got_error *
1885 got_pack_create(uint8_t *packsha1, FILE *packfile,
1886 struct got_object_id **theirs, int ntheirs,
1887 struct got_object_id **ours, int nours,
1888 struct got_repository *repo, int loose_obj_only, int allow_empty,
1889 got_pack_progress_cb progress_cb, void *progress_arg,
1890 got_cancel_cb cancel_cb, void *cancel_arg)
1892 const struct got_error *err;
1893 int delta_cache_fd = -1;
1894 FILE *delta_cache = NULL;
1895 struct got_object_idset *idset;
1896 struct got_ratelimit rl;
1897 struct got_pack_metavec deltify, reuse;
1898 int ncolored = 0, nfound = 0, ntrees = 0;
1899 size_t ndeltify;
1901 memset(&deltify, 0, sizeof(deltify));
1902 memset(&reuse, 0, sizeof(reuse));
1904 got_ratelimit_init(&rl, 0, 500);
1906 idset = got_object_idset_alloc();
1907 if (idset == NULL)
1908 return got_error_from_errno("got_object_idset_alloc");
1910 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1911 ntheirs, ours, nours, repo, loose_obj_only,
1912 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1913 if (err)
1914 return err;
1916 err = got_object_idset_for_each(idset, remove_unused_object, idset);
1917 if (err)
1918 goto done;
1920 if (progress_cb) {
1921 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1922 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1923 if (err)
1924 goto done;
1927 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1928 err = got_error(GOT_ERR_CANNOT_PACK);
1929 goto done;
1932 delta_cache_fd = got_opentempfd();
1933 if (delta_cache_fd == -1) {
1934 err = got_error_from_errno("got_opentemp");
1935 goto done;
1938 reuse.metasz = 64;
1939 reuse.meta = calloc(reuse.metasz,
1940 sizeof(struct got_pack_meta *));
1941 if (reuse.meta == NULL) {
1942 err = got_error_from_errno("calloc");
1943 goto done;
1946 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1947 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1948 cancel_cb, cancel_arg);
1949 if (err)
1950 goto done;
1952 delta_cache = fdopen(delta_cache_fd, "a+");
1953 if (delta_cache == NULL) {
1954 err = got_error_from_errno("fdopen");
1955 goto done;
1957 delta_cache_fd = -1;
1959 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1960 err = got_error_from_errno("fseeko");
1961 goto done;
1964 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1965 if (ndeltify > 0) {
1966 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1967 if (deltify.meta == NULL) {
1968 err = got_error_from_errno("calloc");
1969 goto done;
1971 deltify.metasz = ndeltify;
1973 err = got_object_idset_for_each(idset, add_meta_idset_cb,
1974 &deltify);
1975 if (err)
1976 goto done;
1977 if (deltify.nmeta > 0) {
1978 err = pick_deltas(deltify.meta, deltify.nmeta,
1979 ncolored, nfound, ntrees, nours, reuse.nmeta,
1980 delta_cache, repo, progress_cb, progress_arg, &rl,
1981 cancel_cb, cancel_arg);
1982 if (err)
1983 goto done;
1987 if (fflush(delta_cache) == EOF) {
1988 err = got_error_from_errno("fflush");
1989 goto done;
1991 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1992 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1993 nours, repo, progress_cb, progress_arg, &rl,
1994 cancel_cb, cancel_arg);
1995 if (err)
1996 goto done;
1997 done:
1998 free_nmeta(deltify.meta, deltify.nmeta);
1999 free_nmeta(reuse.meta, reuse.nmeta);
2000 got_object_idset_free(idset);
2001 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2002 err = got_error_from_errno("close");
2003 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2004 err = got_error_from_errno("fclose");
2005 return err;