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"
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 struct got_error *
842 add_object(int want_meta, struct got_object_idset *idset,
843 struct got_object_id *id, const char *path, int obj_type,
844 time_t mtime, int loose_obj_only, struct got_repository *repo,
845 int *ncolored, int *nfound, int *ntrees,
846 got_pack_progress_cb progress_cb, void *progress_arg,
847 struct got_ratelimit *rl)
849 const struct got_error *err;
850 struct got_pack_meta *m = NULL;
852 if (loose_obj_only) {
853 int is_packed;
854 err = search_packidx(&is_packed, id, repo);
855 if (err)
856 return err;
857 if (is_packed && want_meta)
858 return NULL;
861 if (want_meta) {
862 err = alloc_meta(&m, id, path, obj_type, mtime);
863 if (err)
864 return err;
866 (*nfound)++;
867 err = report_progress(progress_cb, progress_arg, rl,
868 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
869 if (err) {
870 clear_meta(m);
871 free(m);
872 return err;
876 err = got_object_idset_add(idset, id, m);
877 if (err) {
878 clear_meta(m);
879 free(m);
881 return err;
884 static const struct got_error *
885 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
886 struct got_object_idset *idset, struct got_object_id *tree_id,
887 const char *dpath, time_t mtime, struct got_repository *repo,
888 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
889 got_pack_progress_cb progress_cb, void *progress_arg,
890 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
892 const struct got_error *err;
893 struct got_tree_object *tree;
894 char *p = NULL;
895 int i;
897 err = got_object_open_as_tree(&tree, repo, tree_id);
898 if (err)
899 return err;
901 (*ntrees)++;
902 err = report_progress(progress_cb, progress_arg, rl,
903 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
904 if (err)
905 return err;
907 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
908 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
909 struct got_object_id *id = got_tree_entry_get_id(e);
910 mode_t mode = got_tree_entry_get_mode(e);
912 if (cancel_cb) {
913 err = (*cancel_cb)(cancel_arg);
914 if (err)
915 break;
918 if (got_object_tree_entry_is_submodule(e) ||
919 got_object_idset_contains(idset, id))
920 continue;
922 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
923 got_tree_entry_get_name(e)) == -1) {
924 err = got_error_from_errno("asprintf");
925 break;
928 if (S_ISDIR(mode)) {
929 struct got_object_qid *qid;
930 err = got_object_qid_alloc(&qid, id);
931 if (err)
932 break;
933 STAILQ_INSERT_TAIL(ids, qid, entry);
934 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
935 err = add_object(want_meta, idset, id, p,
936 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
937 ncolored, nfound, ntrees,
938 progress_cb, progress_arg, rl);
939 if (err)
940 break;
942 free(p);
943 p = NULL;
946 got_object_tree_close(tree);
947 free(p);
948 return err;
951 static const struct got_error *
952 load_tree(int want_meta, struct got_object_idset *idset,
953 struct got_object_id *tree_id, const char *dpath, time_t mtime,
954 struct got_repository *repo, int loose_obj_only,
955 int *ncolored, int *nfound, int *ntrees,
956 got_pack_progress_cb progress_cb, void *progress_arg,
957 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
959 const struct got_error *err = NULL;
960 struct got_object_id_queue tree_ids;
961 struct got_object_qid *qid;
963 if (got_object_idset_contains(idset, tree_id))
964 return NULL;
966 err = got_object_qid_alloc(&qid, tree_id);
967 if (err)
968 return err;
970 STAILQ_INIT(&tree_ids);
971 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
973 while (!STAILQ_EMPTY(&tree_ids)) {
974 if (cancel_cb) {
975 err = (*cancel_cb)(cancel_arg);
976 if (err)
977 break;
980 qid = STAILQ_FIRST(&tree_ids);
981 STAILQ_REMOVE_HEAD(&tree_ids, entry);
983 if (got_object_idset_contains(idset, qid->id)) {
984 got_object_qid_free(qid);
985 continue;
988 err = add_object(want_meta, idset, qid->id, dpath,
989 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo,
990 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
991 if (err) {
992 got_object_qid_free(qid);
993 break;
996 err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
997 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
998 ntrees, progress_cb, progress_arg, rl,
999 cancel_cb, cancel_arg);
1000 got_object_qid_free(qid);
1001 if (err)
1002 break;
1005 got_object_id_queue_free(&tree_ids);
1006 return err;
1009 static const struct got_error *
1010 load_commit(int want_meta, struct got_object_idset *idset,
1011 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1012 int *ncolored, int *nfound, int *ntrees,
1013 got_pack_progress_cb progress_cb, void *progress_arg,
1014 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1016 const struct got_error *err;
1017 struct got_commit_object *commit;
1019 if (got_object_idset_contains(idset, id))
1020 return NULL;
1022 if (loose_obj_only) {
1023 int is_packed;
1024 err = search_packidx(&is_packed, id, repo);
1025 if (err)
1026 return err;
1027 if (is_packed && want_meta)
1028 return NULL;
1031 err = got_object_open_as_commit(&commit, repo, id);
1032 if (err)
1033 return err;
1035 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1036 got_object_commit_get_committer_time(commit),
1037 loose_obj_only, repo,
1038 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1039 if (err)
1040 goto done;
1042 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1043 "", got_object_commit_get_committer_time(commit),
1044 repo, loose_obj_only, ncolored, nfound, ntrees,
1045 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1046 done:
1047 got_object_commit_close(commit);
1048 return err;
1051 static const struct got_error *
1052 load_tag(int want_meta, struct got_object_idset *idset,
1053 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1054 int *ncolored, int *nfound, int *ntrees,
1055 got_pack_progress_cb progress_cb, void *progress_arg,
1056 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1058 const struct got_error *err;
1059 struct got_tag_object *tag = NULL;
1061 if (got_object_idset_contains(idset, id))
1062 return NULL;
1064 if (loose_obj_only) {
1065 int is_packed;
1066 err = search_packidx(&is_packed, id, repo);
1067 if (err)
1068 return err;
1069 if (is_packed && want_meta)
1070 return NULL;
1073 err = got_object_open_as_tag(&tag, repo, id);
1074 if (err)
1075 return err;
1077 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1078 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1079 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1080 if (err)
1081 goto done;
1083 switch (got_object_tag_get_object_type(tag)) {
1084 case GOT_OBJ_TYPE_COMMIT:
1085 err = load_commit(want_meta, idset,
1086 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1087 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1088 cancel_cb, cancel_arg);
1089 break;
1090 case GOT_OBJ_TYPE_TREE:
1091 err = load_tree(want_meta, idset,
1092 got_object_tag_get_object_id(tag), "",
1093 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1094 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1095 cancel_cb, cancel_arg);
1096 break;
1097 default:
1098 break;
1101 done:
1102 got_object_tag_close(tag);
1103 return err;
1106 enum findtwixt_color {
1107 COLOR_KEEP = 0,
1108 COLOR_DROP,
1109 COLOR_BLANK,
1111 static const int findtwixt_colors[] = {
1112 COLOR_KEEP,
1113 COLOR_DROP,
1114 COLOR_BLANK
1117 static const struct got_error *
1118 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1119 int color, struct got_repository *repo)
1121 const struct got_error *err;
1122 struct got_object_qid *qid;
1124 err = got_object_qid_alloc(&qid, id);
1125 if (err)
1126 return err;
1128 STAILQ_INSERT_TAIL(ids, qid, entry);
1129 qid->data = (void *)&findtwixt_colors[color];
1130 return NULL;
1133 static const struct got_error *
1134 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1135 struct got_object_id *id, struct got_repository *repo,
1136 got_cancel_cb cancel_cb, void *cancel_arg)
1138 const struct got_error *err = NULL;
1139 struct got_commit_object *commit;
1140 const struct got_object_id_queue *parents;
1141 struct got_object_id_queue ids;
1142 struct got_object_qid *qid;
1144 STAILQ_INIT(&ids);
1146 err = got_object_qid_alloc(&qid, id);
1147 if (err)
1148 return err;
1149 STAILQ_INSERT_HEAD(&ids, qid, entry);
1151 while (!STAILQ_EMPTY(&ids)) {
1152 if (cancel_cb) {
1153 err = (*cancel_cb)(cancel_arg);
1154 if (err)
1155 break;
1158 qid = STAILQ_FIRST(&ids);
1159 STAILQ_REMOVE_HEAD(&ids, entry);
1161 if (got_object_idset_contains(drop, qid->id)) {
1162 got_object_qid_free(qid);
1163 continue;
1166 err = got_object_idset_add(drop, qid->id, NULL);
1167 if (err) {
1168 got_object_qid_free(qid);
1169 break;
1172 if (!got_object_idset_contains(keep, qid->id)) {
1173 got_object_qid_free(qid);
1174 continue;
1177 err = got_object_open_as_commit(&commit, repo, qid->id);
1178 got_object_qid_free(qid);
1179 if (err)
1180 break;
1182 parents = got_object_commit_get_parent_ids(commit);
1183 if (parents) {
1184 err = got_object_id_queue_copy(parents, &ids);
1185 if (err) {
1186 got_object_commit_close(commit);
1187 break;
1190 got_object_commit_close(commit);
1193 got_object_id_queue_free(&ids);
1194 return err;
1197 struct append_id_arg {
1198 struct got_object_id **array;
1199 int idx;
1202 static const struct got_error *
1203 append_id(struct got_object_id *id, void *data, void *arg)
1205 struct append_id_arg *a = arg;
1207 a->array[a->idx] = got_object_id_dup(id);
1208 if (a->array[a->idx] == NULL)
1209 return got_error_from_errno("got_object_id_dup");
1211 a->idx++;
1212 return NULL;
1215 static const struct got_error *
1216 queue_commit_or_tag_id(struct got_object_id *id, int color,
1217 struct got_object_id_queue *ids, struct got_repository *repo)
1219 const struct got_error *err;
1220 struct got_tag_object *tag = NULL;
1221 int obj_type;
1223 err = got_object_get_type(&obj_type, repo, id);
1224 if (err)
1225 return err;
1227 if (obj_type == GOT_OBJ_TYPE_TAG) {
1228 err = got_object_open_as_tag(&tag, repo, id);
1229 if (err)
1230 return err;
1231 obj_type = got_object_tag_get_object_type(tag);
1232 id = got_object_tag_get_object_id(tag);
1235 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1236 err = queue_commit_id(ids, id, color, repo);
1237 if (err)
1238 goto done;
1240 done:
1241 if (tag)
1242 got_object_tag_close(tag);
1243 return err;
1246 static const struct got_error *
1247 color_commits(int *ncolored, struct got_object_id_queue *ids,
1248 struct got_object_idset *keep, struct got_object_idset *drop,
1249 struct got_repository *repo,
1250 got_pack_progress_cb progress_cb, void *progress_arg,
1251 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1253 const struct got_error *err = NULL;
1254 struct got_commit_object *commit = NULL;
1255 struct got_object_qid *qid;
1257 while (!STAILQ_EMPTY(ids)) {
1258 int qcolor, ncolor;
1260 if (cancel_cb) {
1261 err = cancel_cb(cancel_arg);
1262 if (err)
1263 break;
1266 qid = STAILQ_FIRST(ids);
1267 qcolor = *((int *)qid->data);
1269 if (got_object_idset_contains(drop, qid->id))
1270 ncolor = COLOR_DROP;
1271 else if (got_object_idset_contains(keep, qid->id))
1272 ncolor = COLOR_KEEP;
1273 else
1274 ncolor = COLOR_BLANK;
1276 (*ncolored)++;
1277 err = report_progress(progress_cb, progress_arg, rl,
1278 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1279 if (err)
1280 break;
1282 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1283 qcolor == COLOR_KEEP)) {
1284 STAILQ_REMOVE_HEAD(ids, entry);
1285 got_object_qid_free(qid);
1286 continue;
1289 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1290 err = drop_commit(keep, drop, qid->id, repo,
1291 cancel_cb, cancel_arg);
1292 if (err)
1293 break;
1294 } else if (ncolor == COLOR_BLANK) {
1295 struct got_commit_object *commit;
1296 const struct got_object_id_queue *parents;
1297 struct got_object_qid *pid;
1299 if (qcolor == COLOR_KEEP)
1300 err = got_object_idset_add(keep, qid->id, NULL);
1301 else
1302 err = got_object_idset_add(drop, qid->id, NULL);
1303 if (err)
1304 break;
1306 err = got_object_open_as_commit(&commit, repo, qid->id);
1307 if (err)
1308 break;
1310 parents = got_object_commit_get_parent_ids(commit);
1311 if (parents) {
1312 STAILQ_FOREACH(pid, parents, entry) {
1313 err = queue_commit_id(ids, pid->id,
1314 qcolor, repo);
1315 if (err)
1316 break;
1319 got_object_commit_close(commit);
1320 commit = NULL;
1321 } else {
1322 /* should not happen */
1323 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1324 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1325 break;
1328 STAILQ_REMOVE_HEAD(ids, entry);
1329 got_object_qid_free(qid);
1332 if (commit)
1333 got_object_commit_close(commit);
1334 return err;
1337 static const struct got_error *
1338 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1339 struct got_object_id **head, int nhead,
1340 struct got_object_id **tail, int ntail,
1341 struct got_repository *repo,
1342 got_pack_progress_cb progress_cb, void *progress_arg,
1343 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1345 const struct got_error *err = NULL;
1346 struct got_object_id_queue ids;
1347 struct got_object_idset *keep, *drop;
1348 int i, nkeep;
1350 STAILQ_INIT(&ids);
1351 *res = NULL;
1352 *nres = 0;
1353 *ncolored = 0;
1355 keep = got_object_idset_alloc();
1356 if (keep == NULL)
1357 return got_error_from_errno("got_object_idset_alloc");
1359 drop = got_object_idset_alloc();
1360 if (drop == NULL) {
1361 err = got_error_from_errno("got_object_idset_alloc");
1362 goto done;
1365 for (i = 0; i < nhead; i++) {
1366 struct got_object_id *id = head[i];
1367 if (id == NULL)
1368 continue;
1369 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1370 if (err)
1371 goto done;
1374 for (i = 0; i < ntail; i++) {
1375 struct got_object_id *id = tail[i];
1376 if (id == NULL)
1377 continue;
1378 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1379 if (err)
1380 goto done;
1383 err = color_commits(ncolored, &ids, keep, drop, repo,
1384 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1385 if (err)
1386 goto done;
1388 nkeep = got_object_idset_num_elements(keep);
1389 if (nkeep > 0) {
1390 struct append_id_arg arg;
1391 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1392 if (arg.array == NULL) {
1393 err = got_error_from_errno("calloc");
1394 goto done;
1396 arg.idx = 0;
1397 err = got_object_idset_for_each(keep, append_id, &arg);
1398 if (err) {
1399 free(arg.array);
1400 goto done;
1402 *res = arg.array;
1403 *nres = nkeep;
1405 done:
1406 got_object_idset_free(keep);
1407 got_object_idset_free(drop);
1408 got_object_id_queue_free(&ids);
1409 return err;
1412 static const struct got_error *
1413 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1414 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1415 struct got_object_id **ours, int nours, struct got_repository *repo,
1416 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1417 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1419 const struct got_error *err = NULL;
1420 struct got_object_id **ids = NULL;
1421 int i, nobj = 0, obj_type;
1423 *ncolored = 0;
1424 *nfound = 0;
1425 *ntrees = 0;
1427 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1428 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1429 if (err || nobj == 0)
1430 goto done;
1432 for (i = 0; i < ntheirs; i++) {
1433 struct got_object_id *id = theirs[i];
1434 if (id == NULL)
1435 continue;
1436 err = got_object_get_type(&obj_type, repo, id);
1437 if (err)
1438 return err;
1439 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1440 err = load_commit(0, idset, id, repo,
1441 loose_obj_only, ncolored, nfound, ntrees,
1442 progress_cb, progress_arg, rl,
1443 cancel_cb, cancel_arg);
1444 if (err)
1445 goto done;
1446 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1447 err = load_tag(0, idset, id, repo,
1448 loose_obj_only, ncolored, nfound, ntrees,
1449 progress_cb, progress_arg, rl,
1450 cancel_cb, cancel_arg);
1451 if (err)
1452 goto done;
1456 for (i = 0; i < nobj; i++) {
1457 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1458 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1459 cancel_cb, cancel_arg);
1460 if (err)
1461 goto done;
1464 for (i = 0; i < nours; i++) {
1465 struct got_object_id *id = ours[i];
1466 struct got_pack_meta *m;
1467 if (id == NULL)
1468 continue;
1469 m = got_object_idset_get(idset, id);
1470 if (m == NULL) {
1471 err = got_object_get_type(&obj_type, repo, id);
1472 if (err)
1473 goto done;
1474 } else
1475 obj_type = m->obj_type;
1476 if (obj_type != GOT_OBJ_TYPE_TAG)
1477 continue;
1478 err = load_tag(1, idset, id, repo, loose_obj_only,
1479 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1480 cancel_cb, cancel_arg);
1481 if (err)
1482 goto done;
1484 done:
1485 for (i = 0; i < nobj; i++) {
1486 free(ids[i]);
1488 free(ids);
1489 return err;
1492 const struct got_error *
1493 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1495 size_t n;
1497 SHA1Update(ctx, buf, len);
1498 n = fwrite(buf, 1, len, f);
1499 if (n != len)
1500 return got_ferror(f, GOT_ERR_IO);
1501 return NULL;
1504 static void
1505 putbe32(char *b, uint32_t n)
1507 b[0] = n >> 24;
1508 b[1] = n >> 16;
1509 b[2] = n >> 8;
1510 b[3] = n >> 0;
1513 static int
1514 write_order_cmp(const void *pa, const void *pb)
1516 struct got_pack_meta *a, *b, *ahd, *bhd;
1518 a = *(struct got_pack_meta **)pa;
1519 b = *(struct got_pack_meta **)pb;
1520 ahd = (a->head == NULL) ? a : a->head;
1521 bhd = (b->head == NULL) ? b : b->head;
1522 if (ahd->mtime != bhd->mtime)
1523 return bhd->mtime - ahd->mtime;
1524 if (ahd != bhd)
1525 return (uintptr_t)bhd - (uintptr_t)ahd;
1526 if (a->nchain != b->nchain)
1527 return a->nchain - b->nchain;
1528 return a->mtime - b->mtime;
1531 static int
1532 reuse_write_order_cmp(const void *pa, const void *pb)
1534 struct got_pack_meta *a, *b;
1536 a = *(struct got_pack_meta **)pa;
1537 b = *(struct got_pack_meta **)pb;
1539 if (a->reused_delta_offset < b->reused_delta_offset)
1540 return -1;
1541 if (a->reused_delta_offset > b->reused_delta_offset)
1542 return 1;
1543 return 0;
1546 static const struct got_error *
1547 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1549 size_t i;
1551 *hdrlen = 0;
1553 hdr[0] = obj_type << 4;
1554 hdr[0] |= len & 0xf;
1555 len >>= 4;
1556 for (i = 1; len != 0; i++){
1557 if (i >= bufsize)
1558 return got_error(GOT_ERR_NO_SPACE);
1559 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1560 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1561 len >>= GOT_DELTA_SIZE_SHIFT;
1564 *hdrlen = i;
1565 return NULL;
1568 static int
1569 packoff(char *hdr, off_t off)
1571 int i, j;
1572 char rbuf[8];
1574 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1575 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1576 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1577 GOT_DELTA_SIZE_MORE;
1580 j = 0;
1581 while (i > 0)
1582 hdr[j++] = rbuf[--i];
1583 return j;
1586 static const struct got_error *
1587 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1588 struct got_pack_meta *m)
1590 const struct got_error *err;
1591 char buf[32];
1592 int nh;
1594 if (m->prev->off != 0) {
1595 err = packhdr(&nh, buf, sizeof(buf),
1596 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1597 if (err)
1598 return err;
1599 nh += packoff(buf + nh, m->off - m->prev->off);
1600 err = hwrite(packfile, buf, nh, ctx);
1601 if (err)
1602 return err;
1603 *packfile_size += nh;
1604 } else {
1605 err = packhdr(&nh, buf, sizeof(buf),
1606 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1607 if (err)
1608 return err;
1609 err = hwrite(packfile, buf, nh, ctx);
1610 if (err)
1611 return err;
1612 *packfile_size += nh;
1613 err = hwrite(packfile, m->prev->id.sha1,
1614 sizeof(m->prev->id.sha1), ctx);
1615 if (err)
1616 return err;
1617 *packfile_size += sizeof(m->prev->id.sha1);
1620 return NULL;
1623 static const struct got_error *
1624 write_packed_object(off_t *packfile_size, FILE *packfile,
1625 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1626 SHA1_CTX *ctx, struct got_repository *repo)
1628 const struct got_error *err = NULL;
1629 struct got_deflate_checksum csum;
1630 char buf[32];
1631 int nh;
1632 struct got_raw_object *raw = NULL;
1633 off_t outlen;
1635 csum.output_sha1 = ctx;
1636 csum.output_crc = NULL;
1638 m->off = ftello(packfile);
1639 if (m->delta_len == 0) {
1640 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1641 if (err)
1642 goto done;
1643 err = packhdr(&nh, buf, sizeof(buf),
1644 m->obj_type, raw->size);
1645 if (err)
1646 goto done;
1647 err = hwrite(packfile, buf, nh, ctx);
1648 if (err)
1649 goto done;
1650 *packfile_size += nh;
1651 if (raw->f == NULL) {
1652 err = got_deflate_to_file_mmap(&outlen,
1653 raw->data + raw->hdrlen, 0, raw->size,
1654 packfile, &csum);
1655 if (err)
1656 goto done;
1657 } else {
1658 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1659 == -1) {
1660 err = got_error_from_errno("fseeko");
1661 goto done;
1663 err = got_deflate_to_file(&outlen, raw->f,
1664 raw->size, packfile, &csum);
1665 if (err)
1666 goto done;
1668 *packfile_size += outlen;
1669 got_object_raw_close(raw);
1670 raw = NULL;
1671 } else if (m->delta_buf) {
1672 err = deltahdr(packfile_size, ctx, packfile, m);
1673 if (err)
1674 goto done;
1675 err = got_deflate_to_file_mmap(&outlen,
1676 m->delta_buf, 0, m->delta_len, packfile, &csum);
1677 if (err)
1678 goto done;
1679 *packfile_size += outlen;
1680 free(m->delta_buf);
1681 m->delta_buf = NULL;
1682 } else {
1683 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1684 == -1) {
1685 err = got_error_from_errno("fseeko");
1686 goto done;
1688 err = deltahdr(packfile_size, ctx, packfile, m);
1689 if (err)
1690 goto done;
1691 err = got_deflate_to_file(&outlen, delta_cache,
1692 m->delta_len, packfile, &csum);
1693 if (err)
1694 goto done;
1695 *packfile_size += outlen;
1697 done:
1698 if (raw)
1699 got_object_raw_close(raw);
1700 return err;
1703 static const struct got_error *
1704 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1705 struct got_pack_meta **deltify, int ndeltify,
1706 struct got_pack_meta **reuse, int nreuse,
1707 int ncolored, int nfound, int ntrees, int nours,
1708 struct got_repository *repo,
1709 got_pack_progress_cb progress_cb, void *progress_arg,
1710 struct got_ratelimit *rl,
1711 got_cancel_cb cancel_cb, void *cancel_arg)
1713 const struct got_error *err = NULL;
1714 int i;
1715 SHA1_CTX ctx;
1716 struct got_pack_meta *m;
1717 char buf[32];
1718 size_t n;
1719 off_t packfile_size = 0;
1720 int outfd = -1;
1722 SHA1Init(&ctx);
1724 err = hwrite(packfile, "PACK", 4, &ctx);
1725 if (err)
1726 return err;
1727 putbe32(buf, GOT_PACKFILE_VERSION);
1728 err = hwrite(packfile, buf, 4, &ctx);
1729 if (err)
1730 goto done;
1731 putbe32(buf, ndeltify + nreuse);
1732 err = hwrite(packfile, buf, 4, &ctx);
1733 if (err)
1734 goto done;
1736 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1737 write_order_cmp);
1738 for (i = 0; i < ndeltify; i++) {
1739 err = report_progress(progress_cb, progress_arg, rl,
1740 ncolored, nfound, ntrees, packfile_size, nours,
1741 ndeltify + nreuse, ndeltify + nreuse, i);
1742 if (err)
1743 goto done;
1744 m = deltify[i];
1745 err = write_packed_object(&packfile_size, packfile,
1746 delta_cache, m, &outfd, &ctx, repo);
1747 if (err)
1748 goto done;
1751 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1752 reuse_write_order_cmp);
1753 for (i = 0; i < nreuse; i++) {
1754 err = report_progress(progress_cb, progress_arg, rl,
1755 ncolored, nfound, ntrees, packfile_size, nours,
1756 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1757 if (err)
1758 goto done;
1759 m = reuse[i];
1760 err = write_packed_object(&packfile_size, packfile,
1761 delta_cache, m, &outfd, &ctx, repo);
1762 if (err)
1763 goto done;
1766 SHA1Final(pack_sha1, &ctx);
1767 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1768 if (n != SHA1_DIGEST_LENGTH)
1769 err = got_ferror(packfile, GOT_ERR_IO);
1770 packfile_size += SHA1_DIGEST_LENGTH;
1771 packfile_size += sizeof(struct got_packfile_hdr);
1772 if (progress_cb) {
1773 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1774 packfile_size, nours, ndeltify + nreuse,
1775 ndeltify + nreuse, ndeltify + nreuse);
1776 if (err)
1777 goto done;
1779 done:
1780 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1781 err = got_error_from_errno("close");
1782 return err;
1785 static const struct got_error *
1786 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1788 struct got_object_idset *idset = arg;
1790 if (got_object_idset_get_element_data(entry) == NULL)
1791 got_object_idset_remove_element(idset, entry);
1793 return NULL;
1796 static const struct got_error *
1797 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1799 struct got_object_idset *idset = arg;
1800 struct got_pack_meta *m;
1802 m = got_object_idset_get_element_data(entry);
1803 if (m->have_reused_delta)
1804 got_object_idset_remove_element(idset, entry);
1806 return NULL;
1809 static const struct got_error *
1810 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1812 struct got_pack_meta *m = data;
1813 struct got_pack_metavec *v = arg;
1815 return add_meta(m, v);
1818 const struct got_error *
1819 got_pack_create(uint8_t *packsha1, FILE *packfile,
1820 struct got_object_id **theirs, int ntheirs,
1821 struct got_object_id **ours, int nours,
1822 struct got_repository *repo, int loose_obj_only, int allow_empty,
1823 got_pack_progress_cb progress_cb, void *progress_arg,
1824 got_cancel_cb cancel_cb, void *cancel_arg)
1826 const struct got_error *err;
1827 int delta_cache_fd = -1;
1828 FILE *delta_cache = NULL;
1829 struct got_object_idset *idset;
1830 struct got_ratelimit rl;
1831 struct got_pack_metavec deltify, reuse;
1832 int ncolored = 0, nfound = 0, ntrees = 0;
1834 memset(&deltify, 0, sizeof(deltify));
1835 memset(&reuse, 0, sizeof(reuse));
1837 got_ratelimit_init(&rl, 0, 500);
1839 idset = got_object_idset_alloc();
1840 if (idset == NULL)
1841 return got_error_from_errno("got_object_idset_alloc");
1843 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1844 ntheirs, ours, nours, repo, loose_obj_only,
1845 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1846 if (err)
1847 return err;
1849 err = got_object_idset_for_each_element(idset,
1850 remove_unused_object, idset);
1851 if (err)
1852 goto done;
1854 if (progress_cb) {
1855 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1856 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1857 if (err)
1858 goto done;
1861 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1862 err = got_error(GOT_ERR_CANNOT_PACK);
1863 goto done;
1866 delta_cache_fd = got_opentempfd();
1867 if (delta_cache_fd == -1) {
1868 err = got_error_from_errno("got_opentemp");
1869 goto done;
1872 reuse.metasz = 64;
1873 reuse.meta = calloc(reuse.metasz,
1874 sizeof(struct got_pack_meta *));
1875 if (reuse.meta == NULL) {
1876 err = got_error_from_errno("calloc");
1877 goto done;
1880 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1881 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1882 cancel_cb, cancel_arg);
1883 if (err)
1884 goto done;
1885 if (reuse.nmeta > 0) {
1886 err = got_object_idset_for_each_element(idset,
1887 remove_reused_object, idset);
1888 if (err)
1889 goto done;
1892 delta_cache = fdopen(delta_cache_fd, "a+");
1893 if (delta_cache == NULL) {
1894 err = got_error_from_errno("fdopen");
1895 goto done;
1897 delta_cache_fd = -1;
1899 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1900 err = got_error_from_errno("fseeko");
1901 goto done;
1904 deltify.meta = calloc(got_object_idset_num_elements(idset),
1905 sizeof(struct got_pack_meta *));
1906 if (deltify.meta == NULL) {
1907 err = got_error_from_errno("calloc");
1908 goto done;
1910 deltify.metasz = got_object_idset_num_elements(idset);
1912 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1913 if (err)
1914 goto done;
1915 if (deltify.nmeta > 0) {
1916 err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1917 nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1918 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1919 if (err)
1920 goto done;
1921 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1922 err = got_error_from_errno("fseeko");
1923 goto done;
1927 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1928 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1929 nours, repo, progress_cb, progress_arg, &rl,
1930 cancel_cb, cancel_arg);
1931 if (err)
1932 goto done;
1933 done:
1934 free_nmeta(deltify.meta, deltify.nmeta);
1935 free_nmeta(reuse.meta, reuse.nmeta);
1936 got_object_idset_free(idset);
1937 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1938 err = got_error_from_errno("close");
1939 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1940 err = got_error_from_errno("fclose");
1941 return err;