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 e6bcace5 2021-06-22 stsp #include <sys/queue.h>
20 f8b19efd 2021-10-13 stsp #include <sys/tree.h>
21 08736cf9 2021-06-23 stsp #include <sys/uio.h>
22 e6bcace5 2021-06-22 stsp #include <sys/stat.h>
23 211cfef0 2022-01-05 stsp #include <sys/time.h>
24 e93fb944 2022-05-10 stsp #include <sys/mman.h>
26 e3f86256 2022-02-18 naddy #include <endian.h>
27 e93fb944 2022-05-10 stsp #include <errno.h>
28 08736cf9 2021-06-23 stsp #include <stdint.h>
29 05118f5a 2021-06-22 stsp #include <imsg.h>
30 756050ac 2022-07-19 op #include <inttypes.h>
31 e6bcace5 2021-06-22 stsp #include <stdio.h>
32 e6bcace5 2021-06-22 stsp #include <stdlib.h>
33 e6bcace5 2021-06-22 stsp #include <string.h>
34 e6bcace5 2021-06-22 stsp #include <sha1.h>
35 211cfef0 2022-01-05 stsp #include <time.h>
36 bfc73a47 2022-03-19 naddy #include <unistd.h>
37 e6bcace5 2021-06-22 stsp #include <limits.h>
38 e6bcace5 2021-06-22 stsp #include <zlib.h>
40 e6bcace5 2021-06-22 stsp #include "got_error.h"
41 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
42 e6bcace5 2021-06-22 stsp #include "got_object.h"
43 f8b19efd 2021-10-13 stsp #include "got_path.h"
44 05118f5a 2021-06-22 stsp #include "got_reference.h"
45 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
46 08347b73 2021-10-14 stsp #include "got_opentemp.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
50 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
51 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
52 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
53 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
54 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
55 17cfdba6 2022-05-20 op #include "got_lib_pack_create.h"
56 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
57 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
58 211cfef0 2022-01-05 stsp #include "got_lib_ratelimit.h"
59 2d9e6abf 2022-05-04 stsp #include "got_lib_inflate.h"
61 f8174ca5 2022-05-20 stsp #include "murmurhash2.h"
64 74881701 2021-10-15 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
68 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 70f8f24d 2022-04-14 stsp #ifndef nitems
72 70f8f24d 2022-04-14 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 e6bcace5 2021-06-22 stsp struct got_pack_meta {
76 e6bcace5 2021-06-22 stsp struct got_object_id id;
77 f8174ca5 2022-05-20 stsp uint32_t path_hash;
78 e6bcace5 2021-06-22 stsp int obj_type;
80 e6bcace5 2021-06-22 stsp time_t mtime;
82 e6bcace5 2021-06-22 stsp /* The best delta we picked */
83 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
84 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
85 2d9e6abf 2022-05-04 stsp unsigned char *delta_buf; /* if encoded in memory (compressed) */
86 2d9e6abf 2022-05-04 stsp off_t delta_offset; /* offset in delta cache file (compressed) */
87 5060d5a1 2022-01-10 stsp off_t delta_len; /* encoded delta length */
88 2d9e6abf 2022-05-04 stsp off_t delta_compressed_len; /* encoded+compressed delta length */
91 67fd6849 2022-02-13 stsp off_t reused_delta_offset; /* offset of delta in reused pack file */
92 67fd6849 2022-02-13 stsp struct got_object_id *base_obj_id;
94 e6bcace5 2021-06-22 stsp /* Only used for delta window */
95 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
97 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
101 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
102 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
104 e6bcace5 2021-06-22 stsp int metasz;
107 e6bcace5 2021-06-22 stsp static const struct got_error *
108 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
109 d6a28ffe 2022-05-20 op const char *path, int obj_type, time_t mtime, uint32_t seed)
111 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
113 e6bcace5 2021-06-22 stsp *new = NULL;
115 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
116 e6bcace5 2021-06-22 stsp if (m == NULL)
117 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
119 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
121 d6a28ffe 2022-05-20 op m->path_hash = murmurhash2(path, strlen(path), seed);
122 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
123 e6bcace5 2021-06-22 stsp m->mtime = mtime;
125 e6bcace5 2021-06-22 stsp return NULL;
128 e6bcace5 2021-06-22 stsp static void
129 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
131 e6bcace5 2021-06-22 stsp if (meta == NULL)
133 f8174ca5 2022-05-20 stsp meta->path_hash = 0;
134 5060d5a1 2022-01-10 stsp free(meta->delta_buf);
135 5060d5a1 2022-01-10 stsp meta->delta_buf = NULL;
136 67fd6849 2022-02-13 stsp free(meta->base_obj_id);
137 67fd6849 2022-02-13 stsp meta->base_obj_id = NULL;
138 411cbec1 2022-05-20 stsp meta->reused_delta_offset = 0;
141 e6bcace5 2021-06-22 stsp static void
142 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
146 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
147 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
148 e6bcace5 2021-06-22 stsp free(meta);
152 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
154 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
156 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
157 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
159 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
160 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
161 f8174ca5 2022-05-20 stsp if (a->path_hash < b->path_hash)
163 f8174ca5 2022-05-20 stsp if (a->path_hash > b->path_hash)
165 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
167 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
169 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
172 5060d5a1 2022-01-10 stsp static off_t
173 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
176 5060d5a1 2022-01-10 stsp off_t size = 32;
177 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
178 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
179 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
181 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
183 e6bcace5 2021-06-22 stsp return size;
186 5060d5a1 2022-01-10 stsp static const struct got_error *
187 5060d5a1 2022-01-10 stsp append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
191 5060d5a1 2022-01-10 stsp if (*len + nseg >= *sz) {
192 5060d5a1 2022-01-10 stsp while (*len + nseg >= *sz)
193 5060d5a1 2022-01-10 stsp *sz += *sz / 2;
194 5060d5a1 2022-01-10 stsp n = realloc(*p, *sz);
195 5060d5a1 2022-01-10 stsp if (n == NULL)
196 5060d5a1 2022-01-10 stsp return got_error_from_errno("realloc");
199 5060d5a1 2022-01-10 stsp memcpy(*p + *len, seg, nseg);
200 5060d5a1 2022-01-10 stsp *len += nseg;
201 5060d5a1 2022-01-10 stsp return NULL;
204 5060d5a1 2022-01-10 stsp static const struct got_error *
205 5060d5a1 2022-01-10 stsp encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
206 5060d5a1 2022-01-10 stsp struct got_delta_instruction *deltas, int ndeltas,
207 5060d5a1 2022-01-10 stsp off_t delta_size, off_t base_size)
209 5060d5a1 2022-01-10 stsp const struct got_error *err;
210 5060d5a1 2022-01-10 stsp unsigned char buf[16], *bp;
212 2d9e6abf 2022-05-04 stsp size_t len = 0, compressed_len;
213 2d9e6abf 2022-05-04 stsp off_t bufsize = delta_size;
215 5060d5a1 2022-01-10 stsp struct got_delta_instruction *d;
216 2d9e6abf 2022-05-04 stsp uint8_t *delta_buf;
218 2d9e6abf 2022-05-04 stsp delta_buf = malloc(bufsize);
219 2d9e6abf 2022-05-04 stsp if (delta_buf == NULL)
220 2d9e6abf 2022-05-04 stsp return got_error_from_errno("malloc");
222 5060d5a1 2022-01-10 stsp /* base object size */
223 5060d5a1 2022-01-10 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
224 5060d5a1 2022-01-10 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
225 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
226 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
227 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
228 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
230 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
234 5060d5a1 2022-01-10 stsp /* target object size */
235 5060d5a1 2022-01-10 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
236 5060d5a1 2022-01-10 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
237 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
238 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
239 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
240 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
242 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
246 5060d5a1 2022-01-10 stsp for (j = 0; j < ndeltas; j++) {
247 5060d5a1 2022-01-10 stsp d = &deltas[j];
248 5060d5a1 2022-01-10 stsp if (d->copy) {
249 5060d5a1 2022-01-10 stsp n = d->offset;
250 5060d5a1 2022-01-10 stsp bp = &buf[1];
251 5060d5a1 2022-01-10 stsp buf[0] = GOT_DELTA_BASE_COPY;
252 5060d5a1 2022-01-10 stsp for (i = 0; i < 4; i++) {
253 5060d5a1 2022-01-10 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
254 5060d5a1 2022-01-10 stsp buf[0] |= 1 << i;
255 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
257 5060d5a1 2022-01-10 stsp if (n == 0)
261 5060d5a1 2022-01-10 stsp n = d->len;
262 5060d5a1 2022-01-10 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
263 5060d5a1 2022-01-10 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
264 5060d5a1 2022-01-10 stsp for (i = 0; i < 3 && n > 0; i++) {
265 5060d5a1 2022-01-10 stsp buf[0] |= 1 << (i + 4);
266 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
270 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
271 5060d5a1 2022-01-10 stsp buf, bp - buf);
274 5060d5a1 2022-01-10 stsp } else if (o->f == NULL) {
276 5060d5a1 2022-01-10 stsp while (n != d->len) {
277 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
278 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
282 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
283 5060d5a1 2022-01-10 stsp o->data + o->hdrlen + d->offset + n,
287 5060d5a1 2022-01-10 stsp n += buf[0];
290 5060d5a1 2022-01-10 stsp char content[128];
292 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
293 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
297 5060d5a1 2022-01-10 stsp while (n != d->len) {
298 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
299 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
303 5060d5a1 2022-01-10 stsp r = fread(content, 1, buf[0], o->f);
304 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
305 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
308 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
309 5060d5a1 2022-01-10 stsp content, buf[0]);
312 5060d5a1 2022-01-10 stsp n += buf[0];
317 2d9e6abf 2022-05-04 stsp err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
318 2d9e6abf 2022-05-04 stsp NULL, NULL, delta_buf, 0, len);
322 5060d5a1 2022-01-10 stsp m->delta_len = len;
323 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
325 2d9e6abf 2022-05-04 stsp free(delta_buf);
326 2d9e6abf 2022-05-04 stsp return err;
329 74881701 2021-10-15 stsp static const struct got_error *
330 74881701 2021-10-15 stsp encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
331 74881701 2021-10-15 stsp struct got_delta_instruction *deltas, int ndeltas,
332 a319ca8c 2021-10-15 stsp off_t base_size, FILE *f)
334 2d9e6abf 2022-05-04 stsp const struct got_error *err;
335 a319ca8c 2021-10-15 stsp unsigned char buf[16], *bp;
338 2d9e6abf 2022-05-04 stsp struct got_deflate_buf zb;
339 a319ca8c 2021-10-15 stsp struct got_delta_instruction *d;
340 2d9e6abf 2022-05-04 stsp off_t delta_len = 0, compressed_len = 0;
342 2d9e6abf 2022-05-04 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
344 2d9e6abf 2022-05-04 stsp return err;
346 a319ca8c 2021-10-15 stsp /* base object size */
347 a319ca8c 2021-10-15 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
348 a319ca8c 2021-10-15 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
349 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
350 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
351 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
352 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
355 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
356 2d9e6abf 2022-05-04 stsp buf, 0, i, f, NULL);
359 2d9e6abf 2022-05-04 stsp delta_len += i;
361 a319ca8c 2021-10-15 stsp /* target object size */
362 a319ca8c 2021-10-15 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
363 a319ca8c 2021-10-15 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
364 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
365 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
366 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
367 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
370 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
371 2d9e6abf 2022-05-04 stsp buf, 0, i, f, NULL);
374 2d9e6abf 2022-05-04 stsp delta_len += i;
376 a319ca8c 2021-10-15 stsp for (j = 0; j < ndeltas; j++) {
377 a319ca8c 2021-10-15 stsp d = &deltas[j];
378 a319ca8c 2021-10-15 stsp if (d->copy) {
379 a319ca8c 2021-10-15 stsp n = d->offset;
380 a319ca8c 2021-10-15 stsp bp = &buf[1];
381 a319ca8c 2021-10-15 stsp buf[0] = GOT_DELTA_BASE_COPY;
382 a319ca8c 2021-10-15 stsp for (i = 0; i < 4; i++) {
383 a319ca8c 2021-10-15 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
384 a319ca8c 2021-10-15 stsp buf[0] |= 1 << i;
385 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
387 a319ca8c 2021-10-15 stsp if (n == 0)
390 a319ca8c 2021-10-15 stsp n = d->len;
391 a319ca8c 2021-10-15 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
392 a319ca8c 2021-10-15 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
393 a319ca8c 2021-10-15 stsp for (i = 0; i < 3 && n > 0; i++) {
394 a319ca8c 2021-10-15 stsp buf[0] |= 1 << (i + 4);
395 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
399 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
400 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, bp - buf, f, NULL);
403 2d9e6abf 2022-05-04 stsp delta_len += (bp - buf);
404 64a8571e 2022-01-07 stsp } else if (o->f == NULL) {
406 64a8571e 2022-01-07 stsp while (n != d->len) {
407 64a8571e 2022-01-07 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
408 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
409 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
412 2d9e6abf 2022-05-04 stsp delta_len++;
413 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
414 2d9e6abf 2022-05-04 stsp &compressed_len,
415 2d9e6abf 2022-05-04 stsp o->data + o->hdrlen + d->offset + n, 0,
416 2d9e6abf 2022-05-04 stsp buf[0], f, NULL);
419 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
420 64a8571e 2022-01-07 stsp n += buf[0];
423 a319ca8c 2021-10-15 stsp char content[128];
425 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
426 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
430 a319ca8c 2021-10-15 stsp while (n != d->len) {
431 a319ca8c 2021-10-15 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
432 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
433 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
436 2d9e6abf 2022-05-04 stsp delta_len++;
437 a319ca8c 2021-10-15 stsp r = fread(content, 1, buf[0], o->f);
438 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
439 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
442 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
443 2d9e6abf 2022-05-04 stsp &compressed_len, content, 0, buf[0], f,
447 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
448 a319ca8c 2021-10-15 stsp n += buf[0];
453 2d9e6abf 2022-05-04 stsp err = got_deflate_flush(&zb, f, NULL, &compressed_len);
457 2d9e6abf 2022-05-04 stsp /* sanity check */
458 2d9e6abf 2022-05-04 stsp if (compressed_len != ftello(f) - m->delta_offset) {
459 2d9e6abf 2022-05-04 stsp err = got_error(GOT_ERR_COMPRESSION);
463 2d9e6abf 2022-05-04 stsp m->delta_len = delta_len;
464 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
466 2d9e6abf 2022-05-04 stsp got_deflate_end(&zb);
467 2d9e6abf 2022-05-04 stsp return err;
470 211cfef0 2022-01-05 stsp static const struct got_error *
471 211cfef0 2022-01-05 stsp report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
472 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
473 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
474 b8af7c06 2022-03-15 stsp int nobj_written)
476 211cfef0 2022-01-05 stsp const struct got_error *err;
477 211cfef0 2022-01-05 stsp int elapsed;
479 211cfef0 2022-01-05 stsp if (progress_cb == NULL)
480 211cfef0 2022-01-05 stsp return NULL;
482 211cfef0 2022-01-05 stsp err = got_ratelimit_check(&elapsed, rl);
483 211cfef0 2022-01-05 stsp if (err || !elapsed)
484 211cfef0 2022-01-05 stsp return err;
486 b8af7c06 2022-03-15 stsp return progress_cb(progress_arg, ncolored, nfound, ntrees,
487 b8af7c06 2022-03-15 stsp packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
490 e6bcace5 2021-06-22 stsp static const struct got_error *
491 67fd6849 2022-02-13 stsp add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
493 67fd6849 2022-02-13 stsp if (v->nmeta == v->metasz){
494 67fd6849 2022-02-13 stsp size_t newsize = 2 * v->metasz;
495 67fd6849 2022-02-13 stsp struct got_pack_meta **new;
496 67fd6849 2022-02-13 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
497 67fd6849 2022-02-13 stsp if (new == NULL)
498 67fd6849 2022-02-13 stsp return got_error_from_errno("reallocarray");
499 67fd6849 2022-02-13 stsp v->meta = new;
500 67fd6849 2022-02-13 stsp v->metasz = newsize;
503 67fd6849 2022-02-13 stsp v->meta[v->nmeta++] = m;
504 67fd6849 2022-02-13 stsp return NULL;
507 67fd6849 2022-02-13 stsp static const struct got_error *
508 67fd6849 2022-02-13 stsp find_pack_for_reuse(struct got_packidx **best_packidx,
509 67fd6849 2022-02-13 stsp struct got_repository *repo)
511 9b576444 2022-03-14 stsp const struct got_error *err = NULL;
512 67fd6849 2022-02-13 stsp struct got_pathlist_entry *pe;
513 67fd6849 2022-02-13 stsp const char *best_packidx_path = NULL;
514 67fd6849 2022-02-13 stsp int nobj_max = 0;
516 67fd6849 2022-02-13 stsp *best_packidx = NULL;
518 9b576444 2022-03-14 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
519 67fd6849 2022-02-13 stsp const char *path_packidx = pe->path;
520 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
523 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
527 67fd6849 2022-02-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
528 67fd6849 2022-02-13 stsp if (nobj > nobj_max) {
529 67fd6849 2022-02-13 stsp best_packidx_path = path_packidx;
530 67fd6849 2022-02-13 stsp nobj_max = nobj;
534 67fd6849 2022-02-13 stsp if (best_packidx_path) {
535 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
539 67fd6849 2022-02-13 stsp return err;
542 fae7e038 2022-05-07 stsp struct send_id_arg {
543 fae7e038 2022-05-07 stsp struct imsgbuf *ibuf;
544 fae7e038 2022-05-07 stsp struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
545 fae7e038 2022-05-07 stsp size_t nids;
548 67fd6849 2022-02-13 stsp static const struct got_error *
549 fae7e038 2022-05-07 stsp send_id(struct got_object_id *id, void *data, void *arg)
551 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
552 fae7e038 2022-05-07 stsp struct send_id_arg *a = arg;
554 fae7e038 2022-05-07 stsp a->ids[a->nids++] = id;
556 fae7e038 2022-05-07 stsp if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
557 fae7e038 2022-05-07 stsp err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
559 67fd6849 2022-02-13 stsp return err;
560 fae7e038 2022-05-07 stsp a->nids = 0;
563 fae7e038 2022-05-07 stsp return NULL;
566 61af9b21 2022-06-28 stsp static const struct got_error *
567 61af9b21 2022-06-28 stsp send_idset(struct imsgbuf *ibuf, struct got_object_idset *idset)
569 61af9b21 2022-06-28 stsp const struct got_error *err;
570 61af9b21 2022-06-28 stsp struct send_id_arg sia;
572 61af9b21 2022-06-28 stsp memset(&sia, 0, sizeof(sia));
573 61af9b21 2022-06-28 stsp sia.ibuf = ibuf;
574 61af9b21 2022-06-28 stsp err = got_object_idset_for_each(idset, send_id, &sia);
576 61af9b21 2022-06-28 stsp return err;
578 61af9b21 2022-06-28 stsp if (sia.nids > 0) {
579 61af9b21 2022-06-28 stsp err = got_privsep_send_object_idlist(ibuf, sia.ids, sia.nids);
581 61af9b21 2022-06-28 stsp return err;
584 61af9b21 2022-06-28 stsp return got_privsep_send_object_idlist_done(ibuf);
588 fae7e038 2022-05-07 stsp static const struct got_error *
589 fae7e038 2022-05-07 stsp recv_reused_delta(struct got_imsg_reused_delta *delta,
590 fae7e038 2022-05-07 stsp struct got_object_idset *idset, struct got_pack_metavec *v)
592 fae7e038 2022-05-07 stsp struct got_pack_meta *m, *base;
594 fae7e038 2022-05-07 stsp if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
595 fae7e038 2022-05-07 stsp delta->delta_offset +
596 fae7e038 2022-05-07 stsp delta->delta_compressed_size < delta->delta_offset)
597 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_BAD_PACKFILE);
599 fae7e038 2022-05-07 stsp m = got_object_idset_get(idset, &delta->id);
600 fae7e038 2022-05-07 stsp if (m == NULL)
601 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_OBJ);
603 fae7e038 2022-05-07 stsp base = got_object_idset_get(idset, &delta->base_id);
604 fae7e038 2022-05-07 stsp if (base == NULL)
605 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_OBJ);
607 fae7e038 2022-05-07 stsp m->delta_len = delta->delta_size;
608 fae7e038 2022-05-07 stsp m->delta_compressed_len = delta->delta_compressed_size;
609 fae7e038 2022-05-07 stsp m->delta_offset = delta->delta_out_offset;
610 fae7e038 2022-05-07 stsp m->prev = base;
611 fae7e038 2022-05-07 stsp m->size = delta->result_size;
612 fae7e038 2022-05-07 stsp m->reused_delta_offset = delta->delta_offset;
613 fae7e038 2022-05-07 stsp m->base_obj_id = got_object_id_dup(&delta->base_id);
614 fae7e038 2022-05-07 stsp if (m->base_obj_id == NULL)
615 fae7e038 2022-05-07 stsp return got_error_from_errno("got_object_id_dup");
617 fae7e038 2022-05-07 stsp return add_meta(m, v);
620 3d589bee 2022-06-25 stsp static const struct got_error *
621 61af9b21 2022-06-28 stsp cache_pack_for_packidx(struct got_pack **pack, struct got_packidx *packidx,
622 61af9b21 2022-06-28 stsp struct got_repository *repo)
624 61af9b21 2022-06-28 stsp const struct got_error *err;
625 3d589bee 2022-06-25 stsp char *path_packfile = NULL;
627 3d589bee 2022-06-25 stsp err = got_packidx_get_packfile_path(&path_packfile,
628 3d589bee 2022-06-25 stsp packidx->path_packidx);
630 3d589bee 2022-06-25 stsp return err;
632 3d589bee 2022-06-25 stsp *pack = got_repo_get_cached_pack(repo, path_packfile);
633 3d589bee 2022-06-25 stsp if (*pack == NULL) {
634 3d589bee 2022-06-25 stsp err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
638 3d589bee 2022-06-25 stsp if ((*pack)->privsep_child == NULL) {
639 3d589bee 2022-06-25 stsp err = got_pack_start_privsep_child(*pack, packidx);
644 61af9b21 2022-06-28 stsp free(path_packfile);
645 61af9b21 2022-06-28 stsp return err;
648 61af9b21 2022-06-28 stsp static const struct got_error *
649 61af9b21 2022-06-28 stsp prepare_delta_reuse(struct got_pack *pack, struct got_packidx *packidx,
650 61af9b21 2022-06-28 stsp int delta_outfd, struct got_repository *repo)
652 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
654 61af9b21 2022-06-28 stsp if (!pack->child_has_delta_outfd) {
655 3d589bee 2022-06-25 stsp int outfd_child;
656 3d589bee 2022-06-25 stsp outfd_child = dup(delta_outfd);
657 3d589bee 2022-06-25 stsp if (outfd_child == -1) {
658 3d589bee 2022-06-25 stsp err = got_error_from_errno("dup");
661 3d589bee 2022-06-25 stsp err = got_privsep_send_raw_delta_outfd(
662 61af9b21 2022-06-28 stsp pack->privsep_child->ibuf, outfd_child);
665 61af9b21 2022-06-28 stsp pack->child_has_delta_outfd = 1;
668 61af9b21 2022-06-28 stsp err = got_privsep_send_delta_reuse_req(pack->privsep_child->ibuf);
670 3d589bee 2022-06-25 stsp return err;
674 67fd6849 2022-02-13 stsp static const struct got_error *
675 67fd6849 2022-02-13 stsp search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
676 b8af7c06 2022-03-15 stsp int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
677 b8af7c06 2022-03-15 stsp struct got_repository *repo,
678 67fd6849 2022-02-13 stsp got_pack_progress_cb progress_cb, void *progress_arg,
679 67fd6849 2022-02-13 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
681 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
682 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
683 67fd6849 2022-02-13 stsp struct got_pack *pack;
684 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
685 fae7e038 2022-05-07 stsp size_t ndeltas, i;
687 67fd6849 2022-02-13 stsp err = find_pack_for_reuse(&packidx, repo);
689 67fd6849 2022-02-13 stsp return err;
691 67fd6849 2022-02-13 stsp if (packidx == NULL)
692 67fd6849 2022-02-13 stsp return NULL;
694 61af9b21 2022-06-28 stsp err = cache_pack_for_packidx(&pack, packidx, repo);
696 67fd6849 2022-02-13 stsp return err;
698 61af9b21 2022-06-28 stsp err = prepare_delta_reuse(pack, packidx, delta_cache_fd, repo);
700 fae7e038 2022-05-07 stsp return err;
702 61af9b21 2022-06-28 stsp err = send_idset(pack->privsep_child->ibuf, idset);
704 fae7e038 2022-05-07 stsp return err;
707 fae7e038 2022-05-07 stsp int done = 0;
709 fae7e038 2022-05-07 stsp if (cancel_cb) {
710 fae7e038 2022-05-07 stsp err = (*cancel_cb)(cancel_arg);
715 fae7e038 2022-05-07 stsp err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
716 fae7e038 2022-05-07 stsp pack->privsep_child->ibuf);
717 fae7e038 2022-05-07 stsp if (err || done)
720 fae7e038 2022-05-07 stsp for (i = 0; i < ndeltas; i++) {
721 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta *delta = &deltas[i];
722 fae7e038 2022-05-07 stsp err = recv_reused_delta(delta, idset, v);
727 fae7e038 2022-05-07 stsp err = report_progress(progress_cb, progress_arg, rl,
728 fae7e038 2022-05-07 stsp ncolored, nfound, ntrees, 0L, ncommits,
729 fae7e038 2022-05-07 stsp got_object_idset_num_elements(idset), v->nmeta, 0);
734 67fd6849 2022-02-13 stsp return err;
737 67fd6849 2022-02-13 stsp static const struct got_error *
738 b8af7c06 2022-03-15 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
739 b8af7c06 2022-03-15 stsp int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
740 b8af7c06 2022-03-15 stsp struct got_repository *repo,
741 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
742 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
744 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
745 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
746 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
747 74881701 2021-10-15 stsp struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
748 5060d5a1 2022-01-10 stsp int i, j, ndeltas, best_ndeltas;
749 5060d5a1 2022-01-10 stsp off_t size, best_size;
750 4f4d853e 2021-10-24 stsp const int max_base_candidates = 3;
751 402a5ec1 2022-01-10 stsp size_t delta_memsize = 0;
752 adb4bbb2 2022-05-20 stsp const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
753 cc7a354a 2021-10-15 stsp int outfd = -1;
754 d6a28ffe 2022-05-20 op uint32_t delta_seed;
756 d6a28ffe 2022-05-20 op delta_seed = arc4random();
758 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
759 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
760 e6bcace5 2021-06-22 stsp if (cancel_cb) {
761 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
765 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
766 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
767 b8af7c06 2022-03-15 stsp nreused + i, 0);
770 e6bcace5 2021-06-22 stsp m = meta[i];
772 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
773 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
776 94dac27c 2021-10-15 stsp err = got_object_raw_open(&raw, &outfd, repo, &m->id);
779 600b755e 2021-10-14 stsp m->size = raw->size;
781 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
782 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&m->dtab, raw->data,
783 d6a28ffe 2022-05-20 op raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
785 64a8571e 2022-01-07 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
786 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed);
791 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
792 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
793 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
794 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
795 e6bcace5 2021-06-22 stsp n->dtab = NULL;
798 74881701 2021-10-15 stsp best_size = raw->size;
799 74881701 2021-10-15 stsp best_ndeltas = 0;
800 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
801 e6bcace5 2021-06-22 stsp if (cancel_cb) {
802 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
806 e6bcace5 2021-06-22 stsp base = meta[j];
807 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
808 22edbce7 2021-10-24 stsp if (base->nchain >= 128 ||
809 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
812 d3c116bf 2021-10-15 stsp err = got_object_raw_open(&base_raw, &outfd, repo,
813 94dac27c 2021-10-15 stsp &base->id);
817 64a8571e 2022-01-07 stsp if (raw->f == NULL && base_raw->f == NULL) {
818 64a8571e 2022-01-07 stsp err = got_deltify_mem_mem(&deltas, &ndeltas,
819 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
820 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
821 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
822 64a8571e 2022-01-07 stsp base_raw->hdrlen,
823 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
824 64a8571e 2022-01-07 stsp } else if (raw->f == NULL) {
825 64a8571e 2022-01-07 stsp err = got_deltify_mem_file(&deltas, &ndeltas,
826 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
827 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
828 64a8571e 2022-01-07 stsp base->dtab, base_raw->f,
829 64a8571e 2022-01-07 stsp base_raw->hdrlen,
830 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
831 64a8571e 2022-01-07 stsp } else if (base_raw->f == NULL) {
832 64a8571e 2022-01-07 stsp err = got_deltify_file_mem(&deltas, &ndeltas,
833 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
834 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
835 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
836 64a8571e 2022-01-07 stsp base_raw->hdrlen,
837 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
839 64a8571e 2022-01-07 stsp err = got_deltify(&deltas, &ndeltas,
840 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
841 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
842 64a8571e 2022-01-07 stsp base->dtab, base_raw->f, base_raw->hdrlen,
843 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
845 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
846 e6bcace5 2021-06-22 stsp base_raw = NULL;
850 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
851 74881701 2021-10-15 stsp if (size + 32 < best_size){
853 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
854 e6bcace5 2021-06-22 stsp * replace it.
856 74881701 2021-10-15 stsp best_size = size;
857 74881701 2021-10-15 stsp free(best_deltas);
858 74881701 2021-10-15 stsp best_deltas = deltas;
859 74881701 2021-10-15 stsp best_ndeltas = ndeltas;
860 74881701 2021-10-15 stsp deltas = NULL;
861 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
862 e6bcace5 2021-06-22 stsp m->prev = base;
863 e6bcace5 2021-06-22 stsp m->head = base->head;
864 e6bcace5 2021-06-22 stsp if (m->head == NULL)
865 e6bcace5 2021-06-22 stsp m->head = base;
867 e6bcace5 2021-06-22 stsp free(deltas);
868 e6bcace5 2021-06-22 stsp deltas = NULL;
869 e6bcace5 2021-06-22 stsp ndeltas = 0;
873 74881701 2021-10-15 stsp if (best_ndeltas > 0) {
874 402a5ec1 2022-01-10 stsp if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
875 402a5ec1 2022-01-10 stsp delta_memsize + best_size <= max_delta_memsize) {
876 402a5ec1 2022-01-10 stsp delta_memsize += best_size;
877 5060d5a1 2022-01-10 stsp err = encode_delta_in_mem(m, raw, best_deltas,
878 5060d5a1 2022-01-10 stsp best_ndeltas, best_size, m->prev->size);
880 5060d5a1 2022-01-10 stsp m->delta_offset = ftello(delta_cache);
881 5060d5a1 2022-01-10 stsp err = encode_delta(m, raw, best_deltas,
882 5060d5a1 2022-01-10 stsp best_ndeltas, m->prev->size, delta_cache);
884 74881701 2021-10-15 stsp free(best_deltas);
885 74881701 2021-10-15 stsp best_deltas = NULL;
886 74881701 2021-10-15 stsp best_ndeltas = 0;
891 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
892 e6bcace5 2021-06-22 stsp raw = NULL;
895 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
896 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
897 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
900 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
901 e6bcace5 2021-06-22 stsp if (base_raw)
902 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
903 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
904 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
905 74881701 2021-10-15 stsp free(deltas);
906 74881701 2021-10-15 stsp free(best_deltas);
907 e6bcace5 2021-06-22 stsp return err;
910 e6bcace5 2021-06-22 stsp static const struct got_error *
911 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
912 05118f5a 2021-06-22 stsp struct got_repository *repo)
914 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
915 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
918 05118f5a 2021-06-22 stsp *found = 0;
920 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
921 05118f5a 2021-06-22 stsp if (err == NULL)
922 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
923 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
924 05118f5a 2021-06-22 stsp err = NULL;
925 05118f5a 2021-06-22 stsp return err;
928 05118f5a 2021-06-22 stsp static const struct got_error *
929 67fd6849 2022-02-13 stsp add_object(int want_meta, struct got_object_idset *idset,
930 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
931 d6a28ffe 2022-05-20 op time_t mtime, uint32_t seed, int loose_obj_only,
932 d6a28ffe 2022-05-20 op struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
933 6863cbf9 2022-03-21 stsp got_pack_progress_cb progress_cb, void *progress_arg,
934 6863cbf9 2022-03-21 stsp struct got_ratelimit *rl)
936 e6bcace5 2021-06-22 stsp const struct got_error *err;
937 67fd6849 2022-02-13 stsp struct got_pack_meta *m = NULL;
939 05118f5a 2021-06-22 stsp if (loose_obj_only) {
940 05118f5a 2021-06-22 stsp int is_packed;
941 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
943 05118f5a 2021-06-22 stsp return err;
944 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
945 05118f5a 2021-06-22 stsp return NULL;
948 67fd6849 2022-02-13 stsp if (want_meta) {
949 d6a28ffe 2022-05-20 op err = alloc_meta(&m, id, path, obj_type, mtime, seed);
951 6863cbf9 2022-03-21 stsp return err;
953 6863cbf9 2022-03-21 stsp (*nfound)++;
954 6863cbf9 2022-03-21 stsp err = report_progress(progress_cb, progress_arg, rl,
955 6863cbf9 2022-03-21 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
957 bb6672b6 2022-04-14 tb clear_meta(m);
959 67fd6849 2022-02-13 stsp return err;
963 bb6672b6 2022-04-14 tb err = got_object_idset_add(idset, id, m);
965 bb6672b6 2022-04-14 tb clear_meta(m);
971 e6bcace5 2021-06-22 stsp static const struct got_error *
972 67fd6849 2022-02-13 stsp load_tree_entries(struct got_object_id_queue *ids, int want_meta,
973 2f8438b0 2022-05-04 stsp struct got_object_idset *idset, struct got_object_idset *idset_exclude,
974 0ab4c957 2022-06-13 stsp struct got_tree_object *tree,
975 d6a28ffe 2022-05-20 op const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
976 b8af7c06 2022-03-15 stsp int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
977 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
978 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
980 e6bcace5 2021-06-22 stsp const struct got_error *err;
981 e6bcace5 2021-06-22 stsp char *p = NULL;
984 b8af7c06 2022-03-15 stsp (*ntrees)++;
985 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
986 b8af7c06 2022-03-15 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
988 e6bcace5 2021-06-22 stsp return err;
990 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
991 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
992 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
993 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
995 e6bcace5 2021-06-22 stsp if (cancel_cb) {
996 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
1001 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
1002 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset, id) ||
1003 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1007 0ab4c957 2022-06-13 stsp * If got-read-pack is crawling trees for us then
1008 0ab4c957 2022-06-13 stsp * we are only here to collect blob IDs.
1010 0ab4c957 2022-06-13 stsp if (ids == NULL && S_ISDIR(mode))
1013 0ab4c957 2022-06-13 stsp if (asprintf(&p, "%s%s%s", dpath,
1014 0ab4c957 2022-06-13 stsp got_path_is_root_dir(dpath) ? "" : "/",
1015 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
1016 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
1020 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
1021 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1022 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1025 3e6ceea0 2022-05-20 stsp qid->data = p;
1027 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
1028 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
1029 2f8438b0 2022-05-04 stsp err = add_object(want_meta,
1030 2f8438b0 2022-05-04 stsp want_meta ? idset : idset_exclude, id, p,
1031 d6a28ffe 2022-05-20 op GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
1032 d6a28ffe 2022-05-20 op repo, ncolored, nfound, ntrees,
1033 6863cbf9 2022-03-21 stsp progress_cb, progress_arg, rl);
1045 e6bcace5 2021-06-22 stsp return err;
1048 e6bcace5 2021-06-22 stsp static const struct got_error *
1049 67fd6849 2022-02-13 stsp load_tree(int want_meta, struct got_object_idset *idset,
1050 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1051 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
1052 d6a28ffe 2022-05-20 op uint32_t seed, struct got_repository *repo, int loose_obj_only,
1053 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
1054 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1055 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1057 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1058 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
1059 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1060 0ab4c957 2022-06-13 stsp struct got_tree_object *tree = NULL;
1062 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, tree_id) ||
1063 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, tree_id))
1064 e6bcace5 2021-06-22 stsp return NULL;
1066 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
1068 3e6ceea0 2022-05-20 stsp return err;
1069 3e6ceea0 2022-05-20 stsp qid->data = strdup(dpath);
1070 3e6ceea0 2022-05-20 stsp if (qid->data == NULL) {
1071 3e6ceea0 2022-05-20 stsp err = got_error_from_errno("strdup");
1072 3e6ceea0 2022-05-20 stsp got_object_qid_free(qid);
1073 e6bcace5 2021-06-22 stsp return err;
1076 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
1077 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1079 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
1080 3e6ceea0 2022-05-20 stsp const char *path;
1081 e6bcace5 2021-06-22 stsp if (cancel_cb) {
1082 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
1087 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
1088 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
1089 3e6ceea0 2022-05-20 stsp path = qid->data;
1091 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, &qid->id) ||
1092 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, &qid->id)) {
1093 3e6ceea0 2022-05-20 stsp free(qid->data);
1094 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1098 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1099 3e6ceea0 2022-05-20 stsp &qid->id, path, GOT_OBJ_TYPE_TREE,
1100 d6a28ffe 2022-05-20 op mtime, seed, loose_obj_only, repo,
1101 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1102 e6bcace5 2021-06-22 stsp if (err) {
1103 3e6ceea0 2022-05-20 stsp free(qid->data);
1104 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1108 0ab4c957 2022-06-13 stsp err = got_object_open_as_tree(&tree, repo, &qid->id);
1109 0ab4c957 2022-06-13 stsp if (err) {
1110 0ab4c957 2022-06-13 stsp free(qid->data);
1111 0ab4c957 2022-06-13 stsp got_object_qid_free(qid);
1115 2f8438b0 2022-05-04 stsp err = load_tree_entries(&tree_ids, want_meta, idset,
1116 0ab4c957 2022-06-13 stsp idset_exclude, tree, path, mtime, seed, repo,
1117 0ab4c957 2022-06-13 stsp loose_obj_only, ncolored, nfound, ntrees,
1118 0ab4c957 2022-06-13 stsp progress_cb, progress_arg, rl,
1119 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1120 3e6ceea0 2022-05-20 stsp free(qid->data);
1121 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1125 0ab4c957 2022-06-13 stsp got_object_tree_close(tree);
1126 0ab4c957 2022-06-13 stsp tree = NULL;
1129 3e6ceea0 2022-05-20 stsp STAILQ_FOREACH(qid, &tree_ids, entry)
1130 3e6ceea0 2022-05-20 stsp free(qid->data);
1131 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
1133 0ab4c957 2022-06-13 stsp got_object_tree_close(tree);
1134 e6bcace5 2021-06-22 stsp return err;
1137 e6bcace5 2021-06-22 stsp static const struct got_error *
1138 67fd6849 2022-02-13 stsp load_commit(int want_meta, struct got_object_idset *idset,
1139 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1140 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1141 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1142 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1143 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1145 e6bcace5 2021-06-22 stsp const struct got_error *err;
1146 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1148 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
1149 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1150 e6bcace5 2021-06-22 stsp return NULL;
1152 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1153 05118f5a 2021-06-22 stsp int is_packed;
1154 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1156 05118f5a 2021-06-22 stsp return err;
1157 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1158 05118f5a 2021-06-22 stsp return NULL;
1161 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1163 e6bcace5 2021-06-22 stsp return err;
1165 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1166 2f8438b0 2022-05-04 stsp id, "", GOT_OBJ_TYPE_COMMIT,
1167 d6a28ffe 2022-05-20 op got_object_commit_get_committer_time(commit), seed,
1168 6863cbf9 2022-03-21 stsp loose_obj_only, repo,
1169 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1171 e6bcace5 2021-06-22 stsp goto done;
1173 2f8438b0 2022-05-04 stsp err = load_tree(want_meta, idset, idset_exclude,
1174 2f8438b0 2022-05-04 stsp got_object_commit_get_tree_id(commit),
1175 d6a28ffe 2022-05-20 op "", got_object_commit_get_committer_time(commit), seed,
1176 b8af7c06 2022-03-15 stsp repo, loose_obj_only, ncolored, nfound, ntrees,
1177 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1179 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1180 e6bcace5 2021-06-22 stsp return err;
1183 05118f5a 2021-06-22 stsp static const struct got_error *
1184 67fd6849 2022-02-13 stsp load_tag(int want_meta, struct got_object_idset *idset,
1185 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1186 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1187 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1188 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1189 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1191 05118f5a 2021-06-22 stsp const struct got_error *err;
1192 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
1194 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
1195 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1196 05118f5a 2021-06-22 stsp return NULL;
1198 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1199 05118f5a 2021-06-22 stsp int is_packed;
1200 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1202 05118f5a 2021-06-22 stsp return err;
1203 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1204 05118f5a 2021-06-22 stsp return NULL;
1207 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1209 05118f5a 2021-06-22 stsp return err;
1211 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1212 2f8438b0 2022-05-04 stsp id, "", GOT_OBJ_TYPE_TAG,
1213 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1214 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1216 05118f5a 2021-06-22 stsp goto done;
1218 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1219 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1220 2f8438b0 2022-05-04 stsp err = load_commit(want_meta, idset, idset_exclude,
1221 d6a28ffe 2022-05-20 op got_object_tag_get_object_id(tag), repo, seed,
1222 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1223 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1225 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1226 2f8438b0 2022-05-04 stsp err = load_tree(want_meta, idset, idset_exclude,
1227 67fd6849 2022-02-13 stsp got_object_tag_get_object_id(tag), "",
1228 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, repo,
1229 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1230 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1237 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1238 05118f5a 2021-06-22 stsp return err;
1241 e6bcace5 2021-06-22 stsp enum findtwixt_color {
1242 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
1243 e6bcace5 2021-06-22 stsp COLOR_DROP,
1244 70f8f24d 2022-04-14 stsp COLOR_SKIP,
1245 61af9b21 2022-06-28 stsp COLOR_MAX,
1248 e6bcace5 2021-06-22 stsp static const struct got_error *
1249 61af9b21 2022-06-28 stsp paint_commit(struct got_object_qid *qid, intptr_t color)
1251 61af9b21 2022-06-28 stsp if (color < 0 || color >= COLOR_MAX)
1252 70f8f24d 2022-04-14 stsp return got_error(GOT_ERR_RANGE);
1254 61af9b21 2022-06-28 stsp qid->data = (void *)color;
1255 e6bcace5 2021-06-22 stsp return NULL;
1258 e6bcace5 2021-06-22 stsp static const struct got_error *
1259 70f8f24d 2022-04-14 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1260 61af9b21 2022-06-28 stsp intptr_t color, struct got_repository *repo)
1262 70f8f24d 2022-04-14 stsp const struct got_error *err;
1263 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1265 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1267 e6bcace5 2021-06-22 stsp return err;
1269 70f8f24d 2022-04-14 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
1270 70f8f24d 2022-04-14 stsp return paint_commit(qid, color);
1273 e6bcace5 2021-06-22 stsp struct append_id_arg {
1274 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1276 70f8f24d 2022-04-14 stsp struct got_object_idset *drop;
1277 70f8f24d 2022-04-14 stsp struct got_object_idset *skip;
1280 e6bcace5 2021-06-22 stsp static const struct got_error *
1281 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1283 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1285 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(a->skip, id) ||
1286 70f8f24d 2022-04-14 stsp got_object_idset_contains(a->drop, id))
1287 70f8f24d 2022-04-14 stsp return NULL;
1289 70f8f24d 2022-04-14 stsp a->array[++a->idx] = got_object_id_dup(id);
1290 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1291 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1293 e6bcace5 2021-06-22 stsp return NULL;
1296 29e0594f 2022-04-09 stsp static const struct got_error *
1297 61af9b21 2022-06-28 stsp queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1298 29e0594f 2022-04-09 stsp struct got_object_id_queue *ids, struct got_repository *repo)
1300 29e0594f 2022-04-09 stsp const struct got_error *err;
1301 29e0594f 2022-04-09 stsp struct got_tag_object *tag = NULL;
1302 29e0594f 2022-04-09 stsp int obj_type;
1304 29e0594f 2022-04-09 stsp err = got_object_get_type(&obj_type, repo, id);
1306 29e0594f 2022-04-09 stsp return err;
1308 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1309 29e0594f 2022-04-09 stsp err = got_object_open_as_tag(&tag, repo, id);
1311 29e0594f 2022-04-09 stsp return err;
1312 29e0594f 2022-04-09 stsp obj_type = got_object_tag_get_object_type(tag);
1313 29e0594f 2022-04-09 stsp id = got_object_tag_get_object_id(tag);
1316 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1317 29e0594f 2022-04-09 stsp err = queue_commit_id(ids, id, color, repo);
1319 29e0594f 2022-04-09 stsp goto done;
1323 29e0594f 2022-04-09 stsp got_object_tag_close(tag);
1324 29e0594f 2022-04-09 stsp return err;
1327 61af9b21 2022-06-28 stsp struct recv_painted_commit_arg {
1328 61af9b21 2022-06-28 stsp int *ncolored;
1329 61af9b21 2022-06-28 stsp int *nqueued;
1330 61af9b21 2022-06-28 stsp int *nskip;
1331 61af9b21 2022-06-28 stsp struct got_object_id_queue *ids;
1332 61af9b21 2022-06-28 stsp struct got_object_idset *keep;
1333 61af9b21 2022-06-28 stsp struct got_object_idset *drop;
1334 61af9b21 2022-06-28 stsp struct got_object_idset *skip;
1335 61af9b21 2022-06-28 stsp got_pack_progress_cb progress_cb;
1336 61af9b21 2022-06-28 stsp void *progress_arg;
1337 61af9b21 2022-06-28 stsp struct got_ratelimit *rl;
1338 61af9b21 2022-06-28 stsp got_cancel_cb cancel_cb;
1339 61af9b21 2022-06-28 stsp void *cancel_arg;
1342 e6bcace5 2021-06-22 stsp static const struct got_error *
1343 61af9b21 2022-06-28 stsp recv_painted_commit(void *arg, struct got_object_id *id, intptr_t color)
1345 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
1346 61af9b21 2022-06-28 stsp struct recv_painted_commit_arg *a = arg;
1347 61af9b21 2022-06-28 stsp struct got_object_qid *qid, *tmp;
1349 61af9b21 2022-06-28 stsp if (a->cancel_cb) {
1350 61af9b21 2022-06-28 stsp err = a->cancel_cb(a->cancel_arg);
1352 61af9b21 2022-06-28 stsp return err;
1355 61af9b21 2022-06-28 stsp switch (color) {
1356 61af9b21 2022-06-28 stsp case COLOR_KEEP:
1357 61af9b21 2022-06-28 stsp err = got_object_idset_add(a->keep, id, NULL);
1359 61af9b21 2022-06-28 stsp return err;
1360 61af9b21 2022-06-28 stsp (*a->ncolored)++;
1362 61af9b21 2022-06-28 stsp case COLOR_DROP:
1363 61af9b21 2022-06-28 stsp err = got_object_idset_add(a->drop, id, NULL);
1365 61af9b21 2022-06-28 stsp return err;
1366 61af9b21 2022-06-28 stsp (*a->ncolored)++;
1368 61af9b21 2022-06-28 stsp case COLOR_SKIP:
1369 61af9b21 2022-06-28 stsp err = got_object_idset_add(a->skip, id, NULL);
1371 61af9b21 2022-06-28 stsp return err;
1374 61af9b21 2022-06-28 stsp /* should not happen */
1375 61af9b21 2022-06-28 stsp return got_error_fmt(GOT_ERR_NOT_IMPL,
1376 756050ac 2022-07-19 op "%s invalid commit color %"PRIdPTR, __func__, color);
1379 61af9b21 2022-06-28 stsp STAILQ_FOREACH_SAFE(qid, a->ids, entry, tmp) {
1380 61af9b21 2022-06-28 stsp if (got_object_id_cmp(&qid->id, id) != 0)
1382 61af9b21 2022-06-28 stsp STAILQ_REMOVE(a->ids, qid, got_object_qid, entry);
1383 61af9b21 2022-06-28 stsp color = (intptr_t)qid->data;
1384 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1385 61af9b21 2022-06-28 stsp (*a->nqueued)--;
1386 61af9b21 2022-06-28 stsp if (color == COLOR_SKIP)
1387 61af9b21 2022-06-28 stsp (*a->nskip)--;
1391 61af9b21 2022-06-28 stsp return report_progress(a->progress_cb, a->progress_arg, a->rl,
1392 61af9b21 2022-06-28 stsp *a->ncolored, 0, 0, 0L, 0, 0, 0, 0);
1395 61af9b21 2022-06-28 stsp static const struct got_error *
1396 61af9b21 2022-06-28 stsp paint_packed_commits(struct got_pack *pack, struct got_object_id *id,
1397 61af9b21 2022-06-28 stsp int idx, intptr_t color, int *ncolored, int *nqueued, int *nskip,
1398 61af9b21 2022-06-28 stsp struct got_object_id_queue *ids,
1399 61af9b21 2022-06-28 stsp struct got_object_idset *keep, struct got_object_idset *drop,
1400 61af9b21 2022-06-28 stsp struct got_object_idset *skip, struct got_repository *repo,
1401 61af9b21 2022-06-28 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1402 61af9b21 2022-06-28 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1404 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
1405 61af9b21 2022-06-28 stsp struct got_object_id_queue next_ids;
1406 61af9b21 2022-06-28 stsp struct got_object_qid *qid, *tmp;
1407 61af9b21 2022-06-28 stsp struct recv_painted_commit_arg arg;
1409 61af9b21 2022-06-28 stsp STAILQ_INIT(&next_ids);
1411 61af9b21 2022-06-28 stsp err = got_privsep_send_painting_request(pack->privsep_child->ibuf,
1412 61af9b21 2022-06-28 stsp idx, id, color);
1414 61af9b21 2022-06-28 stsp return err;
1416 61af9b21 2022-06-28 stsp arg.ncolored = ncolored;
1417 61af9b21 2022-06-28 stsp arg.nqueued = nqueued;
1418 61af9b21 2022-06-28 stsp arg.nskip = nskip;
1419 61af9b21 2022-06-28 stsp arg.ids = ids;
1420 61af9b21 2022-06-28 stsp arg.keep = keep;
1421 61af9b21 2022-06-28 stsp arg.drop = drop;
1422 61af9b21 2022-06-28 stsp arg.skip = skip;
1423 61af9b21 2022-06-28 stsp arg.progress_cb = progress_cb;
1424 61af9b21 2022-06-28 stsp arg.progress_arg = progress_arg;
1425 61af9b21 2022-06-28 stsp arg.rl = rl;
1426 61af9b21 2022-06-28 stsp arg.cancel_cb = cancel_cb;
1427 61af9b21 2022-06-28 stsp arg.cancel_arg = cancel_arg;
1428 61af9b21 2022-06-28 stsp err = got_privsep_recv_painted_commits(&next_ids,
1429 61af9b21 2022-06-28 stsp recv_painted_commit, &arg, pack->privsep_child->ibuf);
1431 61af9b21 2022-06-28 stsp return err;
1433 61af9b21 2022-06-28 stsp STAILQ_FOREACH_SAFE(qid, &next_ids, entry, tmp) {
1434 61af9b21 2022-06-28 stsp struct got_object_qid *old_id;
1435 61af9b21 2022-06-28 stsp intptr_t qcolor, ocolor;
1436 61af9b21 2022-06-28 stsp STAILQ_FOREACH(old_id, ids, entry) {
1437 61af9b21 2022-06-28 stsp if (got_object_id_cmp(&qid->id, &old_id->id))
1439 61af9b21 2022-06-28 stsp qcolor = (intptr_t)qid->data;
1440 61af9b21 2022-06-28 stsp ocolor = (intptr_t)old_id->data;
1441 61af9b21 2022-06-28 stsp STAILQ_REMOVE(&next_ids, qid, got_object_qid, entry);
1442 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1443 61af9b21 2022-06-28 stsp qid = NULL;
1444 61af9b21 2022-06-28 stsp if (qcolor != ocolor) {
1445 61af9b21 2022-06-28 stsp paint_commit(old_id, qcolor);
1446 61af9b21 2022-06-28 stsp if (ocolor == COLOR_SKIP)
1447 61af9b21 2022-06-28 stsp (*nskip)--;
1448 61af9b21 2022-06-28 stsp else if (qcolor == COLOR_SKIP)
1449 61af9b21 2022-06-28 stsp (*nskip)++;
1454 61af9b21 2022-06-28 stsp while (!STAILQ_EMPTY(&next_ids)) {
1455 61af9b21 2022-06-28 stsp qid = STAILQ_FIRST(&next_ids);
1456 61af9b21 2022-06-28 stsp STAILQ_REMOVE_HEAD(&next_ids, entry);
1457 61af9b21 2022-06-28 stsp paint_commit(qid, color);
1458 61af9b21 2022-06-28 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
1459 61af9b21 2022-06-28 stsp (*nqueued)++;
1460 61af9b21 2022-06-28 stsp if (color == COLOR_SKIP)
1461 61af9b21 2022-06-28 stsp (*nskip)++;
1464 61af9b21 2022-06-28 stsp return err;
1467 61af9b21 2022-06-28 stsp static const struct got_error *
1468 61af9b21 2022-06-28 stsp find_pack_for_commit_painting(struct got_packidx **best_packidx,
1469 61af9b21 2022-06-28 stsp struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1471 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
1472 61af9b21 2022-06-28 stsp struct got_pathlist_entry *pe;
1473 61af9b21 2022-06-28 stsp const char *best_packidx_path = NULL;
1474 61af9b21 2022-06-28 stsp int nobj_max = 0;
1475 61af9b21 2022-06-28 stsp int ncommits_max = 0;
1477 61af9b21 2022-06-28 stsp *best_packidx = NULL;
1480 61af9b21 2022-06-28 stsp * Find the largest pack which contains at least some of the
1481 61af9b21 2022-06-28 stsp * commits we are interested in.
1483 61af9b21 2022-06-28 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1484 61af9b21 2022-06-28 stsp const char *path_packidx = pe->path;
1485 61af9b21 2022-06-28 stsp struct got_packidx *packidx;
1486 61af9b21 2022-06-28 stsp int nobj, idx, ncommits = 0;
1487 61af9b21 2022-06-28 stsp struct got_object_qid *qid;
1489 61af9b21 2022-06-28 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
1493 61af9b21 2022-06-28 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1494 61af9b21 2022-06-28 stsp if (nobj <= nobj_max)
1497 61af9b21 2022-06-28 stsp STAILQ_FOREACH(qid, ids, entry) {
1498 61af9b21 2022-06-28 stsp idx = got_packidx_get_object_idx(packidx, &qid->id);
1499 61af9b21 2022-06-28 stsp if (idx != -1)
1500 61af9b21 2022-06-28 stsp ncommits++;
1502 61af9b21 2022-06-28 stsp if (ncommits > ncommits_max) {
1503 61af9b21 2022-06-28 stsp best_packidx_path = path_packidx;
1504 61af9b21 2022-06-28 stsp nobj_max = nobj;
1505 61af9b21 2022-06-28 stsp ncommits_max = ncommits;
1509 61af9b21 2022-06-28 stsp if (best_packidx_path && err == NULL) {
1510 61af9b21 2022-06-28 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
1514 61af9b21 2022-06-28 stsp return err;
1517 61af9b21 2022-06-28 stsp static const struct got_error *
1518 70f8f24d 2022-04-14 stsp paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1519 14dbbf48 2022-04-10 stsp struct got_object_idset *keep, struct got_object_idset *drop,
1520 70f8f24d 2022-04-14 stsp struct got_object_idset *skip, struct got_repository *repo,
1521 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1522 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1524 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1525 14dbbf48 2022-04-10 stsp struct got_commit_object *commit = NULL;
1526 61af9b21 2022-06-28 stsp struct got_packidx *packidx = NULL;
1527 61af9b21 2022-06-28 stsp struct got_pack *pack = NULL;
1528 70f8f24d 2022-04-14 stsp const struct got_object_id_queue *parents;
1529 61af9b21 2022-06-28 stsp struct got_object_qid *qid = NULL;
1530 70f8f24d 2022-04-14 stsp int nqueued = nids, nskip = 0;
1533 70f8f24d 2022-04-14 stsp while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1534 61af9b21 2022-06-28 stsp intptr_t color;
1536 57bc7b6d 2022-04-10 stsp if (cancel_cb) {
1537 57bc7b6d 2022-04-10 stsp err = cancel_cb(cancel_arg);
1542 14dbbf48 2022-04-10 stsp qid = STAILQ_FIRST(ids);
1543 70f8f24d 2022-04-14 stsp STAILQ_REMOVE_HEAD(ids, entry);
1544 70f8f24d 2022-04-14 stsp nqueued--;
1545 61af9b21 2022-06-28 stsp color = (intptr_t)qid->data;
1546 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1549 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(skip, &qid->id)) {
1550 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1551 61af9b21 2022-06-28 stsp qid = NULL;
1554 61af9b21 2022-06-28 stsp if (color == COLOR_KEEP &&
1555 61af9b21 2022-06-28 stsp got_object_idset_contains(keep, &qid->id)) {
1556 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1557 61af9b21 2022-06-28 stsp qid = NULL;
1560 61af9b21 2022-06-28 stsp if (color == COLOR_DROP &&
1561 61af9b21 2022-06-28 stsp got_object_idset_contains(drop, &qid->id)) {
1562 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1563 61af9b21 2022-06-28 stsp qid = NULL;
1567 61af9b21 2022-06-28 stsp /* Pinned pack may have moved to different cache slot. */
1568 61af9b21 2022-06-28 stsp pack = got_repo_get_pinned_pack(repo);
1570 61af9b21 2022-06-28 stsp if (packidx && pack) {
1571 61af9b21 2022-06-28 stsp idx = got_packidx_get_object_idx(packidx, &qid->id);
1572 61af9b21 2022-06-28 stsp if (idx != -1) {
1573 61af9b21 2022-06-28 stsp err = paint_packed_commits(pack, &qid->id,
1574 61af9b21 2022-06-28 stsp idx, color, ncolored, &nqueued, &nskip,
1575 61af9b21 2022-06-28 stsp ids, keep, drop, skip, repo,
1576 61af9b21 2022-06-28 stsp progress_cb, progress_arg, rl,
1577 61af9b21 2022-06-28 stsp cancel_cb, cancel_arg);
1580 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1581 61af9b21 2022-06-28 stsp qid = NULL;
1586 61af9b21 2022-06-28 stsp switch (color) {
1587 61af9b21 2022-06-28 stsp case COLOR_KEEP:
1588 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(drop, &qid->id)) {
1589 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1591 70f8f24d 2022-04-14 stsp goto done;
1593 70f8f24d 2022-04-14 stsp (*ncolored)++;
1594 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(keep, &qid->id, NULL);
1596 70f8f24d 2022-04-14 stsp goto done;
1598 70f8f24d 2022-04-14 stsp case COLOR_DROP:
1599 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(keep, &qid->id)) {
1600 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1602 70f8f24d 2022-04-14 stsp goto done;
1604 70f8f24d 2022-04-14 stsp (*ncolored)++;
1605 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(drop, &qid->id, NULL);
1607 70f8f24d 2022-04-14 stsp goto done;
1609 70f8f24d 2022-04-14 stsp case COLOR_SKIP:
1610 d7b5a0e8 2022-04-20 stsp if (!got_object_idset_contains(skip, &qid->id)) {
1611 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(skip, &qid->id,
1614 70f8f24d 2022-04-14 stsp goto done;
1618 70f8f24d 2022-04-14 stsp /* should not happen */
1619 70f8f24d 2022-04-14 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
1620 756050ac 2022-07-19 op "%s invalid commit color %"PRIdPTR, __func__,
1622 70f8f24d 2022-04-14 stsp goto done;
1625 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
1626 b8af7c06 2022-03-15 stsp *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1630 d7b5a0e8 2022-04-20 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
1634 70f8f24d 2022-04-14 stsp parents = got_object_commit_get_parent_ids(commit);
1635 70f8f24d 2022-04-14 stsp if (parents) {
1636 70f8f24d 2022-04-14 stsp struct got_object_qid *pid;
1637 61af9b21 2022-06-28 stsp color = (intptr_t)qid->data;
1638 70f8f24d 2022-04-14 stsp STAILQ_FOREACH(pid, parents, entry) {
1639 61af9b21 2022-06-28 stsp err = queue_commit_id(ids, &pid->id,
1640 61af9b21 2022-06-28 stsp color, repo);
1643 70f8f24d 2022-04-14 stsp nqueued++;
1644 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1649 61af9b21 2022-06-28 stsp if (pack == NULL && (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
1650 61af9b21 2022-06-28 stsp if (packidx == NULL) {
1651 61af9b21 2022-06-28 stsp err = find_pack_for_commit_painting(&packidx,
1652 61af9b21 2022-06-28 stsp ids, nqueued, repo);
1654 61af9b21 2022-06-28 stsp goto done;
1656 61af9b21 2022-06-28 stsp if (packidx != NULL) {
1657 61af9b21 2022-06-28 stsp err = cache_pack_for_packidx(&pack, packidx,
1660 61af9b21 2022-06-28 stsp goto done;
1661 61af9b21 2022-06-28 stsp err = got_privsep_init_commit_painting(
1662 61af9b21 2022-06-28 stsp pack->privsep_child->ibuf);
1664 61af9b21 2022-06-28 stsp goto done;
1665 61af9b21 2022-06-28 stsp err = send_idset(pack->privsep_child->ibuf,
1668 61af9b21 2022-06-28 stsp goto done;
1669 61af9b21 2022-06-28 stsp err = send_idset(pack->privsep_child->ibuf, drop);
1671 61af9b21 2022-06-28 stsp goto done;
1672 61af9b21 2022-06-28 stsp err = send_idset(pack->privsep_child->ibuf, skip);
1674 61af9b21 2022-06-28 stsp goto done;
1675 61af9b21 2022-06-28 stsp err = got_repo_pin_pack(repo, packidx, pack);
1677 61af9b21 2022-06-28 stsp goto done;
1681 70f8f24d 2022-04-14 stsp got_object_commit_close(commit);
1682 70f8f24d 2022-04-14 stsp commit = NULL;
1684 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1685 61af9b21 2022-06-28 stsp qid = NULL;
1688 61af9b21 2022-06-28 stsp if (pack) {
1689 61af9b21 2022-06-28 stsp const struct got_error *pack_err;
1690 61af9b21 2022-06-28 stsp pack_err = got_privsep_send_painting_commits_done(
1691 61af9b21 2022-06-28 stsp pack->privsep_child->ibuf);
1692 61af9b21 2022-06-28 stsp if (err == NULL)
1693 61af9b21 2022-06-28 stsp err = pack_err;
1695 14dbbf48 2022-04-10 stsp if (commit)
1696 14dbbf48 2022-04-10 stsp got_object_commit_close(commit);
1697 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1698 61af9b21 2022-06-28 stsp got_repo_unpin_pack(repo);
1699 14dbbf48 2022-04-10 stsp return err;
1702 14dbbf48 2022-04-10 stsp static const struct got_error *
1703 14dbbf48 2022-04-10 stsp findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1704 14dbbf48 2022-04-10 stsp struct got_object_id **head, int nhead,
1705 14dbbf48 2022-04-10 stsp struct got_object_id **tail, int ntail,
1706 14dbbf48 2022-04-10 stsp struct got_repository *repo,
1707 14dbbf48 2022-04-10 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1708 14dbbf48 2022-04-10 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1710 14dbbf48 2022-04-10 stsp const struct got_error *err = NULL;
1711 14dbbf48 2022-04-10 stsp struct got_object_id_queue ids;
1712 70f8f24d 2022-04-14 stsp struct got_object_idset *keep, *drop, *skip = NULL;
1713 14dbbf48 2022-04-10 stsp int i, nkeep;
1715 14dbbf48 2022-04-10 stsp STAILQ_INIT(&ids);
1716 14dbbf48 2022-04-10 stsp *res = NULL;
1717 14dbbf48 2022-04-10 stsp *nres = 0;
1718 14dbbf48 2022-04-10 stsp *ncolored = 0;
1720 14dbbf48 2022-04-10 stsp keep = got_object_idset_alloc();
1721 14dbbf48 2022-04-10 stsp if (keep == NULL)
1722 14dbbf48 2022-04-10 stsp return got_error_from_errno("got_object_idset_alloc");
1724 14dbbf48 2022-04-10 stsp drop = got_object_idset_alloc();
1725 14dbbf48 2022-04-10 stsp if (drop == NULL) {
1726 14dbbf48 2022-04-10 stsp err = got_error_from_errno("got_object_idset_alloc");
1727 14dbbf48 2022-04-10 stsp goto done;
1730 70f8f24d 2022-04-14 stsp skip = got_object_idset_alloc();
1731 70f8f24d 2022-04-14 stsp if (skip == NULL) {
1732 70f8f24d 2022-04-14 stsp err = got_error_from_errno("got_object_idset_alloc");
1733 70f8f24d 2022-04-14 stsp goto done;
1736 14dbbf48 2022-04-10 stsp for (i = 0; i < nhead; i++) {
1737 14dbbf48 2022-04-10 stsp struct got_object_id *id = head[i];
1738 14dbbf48 2022-04-10 stsp if (id == NULL)
1740 fbafdecf 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1742 14dbbf48 2022-04-10 stsp goto done;
1745 14dbbf48 2022-04-10 stsp for (i = 0; i < ntail; i++) {
1746 14dbbf48 2022-04-10 stsp struct got_object_id *id = tail[i];
1747 14dbbf48 2022-04-10 stsp if (id == NULL)
1749 14dbbf48 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1751 14dbbf48 2022-04-10 stsp goto done;
1754 70f8f24d 2022-04-14 stsp err = paint_commits(ncolored, &ids, nhead + ntail,
1755 70f8f24d 2022-04-14 stsp keep, drop, skip, repo, progress_cb, progress_arg, rl,
1756 70f8f24d 2022-04-14 stsp cancel_cb, cancel_arg);
1758 14dbbf48 2022-04-10 stsp goto done;
1760 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1761 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1762 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1763 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1764 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1765 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1766 e6bcace5 2021-06-22 stsp goto done;
1768 70f8f24d 2022-04-14 stsp arg.idx = -1;
1769 70f8f24d 2022-04-14 stsp arg.skip = skip;
1770 70f8f24d 2022-04-14 stsp arg.drop = drop;
1771 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1772 e6bcace5 2021-06-22 stsp if (err) {
1773 e6bcace5 2021-06-22 stsp free(arg.array);
1774 e6bcace5 2021-06-22 stsp goto done;
1776 e6bcace5 2021-06-22 stsp *res = arg.array;
1777 70f8f24d 2022-04-14 stsp *nres = arg.idx + 1;
1780 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1781 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1783 70f8f24d 2022-04-14 stsp got_object_idset_free(skip);
1784 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1785 0ab4c957 2022-06-13 stsp return err;
1788 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg {
1789 0ab4c957 2022-06-13 stsp /* output parameters: */
1790 0ab4c957 2022-06-13 stsp struct got_object_id *id;
1791 0ab4c957 2022-06-13 stsp char *dpath;
1792 0ab4c957 2022-06-13 stsp time_t mtime;
1794 0ab4c957 2022-06-13 stsp /* input parameters: */
1795 0ab4c957 2022-06-13 stsp uint32_t seed;
1796 0ab4c957 2022-06-13 stsp int want_meta;
1797 0ab4c957 2022-06-13 stsp struct got_object_idset *idset;
1798 0ab4c957 2022-06-13 stsp struct got_object_idset *idset_exclude;
1799 0ab4c957 2022-06-13 stsp int loose_obj_only;
1800 0ab4c957 2022-06-13 stsp int *ncolored;
1801 0ab4c957 2022-06-13 stsp int *nfound;
1802 0ab4c957 2022-06-13 stsp int *ntrees;
1803 0ab4c957 2022-06-13 stsp got_pack_progress_cb progress_cb;
1804 0ab4c957 2022-06-13 stsp void *progress_arg;
1805 0ab4c957 2022-06-13 stsp struct got_ratelimit *rl;
1806 0ab4c957 2022-06-13 stsp got_cancel_cb cancel_cb;
1807 0ab4c957 2022-06-13 stsp void *cancel_arg;
1810 0ab4c957 2022-06-13 stsp static const struct got_error *
1811 0ab4c957 2022-06-13 stsp load_packed_commit_id(void *arg, time_t mtime, struct got_object_id *id,
1812 0ab4c957 2022-06-13 stsp struct got_repository *repo)
1814 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg *a = arg;
1816 0ab4c957 2022-06-13 stsp if (got_object_idset_contains(a->idset, id) ||
1817 0ab4c957 2022-06-13 stsp got_object_idset_contains(a->idset_exclude, id))
1818 0ab4c957 2022-06-13 stsp return NULL;
1820 0ab4c957 2022-06-13 stsp return add_object(a->want_meta,
1821 0ab4c957 2022-06-13 stsp a->want_meta ? a->idset : a->idset_exclude,
1822 0ab4c957 2022-06-13 stsp id, "", GOT_OBJ_TYPE_COMMIT, mtime, a->seed, a->loose_obj_only,
1823 0ab4c957 2022-06-13 stsp repo, a->ncolored, a->nfound, a->ntrees,
1824 0ab4c957 2022-06-13 stsp a->progress_cb, a->progress_arg, a->rl);
1827 0ab4c957 2022-06-13 stsp static const struct got_error *
1828 0ab4c957 2022-06-13 stsp load_packed_tree_ids(void *arg, struct got_tree_object *tree, time_t mtime,
1829 0ab4c957 2022-06-13 stsp struct got_object_id *id, const char *dpath, struct got_repository *repo)
1831 0ab4c957 2022-06-13 stsp const struct got_error *err;
1832 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg *a = arg;
1833 0ab4c957 2022-06-13 stsp const char *relpath;
1836 0ab4c957 2022-06-13 stsp * When we receive a tree's ID and path but not the tree itself,
1837 0ab4c957 2022-06-13 stsp * this tree object was not found in the pack file. This is the
1838 0ab4c957 2022-06-13 stsp * last time we are being called for this optimized traversal.
1839 0ab4c957 2022-06-13 stsp * Return from here and switch to loading objects the slow way.
1841 0ab4c957 2022-06-13 stsp if (tree == NULL) {
1842 0ab4c957 2022-06-13 stsp free(a->id);
1843 0ab4c957 2022-06-13 stsp a->id = got_object_id_dup(id);
1844 0ab4c957 2022-06-13 stsp if (a->id == NULL) {
1845 0ab4c957 2022-06-13 stsp err = got_error_from_errno("got_object_id_dup");
1846 0ab4c957 2022-06-13 stsp free(a->dpath);
1847 0ab4c957 2022-06-13 stsp a->dpath = NULL;
1848 0ab4c957 2022-06-13 stsp return err;
1851 0ab4c957 2022-06-13 stsp free(a->dpath);
1852 0ab4c957 2022-06-13 stsp a->dpath = strdup(dpath);
1853 0ab4c957 2022-06-13 stsp if (a->dpath == NULL) {
1854 0ab4c957 2022-06-13 stsp err = got_error_from_errno("strdup");
1855 0ab4c957 2022-06-13 stsp free(a->id);
1856 0ab4c957 2022-06-13 stsp a->id = NULL;
1857 0ab4c957 2022-06-13 stsp return err;
1860 0ab4c957 2022-06-13 stsp a->mtime = mtime;
1861 0ab4c957 2022-06-13 stsp return NULL;
1864 0ab4c957 2022-06-13 stsp if (got_object_idset_contains(a->idset, id) ||
1865 0ab4c957 2022-06-13 stsp got_object_idset_contains(a->idset_exclude, id))
1866 0ab4c957 2022-06-13 stsp return NULL;
1868 0ab4c957 2022-06-13 stsp relpath = dpath;
1869 0ab4c957 2022-06-13 stsp while (relpath[0] == '/')
1870 0ab4c957 2022-06-13 stsp relpath++;
1872 0ab4c957 2022-06-13 stsp err = add_object(a->want_meta,
1873 0ab4c957 2022-06-13 stsp a->want_meta ? a->idset : a->idset_exclude,
1874 0ab4c957 2022-06-13 stsp id, relpath, GOT_OBJ_TYPE_TREE, mtime, a->seed,
1875 0ab4c957 2022-06-13 stsp a->loose_obj_only, repo, a->ncolored, a->nfound, a->ntrees,
1876 0ab4c957 2022-06-13 stsp a->progress_cb, a->progress_arg, a->rl);
1878 0ab4c957 2022-06-13 stsp return err;
1880 0ab4c957 2022-06-13 stsp return load_tree_entries(NULL, a->want_meta, a->idset,
1881 0ab4c957 2022-06-13 stsp a->idset_exclude, tree, dpath, mtime, a->seed, repo,
1882 0ab4c957 2022-06-13 stsp a->loose_obj_only, a->ncolored, a->nfound, a->ntrees,
1883 0ab4c957 2022-06-13 stsp a->progress_cb, a->progress_arg, a->rl,
1884 0ab4c957 2022-06-13 stsp a->cancel_cb, a->cancel_arg);
1887 0ab4c957 2022-06-13 stsp static const struct got_error *
1888 db9b9b1c 2022-06-14 stsp load_packed_object_ids(int *found_all_objects,
1889 db9b9b1c 2022-06-14 stsp struct got_object_id **ours, int nours,
1890 0ab4c957 2022-06-13 stsp struct got_object_id **theirs, int ntheirs,
1891 0ab4c957 2022-06-13 stsp int want_meta, uint32_t seed, struct got_object_idset *idset,
1892 0ab4c957 2022-06-13 stsp struct got_object_idset *idset_exclude, int loose_obj_only,
1893 0ab4c957 2022-06-13 stsp struct got_repository *repo, struct got_packidx *packidx,
1894 0ab4c957 2022-06-13 stsp int *ncolored, int *nfound, int *ntrees,
1895 0ab4c957 2022-06-13 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1896 0ab4c957 2022-06-13 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1898 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1899 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg lpa;
1901 0ab4c957 2022-06-13 stsp memset(&lpa, 0, sizeof(lpa));
1902 0ab4c957 2022-06-13 stsp lpa.seed = seed;
1903 0ab4c957 2022-06-13 stsp lpa.want_meta = want_meta;
1904 0ab4c957 2022-06-13 stsp lpa.idset = idset;
1905 0ab4c957 2022-06-13 stsp lpa.idset_exclude = idset_exclude;
1906 0ab4c957 2022-06-13 stsp lpa.loose_obj_only = loose_obj_only;
1907 0ab4c957 2022-06-13 stsp lpa.ncolored = ncolored;
1908 0ab4c957 2022-06-13 stsp lpa.nfound = nfound;
1909 0ab4c957 2022-06-13 stsp lpa.ntrees = ntrees;
1910 0ab4c957 2022-06-13 stsp lpa.progress_cb = progress_cb;
1911 0ab4c957 2022-06-13 stsp lpa.progress_arg = progress_arg;
1912 0ab4c957 2022-06-13 stsp lpa.rl = rl;
1913 0ab4c957 2022-06-13 stsp lpa.cancel_cb = cancel_cb;
1914 0ab4c957 2022-06-13 stsp lpa.cancel_arg = cancel_arg;
1916 0ab4c957 2022-06-13 stsp /* Attempt to load objects via got-read-pack, as far as possible. */
1917 db9b9b1c 2022-06-14 stsp err = got_object_enumerate(found_all_objects, load_packed_commit_id,
1918 0ab4c957 2022-06-13 stsp load_packed_tree_ids, &lpa, ours, nours, theirs, ntheirs,
1919 0ab4c957 2022-06-13 stsp packidx, repo);
1921 0ab4c957 2022-06-13 stsp return err;
1923 0ab4c957 2022-06-13 stsp if (lpa.id == NULL)
1924 0ab4c957 2022-06-13 stsp return NULL;
1927 0ab4c957 2022-06-13 stsp * An incomplete tree hierarchy was present in the pack file
1928 db9b9b1c 2022-06-14 stsp * and caused loading to be aborted.
1929 0ab4c957 2022-06-13 stsp * Continue loading trees the slow way.
1931 0ab4c957 2022-06-13 stsp err = load_tree(want_meta, idset, idset_exclude,
1932 0ab4c957 2022-06-13 stsp lpa.id, lpa.dpath, lpa.mtime, seed, repo, loose_obj_only,
1933 0ab4c957 2022-06-13 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1934 0ab4c957 2022-06-13 stsp cancel_cb, cancel_arg);
1935 0ab4c957 2022-06-13 stsp free(lpa.id);
1936 0ab4c957 2022-06-13 stsp free(lpa.dpath);
1937 e6bcace5 2021-06-22 stsp return err;
1940 e6bcace5 2021-06-22 stsp static const struct got_error *
1941 0ab4c957 2022-06-13 stsp find_pack_for_enumeration(struct got_packidx **best_packidx,
1942 0ab4c957 2022-06-13 stsp struct got_object_id **ids, int nids, struct got_repository *repo)
1944 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1945 0ab4c957 2022-06-13 stsp struct got_pathlist_entry *pe;
1946 0ab4c957 2022-06-13 stsp const char *best_packidx_path = NULL;
1947 0ab4c957 2022-06-13 stsp int nobj_max = 0;
1948 0ab4c957 2022-06-13 stsp int ncommits_max = 0;
1950 0ab4c957 2022-06-13 stsp *best_packidx = NULL;
1953 0ab4c957 2022-06-13 stsp * Find the largest pack which contains at least some of the
1954 0ab4c957 2022-06-13 stsp * commits and tags we are interested in.
1956 0ab4c957 2022-06-13 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1957 0ab4c957 2022-06-13 stsp const char *path_packidx = pe->path;
1958 0ab4c957 2022-06-13 stsp struct got_packidx *packidx;
1959 0ab4c957 2022-06-13 stsp int nobj, i, idx, ncommits = 0;
1961 0ab4c957 2022-06-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
1965 0ab4c957 2022-06-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1966 0ab4c957 2022-06-13 stsp if (nobj <= nobj_max)
1969 0ab4c957 2022-06-13 stsp for (i = 0; i < nids; i++) {
1970 0ab4c957 2022-06-13 stsp idx = got_packidx_get_object_idx(packidx, ids[i]);
1971 0ab4c957 2022-06-13 stsp if (idx != -1)
1972 0ab4c957 2022-06-13 stsp ncommits++;
1974 0ab4c957 2022-06-13 stsp if (ncommits > ncommits_max) {
1975 0ab4c957 2022-06-13 stsp best_packidx_path = path_packidx;
1976 0ab4c957 2022-06-13 stsp nobj_max = nobj;
1977 0ab4c957 2022-06-13 stsp ncommits_max = ncommits;
1981 eb7b30a1 2022-06-13 stsp if (best_packidx_path && err == NULL) {
1982 0ab4c957 2022-06-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
1986 0ab4c957 2022-06-13 stsp return err;
1989 0ab4c957 2022-06-13 stsp static const struct got_error *
1990 b8af7c06 2022-03-15 stsp load_object_ids(int *ncolored, int *nfound, int *ntrees,
1991 b8af7c06 2022-03-15 stsp struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1992 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1993 d6a28ffe 2022-05-20 op uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1994 d6a28ffe 2022-05-20 op void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1995 d6a28ffe 2022-05-20 op void *cancel_arg)
1997 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1998 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1999 0ab4c957 2022-06-13 stsp struct got_packidx *packidx = NULL;
2000 db9b9b1c 2022-06-14 stsp int i, nobj = 0, obj_type, found_all_objects = 0;
2001 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude;
2003 2f8438b0 2022-05-04 stsp idset_exclude = got_object_idset_alloc();
2004 2f8438b0 2022-05-04 stsp if (idset_exclude == NULL)
2005 2f8438b0 2022-05-04 stsp return got_error_from_errno("got_object_idset_alloc");
2007 b8af7c06 2022-03-15 stsp *ncolored = 0;
2008 b8af7c06 2022-03-15 stsp *nfound = 0;
2009 b8af7c06 2022-03-15 stsp *ntrees = 0;
2011 b8af7c06 2022-03-15 stsp err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
2012 b8af7c06 2022-03-15 stsp repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2014 e6bcace5 2021-06-22 stsp goto done;
2016 0ab4c957 2022-06-13 stsp err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
2018 0ab4c957 2022-06-13 stsp goto done;
2019 0ab4c957 2022-06-13 stsp if (packidx) {
2020 db9b9b1c 2022-06-14 stsp err = load_packed_object_ids(&found_all_objects,
2021 db9b9b1c 2022-06-14 stsp theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
2022 db9b9b1c 2022-06-14 stsp loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
2023 db9b9b1c 2022-06-14 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2025 0ab4c957 2022-06-13 stsp goto done;
2028 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
2029 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
2030 05118f5a 2021-06-22 stsp if (id == NULL)
2032 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
2034 05118f5a 2021-06-22 stsp return err;
2035 9d34261e 2022-04-07 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
2036 db9b9b1c 2022-06-14 stsp if (!found_all_objects) {
2037 db9b9b1c 2022-06-14 stsp err = load_commit(0, idset, idset_exclude,
2038 db9b9b1c 2022-06-14 stsp id, repo, seed, loose_obj_only,
2039 db9b9b1c 2022-06-14 stsp ncolored, nfound, ntrees,
2040 db9b9b1c 2022-06-14 stsp progress_cb, progress_arg, rl,
2041 db9b9b1c 2022-06-14 stsp cancel_cb, cancel_arg);
2043 db9b9b1c 2022-06-14 stsp goto done;
2045 9d34261e 2022-04-07 stsp } else if (obj_type == GOT_OBJ_TYPE_TAG) {
2046 2f8438b0 2022-05-04 stsp err = load_tag(0, idset, idset_exclude, id, repo,
2047 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
2048 9d34261e 2022-04-07 stsp progress_cb, progress_arg, rl,
2049 9d34261e 2022-04-07 stsp cancel_cb, cancel_arg);
2051 9d34261e 2022-04-07 stsp goto done;
2055 db9b9b1c 2022-06-14 stsp found_all_objects = 0;
2056 0ab4c957 2022-06-13 stsp err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
2058 0ab4c957 2022-06-13 stsp goto done;
2059 0ab4c957 2022-06-13 stsp if (packidx) {
2060 db9b9b1c 2022-06-14 stsp err = load_packed_object_ids(&found_all_objects, ids,
2061 db9b9b1c 2022-06-14 stsp nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
2062 db9b9b1c 2022-06-14 stsp loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
2063 0ab4c957 2022-06-13 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2065 0ab4c957 2022-06-13 stsp goto done;
2068 db9b9b1c 2022-06-14 stsp if (!found_all_objects) {
2069 db9b9b1c 2022-06-14 stsp for (i = 0; i < nobj; i++) {
2070 db9b9b1c 2022-06-14 stsp err = load_commit(1, idset, idset_exclude, ids[i],
2071 db9b9b1c 2022-06-14 stsp repo, seed, loose_obj_only, ncolored, nfound,
2072 db9b9b1c 2022-06-14 stsp ntrees, progress_cb, progress_arg, rl,
2073 db9b9b1c 2022-06-14 stsp cancel_cb, cancel_arg);
2075 db9b9b1c 2022-06-14 stsp goto done;
2079 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
2080 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
2081 67fd6849 2022-02-13 stsp struct got_pack_meta *m;
2082 05118f5a 2021-06-22 stsp if (id == NULL)
2084 67fd6849 2022-02-13 stsp m = got_object_idset_get(idset, id);
2085 67fd6849 2022-02-13 stsp if (m == NULL) {
2086 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
2088 07165b17 2021-07-01 stsp goto done;
2090 67fd6849 2022-02-13 stsp obj_type = m->obj_type;
2091 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
2093 2f8438b0 2022-05-04 stsp err = load_tag(1, idset, idset_exclude, id, repo,
2094 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
2095 2f8438b0 2022-05-04 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2097 e6bcace5 2021-06-22 stsp goto done;
2100 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
2101 e6bcace5 2021-06-22 stsp free(ids[i]);
2103 e6bcace5 2021-06-22 stsp free(ids);
2104 2f8438b0 2022-05-04 stsp got_object_idset_free(idset_exclude);
2105 e6bcace5 2021-06-22 stsp return err;
2108 336075a4 2022-06-25 op static const struct got_error *
2109 58e31a80 2022-06-27 op hwrite(FILE *f, const void *buf, off_t len, SHA1_CTX *ctx)
2113 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
2114 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
2115 e6bcace5 2021-06-22 stsp if (n != len)
2116 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
2117 2d9e6abf 2022-05-04 stsp return NULL;
2120 336075a4 2022-06-25 op static const struct got_error *
2121 2d9e6abf 2022-05-04 stsp hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
2123 2d9e6abf 2022-05-04 stsp unsigned char buf[65536];
2124 2d9e6abf 2022-05-04 stsp off_t remain = len;
2127 2d9e6abf 2022-05-04 stsp while (remain > 0) {
2128 2d9e6abf 2022-05-04 stsp size_t copylen = MIN(sizeof(buf), remain);
2129 2d9e6abf 2022-05-04 stsp n = fread(buf, 1, copylen, fsrc);
2130 2d9e6abf 2022-05-04 stsp if (n != copylen)
2131 2d9e6abf 2022-05-04 stsp return got_ferror(fsrc, GOT_ERR_IO);
2132 2d9e6abf 2022-05-04 stsp SHA1Update(ctx, buf, copylen);
2133 2d9e6abf 2022-05-04 stsp n = fwrite(buf, 1, copylen, fdst);
2134 2d9e6abf 2022-05-04 stsp if (n != copylen)
2135 2d9e6abf 2022-05-04 stsp return got_ferror(fdst, GOT_ERR_IO);
2136 2d9e6abf 2022-05-04 stsp remain -= copylen;
2139 e6bcace5 2021-06-22 stsp return NULL;
2142 336075a4 2022-06-25 op static const struct got_error *
2143 e93fb944 2022-05-10 stsp hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
2144 e93fb944 2022-05-10 stsp FILE *fdst, off_t len, SHA1_CTX *ctx)
2148 e93fb944 2022-05-10 stsp if (src_offset + len > src_size)
2149 e93fb944 2022-05-10 stsp return got_error(GOT_ERR_RANGE);
2151 e93fb944 2022-05-10 stsp SHA1Update(ctx, src + src_offset, len);
2152 e93fb944 2022-05-10 stsp n = fwrite(src + src_offset, 1, len, fdst);
2153 e93fb944 2022-05-10 stsp if (n != len)
2154 e93fb944 2022-05-10 stsp return got_ferror(fdst, GOT_ERR_IO);
2156 e93fb944 2022-05-10 stsp return NULL;
2159 e6bcace5 2021-06-22 stsp static void
2160 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
2162 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
2163 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
2164 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
2165 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
2168 e6bcace5 2021-06-22 stsp static int
2169 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
2171 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
2173 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
2174 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
2175 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
2176 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
2177 611e8e31 2022-05-01 stsp if (bhd->mtime < ahd->mtime)
2178 611e8e31 2022-05-01 stsp return -1;
2179 611e8e31 2022-05-01 stsp if (bhd->mtime > ahd->mtime)
2181 611e8e31 2022-05-01 stsp if (bhd < ahd)
2182 611e8e31 2022-05-01 stsp return -1;
2183 611e8e31 2022-05-01 stsp if (bhd > ahd)
2185 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
2186 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
2187 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
2188 611e8e31 2022-05-01 stsp return -1;
2189 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
2191 611e8e31 2022-05-01 stsp return got_object_id_cmp(&a->id, &b->id);
2194 67fd6849 2022-02-13 stsp static int
2195 67fd6849 2022-02-13 stsp reuse_write_order_cmp(const void *pa, const void *pb)
2197 67fd6849 2022-02-13 stsp struct got_pack_meta *a, *b;
2199 67fd6849 2022-02-13 stsp a = *(struct got_pack_meta **)pa;
2200 67fd6849 2022-02-13 stsp b = *(struct got_pack_meta **)pb;
2202 67fd6849 2022-02-13 stsp if (a->reused_delta_offset < b->reused_delta_offset)
2203 67fd6849 2022-02-13 stsp return -1;
2204 67fd6849 2022-02-13 stsp if (a->reused_delta_offset > b->reused_delta_offset)
2209 e6bcace5 2021-06-22 stsp static const struct got_error *
2210 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
2214 e6bcace5 2021-06-22 stsp *hdrlen = 0;
2216 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
2217 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
2218 e6bcace5 2021-06-22 stsp len >>= 4;
2219 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
2220 e6bcace5 2021-06-22 stsp if (i >= bufsize)
2221 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
2222 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
2223 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
2224 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
2227 e6bcace5 2021-06-22 stsp *hdrlen = i;
2228 e6bcace5 2021-06-22 stsp return NULL;
2231 e6bcace5 2021-06-22 stsp static int
2232 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
2235 e6bcace5 2021-06-22 stsp char rbuf[8];
2237 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
2238 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
2239 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
2240 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
2244 e6bcace5 2021-06-22 stsp while (i > 0)
2245 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
2249 e6bcace5 2021-06-22 stsp static const struct got_error *
2250 5060d5a1 2022-01-10 stsp deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
2251 67fd6849 2022-02-13 stsp struct got_pack_meta *m)
2253 5060d5a1 2022-01-10 stsp const struct got_error *err;
2254 5060d5a1 2022-01-10 stsp char buf[32];
2257 67fd6849 2022-02-13 stsp if (m->prev->off != 0) {
2258 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
2259 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
2261 5060d5a1 2022-01-10 stsp return err;
2262 5060d5a1 2022-01-10 stsp nh += packoff(buf + nh, m->off - m->prev->off);
2263 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
2265 5060d5a1 2022-01-10 stsp return err;
2266 5060d5a1 2022-01-10 stsp *packfile_size += nh;
2268 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
2269 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
2271 5060d5a1 2022-01-10 stsp return err;
2272 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
2274 5060d5a1 2022-01-10 stsp return err;
2275 5060d5a1 2022-01-10 stsp *packfile_size += nh;
2276 5060d5a1 2022-01-10 stsp err = hwrite(packfile, m->prev->id.sha1,
2277 5060d5a1 2022-01-10 stsp sizeof(m->prev->id.sha1), ctx);
2279 5060d5a1 2022-01-10 stsp return err;
2280 5060d5a1 2022-01-10 stsp *packfile_size += sizeof(m->prev->id.sha1);
2283 5060d5a1 2022-01-10 stsp return NULL;
2286 5060d5a1 2022-01-10 stsp static const struct got_error *
2287 67fd6849 2022-02-13 stsp write_packed_object(off_t *packfile_size, FILE *packfile,
2288 e93fb944 2022-05-10 stsp FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
2289 e93fb944 2022-05-10 stsp struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
2290 e93fb944 2022-05-10 stsp struct got_repository *repo)
2292 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
2293 67fd6849 2022-02-13 stsp struct got_deflate_checksum csum;
2294 67fd6849 2022-02-13 stsp char buf[32];
2296 67fd6849 2022-02-13 stsp struct got_raw_object *raw = NULL;
2297 67fd6849 2022-02-13 stsp off_t outlen;
2299 67fd6849 2022-02-13 stsp csum.output_sha1 = ctx;
2300 67fd6849 2022-02-13 stsp csum.output_crc = NULL;
2302 67fd6849 2022-02-13 stsp m->off = ftello(packfile);
2303 67fd6849 2022-02-13 stsp if (m->delta_len == 0) {
2304 67fd6849 2022-02-13 stsp err = got_object_raw_open(&raw, outfd, repo, &m->id);
2306 67fd6849 2022-02-13 stsp goto done;
2307 67fd6849 2022-02-13 stsp err = packhdr(&nh, buf, sizeof(buf),
2308 67fd6849 2022-02-13 stsp m->obj_type, raw->size);
2310 67fd6849 2022-02-13 stsp goto done;
2311 67fd6849 2022-02-13 stsp err = hwrite(packfile, buf, nh, ctx);
2313 67fd6849 2022-02-13 stsp goto done;
2314 67fd6849 2022-02-13 stsp *packfile_size += nh;
2315 67fd6849 2022-02-13 stsp if (raw->f == NULL) {
2316 67fd6849 2022-02-13 stsp err = got_deflate_to_file_mmap(&outlen,
2317 67fd6849 2022-02-13 stsp raw->data + raw->hdrlen, 0, raw->size,
2318 67fd6849 2022-02-13 stsp packfile, &csum);
2320 67fd6849 2022-02-13 stsp goto done;
2322 67fd6849 2022-02-13 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
2324 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
2325 67fd6849 2022-02-13 stsp goto done;
2327 67fd6849 2022-02-13 stsp err = got_deflate_to_file(&outlen, raw->f,
2328 67fd6849 2022-02-13 stsp raw->size, packfile, &csum);
2330 67fd6849 2022-02-13 stsp goto done;
2332 67fd6849 2022-02-13 stsp *packfile_size += outlen;
2333 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
2334 67fd6849 2022-02-13 stsp raw = NULL;
2335 67fd6849 2022-02-13 stsp } else if (m->delta_buf) {
2336 67fd6849 2022-02-13 stsp err = deltahdr(packfile_size, ctx, packfile, m);
2338 67fd6849 2022-02-13 stsp goto done;
2339 2d9e6abf 2022-05-04 stsp err = hwrite(packfile, m->delta_buf,
2340 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
2342 67fd6849 2022-02-13 stsp goto done;
2343 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
2344 67fd6849 2022-02-13 stsp free(m->delta_buf);
2345 67fd6849 2022-02-13 stsp m->delta_buf = NULL;
2346 e93fb944 2022-05-10 stsp } else if (delta_cache_map) {
2347 e93fb944 2022-05-10 stsp err = deltahdr(packfile_size, ctx, packfile, m);
2349 e93fb944 2022-05-10 stsp goto done;
2350 e93fb944 2022-05-10 stsp err = hcopy_mmap(delta_cache_map, m->delta_offset,
2351 e93fb944 2022-05-10 stsp delta_cache_size, packfile, m->delta_compressed_len,
2354 e93fb944 2022-05-10 stsp goto done;
2355 e93fb944 2022-05-10 stsp *packfile_size += m->delta_compressed_len;
2357 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
2359 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
2360 67fd6849 2022-02-13 stsp goto done;
2362 67fd6849 2022-02-13 stsp err = deltahdr(packfile_size, ctx, packfile, m);
2364 67fd6849 2022-02-13 stsp goto done;
2365 2d9e6abf 2022-05-04 stsp err = hcopy(delta_cache, packfile,
2366 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
2368 67fd6849 2022-02-13 stsp goto done;
2369 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
2373 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
2374 67fd6849 2022-02-13 stsp return err;
2377 67fd6849 2022-02-13 stsp static const struct got_error *
2378 74881701 2021-10-15 stsp genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
2379 67fd6849 2022-02-13 stsp struct got_pack_meta **deltify, int ndeltify,
2380 67fd6849 2022-02-13 stsp struct got_pack_meta **reuse, int nreuse,
2381 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees, int nours,
2382 b8af7c06 2022-03-15 stsp struct got_repository *repo,
2383 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
2384 211cfef0 2022-01-05 stsp struct got_ratelimit *rl,
2385 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2387 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
2389 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
2390 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
2391 08347b73 2021-10-14 stsp char buf[32];
2393 67fd6849 2022-02-13 stsp off_t packfile_size = 0;
2394 cc7a354a 2021-10-15 stsp int outfd = -1;
2395 e93fb944 2022-05-10 stsp int delta_cache_fd = -1;
2396 e93fb944 2022-05-10 stsp uint8_t *delta_cache_map = NULL;
2397 e93fb944 2022-05-10 stsp size_t delta_cache_size = 0;
2399 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
2401 e93fb944 2022-05-10 stsp #ifndef GOT_PACK_NO_MMAP
2402 e93fb944 2022-05-10 stsp delta_cache_fd = dup(fileno(delta_cache));
2403 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1) {
2404 e93fb944 2022-05-10 stsp struct stat sb;
2405 e93fb944 2022-05-10 stsp if (fstat(delta_cache_fd, &sb) == -1) {
2406 e93fb944 2022-05-10 stsp err = got_error_from_errno("fstat");
2407 e93fb944 2022-05-10 stsp goto done;
2409 e93fb944 2022-05-10 stsp if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
2410 e93fb944 2022-05-10 stsp delta_cache_map = mmap(NULL, sb.st_size,
2411 e93fb944 2022-05-10 stsp PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
2412 e93fb944 2022-05-10 stsp if (delta_cache_map == MAP_FAILED) {
2413 e93fb944 2022-05-10 stsp if (errno != ENOMEM) {
2414 e93fb944 2022-05-10 stsp err = got_error_from_errno("mmap");
2415 e93fb944 2022-05-10 stsp goto done;
2417 e93fb944 2022-05-10 stsp delta_cache_map = NULL; /* fallback on stdio */
2419 e93fb944 2022-05-10 stsp delta_cache_size = (size_t)sb.st_size;
2423 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
2425 e93fb944 2022-05-10 stsp goto done;
2426 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
2427 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
2429 e6bcace5 2021-06-22 stsp goto done;
2430 67fd6849 2022-02-13 stsp putbe32(buf, ndeltify + nreuse);
2431 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
2433 e6bcace5 2021-06-22 stsp goto done;
2435 67fd6849 2022-02-13 stsp qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
2436 67fd6849 2022-02-13 stsp write_order_cmp);
2437 67fd6849 2022-02-13 stsp for (i = 0; i < ndeltify; i++) {
2438 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
2439 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
2440 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, i);
2442 211cfef0 2022-01-05 stsp goto done;
2443 67fd6849 2022-02-13 stsp m = deltify[i];
2444 67fd6849 2022-02-13 stsp err = write_packed_object(&packfile_size, packfile,
2445 e93fb944 2022-05-10 stsp delta_cache, delta_cache_map, delta_cache_size,
2446 e93fb944 2022-05-10 stsp m, &outfd, &ctx, repo);
2448 67fd6849 2022-02-13 stsp goto done;
2451 67fd6849 2022-02-13 stsp qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
2452 67fd6849 2022-02-13 stsp reuse_write_order_cmp);
2453 67fd6849 2022-02-13 stsp for (i = 0; i < nreuse; i++) {
2454 67fd6849 2022-02-13 stsp err = report_progress(progress_cb, progress_arg, rl,
2455 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
2456 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
2458 67fd6849 2022-02-13 stsp goto done;
2459 67fd6849 2022-02-13 stsp m = reuse[i];
2460 67fd6849 2022-02-13 stsp err = write_packed_object(&packfile_size, packfile,
2461 e93fb944 2022-05-10 stsp delta_cache, delta_cache_map, delta_cache_size,
2462 e93fb944 2022-05-10 stsp m, &outfd, &ctx, repo);
2464 67fd6849 2022-02-13 stsp goto done;
2467 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
2468 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
2469 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
2470 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
2471 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
2472 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
2473 211cfef0 2022-01-05 stsp if (progress_cb) {
2474 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2475 b8af7c06 2022-03-15 stsp packfile_size, nours, ndeltify + nreuse,
2476 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse);
2478 211cfef0 2022-01-05 stsp goto done;
2481 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
2482 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
2483 e93fb944 2022-05-10 stsp if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
2484 e93fb944 2022-05-10 stsp err = got_error_from_errno("munmap");
2485 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2486 e93fb944 2022-05-10 stsp err = got_error_from_errno("close");
2487 e6bcace5 2021-06-22 stsp return err;
2490 67fd6849 2022-02-13 stsp static const struct got_error *
2491 67fd6849 2022-02-13 stsp add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
2493 67fd6849 2022-02-13 stsp struct got_pack_meta *m = data;
2494 67fd6849 2022-02-13 stsp struct got_pack_metavec *v = arg;
2496 411cbec1 2022-05-20 stsp if (m->reused_delta_offset != 0)
2497 f5e78e05 2022-05-04 stsp return NULL;
2499 67fd6849 2022-02-13 stsp return add_meta(m, v);
2502 e6bcace5 2021-06-22 stsp const struct got_error *
2503 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
2504 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
2505 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
2506 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
2507 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
2508 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2510 e6bcace5 2021-06-22 stsp const struct got_error *err;
2511 67fd6849 2022-02-13 stsp int delta_cache_fd = -1;
2512 74881701 2021-10-15 stsp FILE *delta_cache = NULL;
2513 67fd6849 2022-02-13 stsp struct got_object_idset *idset;
2514 211cfef0 2022-01-05 stsp struct got_ratelimit rl;
2515 67fd6849 2022-02-13 stsp struct got_pack_metavec deltify, reuse;
2516 b8af7c06 2022-03-15 stsp int ncolored = 0, nfound = 0, ntrees = 0;
2517 f5e78e05 2022-05-04 stsp size_t ndeltify;
2518 d6a28ffe 2022-05-20 op uint32_t seed;
2520 d6a28ffe 2022-05-20 op seed = arc4random();
2522 67fd6849 2022-02-13 stsp memset(&deltify, 0, sizeof(deltify));
2523 67fd6849 2022-02-13 stsp memset(&reuse, 0, sizeof(reuse));
2525 211cfef0 2022-01-05 stsp got_ratelimit_init(&rl, 0, 500);
2527 67fd6849 2022-02-13 stsp idset = got_object_idset_alloc();
2528 67fd6849 2022-02-13 stsp if (idset == NULL)
2529 67fd6849 2022-02-13 stsp return got_error_from_errno("got_object_idset_alloc");
2531 b8af7c06 2022-03-15 stsp err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
2532 d6a28ffe 2022-05-20 op ntheirs, ours, nours, repo, seed, loose_obj_only,
2533 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
2535 17259bfa 2022-05-19 stsp goto done;
2537 28526235 2022-02-13 stsp if (progress_cb) {
2538 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2539 b8af7c06 2022-03-15 stsp 0L, nours, got_object_idset_num_elements(idset), 0, 0);
2541 28526235 2022-02-13 stsp goto done;
2544 67fd6849 2022-02-13 stsp if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
2545 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
2546 05118f5a 2021-06-22 stsp goto done;
2549 67fd6849 2022-02-13 stsp delta_cache_fd = got_opentempfd();
2550 67fd6849 2022-02-13 stsp if (delta_cache_fd == -1) {
2551 74881701 2021-10-15 stsp err = got_error_from_errno("got_opentemp");
2552 74881701 2021-10-15 stsp goto done;
2555 67fd6849 2022-02-13 stsp reuse.metasz = 64;
2556 67fd6849 2022-02-13 stsp reuse.meta = calloc(reuse.metasz,
2557 67fd6849 2022-02-13 stsp sizeof(struct got_pack_meta *));
2558 67fd6849 2022-02-13 stsp if (reuse.meta == NULL) {
2559 67fd6849 2022-02-13 stsp err = got_error_from_errno("calloc");
2560 67fd6849 2022-02-13 stsp goto done;
2563 b8af7c06 2022-03-15 stsp err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
2564 b8af7c06 2022-03-15 stsp ntrees, nours, repo, progress_cb, progress_arg, &rl,
2565 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
2567 67fd6849 2022-02-13 stsp goto done;
2569 67fd6849 2022-02-13 stsp delta_cache = fdopen(delta_cache_fd, "a+");
2570 67fd6849 2022-02-13 stsp if (delta_cache == NULL) {
2571 67fd6849 2022-02-13 stsp err = got_error_from_errno("fdopen");
2572 67fd6849 2022-02-13 stsp goto done;
2574 67fd6849 2022-02-13 stsp delta_cache_fd = -1;
2576 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
2577 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
2578 67fd6849 2022-02-13 stsp goto done;
2581 f5e78e05 2022-05-04 stsp ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
2582 f5e78e05 2022-05-04 stsp if (ndeltify > 0) {
2583 f5e78e05 2022-05-04 stsp deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
2584 f5e78e05 2022-05-04 stsp if (deltify.meta == NULL) {
2585 f5e78e05 2022-05-04 stsp err = got_error_from_errno("calloc");
2586 f5e78e05 2022-05-04 stsp goto done;
2588 f5e78e05 2022-05-04 stsp deltify.metasz = ndeltify;
2590 f5e78e05 2022-05-04 stsp err = got_object_idset_for_each(idset, add_meta_idset_cb,
2591 f5e78e05 2022-05-04 stsp &deltify);
2593 f8a36e22 2021-08-26 stsp goto done;
2594 f5e78e05 2022-05-04 stsp if (deltify.nmeta > 0) {
2595 f5e78e05 2022-05-04 stsp err = pick_deltas(deltify.meta, deltify.nmeta,
2596 f5e78e05 2022-05-04 stsp ncolored, nfound, ntrees, nours, reuse.nmeta,
2597 f5e78e05 2022-05-04 stsp delta_cache, repo, progress_cb, progress_arg, &rl,
2598 f5e78e05 2022-05-04 stsp cancel_cb, cancel_arg);
2600 f5e78e05 2022-05-04 stsp goto done;
2604 2d9e6abf 2022-05-04 stsp if (fflush(delta_cache) == EOF) {
2605 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fflush");
2606 2d9e6abf 2022-05-04 stsp goto done;
2608 67fd6849 2022-02-13 stsp err = genpack(packsha1, packfile, delta_cache, deltify.meta,
2609 b8af7c06 2022-03-15 stsp deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
2610 b8af7c06 2022-03-15 stsp nours, repo, progress_cb, progress_arg, &rl,
2611 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
2613 e6bcace5 2021-06-22 stsp goto done;
2615 67fd6849 2022-02-13 stsp free_nmeta(deltify.meta, deltify.nmeta);
2616 67fd6849 2022-02-13 stsp free_nmeta(reuse.meta, reuse.nmeta);
2617 67fd6849 2022-02-13 stsp got_object_idset_free(idset);
2618 67fd6849 2022-02-13 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2619 67fd6849 2022-02-13 stsp err = got_error_from_errno("close");
2620 74881701 2021-10-15 stsp if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2621 74881701 2021-10-15 stsp err = got_error_from_errno("fclose");
2622 e6bcace5 2021-06-22 stsp return err;