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 int outfd = -1;
436 qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
437 for (i = 0; i < nmeta; i++) {
438 if (cancel_cb) {
439 err = (*cancel_cb)(cancel_arg);
440 if (err)
441 break;
443 err = report_progress(progress_cb, progress_arg, rl,
444 0L, nours, nmeta, i, 0);
445 if (err)
446 goto done;
447 m = meta[i];
449 if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
450 m->obj_type == GOT_OBJ_TYPE_TAG)
451 continue;
453 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
454 if (err)
455 goto done;
456 m->size = raw->size;
458 if (raw->f == NULL) {
459 err = got_deltify_init_mem(&m->dtab, raw->data,
460 raw->hdrlen, raw->size + raw->hdrlen);
461 } else {
462 err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
463 raw->size + raw->hdrlen);
465 if (err)
466 goto done;
468 if (i > max_base_candidates) {
469 struct got_pack_meta *n = NULL;
470 n = meta[i - (max_base_candidates + 1)];
471 got_deltify_free(n->dtab);
472 n->dtab = NULL;
475 best_size = raw->size;
476 best_ndeltas = 0;
477 for (j = MAX(0, i - max_base_candidates); j < i; j++) {
478 if (cancel_cb) {
479 err = (*cancel_cb)(cancel_arg);
480 if (err)
481 goto done;
483 base = meta[j];
484 /* long chains make unpacking slow, avoid such bases */
485 if (base->nchain >= 128 ||
486 base->obj_type != m->obj_type)
487 continue;
489 err = got_object_raw_open(&base_raw, &outfd, repo,
490 &base->id);
491 if (err)
492 goto done;
493 if (raw->f == NULL && base_raw->f == NULL) {
494 err = got_deltify_mem_mem(&deltas, &ndeltas,
495 raw->data, raw->hdrlen,
496 raw->size + raw->hdrlen,
497 base->dtab, base_raw->data,
498 base_raw->hdrlen,
499 base_raw->size + base_raw->hdrlen);
500 } else if (raw->f == NULL) {
501 err = got_deltify_mem_file(&deltas, &ndeltas,
502 raw->data, raw->hdrlen,
503 raw->size + raw->hdrlen,
504 base->dtab, base_raw->f,
505 base_raw->hdrlen,
506 base_raw->size + base_raw->hdrlen);
507 } else if (base_raw->f == NULL) {
508 err = got_deltify_file_mem(&deltas, &ndeltas,
509 raw->f, raw->hdrlen,
510 raw->size + raw->hdrlen,
511 base->dtab, base_raw->data,
512 base_raw->hdrlen,
513 base_raw->size + base_raw->hdrlen);
514 } else {
515 err = got_deltify(&deltas, &ndeltas,
516 raw->f, raw->hdrlen,
517 raw->size + raw->hdrlen,
518 base->dtab, base_raw->f, base_raw->hdrlen,
519 base_raw->size + base_raw->hdrlen);
521 got_object_raw_close(base_raw);
522 base_raw = NULL;
523 if (err)
524 goto done;
526 size = delta_size(deltas, ndeltas);
527 if (size + 32 < best_size){
528 /*
529 * if we already picked a best delta,
530 * replace it.
531 */
532 best_size = size;
533 free(best_deltas);
534 best_deltas = deltas;
535 best_ndeltas = ndeltas;
536 deltas = NULL;
537 m->nchain = base->nchain + 1;
538 m->prev = base;
539 m->head = base->head;
540 if (m->head == NULL)
541 m->head = base;
542 } else {
543 free(deltas);
544 deltas = NULL;
545 ndeltas = 0;
549 if (best_ndeltas > 0) {
550 if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
551 err = encode_delta_in_mem(m, raw, best_deltas,
552 best_ndeltas, best_size, m->prev->size);
553 } else {
554 m->delta_offset = ftello(delta_cache);
555 err = encode_delta(m, raw, best_deltas,
556 best_ndeltas, m->prev->size, delta_cache);
558 free(best_deltas);
559 best_deltas = NULL;
560 best_ndeltas = 0;
561 if (err)
562 goto done;
565 got_object_raw_close(raw);
566 raw = NULL;
568 done:
569 for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
570 got_deltify_free(meta[i]->dtab);
571 meta[i]->dtab = NULL;
573 if (raw)
574 got_object_raw_close(raw);
575 if (base_raw)
576 got_object_raw_close(base_raw);
577 if (outfd != -1 && close(outfd) == -1 && err == NULL)
578 err = got_error_from_errno("close");
579 free(deltas);
580 free(best_deltas);
581 return err;
584 static const struct got_error *
585 search_packidx(int *found, struct got_object_id *id,
586 struct got_repository *repo)
588 const struct got_error *err = NULL;
589 struct got_packidx *packidx = NULL;
590 int idx;
592 *found = 0;
594 err = got_repo_search_packidx(&packidx, &idx, repo, id);
595 if (err == NULL)
596 *found = 1; /* object is already packed */
597 else if (err->code == GOT_ERR_NO_OBJ)
598 err = NULL;
599 return err;
602 static const int obj_types[] = {
603 GOT_OBJ_TYPE_ANY,
604 GOT_OBJ_TYPE_COMMIT,
605 GOT_OBJ_TYPE_TREE,
606 GOT_OBJ_TYPE_BLOB,
607 GOT_OBJ_TYPE_TAG,
608 GOT_OBJ_TYPE_OFFSET_DELTA,
609 GOT_OBJ_TYPE_REF_DELTA
610 };
612 static const struct got_error *
613 add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
614 struct got_object_id *id, const char *path, int obj_type,
615 time_t mtime, int loose_obj_only, struct got_repository *repo)
617 const struct got_error *err;
618 struct got_pack_meta *m;
620 if (loose_obj_only) {
621 int is_packed;
622 err = search_packidx(&is_packed, id, repo);
623 if (err)
624 return err;
625 if (is_packed)
626 return NULL;
629 err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
630 if (err)
631 return err;
633 if (v == NULL)
634 return NULL;
636 err = alloc_meta(&m, id, path, obj_type, mtime);
637 if (err)
638 goto done;
640 if (v->nmeta == v->metasz){
641 size_t newsize = 2 * v->metasz;
642 struct got_pack_meta **new;
643 new = reallocarray(v->meta, newsize, sizeof(*new));
644 if (new == NULL) {
645 err = got_error_from_errno("reallocarray");
646 goto done;
648 v->meta = new;
649 v->metasz = newsize;
651 done:
652 if (err) {
653 clear_meta(m);
654 free(m);
655 } else
656 v->meta[v->nmeta++] = m;
658 return err;
661 static const struct got_error *
662 load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
663 struct got_object_idset *idset, struct got_object_id *tree_id,
664 const char *dpath, time_t mtime, struct got_repository *repo,
665 int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
667 const struct got_error *err;
668 struct got_tree_object *tree;
669 char *p = NULL;
670 int i;
672 err = got_object_open_as_tree(&tree, repo, tree_id);
673 if (err)
674 return err;
676 for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
677 struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
678 struct got_object_id *id = got_tree_entry_get_id(e);
679 mode_t mode = got_tree_entry_get_mode(e);
681 if (cancel_cb) {
682 err = (*cancel_cb)(cancel_arg);
683 if (err)
684 break;
687 if (got_object_tree_entry_is_submodule(e) ||
688 got_object_idset_contains(idset, id))
689 continue;
691 if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
692 got_tree_entry_get_name(e)) == -1) {
693 err = got_error_from_errno("asprintf");
694 break;
697 if (S_ISDIR(mode)) {
698 struct got_object_qid *qid;
699 err = got_object_qid_alloc(&qid, id);
700 if (err)
701 break;
702 STAILQ_INSERT_TAIL(ids, qid, entry);
703 } else if (S_ISREG(mode) || S_ISLNK(mode)) {
704 err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
705 mtime, loose_obj_only, repo);
706 if (err)
707 break;
709 free(p);
710 p = NULL;
713 got_object_tree_close(tree);
714 free(p);
715 return err;
718 static const struct got_error *
719 load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
720 struct got_object_id *tree_id, const char *dpath, time_t mtime,
721 int loose_obj_only, struct got_repository *repo,
722 got_cancel_cb cancel_cb, void *cancel_arg)
724 const struct got_error *err = NULL;
725 struct got_object_id_queue tree_ids;
726 struct got_object_qid *qid;
728 if (got_object_idset_contains(idset, tree_id))
729 return NULL;
731 err = got_object_qid_alloc(&qid, tree_id);
732 if (err)
733 return err;
735 STAILQ_INIT(&tree_ids);
736 STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
738 while (!STAILQ_EMPTY(&tree_ids)) {
739 if (cancel_cb) {
740 err = (*cancel_cb)(cancel_arg);
741 if (err)
742 break;
745 qid = STAILQ_FIRST(&tree_ids);
746 STAILQ_REMOVE_HEAD(&tree_ids, entry);
748 if (got_object_idset_contains(idset, qid->id)) {
749 got_object_qid_free(qid);
750 continue;
753 err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
754 mtime, loose_obj_only, repo);
755 if (err) {
756 got_object_qid_free(qid);
757 break;
760 err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
761 mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
762 got_object_qid_free(qid);
763 if (err)
764 break;
767 got_object_id_queue_free(&tree_ids);
768 return err;
771 static const struct got_error *
772 load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
773 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
774 got_cancel_cb cancel_cb, void *cancel_arg)
776 const struct got_error *err;
777 struct got_commit_object *commit;
779 if (got_object_idset_contains(idset, id))
780 return NULL;
782 if (loose_obj_only) {
783 int is_packed;
784 err = search_packidx(&is_packed, id, repo);
785 if (err)
786 return err;
787 if (is_packed)
788 return NULL;
791 err = got_object_open_as_commit(&commit, repo, id);
792 if (err)
793 return err;
795 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
796 got_object_commit_get_committer_time(commit),
797 loose_obj_only, repo);
798 if (err)
799 goto done;
801 err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
802 "", got_object_commit_get_committer_time(commit),
803 loose_obj_only, repo, cancel_cb, cancel_arg);
804 done:
805 got_object_commit_close(commit);
806 return err;
809 static const struct got_error *
810 load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
811 struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
812 got_cancel_cb cancel_cb, void *cancel_arg)
814 const struct got_error *err;
815 struct got_tag_object *tag = NULL;
817 if (got_object_idset_contains(idset, id))
818 return NULL;
820 if (loose_obj_only) {
821 int is_packed;
822 err = search_packidx(&is_packed, id, repo);
823 if (err)
824 return err;
825 if (is_packed)
826 return NULL;
829 err = got_object_open_as_tag(&tag, repo, id);
830 if (err)
831 return err;
833 err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
834 got_object_tag_get_tagger_time(tag),
835 loose_obj_only, repo);
836 if (err)
837 goto done;
839 switch (got_object_tag_get_object_type(tag)) {
840 case GOT_OBJ_TYPE_COMMIT:
841 err = load_commit(v, idset,
842 got_object_tag_get_object_id(tag), repo,
843 loose_obj_only, cancel_cb, cancel_arg);
844 break;
845 case GOT_OBJ_TYPE_TREE:
846 err = load_tree(v, idset, got_object_tag_get_object_id(tag),
847 "", got_object_tag_get_tagger_time(tag),
848 loose_obj_only, repo, cancel_cb, cancel_arg);
849 break;
850 default:
851 break;
854 done:
855 got_object_tag_close(tag);
856 return err;
859 enum findtwixt_color {
860 COLOR_KEEP = 0,
861 COLOR_DROP,
862 COLOR_BLANK,
863 };
864 static const int findtwixt_colors[] = {
865 COLOR_KEEP,
866 COLOR_DROP,
867 COLOR_BLANK
868 };
870 static const struct got_error *
871 queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
872 int color, struct got_repository *repo)
874 const struct got_error *err;
875 struct got_object_qid *qid;
877 err = got_object_qid_alloc(&qid, id);
878 if (err)
879 return err;
881 STAILQ_INSERT_TAIL(ids, qid, entry);
882 qid->data = (void *)&findtwixt_colors[color];
883 return NULL;
886 static const struct got_error *
887 drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
888 struct got_object_id *id, struct got_repository *repo,
889 got_cancel_cb cancel_cb, void *cancel_arg)
891 const struct got_error *err = NULL;
892 struct got_commit_object *commit;
893 const struct got_object_id_queue *parents;
894 struct got_object_id_queue ids;
895 struct got_object_qid *qid;
897 STAILQ_INIT(&ids);
899 err = got_object_qid_alloc(&qid, id);
900 if (err)
901 return err;
902 STAILQ_INSERT_HEAD(&ids, qid, entry);
904 while (!STAILQ_EMPTY(&ids)) {
905 if (cancel_cb) {
906 err = (*cancel_cb)(cancel_arg);
907 if (err)
908 break;
911 qid = STAILQ_FIRST(&ids);
912 STAILQ_REMOVE_HEAD(&ids, entry);
914 if (got_object_idset_contains(drop, qid->id)) {
915 got_object_qid_free(qid);
916 continue;
919 err = got_object_idset_add(drop, qid->id,
920 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
921 if (err) {
922 got_object_qid_free(qid);
923 break;
926 if (!got_object_idset_contains(keep, qid->id)) {
927 got_object_qid_free(qid);
928 continue;
931 err = got_object_open_as_commit(&commit, repo, qid->id);
932 got_object_qid_free(qid);
933 if (err)
934 break;
936 parents = got_object_commit_get_parent_ids(commit);
937 if (parents) {
938 err = got_object_id_queue_copy(parents, &ids);
939 if (err) {
940 got_object_commit_close(commit);
941 break;
944 got_object_commit_close(commit);
947 got_object_id_queue_free(&ids);
948 return err;
951 struct append_id_arg {
952 struct got_object_id **array;
953 int idx;
954 };
956 static const struct got_error *
957 append_id(struct got_object_id *id, void *data, void *arg)
959 struct append_id_arg *a = arg;
961 a->array[a->idx] = got_object_id_dup(id);
962 if (a->array[a->idx] == NULL)
963 return got_error_from_errno("got_object_id_dup");
965 a->idx++;
966 return NULL;
969 static const struct got_error *
970 findtwixt(struct got_object_id ***res, int *nres,
971 struct got_object_id **head, int nhead,
972 struct got_object_id **tail, int ntail,
973 struct got_repository *repo,
974 got_cancel_cb cancel_cb, void *cancel_arg)
976 const struct got_error *err = NULL;
977 struct got_object_id_queue ids;
978 struct got_object_idset *keep, *drop;
979 struct got_object_qid *qid;
980 int i, ncolor, nkeep, obj_type;
982 STAILQ_INIT(&ids);
983 *res = NULL;
984 *nres = 0;
986 keep = got_object_idset_alloc();
987 if (keep == NULL)
988 return got_error_from_errno("got_object_idset_alloc");
990 drop = got_object_idset_alloc();
991 if (drop == NULL) {
992 err = got_error_from_errno("got_object_idset_alloc");
993 goto done;
996 for (i = 0; i < nhead; i++) {
997 struct got_object_id *id = head[i];
998 if (id == NULL)
999 continue;
1000 err = got_object_get_type(&obj_type, repo, id);
1001 if (err)
1002 return err;
1003 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1004 continue;
1005 err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1006 if (err)
1007 goto done;
1009 for (i = 0; i < ntail; i++) {
1010 struct got_object_id *id = tail[i];
1011 if (id == NULL)
1012 continue;
1013 err = got_object_get_type(&obj_type, repo, id);
1014 if (err)
1015 return err;
1016 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1017 continue;
1018 err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1019 if (err)
1020 goto done;
1023 while (!STAILQ_EMPTY(&ids)) {
1024 int qcolor;
1025 qid = STAILQ_FIRST(&ids);
1026 qcolor = *((int *)qid->data);
1028 if (got_object_idset_contains(drop, qid->id))
1029 ncolor = COLOR_DROP;
1030 else if (got_object_idset_contains(keep, qid->id))
1031 ncolor = COLOR_KEEP;
1032 else
1033 ncolor = COLOR_BLANK;
1035 if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1036 qcolor == COLOR_KEEP)) {
1037 STAILQ_REMOVE_HEAD(&ids, entry);
1038 got_object_qid_free(qid);
1039 continue;
1042 if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1043 err = drop_commit(keep, drop, qid->id, repo,
1044 cancel_cb, cancel_arg);
1045 if (err)
1046 goto done;
1047 } else if (ncolor == COLOR_BLANK) {
1048 struct got_commit_object *commit;
1049 struct got_object_id *id;
1050 const struct got_object_id_queue *parents;
1051 struct got_object_qid *pid;
1053 id = got_object_id_dup(qid->id);
1054 if (id == NULL) {
1055 err = got_error_from_errno("got_object_id_dup");
1056 goto done;
1058 if (qcolor == COLOR_KEEP)
1059 err = got_object_idset_add(keep, id,
1060 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1061 else
1062 err = got_object_idset_add(drop, id,
1063 (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1064 if (err) {
1065 free(id);
1066 goto done;
1069 err = got_object_open_as_commit(&commit, repo, id);
1070 if (err) {
1071 free(id);
1072 goto done;
1074 parents = got_object_commit_get_parent_ids(commit);
1075 if (parents) {
1076 STAILQ_FOREACH(pid, parents, entry) {
1077 err = queue_commit_id(&ids, pid->id,
1078 qcolor, repo);
1079 if (err) {
1080 free(id);
1081 goto done;
1085 got_object_commit_close(commit);
1086 commit = NULL;
1087 } else {
1088 /* should not happen */
1089 err = got_error_fmt(GOT_ERR_NOT_IMPL,
1090 "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1091 goto done;
1094 STAILQ_REMOVE_HEAD(&ids, entry);
1095 got_object_qid_free(qid);
1098 nkeep = got_object_idset_num_elements(keep);
1099 if (nkeep > 0) {
1100 struct append_id_arg arg;
1101 arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1102 if (arg.array == NULL) {
1103 err = got_error_from_errno("calloc");
1104 goto done;
1106 arg.idx = 0;
1107 err = got_object_idset_for_each(keep, append_id, &arg);
1108 if (err) {
1109 free(arg.array);
1110 goto done;
1112 *res = arg.array;
1113 *nres = nkeep;
1115 done:
1116 got_object_idset_free(keep);
1117 got_object_idset_free(drop);
1118 got_object_id_queue_free(&ids);
1119 return err;
1122 static const struct got_error *
1123 read_meta(struct got_pack_meta ***meta, int *nmeta,
1124 struct got_object_id **theirs, int ntheirs,
1125 struct got_object_id **ours, int nours, struct got_repository *repo,
1126 int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1127 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1129 const struct got_error *err = NULL;
1130 struct got_object_id **ids = NULL;
1131 struct got_object_idset *idset;
1132 int i, nobj = 0, obj_type;
1133 struct got_pack_metavec v;
1135 *meta = NULL;
1136 *nmeta = 0;
1138 idset = got_object_idset_alloc();
1139 if (idset == NULL)
1140 return got_error_from_errno("got_object_idset_alloc");
1142 v.nmeta = 0;
1143 v.metasz = 64;
1144 v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
1145 if (v.meta == NULL) {
1146 err = got_error_from_errno("calloc");
1147 goto done;
1150 err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1151 cancel_cb, cancel_arg);
1152 if (err || nobj == 0)
1153 goto done;
1155 for (i = 0; i < ntheirs; i++) {
1156 struct got_object_id *id = theirs[i];
1157 if (id == NULL)
1158 continue;
1159 err = got_object_get_type(&obj_type, repo, id);
1160 if (err)
1161 return err;
1162 if (obj_type != GOT_OBJ_TYPE_COMMIT)
1163 continue;
1164 err = load_commit(NULL, idset, id, repo,
1165 loose_obj_only, cancel_cb, cancel_arg);
1166 if (err)
1167 goto done;
1168 err = report_progress(progress_cb, progress_arg, rl,
1169 0L, nours, v.nmeta, 0, 0);
1170 if (err)
1171 goto done;
1174 for (i = 0; i < ntheirs; i++) {
1175 struct got_object_id *id = theirs[i];
1176 int *cached_type;
1177 if (id == NULL)
1178 continue;
1179 cached_type = got_object_idset_get(idset, id);
1180 if (cached_type == NULL) {
1181 err = got_object_get_type(&obj_type, repo, id);
1182 if (err)
1183 goto done;
1184 } else
1185 obj_type = *cached_type;
1186 if (obj_type != GOT_OBJ_TYPE_TAG)
1187 continue;
1188 err = load_tag(NULL, idset, id, repo,
1189 loose_obj_only, cancel_cb, cancel_arg);
1190 if (err)
1191 goto done;
1192 err = report_progress(progress_cb, progress_arg, rl,
1193 0L, nours, v.nmeta, 0, 0);
1194 if (err)
1195 goto done;
1198 for (i = 0; i < nobj; i++) {
1199 err = load_commit(&v, idset, ids[i], repo,
1200 loose_obj_only, cancel_cb, cancel_arg);
1201 if (err)
1202 goto done;
1203 if (err)
1204 goto done;
1205 err = report_progress(progress_cb, progress_arg, rl,
1206 0L, nours, v.nmeta, 0, 0);
1207 if (err)
1208 goto done;
1211 for (i = 0; i < nours; i++) {
1212 struct got_object_id *id = ours[i];
1213 int *cached_type;
1214 if (id == NULL)
1215 continue;
1216 cached_type = got_object_idset_get(idset, id);
1217 if (cached_type == NULL) {
1218 err = got_object_get_type(&obj_type, repo, id);
1219 if (err)
1220 goto done;
1221 } else
1222 obj_type = *cached_type;
1223 if (obj_type != GOT_OBJ_TYPE_TAG)
1224 continue;
1225 err = load_tag(&v, idset, id, repo,
1226 loose_obj_only, cancel_cb, cancel_arg);
1227 if (err)
1228 goto done;
1229 err = report_progress(progress_cb, progress_arg, rl,
1230 0L, nours, v.nmeta, 0, 0);
1231 if (err)
1232 goto done;
1235 if (progress_cb) {
1236 err = progress_cb(progress_arg, 0L, nours, v.nmeta, 0, 0);
1237 if (err)
1238 goto done;
1240 done:
1241 for (i = 0; i < nobj; i++) {
1242 free(ids[i]);
1244 free(ids);
1245 got_object_idset_free(idset);
1246 if (err == NULL) {
1247 *meta = v.meta;
1248 *nmeta = v.nmeta;
1249 } else
1250 free(v.meta);
1252 return err;
1255 const struct got_error *
1256 hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1258 size_t n;
1260 SHA1Update(ctx, buf, len);
1261 n = fwrite(buf, 1, len, f);
1262 if (n != len)
1263 return got_ferror(f, GOT_ERR_IO);
1264 return NULL;
1267 static void
1268 putbe32(char *b, uint32_t n)
1270 b[0] = n >> 24;
1271 b[1] = n >> 16;
1272 b[2] = n >> 8;
1273 b[3] = n >> 0;
1276 static int
1277 write_order_cmp(const void *pa, const void *pb)
1279 struct got_pack_meta *a, *b, *ahd, *bhd;
1281 a = *(struct got_pack_meta **)pa;
1282 b = *(struct got_pack_meta **)pb;
1283 ahd = (a->head == NULL) ? a : a->head;
1284 bhd = (b->head == NULL) ? b : b->head;
1285 if (ahd->mtime != bhd->mtime)
1286 return bhd->mtime - ahd->mtime;
1287 if (ahd != bhd)
1288 return (uintptr_t)bhd - (uintptr_t)ahd;
1289 if (a->nchain != b->nchain)
1290 return a->nchain - b->nchain;
1291 return a->mtime - b->mtime;
1294 static const struct got_error *
1295 packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1297 size_t i;
1299 *hdrlen = 0;
1301 hdr[0] = obj_type << 4;
1302 hdr[0] |= len & 0xf;
1303 len >>= 4;
1304 for (i = 1; len != 0; i++){
1305 if (i >= bufsize)
1306 return got_error(GOT_ERR_NO_SPACE);
1307 hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1308 hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1309 len >>= GOT_DELTA_SIZE_SHIFT;
1312 *hdrlen = i;
1313 return NULL;
1316 static int
1317 packoff(char *hdr, off_t off)
1319 int i, j;
1320 char rbuf[8];
1322 rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1323 for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1324 rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1325 GOT_DELTA_SIZE_MORE;
1328 j = 0;
1329 while (i > 0)
1330 hdr[j++] = rbuf[--i];
1331 return j;
1334 static const struct got_error *
1335 deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1336 struct got_pack_meta *m, int use_offset_deltas)
1338 const struct got_error *err;
1339 char buf[32];
1340 int nh;
1342 if (use_offset_deltas && m->prev->off != 0) {
1343 err = packhdr(&nh, buf, sizeof(buf),
1344 GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1345 if (err)
1346 return err;
1347 nh += packoff(buf + nh, m->off - m->prev->off);
1348 err = hwrite(packfile, buf, nh, ctx);
1349 if (err)
1350 return err;
1351 *packfile_size += nh;
1352 } else {
1353 err = packhdr(&nh, buf, sizeof(buf),
1354 GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1355 if (err)
1356 return err;
1357 err = hwrite(packfile, buf, nh, ctx);
1358 if (err)
1359 return err;
1360 *packfile_size += nh;
1361 err = hwrite(packfile, m->prev->id.sha1,
1362 sizeof(m->prev->id.sha1), ctx);
1363 if (err)
1364 return err;
1365 *packfile_size += sizeof(m->prev->id.sha1);
1368 return NULL;
1371 static const struct got_error *
1372 genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1373 struct got_pack_meta **meta, int nmeta, int nours,
1374 int use_offset_deltas, struct got_repository *repo,
1375 got_pack_progress_cb progress_cb, void *progress_arg,
1376 struct got_ratelimit *rl,
1377 got_cancel_cb cancel_cb, void *cancel_arg)
1379 const struct got_error *err = NULL;
1380 int i, nh;
1381 SHA1_CTX ctx;
1382 struct got_pack_meta *m;
1383 struct got_raw_object *raw = NULL;
1384 FILE *delta_file = NULL;
1385 char buf[32];
1386 size_t outlen, n;
1387 struct got_deflate_checksum csum;
1388 off_t packfile_size = 0;
1389 int outfd = -1;
1391 SHA1Init(&ctx);
1392 csum.output_sha1 = &ctx;
1393 csum.output_crc = NULL;
1395 err = hwrite(packfile, "PACK", 4, &ctx);
1396 if (err)
1397 return err;
1398 putbe32(buf, GOT_PACKFILE_VERSION);
1399 err = hwrite(packfile, buf, 4, &ctx);
1400 if (err)
1401 goto done;
1402 putbe32(buf, nmeta);
1403 err = hwrite(packfile, buf, 4, &ctx);
1404 if (err)
1405 goto done;
1406 qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1407 for (i = 0; i < nmeta; i++) {
1408 err = report_progress(progress_cb, progress_arg, rl,
1409 packfile_size, nours, nmeta, nmeta, i);
1410 if (err)
1411 goto done;
1412 m = meta[i];
1413 m->off = ftello(packfile);
1414 if (m->delta_len == 0) {
1415 err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1416 if (err)
1417 goto done;
1418 err = packhdr(&nh, buf, sizeof(buf),
1419 m->obj_type, raw->size);
1420 if (err)
1421 goto done;
1422 err = hwrite(packfile, buf, nh, &ctx);
1423 if (err)
1424 goto done;
1425 packfile_size += nh;
1426 if (raw->f == NULL) {
1427 err = got_deflate_to_file_mmap(&outlen,
1428 raw->data + raw->hdrlen, 0, raw->size,
1429 packfile, &csum);
1430 if (err)
1431 goto done;
1432 } else {
1433 if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1434 == -1) {
1435 err = got_error_from_errno("fseeko");
1436 goto done;
1438 err = got_deflate_to_file(&outlen, raw->f,
1439 packfile, &csum);
1440 if (err)
1441 goto done;
1443 packfile_size += outlen;
1444 got_object_raw_close(raw);
1445 raw = NULL;
1446 } else if (m->delta_buf) {
1447 err = deltahdr(&packfile_size, &ctx, packfile,
1448 m, use_offset_deltas);
1449 if (err)
1450 goto done;
1451 err = got_deflate_to_file_mmap(&outlen,
1452 m->delta_buf, 0, m->delta_len, packfile, &csum);
1453 if (err)
1454 goto done;
1455 packfile_size += outlen;
1456 free(m->delta_buf);
1457 m->delta_buf = NULL;
1458 } else {
1459 off_t remain;
1460 if (delta_file == NULL) {
1461 delta_file = got_opentemp();
1462 if (delta_file == NULL) {
1463 err = got_error_from_errno(
1464 "got_opentemp");
1465 goto done;
1468 if (ftruncate(fileno(delta_file), 0L) == -1) {
1469 err = got_error_from_errno("ftruncate");
1470 goto done;
1472 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1473 err = got_error_from_errno("fseeko");
1474 goto done;
1476 if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1477 == -1) {
1478 err = got_error_from_errno("fseeko");
1479 goto done;
1481 remain = m->delta_len;
1482 while (remain > 0) {
1483 char delta_buf[8192];
1484 size_t r, w, n;
1485 n = MIN(remain, sizeof(delta_buf));
1486 r = fread(delta_buf, 1, n, delta_cache);
1487 if (r != n) {
1488 err = got_ferror(delta_cache,
1489 GOT_ERR_IO);
1490 goto done;
1492 w = fwrite(delta_buf, 1, n, delta_file);
1493 if (w != n) {
1494 err = got_ferror(delta_file,
1495 GOT_ERR_IO);
1496 goto done;
1498 remain -= n;
1500 err = deltahdr(&packfile_size, &ctx, packfile,
1501 m, use_offset_deltas);
1502 if (err)
1503 goto done;
1504 if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1505 err = got_error_from_errno("fseeko");
1506 goto done;
1508 err = got_deflate_to_file(&outlen, delta_file,
1509 packfile, &csum);
1510 if (err)
1511 goto done;
1512 packfile_size += outlen;
1515 SHA1Final(pack_sha1, &ctx);
1516 n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1517 if (n != SHA1_DIGEST_LENGTH)
1518 err = got_ferror(packfile, GOT_ERR_IO);
1519 packfile_size += SHA1_DIGEST_LENGTH;
1520 packfile_size += sizeof(struct got_packfile_hdr);
1521 if (progress_cb) {
1522 err = progress_cb(progress_arg, packfile_size, nours,
1523 nmeta, nmeta, nmeta);
1524 if (err)
1525 goto done;
1527 done:
1528 if (delta_file && fclose(delta_file) == EOF && err == NULL)
1529 err = got_error_from_errno("fclose");
1530 if (raw)
1531 got_object_raw_close(raw);
1532 if (outfd != -1 && close(outfd) == -1 && err == NULL)
1533 err = got_error_from_errno("close");
1534 return err;
1537 const struct got_error *
1538 got_pack_create(uint8_t *packsha1, FILE *packfile,
1539 struct got_object_id **theirs, int ntheirs,
1540 struct got_object_id **ours, int nours,
1541 struct got_repository *repo, int loose_obj_only, int allow_empty,
1542 got_pack_progress_cb progress_cb, void *progress_arg,
1543 got_cancel_cb cancel_cb, void *cancel_arg)
1545 const struct got_error *err;
1546 struct got_pack_meta **meta;
1547 int nmeta;
1548 FILE *delta_cache = NULL;
1549 struct got_ratelimit rl;
1551 got_ratelimit_init(&rl, 0, 500);
1553 err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1554 loose_obj_only, progress_cb, progress_arg, &rl,
1555 cancel_cb, cancel_arg);
1556 if (err)
1557 return err;
1559 if (nmeta == 0 && !allow_empty) {
1560 err = got_error(GOT_ERR_CANNOT_PACK);
1561 goto done;
1564 delta_cache = got_opentemp();
1565 if (delta_cache == NULL) {
1566 err = got_error_from_errno("got_opentemp");
1567 goto done;
1570 if (nmeta > 0) {
1571 err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1572 progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1573 if (err)
1574 goto done;
1575 if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1576 err = got_error_from_errno("fseeko");
1577 goto done;
1581 err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1582 repo, progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1583 if (err)
1584 goto done;
1585 done:
1586 free_nmeta(meta, nmeta);
1587 if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1588 err = got_error_from_errno("fclose");
1589 return err;