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 13b2bc37 2022-10-23 stsp #include <poll.h>
32 e6bcace5 2021-06-22 stsp #include <stdio.h>
33 e6bcace5 2021-06-22 stsp #include <stdlib.h>
34 e6bcace5 2021-06-22 stsp #include <string.h>
35 e6bcace5 2021-06-22 stsp #include <sha1.h>
36 5822e79e 2023-02-23 op #include <sha2.h>
37 211cfef0 2022-01-05 stsp #include <time.h>
38 bfc73a47 2022-03-19 naddy #include <unistd.h>
39 e6bcace5 2021-06-22 stsp #include <limits.h>
40 e6bcace5 2021-06-22 stsp #include <zlib.h>
42 e6bcace5 2021-06-22 stsp #include "got_error.h"
43 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
44 e6bcace5 2021-06-22 stsp #include "got_object.h"
45 f8b19efd 2021-10-13 stsp #include "got_path.h"
46 05118f5a 2021-06-22 stsp #include "got_reference.h"
47 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
50 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
51 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
52 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
53 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
54 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
55 cae60ab8 2022-10-18 stsp #include "got_lib_ratelimit.h"
56 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
57 17cfdba6 2022-05-20 op #include "got_lib_pack_create.h"
58 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
59 2d9e6abf 2022-05-04 stsp #include "got_lib_inflate.h"
60 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
62 f8174ca5 2022-05-20 stsp #include "murmurhash2.h"
65 74881701 2021-10-15 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
69 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
72 70f8f24d 2022-04-14 stsp #ifndef nitems
73 70f8f24d 2022-04-14 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
76 e6bcace5 2021-06-22 stsp static const struct got_error *
77 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
78 d6a28ffe 2022-05-20 op const char *path, int obj_type, time_t mtime, uint32_t seed)
80 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
82 e6bcace5 2021-06-22 stsp *new = NULL;
84 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
85 e6bcace5 2021-06-22 stsp if (m == NULL)
86 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
88 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
90 d6a28ffe 2022-05-20 op m->path_hash = murmurhash2(path, strlen(path), seed);
91 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
92 e6bcace5 2021-06-22 stsp m->mtime = mtime;
94 e6bcace5 2021-06-22 stsp return NULL;
98 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
100 e6bcace5 2021-06-22 stsp if (meta == NULL)
102 f8174ca5 2022-05-20 stsp meta->path_hash = 0;
103 5060d5a1 2022-01-10 stsp free(meta->delta_buf);
104 5060d5a1 2022-01-10 stsp meta->delta_buf = NULL;
105 67fd6849 2022-02-13 stsp free(meta->base_obj_id);
106 67fd6849 2022-02-13 stsp meta->base_obj_id = NULL;
107 411cbec1 2022-05-20 stsp meta->reused_delta_offset = 0;
110 e6bcace5 2021-06-22 stsp static void
111 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
115 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
116 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
117 e6bcace5 2021-06-22 stsp free(meta);
121 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
123 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
125 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
126 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
128 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
129 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
130 f8174ca5 2022-05-20 stsp if (a->path_hash < b->path_hash)
132 f8174ca5 2022-05-20 stsp if (a->path_hash > b->path_hash)
134 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
136 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
138 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
141 5060d5a1 2022-01-10 stsp static off_t
142 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
145 5060d5a1 2022-01-10 stsp off_t size = 32;
146 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
147 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
148 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
150 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
152 e6bcace5 2021-06-22 stsp return size;
155 5060d5a1 2022-01-10 stsp static const struct got_error *
156 5060d5a1 2022-01-10 stsp append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
160 5060d5a1 2022-01-10 stsp if (*len + nseg >= *sz) {
161 5060d5a1 2022-01-10 stsp while (*len + nseg >= *sz)
162 5060d5a1 2022-01-10 stsp *sz += *sz / 2;
163 5060d5a1 2022-01-10 stsp n = realloc(*p, *sz);
164 5060d5a1 2022-01-10 stsp if (n == NULL)
165 5060d5a1 2022-01-10 stsp return got_error_from_errno("realloc");
168 5060d5a1 2022-01-10 stsp memcpy(*p + *len, seg, nseg);
169 5060d5a1 2022-01-10 stsp *len += nseg;
170 5060d5a1 2022-01-10 stsp return NULL;
173 5060d5a1 2022-01-10 stsp static const struct got_error *
174 5060d5a1 2022-01-10 stsp encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
175 5060d5a1 2022-01-10 stsp struct got_delta_instruction *deltas, int ndeltas,
176 5060d5a1 2022-01-10 stsp off_t delta_size, off_t base_size)
178 5060d5a1 2022-01-10 stsp const struct got_error *err;
179 5060d5a1 2022-01-10 stsp unsigned char buf[16], *bp;
181 2d9e6abf 2022-05-04 stsp size_t len = 0, compressed_len;
182 2d9e6abf 2022-05-04 stsp off_t bufsize = delta_size;
184 5060d5a1 2022-01-10 stsp struct got_delta_instruction *d;
185 2d9e6abf 2022-05-04 stsp uint8_t *delta_buf;
187 2d9e6abf 2022-05-04 stsp delta_buf = malloc(bufsize);
188 2d9e6abf 2022-05-04 stsp if (delta_buf == NULL)
189 2d9e6abf 2022-05-04 stsp return got_error_from_errno("malloc");
191 5060d5a1 2022-01-10 stsp /* base object size */
192 5060d5a1 2022-01-10 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
193 5060d5a1 2022-01-10 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
194 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
195 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
196 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
197 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
199 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
203 5060d5a1 2022-01-10 stsp /* target object size */
204 5060d5a1 2022-01-10 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
205 5060d5a1 2022-01-10 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
206 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
207 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
208 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
209 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
211 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
215 5060d5a1 2022-01-10 stsp for (j = 0; j < ndeltas; j++) {
216 5060d5a1 2022-01-10 stsp d = &deltas[j];
217 5060d5a1 2022-01-10 stsp if (d->copy) {
218 5060d5a1 2022-01-10 stsp n = d->offset;
219 5060d5a1 2022-01-10 stsp bp = &buf[1];
220 5060d5a1 2022-01-10 stsp buf[0] = GOT_DELTA_BASE_COPY;
221 5060d5a1 2022-01-10 stsp for (i = 0; i < 4; i++) {
222 5060d5a1 2022-01-10 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
223 5060d5a1 2022-01-10 stsp buf[0] |= 1 << i;
224 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
226 5060d5a1 2022-01-10 stsp if (n == 0)
230 5060d5a1 2022-01-10 stsp n = d->len;
231 5060d5a1 2022-01-10 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
232 5060d5a1 2022-01-10 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
233 5060d5a1 2022-01-10 stsp for (i = 0; i < 3 && n > 0; i++) {
234 5060d5a1 2022-01-10 stsp buf[0] |= 1 << (i + 4);
235 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
239 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
240 5060d5a1 2022-01-10 stsp buf, bp - buf);
243 5060d5a1 2022-01-10 stsp } else if (o->f == NULL) {
245 5060d5a1 2022-01-10 stsp while (n != d->len) {
246 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
247 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
251 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
252 5060d5a1 2022-01-10 stsp o->data + o->hdrlen + d->offset + n,
256 5060d5a1 2022-01-10 stsp n += buf[0];
259 5060d5a1 2022-01-10 stsp char content[128];
261 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
262 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
266 5060d5a1 2022-01-10 stsp while (n != d->len) {
267 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
268 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
272 5060d5a1 2022-01-10 stsp r = fread(content, 1, buf[0], o->f);
273 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
274 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
277 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
278 5060d5a1 2022-01-10 stsp content, buf[0]);
281 5060d5a1 2022-01-10 stsp n += buf[0];
286 2d9e6abf 2022-05-04 stsp err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
287 2d9e6abf 2022-05-04 stsp NULL, NULL, delta_buf, 0, len);
291 5060d5a1 2022-01-10 stsp m->delta_len = len;
292 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
294 2d9e6abf 2022-05-04 stsp free(delta_buf);
295 2d9e6abf 2022-05-04 stsp return err;
298 74881701 2021-10-15 stsp static const struct got_error *
299 74881701 2021-10-15 stsp encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
300 74881701 2021-10-15 stsp struct got_delta_instruction *deltas, int ndeltas,
301 a319ca8c 2021-10-15 stsp off_t base_size, FILE *f)
303 2d9e6abf 2022-05-04 stsp const struct got_error *err;
304 a319ca8c 2021-10-15 stsp unsigned char buf[16], *bp;
307 2d9e6abf 2022-05-04 stsp struct got_deflate_buf zb;
308 a319ca8c 2021-10-15 stsp struct got_delta_instruction *d;
309 2d9e6abf 2022-05-04 stsp off_t delta_len = 0, compressed_len = 0;
311 2d9e6abf 2022-05-04 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
313 2d9e6abf 2022-05-04 stsp return err;
315 a319ca8c 2021-10-15 stsp /* base object size */
316 a319ca8c 2021-10-15 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
317 a319ca8c 2021-10-15 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
318 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
319 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
320 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
321 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
324 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
325 2d9e6abf 2022-05-04 stsp buf, 0, i, f, NULL);
328 2d9e6abf 2022-05-04 stsp delta_len += i;
330 a319ca8c 2021-10-15 stsp /* target object size */
331 a319ca8c 2021-10-15 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
332 a319ca8c 2021-10-15 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
333 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
334 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
335 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
336 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
339 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
340 2d9e6abf 2022-05-04 stsp buf, 0, i, f, NULL);
343 2d9e6abf 2022-05-04 stsp delta_len += i;
345 a319ca8c 2021-10-15 stsp for (j = 0; j < ndeltas; j++) {
346 a319ca8c 2021-10-15 stsp d = &deltas[j];
347 a319ca8c 2021-10-15 stsp if (d->copy) {
348 a319ca8c 2021-10-15 stsp n = d->offset;
349 a319ca8c 2021-10-15 stsp bp = &buf[1];
350 a319ca8c 2021-10-15 stsp buf[0] = GOT_DELTA_BASE_COPY;
351 a319ca8c 2021-10-15 stsp for (i = 0; i < 4; i++) {
352 a319ca8c 2021-10-15 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
353 a319ca8c 2021-10-15 stsp buf[0] |= 1 << i;
354 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
356 a319ca8c 2021-10-15 stsp if (n == 0)
359 a319ca8c 2021-10-15 stsp n = d->len;
360 a319ca8c 2021-10-15 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
361 a319ca8c 2021-10-15 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
362 a319ca8c 2021-10-15 stsp for (i = 0; i < 3 && n > 0; i++) {
363 a319ca8c 2021-10-15 stsp buf[0] |= 1 << (i + 4);
364 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
368 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
369 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, bp - buf, f, NULL);
372 2d9e6abf 2022-05-04 stsp delta_len += (bp - buf);
373 64a8571e 2022-01-07 stsp } else if (o->f == NULL) {
375 64a8571e 2022-01-07 stsp while (n != d->len) {
376 64a8571e 2022-01-07 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
377 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
378 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
381 2d9e6abf 2022-05-04 stsp delta_len++;
382 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
383 2d9e6abf 2022-05-04 stsp &compressed_len,
384 2d9e6abf 2022-05-04 stsp o->data + o->hdrlen + d->offset + n, 0,
385 2d9e6abf 2022-05-04 stsp buf[0], f, NULL);
388 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
389 64a8571e 2022-01-07 stsp n += buf[0];
392 a319ca8c 2021-10-15 stsp char content[128];
394 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
395 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
399 a319ca8c 2021-10-15 stsp while (n != d->len) {
400 a319ca8c 2021-10-15 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
401 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
402 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
405 2d9e6abf 2022-05-04 stsp delta_len++;
406 a319ca8c 2021-10-15 stsp r = fread(content, 1, buf[0], o->f);
407 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
408 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
411 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
412 2d9e6abf 2022-05-04 stsp &compressed_len, content, 0, buf[0], f,
416 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
417 a319ca8c 2021-10-15 stsp n += buf[0];
422 2d9e6abf 2022-05-04 stsp err = got_deflate_flush(&zb, f, NULL, &compressed_len);
426 2d9e6abf 2022-05-04 stsp /* sanity check */
427 2d9e6abf 2022-05-04 stsp if (compressed_len != ftello(f) - m->delta_offset) {
428 2d9e6abf 2022-05-04 stsp err = got_error(GOT_ERR_COMPRESSION);
432 2d9e6abf 2022-05-04 stsp m->delta_len = delta_len;
433 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
435 2d9e6abf 2022-05-04 stsp got_deflate_end(&zb);
436 2d9e6abf 2022-05-04 stsp return err;
439 301e83b3 2022-10-16 stsp const struct got_error *
440 301e83b3 2022-10-16 stsp got_pack_report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
441 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
442 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
443 b8af7c06 2022-03-15 stsp int nobj_written)
445 211cfef0 2022-01-05 stsp const struct got_error *err;
446 211cfef0 2022-01-05 stsp int elapsed;
448 211cfef0 2022-01-05 stsp if (progress_cb == NULL)
449 211cfef0 2022-01-05 stsp return NULL;
451 211cfef0 2022-01-05 stsp err = got_ratelimit_check(&elapsed, rl);
452 211cfef0 2022-01-05 stsp if (err || !elapsed)
453 211cfef0 2022-01-05 stsp return err;
455 b8af7c06 2022-03-15 stsp return progress_cb(progress_arg, ncolored, nfound, ntrees,
456 b8af7c06 2022-03-15 stsp packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
459 301e83b3 2022-10-16 stsp const struct got_error *
460 301e83b3 2022-10-16 stsp got_pack_add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
462 67fd6849 2022-02-13 stsp if (v->nmeta == v->metasz){
463 67fd6849 2022-02-13 stsp size_t newsize = 2 * v->metasz;
464 67fd6849 2022-02-13 stsp struct got_pack_meta **new;
465 67fd6849 2022-02-13 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
466 67fd6849 2022-02-13 stsp if (new == NULL)
467 67fd6849 2022-02-13 stsp return got_error_from_errno("reallocarray");
468 67fd6849 2022-02-13 stsp v->meta = new;
469 5e91dae4 2022-08-30 stsp v->metasz = newsize;
472 67fd6849 2022-02-13 stsp v->meta[v->nmeta++] = m;
473 67fd6849 2022-02-13 stsp return NULL;
476 301e83b3 2022-10-16 stsp const struct got_error *
477 301e83b3 2022-10-16 stsp got_pack_find_pack_for_reuse(struct got_packidx **best_packidx,
478 67fd6849 2022-02-13 stsp struct got_repository *repo)
480 9b576444 2022-03-14 stsp const struct got_error *err = NULL;
481 67fd6849 2022-02-13 stsp struct got_pathlist_entry *pe;
482 67fd6849 2022-02-13 stsp const char *best_packidx_path = NULL;
483 67fd6849 2022-02-13 stsp int nobj_max = 0;
485 67fd6849 2022-02-13 stsp *best_packidx = NULL;
487 9b576444 2022-03-14 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
488 67fd6849 2022-02-13 stsp const char *path_packidx = pe->path;
489 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
492 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
496 67fd6849 2022-02-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
497 67fd6849 2022-02-13 stsp if (nobj > nobj_max) {
498 67fd6849 2022-02-13 stsp best_packidx_path = path_packidx;
499 67fd6849 2022-02-13 stsp nobj_max = nobj;
503 67fd6849 2022-02-13 stsp if (best_packidx_path) {
504 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
508 67fd6849 2022-02-13 stsp return err;
511 301e83b3 2022-10-16 stsp const struct got_error *
512 301e83b3 2022-10-16 stsp got_pack_cache_pack_for_packidx(struct got_pack **pack,
513 301e83b3 2022-10-16 stsp struct got_packidx *packidx, struct got_repository *repo)
515 61af9b21 2022-06-28 stsp const struct got_error *err;
516 3d589bee 2022-06-25 stsp char *path_packfile = NULL;
518 3d589bee 2022-06-25 stsp err = got_packidx_get_packfile_path(&path_packfile,
519 3d589bee 2022-06-25 stsp packidx->path_packidx);
521 3d589bee 2022-06-25 stsp return err;
523 3d589bee 2022-06-25 stsp *pack = got_repo_get_cached_pack(repo, path_packfile);
524 3d589bee 2022-06-25 stsp if (*pack == NULL) {
525 3d589bee 2022-06-25 stsp err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
530 61af9b21 2022-06-28 stsp free(path_packfile);
531 67fd6849 2022-02-13 stsp return err;
534 67fd6849 2022-02-13 stsp static const struct got_error *
535 b8af7c06 2022-03-15 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
536 b8af7c06 2022-03-15 stsp int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
537 b8af7c06 2022-03-15 stsp struct got_repository *repo,
538 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
539 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
541 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
542 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
543 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
544 74881701 2021-10-15 stsp struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
545 5060d5a1 2022-01-10 stsp int i, j, ndeltas, best_ndeltas;
546 5060d5a1 2022-01-10 stsp off_t size, best_size;
547 4f4d853e 2021-10-24 stsp const int max_base_candidates = 3;
548 402a5ec1 2022-01-10 stsp size_t delta_memsize = 0;
549 adb4bbb2 2022-05-20 stsp const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
550 cc7a354a 2021-10-15 stsp int outfd = -1;
551 d6a28ffe 2022-05-20 op uint32_t delta_seed;
553 d6a28ffe 2022-05-20 op delta_seed = arc4random();
555 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
556 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
557 e6bcace5 2021-06-22 stsp if (cancel_cb) {
558 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
562 301e83b3 2022-10-16 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
563 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
564 b8af7c06 2022-03-15 stsp nreused + i, 0);
567 e6bcace5 2021-06-22 stsp m = meta[i];
569 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
570 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
573 94dac27c 2021-10-15 stsp err = got_object_raw_open(&raw, &outfd, repo, &m->id);
576 600b755e 2021-10-14 stsp m->size = raw->size;
578 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
579 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&m->dtab, raw->data,
580 d6a28ffe 2022-05-20 op raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
582 64a8571e 2022-01-07 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
583 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed);
588 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
589 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
590 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
591 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
592 e6bcace5 2021-06-22 stsp n->dtab = NULL;
595 74881701 2021-10-15 stsp best_size = raw->size;
596 74881701 2021-10-15 stsp best_ndeltas = 0;
597 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
598 e6bcace5 2021-06-22 stsp if (cancel_cb) {
599 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
603 e6bcace5 2021-06-22 stsp base = meta[j];
604 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
605 22edbce7 2021-10-24 stsp if (base->nchain >= 128 ||
606 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
609 d3c116bf 2021-10-15 stsp err = got_object_raw_open(&base_raw, &outfd, repo,
610 94dac27c 2021-10-15 stsp &base->id);
614 64a8571e 2022-01-07 stsp if (raw->f == NULL && base_raw->f == NULL) {
615 64a8571e 2022-01-07 stsp err = got_deltify_mem_mem(&deltas, &ndeltas,
616 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
617 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
618 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
619 64a8571e 2022-01-07 stsp base_raw->hdrlen,
620 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
621 64a8571e 2022-01-07 stsp } else if (raw->f == NULL) {
622 64a8571e 2022-01-07 stsp err = got_deltify_mem_file(&deltas, &ndeltas,
623 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
624 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
625 64a8571e 2022-01-07 stsp base->dtab, base_raw->f,
626 64a8571e 2022-01-07 stsp base_raw->hdrlen,
627 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
628 64a8571e 2022-01-07 stsp } else if (base_raw->f == NULL) {
629 64a8571e 2022-01-07 stsp err = got_deltify_file_mem(&deltas, &ndeltas,
630 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
631 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
632 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
633 64a8571e 2022-01-07 stsp base_raw->hdrlen,
634 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
636 64a8571e 2022-01-07 stsp err = got_deltify(&deltas, &ndeltas,
637 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
638 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
639 64a8571e 2022-01-07 stsp base->dtab, base_raw->f, base_raw->hdrlen,
640 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
642 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
643 e6bcace5 2021-06-22 stsp base_raw = NULL;
647 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
648 74881701 2021-10-15 stsp if (size + 32 < best_size){
650 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
651 e6bcace5 2021-06-22 stsp * replace it.
653 74881701 2021-10-15 stsp best_size = size;
654 74881701 2021-10-15 stsp free(best_deltas);
655 74881701 2021-10-15 stsp best_deltas = deltas;
656 74881701 2021-10-15 stsp best_ndeltas = ndeltas;
657 74881701 2021-10-15 stsp deltas = NULL;
658 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
659 e6bcace5 2021-06-22 stsp m->prev = base;
660 e6bcace5 2021-06-22 stsp m->head = base->head;
661 e6bcace5 2021-06-22 stsp if (m->head == NULL)
662 e6bcace5 2021-06-22 stsp m->head = base;
664 e6bcace5 2021-06-22 stsp free(deltas);
665 e6bcace5 2021-06-22 stsp deltas = NULL;
666 e6bcace5 2021-06-22 stsp ndeltas = 0;
670 74881701 2021-10-15 stsp if (best_ndeltas > 0) {
671 402a5ec1 2022-01-10 stsp if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
672 402a5ec1 2022-01-10 stsp delta_memsize + best_size <= max_delta_memsize) {
673 402a5ec1 2022-01-10 stsp delta_memsize += best_size;
674 5060d5a1 2022-01-10 stsp err = encode_delta_in_mem(m, raw, best_deltas,
675 5060d5a1 2022-01-10 stsp best_ndeltas, best_size, m->prev->size);
677 5060d5a1 2022-01-10 stsp m->delta_offset = ftello(delta_cache);
678 5060d5a1 2022-01-10 stsp err = encode_delta(m, raw, best_deltas,
679 5060d5a1 2022-01-10 stsp best_ndeltas, m->prev->size, delta_cache);
681 74881701 2021-10-15 stsp free(best_deltas);
682 74881701 2021-10-15 stsp best_deltas = NULL;
683 74881701 2021-10-15 stsp best_ndeltas = 0;
688 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
689 e6bcace5 2021-06-22 stsp raw = NULL;
692 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
693 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
694 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
697 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
698 e6bcace5 2021-06-22 stsp if (base_raw)
699 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
700 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
701 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
702 74881701 2021-10-15 stsp free(deltas);
703 74881701 2021-10-15 stsp free(best_deltas);
704 e6bcace5 2021-06-22 stsp return err;
707 e6bcace5 2021-06-22 stsp static const struct got_error *
708 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
709 05118f5a 2021-06-22 stsp struct got_repository *repo)
711 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
712 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
715 05118f5a 2021-06-22 stsp *found = 0;
717 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
718 05118f5a 2021-06-22 stsp if (err == NULL)
719 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
720 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
721 05118f5a 2021-06-22 stsp err = NULL;
722 05118f5a 2021-06-22 stsp return err;
725 301e83b3 2022-10-16 stsp const struct got_error *
726 301e83b3 2022-10-16 stsp got_pack_add_object(int want_meta, struct got_object_idset *idset,
727 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
728 d6a28ffe 2022-05-20 op time_t mtime, uint32_t seed, int loose_obj_only,
729 d6a28ffe 2022-05-20 op struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
730 6863cbf9 2022-03-21 stsp got_pack_progress_cb progress_cb, void *progress_arg,
731 6863cbf9 2022-03-21 stsp struct got_ratelimit *rl)
733 e6bcace5 2021-06-22 stsp const struct got_error *err;
734 67fd6849 2022-02-13 stsp struct got_pack_meta *m = NULL;
736 05118f5a 2021-06-22 stsp if (loose_obj_only) {
737 05118f5a 2021-06-22 stsp int is_packed;
738 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
740 05118f5a 2021-06-22 stsp return err;
741 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
742 05118f5a 2021-06-22 stsp return NULL;
745 67fd6849 2022-02-13 stsp if (want_meta) {
746 d6a28ffe 2022-05-20 op err = alloc_meta(&m, id, path, obj_type, mtime, seed);
748 6863cbf9 2022-03-21 stsp return err;
750 6863cbf9 2022-03-21 stsp (*nfound)++;
751 301e83b3 2022-10-16 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
752 6863cbf9 2022-03-21 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
754 bb6672b6 2022-04-14 tb clear_meta(m);
756 67fd6849 2022-02-13 stsp return err;
760 bb6672b6 2022-04-14 tb err = got_object_idset_add(idset, id, m);
762 bb6672b6 2022-04-14 tb clear_meta(m);
768 301e83b3 2022-10-16 stsp const struct got_error *
769 301e83b3 2022-10-16 stsp got_pack_load_tree_entries(struct got_object_id_queue *ids, int want_meta,
770 2f8438b0 2022-05-04 stsp struct got_object_idset *idset, struct got_object_idset *idset_exclude,
771 0ab4c957 2022-06-13 stsp struct got_tree_object *tree,
772 d6a28ffe 2022-05-20 op const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
773 b8af7c06 2022-03-15 stsp int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
774 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
775 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
777 e6bcace5 2021-06-22 stsp const struct got_error *err;
778 e6bcace5 2021-06-22 stsp char *p = NULL;
781 b8af7c06 2022-03-15 stsp (*ntrees)++;
782 301e83b3 2022-10-16 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
783 b8af7c06 2022-03-15 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
785 e6bcace5 2021-06-22 stsp return err;
787 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
788 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
789 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
790 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
792 e6bcace5 2021-06-22 stsp if (cancel_cb) {
793 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
798 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
799 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset, id) ||
800 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
804 0ab4c957 2022-06-13 stsp * If got-read-pack is crawling trees for us then
805 0ab4c957 2022-06-13 stsp * we are only here to collect blob IDs.
807 0ab4c957 2022-06-13 stsp if (ids == NULL && S_ISDIR(mode))
810 0ab4c957 2022-06-13 stsp if (asprintf(&p, "%s%s%s", dpath,
811 0ab4c957 2022-06-13 stsp got_path_is_root_dir(dpath) ? "" : "/",
812 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
813 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
817 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
818 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
819 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
822 3e6ceea0 2022-05-20 stsp qid->data = p;
824 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
825 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
826 301e83b3 2022-10-16 stsp err = got_pack_add_object(want_meta,
827 2f8438b0 2022-05-04 stsp want_meta ? idset : idset_exclude, id, p,
828 d6a28ffe 2022-05-20 op GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
829 d6a28ffe 2022-05-20 op repo, ncolored, nfound, ntrees,
830 6863cbf9 2022-03-21 stsp progress_cb, progress_arg, rl);
842 e6bcace5 2021-06-22 stsp return err;
845 301e83b3 2022-10-16 stsp const struct got_error *
846 301e83b3 2022-10-16 stsp got_pack_load_tree(int want_meta, struct got_object_idset *idset,
847 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
848 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
849 d6a28ffe 2022-05-20 op uint32_t seed, struct got_repository *repo, int loose_obj_only,
850 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
851 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
852 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
854 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
855 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
856 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
857 0ab4c957 2022-06-13 stsp struct got_tree_object *tree = NULL;
859 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, tree_id) ||
860 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, tree_id))
861 e6bcace5 2021-06-22 stsp return NULL;
863 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
865 3e6ceea0 2022-05-20 stsp return err;
866 3e6ceea0 2022-05-20 stsp qid->data = strdup(dpath);
867 3e6ceea0 2022-05-20 stsp if (qid->data == NULL) {
868 3e6ceea0 2022-05-20 stsp err = got_error_from_errno("strdup");
869 3e6ceea0 2022-05-20 stsp got_object_qid_free(qid);
870 e6bcace5 2021-06-22 stsp return err;
873 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
874 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
876 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
877 3e6ceea0 2022-05-20 stsp const char *path;
878 e6bcace5 2021-06-22 stsp if (cancel_cb) {
879 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
884 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
885 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
886 3e6ceea0 2022-05-20 stsp path = qid->data;
888 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, &qid->id) ||
889 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, &qid->id)) {
890 3e6ceea0 2022-05-20 stsp free(qid->data);
891 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
895 301e83b3 2022-10-16 stsp err = got_pack_add_object(want_meta,
896 301e83b3 2022-10-16 stsp want_meta ? idset : idset_exclude,
897 3e6ceea0 2022-05-20 stsp &qid->id, path, GOT_OBJ_TYPE_TREE,
898 d6a28ffe 2022-05-20 op mtime, seed, loose_obj_only, repo,
899 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
901 3e6ceea0 2022-05-20 stsp free(qid->data);
902 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
906 0ab4c957 2022-06-13 stsp err = got_object_open_as_tree(&tree, repo, &qid->id);
908 0ab4c957 2022-06-13 stsp free(qid->data);
909 0ab4c957 2022-06-13 stsp got_object_qid_free(qid);
913 301e83b3 2022-10-16 stsp err = got_pack_load_tree_entries(&tree_ids, want_meta, idset,
914 0ab4c957 2022-06-13 stsp idset_exclude, tree, path, mtime, seed, repo,
915 0ab4c957 2022-06-13 stsp loose_obj_only, ncolored, nfound, ntrees,
916 0ab4c957 2022-06-13 stsp progress_cb, progress_arg, rl,
917 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
918 3e6ceea0 2022-05-20 stsp free(qid->data);
919 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
923 0ab4c957 2022-06-13 stsp got_object_tree_close(tree);
924 0ab4c957 2022-06-13 stsp tree = NULL;
927 3e6ceea0 2022-05-20 stsp STAILQ_FOREACH(qid, &tree_ids, entry)
928 3e6ceea0 2022-05-20 stsp free(qid->data);
929 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
931 0ab4c957 2022-06-13 stsp got_object_tree_close(tree);
932 e6bcace5 2021-06-22 stsp return err;
935 e6bcace5 2021-06-22 stsp static const struct got_error *
936 67fd6849 2022-02-13 stsp load_commit(int want_meta, struct got_object_idset *idset,
937 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
938 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
939 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
940 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
941 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
943 e6bcace5 2021-06-22 stsp const struct got_error *err;
944 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
946 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
947 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
948 e6bcace5 2021-06-22 stsp return NULL;
950 05118f5a 2021-06-22 stsp if (loose_obj_only) {
951 05118f5a 2021-06-22 stsp int is_packed;
952 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
954 05118f5a 2021-06-22 stsp return err;
955 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
956 05118f5a 2021-06-22 stsp return NULL;
959 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
961 e6bcace5 2021-06-22 stsp return err;
963 301e83b3 2022-10-16 stsp err = got_pack_add_object(want_meta,
964 301e83b3 2022-10-16 stsp want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_COMMIT,
965 d6a28ffe 2022-05-20 op got_object_commit_get_committer_time(commit), seed,
966 6863cbf9 2022-03-21 stsp loose_obj_only, repo,
967 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
971 301e83b3 2022-10-16 stsp err = got_pack_load_tree(want_meta, idset, idset_exclude,
972 2f8438b0 2022-05-04 stsp got_object_commit_get_tree_id(commit),
973 d6a28ffe 2022-05-20 op "", got_object_commit_get_committer_time(commit), seed,
974 b8af7c06 2022-03-15 stsp repo, loose_obj_only, ncolored, nfound, ntrees,
975 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
977 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
978 e6bcace5 2021-06-22 stsp return err;
981 05118f5a 2021-06-22 stsp static const struct got_error *
982 67fd6849 2022-02-13 stsp load_tag(int want_meta, struct got_object_idset *idset,
983 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
984 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
985 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
986 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
987 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
989 05118f5a 2021-06-22 stsp const struct got_error *err;
990 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
992 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
993 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
994 05118f5a 2021-06-22 stsp return NULL;
996 05118f5a 2021-06-22 stsp if (loose_obj_only) {
997 05118f5a 2021-06-22 stsp int is_packed;
998 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1000 05118f5a 2021-06-22 stsp return err;
1001 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1002 05118f5a 2021-06-22 stsp return NULL;
1005 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1007 05118f5a 2021-06-22 stsp return err;
1009 301e83b3 2022-10-16 stsp err = got_pack_add_object(want_meta,
1010 301e83b3 2022-10-16 stsp want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_TAG,
1011 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1012 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1014 05118f5a 2021-06-22 stsp goto done;
1016 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1017 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1018 2f8438b0 2022-05-04 stsp err = load_commit(want_meta, idset, idset_exclude,
1019 d6a28ffe 2022-05-20 op got_object_tag_get_object_id(tag), repo, seed,
1020 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1021 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1023 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1024 301e83b3 2022-10-16 stsp err = got_pack_load_tree(want_meta, idset, idset_exclude,
1025 67fd6849 2022-02-13 stsp got_object_tag_get_object_id(tag), "",
1026 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, repo,
1027 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1028 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1035 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1036 05118f5a 2021-06-22 stsp return err;
1039 301e83b3 2022-10-16 stsp const struct got_error *
1040 301e83b3 2022-10-16 stsp got_pack_paint_commit(struct got_object_qid *qid, intptr_t color)
1042 61af9b21 2022-06-28 stsp if (color < 0 || color >= COLOR_MAX)
1043 70f8f24d 2022-04-14 stsp return got_error(GOT_ERR_RANGE);
1045 61af9b21 2022-06-28 stsp qid->data = (void *)color;
1046 e6bcace5 2021-06-22 stsp return NULL;
1049 301e83b3 2022-10-16 stsp const struct got_error *
1050 301e83b3 2022-10-16 stsp got_pack_queue_commit_id(struct got_object_id_queue *ids,
1051 301e83b3 2022-10-16 stsp struct got_object_id *id, intptr_t color, struct got_repository *repo)
1053 70f8f24d 2022-04-14 stsp const struct got_error *err;
1054 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1056 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1058 e6bcace5 2021-06-22 stsp return err;
1060 70f8f24d 2022-04-14 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
1061 301e83b3 2022-10-16 stsp return got_pack_paint_commit(qid, color);
1064 e6bcace5 2021-06-22 stsp struct append_id_arg {
1065 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1067 70f8f24d 2022-04-14 stsp struct got_object_idset *drop;
1068 70f8f24d 2022-04-14 stsp struct got_object_idset *skip;
1071 e6bcace5 2021-06-22 stsp static const struct got_error *
1072 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1074 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1076 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(a->skip, id) ||
1077 70f8f24d 2022-04-14 stsp got_object_idset_contains(a->drop, id))
1078 70f8f24d 2022-04-14 stsp return NULL;
1080 70f8f24d 2022-04-14 stsp a->array[++a->idx] = got_object_id_dup(id);
1081 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1082 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1084 e6bcace5 2021-06-22 stsp return NULL;
1087 29e0594f 2022-04-09 stsp static const struct got_error *
1088 61af9b21 2022-06-28 stsp queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1089 29e0594f 2022-04-09 stsp struct got_object_id_queue *ids, struct got_repository *repo)
1091 29e0594f 2022-04-09 stsp const struct got_error *err;
1092 29e0594f 2022-04-09 stsp struct got_tag_object *tag = NULL;
1093 29e0594f 2022-04-09 stsp int obj_type;
1095 29e0594f 2022-04-09 stsp err = got_object_get_type(&obj_type, repo, id);
1097 29e0594f 2022-04-09 stsp return err;
1099 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1100 29e0594f 2022-04-09 stsp err = got_object_open_as_tag(&tag, repo, id);
1102 29e0594f 2022-04-09 stsp return err;
1103 29e0594f 2022-04-09 stsp obj_type = got_object_tag_get_object_type(tag);
1104 29e0594f 2022-04-09 stsp id = got_object_tag_get_object_id(tag);
1107 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1108 301e83b3 2022-10-16 stsp err = got_pack_queue_commit_id(ids, id, color, repo);
1110 29e0594f 2022-04-09 stsp goto done;
1114 29e0594f 2022-04-09 stsp got_object_tag_close(tag);
1115 61af9b21 2022-06-28 stsp return err;
1118 301e83b3 2022-10-16 stsp const struct got_error *
1119 301e83b3 2022-10-16 stsp got_pack_find_pack_for_commit_painting(struct got_packidx **best_packidx,
1120 61af9b21 2022-06-28 stsp struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1122 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
1123 61af9b21 2022-06-28 stsp struct got_pathlist_entry *pe;
1124 61af9b21 2022-06-28 stsp const char *best_packidx_path = NULL;
1125 61af9b21 2022-06-28 stsp int nobj_max = 0;
1126 61af9b21 2022-06-28 stsp int ncommits_max = 0;
1128 61af9b21 2022-06-28 stsp *best_packidx = NULL;
1131 61af9b21 2022-06-28 stsp * Find the largest pack which contains at least some of the
1132 61af9b21 2022-06-28 stsp * commits we are interested in.
1134 61af9b21 2022-06-28 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1135 61af9b21 2022-06-28 stsp const char *path_packidx = pe->path;
1136 61af9b21 2022-06-28 stsp struct got_packidx *packidx;
1137 61af9b21 2022-06-28 stsp int nobj, idx, ncommits = 0;
1138 61af9b21 2022-06-28 stsp struct got_object_qid *qid;
1140 61af9b21 2022-06-28 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
1144 61af9b21 2022-06-28 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1145 61af9b21 2022-06-28 stsp if (nobj <= nobj_max)
1148 61af9b21 2022-06-28 stsp STAILQ_FOREACH(qid, ids, entry) {
1149 61af9b21 2022-06-28 stsp idx = got_packidx_get_object_idx(packidx, &qid->id);
1150 61af9b21 2022-06-28 stsp if (idx != -1)
1151 61af9b21 2022-06-28 stsp ncommits++;
1153 61af9b21 2022-06-28 stsp if (ncommits > ncommits_max) {
1154 61af9b21 2022-06-28 stsp best_packidx_path = path_packidx;
1155 61af9b21 2022-06-28 stsp nobj_max = nobj;
1156 61af9b21 2022-06-28 stsp ncommits_max = ncommits;
1160 61af9b21 2022-06-28 stsp if (best_packidx_path && err == NULL) {
1161 61af9b21 2022-06-28 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
1165 61af9b21 2022-06-28 stsp return err;
1168 61af9b21 2022-06-28 stsp static const struct got_error *
1169 14dbbf48 2022-04-10 stsp findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1170 14dbbf48 2022-04-10 stsp struct got_object_id **head, int nhead,
1171 14dbbf48 2022-04-10 stsp struct got_object_id **tail, int ntail,
1172 14dbbf48 2022-04-10 stsp struct got_repository *repo,
1173 14dbbf48 2022-04-10 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1174 14dbbf48 2022-04-10 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1176 14dbbf48 2022-04-10 stsp const struct got_error *err = NULL;
1177 14dbbf48 2022-04-10 stsp struct got_object_id_queue ids;
1178 70f8f24d 2022-04-14 stsp struct got_object_idset *keep, *drop, *skip = NULL;
1179 14dbbf48 2022-04-10 stsp int i, nkeep;
1181 14dbbf48 2022-04-10 stsp STAILQ_INIT(&ids);
1182 14dbbf48 2022-04-10 stsp *res = NULL;
1183 14dbbf48 2022-04-10 stsp *nres = 0;
1184 14dbbf48 2022-04-10 stsp *ncolored = 0;
1186 14dbbf48 2022-04-10 stsp keep = got_object_idset_alloc();
1187 14dbbf48 2022-04-10 stsp if (keep == NULL)
1188 14dbbf48 2022-04-10 stsp return got_error_from_errno("got_object_idset_alloc");
1190 14dbbf48 2022-04-10 stsp drop = got_object_idset_alloc();
1191 14dbbf48 2022-04-10 stsp if (drop == NULL) {
1192 14dbbf48 2022-04-10 stsp err = got_error_from_errno("got_object_idset_alloc");
1193 14dbbf48 2022-04-10 stsp goto done;
1196 70f8f24d 2022-04-14 stsp skip = got_object_idset_alloc();
1197 70f8f24d 2022-04-14 stsp if (skip == NULL) {
1198 70f8f24d 2022-04-14 stsp err = got_error_from_errno("got_object_idset_alloc");
1199 70f8f24d 2022-04-14 stsp goto done;
1202 14dbbf48 2022-04-10 stsp for (i = 0; i < nhead; i++) {
1203 14dbbf48 2022-04-10 stsp struct got_object_id *id = head[i];
1204 14dbbf48 2022-04-10 stsp if (id == NULL)
1206 fbafdecf 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1208 14dbbf48 2022-04-10 stsp goto done;
1211 14dbbf48 2022-04-10 stsp for (i = 0; i < ntail; i++) {
1212 14dbbf48 2022-04-10 stsp struct got_object_id *id = tail[i];
1213 14dbbf48 2022-04-10 stsp if (id == NULL)
1215 14dbbf48 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1217 14dbbf48 2022-04-10 stsp goto done;
1220 301e83b3 2022-10-16 stsp err = got_pack_paint_commits(ncolored, &ids, nhead + ntail,
1221 70f8f24d 2022-04-14 stsp keep, drop, skip, repo, progress_cb, progress_arg, rl,
1222 70f8f24d 2022-04-14 stsp cancel_cb, cancel_arg);
1224 14dbbf48 2022-04-10 stsp goto done;
1226 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1227 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1228 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1229 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1230 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1231 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1232 e6bcace5 2021-06-22 stsp goto done;
1234 70f8f24d 2022-04-14 stsp arg.idx = -1;
1235 70f8f24d 2022-04-14 stsp arg.skip = skip;
1236 70f8f24d 2022-04-14 stsp arg.drop = drop;
1237 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1238 e6bcace5 2021-06-22 stsp if (err) {
1239 e6bcace5 2021-06-22 stsp free(arg.array);
1240 e6bcace5 2021-06-22 stsp goto done;
1242 e6bcace5 2021-06-22 stsp *res = arg.array;
1243 70f8f24d 2022-04-14 stsp *nres = arg.idx + 1;
1246 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1247 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1249 70f8f24d 2022-04-14 stsp got_object_idset_free(skip);
1250 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1251 e6bcace5 2021-06-22 stsp return err;
1254 e6bcace5 2021-06-22 stsp static const struct got_error *
1255 0ab4c957 2022-06-13 stsp find_pack_for_enumeration(struct got_packidx **best_packidx,
1256 0ab4c957 2022-06-13 stsp struct got_object_id **ids, int nids, struct got_repository *repo)
1258 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1259 0ab4c957 2022-06-13 stsp struct got_pathlist_entry *pe;
1260 0ab4c957 2022-06-13 stsp const char *best_packidx_path = NULL;
1261 0ab4c957 2022-06-13 stsp int nobj_max = 0;
1262 0ab4c957 2022-06-13 stsp int ncommits_max = 0;
1264 0ab4c957 2022-06-13 stsp *best_packidx = NULL;
1267 0ab4c957 2022-06-13 stsp * Find the largest pack which contains at least some of the
1268 0ab4c957 2022-06-13 stsp * commits and tags we are interested in.
1270 0ab4c957 2022-06-13 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1271 0ab4c957 2022-06-13 stsp const char *path_packidx = pe->path;
1272 0ab4c957 2022-06-13 stsp struct got_packidx *packidx;
1273 0ab4c957 2022-06-13 stsp int nobj, i, idx, ncommits = 0;
1275 0ab4c957 2022-06-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
1279 0ab4c957 2022-06-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1280 0ab4c957 2022-06-13 stsp if (nobj <= nobj_max)
1283 0ab4c957 2022-06-13 stsp for (i = 0; i < nids; i++) {
1284 0ab4c957 2022-06-13 stsp idx = got_packidx_get_object_idx(packidx, ids[i]);
1285 0ab4c957 2022-06-13 stsp if (idx != -1)
1286 0ab4c957 2022-06-13 stsp ncommits++;
1288 0ab4c957 2022-06-13 stsp if (ncommits > ncommits_max) {
1289 0ab4c957 2022-06-13 stsp best_packidx_path = path_packidx;
1290 0ab4c957 2022-06-13 stsp nobj_max = nobj;
1291 0ab4c957 2022-06-13 stsp ncommits_max = ncommits;
1295 eb7b30a1 2022-06-13 stsp if (best_packidx_path && err == NULL) {
1296 0ab4c957 2022-06-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
1300 0ab4c957 2022-06-13 stsp return err;
1303 0ab4c957 2022-06-13 stsp static const struct got_error *
1304 b8af7c06 2022-03-15 stsp load_object_ids(int *ncolored, int *nfound, int *ntrees,
1305 b8af7c06 2022-03-15 stsp struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1306 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1307 d6a28ffe 2022-05-20 op uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1308 d6a28ffe 2022-05-20 op void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1309 d6a28ffe 2022-05-20 op void *cancel_arg)
1311 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1312 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1313 0ab4c957 2022-06-13 stsp struct got_packidx *packidx = NULL;
1314 db9b9b1c 2022-06-14 stsp int i, nobj = 0, obj_type, found_all_objects = 0;
1315 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude;
1317 2f8438b0 2022-05-04 stsp idset_exclude = got_object_idset_alloc();
1318 2f8438b0 2022-05-04 stsp if (idset_exclude == NULL)
1319 2f8438b0 2022-05-04 stsp return got_error_from_errno("got_object_idset_alloc");
1321 b8af7c06 2022-03-15 stsp *ncolored = 0;
1322 b8af7c06 2022-03-15 stsp *nfound = 0;
1323 b8af7c06 2022-03-15 stsp *ntrees = 0;
1325 b8af7c06 2022-03-15 stsp err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1326 b8af7c06 2022-03-15 stsp repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1328 e6bcace5 2021-06-22 stsp goto done;
1330 0ab4c957 2022-06-13 stsp err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
1332 0ab4c957 2022-06-13 stsp goto done;
1333 0ab4c957 2022-06-13 stsp if (packidx) {
1334 301e83b3 2022-10-16 stsp err = got_pack_load_packed_object_ids(&found_all_objects,
1335 db9b9b1c 2022-06-14 stsp theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
1336 db9b9b1c 2022-06-14 stsp loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1337 db9b9b1c 2022-06-14 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1339 0ab4c957 2022-06-13 stsp goto done;
1342 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1343 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
1344 05118f5a 2021-06-22 stsp if (id == NULL)
1346 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1348 05118f5a 2021-06-22 stsp return err;
1349 9d34261e 2022-04-07 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1350 db9b9b1c 2022-06-14 stsp if (!found_all_objects) {
1351 db9b9b1c 2022-06-14 stsp err = load_commit(0, idset, idset_exclude,
1352 db9b9b1c 2022-06-14 stsp id, repo, seed, loose_obj_only,
1353 db9b9b1c 2022-06-14 stsp ncolored, nfound, ntrees,
1354 db9b9b1c 2022-06-14 stsp progress_cb, progress_arg, rl,
1355 db9b9b1c 2022-06-14 stsp cancel_cb, cancel_arg);
1357 db9b9b1c 2022-06-14 stsp goto done;
1359 9d34261e 2022-04-07 stsp } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1360 2f8438b0 2022-05-04 stsp err = load_tag(0, idset, idset_exclude, id, repo,
1361 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
1362 9d34261e 2022-04-07 stsp progress_cb, progress_arg, rl,
1363 9d34261e 2022-04-07 stsp cancel_cb, cancel_arg);
1365 9d34261e 2022-04-07 stsp goto done;
1369 db9b9b1c 2022-06-14 stsp found_all_objects = 0;
1370 0ab4c957 2022-06-13 stsp err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
1372 0ab4c957 2022-06-13 stsp goto done;
1373 0ab4c957 2022-06-13 stsp if (packidx) {
1374 301e83b3 2022-10-16 stsp err = got_pack_load_packed_object_ids(&found_all_objects, ids,
1375 db9b9b1c 2022-06-14 stsp nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
1376 db9b9b1c 2022-06-14 stsp loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1377 0ab4c957 2022-06-13 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1379 0ab4c957 2022-06-13 stsp goto done;
1382 db9b9b1c 2022-06-14 stsp if (!found_all_objects) {
1383 db9b9b1c 2022-06-14 stsp for (i = 0; i < nobj; i++) {
1384 db9b9b1c 2022-06-14 stsp err = load_commit(1, idset, idset_exclude, ids[i],
1385 db9b9b1c 2022-06-14 stsp repo, seed, loose_obj_only, ncolored, nfound,
1386 db9b9b1c 2022-06-14 stsp ntrees, progress_cb, progress_arg, rl,
1387 db9b9b1c 2022-06-14 stsp cancel_cb, cancel_arg);
1389 db9b9b1c 2022-06-14 stsp goto done;
1393 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1394 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1395 67fd6849 2022-02-13 stsp struct got_pack_meta *m;
1396 05118f5a 2021-06-22 stsp if (id == NULL)
1398 67fd6849 2022-02-13 stsp m = got_object_idset_get(idset, id);
1399 67fd6849 2022-02-13 stsp if (m == NULL) {
1400 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1402 07165b17 2021-07-01 stsp goto done;
1404 67fd6849 2022-02-13 stsp obj_type = m->obj_type;
1405 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1407 2f8438b0 2022-05-04 stsp err = load_tag(1, idset, idset_exclude, id, repo,
1408 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
1409 2f8438b0 2022-05-04 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1411 e6bcace5 2021-06-22 stsp goto done;
1414 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
1415 e6bcace5 2021-06-22 stsp free(ids[i]);
1417 e6bcace5 2021-06-22 stsp free(ids);
1418 2f8438b0 2022-05-04 stsp got_object_idset_free(idset_exclude);
1419 e6bcace5 2021-06-22 stsp return err;
1422 336075a4 2022-06-25 op static const struct got_error *
1423 894e4711 2022-10-15 stsp hwrite(int fd, const void *buf, off_t len, SHA1_CTX *ctx)
1425 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1426 13b2bc37 2022-10-23 stsp return got_poll_write_full(fd, buf, len);
1429 336075a4 2022-06-25 op static const struct got_error *
1430 894e4711 2022-10-15 stsp hcopy(FILE *fsrc, int fd_dst, off_t len, SHA1_CTX *ctx)
1432 13b2bc37 2022-10-23 stsp const struct got_error *err;
1433 2d9e6abf 2022-05-04 stsp unsigned char buf[65536];
1434 2d9e6abf 2022-05-04 stsp off_t remain = len;
1437 2d9e6abf 2022-05-04 stsp while (remain > 0) {
1438 2d9e6abf 2022-05-04 stsp size_t copylen = MIN(sizeof(buf), remain);
1439 2d9e6abf 2022-05-04 stsp n = fread(buf, 1, copylen, fsrc);
1440 2d9e6abf 2022-05-04 stsp if (n != copylen)
1441 2d9e6abf 2022-05-04 stsp return got_ferror(fsrc, GOT_ERR_IO);
1442 2d9e6abf 2022-05-04 stsp SHA1Update(ctx, buf, copylen);
1443 13b2bc37 2022-10-23 stsp err = got_poll_write_full(fd_dst, buf, copylen);
1445 13b2bc37 2022-10-23 stsp return err;
1446 2d9e6abf 2022-05-04 stsp remain -= copylen;
1449 e6bcace5 2021-06-22 stsp return NULL;
1452 336075a4 2022-06-25 op static const struct got_error *
1453 e93fb944 2022-05-10 stsp hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1454 894e4711 2022-10-15 stsp int fd, off_t len, SHA1_CTX *ctx)
1456 e93fb944 2022-05-10 stsp if (src_offset + len > src_size)
1457 e93fb944 2022-05-10 stsp return got_error(GOT_ERR_RANGE);
1459 e93fb944 2022-05-10 stsp SHA1Update(ctx, src + src_offset, len);
1460 13b2bc37 2022-10-23 stsp return got_poll_write_full(fd, src + src_offset, len);
1463 e6bcace5 2021-06-22 stsp static void
1464 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1466 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1467 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1468 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1469 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1472 e6bcace5 2021-06-22 stsp static int
1473 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1475 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1477 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1478 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1479 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1480 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1481 611e8e31 2022-05-01 stsp if (bhd->mtime < ahd->mtime)
1482 611e8e31 2022-05-01 stsp return -1;
1483 611e8e31 2022-05-01 stsp if (bhd->mtime > ahd->mtime)
1485 611e8e31 2022-05-01 stsp if (bhd < ahd)
1486 611e8e31 2022-05-01 stsp return -1;
1487 611e8e31 2022-05-01 stsp if (bhd > ahd)
1489 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1490 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1491 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
1492 611e8e31 2022-05-01 stsp return -1;
1493 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
1495 611e8e31 2022-05-01 stsp return got_object_id_cmp(&a->id, &b->id);
1498 67fd6849 2022-02-13 stsp static int
1499 67fd6849 2022-02-13 stsp reuse_write_order_cmp(const void *pa, const void *pb)
1501 67fd6849 2022-02-13 stsp struct got_pack_meta *a, *b;
1503 67fd6849 2022-02-13 stsp a = *(struct got_pack_meta **)pa;
1504 67fd6849 2022-02-13 stsp b = *(struct got_pack_meta **)pb;
1506 67fd6849 2022-02-13 stsp if (a->reused_delta_offset < b->reused_delta_offset)
1507 67fd6849 2022-02-13 stsp return -1;
1508 67fd6849 2022-02-13 stsp if (a->reused_delta_offset > b->reused_delta_offset)
1513 e6bcace5 2021-06-22 stsp static const struct got_error *
1514 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1518 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1520 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1521 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1522 e6bcace5 2021-06-22 stsp len >>= 4;
1523 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1524 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1525 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1526 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1527 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1528 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1531 e6bcace5 2021-06-22 stsp *hdrlen = i;
1532 e6bcace5 2021-06-22 stsp return NULL;
1535 e6bcace5 2021-06-22 stsp static int
1536 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1539 e6bcace5 2021-06-22 stsp char rbuf[8];
1541 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1542 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1543 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1544 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1548 e6bcace5 2021-06-22 stsp while (i > 0)
1549 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1553 e6bcace5 2021-06-22 stsp static const struct got_error *
1554 c7a4fcc8 2023-02-17 op deltahdr(off_t *packfile_size, SHA1_CTX *ctx, int packfd, int force_refdelta,
1555 67fd6849 2022-02-13 stsp struct got_pack_meta *m)
1557 5060d5a1 2022-01-10 stsp const struct got_error *err;
1558 5060d5a1 2022-01-10 stsp char buf[32];
1561 c7a4fcc8 2023-02-17 op if (m->prev->off != 0 && !force_refdelta) {
1562 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1563 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1565 5060d5a1 2022-01-10 stsp return err;
1566 5060d5a1 2022-01-10 stsp nh += packoff(buf + nh, m->off - m->prev->off);
1567 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, nh, ctx);
1569 5060d5a1 2022-01-10 stsp return err;
1570 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1572 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1573 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1575 5060d5a1 2022-01-10 stsp return err;
1576 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, nh, ctx);
1578 5060d5a1 2022-01-10 stsp return err;
1579 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1580 894e4711 2022-10-15 stsp err = hwrite(packfd, m->prev->id.sha1,
1581 5060d5a1 2022-01-10 stsp sizeof(m->prev->id.sha1), ctx);
1583 5060d5a1 2022-01-10 stsp return err;
1584 5060d5a1 2022-01-10 stsp *packfile_size += sizeof(m->prev->id.sha1);
1587 5060d5a1 2022-01-10 stsp return NULL;
1590 5060d5a1 2022-01-10 stsp static const struct got_error *
1591 894e4711 2022-10-15 stsp write_packed_object(off_t *packfile_size, int packfd,
1592 e93fb944 2022-05-10 stsp FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1593 e93fb944 2022-05-10 stsp struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
1594 c7a4fcc8 2023-02-17 op struct got_repository *repo, int force_refdelta)
1596 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
1597 67fd6849 2022-02-13 stsp struct got_deflate_checksum csum;
1598 67fd6849 2022-02-13 stsp char buf[32];
1600 67fd6849 2022-02-13 stsp struct got_raw_object *raw = NULL;
1601 24b7de1c 2022-12-03 stsp off_t outlen, delta_offset;
1603 67fd6849 2022-02-13 stsp csum.output_sha1 = ctx;
1604 67fd6849 2022-02-13 stsp csum.output_crc = NULL;
1606 24b7de1c 2022-12-03 stsp if (m->reused_delta_offset)
1607 24b7de1c 2022-12-03 stsp delta_offset = m->reused_delta_offset;
1609 24b7de1c 2022-12-03 stsp delta_offset = m->delta_offset;
1611 894e4711 2022-10-15 stsp m->off = *packfile_size;
1612 67fd6849 2022-02-13 stsp if (m->delta_len == 0) {
1613 67fd6849 2022-02-13 stsp err = got_object_raw_open(&raw, outfd, repo, &m->id);
1615 67fd6849 2022-02-13 stsp goto done;
1616 67fd6849 2022-02-13 stsp err = packhdr(&nh, buf, sizeof(buf),
1617 67fd6849 2022-02-13 stsp m->obj_type, raw->size);
1619 67fd6849 2022-02-13 stsp goto done;
1620 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, nh, ctx);
1622 67fd6849 2022-02-13 stsp goto done;
1623 67fd6849 2022-02-13 stsp *packfile_size += nh;
1624 67fd6849 2022-02-13 stsp if (raw->f == NULL) {
1625 894e4711 2022-10-15 stsp err = got_deflate_to_fd_mmap(&outlen,
1626 67fd6849 2022-02-13 stsp raw->data + raw->hdrlen, 0, raw->size,
1627 894e4711 2022-10-15 stsp packfd, &csum);
1629 67fd6849 2022-02-13 stsp goto done;
1631 67fd6849 2022-02-13 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1633 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1634 67fd6849 2022-02-13 stsp goto done;
1636 894e4711 2022-10-15 stsp err = got_deflate_to_fd(&outlen, raw->f,
1637 894e4711 2022-10-15 stsp raw->size, packfd, &csum);
1639 67fd6849 2022-02-13 stsp goto done;
1641 67fd6849 2022-02-13 stsp *packfile_size += outlen;
1642 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
1643 67fd6849 2022-02-13 stsp raw = NULL;
1644 67fd6849 2022-02-13 stsp } else if (m->delta_buf) {
1645 c7a4fcc8 2023-02-17 op err = deltahdr(packfile_size, ctx, packfd, force_refdelta, m);
1647 67fd6849 2022-02-13 stsp goto done;
1648 894e4711 2022-10-15 stsp err = hwrite(packfd, m->delta_buf,
1649 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
1651 67fd6849 2022-02-13 stsp goto done;
1652 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
1653 67fd6849 2022-02-13 stsp free(m->delta_buf);
1654 67fd6849 2022-02-13 stsp m->delta_buf = NULL;
1655 e93fb944 2022-05-10 stsp } else if (delta_cache_map) {
1656 c7a4fcc8 2023-02-17 op err = deltahdr(packfile_size, ctx, packfd, force_refdelta, m);
1658 e93fb944 2022-05-10 stsp goto done;
1659 24b7de1c 2022-12-03 stsp err = hcopy_mmap(delta_cache_map, delta_offset,
1660 894e4711 2022-10-15 stsp delta_cache_size, packfd, m->delta_compressed_len,
1663 e93fb944 2022-05-10 stsp goto done;
1664 e93fb944 2022-05-10 stsp *packfile_size += m->delta_compressed_len;
1666 24b7de1c 2022-12-03 stsp if (fseeko(delta_cache, delta_offset, SEEK_SET) == -1) {
1667 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1668 67fd6849 2022-02-13 stsp goto done;
1670 c7a4fcc8 2023-02-17 op err = deltahdr(packfile_size, ctx, packfd, force_refdelta, m);
1672 67fd6849 2022-02-13 stsp goto done;
1673 894e4711 2022-10-15 stsp err = hcopy(delta_cache, packfd,
1674 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
1676 67fd6849 2022-02-13 stsp goto done;
1677 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
1681 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
1682 67fd6849 2022-02-13 stsp return err;
1685 67fd6849 2022-02-13 stsp static const struct got_error *
1686 24b7de1c 2022-12-03 stsp genpack(uint8_t *pack_sha1, int packfd, struct got_pack *reuse_pack,
1687 24b7de1c 2022-12-03 stsp FILE *delta_cache, struct got_pack_meta **deltify, int ndeltify,
1688 67fd6849 2022-02-13 stsp struct got_pack_meta **reuse, int nreuse,
1689 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees, int nours,
1690 c7a4fcc8 2023-02-17 op struct got_repository *repo, int force_refdelta,
1691 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1692 211cfef0 2022-01-05 stsp struct got_ratelimit *rl,
1693 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1695 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1697 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1698 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1699 08347b73 2021-10-14 stsp char buf[32];
1700 67fd6849 2022-02-13 stsp off_t packfile_size = 0;
1701 cc7a354a 2021-10-15 stsp int outfd = -1;
1702 e93fb944 2022-05-10 stsp int delta_cache_fd = -1;
1703 e93fb944 2022-05-10 stsp uint8_t *delta_cache_map = NULL;
1704 e93fb944 2022-05-10 stsp size_t delta_cache_size = 0;
1705 24b7de1c 2022-12-03 stsp FILE *packfile = NULL;
1707 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1709 e93fb944 2022-05-10 stsp #ifndef GOT_PACK_NO_MMAP
1710 e93fb944 2022-05-10 stsp delta_cache_fd = dup(fileno(delta_cache));
1711 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1) {
1712 e93fb944 2022-05-10 stsp struct stat sb;
1713 e93fb944 2022-05-10 stsp if (fstat(delta_cache_fd, &sb) == -1) {
1714 e93fb944 2022-05-10 stsp err = got_error_from_errno("fstat");
1715 e93fb944 2022-05-10 stsp goto done;
1717 e93fb944 2022-05-10 stsp if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1718 e93fb944 2022-05-10 stsp delta_cache_map = mmap(NULL, sb.st_size,
1719 e93fb944 2022-05-10 stsp PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1720 e93fb944 2022-05-10 stsp if (delta_cache_map == MAP_FAILED) {
1721 e93fb944 2022-05-10 stsp if (errno != ENOMEM) {
1722 e93fb944 2022-05-10 stsp err = got_error_from_errno("mmap");
1723 e93fb944 2022-05-10 stsp goto done;
1725 e93fb944 2022-05-10 stsp delta_cache_map = NULL; /* fallback on stdio */
1727 e93fb944 2022-05-10 stsp delta_cache_size = (size_t)sb.st_size;
1731 894e4711 2022-10-15 stsp err = hwrite(packfd, "PACK", 4, &ctx);
1733 e93fb944 2022-05-10 stsp goto done;
1734 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1735 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, 4, &ctx);
1737 e6bcace5 2021-06-22 stsp goto done;
1738 67fd6849 2022-02-13 stsp putbe32(buf, ndeltify + nreuse);
1739 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, 4, &ctx);
1741 e6bcace5 2021-06-22 stsp goto done;
1743 67fd6849 2022-02-13 stsp qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1744 67fd6849 2022-02-13 stsp write_order_cmp);
1745 67fd6849 2022-02-13 stsp for (i = 0; i < ndeltify; i++) {
1746 301e83b3 2022-10-16 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
1747 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
1748 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, i);
1750 211cfef0 2022-01-05 stsp goto done;
1751 67fd6849 2022-02-13 stsp m = deltify[i];
1752 894e4711 2022-10-15 stsp err = write_packed_object(&packfile_size, packfd,
1753 e93fb944 2022-05-10 stsp delta_cache, delta_cache_map, delta_cache_size,
1754 c7a4fcc8 2023-02-17 op m, &outfd, &ctx, repo, force_refdelta);
1756 67fd6849 2022-02-13 stsp goto done;
1759 67fd6849 2022-02-13 stsp qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1760 67fd6849 2022-02-13 stsp reuse_write_order_cmp);
1761 24b7de1c 2022-12-03 stsp if (nreuse > 0 && reuse_pack->map == NULL) {
1762 24b7de1c 2022-12-03 stsp int fd = dup(reuse_pack->fd);
1763 24b7de1c 2022-12-03 stsp if (fd == -1) {
1764 24b7de1c 2022-12-03 stsp err = got_error_from_errno("dup");
1765 24b7de1c 2022-12-03 stsp goto done;
1767 24b7de1c 2022-12-03 stsp packfile = fdopen(fd, "r");
1768 24b7de1c 2022-12-03 stsp if (packfile == NULL) {
1769 24b7de1c 2022-12-03 stsp err = got_error_from_errno("fdopen");
1770 24b7de1c 2022-12-03 stsp close(fd);
1771 24b7de1c 2022-12-03 stsp goto done;
1774 67fd6849 2022-02-13 stsp for (i = 0; i < nreuse; i++) {
1775 301e83b3 2022-10-16 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
1776 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
1777 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1779 67fd6849 2022-02-13 stsp goto done;
1780 67fd6849 2022-02-13 stsp m = reuse[i];
1781 894e4711 2022-10-15 stsp err = write_packed_object(&packfile_size, packfd,
1782 24b7de1c 2022-12-03 stsp packfile, reuse_pack->map, reuse_pack->filesize,
1783 c7a4fcc8 2023-02-17 op m, &outfd, &ctx, repo, force_refdelta);
1785 67fd6849 2022-02-13 stsp goto done;
1788 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1789 13b2bc37 2022-10-23 stsp err = got_poll_write_full(packfd, pack_sha1, SHA1_DIGEST_LENGTH);
1791 894e4711 2022-10-15 stsp goto done;
1792 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1793 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1794 211cfef0 2022-01-05 stsp if (progress_cb) {
1795 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1796 b8af7c06 2022-03-15 stsp packfile_size, nours, ndeltify + nreuse,
1797 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse);
1799 211cfef0 2022-01-05 stsp goto done;
1802 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
1803 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
1804 e93fb944 2022-05-10 stsp if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1805 e93fb944 2022-05-10 stsp err = got_error_from_errno("munmap");
1806 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1807 e93fb944 2022-05-10 stsp err = got_error_from_errno("close");
1808 24b7de1c 2022-12-03 stsp if (packfile && fclose(packfile) == EOF && err == NULL)
1809 24b7de1c 2022-12-03 stsp err = got_error_from_errno("fclose");
1810 e6bcace5 2021-06-22 stsp return err;
1813 67fd6849 2022-02-13 stsp static const struct got_error *
1814 67fd6849 2022-02-13 stsp add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1816 67fd6849 2022-02-13 stsp struct got_pack_meta *m = data;
1817 67fd6849 2022-02-13 stsp struct got_pack_metavec *v = arg;
1819 411cbec1 2022-05-20 stsp if (m->reused_delta_offset != 0)
1820 f5e78e05 2022-05-04 stsp return NULL;
1822 301e83b3 2022-10-16 stsp return got_pack_add_meta(m, v);
1825 e6bcace5 2021-06-22 stsp const struct got_error *
1826 a32780aa 2022-10-15 stsp got_pack_create(uint8_t *packsha1, int packfd, FILE *delta_cache,
1827 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1828 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1829 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1830 c7a4fcc8 2023-02-17 op int force_refdelta, got_pack_progress_cb progress_cb, void *progress_arg,
1831 cae60ab8 2022-10-18 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1833 e6bcace5 2021-06-22 stsp const struct got_error *err;
1834 67fd6849 2022-02-13 stsp struct got_object_idset *idset;
1835 24b7de1c 2022-12-03 stsp struct got_packidx *reuse_packidx = NULL;
1836 24b7de1c 2022-12-03 stsp struct got_pack *reuse_pack = NULL;
1837 67fd6849 2022-02-13 stsp struct got_pack_metavec deltify, reuse;
1838 b8af7c06 2022-03-15 stsp int ncolored = 0, nfound = 0, ntrees = 0;
1839 f5e78e05 2022-05-04 stsp size_t ndeltify;
1840 d6a28ffe 2022-05-20 op uint32_t seed;
1842 d6a28ffe 2022-05-20 op seed = arc4random();
1844 67fd6849 2022-02-13 stsp memset(&deltify, 0, sizeof(deltify));
1845 67fd6849 2022-02-13 stsp memset(&reuse, 0, sizeof(reuse));
1847 67fd6849 2022-02-13 stsp idset = got_object_idset_alloc();
1848 67fd6849 2022-02-13 stsp if (idset == NULL)
1849 67fd6849 2022-02-13 stsp return got_error_from_errno("got_object_idset_alloc");
1851 b8af7c06 2022-03-15 stsp err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1852 d6a28ffe 2022-05-20 op ntheirs, ours, nours, repo, seed, loose_obj_only,
1853 cae60ab8 2022-10-18 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1855 17259bfa 2022-05-19 stsp goto done;
1857 28526235 2022-02-13 stsp if (progress_cb) {
1858 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1859 b8af7c06 2022-03-15 stsp 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1861 28526235 2022-02-13 stsp goto done;
1864 67fd6849 2022-02-13 stsp if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1865 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1866 05118f5a 2021-06-22 stsp goto done;
1869 67fd6849 2022-02-13 stsp reuse.metasz = 64;
1870 67fd6849 2022-02-13 stsp reuse.meta = calloc(reuse.metasz,
1871 67fd6849 2022-02-13 stsp sizeof(struct got_pack_meta *));
1872 67fd6849 2022-02-13 stsp if (reuse.meta == NULL) {
1873 67fd6849 2022-02-13 stsp err = got_error_from_errno("calloc");
1874 67fd6849 2022-02-13 stsp goto done;
1877 24b7de1c 2022-12-03 stsp err = got_pack_search_deltas(&reuse_packidx, &reuse_pack,
1878 24b7de1c 2022-12-03 stsp &reuse, idset, ncolored, nfound, ntrees, nours,
1879 cae60ab8 2022-10-18 stsp repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1881 67fd6849 2022-02-13 stsp goto done;
1883 24b7de1c 2022-12-03 stsp if (reuse_packidx && reuse_pack) {
1884 24b7de1c 2022-12-03 stsp err = got_repo_pin_pack(repo, reuse_packidx, reuse_pack);
1886 24b7de1c 2022-12-03 stsp goto done;
1889 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1890 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1891 67fd6849 2022-02-13 stsp goto done;
1894 f5e78e05 2022-05-04 stsp ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1895 f5e78e05 2022-05-04 stsp if (ndeltify > 0) {
1896 f5e78e05 2022-05-04 stsp deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1897 f5e78e05 2022-05-04 stsp if (deltify.meta == NULL) {
1898 f5e78e05 2022-05-04 stsp err = got_error_from_errno("calloc");
1899 f5e78e05 2022-05-04 stsp goto done;
1901 f5e78e05 2022-05-04 stsp deltify.metasz = ndeltify;
1903 f5e78e05 2022-05-04 stsp err = got_object_idset_for_each(idset, add_meta_idset_cb,
1904 f5e78e05 2022-05-04 stsp &deltify);
1906 f8a36e22 2021-08-26 stsp goto done;
1907 f5e78e05 2022-05-04 stsp if (deltify.nmeta > 0) {
1908 f5e78e05 2022-05-04 stsp err = pick_deltas(deltify.meta, deltify.nmeta,
1909 f5e78e05 2022-05-04 stsp ncolored, nfound, ntrees, nours, reuse.nmeta,
1910 cae60ab8 2022-10-18 stsp delta_cache, repo, progress_cb, progress_arg, rl,
1911 f5e78e05 2022-05-04 stsp cancel_cb, cancel_arg);
1913 f5e78e05 2022-05-04 stsp goto done;
1917 2d9e6abf 2022-05-04 stsp if (fflush(delta_cache) == EOF) {
1918 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fflush");
1919 2d9e6abf 2022-05-04 stsp goto done;
1922 13b2bc37 2022-10-23 stsp if (progress_cb) {
1924 13b2bc37 2022-10-23 stsp * Report a 1-byte packfile write to indicate we are about
1925 13b2bc37 2022-10-23 stsp * to start sending packfile data. gotd(8) needs this.
1927 13b2bc37 2022-10-23 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1928 13b2bc37 2022-10-23 stsp 1 /* packfile_size */, nours,
1929 13b2bc37 2022-10-23 stsp got_object_idset_num_elements(idset),
1930 13b2bc37 2022-10-23 stsp deltify.nmeta + reuse.nmeta, 0);
1932 13b2bc37 2022-10-23 stsp goto done;
1935 24b7de1c 2022-12-03 stsp err = genpack(packsha1, packfd, reuse_pack, delta_cache, deltify.meta,
1936 b8af7c06 2022-03-15 stsp deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1937 c7a4fcc8 2023-02-17 op nours, repo, force_refdelta, progress_cb, progress_arg, rl,
1938 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1940 e6bcace5 2021-06-22 stsp goto done;
1942 67fd6849 2022-02-13 stsp free_nmeta(deltify.meta, deltify.nmeta);
1943 67fd6849 2022-02-13 stsp free_nmeta(reuse.meta, reuse.nmeta);
1944 67fd6849 2022-02-13 stsp got_object_idset_free(idset);
1945 24b7de1c 2022-12-03 stsp got_repo_unpin_pack(repo);
1946 e6bcace5 2021-06-22 stsp return err;