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>
24 #include <sys/mman.h>
26 #include <endian.h>
27 #include <errno.h>
28 #include <stdint.h>
29 #include <imsg.h>
30 #include <inttypes.h>
31 #include <poll.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sha1.h>
36 #include <time.h>
37 #include <unistd.h>
38 #include <limits.h>
39 #include <zlib.h>
41 #include "got_error.h"
42 #include "got_cancel.h"
43 #include "got_object.h"
44 #include "got_path.h"
45 #include "got_reference.h"
46 #include "got_repository_admin.h"
48 #include "got_lib_deltify.h"
49 #include "got_lib_delta.h"
50 #include "got_lib_object.h"
51 #include "got_lib_object_idset.h"
52 #include "got_lib_object_cache.h"
53 #include "got_lib_deflate.h"
54 #include "got_lib_ratelimit.h"
55 #include "got_lib_pack.h"
56 #include "got_lib_pack_create.h"
57 #include "got_lib_repository.h"
58 #include "got_lib_inflate.h"
59 #include "got_lib_poll.h"
61 #include "murmurhash2.h"
63 #ifndef MIN
64 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 #endif
67 #ifndef MAX
68 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
69 #endif
71 #ifndef nitems
72 #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
73 #endif
75 static const struct got_error *
76 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
77 const char *path, int obj_type, time_t mtime, uint32_t seed)
78 {
79 struct got_pack_meta *m;
81 *new = NULL;
83 m = calloc(1, sizeof(*m));
84 if (m == NULL)
85 return got_error_from_errno("calloc");
87 memcpy(&m->id, id, sizeof(m->id));
89 m->path_hash = murmurhash2(path, strlen(path), seed);
90 m->obj_type = obj_type;
91 m->mtime = mtime;
92 *new = m;
93 return NULL;
94 }
96 static void
97 clear_meta(struct got_pack_meta *meta)
98 {
99 if (meta == NULL)
100 return;
101 meta->path_hash = 0;
102 free(meta->delta_buf);
103 meta->delta_buf = NULL;
104 free(meta->base_obj_id);
105 meta->base_obj_id = NULL;
106 meta->reused_delta_offset = 0;
109 static void
110 free_nmeta(struct got_pack_meta **meta, int nmeta)
112 int i;
114 for (i = 0; i < nmeta; i++)
115 clear_meta(meta[i]);
116 free(meta);
119 static int
120 delta_order_cmp(const void *pa, const void *pb)
122 struct got_pack_meta *a, *b;
124 a = *(struct got_pack_meta **)pa;
125 b = *(struct got_pack_meta **)pb;
127 if (a->obj_type != b->obj_type)
128 return a->obj_type - b->obj_type;
129 if (a->path_hash < b->path_hash)
130 return -1;
131 if (a->path_hash > b->path_hash)
132 return 1;
133 if (a->mtime < b->mtime)
134 return -1;
135 if (a->mtime > b->mtime)
136 return 1;
137 return got_object_id_cmp(&a->id, &b->id);
140 static off_t
141 delta_size(struct got_delta_instruction *deltas, int ndeltas)
143 int i;
144 off_t size = 32;
145 for (i = 0; i < ndeltas; i++) {
146 if (deltas[i].copy)
147 size += GOT_DELTA_SIZE_SHIFT;
148 else
149 size += deltas[i].len + 1;
151 return size;
154 static const struct got_error *
155 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
157 char *n;
159 if (*len + nseg >= *sz) {
160 while (*len + nseg >= *sz)
161 *sz += *sz / 2;
162 n = realloc(*p, *sz);
163 if (n == NULL)
164 return got_error_from_errno("realloc");
165 *p = n;
167 memcpy(*p + *len, seg, nseg);
168 *len += nseg;
169 return NULL;
172 static const struct got_error *
173 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
174 struct got_delta_instruction *deltas, int ndeltas,
175 off_t delta_size, off_t base_size)
177 const struct got_error *err;
178 unsigned char buf[16], *bp;
179 int i, j;
180 size_t len = 0, compressed_len;
181 off_t bufsize = delta_size;
182 off_t n;
183 struct got_delta_instruction *d;
184 uint8_t *delta_buf;
186 delta_buf = malloc(bufsize);
187 if (delta_buf == NULL)
188 return got_error_from_errno("malloc");
190 /* base object size */
191 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
192 n = base_size >> GOT_DELTA_SIZE_SHIFT;
193 for (i = 1; n > 0; i++) {
194 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
195 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
196 n >>= GOT_DELTA_SIZE_SHIFT;
198 err = append(&delta_buf, &len, &bufsize, buf, i);
199 if (err)
200 goto done;
202 /* target object size */
203 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
204 n = o->size >> GOT_DELTA_SIZE_SHIFT;
205 for (i = 1; n > 0; i++) {
206 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
207 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
208 n >>= GOT_DELTA_SIZE_SHIFT;
210 err = append(&delta_buf, &len, &bufsize, buf, i);
211 if (err)
212 goto done;
214 for (j = 0; j < ndeltas; j++) {
215 d = &deltas[j];
216 if (d->copy) {
217 n = d->offset;
218 bp = &buf[1];
219 buf[0] = GOT_DELTA_BASE_COPY;
220 for (i = 0; i < 4; i++) {
221 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
222 buf[0] |= 1 << i;
223 *bp++ = n & 0xff;
224 n >>= 8;
225 if (n == 0)
226 break;
229 n = d->len;
230 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
231 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
232 for (i = 0; i < 3 && n > 0; i++) {
233 buf[0] |= 1 << (i + 4);
234 *bp++ = n & 0xff;
235 n >>= 8;
238 err = append(&delta_buf, &len, &bufsize,
239 buf, bp - buf);
240 if (err)
241 goto done;
242 } else if (o->f == NULL) {
243 n = 0;
244 while (n != d->len) {
245 buf[0] = (d->len - n < 127) ? d->len - n : 127;
246 err = append(&delta_buf, &len, &bufsize,
247 buf, 1);
248 if (err)
249 goto done;
250 err = append(&delta_buf, &len, &bufsize,
251 o->data + o->hdrlen + d->offset + n,
252 buf[0]);
253 if (err)
254 goto done;
255 n += buf[0];
257 } else {
258 char content[128];
259 size_t r;
260 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
261 err = got_error_from_errno("fseeko");
262 goto done;
264 n = 0;
265 while (n != d->len) {
266 buf[0] = (d->len - n < 127) ? d->len - n : 127;
267 err = append(&delta_buf, &len, &bufsize,
268 buf, 1);
269 if (err)
270 goto done;
271 r = fread(content, 1, buf[0], o->f);
272 if (r != buf[0]) {
273 err = got_ferror(o->f, GOT_ERR_IO);
274 goto done;
276 err = append(&delta_buf, &len, &bufsize,
277 content, buf[0]);
278 if (err)
279 goto done;
280 n += buf[0];
285 err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
286 NULL, NULL, delta_buf, 0, len);
287 if (err)
288 goto done;
290 m->delta_len = len;
291 m->delta_compressed_len = compressed_len;
292 done:
293 free(delta_buf);
294 return err;
297 static const struct got_error *
298 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
299 struct got_delta_instruction *deltas, int ndeltas,
300 off_t base_size, FILE *f)
302 const struct got_error *err;
303 unsigned char buf[16], *bp;
304 int i, j;
305 off_t n;
306 struct got_deflate_buf zb;
307 struct got_delta_instruction *d;
308 off_t delta_len = 0, compressed_len = 0;
310 err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
311 if (err)
312 return err;
314 /* base object size */
315 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
316 n = base_size >> GOT_DELTA_SIZE_SHIFT;
317 for (i = 1; n > 0; i++) {
318 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
319 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
320 n >>= GOT_DELTA_SIZE_SHIFT;
323 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
324 buf, 0, i, f, NULL);
325 if (err)
326 goto done;
327 delta_len += i;
329 /* target object size */
330 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
331 n = o->size >> GOT_DELTA_SIZE_SHIFT;
332 for (i = 1; n > 0; i++) {
333 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
334 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
335 n >>= GOT_DELTA_SIZE_SHIFT;
338 err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
339 buf, 0, i, f, NULL);
340 if (err)
341 goto done;
342 delta_len += i;
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;
358 n = d->len;
359 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
360 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
361 for (i = 0; i < 3 && n > 0; i++) {
362 buf[0] |= 1 << (i + 4);
363 *bp++ = n & 0xff;
364 n >>= 8;
367 err = got_deflate_append_to_file_mmap(&zb,
368 &compressed_len, buf, 0, bp - buf, f, NULL);
369 if (err)
370 goto done;
371 delta_len += (bp - buf);
372 } else if (o->f == NULL) {
373 n = 0;
374 while (n != d->len) {
375 buf[0] = (d->len - n < 127) ? d->len - n : 127;
376 err = got_deflate_append_to_file_mmap(&zb,
377 &compressed_len, buf, 0, 1, f, NULL);
378 if (err)
379 goto done;
380 delta_len++;
381 err = got_deflate_append_to_file_mmap(&zb,
382 &compressed_len,
383 o->data + o->hdrlen + d->offset + n, 0,
384 buf[0], f, NULL);
385 if (err)
386 goto done;
387 delta_len += buf[0];
388 n += buf[0];
390 } else {
391 char content[128];
392 size_t r;
393 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
394 err = got_error_from_errno("fseeko");
395 goto done;
397 n = 0;
398 while (n != d->len) {
399 buf[0] = (d->len - n < 127) ? d->len - n : 127;
400 err = got_deflate_append_to_file_mmap(&zb,
401 &compressed_len, buf, 0, 1, f, NULL);
402 if (err)
403 goto done;
404 delta_len++;
405 r = fread(content, 1, buf[0], o->f);
406 if (r != buf[0]) {
407 err = got_ferror(o->f, GOT_ERR_IO);
408 goto done;
410 err = got_deflate_append_to_file_mmap(&zb,
411 &compressed_len, content, 0, buf[0], f,
412 NULL);
413 if (err)
414 goto done;
415 delta_len += buf[0];
416 n += buf[0];
421 err = got_deflate_flush(&zb, f, NULL, &compressed_len);
422 if (err)
423 goto done;
425 /* sanity check */
426 if (compressed_len != ftello(f) - m->delta_offset) {
427 err = got_error(GOT_ERR_COMPRESSION);
428 goto done;
431 m->delta_len = delta_len;
432 m->delta_compressed_len = compressed_len;
433 done:
434 got_deflate_end(&zb);
435 return err;
438 const struct got_error *
439 got_pack_report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
440 struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
441 off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
442 int nobj_written)
444 const struct got_error *err;
445 int elapsed;
447 if (progress_cb == NULL)
448 return NULL;
450 err = got_ratelimit_check(&elapsed, rl);
451 if (err || !elapsed)
452 return err;
454 return progress_cb(progress_arg, ncolored, nfound, ntrees,
455 packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
458 const struct got_error *
459 got_pack_add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
461 if (v->nmeta == v->metasz){
462 size_t newsize = 2 * v->metasz;
463 struct got_pack_meta **new;
464 new = reallocarray(v->meta, newsize, sizeof(*new));
465 if (new == NULL)
466 return got_error_from_errno("reallocarray");
467 v->meta = new;
468 v->metasz = newsize;
471 v->meta[v->nmeta++] = m;
472 return NULL;
475 const struct got_error *
476 got_pack_find_pack_for_reuse(struct got_packidx **best_packidx,
477 struct got_repository *repo)
479 const struct got_error *err = NULL;
480 struct got_pathlist_entry *pe;
481 const char *best_packidx_path = NULL;
482 int nobj_max = 0;
484 *best_packidx = NULL;
486 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
487 const char *path_packidx = pe->path;
488 struct got_packidx *packidx;
489 int nobj;
491 err = got_repo_get_packidx(&packidx, path_packidx, repo);
492 if (err)
493 break;
495 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
496 if (nobj > nobj_max) {
497 best_packidx_path = path_packidx;
498 nobj_max = nobj;
502 if (best_packidx_path) {
503 err = got_repo_get_packidx(best_packidx, best_packidx_path,
504 repo);
507 return err;
510 const struct got_error *
511 got_pack_cache_pack_for_packidx(struct got_pack **pack,
512 struct got_packidx *packidx, struct got_repository *repo)
514 const struct got_error *err;
515 char *path_packfile = NULL;
517 err = got_packidx_get_packfile_path(&path_packfile,
518 packidx->path_packidx);
519 if (err)
520 return err;
522 *pack = got_repo_get_cached_pack(repo, path_packfile);
523 if (*pack == NULL) {
524 err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
525 if (err)
526 goto done;
528 done:
529 free(path_packfile);
530 return err;
533 static const struct got_error *
534 pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
535 int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
536 struct got_repository *repo,
537 got_pack_progress_cb progress_cb, void *progress_arg,
538 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
540 const struct got_error *err = NULL;
541 struct got_pack_meta *m = NULL, *base = NULL;
542 struct got_raw_object *raw = NULL, *base_raw = NULL;
543 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
544 int i, j, ndeltas, best_ndeltas;
545 off_t size, best_size;
546 const int max_base_candidates = 3;
547 size_t delta_memsize = 0;
548 const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
549 int outfd = -1;
550 uint32_t delta_seed;
552 delta_seed = arc4random();
554 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
555 for (i = 0; i < nmeta; i++) {
556 if (cancel_cb) {
557 err = (*cancel_cb)(cancel_arg);
558 if (err)
559 break;
561 err = got_pack_report_progress(progress_cb, progress_arg, rl,
562 ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
563 nreused + i, 0);
564 if (err)
565 goto done;
566 m = meta[i];
568 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
569 m->obj_type == GOT_OBJ_TYPE_TAG)
570 continue;
572 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
573 if (err)
574 goto done;
575 m->size = raw->size;
577 if (raw->f == NULL) {
578 err = got_deltify_init_mem(&m->dtab, raw->data,
579 raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
580 } else {
581 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
582 raw->size + raw->hdrlen, delta_seed);
584 if (err)
585 goto done;
587 if (i > max_base_candidates) {
588 struct got_pack_meta *n = NULL;
589 n = meta[i - (max_base_candidates + 1)];
590 got_deltify_free(n->dtab);
591 n->dtab = NULL;
594 best_size = raw->size;
595 best_ndeltas = 0;
596 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
597 if (cancel_cb) {
598 err = (*cancel_cb)(cancel_arg);
599 if (err)
600 goto done;
602 base = meta[j];
603 /* long chains make unpacking slow, avoid such bases */
604 if (base->nchain >= 128 ||
605 base->obj_type != m->obj_type)
606 continue;
608 err = got_object_raw_open(&base_raw, &outfd, repo,
609 &base->id);
610 if (err)
611 goto done;
613 if (raw->f == NULL && base_raw->f == NULL) {
614 err = got_deltify_mem_mem(&deltas, &ndeltas,
615 raw->data, raw->hdrlen,
616 raw->size + raw->hdrlen, delta_seed,
617 base->dtab, base_raw->data,
618 base_raw->hdrlen,
619 base_raw->size + base_raw->hdrlen);
620 } else if (raw->f == NULL) {
621 err = got_deltify_mem_file(&deltas, &ndeltas,
622 raw->data, raw->hdrlen,
623 raw->size + raw->hdrlen, delta_seed,
624 base->dtab, base_raw->f,
625 base_raw->hdrlen,
626 base_raw->size + base_raw->hdrlen);
627 } else if (base_raw->f == NULL) {
628 err = got_deltify_file_mem(&deltas, &ndeltas,
629 raw->f, raw->hdrlen,
630 raw->size + raw->hdrlen, delta_seed,
631 base->dtab, base_raw->data,
632 base_raw->hdrlen,
633 base_raw->size + base_raw->hdrlen);
634 } else {
635 err = got_deltify(&deltas, &ndeltas,
636 raw->f, raw->hdrlen,
637 raw->size + raw->hdrlen, delta_seed,
638 base->dtab, base_raw->f, base_raw->hdrlen,
639 base_raw->size + base_raw->hdrlen);
641 got_object_raw_close(base_raw);
642 base_raw = NULL;
643 if (err)
644 goto done;
646 size = delta_size(deltas, ndeltas);
647 if (size + 32 < best_size){
648 /*
649 * if we already picked a best delta,
650 * replace it.
651 */
652 best_size = size;
653 free(best_deltas);
654 best_deltas = deltas;
655 best_ndeltas = ndeltas;
656 deltas = NULL;
657 m->nchain = base->nchain + 1;
658 m->prev = base;
659 m->head = base->head;
660 if (m->head == NULL)
661 m->head = base;
662 } else {
663 free(deltas);
664 deltas = NULL;
665 ndeltas = 0;
669 if (best_ndeltas > 0) {
670 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
671 delta_memsize + best_size <= max_delta_memsize) {
672 delta_memsize += best_size;
673 err = encode_delta_in_mem(m, raw, best_deltas,
674 best_ndeltas, best_size, m->prev->size);
675 } else {
676 m->delta_offset = ftello(delta_cache);
677 err = encode_delta(m, raw, best_deltas,
678 best_ndeltas, m->prev->size, delta_cache);
680 free(best_deltas);
681 best_deltas = NULL;
682 best_ndeltas = 0;
683 if (err)
684 goto done;
687 got_object_raw_close(raw);
688 raw = NULL;
690 done:
691 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
692 got_deltify_free(meta[i]->dtab);
693 meta[i]->dtab = NULL;
695 if (raw)
696 got_object_raw_close(raw);
697 if (base_raw)
698 got_object_raw_close(base_raw);
699 if (outfd != -1 && close(outfd) == -1 && err == NULL)
700 err = got_error_from_errno("close");
701 free(deltas);
702 free(best_deltas);
703 return err;
706 static const struct got_error *
707 search_packidx(int *found, struct got_object_id *id,
708 struct got_repository *repo)
710 const struct got_error *err = NULL;
711 struct got_packidx *packidx = NULL;
712 int idx;
714 *found = 0;
716 err = got_repo_search_packidx(&packidx, &idx, repo, id);
717 if (err == NULL)
718 *found = 1; /* object is already packed */
719 else if (err->code == GOT_ERR_NO_OBJ)
720 err = NULL;
721 return err;
724 const struct got_error *
725 got_pack_add_object(int want_meta, struct got_object_idset *idset,
726 struct got_object_id *id, const char *path, int obj_type,
727 time_t mtime, uint32_t seed, int loose_obj_only,
728 struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
729 got_pack_progress_cb progress_cb, void *progress_arg,
730 struct got_ratelimit *rl)
732 const struct got_error *err;
733 struct got_pack_meta *m = NULL;
735 if (loose_obj_only) {
736 int is_packed;
737 err = search_packidx(&is_packed, id, repo);
738 if (err)
739 return err;
740 if (is_packed && want_meta)
741 return NULL;
744 if (want_meta) {
745 err = alloc_meta(&m, id, path, obj_type, mtime, seed);
746 if (err)
747 return err;
749 (*nfound)++;
750 err = got_pack_report_progress(progress_cb, progress_arg, rl,
751 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
752 if (err) {
753 clear_meta(m);
754 free(m);
755 return err;
759 err = got_object_idset_add(idset, id, m);
760 if (err) {
761 clear_meta(m);
762 free(m);
764 return err;
767 const struct got_error *
768 got_pack_load_tree_entries(struct got_object_id_queue *ids, int want_meta,
769 struct got_object_idset *idset, struct got_object_idset *idset_exclude,
770 struct got_tree_object *tree,
771 const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
772 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
773 got_pack_progress_cb progress_cb, void *progress_arg,
774 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
776 const struct got_error *err;
777 char *p = NULL;
778 int i;
780 (*ntrees)++;
781 err = got_pack_report_progress(progress_cb, progress_arg, rl,
782 *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
783 if (err)
784 return err;
786 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
787 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
788 struct got_object_id *id = got_tree_entry_get_id(e);
789 mode_t mode = got_tree_entry_get_mode(e);
791 if (cancel_cb) {
792 err = (*cancel_cb)(cancel_arg);
793 if (err)
794 break;
797 if (got_object_tree_entry_is_submodule(e) ||
798 got_object_idset_contains(idset, id) ||
799 got_object_idset_contains(idset_exclude, id))
800 continue;
802 /*
803 * If got-read-pack is crawling trees for us then
804 * we are only here to collect blob IDs.
805 */
806 if (ids == NULL && S_ISDIR(mode))
807 continue;
809 if (asprintf(&p, "%s%s%s", dpath,
810 got_path_is_root_dir(dpath) ? "" : "/",
811 got_tree_entry_get_name(e)) == -1) {
812 err = got_error_from_errno("asprintf");
813 break;
816 if (S_ISDIR(mode)) {
817 struct got_object_qid *qid;
818 err = got_object_qid_alloc(&qid, id);
819 if (err)
820 break;
821 qid->data = p;
822 p = NULL;
823 STAILQ_INSERT_TAIL(ids, qid, entry);
824 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
825 err = got_pack_add_object(want_meta,
826 want_meta ? idset : idset_exclude, id, p,
827 GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
828 repo, ncolored, nfound, ntrees,
829 progress_cb, progress_arg, rl);
830 if (err)
831 break;
832 free(p);
833 p = NULL;
834 } else {
835 free(p);
836 p = NULL;
840 free(p);
841 return err;
844 const struct got_error *
845 got_pack_load_tree(int want_meta, struct got_object_idset *idset,
846 struct got_object_idset *idset_exclude,
847 struct got_object_id *tree_id, const char *dpath, time_t mtime,
848 uint32_t seed, struct got_repository *repo, int loose_obj_only,
849 int *ncolored, int *nfound, int *ntrees,
850 got_pack_progress_cb progress_cb, void *progress_arg,
851 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
853 const struct got_error *err = NULL;
854 struct got_object_id_queue tree_ids;
855 struct got_object_qid *qid;
856 struct got_tree_object *tree = NULL;
858 if (got_object_idset_contains(idset, tree_id) ||
859 got_object_idset_contains(idset_exclude, tree_id))
860 return NULL;
862 err = got_object_qid_alloc(&qid, tree_id);
863 if (err)
864 return err;
865 qid->data = strdup(dpath);
866 if (qid->data == NULL) {
867 err = got_error_from_errno("strdup");
868 got_object_qid_free(qid);
869 return err;
872 STAILQ_INIT(&tree_ids);
873 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
875 while (!STAILQ_EMPTY(&tree_ids)) {
876 const char *path;
877 if (cancel_cb) {
878 err = (*cancel_cb)(cancel_arg);
879 if (err)
880 break;
883 qid = STAILQ_FIRST(&tree_ids);
884 STAILQ_REMOVE_HEAD(&tree_ids, entry);
885 path = qid->data;
887 if (got_object_idset_contains(idset, &qid->id) ||
888 got_object_idset_contains(idset_exclude, &qid->id)) {
889 free(qid->data);
890 got_object_qid_free(qid);
891 continue;
894 err = got_pack_add_object(want_meta,
895 want_meta ? idset : idset_exclude,
896 &qid->id, path, GOT_OBJ_TYPE_TREE,
897 mtime, seed, loose_obj_only, repo,
898 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
899 if (err) {
900 free(qid->data);
901 got_object_qid_free(qid);
902 break;
905 err = got_object_open_as_tree(&tree, repo, &qid->id);
906 if (err) {
907 free(qid->data);
908 got_object_qid_free(qid);
909 break;
912 err = got_pack_load_tree_entries(&tree_ids, want_meta, idset,
913 idset_exclude, tree, path, mtime, seed, repo,
914 loose_obj_only, ncolored, nfound, ntrees,
915 progress_cb, progress_arg, rl,
916 cancel_cb, cancel_arg);
917 free(qid->data);
918 got_object_qid_free(qid);
919 if (err)
920 break;
922 got_object_tree_close(tree);
923 tree = NULL;
926 STAILQ_FOREACH(qid, &tree_ids, entry)
927 free(qid->data);
928 got_object_id_queue_free(&tree_ids);
929 if (tree)
930 got_object_tree_close(tree);
931 return err;
934 static const struct got_error *
935 load_commit(int want_meta, struct got_object_idset *idset,
936 struct got_object_idset *idset_exclude,
937 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
938 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
939 got_pack_progress_cb progress_cb, void *progress_arg,
940 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
942 const struct got_error *err;
943 struct got_commit_object *commit;
945 if (got_object_idset_contains(idset, id) ||
946 got_object_idset_contains(idset_exclude, id))
947 return NULL;
949 if (loose_obj_only) {
950 int is_packed;
951 err = search_packidx(&is_packed, id, repo);
952 if (err)
953 return err;
954 if (is_packed && want_meta)
955 return NULL;
958 err = got_object_open_as_commit(&commit, repo, id);
959 if (err)
960 return err;
962 err = got_pack_add_object(want_meta,
963 want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_COMMIT,
964 got_object_commit_get_committer_time(commit), seed,
965 loose_obj_only, repo,
966 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
967 if (err)
968 goto done;
970 err = got_pack_load_tree(want_meta, idset, idset_exclude,
971 got_object_commit_get_tree_id(commit),
972 "", got_object_commit_get_committer_time(commit), seed,
973 repo, loose_obj_only, ncolored, nfound, ntrees,
974 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
975 done:
976 got_object_commit_close(commit);
977 return err;
980 static const struct got_error *
981 load_tag(int want_meta, struct got_object_idset *idset,
982 struct got_object_idset *idset_exclude,
983 struct got_object_id *id, struct got_repository *repo, uint32_t seed,
984 int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
985 got_pack_progress_cb progress_cb, void *progress_arg,
986 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
988 const struct got_error *err;
989 struct got_tag_object *tag = NULL;
991 if (got_object_idset_contains(idset, id) ||
992 got_object_idset_contains(idset_exclude, id))
993 return NULL;
995 if (loose_obj_only) {
996 int is_packed;
997 err = search_packidx(&is_packed, id, repo);
998 if (err)
999 return err;
1000 if (is_packed && want_meta)
1001 return NULL;
1004 err = got_object_open_as_tag(&tag, repo, id);
1005 if (err)
1006 return err;
1008 err = got_pack_add_object(want_meta,
1009 want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_TAG,
1010 got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1011 ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1012 if (err)
1013 goto done;
1015 switch (got_object_tag_get_object_type(tag)) {
1016 case GOT_OBJ_TYPE_COMMIT:
1017 err = load_commit(want_meta, idset, idset_exclude,
1018 got_object_tag_get_object_id(tag), repo, seed,
1019 loose_obj_only, ncolored, nfound, ntrees,
1020 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1021 break;
1022 case GOT_OBJ_TYPE_TREE:
1023 err = got_pack_load_tree(want_meta, idset, idset_exclude,
1024 got_object_tag_get_object_id(tag), "",
1025 got_object_tag_get_tagger_time(tag), seed, repo,
1026 loose_obj_only, ncolored, nfound, ntrees,
1027 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1028 break;
1029 default:
1030 break;
1033 done:
1034 got_object_tag_close(tag);
1035 return err;
1038 const struct got_error *
1039 got_pack_paint_commit(struct got_object_qid *qid, intptr_t color)
1041 if (color < 0 || color >= COLOR_MAX)
1042 return got_error(GOT_ERR_RANGE);
1044 qid->data = (void *)color;
1045 return NULL;
1048 const struct got_error *
1049 got_pack_queue_commit_id(struct got_object_id_queue *ids,
1050 struct got_object_id *id, intptr_t color, struct got_repository *repo)
1052 const struct got_error *err;
1053 struct got_object_qid *qid;
1055 err = got_object_qid_alloc(&qid, id);
1056 if (err)
1057 return err;
1059 STAILQ_INSERT_TAIL(ids, qid, entry);
1060 return got_pack_paint_commit(qid, color);
1063 struct append_id_arg {
1064 struct got_object_id **array;
1065 int idx;
1066 struct got_object_idset *drop;
1067 struct got_object_idset *skip;
1070 static const struct got_error *
1071 append_id(struct got_object_id *id, void *data, void *arg)
1073 struct append_id_arg *a = arg;
1075 if (got_object_idset_contains(a->skip, id) ||
1076 got_object_idset_contains(a->drop, id))
1077 return NULL;
1079 a->array[++a->idx] = got_object_id_dup(id);
1080 if (a->array[a->idx] == NULL)
1081 return got_error_from_errno("got_object_id_dup");
1083 return NULL;
1086 static const struct got_error *
1087 queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1088 struct got_object_id_queue *ids, struct got_repository *repo)
1090 const struct got_error *err;
1091 struct got_tag_object *tag = NULL;
1092 int obj_type;
1094 err = got_object_get_type(&obj_type, repo, id);
1095 if (err)
1096 return err;
1098 if (obj_type == GOT_OBJ_TYPE_TAG) {
1099 err = got_object_open_as_tag(&tag, repo, id);
1100 if (err)
1101 return err;
1102 obj_type = got_object_tag_get_object_type(tag);
1103 id = got_object_tag_get_object_id(tag);
1106 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1107 err = got_pack_queue_commit_id(ids, id, color, repo);
1108 if (err)
1109 goto done;
1111 done:
1112 if (tag)
1113 got_object_tag_close(tag);
1114 return err;
1117 const struct got_error *
1118 got_pack_find_pack_for_commit_painting(struct got_packidx **best_packidx,
1119 struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1121 const struct got_error *err = NULL;
1122 struct got_pathlist_entry *pe;
1123 const char *best_packidx_path = NULL;
1124 int nobj_max = 0;
1125 int ncommits_max = 0;
1127 *best_packidx = NULL;
1130 * Find the largest pack which contains at least some of the
1131 * commits we are interested in.
1133 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1134 const char *path_packidx = pe->path;
1135 struct got_packidx *packidx;
1136 int nobj, idx, ncommits = 0;
1137 struct got_object_qid *qid;
1139 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1140 if (err)
1141 break;
1143 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1144 if (nobj <= nobj_max)
1145 continue;
1147 STAILQ_FOREACH(qid, ids, entry) {
1148 idx = got_packidx_get_object_idx(packidx, &qid->id);
1149 if (idx != -1)
1150 ncommits++;
1152 if (ncommits > ncommits_max) {
1153 best_packidx_path = path_packidx;
1154 nobj_max = nobj;
1155 ncommits_max = ncommits;
1159 if (best_packidx_path && err == NULL) {
1160 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1161 repo);
1164 return err;
1167 static const struct got_error *
1168 findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1169 struct got_object_id **head, int nhead,
1170 struct got_object_id **tail, int ntail,
1171 struct got_repository *repo,
1172 got_pack_progress_cb progress_cb, void *progress_arg,
1173 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1175 const struct got_error *err = NULL;
1176 struct got_object_id_queue ids;
1177 struct got_object_idset *keep, *drop, *skip = NULL;
1178 int i, nkeep;
1180 STAILQ_INIT(&ids);
1181 *res = NULL;
1182 *nres = 0;
1183 *ncolored = 0;
1185 keep = got_object_idset_alloc();
1186 if (keep == NULL)
1187 return got_error_from_errno("got_object_idset_alloc");
1189 drop = got_object_idset_alloc();
1190 if (drop == NULL) {
1191 err = got_error_from_errno("got_object_idset_alloc");
1192 goto done;
1195 skip = got_object_idset_alloc();
1196 if (skip == NULL) {
1197 err = got_error_from_errno("got_object_idset_alloc");
1198 goto done;
1201 for (i = 0; i < nhead; i++) {
1202 struct got_object_id *id = head[i];
1203 if (id == NULL)
1204 continue;
1205 err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1206 if (err)
1207 goto done;
1210 for (i = 0; i < ntail; i++) {
1211 struct got_object_id *id = tail[i];
1212 if (id == NULL)
1213 continue;
1214 err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1215 if (err)
1216 goto done;
1219 err = got_pack_paint_commits(ncolored, &ids, nhead + ntail,
1220 keep, drop, skip, repo, progress_cb, progress_arg, rl,
1221 cancel_cb, cancel_arg);
1222 if (err)
1223 goto done;
1225 nkeep = got_object_idset_num_elements(keep);
1226 if (nkeep > 0) {
1227 struct append_id_arg arg;
1228 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1229 if (arg.array == NULL) {
1230 err = got_error_from_errno("calloc");
1231 goto done;
1233 arg.idx = -1;
1234 arg.skip = skip;
1235 arg.drop = drop;
1236 err = got_object_idset_for_each(keep, append_id, &arg);
1237 if (err) {
1238 free(arg.array);
1239 goto done;
1241 *res = arg.array;
1242 *nres = arg.idx + 1;
1244 done:
1245 got_object_idset_free(keep);
1246 got_object_idset_free(drop);
1247 if (skip)
1248 got_object_idset_free(skip);
1249 got_object_id_queue_free(&ids);
1250 return err;
1253 static const struct got_error *
1254 find_pack_for_enumeration(struct got_packidx **best_packidx,
1255 struct got_object_id **ids, int nids, struct got_repository *repo)
1257 const struct got_error *err = NULL;
1258 struct got_pathlist_entry *pe;
1259 const char *best_packidx_path = NULL;
1260 int nobj_max = 0;
1261 int ncommits_max = 0;
1263 *best_packidx = NULL;
1266 * Find the largest pack which contains at least some of the
1267 * commits and tags we are interested in.
1269 TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1270 const char *path_packidx = pe->path;
1271 struct got_packidx *packidx;
1272 int nobj, i, idx, ncommits = 0;
1274 err = got_repo_get_packidx(&packidx, path_packidx, repo);
1275 if (err)
1276 break;
1278 nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1279 if (nobj <= nobj_max)
1280 continue;
1282 for (i = 0; i < nids; i++) {
1283 idx = got_packidx_get_object_idx(packidx, ids[i]);
1284 if (idx != -1)
1285 ncommits++;
1287 if (ncommits > ncommits_max) {
1288 best_packidx_path = path_packidx;
1289 nobj_max = nobj;
1290 ncommits_max = ncommits;
1294 if (best_packidx_path && err == NULL) {
1295 err = got_repo_get_packidx(best_packidx, best_packidx_path,
1296 repo);
1299 return err;
1302 static const struct got_error *
1303 load_object_ids(int *ncolored, int *nfound, int *ntrees,
1304 struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1305 struct got_object_id **ours, int nours, struct got_repository *repo,
1306 uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1307 void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1308 void *cancel_arg)
1310 const struct got_error *err = NULL;
1311 struct got_object_id **ids = NULL;
1312 struct got_packidx *packidx = NULL;
1313 int i, nobj = 0, obj_type, found_all_objects = 0;
1314 struct got_object_idset *idset_exclude;
1316 idset_exclude = got_object_idset_alloc();
1317 if (idset_exclude == NULL)
1318 return got_error_from_errno("got_object_idset_alloc");
1320 *ncolored = 0;
1321 *nfound = 0;
1322 *ntrees = 0;
1324 err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1325 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1326 if (err)
1327 goto done;
1329 err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
1330 if (err)
1331 goto done;
1332 if (packidx) {
1333 err = got_pack_load_packed_object_ids(&found_all_objects,
1334 theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
1335 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1336 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1337 if (err)
1338 goto done;
1341 for (i = 0; i < ntheirs; i++) {
1342 struct got_object_id *id = theirs[i];
1343 if (id == NULL)
1344 continue;
1345 err = got_object_get_type(&obj_type, repo, id);
1346 if (err)
1347 return err;
1348 if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1349 if (!found_all_objects) {
1350 err = load_commit(0, idset, idset_exclude,
1351 id, repo, seed, loose_obj_only,
1352 ncolored, nfound, ntrees,
1353 progress_cb, progress_arg, rl,
1354 cancel_cb, cancel_arg);
1355 if (err)
1356 goto done;
1358 } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1359 err = load_tag(0, idset, idset_exclude, id, repo,
1360 seed, loose_obj_only, ncolored, nfound, ntrees,
1361 progress_cb, progress_arg, rl,
1362 cancel_cb, cancel_arg);
1363 if (err)
1364 goto done;
1368 found_all_objects = 0;
1369 err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
1370 if (err)
1371 goto done;
1372 if (packidx) {
1373 err = got_pack_load_packed_object_ids(&found_all_objects, ids,
1374 nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
1375 loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1376 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1377 if (err)
1378 goto done;
1381 if (!found_all_objects) {
1382 for (i = 0; i < nobj; i++) {
1383 err = load_commit(1, idset, idset_exclude, ids[i],
1384 repo, seed, loose_obj_only, ncolored, nfound,
1385 ntrees, progress_cb, progress_arg, rl,
1386 cancel_cb, cancel_arg);
1387 if (err)
1388 goto done;
1392 for (i = 0; i < nours; i++) {
1393 struct got_object_id *id = ours[i];
1394 struct got_pack_meta *m;
1395 if (id == NULL)
1396 continue;
1397 m = got_object_idset_get(idset, id);
1398 if (m == NULL) {
1399 err = got_object_get_type(&obj_type, repo, id);
1400 if (err)
1401 goto done;
1402 } else
1403 obj_type = m->obj_type;
1404 if (obj_type != GOT_OBJ_TYPE_TAG)
1405 continue;
1406 err = load_tag(1, idset, idset_exclude, id, repo,
1407 seed, loose_obj_only, ncolored, nfound, ntrees,
1408 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1409 if (err)
1410 goto done;
1412 done:
1413 for (i = 0; i < nobj; i++) {
1414 free(ids[i]);
1416 free(ids);
1417 got_object_idset_free(idset_exclude);
1418 return err;
1421 static const struct got_error *
1422 hwrite(int fd, const void *buf, off_t len, SHA1_CTX *ctx)
1424 SHA1Update(ctx, buf, len);
1425 return got_poll_write_full(fd, buf, len);
1428 static const struct got_error *
1429 hcopy(FILE *fsrc, int fd_dst, off_t len, SHA1_CTX *ctx)
1431 const struct got_error *err;
1432 unsigned char buf[65536];
1433 off_t remain = len;
1434 size_t n;
1436 while (remain > 0) {
1437 size_t copylen = MIN(sizeof(buf), remain);
1438 n = fread(buf, 1, copylen, fsrc);
1439 if (n != copylen)
1440 return got_ferror(fsrc, GOT_ERR_IO);
1441 SHA1Update(ctx, buf, copylen);
1442 err = got_poll_write_full(fd_dst, buf, copylen);
1443 if (err)
1444 return err;
1445 remain -= copylen;
1448 return NULL;
1451 static const struct got_error *
1452 hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1453 int fd, off_t len, SHA1_CTX *ctx)
1455 if (src_offset + len > src_size)
1456 return got_error(GOT_ERR_RANGE);
1458 SHA1Update(ctx, src + src_offset, len);
1459 return got_poll_write_full(fd, src + src_offset, len);
1462 static void
1463 putbe32(char *b, uint32_t n)
1465 b[0] = n >> 24;
1466 b[1] = n >> 16;
1467 b[2] = n >> 8;
1468 b[3] = n >> 0;
1471 static int
1472 write_order_cmp(const void *pa, const void *pb)
1474 struct got_pack_meta *a, *b, *ahd, *bhd;
1476 a = *(struct got_pack_meta **)pa;
1477 b = *(struct got_pack_meta **)pb;
1478 ahd = (a->head == NULL) ? a : a->head;
1479 bhd = (b->head == NULL) ? b : b->head;
1480 if (bhd->mtime < ahd->mtime)
1481 return -1;
1482 if (bhd->mtime > ahd->mtime)
1483 return 1;
1484 if (bhd < ahd)
1485 return -1;
1486 if (bhd > ahd)
1487 return 1;
1488 if (a->nchain != b->nchain)
1489 return a->nchain - b->nchain;
1490 if (a->mtime < b->mtime)
1491 return -1;
1492 if (a->mtime > b->mtime)
1493 return 1;
1494 return got_object_id_cmp(&a->id, &b->id);
1497 static int
1498 reuse_write_order_cmp(const void *pa, const void *pb)
1500 struct got_pack_meta *a, *b;
1502 a = *(struct got_pack_meta **)pa;
1503 b = *(struct got_pack_meta **)pb;
1505 if (a->reused_delta_offset < b->reused_delta_offset)
1506 return -1;
1507 if (a->reused_delta_offset > b->reused_delta_offset)
1508 return 1;
1509 return 0;
1512 static const struct got_error *
1513 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1515 size_t i;
1517 *hdrlen = 0;
1519 hdr[0] = obj_type << 4;
1520 hdr[0] |= len & 0xf;
1521 len >>= 4;
1522 for (i = 1; len != 0; i++){
1523 if (i >= bufsize)
1524 return got_error(GOT_ERR_NO_SPACE);
1525 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1526 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1527 len >>= GOT_DELTA_SIZE_SHIFT;
1530 *hdrlen = i;
1531 return NULL;
1534 static int
1535 packoff(char *hdr, off_t off)
1537 int i, j;
1538 char rbuf[8];
1540 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1541 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1542 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1543 GOT_DELTA_SIZE_MORE;
1546 j = 0;
1547 while (i > 0)
1548 hdr[j++] = rbuf[--i];
1549 return j;
1552 static const struct got_error *
1553 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, int packfd,
1554 struct got_pack_meta *m)
1556 const struct got_error *err;
1557 char buf[32];
1558 int nh;
1560 if (m->prev->off != 0) {
1561 err = packhdr(&nh, buf, sizeof(buf),
1562 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1563 if (err)
1564 return err;
1565 nh += packoff(buf + nh, m->off - m->prev->off);
1566 err = hwrite(packfd, buf, nh, ctx);
1567 if (err)
1568 return err;
1569 *packfile_size += nh;
1570 } else {
1571 err = packhdr(&nh, buf, sizeof(buf),
1572 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1573 if (err)
1574 return err;
1575 err = hwrite(packfd, buf, nh, ctx);
1576 if (err)
1577 return err;
1578 *packfile_size += nh;
1579 err = hwrite(packfd, m->prev->id.sha1,
1580 sizeof(m->prev->id.sha1), ctx);
1581 if (err)
1582 return err;
1583 *packfile_size += sizeof(m->prev->id.sha1);
1586 return NULL;
1589 static const struct got_error *
1590 write_packed_object(off_t *packfile_size, int packfd,
1591 FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1592 struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
1593 struct got_repository *repo)
1595 const struct got_error *err = NULL;
1596 struct got_deflate_checksum csum;
1597 char buf[32];
1598 int nh;
1599 struct got_raw_object *raw = NULL;
1600 off_t outlen;
1602 csum.output_sha1 = ctx;
1603 csum.output_crc = NULL;
1605 m->off = *packfile_size;
1606 if (m->delta_len == 0) {
1607 err = got_object_raw_open(&raw, outfd, repo, &m->id);
1608 if (err)
1609 goto done;
1610 err = packhdr(&nh, buf, sizeof(buf),
1611 m->obj_type, raw->size);
1612 if (err)
1613 goto done;
1614 err = hwrite(packfd, buf, nh, ctx);
1615 if (err)
1616 goto done;
1617 *packfile_size += nh;
1618 if (raw->f == NULL) {
1619 err = got_deflate_to_fd_mmap(&outlen,
1620 raw->data + raw->hdrlen, 0, raw->size,
1621 packfd, &csum);
1622 if (err)
1623 goto done;
1624 } else {
1625 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1626 == -1) {
1627 err = got_error_from_errno("fseeko");
1628 goto done;
1630 err = got_deflate_to_fd(&outlen, raw->f,
1631 raw->size, packfd, &csum);
1632 if (err)
1633 goto done;
1635 *packfile_size += outlen;
1636 got_object_raw_close(raw);
1637 raw = NULL;
1638 } else if (m->delta_buf) {
1639 err = deltahdr(packfile_size, ctx, packfd, m);
1640 if (err)
1641 goto done;
1642 err = hwrite(packfd, m->delta_buf,
1643 m->delta_compressed_len, ctx);
1644 if (err)
1645 goto done;
1646 *packfile_size += m->delta_compressed_len;
1647 free(m->delta_buf);
1648 m->delta_buf = NULL;
1649 } else if (delta_cache_map) {
1650 err = deltahdr(packfile_size, ctx, packfd, m);
1651 if (err)
1652 goto done;
1653 err = hcopy_mmap(delta_cache_map, m->delta_offset,
1654 delta_cache_size, packfd, m->delta_compressed_len,
1655 ctx);
1656 if (err)
1657 goto done;
1658 *packfile_size += m->delta_compressed_len;
1659 } else {
1660 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1661 == -1) {
1662 err = got_error_from_errno("fseeko");
1663 goto done;
1665 err = deltahdr(packfile_size, ctx, packfd, m);
1666 if (err)
1667 goto done;
1668 err = hcopy(delta_cache, packfd,
1669 m->delta_compressed_len, ctx);
1670 if (err)
1671 goto done;
1672 *packfile_size += m->delta_compressed_len;
1674 done:
1675 if (raw)
1676 got_object_raw_close(raw);
1677 return err;
1680 static const struct got_error *
1681 genpack(uint8_t *pack_sha1, int packfd, FILE *delta_cache,
1682 struct got_pack_meta **deltify, int ndeltify,
1683 struct got_pack_meta **reuse, int nreuse,
1684 int ncolored, int nfound, int ntrees, int nours,
1685 struct got_repository *repo,
1686 got_pack_progress_cb progress_cb, void *progress_arg,
1687 struct got_ratelimit *rl,
1688 got_cancel_cb cancel_cb, void *cancel_arg)
1690 const struct got_error *err = NULL;
1691 int i;
1692 SHA1_CTX ctx;
1693 struct got_pack_meta *m;
1694 char buf[32];
1695 off_t packfile_size = 0;
1696 int outfd = -1;
1697 int delta_cache_fd = -1;
1698 uint8_t *delta_cache_map = NULL;
1699 size_t delta_cache_size = 0;
1701 SHA1Init(&ctx);
1703 #ifndef GOT_PACK_NO_MMAP
1704 delta_cache_fd = dup(fileno(delta_cache));
1705 if (delta_cache_fd != -1) {
1706 struct stat sb;
1707 if (fstat(delta_cache_fd, &sb) == -1) {
1708 err = got_error_from_errno("fstat");
1709 goto done;
1711 if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1712 delta_cache_map = mmap(NULL, sb.st_size,
1713 PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1714 if (delta_cache_map == MAP_FAILED) {
1715 if (errno != ENOMEM) {
1716 err = got_error_from_errno("mmap");
1717 goto done;
1719 delta_cache_map = NULL; /* fallback on stdio */
1720 } else
1721 delta_cache_size = (size_t)sb.st_size;
1724 #endif
1725 err = hwrite(packfd, "PACK", 4, &ctx);
1726 if (err)
1727 goto done;
1728 putbe32(buf, GOT_PACKFILE_VERSION);
1729 err = hwrite(packfd, buf, 4, &ctx);
1730 if (err)
1731 goto done;
1732 putbe32(buf, ndeltify + nreuse);
1733 err = hwrite(packfd, buf, 4, &ctx);
1734 if (err)
1735 goto done;
1737 qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1738 write_order_cmp);
1739 for (i = 0; i < ndeltify; i++) {
1740 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1741 ncolored, nfound, ntrees, packfile_size, nours,
1742 ndeltify + nreuse, ndeltify + nreuse, i);
1743 if (err)
1744 goto done;
1745 m = deltify[i];
1746 err = write_packed_object(&packfile_size, packfd,
1747 delta_cache, delta_cache_map, delta_cache_size,
1748 m, &outfd, &ctx, repo);
1749 if (err)
1750 goto done;
1753 qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1754 reuse_write_order_cmp);
1755 for (i = 0; i < nreuse; i++) {
1756 err = got_pack_report_progress(progress_cb, progress_arg, rl,
1757 ncolored, nfound, ntrees, packfile_size, nours,
1758 ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1759 if (err)
1760 goto done;
1761 m = reuse[i];
1762 err = write_packed_object(&packfile_size, packfd,
1763 delta_cache, delta_cache_map, delta_cache_size,
1764 m, &outfd, &ctx, repo);
1765 if (err)
1766 goto done;
1769 SHA1Final(pack_sha1, &ctx);
1770 err = got_poll_write_full(packfd, pack_sha1, SHA1_DIGEST_LENGTH);
1771 if (err)
1772 goto done;
1773 packfile_size += SHA1_DIGEST_LENGTH;
1774 packfile_size += sizeof(struct got_packfile_hdr);
1775 if (progress_cb) {
1776 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1777 packfile_size, nours, ndeltify + nreuse,
1778 ndeltify + nreuse, ndeltify + nreuse);
1779 if (err)
1780 goto done;
1782 done:
1783 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1784 err = got_error_from_errno("close");
1785 if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1786 err = got_error_from_errno("munmap");
1787 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1788 err = got_error_from_errno("close");
1789 return err;
1792 static const struct got_error *
1793 add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1795 struct got_pack_meta *m = data;
1796 struct got_pack_metavec *v = arg;
1798 if (m->reused_delta_offset != 0)
1799 return NULL;
1801 return got_pack_add_meta(m, v);
1804 const struct got_error *
1805 got_pack_create(uint8_t *packsha1, int packfd, FILE *delta_cache,
1806 struct got_object_id **theirs, int ntheirs,
1807 struct got_object_id **ours, int nours,
1808 struct got_repository *repo, int loose_obj_only, int allow_empty,
1809 got_pack_progress_cb progress_cb, void *progress_arg,
1810 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1812 const struct got_error *err;
1813 int delta_cache_fd = -1;
1814 struct got_object_idset *idset;
1815 struct got_pack_metavec deltify, reuse;
1816 int ncolored = 0, nfound = 0, ntrees = 0;
1817 size_t ndeltify;
1818 uint32_t seed;
1820 seed = arc4random();
1822 memset(&deltify, 0, sizeof(deltify));
1823 memset(&reuse, 0, sizeof(reuse));
1825 idset = got_object_idset_alloc();
1826 if (idset == NULL)
1827 return got_error_from_errno("got_object_idset_alloc");
1829 err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1830 ntheirs, ours, nours, repo, seed, loose_obj_only,
1831 progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1832 if (err)
1833 goto done;
1835 if (progress_cb) {
1836 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1837 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1838 if (err)
1839 goto done;
1842 if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1843 err = got_error(GOT_ERR_CANNOT_PACK);
1844 goto done;
1847 delta_cache_fd = dup(fileno(delta_cache));
1848 if (delta_cache_fd == -1) {
1849 err = got_error_from_errno("dup");
1850 goto done;
1853 reuse.metasz = 64;
1854 reuse.meta = calloc(reuse.metasz,
1855 sizeof(struct got_pack_meta *));
1856 if (reuse.meta == NULL) {
1857 err = got_error_from_errno("calloc");
1858 goto done;
1861 err = got_pack_search_deltas(&reuse, idset, delta_cache_fd,
1862 ncolored, nfound, ntrees, nours,
1863 repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1864 if (err)
1865 goto done;
1867 if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1868 err = got_error_from_errno("fseeko");
1869 goto done;
1872 ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1873 if (ndeltify > 0) {
1874 deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1875 if (deltify.meta == NULL) {
1876 err = got_error_from_errno("calloc");
1877 goto done;
1879 deltify.metasz = ndeltify;
1881 err = got_object_idset_for_each(idset, add_meta_idset_cb,
1882 &deltify);
1883 if (err)
1884 goto done;
1885 if (deltify.nmeta > 0) {
1886 err = pick_deltas(deltify.meta, deltify.nmeta,
1887 ncolored, nfound, ntrees, nours, reuse.nmeta,
1888 delta_cache, repo, progress_cb, progress_arg, rl,
1889 cancel_cb, cancel_arg);
1890 if (err)
1891 goto done;
1895 if (fflush(delta_cache) == EOF) {
1896 err = got_error_from_errno("fflush");
1897 goto done;
1900 if (progress_cb) {
1902 * Report a 1-byte packfile write to indicate we are about
1903 * to start sending packfile data. gotd(8) needs this.
1905 err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1906 1 /* packfile_size */, nours,
1907 got_object_idset_num_elements(idset),
1908 deltify.nmeta + reuse.nmeta, 0);
1909 if (err)
1910 goto done;
1913 err = genpack(packsha1, packfd, delta_cache, deltify.meta,
1914 deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1915 nours, repo, progress_cb, progress_arg, rl,
1916 cancel_cb, cancel_arg);
1917 if (err)
1918 goto done;
1919 done:
1920 free_nmeta(deltify.meta, deltify.nmeta);
1921 free_nmeta(reuse.meta, reuse.nmeta);
1922 got_object_idset_free(idset);
1923 if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1924 err = got_error_from_errno("close");
1925 return err;