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 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,
855 int *ncolored, int *nfound, int *ntrees,
856 got_pack_progress_cb progress_cb, void *progress_arg,
857 struct got_ratelimit *rl)
859 const struct got_error *err;
860 struct got_pack_meta *m = NULL;
862 if (loose_obj_only) {
863 int is_packed;
864 err = search_packidx(&is_packed, id, repo);
865 if (err)
866 return err;
867 if (is_packed && want_meta)
868 return NULL;
871 if (want_meta) {
872 err = alloc_meta(&m, id, path, obj_type, mtime);
873 if (err)
874 return err;
876 (*nfound)++;
877 err = report_progress(progress_cb, progress_arg, rl,
878 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
879 if (err)
880 return err;
883 return got_object_idset_add(idset, id, m);
886 static const struct got_error *
887 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
888 struct got_object_idset *idset, struct got_object_id *tree_id,
889 const char *dpath, time_t mtime, struct got_repository *repo,
890 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
891 got_pack_progress_cb progress_cb, void *progress_arg,
892 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
894 const struct got_error *err;
895 struct got_tree_object *tree;
896 char *p = NULL;
897 int i;
899 err = got_object_open_as_tree(&tree, repo, tree_id);
900 if (err)
901 return err;
903 (*ntrees)++;
904 err = report_progress(progress_cb, progress_arg, rl,
905 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
906 if (err)
907 return err;
909 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
910 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
911 struct got_object_id *id = got_tree_entry_get_id(e);
912 mode_t mode = got_tree_entry_get_mode(e);
914 if (cancel_cb) {
915 err = (*cancel_cb)(cancel_arg);
916 if (err)
917 break;
920 if (got_object_tree_entry_is_submodule(e) ||
921 got_object_idset_contains(idset, id))
922 continue;
924 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
925 got_tree_entry_get_name(e)) == -1) {
926 err = got_error_from_errno("asprintf");
927 break;
930 if (S_ISDIR(mode)) {
931 struct got_object_qid *qid;
932 err = got_object_qid_alloc(&qid, id);
933 if (err)
934 break;
935 STAILQ_INSERT_TAIL(ids, qid, entry);
936 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
937 err = add_object(want_meta, idset, id, p,
938 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
939 ncolored, nfound, ntrees,
940 progress_cb, progress_arg, rl);
941 if (err)
942 break;
944 free(p);
945 p = NULL;
948 got_object_tree_close(tree);
949 free(p);
950 return err;
953 static const struct got_error *
954 load_tree(int want_meta, struct got_object_idset *idset,
955 struct got_object_id *tree_id, const char *dpath, time_t mtime,
956 struct got_repository *repo, int loose_obj_only,
957 int *ncolored, int *nfound, int *ntrees,
958 got_pack_progress_cb progress_cb, void *progress_arg,
959 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
961 const struct got_error *err = NULL;
962 struct got_object_id_queue tree_ids;
963 struct got_object_qid *qid;
965 if (got_object_idset_contains(idset, tree_id))
966 return NULL;
968 err = got_object_qid_alloc(&qid, tree_id);
969 if (err)
970 return err;
972 STAILQ_INIT(&tree_ids);
973 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
975 while (!STAILQ_EMPTY(&tree_ids)) {
976 if (cancel_cb) {
977 err = (*cancel_cb)(cancel_arg);
978 if (err)
979 break;
982 qid = STAILQ_FIRST(&tree_ids);
983 STAILQ_REMOVE_HEAD(&tree_ids, entry);
985 if (got_object_idset_contains(idset, qid->id)) {
986 got_object_qid_free(qid);
987 continue;
990 err = add_object(want_meta, idset, qid->id, dpath,
991 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo,
992 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
993 if (err) {
994 got_object_qid_free(qid);
995 break;
998 err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
999 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1000 ntrees, progress_cb, progress_arg, rl,
1001 cancel_cb, cancel_arg);
1002 got_object_qid_free(qid);
1003 if (err)
1004 break;
1007 got_object_id_queue_free(&tree_ids);
1008 return err;
1011 static const struct got_error *
1012 load_commit(int want_meta, struct got_object_idset *idset,
1013 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1014 int *ncolored, int *nfound, int *ntrees,
1015 got_pack_progress_cb progress_cb, void *progress_arg,
1016 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1018 const struct got_error *err;
1019 struct got_commit_object *commit;
1021 if (got_object_idset_contains(idset, id))
1022 return NULL;
1024 if (loose_obj_only) {
1025 int is_packed;
1026 err = search_packidx(&is_packed, id, repo);
1027 if (err)
1028 return err;
1029 if (is_packed && want_meta)
1030 return NULL;
1033 err = got_object_open_as_commit(&commit, repo, id);
1034 if (err)
1035 return err;
1037 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1038 got_object_commit_get_committer_time(commit),
1039 loose_obj_only, repo,
1040 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1041 if (err)
1042 goto done;
1044 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1045 "", got_object_commit_get_committer_time(commit),
1046 repo, loose_obj_only, ncolored, nfound, ntrees,
1047 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1048 done:
1049 got_object_commit_close(commit);
1050 return err;
1053 static const struct got_error *
1054 load_tag(int want_meta, struct got_object_idset *idset,
1055 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1056 int *ncolored, int *nfound, int *ntrees,
1057 got_pack_progress_cb progress_cb, void *progress_arg,
1058 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1060 const struct got_error *err;
1061 struct got_tag_object *tag = NULL;
1063 if (got_object_idset_contains(idset, id))
1064 return NULL;
1066 if (loose_obj_only) {
1067 int is_packed;
1068 err = search_packidx(&is_packed, id, repo);
1069 if (err)
1070 return err;
1071 if (is_packed && want_meta)
1072 return NULL;
1075 err = got_object_open_as_tag(&tag, repo, id);
1076 if (err)
1077 return err;
1079 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1080 got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1081 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1082 if (err)
1083 goto done;
1085 switch (got_object_tag_get_object_type(tag)) {
1086 case GOT_OBJ_TYPE_COMMIT:
1087 err = load_commit(want_meta, idset,
1088 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1089 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1090 cancel_cb, cancel_arg);
1091 break;
1092 case GOT_OBJ_TYPE_TREE:
1093 err = load_tree(want_meta, idset,
1094 got_object_tag_get_object_id(tag), "",
1095 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1096 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1097 cancel_cb, cancel_arg);
1098 break;
1099 default:
1100 break;
1103 done:
1104 got_object_tag_close(tag);
1105 return err;
1108 enum findtwixt_color {
1109 COLOR_KEEP = 0,
1110 COLOR_DROP,
1111 COLOR_BLANK,
1113 static const int findtwixt_colors[] = {
1114 COLOR_KEEP,
1115 COLOR_DROP,
1116 COLOR_BLANK
1119 static const struct got_error *
1120 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1121 int color, struct got_repository *repo)
1123 const struct got_error *err;
1124 struct got_object_qid *qid;
1126 err = got_object_qid_alloc(&qid, id);
1127 if (err)
1128 return err;
1130 STAILQ_INSERT_TAIL(ids, qid, entry);
1131 qid->data = (void *)&findtwixt_colors[color];
1132 return NULL;
1135 static const struct got_error *
1136 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1137 struct got_object_id *id, struct got_repository *repo,
1138 got_cancel_cb cancel_cb, void *cancel_arg)
1140 const struct got_error *err = NULL;
1141 struct got_commit_object *commit;
1142 const struct got_object_id_queue *parents;
1143 struct got_object_id_queue ids;
1144 struct got_object_qid *qid;
1146 STAILQ_INIT(&ids);
1148 err = got_object_qid_alloc(&qid, id);
1149 if (err)
1150 return err;
1151 STAILQ_INSERT_HEAD(&ids, qid, entry);
1153 while (!STAILQ_EMPTY(&ids)) {
1154 if (cancel_cb) {
1155 err = (*cancel_cb)(cancel_arg);
1156 if (err)
1157 break;
1160 qid = STAILQ_FIRST(&ids);
1161 STAILQ_REMOVE_HEAD(&ids, entry);
1163 if (got_object_idset_contains(drop, qid->id)) {
1164 got_object_qid_free(qid);
1165 continue;
1168 err = got_object_idset_add(drop, qid->id,
1169 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1170 if (err) {
1171 got_object_qid_free(qid);
1172 break;
1175 if (!got_object_idset_contains(keep, qid->id)) {
1176 got_object_qid_free(qid);
1177 continue;
1180 err = got_object_open_as_commit(&commit, repo, qid->id);
1181 got_object_qid_free(qid);
1182 if (err)
1183 break;
1185 parents = got_object_commit_get_parent_ids(commit);
1186 if (parents) {
1187 err = got_object_id_queue_copy(parents, &ids);
1188 if (err) {
1189 got_object_commit_close(commit);
1190 break;
1193 got_object_commit_close(commit);
1196 got_object_id_queue_free(&ids);
1197 return err;
1200 struct append_id_arg {
1201 struct got_object_id **array;
1202 int idx;
1205 static const struct got_error *
1206 append_id(struct got_object_id *id, void *data, void *arg)
1208 struct append_id_arg *a = arg;
1210 a->array[a->idx] = got_object_id_dup(id);
1211 if (a->array[a->idx] == NULL)
1212 return got_error_from_errno("got_object_id_dup");
1214 a->idx++;
1215 return NULL;
1218 static const struct got_error *
1219 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1220 struct got_object_id **head, int nhead,
1221 struct got_object_id **tail, int ntail,
1222 struct got_repository *repo,
1223 got_pack_progress_cb progress_cb, void *progress_arg,
1224 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1226 const struct got_error *err = NULL;
1227 struct got_object_id_queue ids;
1228 struct got_object_idset *keep, *drop;
1229 struct got_object_qid *qid;
1230 int i, ncolor, nkeep, obj_type;
1232 STAILQ_INIT(&ids);
1233 *res = NULL;
1234 *nres = 0;
1235 *ncolored = 0;
1237 keep = got_object_idset_alloc();
1238 if (keep == NULL)
1239 return got_error_from_errno("got_object_idset_alloc");
1241 drop = got_object_idset_alloc();
1242 if (drop == NULL) {
1243 err = got_error_from_errno("got_object_idset_alloc");
1244 goto done;
1247 for (i = 0; i < nhead; i++) {
1248 struct got_object_id *id = head[i];
1249 if (id == NULL)
1250 continue;
1251 err = got_object_get_type(&obj_type, repo, id);
1252 if (err)
1253 return err;
1254 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1255 continue;
1256 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1257 if (err)
1258 goto done;
1260 for (i = 0; i < ntail; i++) {
1261 struct got_object_id *id = tail[i];
1262 if (id == NULL)
1263 continue;
1264 err = got_object_get_type(&obj_type, repo, id);
1265 if (err)
1266 return err;
1267 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1268 continue;
1269 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1270 if (err)
1271 goto done;
1274 while (!STAILQ_EMPTY(&ids)) {
1275 int qcolor;
1276 qid = STAILQ_FIRST(&ids);
1277 qcolor = *((int *)qid->data);
1279 if (got_object_idset_contains(drop, qid->id))
1280 ncolor = COLOR_DROP;
1281 else if (got_object_idset_contains(keep, qid->id))
1282 ncolor = COLOR_KEEP;
1283 else
1284 ncolor = COLOR_BLANK;
1286 (*ncolored)++;
1287 err = report_progress(progress_cb, progress_arg, rl,
1288 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1289 if (err)
1290 goto done;
1292 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1293 qcolor == COLOR_KEEP)) {
1294 STAILQ_REMOVE_HEAD(&ids, entry);
1295 got_object_qid_free(qid);
1296 continue;
1299 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1300 err = drop_commit(keep, drop, qid->id, repo,
1301 cancel_cb, cancel_arg);
1302 if (err)
1303 goto done;
1304 } else if (ncolor == COLOR_BLANK) {
1305 struct got_commit_object *commit;
1306 struct got_object_id *id;
1307 const struct got_object_id_queue *parents;
1308 struct got_object_qid *pid;
1310 id = got_object_id_dup(qid->id);
1311 if (id == NULL) {
1312 err = got_error_from_errno("got_object_id_dup");
1313 goto done;
1315 if (qcolor == COLOR_KEEP)
1316 err = got_object_idset_add(keep, id,
1317 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1318 else
1319 err = got_object_idset_add(drop, id,
1320 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1321 if (err) {
1322 free(id);
1323 goto done;
1326 err = got_object_open_as_commit(&commit, repo, id);
1327 if (err) {
1328 free(id);
1329 goto done;
1331 parents = got_object_commit_get_parent_ids(commit);
1332 if (parents) {
1333 STAILQ_FOREACH(pid, parents, entry) {
1334 err = queue_commit_id(&ids, pid->id,
1335 qcolor, repo);
1336 if (err) {
1337 free(id);
1338 goto done;
1342 got_object_commit_close(commit);
1343 commit = NULL;
1344 } else {
1345 /* should not happen */
1346 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1347 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1348 goto done;
1351 STAILQ_REMOVE_HEAD(&ids, entry);
1352 got_object_qid_free(qid);
1355 nkeep = got_object_idset_num_elements(keep);
1356 if (nkeep > 0) {
1357 struct append_id_arg arg;
1358 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1359 if (arg.array == NULL) {
1360 err = got_error_from_errno("calloc");
1361 goto done;
1363 arg.idx = 0;
1364 err = got_object_idset_for_each(keep, append_id, &arg);
1365 if (err) {
1366 free(arg.array);
1367 goto done;
1369 *res = arg.array;
1370 *nres = nkeep;
1372 done:
1373 got_object_idset_free(keep);
1374 got_object_idset_free(drop);
1375 got_object_id_queue_free(&ids);
1376 return err;
1379 static const struct got_error *
1380 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1381 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1382 struct got_object_id **ours, int nours, struct got_repository *repo,
1383 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1384 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1386 const struct got_error *err = NULL;
1387 struct got_object_id **ids = NULL;
1388 int i, nobj = 0, obj_type;
1390 *ncolored = 0;
1391 *nfound = 0;
1392 *ntrees = 0;
1394 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1395 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1396 if (err || nobj == 0)
1397 goto done;
1399 for (i = 0; i < ntheirs; i++) {
1400 struct got_object_id *id = theirs[i];
1401 if (id == NULL)
1402 continue;
1403 err = got_object_get_type(&obj_type, repo, id);
1404 if (err)
1405 return err;
1406 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1407 continue;
1408 err = load_commit(0, idset, id, repo, loose_obj_only,
1409 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1410 cancel_cb, cancel_arg);
1411 if (err)
1412 goto done;
1415 for (i = 0; i < ntheirs; i++) {
1416 struct got_object_id *id = theirs[i];
1417 struct got_pack_meta *m;
1418 if (id == NULL)
1419 continue;
1420 m = got_object_idset_get(idset, id);
1421 if (m == NULL) {
1422 err = got_object_get_type(&obj_type, repo, id);
1423 if (err)
1424 goto done;
1425 } else
1426 obj_type = m->obj_type;
1427 if (obj_type != GOT_OBJ_TYPE_TAG)
1428 continue;
1429 err = load_tag(0, idset, id, repo, loose_obj_only,
1430 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1431 cancel_cb, cancel_arg);
1432 if (err)
1433 goto done;
1436 for (i = 0; i < nobj; i++) {
1437 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1438 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1439 cancel_cb, cancel_arg);
1440 if (err)
1441 goto done;
1444 for (i = 0; i < nours; i++) {
1445 struct got_object_id *id = ours[i];
1446 struct got_pack_meta *m;
1447 if (id == NULL)
1448 continue;
1449 m = got_object_idset_get(idset, id);
1450 if (m == NULL) {
1451 err = got_object_get_type(&obj_type, repo, id);
1452 if (err)
1453 goto done;
1454 } else
1455 obj_type = m->obj_type;
1456 if (obj_type != GOT_OBJ_TYPE_TAG)
1457 continue;
1458 err = load_tag(1, idset, id, repo, loose_obj_only,
1459 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1460 cancel_cb, cancel_arg);
1461 if (err)
1462 goto done;
1464 done:
1465 for (i = 0; i < nobj; i++) {
1466 free(ids[i]);
1468 free(ids);
1469 return err;
1472 const struct got_error *
1473 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1475 size_t n;
1477 SHA1Update(ctx, buf, len);
1478 n = fwrite(buf, 1, len, f);
1479 if (n != len)
1480 return got_ferror(f, GOT_ERR_IO);
1481 return NULL;
1484 static void
1485 putbe32(char *b, uint32_t n)
1487 b[0] = n >> 24;
1488 b[1] = n >> 16;
1489 b[2] = n >> 8;
1490 b[3] = n >> 0;
1493 static int
1494 write_order_cmp(const void *pa, const void *pb)
1496 struct got_pack_meta *a, *b, *ahd, *bhd;
1498 a = *(struct got_pack_meta **)pa;
1499 b = *(struct got_pack_meta **)pb;
1500 ahd = (a->head == NULL) ? a : a->head;
1501 bhd = (b->head == NULL) ? b : b->head;
1502 if (ahd->mtime != bhd->mtime)
1503 return bhd->mtime - ahd->mtime;
1504 if (ahd != bhd)
1505 return (uintptr_t)bhd - (uintptr_t)ahd;
1506 if (a->nchain != b->nchain)
1507 return a->nchain - b->nchain;
1508 return a->mtime - b->mtime;
1511 static int
1512 reuse_write_order_cmp(const void *pa, const void *pb)
1514 struct got_pack_meta *a, *b;
1516 a = *(struct got_pack_meta **)pa;
1517 b = *(struct got_pack_meta **)pb;
1519 if (a->reused_delta_offset < b->reused_delta_offset)
1520 return -1;
1521 if (a->reused_delta_offset > b->reused_delta_offset)
1522 return 1;
1523 return 0;
1526 static const struct got_error *
1527 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1529 size_t i;
1531 *hdrlen = 0;
1533 hdr[0] = obj_type << 4;
1534 hdr[0] |= len & 0xf;
1535 len >>= 4;
1536 for (i = 1; len != 0; i++){
1537 if (i >= bufsize)
1538 return got_error(GOT_ERR_NO_SPACE);
1539 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1540 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1541 len >>= GOT_DELTA_SIZE_SHIFT;
1544 *hdrlen = i;
1545 return NULL;
1548 static int
1549 packoff(char *hdr, off_t off)
1551 int i, j;
1552 char rbuf[8];
1554 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1555 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1556 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1557 GOT_DELTA_SIZE_MORE;
1560 j = 0;
1561 while (i > 0)
1562 hdr[j++] = rbuf[--i];
1563 return j;
1566 static const struct got_error *
1567 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1568 struct got_pack_meta *m)
1570 const struct got_error *err;
1571 char buf[32];
1572 int nh;
1574 if (m->prev->off != 0) {
1575 err = packhdr(&nh, buf, sizeof(buf),
1576 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1577 if (err)
1578 return err;
1579 nh += packoff(buf + nh, m->off - m->prev->off);
1580 err = hwrite(packfile, buf, nh, ctx);
1581 if (err)
1582 return err;
1583 *packfile_size += nh;
1584 } else {
1585 err = packhdr(&nh, buf, sizeof(buf),
1586 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1587 if (err)
1588 return err;
1589 err = hwrite(packfile, buf, nh, ctx);
1590 if (err)
1591 return err;
1592 *packfile_size += nh;
1593 err = hwrite(packfile, m->prev->id.sha1,
1594 sizeof(m->prev->id.sha1), ctx);
1595 if (err)
1596 return err;
1597 *packfile_size += sizeof(m->prev->id.sha1);
1600 return NULL;
1603 static const struct got_error *
1604 write_packed_object(off_t *packfile_size, FILE *packfile,
1605 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1606 SHA1_CTX *ctx, struct got_repository *repo)
1608 const struct got_error *err = NULL;
1609 struct got_deflate_checksum csum;
1610 char buf[32];
1611 int nh;
1612 struct got_raw_object *raw = NULL;
1613 off_t outlen;
1615 csum.output_sha1 = ctx;
1616 csum.output_crc = NULL;
1618 m->off = ftello(packfile);
1619 if (m->delta_len == 0) {
1620 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1621 if (err)
1622 goto done;
1623 err = packhdr(&nh, buf, sizeof(buf),
1624 m->obj_type, raw->size);
1625 if (err)
1626 goto done;
1627 err = hwrite(packfile, buf, nh, ctx);
1628 if (err)
1629 goto done;
1630 *packfile_size += nh;
1631 if (raw->f == NULL) {
1632 err = got_deflate_to_file_mmap(&outlen,
1633 raw->data + raw->hdrlen, 0, raw->size,
1634 packfile, &csum);
1635 if (err)
1636 goto done;
1637 } else {
1638 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1639 == -1) {
1640 err = got_error_from_errno("fseeko");
1641 goto done;
1643 err = got_deflate_to_file(&outlen, raw->f,
1644 raw->size, packfile, &csum);
1645 if (err)
1646 goto done;
1648 *packfile_size += outlen;
1649 got_object_raw_close(raw);
1650 raw = NULL;
1651 } else if (m->delta_buf) {
1652 err = deltahdr(packfile_size, ctx, packfile, m);
1653 if (err)
1654 goto done;
1655 err = got_deflate_to_file_mmap(&outlen,
1656 m->delta_buf, 0, m->delta_len, packfile, &csum);
1657 if (err)
1658 goto done;
1659 *packfile_size += outlen;
1660 free(m->delta_buf);
1661 m->delta_buf = NULL;
1662 } else {
1663 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1664 == -1) {
1665 err = got_error_from_errno("fseeko");
1666 goto done;
1668 err = deltahdr(packfile_size, ctx, packfile, m);
1669 if (err)
1670 goto done;
1671 err = got_deflate_to_file(&outlen, delta_cache,
1672 m->delta_len, packfile, &csum);
1673 if (err)
1674 goto done;
1675 *packfile_size += outlen;
1677 done:
1678 if (raw)
1679 got_object_raw_close(raw);
1680 return err;
1683 static const struct got_error *
1684 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1685 struct got_pack_meta **deltify, int ndeltify,
1686 struct got_pack_meta **reuse, int nreuse,
1687 int ncolored, int nfound, int ntrees, int nours,
1688 struct got_repository *repo,
1689 got_pack_progress_cb progress_cb, void *progress_arg,
1690 struct got_ratelimit *rl,
1691 got_cancel_cb cancel_cb, void *cancel_arg)
1693 const struct got_error *err = NULL;
1694 int i;
1695 SHA1_CTX ctx;
1696 struct got_pack_meta *m;
1697 char buf[32];
1698 size_t n;
1699 off_t packfile_size = 0;
1700 int outfd = -1;
1702 SHA1Init(&ctx);
1704 err = hwrite(packfile, "PACK", 4, &ctx);
1705 if (err)
1706 return err;
1707 putbe32(buf, GOT_PACKFILE_VERSION);
1708 err = hwrite(packfile, buf, 4, &ctx);
1709 if (err)
1710 goto done;
1711 putbe32(buf, ndeltify + nreuse);
1712 err = hwrite(packfile, buf, 4, &ctx);
1713 if (err)
1714 goto done;
1716 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1717 write_order_cmp);
1718 for (i = 0; i < ndeltify; i++) {
1719 err = report_progress(progress_cb, progress_arg, rl,
1720 ncolored, nfound, ntrees, packfile_size, nours,
1721 ndeltify + nreuse, ndeltify + nreuse, i);
1722 if (err)
1723 goto done;
1724 m = deltify[i];
1725 err = write_packed_object(&packfile_size, packfile,
1726 delta_cache, m, &outfd, &ctx, repo);
1727 if (err)
1728 goto done;
1731 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1732 reuse_write_order_cmp);
1733 for (i = 0; i < nreuse; i++) {
1734 err = report_progress(progress_cb, progress_arg, rl,
1735 ncolored, nfound, ntrees, packfile_size, nours,
1736 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1737 if (err)
1738 goto done;
1739 m = reuse[i];
1740 err = write_packed_object(&packfile_size, packfile,
1741 delta_cache, m, &outfd, &ctx, repo);
1742 if (err)
1743 goto done;
1746 SHA1Final(pack_sha1, &ctx);
1747 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1748 if (n != SHA1_DIGEST_LENGTH)
1749 err = got_ferror(packfile, GOT_ERR_IO);
1750 packfile_size += SHA1_DIGEST_LENGTH;
1751 packfile_size += sizeof(struct got_packfile_hdr);
1752 if (progress_cb) {
1753 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1754 packfile_size, nours, ndeltify + nreuse,
1755 ndeltify + nreuse, ndeltify + nreuse);
1756 if (err)
1757 goto done;
1759 done:
1760 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1761 err = got_error_from_errno("close");
1762 return err;
1765 static const struct got_error *
1766 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1768 struct got_object_idset *idset = arg;
1770 if (got_object_idset_get_element_data(entry) == NULL)
1771 got_object_idset_remove_element(idset, entry);
1773 return NULL;
1776 static const struct got_error *
1777 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1779 struct got_object_idset *idset = arg;
1780 struct got_pack_meta *m;
1782 m = got_object_idset_get_element_data(entry);
1783 if (m->have_reused_delta)
1784 got_object_idset_remove_element(idset, entry);
1786 return NULL;
1789 static const struct got_error *
1790 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1792 struct got_pack_meta *m = data;
1793 struct got_pack_metavec *v = arg;
1795 return add_meta(m, v);
1798 const struct got_error *
1799 got_pack_create(uint8_t *packsha1, FILE *packfile,
1800 struct got_object_id **theirs, int ntheirs,
1801 struct got_object_id **ours, int nours,
1802 struct got_repository *repo, int loose_obj_only, int allow_empty,
1803 got_pack_progress_cb progress_cb, void *progress_arg,
1804 got_cancel_cb cancel_cb, void *cancel_arg)
1806 const struct got_error *err;
1807 int delta_cache_fd = -1;
1808 FILE *delta_cache = NULL;
1809 struct got_object_idset *idset;
1810 struct got_ratelimit rl;
1811 struct got_pack_metavec deltify, reuse;
1812 int ncolored = 0, nfound = 0, ntrees = 0;
1814 memset(&deltify, 0, sizeof(deltify));
1815 memset(&reuse, 0, sizeof(reuse));
1817 got_ratelimit_init(&rl, 0, 500);
1819 idset = got_object_idset_alloc();
1820 if (idset == NULL)
1821 return got_error_from_errno("got_object_idset_alloc");
1823 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1824 ntheirs, ours, nours, repo, loose_obj_only,
1825 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1826 if (err)
1827 return err;
1829 err = got_object_idset_for_each_element(idset,
1830 remove_unused_object, idset);
1831 if (err)
1832 goto done;
1834 if (progress_cb) {
1835 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1836 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1837 if (err)
1838 goto done;
1841 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1842 err = got_error(GOT_ERR_CANNOT_PACK);
1843 goto done;
1846 delta_cache_fd = got_opentempfd();
1847 if (delta_cache_fd == -1) {
1848 err = got_error_from_errno("got_opentemp");
1849 goto done;
1852 reuse.metasz = 64;
1853 reuse.meta = calloc(reuse.metasz,
1854 sizeof(struct got_pack_meta *));
1855 if (reuse.meta == NULL) {
1856 err = got_error_from_errno("calloc");
1857 goto done;
1860 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1861 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1862 cancel_cb, cancel_arg);
1863 if (err)
1864 goto done;
1865 if (reuse.nmeta > 0) {
1866 err = got_object_idset_for_each_element(idset,
1867 remove_reused_object, idset);
1868 if (err)
1869 goto done;
1872 delta_cache = fdopen(delta_cache_fd, "a+");
1873 if (delta_cache == NULL) {
1874 err = got_error_from_errno("fdopen");
1875 goto done;
1877 delta_cache_fd = -1;
1879 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1880 err = got_error_from_errno("fseeko");
1881 goto done;
1884 deltify.meta = calloc(got_object_idset_num_elements(idset),
1885 sizeof(struct got_pack_meta *));
1886 if (deltify.meta == NULL) {
1887 err = got_error_from_errno("calloc");
1888 goto done;
1890 deltify.metasz = got_object_idset_num_elements(idset);
1892 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1893 if (err)
1894 goto done;
1895 if (deltify.nmeta > 0) {
1896 err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1897 nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1898 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1899 if (err)
1900 goto done;
1901 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1902 err = got_error_from_errno("fseeko");
1903 goto done;
1907 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1908 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1909 nours, repo, progress_cb, progress_arg, &rl,
1910 cancel_cb, cancel_arg);
1911 if (err)
1912 goto done;
1913 done:
1914 free_nmeta(deltify.meta, deltify.nmeta);
1915 free_nmeta(reuse.meta, reuse.nmeta);
1916 got_object_idset_free(idset);
1917 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1918 err = got_error_from_errno("close");
1919 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1920 err = got_error_from_errno("fclose");
1921 return err;