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;
492 struct got_pathlist_head packidx_paths;
493 struct got_pathlist_entry *pe;
494 const char *best_packidx_path = NULL;
495 int nobj_max = 0;
497 TAILQ_INIT(&packidx_paths);
498 *best_packidx = NULL;
500 err = got_repo_list_packidx(&packidx_paths, repo);
501 if (err)
502 return err;
504 TAILQ_FOREACH(pe, &packidx_paths, entry) {
505 const char *path_packidx = pe->path;
506 struct got_packidx *packidx;
507 int nobj;
509 err = got_repo_get_packidx(&packidx, path_packidx, repo);
510 if (err)
511 break;
513 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
514 if (nobj > nobj_max) {
515 best_packidx_path = path_packidx;
516 nobj_max = nobj;
520 if (best_packidx_path) {
521 err = got_repo_get_packidx(best_packidx, best_packidx_path,
522 repo);
525 TAILQ_FOREACH(pe, &packidx_paths, entry)
526 free((void *)pe->path);
527 got_pathlist_free(&packidx_paths);
528 return err;
531 struct search_deltas_arg {
532 struct got_packidx *packidx;
533 struct got_pack *pack;
534 struct got_object_idset *idset;
535 struct got_pack_metavec *v;
536 int delta_cache_fd;
537 struct got_repository *repo;
538 got_pack_progress_cb progress_cb;
539 void *progress_arg;
540 struct got_ratelimit *rl;
541 got_cancel_cb cancel_cb;
542 void *cancel_arg;
543 int ncommits;
544 };
546 static const struct got_error *
547 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
549 const struct got_error *err;
550 struct got_pack_meta *m = data;
551 struct search_deltas_arg *a = arg;
552 int obj_idx;
553 struct got_object *obj = NULL;
555 if (a->cancel_cb) {
556 err = (*a->cancel_cb)(a->cancel_arg);
557 if (err)
558 return err;
561 if (!got_repo_check_packidx_bloom_filter(a->repo,
562 a->packidx->path_packidx, id))
563 return NULL;
565 obj_idx = got_packidx_get_object_idx(a->packidx, id);
566 if (obj_idx == -1)
567 return NULL;
569 /* TODO:
570 * Opening and closing an object just to check its flags
571 * is a bit expensive. We could have an imsg which requests
572 * plain type/size information for an object without doing
573 * work such as traversing the object's entire delta chain
574 * to find the base object type, and other such info which
575 * we don't really need here.
576 */
577 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
578 a->packidx, obj_idx, a->repo);
579 if (err)
580 return err;
582 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
583 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
584 a->delta_cache_fd, a->repo);
585 if (err)
586 goto done;
587 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
588 0L, a->ncommits, got_object_idset_num_elements(a->idset),
589 a->v->nmeta, 0);
591 done:
592 got_object_close(obj);
593 return err;
596 static const struct got_error *
597 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
598 int delta_cache_fd, int ncommits, struct got_repository *repo,
599 got_pack_progress_cb progress_cb, void *progress_arg,
600 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
602 const struct got_error *err = NULL;
603 char *path_packfile = NULL;
604 struct got_packidx *packidx;
605 struct got_pack *pack;
606 struct search_deltas_arg sda;
608 err = find_pack_for_reuse(&packidx, repo);
609 if (err)
610 return err;
612 if (packidx == NULL)
613 return NULL;
615 err = got_packidx_get_packfile_path(&path_packfile,
616 packidx->path_packidx);
617 if (err)
618 return err;
620 pack = got_repo_get_cached_pack(repo, path_packfile);
621 if (pack == NULL) {
622 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
623 if (err)
624 goto done;
627 sda.packidx = packidx;
628 sda.pack = pack;
629 sda.idset = idset;
630 sda.v = v;
631 sda.delta_cache_fd = delta_cache_fd;
632 sda.repo = repo;
633 sda.progress_cb = progress_cb;
634 sda.progress_arg = progress_arg;
635 sda.rl = rl;
636 sda.cancel_cb = cancel_cb;
637 sda.cancel_arg = cancel_arg;
638 sda.ncommits = ncommits;
639 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
640 done:
641 free(path_packfile);
642 return err;
645 static const struct got_error *
646 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncommits,
647 int nreused, FILE *delta_cache, struct got_repository *repo,
648 got_pack_progress_cb progress_cb, void *progress_arg,
649 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
651 const struct got_error *err = NULL;
652 struct got_pack_meta *m = NULL, *base = NULL;
653 struct got_raw_object *raw = NULL, *base_raw = NULL;
654 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
655 int i, j, ndeltas, best_ndeltas;
656 off_t size, best_size;
657 const int max_base_candidates = 3;
658 size_t delta_memsize = 0;
659 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
660 int outfd = -1;
662 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
663 for (i = 0; i < nmeta; i++) {
664 if (cancel_cb) {
665 err = (*cancel_cb)(cancel_arg);
666 if (err)
667 break;
669 err = report_progress(progress_cb, progress_arg, rl,
670 0L, ncommits, nreused + nmeta, 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, got_cancel_cb cancel_cb, void *cancel_arg)
882 const struct got_error *err;
883 struct got_tree_object *tree;
884 char *p = NULL;
885 int i;
887 err = got_object_open_as_tree(&tree, repo, tree_id);
888 if (err)
889 return err;
891 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
892 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
893 struct got_object_id *id = got_tree_entry_get_id(e);
894 mode_t mode = got_tree_entry_get_mode(e);
896 if (cancel_cb) {
897 err = (*cancel_cb)(cancel_arg);
898 if (err)
899 break;
902 if (got_object_tree_entry_is_submodule(e) ||
903 got_object_idset_contains(idset, id))
904 continue;
906 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
907 got_tree_entry_get_name(e)) == -1) {
908 err = got_error_from_errno("asprintf");
909 break;
912 if (S_ISDIR(mode)) {
913 struct got_object_qid *qid;
914 err = got_object_qid_alloc(&qid, id);
915 if (err)
916 break;
917 STAILQ_INSERT_TAIL(ids, qid, entry);
918 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
919 err = add_object(want_meta, idset, id, p,
920 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo);
921 if (err)
922 break;
924 free(p);
925 p = NULL;
928 got_object_tree_close(tree);
929 free(p);
930 return err;
933 static const struct got_error *
934 load_tree(int want_meta, struct got_object_idset *idset,
935 struct got_object_id *tree_id, const char *dpath, time_t mtime,
936 int loose_obj_only, struct got_repository *repo,
937 got_cancel_cb cancel_cb, void *cancel_arg)
939 const struct got_error *err = NULL;
940 struct got_object_id_queue tree_ids;
941 struct got_object_qid *qid;
943 if (got_object_idset_contains(idset, tree_id))
944 return NULL;
946 err = got_object_qid_alloc(&qid, tree_id);
947 if (err)
948 return err;
950 STAILQ_INIT(&tree_ids);
951 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
953 while (!STAILQ_EMPTY(&tree_ids)) {
954 if (cancel_cb) {
955 err = (*cancel_cb)(cancel_arg);
956 if (err)
957 break;
960 qid = STAILQ_FIRST(&tree_ids);
961 STAILQ_REMOVE_HEAD(&tree_ids, entry);
963 if (got_object_idset_contains(idset, qid->id)) {
964 got_object_qid_free(qid);
965 continue;
968 err = add_object(want_meta, idset, qid->id, dpath,
969 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo);
970 if (err) {
971 got_object_qid_free(qid);
972 break;
975 err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
976 dpath, mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
977 got_object_qid_free(qid);
978 if (err)
979 break;
982 got_object_id_queue_free(&tree_ids);
983 return err;
986 static const struct got_error *
987 load_commit(int want_meta, struct got_object_idset *idset,
988 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
989 got_cancel_cb cancel_cb, void *cancel_arg)
991 const struct got_error *err;
992 struct got_commit_object *commit;
994 if (got_object_idset_contains(idset, id))
995 return NULL;
997 if (loose_obj_only) {
998 int is_packed;
999 err = search_packidx(&is_packed, id, repo);
1000 if (err)
1001 return err;
1002 if (is_packed)
1003 return NULL;
1006 err = got_object_open_as_commit(&commit, repo, id);
1007 if (err)
1008 return err;
1010 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1011 got_object_commit_get_committer_time(commit),
1012 loose_obj_only, repo);
1013 if (err)
1014 goto done;
1016 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1017 "", got_object_commit_get_committer_time(commit),
1018 loose_obj_only, repo, cancel_cb, cancel_arg);
1019 done:
1020 got_object_commit_close(commit);
1021 return err;
1024 static const struct got_error *
1025 load_tag(int want_meta, struct got_object_idset *idset,
1026 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1027 got_cancel_cb cancel_cb, void *cancel_arg)
1029 const struct got_error *err;
1030 struct got_tag_object *tag = NULL;
1032 if (got_object_idset_contains(idset, id))
1033 return NULL;
1035 if (loose_obj_only) {
1036 int is_packed;
1037 err = search_packidx(&is_packed, id, repo);
1038 if (err)
1039 return err;
1040 if (is_packed)
1041 return NULL;
1044 err = got_object_open_as_tag(&tag, repo, id);
1045 if (err)
1046 return err;
1048 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1049 got_object_tag_get_tagger_time(tag),
1050 loose_obj_only, repo);
1051 if (err)
1052 goto done;
1054 switch (got_object_tag_get_object_type(tag)) {
1055 case GOT_OBJ_TYPE_COMMIT:
1056 err = load_commit(want_meta, idset,
1057 got_object_tag_get_object_id(tag), repo,
1058 loose_obj_only, cancel_cb, cancel_arg);
1059 break;
1060 case GOT_OBJ_TYPE_TREE:
1061 err = load_tree(want_meta, idset,
1062 got_object_tag_get_object_id(tag), "",
1063 got_object_tag_get_tagger_time(tag),
1064 loose_obj_only, repo, cancel_cb, cancel_arg);
1065 break;
1066 default:
1067 break;
1070 done:
1071 got_object_tag_close(tag);
1072 return err;
1075 enum findtwixt_color {
1076 COLOR_KEEP = 0,
1077 COLOR_DROP,
1078 COLOR_BLANK,
1080 static const int findtwixt_colors[] = {
1081 COLOR_KEEP,
1082 COLOR_DROP,
1083 COLOR_BLANK
1086 static const struct got_error *
1087 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1088 int color, struct got_repository *repo)
1090 const struct got_error *err;
1091 struct got_object_qid *qid;
1093 err = got_object_qid_alloc(&qid, id);
1094 if (err)
1095 return err;
1097 STAILQ_INSERT_TAIL(ids, qid, entry);
1098 qid->data = (void *)&findtwixt_colors[color];
1099 return NULL;
1102 static const struct got_error *
1103 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1104 struct got_object_id *id, struct got_repository *repo,
1105 got_cancel_cb cancel_cb, void *cancel_arg)
1107 const struct got_error *err = NULL;
1108 struct got_commit_object *commit;
1109 const struct got_object_id_queue *parents;
1110 struct got_object_id_queue ids;
1111 struct got_object_qid *qid;
1113 STAILQ_INIT(&ids);
1115 err = got_object_qid_alloc(&qid, id);
1116 if (err)
1117 return err;
1118 STAILQ_INSERT_HEAD(&ids, qid, entry);
1120 while (!STAILQ_EMPTY(&ids)) {
1121 if (cancel_cb) {
1122 err = (*cancel_cb)(cancel_arg);
1123 if (err)
1124 break;
1127 qid = STAILQ_FIRST(&ids);
1128 STAILQ_REMOVE_HEAD(&ids, entry);
1130 if (got_object_idset_contains(drop, qid->id)) {
1131 got_object_qid_free(qid);
1132 continue;
1135 err = got_object_idset_add(drop, qid->id,
1136 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1137 if (err) {
1138 got_object_qid_free(qid);
1139 break;
1142 if (!got_object_idset_contains(keep, qid->id)) {
1143 got_object_qid_free(qid);
1144 continue;
1147 err = got_object_open_as_commit(&commit, repo, qid->id);
1148 got_object_qid_free(qid);
1149 if (err)
1150 break;
1152 parents = got_object_commit_get_parent_ids(commit);
1153 if (parents) {
1154 err = got_object_id_queue_copy(parents, &ids);
1155 if (err) {
1156 got_object_commit_close(commit);
1157 break;
1160 got_object_commit_close(commit);
1163 got_object_id_queue_free(&ids);
1164 return err;
1167 struct append_id_arg {
1168 struct got_object_id **array;
1169 int idx;
1172 static const struct got_error *
1173 append_id(struct got_object_id *id, void *data, void *arg)
1175 struct append_id_arg *a = arg;
1177 a->array[a->idx] = got_object_id_dup(id);
1178 if (a->array[a->idx] == NULL)
1179 return got_error_from_errno("got_object_id_dup");
1181 a->idx++;
1182 return NULL;
1185 static const struct got_error *
1186 findtwixt(struct got_object_id ***res, int *nres,
1187 struct got_object_id **head, int nhead,
1188 struct got_object_id **tail, int ntail,
1189 struct got_repository *repo,
1190 got_cancel_cb cancel_cb, void *cancel_arg)
1192 const struct got_error *err = NULL;
1193 struct got_object_id_queue ids;
1194 struct got_object_idset *keep, *drop;
1195 struct got_object_qid *qid;
1196 int i, ncolor, nkeep, obj_type;
1198 STAILQ_INIT(&ids);
1199 *res = NULL;
1200 *nres = 0;
1202 keep = got_object_idset_alloc();
1203 if (keep == NULL)
1204 return got_error_from_errno("got_object_idset_alloc");
1206 drop = got_object_idset_alloc();
1207 if (drop == NULL) {
1208 err = got_error_from_errno("got_object_idset_alloc");
1209 goto done;
1212 for (i = 0; i < nhead; i++) {
1213 struct got_object_id *id = head[i];
1214 if (id == NULL)
1215 continue;
1216 err = got_object_get_type(&obj_type, repo, id);
1217 if (err)
1218 return err;
1219 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1220 continue;
1221 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1222 if (err)
1223 goto done;
1225 for (i = 0; i < ntail; i++) {
1226 struct got_object_id *id = tail[i];
1227 if (id == NULL)
1228 continue;
1229 err = got_object_get_type(&obj_type, repo, id);
1230 if (err)
1231 return err;
1232 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1233 continue;
1234 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1235 if (err)
1236 goto done;
1239 while (!STAILQ_EMPTY(&ids)) {
1240 int qcolor;
1241 qid = STAILQ_FIRST(&ids);
1242 qcolor = *((int *)qid->data);
1244 if (got_object_idset_contains(drop, qid->id))
1245 ncolor = COLOR_DROP;
1246 else if (got_object_idset_contains(keep, qid->id))
1247 ncolor = COLOR_KEEP;
1248 else
1249 ncolor = COLOR_BLANK;
1251 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1252 qcolor == COLOR_KEEP)) {
1253 STAILQ_REMOVE_HEAD(&ids, entry);
1254 got_object_qid_free(qid);
1255 continue;
1258 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1259 err = drop_commit(keep, drop, qid->id, repo,
1260 cancel_cb, cancel_arg);
1261 if (err)
1262 goto done;
1263 } else if (ncolor == COLOR_BLANK) {
1264 struct got_commit_object *commit;
1265 struct got_object_id *id;
1266 const struct got_object_id_queue *parents;
1267 struct got_object_qid *pid;
1269 id = got_object_id_dup(qid->id);
1270 if (id == NULL) {
1271 err = got_error_from_errno("got_object_id_dup");
1272 goto done;
1274 if (qcolor == COLOR_KEEP)
1275 err = got_object_idset_add(keep, id,
1276 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1277 else
1278 err = got_object_idset_add(drop, id,
1279 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1280 if (err) {
1281 free(id);
1282 goto done;
1285 err = got_object_open_as_commit(&commit, repo, id);
1286 if (err) {
1287 free(id);
1288 goto done;
1290 parents = got_object_commit_get_parent_ids(commit);
1291 if (parents) {
1292 STAILQ_FOREACH(pid, parents, entry) {
1293 err = queue_commit_id(&ids, pid->id,
1294 qcolor, repo);
1295 if (err) {
1296 free(id);
1297 goto done;
1301 got_object_commit_close(commit);
1302 commit = NULL;
1303 } else {
1304 /* should not happen */
1305 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1306 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1307 goto done;
1310 STAILQ_REMOVE_HEAD(&ids, entry);
1311 got_object_qid_free(qid);
1314 nkeep = got_object_idset_num_elements(keep);
1315 if (nkeep > 0) {
1316 struct append_id_arg arg;
1317 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1318 if (arg.array == NULL) {
1319 err = got_error_from_errno("calloc");
1320 goto done;
1322 arg.idx = 0;
1323 err = got_object_idset_for_each(keep, append_id, &arg);
1324 if (err) {
1325 free(arg.array);
1326 goto done;
1328 *res = arg.array;
1329 *nres = nkeep;
1331 done:
1332 got_object_idset_free(keep);
1333 got_object_idset_free(drop);
1334 got_object_id_queue_free(&ids);
1335 return err;
1338 static const struct got_error *
1339 load_object_ids(struct got_object_idset *idset,
1340 struct got_object_id **theirs, int ntheirs,
1341 struct got_object_id **ours, int nours, struct got_repository *repo,
1342 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
1344 const struct got_error *err = NULL;
1345 struct got_object_id **ids = NULL;
1346 int i, nobj = 0, obj_type;
1348 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1349 cancel_cb, cancel_arg);
1350 if (err || nobj == 0)
1351 goto done;
1353 for (i = 0; i < ntheirs; i++) {
1354 struct got_object_id *id = theirs[i];
1355 if (id == NULL)
1356 continue;
1357 err = got_object_get_type(&obj_type, repo, id);
1358 if (err)
1359 return err;
1360 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1361 continue;
1362 err = load_commit(0, idset, id, repo,
1363 loose_obj_only, cancel_cb, cancel_arg);
1364 if (err)
1365 goto done;
1368 for (i = 0; i < ntheirs; i++) {
1369 struct got_object_id *id = theirs[i];
1370 struct got_pack_meta *m;
1371 if (id == NULL)
1372 continue;
1373 m = got_object_idset_get(idset, id);
1374 if (m == NULL) {
1375 err = got_object_get_type(&obj_type, repo, id);
1376 if (err)
1377 goto done;
1378 } else
1379 obj_type = m->obj_type;
1380 if (obj_type != GOT_OBJ_TYPE_TAG)
1381 continue;
1382 err = load_tag(0, idset, id, repo,
1383 loose_obj_only, cancel_cb, cancel_arg);
1384 if (err)
1385 goto done;
1388 for (i = 0; i < nobj; i++) {
1389 err = load_commit(1, idset, ids[i], repo,
1390 loose_obj_only, cancel_cb, cancel_arg);
1391 if (err)
1392 goto done;
1395 for (i = 0; i < nours; i++) {
1396 struct got_object_id *id = ours[i];
1397 struct got_pack_meta *m;
1398 if (id == NULL)
1399 continue;
1400 m = got_object_idset_get(idset, id);
1401 if (m == NULL) {
1402 err = got_object_get_type(&obj_type, repo, id);
1403 if (err)
1404 goto done;
1405 } else
1406 obj_type = m->obj_type;
1407 if (obj_type != GOT_OBJ_TYPE_TAG)
1408 continue;
1409 err = load_tag(1, idset, id, repo,
1410 loose_obj_only, cancel_cb, cancel_arg);
1411 if (err)
1412 goto done;
1414 done:
1415 for (i = 0; i < nobj; i++) {
1416 free(ids[i]);
1418 free(ids);
1419 return err;
1422 const struct got_error *
1423 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1425 size_t n;
1427 SHA1Update(ctx, buf, len);
1428 n = fwrite(buf, 1, len, f);
1429 if (n != len)
1430 return got_ferror(f, GOT_ERR_IO);
1431 return NULL;
1434 static void
1435 putbe32(char *b, uint32_t n)
1437 b[0] = n >> 24;
1438 b[1] = n >> 16;
1439 b[2] = n >> 8;
1440 b[3] = n >> 0;
1443 static int
1444 write_order_cmp(const void *pa, const void *pb)
1446 struct got_pack_meta *a, *b, *ahd, *bhd;
1448 a = *(struct got_pack_meta **)pa;
1449 b = *(struct got_pack_meta **)pb;
1450 ahd = (a->head == NULL) ? a : a->head;
1451 bhd = (b->head == NULL) ? b : b->head;
1452 if (ahd->mtime != bhd->mtime)
1453 return bhd->mtime - ahd->mtime;
1454 if (ahd != bhd)
1455 return (uintptr_t)bhd - (uintptr_t)ahd;
1456 if (a->nchain != b->nchain)
1457 return a->nchain - b->nchain;
1458 return a->mtime - b->mtime;
1461 static int
1462 reuse_write_order_cmp(const void *pa, const void *pb)
1464 struct got_pack_meta *a, *b;
1466 a = *(struct got_pack_meta **)pa;
1467 b = *(struct got_pack_meta **)pb;
1469 if (a->reused_delta_offset < b->reused_delta_offset)
1470 return -1;
1471 if (a->reused_delta_offset > b->reused_delta_offset)
1472 return 1;
1473 return 0;
1476 static const struct got_error *
1477 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1479 size_t i;
1481 *hdrlen = 0;
1483 hdr[0] = obj_type << 4;
1484 hdr[0] |= len & 0xf;
1485 len >>= 4;
1486 for (i = 1; len != 0; i++){
1487 if (i >= bufsize)
1488 return got_error(GOT_ERR_NO_SPACE);
1489 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1490 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1491 len >>= GOT_DELTA_SIZE_SHIFT;
1494 *hdrlen = i;
1495 return NULL;
1498 static int
1499 packoff(char *hdr, off_t off)
1501 int i, j;
1502 char rbuf[8];
1504 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1505 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1506 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1507 GOT_DELTA_SIZE_MORE;
1510 j = 0;
1511 while (i > 0)
1512 hdr[j++] = rbuf[--i];
1513 return j;
1516 static const struct got_error *
1517 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1518 struct got_pack_meta *m)
1520 const struct got_error *err;
1521 char buf[32];
1522 int nh;
1524 if (m->prev->off != 0) {
1525 err = packhdr(&nh, buf, sizeof(buf),
1526 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1527 if (err)
1528 return err;
1529 nh += packoff(buf + nh, m->off - m->prev->off);
1530 err = hwrite(packfile, buf, nh, ctx);
1531 if (err)
1532 return err;
1533 *packfile_size += nh;
1534 } else {
1535 err = packhdr(&nh, buf, sizeof(buf),
1536 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1537 if (err)
1538 return err;
1539 err = hwrite(packfile, buf, nh, ctx);
1540 if (err)
1541 return err;
1542 *packfile_size += nh;
1543 err = hwrite(packfile, m->prev->id.sha1,
1544 sizeof(m->prev->id.sha1), ctx);
1545 if (err)
1546 return err;
1547 *packfile_size += sizeof(m->prev->id.sha1);
1550 return NULL;
1553 static const struct got_error *
1554 write_packed_object(off_t *packfile_size, FILE *packfile,
1555 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1556 SHA1_CTX *ctx, struct got_repository *repo)
1558 const struct got_error *err = NULL;
1559 struct got_deflate_checksum csum;
1560 char buf[32];
1561 int nh;
1562 struct got_raw_object *raw = NULL;
1563 off_t outlen;
1565 csum.output_sha1 = ctx;
1566 csum.output_crc = NULL;
1568 m->off = ftello(packfile);
1569 if (m->delta_len == 0) {
1570 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1571 if (err)
1572 goto done;
1573 err = packhdr(&nh, buf, sizeof(buf),
1574 m->obj_type, raw->size);
1575 if (err)
1576 goto done;
1577 err = hwrite(packfile, buf, nh, ctx);
1578 if (err)
1579 goto done;
1580 *packfile_size += nh;
1581 if (raw->f == NULL) {
1582 err = got_deflate_to_file_mmap(&outlen,
1583 raw->data + raw->hdrlen, 0, raw->size,
1584 packfile, &csum);
1585 if (err)
1586 goto done;
1587 } else {
1588 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1589 == -1) {
1590 err = got_error_from_errno("fseeko");
1591 goto done;
1593 err = got_deflate_to_file(&outlen, raw->f,
1594 raw->size, packfile, &csum);
1595 if (err)
1596 goto done;
1598 *packfile_size += outlen;
1599 got_object_raw_close(raw);
1600 raw = NULL;
1601 } else if (m->delta_buf) {
1602 err = deltahdr(packfile_size, ctx, packfile, m);
1603 if (err)
1604 goto done;
1605 err = got_deflate_to_file_mmap(&outlen,
1606 m->delta_buf, 0, m->delta_len, packfile, &csum);
1607 if (err)
1608 goto done;
1609 *packfile_size += outlen;
1610 free(m->delta_buf);
1611 m->delta_buf = NULL;
1612 } else {
1613 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1614 == -1) {
1615 err = got_error_from_errno("fseeko");
1616 goto done;
1618 err = deltahdr(packfile_size, ctx, packfile, m);
1619 if (err)
1620 goto done;
1621 err = got_deflate_to_file(&outlen, delta_cache,
1622 m->delta_len, packfile, &csum);
1623 if (err)
1624 goto done;
1625 *packfile_size += outlen;
1627 done:
1628 if (raw)
1629 got_object_raw_close(raw);
1630 return err;
1633 static const struct got_error *
1634 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1635 struct got_pack_meta **deltify, int ndeltify,
1636 struct got_pack_meta **reuse, int nreuse,
1637 int nours, struct got_repository *repo,
1638 got_pack_progress_cb progress_cb, void *progress_arg,
1639 struct got_ratelimit *rl,
1640 got_cancel_cb cancel_cb, void *cancel_arg)
1642 const struct got_error *err = NULL;
1643 int i;
1644 SHA1_CTX ctx;
1645 struct got_pack_meta *m;
1646 char buf[32];
1647 size_t n;
1648 off_t packfile_size = 0;
1649 int outfd = -1;
1651 SHA1Init(&ctx);
1653 err = hwrite(packfile, "PACK", 4, &ctx);
1654 if (err)
1655 return err;
1656 putbe32(buf, GOT_PACKFILE_VERSION);
1657 err = hwrite(packfile, buf, 4, &ctx);
1658 if (err)
1659 goto done;
1660 putbe32(buf, ndeltify + nreuse);
1661 err = hwrite(packfile, buf, 4, &ctx);
1662 if (err)
1663 goto done;
1665 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1666 write_order_cmp);
1667 for (i = 0; i < ndeltify; i++) {
1668 err = report_progress(progress_cb, progress_arg, rl,
1669 packfile_size, nours, ndeltify + nreuse,
1670 ndeltify + nreuse, i);
1671 if (err)
1672 goto done;
1673 m = deltify[i];
1674 err = write_packed_object(&packfile_size, packfile,
1675 delta_cache, m, &outfd, &ctx, repo);
1676 if (err)
1677 goto done;
1680 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1681 reuse_write_order_cmp);
1682 for (i = 0; i < nreuse; i++) {
1683 err = report_progress(progress_cb, progress_arg, rl,
1684 packfile_size, nours, ndeltify + nreuse,
1685 ndeltify + nreuse, ndeltify + i);
1686 if (err)
1687 goto done;
1688 m = reuse[i];
1689 err = write_packed_object(&packfile_size, packfile,
1690 delta_cache, m, &outfd, &ctx, repo);
1691 if (err)
1692 goto done;
1695 SHA1Final(pack_sha1, &ctx);
1696 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1697 if (n != SHA1_DIGEST_LENGTH)
1698 err = got_ferror(packfile, GOT_ERR_IO);
1699 packfile_size += SHA1_DIGEST_LENGTH;
1700 packfile_size += sizeof(struct got_packfile_hdr);
1701 if (progress_cb) {
1702 err = progress_cb(progress_arg, packfile_size, nours,
1703 ndeltify + nreuse, ndeltify + nreuse,
1704 ndeltify + nreuse);
1705 if (err)
1706 goto done;
1708 done:
1709 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1710 err = got_error_from_errno("close");
1711 return err;
1714 static const struct got_error *
1715 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1717 struct got_object_idset *idset = arg;
1719 if (got_object_idset_get_element_data(entry) == NULL)
1720 got_object_idset_remove_element(idset, entry);
1722 return NULL;
1725 static const struct got_error *
1726 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1728 struct got_object_idset *idset = arg;
1729 struct got_pack_meta *m;
1731 m = got_object_idset_get_element_data(entry);
1732 if (m->have_reused_delta)
1733 got_object_idset_remove_element(idset, entry);
1735 return NULL;
1738 static const struct got_error *
1739 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1741 struct got_pack_meta *m = data;
1742 struct got_pack_metavec *v = arg;
1744 return add_meta(m, v);
1747 const struct got_error *
1748 got_pack_create(uint8_t *packsha1, FILE *packfile,
1749 struct got_object_id **theirs, int ntheirs,
1750 struct got_object_id **ours, int nours,
1751 struct got_repository *repo, int loose_obj_only, int allow_empty,
1752 got_pack_progress_cb progress_cb, void *progress_arg,
1753 got_cancel_cb cancel_cb, void *cancel_arg)
1755 const struct got_error *err;
1756 int delta_cache_fd = -1;
1757 FILE *delta_cache = NULL;
1758 struct got_object_idset *idset;
1759 struct got_ratelimit rl;
1760 struct got_pack_metavec deltify, reuse;
1762 memset(&deltify, 0, sizeof(deltify));
1763 memset(&reuse, 0, sizeof(reuse));
1765 got_ratelimit_init(&rl, 0, 500);
1767 idset = got_object_idset_alloc();
1768 if (idset == NULL)
1769 return got_error_from_errno("got_object_idset_alloc");
1771 err = load_object_ids(idset, theirs, ntheirs, ours, nours,
1772 repo, loose_obj_only, cancel_cb, cancel_arg);
1773 if (err)
1774 return err;
1776 err = got_object_idset_for_each_element(idset,
1777 remove_unused_object, idset);
1778 if (err)
1779 goto done;
1781 if (progress_cb) {
1782 err = progress_cb(progress_arg, 0L, nours,
1783 got_object_idset_num_elements(idset), 0, 0);
1784 if (err)
1785 goto done;
1788 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1789 err = got_error(GOT_ERR_CANNOT_PACK);
1790 goto done;
1793 delta_cache_fd = got_opentempfd();
1794 if (delta_cache_fd == -1) {
1795 err = got_error_from_errno("got_opentemp");
1796 goto done;
1799 reuse.metasz = 64;
1800 reuse.meta = calloc(reuse.metasz,
1801 sizeof(struct got_pack_meta *));
1802 if (reuse.meta == NULL) {
1803 err = got_error_from_errno("calloc");
1804 goto done;
1807 err = search_deltas(&reuse, idset, delta_cache_fd, nours, repo,
1808 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1809 if (err)
1810 goto done;
1811 if (reuse.nmeta > 0) {
1812 err = got_object_idset_for_each_element(idset,
1813 remove_reused_object, idset);
1814 if (err)
1815 goto done;
1818 delta_cache = fdopen(delta_cache_fd, "a+");
1819 if (delta_cache == NULL) {
1820 err = got_error_from_errno("fdopen");
1821 goto done;
1823 delta_cache_fd = -1;
1825 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1826 err = got_error_from_errno("fseeko");
1827 goto done;
1830 deltify.meta = calloc(got_object_idset_num_elements(idset),
1831 sizeof(struct got_pack_meta *));
1832 if (deltify.meta == NULL) {
1833 err = got_error_from_errno("calloc");
1834 goto done;
1836 deltify.metasz = got_object_idset_num_elements(idset);
1838 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1839 if (err)
1840 goto done;
1841 if (deltify.nmeta > 0) {
1842 err = pick_deltas(deltify.meta, deltify.nmeta, nours,
1843 reuse.nmeta, delta_cache, repo,
1844 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1845 if (err)
1846 goto done;
1847 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1848 err = got_error_from_errno("fseeko");
1849 goto done;
1853 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1854 deltify.nmeta, reuse.meta, reuse.nmeta, nours, repo,
1855 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1856 if (err)
1857 goto done;
1858 done:
1859 free_nmeta(deltify.meta, deltify.nmeta);
1860 free_nmeta(reuse.meta, reuse.nmeta);
1861 got_object_idset_free(idset);
1862 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1863 err = got_error_from_errno("close");
1864 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1865 err = got_error_from_errno("fclose");
1866 return err;