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/uio.h>
20 #include <sys/stat.h>
21 #include <sys/time.h>
23 #include <stdint.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include <limits.h>
30 #include <zlib.h>
32 #if defined(__FreeBSD__)
33 #include <unistd.h>
34 #endif
36 #include "got_error.h"
37 #include "got_cancel.h"
38 #include "got_object.h"
39 #include "got_path.h"
40 #include "got_reference.h"
41 #include "got_repository_admin.h"
42 #include "got_opentemp.h"
44 #include "got_lib_deltify.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_idset.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_deflate.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_ratelimit.h"
54 #include "got_lib_inflate.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 #ifndef MAX
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #endif
64 #ifndef nitems
65 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
66 #endif
68 struct got_pack_meta {
69 struct got_object_id id;
70 char *path;
71 int obj_type;
72 off_t size;
73 time_t mtime;
75 /* The best delta we picked */
76 struct got_pack_meta *head;
77 struct got_pack_meta *prev;
78 unsigned char *delta_buf; /* if encoded in memory (compressed) */
79 off_t delta_offset; /* offset in delta cache file (compressed) */
80 off_t delta_len; /* encoded delta length */
81 off_t delta_compressed_len; /* encoded+compressed delta length */
82 int nchain;
84 int have_reused_delta;
85 off_t reused_delta_offset; /* offset of delta in reused pack file */
86 struct got_object_id *base_obj_id;
88 /* Only used for delta window */
89 struct got_delta_table *dtab;
91 /* Only used for writing offset deltas */
92 off_t off;
93 };
95 struct got_pack_metavec {
96 struct got_pack_meta **meta;
97 int nmeta;
98 int metasz;
99 };
101 static const struct got_error *
102 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
103 const char *path, int obj_type, time_t mtime)
105 const struct got_error *err = NULL;
106 struct got_pack_meta *m;
108 *new = NULL;
110 m = calloc(1, sizeof(*m));
111 if (m == NULL)
112 return got_error_from_errno("calloc");
114 memcpy(&m->id, id, sizeof(m->id));
116 m->path = strdup(path);
117 if (m->path == NULL) {
118 err = got_error_from_errno("strdup");
119 free(m);
120 return err;
123 m->obj_type = obj_type;
124 m->mtime = mtime;
125 *new = m;
126 return NULL;
129 static void
130 clear_meta(struct got_pack_meta *meta)
132 if (meta == NULL)
133 return;
134 free(meta->path);
135 meta->path = NULL;
136 free(meta->delta_buf);
137 meta->delta_buf = NULL;
138 free(meta->base_obj_id);
139 meta->base_obj_id = NULL;
142 static void
143 free_nmeta(struct got_pack_meta **meta, int nmeta)
145 int i;
147 for (i = 0; i < nmeta; i++)
148 clear_meta(meta[i]);
149 free(meta);
152 static int
153 delta_order_cmp(const void *pa, const void *pb)
155 struct got_pack_meta *a, *b;
156 int cmp;
158 a = *(struct got_pack_meta **)pa;
159 b = *(struct got_pack_meta **)pb;
161 if (a->obj_type != b->obj_type)
162 return a->obj_type - b->obj_type;
163 cmp = strcmp(a->path, b->path);
164 if (cmp != 0)
165 return cmp;
166 if (a->mtime < b->mtime)
167 return -1;
168 if (a->mtime > b->mtime)
169 return 1;
170 return got_object_id_cmp(&a->id, &b->id);
173 static off_t
174 delta_size(struct got_delta_instruction *deltas, int ndeltas)
176 int i;
177 off_t size = 32;
178 for (i = 0; i < ndeltas; i++) {
179 if (deltas[i].copy)
180 size += GOT_DELTA_SIZE_SHIFT;
181 else
182 size += deltas[i].len + 1;
184 return size;
187 static const struct got_error *
188 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
190 char *n;
192 if (*len + nseg >= *sz) {
193 while (*len + nseg >= *sz)
194 *sz += *sz / 2;
195 n = realloc(*p, *sz);
196 if (n == NULL)
197 return got_error_from_errno("realloc");
198 *p = n;
200 memcpy(*p + *len, seg, nseg);
201 *len += nseg;
202 return NULL;
205 static const struct got_error *
206 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
207 struct got_delta_instruction *deltas, int ndeltas,
208 off_t delta_size, off_t base_size)
210 const struct got_error *err;
211 unsigned char buf[16], *bp;
212 int i, j;
213 size_t len = 0, compressed_len;
214 off_t bufsize = delta_size;
215 off_t n;
216 struct got_delta_instruction *d;
217 uint8_t *delta_buf;
219 delta_buf = malloc(bufsize);
220 if (delta_buf == NULL)
221 return got_error_from_errno("malloc");
223 /* base object size */
224 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
225 n = base_size >> GOT_DELTA_SIZE_SHIFT;
226 for (i = 1; n > 0; i++) {
227 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
228 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
229 n >>= GOT_DELTA_SIZE_SHIFT;
231 err = append(&delta_buf, &len, &bufsize, buf, i);
232 if (err)
233 goto done;
235 /* target object size */
236 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
237 n = o->size >> GOT_DELTA_SIZE_SHIFT;
238 for (i = 1; n > 0; i++) {
239 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
240 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
241 n >>= GOT_DELTA_SIZE_SHIFT;
243 err = append(&delta_buf, &len, &bufsize, buf, i);
244 if (err)
245 goto done;
247 for (j = 0; j < ndeltas; j++) {
248 d = &deltas[j];
249 if (d->copy) {
250 n = d->offset;
251 bp = &buf[1];
252 buf[0] = GOT_DELTA_BASE_COPY;
253 for (i = 0; i < 4; i++) {
254 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
255 buf[0] |= 1 << i;
256 *bp++ = n & 0xff;
257 n >>= 8;
258 if (n == 0)
259 break;
262 n = d->len;
263 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
264 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
265 for (i = 0; i < 3 && n > 0; i++) {
266 buf[0] |= 1 << (i + 4);
267 *bp++ = n & 0xff;
268 n >>= 8;
271 err = append(&delta_buf, &len, &bufsize,
272 buf, bp - buf);
273 if (err)
274 goto done;
275 } else if (o->f == NULL) {
276 n = 0;
277 while (n != d->len) {
278 buf[0] = (d->len - n < 127) ? d->len - n : 127;
279 err = append(&delta_buf, &len, &bufsize,
280 buf, 1);
281 if (err)
282 goto done;
283 err = append(&delta_buf, &len, &bufsize,
284 o->data + o->hdrlen + d->offset + n,
285 buf[0]);
286 if (err)
287 goto done;
288 n += buf[0];
290 } else {
291 char content[128];
292 size_t r;
293 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
294 err = got_error_from_errno("fseeko");
295 goto done;
297 n = 0;
298 while (n != d->len) {
299 buf[0] = (d->len - n < 127) ? d->len - n : 127;
300 err = append(&delta_buf, &len, &bufsize,
301 buf, 1);
302 if (err)
303 goto done;
304 r = fread(content, 1, buf[0], o->f);
305 if (r != buf[0]) {
306 err = got_ferror(o->f, GOT_ERR_IO);
307 goto done;
309 err = append(&delta_buf, &len, &bufsize,
310 content, buf[0]);
311 if (err)
312 goto done;
313 n += buf[0];
318 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
319 NULL, NULL, delta_buf, 0, len);
320 if (err)
321 goto done;
323 m->delta_len = len;
324 m->delta_compressed_len = compressed_len;
325 done:
326 free(delta_buf);
327 return err;
330 static const struct got_error *
331 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
332 struct got_delta_instruction *deltas, int ndeltas,
333 off_t base_size, FILE *f)
335 const struct got_error *err;
336 unsigned char buf[16], *bp;
337 int i, j;
338 off_t n;
339 struct got_deflate_buf zb;
340 struct got_delta_instruction *d;
341 off_t delta_len = 0, compressed_len = 0;
343 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
344 if (err)
345 return err;
347 /* base object size */
348 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
349 n = base_size >> GOT_DELTA_SIZE_SHIFT;
350 for (i = 1; n > 0; i++) {
351 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
352 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
353 n >>= GOT_DELTA_SIZE_SHIFT;
356 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
357 buf, 0, i, f, NULL);
358 if (err)
359 goto done;
360 delta_len += i;
362 /* target object size */
363 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
364 n = o->size >> GOT_DELTA_SIZE_SHIFT;
365 for (i = 1; n > 0; i++) {
366 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
367 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
368 n >>= GOT_DELTA_SIZE_SHIFT;
371 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
372 buf, 0, i, f, NULL);
373 if (err)
374 goto done;
375 delta_len += i;
377 for (j = 0; j < ndeltas; j++) {
378 d = &deltas[j];
379 if (d->copy) {
380 n = d->offset;
381 bp = &buf[1];
382 buf[0] = GOT_DELTA_BASE_COPY;
383 for (i = 0; i < 4; i++) {
384 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
385 buf[0] |= 1 << i;
386 *bp++ = n & 0xff;
387 n >>= 8;
388 if (n == 0)
389 break;
391 n = d->len;
392 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
393 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
394 for (i = 0; i < 3 && n > 0; i++) {
395 buf[0] |= 1 << (i + 4);
396 *bp++ = n & 0xff;
397 n >>= 8;
400 err = got_deflate_append_to_file_mmap(&zb,
401 &compressed_len, buf, 0, bp - buf, f, NULL);
402 if (err)
403 goto done;
404 delta_len += (bp - buf);
405 } else if (o->f == NULL) {
406 n = 0;
407 while (n != d->len) {
408 buf[0] = (d->len - n < 127) ? d->len - n : 127;
409 err = got_deflate_append_to_file_mmap(&zb,
410 &compressed_len, buf, 0, 1, f, NULL);
411 if (err)
412 goto done;
413 delta_len++;
414 err = got_deflate_append_to_file_mmap(&zb,
415 &compressed_len,
416 o->data + o->hdrlen + d->offset + n, 0,
417 buf[0], f, NULL);
418 if (err)
419 goto done;
420 delta_len += buf[0];
421 n += buf[0];
423 } else {
424 char content[128];
425 size_t r;
426 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
427 err = got_error_from_errno("fseeko");
428 goto done;
430 n = 0;
431 while (n != d->len) {
432 buf[0] = (d->len - n < 127) ? d->len - n : 127;
433 err = got_deflate_append_to_file_mmap(&zb,
434 &compressed_len, buf, 0, 1, f, NULL);
435 if (err)
436 goto done;
437 delta_len++;
438 r = fread(content, 1, buf[0], o->f);
439 if (r != buf[0]) {
440 err = got_ferror(o->f, GOT_ERR_IO);
441 goto done;
443 err = got_deflate_append_to_file_mmap(&zb,
444 &compressed_len, content, 0, buf[0], f,
445 NULL);
446 if (err)
447 goto done;
448 delta_len += buf[0];
449 n += buf[0];
454 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
455 if (err)
456 goto done;
458 /* sanity check */
459 if (compressed_len != ftello(f) - m->delta_offset) {
460 err = got_error(GOT_ERR_COMPRESSION);
461 goto done;
464 m->delta_len = delta_len;
465 m->delta_compressed_len = compressed_len;
466 done:
467 got_deflate_end(&zb);
468 return err;
471 static const struct got_error *
472 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
473 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
474 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
475 int nobj_written)
477 const struct got_error *err;
478 int elapsed;
480 if (progress_cb == NULL)
481 return NULL;
483 err = got_ratelimit_check(&elapsed, rl);
484 if (err || !elapsed)
485 return err;
487 return progress_cb(progress_arg, ncolored, nfound, ntrees,
488 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
491 static const struct got_error *
492 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
494 if (v->nmeta == v->metasz){
495 size_t newsize = 2 * v->metasz;
496 struct got_pack_meta **new;
497 new = reallocarray(v->meta, newsize, sizeof(*new));
498 if (new == NULL)
499 return got_error_from_errno("reallocarray");
500 v->meta = new;
501 v->metasz = newsize;
504 v->meta[v->nmeta++] = m;
505 return NULL;
508 static const struct got_error *
509 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
510 struct got_object_idset *idset, struct got_pack *pack,
511 struct got_packidx *packidx, int delta_cache_fd,
512 struct got_repository *repo)
514 const struct got_error *err = NULL;
515 struct got_pack_meta *base = NULL;
516 struct got_object_id *base_obj_id = NULL;
517 off_t delta_len = 0, delta_compressed_len = 0;
518 off_t delta_offset = 0, delta_cache_offset = 0;
519 uint64_t base_size, result_size;
521 if (m->have_reused_delta)
522 return NULL;
524 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
525 &delta_compressed_len, &delta_offset, &delta_cache_offset,
526 &base_obj_id, delta_cache_fd, packidx, idx, &m->id, repo);
527 if (err)
528 return err;
530 if (delta_offset + delta_len < delta_offset)
531 return got_error(GOT_ERR_BAD_PACKFILE);
533 base = got_object_idset_get(idset, base_obj_id);
534 if (base == NULL)
535 goto done;
537 m->delta_len = delta_len;
538 m->delta_compressed_len = delta_compressed_len;
539 m->delta_offset = delta_cache_offset;
540 m->prev = base;
541 m->size = result_size;
542 m->have_reused_delta = 1;
543 m->reused_delta_offset = delta_offset;
544 m->base_obj_id = base_obj_id;
545 base_obj_id = NULL;
546 err = add_meta(m, v);
547 done:
548 free(base_obj_id);
549 return err;
552 static const struct got_error *
553 find_pack_for_reuse(struct got_packidx **best_packidx,
554 struct got_repository *repo)
556 const struct got_error *err = NULL;
557 struct got_pathlist_entry *pe;
558 const char *best_packidx_path = NULL;
559 int nobj_max = 0;
561 *best_packidx = NULL;
563 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
564 const char *path_packidx = pe->path;
565 struct got_packidx *packidx;
566 int nobj;
568 err = got_repo_get_packidx(&packidx, path_packidx, repo);
569 if (err)
570 break;
572 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
573 if (nobj > nobj_max) {
574 best_packidx_path = path_packidx;
575 nobj_max = nobj;
579 if (best_packidx_path) {
580 err = got_repo_get_packidx(best_packidx, best_packidx_path,
581 repo);
584 return err;
587 struct search_deltas_arg {
588 struct got_packidx *packidx;
589 struct got_pack *pack;
590 struct got_object_idset *idset;
591 struct got_pack_metavec *v;
592 int delta_cache_fd;
593 struct got_repository *repo;
594 got_pack_progress_cb progress_cb;
595 void *progress_arg;
596 struct got_ratelimit *rl;
597 got_cancel_cb cancel_cb;
598 void *cancel_arg;
599 int ncolored;
600 int nfound;
601 int ntrees;
602 int ncommits;
603 };
605 static const struct got_error *
606 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
608 const struct got_error *err;
609 struct got_pack_meta *m = data;
610 struct search_deltas_arg *a = arg;
611 int obj_idx;
612 struct got_object *obj = NULL;
614 if (a->cancel_cb) {
615 err = (*a->cancel_cb)(a->cancel_arg);
616 if (err)
617 return err;
620 if (!got_repo_check_packidx_bloom_filter(a->repo,
621 a->packidx->path_packidx, id))
622 return NULL;
624 obj_idx = got_packidx_get_object_idx(a->packidx, id);
625 if (obj_idx == -1)
626 return NULL;
628 /* TODO:
629 * Opening and closing an object just to check its flags
630 * is a bit expensive. We could have an imsg which requests
631 * plain type/size information for an object without doing
632 * work such as traversing the object's entire delta chain
633 * to find the base object type, and other such info which
634 * we don't really need here.
635 */
636 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
637 a->packidx, obj_idx, a->repo);
638 if (err)
639 return err;
641 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
642 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
643 a->delta_cache_fd, a->repo);
644 if (err)
645 goto done;
646 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
647 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
648 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
650 done:
651 got_object_close(obj);
652 return err;
655 static const struct got_error *
656 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
657 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
658 struct got_repository *repo,
659 got_pack_progress_cb progress_cb, void *progress_arg,
660 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
662 const struct got_error *err = NULL;
663 char *path_packfile = NULL;
664 struct got_packidx *packidx;
665 struct got_pack *pack;
666 struct search_deltas_arg sda;
668 err = find_pack_for_reuse(&packidx, repo);
669 if (err)
670 return err;
672 if (packidx == NULL)
673 return NULL;
675 err = got_packidx_get_packfile_path(&path_packfile,
676 packidx->path_packidx);
677 if (err)
678 return err;
680 pack = got_repo_get_cached_pack(repo, path_packfile);
681 if (pack == NULL) {
682 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
683 if (err)
684 goto done;
687 sda.packidx = packidx;
688 sda.pack = pack;
689 sda.idset = idset;
690 sda.v = v;
691 sda.delta_cache_fd = delta_cache_fd;
692 sda.repo = repo;
693 sda.progress_cb = progress_cb;
694 sda.progress_arg = progress_arg;
695 sda.rl = rl;
696 sda.cancel_cb = cancel_cb;
697 sda.cancel_arg = cancel_arg;
698 sda.ncolored = ncolored;
699 sda.nfound = nfound;
700 sda.ntrees = ntrees;
701 sda.ncommits = ncommits;
702 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
703 done:
704 free(path_packfile);
705 return err;
708 static const struct got_error *
709 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
710 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
711 struct got_repository *repo,
712 got_pack_progress_cb progress_cb, void *progress_arg,
713 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
715 const struct got_error *err = NULL;
716 struct got_pack_meta *m = NULL, *base = NULL;
717 struct got_raw_object *raw = NULL, *base_raw = NULL;
718 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
719 int i, j, ndeltas, best_ndeltas;
720 off_t size, best_size;
721 const int max_base_candidates = 3;
722 size_t delta_memsize = 0;
723 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
724 int outfd = -1;
726 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
727 for (i = 0; i < nmeta; i++) {
728 if (cancel_cb) {
729 err = (*cancel_cb)(cancel_arg);
730 if (err)
731 break;
733 err = report_progress(progress_cb, progress_arg, rl,
734 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
735 nreused + i, 0);
736 if (err)
737 goto done;
738 m = meta[i];
740 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
741 m->obj_type == GOT_OBJ_TYPE_TAG)
742 continue;
744 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
745 if (err)
746 goto done;
747 m->size = raw->size;
749 if (raw->f == NULL) {
750 err = got_deltify_init_mem(&m->dtab, raw->data,
751 raw->hdrlen, raw->size + raw->hdrlen);
752 } else {
753 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
754 raw->size + raw->hdrlen);
756 if (err)
757 goto done;
759 if (i > max_base_candidates) {
760 struct got_pack_meta *n = NULL;
761 n = meta[i - (max_base_candidates + 1)];
762 got_deltify_free(n->dtab);
763 n->dtab = NULL;
766 best_size = raw->size;
767 best_ndeltas = 0;
768 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
769 if (cancel_cb) {
770 err = (*cancel_cb)(cancel_arg);
771 if (err)
772 goto done;
774 base = meta[j];
775 /* long chains make unpacking slow, avoid such bases */
776 if (base->nchain >= 128 ||
777 base->obj_type != m->obj_type)
778 continue;
780 err = got_object_raw_open(&base_raw, &outfd, repo,
781 &base->id);
782 if (err)
783 goto done;
785 if (raw->f == NULL && base_raw->f == NULL) {
786 err = got_deltify_mem_mem(&deltas, &ndeltas,
787 raw->data, raw->hdrlen,
788 raw->size + raw->hdrlen,
789 base->dtab, base_raw->data,
790 base_raw->hdrlen,
791 base_raw->size + base_raw->hdrlen);
792 } else if (raw->f == NULL) {
793 err = got_deltify_mem_file(&deltas, &ndeltas,
794 raw->data, raw->hdrlen,
795 raw->size + raw->hdrlen,
796 base->dtab, base_raw->f,
797 base_raw->hdrlen,
798 base_raw->size + base_raw->hdrlen);
799 } else if (base_raw->f == NULL) {
800 err = got_deltify_file_mem(&deltas, &ndeltas,
801 raw->f, raw->hdrlen,
802 raw->size + raw->hdrlen,
803 base->dtab, base_raw->data,
804 base_raw->hdrlen,
805 base_raw->size + base_raw->hdrlen);
806 } else {
807 err = got_deltify(&deltas, &ndeltas,
808 raw->f, raw->hdrlen,
809 raw->size + raw->hdrlen,
810 base->dtab, base_raw->f, base_raw->hdrlen,
811 base_raw->size + base_raw->hdrlen);
813 got_object_raw_close(base_raw);
814 base_raw = NULL;
815 if (err)
816 goto done;
818 size = delta_size(deltas, ndeltas);
819 if (size + 32 < best_size){
820 /*
821 * if we already picked a best delta,
822 * replace it.
823 */
824 best_size = size;
825 free(best_deltas);
826 best_deltas = deltas;
827 best_ndeltas = ndeltas;
828 deltas = NULL;
829 m->nchain = base->nchain + 1;
830 m->prev = base;
831 m->head = base->head;
832 if (m->head == NULL)
833 m->head = base;
834 } else {
835 free(deltas);
836 deltas = NULL;
837 ndeltas = 0;
841 if (best_ndeltas > 0) {
842 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
843 delta_memsize + best_size <= max_delta_memsize) {
844 delta_memsize += best_size;
845 err = encode_delta_in_mem(m, raw, best_deltas,
846 best_ndeltas, best_size, m->prev->size);
847 } else {
848 m->delta_offset = ftello(delta_cache);
849 err = encode_delta(m, raw, best_deltas,
850 best_ndeltas, m->prev->size, delta_cache);
852 free(best_deltas);
853 best_deltas = NULL;
854 best_ndeltas = 0;
855 if (err)
856 goto done;
859 got_object_raw_close(raw);
860 raw = NULL;
862 done:
863 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
864 got_deltify_free(meta[i]->dtab);
865 meta[i]->dtab = NULL;
867 if (raw)
868 got_object_raw_close(raw);
869 if (base_raw)
870 got_object_raw_close(base_raw);
871 if (outfd != -1 && close(outfd) == -1 && err == NULL)
872 err = got_error_from_errno("close");
873 free(deltas);
874 free(best_deltas);
875 return err;
878 static const struct got_error *
879 search_packidx(int *found, struct got_object_id *id,
880 struct got_repository *repo)
882 const struct got_error *err = NULL;
883 struct got_packidx *packidx = NULL;
884 int idx;
886 *found = 0;
888 err = got_repo_search_packidx(&packidx, &idx, repo, id);
889 if (err == NULL)
890 *found = 1; /* object is already packed */
891 else if (err->code == GOT_ERR_NO_OBJ)
892 err = NULL;
893 return err;
896 static const struct got_error *
897 add_object(int want_meta, struct got_object_idset *idset,
898 struct got_object_id *id, const char *path, int obj_type,
899 time_t mtime, int loose_obj_only, struct got_repository *repo,
900 int *ncolored, int *nfound, int *ntrees,
901 got_pack_progress_cb progress_cb, void *progress_arg,
902 struct got_ratelimit *rl)
904 const struct got_error *err;
905 struct got_pack_meta *m = NULL;
907 if (loose_obj_only) {
908 int is_packed;
909 err = search_packidx(&is_packed, id, repo);
910 if (err)
911 return err;
912 if (is_packed && want_meta)
913 return NULL;
916 if (want_meta) {
917 err = alloc_meta(&m, id, path, obj_type, mtime);
918 if (err)
919 return err;
921 (*nfound)++;
922 err = report_progress(progress_cb, progress_arg, rl,
923 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
924 if (err) {
925 clear_meta(m);
926 free(m);
927 return err;
931 err = got_object_idset_add(idset, id, m);
932 if (err) {
933 clear_meta(m);
934 free(m);
936 return err;
939 static const struct got_error *
940 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
941 struct got_object_idset *idset, struct got_object_id *tree_id,
942 const char *dpath, time_t mtime, struct got_repository *repo,
943 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
944 got_pack_progress_cb progress_cb, void *progress_arg,
945 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
947 const struct got_error *err;
948 struct got_tree_object *tree;
949 char *p = NULL;
950 int i;
952 err = got_object_open_as_tree(&tree, repo, tree_id);
953 if (err)
954 return err;
956 (*ntrees)++;
957 err = report_progress(progress_cb, progress_arg, rl,
958 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
959 if (err)
960 return err;
962 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
963 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
964 struct got_object_id *id = got_tree_entry_get_id(e);
965 mode_t mode = got_tree_entry_get_mode(e);
967 if (cancel_cb) {
968 err = (*cancel_cb)(cancel_arg);
969 if (err)
970 break;
973 if (got_object_tree_entry_is_submodule(e) ||
974 got_object_idset_contains(idset, id))
975 continue;
977 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
978 got_tree_entry_get_name(e)) == -1) {
979 err = got_error_from_errno("asprintf");
980 break;
983 if (S_ISDIR(mode)) {
984 struct got_object_qid *qid;
985 err = got_object_qid_alloc(&qid, id);
986 if (err)
987 break;
988 STAILQ_INSERT_TAIL(ids, qid, entry);
989 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
990 err = add_object(want_meta, idset, id, p,
991 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
992 ncolored, nfound, ntrees,
993 progress_cb, progress_arg, rl);
994 if (err)
995 break;
997 free(p);
998 p = NULL;
1001 got_object_tree_close(tree);
1002 free(p);
1003 return err;
1006 static const struct got_error *
1007 load_tree(int want_meta, struct got_object_idset *idset,
1008 struct got_object_id *tree_id, const char *dpath, time_t mtime,
1009 struct got_repository *repo, int loose_obj_only,
1010 int *ncolored, int *nfound, int *ntrees,
1011 got_pack_progress_cb progress_cb, void *progress_arg,
1012 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1014 const struct got_error *err = NULL;
1015 struct got_object_id_queue tree_ids;
1016 struct got_object_qid *qid;
1018 if (got_object_idset_contains(idset, tree_id))
1019 return NULL;
1021 err = got_object_qid_alloc(&qid, tree_id);
1022 if (err)
1023 return err;
1025 STAILQ_INIT(&tree_ids);
1026 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1028 while (!STAILQ_EMPTY(&tree_ids)) {
1029 if (cancel_cb) {
1030 err = (*cancel_cb)(cancel_arg);
1031 if (err)
1032 break;
1035 qid = STAILQ_FIRST(&tree_ids);
1036 STAILQ_REMOVE_HEAD(&tree_ids, entry);
1038 if (got_object_idset_contains(idset, &qid->id)) {
1039 got_object_qid_free(qid);
1040 continue;
1043 err = add_object(want_meta, idset, &qid->id, dpath,
1044 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo,
1045 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1046 if (err) {
1047 got_object_qid_free(qid);
1048 break;
1051 err = load_tree_entries(&tree_ids, want_meta, idset, &qid->id,
1052 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1053 ntrees, progress_cb, progress_arg, rl,
1054 cancel_cb, cancel_arg);
1055 got_object_qid_free(qid);
1056 if (err)
1057 break;
1060 got_object_id_queue_free(&tree_ids);
1061 return err;
1064 static const struct got_error *
1065 load_commit(int want_meta, struct got_object_idset *idset,
1066 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1067 int *ncolored, int *nfound, int *ntrees,
1068 got_pack_progress_cb progress_cb, void *progress_arg,
1069 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1071 const struct got_error *err;
1072 struct got_commit_object *commit;
1074 if (got_object_idset_contains(idset, id))
1075 return NULL;
1077 if (loose_obj_only) {
1078 int is_packed;
1079 err = search_packidx(&is_packed, id, repo);
1080 if (err)
1081 return err;
1082 if (is_packed && want_meta)
1083 return NULL;
1086 err = got_object_open_as_commit(&commit, repo, id);
1087 if (err)
1088 return err;
1090 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1091 got_object_commit_get_committer_time(commit),
1092 loose_obj_only, repo,
1093 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1094 if (err)
1095 goto done;
1097 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1098 "", got_object_commit_get_committer_time(commit),
1099 repo, loose_obj_only, ncolored, nfound, ntrees,
1100 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1101 done:
1102 got_object_commit_close(commit);
1103 return err;
1106 static const struct got_error *
1107 load_tag(int want_meta, struct got_object_idset *idset,
1108 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1109 int *ncolored, int *nfound, int *ntrees,
1110 got_pack_progress_cb progress_cb, void *progress_arg,
1111 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1113 const struct got_error *err;
1114 struct got_tag_object *tag = NULL;
1116 if (got_object_idset_contains(idset, id))
1117 return NULL;
1119 if (loose_obj_only) {
1120 int is_packed;
1121 err = search_packidx(&is_packed, id, repo);
1122 if (err)
1123 return err;
1124 if (is_packed && want_meta)
1125 return NULL;
1128 err = got_object_open_as_tag(&tag, repo, id);
1129 if (err)
1130 return err;
1132 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1133 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1134 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1135 if (err)
1136 goto done;
1138 switch (got_object_tag_get_object_type(tag)) {
1139 case GOT_OBJ_TYPE_COMMIT:
1140 err = load_commit(want_meta, idset,
1141 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1142 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1143 cancel_cb, cancel_arg);
1144 break;
1145 case GOT_OBJ_TYPE_TREE:
1146 err = load_tree(want_meta, idset,
1147 got_object_tag_get_object_id(tag), "",
1148 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1149 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1150 cancel_cb, cancel_arg);
1151 break;
1152 default:
1153 break;
1156 done:
1157 got_object_tag_close(tag);
1158 return err;
1161 enum findtwixt_color {
1162 COLOR_KEEP = 0,
1163 COLOR_DROP,
1164 COLOR_BLANK,
1165 COLOR_SKIP,
1168 static const int findtwixt_colors[] = {
1169 COLOR_KEEP,
1170 COLOR_DROP,
1171 COLOR_BLANK,
1172 COLOR_SKIP,
1175 static const struct got_error *
1176 paint_commit(struct got_object_qid *qid, int color)
1178 if (color < 0 || color >= nitems(findtwixt_colors))
1179 return got_error(GOT_ERR_RANGE);
1181 qid->data = (void *)&findtwixt_colors[color];
1182 return NULL;
1185 static const struct got_error *
1186 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1187 int color, struct got_repository *repo)
1189 const struct got_error *err;
1190 struct got_object_qid *qid;
1192 err = got_object_qid_alloc(&qid, id);
1193 if (err)
1194 return err;
1196 STAILQ_INSERT_TAIL(ids, qid, entry);
1197 return paint_commit(qid, color);
1200 struct append_id_arg {
1201 struct got_object_id **array;
1202 int idx;
1203 struct got_object_idset *drop;
1204 struct got_object_idset *skip;
1207 static const struct got_error *
1208 append_id(struct got_object_id *id, void *data, void *arg)
1210 struct append_id_arg *a = arg;
1212 if (got_object_idset_contains(a->skip, id) ||
1213 got_object_idset_contains(a->drop, id))
1214 return NULL;
1216 a->array[++a->idx] = got_object_id_dup(id);
1217 if (a->array[a->idx] == NULL)
1218 return got_error_from_errno("got_object_id_dup");
1220 return NULL;
1223 static const struct got_error *
1224 queue_commit_or_tag_id(struct got_object_id *id, int color,
1225 struct got_object_id_queue *ids, struct got_repository *repo)
1227 const struct got_error *err;
1228 struct got_tag_object *tag = NULL;
1229 int obj_type;
1231 err = got_object_get_type(&obj_type, repo, id);
1232 if (err)
1233 return err;
1235 if (obj_type == GOT_OBJ_TYPE_TAG) {
1236 err = got_object_open_as_tag(&tag, repo, id);
1237 if (err)
1238 return err;
1239 obj_type = got_object_tag_get_object_type(tag);
1240 id = got_object_tag_get_object_id(tag);
1243 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1244 err = queue_commit_id(ids, id, color, repo);
1245 if (err)
1246 goto done;
1248 done:
1249 if (tag)
1250 got_object_tag_close(tag);
1251 return err;
1254 static const struct got_error *
1255 paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1256 struct got_object_idset *keep, struct got_object_idset *drop,
1257 struct got_object_idset *skip, struct got_repository *repo,
1258 got_pack_progress_cb progress_cb, void *progress_arg,
1259 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1261 const struct got_error *err = NULL;
1262 struct got_commit_object *commit = NULL;
1263 const struct got_object_id_queue *parents;
1264 struct got_object_qid *qid;
1265 int nqueued = nids, nskip = 0;
1267 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1268 int color;
1270 if (cancel_cb) {
1271 err = cancel_cb(cancel_arg);
1272 if (err)
1273 break;
1276 qid = STAILQ_FIRST(ids);
1277 STAILQ_REMOVE_HEAD(ids, entry);
1278 nqueued--;
1279 color = *((int *)qid->data);
1280 if (color == COLOR_SKIP)
1281 nskip--;
1283 if (got_object_idset_contains(skip, &qid->id)) {
1284 got_object_qid_free(qid);
1285 continue;
1288 switch (color) {
1289 case COLOR_KEEP:
1290 if (got_object_idset_contains(keep, &qid->id)) {
1291 got_object_qid_free(qid);
1292 continue;
1294 if (got_object_idset_contains(drop, &qid->id)) {
1295 err = paint_commit(qid, COLOR_SKIP);
1296 if (err)
1297 goto done;
1298 nskip++;
1299 } else
1300 (*ncolored)++;
1301 err = got_object_idset_add(keep, &qid->id, NULL);
1302 if (err)
1303 goto done;
1304 break;
1305 case COLOR_DROP:
1306 if (got_object_idset_contains(drop, &qid->id)) {
1307 got_object_qid_free(qid);
1308 continue;
1310 if (got_object_idset_contains(keep, &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(drop, &qid->id, NULL);
1318 if (err)
1319 goto done;
1320 break;
1321 case COLOR_SKIP:
1322 if (!got_object_idset_contains(skip, &qid->id)) {
1323 err = got_object_idset_add(skip, &qid->id,
1324 NULL);
1325 if (err)
1326 goto done;
1328 break;
1329 default:
1330 /* should not happen */
1331 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1332 "%s invalid commit color %d", __func__, color);
1333 goto done;
1336 err = report_progress(progress_cb, progress_arg, rl,
1337 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1338 if (err)
1339 break;
1342 err = got_object_open_as_commit(&commit, repo, &qid->id);
1343 if (err)
1344 break;
1346 parents = got_object_commit_get_parent_ids(commit);
1347 if (parents) {
1348 struct got_object_qid *pid;
1349 color = *((int *)qid->data);
1350 STAILQ_FOREACH(pid, parents, entry) {
1351 err = queue_commit_id(ids, &pid->id, color,
1352 repo);
1353 if (err)
1354 break;
1355 nqueued++;
1356 if (color == COLOR_SKIP)
1357 nskip++;
1361 got_object_commit_close(commit);
1362 commit = NULL;
1363 got_object_qid_free(qid);
1365 done:
1366 if (commit)
1367 got_object_commit_close(commit);
1368 return err;
1371 static const struct got_error *
1372 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1373 struct got_object_id **head, int nhead,
1374 struct got_object_id **tail, int ntail,
1375 struct got_repository *repo,
1376 got_pack_progress_cb progress_cb, void *progress_arg,
1377 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1379 const struct got_error *err = NULL;
1380 struct got_object_id_queue ids;
1381 struct got_object_idset *keep, *drop, *skip = NULL;
1382 int i, nkeep;
1384 STAILQ_INIT(&ids);
1385 *res = NULL;
1386 *nres = 0;
1387 *ncolored = 0;
1389 keep = got_object_idset_alloc();
1390 if (keep == NULL)
1391 return got_error_from_errno("got_object_idset_alloc");
1393 drop = got_object_idset_alloc();
1394 if (drop == NULL) {
1395 err = got_error_from_errno("got_object_idset_alloc");
1396 goto done;
1399 skip = got_object_idset_alloc();
1400 if (skip == NULL) {
1401 err = got_error_from_errno("got_object_idset_alloc");
1402 goto done;
1405 for (i = 0; i < nhead; i++) {
1406 struct got_object_id *id = head[i];
1407 if (id == NULL)
1408 continue;
1409 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1410 if (err)
1411 goto done;
1414 for (i = 0; i < ntail; i++) {
1415 struct got_object_id *id = tail[i];
1416 if (id == NULL)
1417 continue;
1418 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1419 if (err)
1420 goto done;
1423 err = paint_commits(ncolored, &ids, nhead + ntail,
1424 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1425 cancel_cb, cancel_arg);
1426 if (err)
1427 goto done;
1429 nkeep = got_object_idset_num_elements(keep);
1430 if (nkeep > 0) {
1431 struct append_id_arg arg;
1432 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1433 if (arg.array == NULL) {
1434 err = got_error_from_errno("calloc");
1435 goto done;
1437 arg.idx = -1;
1438 arg.skip = skip;
1439 arg.drop = drop;
1440 err = got_object_idset_for_each(keep, append_id, &arg);
1441 if (err) {
1442 free(arg.array);
1443 goto done;
1445 *res = arg.array;
1446 *nres = arg.idx + 1;
1448 done:
1449 got_object_idset_free(keep);
1450 got_object_idset_free(drop);
1451 if (skip)
1452 got_object_idset_free(skip);
1453 got_object_id_queue_free(&ids);
1454 return err;
1457 static const struct got_error *
1458 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1459 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1460 struct got_object_id **ours, int nours, struct got_repository *repo,
1461 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1462 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1464 const struct got_error *err = NULL;
1465 struct got_object_id **ids = NULL;
1466 int i, nobj = 0, obj_type;
1468 *ncolored = 0;
1469 *nfound = 0;
1470 *ntrees = 0;
1472 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1473 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1474 if (err || nobj == 0)
1475 goto done;
1477 for (i = 0; i < ntheirs; i++) {
1478 struct got_object_id *id = theirs[i];
1479 if (id == NULL)
1480 continue;
1481 err = got_object_get_type(&obj_type, repo, id);
1482 if (err)
1483 return err;
1484 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1485 err = load_commit(0, idset, id, repo,
1486 loose_obj_only, ncolored, nfound, ntrees,
1487 progress_cb, progress_arg, rl,
1488 cancel_cb, cancel_arg);
1489 if (err)
1490 goto done;
1491 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1492 err = load_tag(0, idset, id, repo,
1493 loose_obj_only, ncolored, nfound, ntrees,
1494 progress_cb, progress_arg, rl,
1495 cancel_cb, cancel_arg);
1496 if (err)
1497 goto done;
1501 for (i = 0; i < nobj; i++) {
1502 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1503 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1504 cancel_cb, cancel_arg);
1505 if (err)
1506 goto done;
1509 for (i = 0; i < nours; i++) {
1510 struct got_object_id *id = ours[i];
1511 struct got_pack_meta *m;
1512 if (id == NULL)
1513 continue;
1514 m = got_object_idset_get(idset, id);
1515 if (m == NULL) {
1516 err = got_object_get_type(&obj_type, repo, id);
1517 if (err)
1518 goto done;
1519 } else
1520 obj_type = m->obj_type;
1521 if (obj_type != GOT_OBJ_TYPE_TAG)
1522 continue;
1523 err = load_tag(1, idset, id, repo, loose_obj_only,
1524 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1525 cancel_cb, cancel_arg);
1526 if (err)
1527 goto done;
1529 done:
1530 for (i = 0; i < nobj; i++) {
1531 free(ids[i]);
1533 free(ids);
1534 return err;
1537 const struct got_error *
1538 hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
1540 size_t n;
1542 SHA1Update(ctx, buf, len);
1543 n = fwrite(buf, 1, len, f);
1544 if (n != len)
1545 return got_ferror(f, GOT_ERR_IO);
1546 return NULL;
1549 const struct got_error *
1550 hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1552 unsigned char buf[65536];
1553 off_t remain = len;
1554 size_t n;
1556 while (remain > 0) {
1557 size_t copylen = MIN(sizeof(buf), remain);
1558 n = fread(buf, 1, copylen, fsrc);
1559 if (n != copylen)
1560 return got_ferror(fsrc, GOT_ERR_IO);
1561 SHA1Update(ctx, buf, copylen);
1562 n = fwrite(buf, 1, copylen, fdst);
1563 if (n != copylen)
1564 return got_ferror(fdst, GOT_ERR_IO);
1565 remain -= copylen;
1568 return NULL;
1571 static void
1572 putbe32(char *b, uint32_t n)
1574 b[0] = n >> 24;
1575 b[1] = n >> 16;
1576 b[2] = n >> 8;
1577 b[3] = n >> 0;
1580 static int
1581 write_order_cmp(const void *pa, const void *pb)
1583 struct got_pack_meta *a, *b, *ahd, *bhd;
1585 a = *(struct got_pack_meta **)pa;
1586 b = *(struct got_pack_meta **)pb;
1587 ahd = (a->head == NULL) ? a : a->head;
1588 bhd = (b->head == NULL) ? b : b->head;
1589 if (bhd->mtime < ahd->mtime)
1590 return -1;
1591 if (bhd->mtime > ahd->mtime)
1592 return 1;
1593 if (bhd < ahd)
1594 return -1;
1595 if (bhd > ahd)
1596 return 1;
1597 if (a->nchain != b->nchain)
1598 return a->nchain - b->nchain;
1599 if (a->mtime < b->mtime)
1600 return -1;
1601 if (a->mtime > b->mtime)
1602 return 1;
1603 return got_object_id_cmp(&a->id, &b->id);
1606 static int
1607 reuse_write_order_cmp(const void *pa, const void *pb)
1609 struct got_pack_meta *a, *b;
1611 a = *(struct got_pack_meta **)pa;
1612 b = *(struct got_pack_meta **)pb;
1614 if (a->reused_delta_offset < b->reused_delta_offset)
1615 return -1;
1616 if (a->reused_delta_offset > b->reused_delta_offset)
1617 return 1;
1618 return 0;
1621 static const struct got_error *
1622 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1624 size_t i;
1626 *hdrlen = 0;
1628 hdr[0] = obj_type << 4;
1629 hdr[0] |= len & 0xf;
1630 len >>= 4;
1631 for (i = 1; len != 0; i++){
1632 if (i >= bufsize)
1633 return got_error(GOT_ERR_NO_SPACE);
1634 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1635 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1636 len >>= GOT_DELTA_SIZE_SHIFT;
1639 *hdrlen = i;
1640 return NULL;
1643 static int
1644 packoff(char *hdr, off_t off)
1646 int i, j;
1647 char rbuf[8];
1649 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1650 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1651 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1652 GOT_DELTA_SIZE_MORE;
1655 j = 0;
1656 while (i > 0)
1657 hdr[j++] = rbuf[--i];
1658 return j;
1661 static const struct got_error *
1662 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1663 struct got_pack_meta *m)
1665 const struct got_error *err;
1666 char buf[32];
1667 int nh;
1669 if (m->prev->off != 0) {
1670 err = packhdr(&nh, buf, sizeof(buf),
1671 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1672 if (err)
1673 return err;
1674 nh += packoff(buf + nh, m->off - m->prev->off);
1675 err = hwrite(packfile, buf, nh, ctx);
1676 if (err)
1677 return err;
1678 *packfile_size += nh;
1679 } else {
1680 err = packhdr(&nh, buf, sizeof(buf),
1681 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1682 if (err)
1683 return err;
1684 err = hwrite(packfile, buf, nh, ctx);
1685 if (err)
1686 return err;
1687 *packfile_size += nh;
1688 err = hwrite(packfile, m->prev->id.sha1,
1689 sizeof(m->prev->id.sha1), ctx);
1690 if (err)
1691 return err;
1692 *packfile_size += sizeof(m->prev->id.sha1);
1695 return NULL;
1698 static const struct got_error *
1699 write_packed_object(off_t *packfile_size, FILE *packfile,
1700 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1701 SHA1_CTX *ctx, struct got_repository *repo)
1703 const struct got_error *err = NULL;
1704 struct got_deflate_checksum csum;
1705 char buf[32];
1706 int nh;
1707 struct got_raw_object *raw = NULL;
1708 off_t outlen;
1710 csum.output_sha1 = ctx;
1711 csum.output_crc = NULL;
1713 m->off = ftello(packfile);
1714 if (m->delta_len == 0) {
1715 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1716 if (err)
1717 goto done;
1718 err = packhdr(&nh, buf, sizeof(buf),
1719 m->obj_type, raw->size);
1720 if (err)
1721 goto done;
1722 err = hwrite(packfile, buf, nh, ctx);
1723 if (err)
1724 goto done;
1725 *packfile_size += nh;
1726 if (raw->f == NULL) {
1727 err = got_deflate_to_file_mmap(&outlen,
1728 raw->data + raw->hdrlen, 0, raw->size,
1729 packfile, &csum);
1730 if (err)
1731 goto done;
1732 } else {
1733 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1734 == -1) {
1735 err = got_error_from_errno("fseeko");
1736 goto done;
1738 err = got_deflate_to_file(&outlen, raw->f,
1739 raw->size, packfile, &csum);
1740 if (err)
1741 goto done;
1743 *packfile_size += outlen;
1744 got_object_raw_close(raw);
1745 raw = NULL;
1746 } else if (m->delta_buf) {
1747 err = deltahdr(packfile_size, ctx, packfile, m);
1748 if (err)
1749 goto done;
1750 err = hwrite(packfile, m->delta_buf,
1751 m->delta_compressed_len, ctx);
1752 if (err)
1753 goto done;
1754 *packfile_size += m->delta_compressed_len;
1755 free(m->delta_buf);
1756 m->delta_buf = NULL;
1757 } else {
1758 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1759 == -1) {
1760 err = got_error_from_errno("fseeko");
1761 goto done;
1763 err = deltahdr(packfile_size, ctx, packfile, m);
1764 if (err)
1765 goto done;
1766 err = hcopy(delta_cache, packfile,
1767 m->delta_compressed_len, ctx);
1768 if (err)
1769 goto done;
1770 *packfile_size += m->delta_compressed_len;
1772 done:
1773 if (raw)
1774 got_object_raw_close(raw);
1775 return err;
1778 static const struct got_error *
1779 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1780 struct got_pack_meta **deltify, int ndeltify,
1781 struct got_pack_meta **reuse, int nreuse,
1782 int ncolored, int nfound, int ntrees, int nours,
1783 struct got_repository *repo,
1784 got_pack_progress_cb progress_cb, void *progress_arg,
1785 struct got_ratelimit *rl,
1786 got_cancel_cb cancel_cb, void *cancel_arg)
1788 const struct got_error *err = NULL;
1789 int i;
1790 SHA1_CTX ctx;
1791 struct got_pack_meta *m;
1792 char buf[32];
1793 size_t n;
1794 off_t packfile_size = 0;
1795 int outfd = -1;
1797 SHA1Init(&ctx);
1799 err = hwrite(packfile, "PACK", 4, &ctx);
1800 if (err)
1801 return err;
1802 putbe32(buf, GOT_PACKFILE_VERSION);
1803 err = hwrite(packfile, buf, 4, &ctx);
1804 if (err)
1805 goto done;
1806 putbe32(buf, ndeltify + nreuse);
1807 err = hwrite(packfile, buf, 4, &ctx);
1808 if (err)
1809 goto done;
1811 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1812 write_order_cmp);
1813 for (i = 0; i < ndeltify; i++) {
1814 err = report_progress(progress_cb, progress_arg, rl,
1815 ncolored, nfound, ntrees, packfile_size, nours,
1816 ndeltify + nreuse, ndeltify + nreuse, i);
1817 if (err)
1818 goto done;
1819 m = deltify[i];
1820 err = write_packed_object(&packfile_size, packfile,
1821 delta_cache, m, &outfd, &ctx, repo);
1822 if (err)
1823 goto done;
1826 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1827 reuse_write_order_cmp);
1828 for (i = 0; i < nreuse; i++) {
1829 err = report_progress(progress_cb, progress_arg, rl,
1830 ncolored, nfound, ntrees, packfile_size, nours,
1831 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1832 if (err)
1833 goto done;
1834 m = reuse[i];
1835 err = write_packed_object(&packfile_size, packfile,
1836 delta_cache, m, &outfd, &ctx, repo);
1837 if (err)
1838 goto done;
1841 SHA1Final(pack_sha1, &ctx);
1842 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1843 if (n != SHA1_DIGEST_LENGTH)
1844 err = got_ferror(packfile, GOT_ERR_IO);
1845 packfile_size += SHA1_DIGEST_LENGTH;
1846 packfile_size += sizeof(struct got_packfile_hdr);
1847 if (progress_cb) {
1848 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1849 packfile_size, nours, ndeltify + nreuse,
1850 ndeltify + nreuse, ndeltify + nreuse);
1851 if (err)
1852 goto done;
1854 done:
1855 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1856 err = got_error_from_errno("close");
1857 return err;
1860 static const struct got_error *
1861 remove_unused_object(struct got_object_id *id, void *data, void *arg)
1863 struct got_object_idset *idset = arg;
1865 if (data == NULL)
1866 got_object_idset_remove(NULL, idset, id);
1868 return NULL;
1871 static const struct got_error *
1872 remove_reused_object(struct got_object_id *id, void *data, void *arg)
1874 struct got_object_idset *idset = arg;
1875 struct got_pack_meta *m = data;
1877 if (m->have_reused_delta)
1878 got_object_idset_remove(NULL, idset, id);
1880 return NULL;
1883 static const struct got_error *
1884 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1886 struct got_pack_meta *m = data;
1887 struct got_pack_metavec *v = arg;
1889 return add_meta(m, v);
1892 const struct got_error *
1893 got_pack_create(uint8_t *packsha1, FILE *packfile,
1894 struct got_object_id **theirs, int ntheirs,
1895 struct got_object_id **ours, int nours,
1896 struct got_repository *repo, int loose_obj_only, int allow_empty,
1897 got_pack_progress_cb progress_cb, void *progress_arg,
1898 got_cancel_cb cancel_cb, void *cancel_arg)
1900 const struct got_error *err;
1901 int delta_cache_fd = -1;
1902 FILE *delta_cache = NULL;
1903 struct got_object_idset *idset;
1904 struct got_ratelimit rl;
1905 struct got_pack_metavec deltify, reuse;
1906 int ncolored = 0, nfound = 0, ntrees = 0;
1908 memset(&deltify, 0, sizeof(deltify));
1909 memset(&reuse, 0, sizeof(reuse));
1911 got_ratelimit_init(&rl, 0, 500);
1913 idset = got_object_idset_alloc();
1914 if (idset == NULL)
1915 return got_error_from_errno("got_object_idset_alloc");
1917 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1918 ntheirs, ours, nours, repo, loose_obj_only,
1919 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1920 if (err)
1921 return err;
1923 err = got_object_idset_for_each(idset, remove_unused_object, idset);
1924 if (err)
1925 goto done;
1927 if (progress_cb) {
1928 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1929 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1930 if (err)
1931 goto done;
1934 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1935 err = got_error(GOT_ERR_CANNOT_PACK);
1936 goto done;
1939 delta_cache_fd = got_opentempfd();
1940 if (delta_cache_fd == -1) {
1941 err = got_error_from_errno("got_opentemp");
1942 goto done;
1945 reuse.metasz = 64;
1946 reuse.meta = calloc(reuse.metasz,
1947 sizeof(struct got_pack_meta *));
1948 if (reuse.meta == NULL) {
1949 err = got_error_from_errno("calloc");
1950 goto done;
1953 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1954 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1955 cancel_cb, cancel_arg);
1956 if (err)
1957 goto done;
1958 if (reuse.nmeta > 0) {
1959 err = got_object_idset_for_each(idset,
1960 remove_reused_object, idset);
1961 if (err)
1962 goto done;
1965 delta_cache = fdopen(delta_cache_fd, "a+");
1966 if (delta_cache == NULL) {
1967 err = got_error_from_errno("fdopen");
1968 goto done;
1970 delta_cache_fd = -1;
1972 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1973 err = got_error_from_errno("fseeko");
1974 goto done;
1977 deltify.meta = calloc(got_object_idset_num_elements(idset),
1978 sizeof(struct got_pack_meta *));
1979 if (deltify.meta == NULL) {
1980 err = got_error_from_errno("calloc");
1981 goto done;
1983 deltify.metasz = got_object_idset_num_elements(idset);
1985 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1986 if (err)
1987 goto done;
1988 if (deltify.nmeta > 0) {
1989 err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1990 nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1991 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1992 if (err)
1993 goto done;
1996 if (fflush(delta_cache) == EOF) {
1997 err = got_error_from_errno("fflush");
1998 goto done;
2000 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
2001 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
2002 nours, repo, progress_cb, progress_arg, &rl,
2003 cancel_cb, cancel_arg);
2004 if (err)
2005 goto done;
2006 done:
2007 free_nmeta(deltify.meta, deltify.nmeta);
2008 free_nmeta(reuse.meta, reuse.nmeta);
2009 got_object_idset_free(idset);
2010 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2011 err = got_error_from_errno("close");
2012 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2013 err = got_error_from_errno("fclose");
2014 return err;