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>
25 e3f86256 2022-02-18 naddy #include <endian.h>
26 08736cf9 2021-06-23 stsp #include <stdint.h>
27 05118f5a 2021-06-22 stsp #include <imsg.h>
28 e6bcace5 2021-06-22 stsp #include <stdio.h>
29 e6bcace5 2021-06-22 stsp #include <stdlib.h>
30 e6bcace5 2021-06-22 stsp #include <string.h>
31 e6bcace5 2021-06-22 stsp #include <sha1.h>
32 211cfef0 2022-01-05 stsp #include <time.h>
33 bfc73a47 2022-03-19 naddy #include <unistd.h>
34 e6bcace5 2021-06-22 stsp #include <limits.h>
35 e6bcace5 2021-06-22 stsp #include <zlib.h>
37 e6bcace5 2021-06-22 stsp #include "got_error.h"
38 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
39 e6bcace5 2021-06-22 stsp #include "got_object.h"
40 f8b19efd 2021-10-13 stsp #include "got_path.h"
41 05118f5a 2021-06-22 stsp #include "got_reference.h"
42 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
43 08347b73 2021-10-14 stsp #include "got_opentemp.h"
45 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
46 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
47 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
49 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
50 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
51 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
52 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
53 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
54 211cfef0 2022-01-05 stsp #include "got_lib_ratelimit.h"
57 74881701 2021-10-15 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
61 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
64 70f8f24d 2022-04-14 stsp #ifndef nitems
65 70f8f24d 2022-04-14 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
68 e6bcace5 2021-06-22 stsp struct got_pack_meta {
69 e6bcace5 2021-06-22 stsp struct got_object_id id;
71 e6bcace5 2021-06-22 stsp int obj_type;
73 e6bcace5 2021-06-22 stsp time_t mtime;
75 e6bcace5 2021-06-22 stsp /* The best delta we picked */
76 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
77 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
78 5060d5a1 2022-01-10 stsp unsigned char *delta_buf; /* if not encoded in delta cache file */
79 74881701 2021-10-15 stsp off_t delta_offset; /* offset in delta cache file */
80 5060d5a1 2022-01-10 stsp off_t delta_len; /* encoded delta length */
83 67fd6849 2022-02-13 stsp int have_reused_delta;
84 67fd6849 2022-02-13 stsp off_t reused_delta_offset; /* offset of delta in reused pack file */
85 67fd6849 2022-02-13 stsp struct got_object_id *base_obj_id;
87 e6bcace5 2021-06-22 stsp /* Only used for delta window */
88 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
90 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
94 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
95 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
100 e6bcace5 2021-06-22 stsp static const struct got_error *
101 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
102 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
104 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
105 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
107 e6bcace5 2021-06-22 stsp *new = NULL;
109 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
110 e6bcace5 2021-06-22 stsp if (m == NULL)
111 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
113 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
115 e6bcace5 2021-06-22 stsp m->path = strdup(path);
116 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
117 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
119 e6bcace5 2021-06-22 stsp return err;
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 e6bcace5 2021-06-22 stsp free(meta->path);
134 e6bcace5 2021-06-22 stsp meta->path = NULL;
135 5060d5a1 2022-01-10 stsp free(meta->delta_buf);
136 5060d5a1 2022-01-10 stsp meta->delta_buf = NULL;
137 67fd6849 2022-02-13 stsp free(meta->base_obj_id);
138 67fd6849 2022-02-13 stsp meta->base_obj_id = NULL;
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;
157 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
158 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
160 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
161 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
162 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
163 e6bcace5 2021-06-22 stsp if (cmp != 0)
164 e6bcace5 2021-06-22 stsp return cmp;
165 e6bcace5 2021-06-22 stsp if (a->mtime != b->mtime)
166 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
167 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
170 5060d5a1 2022-01-10 stsp static off_t
171 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
174 5060d5a1 2022-01-10 stsp off_t size = 32;
175 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
176 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
177 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
179 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
181 e6bcace5 2021-06-22 stsp return size;
184 5060d5a1 2022-01-10 stsp static const struct got_error *
185 5060d5a1 2022-01-10 stsp append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
189 5060d5a1 2022-01-10 stsp if (*len + nseg >= *sz) {
190 5060d5a1 2022-01-10 stsp while (*len + nseg >= *sz)
191 5060d5a1 2022-01-10 stsp *sz += *sz / 2;
192 5060d5a1 2022-01-10 stsp n = realloc(*p, *sz);
193 5060d5a1 2022-01-10 stsp if (n == NULL)
194 5060d5a1 2022-01-10 stsp return got_error_from_errno("realloc");
197 5060d5a1 2022-01-10 stsp memcpy(*p + *len, seg, nseg);
198 5060d5a1 2022-01-10 stsp *len += nseg;
199 5060d5a1 2022-01-10 stsp return NULL;
202 5060d5a1 2022-01-10 stsp static const struct got_error *
203 5060d5a1 2022-01-10 stsp encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
204 5060d5a1 2022-01-10 stsp struct got_delta_instruction *deltas, int ndeltas,
205 5060d5a1 2022-01-10 stsp off_t delta_size, off_t base_size)
207 5060d5a1 2022-01-10 stsp const struct got_error *err;
208 5060d5a1 2022-01-10 stsp unsigned char buf[16], *bp;
210 5060d5a1 2022-01-10 stsp size_t len = 0;
212 5060d5a1 2022-01-10 stsp struct got_delta_instruction *d;
214 5060d5a1 2022-01-10 stsp m->delta_buf = malloc(delta_size);
215 5060d5a1 2022-01-10 stsp if (m->delta_buf == NULL)
216 5060d5a1 2022-01-10 stsp return got_error_from_errno("calloc");
218 5060d5a1 2022-01-10 stsp /* base object size */
219 5060d5a1 2022-01-10 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
220 5060d5a1 2022-01-10 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
221 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
222 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
223 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
224 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
226 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size, buf, i);
228 5060d5a1 2022-01-10 stsp return err;
230 5060d5a1 2022-01-10 stsp /* target object size */
231 5060d5a1 2022-01-10 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
232 5060d5a1 2022-01-10 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
233 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
234 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
235 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
236 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
238 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size, buf, i);
240 5060d5a1 2022-01-10 stsp return err;
242 5060d5a1 2022-01-10 stsp for (j = 0; j < ndeltas; j++) {
243 5060d5a1 2022-01-10 stsp d = &deltas[j];
244 5060d5a1 2022-01-10 stsp if (d->copy) {
245 5060d5a1 2022-01-10 stsp n = d->offset;
246 5060d5a1 2022-01-10 stsp bp = &buf[1];
247 5060d5a1 2022-01-10 stsp buf[0] = GOT_DELTA_BASE_COPY;
248 5060d5a1 2022-01-10 stsp for (i = 0; i < 4; i++) {
249 5060d5a1 2022-01-10 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
250 5060d5a1 2022-01-10 stsp buf[0] |= 1 << i;
251 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
253 5060d5a1 2022-01-10 stsp if (n == 0)
257 5060d5a1 2022-01-10 stsp n = d->len;
258 5060d5a1 2022-01-10 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
259 5060d5a1 2022-01-10 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
260 5060d5a1 2022-01-10 stsp for (i = 0; i < 3 && n > 0; i++) {
261 5060d5a1 2022-01-10 stsp buf[0] |= 1 << (i + 4);
262 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
266 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
267 5060d5a1 2022-01-10 stsp buf, bp - buf);
269 5060d5a1 2022-01-10 stsp return err;
270 5060d5a1 2022-01-10 stsp } else if (o->f == NULL) {
272 5060d5a1 2022-01-10 stsp while (n != d->len) {
273 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
274 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
277 5060d5a1 2022-01-10 stsp return err;
278 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
279 5060d5a1 2022-01-10 stsp o->data + o->hdrlen + d->offset + n,
282 5060d5a1 2022-01-10 stsp return err;
283 5060d5a1 2022-01-10 stsp n += buf[0];
286 5060d5a1 2022-01-10 stsp char content[128];
288 5060d5a1 2022-01-10 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
289 5060d5a1 2022-01-10 stsp return got_error_from_errno("fseeko");
291 5060d5a1 2022-01-10 stsp while (n != d->len) {
292 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
293 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
296 5060d5a1 2022-01-10 stsp return err;
297 5060d5a1 2022-01-10 stsp r = fread(content, 1, buf[0], o->f);
298 5060d5a1 2022-01-10 stsp if (r != buf[0])
299 5060d5a1 2022-01-10 stsp return got_ferror(o->f, GOT_ERR_IO);
300 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
301 5060d5a1 2022-01-10 stsp content, buf[0]);
303 5060d5a1 2022-01-10 stsp return err;
304 5060d5a1 2022-01-10 stsp n += buf[0];
309 5060d5a1 2022-01-10 stsp m->delta_len = len;
310 5060d5a1 2022-01-10 stsp return NULL;
313 74881701 2021-10-15 stsp static const struct got_error *
314 74881701 2021-10-15 stsp encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
315 74881701 2021-10-15 stsp struct got_delta_instruction *deltas, int ndeltas,
316 a319ca8c 2021-10-15 stsp off_t base_size, FILE *f)
318 a319ca8c 2021-10-15 stsp unsigned char buf[16], *bp;
322 a319ca8c 2021-10-15 stsp struct got_delta_instruction *d;
324 a319ca8c 2021-10-15 stsp /* base object size */
325 a319ca8c 2021-10-15 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
326 a319ca8c 2021-10-15 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
327 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
328 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
329 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
330 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
332 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, i, f);
333 a319ca8c 2021-10-15 stsp if (w != i)
334 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
336 a319ca8c 2021-10-15 stsp /* target object size */
337 a319ca8c 2021-10-15 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
338 a319ca8c 2021-10-15 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
339 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
340 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
341 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
342 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
344 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, i, f);
345 a319ca8c 2021-10-15 stsp if (w != i)
346 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
348 a319ca8c 2021-10-15 stsp for (j = 0; j < ndeltas; j++) {
349 a319ca8c 2021-10-15 stsp d = &deltas[j];
350 a319ca8c 2021-10-15 stsp if (d->copy) {
351 a319ca8c 2021-10-15 stsp n = d->offset;
352 a319ca8c 2021-10-15 stsp bp = &buf[1];
353 a319ca8c 2021-10-15 stsp buf[0] = GOT_DELTA_BASE_COPY;
354 a319ca8c 2021-10-15 stsp for (i = 0; i < 4; i++) {
355 a319ca8c 2021-10-15 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
356 a319ca8c 2021-10-15 stsp buf[0] |= 1 << i;
357 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
359 a319ca8c 2021-10-15 stsp if (n == 0)
363 a319ca8c 2021-10-15 stsp n = d->len;
364 a319ca8c 2021-10-15 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
365 a319ca8c 2021-10-15 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
366 a319ca8c 2021-10-15 stsp for (i = 0; i < 3 && n > 0; i++) {
367 a319ca8c 2021-10-15 stsp buf[0] |= 1 << (i + 4);
368 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
372 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, bp - buf, f);
373 a319ca8c 2021-10-15 stsp if (w != bp - buf)
374 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
375 64a8571e 2022-01-07 stsp } else if (o->f == NULL) {
377 64a8571e 2022-01-07 stsp while (n != d->len) {
378 64a8571e 2022-01-07 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
379 64a8571e 2022-01-07 stsp w = fwrite(buf, 1, 1, f);
380 64a8571e 2022-01-07 stsp if (w != 1)
381 64a8571e 2022-01-07 stsp return got_ferror(f, GOT_ERR_IO);
382 64a8571e 2022-01-07 stsp w = fwrite(o->data + o->hdrlen + d->offset + n,
383 64a8571e 2022-01-07 stsp 1, buf[0], f);
384 64a8571e 2022-01-07 stsp if (w != buf[0])
385 64a8571e 2022-01-07 stsp return got_ferror(f, GOT_ERR_IO);
386 64a8571e 2022-01-07 stsp n += buf[0];
389 a319ca8c 2021-10-15 stsp char content[128];
391 a319ca8c 2021-10-15 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
392 a319ca8c 2021-10-15 stsp return got_error_from_errno("fseeko");
394 a319ca8c 2021-10-15 stsp while (n != d->len) {
395 a319ca8c 2021-10-15 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
396 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, 1, f);
397 a319ca8c 2021-10-15 stsp if (w != 1)
398 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
399 a319ca8c 2021-10-15 stsp r = fread(content, 1, buf[0], o->f);
400 a319ca8c 2021-10-15 stsp if (r != buf[0])
401 a319ca8c 2021-10-15 stsp return got_ferror(o->f, GOT_ERR_IO);
402 a319ca8c 2021-10-15 stsp w = fwrite(content, 1, buf[0], f);
403 a319ca8c 2021-10-15 stsp if (w != buf[0])
404 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
405 a319ca8c 2021-10-15 stsp n += buf[0];
410 5060d5a1 2022-01-10 stsp m->delta_len = ftello(f) - m->delta_offset;
411 a319ca8c 2021-10-15 stsp return NULL;
414 211cfef0 2022-01-05 stsp static const struct got_error *
415 211cfef0 2022-01-05 stsp report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
416 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
417 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
418 b8af7c06 2022-03-15 stsp int nobj_written)
420 211cfef0 2022-01-05 stsp const struct got_error *err;
421 211cfef0 2022-01-05 stsp int elapsed;
423 211cfef0 2022-01-05 stsp if (progress_cb == NULL)
424 211cfef0 2022-01-05 stsp return NULL;
426 211cfef0 2022-01-05 stsp err = got_ratelimit_check(&elapsed, rl);
427 211cfef0 2022-01-05 stsp if (err || !elapsed)
428 211cfef0 2022-01-05 stsp return err;
430 b8af7c06 2022-03-15 stsp return progress_cb(progress_arg, ncolored, nfound, ntrees,
431 b8af7c06 2022-03-15 stsp packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
434 e6bcace5 2021-06-22 stsp static const struct got_error *
435 67fd6849 2022-02-13 stsp add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
437 67fd6849 2022-02-13 stsp if (v->nmeta == v->metasz){
438 67fd6849 2022-02-13 stsp size_t newsize = 2 * v->metasz;
439 67fd6849 2022-02-13 stsp struct got_pack_meta **new;
440 67fd6849 2022-02-13 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
441 67fd6849 2022-02-13 stsp if (new == NULL)
442 67fd6849 2022-02-13 stsp return got_error_from_errno("reallocarray");
443 67fd6849 2022-02-13 stsp v->meta = new;
444 67fd6849 2022-02-13 stsp v->metasz = newsize;
447 67fd6849 2022-02-13 stsp v->meta[v->nmeta++] = m;
448 67fd6849 2022-02-13 stsp return NULL;
451 67fd6849 2022-02-13 stsp static const struct got_error *
452 67fd6849 2022-02-13 stsp reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
453 67fd6849 2022-02-13 stsp struct got_object_idset *idset, struct got_pack *pack,
454 67fd6849 2022-02-13 stsp struct got_packidx *packidx, int delta_cache_fd,
455 67fd6849 2022-02-13 stsp struct got_repository *repo)
457 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
458 67fd6849 2022-02-13 stsp struct got_pack_meta *base = NULL;
459 67fd6849 2022-02-13 stsp struct got_object_id *base_obj_id = NULL;
460 67fd6849 2022-02-13 stsp off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
461 67fd6849 2022-02-13 stsp uint64_t base_size, result_size;
463 67fd6849 2022-02-13 stsp if (m->have_reused_delta)
464 67fd6849 2022-02-13 stsp return NULL;
466 67fd6849 2022-02-13 stsp err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
467 67fd6849 2022-02-13 stsp &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
468 67fd6849 2022-02-13 stsp packidx, idx, &m->id, repo);
470 67fd6849 2022-02-13 stsp return err;
472 67fd6849 2022-02-13 stsp if (delta_offset + delta_len < delta_offset)
473 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKFILE);
475 67fd6849 2022-02-13 stsp base = got_object_idset_get(idset, base_obj_id);
476 67fd6849 2022-02-13 stsp if (base == NULL)
479 67fd6849 2022-02-13 stsp m->delta_len = delta_len;
480 67fd6849 2022-02-13 stsp m->delta_offset = delta_cache_offset;
481 67fd6849 2022-02-13 stsp m->prev = base;
482 67fd6849 2022-02-13 stsp m->size = result_size;
483 67fd6849 2022-02-13 stsp m->have_reused_delta = 1;
484 67fd6849 2022-02-13 stsp m->reused_delta_offset = delta_offset;
485 67fd6849 2022-02-13 stsp m->base_obj_id = base_obj_id;
486 67fd6849 2022-02-13 stsp base_obj_id = NULL;
487 67fd6849 2022-02-13 stsp err = add_meta(m, v);
489 67fd6849 2022-02-13 stsp free(base_obj_id);
490 67fd6849 2022-02-13 stsp return err;
493 67fd6849 2022-02-13 stsp static const struct got_error *
494 67fd6849 2022-02-13 stsp find_pack_for_reuse(struct got_packidx **best_packidx,
495 67fd6849 2022-02-13 stsp struct got_repository *repo)
497 9b576444 2022-03-14 stsp const struct got_error *err = NULL;
498 67fd6849 2022-02-13 stsp struct got_pathlist_entry *pe;
499 67fd6849 2022-02-13 stsp const char *best_packidx_path = NULL;
500 67fd6849 2022-02-13 stsp int nobj_max = 0;
502 67fd6849 2022-02-13 stsp *best_packidx = NULL;
504 9b576444 2022-03-14 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
505 67fd6849 2022-02-13 stsp const char *path_packidx = pe->path;
506 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
509 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
513 67fd6849 2022-02-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
514 67fd6849 2022-02-13 stsp if (nobj > nobj_max) {
515 67fd6849 2022-02-13 stsp best_packidx_path = path_packidx;
516 67fd6849 2022-02-13 stsp nobj_max = nobj;
520 67fd6849 2022-02-13 stsp if (best_packidx_path) {
521 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
525 67fd6849 2022-02-13 stsp return err;
528 67fd6849 2022-02-13 stsp struct search_deltas_arg {
529 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
530 67fd6849 2022-02-13 stsp struct got_pack *pack;
531 67fd6849 2022-02-13 stsp struct got_object_idset *idset;
532 67fd6849 2022-02-13 stsp struct got_pack_metavec *v;
533 67fd6849 2022-02-13 stsp int delta_cache_fd;
534 67fd6849 2022-02-13 stsp struct got_repository *repo;
535 67fd6849 2022-02-13 stsp got_pack_progress_cb progress_cb;
536 67fd6849 2022-02-13 stsp void *progress_arg;
537 67fd6849 2022-02-13 stsp struct got_ratelimit *rl;
538 67fd6849 2022-02-13 stsp got_cancel_cb cancel_cb;
539 67fd6849 2022-02-13 stsp void *cancel_arg;
540 b8af7c06 2022-03-15 stsp int ncolored;
541 b8af7c06 2022-03-15 stsp int nfound;
542 b8af7c06 2022-03-15 stsp int ntrees;
543 67fd6849 2022-02-13 stsp int ncommits;
546 67fd6849 2022-02-13 stsp static const struct got_error *
547 67fd6849 2022-02-13 stsp search_delta_for_object(struct got_object_id *id, void *data, void *arg)
549 67fd6849 2022-02-13 stsp const struct got_error *err;
550 67fd6849 2022-02-13 stsp struct got_pack_meta *m = data;
551 67fd6849 2022-02-13 stsp struct search_deltas_arg *a = arg;
552 67fd6849 2022-02-13 stsp int obj_idx;
553 67fd6849 2022-02-13 stsp struct got_object *obj = NULL;
555 67fd6849 2022-02-13 stsp if (a->cancel_cb) {
556 67fd6849 2022-02-13 stsp err = (*a->cancel_cb)(a->cancel_arg);
558 67fd6849 2022-02-13 stsp return err;
561 67fd6849 2022-02-13 stsp if (!got_repo_check_packidx_bloom_filter(a->repo,
562 67fd6849 2022-02-13 stsp a->packidx->path_packidx, id))
563 67fd6849 2022-02-13 stsp return NULL;
565 67fd6849 2022-02-13 stsp obj_idx = got_packidx_get_object_idx(a->packidx, id);
566 67fd6849 2022-02-13 stsp if (obj_idx == -1)
567 67fd6849 2022-02-13 stsp return NULL;
570 67fd6849 2022-02-13 stsp * Opening and closing an object just to check its flags
571 67fd6849 2022-02-13 stsp * is a bit expensive. We could have an imsg which requests
572 67fd6849 2022-02-13 stsp * plain type/size information for an object without doing
573 67fd6849 2022-02-13 stsp * work such as traversing the object's entire delta chain
574 67fd6849 2022-02-13 stsp * to find the base object type, and other such info which
575 67fd6849 2022-02-13 stsp * we don't really need here.
577 67fd6849 2022-02-13 stsp err = got_object_open_from_packfile(&obj, &m->id, a->pack,
578 67fd6849 2022-02-13 stsp a->packidx, obj_idx, a->repo);
580 67fd6849 2022-02-13 stsp return err;
582 67fd6849 2022-02-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
583 67fd6849 2022-02-13 stsp reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
584 67fd6849 2022-02-13 stsp a->delta_cache_fd, a->repo);
587 67fd6849 2022-02-13 stsp err = report_progress(a->progress_cb, a->progress_arg, a->rl,
588 b8af7c06 2022-03-15 stsp a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
589 b8af7c06 2022-03-15 stsp got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
592 67fd6849 2022-02-13 stsp got_object_close(obj);
593 67fd6849 2022-02-13 stsp return err;
596 67fd6849 2022-02-13 stsp static const struct got_error *
597 67fd6849 2022-02-13 stsp search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
598 b8af7c06 2022-03-15 stsp int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
599 b8af7c06 2022-03-15 stsp struct got_repository *repo,
600 67fd6849 2022-02-13 stsp got_pack_progress_cb progress_cb, void *progress_arg,
601 67fd6849 2022-02-13 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
603 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
604 67fd6849 2022-02-13 stsp char *path_packfile = NULL;
605 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
606 67fd6849 2022-02-13 stsp struct got_pack *pack;
607 67fd6849 2022-02-13 stsp struct search_deltas_arg sda;
609 67fd6849 2022-02-13 stsp err = find_pack_for_reuse(&packidx, repo);
611 67fd6849 2022-02-13 stsp return err;
613 67fd6849 2022-02-13 stsp if (packidx == NULL)
614 67fd6849 2022-02-13 stsp return NULL;
616 67fd6849 2022-02-13 stsp err = got_packidx_get_packfile_path(&path_packfile,
617 67fd6849 2022-02-13 stsp packidx->path_packidx);
619 67fd6849 2022-02-13 stsp return err;
621 67fd6849 2022-02-13 stsp pack = got_repo_get_cached_pack(repo, path_packfile);
622 67fd6849 2022-02-13 stsp if (pack == NULL) {
623 67fd6849 2022-02-13 stsp err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
628 67fd6849 2022-02-13 stsp sda.packidx = packidx;
629 67fd6849 2022-02-13 stsp sda.pack = pack;
630 67fd6849 2022-02-13 stsp sda.idset = idset;
632 67fd6849 2022-02-13 stsp sda.delta_cache_fd = delta_cache_fd;
633 67fd6849 2022-02-13 stsp sda.repo = repo;
634 67fd6849 2022-02-13 stsp sda.progress_cb = progress_cb;
635 67fd6849 2022-02-13 stsp sda.progress_arg = progress_arg;
636 67fd6849 2022-02-13 stsp sda.rl = rl;
637 67fd6849 2022-02-13 stsp sda.cancel_cb = cancel_cb;
638 67fd6849 2022-02-13 stsp sda.cancel_arg = cancel_arg;
639 b8af7c06 2022-03-15 stsp sda.ncolored = ncolored;
640 b8af7c06 2022-03-15 stsp sda.nfound = nfound;
641 b8af7c06 2022-03-15 stsp sda.ntrees = ntrees;
642 67fd6849 2022-02-13 stsp sda.ncommits = ncommits;
643 67fd6849 2022-02-13 stsp err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
645 67fd6849 2022-02-13 stsp free(path_packfile);
646 67fd6849 2022-02-13 stsp return err;
649 67fd6849 2022-02-13 stsp static const struct got_error *
650 b8af7c06 2022-03-15 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
651 b8af7c06 2022-03-15 stsp int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
652 b8af7c06 2022-03-15 stsp struct got_repository *repo,
653 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
654 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
656 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
657 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
658 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
659 74881701 2021-10-15 stsp struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
660 5060d5a1 2022-01-10 stsp int i, j, ndeltas, best_ndeltas;
661 5060d5a1 2022-01-10 stsp off_t size, best_size;
662 4f4d853e 2021-10-24 stsp const int max_base_candidates = 3;
663 402a5ec1 2022-01-10 stsp size_t delta_memsize = 0;
664 402a5ec1 2022-01-10 stsp const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
665 cc7a354a 2021-10-15 stsp int outfd = -1;
667 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
668 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
669 e6bcace5 2021-06-22 stsp if (cancel_cb) {
670 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
674 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
675 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
676 b8af7c06 2022-03-15 stsp nreused + i, 0);
679 e6bcace5 2021-06-22 stsp m = meta[i];
681 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
682 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
685 94dac27c 2021-10-15 stsp err = got_object_raw_open(&raw, &outfd, repo, &m->id);
688 600b755e 2021-10-14 stsp m->size = raw->size;
690 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
691 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&m->dtab, raw->data,
692 64a8571e 2022-01-07 stsp raw->hdrlen, raw->size + raw->hdrlen);
694 64a8571e 2022-01-07 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
695 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen);
700 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
701 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
702 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
703 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
704 e6bcace5 2021-06-22 stsp n->dtab = NULL;
707 74881701 2021-10-15 stsp best_size = raw->size;
708 74881701 2021-10-15 stsp best_ndeltas = 0;
709 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
710 e6bcace5 2021-06-22 stsp if (cancel_cb) {
711 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
715 e6bcace5 2021-06-22 stsp base = meta[j];
716 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
717 22edbce7 2021-10-24 stsp if (base->nchain >= 128 ||
718 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
721 d3c116bf 2021-10-15 stsp err = got_object_raw_open(&base_raw, &outfd, repo,
722 94dac27c 2021-10-15 stsp &base->id);
726 64a8571e 2022-01-07 stsp if (raw->f == NULL && base_raw->f == NULL) {
727 64a8571e 2022-01-07 stsp err = got_deltify_mem_mem(&deltas, &ndeltas,
728 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
729 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
730 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
731 64a8571e 2022-01-07 stsp base_raw->hdrlen,
732 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
733 64a8571e 2022-01-07 stsp } else if (raw->f == NULL) {
734 64a8571e 2022-01-07 stsp err = got_deltify_mem_file(&deltas, &ndeltas,
735 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
736 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
737 64a8571e 2022-01-07 stsp base->dtab, base_raw->f,
738 64a8571e 2022-01-07 stsp base_raw->hdrlen,
739 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
740 64a8571e 2022-01-07 stsp } else if (base_raw->f == NULL) {
741 64a8571e 2022-01-07 stsp err = got_deltify_file_mem(&deltas, &ndeltas,
742 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
743 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
744 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
745 64a8571e 2022-01-07 stsp base_raw->hdrlen,
746 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
748 64a8571e 2022-01-07 stsp err = got_deltify(&deltas, &ndeltas,
749 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
750 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
751 64a8571e 2022-01-07 stsp base->dtab, base_raw->f, base_raw->hdrlen,
752 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
754 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
755 e6bcace5 2021-06-22 stsp base_raw = NULL;
759 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
760 74881701 2021-10-15 stsp if (size + 32 < best_size){
762 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
763 e6bcace5 2021-06-22 stsp * replace it.
765 74881701 2021-10-15 stsp best_size = size;
766 74881701 2021-10-15 stsp free(best_deltas);
767 74881701 2021-10-15 stsp best_deltas = deltas;
768 74881701 2021-10-15 stsp best_ndeltas = ndeltas;
769 74881701 2021-10-15 stsp deltas = NULL;
770 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
771 e6bcace5 2021-06-22 stsp m->prev = base;
772 e6bcace5 2021-06-22 stsp m->head = base->head;
773 e6bcace5 2021-06-22 stsp if (m->head == NULL)
774 e6bcace5 2021-06-22 stsp m->head = base;
776 e6bcace5 2021-06-22 stsp free(deltas);
777 e6bcace5 2021-06-22 stsp deltas = NULL;
778 e6bcace5 2021-06-22 stsp ndeltas = 0;
782 74881701 2021-10-15 stsp if (best_ndeltas > 0) {
783 402a5ec1 2022-01-10 stsp if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
784 402a5ec1 2022-01-10 stsp delta_memsize + best_size <= max_delta_memsize) {
785 402a5ec1 2022-01-10 stsp delta_memsize += best_size;
786 5060d5a1 2022-01-10 stsp err = encode_delta_in_mem(m, raw, best_deltas,
787 5060d5a1 2022-01-10 stsp best_ndeltas, best_size, m->prev->size);
789 5060d5a1 2022-01-10 stsp m->delta_offset = ftello(delta_cache);
792 67fd6849 2022-02-13 stsp * Storing compressed delta data in the delta
793 67fd6849 2022-02-13 stsp * cache file would probably be more efficient
794 67fd6849 2022-02-13 stsp * than writing uncompressed delta data here
795 67fd6849 2022-02-13 stsp * and compressing it while writing the pack
796 67fd6849 2022-02-13 stsp * file. This would also allow for reusing
797 67fd6849 2022-02-13 stsp * deltas in their compressed form.
799 5060d5a1 2022-01-10 stsp err = encode_delta(m, raw, best_deltas,
800 5060d5a1 2022-01-10 stsp best_ndeltas, m->prev->size, delta_cache);
802 74881701 2021-10-15 stsp free(best_deltas);
803 74881701 2021-10-15 stsp best_deltas = NULL;
804 74881701 2021-10-15 stsp best_ndeltas = 0;
809 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
810 e6bcace5 2021-06-22 stsp raw = NULL;
813 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
814 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
815 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
818 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
819 e6bcace5 2021-06-22 stsp if (base_raw)
820 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
821 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
822 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
823 74881701 2021-10-15 stsp free(deltas);
824 74881701 2021-10-15 stsp free(best_deltas);
825 e6bcace5 2021-06-22 stsp return err;
828 e6bcace5 2021-06-22 stsp static const struct got_error *
829 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
830 05118f5a 2021-06-22 stsp struct got_repository *repo)
832 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
833 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
836 05118f5a 2021-06-22 stsp *found = 0;
838 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
839 05118f5a 2021-06-22 stsp if (err == NULL)
840 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
841 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
842 05118f5a 2021-06-22 stsp err = NULL;
843 05118f5a 2021-06-22 stsp return err;
846 05118f5a 2021-06-22 stsp static const struct got_error *
847 67fd6849 2022-02-13 stsp add_object(int want_meta, struct got_object_idset *idset,
848 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
849 6863cbf9 2022-03-21 stsp time_t mtime, int loose_obj_only, struct got_repository *repo,
850 6863cbf9 2022-03-21 stsp int *ncolored, int *nfound, int *ntrees,
851 6863cbf9 2022-03-21 stsp got_pack_progress_cb progress_cb, void *progress_arg,
852 6863cbf9 2022-03-21 stsp struct got_ratelimit *rl)
854 e6bcace5 2021-06-22 stsp const struct got_error *err;
855 67fd6849 2022-02-13 stsp struct got_pack_meta *m = NULL;
857 05118f5a 2021-06-22 stsp if (loose_obj_only) {
858 05118f5a 2021-06-22 stsp int is_packed;
859 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
861 05118f5a 2021-06-22 stsp return err;
862 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
863 05118f5a 2021-06-22 stsp return NULL;
866 67fd6849 2022-02-13 stsp if (want_meta) {
867 67fd6849 2022-02-13 stsp err = alloc_meta(&m, id, path, obj_type, mtime);
869 6863cbf9 2022-03-21 stsp return err;
871 6863cbf9 2022-03-21 stsp (*nfound)++;
872 6863cbf9 2022-03-21 stsp err = report_progress(progress_cb, progress_arg, rl,
873 6863cbf9 2022-03-21 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
875 bb6672b6 2022-04-14 tb clear_meta(m);
877 67fd6849 2022-02-13 stsp return err;
881 bb6672b6 2022-04-14 tb err = got_object_idset_add(idset, id, m);
883 bb6672b6 2022-04-14 tb clear_meta(m);
889 e6bcace5 2021-06-22 stsp static const struct got_error *
890 67fd6849 2022-02-13 stsp load_tree_entries(struct got_object_id_queue *ids, int want_meta,
891 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
892 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
893 b8af7c06 2022-03-15 stsp int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
894 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
895 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
897 e6bcace5 2021-06-22 stsp const struct got_error *err;
898 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
899 e6bcace5 2021-06-22 stsp char *p = NULL;
902 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
904 b8af7c06 2022-03-15 stsp return err;
906 b8af7c06 2022-03-15 stsp (*ntrees)++;
907 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
908 b8af7c06 2022-03-15 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
910 e6bcace5 2021-06-22 stsp return err;
912 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
913 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
914 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
915 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
917 e6bcace5 2021-06-22 stsp if (cancel_cb) {
918 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
923 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
924 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
927 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
928 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
929 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
933 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
934 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
935 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
938 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
939 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
940 67fd6849 2022-02-13 stsp err = add_object(want_meta, idset, id, p,
941 6863cbf9 2022-03-21 stsp GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
942 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees,
943 6863cbf9 2022-03-21 stsp progress_cb, progress_arg, rl);
951 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
953 e6bcace5 2021-06-22 stsp return err;
956 e6bcace5 2021-06-22 stsp static const struct got_error *
957 67fd6849 2022-02-13 stsp load_tree(int want_meta, struct got_object_idset *idset,
958 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
959 b8af7c06 2022-03-15 stsp struct got_repository *repo, int loose_obj_only,
960 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
961 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
962 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
964 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
965 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
966 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
968 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
969 e6bcace5 2021-06-22 stsp return NULL;
971 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
973 e6bcace5 2021-06-22 stsp return err;
975 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
976 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
978 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
979 e6bcace5 2021-06-22 stsp if (cancel_cb) {
980 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
985 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
986 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
988 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
989 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
993 67fd6849 2022-02-13 stsp err = add_object(want_meta, idset, qid->id, dpath,
994 6863cbf9 2022-03-21 stsp GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo,
995 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
997 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1001 67fd6849 2022-02-13 stsp err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
1002 b8af7c06 2022-03-15 stsp dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1003 b8af7c06 2022-03-15 stsp ntrees, progress_cb, progress_arg, rl,
1004 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1005 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1010 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
1011 e6bcace5 2021-06-22 stsp return err;
1014 e6bcace5 2021-06-22 stsp static const struct got_error *
1015 67fd6849 2022-02-13 stsp load_commit(int want_meta, struct got_object_idset *idset,
1016 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1017 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
1018 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1019 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1021 e6bcace5 2021-06-22 stsp const struct got_error *err;
1022 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1024 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
1025 e6bcace5 2021-06-22 stsp return NULL;
1027 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1028 05118f5a 2021-06-22 stsp int is_packed;
1029 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1031 05118f5a 2021-06-22 stsp return err;
1032 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1033 05118f5a 2021-06-22 stsp return NULL;
1036 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1038 e6bcace5 2021-06-22 stsp return err;
1040 67fd6849 2022-02-13 stsp err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1041 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
1042 6863cbf9 2022-03-21 stsp loose_obj_only, repo,
1043 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1045 e6bcace5 2021-06-22 stsp goto done;
1047 67fd6849 2022-02-13 stsp err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1048 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
1049 b8af7c06 2022-03-15 stsp repo, loose_obj_only, ncolored, nfound, ntrees,
1050 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1052 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1053 e6bcace5 2021-06-22 stsp return err;
1056 05118f5a 2021-06-22 stsp static const struct got_error *
1057 67fd6849 2022-02-13 stsp load_tag(int want_meta, struct got_object_idset *idset,
1058 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1059 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
1060 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1061 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1063 05118f5a 2021-06-22 stsp const struct got_error *err;
1064 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
1066 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
1067 05118f5a 2021-06-22 stsp return NULL;
1069 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1070 05118f5a 2021-06-22 stsp int is_packed;
1071 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1073 05118f5a 2021-06-22 stsp return err;
1074 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1075 05118f5a 2021-06-22 stsp return NULL;
1078 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1080 05118f5a 2021-06-22 stsp return err;
1082 67fd6849 2022-02-13 stsp err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1083 6863cbf9 2022-03-21 stsp got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1084 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1086 05118f5a 2021-06-22 stsp goto done;
1088 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1089 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1090 67fd6849 2022-02-13 stsp err = load_commit(want_meta, idset,
1091 b8af7c06 2022-03-15 stsp got_object_tag_get_object_id(tag), repo, loose_obj_only,
1092 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1093 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1095 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1096 67fd6849 2022-02-13 stsp err = load_tree(want_meta, idset,
1097 67fd6849 2022-02-13 stsp got_object_tag_get_object_id(tag), "",
1098 b8af7c06 2022-03-15 stsp got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1099 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1100 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1107 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1108 05118f5a 2021-06-22 stsp return err;
1111 e6bcace5 2021-06-22 stsp enum findtwixt_color {
1112 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
1113 e6bcace5 2021-06-22 stsp COLOR_DROP,
1114 e6bcace5 2021-06-22 stsp COLOR_BLANK,
1115 70f8f24d 2022-04-14 stsp COLOR_SKIP,
1118 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
1119 e6bcace5 2021-06-22 stsp COLOR_KEEP,
1120 e6bcace5 2021-06-22 stsp COLOR_DROP,
1121 70f8f24d 2022-04-14 stsp COLOR_BLANK,
1122 70f8f24d 2022-04-14 stsp COLOR_SKIP,
1125 e6bcace5 2021-06-22 stsp static const struct got_error *
1126 70f8f24d 2022-04-14 stsp paint_commit(struct got_object_qid *qid, int color)
1128 70f8f24d 2022-04-14 stsp if (color < 0 || color >= nitems(findtwixt_colors))
1129 70f8f24d 2022-04-14 stsp return got_error(GOT_ERR_RANGE);
1131 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
1132 e6bcace5 2021-06-22 stsp return NULL;
1135 e6bcace5 2021-06-22 stsp static const struct got_error *
1136 70f8f24d 2022-04-14 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1137 70f8f24d 2022-04-14 stsp int color, struct got_repository *repo)
1139 70f8f24d 2022-04-14 stsp const struct got_error *err;
1140 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1142 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1144 e6bcace5 2021-06-22 stsp return err;
1146 70f8f24d 2022-04-14 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
1147 70f8f24d 2022-04-14 stsp return paint_commit(qid, color);
1150 e6bcace5 2021-06-22 stsp struct append_id_arg {
1151 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1153 70f8f24d 2022-04-14 stsp struct got_object_idset *drop;
1154 70f8f24d 2022-04-14 stsp struct got_object_idset *skip;
1157 e6bcace5 2021-06-22 stsp static const struct got_error *
1158 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1160 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1162 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(a->skip, id) ||
1163 70f8f24d 2022-04-14 stsp got_object_idset_contains(a->drop, id))
1164 70f8f24d 2022-04-14 stsp return NULL;
1166 70f8f24d 2022-04-14 stsp a->array[++a->idx] = got_object_id_dup(id);
1167 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1168 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1170 e6bcace5 2021-06-22 stsp return NULL;
1173 29e0594f 2022-04-09 stsp static const struct got_error *
1174 29e0594f 2022-04-09 stsp queue_commit_or_tag_id(struct got_object_id *id, int color,
1175 29e0594f 2022-04-09 stsp struct got_object_id_queue *ids, struct got_repository *repo)
1177 29e0594f 2022-04-09 stsp const struct got_error *err;
1178 29e0594f 2022-04-09 stsp struct got_tag_object *tag = NULL;
1179 29e0594f 2022-04-09 stsp int obj_type;
1181 29e0594f 2022-04-09 stsp err = got_object_get_type(&obj_type, repo, id);
1183 29e0594f 2022-04-09 stsp return err;
1185 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1186 29e0594f 2022-04-09 stsp err = got_object_open_as_tag(&tag, repo, id);
1188 29e0594f 2022-04-09 stsp return err;
1189 29e0594f 2022-04-09 stsp obj_type = got_object_tag_get_object_type(tag);
1190 29e0594f 2022-04-09 stsp id = got_object_tag_get_object_id(tag);
1193 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1194 29e0594f 2022-04-09 stsp err = queue_commit_id(ids, id, color, repo);
1196 29e0594f 2022-04-09 stsp goto done;
1200 29e0594f 2022-04-09 stsp got_object_tag_close(tag);
1201 29e0594f 2022-04-09 stsp return err;
1204 e6bcace5 2021-06-22 stsp static const struct got_error *
1205 70f8f24d 2022-04-14 stsp paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1206 14dbbf48 2022-04-10 stsp struct got_object_idset *keep, struct got_object_idset *drop,
1207 70f8f24d 2022-04-14 stsp struct got_object_idset *skip, struct got_repository *repo,
1208 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1209 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1211 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1212 14dbbf48 2022-04-10 stsp struct got_commit_object *commit = NULL;
1213 70f8f24d 2022-04-14 stsp const struct got_object_id_queue *parents;
1214 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1215 70f8f24d 2022-04-14 stsp int nqueued = nids, nskip = 0;
1217 70f8f24d 2022-04-14 stsp while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1218 70f8f24d 2022-04-14 stsp int color;
1220 57bc7b6d 2022-04-10 stsp if (cancel_cb) {
1221 57bc7b6d 2022-04-10 stsp err = cancel_cb(cancel_arg);
1226 14dbbf48 2022-04-10 stsp qid = STAILQ_FIRST(ids);
1227 70f8f24d 2022-04-14 stsp STAILQ_REMOVE_HEAD(ids, entry);
1228 70f8f24d 2022-04-14 stsp nqueued--;
1229 70f8f24d 2022-04-14 stsp color = *((int *)qid->data);
1230 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1233 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(skip, qid->id)) {
1234 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1238 70f8f24d 2022-04-14 stsp switch (color) {
1239 70f8f24d 2022-04-14 stsp case COLOR_KEEP:
1240 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(keep, qid->id)) {
1241 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1244 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(drop, qid->id)) {
1245 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1247 70f8f24d 2022-04-14 stsp goto done;
1250 70f8f24d 2022-04-14 stsp (*ncolored)++;
1251 70f8f24d 2022-04-14 stsp err = got_object_idset_add(keep, qid->id, NULL);
1253 70f8f24d 2022-04-14 stsp goto done;
1255 70f8f24d 2022-04-14 stsp case COLOR_DROP:
1256 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(drop, qid->id)) {
1257 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1260 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(keep, qid->id)) {
1261 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1263 70f8f24d 2022-04-14 stsp goto done;
1266 70f8f24d 2022-04-14 stsp (*ncolored)++;
1267 70f8f24d 2022-04-14 stsp err = got_object_idset_add(drop, qid->id, NULL);
1269 70f8f24d 2022-04-14 stsp goto done;
1271 70f8f24d 2022-04-14 stsp case COLOR_SKIP:
1272 70f8f24d 2022-04-14 stsp if (!got_object_idset_contains(skip, qid->id)) {
1273 70f8f24d 2022-04-14 stsp err = got_object_idset_add(skip, qid->id, NULL);
1275 70f8f24d 2022-04-14 stsp goto done;
1279 70f8f24d 2022-04-14 stsp /* should not happen */
1280 70f8f24d 2022-04-14 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
1281 70f8f24d 2022-04-14 stsp "%s invalid commit color %d", __func__, color);
1282 70f8f24d 2022-04-14 stsp goto done;
1285 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
1286 b8af7c06 2022-03-15 stsp *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1291 70f8f24d 2022-04-14 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
1295 70f8f24d 2022-04-14 stsp parents = got_object_commit_get_parent_ids(commit);
1296 70f8f24d 2022-04-14 stsp if (parents) {
1297 70f8f24d 2022-04-14 stsp struct got_object_qid *pid;
1298 70f8f24d 2022-04-14 stsp color = *((int *)qid->data);
1299 70f8f24d 2022-04-14 stsp STAILQ_FOREACH(pid, parents, entry) {
1300 70f8f24d 2022-04-14 stsp err = queue_commit_id(ids, pid->id, color,
1304 70f8f24d 2022-04-14 stsp nqueued++;
1305 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1310 70f8f24d 2022-04-14 stsp got_object_commit_close(commit);
1311 70f8f24d 2022-04-14 stsp commit = NULL;
1312 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1315 14dbbf48 2022-04-10 stsp if (commit)
1316 14dbbf48 2022-04-10 stsp got_object_commit_close(commit);
1317 14dbbf48 2022-04-10 stsp return err;
1320 14dbbf48 2022-04-10 stsp static const struct got_error *
1321 14dbbf48 2022-04-10 stsp findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1322 14dbbf48 2022-04-10 stsp struct got_object_id **head, int nhead,
1323 14dbbf48 2022-04-10 stsp struct got_object_id **tail, int ntail,
1324 14dbbf48 2022-04-10 stsp struct got_repository *repo,
1325 14dbbf48 2022-04-10 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1326 14dbbf48 2022-04-10 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1328 14dbbf48 2022-04-10 stsp const struct got_error *err = NULL;
1329 14dbbf48 2022-04-10 stsp struct got_object_id_queue ids;
1330 70f8f24d 2022-04-14 stsp struct got_object_idset *keep, *drop, *skip = NULL;
1331 14dbbf48 2022-04-10 stsp int i, nkeep;
1333 14dbbf48 2022-04-10 stsp STAILQ_INIT(&ids);
1334 14dbbf48 2022-04-10 stsp *res = NULL;
1335 14dbbf48 2022-04-10 stsp *nres = 0;
1336 14dbbf48 2022-04-10 stsp *ncolored = 0;
1338 14dbbf48 2022-04-10 stsp keep = got_object_idset_alloc();
1339 14dbbf48 2022-04-10 stsp if (keep == NULL)
1340 14dbbf48 2022-04-10 stsp return got_error_from_errno("got_object_idset_alloc");
1342 14dbbf48 2022-04-10 stsp drop = got_object_idset_alloc();
1343 14dbbf48 2022-04-10 stsp if (drop == NULL) {
1344 14dbbf48 2022-04-10 stsp err = got_error_from_errno("got_object_idset_alloc");
1345 14dbbf48 2022-04-10 stsp goto done;
1348 70f8f24d 2022-04-14 stsp skip = got_object_idset_alloc();
1349 70f8f24d 2022-04-14 stsp if (skip == NULL) {
1350 70f8f24d 2022-04-14 stsp err = got_error_from_errno("got_object_idset_alloc");
1351 70f8f24d 2022-04-14 stsp goto done;
1354 14dbbf48 2022-04-10 stsp for (i = 0; i < nhead; i++) {
1355 14dbbf48 2022-04-10 stsp struct got_object_id *id = head[i];
1356 14dbbf48 2022-04-10 stsp if (id == NULL)
1358 fbafdecf 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1360 14dbbf48 2022-04-10 stsp goto done;
1363 14dbbf48 2022-04-10 stsp for (i = 0; i < ntail; i++) {
1364 14dbbf48 2022-04-10 stsp struct got_object_id *id = tail[i];
1365 14dbbf48 2022-04-10 stsp if (id == NULL)
1367 14dbbf48 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1369 14dbbf48 2022-04-10 stsp goto done;
1372 70f8f24d 2022-04-14 stsp err = paint_commits(ncolored, &ids, nhead + ntail,
1373 70f8f24d 2022-04-14 stsp keep, drop, skip, repo, progress_cb, progress_arg, rl,
1374 70f8f24d 2022-04-14 stsp cancel_cb, cancel_arg);
1376 14dbbf48 2022-04-10 stsp goto done;
1378 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1379 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1380 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1381 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1382 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1383 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1384 e6bcace5 2021-06-22 stsp goto done;
1386 70f8f24d 2022-04-14 stsp arg.idx = -1;
1387 70f8f24d 2022-04-14 stsp arg.skip = skip;
1388 70f8f24d 2022-04-14 stsp arg.drop = drop;
1389 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1390 e6bcace5 2021-06-22 stsp if (err) {
1391 e6bcace5 2021-06-22 stsp free(arg.array);
1392 e6bcace5 2021-06-22 stsp goto done;
1394 e6bcace5 2021-06-22 stsp *res = arg.array;
1395 70f8f24d 2022-04-14 stsp *nres = arg.idx + 1;
1398 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1399 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1401 70f8f24d 2022-04-14 stsp got_object_idset_free(skip);
1402 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1403 e6bcace5 2021-06-22 stsp return err;
1406 e6bcace5 2021-06-22 stsp static const struct got_error *
1407 b8af7c06 2022-03-15 stsp load_object_ids(int *ncolored, int *nfound, int *ntrees,
1408 b8af7c06 2022-03-15 stsp struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1409 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1410 b8af7c06 2022-03-15 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1411 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1413 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1414 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1415 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
1417 b8af7c06 2022-03-15 stsp *ncolored = 0;
1418 b8af7c06 2022-03-15 stsp *nfound = 0;
1419 b8af7c06 2022-03-15 stsp *ntrees = 0;
1421 b8af7c06 2022-03-15 stsp err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1422 b8af7c06 2022-03-15 stsp repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1423 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
1424 e6bcace5 2021-06-22 stsp goto done;
1426 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1427 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
1428 05118f5a 2021-06-22 stsp if (id == NULL)
1430 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1432 05118f5a 2021-06-22 stsp return err;
1433 9d34261e 2022-04-07 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1434 9d34261e 2022-04-07 stsp err = load_commit(0, idset, id, repo,
1435 9d34261e 2022-04-07 stsp loose_obj_only, ncolored, nfound, ntrees,
1436 9d34261e 2022-04-07 stsp progress_cb, progress_arg, rl,
1437 9d34261e 2022-04-07 stsp cancel_cb, cancel_arg);
1439 07165b17 2021-07-01 stsp goto done;
1440 9d34261e 2022-04-07 stsp } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1441 9d34261e 2022-04-07 stsp err = load_tag(0, idset, id, repo,
1442 9d34261e 2022-04-07 stsp loose_obj_only, ncolored, nfound, ntrees,
1443 9d34261e 2022-04-07 stsp progress_cb, progress_arg, rl,
1444 9d34261e 2022-04-07 stsp cancel_cb, cancel_arg);
1446 9d34261e 2022-04-07 stsp goto done;
1450 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
1451 b8af7c06 2022-03-15 stsp err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1452 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1453 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1455 eca70f98 2021-09-03 stsp goto done;
1458 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1459 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1460 67fd6849 2022-02-13 stsp struct got_pack_meta *m;
1461 05118f5a 2021-06-22 stsp if (id == NULL)
1463 67fd6849 2022-02-13 stsp m = got_object_idset_get(idset, id);
1464 67fd6849 2022-02-13 stsp if (m == NULL) {
1465 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1467 07165b17 2021-07-01 stsp goto done;
1469 67fd6849 2022-02-13 stsp obj_type = m->obj_type;
1470 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1472 b8af7c06 2022-03-15 stsp err = load_tag(1, idset, id, repo, loose_obj_only,
1473 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1474 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1476 e6bcace5 2021-06-22 stsp goto done;
1479 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
1480 e6bcace5 2021-06-22 stsp free(ids[i]);
1482 e6bcace5 2021-06-22 stsp free(ids);
1483 e6bcace5 2021-06-22 stsp return err;
1486 e6bcace5 2021-06-22 stsp const struct got_error *
1487 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1491 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1492 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
1493 e6bcace5 2021-06-22 stsp if (n != len)
1494 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
1495 e6bcace5 2021-06-22 stsp return NULL;
1498 e6bcace5 2021-06-22 stsp static void
1499 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1501 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1502 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1503 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1504 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1507 e6bcace5 2021-06-22 stsp static int
1508 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1510 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1512 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1513 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1514 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1515 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1516 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
1517 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
1518 e6bcace5 2021-06-22 stsp if (ahd != bhd)
1519 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
1520 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1521 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1522 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
1525 67fd6849 2022-02-13 stsp static int
1526 67fd6849 2022-02-13 stsp reuse_write_order_cmp(const void *pa, const void *pb)
1528 67fd6849 2022-02-13 stsp struct got_pack_meta *a, *b;
1530 67fd6849 2022-02-13 stsp a = *(struct got_pack_meta **)pa;
1531 67fd6849 2022-02-13 stsp b = *(struct got_pack_meta **)pb;
1533 67fd6849 2022-02-13 stsp if (a->reused_delta_offset < b->reused_delta_offset)
1534 67fd6849 2022-02-13 stsp return -1;
1535 67fd6849 2022-02-13 stsp if (a->reused_delta_offset > b->reused_delta_offset)
1540 e6bcace5 2021-06-22 stsp static const struct got_error *
1541 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1545 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1547 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1548 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1549 e6bcace5 2021-06-22 stsp len >>= 4;
1550 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1551 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1552 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1553 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1554 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1555 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1558 e6bcace5 2021-06-22 stsp *hdrlen = i;
1559 e6bcace5 2021-06-22 stsp return NULL;
1562 e6bcace5 2021-06-22 stsp static int
1563 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1566 e6bcace5 2021-06-22 stsp char rbuf[8];
1568 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1569 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1570 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1571 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1575 e6bcace5 2021-06-22 stsp while (i > 0)
1576 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1580 e6bcace5 2021-06-22 stsp static const struct got_error *
1581 5060d5a1 2022-01-10 stsp deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1582 67fd6849 2022-02-13 stsp struct got_pack_meta *m)
1584 5060d5a1 2022-01-10 stsp const struct got_error *err;
1585 5060d5a1 2022-01-10 stsp char buf[32];
1588 67fd6849 2022-02-13 stsp if (m->prev->off != 0) {
1589 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1590 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1592 5060d5a1 2022-01-10 stsp return err;
1593 5060d5a1 2022-01-10 stsp nh += packoff(buf + nh, m->off - m->prev->off);
1594 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1596 5060d5a1 2022-01-10 stsp return err;
1597 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1599 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1600 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1602 5060d5a1 2022-01-10 stsp return err;
1603 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1605 5060d5a1 2022-01-10 stsp return err;
1606 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1607 5060d5a1 2022-01-10 stsp err = hwrite(packfile, m->prev->id.sha1,
1608 5060d5a1 2022-01-10 stsp sizeof(m->prev->id.sha1), ctx);
1610 5060d5a1 2022-01-10 stsp return err;
1611 5060d5a1 2022-01-10 stsp *packfile_size += sizeof(m->prev->id.sha1);
1614 5060d5a1 2022-01-10 stsp return NULL;
1617 5060d5a1 2022-01-10 stsp static const struct got_error *
1618 67fd6849 2022-02-13 stsp write_packed_object(off_t *packfile_size, FILE *packfile,
1619 67fd6849 2022-02-13 stsp FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1620 67fd6849 2022-02-13 stsp SHA1_CTX *ctx, struct got_repository *repo)
1622 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
1623 67fd6849 2022-02-13 stsp struct got_deflate_checksum csum;
1624 67fd6849 2022-02-13 stsp char buf[32];
1626 67fd6849 2022-02-13 stsp struct got_raw_object *raw = NULL;
1627 67fd6849 2022-02-13 stsp off_t outlen;
1629 67fd6849 2022-02-13 stsp csum.output_sha1 = ctx;
1630 67fd6849 2022-02-13 stsp csum.output_crc = NULL;
1632 67fd6849 2022-02-13 stsp m->off = ftello(packfile);
1633 67fd6849 2022-02-13 stsp if (m->delta_len == 0) {
1634 67fd6849 2022-02-13 stsp err = got_object_raw_open(&raw, outfd, repo, &m->id);
1636 67fd6849 2022-02-13 stsp goto done;
1637 67fd6849 2022-02-13 stsp err = packhdr(&nh, buf, sizeof(buf),
1638 67fd6849 2022-02-13 stsp m->obj_type, raw->size);
1640 67fd6849 2022-02-13 stsp goto done;
1641 67fd6849 2022-02-13 stsp err = hwrite(packfile, buf, nh, ctx);
1643 67fd6849 2022-02-13 stsp goto done;
1644 67fd6849 2022-02-13 stsp *packfile_size += nh;
1645 67fd6849 2022-02-13 stsp if (raw->f == NULL) {
1646 67fd6849 2022-02-13 stsp err = got_deflate_to_file_mmap(&outlen,
1647 67fd6849 2022-02-13 stsp raw->data + raw->hdrlen, 0, raw->size,
1648 67fd6849 2022-02-13 stsp packfile, &csum);
1650 67fd6849 2022-02-13 stsp goto done;
1652 67fd6849 2022-02-13 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1654 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1655 67fd6849 2022-02-13 stsp goto done;
1657 67fd6849 2022-02-13 stsp err = got_deflate_to_file(&outlen, raw->f,
1658 67fd6849 2022-02-13 stsp raw->size, packfile, &csum);
1660 67fd6849 2022-02-13 stsp goto done;
1662 67fd6849 2022-02-13 stsp *packfile_size += outlen;
1663 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
1664 67fd6849 2022-02-13 stsp raw = NULL;
1665 67fd6849 2022-02-13 stsp } else if (m->delta_buf) {
1666 67fd6849 2022-02-13 stsp err = deltahdr(packfile_size, ctx, packfile, m);
1668 67fd6849 2022-02-13 stsp goto done;
1669 67fd6849 2022-02-13 stsp err = got_deflate_to_file_mmap(&outlen,
1670 67fd6849 2022-02-13 stsp m->delta_buf, 0, m->delta_len, packfile, &csum);
1672 67fd6849 2022-02-13 stsp goto done;
1673 67fd6849 2022-02-13 stsp *packfile_size += outlen;
1674 67fd6849 2022-02-13 stsp free(m->delta_buf);
1675 67fd6849 2022-02-13 stsp m->delta_buf = NULL;
1677 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1679 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1680 67fd6849 2022-02-13 stsp goto done;
1682 67fd6849 2022-02-13 stsp err = deltahdr(packfile_size, ctx, packfile, m);
1684 67fd6849 2022-02-13 stsp goto done;
1685 67fd6849 2022-02-13 stsp err = got_deflate_to_file(&outlen, delta_cache,
1686 67fd6849 2022-02-13 stsp m->delta_len, packfile, &csum);
1688 67fd6849 2022-02-13 stsp goto done;
1689 67fd6849 2022-02-13 stsp *packfile_size += outlen;
1693 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
1694 67fd6849 2022-02-13 stsp return err;
1697 67fd6849 2022-02-13 stsp static const struct got_error *
1698 74881701 2021-10-15 stsp genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1699 67fd6849 2022-02-13 stsp struct got_pack_meta **deltify, int ndeltify,
1700 67fd6849 2022-02-13 stsp struct got_pack_meta **reuse, int nreuse,
1701 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees, int nours,
1702 b8af7c06 2022-03-15 stsp struct got_repository *repo,
1703 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1704 211cfef0 2022-01-05 stsp struct got_ratelimit *rl,
1705 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1707 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1709 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1710 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1711 08347b73 2021-10-14 stsp char buf[32];
1713 67fd6849 2022-02-13 stsp off_t packfile_size = 0;
1714 cc7a354a 2021-10-15 stsp int outfd = -1;
1716 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1718 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1720 e6bcace5 2021-06-22 stsp return err;
1721 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1722 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1724 e6bcace5 2021-06-22 stsp goto done;
1725 67fd6849 2022-02-13 stsp putbe32(buf, ndeltify + nreuse);
1726 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1728 e6bcace5 2021-06-22 stsp goto done;
1730 67fd6849 2022-02-13 stsp qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1731 67fd6849 2022-02-13 stsp write_order_cmp);
1732 67fd6849 2022-02-13 stsp for (i = 0; i < ndeltify; i++) {
1733 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1734 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
1735 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, i);
1737 211cfef0 2022-01-05 stsp goto done;
1738 67fd6849 2022-02-13 stsp m = deltify[i];
1739 67fd6849 2022-02-13 stsp err = write_packed_object(&packfile_size, packfile,
1740 67fd6849 2022-02-13 stsp delta_cache, m, &outfd, &ctx, repo);
1742 67fd6849 2022-02-13 stsp goto done;
1745 67fd6849 2022-02-13 stsp qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1746 67fd6849 2022-02-13 stsp reuse_write_order_cmp);
1747 67fd6849 2022-02-13 stsp for (i = 0; i < nreuse; i++) {
1748 67fd6849 2022-02-13 stsp err = report_progress(progress_cb, progress_arg, rl,
1749 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
1750 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1752 67fd6849 2022-02-13 stsp goto done;
1753 67fd6849 2022-02-13 stsp m = reuse[i];
1754 67fd6849 2022-02-13 stsp err = write_packed_object(&packfile_size, packfile,
1755 67fd6849 2022-02-13 stsp delta_cache, m, &outfd, &ctx, repo);
1757 67fd6849 2022-02-13 stsp goto done;
1760 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1761 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1762 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1763 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1764 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1765 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1766 211cfef0 2022-01-05 stsp if (progress_cb) {
1767 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1768 b8af7c06 2022-03-15 stsp packfile_size, nours, ndeltify + nreuse,
1769 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse);
1771 211cfef0 2022-01-05 stsp goto done;
1774 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
1775 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
1776 e6bcace5 2021-06-22 stsp return err;
1779 67fd6849 2022-02-13 stsp static const struct got_error *
1780 cbc287dc 2022-04-19 stsp remove_unused_object(struct got_object_id *id, void *data, void *arg)
1782 67fd6849 2022-02-13 stsp struct got_object_idset *idset = arg;
1784 cbc287dc 2022-04-19 stsp if (data == NULL)
1785 cbc287dc 2022-04-19 stsp got_object_idset_remove(NULL, idset, id);
1787 67fd6849 2022-02-13 stsp return NULL;
1790 67fd6849 2022-02-13 stsp static const struct got_error *
1791 cbc287dc 2022-04-19 stsp remove_reused_object(struct got_object_id *id, void *data, void *arg)
1793 67fd6849 2022-02-13 stsp struct got_object_idset *idset = arg;
1794 cbc287dc 2022-04-19 stsp struct got_pack_meta *m = data;
1796 67fd6849 2022-02-13 stsp if (m->have_reused_delta)
1797 cbc287dc 2022-04-19 stsp got_object_idset_remove(NULL, idset, id);
1799 67fd6849 2022-02-13 stsp return NULL;
1802 67fd6849 2022-02-13 stsp static const struct got_error *
1803 67fd6849 2022-02-13 stsp add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1805 67fd6849 2022-02-13 stsp struct got_pack_meta *m = data;
1806 67fd6849 2022-02-13 stsp struct got_pack_metavec *v = arg;
1808 67fd6849 2022-02-13 stsp return add_meta(m, v);
1811 e6bcace5 2021-06-22 stsp const struct got_error *
1812 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1813 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1814 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1815 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1816 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1817 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1819 e6bcace5 2021-06-22 stsp const struct got_error *err;
1820 67fd6849 2022-02-13 stsp int delta_cache_fd = -1;
1821 74881701 2021-10-15 stsp FILE *delta_cache = NULL;
1822 67fd6849 2022-02-13 stsp struct got_object_idset *idset;
1823 211cfef0 2022-01-05 stsp struct got_ratelimit rl;
1824 67fd6849 2022-02-13 stsp struct got_pack_metavec deltify, reuse;
1825 b8af7c06 2022-03-15 stsp int ncolored = 0, nfound = 0, ntrees = 0;
1827 67fd6849 2022-02-13 stsp memset(&deltify, 0, sizeof(deltify));
1828 67fd6849 2022-02-13 stsp memset(&reuse, 0, sizeof(reuse));
1830 211cfef0 2022-01-05 stsp got_ratelimit_init(&rl, 0, 500);
1832 67fd6849 2022-02-13 stsp idset = got_object_idset_alloc();
1833 67fd6849 2022-02-13 stsp if (idset == NULL)
1834 67fd6849 2022-02-13 stsp return got_error_from_errno("got_object_idset_alloc");
1836 b8af7c06 2022-03-15 stsp err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1837 b8af7c06 2022-03-15 stsp ntheirs, ours, nours, repo, loose_obj_only,
1838 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1840 e6bcace5 2021-06-22 stsp return err;
1842 cbc287dc 2022-04-19 stsp err = got_object_idset_for_each(idset, remove_unused_object, idset);
1844 67fd6849 2022-02-13 stsp goto done;
1846 28526235 2022-02-13 stsp if (progress_cb) {
1847 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1848 b8af7c06 2022-03-15 stsp 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1850 28526235 2022-02-13 stsp goto done;
1853 67fd6849 2022-02-13 stsp if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1854 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1855 05118f5a 2021-06-22 stsp goto done;
1858 67fd6849 2022-02-13 stsp delta_cache_fd = got_opentempfd();
1859 67fd6849 2022-02-13 stsp if (delta_cache_fd == -1) {
1860 74881701 2021-10-15 stsp err = got_error_from_errno("got_opentemp");
1861 74881701 2021-10-15 stsp goto done;
1864 67fd6849 2022-02-13 stsp reuse.metasz = 64;
1865 67fd6849 2022-02-13 stsp reuse.meta = calloc(reuse.metasz,
1866 67fd6849 2022-02-13 stsp sizeof(struct got_pack_meta *));
1867 67fd6849 2022-02-13 stsp if (reuse.meta == NULL) {
1868 67fd6849 2022-02-13 stsp err = got_error_from_errno("calloc");
1869 67fd6849 2022-02-13 stsp goto done;
1872 b8af7c06 2022-03-15 stsp err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1873 b8af7c06 2022-03-15 stsp ntrees, nours, repo, progress_cb, progress_arg, &rl,
1874 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1876 67fd6849 2022-02-13 stsp goto done;
1877 67fd6849 2022-02-13 stsp if (reuse.nmeta > 0) {
1878 cbc287dc 2022-04-19 stsp err = got_object_idset_for_each(idset,
1879 67fd6849 2022-02-13 stsp remove_reused_object, idset);
1881 67fd6849 2022-02-13 stsp goto done;
1884 67fd6849 2022-02-13 stsp delta_cache = fdopen(delta_cache_fd, "a+");
1885 67fd6849 2022-02-13 stsp if (delta_cache == NULL) {
1886 67fd6849 2022-02-13 stsp err = got_error_from_errno("fdopen");
1887 67fd6849 2022-02-13 stsp goto done;
1889 67fd6849 2022-02-13 stsp delta_cache_fd = -1;
1891 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1892 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1893 67fd6849 2022-02-13 stsp goto done;
1896 67fd6849 2022-02-13 stsp deltify.meta = calloc(got_object_idset_num_elements(idset),
1897 67fd6849 2022-02-13 stsp sizeof(struct got_pack_meta *));
1898 67fd6849 2022-02-13 stsp if (deltify.meta == NULL) {
1899 67fd6849 2022-02-13 stsp err = got_error_from_errno("calloc");
1900 67fd6849 2022-02-13 stsp goto done;
1902 67fd6849 2022-02-13 stsp deltify.metasz = got_object_idset_num_elements(idset);
1904 67fd6849 2022-02-13 stsp err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1906 67fd6849 2022-02-13 stsp goto done;
1907 67fd6849 2022-02-13 stsp if (deltify.nmeta > 0) {
1908 b8af7c06 2022-03-15 stsp err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1909 b8af7c06 2022-03-15 stsp nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1910 211cfef0 2022-01-05 stsp progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1912 f8a36e22 2021-08-26 stsp goto done;
1913 74881701 2021-10-15 stsp if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1914 74881701 2021-10-15 stsp err = got_error_from_errno("fseeko");
1915 74881701 2021-10-15 stsp goto done;
1919 67fd6849 2022-02-13 stsp err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1920 b8af7c06 2022-03-15 stsp deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1921 b8af7c06 2022-03-15 stsp nours, repo, progress_cb, progress_arg, &rl,
1922 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1924 e6bcace5 2021-06-22 stsp goto done;
1926 67fd6849 2022-02-13 stsp free_nmeta(deltify.meta, deltify.nmeta);
1927 67fd6849 2022-02-13 stsp free_nmeta(reuse.meta, reuse.nmeta);
1928 67fd6849 2022-02-13 stsp got_object_idset_free(idset);
1929 67fd6849 2022-02-13 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1930 67fd6849 2022-02-13 stsp err = got_error_from_errno("close");
1931 74881701 2021-10-15 stsp if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1932 74881701 2021-10-15 stsp err = got_error_from_errno("fclose");
1933 e6bcace5 2021-06-22 stsp return err;