Blob


1 /*
2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
25 #include <endian.h>
26 #include <stdint.h>
27 #include <imsg.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sha1.h>
32 #include <time.h>
33 #include <limits.h>
34 #include <zlib.h>
36 #include "got_error.h"
37 #include "got_cancel.h"
38 #include "got_object.h"
39 #include "got_path.h"
40 #include "got_reference.h"
41 #include "got_repository_admin.h"
42 #include "got_opentemp.h"
44 #include "got_lib_deltify.h"
45 #include "got_lib_delta.h"
46 #include "got_lib_object.h"
47 #include "got_lib_object_idset.h"
48 #include "got_lib_object_cache.h"
49 #include "got_lib_deflate.h"
50 #include "got_lib_pack.h"
51 #include "got_lib_privsep.h"
52 #include "got_lib_repository.h"
53 #include "got_lib_ratelimit.h"
55 #ifndef MIN
56 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
57 #endif
59 #ifndef MAX
60 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
61 #endif
63 struct got_pack_meta {
64 struct got_object_id id;
65 char *path;
66 int obj_type;
67 off_t size;
68 time_t mtime;
70 /* The best delta we picked */
71 struct got_pack_meta *head;
72 struct got_pack_meta *prev;
73 unsigned char *delta_buf; /* if not encoded in delta cache file */
74 off_t delta_offset; /* offset in delta cache file */
75 off_t delta_len; /* encoded delta length */
76 int nchain;
78 int have_reused_delta;
79 off_t reused_delta_offset; /* offset of delta in reused pack file */
80 struct got_object_id *base_obj_id;
82 /* Only used for delta window */
83 struct got_delta_table *dtab;
85 /* Only used for writing offset deltas */
86 off_t off;
87 };
89 struct got_pack_metavec {
90 struct got_pack_meta **meta;
91 int nmeta;
92 int metasz;
93 };
95 static const struct got_error *
96 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
97 const char *path, int obj_type, time_t mtime)
98 {
99 const struct got_error *err = NULL;
100 struct got_pack_meta *m;
102 *new = NULL;
104 m = calloc(1, sizeof(*m));
105 if (m == NULL)
106 return got_error_from_errno("calloc");
108 memcpy(&m->id, id, sizeof(m->id));
110 m->path = strdup(path);
111 if (m->path == NULL) {
112 err = got_error_from_errno("strdup");
113 free(m);
114 return err;
117 m->obj_type = obj_type;
118 m->mtime = mtime;
119 *new = m;
120 return NULL;
123 static void
124 clear_meta(struct got_pack_meta *meta)
126 if (meta == NULL)
127 return;
128 free(meta->path);
129 meta->path = NULL;
130 free(meta->delta_buf);
131 meta->delta_buf = NULL;
132 free(meta->base_obj_id);
133 meta->base_obj_id = NULL;
136 static void
137 free_nmeta(struct got_pack_meta **meta, int nmeta)
139 int i;
141 for (i = 0; i < nmeta; i++)
142 clear_meta(meta[i]);
143 free(meta);
146 static int
147 delta_order_cmp(const void *pa, const void *pb)
149 struct got_pack_meta *a, *b;
150 int cmp;
152 a = *(struct got_pack_meta **)pa;
153 b = *(struct got_pack_meta **)pb;
155 if (a->obj_type != b->obj_type)
156 return a->obj_type - b->obj_type;
157 cmp = strcmp(a->path, b->path);
158 if (cmp != 0)
159 return cmp;
160 if (a->mtime != b->mtime)
161 return a->mtime - b->mtime;
162 return got_object_id_cmp(&a->id, &b->id);
165 static off_t
166 delta_size(struct got_delta_instruction *deltas, int ndeltas)
168 int i;
169 off_t size = 32;
170 for (i = 0; i < ndeltas; i++) {
171 if (deltas[i].copy)
172 size += GOT_DELTA_SIZE_SHIFT;
173 else
174 size += deltas[i].len + 1;
176 return size;
179 static const struct got_error *
180 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
182 char *n;
184 if (*len + nseg >= *sz) {
185 while (*len + nseg >= *sz)
186 *sz += *sz / 2;
187 n = realloc(*p, *sz);
188 if (n == NULL)
189 return got_error_from_errno("realloc");
190 *p = n;
192 memcpy(*p + *len, seg, nseg);
193 *len += nseg;
194 return NULL;
197 static const struct got_error *
198 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
199 struct got_delta_instruction *deltas, int ndeltas,
200 off_t delta_size, off_t base_size)
202 const struct got_error *err;
203 unsigned char buf[16], *bp;
204 int i, j;
205 size_t len = 0;
206 off_t n;
207 struct got_delta_instruction *d;
209 m->delta_buf = malloc(delta_size);
210 if (m->delta_buf == NULL)
211 return got_error_from_errno("calloc");
213 /* base object size */
214 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
215 n = base_size >> GOT_DELTA_SIZE_SHIFT;
216 for (i = 1; n > 0; i++) {
217 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
218 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
219 n >>= GOT_DELTA_SIZE_SHIFT;
221 err = append(&m->delta_buf, &len, &delta_size, buf, i);
222 if (err)
223 return err;
225 /* target object size */
226 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
227 n = o->size >> GOT_DELTA_SIZE_SHIFT;
228 for (i = 1; n > 0; i++) {
229 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
230 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
231 n >>= GOT_DELTA_SIZE_SHIFT;
233 err = append(&m->delta_buf, &len, &delta_size, buf, i);
234 if (err)
235 return err;
237 for (j = 0; j < ndeltas; j++) {
238 d = &deltas[j];
239 if (d->copy) {
240 n = d->offset;
241 bp = &buf[1];
242 buf[0] = GOT_DELTA_BASE_COPY;
243 for (i = 0; i < 4; i++) {
244 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
245 buf[0] |= 1 << i;
246 *bp++ = n & 0xff;
247 n >>= 8;
248 if (n == 0)
249 break;
252 n = d->len;
253 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
254 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
255 for (i = 0; i < 3 && n > 0; i++) {
256 buf[0] |= 1 << (i + 4);
257 *bp++ = n & 0xff;
258 n >>= 8;
261 err = append(&m->delta_buf, &len, &delta_size,
262 buf, bp - buf);
263 if (err)
264 return err;
265 } else if (o->f == NULL) {
266 n = 0;
267 while (n != d->len) {
268 buf[0] = (d->len - n < 127) ? d->len - n : 127;
269 err = append(&m->delta_buf, &len, &delta_size,
270 buf, 1);
271 if (err)
272 return err;
273 err = append(&m->delta_buf, &len, &delta_size,
274 o->data + o->hdrlen + d->offset + n,
275 buf[0]);
276 if (err)
277 return err;
278 n += buf[0];
280 } else {
281 char content[128];
282 size_t r;
283 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
284 return got_error_from_errno("fseeko");
285 n = 0;
286 while (n != d->len) {
287 buf[0] = (d->len - n < 127) ? d->len - n : 127;
288 err = append(&m->delta_buf, &len, &delta_size,
289 buf, 1);
290 if (err)
291 return err;
292 r = fread(content, 1, buf[0], o->f);
293 if (r != buf[0])
294 return got_ferror(o->f, GOT_ERR_IO);
295 err = append(&m->delta_buf, &len, &delta_size,
296 content, buf[0]);
297 if (err)
298 return err;
299 n += buf[0];
304 m->delta_len = len;
305 return NULL;
308 static const struct got_error *
309 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
310 struct got_delta_instruction *deltas, int ndeltas,
311 off_t base_size, FILE *f)
313 unsigned char buf[16], *bp;
314 int i, j;
315 off_t n;
316 size_t w;
317 struct got_delta_instruction *d;
319 /* base object size */
320 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
321 n = base_size >> GOT_DELTA_SIZE_SHIFT;
322 for (i = 1; n > 0; i++) {
323 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
324 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
325 n >>= GOT_DELTA_SIZE_SHIFT;
327 w = fwrite(buf, 1, i, f);
328 if (w != i)
329 return got_ferror(f, GOT_ERR_IO);
331 /* target object size */
332 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
333 n = o->size >> GOT_DELTA_SIZE_SHIFT;
334 for (i = 1; n > 0; i++) {
335 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
336 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
337 n >>= GOT_DELTA_SIZE_SHIFT;
339 w = fwrite(buf, 1, i, f);
340 if (w != i)
341 return got_ferror(f, GOT_ERR_IO);
343 for (j = 0; j < ndeltas; j++) {
344 d = &deltas[j];
345 if (d->copy) {
346 n = d->offset;
347 bp = &buf[1];
348 buf[0] = GOT_DELTA_BASE_COPY;
349 for (i = 0; i < 4; i++) {
350 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
351 buf[0] |= 1 << i;
352 *bp++ = n & 0xff;
353 n >>= 8;
354 if (n == 0)
355 break;
358 n = d->len;
359 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
360 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
361 for (i = 0; i < 3 && n > 0; i++) {
362 buf[0] |= 1 << (i + 4);
363 *bp++ = n & 0xff;
364 n >>= 8;
367 w = fwrite(buf, 1, bp - buf, f);
368 if (w != bp - buf)
369 return got_ferror(f, GOT_ERR_IO);
370 } else if (o->f == NULL) {
371 n = 0;
372 while (n != d->len) {
373 buf[0] = (d->len - n < 127) ? d->len - n : 127;
374 w = fwrite(buf, 1, 1, f);
375 if (w != 1)
376 return got_ferror(f, GOT_ERR_IO);
377 w = fwrite(o->data + o->hdrlen + d->offset + n,
378 1, buf[0], f);
379 if (w != buf[0])
380 return got_ferror(f, GOT_ERR_IO);
381 n += buf[0];
383 } else {
384 char content[128];
385 size_t r;
386 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
387 return got_error_from_errno("fseeko");
388 n = 0;
389 while (n != d->len) {
390 buf[0] = (d->len - n < 127) ? d->len - n : 127;
391 w = fwrite(buf, 1, 1, f);
392 if (w != 1)
393 return got_ferror(f, GOT_ERR_IO);
394 r = fread(content, 1, buf[0], o->f);
395 if (r != buf[0])
396 return got_ferror(o->f, GOT_ERR_IO);
397 w = fwrite(content, 1, buf[0], f);
398 if (w != buf[0])
399 return got_ferror(f, GOT_ERR_IO);
400 n += buf[0];
405 m->delta_len = ftello(f) - m->delta_offset;
406 return NULL;
409 static const struct got_error *
410 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
411 struct got_ratelimit *rl, off_t packfile_size, int ncommits,
412 int nobj_total, int obj_deltify, 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, packfile_size, ncommits,
425 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 ncommits;
535 };
537 static const struct got_error *
538 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
540 const struct got_error *err;
541 struct got_pack_meta *m = data;
542 struct search_deltas_arg *a = arg;
543 int obj_idx;
544 struct got_object *obj = NULL;
546 if (a->cancel_cb) {
547 err = (*a->cancel_cb)(a->cancel_arg);
548 if (err)
549 return err;
552 if (!got_repo_check_packidx_bloom_filter(a->repo,
553 a->packidx->path_packidx, id))
554 return NULL;
556 obj_idx = got_packidx_get_object_idx(a->packidx, id);
557 if (obj_idx == -1)
558 return NULL;
560 /* TODO:
561 * Opening and closing an object just to check its flags
562 * is a bit expensive. We could have an imsg which requests
563 * plain type/size information for an object without doing
564 * work such as traversing the object's entire delta chain
565 * to find the base object type, and other such info which
566 * we don't really need here.
567 */
568 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
569 a->packidx, obj_idx, a->repo);
570 if (err)
571 return err;
573 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
574 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
575 a->delta_cache_fd, a->repo);
576 if (err)
577 goto done;
578 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
579 0L, a->ncommits, got_object_idset_num_elements(a->idset),
580 a->v->nmeta, 0);
582 done:
583 got_object_close(obj);
584 return err;
587 static const struct got_error *
588 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
589 int delta_cache_fd, int ncommits, struct got_repository *repo,
590 got_pack_progress_cb progress_cb, void *progress_arg,
591 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
593 const struct got_error *err = NULL;
594 char *path_packfile = NULL;
595 struct got_packidx *packidx;
596 struct got_pack *pack;
597 struct search_deltas_arg sda;
599 err = find_pack_for_reuse(&packidx, repo);
600 if (err)
601 return err;
603 if (packidx == NULL)
604 return NULL;
606 err = got_packidx_get_packfile_path(&path_packfile,
607 packidx->path_packidx);
608 if (err)
609 return err;
611 pack = got_repo_get_cached_pack(repo, path_packfile);
612 if (pack == NULL) {
613 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
614 if (err)
615 goto done;
618 sda.packidx = packidx;
619 sda.pack = pack;
620 sda.idset = idset;
621 sda.v = v;
622 sda.delta_cache_fd = delta_cache_fd;
623 sda.repo = repo;
624 sda.progress_cb = progress_cb;
625 sda.progress_arg = progress_arg;
626 sda.rl = rl;
627 sda.cancel_cb = cancel_cb;
628 sda.cancel_arg = cancel_arg;
629 sda.ncommits = ncommits;
630 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
631 done:
632 free(path_packfile);
633 return err;
636 static const struct got_error *
637 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncommits,
638 int nreused, FILE *delta_cache, struct got_repository *repo,
639 got_pack_progress_cb progress_cb, void *progress_arg,
640 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
642 const struct got_error *err = NULL;
643 struct got_pack_meta *m = NULL, *base = NULL;
644 struct got_raw_object *raw = NULL, *base_raw = NULL;
645 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
646 int i, j, ndeltas, best_ndeltas;
647 off_t size, best_size;
648 const int max_base_candidates = 3;
649 size_t delta_memsize = 0;
650 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
651 int outfd = -1;
653 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
654 for (i = 0; i < nmeta; i++) {
655 if (cancel_cb) {
656 err = (*cancel_cb)(cancel_arg);
657 if (err)
658 break;
660 err = report_progress(progress_cb, progress_arg, rl,
661 0L, ncommits, nreused + nmeta, nreused + i, 0);
662 if (err)
663 goto done;
664 m = meta[i];
666 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
667 m->obj_type == GOT_OBJ_TYPE_TAG)
668 continue;
670 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
671 if (err)
672 goto done;
673 m->size = raw->size;
675 if (raw->f == NULL) {
676 err = got_deltify_init_mem(&m->dtab, raw->data,
677 raw->hdrlen, raw->size + raw->hdrlen);
678 } else {
679 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
680 raw->size + raw->hdrlen);
682 if (err)
683 goto done;
685 if (i > max_base_candidates) {
686 struct got_pack_meta *n = NULL;
687 n = meta[i - (max_base_candidates + 1)];
688 got_deltify_free(n->dtab);
689 n->dtab = NULL;
692 best_size = raw->size;
693 best_ndeltas = 0;
694 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
695 if (cancel_cb) {
696 err = (*cancel_cb)(cancel_arg);
697 if (err)
698 goto done;
700 base = meta[j];
701 /* long chains make unpacking slow, avoid such bases */
702 if (base->nchain >= 128 ||
703 base->obj_type != m->obj_type)
704 continue;
706 err = got_object_raw_open(&base_raw, &outfd, repo,
707 &base->id);
708 if (err)
709 goto done;
711 if (raw->f == NULL && base_raw->f == NULL) {
712 err = got_deltify_mem_mem(&deltas, &ndeltas,
713 raw->data, raw->hdrlen,
714 raw->size + raw->hdrlen,
715 base->dtab, base_raw->data,
716 base_raw->hdrlen,
717 base_raw->size + base_raw->hdrlen);
718 } else if (raw->f == NULL) {
719 err = got_deltify_mem_file(&deltas, &ndeltas,
720 raw->data, raw->hdrlen,
721 raw->size + raw->hdrlen,
722 base->dtab, base_raw->f,
723 base_raw->hdrlen,
724 base_raw->size + base_raw->hdrlen);
725 } else if (base_raw->f == NULL) {
726 err = got_deltify_file_mem(&deltas, &ndeltas,
727 raw->f, raw->hdrlen,
728 raw->size + raw->hdrlen,
729 base->dtab, base_raw->data,
730 base_raw->hdrlen,
731 base_raw->size + base_raw->hdrlen);
732 } else {
733 err = got_deltify(&deltas, &ndeltas,
734 raw->f, raw->hdrlen,
735 raw->size + raw->hdrlen,
736 base->dtab, base_raw->f, base_raw->hdrlen,
737 base_raw->size + base_raw->hdrlen);
739 got_object_raw_close(base_raw);
740 base_raw = NULL;
741 if (err)
742 goto done;
744 size = delta_size(deltas, ndeltas);
745 if (size + 32 < best_size){
746 /*
747 * if we already picked a best delta,
748 * replace it.
749 */
750 best_size = size;
751 free(best_deltas);
752 best_deltas = deltas;
753 best_ndeltas = ndeltas;
754 deltas = NULL;
755 m->nchain = base->nchain + 1;
756 m->prev = base;
757 m->head = base->head;
758 if (m->head == NULL)
759 m->head = base;
760 } else {
761 free(deltas);
762 deltas = NULL;
763 ndeltas = 0;
767 if (best_ndeltas > 0) {
768 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
769 delta_memsize + best_size <= max_delta_memsize) {
770 delta_memsize += best_size;
771 err = encode_delta_in_mem(m, raw, best_deltas,
772 best_ndeltas, best_size, m->prev->size);
773 } else {
774 m->delta_offset = ftello(delta_cache);
775 /*
776 * TODO:
777 * Storing compressed delta data in the delta
778 * cache file would probably be more efficient
779 * than writing uncompressed delta data here
780 * and compressing it while writing the pack
781 * file. This would also allow for reusing
782 * deltas in their compressed form.
783 */
784 err = encode_delta(m, raw, best_deltas,
785 best_ndeltas, m->prev->size, delta_cache);
787 free(best_deltas);
788 best_deltas = NULL;
789 best_ndeltas = 0;
790 if (err)
791 goto done;
794 got_object_raw_close(raw);
795 raw = NULL;
797 done:
798 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
799 got_deltify_free(meta[i]->dtab);
800 meta[i]->dtab = NULL;
802 if (raw)
803 got_object_raw_close(raw);
804 if (base_raw)
805 got_object_raw_close(base_raw);
806 if (outfd != -1 && close(outfd) == -1 && err == NULL)
807 err = got_error_from_errno("close");
808 free(deltas);
809 free(best_deltas);
810 return err;
813 static const struct got_error *
814 search_packidx(int *found, struct got_object_id *id,
815 struct got_repository *repo)
817 const struct got_error *err = NULL;
818 struct got_packidx *packidx = NULL;
819 int idx;
821 *found = 0;
823 err = got_repo_search_packidx(&packidx, &idx, repo, id);
824 if (err == NULL)
825 *found = 1; /* object is already packed */
826 else if (err->code == GOT_ERR_NO_OBJ)
827 err = NULL;
828 return err;
831 static const int obj_types[] = {
832 GOT_OBJ_TYPE_ANY,
833 GOT_OBJ_TYPE_COMMIT,
834 GOT_OBJ_TYPE_TREE,
835 GOT_OBJ_TYPE_BLOB,
836 GOT_OBJ_TYPE_TAG,
837 GOT_OBJ_TYPE_OFFSET_DELTA,
838 GOT_OBJ_TYPE_REF_DELTA
839 };
841 static const struct got_error *
842 add_object(int want_meta, struct got_object_idset *idset,
843 struct got_object_id *id, const char *path, int obj_type,
844 time_t mtime, int loose_obj_only, struct got_repository *repo)
846 const struct got_error *err;
847 struct got_pack_meta *m = NULL;
849 if (loose_obj_only) {
850 int is_packed;
851 err = search_packidx(&is_packed, id, repo);
852 if (err)
853 return err;
854 if (is_packed)
855 return NULL;
858 if (want_meta) {
859 err = alloc_meta(&m, id, path, obj_type, mtime);
860 if (err)
861 return err;
864 return got_object_idset_add(idset, id, m);
867 static const struct got_error *
868 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
869 struct got_object_idset *idset, struct got_object_id *tree_id,
870 const char *dpath, time_t mtime, struct got_repository *repo,
871 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
873 const struct got_error *err;
874 struct got_tree_object *tree;
875 char *p = NULL;
876 int i;
878 err = got_object_open_as_tree(&tree, repo, tree_id);
879 if (err)
880 return err;
882 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
883 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
884 struct got_object_id *id = got_tree_entry_get_id(e);
885 mode_t mode = got_tree_entry_get_mode(e);
887 if (cancel_cb) {
888 err = (*cancel_cb)(cancel_arg);
889 if (err)
890 break;
893 if (got_object_tree_entry_is_submodule(e) ||
894 got_object_idset_contains(idset, id))
895 continue;
897 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
898 got_tree_entry_get_name(e)) == -1) {
899 err = got_error_from_errno("asprintf");
900 break;
903 if (S_ISDIR(mode)) {
904 struct got_object_qid *qid;
905 err = got_object_qid_alloc(&qid, id);
906 if (err)
907 break;
908 STAILQ_INSERT_TAIL(ids, qid, entry);
909 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
910 err = add_object(want_meta, idset, id, p,
911 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo);
912 if (err)
913 break;
915 free(p);
916 p = NULL;
919 got_object_tree_close(tree);
920 free(p);
921 return err;
924 static const struct got_error *
925 load_tree(int want_meta, struct got_object_idset *idset,
926 struct got_object_id *tree_id, const char *dpath, time_t mtime,
927 int loose_obj_only, struct got_repository *repo,
928 got_cancel_cb cancel_cb, void *cancel_arg)
930 const struct got_error *err = NULL;
931 struct got_object_id_queue tree_ids;
932 struct got_object_qid *qid;
934 if (got_object_idset_contains(idset, tree_id))
935 return NULL;
937 err = got_object_qid_alloc(&qid, tree_id);
938 if (err)
939 return err;
941 STAILQ_INIT(&tree_ids);
942 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
944 while (!STAILQ_EMPTY(&tree_ids)) {
945 if (cancel_cb) {
946 err = (*cancel_cb)(cancel_arg);
947 if (err)
948 break;
951 qid = STAILQ_FIRST(&tree_ids);
952 STAILQ_REMOVE_HEAD(&tree_ids, entry);
954 if (got_object_idset_contains(idset, qid->id)) {
955 got_object_qid_free(qid);
956 continue;
959 err = add_object(want_meta, idset, qid->id, dpath,
960 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo);
961 if (err) {
962 got_object_qid_free(qid);
963 break;
966 err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
967 dpath, mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
968 got_object_qid_free(qid);
969 if (err)
970 break;
973 got_object_id_queue_free(&tree_ids);
974 return err;
977 static const struct got_error *
978 load_commit(int want_meta, struct got_object_idset *idset,
979 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
980 got_cancel_cb cancel_cb, void *cancel_arg)
982 const struct got_error *err;
983 struct got_commit_object *commit;
985 if (got_object_idset_contains(idset, id))
986 return NULL;
988 if (loose_obj_only) {
989 int is_packed;
990 err = search_packidx(&is_packed, id, repo);
991 if (err)
992 return err;
993 if (is_packed)
994 return NULL;
997 err = got_object_open_as_commit(&commit, repo, id);
998 if (err)
999 return err;
1001 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1002 got_object_commit_get_committer_time(commit),
1003 loose_obj_only, repo);
1004 if (err)
1005 goto done;
1007 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1008 "", got_object_commit_get_committer_time(commit),
1009 loose_obj_only, repo, cancel_cb, cancel_arg);
1010 done:
1011 got_object_commit_close(commit);
1012 return err;
1015 static const struct got_error *
1016 load_tag(int want_meta, struct got_object_idset *idset,
1017 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1018 got_cancel_cb cancel_cb, void *cancel_arg)
1020 const struct got_error *err;
1021 struct got_tag_object *tag = NULL;
1023 if (got_object_idset_contains(idset, id))
1024 return NULL;
1026 if (loose_obj_only) {
1027 int is_packed;
1028 err = search_packidx(&is_packed, id, repo);
1029 if (err)
1030 return err;
1031 if (is_packed)
1032 return NULL;
1035 err = got_object_open_as_tag(&tag, repo, id);
1036 if (err)
1037 return err;
1039 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1040 got_object_tag_get_tagger_time(tag),
1041 loose_obj_only, repo);
1042 if (err)
1043 goto done;
1045 switch (got_object_tag_get_object_type(tag)) {
1046 case GOT_OBJ_TYPE_COMMIT:
1047 err = load_commit(want_meta, idset,
1048 got_object_tag_get_object_id(tag), repo,
1049 loose_obj_only, cancel_cb, cancel_arg);
1050 break;
1051 case GOT_OBJ_TYPE_TREE:
1052 err = load_tree(want_meta, idset,
1053 got_object_tag_get_object_id(tag), "",
1054 got_object_tag_get_tagger_time(tag),
1055 loose_obj_only, repo, cancel_cb, cancel_arg);
1056 break;
1057 default:
1058 break;
1061 done:
1062 got_object_tag_close(tag);
1063 return err;
1066 enum findtwixt_color {
1067 COLOR_KEEP = 0,
1068 COLOR_DROP,
1069 COLOR_BLANK,
1071 static const int findtwixt_colors[] = {
1072 COLOR_KEEP,
1073 COLOR_DROP,
1074 COLOR_BLANK
1077 static const struct got_error *
1078 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1079 int color, struct got_repository *repo)
1081 const struct got_error *err;
1082 struct got_object_qid *qid;
1084 err = got_object_qid_alloc(&qid, id);
1085 if (err)
1086 return err;
1088 STAILQ_INSERT_TAIL(ids, qid, entry);
1089 qid->data = (void *)&findtwixt_colors[color];
1090 return NULL;
1093 static const struct got_error *
1094 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1095 struct got_object_id *id, struct got_repository *repo,
1096 got_cancel_cb cancel_cb, void *cancel_arg)
1098 const struct got_error *err = NULL;
1099 struct got_commit_object *commit;
1100 const struct got_object_id_queue *parents;
1101 struct got_object_id_queue ids;
1102 struct got_object_qid *qid;
1104 STAILQ_INIT(&ids);
1106 err = got_object_qid_alloc(&qid, id);
1107 if (err)
1108 return err;
1109 STAILQ_INSERT_HEAD(&ids, qid, entry);
1111 while (!STAILQ_EMPTY(&ids)) {
1112 if (cancel_cb) {
1113 err = (*cancel_cb)(cancel_arg);
1114 if (err)
1115 break;
1118 qid = STAILQ_FIRST(&ids);
1119 STAILQ_REMOVE_HEAD(&ids, entry);
1121 if (got_object_idset_contains(drop, qid->id)) {
1122 got_object_qid_free(qid);
1123 continue;
1126 err = got_object_idset_add(drop, qid->id,
1127 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1128 if (err) {
1129 got_object_qid_free(qid);
1130 break;
1133 if (!got_object_idset_contains(keep, qid->id)) {
1134 got_object_qid_free(qid);
1135 continue;
1138 err = got_object_open_as_commit(&commit, repo, qid->id);
1139 got_object_qid_free(qid);
1140 if (err)
1141 break;
1143 parents = got_object_commit_get_parent_ids(commit);
1144 if (parents) {
1145 err = got_object_id_queue_copy(parents, &ids);
1146 if (err) {
1147 got_object_commit_close(commit);
1148 break;
1151 got_object_commit_close(commit);
1154 got_object_id_queue_free(&ids);
1155 return err;
1158 struct append_id_arg {
1159 struct got_object_id **array;
1160 int idx;
1163 static const struct got_error *
1164 append_id(struct got_object_id *id, void *data, void *arg)
1166 struct append_id_arg *a = arg;
1168 a->array[a->idx] = got_object_id_dup(id);
1169 if (a->array[a->idx] == NULL)
1170 return got_error_from_errno("got_object_id_dup");
1172 a->idx++;
1173 return NULL;
1176 static const struct got_error *
1177 findtwixt(struct got_object_id ***res, int *nres,
1178 struct got_object_id **head, int nhead,
1179 struct got_object_id **tail, int ntail,
1180 struct got_repository *repo,
1181 got_cancel_cb cancel_cb, void *cancel_arg)
1183 const struct got_error *err = NULL;
1184 struct got_object_id_queue ids;
1185 struct got_object_idset *keep, *drop;
1186 struct got_object_qid *qid;
1187 int i, ncolor, nkeep, obj_type;
1189 STAILQ_INIT(&ids);
1190 *res = NULL;
1191 *nres = 0;
1193 keep = got_object_idset_alloc();
1194 if (keep == NULL)
1195 return got_error_from_errno("got_object_idset_alloc");
1197 drop = got_object_idset_alloc();
1198 if (drop == NULL) {
1199 err = got_error_from_errno("got_object_idset_alloc");
1200 goto done;
1203 for (i = 0; i < nhead; i++) {
1204 struct got_object_id *id = head[i];
1205 if (id == NULL)
1206 continue;
1207 err = got_object_get_type(&obj_type, repo, id);
1208 if (err)
1209 return err;
1210 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1211 continue;
1212 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1213 if (err)
1214 goto done;
1216 for (i = 0; i < ntail; i++) {
1217 struct got_object_id *id = tail[i];
1218 if (id == NULL)
1219 continue;
1220 err = got_object_get_type(&obj_type, repo, id);
1221 if (err)
1222 return err;
1223 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1224 continue;
1225 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1226 if (err)
1227 goto done;
1230 while (!STAILQ_EMPTY(&ids)) {
1231 int qcolor;
1232 qid = STAILQ_FIRST(&ids);
1233 qcolor = *((int *)qid->data);
1235 if (got_object_idset_contains(drop, qid->id))
1236 ncolor = COLOR_DROP;
1237 else if (got_object_idset_contains(keep, qid->id))
1238 ncolor = COLOR_KEEP;
1239 else
1240 ncolor = COLOR_BLANK;
1242 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1243 qcolor == COLOR_KEEP)) {
1244 STAILQ_REMOVE_HEAD(&ids, entry);
1245 got_object_qid_free(qid);
1246 continue;
1249 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1250 err = drop_commit(keep, drop, qid->id, repo,
1251 cancel_cb, cancel_arg);
1252 if (err)
1253 goto done;
1254 } else if (ncolor == COLOR_BLANK) {
1255 struct got_commit_object *commit;
1256 struct got_object_id *id;
1257 const struct got_object_id_queue *parents;
1258 struct got_object_qid *pid;
1260 id = got_object_id_dup(qid->id);
1261 if (id == NULL) {
1262 err = got_error_from_errno("got_object_id_dup");
1263 goto done;
1265 if (qcolor == COLOR_KEEP)
1266 err = got_object_idset_add(keep, id,
1267 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1268 else
1269 err = got_object_idset_add(drop, id,
1270 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1271 if (err) {
1272 free(id);
1273 goto done;
1276 err = got_object_open_as_commit(&commit, repo, id);
1277 if (err) {
1278 free(id);
1279 goto done;
1281 parents = got_object_commit_get_parent_ids(commit);
1282 if (parents) {
1283 STAILQ_FOREACH(pid, parents, entry) {
1284 err = queue_commit_id(&ids, pid->id,
1285 qcolor, repo);
1286 if (err) {
1287 free(id);
1288 goto done;
1292 got_object_commit_close(commit);
1293 commit = NULL;
1294 } else {
1295 /* should not happen */
1296 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1297 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1298 goto done;
1301 STAILQ_REMOVE_HEAD(&ids, entry);
1302 got_object_qid_free(qid);
1305 nkeep = got_object_idset_num_elements(keep);
1306 if (nkeep > 0) {
1307 struct append_id_arg arg;
1308 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1309 if (arg.array == NULL) {
1310 err = got_error_from_errno("calloc");
1311 goto done;
1313 arg.idx = 0;
1314 err = got_object_idset_for_each(keep, append_id, &arg);
1315 if (err) {
1316 free(arg.array);
1317 goto done;
1319 *res = arg.array;
1320 *nres = nkeep;
1322 done:
1323 got_object_idset_free(keep);
1324 got_object_idset_free(drop);
1325 got_object_id_queue_free(&ids);
1326 return err;
1329 static const struct got_error *
1330 load_object_ids(struct got_object_idset *idset,
1331 struct got_object_id **theirs, int ntheirs,
1332 struct got_object_id **ours, int nours, struct got_repository *repo,
1333 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
1335 const struct got_error *err = NULL;
1336 struct got_object_id **ids = NULL;
1337 int i, nobj = 0, obj_type;
1339 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1340 cancel_cb, cancel_arg);
1341 if (err || nobj == 0)
1342 goto done;
1344 for (i = 0; i < ntheirs; i++) {
1345 struct got_object_id *id = theirs[i];
1346 if (id == NULL)
1347 continue;
1348 err = got_object_get_type(&obj_type, repo, id);
1349 if (err)
1350 return err;
1351 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1352 continue;
1353 err = load_commit(0, idset, id, repo,
1354 loose_obj_only, cancel_cb, cancel_arg);
1355 if (err)
1356 goto done;
1359 for (i = 0; i < ntheirs; i++) {
1360 struct got_object_id *id = theirs[i];
1361 struct got_pack_meta *m;
1362 if (id == NULL)
1363 continue;
1364 m = got_object_idset_get(idset, id);
1365 if (m == NULL) {
1366 err = got_object_get_type(&obj_type, repo, id);
1367 if (err)
1368 goto done;
1369 } else
1370 obj_type = m->obj_type;
1371 if (obj_type != GOT_OBJ_TYPE_TAG)
1372 continue;
1373 err = load_tag(0, idset, id, repo,
1374 loose_obj_only, cancel_cb, cancel_arg);
1375 if (err)
1376 goto done;
1379 for (i = 0; i < nobj; i++) {
1380 err = load_commit(1, idset, ids[i], repo,
1381 loose_obj_only, cancel_cb, cancel_arg);
1382 if (err)
1383 goto done;
1386 for (i = 0; i < nours; i++) {
1387 struct got_object_id *id = ours[i];
1388 struct got_pack_meta *m;
1389 if (id == NULL)
1390 continue;
1391 m = got_object_idset_get(idset, id);
1392 if (m == NULL) {
1393 err = got_object_get_type(&obj_type, repo, id);
1394 if (err)
1395 goto done;
1396 } else
1397 obj_type = m->obj_type;
1398 if (obj_type != GOT_OBJ_TYPE_TAG)
1399 continue;
1400 err = load_tag(1, idset, id, repo,
1401 loose_obj_only, cancel_cb, cancel_arg);
1402 if (err)
1403 goto done;
1405 done:
1406 for (i = 0; i < nobj; i++) {
1407 free(ids[i]);
1409 free(ids);
1410 return err;
1413 const struct got_error *
1414 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1416 size_t n;
1418 SHA1Update(ctx, buf, len);
1419 n = fwrite(buf, 1, len, f);
1420 if (n != len)
1421 return got_ferror(f, GOT_ERR_IO);
1422 return NULL;
1425 static void
1426 putbe32(char *b, uint32_t n)
1428 b[0] = n >> 24;
1429 b[1] = n >> 16;
1430 b[2] = n >> 8;
1431 b[3] = n >> 0;
1434 static int
1435 write_order_cmp(const void *pa, const void *pb)
1437 struct got_pack_meta *a, *b, *ahd, *bhd;
1439 a = *(struct got_pack_meta **)pa;
1440 b = *(struct got_pack_meta **)pb;
1441 ahd = (a->head == NULL) ? a : a->head;
1442 bhd = (b->head == NULL) ? b : b->head;
1443 if (ahd->mtime != bhd->mtime)
1444 return bhd->mtime - ahd->mtime;
1445 if (ahd != bhd)
1446 return (uintptr_t)bhd - (uintptr_t)ahd;
1447 if (a->nchain != b->nchain)
1448 return a->nchain - b->nchain;
1449 return a->mtime - b->mtime;
1452 static int
1453 reuse_write_order_cmp(const void *pa, const void *pb)
1455 struct got_pack_meta *a, *b;
1457 a = *(struct got_pack_meta **)pa;
1458 b = *(struct got_pack_meta **)pb;
1460 if (a->reused_delta_offset < b->reused_delta_offset)
1461 return -1;
1462 if (a->reused_delta_offset > b->reused_delta_offset)
1463 return 1;
1464 return 0;
1467 static const struct got_error *
1468 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1470 size_t i;
1472 *hdrlen = 0;
1474 hdr[0] = obj_type << 4;
1475 hdr[0] |= len & 0xf;
1476 len >>= 4;
1477 for (i = 1; len != 0; i++){
1478 if (i >= bufsize)
1479 return got_error(GOT_ERR_NO_SPACE);
1480 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1481 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1482 len >>= GOT_DELTA_SIZE_SHIFT;
1485 *hdrlen = i;
1486 return NULL;
1489 static int
1490 packoff(char *hdr, off_t off)
1492 int i, j;
1493 char rbuf[8];
1495 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1496 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1497 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1498 GOT_DELTA_SIZE_MORE;
1501 j = 0;
1502 while (i > 0)
1503 hdr[j++] = rbuf[--i];
1504 return j;
1507 static const struct got_error *
1508 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1509 struct got_pack_meta *m)
1511 const struct got_error *err;
1512 char buf[32];
1513 int nh;
1515 if (m->prev->off != 0) {
1516 err = packhdr(&nh, buf, sizeof(buf),
1517 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1518 if (err)
1519 return err;
1520 nh += packoff(buf + nh, m->off - m->prev->off);
1521 err = hwrite(packfile, buf, nh, ctx);
1522 if (err)
1523 return err;
1524 *packfile_size += nh;
1525 } else {
1526 err = packhdr(&nh, buf, sizeof(buf),
1527 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1528 if (err)
1529 return err;
1530 err = hwrite(packfile, buf, nh, ctx);
1531 if (err)
1532 return err;
1533 *packfile_size += nh;
1534 err = hwrite(packfile, m->prev->id.sha1,
1535 sizeof(m->prev->id.sha1), ctx);
1536 if (err)
1537 return err;
1538 *packfile_size += sizeof(m->prev->id.sha1);
1541 return NULL;
1544 static const struct got_error *
1545 write_packed_object(off_t *packfile_size, FILE *packfile,
1546 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1547 SHA1_CTX *ctx, struct got_repository *repo)
1549 const struct got_error *err = NULL;
1550 struct got_deflate_checksum csum;
1551 char buf[32];
1552 int nh;
1553 struct got_raw_object *raw = NULL;
1554 off_t outlen;
1556 csum.output_sha1 = ctx;
1557 csum.output_crc = NULL;
1559 m->off = ftello(packfile);
1560 if (m->delta_len == 0) {
1561 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1562 if (err)
1563 goto done;
1564 err = packhdr(&nh, buf, sizeof(buf),
1565 m->obj_type, raw->size);
1566 if (err)
1567 goto done;
1568 err = hwrite(packfile, buf, nh, ctx);
1569 if (err)
1570 goto done;
1571 *packfile_size += nh;
1572 if (raw->f == NULL) {
1573 err = got_deflate_to_file_mmap(&outlen,
1574 raw->data + raw->hdrlen, 0, raw->size,
1575 packfile, &csum);
1576 if (err)
1577 goto done;
1578 } else {
1579 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1580 == -1) {
1581 err = got_error_from_errno("fseeko");
1582 goto done;
1584 err = got_deflate_to_file(&outlen, raw->f,
1585 raw->size, packfile, &csum);
1586 if (err)
1587 goto done;
1589 *packfile_size += outlen;
1590 got_object_raw_close(raw);
1591 raw = NULL;
1592 } else if (m->delta_buf) {
1593 err = deltahdr(packfile_size, ctx, packfile, m);
1594 if (err)
1595 goto done;
1596 err = got_deflate_to_file_mmap(&outlen,
1597 m->delta_buf, 0, m->delta_len, packfile, &csum);
1598 if (err)
1599 goto done;
1600 *packfile_size += outlen;
1601 free(m->delta_buf);
1602 m->delta_buf = NULL;
1603 } else {
1604 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1605 == -1) {
1606 err = got_error_from_errno("fseeko");
1607 goto done;
1609 err = deltahdr(packfile_size, ctx, packfile, m);
1610 if (err)
1611 goto done;
1612 err = got_deflate_to_file(&outlen, delta_cache,
1613 m->delta_len, packfile, &csum);
1614 if (err)
1615 goto done;
1616 *packfile_size += outlen;
1618 done:
1619 if (raw)
1620 got_object_raw_close(raw);
1621 return err;
1624 static const struct got_error *
1625 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1626 struct got_pack_meta **deltify, int ndeltify,
1627 struct got_pack_meta **reuse, int nreuse,
1628 int nours, struct got_repository *repo,
1629 got_pack_progress_cb progress_cb, void *progress_arg,
1630 struct got_ratelimit *rl,
1631 got_cancel_cb cancel_cb, void *cancel_arg)
1633 const struct got_error *err = NULL;
1634 int i;
1635 SHA1_CTX ctx;
1636 struct got_pack_meta *m;
1637 char buf[32];
1638 size_t n;
1639 off_t packfile_size = 0;
1640 int outfd = -1;
1642 SHA1Init(&ctx);
1644 err = hwrite(packfile, "PACK", 4, &ctx);
1645 if (err)
1646 return err;
1647 putbe32(buf, GOT_PACKFILE_VERSION);
1648 err = hwrite(packfile, buf, 4, &ctx);
1649 if (err)
1650 goto done;
1651 putbe32(buf, ndeltify + nreuse);
1652 err = hwrite(packfile, buf, 4, &ctx);
1653 if (err)
1654 goto done;
1656 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1657 write_order_cmp);
1658 for (i = 0; i < ndeltify; i++) {
1659 err = report_progress(progress_cb, progress_arg, rl,
1660 packfile_size, nours, ndeltify + nreuse,
1661 ndeltify + nreuse, i);
1662 if (err)
1663 goto done;
1664 m = deltify[i];
1665 err = write_packed_object(&packfile_size, packfile,
1666 delta_cache, m, &outfd, &ctx, repo);
1667 if (err)
1668 goto done;
1671 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1672 reuse_write_order_cmp);
1673 for (i = 0; i < nreuse; i++) {
1674 err = report_progress(progress_cb, progress_arg, rl,
1675 packfile_size, nours, ndeltify + nreuse,
1676 ndeltify + nreuse, ndeltify + i);
1677 if (err)
1678 goto done;
1679 m = reuse[i];
1680 err = write_packed_object(&packfile_size, packfile,
1681 delta_cache, m, &outfd, &ctx, repo);
1682 if (err)
1683 goto done;
1686 SHA1Final(pack_sha1, &ctx);
1687 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1688 if (n != SHA1_DIGEST_LENGTH)
1689 err = got_ferror(packfile, GOT_ERR_IO);
1690 packfile_size += SHA1_DIGEST_LENGTH;
1691 packfile_size += sizeof(struct got_packfile_hdr);
1692 if (progress_cb) {
1693 err = progress_cb(progress_arg, packfile_size, nours,
1694 ndeltify + nreuse, ndeltify + nreuse,
1695 ndeltify + nreuse);
1696 if (err)
1697 goto done;
1699 done:
1700 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1701 err = got_error_from_errno("close");
1702 return err;
1705 static const struct got_error *
1706 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1708 struct got_object_idset *idset = arg;
1710 if (got_object_idset_get_element_data(entry) == NULL)
1711 got_object_idset_remove_element(idset, entry);
1713 return NULL;
1716 static const struct got_error *
1717 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1719 struct got_object_idset *idset = arg;
1720 struct got_pack_meta *m;
1722 m = got_object_idset_get_element_data(entry);
1723 if (m->have_reused_delta)
1724 got_object_idset_remove_element(idset, entry);
1726 return NULL;
1729 static const struct got_error *
1730 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1732 struct got_pack_meta *m = data;
1733 struct got_pack_metavec *v = arg;
1735 return add_meta(m, v);
1738 const struct got_error *
1739 got_pack_create(uint8_t *packsha1, FILE *packfile,
1740 struct got_object_id **theirs, int ntheirs,
1741 struct got_object_id **ours, int nours,
1742 struct got_repository *repo, int loose_obj_only, int allow_empty,
1743 got_pack_progress_cb progress_cb, void *progress_arg,
1744 got_cancel_cb cancel_cb, void *cancel_arg)
1746 const struct got_error *err;
1747 int delta_cache_fd = -1;
1748 FILE *delta_cache = NULL;
1749 struct got_object_idset *idset;
1750 struct got_ratelimit rl;
1751 struct got_pack_metavec deltify, reuse;
1753 memset(&deltify, 0, sizeof(deltify));
1754 memset(&reuse, 0, sizeof(reuse));
1756 got_ratelimit_init(&rl, 0, 500);
1758 idset = got_object_idset_alloc();
1759 if (idset == NULL)
1760 return got_error_from_errno("got_object_idset_alloc");
1762 err = load_object_ids(idset, theirs, ntheirs, ours, nours,
1763 repo, loose_obj_only, cancel_cb, cancel_arg);
1764 if (err)
1765 return err;
1767 err = got_object_idset_for_each_element(idset,
1768 remove_unused_object, idset);
1769 if (err)
1770 goto done;
1772 if (progress_cb) {
1773 err = progress_cb(progress_arg, 0L, nours,
1774 got_object_idset_num_elements(idset), 0, 0);
1775 if (err)
1776 goto done;
1779 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1780 err = got_error(GOT_ERR_CANNOT_PACK);
1781 goto done;
1784 delta_cache_fd = got_opentempfd();
1785 if (delta_cache_fd == -1) {
1786 err = got_error_from_errno("got_opentemp");
1787 goto done;
1790 reuse.metasz = 64;
1791 reuse.meta = calloc(reuse.metasz,
1792 sizeof(struct got_pack_meta *));
1793 if (reuse.meta == NULL) {
1794 err = got_error_from_errno("calloc");
1795 goto done;
1798 err = search_deltas(&reuse, idset, delta_cache_fd, nours, repo,
1799 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1800 if (err)
1801 goto done;
1802 if (reuse.nmeta > 0) {
1803 err = got_object_idset_for_each_element(idset,
1804 remove_reused_object, idset);
1805 if (err)
1806 goto done;
1809 delta_cache = fdopen(delta_cache_fd, "a+");
1810 if (delta_cache == NULL) {
1811 err = got_error_from_errno("fdopen");
1812 goto done;
1814 delta_cache_fd = -1;
1816 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1817 err = got_error_from_errno("fseeko");
1818 goto done;
1821 deltify.meta = calloc(got_object_idset_num_elements(idset),
1822 sizeof(struct got_pack_meta *));
1823 if (deltify.meta == NULL) {
1824 err = got_error_from_errno("calloc");
1825 goto done;
1827 deltify.metasz = got_object_idset_num_elements(idset);
1829 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1830 if (err)
1831 goto done;
1832 if (deltify.nmeta > 0) {
1833 err = pick_deltas(deltify.meta, deltify.nmeta, nours,
1834 reuse.nmeta, delta_cache, repo,
1835 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1836 if (err)
1837 goto done;
1838 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1839 err = got_error_from_errno("fseeko");
1840 goto done;
1844 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1845 deltify.nmeta, reuse.meta, reuse.nmeta, nours, repo,
1846 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1847 if (err)
1848 goto done;
1849 done:
1850 free_nmeta(deltify.meta, deltify.nmeta);
1851 free_nmeta(reuse.meta, reuse.nmeta);
1852 got_object_idset_free(idset);
1853 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1854 err = got_error_from_errno("close");
1855 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1856 err = got_error_from_errno("fclose");
1857 return err;