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 <stdint.h>
26 #include <imsg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sha1.h>
31 #include <time.h>
32 #include <limits.h>
33 #include <zlib.h>
35 #include "got_error.h"
36 #include "got_cancel.h"
37 #include "got_object.h"
38 #include "got_path.h"
39 #include "got_reference.h"
40 #include "got_repository_admin.h"
41 #include "got_opentemp.h"
43 #include "got_lib_deltify.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_deflate.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_ratelimit.h"
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 #ifndef MAX
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 #endif
62 struct got_pack_meta {
63 struct got_object_id id;
64 char *path;
65 int obj_type;
66 off_t size;
67 time_t mtime;
69 /* The best delta we picked */
70 struct got_pack_meta *head;
71 struct got_pack_meta *prev;
72 unsigned char *delta_buf; /* if not encoded in delta cache file */
73 off_t delta_offset; /* offset in delta cache file */
74 off_t delta_len; /* encoded delta length */
75 int nchain;
77 int have_reused_delta;
78 off_t reused_delta_offset; /* offset of delta in reused pack file */
79 struct got_object_id *base_obj_id;
81 /* Only used for delta window */
82 struct got_delta_table *dtab;
84 /* Only used for writing offset deltas */
85 off_t off;
86 };
88 struct got_pack_metavec {
89 struct got_pack_meta **meta;
90 int nmeta;
91 int metasz;
92 };
94 static const struct got_error *
95 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
96 const char *path, int obj_type, time_t mtime)
97 {
98 const struct got_error *err = NULL;
99 struct got_pack_meta *m;
101 *new = NULL;
103 m = calloc(1, sizeof(*m));
104 if (m == NULL)
105 return got_error_from_errno("calloc");
107 memcpy(&m->id, id, sizeof(m->id));
109 m->path = strdup(path);
110 if (m->path == NULL) {
111 err = got_error_from_errno("strdup");
112 free(m);
113 return err;
116 m->obj_type = obj_type;
117 m->mtime = mtime;
118 *new = m;
119 return NULL;
122 static void
123 clear_meta(struct got_pack_meta *meta)
125 if (meta == NULL)
126 return;
127 free(meta->path);
128 meta->path = NULL;
129 free(meta->delta_buf);
130 meta->delta_buf = NULL;
131 free(meta->base_obj_id);
132 meta->base_obj_id = NULL;
135 static void
136 free_nmeta(struct got_pack_meta **meta, int nmeta)
138 int i;
140 for (i = 0; i < nmeta; i++)
141 clear_meta(meta[i]);
142 free(meta);
145 static int
146 delta_order_cmp(const void *pa, const void *pb)
148 struct got_pack_meta *a, *b;
149 int cmp;
151 a = *(struct got_pack_meta **)pa;
152 b = *(struct got_pack_meta **)pb;
154 if (a->obj_type != b->obj_type)
155 return a->obj_type - b->obj_type;
156 cmp = strcmp(a->path, b->path);
157 if (cmp != 0)
158 return cmp;
159 if (a->mtime != b->mtime)
160 return a->mtime - b->mtime;
161 return got_object_id_cmp(&a->id, &b->id);
164 static off_t
165 delta_size(struct got_delta_instruction *deltas, int ndeltas)
167 int i;
168 off_t size = 32;
169 for (i = 0; i < ndeltas; i++) {
170 if (deltas[i].copy)
171 size += GOT_DELTA_SIZE_SHIFT;
172 else
173 size += deltas[i].len + 1;
175 return size;
178 static const struct got_error *
179 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
181 char *n;
183 if (*len + nseg >= *sz) {
184 while (*len + nseg >= *sz)
185 *sz += *sz / 2;
186 n = realloc(*p, *sz);
187 if (n == NULL)
188 return got_error_from_errno("realloc");
189 *p = n;
191 memcpy(*p + *len, seg, nseg);
192 *len += nseg;
193 return NULL;
196 static const struct got_error *
197 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
198 struct got_delta_instruction *deltas, int ndeltas,
199 off_t delta_size, off_t base_size)
201 const struct got_error *err;
202 unsigned char buf[16], *bp;
203 int i, j;
204 size_t len = 0;
205 off_t n;
206 struct got_delta_instruction *d;
208 m->delta_buf = malloc(delta_size);
209 if (m->delta_buf == NULL)
210 return got_error_from_errno("calloc");
212 /* base object size */
213 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
214 n = base_size >> GOT_DELTA_SIZE_SHIFT;
215 for (i = 1; n > 0; i++) {
216 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
217 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
218 n >>= GOT_DELTA_SIZE_SHIFT;
220 err = append(&m->delta_buf, &len, &delta_size, buf, i);
221 if (err)
222 return err;
224 /* target object size */
225 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
226 n = o->size >> GOT_DELTA_SIZE_SHIFT;
227 for (i = 1; n > 0; i++) {
228 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
229 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
230 n >>= GOT_DELTA_SIZE_SHIFT;
232 err = append(&m->delta_buf, &len, &delta_size, buf, i);
233 if (err)
234 return err;
236 for (j = 0; j < ndeltas; j++) {
237 d = &deltas[j];
238 if (d->copy) {
239 n = d->offset;
240 bp = &buf[1];
241 buf[0] = GOT_DELTA_BASE_COPY;
242 for (i = 0; i < 4; i++) {
243 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
244 buf[0] |= 1 << i;
245 *bp++ = n & 0xff;
246 n >>= 8;
247 if (n == 0)
248 break;
251 n = d->len;
252 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
253 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
254 for (i = 0; i < 3 && n > 0; i++) {
255 buf[0] |= 1 << (i + 4);
256 *bp++ = n & 0xff;
257 n >>= 8;
260 err = append(&m->delta_buf, &len, &delta_size,
261 buf, bp - buf);
262 if (err)
263 return err;
264 } else if (o->f == NULL) {
265 n = 0;
266 while (n != d->len) {
267 buf[0] = (d->len - n < 127) ? d->len - n : 127;
268 err = append(&m->delta_buf, &len, &delta_size,
269 buf, 1);
270 if (err)
271 return err;
272 err = append(&m->delta_buf, &len, &delta_size,
273 o->data + o->hdrlen + d->offset + n,
274 buf[0]);
275 if (err)
276 return err;
277 n += buf[0];
279 } else {
280 char content[128];
281 size_t r;
282 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
283 return got_error_from_errno("fseeko");
284 n = 0;
285 while (n != d->len) {
286 buf[0] = (d->len - n < 127) ? d->len - n : 127;
287 err = append(&m->delta_buf, &len, &delta_size,
288 buf, 1);
289 if (err)
290 return err;
291 r = fread(content, 1, buf[0], o->f);
292 if (r != buf[0])
293 return got_ferror(o->f, GOT_ERR_IO);
294 err = append(&m->delta_buf, &len, &delta_size,
295 content, buf[0]);
296 if (err)
297 return err;
298 n += buf[0];
303 m->delta_len = len;
304 return NULL;
307 static const struct got_error *
308 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
309 struct got_delta_instruction *deltas, int ndeltas,
310 off_t base_size, FILE *f)
312 unsigned char buf[16], *bp;
313 int i, j;
314 off_t n;
315 size_t w;
316 struct got_delta_instruction *d;
318 /* base object size */
319 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
320 n = base_size >> GOT_DELTA_SIZE_SHIFT;
321 for (i = 1; n > 0; i++) {
322 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
323 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
324 n >>= GOT_DELTA_SIZE_SHIFT;
326 w = fwrite(buf, 1, i, f);
327 if (w != i)
328 return got_ferror(f, GOT_ERR_IO);
330 /* target object size */
331 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
332 n = o->size >> GOT_DELTA_SIZE_SHIFT;
333 for (i = 1; n > 0; i++) {
334 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
335 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
336 n >>= GOT_DELTA_SIZE_SHIFT;
338 w = fwrite(buf, 1, i, f);
339 if (w != i)
340 return got_ferror(f, GOT_ERR_IO);
342 for (j = 0; j < ndeltas; j++) {
343 d = &deltas[j];
344 if (d->copy) {
345 n = d->offset;
346 bp = &buf[1];
347 buf[0] = GOT_DELTA_BASE_COPY;
348 for (i = 0; i < 4; i++) {
349 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
350 buf[0] |= 1 << i;
351 *bp++ = n & 0xff;
352 n >>= 8;
353 if (n == 0)
354 break;
357 n = d->len;
358 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
359 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
360 for (i = 0; i < 3 && n > 0; i++) {
361 buf[0] |= 1 << (i + 4);
362 *bp++ = n & 0xff;
363 n >>= 8;
366 w = fwrite(buf, 1, bp - buf, f);
367 if (w != bp - buf)
368 return got_ferror(f, GOT_ERR_IO);
369 } else if (o->f == NULL) {
370 n = 0;
371 while (n != d->len) {
372 buf[0] = (d->len - n < 127) ? d->len - n : 127;
373 w = fwrite(buf, 1, 1, f);
374 if (w != 1)
375 return got_ferror(f, GOT_ERR_IO);
376 w = fwrite(o->data + o->hdrlen + d->offset + n,
377 1, buf[0], f);
378 if (w != buf[0])
379 return got_ferror(f, GOT_ERR_IO);
380 n += buf[0];
382 } else {
383 char content[128];
384 size_t r;
385 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
386 return got_error_from_errno("fseeko");
387 n = 0;
388 while (n != d->len) {
389 buf[0] = (d->len - n < 127) ? d->len - n : 127;
390 w = fwrite(buf, 1, 1, f);
391 if (w != 1)
392 return got_ferror(f, GOT_ERR_IO);
393 r = fread(content, 1, buf[0], o->f);
394 if (r != buf[0])
395 return got_ferror(o->f, GOT_ERR_IO);
396 w = fwrite(content, 1, buf[0], f);
397 if (w != buf[0])
398 return got_ferror(f, GOT_ERR_IO);
399 n += buf[0];
404 m->delta_len = ftello(f) - m->delta_offset;
405 return NULL;
408 static const struct got_error *
409 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
410 struct got_ratelimit *rl, off_t packfile_size, int ncommits,
411 int nobj_total, int obj_deltify, int nobj_written)
413 const struct got_error *err;
414 int elapsed;
416 if (progress_cb == NULL)
417 return NULL;
419 err = got_ratelimit_check(&elapsed, rl);
420 if (err || !elapsed)
421 return err;
423 return progress_cb(progress_arg, packfile_size, ncommits,
424 nobj_total, obj_deltify, nobj_written);
427 static const struct got_error *
428 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
430 if (v->nmeta == v->metasz){
431 size_t newsize = 2 * v->metasz;
432 struct got_pack_meta **new;
433 new = reallocarray(v->meta, newsize, sizeof(*new));
434 if (new == NULL)
435 return got_error_from_errno("reallocarray");
436 v->meta = new;
437 v->metasz = newsize;
440 v->meta[v->nmeta++] = m;
441 return NULL;
444 static const struct got_error *
445 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
446 struct got_object_idset *idset, struct got_pack *pack,
447 struct got_packidx *packidx, int delta_cache_fd,
448 struct got_repository *repo)
450 const struct got_error *err = NULL;
451 struct got_pack_meta *base = NULL;
452 struct got_object_id *base_obj_id = NULL;
453 off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
454 uint64_t base_size, result_size;
456 if (m->have_reused_delta)
457 return NULL;
459 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
460 &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
461 packidx, idx, &m->id, repo);
462 if (err)
463 return err;
465 if (delta_offset + delta_len < delta_offset)
466 return got_error(GOT_ERR_BAD_PACKFILE);
468 base = got_object_idset_get(idset, base_obj_id);
469 if (base == NULL)
470 goto done;
472 m->delta_len = delta_len;
473 m->delta_offset = delta_cache_offset;
474 m->prev = base;
475 m->size = result_size;
476 m->have_reused_delta = 1;
477 m->reused_delta_offset = delta_offset;
478 m->base_obj_id = base_obj_id;
479 base_obj_id = NULL;
480 err = add_meta(m, v);
481 done:
482 free(base_obj_id);
483 return err;
486 static const struct got_error *
487 find_pack_for_reuse(struct got_packidx **best_packidx,
488 struct got_repository *repo)
490 const struct got_error *err;
491 struct got_pathlist_head packidx_paths;
492 struct got_pathlist_entry *pe;
493 const char *best_packidx_path = NULL;
494 int nobj_max = 0;
496 TAILQ_INIT(&packidx_paths);
497 *best_packidx = NULL;
499 err = got_repo_list_packidx(&packidx_paths, repo);
500 if (err)
501 return err;
503 TAILQ_FOREACH(pe, &packidx_paths, entry) {
504 const char *path_packidx = pe->path;
505 struct got_packidx *packidx;
506 int nobj;
508 err = got_repo_get_packidx(&packidx, path_packidx, repo);
509 if (err)
510 break;
512 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
513 if (nobj > nobj_max) {
514 best_packidx_path = path_packidx;
515 nobj_max = nobj;
519 if (best_packidx_path) {
520 err = got_repo_get_packidx(best_packidx, best_packidx_path,
521 repo);
524 TAILQ_FOREACH(pe, &packidx_paths, entry)
525 free((void *)pe->path);
526 got_pathlist_free(&packidx_paths);
527 return err;
530 struct search_deltas_arg {
531 struct got_packidx *packidx;
532 struct got_pack *pack;
533 struct got_object_idset *idset;
534 struct got_pack_metavec *v;
535 int delta_cache_fd;
536 struct got_repository *repo;
537 got_pack_progress_cb progress_cb;
538 void *progress_arg;
539 struct got_ratelimit *rl;
540 got_cancel_cb cancel_cb;
541 void *cancel_arg;
542 int ncommits;
543 };
545 static const struct got_error *
546 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
548 const struct got_error *err;
549 struct got_pack_meta *m = data;
550 struct search_deltas_arg *a = arg;
551 int obj_idx;
552 struct got_object *obj = NULL;
554 if (a->cancel_cb) {
555 err = (*a->cancel_cb)(a->cancel_arg);
556 if (err)
557 return err;
560 if (!got_repo_check_packidx_bloom_filter(a->repo,
561 a->packidx->path_packidx, id))
562 return NULL;
564 obj_idx = got_packidx_get_object_idx(a->packidx, id);
565 if (obj_idx == -1)
566 return NULL;
568 /* TODO:
569 * Opening and closing an object just to check its flags
570 * is a bit expensive. We could have an imsg which requests
571 * plain type/size information for an object without doing
572 * work such as traversing the object's entire delta chain
573 * to find the base object type, and other such info which
574 * we don't really need here.
575 */
576 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
577 a->packidx, obj_idx, a->repo);
578 if (err)
579 return err;
581 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
582 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
583 a->delta_cache_fd, a->repo);
584 if (err)
585 goto done;
586 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
587 0L, a->ncommits, got_object_idset_num_elements(a->idset),
588 a->v->nmeta, 0);
590 done:
591 got_object_close(obj);
592 return err;
595 static const struct got_error *
596 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
597 int delta_cache_fd, int ncommits, struct got_repository *repo,
598 got_pack_progress_cb progress_cb, void *progress_arg,
599 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
601 const struct got_error *err = NULL;
602 char *path_packfile = NULL;
603 struct got_packidx *packidx;
604 struct got_pack *pack;
605 struct search_deltas_arg sda;
607 err = find_pack_for_reuse(&packidx, repo);
608 if (err)
609 return err;
611 if (packidx == NULL)
612 return NULL;
614 err = got_packidx_get_packfile_path(&path_packfile,
615 packidx->path_packidx);
616 if (err)
617 return err;
619 pack = got_repo_get_cached_pack(repo, path_packfile);
620 if (pack == NULL) {
621 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
622 if (err)
623 goto done;
626 sda.packidx = packidx;
627 sda.pack = pack;
628 sda.idset = idset;
629 sda.v = v;
630 sda.delta_cache_fd = delta_cache_fd;
631 sda.repo = repo;
632 sda.progress_cb = progress_cb;
633 sda.progress_arg = progress_arg;
634 sda.rl = rl;
635 sda.cancel_cb = cancel_cb;
636 sda.cancel_arg = cancel_arg;
637 sda.ncommits = ncommits;
638 err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
639 done:
640 free(path_packfile);
641 return err;
644 static const struct got_error *
645 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncommits,
646 int nreused, FILE *delta_cache, struct got_repository *repo,
647 got_pack_progress_cb progress_cb, void *progress_arg,
648 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
650 const struct got_error *err = NULL;
651 struct got_pack_meta *m = NULL, *base = NULL;
652 struct got_raw_object *raw = NULL, *base_raw = NULL;
653 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
654 int i, j, ndeltas, best_ndeltas;
655 off_t size, best_size;
656 const int max_base_candidates = 3;
657 size_t delta_memsize = 0;
658 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
659 int outfd = -1;
661 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
662 for (i = 0; i < nmeta; i++) {
663 if (cancel_cb) {
664 err = (*cancel_cb)(cancel_arg);
665 if (err)
666 break;
668 err = report_progress(progress_cb, progress_arg, rl,
669 0L, ncommits, nreused + nmeta, nreused + i, 0);
670 if (err)
671 goto done;
672 m = meta[i];
674 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
675 m->obj_type == GOT_OBJ_TYPE_TAG)
676 continue;
678 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
679 if (err)
680 goto done;
681 m->size = raw->size;
683 if (raw->f == NULL) {
684 err = got_deltify_init_mem(&m->dtab, raw->data,
685 raw->hdrlen, raw->size + raw->hdrlen);
686 } else {
687 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
688 raw->size + raw->hdrlen);
690 if (err)
691 goto done;
693 if (i > max_base_candidates) {
694 struct got_pack_meta *n = NULL;
695 n = meta[i - (max_base_candidates + 1)];
696 got_deltify_free(n->dtab);
697 n->dtab = NULL;
700 best_size = raw->size;
701 best_ndeltas = 0;
702 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
703 if (cancel_cb) {
704 err = (*cancel_cb)(cancel_arg);
705 if (err)
706 goto done;
708 base = meta[j];
709 /* long chains make unpacking slow, avoid such bases */
710 if (base->nchain >= 128 ||
711 base->obj_type != m->obj_type)
712 continue;
714 err = got_object_raw_open(&base_raw, &outfd, repo,
715 &base->id);
716 if (err)
717 goto done;
719 if (raw->f == NULL && base_raw->f == NULL) {
720 err = got_deltify_mem_mem(&deltas, &ndeltas,
721 raw->data, raw->hdrlen,
722 raw->size + raw->hdrlen,
723 base->dtab, base_raw->data,
724 base_raw->hdrlen,
725 base_raw->size + base_raw->hdrlen);
726 } else if (raw->f == NULL) {
727 err = got_deltify_mem_file(&deltas, &ndeltas,
728 raw->data, raw->hdrlen,
729 raw->size + raw->hdrlen,
730 base->dtab, base_raw->f,
731 base_raw->hdrlen,
732 base_raw->size + base_raw->hdrlen);
733 } else if (base_raw->f == NULL) {
734 err = got_deltify_file_mem(&deltas, &ndeltas,
735 raw->f, raw->hdrlen,
736 raw->size + raw->hdrlen,
737 base->dtab, base_raw->data,
738 base_raw->hdrlen,
739 base_raw->size + base_raw->hdrlen);
740 } else {
741 err = got_deltify(&deltas, &ndeltas,
742 raw->f, raw->hdrlen,
743 raw->size + raw->hdrlen,
744 base->dtab, base_raw->f, base_raw->hdrlen,
745 base_raw->size + base_raw->hdrlen);
747 got_object_raw_close(base_raw);
748 base_raw = NULL;
749 if (err)
750 goto done;
752 size = delta_size(deltas, ndeltas);
753 if (size + 32 < best_size){
754 /*
755 * if we already picked a best delta,
756 * replace it.
757 */
758 best_size = size;
759 free(best_deltas);
760 best_deltas = deltas;
761 best_ndeltas = ndeltas;
762 deltas = NULL;
763 m->nchain = base->nchain + 1;
764 m->prev = base;
765 m->head = base->head;
766 if (m->head == NULL)
767 m->head = base;
768 } else {
769 free(deltas);
770 deltas = NULL;
771 ndeltas = 0;
775 if (best_ndeltas > 0) {
776 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
777 delta_memsize + best_size <= max_delta_memsize) {
778 delta_memsize += best_size;
779 err = encode_delta_in_mem(m, raw, best_deltas,
780 best_ndeltas, best_size, m->prev->size);
781 } else {
782 m->delta_offset = ftello(delta_cache);
783 /*
784 * TODO:
785 * Storing compressed delta data in the delta
786 * cache file would probably be more efficient
787 * than writing uncompressed delta data here
788 * and compressing it while writing the pack
789 * file. This would also allow for reusing
790 * deltas in their compressed form.
791 */
792 err = encode_delta(m, raw, best_deltas,
793 best_ndeltas, m->prev->size, delta_cache);
795 free(best_deltas);
796 best_deltas = NULL;
797 best_ndeltas = 0;
798 if (err)
799 goto done;
802 got_object_raw_close(raw);
803 raw = NULL;
805 done:
806 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
807 got_deltify_free(meta[i]->dtab);
808 meta[i]->dtab = NULL;
810 if (raw)
811 got_object_raw_close(raw);
812 if (base_raw)
813 got_object_raw_close(base_raw);
814 if (outfd != -1 && close(outfd) == -1 && err == NULL)
815 err = got_error_from_errno("close");
816 free(deltas);
817 free(best_deltas);
818 return err;
821 static const struct got_error *
822 search_packidx(int *found, struct got_object_id *id,
823 struct got_repository *repo)
825 const struct got_error *err = NULL;
826 struct got_packidx *packidx = NULL;
827 int idx;
829 *found = 0;
831 err = got_repo_search_packidx(&packidx, &idx, repo, id);
832 if (err == NULL)
833 *found = 1; /* object is already packed */
834 else if (err->code == GOT_ERR_NO_OBJ)
835 err = NULL;
836 return err;
839 static const int obj_types[] = {
840 GOT_OBJ_TYPE_ANY,
841 GOT_OBJ_TYPE_COMMIT,
842 GOT_OBJ_TYPE_TREE,
843 GOT_OBJ_TYPE_BLOB,
844 GOT_OBJ_TYPE_TAG,
845 GOT_OBJ_TYPE_OFFSET_DELTA,
846 GOT_OBJ_TYPE_REF_DELTA
847 };
849 static const struct got_error *
850 add_object(int want_meta, struct got_object_idset *idset,
851 struct got_object_id *id, const char *path, int obj_type,
852 time_t mtime, int loose_obj_only, struct got_repository *repo)
854 const struct got_error *err;
855 struct got_pack_meta *m = NULL;
857 if (loose_obj_only) {
858 int is_packed;
859 err = search_packidx(&is_packed, id, repo);
860 if (err)
861 return err;
862 if (is_packed)
863 return NULL;
866 if (want_meta) {
867 err = alloc_meta(&m, id, path, obj_type, mtime);
868 if (err)
869 return err;
872 return got_object_idset_add(idset, id, m);
875 static const struct got_error *
876 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
877 struct got_object_idset *idset, struct got_object_id *tree_id,
878 const char *dpath, time_t mtime, struct got_repository *repo,
879 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
881 const struct got_error *err;
882 struct got_tree_object *tree;
883 char *p = NULL;
884 int i;
886 err = got_object_open_as_tree(&tree, repo, tree_id);
887 if (err)
888 return err;
890 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
891 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
892 struct got_object_id *id = got_tree_entry_get_id(e);
893 mode_t mode = got_tree_entry_get_mode(e);
895 if (cancel_cb) {
896 err = (*cancel_cb)(cancel_arg);
897 if (err)
898 break;
901 if (got_object_tree_entry_is_submodule(e) ||
902 got_object_idset_contains(idset, id))
903 continue;
905 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
906 got_tree_entry_get_name(e)) == -1) {
907 err = got_error_from_errno("asprintf");
908 break;
911 if (S_ISDIR(mode)) {
912 struct got_object_qid *qid;
913 err = got_object_qid_alloc(&qid, id);
914 if (err)
915 break;
916 STAILQ_INSERT_TAIL(ids, qid, entry);
917 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
918 err = add_object(want_meta, idset, id, p,
919 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo);
920 if (err)
921 break;
923 free(p);
924 p = NULL;
927 got_object_tree_close(tree);
928 free(p);
929 return err;
932 static const struct got_error *
933 load_tree(int want_meta, struct got_object_idset *idset,
934 struct got_object_id *tree_id, const char *dpath, time_t mtime,
935 int loose_obj_only, struct got_repository *repo,
936 got_cancel_cb cancel_cb, void *cancel_arg)
938 const struct got_error *err = NULL;
939 struct got_object_id_queue tree_ids;
940 struct got_object_qid *qid;
942 if (got_object_idset_contains(idset, tree_id))
943 return NULL;
945 err = got_object_qid_alloc(&qid, tree_id);
946 if (err)
947 return err;
949 STAILQ_INIT(&tree_ids);
950 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
952 while (!STAILQ_EMPTY(&tree_ids)) {
953 if (cancel_cb) {
954 err = (*cancel_cb)(cancel_arg);
955 if (err)
956 break;
959 qid = STAILQ_FIRST(&tree_ids);
960 STAILQ_REMOVE_HEAD(&tree_ids, entry);
962 if (got_object_idset_contains(idset, qid->id)) {
963 got_object_qid_free(qid);
964 continue;
967 err = add_object(want_meta, idset, qid->id, dpath,
968 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo);
969 if (err) {
970 got_object_qid_free(qid);
971 break;
974 err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
975 dpath, mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
976 got_object_qid_free(qid);
977 if (err)
978 break;
981 got_object_id_queue_free(&tree_ids);
982 return err;
985 static const struct got_error *
986 load_commit(int want_meta, struct got_object_idset *idset,
987 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
988 got_cancel_cb cancel_cb, void *cancel_arg)
990 const struct got_error *err;
991 struct got_commit_object *commit;
993 if (got_object_idset_contains(idset, id))
994 return NULL;
996 if (loose_obj_only) {
997 int is_packed;
998 err = search_packidx(&is_packed, id, repo);
999 if (err)
1000 return err;
1001 if (is_packed)
1002 return NULL;
1005 err = got_object_open_as_commit(&commit, repo, id);
1006 if (err)
1007 return err;
1009 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1010 got_object_commit_get_committer_time(commit),
1011 loose_obj_only, repo);
1012 if (err)
1013 goto done;
1015 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1016 "", got_object_commit_get_committer_time(commit),
1017 loose_obj_only, repo, cancel_cb, cancel_arg);
1018 done:
1019 got_object_commit_close(commit);
1020 return err;
1023 static const struct got_error *
1024 load_tag(int want_meta, struct got_object_idset *idset,
1025 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1026 got_cancel_cb cancel_cb, void *cancel_arg)
1028 const struct got_error *err;
1029 struct got_tag_object *tag = NULL;
1031 if (got_object_idset_contains(idset, id))
1032 return NULL;
1034 if (loose_obj_only) {
1035 int is_packed;
1036 err = search_packidx(&is_packed, id, repo);
1037 if (err)
1038 return err;
1039 if (is_packed)
1040 return NULL;
1043 err = got_object_open_as_tag(&tag, repo, id);
1044 if (err)
1045 return err;
1047 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1048 got_object_tag_get_tagger_time(tag),
1049 loose_obj_only, repo);
1050 if (err)
1051 goto done;
1053 switch (got_object_tag_get_object_type(tag)) {
1054 case GOT_OBJ_TYPE_COMMIT:
1055 err = load_commit(want_meta, idset,
1056 got_object_tag_get_object_id(tag), repo,
1057 loose_obj_only, cancel_cb, cancel_arg);
1058 break;
1059 case GOT_OBJ_TYPE_TREE:
1060 err = load_tree(want_meta, idset,
1061 got_object_tag_get_object_id(tag), "",
1062 got_object_tag_get_tagger_time(tag),
1063 loose_obj_only, repo, cancel_cb, cancel_arg);
1064 break;
1065 default:
1066 break;
1069 done:
1070 got_object_tag_close(tag);
1071 return err;
1074 enum findtwixt_color {
1075 COLOR_KEEP = 0,
1076 COLOR_DROP,
1077 COLOR_BLANK,
1079 static const int findtwixt_colors[] = {
1080 COLOR_KEEP,
1081 COLOR_DROP,
1082 COLOR_BLANK
1085 static const struct got_error *
1086 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1087 int color, struct got_repository *repo)
1089 const struct got_error *err;
1090 struct got_object_qid *qid;
1092 err = got_object_qid_alloc(&qid, id);
1093 if (err)
1094 return err;
1096 STAILQ_INSERT_TAIL(ids, qid, entry);
1097 qid->data = (void *)&findtwixt_colors[color];
1098 return NULL;
1101 static const struct got_error *
1102 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1103 struct got_object_id *id, struct got_repository *repo,
1104 got_cancel_cb cancel_cb, void *cancel_arg)
1106 const struct got_error *err = NULL;
1107 struct got_commit_object *commit;
1108 const struct got_object_id_queue *parents;
1109 struct got_object_id_queue ids;
1110 struct got_object_qid *qid;
1112 STAILQ_INIT(&ids);
1114 err = got_object_qid_alloc(&qid, id);
1115 if (err)
1116 return err;
1117 STAILQ_INSERT_HEAD(&ids, qid, entry);
1119 while (!STAILQ_EMPTY(&ids)) {
1120 if (cancel_cb) {
1121 err = (*cancel_cb)(cancel_arg);
1122 if (err)
1123 break;
1126 qid = STAILQ_FIRST(&ids);
1127 STAILQ_REMOVE_HEAD(&ids, entry);
1129 if (got_object_idset_contains(drop, qid->id)) {
1130 got_object_qid_free(qid);
1131 continue;
1134 err = got_object_idset_add(drop, qid->id,
1135 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1136 if (err) {
1137 got_object_qid_free(qid);
1138 break;
1141 if (!got_object_idset_contains(keep, qid->id)) {
1142 got_object_qid_free(qid);
1143 continue;
1146 err = got_object_open_as_commit(&commit, repo, qid->id);
1147 got_object_qid_free(qid);
1148 if (err)
1149 break;
1151 parents = got_object_commit_get_parent_ids(commit);
1152 if (parents) {
1153 err = got_object_id_queue_copy(parents, &ids);
1154 if (err) {
1155 got_object_commit_close(commit);
1156 break;
1159 got_object_commit_close(commit);
1162 got_object_id_queue_free(&ids);
1163 return err;
1166 struct append_id_arg {
1167 struct got_object_id **array;
1168 int idx;
1171 static const struct got_error *
1172 append_id(struct got_object_id *id, void *data, void *arg)
1174 struct append_id_arg *a = arg;
1176 a->array[a->idx] = got_object_id_dup(id);
1177 if (a->array[a->idx] == NULL)
1178 return got_error_from_errno("got_object_id_dup");
1180 a->idx++;
1181 return NULL;
1184 static const struct got_error *
1185 findtwixt(struct got_object_id ***res, int *nres,
1186 struct got_object_id **head, int nhead,
1187 struct got_object_id **tail, int ntail,
1188 struct got_repository *repo,
1189 got_cancel_cb cancel_cb, void *cancel_arg)
1191 const struct got_error *err = NULL;
1192 struct got_object_id_queue ids;
1193 struct got_object_idset *keep, *drop;
1194 struct got_object_qid *qid;
1195 int i, ncolor, nkeep, obj_type;
1197 STAILQ_INIT(&ids);
1198 *res = NULL;
1199 *nres = 0;
1201 keep = got_object_idset_alloc();
1202 if (keep == NULL)
1203 return got_error_from_errno("got_object_idset_alloc");
1205 drop = got_object_idset_alloc();
1206 if (drop == NULL) {
1207 err = got_error_from_errno("got_object_idset_alloc");
1208 goto done;
1211 for (i = 0; i < nhead; i++) {
1212 struct got_object_id *id = head[i];
1213 if (id == NULL)
1214 continue;
1215 err = got_object_get_type(&obj_type, repo, id);
1216 if (err)
1217 return err;
1218 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1219 continue;
1220 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1221 if (err)
1222 goto done;
1224 for (i = 0; i < ntail; i++) {
1225 struct got_object_id *id = tail[i];
1226 if (id == NULL)
1227 continue;
1228 err = got_object_get_type(&obj_type, repo, id);
1229 if (err)
1230 return err;
1231 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1232 continue;
1233 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1234 if (err)
1235 goto done;
1238 while (!STAILQ_EMPTY(&ids)) {
1239 int qcolor;
1240 qid = STAILQ_FIRST(&ids);
1241 qcolor = *((int *)qid->data);
1243 if (got_object_idset_contains(drop, qid->id))
1244 ncolor = COLOR_DROP;
1245 else if (got_object_idset_contains(keep, qid->id))
1246 ncolor = COLOR_KEEP;
1247 else
1248 ncolor = COLOR_BLANK;
1250 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1251 qcolor == COLOR_KEEP)) {
1252 STAILQ_REMOVE_HEAD(&ids, entry);
1253 got_object_qid_free(qid);
1254 continue;
1257 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1258 err = drop_commit(keep, drop, qid->id, repo,
1259 cancel_cb, cancel_arg);
1260 if (err)
1261 goto done;
1262 } else if (ncolor == COLOR_BLANK) {
1263 struct got_commit_object *commit;
1264 struct got_object_id *id;
1265 const struct got_object_id_queue *parents;
1266 struct got_object_qid *pid;
1268 id = got_object_id_dup(qid->id);
1269 if (id == NULL) {
1270 err = got_error_from_errno("got_object_id_dup");
1271 goto done;
1273 if (qcolor == COLOR_KEEP)
1274 err = got_object_idset_add(keep, id,
1275 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1276 else
1277 err = got_object_idset_add(drop, id,
1278 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1279 if (err) {
1280 free(id);
1281 goto done;
1284 err = got_object_open_as_commit(&commit, repo, id);
1285 if (err) {
1286 free(id);
1287 goto done;
1289 parents = got_object_commit_get_parent_ids(commit);
1290 if (parents) {
1291 STAILQ_FOREACH(pid, parents, entry) {
1292 err = queue_commit_id(&ids, pid->id,
1293 qcolor, repo);
1294 if (err) {
1295 free(id);
1296 goto done;
1300 got_object_commit_close(commit);
1301 commit = NULL;
1302 } else {
1303 /* should not happen */
1304 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1305 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1306 goto done;
1309 STAILQ_REMOVE_HEAD(&ids, entry);
1310 got_object_qid_free(qid);
1313 nkeep = got_object_idset_num_elements(keep);
1314 if (nkeep > 0) {
1315 struct append_id_arg arg;
1316 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1317 if (arg.array == NULL) {
1318 err = got_error_from_errno("calloc");
1319 goto done;
1321 arg.idx = 0;
1322 err = got_object_idset_for_each(keep, append_id, &arg);
1323 if (err) {
1324 free(arg.array);
1325 goto done;
1327 *res = arg.array;
1328 *nres = nkeep;
1330 done:
1331 got_object_idset_free(keep);
1332 got_object_idset_free(drop);
1333 got_object_id_queue_free(&ids);
1334 return err;
1337 static const struct got_error *
1338 load_object_ids(struct got_object_idset *idset,
1339 struct got_object_id **theirs, int ntheirs,
1340 struct got_object_id **ours, int nours, struct got_repository *repo,
1341 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
1343 const struct got_error *err = NULL;
1344 struct got_object_id **ids = NULL;
1345 int i, nobj = 0, obj_type;
1347 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1348 cancel_cb, cancel_arg);
1349 if (err || nobj == 0)
1350 goto done;
1352 for (i = 0; i < ntheirs; i++) {
1353 struct got_object_id *id = theirs[i];
1354 if (id == NULL)
1355 continue;
1356 err = got_object_get_type(&obj_type, repo, id);
1357 if (err)
1358 return err;
1359 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1360 continue;
1361 err = load_commit(0, idset, id, repo,
1362 loose_obj_only, cancel_cb, cancel_arg);
1363 if (err)
1364 goto done;
1367 for (i = 0; i < ntheirs; i++) {
1368 struct got_object_id *id = theirs[i];
1369 struct got_pack_meta *m;
1370 if (id == NULL)
1371 continue;
1372 m = got_object_idset_get(idset, id);
1373 if (m == NULL) {
1374 err = got_object_get_type(&obj_type, repo, id);
1375 if (err)
1376 goto done;
1377 } else
1378 obj_type = m->obj_type;
1379 if (obj_type != GOT_OBJ_TYPE_TAG)
1380 continue;
1381 err = load_tag(0, idset, id, repo,
1382 loose_obj_only, cancel_cb, cancel_arg);
1383 if (err)
1384 goto done;
1387 for (i = 0; i < nobj; i++) {
1388 err = load_commit(1, idset, ids[i], repo,
1389 loose_obj_only, cancel_cb, cancel_arg);
1390 if (err)
1391 goto done;
1394 for (i = 0; i < nours; i++) {
1395 struct got_object_id *id = ours[i];
1396 struct got_pack_meta *m;
1397 if (id == NULL)
1398 continue;
1399 m = got_object_idset_get(idset, id);
1400 if (m == NULL) {
1401 err = got_object_get_type(&obj_type, repo, id);
1402 if (err)
1403 goto done;
1404 } else
1405 obj_type = m->obj_type;
1406 if (obj_type != GOT_OBJ_TYPE_TAG)
1407 continue;
1408 err = load_tag(1, idset, id, repo,
1409 loose_obj_only, cancel_cb, cancel_arg);
1410 if (err)
1411 goto done;
1413 done:
1414 for (i = 0; i < nobj; i++) {
1415 free(ids[i]);
1417 free(ids);
1418 return err;
1421 const struct got_error *
1422 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1424 size_t n;
1426 SHA1Update(ctx, buf, len);
1427 n = fwrite(buf, 1, len, f);
1428 if (n != len)
1429 return got_ferror(f, GOT_ERR_IO);
1430 return NULL;
1433 static void
1434 putbe32(char *b, uint32_t n)
1436 b[0] = n >> 24;
1437 b[1] = n >> 16;
1438 b[2] = n >> 8;
1439 b[3] = n >> 0;
1442 static int
1443 write_order_cmp(const void *pa, const void *pb)
1445 struct got_pack_meta *a, *b, *ahd, *bhd;
1447 a = *(struct got_pack_meta **)pa;
1448 b = *(struct got_pack_meta **)pb;
1449 ahd = (a->head == NULL) ? a : a->head;
1450 bhd = (b->head == NULL) ? b : b->head;
1451 if (ahd->mtime != bhd->mtime)
1452 return bhd->mtime - ahd->mtime;
1453 if (ahd != bhd)
1454 return (uintptr_t)bhd - (uintptr_t)ahd;
1455 if (a->nchain != b->nchain)
1456 return a->nchain - b->nchain;
1457 return a->mtime - b->mtime;
1460 static int
1461 reuse_write_order_cmp(const void *pa, const void *pb)
1463 struct got_pack_meta *a, *b;
1465 a = *(struct got_pack_meta **)pa;
1466 b = *(struct got_pack_meta **)pb;
1468 if (a->reused_delta_offset < b->reused_delta_offset)
1469 return -1;
1470 if (a->reused_delta_offset > b->reused_delta_offset)
1471 return 1;
1472 return 0;
1475 static const struct got_error *
1476 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1478 size_t i;
1480 *hdrlen = 0;
1482 hdr[0] = obj_type << 4;
1483 hdr[0] |= len & 0xf;
1484 len >>= 4;
1485 for (i = 1; len != 0; i++){
1486 if (i >= bufsize)
1487 return got_error(GOT_ERR_NO_SPACE);
1488 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1489 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1490 len >>= GOT_DELTA_SIZE_SHIFT;
1493 *hdrlen = i;
1494 return NULL;
1497 static int
1498 packoff(char *hdr, off_t off)
1500 int i, j;
1501 char rbuf[8];
1503 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1504 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1505 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1506 GOT_DELTA_SIZE_MORE;
1509 j = 0;
1510 while (i > 0)
1511 hdr[j++] = rbuf[--i];
1512 return j;
1515 static const struct got_error *
1516 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1517 struct got_pack_meta *m)
1519 const struct got_error *err;
1520 char buf[32];
1521 int nh;
1523 if (m->prev->off != 0) {
1524 err = packhdr(&nh, buf, sizeof(buf),
1525 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1526 if (err)
1527 return err;
1528 nh += packoff(buf + nh, m->off - m->prev->off);
1529 err = hwrite(packfile, buf, nh, ctx);
1530 if (err)
1531 return err;
1532 *packfile_size += nh;
1533 } else {
1534 err = packhdr(&nh, buf, sizeof(buf),
1535 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1536 if (err)
1537 return err;
1538 err = hwrite(packfile, buf, nh, ctx);
1539 if (err)
1540 return err;
1541 *packfile_size += nh;
1542 err = hwrite(packfile, m->prev->id.sha1,
1543 sizeof(m->prev->id.sha1), ctx);
1544 if (err)
1545 return err;
1546 *packfile_size += sizeof(m->prev->id.sha1);
1549 return NULL;
1552 static const struct got_error *
1553 write_packed_object(off_t *packfile_size, FILE *packfile,
1554 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1555 SHA1_CTX *ctx, struct got_repository *repo)
1557 const struct got_error *err = NULL;
1558 struct got_deflate_checksum csum;
1559 char buf[32];
1560 int nh;
1561 struct got_raw_object *raw = NULL;
1562 off_t outlen;
1564 csum.output_sha1 = ctx;
1565 csum.output_crc = NULL;
1567 m->off = ftello(packfile);
1568 if (m->delta_len == 0) {
1569 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1570 if (err)
1571 goto done;
1572 err = packhdr(&nh, buf, sizeof(buf),
1573 m->obj_type, raw->size);
1574 if (err)
1575 goto done;
1576 err = hwrite(packfile, buf, nh, ctx);
1577 if (err)
1578 goto done;
1579 *packfile_size += nh;
1580 if (raw->f == NULL) {
1581 err = got_deflate_to_file_mmap(&outlen,
1582 raw->data + raw->hdrlen, 0, raw->size,
1583 packfile, &csum);
1584 if (err)
1585 goto done;
1586 } else {
1587 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1588 == -1) {
1589 err = got_error_from_errno("fseeko");
1590 goto done;
1592 err = got_deflate_to_file(&outlen, raw->f,
1593 raw->size, packfile, &csum);
1594 if (err)
1595 goto done;
1597 *packfile_size += outlen;
1598 got_object_raw_close(raw);
1599 raw = NULL;
1600 } else if (m->delta_buf) {
1601 err = deltahdr(packfile_size, ctx, packfile, m);
1602 if (err)
1603 goto done;
1604 err = got_deflate_to_file_mmap(&outlen,
1605 m->delta_buf, 0, m->delta_len, packfile, &csum);
1606 if (err)
1607 goto done;
1608 *packfile_size += outlen;
1609 free(m->delta_buf);
1610 m->delta_buf = NULL;
1611 } else {
1612 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1613 == -1) {
1614 err = got_error_from_errno("fseeko");
1615 goto done;
1617 err = deltahdr(packfile_size, ctx, packfile, m);
1618 if (err)
1619 goto done;
1620 err = got_deflate_to_file(&outlen, delta_cache,
1621 m->delta_len, packfile, &csum);
1622 if (err)
1623 goto done;
1624 *packfile_size += outlen;
1626 done:
1627 if (raw)
1628 got_object_raw_close(raw);
1629 return err;
1632 static const struct got_error *
1633 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1634 struct got_pack_meta **deltify, int ndeltify,
1635 struct got_pack_meta **reuse, int nreuse,
1636 int nours, struct got_repository *repo,
1637 got_pack_progress_cb progress_cb, void *progress_arg,
1638 struct got_ratelimit *rl,
1639 got_cancel_cb cancel_cb, void *cancel_arg)
1641 const struct got_error *err = NULL;
1642 int i;
1643 SHA1_CTX ctx;
1644 struct got_pack_meta *m;
1645 char buf[32];
1646 size_t n;
1647 off_t packfile_size = 0;
1648 int outfd = -1;
1650 SHA1Init(&ctx);
1652 err = hwrite(packfile, "PACK", 4, &ctx);
1653 if (err)
1654 return err;
1655 putbe32(buf, GOT_PACKFILE_VERSION);
1656 err = hwrite(packfile, buf, 4, &ctx);
1657 if (err)
1658 goto done;
1659 putbe32(buf, ndeltify + nreuse);
1660 err = hwrite(packfile, buf, 4, &ctx);
1661 if (err)
1662 goto done;
1664 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1665 write_order_cmp);
1666 for (i = 0; i < ndeltify; i++) {
1667 err = report_progress(progress_cb, progress_arg, rl,
1668 packfile_size, nours, ndeltify + nreuse,
1669 ndeltify + nreuse, i);
1670 if (err)
1671 goto done;
1672 m = deltify[i];
1673 err = write_packed_object(&packfile_size, packfile,
1674 delta_cache, m, &outfd, &ctx, repo);
1675 if (err)
1676 goto done;
1679 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1680 reuse_write_order_cmp);
1681 for (i = 0; i < nreuse; i++) {
1682 err = report_progress(progress_cb, progress_arg, rl,
1683 packfile_size, nours, ndeltify + nreuse,
1684 ndeltify + nreuse, ndeltify + i);
1685 if (err)
1686 goto done;
1687 m = reuse[i];
1688 err = write_packed_object(&packfile_size, packfile,
1689 delta_cache, m, &outfd, &ctx, repo);
1690 if (err)
1691 goto done;
1694 SHA1Final(pack_sha1, &ctx);
1695 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1696 if (n != SHA1_DIGEST_LENGTH)
1697 err = got_ferror(packfile, GOT_ERR_IO);
1698 packfile_size += SHA1_DIGEST_LENGTH;
1699 packfile_size += sizeof(struct got_packfile_hdr);
1700 if (progress_cb) {
1701 err = progress_cb(progress_arg, packfile_size, nours,
1702 ndeltify + nreuse, ndeltify + nreuse,
1703 ndeltify + nreuse);
1704 if (err)
1705 goto done;
1707 done:
1708 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1709 err = got_error_from_errno("close");
1710 return err;
1713 static const struct got_error *
1714 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1716 struct got_object_idset *idset = arg;
1718 if (got_object_idset_get_element_data(entry) == NULL)
1719 got_object_idset_remove_element(idset, entry);
1721 return NULL;
1724 static const struct got_error *
1725 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1727 struct got_object_idset *idset = arg;
1728 struct got_pack_meta *m;
1730 m = got_object_idset_get_element_data(entry);
1731 if (m->have_reused_delta)
1732 got_object_idset_remove_element(idset, entry);
1734 return NULL;
1737 static const struct got_error *
1738 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1740 struct got_pack_meta *m = data;
1741 struct got_pack_metavec *v = arg;
1743 return add_meta(m, v);
1746 const struct got_error *
1747 got_pack_create(uint8_t *packsha1, FILE *packfile,
1748 struct got_object_id **theirs, int ntheirs,
1749 struct got_object_id **ours, int nours,
1750 struct got_repository *repo, int loose_obj_only, int allow_empty,
1751 got_pack_progress_cb progress_cb, void *progress_arg,
1752 got_cancel_cb cancel_cb, void *cancel_arg)
1754 const struct got_error *err;
1755 int delta_cache_fd = -1;
1756 FILE *delta_cache = NULL;
1757 struct got_object_idset *idset;
1758 struct got_ratelimit rl;
1759 struct got_pack_metavec deltify, reuse;
1761 memset(&deltify, 0, sizeof(deltify));
1762 memset(&reuse, 0, sizeof(reuse));
1764 got_ratelimit_init(&rl, 0, 500);
1766 idset = got_object_idset_alloc();
1767 if (idset == NULL)
1768 return got_error_from_errno("got_object_idset_alloc");
1770 err = load_object_ids(idset, theirs, ntheirs, ours, nours,
1771 repo, loose_obj_only, cancel_cb, cancel_arg);
1772 if (err)
1773 return err;
1775 err = got_object_idset_for_each_element(idset,
1776 remove_unused_object, idset);
1777 if (err)
1778 goto done;
1780 if (progress_cb) {
1781 err = progress_cb(progress_arg, 0L, nours,
1782 got_object_idset_num_elements(idset), 0, 0);
1783 if (err)
1784 goto done;
1787 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1788 err = got_error(GOT_ERR_CANNOT_PACK);
1789 goto done;
1792 delta_cache_fd = got_opentempfd();
1793 if (delta_cache_fd == -1) {
1794 err = got_error_from_errno("got_opentemp");
1795 goto done;
1798 reuse.metasz = 64;
1799 reuse.meta = calloc(reuse.metasz,
1800 sizeof(struct got_pack_meta *));
1801 if (reuse.meta == NULL) {
1802 err = got_error_from_errno("calloc");
1803 goto done;
1806 err = search_deltas(&reuse, idset, delta_cache_fd, nours, repo,
1807 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1808 if (err)
1809 goto done;
1810 if (reuse.nmeta > 0) {
1811 err = got_object_idset_for_each_element(idset,
1812 remove_reused_object, idset);
1813 if (err)
1814 goto done;
1817 delta_cache = fdopen(delta_cache_fd, "a+");
1818 if (delta_cache == NULL) {
1819 err = got_error_from_errno("fdopen");
1820 goto done;
1822 delta_cache_fd = -1;
1824 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1825 err = got_error_from_errno("fseeko");
1826 goto done;
1829 deltify.meta = calloc(got_object_idset_num_elements(idset),
1830 sizeof(struct got_pack_meta *));
1831 if (deltify.meta == NULL) {
1832 err = got_error_from_errno("calloc");
1833 goto done;
1835 deltify.metasz = got_object_idset_num_elements(idset);
1837 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1838 if (err)
1839 goto done;
1840 if (deltify.nmeta > 0) {
1841 err = pick_deltas(deltify.meta, deltify.nmeta, nours,
1842 reuse.nmeta, delta_cache, repo,
1843 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1844 if (err)
1845 goto done;
1846 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1847 err = got_error_from_errno("fseeko");
1848 goto done;
1852 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1853 deltify.nmeta, reuse.meta, reuse.nmeta, nours, repo,
1854 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1855 if (err)
1856 goto done;
1857 done:
1858 free_nmeta(deltify.meta, deltify.nmeta);
1859 free_nmeta(reuse.meta, reuse.nmeta);
1860 got_object_idset_free(idset);
1861 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1862 err = got_error_from_errno("close");
1863 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1864 err = got_error_from_errno("fclose");
1865 return err;