2 e6bcace5 2021-06-22 stsp * Copyright (c) 2020 Ori Bernstein
3 e6bcace5 2021-06-22 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 e6bcace5 2021-06-22 stsp * Permission to use, copy, modify, and distribute this software for any
6 e6bcace5 2021-06-22 stsp * purpose with or without fee is hereby granted, provided that the above
7 e6bcace5 2021-06-22 stsp * copyright notice and this permission notice appear in all copies.
9 e6bcace5 2021-06-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 e6bcace5 2021-06-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 e6bcace5 2021-06-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 e6bcace5 2021-06-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 e6bcace5 2021-06-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 e6bcace5 2021-06-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 e6bcace5 2021-06-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 08736cf9 2021-06-23 stsp #include <sys/types.h>
19 08736cf9 2021-06-23 stsp #include <sys/uio.h>
20 e6bcace5 2021-06-22 stsp #include <sys/stat.h>
21 31ba2236 2022-01-05 thomas #include <sys/time.h>
23 08736cf9 2021-06-23 stsp #include <stdint.h>
24 e6bcace5 2021-06-22 stsp #include <stdio.h>
25 e6bcace5 2021-06-22 stsp #include <stdlib.h>
26 e6bcace5 2021-06-22 stsp #include <string.h>
27 31ba2236 2022-01-05 thomas #include <time.h>
28 e6bcace5 2021-06-22 stsp #include <limits.h>
29 e6bcace5 2021-06-22 stsp #include <zlib.h>
31 b347007e 2021-10-15 thomas #if defined(__FreeBSD__)
32 b347007e 2021-10-15 thomas #include <unistd.h>
35 e6bcace5 2021-06-22 stsp #include "got_error.h"
36 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
37 e6bcace5 2021-06-22 stsp #include "got_object.h"
38 0be8fa4c 2021-10-15 thomas #include "got_path.h"
39 05118f5a 2021-06-22 stsp #include "got_reference.h"
40 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
41 e4d8ab47 2021-10-15 thomas #include "got_opentemp.h"
43 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
44 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
45 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
46 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
47 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
50 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
52 31ba2236 2022-01-05 thomas #include "got_lib_ratelimit.h"
54 b79280dd 2021-10-15 thomas #ifndef MIN
55 b79280dd 2021-10-15 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 e6bcace5 2021-06-22 stsp struct got_pack_meta {
63 e6bcace5 2021-06-22 stsp struct got_object_id id;
65 e6bcace5 2021-06-22 stsp int obj_type;
66 ab6186ae 2021-10-15 thomas off_t size;
67 e6bcace5 2021-06-22 stsp time_t mtime;
69 e6bcace5 2021-06-22 stsp /* The best delta we picked */
70 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
71 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
72 3b6ceab7 2022-01-10 thomas unsigned char *delta_buf; /* if not encoded in delta cache file */
73 b79280dd 2021-10-15 thomas off_t delta_offset; /* offset in delta cache file */
74 3b6ceab7 2022-01-10 thomas off_t delta_len; /* encoded delta length */
77 f9c2e8e5 2022-02-13 thomas int have_reused_delta;
78 f9c2e8e5 2022-02-13 thomas off_t reused_delta_offset; /* offset of delta in reused pack file */
79 f9c2e8e5 2022-02-13 thomas struct got_object_id *base_obj_id;
81 e6bcace5 2021-06-22 stsp /* Only used for delta window */
82 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
84 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
88 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
89 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
94 e6bcace5 2021-06-22 stsp static const struct got_error *
95 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
96 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
98 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
99 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
101 e6bcace5 2021-06-22 stsp *new = NULL;
103 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
104 e6bcace5 2021-06-22 stsp if (m == NULL)
105 b7e0a384 2021-10-15 thomas return got_error_from_errno("calloc");
107 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
109 e6bcace5 2021-06-22 stsp m->path = strdup(path);
110 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
111 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
113 e6bcace5 2021-06-22 stsp return err;
116 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
117 e6bcace5 2021-06-22 stsp m->mtime = mtime;
119 e6bcace5 2021-06-22 stsp return NULL;
122 e6bcace5 2021-06-22 stsp static void
123 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
125 e6bcace5 2021-06-22 stsp if (meta == NULL)
127 e6bcace5 2021-06-22 stsp free(meta->path);
128 e6bcace5 2021-06-22 stsp meta->path = NULL;
129 3b6ceab7 2022-01-10 thomas free(meta->delta_buf);
130 3b6ceab7 2022-01-10 thomas meta->delta_buf = NULL;
131 f9c2e8e5 2022-02-13 thomas free(meta->base_obj_id);
132 f9c2e8e5 2022-02-13 thomas meta->base_obj_id = NULL;
135 e6bcace5 2021-06-22 stsp static void
136 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
140 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
141 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
142 e6bcace5 2021-06-22 stsp free(meta);
146 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
148 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
151 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
152 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
154 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
155 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
156 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
157 e6bcace5 2021-06-22 stsp if (cmp != 0)
158 e6bcace5 2021-06-22 stsp return cmp;
159 e6bcace5 2021-06-22 stsp if (a->mtime != b->mtime)
160 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
161 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
164 3b6ceab7 2022-01-10 thomas static off_t
165 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
168 3b6ceab7 2022-01-10 thomas off_t size = 32;
169 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
170 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
171 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
173 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
175 e6bcace5 2021-06-22 stsp return size;
178 3b6ceab7 2022-01-10 thomas static const struct got_error *
179 3b6ceab7 2022-01-10 thomas append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
183 3b6ceab7 2022-01-10 thomas if (*len + nseg >= *sz) {
184 3b6ceab7 2022-01-10 thomas while (*len + nseg >= *sz)
185 3b6ceab7 2022-01-10 thomas *sz += *sz / 2;
186 3b6ceab7 2022-01-10 thomas n = realloc(*p, *sz);
187 3b6ceab7 2022-01-10 thomas if (n == NULL)
188 3b6ceab7 2022-01-10 thomas return got_error_from_errno("realloc");
191 3b6ceab7 2022-01-10 thomas memcpy(*p + *len, seg, nseg);
192 3b6ceab7 2022-01-10 thomas *len += nseg;
193 3b6ceab7 2022-01-10 thomas return NULL;
196 3b6ceab7 2022-01-10 thomas static const struct got_error *
197 3b6ceab7 2022-01-10 thomas encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
198 3b6ceab7 2022-01-10 thomas struct got_delta_instruction *deltas, int ndeltas,
199 3b6ceab7 2022-01-10 thomas off_t delta_size, off_t base_size)
201 3b6ceab7 2022-01-10 thomas const struct got_error *err;
202 3b6ceab7 2022-01-10 thomas unsigned char buf[16], *bp;
203 3b6ceab7 2022-01-10 thomas int i, j;
204 3b6ceab7 2022-01-10 thomas size_t len = 0;
206 3b6ceab7 2022-01-10 thomas struct got_delta_instruction *d;
208 3b6ceab7 2022-01-10 thomas m->delta_buf = malloc(delta_size);
209 3b6ceab7 2022-01-10 thomas if (m->delta_buf == NULL)
210 3b6ceab7 2022-01-10 thomas return got_error_from_errno("calloc");
212 3b6ceab7 2022-01-10 thomas /* base object size */
213 3b6ceab7 2022-01-10 thomas buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
214 3b6ceab7 2022-01-10 thomas n = base_size >> GOT_DELTA_SIZE_SHIFT;
215 3b6ceab7 2022-01-10 thomas for (i = 1; n > 0; i++) {
216 3b6ceab7 2022-01-10 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
217 3b6ceab7 2022-01-10 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
218 3b6ceab7 2022-01-10 thomas n >>= GOT_DELTA_SIZE_SHIFT;
220 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size, buf, i);
222 3b6ceab7 2022-01-10 thomas return err;
224 3b6ceab7 2022-01-10 thomas /* target object size */
225 3b6ceab7 2022-01-10 thomas buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
226 3b6ceab7 2022-01-10 thomas n = o->size >> GOT_DELTA_SIZE_SHIFT;
227 3b6ceab7 2022-01-10 thomas for (i = 1; n > 0; i++) {
228 3b6ceab7 2022-01-10 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
229 3b6ceab7 2022-01-10 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
230 3b6ceab7 2022-01-10 thomas n >>= GOT_DELTA_SIZE_SHIFT;
232 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size, buf, i);
234 3b6ceab7 2022-01-10 thomas return err;
236 3b6ceab7 2022-01-10 thomas for (j = 0; j < ndeltas; j++) {
237 3b6ceab7 2022-01-10 thomas d = &deltas[j];
238 3b6ceab7 2022-01-10 thomas if (d->copy) {
239 3b6ceab7 2022-01-10 thomas n = d->offset;
240 3b6ceab7 2022-01-10 thomas bp = &buf[1];
241 3b6ceab7 2022-01-10 thomas buf[0] = GOT_DELTA_BASE_COPY;
242 3b6ceab7 2022-01-10 thomas for (i = 0; i < 4; i++) {
243 3b6ceab7 2022-01-10 thomas /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
244 3b6ceab7 2022-01-10 thomas buf[0] |= 1 << i;
245 3b6ceab7 2022-01-10 thomas *bp++ = n & 0xff;
247 3b6ceab7 2022-01-10 thomas if (n == 0)
251 3b6ceab7 2022-01-10 thomas n = d->len;
252 3b6ceab7 2022-01-10 thomas if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
253 3b6ceab7 2022-01-10 thomas /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
254 3b6ceab7 2022-01-10 thomas for (i = 0; i < 3 && n > 0; i++) {
255 3b6ceab7 2022-01-10 thomas buf[0] |= 1 << (i + 4);
256 3b6ceab7 2022-01-10 thomas *bp++ = n & 0xff;
260 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size,
261 3b6ceab7 2022-01-10 thomas buf, bp - buf);
263 3b6ceab7 2022-01-10 thomas return err;
264 3b6ceab7 2022-01-10 thomas } else if (o->f == NULL) {
266 3b6ceab7 2022-01-10 thomas while (n != d->len) {
267 3b6ceab7 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
268 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size,
271 3b6ceab7 2022-01-10 thomas return err;
272 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size,
273 3b6ceab7 2022-01-10 thomas o->data + o->hdrlen + d->offset + n,
276 3b6ceab7 2022-01-10 thomas return err;
277 3b6ceab7 2022-01-10 thomas n += buf[0];
280 3b6ceab7 2022-01-10 thomas char content[128];
281 3b6ceab7 2022-01-10 thomas size_t r;
282 3b6ceab7 2022-01-10 thomas if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
283 3b6ceab7 2022-01-10 thomas return got_error_from_errno("fseeko");
285 3b6ceab7 2022-01-10 thomas while (n != d->len) {
286 3b6ceab7 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
287 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size,
290 3b6ceab7 2022-01-10 thomas return err;
291 3b6ceab7 2022-01-10 thomas r = fread(content, 1, buf[0], o->f);
292 3b6ceab7 2022-01-10 thomas if (r != buf[0])
293 3b6ceab7 2022-01-10 thomas return got_ferror(o->f, GOT_ERR_IO);
294 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size,
295 3b6ceab7 2022-01-10 thomas content, buf[0]);
297 3b6ceab7 2022-01-10 thomas return err;
298 3b6ceab7 2022-01-10 thomas n += buf[0];
303 3b6ceab7 2022-01-10 thomas m->delta_len = len;
304 3b6ceab7 2022-01-10 thomas return NULL;
307 b79280dd 2021-10-15 thomas static const struct got_error *
308 b79280dd 2021-10-15 thomas encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
309 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas, int ndeltas,
310 d0772de9 2021-10-15 thomas off_t base_size, FILE *f)
312 d0772de9 2021-10-15 thomas unsigned char buf[16], *bp;
313 d0772de9 2021-10-15 thomas int i, j;
315 d0772de9 2021-10-15 thomas size_t w;
316 d0772de9 2021-10-15 thomas struct got_delta_instruction *d;
318 d0772de9 2021-10-15 thomas /* base object size */
319 d0772de9 2021-10-15 thomas buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
320 d0772de9 2021-10-15 thomas n = base_size >> GOT_DELTA_SIZE_SHIFT;
321 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
322 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
323 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
324 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
326 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, i, f);
327 d0772de9 2021-10-15 thomas if (w != i)
328 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
330 d0772de9 2021-10-15 thomas /* target object size */
331 d0772de9 2021-10-15 thomas buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
332 d0772de9 2021-10-15 thomas n = o->size >> GOT_DELTA_SIZE_SHIFT;
333 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
334 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
335 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
336 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
338 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, i, f);
339 d0772de9 2021-10-15 thomas if (w != i)
340 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
342 d0772de9 2021-10-15 thomas for (j = 0; j < ndeltas; j++) {
343 d0772de9 2021-10-15 thomas d = &deltas[j];
344 d0772de9 2021-10-15 thomas if (d->copy) {
345 d0772de9 2021-10-15 thomas n = d->offset;
346 d0772de9 2021-10-15 thomas bp = &buf[1];
347 d0772de9 2021-10-15 thomas buf[0] = GOT_DELTA_BASE_COPY;
348 d0772de9 2021-10-15 thomas for (i = 0; i < 4; i++) {
349 d0772de9 2021-10-15 thomas /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
350 d0772de9 2021-10-15 thomas buf[0] |= 1 << i;
351 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
353 d0772de9 2021-10-15 thomas if (n == 0)
357 d0772de9 2021-10-15 thomas n = d->len;
358 d0772de9 2021-10-15 thomas if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
359 d0772de9 2021-10-15 thomas /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
360 d0772de9 2021-10-15 thomas for (i = 0; i < 3 && n > 0; i++) {
361 d0772de9 2021-10-15 thomas buf[0] |= 1 << (i + 4);
362 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
366 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, bp - buf, f);
367 d0772de9 2021-10-15 thomas if (w != bp - buf)
368 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
369 2b0ae357 2022-01-10 thomas } else if (o->f == NULL) {
371 2b0ae357 2022-01-10 thomas while (n != d->len) {
372 2b0ae357 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
373 2b0ae357 2022-01-10 thomas w = fwrite(buf, 1, 1, f);
374 2b0ae357 2022-01-10 thomas if (w != 1)
375 2b0ae357 2022-01-10 thomas return got_ferror(f, GOT_ERR_IO);
376 2b0ae357 2022-01-10 thomas w = fwrite(o->data + o->hdrlen + d->offset + n,
377 2b0ae357 2022-01-10 thomas 1, buf[0], f);
378 2b0ae357 2022-01-10 thomas if (w != buf[0])
379 2b0ae357 2022-01-10 thomas return got_ferror(f, GOT_ERR_IO);
380 2b0ae357 2022-01-10 thomas n += buf[0];
383 d0772de9 2021-10-15 thomas char content[128];
384 d0772de9 2021-10-15 thomas size_t r;
385 d0772de9 2021-10-15 thomas if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
386 d0772de9 2021-10-15 thomas return got_error_from_errno("fseeko");
388 d0772de9 2021-10-15 thomas while (n != d->len) {
389 d0772de9 2021-10-15 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
390 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, 1, f);
391 d0772de9 2021-10-15 thomas if (w != 1)
392 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
393 d0772de9 2021-10-15 thomas r = fread(content, 1, buf[0], o->f);
394 d0772de9 2021-10-15 thomas if (r != buf[0])
395 d0772de9 2021-10-15 thomas return got_ferror(o->f, GOT_ERR_IO);
396 d0772de9 2021-10-15 thomas w = fwrite(content, 1, buf[0], f);
397 d0772de9 2021-10-15 thomas if (w != buf[0])
398 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
399 d0772de9 2021-10-15 thomas n += buf[0];
404 3b6ceab7 2022-01-10 thomas m->delta_len = ftello(f) - m->delta_offset;
405 d0772de9 2021-10-15 thomas return NULL;
408 31ba2236 2022-01-05 thomas static const struct got_error *
409 31ba2236 2022-01-05 thomas report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
410 31ba2236 2022-01-05 thomas struct got_ratelimit *rl, off_t packfile_size, int ncommits,
411 31ba2236 2022-01-05 thomas int nobj_total, int obj_deltify, int nobj_written)
413 31ba2236 2022-01-05 thomas const struct got_error *err;
414 31ba2236 2022-01-05 thomas int elapsed;
416 31ba2236 2022-01-05 thomas if (progress_cb == NULL)
417 31ba2236 2022-01-05 thomas return NULL;
419 31ba2236 2022-01-05 thomas err = got_ratelimit_check(&elapsed, rl);
420 31ba2236 2022-01-05 thomas if (err || !elapsed)
421 31ba2236 2022-01-05 thomas return err;
423 31ba2236 2022-01-05 thomas return progress_cb(progress_arg, packfile_size, ncommits,
424 31ba2236 2022-01-05 thomas nobj_total, obj_deltify, nobj_written);
427 e6bcace5 2021-06-22 stsp static const struct got_error *
428 f9c2e8e5 2022-02-13 thomas add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
430 f9c2e8e5 2022-02-13 thomas if (v->nmeta == v->metasz){
431 f9c2e8e5 2022-02-13 thomas size_t newsize = 2 * v->metasz;
432 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **new;
433 f9c2e8e5 2022-02-13 thomas new = reallocarray(v->meta, newsize, sizeof(*new));
434 f9c2e8e5 2022-02-13 thomas if (new == NULL)
435 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("reallocarray");
436 f9c2e8e5 2022-02-13 thomas v->meta = new;
437 f9c2e8e5 2022-02-13 thomas v->metasz = newsize;
440 f9c2e8e5 2022-02-13 thomas v->meta[v->nmeta++] = m;
441 f9c2e8e5 2022-02-13 thomas return NULL;
444 f9c2e8e5 2022-02-13 thomas static const struct got_error *
445 f9c2e8e5 2022-02-13 thomas reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
446 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset, struct got_pack *pack,
447 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx, int delta_cache_fd,
448 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
450 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
451 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *base = NULL;
452 f9c2e8e5 2022-02-13 thomas struct got_object_id *base_obj_id = NULL;
453 f9c2e8e5 2022-02-13 thomas off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
454 f9c2e8e5 2022-02-13 thomas uint64_t base_size, result_size;
456 f9c2e8e5 2022-02-13 thomas if (m->have_reused_delta)
457 f9c2e8e5 2022-02-13 thomas return NULL;
459 f9c2e8e5 2022-02-13 thomas err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
460 f9c2e8e5 2022-02-13 thomas &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
461 f9c2e8e5 2022-02-13 thomas packidx, idx, &m->id, repo);
463 f9c2e8e5 2022-02-13 thomas return err;
465 f9c2e8e5 2022-02-13 thomas if (delta_offset + delta_len < delta_offset)
466 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_BAD_PACKFILE);
468 f9c2e8e5 2022-02-13 thomas base = got_object_idset_get(idset, base_obj_id);
469 f9c2e8e5 2022-02-13 thomas if (base == NULL)
470 f9c2e8e5 2022-02-13 thomas goto done;
472 f9c2e8e5 2022-02-13 thomas m->delta_len = delta_len;
473 f9c2e8e5 2022-02-13 thomas m->delta_offset = delta_cache_offset;
474 f9c2e8e5 2022-02-13 thomas m->prev = base;
475 f9c2e8e5 2022-02-13 thomas m->size = result_size;
476 f9c2e8e5 2022-02-13 thomas m->have_reused_delta = 1;
477 f9c2e8e5 2022-02-13 thomas m->reused_delta_offset = delta_offset;
478 f9c2e8e5 2022-02-13 thomas m->base_obj_id = base_obj_id;
479 f9c2e8e5 2022-02-13 thomas base_obj_id = NULL;
480 f9c2e8e5 2022-02-13 thomas err = add_meta(m, v);
482 f9c2e8e5 2022-02-13 thomas free(base_obj_id);
483 f9c2e8e5 2022-02-13 thomas return err;
486 f9c2e8e5 2022-02-13 thomas static const struct got_error *
487 f9c2e8e5 2022-02-13 thomas find_pack_for_reuse(struct got_packidx **best_packidx,
488 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
490 f9c2e8e5 2022-02-13 thomas const struct got_error *err;
491 f9c2e8e5 2022-02-13 thomas struct got_pathlist_head packidx_paths;
492 f9c2e8e5 2022-02-13 thomas struct got_pathlist_entry *pe;
493 f9c2e8e5 2022-02-13 thomas const char *best_packidx_path = NULL;
494 f9c2e8e5 2022-02-13 thomas int nobj_max = 0;
496 f9c2e8e5 2022-02-13 thomas TAILQ_INIT(&packidx_paths);
497 f9c2e8e5 2022-02-13 thomas *best_packidx = NULL;
499 f9c2e8e5 2022-02-13 thomas err = got_repo_list_packidx(&packidx_paths, repo);
501 f9c2e8e5 2022-02-13 thomas return err;
503 f9c2e8e5 2022-02-13 thomas TAILQ_FOREACH(pe, &packidx_paths, entry) {
504 f9c2e8e5 2022-02-13 thomas const char *path_packidx = pe->path;
505 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx;
506 f9c2e8e5 2022-02-13 thomas int nobj;
508 f9c2e8e5 2022-02-13 thomas err = got_repo_get_packidx(&packidx, path_packidx, repo);
512 f9c2e8e5 2022-02-13 thomas nobj = be32toh(packidx->hdr.fanout_table[0xff]);
513 f9c2e8e5 2022-02-13 thomas if (nobj > nobj_max) {
514 f9c2e8e5 2022-02-13 thomas best_packidx_path = path_packidx;
515 f9c2e8e5 2022-02-13 thomas nobj_max = nobj;
519 f9c2e8e5 2022-02-13 thomas if (best_packidx_path) {
520 f9c2e8e5 2022-02-13 thomas err = got_repo_get_packidx(best_packidx, best_packidx_path,
524 f9c2e8e5 2022-02-13 thomas TAILQ_FOREACH(pe, &packidx_paths, entry)
525 f9c2e8e5 2022-02-13 thomas free((void *)pe->path);
526 f9c2e8e5 2022-02-13 thomas got_pathlist_free(&packidx_paths);
527 f9c2e8e5 2022-02-13 thomas return err;
530 f9c2e8e5 2022-02-13 thomas struct search_deltas_arg {
531 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx;
532 f9c2e8e5 2022-02-13 thomas struct got_pack *pack;
533 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset;
534 f9c2e8e5 2022-02-13 thomas struct got_pack_metavec *v;
535 f9c2e8e5 2022-02-13 thomas int delta_cache_fd;
536 f9c2e8e5 2022-02-13 thomas struct got_repository *repo;
537 f9c2e8e5 2022-02-13 thomas got_pack_progress_cb progress_cb;
538 f9c2e8e5 2022-02-13 thomas void *progress_arg;
539 f9c2e8e5 2022-02-13 thomas struct got_ratelimit *rl;
540 f9c2e8e5 2022-02-13 thomas got_cancel_cb cancel_cb;
541 f9c2e8e5 2022-02-13 thomas void *cancel_arg;
542 f9c2e8e5 2022-02-13 thomas int ncommits;
545 f9c2e8e5 2022-02-13 thomas static const struct got_error *
546 f9c2e8e5 2022-02-13 thomas search_delta_for_object(struct got_object_id *id, void *data, void *arg)
548 f9c2e8e5 2022-02-13 thomas const struct got_error *err;
549 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m = data;
550 f9c2e8e5 2022-02-13 thomas struct search_deltas_arg *a = arg;
551 f9c2e8e5 2022-02-13 thomas int obj_idx;
552 f9c2e8e5 2022-02-13 thomas struct got_object *obj = NULL;
554 f9c2e8e5 2022-02-13 thomas if (a->cancel_cb) {
555 f9c2e8e5 2022-02-13 thomas err = (*a->cancel_cb)(a->cancel_arg);
557 f9c2e8e5 2022-02-13 thomas return err;
560 f9c2e8e5 2022-02-13 thomas if (!got_repo_check_packidx_bloom_filter(a->repo,
561 f9c2e8e5 2022-02-13 thomas a->packidx->path_packidx, id))
562 f9c2e8e5 2022-02-13 thomas return NULL;
564 f9c2e8e5 2022-02-13 thomas obj_idx = got_packidx_get_object_idx(a->packidx, id);
565 f9c2e8e5 2022-02-13 thomas if (obj_idx == -1)
566 f9c2e8e5 2022-02-13 thomas return NULL;
569 f9c2e8e5 2022-02-13 thomas * Opening and closing an object just to check its flags
570 f9c2e8e5 2022-02-13 thomas * is a bit expensive. We could have an imsg which requests
571 f9c2e8e5 2022-02-13 thomas * plain type/size information for an object without doing
572 f9c2e8e5 2022-02-13 thomas * work such as traversing the object's entire delta chain
573 f9c2e8e5 2022-02-13 thomas * to find the base object type, and other such info which
574 f9c2e8e5 2022-02-13 thomas * we don't really need here.
576 f9c2e8e5 2022-02-13 thomas err = got_object_open_from_packfile(&obj, &m->id, a->pack,
577 f9c2e8e5 2022-02-13 thomas a->packidx, obj_idx, a->repo);
579 f9c2e8e5 2022-02-13 thomas return err;
581 f9c2e8e5 2022-02-13 thomas if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
582 f9c2e8e5 2022-02-13 thomas reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
583 f9c2e8e5 2022-02-13 thomas a->delta_cache_fd, a->repo);
585 f9c2e8e5 2022-02-13 thomas goto done;
586 f9c2e8e5 2022-02-13 thomas err = report_progress(a->progress_cb, a->progress_arg, a->rl,
587 f9c2e8e5 2022-02-13 thomas 0L, a->ncommits, got_object_idset_num_elements(a->idset),
588 f9c2e8e5 2022-02-13 thomas a->v->nmeta, 0);
591 f9c2e8e5 2022-02-13 thomas got_object_close(obj);
592 f9c2e8e5 2022-02-13 thomas return err;
595 f9c2e8e5 2022-02-13 thomas static const struct got_error *
596 f9c2e8e5 2022-02-13 thomas search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
597 f9c2e8e5 2022-02-13 thomas int delta_cache_fd, int ncommits, struct got_repository *repo,
598 f9c2e8e5 2022-02-13 thomas got_pack_progress_cb progress_cb, void *progress_arg,
599 f9c2e8e5 2022-02-13 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
601 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
602 f9c2e8e5 2022-02-13 thomas char *path_packfile = NULL;
603 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx;
604 f9c2e8e5 2022-02-13 thomas struct got_pack *pack;
605 f9c2e8e5 2022-02-13 thomas struct search_deltas_arg sda;
607 f9c2e8e5 2022-02-13 thomas err = find_pack_for_reuse(&packidx, repo);
609 f9c2e8e5 2022-02-13 thomas return err;
611 f9c2e8e5 2022-02-13 thomas if (packidx == NULL)
612 f9c2e8e5 2022-02-13 thomas return NULL;
614 f9c2e8e5 2022-02-13 thomas err = got_packidx_get_packfile_path(&path_packfile,
615 f9c2e8e5 2022-02-13 thomas packidx->path_packidx);
617 f9c2e8e5 2022-02-13 thomas return err;
619 f9c2e8e5 2022-02-13 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
620 f9c2e8e5 2022-02-13 thomas if (pack == NULL) {
621 f9c2e8e5 2022-02-13 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
623 f9c2e8e5 2022-02-13 thomas goto done;
626 f9c2e8e5 2022-02-13 thomas sda.packidx = packidx;
627 f9c2e8e5 2022-02-13 thomas sda.pack = pack;
628 f9c2e8e5 2022-02-13 thomas sda.idset = idset;
629 f9c2e8e5 2022-02-13 thomas sda.v = v;
630 f9c2e8e5 2022-02-13 thomas sda.delta_cache_fd = delta_cache_fd;
631 f9c2e8e5 2022-02-13 thomas sda.repo = repo;
632 f9c2e8e5 2022-02-13 thomas sda.progress_cb = progress_cb;
633 f9c2e8e5 2022-02-13 thomas sda.progress_arg = progress_arg;
634 f9c2e8e5 2022-02-13 thomas sda.rl = rl;
635 f9c2e8e5 2022-02-13 thomas sda.cancel_cb = cancel_cb;
636 f9c2e8e5 2022-02-13 thomas sda.cancel_arg = cancel_arg;
637 f9c2e8e5 2022-02-13 thomas sda.ncommits = ncommits;
638 f9c2e8e5 2022-02-13 thomas err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
640 f9c2e8e5 2022-02-13 thomas free(path_packfile);
641 f9c2e8e5 2022-02-13 thomas return err;
644 f9c2e8e5 2022-02-13 thomas static const struct got_error *
645 f9c2e8e5 2022-02-13 thomas pick_deltas(struct got_pack_meta **meta, int nmeta, int ncommits,
646 f9c2e8e5 2022-02-13 thomas int nreused, FILE *delta_cache, struct got_repository *repo,
647 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
648 31ba2236 2022-01-05 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
650 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
651 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
652 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
653 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
654 3b6ceab7 2022-01-10 thomas int i, j, ndeltas, best_ndeltas;
655 3b6ceab7 2022-01-10 thomas off_t size, best_size;
656 959daf23 2021-10-29 thomas const int max_base_candidates = 3;
657 510885f7 2022-01-10 thomas size_t delta_memsize = 0;
658 510885f7 2022-01-10 thomas const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
659 ecf9545f 2021-10-15 thomas int outfd = -1;
661 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
662 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
663 e6bcace5 2021-06-22 stsp if (cancel_cb) {
664 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
668 31ba2236 2022-01-05 thomas err = report_progress(progress_cb, progress_arg, rl,
669 f9c2e8e5 2022-02-13 thomas 0L, ncommits, nreused + nmeta, nreused + i, 0);
671 31ba2236 2022-01-05 thomas goto done;
672 e6bcace5 2021-06-22 stsp m = meta[i];
674 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
675 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
678 f8bb1d3e 2021-10-17 thomas err = got_object_raw_open(&raw, &outfd, repo, &m->id);
681 ab6186ae 2021-10-15 thomas m->size = raw->size;
683 2b0ae357 2022-01-10 thomas if (raw->f == NULL) {
684 2b0ae357 2022-01-10 thomas err = got_deltify_init_mem(&m->dtab, raw->data,
685 2b0ae357 2022-01-10 thomas raw->hdrlen, raw->size + raw->hdrlen);
687 2b0ae357 2022-01-10 thomas err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
688 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen);
693 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
694 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
695 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
696 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
697 e6bcace5 2021-06-22 stsp n->dtab = NULL;
700 b79280dd 2021-10-15 thomas best_size = raw->size;
701 b79280dd 2021-10-15 thomas best_ndeltas = 0;
702 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
703 e6bcace5 2021-06-22 stsp if (cancel_cb) {
704 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
708 e6bcace5 2021-06-22 stsp base = meta[j];
709 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
710 62a05ae9 2021-10-29 thomas if (base->nchain >= 128 ||
711 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
714 8ab9215c 2021-10-15 thomas err = got_object_raw_open(&base_raw, &outfd, repo,
715 f8bb1d3e 2021-10-17 thomas &base->id);
719 2b0ae357 2022-01-10 thomas if (raw->f == NULL && base_raw->f == NULL) {
720 2b0ae357 2022-01-10 thomas err = got_deltify_mem_mem(&deltas, &ndeltas,
721 2b0ae357 2022-01-10 thomas raw->data, raw->hdrlen,
722 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
723 2b0ae357 2022-01-10 thomas base->dtab, base_raw->data,
724 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
725 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
726 2b0ae357 2022-01-10 thomas } else if (raw->f == NULL) {
727 2b0ae357 2022-01-10 thomas err = got_deltify_mem_file(&deltas, &ndeltas,
728 2b0ae357 2022-01-10 thomas raw->data, raw->hdrlen,
729 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
730 2b0ae357 2022-01-10 thomas base->dtab, base_raw->f,
731 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
732 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
733 2b0ae357 2022-01-10 thomas } else if (base_raw->f == NULL) {
734 2b0ae357 2022-01-10 thomas err = got_deltify_file_mem(&deltas, &ndeltas,
735 2b0ae357 2022-01-10 thomas raw->f, raw->hdrlen,
736 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
737 2b0ae357 2022-01-10 thomas base->dtab, base_raw->data,
738 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
739 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
741 2b0ae357 2022-01-10 thomas err = got_deltify(&deltas, &ndeltas,
742 2b0ae357 2022-01-10 thomas raw->f, raw->hdrlen,
743 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
744 2b0ae357 2022-01-10 thomas base->dtab, base_raw->f, base_raw->hdrlen,
745 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
747 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
748 e6bcace5 2021-06-22 stsp base_raw = NULL;
752 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
753 b79280dd 2021-10-15 thomas if (size + 32 < best_size){
755 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
756 e6bcace5 2021-06-22 stsp * replace it.
758 b79280dd 2021-10-15 thomas best_size = size;
759 b79280dd 2021-10-15 thomas free(best_deltas);
760 b79280dd 2021-10-15 thomas best_deltas = deltas;
761 b79280dd 2021-10-15 thomas best_ndeltas = ndeltas;
762 b79280dd 2021-10-15 thomas deltas = NULL;
763 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
764 e6bcace5 2021-06-22 stsp m->prev = base;
765 e6bcace5 2021-06-22 stsp m->head = base->head;
766 e6bcace5 2021-06-22 stsp if (m->head == NULL)
767 e6bcace5 2021-06-22 stsp m->head = base;
769 e6bcace5 2021-06-22 stsp free(deltas);
770 e6bcace5 2021-06-22 stsp deltas = NULL;
771 e6bcace5 2021-06-22 stsp ndeltas = 0;
775 b79280dd 2021-10-15 thomas if (best_ndeltas > 0) {
776 510885f7 2022-01-10 thomas if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
777 510885f7 2022-01-10 thomas delta_memsize + best_size <= max_delta_memsize) {
778 510885f7 2022-01-10 thomas delta_memsize += best_size;
779 3b6ceab7 2022-01-10 thomas err = encode_delta_in_mem(m, raw, best_deltas,
780 3b6ceab7 2022-01-10 thomas best_ndeltas, best_size, m->prev->size);
782 3b6ceab7 2022-01-10 thomas m->delta_offset = ftello(delta_cache);
785 f9c2e8e5 2022-02-13 thomas * Storing compressed delta data in the delta
786 f9c2e8e5 2022-02-13 thomas * cache file would probably be more efficient
787 f9c2e8e5 2022-02-13 thomas * than writing uncompressed delta data here
788 f9c2e8e5 2022-02-13 thomas * and compressing it while writing the pack
789 f9c2e8e5 2022-02-13 thomas * file. This would also allow for reusing
790 f9c2e8e5 2022-02-13 thomas * deltas in their compressed form.
792 3b6ceab7 2022-01-10 thomas err = encode_delta(m, raw, best_deltas,
793 3b6ceab7 2022-01-10 thomas best_ndeltas, m->prev->size, delta_cache);
795 b79280dd 2021-10-15 thomas free(best_deltas);
796 b79280dd 2021-10-15 thomas best_deltas = NULL;
797 b79280dd 2021-10-15 thomas best_ndeltas = 0;
799 b79280dd 2021-10-15 thomas goto done;
802 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
803 e6bcace5 2021-06-22 stsp raw = NULL;
806 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
807 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
808 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
811 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
812 e6bcace5 2021-06-22 stsp if (base_raw)
813 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
814 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
815 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
816 b79280dd 2021-10-15 thomas free(deltas);
817 b79280dd 2021-10-15 thomas free(best_deltas);
818 e6bcace5 2021-06-22 stsp return err;
821 e6bcace5 2021-06-22 stsp static const struct got_error *
822 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
823 05118f5a 2021-06-22 stsp struct got_repository *repo)
825 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
826 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
829 05118f5a 2021-06-22 stsp *found = 0;
831 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
832 05118f5a 2021-06-22 stsp if (err == NULL)
833 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
834 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
835 05118f5a 2021-06-22 stsp err = NULL;
836 05118f5a 2021-06-22 stsp return err;
839 07165b17 2021-07-01 stsp static const int obj_types[] = {
840 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_ANY,
841 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_COMMIT,
842 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TREE,
843 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_BLOB,
844 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TAG,
845 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_OFFSET_DELTA,
846 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_REF_DELTA
849 05118f5a 2021-06-22 stsp static const struct got_error *
850 f9c2e8e5 2022-02-13 thomas add_object(int want_meta, struct got_object_idset *idset,
851 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
852 05118f5a 2021-06-22 stsp time_t mtime, int loose_obj_only, struct got_repository *repo)
854 e6bcace5 2021-06-22 stsp const struct got_error *err;
855 f9c2e8e5 2022-02-13 thomas 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 05118f5a 2021-06-22 stsp if (is_packed)
863 05118f5a 2021-06-22 stsp return NULL;
866 f9c2e8e5 2022-02-13 thomas if (want_meta) {
867 f9c2e8e5 2022-02-13 thomas err = alloc_meta(&m, id, path, obj_type, mtime);
869 f9c2e8e5 2022-02-13 thomas return err;
872 f9c2e8e5 2022-02-13 thomas return got_object_idset_add(idset, id, m);
875 e6bcace5 2021-06-22 stsp static const struct got_error *
876 f9c2e8e5 2022-02-13 thomas load_tree_entries(struct got_object_id_queue *ids, int want_meta,
877 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
878 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
879 05118f5a 2021-06-22 stsp int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
881 e6bcace5 2021-06-22 stsp const struct got_error *err;
882 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
883 e6bcace5 2021-06-22 stsp char *p = NULL;
886 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
888 e6bcace5 2021-06-22 stsp return err;
890 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
891 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
892 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
893 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
895 e6bcace5 2021-06-22 stsp if (cancel_cb) {
896 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
901 08511b5e 2021-09-24 thomas if (got_object_tree_entry_is_submodule(e) ||
902 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
905 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
906 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
907 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
911 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
912 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
913 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
916 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
917 08511b5e 2021-09-24 thomas } else if (S_ISREG(mode) || S_ISLNK(mode)) {
918 f9c2e8e5 2022-02-13 thomas err = add_object(want_meta, idset, id, p,
919 f9c2e8e5 2022-02-13 thomas GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo);
927 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
929 e6bcace5 2021-06-22 stsp return err;
932 e6bcace5 2021-06-22 stsp static const struct got_error *
933 f9c2e8e5 2022-02-13 thomas load_tree(int want_meta, struct got_object_idset *idset,
934 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
935 05118f5a 2021-06-22 stsp int loose_obj_only, struct got_repository *repo,
936 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
938 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
939 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
940 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
942 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
943 e6bcace5 2021-06-22 stsp return NULL;
945 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
947 e6bcace5 2021-06-22 stsp return err;
949 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
950 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
952 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
953 e6bcace5 2021-06-22 stsp if (cancel_cb) {
954 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
959 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
960 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
962 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
963 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
967 f9c2e8e5 2022-02-13 thomas err = add_object(want_meta, idset, qid->id, dpath,
968 f9c2e8e5 2022-02-13 thomas GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo);
970 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
974 f9c2e8e5 2022-02-13 thomas err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
975 f9c2e8e5 2022-02-13 thomas dpath, mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
976 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
981 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
982 e6bcace5 2021-06-22 stsp return err;
985 e6bcace5 2021-06-22 stsp static const struct got_error *
986 f9c2e8e5 2022-02-13 thomas load_commit(int want_meta, struct got_object_idset *idset,
987 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
988 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
990 e6bcace5 2021-06-22 stsp const struct got_error *err;
991 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
993 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
994 e6bcace5 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 05118f5a 2021-06-22 stsp if (is_packed)
1002 05118f5a 2021-06-22 stsp return NULL;
1005 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1007 e6bcace5 2021-06-22 stsp return err;
1009 f9c2e8e5 2022-02-13 thomas err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1010 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
1011 05118f5a 2021-06-22 stsp loose_obj_only, repo);
1013 e6bcace5 2021-06-22 stsp goto done;
1015 f9c2e8e5 2022-02-13 thomas err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1016 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
1017 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
1019 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1020 e6bcace5 2021-06-22 stsp return err;
1023 05118f5a 2021-06-22 stsp static const struct got_error *
1024 f9c2e8e5 2022-02-13 thomas load_tag(int want_meta, struct got_object_idset *idset,
1025 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1026 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1028 05118f5a 2021-06-22 stsp const struct got_error *err;
1029 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
1031 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
1032 05118f5a 2021-06-22 stsp return NULL;
1034 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1035 05118f5a 2021-06-22 stsp int is_packed;
1036 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1038 05118f5a 2021-06-22 stsp return err;
1039 05118f5a 2021-06-22 stsp if (is_packed)
1040 05118f5a 2021-06-22 stsp return NULL;
1043 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1045 05118f5a 2021-06-22 stsp return err;
1047 f9c2e8e5 2022-02-13 thomas err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1048 05118f5a 2021-06-22 stsp got_object_tag_get_tagger_time(tag),
1049 05118f5a 2021-06-22 stsp loose_obj_only, repo);
1051 05118f5a 2021-06-22 stsp goto done;
1053 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1054 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1055 f9c2e8e5 2022-02-13 thomas err = load_commit(want_meta, idset,
1056 05118f5a 2021-06-22 stsp got_object_tag_get_object_id(tag), repo,
1057 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
1059 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1060 f9c2e8e5 2022-02-13 thomas err = load_tree(want_meta, idset,
1061 f9c2e8e5 2022-02-13 thomas got_object_tag_get_object_id(tag), "",
1062 f9c2e8e5 2022-02-13 thomas got_object_tag_get_tagger_time(tag),
1063 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
1070 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1071 05118f5a 2021-06-22 stsp return err;
1074 e6bcace5 2021-06-22 stsp enum findtwixt_color {
1075 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
1076 e6bcace5 2021-06-22 stsp COLOR_DROP,
1077 e6bcace5 2021-06-22 stsp COLOR_BLANK,
1079 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
1080 e6bcace5 2021-06-22 stsp COLOR_KEEP,
1081 e6bcace5 2021-06-22 stsp COLOR_DROP,
1082 e6bcace5 2021-06-22 stsp COLOR_BLANK
1085 e6bcace5 2021-06-22 stsp static const struct got_error *
1086 e6bcace5 2021-06-22 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1087 e6bcace5 2021-06-22 stsp int color, struct got_repository *repo)
1089 e6bcace5 2021-06-22 stsp const struct got_error *err;
1090 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1092 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1094 e6bcace5 2021-06-22 stsp return err;
1096 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
1097 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
1098 e6bcace5 2021-06-22 stsp return NULL;
1101 e6bcace5 2021-06-22 stsp static const struct got_error *
1102 e6bcace5 2021-06-22 stsp drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1103 e6bcace5 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo,
1104 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1106 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1107 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1108 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
1109 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
1110 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1112 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
1114 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1116 e6bcace5 2021-06-22 stsp return err;
1117 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&ids, qid, entry);
1119 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
1120 e6bcace5 2021-06-22 stsp if (cancel_cb) {
1121 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
1126 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
1127 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
1129 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id)) {
1130 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1134 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, qid->id,
1135 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1136 e6bcace5 2021-06-22 stsp if (err) {
1137 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1141 e6bcace5 2021-06-22 stsp if (!got_object_idset_contains(keep, qid->id)) {
1142 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1146 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
1147 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1151 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
1152 e6bcace5 2021-06-22 stsp if (parents) {
1153 e6bcace5 2021-06-22 stsp err = got_object_id_queue_copy(parents, &ids);
1154 e6bcace5 2021-06-22 stsp if (err) {
1155 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1159 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1162 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1163 e6bcace5 2021-06-22 stsp return err;
1166 e6bcace5 2021-06-22 stsp struct append_id_arg {
1167 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1171 e6bcace5 2021-06-22 stsp static const struct got_error *
1172 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1174 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1176 e6bcace5 2021-06-22 stsp a->array[a->idx] = got_object_id_dup(id);
1177 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1178 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1181 e6bcace5 2021-06-22 stsp return NULL;
1184 e6bcace5 2021-06-22 stsp static const struct got_error *
1185 e6bcace5 2021-06-22 stsp findtwixt(struct got_object_id ***res, int *nres,
1186 e6bcace5 2021-06-22 stsp struct got_object_id **head, int nhead,
1187 e6bcace5 2021-06-22 stsp struct got_object_id **tail, int ntail,
1188 e6bcace5 2021-06-22 stsp struct got_repository *repo,
1189 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1191 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1192 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
1193 e6bcace5 2021-06-22 stsp struct got_object_idset *keep, *drop;
1194 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1195 05118f5a 2021-06-22 stsp int i, ncolor, nkeep, obj_type;
1197 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
1198 e6bcace5 2021-06-22 stsp *res = NULL;
1199 e6bcace5 2021-06-22 stsp *nres = 0;
1201 e6bcace5 2021-06-22 stsp keep = got_object_idset_alloc();
1202 e6bcace5 2021-06-22 stsp if (keep == NULL)
1203 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
1205 e6bcace5 2021-06-22 stsp drop = got_object_idset_alloc();
1206 e6bcace5 2021-06-22 stsp if (drop == NULL) {
1207 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_idset_alloc");
1208 e6bcace5 2021-06-22 stsp goto done;
1211 05118f5a 2021-06-22 stsp for (i = 0; i < nhead; i++) {
1212 05118f5a 2021-06-22 stsp struct got_object_id *id = head[i];
1213 05118f5a 2021-06-22 stsp if (id == NULL)
1215 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1217 05118f5a 2021-06-22 stsp return err;
1218 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
1220 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1222 05118f5a 2021-06-22 stsp goto done;
1224 05118f5a 2021-06-22 stsp for (i = 0; i < ntail; i++) {
1225 05118f5a 2021-06-22 stsp struct got_object_id *id = tail[i];
1226 05118f5a 2021-06-22 stsp if (id == NULL)
1228 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1230 05118f5a 2021-06-22 stsp return err;
1231 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
1233 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1235 05118f5a 2021-06-22 stsp goto done;
1238 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
1239 e6bcace5 2021-06-22 stsp int qcolor;
1240 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
1241 e6bcace5 2021-06-22 stsp qcolor = *((int *)qid->data);
1243 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id))
1244 e6bcace5 2021-06-22 stsp ncolor = COLOR_DROP;
1245 e6bcace5 2021-06-22 stsp else if (got_object_idset_contains(keep, qid->id))
1246 e6bcace5 2021-06-22 stsp ncolor = COLOR_KEEP;
1248 e6bcace5 2021-06-22 stsp ncolor = COLOR_BLANK;
1250 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1251 e6bcace5 2021-06-22 stsp qcolor == COLOR_KEEP)) {
1252 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
1253 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1257 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1258 e6bcace5 2021-06-22 stsp err = drop_commit(keep, drop, qid->id, repo,
1259 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
1261 e6bcace5 2021-06-22 stsp goto done;
1262 e6bcace5 2021-06-22 stsp } else if (ncolor == COLOR_BLANK) {
1263 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1264 e6bcace5 2021-06-22 stsp struct got_object_id *id;
1265 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
1266 e6bcace5 2021-06-22 stsp struct got_object_qid *pid;
1268 e6bcace5 2021-06-22 stsp id = got_object_id_dup(qid->id);
1269 e6bcace5 2021-06-22 stsp if (id == NULL) {
1270 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
1271 e6bcace5 2021-06-22 stsp goto done;
1273 e6bcace5 2021-06-22 stsp if (qcolor == COLOR_KEEP)
1274 07165b17 2021-07-01 stsp err = got_object_idset_add(keep, id,
1275 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1277 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, id,
1278 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1279 e6bcace5 2021-06-22 stsp if (err) {
1281 e6bcace5 2021-06-22 stsp goto done;
1284 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1285 e6bcace5 2021-06-22 stsp if (err) {
1287 e6bcace5 2021-06-22 stsp goto done;
1289 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
1290 e6bcace5 2021-06-22 stsp if (parents) {
1291 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parents, entry) {
1292 e6bcace5 2021-06-22 stsp err = queue_commit_id(&ids, pid->id,
1293 e6bcace5 2021-06-22 stsp qcolor, repo);
1294 e6bcace5 2021-06-22 stsp if (err) {
1296 e6bcace5 2021-06-22 stsp goto done;
1300 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1301 e6bcace5 2021-06-22 stsp commit = NULL;
1303 e6bcace5 2021-06-22 stsp /* should not happen */
1304 e6bcace5 2021-06-22 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
1305 e6bcace5 2021-06-22 stsp "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1306 e6bcace5 2021-06-22 stsp goto done;
1309 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
1310 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1313 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1314 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1315 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1316 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1317 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1318 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1319 e6bcace5 2021-06-22 stsp goto done;
1321 e6bcace5 2021-06-22 stsp arg.idx = 0;
1322 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1323 e6bcace5 2021-06-22 stsp if (err) {
1324 e6bcace5 2021-06-22 stsp free(arg.array);
1325 e6bcace5 2021-06-22 stsp goto done;
1327 e6bcace5 2021-06-22 stsp *res = arg.array;
1328 e6bcace5 2021-06-22 stsp *nres = nkeep;
1331 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1332 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1333 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1334 e6bcace5 2021-06-22 stsp return err;
1337 e6bcace5 2021-06-22 stsp static const struct got_error *
1338 f9c2e8e5 2022-02-13 thomas load_object_ids(struct got_object_idset *idset,
1339 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1340 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1341 f9a643e8 2022-02-13 thomas int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
1343 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1344 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1345 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
1347 e6bcace5 2021-06-22 stsp err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1348 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
1349 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
1350 e6bcace5 2021-06-22 stsp goto done;
1352 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1353 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
1354 05118f5a 2021-06-22 stsp if (id == NULL)
1356 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1358 05118f5a 2021-06-22 stsp return err;
1359 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
1361 f9c2e8e5 2022-02-13 thomas err = load_commit(0, idset, id, repo,
1362 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
1364 05118f5a 2021-06-22 stsp goto done;
1367 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1368 f4a2ff2d 2021-07-01 stsp struct got_object_id *id = theirs[i];
1369 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m;
1370 05118f5a 2021-06-22 stsp if (id == NULL)
1372 f9c2e8e5 2022-02-13 thomas m = got_object_idset_get(idset, id);
1373 f9c2e8e5 2022-02-13 thomas if (m == NULL) {
1374 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1376 07165b17 2021-07-01 stsp goto done;
1378 f9c2e8e5 2022-02-13 thomas obj_type = m->obj_type;
1379 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1381 f9c2e8e5 2022-02-13 thomas err = load_tag(0, idset, id, repo,
1382 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
1384 05118f5a 2021-06-22 stsp goto done;
1387 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
1388 f9c2e8e5 2022-02-13 thomas err = load_commit(1, idset, ids[i], repo,
1389 eca70f98 2021-09-03 stsp loose_obj_only, cancel_cb, cancel_arg);
1391 eca70f98 2021-09-03 stsp goto done;
1394 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1395 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1396 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m;
1397 05118f5a 2021-06-22 stsp if (id == NULL)
1399 f9c2e8e5 2022-02-13 thomas m = got_object_idset_get(idset, id);
1400 f9c2e8e5 2022-02-13 thomas if (m == NULL) {
1401 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1403 07165b17 2021-07-01 stsp goto done;
1405 f9c2e8e5 2022-02-13 thomas obj_type = m->obj_type;
1406 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1408 f9c2e8e5 2022-02-13 thomas err = load_tag(1, idset, id, repo,
1409 05118f5a 2021-06-22 stsp loose_obj_only, 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 e6bcace5 2021-06-22 stsp return err;
1421 e6bcace5 2021-06-22 stsp const struct got_error *
1422 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1426 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1427 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
1428 e6bcace5 2021-06-22 stsp if (n != len)
1429 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
1430 e6bcace5 2021-06-22 stsp return NULL;
1433 e6bcace5 2021-06-22 stsp static void
1434 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1436 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1437 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1438 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1439 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1442 e6bcace5 2021-06-22 stsp static int
1443 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1445 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1447 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1448 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1449 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1450 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1451 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
1452 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
1453 e6bcace5 2021-06-22 stsp if (ahd != bhd)
1454 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
1455 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1456 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1457 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
1460 f9c2e8e5 2022-02-13 thomas static int
1461 f9c2e8e5 2022-02-13 thomas reuse_write_order_cmp(const void *pa, const void *pb)
1463 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *a, *b;
1465 f9c2e8e5 2022-02-13 thomas a = *(struct got_pack_meta **)pa;
1466 f9c2e8e5 2022-02-13 thomas b = *(struct got_pack_meta **)pb;
1468 f9c2e8e5 2022-02-13 thomas if (a->reused_delta_offset < b->reused_delta_offset)
1469 f9c2e8e5 2022-02-13 thomas return -1;
1470 f9c2e8e5 2022-02-13 thomas if (a->reused_delta_offset > b->reused_delta_offset)
1471 f9c2e8e5 2022-02-13 thomas return 1;
1472 f9c2e8e5 2022-02-13 thomas return 0;
1475 e6bcace5 2021-06-22 stsp static const struct got_error *
1476 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1480 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1482 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1483 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1484 e6bcace5 2021-06-22 stsp len >>= 4;
1485 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1486 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1487 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1488 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1489 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1490 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1493 e6bcace5 2021-06-22 stsp *hdrlen = i;
1494 e6bcace5 2021-06-22 stsp return NULL;
1497 e6bcace5 2021-06-22 stsp static int
1498 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1501 e6bcace5 2021-06-22 stsp char rbuf[8];
1503 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1504 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1505 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1506 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1510 e6bcace5 2021-06-22 stsp while (i > 0)
1511 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1515 e6bcace5 2021-06-22 stsp static const struct got_error *
1516 3b6ceab7 2022-01-10 thomas deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1517 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m)
1519 3b6ceab7 2022-01-10 thomas const struct got_error *err;
1520 3b6ceab7 2022-01-10 thomas char buf[32];
1523 f9c2e8e5 2022-02-13 thomas if (m->prev->off != 0) {
1524 3b6ceab7 2022-01-10 thomas err = packhdr(&nh, buf, sizeof(buf),
1525 3b6ceab7 2022-01-10 thomas GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1526 3b6ceab7 2022-01-10 thomas if (err)
1527 3b6ceab7 2022-01-10 thomas return err;
1528 3b6ceab7 2022-01-10 thomas nh += packoff(buf + nh, m->off - m->prev->off);
1529 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, buf, nh, ctx);
1530 3b6ceab7 2022-01-10 thomas if (err)
1531 3b6ceab7 2022-01-10 thomas return err;
1532 3b6ceab7 2022-01-10 thomas *packfile_size += nh;
1533 3b6ceab7 2022-01-10 thomas } else {
1534 3b6ceab7 2022-01-10 thomas err = packhdr(&nh, buf, sizeof(buf),
1535 3b6ceab7 2022-01-10 thomas GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1536 3b6ceab7 2022-01-10 thomas if (err)
1537 3b6ceab7 2022-01-10 thomas return err;
1538 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, buf, nh, ctx);
1539 3b6ceab7 2022-01-10 thomas if (err)
1540 3b6ceab7 2022-01-10 thomas return err;
1541 3b6ceab7 2022-01-10 thomas *packfile_size += nh;
1542 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, m->prev->id.sha1,
1543 3b6ceab7 2022-01-10 thomas sizeof(m->prev->id.sha1), ctx);
1544 3b6ceab7 2022-01-10 thomas if (err)
1545 3b6ceab7 2022-01-10 thomas return err;
1546 3b6ceab7 2022-01-10 thomas *packfile_size += sizeof(m->prev->id.sha1);
1549 3b6ceab7 2022-01-10 thomas return NULL;
1552 3b6ceab7 2022-01-10 thomas static const struct got_error *
1553 f9c2e8e5 2022-02-13 thomas write_packed_object(off_t *packfile_size, FILE *packfile,
1554 f9c2e8e5 2022-02-13 thomas FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1555 f9c2e8e5 2022-02-13 thomas SHA1_CTX *ctx, struct got_repository *repo)
1557 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
1558 f9c2e8e5 2022-02-13 thomas struct got_deflate_checksum csum;
1559 f9c2e8e5 2022-02-13 thomas char buf[32];
1561 f9c2e8e5 2022-02-13 thomas struct got_raw_object *raw = NULL;
1562 f9c2e8e5 2022-02-13 thomas off_t outlen;
1564 f9c2e8e5 2022-02-13 thomas csum.output_sha1 = ctx;
1565 f9c2e8e5 2022-02-13 thomas csum.output_crc = NULL;
1567 f9c2e8e5 2022-02-13 thomas m->off = ftello(packfile);
1568 f9c2e8e5 2022-02-13 thomas if (m->delta_len == 0) {
1569 f9c2e8e5 2022-02-13 thomas err = got_object_raw_open(&raw, outfd, repo, &m->id);
1570 f9c2e8e5 2022-02-13 thomas if (err)
1571 f9c2e8e5 2022-02-13 thomas goto done;
1572 f9c2e8e5 2022-02-13 thomas err = packhdr(&nh, buf, sizeof(buf),
1573 f9c2e8e5 2022-02-13 thomas m->obj_type, raw->size);
1574 f9c2e8e5 2022-02-13 thomas if (err)
1575 f9c2e8e5 2022-02-13 thomas goto done;
1576 f9c2e8e5 2022-02-13 thomas err = hwrite(packfile, buf, nh, ctx);
1577 f9c2e8e5 2022-02-13 thomas if (err)
1578 f9c2e8e5 2022-02-13 thomas goto done;
1579 f9c2e8e5 2022-02-13 thomas *packfile_size += nh;
1580 f9c2e8e5 2022-02-13 thomas if (raw->f == NULL) {
1581 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file_mmap(&outlen,
1582 f9c2e8e5 2022-02-13 thomas raw->data + raw->hdrlen, 0, raw->size,
1583 f9c2e8e5 2022-02-13 thomas packfile, &csum);
1584 f9c2e8e5 2022-02-13 thomas if (err)
1585 f9c2e8e5 2022-02-13 thomas goto done;
1586 f9c2e8e5 2022-02-13 thomas } else {
1587 f9c2e8e5 2022-02-13 thomas if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1588 f9c2e8e5 2022-02-13 thomas == -1) {
1589 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
1590 f9c2e8e5 2022-02-13 thomas goto done;
1592 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file(&outlen, raw->f,
1593 f9c2e8e5 2022-02-13 thomas raw->size, packfile, &csum);
1594 f9c2e8e5 2022-02-13 thomas if (err)
1595 f9c2e8e5 2022-02-13 thomas goto done;
1597 f9c2e8e5 2022-02-13 thomas *packfile_size += outlen;
1598 f9c2e8e5 2022-02-13 thomas got_object_raw_close(raw);
1599 f9c2e8e5 2022-02-13 thomas raw = NULL;
1600 f9c2e8e5 2022-02-13 thomas } else if (m->delta_buf) {
1601 f9c2e8e5 2022-02-13 thomas err = deltahdr(packfile_size, ctx, packfile, m);
1602 f9c2e8e5 2022-02-13 thomas if (err)
1603 f9c2e8e5 2022-02-13 thomas goto done;
1604 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file_mmap(&outlen,
1605 f9c2e8e5 2022-02-13 thomas m->delta_buf, 0, m->delta_len, packfile, &csum);
1606 f9c2e8e5 2022-02-13 thomas if (err)
1607 f9c2e8e5 2022-02-13 thomas goto done;
1608 f9c2e8e5 2022-02-13 thomas *packfile_size += outlen;
1609 f9c2e8e5 2022-02-13 thomas free(m->delta_buf);
1610 f9c2e8e5 2022-02-13 thomas m->delta_buf = NULL;
1611 f9c2e8e5 2022-02-13 thomas } else {
1612 f9c2e8e5 2022-02-13 thomas if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1613 f9c2e8e5 2022-02-13 thomas == -1) {
1614 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
1615 f9c2e8e5 2022-02-13 thomas goto done;
1617 f9c2e8e5 2022-02-13 thomas err = deltahdr(packfile_size, ctx, packfile, m);
1618 f9c2e8e5 2022-02-13 thomas if (err)
1619 f9c2e8e5 2022-02-13 thomas goto done;
1620 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file(&outlen, delta_cache,
1621 f9c2e8e5 2022-02-13 thomas m->delta_len, packfile, &csum);
1622 f9c2e8e5 2022-02-13 thomas if (err)
1623 f9c2e8e5 2022-02-13 thomas goto done;
1624 f9c2e8e5 2022-02-13 thomas *packfile_size += outlen;
1627 f9c2e8e5 2022-02-13 thomas if (raw)
1628 f9c2e8e5 2022-02-13 thomas got_object_raw_close(raw);
1629 f9c2e8e5 2022-02-13 thomas return err;
1632 f9c2e8e5 2022-02-13 thomas static const struct got_error *
1633 b79280dd 2021-10-15 thomas genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1634 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **deltify, int ndeltify,
1635 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **reuse, int nreuse,
1636 f9c2e8e5 2022-02-13 thomas int nours, struct got_repository *repo,
1637 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1638 31ba2236 2022-01-05 thomas struct got_ratelimit *rl,
1639 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1641 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1643 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1644 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1645 e4d8ab47 2021-10-15 thomas char buf[32];
1646 e8f02263 2022-01-23 thomas size_t n;
1647 f9c2e8e5 2022-02-13 thomas off_t packfile_size = 0;
1648 ecf9545f 2021-10-15 thomas int outfd = -1;
1650 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1652 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1654 e6bcace5 2021-06-22 stsp return err;
1655 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1656 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1658 e6bcace5 2021-06-22 stsp goto done;
1659 f9c2e8e5 2022-02-13 thomas putbe32(buf, ndeltify + nreuse);
1660 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1662 e6bcace5 2021-06-22 stsp goto done;
1664 f9c2e8e5 2022-02-13 thomas qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1665 f9c2e8e5 2022-02-13 thomas write_order_cmp);
1666 f9c2e8e5 2022-02-13 thomas for (i = 0; i < ndeltify; i++) {
1667 31ba2236 2022-01-05 thomas err = report_progress(progress_cb, progress_arg, rl,
1668 f9c2e8e5 2022-02-13 thomas packfile_size, nours, ndeltify + nreuse,
1669 f9c2e8e5 2022-02-13 thomas ndeltify + nreuse, i);
1670 31ba2236 2022-01-05 thomas if (err)
1671 31ba2236 2022-01-05 thomas goto done;
1672 f9c2e8e5 2022-02-13 thomas m = deltify[i];
1673 f9c2e8e5 2022-02-13 thomas err = write_packed_object(&packfile_size, packfile,
1674 f9c2e8e5 2022-02-13 thomas delta_cache, m, &outfd, &ctx, repo);
1675 f9c2e8e5 2022-02-13 thomas if (err)
1676 f9c2e8e5 2022-02-13 thomas goto done;
1679 f9c2e8e5 2022-02-13 thomas qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1680 f9c2e8e5 2022-02-13 thomas reuse_write_order_cmp);
1681 f9c2e8e5 2022-02-13 thomas for (i = 0; i < nreuse; i++) {
1682 f9c2e8e5 2022-02-13 thomas err = report_progress(progress_cb, progress_arg, rl,
1683 f9c2e8e5 2022-02-13 thomas packfile_size, nours, ndeltify + nreuse,
1684 f9c2e8e5 2022-02-13 thomas ndeltify + nreuse, ndeltify + i);
1685 f9c2e8e5 2022-02-13 thomas if (err)
1686 f9c2e8e5 2022-02-13 thomas goto done;
1687 f9c2e8e5 2022-02-13 thomas m = reuse[i];
1688 f9c2e8e5 2022-02-13 thomas err = write_packed_object(&packfile_size, packfile,
1689 f9c2e8e5 2022-02-13 thomas delta_cache, m, &outfd, &ctx, repo);
1690 f9c2e8e5 2022-02-13 thomas if (err)
1691 f9c2e8e5 2022-02-13 thomas goto done;
1694 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1695 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1696 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1697 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1698 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1699 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1700 31ba2236 2022-01-05 thomas if (progress_cb) {
1701 31ba2236 2022-01-05 thomas err = progress_cb(progress_arg, packfile_size, nours,
1702 f9c2e8e5 2022-02-13 thomas ndeltify + nreuse, ndeltify + nreuse,
1703 f9c2e8e5 2022-02-13 thomas ndeltify + nreuse);
1704 31ba2236 2022-01-05 thomas if (err)
1705 31ba2236 2022-01-05 thomas goto done;
1708 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
1709 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
1710 e6bcace5 2021-06-22 stsp return err;
1713 f9c2e8e5 2022-02-13 thomas static const struct got_error *
1714 f9c2e8e5 2022-02-13 thomas remove_unused_object(struct got_object_idset_element *entry, void *arg)
1716 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset = arg;
1718 f9c2e8e5 2022-02-13 thomas if (got_object_idset_get_element_data(entry) == NULL)
1719 f9c2e8e5 2022-02-13 thomas got_object_idset_remove_element(idset, entry);
1721 f9c2e8e5 2022-02-13 thomas return NULL;
1724 f9c2e8e5 2022-02-13 thomas static const struct got_error *
1725 f9c2e8e5 2022-02-13 thomas remove_reused_object(struct got_object_idset_element *entry, void *arg)
1727 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset = arg;
1728 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m;
1730 f9c2e8e5 2022-02-13 thomas m = got_object_idset_get_element_data(entry);
1731 f9c2e8e5 2022-02-13 thomas if (m->have_reused_delta)
1732 f9c2e8e5 2022-02-13 thomas got_object_idset_remove_element(idset, entry);
1734 f9c2e8e5 2022-02-13 thomas return NULL;
1737 f9c2e8e5 2022-02-13 thomas static const struct got_error *
1738 f9c2e8e5 2022-02-13 thomas add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1740 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m = data;
1741 f9c2e8e5 2022-02-13 thomas struct got_pack_metavec *v = arg;
1743 f9c2e8e5 2022-02-13 thomas return add_meta(m, v);
1746 e6bcace5 2021-06-22 stsp const struct got_error *
1747 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1748 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1749 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1750 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1751 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1752 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1754 e6bcace5 2021-06-22 stsp const struct got_error *err;
1755 f9c2e8e5 2022-02-13 thomas int delta_cache_fd = -1;
1756 b79280dd 2021-10-15 thomas FILE *delta_cache = NULL;
1757 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset;
1758 31ba2236 2022-01-05 thomas struct got_ratelimit rl;
1759 f9c2e8e5 2022-02-13 thomas struct got_pack_metavec deltify, reuse;
1761 f9c2e8e5 2022-02-13 thomas memset(&deltify, 0, sizeof(deltify));
1762 f9c2e8e5 2022-02-13 thomas memset(&reuse, 0, sizeof(reuse));
1764 31ba2236 2022-01-05 thomas got_ratelimit_init(&rl, 0, 500);
1766 f9c2e8e5 2022-02-13 thomas idset = got_object_idset_alloc();
1767 f9c2e8e5 2022-02-13 thomas if (idset == NULL)
1768 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("got_object_idset_alloc");
1770 f9c2e8e5 2022-02-13 thomas err = load_object_ids(idset, theirs, ntheirs, ours, nours,
1771 f9a643e8 2022-02-13 thomas repo, loose_obj_only, cancel_cb, cancel_arg);
1773 e6bcace5 2021-06-22 stsp return err;
1775 f9c2e8e5 2022-02-13 thomas err = got_object_idset_for_each_element(idset,
1776 f9c2e8e5 2022-02-13 thomas remove_unused_object, idset);
1777 f9c2e8e5 2022-02-13 thomas if (err)
1778 f9c2e8e5 2022-02-13 thomas goto done;
1780 f9a643e8 2022-02-13 thomas if (progress_cb) {
1781 f9a643e8 2022-02-13 thomas err = progress_cb(progress_arg, 0L, nours,
1782 f9a643e8 2022-02-13 thomas got_object_idset_num_elements(idset), 0, 0);
1783 f9a643e8 2022-02-13 thomas if (err)
1784 f9a643e8 2022-02-13 thomas goto done;
1787 f9c2e8e5 2022-02-13 thomas if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1788 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1789 05118f5a 2021-06-22 stsp goto done;
1792 f9c2e8e5 2022-02-13 thomas delta_cache_fd = got_opentempfd();
1793 f9c2e8e5 2022-02-13 thomas if (delta_cache_fd == -1) {
1794 b79280dd 2021-10-15 thomas err = got_error_from_errno("got_opentemp");
1795 b79280dd 2021-10-15 thomas goto done;
1798 f9c2e8e5 2022-02-13 thomas reuse.metasz = 64;
1799 f9c2e8e5 2022-02-13 thomas reuse.meta = calloc(reuse.metasz,
1800 f9c2e8e5 2022-02-13 thomas sizeof(struct got_pack_meta *));
1801 f9c2e8e5 2022-02-13 thomas if (reuse.meta == NULL) {
1802 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("calloc");
1803 f9c2e8e5 2022-02-13 thomas goto done;
1806 f9c2e8e5 2022-02-13 thomas err = search_deltas(&reuse, idset, delta_cache_fd, nours, repo,
1807 f9c2e8e5 2022-02-13 thomas progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1808 f9c2e8e5 2022-02-13 thomas if (err)
1809 f9c2e8e5 2022-02-13 thomas goto done;
1810 f9c2e8e5 2022-02-13 thomas if (reuse.nmeta > 0) {
1811 f9c2e8e5 2022-02-13 thomas err = got_object_idset_for_each_element(idset,
1812 f9c2e8e5 2022-02-13 thomas remove_reused_object, idset);
1813 f9c2e8e5 2022-02-13 thomas if (err)
1814 f9c2e8e5 2022-02-13 thomas goto done;
1817 f9c2e8e5 2022-02-13 thomas delta_cache = fdopen(delta_cache_fd, "a+");
1818 f9c2e8e5 2022-02-13 thomas if (delta_cache == NULL) {
1819 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fdopen");
1820 f9c2e8e5 2022-02-13 thomas goto done;
1822 f9c2e8e5 2022-02-13 thomas delta_cache_fd = -1;
1824 f9c2e8e5 2022-02-13 thomas if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1825 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
1826 f9c2e8e5 2022-02-13 thomas goto done;
1829 f9c2e8e5 2022-02-13 thomas deltify.meta = calloc(got_object_idset_num_elements(idset),
1830 f9c2e8e5 2022-02-13 thomas sizeof(struct got_pack_meta *));
1831 f9c2e8e5 2022-02-13 thomas if (deltify.meta == NULL) {
1832 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("calloc");
1833 f9c2e8e5 2022-02-13 thomas goto done;
1835 f9c2e8e5 2022-02-13 thomas deltify.metasz = got_object_idset_num_elements(idset);
1837 f9c2e8e5 2022-02-13 thomas err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1838 f9c2e8e5 2022-02-13 thomas if (err)
1839 f9c2e8e5 2022-02-13 thomas goto done;
1840 f9c2e8e5 2022-02-13 thomas if (deltify.nmeta > 0) {
1841 f9c2e8e5 2022-02-13 thomas err = pick_deltas(deltify.meta, deltify.nmeta, nours,
1842 f9c2e8e5 2022-02-13 thomas reuse.nmeta, delta_cache, repo,
1843 31ba2236 2022-01-05 thomas progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1845 f8a36e22 2021-08-26 stsp goto done;
1846 b79280dd 2021-10-15 thomas if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1847 b79280dd 2021-10-15 thomas err = got_error_from_errno("fseeko");
1848 b79280dd 2021-10-15 thomas goto done;
1852 f9c2e8e5 2022-02-13 thomas err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1853 f9c2e8e5 2022-02-13 thomas deltify.nmeta, reuse.meta, reuse.nmeta, nours, repo,
1854 f9c2e8e5 2022-02-13 thomas progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1856 e6bcace5 2021-06-22 stsp goto done;
1858 f9c2e8e5 2022-02-13 thomas free_nmeta(deltify.meta, deltify.nmeta);
1859 f9c2e8e5 2022-02-13 thomas free_nmeta(reuse.meta, reuse.nmeta);
1860 f9c2e8e5 2022-02-13 thomas got_object_idset_free(idset);
1861 f9c2e8e5 2022-02-13 thomas if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1862 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("close");
1863 b79280dd 2021-10-15 thomas if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1864 b79280dd 2021-10-15 thomas err = got_error_from_errno("fclose");
1865 e6bcace5 2021-06-22 stsp return err;