Blob


1 /*
2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #include <sys/types.h>
19 #include <sys/queue.h>
20 #include <sys/tree.h>
21 #include <sys/uio.h>
22 #include <sys/stat.h>
23 #include <sys/time.h>
25 #include <stdint.h>
26 #include <imsg.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <sha1.h>
31 #include <time.h>
32 #include <limits.h>
33 #include <zlib.h>
35 #include "got_error.h"
36 #include "got_cancel.h"
37 #include "got_object.h"
38 #include "got_path.h"
39 #include "got_reference.h"
40 #include "got_repository_admin.h"
41 #include "got_opentemp.h"
43 #include "got_lib_deltify.h"
44 #include "got_lib_delta.h"
45 #include "got_lib_object.h"
46 #include "got_lib_object_idset.h"
47 #include "got_lib_object_cache.h"
48 #include "got_lib_deflate.h"
49 #include "got_lib_pack.h"
50 #include "got_lib_privsep.h"
51 #include "got_lib_repository.h"
52 #include "got_lib_ratelimit.h"
54 #ifndef MIN
55 #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 #endif
58 #ifndef MAX
59 #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 #endif
62 struct got_pack_meta {
63 struct got_object_id id;
64 char *path;
65 int obj_type;
66 off_t size;
67 time_t mtime;
69 /* The best delta we picked */
70 struct got_pack_meta *head;
71 struct got_pack_meta *prev;
72 unsigned char *delta_buf; /* if not encoded in delta cache file */
73 off_t delta_offset; /* offset in delta cache file */
74 off_t delta_len; /* encoded delta length */
75 int nchain;
77 /* Only used for delta window */
78 struct got_delta_table *dtab;
80 /* Only used for writing offset deltas */
81 off_t off;
82 };
84 struct got_pack_metavec {
85 struct got_pack_meta **meta;
86 int nmeta;
87 int metasz;
88 };
90 static const struct got_error *
91 alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
92 const char *path, int obj_type, time_t mtime)
93 {
94 const struct got_error *err = NULL;
95 struct got_pack_meta *m;
97 *new = NULL;
99 m = calloc(1, sizeof(*m));
100 if (m == NULL)
101 return got_error_from_errno("calloc");
103 memcpy(&m->id, id, sizeof(m->id));
105 m->path = strdup(path);
106 if (m->path == NULL) {
107 err = got_error_from_errno("strdup");
108 free(m);
109 return err;
112 m->obj_type = obj_type;
113 m->mtime = mtime;
114 *new = m;
115 return NULL;
118 static void
119 clear_meta(struct got_pack_meta *meta)
121 if (meta == NULL)
122 return;
123 free(meta->path);
124 meta->path = NULL;
125 free(meta->delta_buf);
126 meta->delta_buf = NULL;
129 static void
130 free_nmeta(struct got_pack_meta **meta, int nmeta)
132 int i;
134 for (i = 0; i < nmeta; i++)
135 clear_meta(meta[i]);
136 free(meta);
139 static int
140 delta_order_cmp(const void *pa, const void *pb)
142 struct got_pack_meta *a, *b;
143 int cmp;
145 a = *(struct got_pack_meta **)pa;
146 b = *(struct got_pack_meta **)pb;
148 if (a->obj_type != b->obj_type)
149 return a->obj_type - b->obj_type;
150 cmp = strcmp(a->path, b->path);
151 if (cmp != 0)
152 return cmp;
153 if (a->mtime != b->mtime)
154 return a->mtime - b->mtime;
155 return got_object_id_cmp(&a->id, &b->id);
158 static off_t
159 delta_size(struct got_delta_instruction *deltas, int ndeltas)
161 int i;
162 off_t size = 32;
163 for (i = 0; i < ndeltas; i++) {
164 if (deltas[i].copy)
165 size += GOT_DELTA_SIZE_SHIFT;
166 else
167 size += deltas[i].len + 1;
169 return size;
172 static const struct got_error *
173 append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
175 char *n;
177 if (*len + nseg >= *sz) {
178 while (*len + nseg >= *sz)
179 *sz += *sz / 2;
180 n = realloc(*p, *sz);
181 if (n == NULL)
182 return got_error_from_errno("realloc");
183 *p = n;
185 memcpy(*p + *len, seg, nseg);
186 *len += nseg;
187 return NULL;
190 static const struct got_error *
191 encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
192 struct got_delta_instruction *deltas, int ndeltas,
193 off_t delta_size, off_t base_size)
195 const struct got_error *err;
196 unsigned char buf[16], *bp;
197 int i, j;
198 size_t len = 0;
199 off_t n;
200 struct got_delta_instruction *d;
202 m->delta_buf = malloc(delta_size);
203 if (m->delta_buf == NULL)
204 return got_error_from_errno("calloc");
206 /* base object size */
207 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
208 n = base_size >> GOT_DELTA_SIZE_SHIFT;
209 for (i = 1; n > 0; i++) {
210 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
211 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
212 n >>= GOT_DELTA_SIZE_SHIFT;
214 err = append(&m->delta_buf, &len, &delta_size, buf, i);
215 if (err)
216 return err;
218 /* target object size */
219 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
220 n = o->size >> GOT_DELTA_SIZE_SHIFT;
221 for (i = 1; n > 0; i++) {
222 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
223 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
224 n >>= GOT_DELTA_SIZE_SHIFT;
226 err = append(&m->delta_buf, &len, &delta_size, buf, i);
227 if (err)
228 return err;
230 for (j = 0; j < ndeltas; j++) {
231 d = &deltas[j];
232 if (d->copy) {
233 n = d->offset;
234 bp = &buf[1];
235 buf[0] = GOT_DELTA_BASE_COPY;
236 for (i = 0; i < 4; i++) {
237 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
238 buf[0] |= 1 << i;
239 *bp++ = n & 0xff;
240 n >>= 8;
241 if (n == 0)
242 break;
245 n = d->len;
246 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
247 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
248 for (i = 0; i < 3 && n > 0; i++) {
249 buf[0] |= 1 << (i + 4);
250 *bp++ = n & 0xff;
251 n >>= 8;
254 err = append(&m->delta_buf, &len, &delta_size,
255 buf, bp - buf);
256 if (err)
257 return err;
258 } else if (o->f == NULL) {
259 n = 0;
260 while (n != d->len) {
261 buf[0] = (d->len - n < 127) ? d->len - n : 127;
262 err = append(&m->delta_buf, &len, &delta_size,
263 buf, 1);
264 if (err)
265 return err;
266 err = append(&m->delta_buf, &len, &delta_size,
267 o->data + o->hdrlen + d->offset + n,
268 buf[0]);
269 if (err)
270 return err;
271 n += buf[0];
273 } else {
274 char content[128];
275 size_t r;
276 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
277 return got_error_from_errno("fseeko");
278 n = 0;
279 while (n != d->len) {
280 buf[0] = (d->len - n < 127) ? d->len - n : 127;
281 err = append(&m->delta_buf, &len, &delta_size,
282 buf, 1);
283 if (err)
284 return err;
285 r = fread(content, 1, buf[0], o->f);
286 if (r != buf[0])
287 return got_ferror(o->f, GOT_ERR_IO);
288 err = append(&m->delta_buf, &len, &delta_size,
289 content, buf[0]);
290 if (err)
291 return err;
292 n += buf[0];
297 m->delta_len = len;
298 return NULL;
301 static const struct got_error *
302 encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
303 struct got_delta_instruction *deltas, int ndeltas,
304 off_t base_size, FILE *f)
306 unsigned char buf[16], *bp;
307 int i, j;
308 off_t n;
309 size_t w;
310 struct got_delta_instruction *d;
312 /* base object size */
313 buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
314 n = base_size >> GOT_DELTA_SIZE_SHIFT;
315 for (i = 1; n > 0; i++) {
316 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
317 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
318 n >>= GOT_DELTA_SIZE_SHIFT;
320 w = fwrite(buf, 1, i, f);
321 if (w != i)
322 return got_ferror(f, GOT_ERR_IO);
324 /* target object size */
325 buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
326 n = o->size >> GOT_DELTA_SIZE_SHIFT;
327 for (i = 1; n > 0; i++) {
328 buf[i - 1] |= GOT_DELTA_SIZE_MORE;
329 buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
330 n >>= GOT_DELTA_SIZE_SHIFT;
332 w = fwrite(buf, 1, i, f);
333 if (w != i)
334 return got_ferror(f, GOT_ERR_IO);
336 for (j = 0; j < ndeltas; j++) {
337 d = &deltas[j];
338 if (d->copy) {
339 n = d->offset;
340 bp = &buf[1];
341 buf[0] = GOT_DELTA_BASE_COPY;
342 for (i = 0; i < 4; i++) {
343 /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
344 buf[0] |= 1 << i;
345 *bp++ = n & 0xff;
346 n >>= 8;
347 if (n == 0)
348 break;
351 n = d->len;
352 if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
353 /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
354 for (i = 0; i < 3 && n > 0; i++) {
355 buf[0] |= 1 << (i + 4);
356 *bp++ = n & 0xff;
357 n >>= 8;
360 w = fwrite(buf, 1, bp - buf, f);
361 if (w != bp - buf)
362 return got_ferror(f, GOT_ERR_IO);
363 } else if (o->f == NULL) {
364 n = 0;
365 while (n != d->len) {
366 buf[0] = (d->len - n < 127) ? d->len - n : 127;
367 w = fwrite(buf, 1, 1, f);
368 if (w != 1)
369 return got_ferror(f, GOT_ERR_IO);
370 w = fwrite(o->data + o->hdrlen + d->offset + n,
371 1, buf[0], f);
372 if (w != buf[0])
373 return got_ferror(f, GOT_ERR_IO);
374 n += buf[0];
376 } else {
377 char content[128];
378 size_t r;
379 if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
380 return got_error_from_errno("fseeko");
381 n = 0;
382 while (n != d->len) {
383 buf[0] = (d->len - n < 127) ? d->len - n : 127;
384 w = fwrite(buf, 1, 1, f);
385 if (w != 1)
386 return got_ferror(f, GOT_ERR_IO);
387 r = fread(content, 1, buf[0], o->f);
388 if (r != buf[0])
389 return got_ferror(o->f, GOT_ERR_IO);
390 w = fwrite(content, 1, buf[0], f);
391 if (w != buf[0])
392 return got_ferror(f, GOT_ERR_IO);
393 n += buf[0];
398 m->delta_len = ftello(f) - m->delta_offset;
399 return NULL;
402 static const struct got_error *
403 report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
404 struct got_ratelimit *rl, off_t packfile_size, int ncommits,
405 int nobj_total, int obj_deltify, int nobj_written)
407 const struct got_error *err;
408 int elapsed;
410 if (progress_cb == NULL)
411 return NULL;
413 err = got_ratelimit_check(&elapsed, rl);
414 if (err || !elapsed)
415 return err;
417 return progress_cb(progress_arg, packfile_size, ncommits,
418 nobj_total, obj_deltify, nobj_written);
421 static const struct got_error *
422 pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
423 FILE *delta_cache, struct got_repository *repo,
424 got_pack_progress_cb progress_cb, void *progress_arg,
425 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
427 const struct got_error *err = NULL;
428 struct got_pack_meta *m = NULL, *base = NULL;
429 struct got_raw_object *raw = NULL, *base_raw = NULL;
430 struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
431 int i, j, ndeltas, best_ndeltas;
432 off_t size, best_size;
433 const int max_base_candidates = 3;
434 size_t delta_memsize = 0;
435 const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
436 int outfd = -1;
438 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
439 for (i = 0; i < nmeta; i++) {
440 if (cancel_cb) {
441 err = (*cancel_cb)(cancel_arg);
442 if (err)
443 break;
445 err = report_progress(progress_cb, progress_arg, rl,
446 0L, nours, nmeta, i, 0);
447 if (err)
448 goto done;
449 m = meta[i];
451 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
452 m->obj_type == GOT_OBJ_TYPE_TAG)
453 continue;
455 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
456 if (err)
457 goto done;
458 m->size = raw->size;
460 if (raw->f == NULL) {
461 err = got_deltify_init_mem(&m->dtab, raw->data,
462 raw->hdrlen, raw->size + raw->hdrlen);
463 } else {
464 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
465 raw->size + raw->hdrlen);
467 if (err)
468 goto done;
470 if (i > max_base_candidates) {
471 struct got_pack_meta *n = NULL;
472 n = meta[i - (max_base_candidates + 1)];
473 got_deltify_free(n->dtab);
474 n->dtab = NULL;
477 best_size = raw->size;
478 best_ndeltas = 0;
479 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
480 if (cancel_cb) {
481 err = (*cancel_cb)(cancel_arg);
482 if (err)
483 goto done;
485 base = meta[j];
486 /* long chains make unpacking slow, avoid such bases */
487 if (base->nchain >= 128 ||
488 base->obj_type != m->obj_type)
489 continue;
491 err = got_object_raw_open(&base_raw, &outfd, repo,
492 &base->id);
493 if (err)
494 goto done;
495 if (raw->f == NULL && base_raw->f == NULL) {
496 err = got_deltify_mem_mem(&deltas, &ndeltas,
497 raw->data, raw->hdrlen,
498 raw->size + raw->hdrlen,
499 base->dtab, base_raw->data,
500 base_raw->hdrlen,
501 base_raw->size + base_raw->hdrlen);
502 } else if (raw->f == NULL) {
503 err = got_deltify_mem_file(&deltas, &ndeltas,
504 raw->data, raw->hdrlen,
505 raw->size + raw->hdrlen,
506 base->dtab, base_raw->f,
507 base_raw->hdrlen,
508 base_raw->size + base_raw->hdrlen);
509 } else if (base_raw->f == NULL) {
510 err = got_deltify_file_mem(&deltas, &ndeltas,
511 raw->f, raw->hdrlen,
512 raw->size + raw->hdrlen,
513 base->dtab, base_raw->data,
514 base_raw->hdrlen,
515 base_raw->size + base_raw->hdrlen);
516 } else {
517 err = got_deltify(&deltas, &ndeltas,
518 raw->f, raw->hdrlen,
519 raw->size + raw->hdrlen,
520 base->dtab, base_raw->f, base_raw->hdrlen,
521 base_raw->size + base_raw->hdrlen);
523 got_object_raw_close(base_raw);
524 base_raw = NULL;
525 if (err)
526 goto done;
528 size = delta_size(deltas, ndeltas);
529 if (size + 32 < best_size){
530 /*
531 * if we already picked a best delta,
532 * replace it.
533 */
534 best_size = size;
535 free(best_deltas);
536 best_deltas = deltas;
537 best_ndeltas = ndeltas;
538 deltas = NULL;
539 m->nchain = base->nchain + 1;
540 m->prev = base;
541 m->head = base->head;
542 if (m->head == NULL)
543 m->head = base;
544 } else {
545 free(deltas);
546 deltas = NULL;
547 ndeltas = 0;
551 if (best_ndeltas > 0) {
552 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
553 delta_memsize + best_size <= max_delta_memsize) {
554 delta_memsize += best_size;
555 err = encode_delta_in_mem(m, raw, best_deltas,
556 best_ndeltas, best_size, m->prev->size);
557 } else {
558 m->delta_offset = ftello(delta_cache);
559 err = encode_delta(m, raw, best_deltas,
560 best_ndeltas, m->prev->size, delta_cache);
562 free(best_deltas);
563 best_deltas = NULL;
564 best_ndeltas = 0;
565 if (err)
566 goto done;
569 got_object_raw_close(raw);
570 raw = NULL;
572 done:
573 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
574 got_deltify_free(meta[i]->dtab);
575 meta[i]->dtab = NULL;
577 if (raw)
578 got_object_raw_close(raw);
579 if (base_raw)
580 got_object_raw_close(base_raw);
581 if (outfd != -1 && close(outfd) == -1 && err == NULL)
582 err = got_error_from_errno("close");
583 free(deltas);
584 free(best_deltas);
585 return err;
588 static const struct got_error *
589 search_packidx(int *found, struct got_object_id *id,
590 struct got_repository *repo)
592 const struct got_error *err = NULL;
593 struct got_packidx *packidx = NULL;
594 int idx;
596 *found = 0;
598 err = got_repo_search_packidx(&packidx, &idx, repo, id);
599 if (err == NULL)
600 *found = 1; /* object is already packed */
601 else if (err->code == GOT_ERR_NO_OBJ)
602 err = NULL;
603 return err;
606 static const int obj_types[] = {
607 GOT_OBJ_TYPE_ANY,
608 GOT_OBJ_TYPE_COMMIT,
609 GOT_OBJ_TYPE_TREE,
610 GOT_OBJ_TYPE_BLOB,
611 GOT_OBJ_TYPE_TAG,
612 GOT_OBJ_TYPE_OFFSET_DELTA,
613 GOT_OBJ_TYPE_REF_DELTA
614 };
616 static const struct got_error *
617 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
618 struct got_object_id *id, const char *path, int obj_type,
619 time_t mtime, int loose_obj_only, struct got_repository *repo)
621 const struct got_error *err;
622 struct got_pack_meta *m;
624 if (loose_obj_only) {
625 int is_packed;
626 err = search_packidx(&is_packed, id, repo);
627 if (err)
628 return err;
629 if (is_packed)
630 return NULL;
633 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
634 if (err)
635 return err;
637 if (v == NULL)
638 return NULL;
640 err = alloc_meta(&m, id, path, obj_type, mtime);
641 if (err)
642 goto done;
644 if (v->nmeta == v->metasz){
645 size_t newsize = 2 * v->metasz;
646 struct got_pack_meta **new;
647 new = reallocarray(v->meta, newsize, sizeof(*new));
648 if (new == NULL) {
649 err = got_error_from_errno("reallocarray");
650 goto done;
652 v->meta = new;
653 v->metasz = newsize;
655 done:
656 if (err) {
657 clear_meta(m);
658 free(m);
659 } else
660 v->meta[v->nmeta++] = m;
662 return err;
665 static const struct got_error *
666 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
667 struct got_object_idset *idset, struct got_object_id *tree_id,
668 const char *dpath, time_t mtime, struct got_repository *repo,
669 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
671 const struct got_error *err;
672 struct got_tree_object *tree;
673 char *p = NULL;
674 int i;
676 err = got_object_open_as_tree(&tree, repo, tree_id);
677 if (err)
678 return err;
680 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
681 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
682 struct got_object_id *id = got_tree_entry_get_id(e);
683 mode_t mode = got_tree_entry_get_mode(e);
685 if (cancel_cb) {
686 err = (*cancel_cb)(cancel_arg);
687 if (err)
688 break;
691 if (got_object_tree_entry_is_submodule(e) ||
692 got_object_idset_contains(idset, id))
693 continue;
695 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
696 got_tree_entry_get_name(e)) == -1) {
697 err = got_error_from_errno("asprintf");
698 break;
701 if (S_ISDIR(mode)) {
702 struct got_object_qid *qid;
703 err = got_object_qid_alloc(&qid, id);
704 if (err)
705 break;
706 STAILQ_INSERT_TAIL(ids, qid, entry);
707 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
708 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
709 mtime, loose_obj_only, repo);
710 if (err)
711 break;
713 free(p);
714 p = NULL;
717 got_object_tree_close(tree);
718 free(p);
719 return err;
722 static const struct got_error *
723 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
724 struct got_object_id *tree_id, const char *dpath, time_t mtime,
725 int loose_obj_only, struct got_repository *repo,
726 got_cancel_cb cancel_cb, void *cancel_arg)
728 const struct got_error *err = NULL;
729 struct got_object_id_queue tree_ids;
730 struct got_object_qid *qid;
732 if (got_object_idset_contains(idset, tree_id))
733 return NULL;
735 err = got_object_qid_alloc(&qid, tree_id);
736 if (err)
737 return err;
739 STAILQ_INIT(&tree_ids);
740 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
742 while (!STAILQ_EMPTY(&tree_ids)) {
743 if (cancel_cb) {
744 err = (*cancel_cb)(cancel_arg);
745 if (err)
746 break;
749 qid = STAILQ_FIRST(&tree_ids);
750 STAILQ_REMOVE_HEAD(&tree_ids, entry);
752 if (got_object_idset_contains(idset, qid->id)) {
753 got_object_qid_free(qid);
754 continue;
757 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
758 mtime, loose_obj_only, repo);
759 if (err) {
760 got_object_qid_free(qid);
761 break;
764 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
765 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
766 got_object_qid_free(qid);
767 if (err)
768 break;
771 got_object_id_queue_free(&tree_ids);
772 return err;
775 static const struct got_error *
776 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
777 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
778 got_cancel_cb cancel_cb, void *cancel_arg)
780 const struct got_error *err;
781 struct got_commit_object *commit;
783 if (got_object_idset_contains(idset, id))
784 return NULL;
786 if (loose_obj_only) {
787 int is_packed;
788 err = search_packidx(&is_packed, id, repo);
789 if (err)
790 return err;
791 if (is_packed)
792 return NULL;
795 err = got_object_open_as_commit(&commit, repo, id);
796 if (err)
797 return err;
799 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
800 got_object_commit_get_committer_time(commit),
801 loose_obj_only, repo);
802 if (err)
803 goto done;
805 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
806 "", got_object_commit_get_committer_time(commit),
807 loose_obj_only, repo, cancel_cb, cancel_arg);
808 done:
809 got_object_commit_close(commit);
810 return err;
813 static const struct got_error *
814 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
815 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
816 got_cancel_cb cancel_cb, void *cancel_arg)
818 const struct got_error *err;
819 struct got_tag_object *tag = NULL;
821 if (got_object_idset_contains(idset, id))
822 return NULL;
824 if (loose_obj_only) {
825 int is_packed;
826 err = search_packidx(&is_packed, id, repo);
827 if (err)
828 return err;
829 if (is_packed)
830 return NULL;
833 err = got_object_open_as_tag(&tag, repo, id);
834 if (err)
835 return err;
837 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
838 got_object_tag_get_tagger_time(tag),
839 loose_obj_only, repo);
840 if (err)
841 goto done;
843 switch (got_object_tag_get_object_type(tag)) {
844 case GOT_OBJ_TYPE_COMMIT:
845 err = load_commit(v, idset,
846 got_object_tag_get_object_id(tag), repo,
847 loose_obj_only, cancel_cb, cancel_arg);
848 break;
849 case GOT_OBJ_TYPE_TREE:
850 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
851 "", got_object_tag_get_tagger_time(tag),
852 loose_obj_only, repo, cancel_cb, cancel_arg);
853 break;
854 default:
855 break;
858 done:
859 got_object_tag_close(tag);
860 return err;
863 enum findtwixt_color {
864 COLOR_KEEP = 0,
865 COLOR_DROP,
866 COLOR_BLANK,
867 };
868 static const int findtwixt_colors[] = {
869 COLOR_KEEP,
870 COLOR_DROP,
871 COLOR_BLANK
872 };
874 static const struct got_error *
875 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
876 int color, struct got_repository *repo)
878 const struct got_error *err;
879 struct got_object_qid *qid;
881 err = got_object_qid_alloc(&qid, id);
882 if (err)
883 return err;
885 STAILQ_INSERT_TAIL(ids, qid, entry);
886 qid->data = (void *)&findtwixt_colors[color];
887 return NULL;
890 static const struct got_error *
891 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
892 struct got_object_id *id, struct got_repository *repo,
893 got_cancel_cb cancel_cb, void *cancel_arg)
895 const struct got_error *err = NULL;
896 struct got_commit_object *commit;
897 const struct got_object_id_queue *parents;
898 struct got_object_id_queue ids;
899 struct got_object_qid *qid;
901 STAILQ_INIT(&ids);
903 err = got_object_qid_alloc(&qid, id);
904 if (err)
905 return err;
906 STAILQ_INSERT_HEAD(&ids, qid, entry);
908 while (!STAILQ_EMPTY(&ids)) {
909 if (cancel_cb) {
910 err = (*cancel_cb)(cancel_arg);
911 if (err)
912 break;
915 qid = STAILQ_FIRST(&ids);
916 STAILQ_REMOVE_HEAD(&ids, entry);
918 if (got_object_idset_contains(drop, qid->id)) {
919 got_object_qid_free(qid);
920 continue;
923 err = got_object_idset_add(drop, qid->id,
924 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
925 if (err) {
926 got_object_qid_free(qid);
927 break;
930 if (!got_object_idset_contains(keep, qid->id)) {
931 got_object_qid_free(qid);
932 continue;
935 err = got_object_open_as_commit(&commit, repo, qid->id);
936 got_object_qid_free(qid);
937 if (err)
938 break;
940 parents = got_object_commit_get_parent_ids(commit);
941 if (parents) {
942 err = got_object_id_queue_copy(parents, &ids);
943 if (err) {
944 got_object_commit_close(commit);
945 break;
948 got_object_commit_close(commit);
951 got_object_id_queue_free(&ids);
952 return err;
955 struct append_id_arg {
956 struct got_object_id **array;
957 int idx;
958 };
960 static const struct got_error *
961 append_id(struct got_object_id *id, void *data, void *arg)
963 struct append_id_arg *a = arg;
965 a->array[a->idx] = got_object_id_dup(id);
966 if (a->array[a->idx] == NULL)
967 return got_error_from_errno("got_object_id_dup");
969 a->idx++;
970 return NULL;
973 static const struct got_error *
974 findtwixt(struct got_object_id ***res, int *nres,
975 struct got_object_id **head, int nhead,
976 struct got_object_id **tail, int ntail,
977 struct got_repository *repo,
978 got_cancel_cb cancel_cb, void *cancel_arg)
980 const struct got_error *err = NULL;
981 struct got_object_id_queue ids;
982 struct got_object_idset *keep, *drop;
983 struct got_object_qid *qid;
984 int i, ncolor, nkeep, obj_type;
986 STAILQ_INIT(&ids);
987 *res = NULL;
988 *nres = 0;
990 keep = got_object_idset_alloc();
991 if (keep == NULL)
992 return got_error_from_errno("got_object_idset_alloc");
994 drop = got_object_idset_alloc();
995 if (drop == NULL) {
996 err = got_error_from_errno("got_object_idset_alloc");
997 goto done;
1000 for (i = 0; i < nhead; i++) {
1001 struct got_object_id *id = head[i];
1002 if (id == NULL)
1003 continue;
1004 err = got_object_get_type(&obj_type, repo, id);
1005 if (err)
1006 return err;
1007 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1008 continue;
1009 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1010 if (err)
1011 goto done;
1013 for (i = 0; i < ntail; i++) {
1014 struct got_object_id *id = tail[i];
1015 if (id == NULL)
1016 continue;
1017 err = got_object_get_type(&obj_type, repo, id);
1018 if (err)
1019 return err;
1020 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1021 continue;
1022 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1023 if (err)
1024 goto done;
1027 while (!STAILQ_EMPTY(&ids)) {
1028 int qcolor;
1029 qid = STAILQ_FIRST(&ids);
1030 qcolor = *((int *)qid->data);
1032 if (got_object_idset_contains(drop, qid->id))
1033 ncolor = COLOR_DROP;
1034 else if (got_object_idset_contains(keep, qid->id))
1035 ncolor = COLOR_KEEP;
1036 else
1037 ncolor = COLOR_BLANK;
1039 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1040 qcolor == COLOR_KEEP)) {
1041 STAILQ_REMOVE_HEAD(&ids, entry);
1042 got_object_qid_free(qid);
1043 continue;
1046 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1047 err = drop_commit(keep, drop, qid->id, repo,
1048 cancel_cb, cancel_arg);
1049 if (err)
1050 goto done;
1051 } else if (ncolor == COLOR_BLANK) {
1052 struct got_commit_object *commit;
1053 struct got_object_id *id;
1054 const struct got_object_id_queue *parents;
1055 struct got_object_qid *pid;
1057 id = got_object_id_dup(qid->id);
1058 if (id == NULL) {
1059 err = got_error_from_errno("got_object_id_dup");
1060 goto done;
1062 if (qcolor == COLOR_KEEP)
1063 err = got_object_idset_add(keep, id,
1064 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1065 else
1066 err = got_object_idset_add(drop, id,
1067 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1068 if (err) {
1069 free(id);
1070 goto done;
1073 err = got_object_open_as_commit(&commit, repo, id);
1074 if (err) {
1075 free(id);
1076 goto done;
1078 parents = got_object_commit_get_parent_ids(commit);
1079 if (parents) {
1080 STAILQ_FOREACH(pid, parents, entry) {
1081 err = queue_commit_id(&ids, pid->id,
1082 qcolor, repo);
1083 if (err) {
1084 free(id);
1085 goto done;
1089 got_object_commit_close(commit);
1090 commit = NULL;
1091 } else {
1092 /* should not happen */
1093 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1094 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1095 goto done;
1098 STAILQ_REMOVE_HEAD(&ids, entry);
1099 got_object_qid_free(qid);
1102 nkeep = got_object_idset_num_elements(keep);
1103 if (nkeep > 0) {
1104 struct append_id_arg arg;
1105 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1106 if (arg.array == NULL) {
1107 err = got_error_from_errno("calloc");
1108 goto done;
1110 arg.idx = 0;
1111 err = got_object_idset_for_each(keep, append_id, &arg);
1112 if (err) {
1113 free(arg.array);
1114 goto done;
1116 *res = arg.array;
1117 *nres = nkeep;
1119 done:
1120 got_object_idset_free(keep);
1121 got_object_idset_free(drop);
1122 got_object_id_queue_free(&ids);
1123 return err;
1126 static const struct got_error *
1127 read_meta(struct got_pack_meta ***meta, int *nmeta,
1128 struct got_object_id **theirs, int ntheirs,
1129 struct got_object_id **ours, int nours, struct got_repository *repo,
1130 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1131 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1133 const struct got_error *err = NULL;
1134 struct got_object_id **ids = NULL;
1135 struct got_object_idset *idset;
1136 int i, nobj = 0, obj_type;
1137 struct got_pack_metavec v;
1139 *meta = NULL;
1140 *nmeta = 0;
1142 idset = got_object_idset_alloc();
1143 if (idset == NULL)
1144 return got_error_from_errno("got_object_idset_alloc");
1146 v.nmeta = 0;
1147 v.metasz = 64;
1148 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
1149 if (v.meta == NULL) {
1150 err = got_error_from_errno("calloc");
1151 goto done;
1154 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1155 cancel_cb, cancel_arg);
1156 if (err || nobj == 0)
1157 goto done;
1159 for (i = 0; i < ntheirs; i++) {
1160 struct got_object_id *id = theirs[i];
1161 if (id == NULL)
1162 continue;
1163 err = got_object_get_type(&obj_type, repo, id);
1164 if (err)
1165 return err;
1166 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1167 continue;
1168 err = load_commit(NULL, idset, id, repo,
1169 loose_obj_only, cancel_cb, cancel_arg);
1170 if (err)
1171 goto done;
1172 err = report_progress(progress_cb, progress_arg, rl,
1173 0L, nours, v.nmeta, 0, 0);
1174 if (err)
1175 goto done;
1178 for (i = 0; i < ntheirs; i++) {
1179 struct got_object_id *id = theirs[i];
1180 int *cached_type;
1181 if (id == NULL)
1182 continue;
1183 cached_type = got_object_idset_get(idset, id);
1184 if (cached_type == NULL) {
1185 err = got_object_get_type(&obj_type, repo, id);
1186 if (err)
1187 goto done;
1188 } else
1189 obj_type = *cached_type;
1190 if (obj_type != GOT_OBJ_TYPE_TAG)
1191 continue;
1192 err = load_tag(NULL, idset, id, repo,
1193 loose_obj_only, cancel_cb, cancel_arg);
1194 if (err)
1195 goto done;
1196 err = report_progress(progress_cb, progress_arg, rl,
1197 0L, nours, v.nmeta, 0, 0);
1198 if (err)
1199 goto done;
1202 for (i = 0; i < nobj; i++) {
1203 err = load_commit(&v, idset, ids[i], repo,
1204 loose_obj_only, cancel_cb, cancel_arg);
1205 if (err)
1206 goto done;
1207 if (err)
1208 goto done;
1209 err = report_progress(progress_cb, progress_arg, rl,
1210 0L, nours, v.nmeta, 0, 0);
1211 if (err)
1212 goto done;
1215 for (i = 0; i < nours; i++) {
1216 struct got_object_id *id = ours[i];
1217 int *cached_type;
1218 if (id == NULL)
1219 continue;
1220 cached_type = got_object_idset_get(idset, id);
1221 if (cached_type == NULL) {
1222 err = got_object_get_type(&obj_type, repo, id);
1223 if (err)
1224 goto done;
1225 } else
1226 obj_type = *cached_type;
1227 if (obj_type != GOT_OBJ_TYPE_TAG)
1228 continue;
1229 err = load_tag(&v, idset, id, repo,
1230 loose_obj_only, cancel_cb, cancel_arg);
1231 if (err)
1232 goto done;
1233 err = report_progress(progress_cb, progress_arg, rl,
1234 0L, nours, v.nmeta, 0, 0);
1235 if (err)
1236 goto done;
1239 if (progress_cb) {
1240 err = progress_cb(progress_arg, 0L, nours, v.nmeta, 0, 0);
1241 if (err)
1242 goto done;
1244 done:
1245 for (i = 0; i < nobj; i++) {
1246 free(ids[i]);
1248 free(ids);
1249 got_object_idset_free(idset);
1250 if (err == NULL) {
1251 *meta = v.meta;
1252 *nmeta = v.nmeta;
1253 } else
1254 free(v.meta);
1256 return err;
1259 const struct got_error *
1260 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1262 size_t n;
1264 SHA1Update(ctx, buf, len);
1265 n = fwrite(buf, 1, len, f);
1266 if (n != len)
1267 return got_ferror(f, GOT_ERR_IO);
1268 return NULL;
1271 static void
1272 putbe32(char *b, uint32_t n)
1274 b[0] = n >> 24;
1275 b[1] = n >> 16;
1276 b[2] = n >> 8;
1277 b[3] = n >> 0;
1280 static int
1281 write_order_cmp(const void *pa, const void *pb)
1283 struct got_pack_meta *a, *b, *ahd, *bhd;
1285 a = *(struct got_pack_meta **)pa;
1286 b = *(struct got_pack_meta **)pb;
1287 ahd = (a->head == NULL) ? a : a->head;
1288 bhd = (b->head == NULL) ? b : b->head;
1289 if (ahd->mtime != bhd->mtime)
1290 return bhd->mtime - ahd->mtime;
1291 if (ahd != bhd)
1292 return (uintptr_t)bhd - (uintptr_t)ahd;
1293 if (a->nchain != b->nchain)
1294 return a->nchain - b->nchain;
1295 return a->mtime - b->mtime;
1298 static const struct got_error *
1299 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1301 size_t i;
1303 *hdrlen = 0;
1305 hdr[0] = obj_type << 4;
1306 hdr[0] |= len & 0xf;
1307 len >>= 4;
1308 for (i = 1; len != 0; i++){
1309 if (i >= bufsize)
1310 return got_error(GOT_ERR_NO_SPACE);
1311 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1312 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1313 len >>= GOT_DELTA_SIZE_SHIFT;
1316 *hdrlen = i;
1317 return NULL;
1320 static int
1321 packoff(char *hdr, off_t off)
1323 int i, j;
1324 char rbuf[8];
1326 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1327 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1328 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1329 GOT_DELTA_SIZE_MORE;
1332 j = 0;
1333 while (i > 0)
1334 hdr[j++] = rbuf[--i];
1335 return j;
1338 static const struct got_error *
1339 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1340 struct got_pack_meta *m, int use_offset_deltas)
1342 const struct got_error *err;
1343 char buf[32];
1344 int nh;
1346 if (use_offset_deltas && m->prev->off != 0) {
1347 err = packhdr(&nh, buf, sizeof(buf),
1348 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1349 if (err)
1350 return err;
1351 nh += packoff(buf + nh, m->off - m->prev->off);
1352 err = hwrite(packfile, buf, nh, ctx);
1353 if (err)
1354 return err;
1355 *packfile_size += nh;
1356 } else {
1357 err = packhdr(&nh, buf, sizeof(buf),
1358 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1359 if (err)
1360 return err;
1361 err = hwrite(packfile, buf, nh, ctx);
1362 if (err)
1363 return err;
1364 *packfile_size += nh;
1365 err = hwrite(packfile, m->prev->id.sha1,
1366 sizeof(m->prev->id.sha1), ctx);
1367 if (err)
1368 return err;
1369 *packfile_size += sizeof(m->prev->id.sha1);
1372 return NULL;
1375 static const struct got_error *
1376 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1377 struct got_pack_meta **meta, int nmeta, int nours,
1378 int use_offset_deltas, struct got_repository *repo,
1379 got_pack_progress_cb progress_cb, void *progress_arg,
1380 struct got_ratelimit *rl,
1381 got_cancel_cb cancel_cb, void *cancel_arg)
1383 const struct got_error *err = NULL;
1384 int i, nh;
1385 SHA1_CTX ctx;
1386 struct got_pack_meta *m;
1387 struct got_raw_object *raw = NULL;
1388 char buf[32];
1389 size_t n;
1390 struct got_deflate_checksum csum;
1391 off_t outlen, packfile_size = 0;
1392 int outfd = -1;
1394 SHA1Init(&ctx);
1395 csum.output_sha1 = &ctx;
1396 csum.output_crc = NULL;
1398 err = hwrite(packfile, "PACK", 4, &ctx);
1399 if (err)
1400 return err;
1401 putbe32(buf, GOT_PACKFILE_VERSION);
1402 err = hwrite(packfile, buf, 4, &ctx);
1403 if (err)
1404 goto done;
1405 putbe32(buf, nmeta);
1406 err = hwrite(packfile, buf, 4, &ctx);
1407 if (err)
1408 goto done;
1409 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1410 for (i = 0; i < nmeta; i++) {
1411 err = report_progress(progress_cb, progress_arg, rl,
1412 packfile_size, nours, nmeta, nmeta, i);
1413 if (err)
1414 goto done;
1415 m = meta[i];
1416 m->off = ftello(packfile);
1417 if (m->delta_len == 0) {
1418 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1419 if (err)
1420 goto done;
1421 err = packhdr(&nh, buf, sizeof(buf),
1422 m->obj_type, raw->size);
1423 if (err)
1424 goto done;
1425 err = hwrite(packfile, buf, nh, &ctx);
1426 if (err)
1427 goto done;
1428 packfile_size += nh;
1429 if (raw->f == NULL) {
1430 err = got_deflate_to_file_mmap(&outlen,
1431 raw->data + raw->hdrlen, 0, raw->size,
1432 packfile, &csum);
1433 if (err)
1434 goto done;
1435 } else {
1436 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1437 == -1) {
1438 err = got_error_from_errno("fseeko");
1439 goto done;
1441 err = got_deflate_to_file(&outlen, raw->f,
1442 raw->size, packfile, &csum);
1443 if (err)
1444 goto done;
1446 packfile_size += outlen;
1447 got_object_raw_close(raw);
1448 raw = NULL;
1449 } else if (m->delta_buf) {
1450 err = deltahdr(&packfile_size, &ctx, packfile,
1451 m, use_offset_deltas);
1452 if (err)
1453 goto done;
1454 err = got_deflate_to_file_mmap(&outlen,
1455 m->delta_buf, 0, m->delta_len, packfile, &csum);
1456 if (err)
1457 goto done;
1458 packfile_size += outlen;
1459 free(m->delta_buf);
1460 m->delta_buf = NULL;
1461 } else {
1462 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1463 == -1) {
1464 err = got_error_from_errno("fseeko");
1465 goto done;
1467 err = deltahdr(&packfile_size, &ctx, packfile,
1468 m, use_offset_deltas);
1469 if (err)
1470 goto done;
1471 err = got_deflate_to_file(&outlen, delta_cache,
1472 m->delta_len, packfile, &csum);
1473 if (err)
1474 goto done;
1475 packfile_size += outlen;
1478 SHA1Final(pack_sha1, &ctx);
1479 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1480 if (n != SHA1_DIGEST_LENGTH)
1481 err = got_ferror(packfile, GOT_ERR_IO);
1482 packfile_size += SHA1_DIGEST_LENGTH;
1483 packfile_size += sizeof(struct got_packfile_hdr);
1484 if (progress_cb) {
1485 err = progress_cb(progress_arg, packfile_size, nours,
1486 nmeta, nmeta, nmeta);
1487 if (err)
1488 goto done;
1490 done:
1491 if (raw)
1492 got_object_raw_close(raw);
1493 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1494 err = got_error_from_errno("close");
1495 return err;
1498 const struct got_error *
1499 got_pack_create(uint8_t *packsha1, FILE *packfile,
1500 struct got_object_id **theirs, int ntheirs,
1501 struct got_object_id **ours, int nours,
1502 struct got_repository *repo, int loose_obj_only, int allow_empty,
1503 got_pack_progress_cb progress_cb, void *progress_arg,
1504 got_cancel_cb cancel_cb, void *cancel_arg)
1506 const struct got_error *err;
1507 struct got_pack_meta **meta;
1508 int nmeta;
1509 FILE *delta_cache = NULL;
1510 struct got_ratelimit rl;
1512 got_ratelimit_init(&rl, 0, 500);
1514 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1515 loose_obj_only, progress_cb, progress_arg, &rl,
1516 cancel_cb, cancel_arg);
1517 if (err)
1518 return err;
1520 if (nmeta == 0 && !allow_empty) {
1521 err = got_error(GOT_ERR_CANNOT_PACK);
1522 goto done;
1525 delta_cache = got_opentemp();
1526 if (delta_cache == NULL) {
1527 err = got_error_from_errno("got_opentemp");
1528 goto done;
1531 if (nmeta > 0) {
1532 err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1533 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1534 if (err)
1535 goto done;
1536 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1537 err = got_error_from_errno("fseeko");
1538 goto done;
1542 err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1543 repo, progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1544 if (err)
1545 goto done;
1546 done:
1547 free_nmeta(meta, nmeta);
1548 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1549 err = got_error_from_errno("fclose");
1550 return err;