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 <limits.h>
29 #include <zlib.h>
31 #if defined(__FreeBSD__)
32 #include <unistd.h>
33 #endif
35 #include "got_error.h"
36 #include "got_cancel.h"
37 #include "got_object.h"
38 #include "got_path.h"
39 #include "got_reference.h"
40 #include "got_repository_admin.h"
41 #include "got_opentemp.h"
43 #include "got_lib_deltify.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_deflate.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_ratelimit.h"
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 #ifndef MAX
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 #endif
62 struct got_pack_meta {
63 struct got_object_id id;
64 char *path;
65 int obj_type;
66 off_t size;
67 time_t mtime;
69 /* The best delta we picked */
70 struct got_pack_meta *head;
71 struct got_pack_meta *prev;
72 unsigned char *delta_buf; /* if not encoded in delta cache file */
73 off_t delta_offset; /* offset in delta cache file */
74 off_t delta_len; /* encoded delta length */
75 int nchain;
77 int have_reused_delta;
78 off_t reused_delta_offset; /* offset of delta in reused pack file */
79 struct got_object_id *base_obj_id;
81 /* Only used for delta window */
82 struct got_delta_table *dtab;
84 /* Only used for writing offset deltas */
85 off_t off;
86 };
88 struct got_pack_metavec {
89 struct got_pack_meta **meta;
90 int nmeta;
91 int metasz;
92 };
94 static const struct got_error *
95 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
96 const char *path, int obj_type, time_t mtime)
97 {
98 const struct got_error *err = NULL;
99 struct got_pack_meta *m;
101 *new = NULL;
103 m = calloc(1, sizeof(*m));
104 if (m == NULL)
105 return got_error_from_errno("calloc");
107 memcpy(&m->id, id, sizeof(m->id));
109 m->path = strdup(path);
110 if (m->path == NULL) {
111 err = got_error_from_errno("strdup");
112 free(m);
113 return err;
116 m->obj_type = obj_type;
117 m->mtime = mtime;
118 *new = m;
119 return NULL;
122 static void
123 clear_meta(struct got_pack_meta *meta)
125 if (meta == NULL)
126 return;
127 free(meta->path);
128 meta->path = NULL;
129 free(meta->delta_buf);
130 meta->delta_buf = NULL;
131 free(meta->base_obj_id);
132 meta->base_obj_id = NULL;
135 static void
136 free_nmeta(struct got_pack_meta **meta, int nmeta)
138 int i;
140 for (i = 0; i < nmeta; i++)
141 clear_meta(meta[i]);
142 free(meta);
145 static int
146 delta_order_cmp(const void *pa, const void *pb)
148 struct got_pack_meta *a, *b;
149 int cmp;
151 a = *(struct got_pack_meta **)pa;
152 b = *(struct got_pack_meta **)pb;
154 if (a->obj_type != b->obj_type)
155 return a->obj_type - b->obj_type;
156 cmp = strcmp(a->path, b->path);
157 if (cmp != 0)
158 return cmp;
159 if (a->mtime != b->mtime)
160 return a->mtime - b->mtime;
161 return got_object_id_cmp(&a->id, &b->id);
164 static off_t
165 delta_size(struct got_delta_instruction *deltas, int ndeltas)
167 int i;
168 off_t size = 32;
169 for (i = 0; i < ndeltas; i++) {
170 if (deltas[i].copy)
171 size += GOT_DELTA_SIZE_SHIFT;
172 else
173 size += deltas[i].len + 1;
175 return size;
178 static const struct got_error *
179 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
181 char *n;
183 if (*len + nseg >= *sz) {
184 while (*len + nseg >= *sz)
185 *sz += *sz / 2;
186 n = realloc(*p, *sz);
187 if (n == NULL)
188 return got_error_from_errno("realloc");
189 *p = n;
191 memcpy(*p + *len, seg, nseg);
192 *len += nseg;
193 return NULL;
196 static const struct got_error *
197 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
198 struct got_delta_instruction *deltas, int ndeltas,
199 off_t delta_size, off_t base_size)
201 const struct got_error *err;
202 unsigned char buf[16], *bp;
203 int i, j;
204 size_t len = 0;
205 off_t n;
206 struct got_delta_instruction *d;
208 m->delta_buf = malloc(delta_size);
209 if (m->delta_buf == NULL)
210 return got_error_from_errno("calloc");
212 /* base object size */
213 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
214 n = base_size >> GOT_DELTA_SIZE_SHIFT;
215 for (i = 1; n > 0; i++) {
216 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
217 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
218 n >>= GOT_DELTA_SIZE_SHIFT;
220 err = append(&m->delta_buf, &len, &delta_size, buf, i);
221 if (err)
222 return err;
224 /* target object size */
225 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
226 n = o->size >> GOT_DELTA_SIZE_SHIFT;
227 for (i = 1; n > 0; i++) {
228 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
229 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
230 n >>= GOT_DELTA_SIZE_SHIFT;
232 err = append(&m->delta_buf, &len, &delta_size, buf, i);
233 if (err)
234 return err;
236 for (j = 0; j < ndeltas; j++) {
237 d = &deltas[j];
238 if (d->copy) {
239 n = d->offset;
240 bp = &buf[1];
241 buf[0] = GOT_DELTA_BASE_COPY;
242 for (i = 0; i < 4; i++) {
243 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
244 buf[0] |= 1 << i;
245 *bp++ = n & 0xff;
246 n >>= 8;
247 if (n == 0)
248 break;
251 n = d->len;
252 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
253 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
254 for (i = 0; i < 3 && n > 0; i++) {
255 buf[0] |= 1 << (i + 4);
256 *bp++ = n & 0xff;
257 n >>= 8;
260 err = append(&m->delta_buf, &len, &delta_size,
261 buf, bp - buf);
262 if (err)
263 return err;
264 } else if (o->f == NULL) {
265 n = 0;
266 while (n != d->len) {
267 buf[0] = (d->len - n < 127) ? d->len - n : 127;
268 err = append(&m->delta_buf, &len, &delta_size,
269 buf, 1);
270 if (err)
271 return err;
272 err = append(&m->delta_buf, &len, &delta_size,
273 o->data + o->hdrlen + d->offset + n,
274 buf[0]);
275 if (err)
276 return err;
277 n += buf[0];
279 } else {
280 char content[128];
281 size_t r;
282 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
283 return got_error_from_errno("fseeko");
284 n = 0;
285 while (n != d->len) {
286 buf[0] = (d->len - n < 127) ? d->len - n : 127;
287 err = append(&m->delta_buf, &len, &delta_size,
288 buf, 1);
289 if (err)
290 return err;
291 r = fread(content, 1, buf[0], o->f);
292 if (r != buf[0])
293 return got_ferror(o->f, GOT_ERR_IO);
294 err = append(&m->delta_buf, &len, &delta_size,
295 content, buf[0]);
296 if (err)
297 return err;
298 n += buf[0];
303 m->delta_len = len;
304 return NULL;
307 static const struct got_error *
308 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
309 struct got_delta_instruction *deltas, int ndeltas,
310 off_t base_size, FILE *f)
312 unsigned char buf[16], *bp;
313 int i, j;
314 off_t n;
315 size_t w;
316 struct got_delta_instruction *d;
318 /* base object size */
319 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
320 n = base_size >> GOT_DELTA_SIZE_SHIFT;
321 for (i = 1; n > 0; i++) {
322 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
323 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
324 n >>= GOT_DELTA_SIZE_SHIFT;
326 w = fwrite(buf, 1, i, f);
327 if (w != i)
328 return got_ferror(f, GOT_ERR_IO);
330 /* target object size */
331 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
332 n = o->size >> GOT_DELTA_SIZE_SHIFT;
333 for (i = 1; n > 0; i++) {
334 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
335 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
336 n >>= GOT_DELTA_SIZE_SHIFT;
338 w = fwrite(buf, 1, i, f);
339 if (w != i)
340 return got_ferror(f, GOT_ERR_IO);
342 for (j = 0; j < ndeltas; j++) {
343 d = &deltas[j];
344 if (d->copy) {
345 n = d->offset;
346 bp = &buf[1];
347 buf[0] = GOT_DELTA_BASE_COPY;
348 for (i = 0; i < 4; i++) {
349 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
350 buf[0] |= 1 << i;
351 *bp++ = n & 0xff;
352 n >>= 8;
353 if (n == 0)
354 break;
357 n = d->len;
358 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
359 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
360 for (i = 0; i < 3 && n > 0; i++) {
361 buf[0] |= 1 << (i + 4);
362 *bp++ = n & 0xff;
363 n >>= 8;
366 w = fwrite(buf, 1, bp - buf, f);
367 if (w != bp - buf)
368 return got_ferror(f, GOT_ERR_IO);
369 } else if (o->f == NULL) {
370 n = 0;
371 while (n != d->len) {
372 buf[0] = (d->len - n < 127) ? d->len - n : 127;
373 w = fwrite(buf, 1, 1, f);
374 if (w != 1)
375 return got_ferror(f, GOT_ERR_IO);
376 w = fwrite(o->data + o->hdrlen + d->offset + n,
377 1, buf[0], f);
378 if (w != buf[0])
379 return got_ferror(f, GOT_ERR_IO);
380 n += buf[0];
382 } else {
383 char content[128];
384 size_t r;
385 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
386 return got_error_from_errno("fseeko");
387 n = 0;
388 while (n != d->len) {
389 buf[0] = (d->len - n < 127) ? d->len - n : 127;
390 w = fwrite(buf, 1, 1, f);
391 if (w != 1)
392 return got_ferror(f, GOT_ERR_IO);
393 r = fread(content, 1, buf[0], o->f);
394 if (r != buf[0])
395 return got_ferror(o->f, GOT_ERR_IO);
396 w = fwrite(content, 1, buf[0], f);
397 if (w != buf[0])
398 return got_ferror(f, GOT_ERR_IO);
399 n += buf[0];
404 m->delta_len = ftello(f) - m->delta_offset;
405 return NULL;
408 static const struct got_error *
409 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
410 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
411 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
412 int nobj_written)
414 const struct got_error *err;
415 int elapsed;
417 if (progress_cb == NULL)
418 return NULL;
420 err = got_ratelimit_check(&elapsed, rl);
421 if (err || !elapsed)
422 return err;
424 return progress_cb(progress_arg, ncolored, nfound, ntrees,
425 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
428 static const struct got_error *
429 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
431 if (v->nmeta == v->metasz){
432 size_t newsize = 2 * v->metasz;
433 struct got_pack_meta **new;
434 new = reallocarray(v->meta, newsize, sizeof(*new));
435 if (new == NULL)
436 return got_error_from_errno("reallocarray");
437 v->meta = new;
438 v->metasz = newsize;
441 v->meta[v->nmeta++] = m;
442 return NULL;
445 static const struct got_error *
446 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
447 struct got_object_idset *idset, struct got_pack *pack,
448 struct got_packidx *packidx, int delta_cache_fd,
449 struct got_repository *repo)
451 const struct got_error *err = NULL;
452 struct got_pack_meta *base = NULL;
453 struct got_object_id *base_obj_id = NULL;
454 off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
455 uint64_t base_size, result_size;
457 if (m->have_reused_delta)
458 return NULL;
460 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
461 &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
462 packidx, idx, &m->id, repo);
463 if (err)
464 return err;
466 if (delta_offset + delta_len < delta_offset)
467 return got_error(GOT_ERR_BAD_PACKFILE);
469 base = got_object_idset_get(idset, base_obj_id);
470 if (base == NULL)
471 goto done;
473 m->delta_len = delta_len;
474 m->delta_offset = delta_cache_offset;
475 m->prev = base;
476 m->size = result_size;
477 m->have_reused_delta = 1;
478 m->reused_delta_offset = delta_offset;
479 m->base_obj_id = base_obj_id;
480 base_obj_id = NULL;
481 err = add_meta(m, v);
482 done:
483 free(base_obj_id);
484 return err;
487 static const struct got_error *
488 find_pack_for_reuse(struct got_packidx **best_packidx,
489 struct got_repository *repo)
491 const struct got_error *err = NULL;
492 struct got_pathlist_entry *pe;
493 const char *best_packidx_path = NULL;
494 int nobj_max = 0;
496 *best_packidx = NULL;
498 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
499 const char *path_packidx = pe->path;
500 struct got_packidx *packidx;
501 int nobj;
503 err = got_repo_get_packidx(&packidx, path_packidx, repo);
504 if (err)
505 break;
507 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
508 if (nobj > nobj_max) {
509 best_packidx_path = path_packidx;
510 nobj_max = nobj;
514 if (best_packidx_path) {
515 err = got_repo_get_packidx(best_packidx, best_packidx_path,
516 repo);
519 return err;
522 struct search_deltas_arg {
523 struct got_packidx *packidx;
524 struct got_pack *pack;
525 struct got_object_idset *idset;
526 struct got_pack_metavec *v;
527 int delta_cache_fd;
528 struct got_repository *repo;
529 got_pack_progress_cb progress_cb;
530 void *progress_arg;
531 struct got_ratelimit *rl;
532 got_cancel_cb cancel_cb;
533 void *cancel_arg;
534 int ncolored;
535 int nfound;
536 int ntrees;
537 int ncommits;
538 };
540 static const struct got_error *
541 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
543 const struct got_error *err;
544 struct got_pack_meta *m = data;
545 struct search_deltas_arg *a = arg;
546 int obj_idx;
547 struct got_object *obj = NULL;
549 if (a->cancel_cb) {
550 err = (*a->cancel_cb)(a->cancel_arg);
551 if (err)
552 return err;
555 if (!got_repo_check_packidx_bloom_filter(a->repo,
556 a->packidx->path_packidx, id))
557 return NULL;
559 obj_idx = got_packidx_get_object_idx(a->packidx, id);
560 if (obj_idx == -1)
561 return NULL;
563 /* TODO:
564 * Opening and closing an object just to check its flags
565 * is a bit expensive. We could have an imsg which requests
566 * plain type/size information for an object without doing
567 * work such as traversing the object's entire delta chain
568 * to find the base object type, and other such info which
569 * we don't really need here.
570 */
571 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
572 a->packidx, obj_idx, a->repo);
573 if (err)
574 return err;
576 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
577 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
578 a->delta_cache_fd, a->repo);
579 if (err)
580 goto done;
581 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
582 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
583 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
585 done:
586 got_object_close(obj);
587 return err;
590 static const struct got_error *
591 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
592 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
593 struct got_repository *repo,
594 got_pack_progress_cb progress_cb, void *progress_arg,
595 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
597 const struct got_error *err = NULL;
598 char *path_packfile = NULL;
599 struct got_packidx *packidx;
600 struct got_pack *pack;
601 struct search_deltas_arg sda;
603 err = find_pack_for_reuse(&packidx, repo);
604 if (err)
605 return err;
607 if (packidx == NULL)
608 return NULL;
610 err = got_packidx_get_packfile_path(&path_packfile,
611 packidx->path_packidx);
612 if (err)
613 return err;
615 pack = got_repo_get_cached_pack(repo, path_packfile);
616 if (pack == NULL) {
617 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
618 if (err)
619 goto done;
622 sda.packidx = packidx;
623 sda.pack = pack;
624 sda.idset = idset;
625 sda.v = v;
626 sda.delta_cache_fd = delta_cache_fd;
627 sda.repo = repo;
628 sda.progress_cb = progress_cb;
629 sda.progress_arg = progress_arg;
630 sda.rl = rl;
631 sda.cancel_cb = cancel_cb;
632 sda.cancel_arg = cancel_arg;
633 sda.ncolored = ncolored;
634 sda.nfound = nfound;
635 sda.ntrees = ntrees;
636 sda.ncommits = ncommits;
637 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
638 done:
639 free(path_packfile);
640 return err;
643 static const struct got_error *
644 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
645 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
646 struct got_repository *repo,
647 got_pack_progress_cb progress_cb, void *progress_arg,
648 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
650 const struct got_error *err = NULL;
651 struct got_pack_meta *m = NULL, *base = NULL;
652 struct got_raw_object *raw = NULL, *base_raw = NULL;
653 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
654 int i, j, ndeltas, best_ndeltas;
655 off_t size, best_size;
656 const int max_base_candidates = 3;
657 size_t delta_memsize = 0;
658 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
659 int outfd = -1;
661 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
662 for (i = 0; i < nmeta; i++) {
663 if (cancel_cb) {
664 err = (*cancel_cb)(cancel_arg);
665 if (err)
666 break;
668 err = report_progress(progress_cb, progress_arg, rl,
669 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
670 nreused + i, 0);
671 if (err)
672 goto done;
673 m = meta[i];
675 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
676 m->obj_type == GOT_OBJ_TYPE_TAG)
677 continue;
679 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
680 if (err)
681 goto done;
682 m->size = raw->size;
684 if (raw->f == NULL) {
685 err = got_deltify_init_mem(&m->dtab, raw->data,
686 raw->hdrlen, raw->size + raw->hdrlen);
687 } else {
688 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
689 raw->size + raw->hdrlen);
691 if (err)
692 goto done;
694 if (i > max_base_candidates) {
695 struct got_pack_meta *n = NULL;
696 n = meta[i - (max_base_candidates + 1)];
697 got_deltify_free(n->dtab);
698 n->dtab = NULL;
701 best_size = raw->size;
702 best_ndeltas = 0;
703 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
704 if (cancel_cb) {
705 err = (*cancel_cb)(cancel_arg);
706 if (err)
707 goto done;
709 base = meta[j];
710 /* long chains make unpacking slow, avoid such bases */
711 if (base->nchain >= 128 ||
712 base->obj_type != m->obj_type)
713 continue;
715 err = got_object_raw_open(&base_raw, &outfd, repo,
716 &base->id);
717 if (err)
718 goto done;
720 if (raw->f == NULL && base_raw->f == NULL) {
721 err = got_deltify_mem_mem(&deltas, &ndeltas,
722 raw->data, raw->hdrlen,
723 raw->size + raw->hdrlen,
724 base->dtab, base_raw->data,
725 base_raw->hdrlen,
726 base_raw->size + base_raw->hdrlen);
727 } else if (raw->f == NULL) {
728 err = got_deltify_mem_file(&deltas, &ndeltas,
729 raw->data, raw->hdrlen,
730 raw->size + raw->hdrlen,
731 base->dtab, base_raw->f,
732 base_raw->hdrlen,
733 base_raw->size + base_raw->hdrlen);
734 } else if (base_raw->f == NULL) {
735 err = got_deltify_file_mem(&deltas, &ndeltas,
736 raw->f, raw->hdrlen,
737 raw->size + raw->hdrlen,
738 base->dtab, base_raw->data,
739 base_raw->hdrlen,
740 base_raw->size + base_raw->hdrlen);
741 } else {
742 err = got_deltify(&deltas, &ndeltas,
743 raw->f, raw->hdrlen,
744 raw->size + raw->hdrlen,
745 base->dtab, base_raw->f, base_raw->hdrlen,
746 base_raw->size + base_raw->hdrlen);
748 got_object_raw_close(base_raw);
749 base_raw = NULL;
750 if (err)
751 goto done;
753 size = delta_size(deltas, ndeltas);
754 if (size + 32 < best_size){
755 /*
756 * if we already picked a best delta,
757 * replace it.
758 */
759 best_size = size;
760 free(best_deltas);
761 best_deltas = deltas;
762 best_ndeltas = ndeltas;
763 deltas = NULL;
764 m->nchain = base->nchain + 1;
765 m->prev = base;
766 m->head = base->head;
767 if (m->head == NULL)
768 m->head = base;
769 } else {
770 free(deltas);
771 deltas = NULL;
772 ndeltas = 0;
776 if (best_ndeltas > 0) {
777 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
778 delta_memsize + best_size <= max_delta_memsize) {
779 delta_memsize += best_size;
780 err = encode_delta_in_mem(m, raw, best_deltas,
781 best_ndeltas, best_size, m->prev->size);
782 } else {
783 m->delta_offset = ftello(delta_cache);
784 /*
785 * TODO:
786 * Storing compressed delta data in the delta
787 * cache file would probably be more efficient
788 * than writing uncompressed delta data here
789 * and compressing it while writing the pack
790 * file. This would also allow for reusing
791 * deltas in their compressed form.
792 */
793 err = encode_delta(m, raw, best_deltas,
794 best_ndeltas, m->prev->size, delta_cache);
796 free(best_deltas);
797 best_deltas = NULL;
798 best_ndeltas = 0;
799 if (err)
800 goto done;
803 got_object_raw_close(raw);
804 raw = NULL;
806 done:
807 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
808 got_deltify_free(meta[i]->dtab);
809 meta[i]->dtab = NULL;
811 if (raw)
812 got_object_raw_close(raw);
813 if (base_raw)
814 got_object_raw_close(base_raw);
815 if (outfd != -1 && close(outfd) == -1 && err == NULL)
816 err = got_error_from_errno("close");
817 free(deltas);
818 free(best_deltas);
819 return err;
822 static const struct got_error *
823 search_packidx(int *found, struct got_object_id *id,
824 struct got_repository *repo)
826 const struct got_error *err = NULL;
827 struct got_packidx *packidx = NULL;
828 int idx;
830 *found = 0;
832 err = got_repo_search_packidx(&packidx, &idx, repo, id);
833 if (err == NULL)
834 *found = 1; /* object is already packed */
835 else if (err->code == GOT_ERR_NO_OBJ)
836 err = NULL;
837 return err;
840 static const int obj_types[] = {
841 GOT_OBJ_TYPE_ANY,
842 GOT_OBJ_TYPE_COMMIT,
843 GOT_OBJ_TYPE_TREE,
844 GOT_OBJ_TYPE_BLOB,
845 GOT_OBJ_TYPE_TAG,
846 GOT_OBJ_TYPE_OFFSET_DELTA,
847 GOT_OBJ_TYPE_REF_DELTA
848 };
850 static const struct got_error *
851 add_object(int want_meta, struct got_object_idset *idset,
852 struct got_object_id *id, const char *path, int obj_type,
853 time_t mtime, int loose_obj_only, struct got_repository *repo)
855 const struct got_error *err;
856 struct got_pack_meta *m = NULL;
858 if (loose_obj_only) {
859 int is_packed;
860 err = search_packidx(&is_packed, id, repo);
861 if (err)
862 return err;
863 if (is_packed)
864 return NULL;
867 if (want_meta) {
868 err = alloc_meta(&m, id, path, obj_type, mtime);
869 if (err)
870 return err;
873 return got_object_idset_add(idset, id, m);
876 static const struct got_error *
877 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
878 struct got_object_idset *idset, struct got_object_id *tree_id,
879 const char *dpath, time_t mtime, struct got_repository *repo,
880 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
881 got_pack_progress_cb progress_cb, void *progress_arg,
882 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
884 const struct got_error *err;
885 struct got_tree_object *tree;
886 char *p = NULL;
887 int i;
889 err = got_object_open_as_tree(&tree, repo, tree_id);
890 if (err)
891 return err;
893 (*ntrees)++;
894 err = report_progress(progress_cb, progress_arg, rl,
895 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
896 if (err)
897 return err;
899 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
900 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
901 struct got_object_id *id = got_tree_entry_get_id(e);
902 mode_t mode = got_tree_entry_get_mode(e);
904 if (cancel_cb) {
905 err = (*cancel_cb)(cancel_arg);
906 if (err)
907 break;
910 if (got_object_tree_entry_is_submodule(e) ||
911 got_object_idset_contains(idset, id))
912 continue;
914 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
915 got_tree_entry_get_name(e)) == -1) {
916 err = got_error_from_errno("asprintf");
917 break;
920 if (S_ISDIR(mode)) {
921 struct got_object_qid *qid;
922 err = got_object_qid_alloc(&qid, id);
923 if (err)
924 break;
925 STAILQ_INSERT_TAIL(ids, qid, entry);
926 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
927 err = add_object(want_meta, idset, id, p,
928 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo);
929 if (err)
930 break;
931 (*nfound)++;
932 err = report_progress(progress_cb, progress_arg, rl,
933 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
934 if (err)
935 break;
937 free(p);
938 p = NULL;
941 got_object_tree_close(tree);
942 free(p);
943 return err;
946 static const struct got_error *
947 load_tree(int want_meta, struct got_object_idset *idset,
948 struct got_object_id *tree_id, const char *dpath, time_t mtime,
949 struct got_repository *repo, int loose_obj_only,
950 int *ncolored, int *nfound, int *ntrees,
951 got_pack_progress_cb progress_cb, void *progress_arg,
952 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
954 const struct got_error *err = NULL;
955 struct got_object_id_queue tree_ids;
956 struct got_object_qid *qid;
958 if (got_object_idset_contains(idset, tree_id))
959 return NULL;
961 err = got_object_qid_alloc(&qid, tree_id);
962 if (err)
963 return err;
965 STAILQ_INIT(&tree_ids);
966 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
968 while (!STAILQ_EMPTY(&tree_ids)) {
969 if (cancel_cb) {
970 err = (*cancel_cb)(cancel_arg);
971 if (err)
972 break;
975 qid = STAILQ_FIRST(&tree_ids);
976 STAILQ_REMOVE_HEAD(&tree_ids, entry);
978 if (got_object_idset_contains(idset, qid->id)) {
979 got_object_qid_free(qid);
980 continue;
983 err = add_object(want_meta, idset, qid->id, dpath,
984 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo);
985 if (err) {
986 got_object_qid_free(qid);
987 break;
990 (*nfound)++;
991 err = report_progress(progress_cb, progress_arg, rl,
992 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
993 if (err)
994 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)
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 if (err)
1039 goto done;
1041 (*nfound)++;
1042 err = report_progress(progress_cb, progress_arg, rl,
1043 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
1044 if (err)
1045 goto done;
1047 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1048 "", got_object_commit_get_committer_time(commit),
1049 repo, loose_obj_only, ncolored, nfound, ntrees,
1050 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1051 done:
1052 got_object_commit_close(commit);
1053 return err;
1056 static const struct got_error *
1057 load_tag(int want_meta, struct got_object_idset *idset,
1058 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1059 int *ncolored, int *nfound, int *ntrees,
1060 got_pack_progress_cb progress_cb, void *progress_arg,
1061 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1063 const struct got_error *err;
1064 struct got_tag_object *tag = NULL;
1066 if (got_object_idset_contains(idset, id))
1067 return NULL;
1069 if (loose_obj_only) {
1070 int is_packed;
1071 err = search_packidx(&is_packed, id, repo);
1072 if (err)
1073 return err;
1074 if (is_packed)
1075 return NULL;
1078 err = got_object_open_as_tag(&tag, repo, id);
1079 if (err)
1080 return err;
1082 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1083 got_object_tag_get_tagger_time(tag),
1084 loose_obj_only, repo);
1085 if (err)
1086 goto done;
1088 (*nfound)++;
1089 err = report_progress(progress_cb, progress_arg, rl,
1090 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
1091 if (err)
1092 goto done;
1094 switch (got_object_tag_get_object_type(tag)) {
1095 case GOT_OBJ_TYPE_COMMIT:
1096 err = load_commit(want_meta, idset,
1097 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1098 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1099 cancel_cb, cancel_arg);
1100 break;
1101 case GOT_OBJ_TYPE_TREE:
1102 err = load_tree(want_meta, idset,
1103 got_object_tag_get_object_id(tag), "",
1104 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1105 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1106 cancel_cb, cancel_arg);
1107 break;
1108 default:
1109 break;
1112 done:
1113 got_object_tag_close(tag);
1114 return err;
1117 enum findtwixt_color {
1118 COLOR_KEEP = 0,
1119 COLOR_DROP,
1120 COLOR_BLANK,
1122 static const int findtwixt_colors[] = {
1123 COLOR_KEEP,
1124 COLOR_DROP,
1125 COLOR_BLANK
1128 static const struct got_error *
1129 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1130 int color, struct got_repository *repo)
1132 const struct got_error *err;
1133 struct got_object_qid *qid;
1135 err = got_object_qid_alloc(&qid, id);
1136 if (err)
1137 return err;
1139 STAILQ_INSERT_TAIL(ids, qid, entry);
1140 qid->data = (void *)&findtwixt_colors[color];
1141 return NULL;
1144 static const struct got_error *
1145 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1146 struct got_object_id *id, struct got_repository *repo,
1147 got_cancel_cb cancel_cb, void *cancel_arg)
1149 const struct got_error *err = NULL;
1150 struct got_commit_object *commit;
1151 const struct got_object_id_queue *parents;
1152 struct got_object_id_queue ids;
1153 struct got_object_qid *qid;
1155 STAILQ_INIT(&ids);
1157 err = got_object_qid_alloc(&qid, id);
1158 if (err)
1159 return err;
1160 STAILQ_INSERT_HEAD(&ids, qid, entry);
1162 while (!STAILQ_EMPTY(&ids)) {
1163 if (cancel_cb) {
1164 err = (*cancel_cb)(cancel_arg);
1165 if (err)
1166 break;
1169 qid = STAILQ_FIRST(&ids);
1170 STAILQ_REMOVE_HEAD(&ids, entry);
1172 if (got_object_idset_contains(drop, qid->id)) {
1173 got_object_qid_free(qid);
1174 continue;
1177 err = got_object_idset_add(drop, qid->id,
1178 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1179 if (err) {
1180 got_object_qid_free(qid);
1181 break;
1184 if (!got_object_idset_contains(keep, qid->id)) {
1185 got_object_qid_free(qid);
1186 continue;
1189 err = got_object_open_as_commit(&commit, repo, qid->id);
1190 got_object_qid_free(qid);
1191 if (err)
1192 break;
1194 parents = got_object_commit_get_parent_ids(commit);
1195 if (parents) {
1196 err = got_object_id_queue_copy(parents, &ids);
1197 if (err) {
1198 got_object_commit_close(commit);
1199 break;
1202 got_object_commit_close(commit);
1205 got_object_id_queue_free(&ids);
1206 return err;
1209 struct append_id_arg {
1210 struct got_object_id **array;
1211 int idx;
1214 static const struct got_error *
1215 append_id(struct got_object_id *id, void *data, void *arg)
1217 struct append_id_arg *a = arg;
1219 a->array[a->idx] = got_object_id_dup(id);
1220 if (a->array[a->idx] == NULL)
1221 return got_error_from_errno("got_object_id_dup");
1223 a->idx++;
1224 return NULL;
1227 static const struct got_error *
1228 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1229 struct got_object_id **head, int nhead,
1230 struct got_object_id **tail, int ntail,
1231 struct got_repository *repo,
1232 got_pack_progress_cb progress_cb, void *progress_arg,
1233 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1235 const struct got_error *err = NULL;
1236 struct got_object_id_queue ids;
1237 struct got_object_idset *keep, *drop;
1238 struct got_object_qid *qid;
1239 int i, ncolor, nkeep, obj_type;
1241 STAILQ_INIT(&ids);
1242 *res = NULL;
1243 *nres = 0;
1244 *ncolored = 0;
1246 keep = got_object_idset_alloc();
1247 if (keep == NULL)
1248 return got_error_from_errno("got_object_idset_alloc");
1250 drop = got_object_idset_alloc();
1251 if (drop == NULL) {
1252 err = got_error_from_errno("got_object_idset_alloc");
1253 goto done;
1256 for (i = 0; i < nhead; i++) {
1257 struct got_object_id *id = head[i];
1258 if (id == NULL)
1259 continue;
1260 err = got_object_get_type(&obj_type, repo, id);
1261 if (err)
1262 return err;
1263 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1264 continue;
1265 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1266 if (err)
1267 goto done;
1269 for (i = 0; i < ntail; i++) {
1270 struct got_object_id *id = tail[i];
1271 if (id == NULL)
1272 continue;
1273 err = got_object_get_type(&obj_type, repo, id);
1274 if (err)
1275 return err;
1276 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1277 continue;
1278 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1279 if (err)
1280 goto done;
1283 while (!STAILQ_EMPTY(&ids)) {
1284 int qcolor;
1285 qid = STAILQ_FIRST(&ids);
1286 qcolor = *((int *)qid->data);
1288 if (got_object_idset_contains(drop, qid->id))
1289 ncolor = COLOR_DROP;
1290 else if (got_object_idset_contains(keep, qid->id))
1291 ncolor = COLOR_KEEP;
1292 else
1293 ncolor = COLOR_BLANK;
1295 (*ncolored)++;
1296 err = report_progress(progress_cb, progress_arg, rl,
1297 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1298 if (err)
1299 goto done;
1301 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1302 qcolor == COLOR_KEEP)) {
1303 STAILQ_REMOVE_HEAD(&ids, entry);
1304 got_object_qid_free(qid);
1305 continue;
1308 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1309 err = drop_commit(keep, drop, qid->id, repo,
1310 cancel_cb, cancel_arg);
1311 if (err)
1312 goto done;
1313 } else if (ncolor == COLOR_BLANK) {
1314 struct got_commit_object *commit;
1315 struct got_object_id *id;
1316 const struct got_object_id_queue *parents;
1317 struct got_object_qid *pid;
1319 id = got_object_id_dup(qid->id);
1320 if (id == NULL) {
1321 err = got_error_from_errno("got_object_id_dup");
1322 goto done;
1324 if (qcolor == COLOR_KEEP)
1325 err = got_object_idset_add(keep, id,
1326 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1327 else
1328 err = got_object_idset_add(drop, id,
1329 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1330 if (err) {
1331 free(id);
1332 goto done;
1335 err = got_object_open_as_commit(&commit, repo, id);
1336 if (err) {
1337 free(id);
1338 goto done;
1340 parents = got_object_commit_get_parent_ids(commit);
1341 if (parents) {
1342 STAILQ_FOREACH(pid, parents, entry) {
1343 err = queue_commit_id(&ids, pid->id,
1344 qcolor, repo);
1345 if (err) {
1346 free(id);
1347 goto done;
1351 got_object_commit_close(commit);
1352 commit = NULL;
1353 } else {
1354 /* should not happen */
1355 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1356 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1357 goto done;
1360 STAILQ_REMOVE_HEAD(&ids, entry);
1361 got_object_qid_free(qid);
1364 nkeep = got_object_idset_num_elements(keep);
1365 if (nkeep > 0) {
1366 struct append_id_arg arg;
1367 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1368 if (arg.array == NULL) {
1369 err = got_error_from_errno("calloc");
1370 goto done;
1372 arg.idx = 0;
1373 err = got_object_idset_for_each(keep, append_id, &arg);
1374 if (err) {
1375 free(arg.array);
1376 goto done;
1378 *res = arg.array;
1379 *nres = nkeep;
1381 done:
1382 got_object_idset_free(keep);
1383 got_object_idset_free(drop);
1384 got_object_id_queue_free(&ids);
1385 return err;
1388 static const struct got_error *
1389 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1390 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1391 struct got_object_id **ours, int nours, struct got_repository *repo,
1392 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1393 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1395 const struct got_error *err = NULL;
1396 struct got_object_id **ids = NULL;
1397 int i, nobj = 0, obj_type;
1399 *ncolored = 0;
1400 *nfound = 0;
1401 *ntrees = 0;
1403 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1404 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1405 if (err || nobj == 0)
1406 goto done;
1408 for (i = 0; i < ntheirs; i++) {
1409 struct got_object_id *id = theirs[i];
1410 if (id == NULL)
1411 continue;
1412 err = got_object_get_type(&obj_type, repo, id);
1413 if (err)
1414 return err;
1415 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1416 continue;
1417 err = load_commit(0, idset, id, repo, loose_obj_only,
1418 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1419 cancel_cb, cancel_arg);
1420 if (err)
1421 goto done;
1424 for (i = 0; i < ntheirs; i++) {
1425 struct got_object_id *id = theirs[i];
1426 struct got_pack_meta *m;
1427 if (id == NULL)
1428 continue;
1429 m = got_object_idset_get(idset, id);
1430 if (m == NULL) {
1431 err = got_object_get_type(&obj_type, repo, id);
1432 if (err)
1433 goto done;
1434 } else
1435 obj_type = m->obj_type;
1436 if (obj_type != GOT_OBJ_TYPE_TAG)
1437 continue;
1438 err = load_tag(0, idset, id, repo, loose_obj_only,
1439 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1440 cancel_cb, cancel_arg);
1441 if (err)
1442 goto done;
1445 for (i = 0; i < nobj; i++) {
1446 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1447 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1448 cancel_cb, cancel_arg);
1449 if (err)
1450 goto done;
1453 for (i = 0; i < nours; i++) {
1454 struct got_object_id *id = ours[i];
1455 struct got_pack_meta *m;
1456 if (id == NULL)
1457 continue;
1458 m = got_object_idset_get(idset, id);
1459 if (m == NULL) {
1460 err = got_object_get_type(&obj_type, repo, id);
1461 if (err)
1462 goto done;
1463 } else
1464 obj_type = m->obj_type;
1465 if (obj_type != GOT_OBJ_TYPE_TAG)
1466 continue;
1467 err = load_tag(1, idset, id, repo, loose_obj_only,
1468 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1469 cancel_cb, cancel_arg);
1470 if (err)
1471 goto done;
1473 done:
1474 for (i = 0; i < nobj; i++) {
1475 free(ids[i]);
1477 free(ids);
1478 return err;
1481 const struct got_error *
1482 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1484 size_t n;
1486 SHA1Update(ctx, buf, len);
1487 n = fwrite(buf, 1, len, f);
1488 if (n != len)
1489 return got_ferror(f, GOT_ERR_IO);
1490 return NULL;
1493 static void
1494 putbe32(char *b, uint32_t n)
1496 b[0] = n >> 24;
1497 b[1] = n >> 16;
1498 b[2] = n >> 8;
1499 b[3] = n >> 0;
1502 static int
1503 write_order_cmp(const void *pa, const void *pb)
1505 struct got_pack_meta *a, *b, *ahd, *bhd;
1507 a = *(struct got_pack_meta **)pa;
1508 b = *(struct got_pack_meta **)pb;
1509 ahd = (a->head == NULL) ? a : a->head;
1510 bhd = (b->head == NULL) ? b : b->head;
1511 if (ahd->mtime != bhd->mtime)
1512 return bhd->mtime - ahd->mtime;
1513 if (ahd != bhd)
1514 return (uintptr_t)bhd - (uintptr_t)ahd;
1515 if (a->nchain != b->nchain)
1516 return a->nchain - b->nchain;
1517 return a->mtime - b->mtime;
1520 static int
1521 reuse_write_order_cmp(const void *pa, const void *pb)
1523 struct got_pack_meta *a, *b;
1525 a = *(struct got_pack_meta **)pa;
1526 b = *(struct got_pack_meta **)pb;
1528 if (a->reused_delta_offset < b->reused_delta_offset)
1529 return -1;
1530 if (a->reused_delta_offset > b->reused_delta_offset)
1531 return 1;
1532 return 0;
1535 static const struct got_error *
1536 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1538 size_t i;
1540 *hdrlen = 0;
1542 hdr[0] = obj_type << 4;
1543 hdr[0] |= len & 0xf;
1544 len >>= 4;
1545 for (i = 1; len != 0; i++){
1546 if (i >= bufsize)
1547 return got_error(GOT_ERR_NO_SPACE);
1548 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1549 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1550 len >>= GOT_DELTA_SIZE_SHIFT;
1553 *hdrlen = i;
1554 return NULL;
1557 static int
1558 packoff(char *hdr, off_t off)
1560 int i, j;
1561 char rbuf[8];
1563 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1564 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1565 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1566 GOT_DELTA_SIZE_MORE;
1569 j = 0;
1570 while (i > 0)
1571 hdr[j++] = rbuf[--i];
1572 return j;
1575 static const struct got_error *
1576 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1577 struct got_pack_meta *m)
1579 const struct got_error *err;
1580 char buf[32];
1581 int nh;
1583 if (m->prev->off != 0) {
1584 err = packhdr(&nh, buf, sizeof(buf),
1585 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1586 if (err)
1587 return err;
1588 nh += packoff(buf + nh, m->off - m->prev->off);
1589 err = hwrite(packfile, buf, nh, ctx);
1590 if (err)
1591 return err;
1592 *packfile_size += nh;
1593 } else {
1594 err = packhdr(&nh, buf, sizeof(buf),
1595 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1596 if (err)
1597 return err;
1598 err = hwrite(packfile, buf, nh, ctx);
1599 if (err)
1600 return err;
1601 *packfile_size += nh;
1602 err = hwrite(packfile, m->prev->id.sha1,
1603 sizeof(m->prev->id.sha1), ctx);
1604 if (err)
1605 return err;
1606 *packfile_size += sizeof(m->prev->id.sha1);
1609 return NULL;
1612 static const struct got_error *
1613 write_packed_object(off_t *packfile_size, FILE *packfile,
1614 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1615 SHA1_CTX *ctx, struct got_repository *repo)
1617 const struct got_error *err = NULL;
1618 struct got_deflate_checksum csum;
1619 char buf[32];
1620 int nh;
1621 struct got_raw_object *raw = NULL;
1622 off_t outlen;
1624 csum.output_sha1 = ctx;
1625 csum.output_crc = NULL;
1627 m->off = ftello(packfile);
1628 if (m->delta_len == 0) {
1629 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1630 if (err)
1631 goto done;
1632 err = packhdr(&nh, buf, sizeof(buf),
1633 m->obj_type, raw->size);
1634 if (err)
1635 goto done;
1636 err = hwrite(packfile, buf, nh, ctx);
1637 if (err)
1638 goto done;
1639 *packfile_size += nh;
1640 if (raw->f == NULL) {
1641 err = got_deflate_to_file_mmap(&outlen,
1642 raw->data + raw->hdrlen, 0, raw->size,
1643 packfile, &csum);
1644 if (err)
1645 goto done;
1646 } else {
1647 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1648 == -1) {
1649 err = got_error_from_errno("fseeko");
1650 goto done;
1652 err = got_deflate_to_file(&outlen, raw->f,
1653 raw->size, packfile, &csum);
1654 if (err)
1655 goto done;
1657 *packfile_size += outlen;
1658 got_object_raw_close(raw);
1659 raw = NULL;
1660 } else if (m->delta_buf) {
1661 err = deltahdr(packfile_size, ctx, packfile, m);
1662 if (err)
1663 goto done;
1664 err = got_deflate_to_file_mmap(&outlen,
1665 m->delta_buf, 0, m->delta_len, packfile, &csum);
1666 if (err)
1667 goto done;
1668 *packfile_size += outlen;
1669 free(m->delta_buf);
1670 m->delta_buf = NULL;
1671 } else {
1672 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1673 == -1) {
1674 err = got_error_from_errno("fseeko");
1675 goto done;
1677 err = deltahdr(packfile_size, ctx, packfile, m);
1678 if (err)
1679 goto done;
1680 err = got_deflate_to_file(&outlen, delta_cache,
1681 m->delta_len, packfile, &csum);
1682 if (err)
1683 goto done;
1684 *packfile_size += outlen;
1686 done:
1687 if (raw)
1688 got_object_raw_close(raw);
1689 return err;
1692 static const struct got_error *
1693 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1694 struct got_pack_meta **deltify, int ndeltify,
1695 struct got_pack_meta **reuse, int nreuse,
1696 int ncolored, int nfound, int ntrees, int nours,
1697 struct got_repository *repo,
1698 got_pack_progress_cb progress_cb, void *progress_arg,
1699 struct got_ratelimit *rl,
1700 got_cancel_cb cancel_cb, void *cancel_arg)
1702 const struct got_error *err = NULL;
1703 int i;
1704 SHA1_CTX ctx;
1705 struct got_pack_meta *m;
1706 char buf[32];
1707 size_t n;
1708 off_t packfile_size = 0;
1709 int outfd = -1;
1711 SHA1Init(&ctx);
1713 err = hwrite(packfile, "PACK", 4, &ctx);
1714 if (err)
1715 return err;
1716 putbe32(buf, GOT_PACKFILE_VERSION);
1717 err = hwrite(packfile, buf, 4, &ctx);
1718 if (err)
1719 goto done;
1720 putbe32(buf, ndeltify + nreuse);
1721 err = hwrite(packfile, buf, 4, &ctx);
1722 if (err)
1723 goto done;
1725 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1726 write_order_cmp);
1727 for (i = 0; i < ndeltify; i++) {
1728 err = report_progress(progress_cb, progress_arg, rl,
1729 ncolored, nfound, ntrees, packfile_size, nours,
1730 ndeltify + nreuse, ndeltify + nreuse, i);
1731 if (err)
1732 goto done;
1733 m = deltify[i];
1734 err = write_packed_object(&packfile_size, packfile,
1735 delta_cache, m, &outfd, &ctx, repo);
1736 if (err)
1737 goto done;
1740 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1741 reuse_write_order_cmp);
1742 for (i = 0; i < nreuse; i++) {
1743 err = report_progress(progress_cb, progress_arg, rl,
1744 ncolored, nfound, ntrees, packfile_size, nours,
1745 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1746 if (err)
1747 goto done;
1748 m = reuse[i];
1749 err = write_packed_object(&packfile_size, packfile,
1750 delta_cache, m, &outfd, &ctx, repo);
1751 if (err)
1752 goto done;
1755 SHA1Final(pack_sha1, &ctx);
1756 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1757 if (n != SHA1_DIGEST_LENGTH)
1758 err = got_ferror(packfile, GOT_ERR_IO);
1759 packfile_size += SHA1_DIGEST_LENGTH;
1760 packfile_size += sizeof(struct got_packfile_hdr);
1761 if (progress_cb) {
1762 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1763 packfile_size, nours, ndeltify + nreuse,
1764 ndeltify + nreuse, ndeltify + nreuse);
1765 if (err)
1766 goto done;
1768 done:
1769 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1770 err = got_error_from_errno("close");
1771 return err;
1774 static const struct got_error *
1775 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1777 struct got_object_idset *idset = arg;
1779 if (got_object_idset_get_element_data(entry) == NULL)
1780 got_object_idset_remove_element(idset, entry);
1782 return NULL;
1785 static const struct got_error *
1786 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1788 struct got_object_idset *idset = arg;
1789 struct got_pack_meta *m;
1791 m = got_object_idset_get_element_data(entry);
1792 if (m->have_reused_delta)
1793 got_object_idset_remove_element(idset, entry);
1795 return NULL;
1798 static const struct got_error *
1799 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1801 struct got_pack_meta *m = data;
1802 struct got_pack_metavec *v = arg;
1804 return add_meta(m, v);
1807 const struct got_error *
1808 got_pack_create(uint8_t *packsha1, FILE *packfile,
1809 struct got_object_id **theirs, int ntheirs,
1810 struct got_object_id **ours, int nours,
1811 struct got_repository *repo, int loose_obj_only, int allow_empty,
1812 got_pack_progress_cb progress_cb, void *progress_arg,
1813 got_cancel_cb cancel_cb, void *cancel_arg)
1815 const struct got_error *err;
1816 int delta_cache_fd = -1;
1817 FILE *delta_cache = NULL;
1818 struct got_object_idset *idset;
1819 struct got_ratelimit rl;
1820 struct got_pack_metavec deltify, reuse;
1821 int ncolored = 0, nfound = 0, ntrees = 0;
1823 memset(&deltify, 0, sizeof(deltify));
1824 memset(&reuse, 0, sizeof(reuse));
1826 got_ratelimit_init(&rl, 0, 500);
1828 idset = got_object_idset_alloc();
1829 if (idset == NULL)
1830 return got_error_from_errno("got_object_idset_alloc");
1832 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1833 ntheirs, ours, nours, repo, loose_obj_only,
1834 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1835 if (err)
1836 return err;
1838 err = got_object_idset_for_each_element(idset,
1839 remove_unused_object, idset);
1840 if (err)
1841 goto done;
1843 if (progress_cb) {
1844 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1845 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1846 if (err)
1847 goto done;
1850 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1851 err = got_error(GOT_ERR_CANNOT_PACK);
1852 goto done;
1855 delta_cache_fd = got_opentempfd();
1856 if (delta_cache_fd == -1) {
1857 err = got_error_from_errno("got_opentemp");
1858 goto done;
1861 reuse.metasz = 64;
1862 reuse.meta = calloc(reuse.metasz,
1863 sizeof(struct got_pack_meta *));
1864 if (reuse.meta == NULL) {
1865 err = got_error_from_errno("calloc");
1866 goto done;
1869 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1870 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1871 cancel_cb, cancel_arg);
1872 if (err)
1873 goto done;
1874 if (reuse.nmeta > 0) {
1875 err = got_object_idset_for_each_element(idset,
1876 remove_reused_object, idset);
1877 if (err)
1878 goto done;
1881 delta_cache = fdopen(delta_cache_fd, "a+");
1882 if (delta_cache == NULL) {
1883 err = got_error_from_errno("fdopen");
1884 goto done;
1886 delta_cache_fd = -1;
1888 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1889 err = got_error_from_errno("fseeko");
1890 goto done;
1893 deltify.meta = calloc(got_object_idset_num_elements(idset),
1894 sizeof(struct got_pack_meta *));
1895 if (deltify.meta == NULL) {
1896 err = got_error_from_errno("calloc");
1897 goto done;
1899 deltify.metasz = got_object_idset_num_elements(idset);
1901 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1902 if (err)
1903 goto done;
1904 if (deltify.nmeta > 0) {
1905 err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1906 nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1907 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1908 if (err)
1909 goto done;
1910 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1911 err = got_error_from_errno("fseeko");
1912 goto done;
1916 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1917 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1918 nours, repo, progress_cb, progress_arg, &rl,
1919 cancel_cb, cancel_arg);
1920 if (err)
1921 goto done;
1922 done:
1923 free_nmeta(deltify.meta, deltify.nmeta);
1924 free_nmeta(reuse.meta, reuse.nmeta);
1925 got_object_idset_free(idset);
1926 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1927 err = got_error_from_errno("close");
1928 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1929 err = got_error_from_errno("fclose");
1930 return err;