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 <limits.h>
34 #include <zlib.h>
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"
55 #ifndef MIN
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #endif
59 #ifndef MAX
60 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
61 #endif
63 struct got_pack_meta {
64 struct got_object_id id;
65 char *path;
66 int obj_type;
67 off_t size;
68 time_t mtime;
70 /* The best delta we picked */
71 struct got_pack_meta *head;
72 struct got_pack_meta *prev;
73 unsigned char *delta_buf; /* if not encoded in delta cache file */
74 off_t delta_offset; /* offset in delta cache file */
75 off_t delta_len; /* encoded delta length */
76 int nchain;
78 int have_reused_delta;
79 off_t reused_delta_offset; /* offset of delta in reused pack file */
80 struct got_object_id *base_obj_id;
82 /* Only used for delta window */
83 struct got_delta_table *dtab;
85 /* Only used for writing offset deltas */
86 off_t off;
87 };
89 struct got_pack_metavec {
90 struct got_pack_meta **meta;
91 int nmeta;
92 int metasz;
93 };
95 static const struct got_error *
96 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
97 const char *path, int obj_type, time_t mtime)
98 {
99 const struct got_error *err = NULL;
100 struct got_pack_meta *m;
102 *new = NULL;
104 m = calloc(1, sizeof(*m));
105 if (m == NULL)
106 return got_error_from_errno("calloc");
108 memcpy(&m->id, id, sizeof(m->id));
110 m->path = strdup(path);
111 if (m->path == NULL) {
112 err = got_error_from_errno("strdup");
113 free(m);
114 return err;
117 m->obj_type = obj_type;
118 m->mtime = mtime;
119 *new = m;
120 return NULL;
123 static void
124 clear_meta(struct got_pack_meta *meta)
126 if (meta == NULL)
127 return;
128 free(meta->path);
129 meta->path = NULL;
130 free(meta->delta_buf);
131 meta->delta_buf = NULL;
132 free(meta->base_obj_id);
133 meta->base_obj_id = NULL;
136 static void
137 free_nmeta(struct got_pack_meta **meta, int nmeta)
139 int i;
141 for (i = 0; i < nmeta; i++)
142 clear_meta(meta[i]);
143 free(meta);
146 static int
147 delta_order_cmp(const void *pa, const void *pb)
149 struct got_pack_meta *a, *b;
150 int cmp;
152 a = *(struct got_pack_meta **)pa;
153 b = *(struct got_pack_meta **)pb;
155 if (a->obj_type != b->obj_type)
156 return a->obj_type - b->obj_type;
157 cmp = strcmp(a->path, b->path);
158 if (cmp != 0)
159 return cmp;
160 if (a->mtime != b->mtime)
161 return a->mtime - b->mtime;
162 return got_object_id_cmp(&a->id, &b->id);
165 static off_t
166 delta_size(struct got_delta_instruction *deltas, int ndeltas)
168 int i;
169 off_t size = 32;
170 for (i = 0; i < ndeltas; i++) {
171 if (deltas[i].copy)
172 size += GOT_DELTA_SIZE_SHIFT;
173 else
174 size += deltas[i].len + 1;
176 return size;
179 static const struct got_error *
180 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
182 char *n;
184 if (*len + nseg >= *sz) {
185 while (*len + nseg >= *sz)
186 *sz += *sz / 2;
187 n = realloc(*p, *sz);
188 if (n == NULL)
189 return got_error_from_errno("realloc");
190 *p = n;
192 memcpy(*p + *len, seg, nseg);
193 *len += nseg;
194 return NULL;
197 static const struct got_error *
198 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
199 struct got_delta_instruction *deltas, int ndeltas,
200 off_t delta_size, off_t base_size)
202 const struct got_error *err;
203 unsigned char buf[16], *bp;
204 int i, j;
205 size_t len = 0;
206 off_t n;
207 struct got_delta_instruction *d;
209 m->delta_buf = malloc(delta_size);
210 if (m->delta_buf == NULL)
211 return got_error_from_errno("calloc");
213 /* base object size */
214 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
215 n = base_size >> GOT_DELTA_SIZE_SHIFT;
216 for (i = 1; n > 0; i++) {
217 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
218 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
219 n >>= GOT_DELTA_SIZE_SHIFT;
221 err = append(&m->delta_buf, &len, &delta_size, buf, i);
222 if (err)
223 return err;
225 /* target object size */
226 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
227 n = o->size >> GOT_DELTA_SIZE_SHIFT;
228 for (i = 1; n > 0; i++) {
229 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
230 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
231 n >>= GOT_DELTA_SIZE_SHIFT;
233 err = append(&m->delta_buf, &len, &delta_size, buf, i);
234 if (err)
235 return err;
237 for (j = 0; j < ndeltas; j++) {
238 d = &deltas[j];
239 if (d->copy) {
240 n = d->offset;
241 bp = &buf[1];
242 buf[0] = GOT_DELTA_BASE_COPY;
243 for (i = 0; i < 4; i++) {
244 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
245 buf[0] |= 1 << i;
246 *bp++ = n & 0xff;
247 n >>= 8;
248 if (n == 0)
249 break;
252 n = d->len;
253 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
254 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
255 for (i = 0; i < 3 && n > 0; i++) {
256 buf[0] |= 1 << (i + 4);
257 *bp++ = n & 0xff;
258 n >>= 8;
261 err = append(&m->delta_buf, &len, &delta_size,
262 buf, bp - buf);
263 if (err)
264 return err;
265 } else if (o->f == NULL) {
266 n = 0;
267 while (n != d->len) {
268 buf[0] = (d->len - n < 127) ? d->len - n : 127;
269 err = append(&m->delta_buf, &len, &delta_size,
270 buf, 1);
271 if (err)
272 return err;
273 err = append(&m->delta_buf, &len, &delta_size,
274 o->data + o->hdrlen + d->offset + n,
275 buf[0]);
276 if (err)
277 return err;
278 n += buf[0];
280 } else {
281 char content[128];
282 size_t r;
283 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
284 return got_error_from_errno("fseeko");
285 n = 0;
286 while (n != d->len) {
287 buf[0] = (d->len - n < 127) ? d->len - n : 127;
288 err = append(&m->delta_buf, &len, &delta_size,
289 buf, 1);
290 if (err)
291 return err;
292 r = fread(content, 1, buf[0], o->f);
293 if (r != buf[0])
294 return got_ferror(o->f, GOT_ERR_IO);
295 err = append(&m->delta_buf, &len, &delta_size,
296 content, buf[0]);
297 if (err)
298 return err;
299 n += buf[0];
304 m->delta_len = len;
305 return NULL;
308 static const struct got_error *
309 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
310 struct got_delta_instruction *deltas, int ndeltas,
311 off_t base_size, FILE *f)
313 unsigned char buf[16], *bp;
314 int i, j;
315 off_t n;
316 size_t w;
317 struct got_delta_instruction *d;
319 /* base object size */
320 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
321 n = base_size >> GOT_DELTA_SIZE_SHIFT;
322 for (i = 1; n > 0; i++) {
323 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
324 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
325 n >>= GOT_DELTA_SIZE_SHIFT;
327 w = fwrite(buf, 1, i, f);
328 if (w != i)
329 return got_ferror(f, GOT_ERR_IO);
331 /* target object size */
332 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
333 n = o->size >> GOT_DELTA_SIZE_SHIFT;
334 for (i = 1; n > 0; i++) {
335 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
336 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
337 n >>= GOT_DELTA_SIZE_SHIFT;
339 w = fwrite(buf, 1, i, f);
340 if (w != i)
341 return got_ferror(f, GOT_ERR_IO);
343 for (j = 0; j < ndeltas; j++) {
344 d = &deltas[j];
345 if (d->copy) {
346 n = d->offset;
347 bp = &buf[1];
348 buf[0] = GOT_DELTA_BASE_COPY;
349 for (i = 0; i < 4; i++) {
350 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
351 buf[0] |= 1 << i;
352 *bp++ = n & 0xff;
353 n >>= 8;
354 if (n == 0)
355 break;
358 n = d->len;
359 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
360 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
361 for (i = 0; i < 3 && n > 0; i++) {
362 buf[0] |= 1 << (i + 4);
363 *bp++ = n & 0xff;
364 n >>= 8;
367 w = fwrite(buf, 1, bp - buf, f);
368 if (w != bp - buf)
369 return got_ferror(f, GOT_ERR_IO);
370 } else if (o->f == NULL) {
371 n = 0;
372 while (n != d->len) {
373 buf[0] = (d->len - n < 127) ? d->len - n : 127;
374 w = fwrite(buf, 1, 1, f);
375 if (w != 1)
376 return got_ferror(f, GOT_ERR_IO);
377 w = fwrite(o->data + o->hdrlen + d->offset + n,
378 1, buf[0], f);
379 if (w != buf[0])
380 return got_ferror(f, GOT_ERR_IO);
381 n += buf[0];
383 } else {
384 char content[128];
385 size_t r;
386 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
387 return got_error_from_errno("fseeko");
388 n = 0;
389 while (n != d->len) {
390 buf[0] = (d->len - n < 127) ? d->len - n : 127;
391 w = fwrite(buf, 1, 1, f);
392 if (w != 1)
393 return got_ferror(f, GOT_ERR_IO);
394 r = fread(content, 1, buf[0], o->f);
395 if (r != buf[0])
396 return got_ferror(o->f, GOT_ERR_IO);
397 w = fwrite(content, 1, buf[0], f);
398 if (w != buf[0])
399 return got_ferror(f, GOT_ERR_IO);
400 n += buf[0];
405 m->delta_len = ftello(f) - m->delta_offset;
406 return NULL;
409 static const struct got_error *
410 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
411 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
412 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
413 int nobj_written)
415 const struct got_error *err;
416 int elapsed;
418 if (progress_cb == NULL)
419 return NULL;
421 err = got_ratelimit_check(&elapsed, rl);
422 if (err || !elapsed)
423 return err;
425 return progress_cb(progress_arg, ncolored, nfound, ntrees,
426 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
429 static const struct got_error *
430 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
432 if (v->nmeta == v->metasz){
433 size_t newsize = 2 * v->metasz;
434 struct got_pack_meta **new;
435 new = reallocarray(v->meta, newsize, sizeof(*new));
436 if (new == NULL)
437 return got_error_from_errno("reallocarray");
438 v->meta = new;
439 v->metasz = newsize;
442 v->meta[v->nmeta++] = m;
443 return NULL;
446 static const struct got_error *
447 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
448 struct got_object_idset *idset, struct got_pack *pack,
449 struct got_packidx *packidx, int delta_cache_fd,
450 struct got_repository *repo)
452 const struct got_error *err = NULL;
453 struct got_pack_meta *base = NULL;
454 struct got_object_id *base_obj_id = NULL;
455 off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
456 uint64_t base_size, result_size;
458 if (m->have_reused_delta)
459 return NULL;
461 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
462 &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
463 packidx, idx, &m->id, repo);
464 if (err)
465 return err;
467 if (delta_offset + delta_len < delta_offset)
468 return got_error(GOT_ERR_BAD_PACKFILE);
470 base = got_object_idset_get(idset, base_obj_id);
471 if (base == NULL)
472 goto done;
474 m->delta_len = delta_len;
475 m->delta_offset = delta_cache_offset;
476 m->prev = base;
477 m->size = result_size;
478 m->have_reused_delta = 1;
479 m->reused_delta_offset = delta_offset;
480 m->base_obj_id = base_obj_id;
481 base_obj_id = NULL;
482 err = add_meta(m, v);
483 done:
484 free(base_obj_id);
485 return err;
488 static const struct got_error *
489 find_pack_for_reuse(struct got_packidx **best_packidx,
490 struct got_repository *repo)
492 const struct got_error *err = NULL;
493 struct got_pathlist_entry *pe;
494 const char *best_packidx_path = NULL;
495 int nobj_max = 0;
497 *best_packidx = NULL;
499 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
500 const char *path_packidx = pe->path;
501 struct got_packidx *packidx;
502 int nobj;
504 err = got_repo_get_packidx(&packidx, path_packidx, repo);
505 if (err)
506 break;
508 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
509 if (nobj > nobj_max) {
510 best_packidx_path = path_packidx;
511 nobj_max = nobj;
515 if (best_packidx_path) {
516 err = got_repo_get_packidx(best_packidx, best_packidx_path,
517 repo);
520 return err;
523 struct search_deltas_arg {
524 struct got_packidx *packidx;
525 struct got_pack *pack;
526 struct got_object_idset *idset;
527 struct got_pack_metavec *v;
528 int delta_cache_fd;
529 struct got_repository *repo;
530 got_pack_progress_cb progress_cb;
531 void *progress_arg;
532 struct got_ratelimit *rl;
533 got_cancel_cb cancel_cb;
534 void *cancel_arg;
535 int ncolored;
536 int nfound;
537 int ntrees;
538 int ncommits;
539 };
541 static const struct got_error *
542 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
544 const struct got_error *err;
545 struct got_pack_meta *m = data;
546 struct search_deltas_arg *a = arg;
547 int obj_idx;
548 struct got_object *obj = NULL;
550 if (a->cancel_cb) {
551 err = (*a->cancel_cb)(a->cancel_arg);
552 if (err)
553 return err;
556 if (!got_repo_check_packidx_bloom_filter(a->repo,
557 a->packidx->path_packidx, id))
558 return NULL;
560 obj_idx = got_packidx_get_object_idx(a->packidx, id);
561 if (obj_idx == -1)
562 return NULL;
564 /* TODO:
565 * Opening and closing an object just to check its flags
566 * is a bit expensive. We could have an imsg which requests
567 * plain type/size information for an object without doing
568 * work such as traversing the object's entire delta chain
569 * to find the base object type, and other such info which
570 * we don't really need here.
571 */
572 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
573 a->packidx, obj_idx, a->repo);
574 if (err)
575 return err;
577 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
578 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
579 a->delta_cache_fd, a->repo);
580 if (err)
581 goto done;
582 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
583 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
584 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
586 done:
587 got_object_close(obj);
588 return err;
591 static const struct got_error *
592 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
593 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
594 struct got_repository *repo,
595 got_pack_progress_cb progress_cb, void *progress_arg,
596 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
598 const struct got_error *err = NULL;
599 char *path_packfile = NULL;
600 struct got_packidx *packidx;
601 struct got_pack *pack;
602 struct search_deltas_arg sda;
604 err = find_pack_for_reuse(&packidx, repo);
605 if (err)
606 return err;
608 if (packidx == NULL)
609 return NULL;
611 err = got_packidx_get_packfile_path(&path_packfile,
612 packidx->path_packidx);
613 if (err)
614 return err;
616 pack = got_repo_get_cached_pack(repo, path_packfile);
617 if (pack == NULL) {
618 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
619 if (err)
620 goto done;
623 sda.packidx = packidx;
624 sda.pack = pack;
625 sda.idset = idset;
626 sda.v = v;
627 sda.delta_cache_fd = delta_cache_fd;
628 sda.repo = repo;
629 sda.progress_cb = progress_cb;
630 sda.progress_arg = progress_arg;
631 sda.rl = rl;
632 sda.cancel_cb = cancel_cb;
633 sda.cancel_arg = cancel_arg;
634 sda.ncolored = ncolored;
635 sda.nfound = nfound;
636 sda.ntrees = ntrees;
637 sda.ncommits = ncommits;
638 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
639 done:
640 free(path_packfile);
641 return err;
644 static const struct got_error *
645 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
646 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
647 struct got_repository *repo,
648 got_pack_progress_cb progress_cb, void *progress_arg,
649 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
651 const struct got_error *err = NULL;
652 struct got_pack_meta *m = NULL, *base = NULL;
653 struct got_raw_object *raw = NULL, *base_raw = NULL;
654 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
655 int i, j, ndeltas, best_ndeltas;
656 off_t size, best_size;
657 const int max_base_candidates = 3;
658 size_t delta_memsize = 0;
659 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
660 int outfd = -1;
662 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
663 for (i = 0; i < nmeta; i++) {
664 if (cancel_cb) {
665 err = (*cancel_cb)(cancel_arg);
666 if (err)
667 break;
669 err = report_progress(progress_cb, progress_arg, rl,
670 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
671 nreused + i, 0);
672 if (err)
673 goto done;
674 m = meta[i];
676 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
677 m->obj_type == GOT_OBJ_TYPE_TAG)
678 continue;
680 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
681 if (err)
682 goto done;
683 m->size = raw->size;
685 if (raw->f == NULL) {
686 err = got_deltify_init_mem(&m->dtab, raw->data,
687 raw->hdrlen, raw->size + raw->hdrlen);
688 } else {
689 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
690 raw->size + raw->hdrlen);
692 if (err)
693 goto done;
695 if (i > max_base_candidates) {
696 struct got_pack_meta *n = NULL;
697 n = meta[i - (max_base_candidates + 1)];
698 got_deltify_free(n->dtab);
699 n->dtab = NULL;
702 best_size = raw->size;
703 best_ndeltas = 0;
704 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
705 if (cancel_cb) {
706 err = (*cancel_cb)(cancel_arg);
707 if (err)
708 goto done;
710 base = meta[j];
711 /* long chains make unpacking slow, avoid such bases */
712 if (base->nchain >= 128 ||
713 base->obj_type != m->obj_type)
714 continue;
716 err = got_object_raw_open(&base_raw, &outfd, repo,
717 &base->id);
718 if (err)
719 goto done;
721 if (raw->f == NULL && base_raw->f == NULL) {
722 err = got_deltify_mem_mem(&deltas, &ndeltas,
723 raw->data, raw->hdrlen,
724 raw->size + raw->hdrlen,
725 base->dtab, base_raw->data,
726 base_raw->hdrlen,
727 base_raw->size + base_raw->hdrlen);
728 } else if (raw->f == NULL) {
729 err = got_deltify_mem_file(&deltas, &ndeltas,
730 raw->data, raw->hdrlen,
731 raw->size + raw->hdrlen,
732 base->dtab, base_raw->f,
733 base_raw->hdrlen,
734 base_raw->size + base_raw->hdrlen);
735 } else if (base_raw->f == NULL) {
736 err = got_deltify_file_mem(&deltas, &ndeltas,
737 raw->f, raw->hdrlen,
738 raw->size + raw->hdrlen,
739 base->dtab, base_raw->data,
740 base_raw->hdrlen,
741 base_raw->size + base_raw->hdrlen);
742 } else {
743 err = got_deltify(&deltas, &ndeltas,
744 raw->f, raw->hdrlen,
745 raw->size + raw->hdrlen,
746 base->dtab, base_raw->f, base_raw->hdrlen,
747 base_raw->size + base_raw->hdrlen);
749 got_object_raw_close(base_raw);
750 base_raw = NULL;
751 if (err)
752 goto done;
754 size = delta_size(deltas, ndeltas);
755 if (size + 32 < best_size){
756 /*
757 * if we already picked a best delta,
758 * replace it.
759 */
760 best_size = size;
761 free(best_deltas);
762 best_deltas = deltas;
763 best_ndeltas = ndeltas;
764 deltas = NULL;
765 m->nchain = base->nchain + 1;
766 m->prev = base;
767 m->head = base->head;
768 if (m->head == NULL)
769 m->head = base;
770 } else {
771 free(deltas);
772 deltas = NULL;
773 ndeltas = 0;
777 if (best_ndeltas > 0) {
778 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
779 delta_memsize + best_size <= max_delta_memsize) {
780 delta_memsize += best_size;
781 err = encode_delta_in_mem(m, raw, best_deltas,
782 best_ndeltas, best_size, m->prev->size);
783 } else {
784 m->delta_offset = ftello(delta_cache);
785 /*
786 * TODO:
787 * Storing compressed delta data in the delta
788 * cache file would probably be more efficient
789 * than writing uncompressed delta data here
790 * and compressing it while writing the pack
791 * file. This would also allow for reusing
792 * deltas in their compressed form.
793 */
794 err = encode_delta(m, raw, best_deltas,
795 best_ndeltas, m->prev->size, delta_cache);
797 free(best_deltas);
798 best_deltas = NULL;
799 best_ndeltas = 0;
800 if (err)
801 goto done;
804 got_object_raw_close(raw);
805 raw = NULL;
807 done:
808 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
809 got_deltify_free(meta[i]->dtab);
810 meta[i]->dtab = NULL;
812 if (raw)
813 got_object_raw_close(raw);
814 if (base_raw)
815 got_object_raw_close(base_raw);
816 if (outfd != -1 && close(outfd) == -1 && err == NULL)
817 err = got_error_from_errno("close");
818 free(deltas);
819 free(best_deltas);
820 return err;
823 static const struct got_error *
824 search_packidx(int *found, struct got_object_id *id,
825 struct got_repository *repo)
827 const struct got_error *err = NULL;
828 struct got_packidx *packidx = NULL;
829 int idx;
831 *found = 0;
833 err = got_repo_search_packidx(&packidx, &idx, repo, id);
834 if (err == NULL)
835 *found = 1; /* object is already packed */
836 else if (err->code == GOT_ERR_NO_OBJ)
837 err = NULL;
838 return err;
841 static const int obj_types[] = {
842 GOT_OBJ_TYPE_ANY,
843 GOT_OBJ_TYPE_COMMIT,
844 GOT_OBJ_TYPE_TREE,
845 GOT_OBJ_TYPE_BLOB,
846 GOT_OBJ_TYPE_TAG,
847 GOT_OBJ_TYPE_OFFSET_DELTA,
848 GOT_OBJ_TYPE_REF_DELTA
849 };
851 static const struct got_error *
852 add_object(int want_meta, struct got_object_idset *idset,
853 struct got_object_id *id, const char *path, int obj_type,
854 time_t mtime, int loose_obj_only, struct got_repository *repo)
856 const struct got_error *err;
857 struct got_pack_meta *m = NULL;
859 if (loose_obj_only) {
860 int is_packed;
861 err = search_packidx(&is_packed, id, repo);
862 if (err)
863 return err;
864 if (is_packed)
865 return NULL;
868 if (want_meta) {
869 err = alloc_meta(&m, id, path, obj_type, mtime);
870 if (err)
871 return err;
874 return got_object_idset_add(idset, id, m);
877 static const struct got_error *
878 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
879 struct got_object_idset *idset, struct got_object_id *tree_id,
880 const char *dpath, time_t mtime, struct got_repository *repo,
881 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
882 got_pack_progress_cb progress_cb, void *progress_arg,
883 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
885 const struct got_error *err;
886 struct got_tree_object *tree;
887 char *p = NULL;
888 int i;
890 err = got_object_open_as_tree(&tree, repo, tree_id);
891 if (err)
892 return err;
894 (*ntrees)++;
895 err = report_progress(progress_cb, progress_arg, rl,
896 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
897 if (err)
898 return err;
900 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
901 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
902 struct got_object_id *id = got_tree_entry_get_id(e);
903 mode_t mode = got_tree_entry_get_mode(e);
905 if (cancel_cb) {
906 err = (*cancel_cb)(cancel_arg);
907 if (err)
908 break;
911 if (got_object_tree_entry_is_submodule(e) ||
912 got_object_idset_contains(idset, id))
913 continue;
915 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
916 got_tree_entry_get_name(e)) == -1) {
917 err = got_error_from_errno("asprintf");
918 break;
921 if (S_ISDIR(mode)) {
922 struct got_object_qid *qid;
923 err = got_object_qid_alloc(&qid, id);
924 if (err)
925 break;
926 STAILQ_INSERT_TAIL(ids, qid, entry);
927 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
928 err = add_object(want_meta, idset, id, p,
929 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo);
930 if (err)
931 break;
932 (*nfound)++;
933 err = report_progress(progress_cb, progress_arg, rl,
934 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
935 if (err)
936 break;
938 free(p);
939 p = NULL;
942 got_object_tree_close(tree);
943 free(p);
944 return err;
947 static const struct got_error *
948 load_tree(int want_meta, struct got_object_idset *idset,
949 struct got_object_id *tree_id, const char *dpath, time_t mtime,
950 struct got_repository *repo, int loose_obj_only,
951 int *ncolored, int *nfound, int *ntrees,
952 got_pack_progress_cb progress_cb, void *progress_arg,
953 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
955 const struct got_error *err = NULL;
956 struct got_object_id_queue tree_ids;
957 struct got_object_qid *qid;
959 if (got_object_idset_contains(idset, tree_id))
960 return NULL;
962 err = got_object_qid_alloc(&qid, tree_id);
963 if (err)
964 return err;
966 STAILQ_INIT(&tree_ids);
967 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
969 while (!STAILQ_EMPTY(&tree_ids)) {
970 if (cancel_cb) {
971 err = (*cancel_cb)(cancel_arg);
972 if (err)
973 break;
976 qid = STAILQ_FIRST(&tree_ids);
977 STAILQ_REMOVE_HEAD(&tree_ids, entry);
979 if (got_object_idset_contains(idset, qid->id)) {
980 got_object_qid_free(qid);
981 continue;
984 err = add_object(want_meta, idset, qid->id, dpath,
985 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo);
986 if (err) {
987 got_object_qid_free(qid);
988 break;
991 (*nfound)++;
992 err = report_progress(progress_cb, progress_arg, rl,
993 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
994 if (err)
995 break;
997 err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
998 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
999 ntrees, progress_cb, progress_arg, rl,
1000 cancel_cb, cancel_arg);
1001 got_object_qid_free(qid);
1002 if (err)
1003 break;
1006 got_object_id_queue_free(&tree_ids);
1007 return err;
1010 static const struct got_error *
1011 load_commit(int want_meta, struct got_object_idset *idset,
1012 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1013 int *ncolored, int *nfound, int *ntrees,
1014 got_pack_progress_cb progress_cb, void *progress_arg,
1015 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1017 const struct got_error *err;
1018 struct got_commit_object *commit;
1020 if (got_object_idset_contains(idset, id))
1021 return NULL;
1023 if (loose_obj_only) {
1024 int is_packed;
1025 err = search_packidx(&is_packed, id, repo);
1026 if (err)
1027 return err;
1028 if (is_packed)
1029 return NULL;
1032 err = got_object_open_as_commit(&commit, repo, id);
1033 if (err)
1034 return err;
1036 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1037 got_object_commit_get_committer_time(commit),
1038 loose_obj_only, repo);
1039 if (err)
1040 goto done;
1042 (*nfound)++;
1043 err = report_progress(progress_cb, progress_arg, rl,
1044 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
1045 if (err)
1046 goto done;
1048 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1049 "", got_object_commit_get_committer_time(commit),
1050 repo, loose_obj_only, ncolored, nfound, ntrees,
1051 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1052 done:
1053 got_object_commit_close(commit);
1054 return err;
1057 static const struct got_error *
1058 load_tag(int want_meta, struct got_object_idset *idset,
1059 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1060 int *ncolored, int *nfound, int *ntrees,
1061 got_pack_progress_cb progress_cb, void *progress_arg,
1062 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1064 const struct got_error *err;
1065 struct got_tag_object *tag = NULL;
1067 if (got_object_idset_contains(idset, id))
1068 return NULL;
1070 if (loose_obj_only) {
1071 int is_packed;
1072 err = search_packidx(&is_packed, id, repo);
1073 if (err)
1074 return err;
1075 if (is_packed)
1076 return NULL;
1079 err = got_object_open_as_tag(&tag, repo, id);
1080 if (err)
1081 return err;
1083 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1084 got_object_tag_get_tagger_time(tag),
1085 loose_obj_only, repo);
1086 if (err)
1087 goto done;
1089 (*nfound)++;
1090 err = report_progress(progress_cb, progress_arg, rl,
1091 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
1092 if (err)
1093 goto done;
1095 switch (got_object_tag_get_object_type(tag)) {
1096 case GOT_OBJ_TYPE_COMMIT:
1097 err = load_commit(want_meta, idset,
1098 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1099 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1100 cancel_cb, cancel_arg);
1101 break;
1102 case GOT_OBJ_TYPE_TREE:
1103 err = load_tree(want_meta, idset,
1104 got_object_tag_get_object_id(tag), "",
1105 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1106 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1107 cancel_cb, cancel_arg);
1108 break;
1109 default:
1110 break;
1113 done:
1114 got_object_tag_close(tag);
1115 return err;
1118 enum findtwixt_color {
1119 COLOR_KEEP = 0,
1120 COLOR_DROP,
1121 COLOR_BLANK,
1123 static const int findtwixt_colors[] = {
1124 COLOR_KEEP,
1125 COLOR_DROP,
1126 COLOR_BLANK
1129 static const struct got_error *
1130 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1131 int color, struct got_repository *repo)
1133 const struct got_error *err;
1134 struct got_object_qid *qid;
1136 err = got_object_qid_alloc(&qid, id);
1137 if (err)
1138 return err;
1140 STAILQ_INSERT_TAIL(ids, qid, entry);
1141 qid->data = (void *)&findtwixt_colors[color];
1142 return NULL;
1145 static const struct got_error *
1146 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1147 struct got_object_id *id, struct got_repository *repo,
1148 got_cancel_cb cancel_cb, void *cancel_arg)
1150 const struct got_error *err = NULL;
1151 struct got_commit_object *commit;
1152 const struct got_object_id_queue *parents;
1153 struct got_object_id_queue ids;
1154 struct got_object_qid *qid;
1156 STAILQ_INIT(&ids);
1158 err = got_object_qid_alloc(&qid, id);
1159 if (err)
1160 return err;
1161 STAILQ_INSERT_HEAD(&ids, qid, entry);
1163 while (!STAILQ_EMPTY(&ids)) {
1164 if (cancel_cb) {
1165 err = (*cancel_cb)(cancel_arg);
1166 if (err)
1167 break;
1170 qid = STAILQ_FIRST(&ids);
1171 STAILQ_REMOVE_HEAD(&ids, entry);
1173 if (got_object_idset_contains(drop, qid->id)) {
1174 got_object_qid_free(qid);
1175 continue;
1178 err = got_object_idset_add(drop, qid->id,
1179 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1180 if (err) {
1181 got_object_qid_free(qid);
1182 break;
1185 if (!got_object_idset_contains(keep, qid->id)) {
1186 got_object_qid_free(qid);
1187 continue;
1190 err = got_object_open_as_commit(&commit, repo, qid->id);
1191 got_object_qid_free(qid);
1192 if (err)
1193 break;
1195 parents = got_object_commit_get_parent_ids(commit);
1196 if (parents) {
1197 err = got_object_id_queue_copy(parents, &ids);
1198 if (err) {
1199 got_object_commit_close(commit);
1200 break;
1203 got_object_commit_close(commit);
1206 got_object_id_queue_free(&ids);
1207 return err;
1210 struct append_id_arg {
1211 struct got_object_id **array;
1212 int idx;
1215 static const struct got_error *
1216 append_id(struct got_object_id *id, void *data, void *arg)
1218 struct append_id_arg *a = arg;
1220 a->array[a->idx] = got_object_id_dup(id);
1221 if (a->array[a->idx] == NULL)
1222 return got_error_from_errno("got_object_id_dup");
1224 a->idx++;
1225 return NULL;
1228 static const struct got_error *
1229 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1230 struct got_object_id **head, int nhead,
1231 struct got_object_id **tail, int ntail,
1232 struct got_repository *repo,
1233 got_pack_progress_cb progress_cb, void *progress_arg,
1234 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1236 const struct got_error *err = NULL;
1237 struct got_object_id_queue ids;
1238 struct got_object_idset *keep, *drop;
1239 struct got_object_qid *qid;
1240 int i, ncolor, nkeep, obj_type;
1242 STAILQ_INIT(&ids);
1243 *res = NULL;
1244 *nres = 0;
1245 *ncolored = 0;
1247 keep = got_object_idset_alloc();
1248 if (keep == NULL)
1249 return got_error_from_errno("got_object_idset_alloc");
1251 drop = got_object_idset_alloc();
1252 if (drop == NULL) {
1253 err = got_error_from_errno("got_object_idset_alloc");
1254 goto done;
1257 for (i = 0; i < nhead; i++) {
1258 struct got_object_id *id = head[i];
1259 if (id == NULL)
1260 continue;
1261 err = got_object_get_type(&obj_type, repo, id);
1262 if (err)
1263 return err;
1264 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1265 continue;
1266 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1267 if (err)
1268 goto done;
1270 for (i = 0; i < ntail; i++) {
1271 struct got_object_id *id = tail[i];
1272 if (id == NULL)
1273 continue;
1274 err = got_object_get_type(&obj_type, repo, id);
1275 if (err)
1276 return err;
1277 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1278 continue;
1279 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1280 if (err)
1281 goto done;
1284 while (!STAILQ_EMPTY(&ids)) {
1285 int qcolor;
1286 qid = STAILQ_FIRST(&ids);
1287 qcolor = *((int *)qid->data);
1289 if (got_object_idset_contains(drop, qid->id))
1290 ncolor = COLOR_DROP;
1291 else if (got_object_idset_contains(keep, qid->id))
1292 ncolor = COLOR_KEEP;
1293 else
1294 ncolor = COLOR_BLANK;
1296 (*ncolored)++;
1297 err = report_progress(progress_cb, progress_arg, rl,
1298 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1299 if (err)
1300 goto done;
1302 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1303 qcolor == COLOR_KEEP)) {
1304 STAILQ_REMOVE_HEAD(&ids, entry);
1305 got_object_qid_free(qid);
1306 continue;
1309 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1310 err = drop_commit(keep, drop, qid->id, repo,
1311 cancel_cb, cancel_arg);
1312 if (err)
1313 goto done;
1314 } else if (ncolor == COLOR_BLANK) {
1315 struct got_commit_object *commit;
1316 struct got_object_id *id;
1317 const struct got_object_id_queue *parents;
1318 struct got_object_qid *pid;
1320 id = got_object_id_dup(qid->id);
1321 if (id == NULL) {
1322 err = got_error_from_errno("got_object_id_dup");
1323 goto done;
1325 if (qcolor == COLOR_KEEP)
1326 err = got_object_idset_add(keep, id,
1327 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1328 else
1329 err = got_object_idset_add(drop, id,
1330 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1331 if (err) {
1332 free(id);
1333 goto done;
1336 err = got_object_open_as_commit(&commit, repo, id);
1337 if (err) {
1338 free(id);
1339 goto done;
1341 parents = got_object_commit_get_parent_ids(commit);
1342 if (parents) {
1343 STAILQ_FOREACH(pid, parents, entry) {
1344 err = queue_commit_id(&ids, pid->id,
1345 qcolor, repo);
1346 if (err) {
1347 free(id);
1348 goto done;
1352 got_object_commit_close(commit);
1353 commit = NULL;
1354 } else {
1355 /* should not happen */
1356 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1357 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1358 goto done;
1361 STAILQ_REMOVE_HEAD(&ids, entry);
1362 got_object_qid_free(qid);
1365 nkeep = got_object_idset_num_elements(keep);
1366 if (nkeep > 0) {
1367 struct append_id_arg arg;
1368 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1369 if (arg.array == NULL) {
1370 err = got_error_from_errno("calloc");
1371 goto done;
1373 arg.idx = 0;
1374 err = got_object_idset_for_each(keep, append_id, &arg);
1375 if (err) {
1376 free(arg.array);
1377 goto done;
1379 *res = arg.array;
1380 *nres = nkeep;
1382 done:
1383 got_object_idset_free(keep);
1384 got_object_idset_free(drop);
1385 got_object_id_queue_free(&ids);
1386 return err;
1389 static const struct got_error *
1390 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1391 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1392 struct got_object_id **ours, int nours, struct got_repository *repo,
1393 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1394 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1396 const struct got_error *err = NULL;
1397 struct got_object_id **ids = NULL;
1398 int i, nobj = 0, obj_type;
1400 *ncolored = 0;
1401 *nfound = 0;
1402 *ntrees = 0;
1404 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1405 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1406 if (err || nobj == 0)
1407 goto done;
1409 for (i = 0; i < ntheirs; i++) {
1410 struct got_object_id *id = theirs[i];
1411 if (id == NULL)
1412 continue;
1413 err = got_object_get_type(&obj_type, repo, id);
1414 if (err)
1415 return err;
1416 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1417 continue;
1418 err = load_commit(0, idset, id, repo, loose_obj_only,
1419 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1420 cancel_cb, cancel_arg);
1421 if (err)
1422 goto done;
1425 for (i = 0; i < ntheirs; i++) {
1426 struct got_object_id *id = theirs[i];
1427 struct got_pack_meta *m;
1428 if (id == NULL)
1429 continue;
1430 m = got_object_idset_get(idset, id);
1431 if (m == NULL) {
1432 err = got_object_get_type(&obj_type, repo, id);
1433 if (err)
1434 goto done;
1435 } else
1436 obj_type = m->obj_type;
1437 if (obj_type != GOT_OBJ_TYPE_TAG)
1438 continue;
1439 err = load_tag(0, idset, id, repo, loose_obj_only,
1440 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1441 cancel_cb, cancel_arg);
1442 if (err)
1443 goto done;
1446 for (i = 0; i < nobj; i++) {
1447 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1448 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1449 cancel_cb, cancel_arg);
1450 if (err)
1451 goto done;
1454 for (i = 0; i < nours; i++) {
1455 struct got_object_id *id = ours[i];
1456 struct got_pack_meta *m;
1457 if (id == NULL)
1458 continue;
1459 m = got_object_idset_get(idset, id);
1460 if (m == NULL) {
1461 err = got_object_get_type(&obj_type, repo, id);
1462 if (err)
1463 goto done;
1464 } else
1465 obj_type = m->obj_type;
1466 if (obj_type != GOT_OBJ_TYPE_TAG)
1467 continue;
1468 err = load_tag(1, idset, id, repo, loose_obj_only,
1469 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1470 cancel_cb, cancel_arg);
1471 if (err)
1472 goto done;
1474 done:
1475 for (i = 0; i < nobj; i++) {
1476 free(ids[i]);
1478 free(ids);
1479 return err;
1482 const struct got_error *
1483 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1485 size_t n;
1487 SHA1Update(ctx, buf, len);
1488 n = fwrite(buf, 1, len, f);
1489 if (n != len)
1490 return got_ferror(f, GOT_ERR_IO);
1491 return NULL;
1494 static void
1495 putbe32(char *b, uint32_t n)
1497 b[0] = n >> 24;
1498 b[1] = n >> 16;
1499 b[2] = n >> 8;
1500 b[3] = n >> 0;
1503 static int
1504 write_order_cmp(const void *pa, const void *pb)
1506 struct got_pack_meta *a, *b, *ahd, *bhd;
1508 a = *(struct got_pack_meta **)pa;
1509 b = *(struct got_pack_meta **)pb;
1510 ahd = (a->head == NULL) ? a : a->head;
1511 bhd = (b->head == NULL) ? b : b->head;
1512 if (ahd->mtime != bhd->mtime)
1513 return bhd->mtime - ahd->mtime;
1514 if (ahd != bhd)
1515 return (uintptr_t)bhd - (uintptr_t)ahd;
1516 if (a->nchain != b->nchain)
1517 return a->nchain - b->nchain;
1518 return a->mtime - b->mtime;
1521 static int
1522 reuse_write_order_cmp(const void *pa, const void *pb)
1524 struct got_pack_meta *a, *b;
1526 a = *(struct got_pack_meta **)pa;
1527 b = *(struct got_pack_meta **)pb;
1529 if (a->reused_delta_offset < b->reused_delta_offset)
1530 return -1;
1531 if (a->reused_delta_offset > b->reused_delta_offset)
1532 return 1;
1533 return 0;
1536 static const struct got_error *
1537 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1539 size_t i;
1541 *hdrlen = 0;
1543 hdr[0] = obj_type << 4;
1544 hdr[0] |= len & 0xf;
1545 len >>= 4;
1546 for (i = 1; len != 0; i++){
1547 if (i >= bufsize)
1548 return got_error(GOT_ERR_NO_SPACE);
1549 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1550 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1551 len >>= GOT_DELTA_SIZE_SHIFT;
1554 *hdrlen = i;
1555 return NULL;
1558 static int
1559 packoff(char *hdr, off_t off)
1561 int i, j;
1562 char rbuf[8];
1564 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1565 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1566 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1567 GOT_DELTA_SIZE_MORE;
1570 j = 0;
1571 while (i > 0)
1572 hdr[j++] = rbuf[--i];
1573 return j;
1576 static const struct got_error *
1577 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1578 struct got_pack_meta *m)
1580 const struct got_error *err;
1581 char buf[32];
1582 int nh;
1584 if (m->prev->off != 0) {
1585 err = packhdr(&nh, buf, sizeof(buf),
1586 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1587 if (err)
1588 return err;
1589 nh += packoff(buf + nh, m->off - m->prev->off);
1590 err = hwrite(packfile, buf, nh, ctx);
1591 if (err)
1592 return err;
1593 *packfile_size += nh;
1594 } else {
1595 err = packhdr(&nh, buf, sizeof(buf),
1596 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1597 if (err)
1598 return err;
1599 err = hwrite(packfile, buf, nh, ctx);
1600 if (err)
1601 return err;
1602 *packfile_size += nh;
1603 err = hwrite(packfile, m->prev->id.sha1,
1604 sizeof(m->prev->id.sha1), ctx);
1605 if (err)
1606 return err;
1607 *packfile_size += sizeof(m->prev->id.sha1);
1610 return NULL;
1613 static const struct got_error *
1614 write_packed_object(off_t *packfile_size, FILE *packfile,
1615 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1616 SHA1_CTX *ctx, struct got_repository *repo)
1618 const struct got_error *err = NULL;
1619 struct got_deflate_checksum csum;
1620 char buf[32];
1621 int nh;
1622 struct got_raw_object *raw = NULL;
1623 off_t outlen;
1625 csum.output_sha1 = ctx;
1626 csum.output_crc = NULL;
1628 m->off = ftello(packfile);
1629 if (m->delta_len == 0) {
1630 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1631 if (err)
1632 goto done;
1633 err = packhdr(&nh, buf, sizeof(buf),
1634 m->obj_type, raw->size);
1635 if (err)
1636 goto done;
1637 err = hwrite(packfile, buf, nh, ctx);
1638 if (err)
1639 goto done;
1640 *packfile_size += nh;
1641 if (raw->f == NULL) {
1642 err = got_deflate_to_file_mmap(&outlen,
1643 raw->data + raw->hdrlen, 0, raw->size,
1644 packfile, &csum);
1645 if (err)
1646 goto done;
1647 } else {
1648 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1649 == -1) {
1650 err = got_error_from_errno("fseeko");
1651 goto done;
1653 err = got_deflate_to_file(&outlen, raw->f,
1654 raw->size, packfile, &csum);
1655 if (err)
1656 goto done;
1658 *packfile_size += outlen;
1659 got_object_raw_close(raw);
1660 raw = NULL;
1661 } else if (m->delta_buf) {
1662 err = deltahdr(packfile_size, ctx, packfile, m);
1663 if (err)
1664 goto done;
1665 err = got_deflate_to_file_mmap(&outlen,
1666 m->delta_buf, 0, m->delta_len, packfile, &csum);
1667 if (err)
1668 goto done;
1669 *packfile_size += outlen;
1670 free(m->delta_buf);
1671 m->delta_buf = NULL;
1672 } else {
1673 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1674 == -1) {
1675 err = got_error_from_errno("fseeko");
1676 goto done;
1678 err = deltahdr(packfile_size, ctx, packfile, m);
1679 if (err)
1680 goto done;
1681 err = got_deflate_to_file(&outlen, delta_cache,
1682 m->delta_len, packfile, &csum);
1683 if (err)
1684 goto done;
1685 *packfile_size += outlen;
1687 done:
1688 if (raw)
1689 got_object_raw_close(raw);
1690 return err;
1693 static const struct got_error *
1694 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1695 struct got_pack_meta **deltify, int ndeltify,
1696 struct got_pack_meta **reuse, int nreuse,
1697 int ncolored, int nfound, int ntrees, int nours,
1698 struct got_repository *repo,
1699 got_pack_progress_cb progress_cb, void *progress_arg,
1700 struct got_ratelimit *rl,
1701 got_cancel_cb cancel_cb, void *cancel_arg)
1703 const struct got_error *err = NULL;
1704 int i;
1705 SHA1_CTX ctx;
1706 struct got_pack_meta *m;
1707 char buf[32];
1708 size_t n;
1709 off_t packfile_size = 0;
1710 int outfd = -1;
1712 SHA1Init(&ctx);
1714 err = hwrite(packfile, "PACK", 4, &ctx);
1715 if (err)
1716 return err;
1717 putbe32(buf, GOT_PACKFILE_VERSION);
1718 err = hwrite(packfile, buf, 4, &ctx);
1719 if (err)
1720 goto done;
1721 putbe32(buf, ndeltify + nreuse);
1722 err = hwrite(packfile, buf, 4, &ctx);
1723 if (err)
1724 goto done;
1726 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1727 write_order_cmp);
1728 for (i = 0; i < ndeltify; i++) {
1729 err = report_progress(progress_cb, progress_arg, rl,
1730 ncolored, nfound, ntrees, packfile_size, nours,
1731 ndeltify + nreuse, ndeltify + nreuse, i);
1732 if (err)
1733 goto done;
1734 m = deltify[i];
1735 err = write_packed_object(&packfile_size, packfile,
1736 delta_cache, m, &outfd, &ctx, repo);
1737 if (err)
1738 goto done;
1741 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1742 reuse_write_order_cmp);
1743 for (i = 0; i < nreuse; i++) {
1744 err = report_progress(progress_cb, progress_arg, rl,
1745 ncolored, nfound, ntrees, packfile_size, nours,
1746 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1747 if (err)
1748 goto done;
1749 m = reuse[i];
1750 err = write_packed_object(&packfile_size, packfile,
1751 delta_cache, m, &outfd, &ctx, repo);
1752 if (err)
1753 goto done;
1756 SHA1Final(pack_sha1, &ctx);
1757 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1758 if (n != SHA1_DIGEST_LENGTH)
1759 err = got_ferror(packfile, GOT_ERR_IO);
1760 packfile_size += SHA1_DIGEST_LENGTH;
1761 packfile_size += sizeof(struct got_packfile_hdr);
1762 if (progress_cb) {
1763 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1764 packfile_size, nours, ndeltify + nreuse,
1765 ndeltify + nreuse, ndeltify + nreuse);
1766 if (err)
1767 goto done;
1769 done:
1770 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1771 err = got_error_from_errno("close");
1772 return err;
1775 static const struct got_error *
1776 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1778 struct got_object_idset *idset = arg;
1780 if (got_object_idset_get_element_data(entry) == NULL)
1781 got_object_idset_remove_element(idset, entry);
1783 return NULL;
1786 static const struct got_error *
1787 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1789 struct got_object_idset *idset = arg;
1790 struct got_pack_meta *m;
1792 m = got_object_idset_get_element_data(entry);
1793 if (m->have_reused_delta)
1794 got_object_idset_remove_element(idset, entry);
1796 return NULL;
1799 static const struct got_error *
1800 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1802 struct got_pack_meta *m = data;
1803 struct got_pack_metavec *v = arg;
1805 return add_meta(m, v);
1808 const struct got_error *
1809 got_pack_create(uint8_t *packsha1, FILE *packfile,
1810 struct got_object_id **theirs, int ntheirs,
1811 struct got_object_id **ours, int nours,
1812 struct got_repository *repo, int loose_obj_only, int allow_empty,
1813 got_pack_progress_cb progress_cb, void *progress_arg,
1814 got_cancel_cb cancel_cb, void *cancel_arg)
1816 const struct got_error *err;
1817 int delta_cache_fd = -1;
1818 FILE *delta_cache = NULL;
1819 struct got_object_idset *idset;
1820 struct got_ratelimit rl;
1821 struct got_pack_metavec deltify, reuse;
1822 int ncolored = 0, nfound = 0, ntrees = 0;
1824 memset(&deltify, 0, sizeof(deltify));
1825 memset(&reuse, 0, sizeof(reuse));
1827 got_ratelimit_init(&rl, 0, 500);
1829 idset = got_object_idset_alloc();
1830 if (idset == NULL)
1831 return got_error_from_errno("got_object_idset_alloc");
1833 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1834 ntheirs, ours, nours, repo, loose_obj_only,
1835 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1836 if (err)
1837 return err;
1839 err = got_object_idset_for_each_element(idset,
1840 remove_unused_object, idset);
1841 if (err)
1842 goto done;
1844 if (progress_cb) {
1845 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1846 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1847 if (err)
1848 goto done;
1851 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1852 err = got_error(GOT_ERR_CANNOT_PACK);
1853 goto done;
1856 delta_cache_fd = got_opentempfd();
1857 if (delta_cache_fd == -1) {
1858 err = got_error_from_errno("got_opentemp");
1859 goto done;
1862 reuse.metasz = 64;
1863 reuse.meta = calloc(reuse.metasz,
1864 sizeof(struct got_pack_meta *));
1865 if (reuse.meta == NULL) {
1866 err = got_error_from_errno("calloc");
1867 goto done;
1870 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1871 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1872 cancel_cb, cancel_arg);
1873 if (err)
1874 goto done;
1875 if (reuse.nmeta > 0) {
1876 err = got_object_idset_for_each_element(idset,
1877 remove_reused_object, idset);
1878 if (err)
1879 goto done;
1882 delta_cache = fdopen(delta_cache_fd, "a+");
1883 if (delta_cache == NULL) {
1884 err = got_error_from_errno("fdopen");
1885 goto done;
1887 delta_cache_fd = -1;
1889 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1890 err = got_error_from_errno("fseeko");
1891 goto done;
1894 deltify.meta = calloc(got_object_idset_num_elements(idset),
1895 sizeof(struct got_pack_meta *));
1896 if (deltify.meta == NULL) {
1897 err = got_error_from_errno("calloc");
1898 goto done;
1900 deltify.metasz = got_object_idset_num_elements(idset);
1902 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1903 if (err)
1904 goto done;
1905 if (deltify.nmeta > 0) {
1906 err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1907 nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1908 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1909 if (err)
1910 goto done;
1911 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1912 err = got_error_from_errno("fseeko");
1913 goto done;
1917 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1918 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1919 nours, repo, progress_cb, progress_arg, &rl,
1920 cancel_cb, cancel_arg);
1921 if (err)
1922 goto done;
1923 done:
1924 free_nmeta(deltify.meta, deltify.nmeta);
1925 free_nmeta(reuse.meta, reuse.nmeta);
1926 got_object_idset_free(idset);
1927 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1928 err = got_error_from_errno("close");
1929 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1930 err = got_error_from_errno("fclose");
1931 return err;