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 <unistd.h>
34 #include <limits.h>
35 #include <zlib.h>
37 #include "got_error.h"
38 #include "got_cancel.h"
39 #include "got_object.h"
40 #include "got_path.h"
41 #include "got_reference.h"
42 #include "got_repository_admin.h"
43 #include "got_opentemp.h"
45 #include "got_lib_deltify.h"
46 #include "got_lib_delta.h"
47 #include "got_lib_object.h"
48 #include "got_lib_object_idset.h"
49 #include "got_lib_object_cache.h"
50 #include "got_lib_deflate.h"
51 #include "got_lib_pack.h"
52 #include "got_lib_privsep.h"
53 #include "got_lib_repository.h"
54 #include "got_lib_ratelimit.h"
56 #ifndef MIN
57 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 #endif
60 #ifndef MAX
61 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 #endif
64 struct got_pack_meta {
65 struct got_object_id id;
66 char *path;
67 int obj_type;
68 off_t size;
69 time_t mtime;
71 /* The best delta we picked */
72 struct got_pack_meta *head;
73 struct got_pack_meta *prev;
74 unsigned char *delta_buf; /* if not encoded in delta cache file */
75 off_t delta_offset; /* offset in delta cache file */
76 off_t delta_len; /* encoded delta length */
77 int nchain;
79 int have_reused_delta;
80 off_t reused_delta_offset; /* offset of delta in reused pack file */
81 struct got_object_id *base_obj_id;
83 /* Only used for delta window */
84 struct got_delta_table *dtab;
86 /* Only used for writing offset deltas */
87 off_t off;
88 };
90 struct got_pack_metavec {
91 struct got_pack_meta **meta;
92 int nmeta;
93 int metasz;
94 };
96 static const struct got_error *
97 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
98 const char *path, int obj_type, time_t mtime)
99 {
100 const struct got_error *err = NULL;
101 struct got_pack_meta *m;
103 *new = NULL;
105 m = calloc(1, sizeof(*m));
106 if (m == NULL)
107 return got_error_from_errno("calloc");
109 memcpy(&m->id, id, sizeof(m->id));
111 m->path = strdup(path);
112 if (m->path == NULL) {
113 err = got_error_from_errno("strdup");
114 free(m);
115 return err;
118 m->obj_type = obj_type;
119 m->mtime = mtime;
120 *new = m;
121 return NULL;
124 static void
125 clear_meta(struct got_pack_meta *meta)
127 if (meta == NULL)
128 return;
129 free(meta->path);
130 meta->path = NULL;
131 free(meta->delta_buf);
132 meta->delta_buf = NULL;
133 free(meta->base_obj_id);
134 meta->base_obj_id = NULL;
137 static void
138 free_nmeta(struct got_pack_meta **meta, int nmeta)
140 int i;
142 for (i = 0; i < nmeta; i++)
143 clear_meta(meta[i]);
144 free(meta);
147 static int
148 delta_order_cmp(const void *pa, const void *pb)
150 struct got_pack_meta *a, *b;
151 int cmp;
153 a = *(struct got_pack_meta **)pa;
154 b = *(struct got_pack_meta **)pb;
156 if (a->obj_type != b->obj_type)
157 return a->obj_type - b->obj_type;
158 cmp = strcmp(a->path, b->path);
159 if (cmp != 0)
160 return cmp;
161 if (a->mtime != b->mtime)
162 return a->mtime - b->mtime;
163 return got_object_id_cmp(&a->id, &b->id);
166 static off_t
167 delta_size(struct got_delta_instruction *deltas, int ndeltas)
169 int i;
170 off_t size = 32;
171 for (i = 0; i < ndeltas; i++) {
172 if (deltas[i].copy)
173 size += GOT_DELTA_SIZE_SHIFT;
174 else
175 size += deltas[i].len + 1;
177 return size;
180 static const struct got_error *
181 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
183 char *n;
185 if (*len + nseg >= *sz) {
186 while (*len + nseg >= *sz)
187 *sz += *sz / 2;
188 n = realloc(*p, *sz);
189 if (n == NULL)
190 return got_error_from_errno("realloc");
191 *p = n;
193 memcpy(*p + *len, seg, nseg);
194 *len += nseg;
195 return NULL;
198 static const struct got_error *
199 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
200 struct got_delta_instruction *deltas, int ndeltas,
201 off_t delta_size, off_t base_size)
203 const struct got_error *err;
204 unsigned char buf[16], *bp;
205 int i, j;
206 size_t len = 0;
207 off_t n;
208 struct got_delta_instruction *d;
210 m->delta_buf = malloc(delta_size);
211 if (m->delta_buf == NULL)
212 return got_error_from_errno("calloc");
214 /* base object size */
215 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
216 n = base_size >> GOT_DELTA_SIZE_SHIFT;
217 for (i = 1; n > 0; i++) {
218 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
219 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
220 n >>= GOT_DELTA_SIZE_SHIFT;
222 err = append(&m->delta_buf, &len, &delta_size, buf, i);
223 if (err)
224 return err;
226 /* target object size */
227 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
228 n = o->size >> GOT_DELTA_SIZE_SHIFT;
229 for (i = 1; n > 0; i++) {
230 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
231 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
232 n >>= GOT_DELTA_SIZE_SHIFT;
234 err = append(&m->delta_buf, &len, &delta_size, buf, i);
235 if (err)
236 return err;
238 for (j = 0; j < ndeltas; j++) {
239 d = &deltas[j];
240 if (d->copy) {
241 n = d->offset;
242 bp = &buf[1];
243 buf[0] = GOT_DELTA_BASE_COPY;
244 for (i = 0; i < 4; i++) {
245 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
246 buf[0] |= 1 << i;
247 *bp++ = n & 0xff;
248 n >>= 8;
249 if (n == 0)
250 break;
253 n = d->len;
254 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
255 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
256 for (i = 0; i < 3 && n > 0; i++) {
257 buf[0] |= 1 << (i + 4);
258 *bp++ = n & 0xff;
259 n >>= 8;
262 err = append(&m->delta_buf, &len, &delta_size,
263 buf, bp - buf);
264 if (err)
265 return err;
266 } else if (o->f == NULL) {
267 n = 0;
268 while (n != d->len) {
269 buf[0] = (d->len - n < 127) ? d->len - n : 127;
270 err = append(&m->delta_buf, &len, &delta_size,
271 buf, 1);
272 if (err)
273 return err;
274 err = append(&m->delta_buf, &len, &delta_size,
275 o->data + o->hdrlen + d->offset + n,
276 buf[0]);
277 if (err)
278 return err;
279 n += buf[0];
281 } else {
282 char content[128];
283 size_t r;
284 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
285 return got_error_from_errno("fseeko");
286 n = 0;
287 while (n != d->len) {
288 buf[0] = (d->len - n < 127) ? d->len - n : 127;
289 err = append(&m->delta_buf, &len, &delta_size,
290 buf, 1);
291 if (err)
292 return err;
293 r = fread(content, 1, buf[0], o->f);
294 if (r != buf[0])
295 return got_ferror(o->f, GOT_ERR_IO);
296 err = append(&m->delta_buf, &len, &delta_size,
297 content, buf[0]);
298 if (err)
299 return err;
300 n += buf[0];
305 m->delta_len = len;
306 return NULL;
309 static const struct got_error *
310 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
311 struct got_delta_instruction *deltas, int ndeltas,
312 off_t base_size, FILE *f)
314 unsigned char buf[16], *bp;
315 int i, j;
316 off_t n;
317 size_t w;
318 struct got_delta_instruction *d;
320 /* base object size */
321 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
322 n = base_size >> GOT_DELTA_SIZE_SHIFT;
323 for (i = 1; n > 0; i++) {
324 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
325 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
326 n >>= GOT_DELTA_SIZE_SHIFT;
328 w = fwrite(buf, 1, i, f);
329 if (w != i)
330 return got_ferror(f, GOT_ERR_IO);
332 /* target object size */
333 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
334 n = o->size >> GOT_DELTA_SIZE_SHIFT;
335 for (i = 1; n > 0; i++) {
336 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
337 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
338 n >>= GOT_DELTA_SIZE_SHIFT;
340 w = fwrite(buf, 1, i, f);
341 if (w != i)
342 return got_ferror(f, GOT_ERR_IO);
344 for (j = 0; j < ndeltas; j++) {
345 d = &deltas[j];
346 if (d->copy) {
347 n = d->offset;
348 bp = &buf[1];
349 buf[0] = GOT_DELTA_BASE_COPY;
350 for (i = 0; i < 4; i++) {
351 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
352 buf[0] |= 1 << i;
353 *bp++ = n & 0xff;
354 n >>= 8;
355 if (n == 0)
356 break;
359 n = d->len;
360 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
361 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
362 for (i = 0; i < 3 && n > 0; i++) {
363 buf[0] |= 1 << (i + 4);
364 *bp++ = n & 0xff;
365 n >>= 8;
368 w = fwrite(buf, 1, bp - buf, f);
369 if (w != bp - buf)
370 return got_ferror(f, GOT_ERR_IO);
371 } else if (o->f == NULL) {
372 n = 0;
373 while (n != d->len) {
374 buf[0] = (d->len - n < 127) ? d->len - n : 127;
375 w = fwrite(buf, 1, 1, f);
376 if (w != 1)
377 return got_ferror(f, GOT_ERR_IO);
378 w = fwrite(o->data + o->hdrlen + d->offset + n,
379 1, buf[0], f);
380 if (w != buf[0])
381 return got_ferror(f, GOT_ERR_IO);
382 n += buf[0];
384 } else {
385 char content[128];
386 size_t r;
387 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
388 return got_error_from_errno("fseeko");
389 n = 0;
390 while (n != d->len) {
391 buf[0] = (d->len - n < 127) ? d->len - n : 127;
392 w = fwrite(buf, 1, 1, f);
393 if (w != 1)
394 return got_ferror(f, GOT_ERR_IO);
395 r = fread(content, 1, buf[0], o->f);
396 if (r != buf[0])
397 return got_ferror(o->f, GOT_ERR_IO);
398 w = fwrite(content, 1, buf[0], f);
399 if (w != buf[0])
400 return got_ferror(f, GOT_ERR_IO);
401 n += buf[0];
406 m->delta_len = ftello(f) - m->delta_offset;
407 return NULL;
410 static const struct got_error *
411 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
412 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
413 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
414 int nobj_written)
416 const struct got_error *err;
417 int elapsed;
419 if (progress_cb == NULL)
420 return NULL;
422 err = got_ratelimit_check(&elapsed, rl);
423 if (err || !elapsed)
424 return err;
426 return progress_cb(progress_arg, ncolored, nfound, ntrees,
427 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
430 static const struct got_error *
431 add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
433 if (v->nmeta == v->metasz){
434 size_t newsize = 2 * v->metasz;
435 struct got_pack_meta **new;
436 new = reallocarray(v->meta, newsize, sizeof(*new));
437 if (new == NULL)
438 return got_error_from_errno("reallocarray");
439 v->meta = new;
440 v->metasz = newsize;
443 v->meta[v->nmeta++] = m;
444 return NULL;
447 static const struct got_error *
448 reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
449 struct got_object_idset *idset, struct got_pack *pack,
450 struct got_packidx *packidx, int delta_cache_fd,
451 struct got_repository *repo)
453 const struct got_error *err = NULL;
454 struct got_pack_meta *base = NULL;
455 struct got_object_id *base_obj_id = NULL;
456 off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
457 uint64_t base_size, result_size;
459 if (m->have_reused_delta)
460 return NULL;
462 err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
463 &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
464 packidx, idx, &m->id, repo);
465 if (err)
466 return err;
468 if (delta_offset + delta_len < delta_offset)
469 return got_error(GOT_ERR_BAD_PACKFILE);
471 base = got_object_idset_get(idset, base_obj_id);
472 if (base == NULL)
473 goto done;
475 m->delta_len = delta_len;
476 m->delta_offset = delta_cache_offset;
477 m->prev = base;
478 m->size = result_size;
479 m->have_reused_delta = 1;
480 m->reused_delta_offset = delta_offset;
481 m->base_obj_id = base_obj_id;
482 base_obj_id = NULL;
483 err = add_meta(m, v);
484 done:
485 free(base_obj_id);
486 return err;
489 static const struct got_error *
490 find_pack_for_reuse(struct got_packidx **best_packidx,
491 struct got_repository *repo)
493 const struct got_error *err = NULL;
494 struct got_pathlist_entry *pe;
495 const char *best_packidx_path = NULL;
496 int nobj_max = 0;
498 *best_packidx = NULL;
500 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
501 const char *path_packidx = pe->path;
502 struct got_packidx *packidx;
503 int nobj;
505 err = got_repo_get_packidx(&packidx, path_packidx, repo);
506 if (err)
507 break;
509 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
510 if (nobj > nobj_max) {
511 best_packidx_path = path_packidx;
512 nobj_max = nobj;
516 if (best_packidx_path) {
517 err = got_repo_get_packidx(best_packidx, best_packidx_path,
518 repo);
521 return err;
524 struct search_deltas_arg {
525 struct got_packidx *packidx;
526 struct got_pack *pack;
527 struct got_object_idset *idset;
528 struct got_pack_metavec *v;
529 int delta_cache_fd;
530 struct got_repository *repo;
531 got_pack_progress_cb progress_cb;
532 void *progress_arg;
533 struct got_ratelimit *rl;
534 got_cancel_cb cancel_cb;
535 void *cancel_arg;
536 int ncolored;
537 int nfound;
538 int ntrees;
539 int ncommits;
540 };
542 static const struct got_error *
543 search_delta_for_object(struct got_object_id *id, void *data, void *arg)
545 const struct got_error *err;
546 struct got_pack_meta *m = data;
547 struct search_deltas_arg *a = arg;
548 int obj_idx;
549 struct got_object *obj = NULL;
551 if (a->cancel_cb) {
552 err = (*a->cancel_cb)(a->cancel_arg);
553 if (err)
554 return err;
557 if (!got_repo_check_packidx_bloom_filter(a->repo,
558 a->packidx->path_packidx, id))
559 return NULL;
561 obj_idx = got_packidx_get_object_idx(a->packidx, id);
562 if (obj_idx == -1)
563 return NULL;
565 /* TODO:
566 * Opening and closing an object just to check its flags
567 * is a bit expensive. We could have an imsg which requests
568 * plain type/size information for an object without doing
569 * work such as traversing the object's entire delta chain
570 * to find the base object type, and other such info which
571 * we don't really need here.
572 */
573 err = got_object_open_from_packfile(&obj, &m->id, a->pack,
574 a->packidx, obj_idx, a->repo);
575 if (err)
576 return err;
578 if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
579 reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
580 a->delta_cache_fd, a->repo);
581 if (err)
582 goto done;
583 err = report_progress(a->progress_cb, a->progress_arg, a->rl,
584 a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
585 got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
587 done:
588 got_object_close(obj);
589 return err;
592 static const struct got_error *
593 search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
594 int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
595 struct got_repository *repo,
596 got_pack_progress_cb progress_cb, void *progress_arg,
597 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
599 const struct got_error *err = NULL;
600 char *path_packfile = NULL;
601 struct got_packidx *packidx;
602 struct got_pack *pack;
603 struct search_deltas_arg sda;
605 err = find_pack_for_reuse(&packidx, repo);
606 if (err)
607 return err;
609 if (packidx == NULL)
610 return NULL;
612 err = got_packidx_get_packfile_path(&path_packfile,
613 packidx->path_packidx);
614 if (err)
615 return err;
617 pack = got_repo_get_cached_pack(repo, path_packfile);
618 if (pack == NULL) {
619 err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
620 if (err)
621 goto done;
624 sda.packidx = packidx;
625 sda.pack = pack;
626 sda.idset = idset;
627 sda.v = v;
628 sda.delta_cache_fd = delta_cache_fd;
629 sda.repo = repo;
630 sda.progress_cb = progress_cb;
631 sda.progress_arg = progress_arg;
632 sda.rl = rl;
633 sda.cancel_cb = cancel_cb;
634 sda.cancel_arg = cancel_arg;
635 sda.ncolored = ncolored;
636 sda.nfound = nfound;
637 sda.ntrees = ntrees;
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 ncolored,
647 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
648 struct got_repository *repo,
649 got_pack_progress_cb progress_cb, void *progress_arg,
650 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
652 const struct got_error *err = NULL;
653 struct got_pack_meta *m = NULL, *base = NULL;
654 struct got_raw_object *raw = NULL, *base_raw = NULL;
655 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
656 int i, j, ndeltas, best_ndeltas;
657 off_t size, best_size;
658 const int max_base_candidates = 3;
659 size_t delta_memsize = 0;
660 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
661 int outfd = -1;
663 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
664 for (i = 0; i < nmeta; i++) {
665 if (cancel_cb) {
666 err = (*cancel_cb)(cancel_arg);
667 if (err)
668 break;
670 err = report_progress(progress_cb, progress_arg, rl,
671 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
672 nreused + i, 0);
673 if (err)
674 goto done;
675 m = meta[i];
677 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
678 m->obj_type == GOT_OBJ_TYPE_TAG)
679 continue;
681 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
682 if (err)
683 goto done;
684 m->size = raw->size;
686 if (raw->f == NULL) {
687 err = got_deltify_init_mem(&m->dtab, raw->data,
688 raw->hdrlen, raw->size + raw->hdrlen);
689 } else {
690 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
691 raw->size + raw->hdrlen);
693 if (err)
694 goto done;
696 if (i > max_base_candidates) {
697 struct got_pack_meta *n = NULL;
698 n = meta[i - (max_base_candidates + 1)];
699 got_deltify_free(n->dtab);
700 n->dtab = NULL;
703 best_size = raw->size;
704 best_ndeltas = 0;
705 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
706 if (cancel_cb) {
707 err = (*cancel_cb)(cancel_arg);
708 if (err)
709 goto done;
711 base = meta[j];
712 /* long chains make unpacking slow, avoid such bases */
713 if (base->nchain >= 128 ||
714 base->obj_type != m->obj_type)
715 continue;
717 err = got_object_raw_open(&base_raw, &outfd, repo,
718 &base->id);
719 if (err)
720 goto done;
722 if (raw->f == NULL && base_raw->f == NULL) {
723 err = got_deltify_mem_mem(&deltas, &ndeltas,
724 raw->data, raw->hdrlen,
725 raw->size + raw->hdrlen,
726 base->dtab, base_raw->data,
727 base_raw->hdrlen,
728 base_raw->size + base_raw->hdrlen);
729 } else if (raw->f == NULL) {
730 err = got_deltify_mem_file(&deltas, &ndeltas,
731 raw->data, raw->hdrlen,
732 raw->size + raw->hdrlen,
733 base->dtab, base_raw->f,
734 base_raw->hdrlen,
735 base_raw->size + base_raw->hdrlen);
736 } else if (base_raw->f == NULL) {
737 err = got_deltify_file_mem(&deltas, &ndeltas,
738 raw->f, raw->hdrlen,
739 raw->size + raw->hdrlen,
740 base->dtab, base_raw->data,
741 base_raw->hdrlen,
742 base_raw->size + base_raw->hdrlen);
743 } else {
744 err = got_deltify(&deltas, &ndeltas,
745 raw->f, raw->hdrlen,
746 raw->size + raw->hdrlen,
747 base->dtab, base_raw->f, base_raw->hdrlen,
748 base_raw->size + base_raw->hdrlen);
750 got_object_raw_close(base_raw);
751 base_raw = NULL;
752 if (err)
753 goto done;
755 size = delta_size(deltas, ndeltas);
756 if (size + 32 < best_size){
757 /*
758 * if we already picked a best delta,
759 * replace it.
760 */
761 best_size = size;
762 free(best_deltas);
763 best_deltas = deltas;
764 best_ndeltas = ndeltas;
765 deltas = NULL;
766 m->nchain = base->nchain + 1;
767 m->prev = base;
768 m->head = base->head;
769 if (m->head == NULL)
770 m->head = base;
771 } else {
772 free(deltas);
773 deltas = NULL;
774 ndeltas = 0;
778 if (best_ndeltas > 0) {
779 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
780 delta_memsize + best_size <= max_delta_memsize) {
781 delta_memsize += best_size;
782 err = encode_delta_in_mem(m, raw, best_deltas,
783 best_ndeltas, best_size, m->prev->size);
784 } else {
785 m->delta_offset = ftello(delta_cache);
786 /*
787 * TODO:
788 * Storing compressed delta data in the delta
789 * cache file would probably be more efficient
790 * than writing uncompressed delta data here
791 * and compressing it while writing the pack
792 * file. This would also allow for reusing
793 * deltas in their compressed form.
794 */
795 err = encode_delta(m, raw, best_deltas,
796 best_ndeltas, m->prev->size, delta_cache);
798 free(best_deltas);
799 best_deltas = NULL;
800 best_ndeltas = 0;
801 if (err)
802 goto done;
805 got_object_raw_close(raw);
806 raw = NULL;
808 done:
809 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
810 got_deltify_free(meta[i]->dtab);
811 meta[i]->dtab = NULL;
813 if (raw)
814 got_object_raw_close(raw);
815 if (base_raw)
816 got_object_raw_close(base_raw);
817 if (outfd != -1 && close(outfd) == -1 && err == NULL)
818 err = got_error_from_errno("close");
819 free(deltas);
820 free(best_deltas);
821 return err;
824 static const struct got_error *
825 search_packidx(int *found, struct got_object_id *id,
826 struct got_repository *repo)
828 const struct got_error *err = NULL;
829 struct got_packidx *packidx = NULL;
830 int idx;
832 *found = 0;
834 err = got_repo_search_packidx(&packidx, &idx, repo, id);
835 if (err == NULL)
836 *found = 1; /* object is already packed */
837 else if (err->code == GOT_ERR_NO_OBJ)
838 err = NULL;
839 return err;
842 static const int obj_types[] = {
843 GOT_OBJ_TYPE_ANY,
844 GOT_OBJ_TYPE_COMMIT,
845 GOT_OBJ_TYPE_TREE,
846 GOT_OBJ_TYPE_BLOB,
847 GOT_OBJ_TYPE_TAG,
848 GOT_OBJ_TYPE_OFFSET_DELTA,
849 GOT_OBJ_TYPE_REF_DELTA
850 };
852 static const struct got_error *
853 add_object(int want_meta, struct got_object_idset *idset,
854 struct got_object_id *id, const char *path, int obj_type,
855 time_t mtime, int loose_obj_only, struct got_repository *repo)
857 const struct got_error *err;
858 struct got_pack_meta *m = NULL;
860 if (loose_obj_only) {
861 int is_packed;
862 err = search_packidx(&is_packed, id, repo);
863 if (err)
864 return err;
865 if (is_packed && want_meta)
866 return NULL;
869 if (want_meta) {
870 err = alloc_meta(&m, id, path, obj_type, mtime);
871 if (err)
872 return err;
875 return got_object_idset_add(idset, id, m);
878 static const struct got_error *
879 load_tree_entries(struct got_object_id_queue *ids, int want_meta,
880 struct got_object_idset *idset, struct got_object_id *tree_id,
881 const char *dpath, time_t mtime, struct got_repository *repo,
882 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
883 got_pack_progress_cb progress_cb, void *progress_arg,
884 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
886 const struct got_error *err;
887 struct got_tree_object *tree;
888 char *p = NULL;
889 int i;
891 err = got_object_open_as_tree(&tree, repo, tree_id);
892 if (err)
893 return err;
895 (*ntrees)++;
896 err = report_progress(progress_cb, progress_arg, rl,
897 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
898 if (err)
899 return err;
901 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
902 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
903 struct got_object_id *id = got_tree_entry_get_id(e);
904 mode_t mode = got_tree_entry_get_mode(e);
906 if (cancel_cb) {
907 err = (*cancel_cb)(cancel_arg);
908 if (err)
909 break;
912 if (got_object_tree_entry_is_submodule(e) ||
913 got_object_idset_contains(idset, id))
914 continue;
916 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
917 got_tree_entry_get_name(e)) == -1) {
918 err = got_error_from_errno("asprintf");
919 break;
922 if (S_ISDIR(mode)) {
923 struct got_object_qid *qid;
924 err = got_object_qid_alloc(&qid, id);
925 if (err)
926 break;
927 STAILQ_INSERT_TAIL(ids, qid, entry);
928 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
929 err = add_object(want_meta, idset, id, p,
930 GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo);
931 if (err)
932 break;
933 if (want_meta) {
934 (*nfound)++;
935 err = report_progress(progress_cb, progress_arg,
936 rl, *ncolored, *nfound, *ntrees,
937 0L, 0, 0, 0, 0);
938 if (err)
939 break;
942 free(p);
943 p = NULL;
946 got_object_tree_close(tree);
947 free(p);
948 return err;
951 static const struct got_error *
952 load_tree(int want_meta, struct got_object_idset *idset,
953 struct got_object_id *tree_id, const char *dpath, time_t mtime,
954 struct got_repository *repo, int loose_obj_only,
955 int *ncolored, int *nfound, int *ntrees,
956 got_pack_progress_cb progress_cb, void *progress_arg,
957 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
959 const struct got_error *err = NULL;
960 struct got_object_id_queue tree_ids;
961 struct got_object_qid *qid;
963 if (got_object_idset_contains(idset, tree_id))
964 return NULL;
966 err = got_object_qid_alloc(&qid, tree_id);
967 if (err)
968 return err;
970 STAILQ_INIT(&tree_ids);
971 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
973 while (!STAILQ_EMPTY(&tree_ids)) {
974 if (cancel_cb) {
975 err = (*cancel_cb)(cancel_arg);
976 if (err)
977 break;
980 qid = STAILQ_FIRST(&tree_ids);
981 STAILQ_REMOVE_HEAD(&tree_ids, entry);
983 if (got_object_idset_contains(idset, qid->id)) {
984 got_object_qid_free(qid);
985 continue;
988 err = add_object(want_meta, idset, qid->id, dpath,
989 GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo);
990 if (err) {
991 got_object_qid_free(qid);
992 break;
995 if (want_meta) {
996 (*nfound)++;
997 err = report_progress(progress_cb, progress_arg, rl,
998 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
999 if (err)
1000 break;
1003 err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
1004 dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1005 ntrees, progress_cb, progress_arg, rl,
1006 cancel_cb, cancel_arg);
1007 got_object_qid_free(qid);
1008 if (err)
1009 break;
1012 got_object_id_queue_free(&tree_ids);
1013 return err;
1016 static const struct got_error *
1017 load_commit(int want_meta, struct got_object_idset *idset,
1018 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1019 int *ncolored, int *nfound, int *ntrees,
1020 got_pack_progress_cb progress_cb, void *progress_arg,
1021 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1023 const struct got_error *err;
1024 struct got_commit_object *commit;
1026 if (got_object_idset_contains(idset, id))
1027 return NULL;
1029 if (loose_obj_only) {
1030 int is_packed;
1031 err = search_packidx(&is_packed, id, repo);
1032 if (err)
1033 return err;
1034 if (is_packed && want_meta)
1035 return NULL;
1038 err = got_object_open_as_commit(&commit, repo, id);
1039 if (err)
1040 return err;
1042 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1043 got_object_commit_get_committer_time(commit),
1044 loose_obj_only, repo);
1045 if (err)
1046 goto done;
1048 if (want_meta) {
1049 (*nfound)++;
1050 err = report_progress(progress_cb, progress_arg, rl,
1051 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
1052 if (err)
1053 goto done;
1056 err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1057 "", got_object_commit_get_committer_time(commit),
1058 repo, loose_obj_only, ncolored, nfound, ntrees,
1059 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1060 done:
1061 got_object_commit_close(commit);
1062 return err;
1065 static const struct got_error *
1066 load_tag(int want_meta, struct got_object_idset *idset,
1067 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1068 int *ncolored, int *nfound, int *ntrees,
1069 got_pack_progress_cb progress_cb, void *progress_arg,
1070 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1072 const struct got_error *err;
1073 struct got_tag_object *tag = NULL;
1075 if (got_object_idset_contains(idset, id))
1076 return NULL;
1078 if (loose_obj_only) {
1079 int is_packed;
1080 err = search_packidx(&is_packed, id, repo);
1081 if (err)
1082 return err;
1083 if (is_packed && want_meta)
1084 return NULL;
1087 err = got_object_open_as_tag(&tag, repo, id);
1088 if (err)
1089 return err;
1091 err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1092 got_object_tag_get_tagger_time(tag),
1093 loose_obj_only, repo);
1094 if (err)
1095 goto done;
1097 if (want_meta) {
1098 (*nfound)++;
1099 err = report_progress(progress_cb, progress_arg, rl,
1100 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
1101 if (err)
1102 goto done;
1105 switch (got_object_tag_get_object_type(tag)) {
1106 case GOT_OBJ_TYPE_COMMIT:
1107 err = load_commit(want_meta, idset,
1108 got_object_tag_get_object_id(tag), repo, loose_obj_only,
1109 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1110 cancel_cb, cancel_arg);
1111 break;
1112 case GOT_OBJ_TYPE_TREE:
1113 err = load_tree(want_meta, idset,
1114 got_object_tag_get_object_id(tag), "",
1115 got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1116 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1117 cancel_cb, cancel_arg);
1118 break;
1119 default:
1120 break;
1123 done:
1124 got_object_tag_close(tag);
1125 return err;
1128 enum findtwixt_color {
1129 COLOR_KEEP = 0,
1130 COLOR_DROP,
1131 COLOR_BLANK,
1133 static const int findtwixt_colors[] = {
1134 COLOR_KEEP,
1135 COLOR_DROP,
1136 COLOR_BLANK
1139 static const struct got_error *
1140 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1141 int color, struct got_repository *repo)
1143 const struct got_error *err;
1144 struct got_object_qid *qid;
1146 err = got_object_qid_alloc(&qid, id);
1147 if (err)
1148 return err;
1150 STAILQ_INSERT_TAIL(ids, qid, entry);
1151 qid->data = (void *)&findtwixt_colors[color];
1152 return NULL;
1155 static const struct got_error *
1156 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1157 struct got_object_id *id, struct got_repository *repo,
1158 got_cancel_cb cancel_cb, void *cancel_arg)
1160 const struct got_error *err = NULL;
1161 struct got_commit_object *commit;
1162 const struct got_object_id_queue *parents;
1163 struct got_object_id_queue ids;
1164 struct got_object_qid *qid;
1166 STAILQ_INIT(&ids);
1168 err = got_object_qid_alloc(&qid, id);
1169 if (err)
1170 return err;
1171 STAILQ_INSERT_HEAD(&ids, qid, entry);
1173 while (!STAILQ_EMPTY(&ids)) {
1174 if (cancel_cb) {
1175 err = (*cancel_cb)(cancel_arg);
1176 if (err)
1177 break;
1180 qid = STAILQ_FIRST(&ids);
1181 STAILQ_REMOVE_HEAD(&ids, entry);
1183 if (got_object_idset_contains(drop, qid->id)) {
1184 got_object_qid_free(qid);
1185 continue;
1188 err = got_object_idset_add(drop, qid->id,
1189 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1190 if (err) {
1191 got_object_qid_free(qid);
1192 break;
1195 if (!got_object_idset_contains(keep, qid->id)) {
1196 got_object_qid_free(qid);
1197 continue;
1200 err = got_object_open_as_commit(&commit, repo, qid->id);
1201 got_object_qid_free(qid);
1202 if (err)
1203 break;
1205 parents = got_object_commit_get_parent_ids(commit);
1206 if (parents) {
1207 err = got_object_id_queue_copy(parents, &ids);
1208 if (err) {
1209 got_object_commit_close(commit);
1210 break;
1213 got_object_commit_close(commit);
1216 got_object_id_queue_free(&ids);
1217 return err;
1220 struct append_id_arg {
1221 struct got_object_id **array;
1222 int idx;
1225 static const struct got_error *
1226 append_id(struct got_object_id *id, void *data, void *arg)
1228 struct append_id_arg *a = arg;
1230 a->array[a->idx] = got_object_id_dup(id);
1231 if (a->array[a->idx] == NULL)
1232 return got_error_from_errno("got_object_id_dup");
1234 a->idx++;
1235 return NULL;
1238 static const struct got_error *
1239 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1240 struct got_object_id **head, int nhead,
1241 struct got_object_id **tail, int ntail,
1242 struct got_repository *repo,
1243 got_pack_progress_cb progress_cb, void *progress_arg,
1244 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1246 const struct got_error *err = NULL;
1247 struct got_object_id_queue ids;
1248 struct got_object_idset *keep, *drop;
1249 struct got_object_qid *qid;
1250 int i, ncolor, nkeep, obj_type;
1252 STAILQ_INIT(&ids);
1253 *res = NULL;
1254 *nres = 0;
1255 *ncolored = 0;
1257 keep = got_object_idset_alloc();
1258 if (keep == NULL)
1259 return got_error_from_errno("got_object_idset_alloc");
1261 drop = got_object_idset_alloc();
1262 if (drop == NULL) {
1263 err = got_error_from_errno("got_object_idset_alloc");
1264 goto done;
1267 for (i = 0; i < nhead; i++) {
1268 struct got_object_id *id = head[i];
1269 if (id == NULL)
1270 continue;
1271 err = got_object_get_type(&obj_type, repo, id);
1272 if (err)
1273 return err;
1274 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1275 continue;
1276 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1277 if (err)
1278 goto done;
1280 for (i = 0; i < ntail; i++) {
1281 struct got_object_id *id = tail[i];
1282 if (id == NULL)
1283 continue;
1284 err = got_object_get_type(&obj_type, repo, id);
1285 if (err)
1286 return err;
1287 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1288 continue;
1289 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1290 if (err)
1291 goto done;
1294 while (!STAILQ_EMPTY(&ids)) {
1295 int qcolor;
1296 qid = STAILQ_FIRST(&ids);
1297 qcolor = *((int *)qid->data);
1299 if (got_object_idset_contains(drop, qid->id))
1300 ncolor = COLOR_DROP;
1301 else if (got_object_idset_contains(keep, qid->id))
1302 ncolor = COLOR_KEEP;
1303 else
1304 ncolor = COLOR_BLANK;
1306 (*ncolored)++;
1307 err = report_progress(progress_cb, progress_arg, rl,
1308 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1309 if (err)
1310 goto done;
1312 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1313 qcolor == COLOR_KEEP)) {
1314 STAILQ_REMOVE_HEAD(&ids, entry);
1315 got_object_qid_free(qid);
1316 continue;
1319 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1320 err = drop_commit(keep, drop, qid->id, repo,
1321 cancel_cb, cancel_arg);
1322 if (err)
1323 goto done;
1324 } else if (ncolor == COLOR_BLANK) {
1325 struct got_commit_object *commit;
1326 struct got_object_id *id;
1327 const struct got_object_id_queue *parents;
1328 struct got_object_qid *pid;
1330 id = got_object_id_dup(qid->id);
1331 if (id == NULL) {
1332 err = got_error_from_errno("got_object_id_dup");
1333 goto done;
1335 if (qcolor == COLOR_KEEP)
1336 err = got_object_idset_add(keep, id,
1337 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1338 else
1339 err = got_object_idset_add(drop, id,
1340 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1341 if (err) {
1342 free(id);
1343 goto done;
1346 err = got_object_open_as_commit(&commit, repo, id);
1347 if (err) {
1348 free(id);
1349 goto done;
1351 parents = got_object_commit_get_parent_ids(commit);
1352 if (parents) {
1353 STAILQ_FOREACH(pid, parents, entry) {
1354 err = queue_commit_id(&ids, pid->id,
1355 qcolor, repo);
1356 if (err) {
1357 free(id);
1358 goto done;
1362 got_object_commit_close(commit);
1363 commit = NULL;
1364 } else {
1365 /* should not happen */
1366 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1367 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1368 goto done;
1371 STAILQ_REMOVE_HEAD(&ids, entry);
1372 got_object_qid_free(qid);
1375 nkeep = got_object_idset_num_elements(keep);
1376 if (nkeep > 0) {
1377 struct append_id_arg arg;
1378 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1379 if (arg.array == NULL) {
1380 err = got_error_from_errno("calloc");
1381 goto done;
1383 arg.idx = 0;
1384 err = got_object_idset_for_each(keep, append_id, &arg);
1385 if (err) {
1386 free(arg.array);
1387 goto done;
1389 *res = arg.array;
1390 *nres = nkeep;
1392 done:
1393 got_object_idset_free(keep);
1394 got_object_idset_free(drop);
1395 got_object_id_queue_free(&ids);
1396 return err;
1399 static const struct got_error *
1400 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1401 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1402 struct got_object_id **ours, int nours, struct got_repository *repo,
1403 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1404 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1406 const struct got_error *err = NULL;
1407 struct got_object_id **ids = NULL;
1408 int i, nobj = 0, obj_type;
1410 *ncolored = 0;
1411 *nfound = 0;
1412 *ntrees = 0;
1414 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1415 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1416 if (err || nobj == 0)
1417 goto done;
1419 for (i = 0; i < ntheirs; i++) {
1420 struct got_object_id *id = theirs[i];
1421 if (id == NULL)
1422 continue;
1423 err = got_object_get_type(&obj_type, repo, id);
1424 if (err)
1425 return err;
1426 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1427 continue;
1428 err = load_commit(0, idset, id, repo, loose_obj_only,
1429 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1430 cancel_cb, cancel_arg);
1431 if (err)
1432 goto done;
1435 for (i = 0; i < ntheirs; i++) {
1436 struct got_object_id *id = theirs[i];
1437 struct got_pack_meta *m;
1438 if (id == NULL)
1439 continue;
1440 m = got_object_idset_get(idset, id);
1441 if (m == NULL) {
1442 err = got_object_get_type(&obj_type, repo, id);
1443 if (err)
1444 goto done;
1445 } else
1446 obj_type = m->obj_type;
1447 if (obj_type != GOT_OBJ_TYPE_TAG)
1448 continue;
1449 err = load_tag(0, idset, id, repo, loose_obj_only,
1450 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1451 cancel_cb, cancel_arg);
1452 if (err)
1453 goto done;
1456 for (i = 0; i < nobj; i++) {
1457 err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1458 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1459 cancel_cb, cancel_arg);
1460 if (err)
1461 goto done;
1464 for (i = 0; i < nours; i++) {
1465 struct got_object_id *id = ours[i];
1466 struct got_pack_meta *m;
1467 if (id == NULL)
1468 continue;
1469 m = got_object_idset_get(idset, id);
1470 if (m == NULL) {
1471 err = got_object_get_type(&obj_type, repo, id);
1472 if (err)
1473 goto done;
1474 } else
1475 obj_type = m->obj_type;
1476 if (obj_type != GOT_OBJ_TYPE_TAG)
1477 continue;
1478 err = load_tag(1, idset, id, repo, loose_obj_only,
1479 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1480 cancel_cb, cancel_arg);
1481 if (err)
1482 goto done;
1484 done:
1485 for (i = 0; i < nobj; i++) {
1486 free(ids[i]);
1488 free(ids);
1489 return err;
1492 const struct got_error *
1493 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1495 size_t n;
1497 SHA1Update(ctx, buf, len);
1498 n = fwrite(buf, 1, len, f);
1499 if (n != len)
1500 return got_ferror(f, GOT_ERR_IO);
1501 return NULL;
1504 static void
1505 putbe32(char *b, uint32_t n)
1507 b[0] = n >> 24;
1508 b[1] = n >> 16;
1509 b[2] = n >> 8;
1510 b[3] = n >> 0;
1513 static int
1514 write_order_cmp(const void *pa, const void *pb)
1516 struct got_pack_meta *a, *b, *ahd, *bhd;
1518 a = *(struct got_pack_meta **)pa;
1519 b = *(struct got_pack_meta **)pb;
1520 ahd = (a->head == NULL) ? a : a->head;
1521 bhd = (b->head == NULL) ? b : b->head;
1522 if (ahd->mtime != bhd->mtime)
1523 return bhd->mtime - ahd->mtime;
1524 if (ahd != bhd)
1525 return (uintptr_t)bhd - (uintptr_t)ahd;
1526 if (a->nchain != b->nchain)
1527 return a->nchain - b->nchain;
1528 return a->mtime - b->mtime;
1531 static int
1532 reuse_write_order_cmp(const void *pa, const void *pb)
1534 struct got_pack_meta *a, *b;
1536 a = *(struct got_pack_meta **)pa;
1537 b = *(struct got_pack_meta **)pb;
1539 if (a->reused_delta_offset < b->reused_delta_offset)
1540 return -1;
1541 if (a->reused_delta_offset > b->reused_delta_offset)
1542 return 1;
1543 return 0;
1546 static const struct got_error *
1547 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1549 size_t i;
1551 *hdrlen = 0;
1553 hdr[0] = obj_type << 4;
1554 hdr[0] |= len & 0xf;
1555 len >>= 4;
1556 for (i = 1; len != 0; i++){
1557 if (i >= bufsize)
1558 return got_error(GOT_ERR_NO_SPACE);
1559 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1560 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1561 len >>= GOT_DELTA_SIZE_SHIFT;
1564 *hdrlen = i;
1565 return NULL;
1568 static int
1569 packoff(char *hdr, off_t off)
1571 int i, j;
1572 char rbuf[8];
1574 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1575 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1576 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1577 GOT_DELTA_SIZE_MORE;
1580 j = 0;
1581 while (i > 0)
1582 hdr[j++] = rbuf[--i];
1583 return j;
1586 static const struct got_error *
1587 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1588 struct got_pack_meta *m)
1590 const struct got_error *err;
1591 char buf[32];
1592 int nh;
1594 if (m->prev->off != 0) {
1595 err = packhdr(&nh, buf, sizeof(buf),
1596 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1597 if (err)
1598 return err;
1599 nh += packoff(buf + nh, m->off - m->prev->off);
1600 err = hwrite(packfile, buf, nh, ctx);
1601 if (err)
1602 return err;
1603 *packfile_size += nh;
1604 } else {
1605 err = packhdr(&nh, buf, sizeof(buf),
1606 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1607 if (err)
1608 return err;
1609 err = hwrite(packfile, buf, nh, ctx);
1610 if (err)
1611 return err;
1612 *packfile_size += nh;
1613 err = hwrite(packfile, m->prev->id.sha1,
1614 sizeof(m->prev->id.sha1), ctx);
1615 if (err)
1616 return err;
1617 *packfile_size += sizeof(m->prev->id.sha1);
1620 return NULL;
1623 static const struct got_error *
1624 write_packed_object(off_t *packfile_size, FILE *packfile,
1625 FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1626 SHA1_CTX *ctx, struct got_repository *repo)
1628 const struct got_error *err = NULL;
1629 struct got_deflate_checksum csum;
1630 char buf[32];
1631 int nh;
1632 struct got_raw_object *raw = NULL;
1633 off_t outlen;
1635 csum.output_sha1 = ctx;
1636 csum.output_crc = NULL;
1638 m->off = ftello(packfile);
1639 if (m->delta_len == 0) {
1640 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1641 if (err)
1642 goto done;
1643 err = packhdr(&nh, buf, sizeof(buf),
1644 m->obj_type, raw->size);
1645 if (err)
1646 goto done;
1647 err = hwrite(packfile, buf, nh, ctx);
1648 if (err)
1649 goto done;
1650 *packfile_size += nh;
1651 if (raw->f == NULL) {
1652 err = got_deflate_to_file_mmap(&outlen,
1653 raw->data + raw->hdrlen, 0, raw->size,
1654 packfile, &csum);
1655 if (err)
1656 goto done;
1657 } else {
1658 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1659 == -1) {
1660 err = got_error_from_errno("fseeko");
1661 goto done;
1663 err = got_deflate_to_file(&outlen, raw->f,
1664 raw->size, packfile, &csum);
1665 if (err)
1666 goto done;
1668 *packfile_size += outlen;
1669 got_object_raw_close(raw);
1670 raw = NULL;
1671 } else if (m->delta_buf) {
1672 err = deltahdr(packfile_size, ctx, packfile, m);
1673 if (err)
1674 goto done;
1675 err = got_deflate_to_file_mmap(&outlen,
1676 m->delta_buf, 0, m->delta_len, packfile, &csum);
1677 if (err)
1678 goto done;
1679 *packfile_size += outlen;
1680 free(m->delta_buf);
1681 m->delta_buf = NULL;
1682 } else {
1683 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1684 == -1) {
1685 err = got_error_from_errno("fseeko");
1686 goto done;
1688 err = deltahdr(packfile_size, ctx, packfile, m);
1689 if (err)
1690 goto done;
1691 err = got_deflate_to_file(&outlen, delta_cache,
1692 m->delta_len, packfile, &csum);
1693 if (err)
1694 goto done;
1695 *packfile_size += outlen;
1697 done:
1698 if (raw)
1699 got_object_raw_close(raw);
1700 return err;
1703 static const struct got_error *
1704 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1705 struct got_pack_meta **deltify, int ndeltify,
1706 struct got_pack_meta **reuse, int nreuse,
1707 int ncolored, int nfound, int ntrees, int nours,
1708 struct got_repository *repo,
1709 got_pack_progress_cb progress_cb, void *progress_arg,
1710 struct got_ratelimit *rl,
1711 got_cancel_cb cancel_cb, void *cancel_arg)
1713 const struct got_error *err = NULL;
1714 int i;
1715 SHA1_CTX ctx;
1716 struct got_pack_meta *m;
1717 char buf[32];
1718 size_t n;
1719 off_t packfile_size = 0;
1720 int outfd = -1;
1722 SHA1Init(&ctx);
1724 err = hwrite(packfile, "PACK", 4, &ctx);
1725 if (err)
1726 return err;
1727 putbe32(buf, GOT_PACKFILE_VERSION);
1728 err = hwrite(packfile, buf, 4, &ctx);
1729 if (err)
1730 goto done;
1731 putbe32(buf, ndeltify + nreuse);
1732 err = hwrite(packfile, buf, 4, &ctx);
1733 if (err)
1734 goto done;
1736 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1737 write_order_cmp);
1738 for (i = 0; i < ndeltify; i++) {
1739 err = report_progress(progress_cb, progress_arg, rl,
1740 ncolored, nfound, ntrees, packfile_size, nours,
1741 ndeltify + nreuse, ndeltify + nreuse, i);
1742 if (err)
1743 goto done;
1744 m = deltify[i];
1745 err = write_packed_object(&packfile_size, packfile,
1746 delta_cache, m, &outfd, &ctx, repo);
1747 if (err)
1748 goto done;
1751 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1752 reuse_write_order_cmp);
1753 for (i = 0; i < nreuse; i++) {
1754 err = report_progress(progress_cb, progress_arg, rl,
1755 ncolored, nfound, ntrees, packfile_size, nours,
1756 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1757 if (err)
1758 goto done;
1759 m = reuse[i];
1760 err = write_packed_object(&packfile_size, packfile,
1761 delta_cache, m, &outfd, &ctx, repo);
1762 if (err)
1763 goto done;
1766 SHA1Final(pack_sha1, &ctx);
1767 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1768 if (n != SHA1_DIGEST_LENGTH)
1769 err = got_ferror(packfile, GOT_ERR_IO);
1770 packfile_size += SHA1_DIGEST_LENGTH;
1771 packfile_size += sizeof(struct got_packfile_hdr);
1772 if (progress_cb) {
1773 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1774 packfile_size, nours, ndeltify + nreuse,
1775 ndeltify + nreuse, ndeltify + nreuse);
1776 if (err)
1777 goto done;
1779 done:
1780 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1781 err = got_error_from_errno("close");
1782 return err;
1785 static const struct got_error *
1786 remove_unused_object(struct got_object_idset_element *entry, void *arg)
1788 struct got_object_idset *idset = arg;
1790 if (got_object_idset_get_element_data(entry) == NULL)
1791 got_object_idset_remove_element(idset, entry);
1793 return NULL;
1796 static const struct got_error *
1797 remove_reused_object(struct got_object_idset_element *entry, void *arg)
1799 struct got_object_idset *idset = arg;
1800 struct got_pack_meta *m;
1802 m = got_object_idset_get_element_data(entry);
1803 if (m->have_reused_delta)
1804 got_object_idset_remove_element(idset, entry);
1806 return NULL;
1809 static const struct got_error *
1810 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1812 struct got_pack_meta *m = data;
1813 struct got_pack_metavec *v = arg;
1815 return add_meta(m, v);
1818 const struct got_error *
1819 got_pack_create(uint8_t *packsha1, FILE *packfile,
1820 struct got_object_id **theirs, int ntheirs,
1821 struct got_object_id **ours, int nours,
1822 struct got_repository *repo, int loose_obj_only, int allow_empty,
1823 got_pack_progress_cb progress_cb, void *progress_arg,
1824 got_cancel_cb cancel_cb, void *cancel_arg)
1826 const struct got_error *err;
1827 int delta_cache_fd = -1;
1828 FILE *delta_cache = NULL;
1829 struct got_object_idset *idset;
1830 struct got_ratelimit rl;
1831 struct got_pack_metavec deltify, reuse;
1832 int ncolored = 0, nfound = 0, ntrees = 0;
1834 memset(&deltify, 0, sizeof(deltify));
1835 memset(&reuse, 0, sizeof(reuse));
1837 got_ratelimit_init(&rl, 0, 500);
1839 idset = got_object_idset_alloc();
1840 if (idset == NULL)
1841 return got_error_from_errno("got_object_idset_alloc");
1843 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1844 ntheirs, ours, nours, repo, loose_obj_only,
1845 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1846 if (err)
1847 return err;
1849 err = got_object_idset_for_each_element(idset,
1850 remove_unused_object, idset);
1851 if (err)
1852 goto done;
1854 if (progress_cb) {
1855 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1856 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1857 if (err)
1858 goto done;
1861 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1862 err = got_error(GOT_ERR_CANNOT_PACK);
1863 goto done;
1866 delta_cache_fd = got_opentempfd();
1867 if (delta_cache_fd == -1) {
1868 err = got_error_from_errno("got_opentemp");
1869 goto done;
1872 reuse.metasz = 64;
1873 reuse.meta = calloc(reuse.metasz,
1874 sizeof(struct got_pack_meta *));
1875 if (reuse.meta == NULL) {
1876 err = got_error_from_errno("calloc");
1877 goto done;
1880 err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1881 ntrees, nours, repo, progress_cb, progress_arg, &rl,
1882 cancel_cb, cancel_arg);
1883 if (err)
1884 goto done;
1885 if (reuse.nmeta > 0) {
1886 err = got_object_idset_for_each_element(idset,
1887 remove_reused_object, idset);
1888 if (err)
1889 goto done;
1892 delta_cache = fdopen(delta_cache_fd, "a+");
1893 if (delta_cache == NULL) {
1894 err = got_error_from_errno("fdopen");
1895 goto done;
1897 delta_cache_fd = -1;
1899 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1900 err = got_error_from_errno("fseeko");
1901 goto done;
1904 deltify.meta = calloc(got_object_idset_num_elements(idset),
1905 sizeof(struct got_pack_meta *));
1906 if (deltify.meta == NULL) {
1907 err = got_error_from_errno("calloc");
1908 goto done;
1910 deltify.metasz = got_object_idset_num_elements(idset);
1912 err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1913 if (err)
1914 goto done;
1915 if (deltify.nmeta > 0) {
1916 err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1917 nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1918 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1919 if (err)
1920 goto done;
1921 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1922 err = got_error_from_errno("fseeko");
1923 goto done;
1927 err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1928 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1929 nours, repo, progress_cb, progress_arg, &rl,
1930 cancel_cb, cancel_arg);
1931 if (err)
1932 goto done;
1933 done:
1934 free_nmeta(deltify.meta, deltify.nmeta);
1935 free_nmeta(reuse.meta, reuse.nmeta);
1936 got_object_idset_free(idset);
1937 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1938 err = got_error_from_errno("close");
1939 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1940 err = got_error_from_errno("fclose");
1941 return err;