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"
55 2d9e6abf 2022-05-04 stsp #include "got_lib_inflate.h"
58 74881701 2021-10-15 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
62 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
65 70f8f24d 2022-04-14 stsp #ifndef nitems
66 70f8f24d 2022-04-14 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
69 e6bcace5 2021-06-22 stsp struct got_pack_meta {
70 e6bcace5 2021-06-22 stsp struct got_object_id id;
72 e6bcace5 2021-06-22 stsp int obj_type;
74 e6bcace5 2021-06-22 stsp time_t mtime;
76 e6bcace5 2021-06-22 stsp /* The best delta we picked */
77 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
78 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
79 2d9e6abf 2022-05-04 stsp unsigned char *delta_buf; /* if encoded in memory (compressed) */
80 2d9e6abf 2022-05-04 stsp off_t delta_offset; /* offset in delta cache file (compressed) */
81 5060d5a1 2022-01-10 stsp off_t delta_len; /* encoded delta length */
82 2d9e6abf 2022-05-04 stsp off_t delta_compressed_len; /* encoded+compressed delta length */
85 67fd6849 2022-02-13 stsp int have_reused_delta;
86 67fd6849 2022-02-13 stsp off_t reused_delta_offset; /* offset of delta in reused pack file */
87 67fd6849 2022-02-13 stsp struct got_object_id *base_obj_id;
89 e6bcace5 2021-06-22 stsp /* Only used for delta window */
90 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
92 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
96 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
97 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
102 e6bcace5 2021-06-22 stsp static const struct got_error *
103 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
104 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
106 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
107 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
109 e6bcace5 2021-06-22 stsp *new = NULL;
111 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
112 e6bcace5 2021-06-22 stsp if (m == NULL)
113 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
115 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
117 e6bcace5 2021-06-22 stsp m->path = strdup(path);
118 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
119 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
121 e6bcace5 2021-06-22 stsp return err;
124 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
125 e6bcace5 2021-06-22 stsp m->mtime = mtime;
127 e6bcace5 2021-06-22 stsp return NULL;
130 e6bcace5 2021-06-22 stsp static void
131 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
133 e6bcace5 2021-06-22 stsp if (meta == NULL)
135 e6bcace5 2021-06-22 stsp free(meta->path);
136 e6bcace5 2021-06-22 stsp meta->path = NULL;
137 5060d5a1 2022-01-10 stsp free(meta->delta_buf);
138 5060d5a1 2022-01-10 stsp meta->delta_buf = NULL;
139 67fd6849 2022-02-13 stsp free(meta->base_obj_id);
140 67fd6849 2022-02-13 stsp meta->base_obj_id = NULL;
143 e6bcace5 2021-06-22 stsp static void
144 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
148 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
149 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
150 e6bcace5 2021-06-22 stsp free(meta);
154 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
156 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
159 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
160 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
162 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
163 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
164 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
165 e6bcace5 2021-06-22 stsp if (cmp != 0)
166 e6bcace5 2021-06-22 stsp return cmp;
167 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
169 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
171 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
174 5060d5a1 2022-01-10 stsp static off_t
175 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
178 5060d5a1 2022-01-10 stsp off_t size = 32;
179 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
180 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
181 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
183 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
185 e6bcace5 2021-06-22 stsp return size;
188 5060d5a1 2022-01-10 stsp static const struct got_error *
189 5060d5a1 2022-01-10 stsp append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
193 5060d5a1 2022-01-10 stsp if (*len + nseg >= *sz) {
194 5060d5a1 2022-01-10 stsp while (*len + nseg >= *sz)
195 5060d5a1 2022-01-10 stsp *sz += *sz / 2;
196 5060d5a1 2022-01-10 stsp n = realloc(*p, *sz);
197 5060d5a1 2022-01-10 stsp if (n == NULL)
198 5060d5a1 2022-01-10 stsp return got_error_from_errno("realloc");
201 5060d5a1 2022-01-10 stsp memcpy(*p + *len, seg, nseg);
202 5060d5a1 2022-01-10 stsp *len += nseg;
203 5060d5a1 2022-01-10 stsp return NULL;
206 5060d5a1 2022-01-10 stsp static const struct got_error *
207 5060d5a1 2022-01-10 stsp encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
208 5060d5a1 2022-01-10 stsp struct got_delta_instruction *deltas, int ndeltas,
209 5060d5a1 2022-01-10 stsp off_t delta_size, off_t base_size)
211 5060d5a1 2022-01-10 stsp const struct got_error *err;
212 5060d5a1 2022-01-10 stsp unsigned char buf[16], *bp;
214 2d9e6abf 2022-05-04 stsp size_t len = 0, compressed_len;
215 2d9e6abf 2022-05-04 stsp off_t bufsize = delta_size;
217 5060d5a1 2022-01-10 stsp struct got_delta_instruction *d;
218 2d9e6abf 2022-05-04 stsp uint8_t *delta_buf;
220 2d9e6abf 2022-05-04 stsp delta_buf = malloc(bufsize);
221 2d9e6abf 2022-05-04 stsp if (delta_buf == NULL)
222 2d9e6abf 2022-05-04 stsp return got_error_from_errno("malloc");
224 5060d5a1 2022-01-10 stsp /* base object size */
225 5060d5a1 2022-01-10 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
226 5060d5a1 2022-01-10 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
227 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
228 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
229 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
230 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
232 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
236 5060d5a1 2022-01-10 stsp /* target object size */
237 5060d5a1 2022-01-10 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
238 5060d5a1 2022-01-10 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
239 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
240 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
241 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
242 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
244 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
248 5060d5a1 2022-01-10 stsp for (j = 0; j < ndeltas; j++) {
249 5060d5a1 2022-01-10 stsp d = &deltas[j];
250 5060d5a1 2022-01-10 stsp if (d->copy) {
251 5060d5a1 2022-01-10 stsp n = d->offset;
252 5060d5a1 2022-01-10 stsp bp = &buf[1];
253 5060d5a1 2022-01-10 stsp buf[0] = GOT_DELTA_BASE_COPY;
254 5060d5a1 2022-01-10 stsp for (i = 0; i < 4; i++) {
255 5060d5a1 2022-01-10 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
256 5060d5a1 2022-01-10 stsp buf[0] |= 1 << i;
257 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
259 5060d5a1 2022-01-10 stsp if (n == 0)
263 5060d5a1 2022-01-10 stsp n = d->len;
264 5060d5a1 2022-01-10 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
265 5060d5a1 2022-01-10 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
266 5060d5a1 2022-01-10 stsp for (i = 0; i < 3 && n > 0; i++) {
267 5060d5a1 2022-01-10 stsp buf[0] |= 1 << (i + 4);
268 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
272 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
273 5060d5a1 2022-01-10 stsp buf, bp - buf);
276 5060d5a1 2022-01-10 stsp } else if (o->f == NULL) {
278 5060d5a1 2022-01-10 stsp while (n != d->len) {
279 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
280 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
284 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
285 5060d5a1 2022-01-10 stsp o->data + o->hdrlen + d->offset + n,
289 5060d5a1 2022-01-10 stsp n += buf[0];
292 5060d5a1 2022-01-10 stsp char content[128];
294 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
295 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
299 5060d5a1 2022-01-10 stsp while (n != d->len) {
300 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
301 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
305 5060d5a1 2022-01-10 stsp r = fread(content, 1, buf[0], o->f);
306 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
307 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
310 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
311 5060d5a1 2022-01-10 stsp content, buf[0]);
314 5060d5a1 2022-01-10 stsp n += buf[0];
319 2d9e6abf 2022-05-04 stsp err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
320 2d9e6abf 2022-05-04 stsp NULL, NULL, delta_buf, 0, len);
324 5060d5a1 2022-01-10 stsp m->delta_len = len;
325 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
327 2d9e6abf 2022-05-04 stsp free(delta_buf);
328 2d9e6abf 2022-05-04 stsp return err;
331 74881701 2021-10-15 stsp static const struct got_error *
332 74881701 2021-10-15 stsp encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
333 74881701 2021-10-15 stsp struct got_delta_instruction *deltas, int ndeltas,
334 a319ca8c 2021-10-15 stsp off_t base_size, FILE *f)
336 2d9e6abf 2022-05-04 stsp const struct got_error *err;
337 a319ca8c 2021-10-15 stsp unsigned char buf[16], *bp;
340 2d9e6abf 2022-05-04 stsp struct got_deflate_buf zb;
341 a319ca8c 2021-10-15 stsp struct got_delta_instruction *d;
342 2d9e6abf 2022-05-04 stsp off_t delta_len = 0, compressed_len = 0;
344 2d9e6abf 2022-05-04 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
346 2d9e6abf 2022-05-04 stsp return err;
348 a319ca8c 2021-10-15 stsp /* base object size */
349 a319ca8c 2021-10-15 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
350 a319ca8c 2021-10-15 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
351 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
352 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
353 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
354 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
357 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
358 2d9e6abf 2022-05-04 stsp buf, 0, i, f, NULL);
361 2d9e6abf 2022-05-04 stsp delta_len += i;
363 a319ca8c 2021-10-15 stsp /* target object size */
364 a319ca8c 2021-10-15 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
365 a319ca8c 2021-10-15 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
366 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
367 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
368 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
369 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
372 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
373 2d9e6abf 2022-05-04 stsp buf, 0, i, f, NULL);
376 2d9e6abf 2022-05-04 stsp delta_len += i;
378 a319ca8c 2021-10-15 stsp for (j = 0; j < ndeltas; j++) {
379 a319ca8c 2021-10-15 stsp d = &deltas[j];
380 a319ca8c 2021-10-15 stsp if (d->copy) {
381 a319ca8c 2021-10-15 stsp n = d->offset;
382 a319ca8c 2021-10-15 stsp bp = &buf[1];
383 a319ca8c 2021-10-15 stsp buf[0] = GOT_DELTA_BASE_COPY;
384 a319ca8c 2021-10-15 stsp for (i = 0; i < 4; i++) {
385 a319ca8c 2021-10-15 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
386 a319ca8c 2021-10-15 stsp buf[0] |= 1 << i;
387 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
389 a319ca8c 2021-10-15 stsp if (n == 0)
392 a319ca8c 2021-10-15 stsp n = d->len;
393 a319ca8c 2021-10-15 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
394 a319ca8c 2021-10-15 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
395 a319ca8c 2021-10-15 stsp for (i = 0; i < 3 && n > 0; i++) {
396 a319ca8c 2021-10-15 stsp buf[0] |= 1 << (i + 4);
397 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
401 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
402 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, bp - buf, f, NULL);
405 2d9e6abf 2022-05-04 stsp delta_len += (bp - buf);
406 64a8571e 2022-01-07 stsp } else if (o->f == NULL) {
408 64a8571e 2022-01-07 stsp while (n != d->len) {
409 64a8571e 2022-01-07 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
410 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
411 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
414 2d9e6abf 2022-05-04 stsp delta_len++;
415 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
416 2d9e6abf 2022-05-04 stsp &compressed_len,
417 2d9e6abf 2022-05-04 stsp o->data + o->hdrlen + d->offset + n, 0,
418 2d9e6abf 2022-05-04 stsp buf[0], f, NULL);
421 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
422 64a8571e 2022-01-07 stsp n += buf[0];
425 a319ca8c 2021-10-15 stsp char content[128];
427 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
428 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
432 a319ca8c 2021-10-15 stsp while (n != d->len) {
433 a319ca8c 2021-10-15 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
434 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
435 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
438 2d9e6abf 2022-05-04 stsp delta_len++;
439 a319ca8c 2021-10-15 stsp r = fread(content, 1, buf[0], o->f);
440 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
441 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
444 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
445 2d9e6abf 2022-05-04 stsp &compressed_len, content, 0, buf[0], f,
449 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
450 a319ca8c 2021-10-15 stsp n += buf[0];
455 2d9e6abf 2022-05-04 stsp err = got_deflate_flush(&zb, f, NULL, &compressed_len);
459 2d9e6abf 2022-05-04 stsp /* sanity check */
460 2d9e6abf 2022-05-04 stsp if (compressed_len != ftello(f) - m->delta_offset) {
461 2d9e6abf 2022-05-04 stsp err = got_error(GOT_ERR_COMPRESSION);
465 2d9e6abf 2022-05-04 stsp m->delta_len = delta_len;
466 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
468 2d9e6abf 2022-05-04 stsp got_deflate_end(&zb);
469 2d9e6abf 2022-05-04 stsp return err;
472 211cfef0 2022-01-05 stsp static const struct got_error *
473 211cfef0 2022-01-05 stsp report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
474 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
475 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
476 b8af7c06 2022-03-15 stsp int nobj_written)
478 211cfef0 2022-01-05 stsp const struct got_error *err;
479 211cfef0 2022-01-05 stsp int elapsed;
481 211cfef0 2022-01-05 stsp if (progress_cb == NULL)
482 211cfef0 2022-01-05 stsp return NULL;
484 211cfef0 2022-01-05 stsp err = got_ratelimit_check(&elapsed, rl);
485 211cfef0 2022-01-05 stsp if (err || !elapsed)
486 211cfef0 2022-01-05 stsp return err;
488 b8af7c06 2022-03-15 stsp return progress_cb(progress_arg, ncolored, nfound, ntrees,
489 b8af7c06 2022-03-15 stsp packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
492 e6bcace5 2021-06-22 stsp static const struct got_error *
493 67fd6849 2022-02-13 stsp add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
495 67fd6849 2022-02-13 stsp if (v->nmeta == v->metasz){
496 67fd6849 2022-02-13 stsp size_t newsize = 2 * v->metasz;
497 67fd6849 2022-02-13 stsp struct got_pack_meta **new;
498 67fd6849 2022-02-13 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
499 67fd6849 2022-02-13 stsp if (new == NULL)
500 67fd6849 2022-02-13 stsp return got_error_from_errno("reallocarray");
501 67fd6849 2022-02-13 stsp v->meta = new;
502 67fd6849 2022-02-13 stsp v->metasz = newsize;
505 67fd6849 2022-02-13 stsp v->meta[v->nmeta++] = m;
506 67fd6849 2022-02-13 stsp return NULL;
509 67fd6849 2022-02-13 stsp static const struct got_error *
510 67fd6849 2022-02-13 stsp find_pack_for_reuse(struct got_packidx **best_packidx,
511 67fd6849 2022-02-13 stsp struct got_repository *repo)
513 9b576444 2022-03-14 stsp const struct got_error *err = NULL;
514 67fd6849 2022-02-13 stsp struct got_pathlist_entry *pe;
515 67fd6849 2022-02-13 stsp const char *best_packidx_path = NULL;
516 67fd6849 2022-02-13 stsp int nobj_max = 0;
518 67fd6849 2022-02-13 stsp *best_packidx = NULL;
520 9b576444 2022-03-14 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
521 67fd6849 2022-02-13 stsp const char *path_packidx = pe->path;
522 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
525 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
529 67fd6849 2022-02-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
530 67fd6849 2022-02-13 stsp if (nobj > nobj_max) {
531 67fd6849 2022-02-13 stsp best_packidx_path = path_packidx;
532 67fd6849 2022-02-13 stsp nobj_max = nobj;
536 67fd6849 2022-02-13 stsp if (best_packidx_path) {
537 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
541 67fd6849 2022-02-13 stsp return err;
544 fae7e038 2022-05-07 stsp struct send_id_arg {
545 fae7e038 2022-05-07 stsp struct imsgbuf *ibuf;
546 fae7e038 2022-05-07 stsp struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
547 fae7e038 2022-05-07 stsp size_t nids;
550 67fd6849 2022-02-13 stsp static const struct got_error *
551 fae7e038 2022-05-07 stsp send_id(struct got_object_id *id, void *data, void *arg)
553 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
554 fae7e038 2022-05-07 stsp struct send_id_arg *a = arg;
556 fae7e038 2022-05-07 stsp a->ids[a->nids++] = id;
558 fae7e038 2022-05-07 stsp if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
559 fae7e038 2022-05-07 stsp err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
561 67fd6849 2022-02-13 stsp return err;
562 fae7e038 2022-05-07 stsp a->nids = 0;
565 fae7e038 2022-05-07 stsp return NULL;
568 fae7e038 2022-05-07 stsp static const struct got_error *
569 fae7e038 2022-05-07 stsp recv_reused_delta(struct got_imsg_reused_delta *delta,
570 fae7e038 2022-05-07 stsp struct got_object_idset *idset, struct got_pack_metavec *v)
572 fae7e038 2022-05-07 stsp struct got_pack_meta *m, *base;
574 fae7e038 2022-05-07 stsp if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
575 fae7e038 2022-05-07 stsp delta->delta_offset +
576 fae7e038 2022-05-07 stsp delta->delta_compressed_size < delta->delta_offset)
577 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_BAD_PACKFILE);
579 fae7e038 2022-05-07 stsp m = got_object_idset_get(idset, &delta->id);
580 fae7e038 2022-05-07 stsp if (m == NULL)
581 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_OBJ);
583 fae7e038 2022-05-07 stsp base = got_object_idset_get(idset, &delta->base_id);
584 fae7e038 2022-05-07 stsp if (base == NULL)
585 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_OBJ);
587 fae7e038 2022-05-07 stsp m->delta_len = delta->delta_size;
588 fae7e038 2022-05-07 stsp m->delta_compressed_len = delta->delta_compressed_size;
589 fae7e038 2022-05-07 stsp m->delta_offset = delta->delta_out_offset;
590 fae7e038 2022-05-07 stsp m->prev = base;
591 fae7e038 2022-05-07 stsp m->size = delta->result_size;
592 fae7e038 2022-05-07 stsp m->have_reused_delta = 1;
593 fae7e038 2022-05-07 stsp m->reused_delta_offset = delta->delta_offset;
594 fae7e038 2022-05-07 stsp m->base_obj_id = got_object_id_dup(&delta->base_id);
595 fae7e038 2022-05-07 stsp if (m->base_obj_id == NULL)
596 fae7e038 2022-05-07 stsp return got_error_from_errno("got_object_id_dup");
598 fae7e038 2022-05-07 stsp return add_meta(m, v);
601 67fd6849 2022-02-13 stsp static const struct got_error *
602 67fd6849 2022-02-13 stsp search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
603 b8af7c06 2022-03-15 stsp int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
604 b8af7c06 2022-03-15 stsp struct got_repository *repo,
605 67fd6849 2022-02-13 stsp got_pack_progress_cb progress_cb, void *progress_arg,
606 67fd6849 2022-02-13 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
608 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
609 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
610 67fd6849 2022-02-13 stsp struct got_pack *pack;
611 fae7e038 2022-05-07 stsp struct send_id_arg sia;
612 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
613 fae7e038 2022-05-07 stsp size_t ndeltas, i;
615 67fd6849 2022-02-13 stsp err = find_pack_for_reuse(&packidx, repo);
617 67fd6849 2022-02-13 stsp return err;
619 67fd6849 2022-02-13 stsp if (packidx == NULL)
620 67fd6849 2022-02-13 stsp return NULL;
622 fae7e038 2022-05-07 stsp err = got_object_prepare_delta_reuse(&pack, packidx,
623 fae7e038 2022-05-07 stsp delta_cache_fd, repo);
625 67fd6849 2022-02-13 stsp return err;
627 fae7e038 2022-05-07 stsp memset(&sia, 0, sizeof(sia));
628 fae7e038 2022-05-07 stsp sia.ibuf = pack->privsep_child->ibuf;
629 fae7e038 2022-05-07 stsp err = got_object_idset_for_each(idset, send_id, &sia);
631 fae7e038 2022-05-07 stsp return err;
632 fae7e038 2022-05-07 stsp if (sia.nids > 0) {
633 fae7e038 2022-05-07 stsp err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
634 fae7e038 2022-05-07 stsp sia.ids, sia.nids);
636 fae7e038 2022-05-07 stsp return err;
638 fae7e038 2022-05-07 stsp err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
640 fae7e038 2022-05-07 stsp return err;
643 fae7e038 2022-05-07 stsp int done = 0;
645 fae7e038 2022-05-07 stsp if (cancel_cb) {
646 fae7e038 2022-05-07 stsp err = (*cancel_cb)(cancel_arg);
651 fae7e038 2022-05-07 stsp err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
652 fae7e038 2022-05-07 stsp pack->privsep_child->ibuf);
653 fae7e038 2022-05-07 stsp if (err || done)
656 fae7e038 2022-05-07 stsp for (i = 0; i < ndeltas; i++) {
657 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta *delta = &deltas[i];
658 fae7e038 2022-05-07 stsp err = recv_reused_delta(delta, idset, v);
663 fae7e038 2022-05-07 stsp err = report_progress(progress_cb, progress_arg, rl,
664 fae7e038 2022-05-07 stsp ncolored, nfound, ntrees, 0L, ncommits,
665 fae7e038 2022-05-07 stsp got_object_idset_num_elements(idset), v->nmeta, 0);
670 67fd6849 2022-02-13 stsp return err;
673 67fd6849 2022-02-13 stsp static const struct got_error *
674 b8af7c06 2022-03-15 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
675 b8af7c06 2022-03-15 stsp int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
676 b8af7c06 2022-03-15 stsp struct got_repository *repo,
677 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
678 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
680 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
681 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
682 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
683 74881701 2021-10-15 stsp struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
684 5060d5a1 2022-01-10 stsp int i, j, ndeltas, best_ndeltas;
685 5060d5a1 2022-01-10 stsp off_t size, best_size;
686 4f4d853e 2021-10-24 stsp const int max_base_candidates = 3;
687 402a5ec1 2022-01-10 stsp size_t delta_memsize = 0;
688 402a5ec1 2022-01-10 stsp const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
689 cc7a354a 2021-10-15 stsp int outfd = -1;
691 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
692 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
693 e6bcace5 2021-06-22 stsp if (cancel_cb) {
694 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
698 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
699 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
700 b8af7c06 2022-03-15 stsp nreused + i, 0);
703 e6bcace5 2021-06-22 stsp m = meta[i];
705 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
706 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
709 94dac27c 2021-10-15 stsp err = got_object_raw_open(&raw, &outfd, repo, &m->id);
712 600b755e 2021-10-14 stsp m->size = raw->size;
714 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
715 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&m->dtab, raw->data,
716 64a8571e 2022-01-07 stsp raw->hdrlen, raw->size + raw->hdrlen);
718 64a8571e 2022-01-07 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
719 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen);
724 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
725 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
726 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
727 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
728 e6bcace5 2021-06-22 stsp n->dtab = NULL;
731 74881701 2021-10-15 stsp best_size = raw->size;
732 74881701 2021-10-15 stsp best_ndeltas = 0;
733 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
734 e6bcace5 2021-06-22 stsp if (cancel_cb) {
735 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
739 e6bcace5 2021-06-22 stsp base = meta[j];
740 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
741 22edbce7 2021-10-24 stsp if (base->nchain >= 128 ||
742 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
745 d3c116bf 2021-10-15 stsp err = got_object_raw_open(&base_raw, &outfd, repo,
746 94dac27c 2021-10-15 stsp &base->id);
750 64a8571e 2022-01-07 stsp if (raw->f == NULL && base_raw->f == NULL) {
751 64a8571e 2022-01-07 stsp err = got_deltify_mem_mem(&deltas, &ndeltas,
752 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
753 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
754 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
755 64a8571e 2022-01-07 stsp base_raw->hdrlen,
756 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
757 64a8571e 2022-01-07 stsp } else if (raw->f == NULL) {
758 64a8571e 2022-01-07 stsp err = got_deltify_mem_file(&deltas, &ndeltas,
759 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
760 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
761 64a8571e 2022-01-07 stsp base->dtab, base_raw->f,
762 64a8571e 2022-01-07 stsp base_raw->hdrlen,
763 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
764 64a8571e 2022-01-07 stsp } else if (base_raw->f == NULL) {
765 64a8571e 2022-01-07 stsp err = got_deltify_file_mem(&deltas, &ndeltas,
766 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
767 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
768 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
769 64a8571e 2022-01-07 stsp base_raw->hdrlen,
770 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
772 64a8571e 2022-01-07 stsp err = got_deltify(&deltas, &ndeltas,
773 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
774 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
775 64a8571e 2022-01-07 stsp base->dtab, base_raw->f, base_raw->hdrlen,
776 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
778 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
779 e6bcace5 2021-06-22 stsp base_raw = NULL;
783 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
784 74881701 2021-10-15 stsp if (size + 32 < best_size){
786 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
787 e6bcace5 2021-06-22 stsp * replace it.
789 74881701 2021-10-15 stsp best_size = size;
790 74881701 2021-10-15 stsp free(best_deltas);
791 74881701 2021-10-15 stsp best_deltas = deltas;
792 74881701 2021-10-15 stsp best_ndeltas = ndeltas;
793 74881701 2021-10-15 stsp deltas = NULL;
794 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
795 e6bcace5 2021-06-22 stsp m->prev = base;
796 e6bcace5 2021-06-22 stsp m->head = base->head;
797 e6bcace5 2021-06-22 stsp if (m->head == NULL)
798 e6bcace5 2021-06-22 stsp m->head = base;
800 e6bcace5 2021-06-22 stsp free(deltas);
801 e6bcace5 2021-06-22 stsp deltas = NULL;
802 e6bcace5 2021-06-22 stsp ndeltas = 0;
806 74881701 2021-10-15 stsp if (best_ndeltas > 0) {
807 402a5ec1 2022-01-10 stsp if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
808 402a5ec1 2022-01-10 stsp delta_memsize + best_size <= max_delta_memsize) {
809 402a5ec1 2022-01-10 stsp delta_memsize += best_size;
810 5060d5a1 2022-01-10 stsp err = encode_delta_in_mem(m, raw, best_deltas,
811 5060d5a1 2022-01-10 stsp best_ndeltas, best_size, m->prev->size);
813 5060d5a1 2022-01-10 stsp m->delta_offset = ftello(delta_cache);
814 5060d5a1 2022-01-10 stsp err = encode_delta(m, raw, best_deltas,
815 5060d5a1 2022-01-10 stsp best_ndeltas, m->prev->size, delta_cache);
817 74881701 2021-10-15 stsp free(best_deltas);
818 74881701 2021-10-15 stsp best_deltas = NULL;
819 74881701 2021-10-15 stsp best_ndeltas = 0;
824 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
825 e6bcace5 2021-06-22 stsp raw = NULL;
828 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
829 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
830 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
833 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
834 e6bcace5 2021-06-22 stsp if (base_raw)
835 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
836 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
837 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
838 74881701 2021-10-15 stsp free(deltas);
839 74881701 2021-10-15 stsp free(best_deltas);
840 e6bcace5 2021-06-22 stsp return err;
843 e6bcace5 2021-06-22 stsp static const struct got_error *
844 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
845 05118f5a 2021-06-22 stsp struct got_repository *repo)
847 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
848 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
851 05118f5a 2021-06-22 stsp *found = 0;
853 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
854 05118f5a 2021-06-22 stsp if (err == NULL)
855 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
856 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
857 05118f5a 2021-06-22 stsp err = NULL;
858 05118f5a 2021-06-22 stsp return err;
861 05118f5a 2021-06-22 stsp static const struct got_error *
862 67fd6849 2022-02-13 stsp add_object(int want_meta, struct got_object_idset *idset,
863 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
864 6863cbf9 2022-03-21 stsp time_t mtime, int loose_obj_only, struct got_repository *repo,
865 6863cbf9 2022-03-21 stsp int *ncolored, int *nfound, int *ntrees,
866 6863cbf9 2022-03-21 stsp got_pack_progress_cb progress_cb, void *progress_arg,
867 6863cbf9 2022-03-21 stsp struct got_ratelimit *rl)
869 e6bcace5 2021-06-22 stsp const struct got_error *err;
870 67fd6849 2022-02-13 stsp struct got_pack_meta *m = NULL;
872 05118f5a 2021-06-22 stsp if (loose_obj_only) {
873 05118f5a 2021-06-22 stsp int is_packed;
874 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
876 05118f5a 2021-06-22 stsp return err;
877 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
878 05118f5a 2021-06-22 stsp return NULL;
881 67fd6849 2022-02-13 stsp if (want_meta) {
882 67fd6849 2022-02-13 stsp err = alloc_meta(&m, id, path, obj_type, mtime);
884 6863cbf9 2022-03-21 stsp return err;
886 6863cbf9 2022-03-21 stsp (*nfound)++;
887 6863cbf9 2022-03-21 stsp err = report_progress(progress_cb, progress_arg, rl,
888 6863cbf9 2022-03-21 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
890 bb6672b6 2022-04-14 tb clear_meta(m);
892 67fd6849 2022-02-13 stsp return err;
896 bb6672b6 2022-04-14 tb err = got_object_idset_add(idset, id, m);
898 bb6672b6 2022-04-14 tb clear_meta(m);
904 e6bcace5 2021-06-22 stsp static const struct got_error *
905 67fd6849 2022-02-13 stsp load_tree_entries(struct got_object_id_queue *ids, int want_meta,
906 2f8438b0 2022-05-04 stsp struct got_object_idset *idset, struct got_object_idset *idset_exclude,
907 2f8438b0 2022-05-04 stsp struct got_object_id *tree_id,
908 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
909 b8af7c06 2022-03-15 stsp int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
910 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
911 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
913 e6bcace5 2021-06-22 stsp const struct got_error *err;
914 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
915 e6bcace5 2021-06-22 stsp char *p = NULL;
918 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
920 b8af7c06 2022-03-15 stsp return err;
922 b8af7c06 2022-03-15 stsp (*ntrees)++;
923 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
924 b8af7c06 2022-03-15 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
926 e6bcace5 2021-06-22 stsp return err;
928 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
929 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
930 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
931 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
933 e6bcace5 2021-06-22 stsp if (cancel_cb) {
934 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
939 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
940 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset, id) ||
941 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
944 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
945 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
946 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
950 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
951 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
952 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
955 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
956 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
957 2f8438b0 2022-05-04 stsp err = add_object(want_meta,
958 2f8438b0 2022-05-04 stsp want_meta ? idset : idset_exclude, id, p,
959 6863cbf9 2022-03-21 stsp GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo,
960 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees,
961 6863cbf9 2022-03-21 stsp progress_cb, progress_arg, rl);
969 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
971 e6bcace5 2021-06-22 stsp return err;
974 e6bcace5 2021-06-22 stsp static const struct got_error *
975 67fd6849 2022-02-13 stsp load_tree(int want_meta, struct got_object_idset *idset,
976 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
977 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
978 b8af7c06 2022-03-15 stsp struct got_repository *repo, int loose_obj_only,
979 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
980 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
981 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
983 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
984 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
985 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
987 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, tree_id) ||
988 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, tree_id))
989 e6bcace5 2021-06-22 stsp return NULL;
991 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
993 e6bcace5 2021-06-22 stsp return err;
995 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
996 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
998 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
999 e6bcace5 2021-06-22 stsp if (cancel_cb) {
1000 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
1005 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
1006 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
1008 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, &qid->id) ||
1009 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, &qid->id)) {
1010 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1014 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1015 2f8438b0 2022-05-04 stsp &qid->id, dpath, GOT_OBJ_TYPE_TREE,
1016 2f8438b0 2022-05-04 stsp mtime, loose_obj_only, repo,
1017 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1018 e6bcace5 2021-06-22 stsp if (err) {
1019 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1023 2f8438b0 2022-05-04 stsp err = load_tree_entries(&tree_ids, want_meta, idset,
1024 2f8438b0 2022-05-04 stsp idset_exclude, &qid->id,
1025 b8af7c06 2022-03-15 stsp dpath, mtime, repo, loose_obj_only, ncolored, nfound,
1026 b8af7c06 2022-03-15 stsp ntrees, progress_cb, progress_arg, rl,
1027 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1028 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1033 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
1034 e6bcace5 2021-06-22 stsp return err;
1037 e6bcace5 2021-06-22 stsp static const struct got_error *
1038 67fd6849 2022-02-13 stsp load_commit(int want_meta, struct got_object_idset *idset,
1039 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1040 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1041 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
1042 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1043 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1045 e6bcace5 2021-06-22 stsp const struct got_error *err;
1046 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1048 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
1049 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1050 e6bcace5 2021-06-22 stsp return NULL;
1052 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1053 05118f5a 2021-06-22 stsp int is_packed;
1054 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1056 05118f5a 2021-06-22 stsp return err;
1057 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1058 05118f5a 2021-06-22 stsp return NULL;
1061 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1063 e6bcace5 2021-06-22 stsp return err;
1065 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1066 2f8438b0 2022-05-04 stsp id, "", GOT_OBJ_TYPE_COMMIT,
1067 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
1068 6863cbf9 2022-03-21 stsp loose_obj_only, repo,
1069 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1071 e6bcace5 2021-06-22 stsp goto done;
1073 2f8438b0 2022-05-04 stsp err = load_tree(want_meta, idset, idset_exclude,
1074 2f8438b0 2022-05-04 stsp got_object_commit_get_tree_id(commit),
1075 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
1076 b8af7c06 2022-03-15 stsp repo, loose_obj_only, ncolored, nfound, ntrees,
1077 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1079 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1080 e6bcace5 2021-06-22 stsp return err;
1083 05118f5a 2021-06-22 stsp static const struct got_error *
1084 67fd6849 2022-02-13 stsp load_tag(int want_meta, struct got_object_idset *idset,
1085 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1086 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1087 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
1088 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1089 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1091 05118f5a 2021-06-22 stsp const struct got_error *err;
1092 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
1094 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
1095 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1096 05118f5a 2021-06-22 stsp return NULL;
1098 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1099 05118f5a 2021-06-22 stsp int is_packed;
1100 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1102 05118f5a 2021-06-22 stsp return err;
1103 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1104 05118f5a 2021-06-22 stsp return NULL;
1107 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1109 05118f5a 2021-06-22 stsp return err;
1111 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1112 2f8438b0 2022-05-04 stsp id, "", GOT_OBJ_TYPE_TAG,
1113 6863cbf9 2022-03-21 stsp got_object_tag_get_tagger_time(tag), loose_obj_only, repo,
1114 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1116 05118f5a 2021-06-22 stsp goto done;
1118 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1119 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1120 2f8438b0 2022-05-04 stsp err = load_commit(want_meta, idset, idset_exclude,
1121 b8af7c06 2022-03-15 stsp got_object_tag_get_object_id(tag), repo, loose_obj_only,
1122 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1123 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1125 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1126 2f8438b0 2022-05-04 stsp err = load_tree(want_meta, idset, idset_exclude,
1127 67fd6849 2022-02-13 stsp got_object_tag_get_object_id(tag), "",
1128 b8af7c06 2022-03-15 stsp got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1129 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1130 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1137 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1138 05118f5a 2021-06-22 stsp return err;
1141 e6bcace5 2021-06-22 stsp enum findtwixt_color {
1142 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
1143 e6bcace5 2021-06-22 stsp COLOR_DROP,
1144 e6bcace5 2021-06-22 stsp COLOR_BLANK,
1145 70f8f24d 2022-04-14 stsp COLOR_SKIP,
1148 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
1149 e6bcace5 2021-06-22 stsp COLOR_KEEP,
1150 e6bcace5 2021-06-22 stsp COLOR_DROP,
1151 70f8f24d 2022-04-14 stsp COLOR_BLANK,
1152 70f8f24d 2022-04-14 stsp COLOR_SKIP,
1155 e6bcace5 2021-06-22 stsp static const struct got_error *
1156 70f8f24d 2022-04-14 stsp paint_commit(struct got_object_qid *qid, int color)
1158 70f8f24d 2022-04-14 stsp if (color < 0 || color >= nitems(findtwixt_colors))
1159 70f8f24d 2022-04-14 stsp return got_error(GOT_ERR_RANGE);
1161 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
1162 e6bcace5 2021-06-22 stsp return NULL;
1165 e6bcace5 2021-06-22 stsp static const struct got_error *
1166 70f8f24d 2022-04-14 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1167 70f8f24d 2022-04-14 stsp int color, struct got_repository *repo)
1169 70f8f24d 2022-04-14 stsp const struct got_error *err;
1170 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1172 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1174 e6bcace5 2021-06-22 stsp return err;
1176 70f8f24d 2022-04-14 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
1177 70f8f24d 2022-04-14 stsp return paint_commit(qid, color);
1180 e6bcace5 2021-06-22 stsp struct append_id_arg {
1181 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1183 70f8f24d 2022-04-14 stsp struct got_object_idset *drop;
1184 70f8f24d 2022-04-14 stsp struct got_object_idset *skip;
1187 e6bcace5 2021-06-22 stsp static const struct got_error *
1188 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1190 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1192 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(a->skip, id) ||
1193 70f8f24d 2022-04-14 stsp got_object_idset_contains(a->drop, id))
1194 70f8f24d 2022-04-14 stsp return NULL;
1196 70f8f24d 2022-04-14 stsp a->array[++a->idx] = got_object_id_dup(id);
1197 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1198 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1200 e6bcace5 2021-06-22 stsp return NULL;
1203 29e0594f 2022-04-09 stsp static const struct got_error *
1204 29e0594f 2022-04-09 stsp queue_commit_or_tag_id(struct got_object_id *id, int color,
1205 29e0594f 2022-04-09 stsp struct got_object_id_queue *ids, struct got_repository *repo)
1207 29e0594f 2022-04-09 stsp const struct got_error *err;
1208 29e0594f 2022-04-09 stsp struct got_tag_object *tag = NULL;
1209 29e0594f 2022-04-09 stsp int obj_type;
1211 29e0594f 2022-04-09 stsp err = got_object_get_type(&obj_type, repo, id);
1213 29e0594f 2022-04-09 stsp return err;
1215 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1216 29e0594f 2022-04-09 stsp err = got_object_open_as_tag(&tag, repo, id);
1218 29e0594f 2022-04-09 stsp return err;
1219 29e0594f 2022-04-09 stsp obj_type = got_object_tag_get_object_type(tag);
1220 29e0594f 2022-04-09 stsp id = got_object_tag_get_object_id(tag);
1223 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1224 29e0594f 2022-04-09 stsp err = queue_commit_id(ids, id, color, repo);
1226 29e0594f 2022-04-09 stsp goto done;
1230 29e0594f 2022-04-09 stsp got_object_tag_close(tag);
1231 29e0594f 2022-04-09 stsp return err;
1234 e6bcace5 2021-06-22 stsp static const struct got_error *
1235 70f8f24d 2022-04-14 stsp paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1236 14dbbf48 2022-04-10 stsp struct got_object_idset *keep, struct got_object_idset *drop,
1237 70f8f24d 2022-04-14 stsp struct got_object_idset *skip, struct got_repository *repo,
1238 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1239 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1241 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1242 14dbbf48 2022-04-10 stsp struct got_commit_object *commit = NULL;
1243 70f8f24d 2022-04-14 stsp const struct got_object_id_queue *parents;
1244 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1245 70f8f24d 2022-04-14 stsp int nqueued = nids, nskip = 0;
1247 70f8f24d 2022-04-14 stsp while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1248 70f8f24d 2022-04-14 stsp int color;
1250 57bc7b6d 2022-04-10 stsp if (cancel_cb) {
1251 57bc7b6d 2022-04-10 stsp err = cancel_cb(cancel_arg);
1256 14dbbf48 2022-04-10 stsp qid = STAILQ_FIRST(ids);
1257 70f8f24d 2022-04-14 stsp STAILQ_REMOVE_HEAD(ids, entry);
1258 70f8f24d 2022-04-14 stsp nqueued--;
1259 70f8f24d 2022-04-14 stsp color = *((int *)qid->data);
1260 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1263 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(skip, &qid->id)) {
1264 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1268 70f8f24d 2022-04-14 stsp switch (color) {
1269 70f8f24d 2022-04-14 stsp case COLOR_KEEP:
1270 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(keep, &qid->id)) {
1271 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1274 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(drop, &qid->id)) {
1275 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1277 70f8f24d 2022-04-14 stsp goto done;
1280 70f8f24d 2022-04-14 stsp (*ncolored)++;
1281 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(keep, &qid->id, NULL);
1283 70f8f24d 2022-04-14 stsp goto done;
1285 70f8f24d 2022-04-14 stsp case COLOR_DROP:
1286 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(drop, &qid->id)) {
1287 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1290 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(keep, &qid->id)) {
1291 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1293 70f8f24d 2022-04-14 stsp goto done;
1296 70f8f24d 2022-04-14 stsp (*ncolored)++;
1297 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(drop, &qid->id, NULL);
1299 70f8f24d 2022-04-14 stsp goto done;
1301 70f8f24d 2022-04-14 stsp case COLOR_SKIP:
1302 d7b5a0e8 2022-04-20 stsp if (!got_object_idset_contains(skip, &qid->id)) {
1303 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(skip, &qid->id,
1306 70f8f24d 2022-04-14 stsp goto done;
1310 70f8f24d 2022-04-14 stsp /* should not happen */
1311 70f8f24d 2022-04-14 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
1312 70f8f24d 2022-04-14 stsp "%s invalid commit color %d", __func__, color);
1313 70f8f24d 2022-04-14 stsp goto done;
1316 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
1317 b8af7c06 2022-03-15 stsp *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1322 d7b5a0e8 2022-04-20 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
1326 70f8f24d 2022-04-14 stsp parents = got_object_commit_get_parent_ids(commit);
1327 70f8f24d 2022-04-14 stsp if (parents) {
1328 70f8f24d 2022-04-14 stsp struct got_object_qid *pid;
1329 70f8f24d 2022-04-14 stsp color = *((int *)qid->data);
1330 70f8f24d 2022-04-14 stsp STAILQ_FOREACH(pid, parents, entry) {
1331 d7b5a0e8 2022-04-20 stsp err = queue_commit_id(ids, &pid->id, color,
1335 70f8f24d 2022-04-14 stsp nqueued++;
1336 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1341 70f8f24d 2022-04-14 stsp got_object_commit_close(commit);
1342 70f8f24d 2022-04-14 stsp commit = NULL;
1343 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1346 14dbbf48 2022-04-10 stsp if (commit)
1347 14dbbf48 2022-04-10 stsp got_object_commit_close(commit);
1348 14dbbf48 2022-04-10 stsp return err;
1351 14dbbf48 2022-04-10 stsp static const struct got_error *
1352 14dbbf48 2022-04-10 stsp findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1353 14dbbf48 2022-04-10 stsp struct got_object_id **head, int nhead,
1354 14dbbf48 2022-04-10 stsp struct got_object_id **tail, int ntail,
1355 14dbbf48 2022-04-10 stsp struct got_repository *repo,
1356 14dbbf48 2022-04-10 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1357 14dbbf48 2022-04-10 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1359 14dbbf48 2022-04-10 stsp const struct got_error *err = NULL;
1360 14dbbf48 2022-04-10 stsp struct got_object_id_queue ids;
1361 70f8f24d 2022-04-14 stsp struct got_object_idset *keep, *drop, *skip = NULL;
1362 14dbbf48 2022-04-10 stsp int i, nkeep;
1364 14dbbf48 2022-04-10 stsp STAILQ_INIT(&ids);
1365 14dbbf48 2022-04-10 stsp *res = NULL;
1366 14dbbf48 2022-04-10 stsp *nres = 0;
1367 14dbbf48 2022-04-10 stsp *ncolored = 0;
1369 14dbbf48 2022-04-10 stsp keep = got_object_idset_alloc();
1370 14dbbf48 2022-04-10 stsp if (keep == NULL)
1371 14dbbf48 2022-04-10 stsp return got_error_from_errno("got_object_idset_alloc");
1373 14dbbf48 2022-04-10 stsp drop = got_object_idset_alloc();
1374 14dbbf48 2022-04-10 stsp if (drop == NULL) {
1375 14dbbf48 2022-04-10 stsp err = got_error_from_errno("got_object_idset_alloc");
1376 14dbbf48 2022-04-10 stsp goto done;
1379 70f8f24d 2022-04-14 stsp skip = got_object_idset_alloc();
1380 70f8f24d 2022-04-14 stsp if (skip == NULL) {
1381 70f8f24d 2022-04-14 stsp err = got_error_from_errno("got_object_idset_alloc");
1382 70f8f24d 2022-04-14 stsp goto done;
1385 14dbbf48 2022-04-10 stsp for (i = 0; i < nhead; i++) {
1386 14dbbf48 2022-04-10 stsp struct got_object_id *id = head[i];
1387 14dbbf48 2022-04-10 stsp if (id == NULL)
1389 fbafdecf 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1391 14dbbf48 2022-04-10 stsp goto done;
1394 14dbbf48 2022-04-10 stsp for (i = 0; i < ntail; i++) {
1395 14dbbf48 2022-04-10 stsp struct got_object_id *id = tail[i];
1396 14dbbf48 2022-04-10 stsp if (id == NULL)
1398 14dbbf48 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1400 14dbbf48 2022-04-10 stsp goto done;
1403 70f8f24d 2022-04-14 stsp err = paint_commits(ncolored, &ids, nhead + ntail,
1404 70f8f24d 2022-04-14 stsp keep, drop, skip, repo, progress_cb, progress_arg, rl,
1405 70f8f24d 2022-04-14 stsp cancel_cb, cancel_arg);
1407 14dbbf48 2022-04-10 stsp goto done;
1409 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1410 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1411 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1412 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1413 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1414 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1415 e6bcace5 2021-06-22 stsp goto done;
1417 70f8f24d 2022-04-14 stsp arg.idx = -1;
1418 70f8f24d 2022-04-14 stsp arg.skip = skip;
1419 70f8f24d 2022-04-14 stsp arg.drop = drop;
1420 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1421 e6bcace5 2021-06-22 stsp if (err) {
1422 e6bcace5 2021-06-22 stsp free(arg.array);
1423 e6bcace5 2021-06-22 stsp goto done;
1425 e6bcace5 2021-06-22 stsp *res = arg.array;
1426 70f8f24d 2022-04-14 stsp *nres = arg.idx + 1;
1429 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1430 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1432 70f8f24d 2022-04-14 stsp got_object_idset_free(skip);
1433 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1434 e6bcace5 2021-06-22 stsp return err;
1437 e6bcace5 2021-06-22 stsp static const struct got_error *
1438 b8af7c06 2022-03-15 stsp load_object_ids(int *ncolored, int *nfound, int *ntrees,
1439 b8af7c06 2022-03-15 stsp struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1440 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1441 b8af7c06 2022-03-15 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1442 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1444 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1445 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1446 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
1447 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude;
1449 2f8438b0 2022-05-04 stsp idset_exclude = got_object_idset_alloc();
1450 2f8438b0 2022-05-04 stsp if (idset_exclude == NULL)
1451 2f8438b0 2022-05-04 stsp return got_error_from_errno("got_object_idset_alloc");
1453 b8af7c06 2022-03-15 stsp *ncolored = 0;
1454 b8af7c06 2022-03-15 stsp *nfound = 0;
1455 b8af7c06 2022-03-15 stsp *ntrees = 0;
1457 b8af7c06 2022-03-15 stsp err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1458 b8af7c06 2022-03-15 stsp repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1459 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
1460 e6bcace5 2021-06-22 stsp goto done;
1462 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1463 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
1464 05118f5a 2021-06-22 stsp if (id == NULL)
1466 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1468 05118f5a 2021-06-22 stsp return err;
1469 9d34261e 2022-04-07 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1470 2f8438b0 2022-05-04 stsp err = load_commit(0, idset, idset_exclude, id, repo,
1471 9d34261e 2022-04-07 stsp loose_obj_only, ncolored, nfound, ntrees,
1472 9d34261e 2022-04-07 stsp progress_cb, progress_arg, rl,
1473 9d34261e 2022-04-07 stsp cancel_cb, cancel_arg);
1475 07165b17 2021-07-01 stsp goto done;
1476 9d34261e 2022-04-07 stsp } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1477 2f8438b0 2022-05-04 stsp err = load_tag(0, idset, idset_exclude, id, repo,
1478 9d34261e 2022-04-07 stsp loose_obj_only, ncolored, nfound, ntrees,
1479 9d34261e 2022-04-07 stsp progress_cb, progress_arg, rl,
1480 9d34261e 2022-04-07 stsp cancel_cb, cancel_arg);
1482 9d34261e 2022-04-07 stsp goto done;
1486 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
1487 2f8438b0 2022-05-04 stsp err = load_commit(1, idset, idset_exclude,
1488 2f8438b0 2022-05-04 stsp ids[i], repo, loose_obj_only, ncolored, nfound, ntrees,
1489 2f8438b0 2022-05-04 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1491 eca70f98 2021-09-03 stsp goto done;
1494 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1495 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1496 67fd6849 2022-02-13 stsp struct got_pack_meta *m;
1497 05118f5a 2021-06-22 stsp if (id == NULL)
1499 67fd6849 2022-02-13 stsp m = got_object_idset_get(idset, id);
1500 67fd6849 2022-02-13 stsp if (m == NULL) {
1501 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1503 07165b17 2021-07-01 stsp goto done;
1505 67fd6849 2022-02-13 stsp obj_type = m->obj_type;
1506 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1508 2f8438b0 2022-05-04 stsp err = load_tag(1, idset, idset_exclude, id, repo,
1509 2f8438b0 2022-05-04 stsp loose_obj_only, ncolored, nfound, ntrees,
1510 2f8438b0 2022-05-04 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1512 e6bcace5 2021-06-22 stsp goto done;
1515 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
1516 e6bcace5 2021-06-22 stsp free(ids[i]);
1518 e6bcace5 2021-06-22 stsp free(ids);
1519 2f8438b0 2022-05-04 stsp got_object_idset_free(idset_exclude);
1520 e6bcace5 2021-06-22 stsp return err;
1523 e6bcace5 2021-06-22 stsp const struct got_error *
1524 2d9e6abf 2022-05-04 stsp hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
1528 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1529 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
1530 e6bcace5 2021-06-22 stsp if (n != len)
1531 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
1532 2d9e6abf 2022-05-04 stsp return NULL;
1535 2d9e6abf 2022-05-04 stsp const struct got_error *
1536 2d9e6abf 2022-05-04 stsp hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1538 2d9e6abf 2022-05-04 stsp unsigned char buf[65536];
1539 2d9e6abf 2022-05-04 stsp off_t remain = len;
1542 2d9e6abf 2022-05-04 stsp while (remain > 0) {
1543 2d9e6abf 2022-05-04 stsp size_t copylen = MIN(sizeof(buf), remain);
1544 2d9e6abf 2022-05-04 stsp n = fread(buf, 1, copylen, fsrc);
1545 2d9e6abf 2022-05-04 stsp if (n != copylen)
1546 2d9e6abf 2022-05-04 stsp return got_ferror(fsrc, GOT_ERR_IO);
1547 2d9e6abf 2022-05-04 stsp SHA1Update(ctx, buf, copylen);
1548 2d9e6abf 2022-05-04 stsp n = fwrite(buf, 1, copylen, fdst);
1549 2d9e6abf 2022-05-04 stsp if (n != copylen)
1550 2d9e6abf 2022-05-04 stsp return got_ferror(fdst, GOT_ERR_IO);
1551 2d9e6abf 2022-05-04 stsp remain -= copylen;
1554 e6bcace5 2021-06-22 stsp return NULL;
1557 e6bcace5 2021-06-22 stsp static void
1558 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1560 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1561 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1562 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1563 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1566 e6bcace5 2021-06-22 stsp static int
1567 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1569 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1571 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1572 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1573 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1574 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1575 611e8e31 2022-05-01 stsp if (bhd->mtime < ahd->mtime)
1576 611e8e31 2022-05-01 stsp return -1;
1577 611e8e31 2022-05-01 stsp if (bhd->mtime > ahd->mtime)
1579 611e8e31 2022-05-01 stsp if (bhd < ahd)
1580 611e8e31 2022-05-01 stsp return -1;
1581 611e8e31 2022-05-01 stsp if (bhd > ahd)
1583 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1584 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1585 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
1586 611e8e31 2022-05-01 stsp return -1;
1587 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
1589 611e8e31 2022-05-01 stsp return got_object_id_cmp(&a->id, &b->id);
1592 67fd6849 2022-02-13 stsp static int
1593 67fd6849 2022-02-13 stsp reuse_write_order_cmp(const void *pa, const void *pb)
1595 67fd6849 2022-02-13 stsp struct got_pack_meta *a, *b;
1597 67fd6849 2022-02-13 stsp a = *(struct got_pack_meta **)pa;
1598 67fd6849 2022-02-13 stsp b = *(struct got_pack_meta **)pb;
1600 67fd6849 2022-02-13 stsp if (a->reused_delta_offset < b->reused_delta_offset)
1601 67fd6849 2022-02-13 stsp return -1;
1602 67fd6849 2022-02-13 stsp if (a->reused_delta_offset > b->reused_delta_offset)
1607 e6bcace5 2021-06-22 stsp static const struct got_error *
1608 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1612 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1614 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1615 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1616 e6bcace5 2021-06-22 stsp len >>= 4;
1617 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1618 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1619 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1620 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1621 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1622 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1625 e6bcace5 2021-06-22 stsp *hdrlen = i;
1626 e6bcace5 2021-06-22 stsp return NULL;
1629 e6bcace5 2021-06-22 stsp static int
1630 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1633 e6bcace5 2021-06-22 stsp char rbuf[8];
1635 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1636 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1637 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1638 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1642 e6bcace5 2021-06-22 stsp while (i > 0)
1643 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1647 e6bcace5 2021-06-22 stsp static const struct got_error *
1648 5060d5a1 2022-01-10 stsp deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1649 67fd6849 2022-02-13 stsp struct got_pack_meta *m)
1651 5060d5a1 2022-01-10 stsp const struct got_error *err;
1652 5060d5a1 2022-01-10 stsp char buf[32];
1655 67fd6849 2022-02-13 stsp if (m->prev->off != 0) {
1656 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1657 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1659 5060d5a1 2022-01-10 stsp return err;
1660 5060d5a1 2022-01-10 stsp nh += packoff(buf + nh, m->off - m->prev->off);
1661 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1663 5060d5a1 2022-01-10 stsp return err;
1664 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1666 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1667 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1669 5060d5a1 2022-01-10 stsp return err;
1670 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1672 5060d5a1 2022-01-10 stsp return err;
1673 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1674 5060d5a1 2022-01-10 stsp err = hwrite(packfile, m->prev->id.sha1,
1675 5060d5a1 2022-01-10 stsp sizeof(m->prev->id.sha1), ctx);
1677 5060d5a1 2022-01-10 stsp return err;
1678 5060d5a1 2022-01-10 stsp *packfile_size += sizeof(m->prev->id.sha1);
1681 5060d5a1 2022-01-10 stsp return NULL;
1684 5060d5a1 2022-01-10 stsp static const struct got_error *
1685 67fd6849 2022-02-13 stsp write_packed_object(off_t *packfile_size, FILE *packfile,
1686 67fd6849 2022-02-13 stsp FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1687 67fd6849 2022-02-13 stsp SHA1_CTX *ctx, struct got_repository *repo)
1689 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
1690 67fd6849 2022-02-13 stsp struct got_deflate_checksum csum;
1691 67fd6849 2022-02-13 stsp char buf[32];
1693 67fd6849 2022-02-13 stsp struct got_raw_object *raw = NULL;
1694 67fd6849 2022-02-13 stsp off_t outlen;
1696 67fd6849 2022-02-13 stsp csum.output_sha1 = ctx;
1697 67fd6849 2022-02-13 stsp csum.output_crc = NULL;
1699 67fd6849 2022-02-13 stsp m->off = ftello(packfile);
1700 67fd6849 2022-02-13 stsp if (m->delta_len == 0) {
1701 67fd6849 2022-02-13 stsp err = got_object_raw_open(&raw, outfd, repo, &m->id);
1703 67fd6849 2022-02-13 stsp goto done;
1704 67fd6849 2022-02-13 stsp err = packhdr(&nh, buf, sizeof(buf),
1705 67fd6849 2022-02-13 stsp m->obj_type, raw->size);
1707 67fd6849 2022-02-13 stsp goto done;
1708 67fd6849 2022-02-13 stsp err = hwrite(packfile, buf, nh, ctx);
1710 67fd6849 2022-02-13 stsp goto done;
1711 67fd6849 2022-02-13 stsp *packfile_size += nh;
1712 67fd6849 2022-02-13 stsp if (raw->f == NULL) {
1713 67fd6849 2022-02-13 stsp err = got_deflate_to_file_mmap(&outlen,
1714 67fd6849 2022-02-13 stsp raw->data + raw->hdrlen, 0, raw->size,
1715 67fd6849 2022-02-13 stsp packfile, &csum);
1717 67fd6849 2022-02-13 stsp goto done;
1719 67fd6849 2022-02-13 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1721 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1722 67fd6849 2022-02-13 stsp goto done;
1724 67fd6849 2022-02-13 stsp err = got_deflate_to_file(&outlen, raw->f,
1725 67fd6849 2022-02-13 stsp raw->size, packfile, &csum);
1727 67fd6849 2022-02-13 stsp goto done;
1729 67fd6849 2022-02-13 stsp *packfile_size += outlen;
1730 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
1731 67fd6849 2022-02-13 stsp raw = NULL;
1732 67fd6849 2022-02-13 stsp } else if (m->delta_buf) {
1733 67fd6849 2022-02-13 stsp err = deltahdr(packfile_size, ctx, packfile, m);
1735 67fd6849 2022-02-13 stsp goto done;
1736 2d9e6abf 2022-05-04 stsp err = hwrite(packfile, m->delta_buf,
1737 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
1739 67fd6849 2022-02-13 stsp goto done;
1740 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
1741 67fd6849 2022-02-13 stsp free(m->delta_buf);
1742 67fd6849 2022-02-13 stsp m->delta_buf = NULL;
1744 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1746 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1747 67fd6849 2022-02-13 stsp goto done;
1749 67fd6849 2022-02-13 stsp err = deltahdr(packfile_size, ctx, packfile, m);
1751 67fd6849 2022-02-13 stsp goto done;
1752 2d9e6abf 2022-05-04 stsp err = hcopy(delta_cache, packfile,
1753 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
1755 67fd6849 2022-02-13 stsp goto done;
1756 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
1760 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
1761 67fd6849 2022-02-13 stsp return err;
1764 67fd6849 2022-02-13 stsp static const struct got_error *
1765 74881701 2021-10-15 stsp genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1766 67fd6849 2022-02-13 stsp struct got_pack_meta **deltify, int ndeltify,
1767 67fd6849 2022-02-13 stsp struct got_pack_meta **reuse, int nreuse,
1768 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees, int nours,
1769 b8af7c06 2022-03-15 stsp struct got_repository *repo,
1770 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1771 211cfef0 2022-01-05 stsp struct got_ratelimit *rl,
1772 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1774 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1776 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1777 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1778 08347b73 2021-10-14 stsp char buf[32];
1780 67fd6849 2022-02-13 stsp off_t packfile_size = 0;
1781 cc7a354a 2021-10-15 stsp int outfd = -1;
1783 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1785 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1787 e6bcace5 2021-06-22 stsp return err;
1788 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1789 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1791 e6bcace5 2021-06-22 stsp goto done;
1792 67fd6849 2022-02-13 stsp putbe32(buf, ndeltify + nreuse);
1793 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1795 e6bcace5 2021-06-22 stsp goto done;
1797 67fd6849 2022-02-13 stsp qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1798 67fd6849 2022-02-13 stsp write_order_cmp);
1799 67fd6849 2022-02-13 stsp for (i = 0; i < ndeltify; i++) {
1800 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1801 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
1802 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, i);
1804 211cfef0 2022-01-05 stsp goto done;
1805 67fd6849 2022-02-13 stsp m = deltify[i];
1806 67fd6849 2022-02-13 stsp err = write_packed_object(&packfile_size, packfile,
1807 67fd6849 2022-02-13 stsp delta_cache, m, &outfd, &ctx, repo);
1809 67fd6849 2022-02-13 stsp goto done;
1812 67fd6849 2022-02-13 stsp qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1813 67fd6849 2022-02-13 stsp reuse_write_order_cmp);
1814 67fd6849 2022-02-13 stsp for (i = 0; i < nreuse; i++) {
1815 67fd6849 2022-02-13 stsp err = report_progress(progress_cb, progress_arg, rl,
1816 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
1817 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1819 67fd6849 2022-02-13 stsp goto done;
1820 67fd6849 2022-02-13 stsp m = reuse[i];
1821 67fd6849 2022-02-13 stsp err = write_packed_object(&packfile_size, packfile,
1822 67fd6849 2022-02-13 stsp delta_cache, m, &outfd, &ctx, repo);
1824 67fd6849 2022-02-13 stsp goto done;
1827 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1828 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1829 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1830 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1831 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1832 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1833 211cfef0 2022-01-05 stsp if (progress_cb) {
1834 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1835 b8af7c06 2022-03-15 stsp packfile_size, nours, ndeltify + nreuse,
1836 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse);
1838 211cfef0 2022-01-05 stsp goto done;
1841 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
1842 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
1843 e6bcace5 2021-06-22 stsp return err;
1846 67fd6849 2022-02-13 stsp static const struct got_error *
1847 67fd6849 2022-02-13 stsp add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1849 67fd6849 2022-02-13 stsp struct got_pack_meta *m = data;
1850 67fd6849 2022-02-13 stsp struct got_pack_metavec *v = arg;
1852 f5e78e05 2022-05-04 stsp if (m->have_reused_delta)
1853 f5e78e05 2022-05-04 stsp return NULL;
1855 67fd6849 2022-02-13 stsp return add_meta(m, v);
1858 e6bcace5 2021-06-22 stsp const struct got_error *
1859 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1860 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1861 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1862 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1863 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1864 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1866 e6bcace5 2021-06-22 stsp const struct got_error *err;
1867 67fd6849 2022-02-13 stsp int delta_cache_fd = -1;
1868 74881701 2021-10-15 stsp FILE *delta_cache = NULL;
1869 67fd6849 2022-02-13 stsp struct got_object_idset *idset;
1870 211cfef0 2022-01-05 stsp struct got_ratelimit rl;
1871 67fd6849 2022-02-13 stsp struct got_pack_metavec deltify, reuse;
1872 b8af7c06 2022-03-15 stsp int ncolored = 0, nfound = 0, ntrees = 0;
1873 f5e78e05 2022-05-04 stsp size_t ndeltify;
1875 67fd6849 2022-02-13 stsp memset(&deltify, 0, sizeof(deltify));
1876 67fd6849 2022-02-13 stsp memset(&reuse, 0, sizeof(reuse));
1878 211cfef0 2022-01-05 stsp got_ratelimit_init(&rl, 0, 500);
1880 67fd6849 2022-02-13 stsp idset = got_object_idset_alloc();
1881 67fd6849 2022-02-13 stsp if (idset == NULL)
1882 67fd6849 2022-02-13 stsp return got_error_from_errno("got_object_idset_alloc");
1884 b8af7c06 2022-03-15 stsp err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1885 b8af7c06 2022-03-15 stsp ntheirs, ours, nours, repo, loose_obj_only,
1886 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1888 e6bcace5 2021-06-22 stsp return err;
1890 28526235 2022-02-13 stsp if (progress_cb) {
1891 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1892 b8af7c06 2022-03-15 stsp 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1894 28526235 2022-02-13 stsp goto done;
1897 67fd6849 2022-02-13 stsp if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1898 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1899 05118f5a 2021-06-22 stsp goto done;
1902 67fd6849 2022-02-13 stsp delta_cache_fd = got_opentempfd();
1903 67fd6849 2022-02-13 stsp if (delta_cache_fd == -1) {
1904 74881701 2021-10-15 stsp err = got_error_from_errno("got_opentemp");
1905 74881701 2021-10-15 stsp goto done;
1908 67fd6849 2022-02-13 stsp reuse.metasz = 64;
1909 67fd6849 2022-02-13 stsp reuse.meta = calloc(reuse.metasz,
1910 67fd6849 2022-02-13 stsp sizeof(struct got_pack_meta *));
1911 67fd6849 2022-02-13 stsp if (reuse.meta == NULL) {
1912 67fd6849 2022-02-13 stsp err = got_error_from_errno("calloc");
1913 67fd6849 2022-02-13 stsp goto done;
1916 b8af7c06 2022-03-15 stsp err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1917 b8af7c06 2022-03-15 stsp ntrees, nours, repo, progress_cb, progress_arg, &rl,
1918 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1920 67fd6849 2022-02-13 stsp goto done;
1922 67fd6849 2022-02-13 stsp delta_cache = fdopen(delta_cache_fd, "a+");
1923 67fd6849 2022-02-13 stsp if (delta_cache == NULL) {
1924 67fd6849 2022-02-13 stsp err = got_error_from_errno("fdopen");
1925 67fd6849 2022-02-13 stsp goto done;
1927 67fd6849 2022-02-13 stsp delta_cache_fd = -1;
1929 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1930 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1931 67fd6849 2022-02-13 stsp goto done;
1934 f5e78e05 2022-05-04 stsp ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1935 f5e78e05 2022-05-04 stsp if (ndeltify > 0) {
1936 f5e78e05 2022-05-04 stsp deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1937 f5e78e05 2022-05-04 stsp if (deltify.meta == NULL) {
1938 f5e78e05 2022-05-04 stsp err = got_error_from_errno("calloc");
1939 f5e78e05 2022-05-04 stsp goto done;
1941 f5e78e05 2022-05-04 stsp deltify.metasz = ndeltify;
1943 f5e78e05 2022-05-04 stsp err = got_object_idset_for_each(idset, add_meta_idset_cb,
1944 f5e78e05 2022-05-04 stsp &deltify);
1946 f8a36e22 2021-08-26 stsp goto done;
1947 f5e78e05 2022-05-04 stsp if (deltify.nmeta > 0) {
1948 f5e78e05 2022-05-04 stsp err = pick_deltas(deltify.meta, deltify.nmeta,
1949 f5e78e05 2022-05-04 stsp ncolored, nfound, ntrees, nours, reuse.nmeta,
1950 f5e78e05 2022-05-04 stsp delta_cache, repo, progress_cb, progress_arg, &rl,
1951 f5e78e05 2022-05-04 stsp cancel_cb, cancel_arg);
1953 f5e78e05 2022-05-04 stsp goto done;
1957 2d9e6abf 2022-05-04 stsp if (fflush(delta_cache) == EOF) {
1958 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fflush");
1959 2d9e6abf 2022-05-04 stsp goto done;
1961 67fd6849 2022-02-13 stsp err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1962 b8af7c06 2022-03-15 stsp deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1963 b8af7c06 2022-03-15 stsp nours, repo, progress_cb, progress_arg, &rl,
1964 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1966 e6bcace5 2021-06-22 stsp goto done;
1968 67fd6849 2022-02-13 stsp free_nmeta(deltify.meta, deltify.nmeta);
1969 67fd6849 2022-02-13 stsp free_nmeta(reuse.meta, reuse.nmeta);
1970 67fd6849 2022-02-13 stsp got_object_idset_free(idset);
1971 67fd6849 2022-02-13 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1972 67fd6849 2022-02-13 stsp err = got_error_from_errno("close");
1973 74881701 2021-10-15 stsp if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1974 74881701 2021-10-15 stsp err = got_error_from_errno("fclose");
1975 e6bcace5 2021-06-22 stsp return err;