2 e6bcace5 2021-06-22 stsp * Copyright (c) 2020 Ori Bernstein
3 e6bcace5 2021-06-22 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 e6bcace5 2021-06-22 stsp * Permission to use, copy, modify, and distribute this software for any
6 e6bcace5 2021-06-22 stsp * purpose with or without fee is hereby granted, provided that the above
7 e6bcace5 2021-06-22 stsp * copyright notice and this permission notice appear in all copies.
9 e6bcace5 2021-06-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 e6bcace5 2021-06-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 e6bcace5 2021-06-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 e6bcace5 2021-06-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 e6bcace5 2021-06-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 e6bcace5 2021-06-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 e6bcace5 2021-06-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 08736cf9 2021-06-23 stsp #include <sys/types.h>
19 08736cf9 2021-06-23 stsp #include <sys/uio.h>
20 e6bcace5 2021-06-22 stsp #include <sys/stat.h>
21 31ba2236 2022-01-05 thomas #include <sys/time.h>
22 d93dda9a 2022-05-12 thomas #include <sys/mman.h>
24 d93dda9a 2022-05-12 thomas #include <errno.h>
25 08736cf9 2021-06-23 stsp #include <stdint.h>
26 e6bcace5 2021-06-22 stsp #include <stdio.h>
27 e6bcace5 2021-06-22 stsp #include <stdlib.h>
28 e6bcace5 2021-06-22 stsp #include <string.h>
29 31ba2236 2022-01-05 thomas #include <time.h>
30 db847d2c 2022-03-22 thomas #include <unistd.h>
31 e6bcace5 2021-06-22 stsp #include <limits.h>
32 e6bcace5 2021-06-22 stsp #include <zlib.h>
34 b347007e 2021-10-15 thomas #if defined(__FreeBSD__)
35 b347007e 2021-10-15 thomas #include <unistd.h>
38 e6bcace5 2021-06-22 stsp #include "got_error.h"
39 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
40 e6bcace5 2021-06-22 stsp #include "got_object.h"
41 0be8fa4c 2021-10-15 thomas #include "got_path.h"
42 05118f5a 2021-06-22 stsp #include "got_reference.h"
43 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
44 e4d8ab47 2021-10-15 thomas #include "got_opentemp.h"
46 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
47 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
50 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
51 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
52 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
53 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
54 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
55 31ba2236 2022-01-05 thomas #include "got_lib_ratelimit.h"
56 9249e7e3 2022-05-12 thomas #include "got_lib_inflate.h"
58 d23a6c95 2022-05-31 thomas #include "murmurhash2.h"
60 b79280dd 2021-10-15 thomas #ifndef MIN
61 b79280dd 2021-10-15 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
65 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 5fe7b5c1 2022-04-16 thomas #ifndef nitems
69 5fe7b5c1 2022-04-16 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 e6bcace5 2021-06-22 stsp struct got_pack_meta {
73 e6bcace5 2021-06-22 stsp struct got_object_id id;
74 d23a6c95 2022-05-31 thomas uint32_t path_hash;
75 e6bcace5 2021-06-22 stsp int obj_type;
76 ab6186ae 2021-10-15 thomas off_t size;
77 e6bcace5 2021-06-22 stsp time_t mtime;
79 e6bcace5 2021-06-22 stsp /* The best delta we picked */
80 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
81 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
82 9249e7e3 2022-05-12 thomas unsigned char *delta_buf; /* if encoded in memory (compressed) */
83 9249e7e3 2022-05-12 thomas off_t delta_offset; /* offset in delta cache file (compressed) */
84 3b6ceab7 2022-01-10 thomas off_t delta_len; /* encoded delta length */
85 9249e7e3 2022-05-12 thomas off_t delta_compressed_len; /* encoded+compressed delta length */
88 f9c2e8e5 2022-02-13 thomas int have_reused_delta;
89 f9c2e8e5 2022-02-13 thomas off_t reused_delta_offset; /* offset of delta in reused pack file */
90 f9c2e8e5 2022-02-13 thomas struct got_object_id *base_obj_id;
92 e6bcace5 2021-06-22 stsp /* Only used for delta window */
93 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
95 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
99 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
100 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
102 e6bcace5 2021-06-22 stsp int metasz;
105 e6bcace5 2021-06-22 stsp static const struct got_error *
106 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
107 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
109 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
111 e6bcace5 2021-06-22 stsp *new = NULL;
113 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
114 e6bcace5 2021-06-22 stsp if (m == NULL)
115 b7e0a384 2021-10-15 thomas return got_error_from_errno("calloc");
117 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
119 d23a6c95 2022-05-31 thomas m->path_hash = murmurhash2(path, strlen(path), 0xd70af26a);
120 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
121 e6bcace5 2021-06-22 stsp m->mtime = mtime;
123 e6bcace5 2021-06-22 stsp return NULL;
126 e6bcace5 2021-06-22 stsp static void
127 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
129 e6bcace5 2021-06-22 stsp if (meta == NULL)
131 d23a6c95 2022-05-31 thomas meta->path_hash = 0;
132 3b6ceab7 2022-01-10 thomas free(meta->delta_buf);
133 3b6ceab7 2022-01-10 thomas meta->delta_buf = NULL;
134 f9c2e8e5 2022-02-13 thomas free(meta->base_obj_id);
135 f9c2e8e5 2022-02-13 thomas meta->base_obj_id = NULL;
138 e6bcace5 2021-06-22 stsp static void
139 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
143 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
144 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
145 e6bcace5 2021-06-22 stsp free(meta);
149 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
151 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
153 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
154 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
156 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
157 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
158 d23a6c95 2022-05-31 thomas if (a->path_hash < b->path_hash)
159 d23a6c95 2022-05-31 thomas return -1;
160 d23a6c95 2022-05-31 thomas if (a->path_hash > b->path_hash)
161 d23a6c95 2022-05-31 thomas return 1;
162 f6b43367 2022-05-01 thomas if (a->mtime < b->mtime)
163 f6b43367 2022-05-01 thomas return -1;
164 f6b43367 2022-05-01 thomas if (a->mtime > b->mtime)
165 f6b43367 2022-05-01 thomas return 1;
166 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
169 3b6ceab7 2022-01-10 thomas static off_t
170 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
173 3b6ceab7 2022-01-10 thomas off_t size = 32;
174 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
175 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
176 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
178 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
180 e6bcace5 2021-06-22 stsp return size;
183 3b6ceab7 2022-01-10 thomas static const struct got_error *
184 3b6ceab7 2022-01-10 thomas append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
188 3b6ceab7 2022-01-10 thomas if (*len + nseg >= *sz) {
189 3b6ceab7 2022-01-10 thomas while (*len + nseg >= *sz)
190 3b6ceab7 2022-01-10 thomas *sz += *sz / 2;
191 3b6ceab7 2022-01-10 thomas n = realloc(*p, *sz);
192 3b6ceab7 2022-01-10 thomas if (n == NULL)
193 3b6ceab7 2022-01-10 thomas return got_error_from_errno("realloc");
196 3b6ceab7 2022-01-10 thomas memcpy(*p + *len, seg, nseg);
197 3b6ceab7 2022-01-10 thomas *len += nseg;
198 3b6ceab7 2022-01-10 thomas return NULL;
201 3b6ceab7 2022-01-10 thomas static const struct got_error *
202 3b6ceab7 2022-01-10 thomas encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
203 3b6ceab7 2022-01-10 thomas struct got_delta_instruction *deltas, int ndeltas,
204 3b6ceab7 2022-01-10 thomas off_t delta_size, off_t base_size)
206 3b6ceab7 2022-01-10 thomas const struct got_error *err;
207 3b6ceab7 2022-01-10 thomas unsigned char buf[16], *bp;
208 3b6ceab7 2022-01-10 thomas int i, j;
209 9249e7e3 2022-05-12 thomas size_t len = 0, compressed_len;
210 9249e7e3 2022-05-12 thomas off_t bufsize = delta_size;
212 3b6ceab7 2022-01-10 thomas struct got_delta_instruction *d;
213 9249e7e3 2022-05-12 thomas uint8_t *delta_buf;
215 9249e7e3 2022-05-12 thomas delta_buf = malloc(bufsize);
216 9249e7e3 2022-05-12 thomas if (delta_buf == NULL)
217 9249e7e3 2022-05-12 thomas return got_error_from_errno("malloc");
219 3b6ceab7 2022-01-10 thomas /* base object size */
220 3b6ceab7 2022-01-10 thomas buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
221 3b6ceab7 2022-01-10 thomas n = base_size >> GOT_DELTA_SIZE_SHIFT;
222 3b6ceab7 2022-01-10 thomas for (i = 1; n > 0; i++) {
223 3b6ceab7 2022-01-10 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
224 3b6ceab7 2022-01-10 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
225 3b6ceab7 2022-01-10 thomas n >>= GOT_DELTA_SIZE_SHIFT;
227 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize, buf, i);
229 9249e7e3 2022-05-12 thomas goto done;
231 3b6ceab7 2022-01-10 thomas /* target object size */
232 3b6ceab7 2022-01-10 thomas buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
233 3b6ceab7 2022-01-10 thomas n = o->size >> GOT_DELTA_SIZE_SHIFT;
234 3b6ceab7 2022-01-10 thomas for (i = 1; n > 0; i++) {
235 3b6ceab7 2022-01-10 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
236 3b6ceab7 2022-01-10 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
237 3b6ceab7 2022-01-10 thomas n >>= GOT_DELTA_SIZE_SHIFT;
239 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize, buf, i);
241 9249e7e3 2022-05-12 thomas goto done;
243 3b6ceab7 2022-01-10 thomas for (j = 0; j < ndeltas; j++) {
244 3b6ceab7 2022-01-10 thomas d = &deltas[j];
245 3b6ceab7 2022-01-10 thomas if (d->copy) {
246 3b6ceab7 2022-01-10 thomas n = d->offset;
247 3b6ceab7 2022-01-10 thomas bp = &buf[1];
248 3b6ceab7 2022-01-10 thomas buf[0] = GOT_DELTA_BASE_COPY;
249 3b6ceab7 2022-01-10 thomas for (i = 0; i < 4; i++) {
250 3b6ceab7 2022-01-10 thomas /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
251 3b6ceab7 2022-01-10 thomas buf[0] |= 1 << i;
252 3b6ceab7 2022-01-10 thomas *bp++ = n & 0xff;
254 3b6ceab7 2022-01-10 thomas if (n == 0)
258 3b6ceab7 2022-01-10 thomas n = d->len;
259 3b6ceab7 2022-01-10 thomas if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
260 3b6ceab7 2022-01-10 thomas /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
261 3b6ceab7 2022-01-10 thomas for (i = 0; i < 3 && n > 0; i++) {
262 3b6ceab7 2022-01-10 thomas buf[0] |= 1 << (i + 4);
263 3b6ceab7 2022-01-10 thomas *bp++ = n & 0xff;
267 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize,
268 3b6ceab7 2022-01-10 thomas buf, bp - buf);
270 9249e7e3 2022-05-12 thomas goto done;
271 3b6ceab7 2022-01-10 thomas } else if (o->f == NULL) {
273 3b6ceab7 2022-01-10 thomas while (n != d->len) {
274 3b6ceab7 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
275 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize,
278 9249e7e3 2022-05-12 thomas goto done;
279 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize,
280 3b6ceab7 2022-01-10 thomas o->data + o->hdrlen + d->offset + n,
283 9249e7e3 2022-05-12 thomas goto done;
284 3b6ceab7 2022-01-10 thomas n += buf[0];
287 3b6ceab7 2022-01-10 thomas char content[128];
288 3b6ceab7 2022-01-10 thomas size_t r;
289 9249e7e3 2022-05-12 thomas if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
290 9249e7e3 2022-05-12 thomas err = got_error_from_errno("fseeko");
291 9249e7e3 2022-05-12 thomas goto done;
294 3b6ceab7 2022-01-10 thomas while (n != d->len) {
295 3b6ceab7 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
296 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize,
299 9249e7e3 2022-05-12 thomas goto done;
300 3b6ceab7 2022-01-10 thomas r = fread(content, 1, buf[0], o->f);
301 9249e7e3 2022-05-12 thomas if (r != buf[0]) {
302 9249e7e3 2022-05-12 thomas err = got_ferror(o->f, GOT_ERR_IO);
303 9249e7e3 2022-05-12 thomas goto done;
305 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize,
306 3b6ceab7 2022-01-10 thomas content, buf[0]);
308 9249e7e3 2022-05-12 thomas goto done;
309 3b6ceab7 2022-01-10 thomas n += buf[0];
314 9249e7e3 2022-05-12 thomas err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
315 9249e7e3 2022-05-12 thomas NULL, NULL, delta_buf, 0, len);
317 9249e7e3 2022-05-12 thomas goto done;
319 3b6ceab7 2022-01-10 thomas m->delta_len = len;
320 9249e7e3 2022-05-12 thomas m->delta_compressed_len = compressed_len;
322 9249e7e3 2022-05-12 thomas free(delta_buf);
323 9249e7e3 2022-05-12 thomas return err;
326 b79280dd 2021-10-15 thomas static const struct got_error *
327 b79280dd 2021-10-15 thomas encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
328 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas, int ndeltas,
329 d0772de9 2021-10-15 thomas off_t base_size, FILE *f)
331 9249e7e3 2022-05-12 thomas const struct got_error *err;
332 d0772de9 2021-10-15 thomas unsigned char buf[16], *bp;
333 d0772de9 2021-10-15 thomas int i, j;
335 9249e7e3 2022-05-12 thomas struct got_deflate_buf zb;
336 d0772de9 2021-10-15 thomas struct got_delta_instruction *d;
337 9249e7e3 2022-05-12 thomas off_t delta_len = 0, compressed_len = 0;
339 9249e7e3 2022-05-12 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
341 9249e7e3 2022-05-12 thomas return err;
343 d0772de9 2021-10-15 thomas /* base object size */
344 d0772de9 2021-10-15 thomas buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
345 d0772de9 2021-10-15 thomas n = base_size >> GOT_DELTA_SIZE_SHIFT;
346 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
347 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
348 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
349 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
352 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
353 9249e7e3 2022-05-12 thomas buf, 0, i, f, NULL);
355 9249e7e3 2022-05-12 thomas goto done;
356 9249e7e3 2022-05-12 thomas delta_len += i;
358 d0772de9 2021-10-15 thomas /* target object size */
359 d0772de9 2021-10-15 thomas buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
360 d0772de9 2021-10-15 thomas n = o->size >> GOT_DELTA_SIZE_SHIFT;
361 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
362 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
363 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
364 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
367 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
368 9249e7e3 2022-05-12 thomas buf, 0, i, f, NULL);
370 9249e7e3 2022-05-12 thomas goto done;
371 9249e7e3 2022-05-12 thomas delta_len += i;
373 d0772de9 2021-10-15 thomas for (j = 0; j < ndeltas; j++) {
374 d0772de9 2021-10-15 thomas d = &deltas[j];
375 d0772de9 2021-10-15 thomas if (d->copy) {
376 d0772de9 2021-10-15 thomas n = d->offset;
377 d0772de9 2021-10-15 thomas bp = &buf[1];
378 d0772de9 2021-10-15 thomas buf[0] = GOT_DELTA_BASE_COPY;
379 d0772de9 2021-10-15 thomas for (i = 0; i < 4; i++) {
380 d0772de9 2021-10-15 thomas /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
381 d0772de9 2021-10-15 thomas buf[0] |= 1 << i;
382 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
384 d0772de9 2021-10-15 thomas if (n == 0)
387 d0772de9 2021-10-15 thomas n = d->len;
388 d0772de9 2021-10-15 thomas if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
389 d0772de9 2021-10-15 thomas /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
390 d0772de9 2021-10-15 thomas for (i = 0; i < 3 && n > 0; i++) {
391 d0772de9 2021-10-15 thomas buf[0] |= 1 << (i + 4);
392 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
396 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb,
397 9249e7e3 2022-05-12 thomas &compressed_len, buf, 0, bp - buf, f, NULL);
399 9249e7e3 2022-05-12 thomas goto done;
400 9249e7e3 2022-05-12 thomas delta_len += (bp - buf);
401 2b0ae357 2022-01-10 thomas } else if (o->f == NULL) {
403 2b0ae357 2022-01-10 thomas while (n != d->len) {
404 2b0ae357 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
405 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb,
406 9249e7e3 2022-05-12 thomas &compressed_len, buf, 0, 1, f, NULL);
408 9249e7e3 2022-05-12 thomas goto done;
409 9249e7e3 2022-05-12 thomas delta_len++;
410 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb,
411 9249e7e3 2022-05-12 thomas &compressed_len,
412 9249e7e3 2022-05-12 thomas o->data + o->hdrlen + d->offset + n, 0,
413 9249e7e3 2022-05-12 thomas buf[0], f, NULL);
415 9249e7e3 2022-05-12 thomas goto done;
416 9249e7e3 2022-05-12 thomas delta_len += buf[0];
417 2b0ae357 2022-01-10 thomas n += buf[0];
420 d0772de9 2021-10-15 thomas char content[128];
421 d0772de9 2021-10-15 thomas size_t r;
422 9249e7e3 2022-05-12 thomas if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
423 9249e7e3 2022-05-12 thomas err = got_error_from_errno("fseeko");
424 9249e7e3 2022-05-12 thomas goto done;
427 d0772de9 2021-10-15 thomas while (n != d->len) {
428 d0772de9 2021-10-15 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
429 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb,
430 9249e7e3 2022-05-12 thomas &compressed_len, buf, 0, 1, f, NULL);
432 9249e7e3 2022-05-12 thomas goto done;
433 9249e7e3 2022-05-12 thomas delta_len++;
434 d0772de9 2021-10-15 thomas r = fread(content, 1, buf[0], o->f);
435 9249e7e3 2022-05-12 thomas if (r != buf[0]) {
436 9249e7e3 2022-05-12 thomas err = got_ferror(o->f, GOT_ERR_IO);
437 9249e7e3 2022-05-12 thomas goto done;
439 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb,
440 9249e7e3 2022-05-12 thomas &compressed_len, content, 0, buf[0], f,
443 9249e7e3 2022-05-12 thomas goto done;
444 9249e7e3 2022-05-12 thomas delta_len += buf[0];
445 d0772de9 2021-10-15 thomas n += buf[0];
450 9249e7e3 2022-05-12 thomas err = got_deflate_flush(&zb, f, NULL, &compressed_len);
452 9249e7e3 2022-05-12 thomas goto done;
454 9249e7e3 2022-05-12 thomas /* sanity check */
455 9249e7e3 2022-05-12 thomas if (compressed_len != ftello(f) - m->delta_offset) {
456 9249e7e3 2022-05-12 thomas err = got_error(GOT_ERR_COMPRESSION);
457 9249e7e3 2022-05-12 thomas goto done;
460 9249e7e3 2022-05-12 thomas m->delta_len = delta_len;
461 9249e7e3 2022-05-12 thomas m->delta_compressed_len = compressed_len;
463 9249e7e3 2022-05-12 thomas got_deflate_end(&zb);
464 9249e7e3 2022-05-12 thomas return err;
467 31ba2236 2022-01-05 thomas static const struct got_error *
468 31ba2236 2022-01-05 thomas report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
469 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
470 85220b0e 2022-03-22 thomas off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
471 85220b0e 2022-03-22 thomas int nobj_written)
473 31ba2236 2022-01-05 thomas const struct got_error *err;
474 31ba2236 2022-01-05 thomas int elapsed;
476 31ba2236 2022-01-05 thomas if (progress_cb == NULL)
477 31ba2236 2022-01-05 thomas return NULL;
479 31ba2236 2022-01-05 thomas err = got_ratelimit_check(&elapsed, rl);
480 31ba2236 2022-01-05 thomas if (err || !elapsed)
481 31ba2236 2022-01-05 thomas return err;
483 85220b0e 2022-03-22 thomas return progress_cb(progress_arg, ncolored, nfound, ntrees,
484 85220b0e 2022-03-22 thomas packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
487 e6bcace5 2021-06-22 stsp static const struct got_error *
488 f9c2e8e5 2022-02-13 thomas add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
490 f9c2e8e5 2022-02-13 thomas if (v->nmeta == v->metasz){
491 f9c2e8e5 2022-02-13 thomas size_t newsize = 2 * v->metasz;
492 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **new;
493 f9c2e8e5 2022-02-13 thomas new = reallocarray(v->meta, newsize, sizeof(*new));
494 f9c2e8e5 2022-02-13 thomas if (new == NULL)
495 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("reallocarray");
496 f9c2e8e5 2022-02-13 thomas v->meta = new;
497 f9c2e8e5 2022-02-13 thomas v->metasz = newsize;
500 f9c2e8e5 2022-02-13 thomas v->meta[v->nmeta++] = m;
501 f9c2e8e5 2022-02-13 thomas return NULL;
504 f9c2e8e5 2022-02-13 thomas static const struct got_error *
505 f9c2e8e5 2022-02-13 thomas find_pack_for_reuse(struct got_packidx **best_packidx,
506 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
508 de47d040 2022-03-22 thomas const struct got_error *err = NULL;
509 f9c2e8e5 2022-02-13 thomas struct got_pathlist_entry *pe;
510 f9c2e8e5 2022-02-13 thomas const char *best_packidx_path = NULL;
511 f9c2e8e5 2022-02-13 thomas int nobj_max = 0;
513 f9c2e8e5 2022-02-13 thomas *best_packidx = NULL;
515 de47d040 2022-03-22 thomas TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
516 f9c2e8e5 2022-02-13 thomas const char *path_packidx = pe->path;
517 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx;
518 f9c2e8e5 2022-02-13 thomas int nobj;
520 f9c2e8e5 2022-02-13 thomas err = got_repo_get_packidx(&packidx, path_packidx, repo);
524 f9c2e8e5 2022-02-13 thomas nobj = be32toh(packidx->hdr.fanout_table[0xff]);
525 f9c2e8e5 2022-02-13 thomas if (nobj > nobj_max) {
526 f9c2e8e5 2022-02-13 thomas best_packidx_path = path_packidx;
527 f9c2e8e5 2022-02-13 thomas nobj_max = nobj;
531 f9c2e8e5 2022-02-13 thomas if (best_packidx_path) {
532 f9c2e8e5 2022-02-13 thomas err = got_repo_get_packidx(best_packidx, best_packidx_path,
536 f9c2e8e5 2022-02-13 thomas return err;
539 7d0d4920 2022-05-12 thomas struct send_id_arg {
540 7d0d4920 2022-05-12 thomas struct imsgbuf *ibuf;
541 7d0d4920 2022-05-12 thomas struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
542 7d0d4920 2022-05-12 thomas size_t nids;
545 f9c2e8e5 2022-02-13 thomas static const struct got_error *
546 7d0d4920 2022-05-12 thomas send_id(struct got_object_id *id, void *data, void *arg)
548 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
549 7d0d4920 2022-05-12 thomas struct send_id_arg *a = arg;
551 7d0d4920 2022-05-12 thomas a->ids[a->nids++] = id;
553 7d0d4920 2022-05-12 thomas if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
554 7d0d4920 2022-05-12 thomas err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
556 f9c2e8e5 2022-02-13 thomas return err;
557 7d0d4920 2022-05-12 thomas a->nids = 0;
560 7d0d4920 2022-05-12 thomas return NULL;
563 7d0d4920 2022-05-12 thomas static const struct got_error *
564 7d0d4920 2022-05-12 thomas recv_reused_delta(struct got_imsg_reused_delta *delta,
565 7d0d4920 2022-05-12 thomas struct got_object_idset *idset, struct got_pack_metavec *v)
567 7d0d4920 2022-05-12 thomas struct got_pack_meta *m, *base;
569 7d0d4920 2022-05-12 thomas if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
570 7d0d4920 2022-05-12 thomas delta->delta_offset +
571 7d0d4920 2022-05-12 thomas delta->delta_compressed_size < delta->delta_offset)
572 7d0d4920 2022-05-12 thomas return got_error(GOT_ERR_BAD_PACKFILE);
574 7d0d4920 2022-05-12 thomas m = got_object_idset_get(idset, &delta->id);
575 7d0d4920 2022-05-12 thomas if (m == NULL)
576 7d0d4920 2022-05-12 thomas return got_error(GOT_ERR_NO_OBJ);
578 7d0d4920 2022-05-12 thomas base = got_object_idset_get(idset, &delta->base_id);
579 7d0d4920 2022-05-12 thomas if (base == NULL)
580 7d0d4920 2022-05-12 thomas return got_error(GOT_ERR_NO_OBJ);
582 7d0d4920 2022-05-12 thomas m->delta_len = delta->delta_size;
583 7d0d4920 2022-05-12 thomas m->delta_compressed_len = delta->delta_compressed_size;
584 7d0d4920 2022-05-12 thomas m->delta_offset = delta->delta_out_offset;
585 7d0d4920 2022-05-12 thomas m->prev = base;
586 7d0d4920 2022-05-12 thomas m->size = delta->result_size;
587 7d0d4920 2022-05-12 thomas m->have_reused_delta = 1;
588 7d0d4920 2022-05-12 thomas m->reused_delta_offset = delta->delta_offset;
589 7d0d4920 2022-05-12 thomas m->base_obj_id = got_object_id_dup(&delta->base_id);
590 7d0d4920 2022-05-12 thomas if (m->base_obj_id == NULL)
591 7d0d4920 2022-05-12 thomas return got_error_from_errno("got_object_id_dup");
593 7d0d4920 2022-05-12 thomas return add_meta(m, v);
596 f9c2e8e5 2022-02-13 thomas static const struct got_error *
597 f9c2e8e5 2022-02-13 thomas search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
598 85220b0e 2022-03-22 thomas int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
599 85220b0e 2022-03-22 thomas struct got_repository *repo,
600 f9c2e8e5 2022-02-13 thomas got_pack_progress_cb progress_cb, void *progress_arg,
601 f9c2e8e5 2022-02-13 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
603 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
604 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx;
605 f9c2e8e5 2022-02-13 thomas struct got_pack *pack;
606 7d0d4920 2022-05-12 thomas struct send_id_arg sia;
607 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
608 7d0d4920 2022-05-12 thomas size_t ndeltas, i;
610 f9c2e8e5 2022-02-13 thomas err = find_pack_for_reuse(&packidx, repo);
612 f9c2e8e5 2022-02-13 thomas return err;
614 f9c2e8e5 2022-02-13 thomas if (packidx == NULL)
615 f9c2e8e5 2022-02-13 thomas return NULL;
617 7d0d4920 2022-05-12 thomas err = got_object_prepare_delta_reuse(&pack, packidx,
618 7d0d4920 2022-05-12 thomas delta_cache_fd, repo);
620 f9c2e8e5 2022-02-13 thomas return err;
622 7d0d4920 2022-05-12 thomas memset(&sia, 0, sizeof(sia));
623 7d0d4920 2022-05-12 thomas sia.ibuf = pack->privsep_child->ibuf;
624 7d0d4920 2022-05-12 thomas err = got_object_idset_for_each(idset, send_id, &sia);
626 7d0d4920 2022-05-12 thomas return err;
627 7d0d4920 2022-05-12 thomas if (sia.nids > 0) {
628 7d0d4920 2022-05-12 thomas err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
629 7d0d4920 2022-05-12 thomas sia.ids, sia.nids);
631 7d0d4920 2022-05-12 thomas return err;
633 7d0d4920 2022-05-12 thomas err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
635 7d0d4920 2022-05-12 thomas return err;
637 7d0d4920 2022-05-12 thomas for (;;) {
638 7d0d4920 2022-05-12 thomas int done = 0;
640 7d0d4920 2022-05-12 thomas if (cancel_cb) {
641 7d0d4920 2022-05-12 thomas err = (*cancel_cb)(cancel_arg);
646 7d0d4920 2022-05-12 thomas err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
647 7d0d4920 2022-05-12 thomas pack->privsep_child->ibuf);
648 7d0d4920 2022-05-12 thomas if (err || done)
651 7d0d4920 2022-05-12 thomas for (i = 0; i < ndeltas; i++) {
652 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta *delta = &deltas[i];
653 7d0d4920 2022-05-12 thomas err = recv_reused_delta(delta, idset, v);
655 7d0d4920 2022-05-12 thomas goto done;
658 7d0d4920 2022-05-12 thomas err = report_progress(progress_cb, progress_arg, rl,
659 7d0d4920 2022-05-12 thomas ncolored, nfound, ntrees, 0L, ncommits,
660 7d0d4920 2022-05-12 thomas got_object_idset_num_elements(idset), v->nmeta, 0);
665 f9c2e8e5 2022-02-13 thomas return err;
668 f9c2e8e5 2022-02-13 thomas static const struct got_error *
669 85220b0e 2022-03-22 thomas pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
670 85220b0e 2022-03-22 thomas int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
671 85220b0e 2022-03-22 thomas struct got_repository *repo,
672 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
673 31ba2236 2022-01-05 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
675 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
676 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
677 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
678 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
679 3b6ceab7 2022-01-10 thomas int i, j, ndeltas, best_ndeltas;
680 3b6ceab7 2022-01-10 thomas off_t size, best_size;
681 959daf23 2021-10-29 thomas const int max_base_candidates = 3;
682 510885f7 2022-01-10 thomas size_t delta_memsize = 0;
683 510885f7 2022-01-10 thomas const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
684 ecf9545f 2021-10-15 thomas int outfd = -1;
686 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
687 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
688 e6bcace5 2021-06-22 stsp if (cancel_cb) {
689 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
693 31ba2236 2022-01-05 thomas err = report_progress(progress_cb, progress_arg, rl,
694 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
695 85220b0e 2022-03-22 thomas nreused + i, 0);
697 31ba2236 2022-01-05 thomas goto done;
698 e6bcace5 2021-06-22 stsp m = meta[i];
700 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
701 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
704 f8bb1d3e 2021-10-17 thomas err = got_object_raw_open(&raw, &outfd, repo, &m->id);
707 ab6186ae 2021-10-15 thomas m->size = raw->size;
709 2b0ae357 2022-01-10 thomas if (raw->f == NULL) {
710 2b0ae357 2022-01-10 thomas err = got_deltify_init_mem(&m->dtab, raw->data,
711 2b0ae357 2022-01-10 thomas raw->hdrlen, raw->size + raw->hdrlen);
713 2b0ae357 2022-01-10 thomas err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
714 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen);
719 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
720 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
721 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
722 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
723 e6bcace5 2021-06-22 stsp n->dtab = NULL;
726 b79280dd 2021-10-15 thomas best_size = raw->size;
727 b79280dd 2021-10-15 thomas best_ndeltas = 0;
728 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
729 e6bcace5 2021-06-22 stsp if (cancel_cb) {
730 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
734 e6bcace5 2021-06-22 stsp base = meta[j];
735 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
736 62a05ae9 2021-10-29 thomas if (base->nchain >= 128 ||
737 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
740 8ab9215c 2021-10-15 thomas err = got_object_raw_open(&base_raw, &outfd, repo,
741 f8bb1d3e 2021-10-17 thomas &base->id);
745 2b0ae357 2022-01-10 thomas if (raw->f == NULL && base_raw->f == NULL) {
746 2b0ae357 2022-01-10 thomas err = got_deltify_mem_mem(&deltas, &ndeltas,
747 2b0ae357 2022-01-10 thomas raw->data, raw->hdrlen,
748 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
749 2b0ae357 2022-01-10 thomas base->dtab, base_raw->data,
750 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
751 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
752 2b0ae357 2022-01-10 thomas } else if (raw->f == NULL) {
753 2b0ae357 2022-01-10 thomas err = got_deltify_mem_file(&deltas, &ndeltas,
754 2b0ae357 2022-01-10 thomas raw->data, raw->hdrlen,
755 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
756 2b0ae357 2022-01-10 thomas base->dtab, base_raw->f,
757 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
758 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
759 2b0ae357 2022-01-10 thomas } else if (base_raw->f == NULL) {
760 2b0ae357 2022-01-10 thomas err = got_deltify_file_mem(&deltas, &ndeltas,
761 2b0ae357 2022-01-10 thomas raw->f, raw->hdrlen,
762 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
763 2b0ae357 2022-01-10 thomas base->dtab, base_raw->data,
764 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
765 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
767 2b0ae357 2022-01-10 thomas err = got_deltify(&deltas, &ndeltas,
768 2b0ae357 2022-01-10 thomas raw->f, raw->hdrlen,
769 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
770 2b0ae357 2022-01-10 thomas base->dtab, base_raw->f, base_raw->hdrlen,
771 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
773 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
774 e6bcace5 2021-06-22 stsp base_raw = NULL;
778 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
779 b79280dd 2021-10-15 thomas if (size + 32 < best_size){
781 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
782 e6bcace5 2021-06-22 stsp * replace it.
784 b79280dd 2021-10-15 thomas best_size = size;
785 b79280dd 2021-10-15 thomas free(best_deltas);
786 b79280dd 2021-10-15 thomas best_deltas = deltas;
787 b79280dd 2021-10-15 thomas best_ndeltas = ndeltas;
788 b79280dd 2021-10-15 thomas deltas = NULL;
789 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
790 e6bcace5 2021-06-22 stsp m->prev = base;
791 e6bcace5 2021-06-22 stsp m->head = base->head;
792 e6bcace5 2021-06-22 stsp if (m->head == NULL)
793 e6bcace5 2021-06-22 stsp m->head = base;
795 e6bcace5 2021-06-22 stsp free(deltas);
796 e6bcace5 2021-06-22 stsp deltas = NULL;
797 e6bcace5 2021-06-22 stsp ndeltas = 0;
801 b79280dd 2021-10-15 thomas if (best_ndeltas > 0) {
802 510885f7 2022-01-10 thomas if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
803 510885f7 2022-01-10 thomas delta_memsize + best_size <= max_delta_memsize) {
804 510885f7 2022-01-10 thomas delta_memsize += best_size;
805 3b6ceab7 2022-01-10 thomas err = encode_delta_in_mem(m, raw, best_deltas,
806 3b6ceab7 2022-01-10 thomas best_ndeltas, best_size, m->prev->size);
808 3b6ceab7 2022-01-10 thomas m->delta_offset = ftello(delta_cache);
809 3b6ceab7 2022-01-10 thomas err = encode_delta(m, raw, best_deltas,
810 3b6ceab7 2022-01-10 thomas best_ndeltas, m->prev->size, delta_cache);
812 b79280dd 2021-10-15 thomas free(best_deltas);
813 b79280dd 2021-10-15 thomas best_deltas = NULL;
814 b79280dd 2021-10-15 thomas best_ndeltas = 0;
816 b79280dd 2021-10-15 thomas goto done;
819 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
820 e6bcace5 2021-06-22 stsp raw = NULL;
823 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
824 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
825 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
828 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
829 e6bcace5 2021-06-22 stsp if (base_raw)
830 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
831 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
832 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
833 b79280dd 2021-10-15 thomas free(deltas);
834 b79280dd 2021-10-15 thomas free(best_deltas);
835 e6bcace5 2021-06-22 stsp return err;
838 e6bcace5 2021-06-22 stsp static const struct got_error *
839 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
840 05118f5a 2021-06-22 stsp struct got_repository *repo)
842 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
843 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
846 05118f5a 2021-06-22 stsp *found = 0;
848 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
849 05118f5a 2021-06-22 stsp if (err == NULL)
850 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
851 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
852 05118f5a 2021-06-22 stsp err = NULL;
853 05118f5a 2021-06-22 stsp return err;
856 05118f5a 2021-06-22 stsp static const struct got_error *
857 f9c2e8e5 2022-02-13 thomas add_object(int want_meta, struct got_object_idset *idset,
858 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
859 78a00876 2022-03-22 thomas time_t mtime, int loose_obj_only, struct got_repository *repo,
860 78a00876 2022-03-22 thomas int *ncolored, int *nfound, int *ntrees,
861 78a00876 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
862 78a00876 2022-03-22 thomas struct got_ratelimit *rl)
864 e6bcace5 2021-06-22 stsp const struct got_error *err;
865 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m = NULL;
867 05118f5a 2021-06-22 stsp if (loose_obj_only) {
868 05118f5a 2021-06-22 stsp int is_packed;
869 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
871 05118f5a 2021-06-22 stsp return err;
872 a605f678 2022-03-22 thomas if (is_packed && want_meta)
873 05118f5a 2021-06-22 stsp return NULL;
876 f9c2e8e5 2022-02-13 thomas if (want_meta) {
877 f9c2e8e5 2022-02-13 thomas err = alloc_meta(&m, id, path, obj_type, mtime);
879 78a00876 2022-03-22 thomas return err;
881 78a00876 2022-03-22 thomas (*nfound)++;
882 78a00876 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
883 78a00876 2022-03-22 thomas *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
884 ee7b409c 2022-04-16 thomas if (err) {
885 ee7b409c 2022-04-16 thomas clear_meta(m);
887 f9c2e8e5 2022-02-13 thomas return err;
891 ee7b409c 2022-04-16 thomas err = got_object_idset_add(idset, id, m);
892 ee7b409c 2022-04-16 thomas if (err) {
893 ee7b409c 2022-04-16 thomas clear_meta(m);
896 ee7b409c 2022-04-16 thomas return err;
899 e6bcace5 2021-06-22 stsp static const struct got_error *
900 f9c2e8e5 2022-02-13 thomas load_tree_entries(struct got_object_id_queue *ids, int want_meta,
901 7afbdbad 2022-05-12 thomas struct got_object_idset *idset, struct got_object_idset *idset_exclude,
902 7afbdbad 2022-05-12 thomas struct got_object_id *tree_id,
903 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
904 85220b0e 2022-03-22 thomas int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
905 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
906 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
908 e6bcace5 2021-06-22 stsp const struct got_error *err;
909 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
910 e6bcace5 2021-06-22 stsp char *p = NULL;
913 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
915 85220b0e 2022-03-22 thomas return err;
917 85220b0e 2022-03-22 thomas (*ntrees)++;
918 85220b0e 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
919 85220b0e 2022-03-22 thomas *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
921 e6bcace5 2021-06-22 stsp return err;
923 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
924 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
925 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
926 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
928 e6bcace5 2021-06-22 stsp if (cancel_cb) {
929 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
934 08511b5e 2021-09-24 thomas if (got_object_tree_entry_is_submodule(e) ||
935 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset, id) ||
936 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset_exclude, id))
939 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
940 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
941 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
945 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
946 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
947 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
950 eee114b4 2022-05-31 thomas qid->data = p;
951 eee114b4 2022-05-31 thomas p = NULL;
952 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
953 08511b5e 2021-09-24 thomas } else if (S_ISREG(mode) || S_ISLNK(mode)) {
954 7afbdbad 2022-05-12 thomas err = add_object(want_meta,
955 7afbdbad 2022-05-12 thomas want_meta ? idset : idset_exclude, id, p,
956 78a00876 2022-03-22 thomas GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
957 78a00876 2022-03-22 thomas ncolored, nfound, ntrees,
958 78a00876 2022-03-22 thomas progress_cb, progress_arg, rl);
962 eee114b4 2022-05-31 thomas p = NULL;
965 eee114b4 2022-05-31 thomas p = NULL;
969 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
971 e6bcace5 2021-06-22 stsp return err;
974 e6bcace5 2021-06-22 stsp static const struct got_error *
975 f9c2e8e5 2022-02-13 thomas load_tree(int want_meta, struct got_object_idset *idset,
976 7afbdbad 2022-05-12 thomas struct got_object_idset *idset_exclude,
977 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
978 85220b0e 2022-03-22 thomas struct got_repository *repo, int loose_obj_only,
979 85220b0e 2022-03-22 thomas int *ncolored, int *nfound, int *ntrees,
980 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
981 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
983 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
984 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
985 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
987 7afbdbad 2022-05-12 thomas if (got_object_idset_contains(idset, tree_id) ||
988 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset_exclude, tree_id))
989 e6bcace5 2021-06-22 stsp return NULL;
991 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
993 eee114b4 2022-05-31 thomas return err;
994 eee114b4 2022-05-31 thomas qid->data = strdup(dpath);
995 eee114b4 2022-05-31 thomas if (qid->data == NULL) {
996 eee114b4 2022-05-31 thomas err = got_error_from_errno("strdup");
997 eee114b4 2022-05-31 thomas got_object_qid_free(qid);
998 e6bcace5 2021-06-22 stsp return err;
1001 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
1002 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1004 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
1005 eee114b4 2022-05-31 thomas const char *path;
1006 e6bcace5 2021-06-22 stsp if (cancel_cb) {
1007 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
1012 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
1013 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
1014 eee114b4 2022-05-31 thomas path = qid->data;
1016 7afbdbad 2022-05-12 thomas if (got_object_idset_contains(idset, &qid->id) ||
1017 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset_exclude, &qid->id)) {
1018 eee114b4 2022-05-31 thomas free(qid->data);
1019 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1023 7afbdbad 2022-05-12 thomas err = add_object(want_meta, want_meta ? idset : idset_exclude,
1024 eee114b4 2022-05-31 thomas &qid->id, path, GOT_OBJ_TYPE_TREE,
1025 7afbdbad 2022-05-12 thomas mtime, loose_obj_only, repo,
1026 78a00876 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1027 e6bcace5 2021-06-22 stsp if (err) {
1028 eee114b4 2022-05-31 thomas free(qid->data);
1029 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1033 7afbdbad 2022-05-12 thomas err = load_tree_entries(&tree_ids, want_meta, idset,
1034 7afbdbad 2022-05-12 thomas idset_exclude, &qid->id,
1035 eee114b4 2022-05-31 thomas path, mtime, repo, loose_obj_only, ncolored, nfound,
1036 85220b0e 2022-03-22 thomas ntrees, progress_cb, progress_arg, rl,
1037 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1038 eee114b4 2022-05-31 thomas free(qid->data);
1039 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1044 eee114b4 2022-05-31 thomas STAILQ_FOREACH(qid, &tree_ids, entry)
1045 eee114b4 2022-05-31 thomas free(qid->data);
1046 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
1047 e6bcace5 2021-06-22 stsp return err;
1050 e6bcace5 2021-06-22 stsp static const struct got_error *
1051 f9c2e8e5 2022-02-13 thomas load_commit(int want_meta, struct got_object_idset *idset,
1052 7afbdbad 2022-05-12 thomas struct got_object_idset *idset_exclude,
1053 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1054 85220b0e 2022-03-22 thomas int *ncolored, int *nfound, int *ntrees,
1055 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1056 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1058 e6bcace5 2021-06-22 stsp const struct got_error *err;
1059 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1061 7afbdbad 2022-05-12 thomas if (got_object_idset_contains(idset, id) ||
1062 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset_exclude, id))
1063 e6bcace5 2021-06-22 stsp return NULL;
1065 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1066 05118f5a 2021-06-22 stsp int is_packed;
1067 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1069 05118f5a 2021-06-22 stsp return err;
1070 a605f678 2022-03-22 thomas if (is_packed && want_meta)
1071 05118f5a 2021-06-22 stsp return NULL;
1074 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1076 e6bcace5 2021-06-22 stsp return err;
1078 7afbdbad 2022-05-12 thomas err = add_object(want_meta, want_meta ? idset : idset_exclude,
1079 7afbdbad 2022-05-12 thomas id, "", GOT_OBJ_TYPE_COMMIT,
1080 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
1081 78a00876 2022-03-22 thomas loose_obj_only, repo,
1082 78a00876 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1084 e6bcace5 2021-06-22 stsp goto done;
1086 7afbdbad 2022-05-12 thomas err = load_tree(want_meta, idset, idset_exclude,
1087 7afbdbad 2022-05-12 thomas got_object_commit_get_tree_id(commit),
1088 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
1089 85220b0e 2022-03-22 thomas repo, loose_obj_only, ncolored, nfound, ntrees,
1090 85220b0e 2022-03-22 thomas progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1092 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1093 e6bcace5 2021-06-22 stsp return err;
1096 05118f5a 2021-06-22 stsp static const struct got_error *
1097 f9c2e8e5 2022-02-13 thomas load_tag(int want_meta, struct got_object_idset *idset,
1098 7afbdbad 2022-05-12 thomas struct got_object_idset *idset_exclude,
1099 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1100 85220b0e 2022-03-22 thomas int *ncolored, int *nfound, int *ntrees,
1101 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1102 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1104 05118f5a 2021-06-22 stsp const struct got_error *err;
1105 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
1107 7afbdbad 2022-05-12 thomas if (got_object_idset_contains(idset, id) ||
1108 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset_exclude, id))
1109 05118f5a 2021-06-22 stsp return NULL;
1111 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1112 05118f5a 2021-06-22 stsp int is_packed;
1113 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1115 05118f5a 2021-06-22 stsp return err;
1116 a605f678 2022-03-22 thomas if (is_packed && want_meta)
1117 05118f5a 2021-06-22 stsp return NULL;
1120 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1122 05118f5a 2021-06-22 stsp return err;
1124 7afbdbad 2022-05-12 thomas err = add_object(want_meta, want_meta ? idset : idset_exclude,
1125 7afbdbad 2022-05-12 thomas id, "", GOT_OBJ_TYPE_TAG,
1126 78a00876 2022-03-22 thomas got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1127 78a00876 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1129 05118f5a 2021-06-22 stsp goto done;
1131 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1132 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1133 7afbdbad 2022-05-12 thomas err = load_commit(want_meta, idset, idset_exclude,
1134 85220b0e 2022-03-22 thomas got_object_tag_get_object_id(tag), repo, loose_obj_only,
1135 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1136 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1138 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1139 7afbdbad 2022-05-12 thomas err = load_tree(want_meta, idset, idset_exclude,
1140 f9c2e8e5 2022-02-13 thomas got_object_tag_get_object_id(tag), "",
1141 85220b0e 2022-03-22 thomas got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1142 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1143 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1150 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1151 05118f5a 2021-06-22 stsp return err;
1154 e6bcace5 2021-06-22 stsp enum findtwixt_color {
1155 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
1156 e6bcace5 2021-06-22 stsp COLOR_DROP,
1157 e6bcace5 2021-06-22 stsp COLOR_BLANK,
1158 5fe7b5c1 2022-04-16 thomas COLOR_SKIP,
1161 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
1162 e6bcace5 2021-06-22 stsp COLOR_KEEP,
1163 e6bcace5 2021-06-22 stsp COLOR_DROP,
1164 5fe7b5c1 2022-04-16 thomas COLOR_BLANK,
1165 5fe7b5c1 2022-04-16 thomas COLOR_SKIP,
1168 e6bcace5 2021-06-22 stsp static const struct got_error *
1169 5fe7b5c1 2022-04-16 thomas paint_commit(struct got_object_qid *qid, int color)
1171 5fe7b5c1 2022-04-16 thomas if (color < 0 || color >= nitems(findtwixt_colors))
1172 5fe7b5c1 2022-04-16 thomas return got_error(GOT_ERR_RANGE);
1174 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
1175 e6bcace5 2021-06-22 stsp return NULL;
1178 e6bcace5 2021-06-22 stsp static const struct got_error *
1179 5fe7b5c1 2022-04-16 thomas queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1180 5fe7b5c1 2022-04-16 thomas int color, struct got_repository *repo)
1182 5fe7b5c1 2022-04-16 thomas const struct got_error *err;
1183 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1185 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1187 e6bcace5 2021-06-22 stsp return err;
1189 5fe7b5c1 2022-04-16 thomas STAILQ_INSERT_TAIL(ids, qid, entry);
1190 5fe7b5c1 2022-04-16 thomas return paint_commit(qid, color);
1193 e6bcace5 2021-06-22 stsp struct append_id_arg {
1194 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1196 5fe7b5c1 2022-04-16 thomas struct got_object_idset *drop;
1197 5fe7b5c1 2022-04-16 thomas struct got_object_idset *skip;
1200 e6bcace5 2021-06-22 stsp static const struct got_error *
1201 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1203 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1205 5fe7b5c1 2022-04-16 thomas if (got_object_idset_contains(a->skip, id) ||
1206 5fe7b5c1 2022-04-16 thomas got_object_idset_contains(a->drop, id))
1207 5fe7b5c1 2022-04-16 thomas return NULL;
1209 5fe7b5c1 2022-04-16 thomas a->array[++a->idx] = got_object_id_dup(id);
1210 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1211 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1213 e6bcace5 2021-06-22 stsp return NULL;
1216 a3a0e34e 2022-04-16 thomas static const struct got_error *
1217 a3a0e34e 2022-04-16 thomas queue_commit_or_tag_id(struct got_object_id *id, int color,
1218 a3a0e34e 2022-04-16 thomas struct got_object_id_queue *ids, struct got_repository *repo)
1220 a3a0e34e 2022-04-16 thomas const struct got_error *err;
1221 a3a0e34e 2022-04-16 thomas struct got_tag_object *tag = NULL;
1222 a3a0e34e 2022-04-16 thomas int obj_type;
1224 a3a0e34e 2022-04-16 thomas err = got_object_get_type(&obj_type, repo, id);
1225 a3a0e34e 2022-04-16 thomas if (err)
1226 a3a0e34e 2022-04-16 thomas return err;
1228 a3a0e34e 2022-04-16 thomas if (obj_type == GOT_OBJ_TYPE_TAG) {
1229 a3a0e34e 2022-04-16 thomas err = got_object_open_as_tag(&tag, repo, id);
1230 a3a0e34e 2022-04-16 thomas if (err)
1231 a3a0e34e 2022-04-16 thomas return err;
1232 a3a0e34e 2022-04-16 thomas obj_type = got_object_tag_get_object_type(tag);
1233 a3a0e34e 2022-04-16 thomas id = got_object_tag_get_object_id(tag);
1236 a3a0e34e 2022-04-16 thomas if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1237 a3a0e34e 2022-04-16 thomas err = queue_commit_id(ids, id, color, repo);
1238 a3a0e34e 2022-04-16 thomas if (err)
1239 a3a0e34e 2022-04-16 thomas goto done;
1242 a3a0e34e 2022-04-16 thomas if (tag)
1243 a3a0e34e 2022-04-16 thomas got_object_tag_close(tag);
1244 a3a0e34e 2022-04-16 thomas return err;
1247 e6bcace5 2021-06-22 stsp static const struct got_error *
1248 5fe7b5c1 2022-04-16 thomas paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1249 e9371be6 2022-04-16 thomas struct got_object_idset *keep, struct got_object_idset *drop,
1250 5fe7b5c1 2022-04-16 thomas struct got_object_idset *skip, struct got_repository *repo,
1251 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1252 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1254 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1255 e9371be6 2022-04-16 thomas struct got_commit_object *commit = NULL;
1256 5fe7b5c1 2022-04-16 thomas const struct got_object_id_queue *parents;
1257 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1258 5fe7b5c1 2022-04-16 thomas int nqueued = nids, nskip = 0;
1260 5fe7b5c1 2022-04-16 thomas while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1261 5fe7b5c1 2022-04-16 thomas int color;
1263 21a0da85 2022-04-16 thomas if (cancel_cb) {
1264 21a0da85 2022-04-16 thomas err = cancel_cb(cancel_arg);
1265 21a0da85 2022-04-16 thomas if (err)
1269 e9371be6 2022-04-16 thomas qid = STAILQ_FIRST(ids);
1270 5fe7b5c1 2022-04-16 thomas STAILQ_REMOVE_HEAD(ids, entry);
1271 5fe7b5c1 2022-04-16 thomas nqueued--;
1272 5fe7b5c1 2022-04-16 thomas color = *((int *)qid->data);
1273 5fe7b5c1 2022-04-16 thomas if (color == COLOR_SKIP)
1274 5fe7b5c1 2022-04-16 thomas nskip--;
1276 ec242592 2022-04-22 thomas if (got_object_idset_contains(skip, &qid->id)) {
1277 5fe7b5c1 2022-04-16 thomas got_object_qid_free(qid);
1278 5fe7b5c1 2022-04-16 thomas continue;
1281 5fe7b5c1 2022-04-16 thomas switch (color) {
1282 5fe7b5c1 2022-04-16 thomas case COLOR_KEEP:
1283 ec242592 2022-04-22 thomas if (got_object_idset_contains(keep, &qid->id)) {
1284 5fe7b5c1 2022-04-16 thomas got_object_qid_free(qid);
1285 5fe7b5c1 2022-04-16 thomas continue;
1287 ec242592 2022-04-22 thomas if (got_object_idset_contains(drop, &qid->id)) {
1288 5fe7b5c1 2022-04-16 thomas err = paint_commit(qid, COLOR_SKIP);
1289 5fe7b5c1 2022-04-16 thomas if (err)
1290 5fe7b5c1 2022-04-16 thomas goto done;
1291 5fe7b5c1 2022-04-16 thomas nskip++;
1293 5fe7b5c1 2022-04-16 thomas (*ncolored)++;
1294 ec242592 2022-04-22 thomas err = got_object_idset_add(keep, &qid->id, NULL);
1295 5fe7b5c1 2022-04-16 thomas if (err)
1296 5fe7b5c1 2022-04-16 thomas goto done;
1298 5fe7b5c1 2022-04-16 thomas case COLOR_DROP:
1299 ec242592 2022-04-22 thomas if (got_object_idset_contains(drop, &qid->id)) {
1300 5fe7b5c1 2022-04-16 thomas got_object_qid_free(qid);
1301 5fe7b5c1 2022-04-16 thomas continue;
1303 ec242592 2022-04-22 thomas if (got_object_idset_contains(keep, &qid->id)) {
1304 5fe7b5c1 2022-04-16 thomas err = paint_commit(qid, COLOR_SKIP);
1305 5fe7b5c1 2022-04-16 thomas if (err)
1306 5fe7b5c1 2022-04-16 thomas goto done;
1307 5fe7b5c1 2022-04-16 thomas nskip++;
1309 5fe7b5c1 2022-04-16 thomas (*ncolored)++;
1310 ec242592 2022-04-22 thomas err = got_object_idset_add(drop, &qid->id, NULL);
1311 5fe7b5c1 2022-04-16 thomas if (err)
1312 5fe7b5c1 2022-04-16 thomas goto done;
1314 5fe7b5c1 2022-04-16 thomas case COLOR_SKIP:
1315 ec242592 2022-04-22 thomas if (!got_object_idset_contains(skip, &qid->id)) {
1316 ec242592 2022-04-22 thomas err = got_object_idset_add(skip, &qid->id,
1318 5fe7b5c1 2022-04-16 thomas if (err)
1319 5fe7b5c1 2022-04-16 thomas goto done;
1322 5fe7b5c1 2022-04-16 thomas default:
1323 5fe7b5c1 2022-04-16 thomas /* should not happen */
1324 5fe7b5c1 2022-04-16 thomas err = got_error_fmt(GOT_ERR_NOT_IMPL,
1325 5fe7b5c1 2022-04-16 thomas "%s invalid commit color %d", __func__, color);
1326 5fe7b5c1 2022-04-16 thomas goto done;
1329 85220b0e 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
1330 85220b0e 2022-03-22 thomas *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1331 85220b0e 2022-03-22 thomas if (err)
1335 ec242592 2022-04-22 thomas err = got_object_open_as_commit(&commit, repo, &qid->id);
1336 5fe7b5c1 2022-04-16 thomas if (err)
1339 5fe7b5c1 2022-04-16 thomas parents = got_object_commit_get_parent_ids(commit);
1340 5fe7b5c1 2022-04-16 thomas if (parents) {
1341 5fe7b5c1 2022-04-16 thomas struct got_object_qid *pid;
1342 5fe7b5c1 2022-04-16 thomas color = *((int *)qid->data);
1343 5fe7b5c1 2022-04-16 thomas STAILQ_FOREACH(pid, parents, entry) {
1344 ec242592 2022-04-22 thomas err = queue_commit_id(ids, &pid->id, color,
1346 5fe7b5c1 2022-04-16 thomas if (err)
1348 5fe7b5c1 2022-04-16 thomas nqueued++;
1349 5fe7b5c1 2022-04-16 thomas if (color == COLOR_SKIP)
1350 5fe7b5c1 2022-04-16 thomas nskip++;
1354 5fe7b5c1 2022-04-16 thomas got_object_commit_close(commit);
1355 5fe7b5c1 2022-04-16 thomas commit = NULL;
1356 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1359 e9371be6 2022-04-16 thomas if (commit)
1360 e9371be6 2022-04-16 thomas got_object_commit_close(commit);
1361 e9371be6 2022-04-16 thomas return err;
1364 e9371be6 2022-04-16 thomas static const struct got_error *
1365 e9371be6 2022-04-16 thomas findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1366 e9371be6 2022-04-16 thomas struct got_object_id **head, int nhead,
1367 e9371be6 2022-04-16 thomas struct got_object_id **tail, int ntail,
1368 e9371be6 2022-04-16 thomas struct got_repository *repo,
1369 e9371be6 2022-04-16 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1370 e9371be6 2022-04-16 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1372 e9371be6 2022-04-16 thomas const struct got_error *err = NULL;
1373 e9371be6 2022-04-16 thomas struct got_object_id_queue ids;
1374 5fe7b5c1 2022-04-16 thomas struct got_object_idset *keep, *drop, *skip = NULL;
1375 e9371be6 2022-04-16 thomas int i, nkeep;
1377 e9371be6 2022-04-16 thomas STAILQ_INIT(&ids);
1378 e9371be6 2022-04-16 thomas *res = NULL;
1379 e9371be6 2022-04-16 thomas *nres = 0;
1380 e9371be6 2022-04-16 thomas *ncolored = 0;
1382 e9371be6 2022-04-16 thomas keep = got_object_idset_alloc();
1383 e9371be6 2022-04-16 thomas if (keep == NULL)
1384 e9371be6 2022-04-16 thomas return got_error_from_errno("got_object_idset_alloc");
1386 e9371be6 2022-04-16 thomas drop = got_object_idset_alloc();
1387 e9371be6 2022-04-16 thomas if (drop == NULL) {
1388 e9371be6 2022-04-16 thomas err = got_error_from_errno("got_object_idset_alloc");
1389 e9371be6 2022-04-16 thomas goto done;
1392 5fe7b5c1 2022-04-16 thomas skip = got_object_idset_alloc();
1393 5fe7b5c1 2022-04-16 thomas if (skip == NULL) {
1394 5fe7b5c1 2022-04-16 thomas err = got_error_from_errno("got_object_idset_alloc");
1395 5fe7b5c1 2022-04-16 thomas goto done;
1398 e9371be6 2022-04-16 thomas for (i = 0; i < nhead; i++) {
1399 e9371be6 2022-04-16 thomas struct got_object_id *id = head[i];
1400 e9371be6 2022-04-16 thomas if (id == NULL)
1401 e9371be6 2022-04-16 thomas continue;
1402 fc457d24 2022-04-16 thomas err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1403 e9371be6 2022-04-16 thomas if (err)
1404 e9371be6 2022-04-16 thomas goto done;
1407 e9371be6 2022-04-16 thomas for (i = 0; i < ntail; i++) {
1408 e9371be6 2022-04-16 thomas struct got_object_id *id = tail[i];
1409 e9371be6 2022-04-16 thomas if (id == NULL)
1410 e9371be6 2022-04-16 thomas continue;
1411 e9371be6 2022-04-16 thomas err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1412 e9371be6 2022-04-16 thomas if (err)
1413 e9371be6 2022-04-16 thomas goto done;
1416 5fe7b5c1 2022-04-16 thomas err = paint_commits(ncolored, &ids, nhead + ntail,
1417 5fe7b5c1 2022-04-16 thomas keep, drop, skip, repo, progress_cb, progress_arg, rl,
1418 5fe7b5c1 2022-04-16 thomas cancel_cb, cancel_arg);
1419 e9371be6 2022-04-16 thomas if (err)
1420 e9371be6 2022-04-16 thomas goto done;
1422 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1423 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1424 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1425 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1426 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1427 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1428 e6bcace5 2021-06-22 stsp goto done;
1430 5fe7b5c1 2022-04-16 thomas arg.idx = -1;
1431 5fe7b5c1 2022-04-16 thomas arg.skip = skip;
1432 5fe7b5c1 2022-04-16 thomas arg.drop = drop;
1433 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1434 e6bcace5 2021-06-22 stsp if (err) {
1435 e6bcace5 2021-06-22 stsp free(arg.array);
1436 e6bcace5 2021-06-22 stsp goto done;
1438 e6bcace5 2021-06-22 stsp *res = arg.array;
1439 5fe7b5c1 2022-04-16 thomas *nres = arg.idx + 1;
1442 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1443 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1444 5fe7b5c1 2022-04-16 thomas if (skip)
1445 5fe7b5c1 2022-04-16 thomas got_object_idset_free(skip);
1446 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1447 e6bcace5 2021-06-22 stsp return err;
1450 e6bcace5 2021-06-22 stsp static const struct got_error *
1451 85220b0e 2022-03-22 thomas load_object_ids(int *ncolored, int *nfound, int *ntrees,
1452 85220b0e 2022-03-22 thomas struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1453 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1454 85220b0e 2022-03-22 thomas int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1455 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1457 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1458 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1459 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
1460 7afbdbad 2022-05-12 thomas struct got_object_idset *idset_exclude;
1462 7afbdbad 2022-05-12 thomas idset_exclude = got_object_idset_alloc();
1463 7afbdbad 2022-05-12 thomas if (idset_exclude == NULL)
1464 7afbdbad 2022-05-12 thomas return got_error_from_errno("got_object_idset_alloc");
1466 85220b0e 2022-03-22 thomas *ncolored = 0;
1467 85220b0e 2022-03-22 thomas *nfound = 0;
1468 85220b0e 2022-03-22 thomas *ntrees = 0;
1470 85220b0e 2022-03-22 thomas err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1471 85220b0e 2022-03-22 thomas repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1472 ae2b0251 2022-05-12 thomas if (err)
1473 e6bcace5 2021-06-22 stsp goto done;
1475 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1476 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
1477 05118f5a 2021-06-22 stsp if (id == NULL)
1479 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1481 05118f5a 2021-06-22 stsp return err;
1482 8eef4276 2022-04-16 thomas if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1483 7afbdbad 2022-05-12 thomas err = load_commit(0, idset, idset_exclude, id, repo,
1484 8eef4276 2022-04-16 thomas loose_obj_only, ncolored, nfound, ntrees,
1485 8eef4276 2022-04-16 thomas progress_cb, progress_arg, rl,
1486 8eef4276 2022-04-16 thomas cancel_cb, cancel_arg);
1488 07165b17 2021-07-01 stsp goto done;
1489 8eef4276 2022-04-16 thomas } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1490 7afbdbad 2022-05-12 thomas err = load_tag(0, idset, idset_exclude, id, repo,
1491 8eef4276 2022-04-16 thomas loose_obj_only, ncolored, nfound, ntrees,
1492 8eef4276 2022-04-16 thomas progress_cb, progress_arg, rl,
1493 8eef4276 2022-04-16 thomas cancel_cb, cancel_arg);
1494 8eef4276 2022-04-16 thomas if (err)
1495 8eef4276 2022-04-16 thomas goto done;
1499 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
1500 7afbdbad 2022-05-12 thomas err = load_commit(1, idset, idset_exclude,
1501 7afbdbad 2022-05-12 thomas ids[i], repo, loose_obj_only, ncolored, nfound, ntrees,
1502 7afbdbad 2022-05-12 thomas progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1504 eca70f98 2021-09-03 stsp goto done;
1507 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1508 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1509 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m;
1510 05118f5a 2021-06-22 stsp if (id == NULL)
1512 f9c2e8e5 2022-02-13 thomas m = got_object_idset_get(idset, id);
1513 f9c2e8e5 2022-02-13 thomas if (m == NULL) {
1514 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1516 07165b17 2021-07-01 stsp goto done;
1518 f9c2e8e5 2022-02-13 thomas obj_type = m->obj_type;
1519 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1521 7afbdbad 2022-05-12 thomas err = load_tag(1, idset, idset_exclude, id, repo,
1522 7afbdbad 2022-05-12 thomas loose_obj_only, ncolored, nfound, ntrees,
1523 7afbdbad 2022-05-12 thomas progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1525 e6bcace5 2021-06-22 stsp goto done;
1528 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
1529 e6bcace5 2021-06-22 stsp free(ids[i]);
1531 e6bcace5 2021-06-22 stsp free(ids);
1532 7afbdbad 2022-05-12 thomas got_object_idset_free(idset_exclude);
1533 e6bcace5 2021-06-22 stsp return err;
1536 e6bcace5 2021-06-22 stsp const struct got_error *
1537 9249e7e3 2022-05-12 thomas hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
1541 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1542 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
1543 e6bcace5 2021-06-22 stsp if (n != len)
1544 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
1545 9249e7e3 2022-05-12 thomas return NULL;
1548 9249e7e3 2022-05-12 thomas const struct got_error *
1549 9249e7e3 2022-05-12 thomas hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1551 9249e7e3 2022-05-12 thomas unsigned char buf[65536];
1552 9249e7e3 2022-05-12 thomas off_t remain = len;
1553 9249e7e3 2022-05-12 thomas size_t n;
1555 9249e7e3 2022-05-12 thomas while (remain > 0) {
1556 9249e7e3 2022-05-12 thomas size_t copylen = MIN(sizeof(buf), remain);
1557 9249e7e3 2022-05-12 thomas n = fread(buf, 1, copylen, fsrc);
1558 9249e7e3 2022-05-12 thomas if (n != copylen)
1559 9249e7e3 2022-05-12 thomas return got_ferror(fsrc, GOT_ERR_IO);
1560 9249e7e3 2022-05-12 thomas SHA1Update(ctx, buf, copylen);
1561 9249e7e3 2022-05-12 thomas n = fwrite(buf, 1, copylen, fdst);
1562 9249e7e3 2022-05-12 thomas if (n != copylen)
1563 9249e7e3 2022-05-12 thomas return got_ferror(fdst, GOT_ERR_IO);
1564 9249e7e3 2022-05-12 thomas remain -= copylen;
1567 e6bcace5 2021-06-22 stsp return NULL;
1570 d93dda9a 2022-05-12 thomas const struct got_error *
1571 d93dda9a 2022-05-12 thomas hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1572 d93dda9a 2022-05-12 thomas FILE *fdst, off_t len, SHA1_CTX *ctx)
1574 d93dda9a 2022-05-12 thomas size_t n;
1576 d93dda9a 2022-05-12 thomas if (src_offset + len > src_size)
1577 d93dda9a 2022-05-12 thomas return got_error(GOT_ERR_RANGE);
1579 d93dda9a 2022-05-12 thomas SHA1Update(ctx, src + src_offset, len);
1580 d93dda9a 2022-05-12 thomas n = fwrite(src + src_offset, 1, len, fdst);
1581 d93dda9a 2022-05-12 thomas if (n != len)
1582 d93dda9a 2022-05-12 thomas return got_ferror(fdst, GOT_ERR_IO);
1584 d93dda9a 2022-05-12 thomas return NULL;
1587 e6bcace5 2021-06-22 stsp static void
1588 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1590 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1591 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1592 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1593 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1596 e6bcace5 2021-06-22 stsp static int
1597 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1599 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1601 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1602 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1603 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1604 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1605 f6b43367 2022-05-01 thomas if (bhd->mtime < ahd->mtime)
1606 f6b43367 2022-05-01 thomas return -1;
1607 f6b43367 2022-05-01 thomas if (bhd->mtime > ahd->mtime)
1608 f6b43367 2022-05-01 thomas return 1;
1609 f6b43367 2022-05-01 thomas if (bhd < ahd)
1610 f6b43367 2022-05-01 thomas return -1;
1611 f6b43367 2022-05-01 thomas if (bhd > ahd)
1612 f6b43367 2022-05-01 thomas return 1;
1613 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1614 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1615 f6b43367 2022-05-01 thomas if (a->mtime < b->mtime)
1616 f6b43367 2022-05-01 thomas return -1;
1617 f6b43367 2022-05-01 thomas if (a->mtime > b->mtime)
1618 f6b43367 2022-05-01 thomas return 1;
1619 f6b43367 2022-05-01 thomas return got_object_id_cmp(&a->id, &b->id);
1622 f9c2e8e5 2022-02-13 thomas static int
1623 f9c2e8e5 2022-02-13 thomas reuse_write_order_cmp(const void *pa, const void *pb)
1625 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *a, *b;
1627 f9c2e8e5 2022-02-13 thomas a = *(struct got_pack_meta **)pa;
1628 f9c2e8e5 2022-02-13 thomas b = *(struct got_pack_meta **)pb;
1630 f9c2e8e5 2022-02-13 thomas if (a->reused_delta_offset < b->reused_delta_offset)
1631 f9c2e8e5 2022-02-13 thomas return -1;
1632 f9c2e8e5 2022-02-13 thomas if (a->reused_delta_offset > b->reused_delta_offset)
1633 f9c2e8e5 2022-02-13 thomas return 1;
1634 f9c2e8e5 2022-02-13 thomas return 0;
1637 e6bcace5 2021-06-22 stsp static const struct got_error *
1638 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1642 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1644 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1645 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1646 e6bcace5 2021-06-22 stsp len >>= 4;
1647 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1648 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1649 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1650 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1651 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1652 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1655 e6bcace5 2021-06-22 stsp *hdrlen = i;
1656 e6bcace5 2021-06-22 stsp return NULL;
1659 e6bcace5 2021-06-22 stsp static int
1660 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1663 e6bcace5 2021-06-22 stsp char rbuf[8];
1665 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1666 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1667 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1668 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1672 e6bcace5 2021-06-22 stsp while (i > 0)
1673 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1677 e6bcace5 2021-06-22 stsp static const struct got_error *
1678 3b6ceab7 2022-01-10 thomas deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1679 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m)
1681 3b6ceab7 2022-01-10 thomas const struct got_error *err;
1682 3b6ceab7 2022-01-10 thomas char buf[32];
1685 f9c2e8e5 2022-02-13 thomas if (m->prev->off != 0) {
1686 3b6ceab7 2022-01-10 thomas err = packhdr(&nh, buf, sizeof(buf),
1687 3b6ceab7 2022-01-10 thomas GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1688 3b6ceab7 2022-01-10 thomas if (err)
1689 3b6ceab7 2022-01-10 thomas return err;
1690 3b6ceab7 2022-01-10 thomas nh += packoff(buf + nh, m->off - m->prev->off);
1691 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, buf, nh, ctx);
1692 3b6ceab7 2022-01-10 thomas if (err)
1693 3b6ceab7 2022-01-10 thomas return err;
1694 3b6ceab7 2022-01-10 thomas *packfile_size += nh;
1695 3b6ceab7 2022-01-10 thomas } else {
1696 3b6ceab7 2022-01-10 thomas err = packhdr(&nh, buf, sizeof(buf),
1697 3b6ceab7 2022-01-10 thomas GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1698 3b6ceab7 2022-01-10 thomas if (err)
1699 3b6ceab7 2022-01-10 thomas return err;
1700 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, buf, nh, ctx);
1701 3b6ceab7 2022-01-10 thomas if (err)
1702 3b6ceab7 2022-01-10 thomas return err;
1703 3b6ceab7 2022-01-10 thomas *packfile_size += nh;
1704 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, m->prev->id.sha1,
1705 3b6ceab7 2022-01-10 thomas sizeof(m->prev->id.sha1), ctx);
1706 3b6ceab7 2022-01-10 thomas if (err)
1707 3b6ceab7 2022-01-10 thomas return err;
1708 3b6ceab7 2022-01-10 thomas *packfile_size += sizeof(m->prev->id.sha1);
1711 3b6ceab7 2022-01-10 thomas return NULL;
1714 3b6ceab7 2022-01-10 thomas static const struct got_error *
1715 f9c2e8e5 2022-02-13 thomas write_packed_object(off_t *packfile_size, FILE *packfile,
1716 d93dda9a 2022-05-12 thomas FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1717 d93dda9a 2022-05-12 thomas struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
1718 d93dda9a 2022-05-12 thomas struct got_repository *repo)
1720 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
1721 f9c2e8e5 2022-02-13 thomas struct got_deflate_checksum csum;
1722 f9c2e8e5 2022-02-13 thomas char buf[32];
1724 f9c2e8e5 2022-02-13 thomas struct got_raw_object *raw = NULL;
1725 f9c2e8e5 2022-02-13 thomas off_t outlen;
1727 f9c2e8e5 2022-02-13 thomas csum.output_sha1 = ctx;
1728 f9c2e8e5 2022-02-13 thomas csum.output_crc = NULL;
1730 f9c2e8e5 2022-02-13 thomas m->off = ftello(packfile);
1731 f9c2e8e5 2022-02-13 thomas if (m->delta_len == 0) {
1732 f9c2e8e5 2022-02-13 thomas err = got_object_raw_open(&raw, outfd, repo, &m->id);
1733 f9c2e8e5 2022-02-13 thomas if (err)
1734 f9c2e8e5 2022-02-13 thomas goto done;
1735 f9c2e8e5 2022-02-13 thomas err = packhdr(&nh, buf, sizeof(buf),
1736 f9c2e8e5 2022-02-13 thomas m->obj_type, raw->size);
1737 f9c2e8e5 2022-02-13 thomas if (err)
1738 f9c2e8e5 2022-02-13 thomas goto done;
1739 f9c2e8e5 2022-02-13 thomas err = hwrite(packfile, buf, nh, ctx);
1740 f9c2e8e5 2022-02-13 thomas if (err)
1741 f9c2e8e5 2022-02-13 thomas goto done;
1742 f9c2e8e5 2022-02-13 thomas *packfile_size += nh;
1743 f9c2e8e5 2022-02-13 thomas if (raw->f == NULL) {
1744 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file_mmap(&outlen,
1745 f9c2e8e5 2022-02-13 thomas raw->data + raw->hdrlen, 0, raw->size,
1746 f9c2e8e5 2022-02-13 thomas packfile, &csum);
1747 f9c2e8e5 2022-02-13 thomas if (err)
1748 f9c2e8e5 2022-02-13 thomas goto done;
1749 f9c2e8e5 2022-02-13 thomas } else {
1750 f9c2e8e5 2022-02-13 thomas if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1751 f9c2e8e5 2022-02-13 thomas == -1) {
1752 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
1753 f9c2e8e5 2022-02-13 thomas goto done;
1755 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file(&outlen, raw->f,
1756 f9c2e8e5 2022-02-13 thomas raw->size, packfile, &csum);
1757 f9c2e8e5 2022-02-13 thomas if (err)
1758 f9c2e8e5 2022-02-13 thomas goto done;
1760 f9c2e8e5 2022-02-13 thomas *packfile_size += outlen;
1761 f9c2e8e5 2022-02-13 thomas got_object_raw_close(raw);
1762 f9c2e8e5 2022-02-13 thomas raw = NULL;
1763 f9c2e8e5 2022-02-13 thomas } else if (m->delta_buf) {
1764 f9c2e8e5 2022-02-13 thomas err = deltahdr(packfile_size, ctx, packfile, m);
1765 f9c2e8e5 2022-02-13 thomas if (err)
1766 f9c2e8e5 2022-02-13 thomas goto done;
1767 9249e7e3 2022-05-12 thomas err = hwrite(packfile, m->delta_buf,
1768 9249e7e3 2022-05-12 thomas m->delta_compressed_len, ctx);
1769 f9c2e8e5 2022-02-13 thomas if (err)
1770 f9c2e8e5 2022-02-13 thomas goto done;
1771 9249e7e3 2022-05-12 thomas *packfile_size += m->delta_compressed_len;
1772 f9c2e8e5 2022-02-13 thomas free(m->delta_buf);
1773 f9c2e8e5 2022-02-13 thomas m->delta_buf = NULL;
1774 d93dda9a 2022-05-12 thomas } else if (delta_cache_map) {
1775 d93dda9a 2022-05-12 thomas err = deltahdr(packfile_size, ctx, packfile, m);
1776 d93dda9a 2022-05-12 thomas if (err)
1777 d93dda9a 2022-05-12 thomas goto done;
1778 d93dda9a 2022-05-12 thomas err = hcopy_mmap(delta_cache_map, m->delta_offset,
1779 d93dda9a 2022-05-12 thomas delta_cache_size, packfile, m->delta_compressed_len,
1781 d93dda9a 2022-05-12 thomas if (err)
1782 d93dda9a 2022-05-12 thomas goto done;
1783 d93dda9a 2022-05-12 thomas *packfile_size += m->delta_compressed_len;
1784 f9c2e8e5 2022-02-13 thomas } else {
1785 f9c2e8e5 2022-02-13 thomas if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1786 f9c2e8e5 2022-02-13 thomas == -1) {
1787 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
1788 f9c2e8e5 2022-02-13 thomas goto done;
1790 f9c2e8e5 2022-02-13 thomas err = deltahdr(packfile_size, ctx, packfile, m);
1791 f9c2e8e5 2022-02-13 thomas if (err)
1792 f9c2e8e5 2022-02-13 thomas goto done;
1793 9249e7e3 2022-05-12 thomas err = hcopy(delta_cache, packfile,
1794 9249e7e3 2022-05-12 thomas m->delta_compressed_len, ctx);
1795 f9c2e8e5 2022-02-13 thomas if (err)
1796 f9c2e8e5 2022-02-13 thomas goto done;
1797 9249e7e3 2022-05-12 thomas *packfile_size += m->delta_compressed_len;
1800 f9c2e8e5 2022-02-13 thomas if (raw)
1801 f9c2e8e5 2022-02-13 thomas got_object_raw_close(raw);
1802 f9c2e8e5 2022-02-13 thomas return err;
1805 f9c2e8e5 2022-02-13 thomas static const struct got_error *
1806 b79280dd 2021-10-15 thomas genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1807 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **deltify, int ndeltify,
1808 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **reuse, int nreuse,
1809 85220b0e 2022-03-22 thomas int ncolored, int nfound, int ntrees, int nours,
1810 85220b0e 2022-03-22 thomas struct got_repository *repo,
1811 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1812 31ba2236 2022-01-05 thomas struct got_ratelimit *rl,
1813 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1815 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1817 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1818 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1819 e4d8ab47 2021-10-15 thomas char buf[32];
1820 e8f02263 2022-01-23 thomas size_t n;
1821 f9c2e8e5 2022-02-13 thomas off_t packfile_size = 0;
1822 ecf9545f 2021-10-15 thomas int outfd = -1;
1823 d93dda9a 2022-05-12 thomas int delta_cache_fd = -1;
1824 d93dda9a 2022-05-12 thomas uint8_t *delta_cache_map = NULL;
1825 d93dda9a 2022-05-12 thomas size_t delta_cache_size = 0;
1827 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1829 d93dda9a 2022-05-12 thomas #ifndef GOT_PACK_NO_MMAP
1830 d93dda9a 2022-05-12 thomas delta_cache_fd = dup(fileno(delta_cache));
1831 d93dda9a 2022-05-12 thomas if (delta_cache_fd != -1) {
1832 d93dda9a 2022-05-12 thomas struct stat sb;
1833 d93dda9a 2022-05-12 thomas if (fstat(delta_cache_fd, &sb) == -1) {
1834 d93dda9a 2022-05-12 thomas err = got_error_from_errno("fstat");
1835 d93dda9a 2022-05-12 thomas goto done;
1837 d93dda9a 2022-05-12 thomas if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1838 d93dda9a 2022-05-12 thomas delta_cache_map = mmap(NULL, sb.st_size,
1839 d93dda9a 2022-05-12 thomas PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1840 d93dda9a 2022-05-12 thomas if (delta_cache_map == MAP_FAILED) {
1841 d93dda9a 2022-05-12 thomas if (errno != ENOMEM) {
1842 d93dda9a 2022-05-12 thomas err = got_error_from_errno("mmap");
1843 d93dda9a 2022-05-12 thomas goto done;
1845 d93dda9a 2022-05-12 thomas delta_cache_map = NULL; /* fallback on stdio */
1847 d93dda9a 2022-05-12 thomas delta_cache_size = (size_t)sb.st_size;
1851 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1853 d93dda9a 2022-05-12 thomas goto done;
1854 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1855 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1857 e6bcace5 2021-06-22 stsp goto done;
1858 f9c2e8e5 2022-02-13 thomas putbe32(buf, ndeltify + nreuse);
1859 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1861 e6bcace5 2021-06-22 stsp goto done;
1863 f9c2e8e5 2022-02-13 thomas qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1864 f9c2e8e5 2022-02-13 thomas write_order_cmp);
1865 f9c2e8e5 2022-02-13 thomas for (i = 0; i < ndeltify; i++) {
1866 31ba2236 2022-01-05 thomas err = report_progress(progress_cb, progress_arg, rl,
1867 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, packfile_size, nours,
1868 85220b0e 2022-03-22 thomas ndeltify + nreuse, ndeltify + nreuse, i);
1869 31ba2236 2022-01-05 thomas if (err)
1870 31ba2236 2022-01-05 thomas goto done;
1871 f9c2e8e5 2022-02-13 thomas m = deltify[i];
1872 f9c2e8e5 2022-02-13 thomas err = write_packed_object(&packfile_size, packfile,
1873 d93dda9a 2022-05-12 thomas delta_cache, delta_cache_map, delta_cache_size,
1874 d93dda9a 2022-05-12 thomas m, &outfd, &ctx, repo);
1875 f9c2e8e5 2022-02-13 thomas if (err)
1876 f9c2e8e5 2022-02-13 thomas goto done;
1879 f9c2e8e5 2022-02-13 thomas qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1880 f9c2e8e5 2022-02-13 thomas reuse_write_order_cmp);
1881 f9c2e8e5 2022-02-13 thomas for (i = 0; i < nreuse; i++) {
1882 f9c2e8e5 2022-02-13 thomas err = report_progress(progress_cb, progress_arg, rl,
1883 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, packfile_size, nours,
1884 85220b0e 2022-03-22 thomas ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1885 f9c2e8e5 2022-02-13 thomas if (err)
1886 f9c2e8e5 2022-02-13 thomas goto done;
1887 f9c2e8e5 2022-02-13 thomas m = reuse[i];
1888 f9c2e8e5 2022-02-13 thomas err = write_packed_object(&packfile_size, packfile,
1889 d93dda9a 2022-05-12 thomas delta_cache, delta_cache_map, delta_cache_size,
1890 d93dda9a 2022-05-12 thomas m, &outfd, &ctx, repo);
1891 f9c2e8e5 2022-02-13 thomas if (err)
1892 f9c2e8e5 2022-02-13 thomas goto done;
1895 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1896 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1897 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1898 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1899 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1900 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1901 31ba2236 2022-01-05 thomas if (progress_cb) {
1902 85220b0e 2022-03-22 thomas err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1903 85220b0e 2022-03-22 thomas packfile_size, nours, ndeltify + nreuse,
1904 85220b0e 2022-03-22 thomas ndeltify + nreuse, ndeltify + nreuse);
1905 31ba2236 2022-01-05 thomas if (err)
1906 31ba2236 2022-01-05 thomas goto done;
1909 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
1910 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
1911 d93dda9a 2022-05-12 thomas if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1912 d93dda9a 2022-05-12 thomas err = got_error_from_errno("munmap");
1913 d93dda9a 2022-05-12 thomas if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1914 d93dda9a 2022-05-12 thomas err = got_error_from_errno("close");
1915 e6bcace5 2021-06-22 stsp return err;
1918 f9c2e8e5 2022-02-13 thomas static const struct got_error *
1919 f9c2e8e5 2022-02-13 thomas add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1921 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m = data;
1922 f9c2e8e5 2022-02-13 thomas struct got_pack_metavec *v = arg;
1924 f291ef1f 2022-05-12 thomas if (m->have_reused_delta)
1925 f291ef1f 2022-05-12 thomas return NULL;
1927 f9c2e8e5 2022-02-13 thomas return add_meta(m, v);
1930 e6bcace5 2021-06-22 stsp const struct got_error *
1931 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1932 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1933 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1934 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1935 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1936 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1938 e6bcace5 2021-06-22 stsp const struct got_error *err;
1939 f9c2e8e5 2022-02-13 thomas int delta_cache_fd = -1;
1940 b79280dd 2021-10-15 thomas FILE *delta_cache = NULL;
1941 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset;
1942 31ba2236 2022-01-05 thomas struct got_ratelimit rl;
1943 f9c2e8e5 2022-02-13 thomas struct got_pack_metavec deltify, reuse;
1944 85220b0e 2022-03-22 thomas int ncolored = 0, nfound = 0, ntrees = 0;
1945 f291ef1f 2022-05-12 thomas size_t ndeltify;
1947 f9c2e8e5 2022-02-13 thomas memset(&deltify, 0, sizeof(deltify));
1948 f9c2e8e5 2022-02-13 thomas memset(&reuse, 0, sizeof(reuse));
1950 31ba2236 2022-01-05 thomas got_ratelimit_init(&rl, 0, 500);
1952 f9c2e8e5 2022-02-13 thomas idset = got_object_idset_alloc();
1953 f9c2e8e5 2022-02-13 thomas if (idset == NULL)
1954 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("got_object_idset_alloc");
1956 85220b0e 2022-03-22 thomas err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1957 85220b0e 2022-03-22 thomas ntheirs, ours, nours, repo, loose_obj_only,
1958 85220b0e 2022-03-22 thomas progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1960 20c6bdb6 2022-05-19 thomas goto done;
1962 f9a643e8 2022-02-13 thomas if (progress_cb) {
1963 85220b0e 2022-03-22 thomas err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1964 85220b0e 2022-03-22 thomas 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1965 f9a643e8 2022-02-13 thomas if (err)
1966 f9a643e8 2022-02-13 thomas goto done;
1969 f9c2e8e5 2022-02-13 thomas if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1970 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1971 05118f5a 2021-06-22 stsp goto done;
1974 f9c2e8e5 2022-02-13 thomas delta_cache_fd = got_opentempfd();
1975 f9c2e8e5 2022-02-13 thomas if (delta_cache_fd == -1) {
1976 b79280dd 2021-10-15 thomas err = got_error_from_errno("got_opentemp");
1977 b79280dd 2021-10-15 thomas goto done;
1980 f9c2e8e5 2022-02-13 thomas reuse.metasz = 64;
1981 f9c2e8e5 2022-02-13 thomas reuse.meta = calloc(reuse.metasz,
1982 f9c2e8e5 2022-02-13 thomas sizeof(struct got_pack_meta *));
1983 f9c2e8e5 2022-02-13 thomas if (reuse.meta == NULL) {
1984 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("calloc");
1985 f9c2e8e5 2022-02-13 thomas goto done;
1988 85220b0e 2022-03-22 thomas err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1989 85220b0e 2022-03-22 thomas ntrees, nours, repo, progress_cb, progress_arg, &rl,
1990 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1991 f9c2e8e5 2022-02-13 thomas if (err)
1992 f9c2e8e5 2022-02-13 thomas goto done;
1994 f9c2e8e5 2022-02-13 thomas delta_cache = fdopen(delta_cache_fd, "a+");
1995 f9c2e8e5 2022-02-13 thomas if (delta_cache == NULL) {
1996 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fdopen");
1997 f9c2e8e5 2022-02-13 thomas goto done;
1999 f9c2e8e5 2022-02-13 thomas delta_cache_fd = -1;
2001 f9c2e8e5 2022-02-13 thomas if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
2002 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
2003 f9c2e8e5 2022-02-13 thomas goto done;
2006 f291ef1f 2022-05-12 thomas ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
2007 f291ef1f 2022-05-12 thomas if (ndeltify > 0) {
2008 f291ef1f 2022-05-12 thomas deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
2009 f291ef1f 2022-05-12 thomas if (deltify.meta == NULL) {
2010 f291ef1f 2022-05-12 thomas err = got_error_from_errno("calloc");
2011 f291ef1f 2022-05-12 thomas goto done;
2013 f291ef1f 2022-05-12 thomas deltify.metasz = ndeltify;
2015 f291ef1f 2022-05-12 thomas err = got_object_idset_for_each(idset, add_meta_idset_cb,
2016 f291ef1f 2022-05-12 thomas &deltify);
2018 f8a36e22 2021-08-26 stsp goto done;
2019 f291ef1f 2022-05-12 thomas if (deltify.nmeta > 0) {
2020 f291ef1f 2022-05-12 thomas err = pick_deltas(deltify.meta, deltify.nmeta,
2021 f291ef1f 2022-05-12 thomas ncolored, nfound, ntrees, nours, reuse.nmeta,
2022 f291ef1f 2022-05-12 thomas delta_cache, repo, progress_cb, progress_arg, &rl,
2023 f291ef1f 2022-05-12 thomas cancel_cb, cancel_arg);
2024 f291ef1f 2022-05-12 thomas if (err)
2025 f291ef1f 2022-05-12 thomas goto done;
2029 9249e7e3 2022-05-12 thomas if (fflush(delta_cache) == EOF) {
2030 9249e7e3 2022-05-12 thomas err = got_error_from_errno("fflush");
2031 9249e7e3 2022-05-12 thomas goto done;
2033 f9c2e8e5 2022-02-13 thomas err = genpack(packsha1, packfile, delta_cache, deltify.meta,
2034 85220b0e 2022-03-22 thomas deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
2035 85220b0e 2022-03-22 thomas nours, repo, progress_cb, progress_arg, &rl,
2036 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
2038 e6bcace5 2021-06-22 stsp goto done;
2040 f9c2e8e5 2022-02-13 thomas free_nmeta(deltify.meta, deltify.nmeta);
2041 f9c2e8e5 2022-02-13 thomas free_nmeta(reuse.meta, reuse.nmeta);
2042 f9c2e8e5 2022-02-13 thomas got_object_idset_free(idset);
2043 f9c2e8e5 2022-02-13 thomas if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2044 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("close");
2045 b79280dd 2021-10-15 thomas if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2046 b79280dd 2021-10-15 thomas err = got_error_from_errno("fclose");
2047 e6bcace5 2021-06-22 stsp return err;