Blame


1 e6bcace5 2021-06-22 stsp /*
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>
4 e6bcace5 2021-06-22 stsp *
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.
8 e6bcace5 2021-06-22 stsp *
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.
16 e6bcace5 2021-06-22 stsp */
17 e6bcace5 2021-06-22 stsp
18 08736cf9 2021-06-23 stsp #include <sys/types.h>
19 8b925c6c 2022-07-16 thomas #include <sys/queue.h>
20 08736cf9 2021-06-23 stsp #include <sys/uio.h>
21 e6bcace5 2021-06-22 stsp #include <sys/stat.h>
22 31ba2236 2022-01-05 thomas #include <sys/time.h>
23 d93dda9a 2022-05-12 thomas #include <sys/mman.h>
24 e6bcace5 2021-06-22 stsp
25 d93dda9a 2022-05-12 thomas #include <errno.h>
26 08736cf9 2021-06-23 stsp #include <stdint.h>
27 e6bcace5 2021-06-22 stsp #include <stdio.h>
28 e6bcace5 2021-06-22 stsp #include <stdlib.h>
29 e6bcace5 2021-06-22 stsp #include <string.h>
30 31ba2236 2022-01-05 thomas #include <time.h>
31 db847d2c 2022-03-22 thomas #include <unistd.h>
32 e6bcace5 2021-06-22 stsp #include <limits.h>
33 e6bcace5 2021-06-22 stsp #include <zlib.h>
34 b347007e 2021-10-15 thomas
35 b347007e 2021-10-15 thomas #if defined(__FreeBSD__)
36 b347007e 2021-10-15 thomas #include <unistd.h>
37 b347007e 2021-10-15 thomas #endif
38 e6bcace5 2021-06-22 stsp
39 e6bcace5 2021-06-22 stsp #include "got_error.h"
40 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
41 e6bcace5 2021-06-22 stsp #include "got_object.h"
42 0be8fa4c 2021-10-15 thomas #include "got_path.h"
43 05118f5a 2021-06-22 stsp #include "got_reference.h"
44 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
45 e4d8ab47 2021-10-15 thomas #include "got_opentemp.h"
46 e6bcace5 2021-06-22 stsp
47 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
50 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
52 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
53 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
54 036966ba 2022-05-31 thomas #include "got_lib_pack_create.h"
55 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
56 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
57 31ba2236 2022-01-05 thomas #include "got_lib_ratelimit.h"
58 9249e7e3 2022-05-12 thomas #include "got_lib_inflate.h"
59 e6bcace5 2021-06-22 stsp
60 d23a6c95 2022-05-31 thomas #include "murmurhash2.h"
61 d23a6c95 2022-05-31 thomas
62 b79280dd 2021-10-15 thomas #ifndef MIN
63 b79280dd 2021-10-15 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 b79280dd 2021-10-15 thomas #endif
65 b79280dd 2021-10-15 thomas
66 e6bcace5 2021-06-22 stsp #ifndef MAX
67 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 e6bcace5 2021-06-22 stsp #endif
69 e6bcace5 2021-06-22 stsp
70 5fe7b5c1 2022-04-16 thomas #ifndef nitems
71 5fe7b5c1 2022-04-16 thomas #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 5fe7b5c1 2022-04-16 thomas #endif
73 5fe7b5c1 2022-04-16 thomas
74 e6bcace5 2021-06-22 stsp struct got_pack_meta {
75 e6bcace5 2021-06-22 stsp struct got_object_id id;
76 d23a6c95 2022-05-31 thomas uint32_t path_hash;
77 e6bcace5 2021-06-22 stsp int obj_type;
78 ab6186ae 2021-10-15 thomas off_t size;
79 e6bcace5 2021-06-22 stsp time_t mtime;
80 e6bcace5 2021-06-22 stsp
81 e6bcace5 2021-06-22 stsp /* The best delta we picked */
82 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
83 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
84 9249e7e3 2022-05-12 thomas unsigned char *delta_buf; /* if encoded in memory (compressed) */
85 9249e7e3 2022-05-12 thomas off_t delta_offset; /* offset in delta cache file (compressed) */
86 3b6ceab7 2022-01-10 thomas off_t delta_len; /* encoded delta length */
87 9249e7e3 2022-05-12 thomas off_t delta_compressed_len; /* encoded+compressed delta length */
88 e6bcace5 2021-06-22 stsp int nchain;
89 e6bcace5 2021-06-22 stsp
90 f9c2e8e5 2022-02-13 thomas off_t reused_delta_offset; /* offset of delta in reused pack file */
91 f9c2e8e5 2022-02-13 thomas struct got_object_id *base_obj_id;
92 f9c2e8e5 2022-02-13 thomas
93 e6bcace5 2021-06-22 stsp /* Only used for delta window */
94 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
95 e6bcace5 2021-06-22 stsp
96 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
97 e6bcace5 2021-06-22 stsp off_t off;
98 e6bcace5 2021-06-22 stsp };
99 e6bcace5 2021-06-22 stsp
100 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
101 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
102 e6bcace5 2021-06-22 stsp int nmeta;
103 e6bcace5 2021-06-22 stsp int metasz;
104 e6bcace5 2021-06-22 stsp };
105 e6bcace5 2021-06-22 stsp
106 e6bcace5 2021-06-22 stsp static const struct got_error *
107 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
108 cc524d36 2022-05-31 thomas const char *path, int obj_type, time_t mtime, uint32_t seed)
109 e6bcace5 2021-06-22 stsp {
110 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
111 e6bcace5 2021-06-22 stsp
112 e6bcace5 2021-06-22 stsp *new = NULL;
113 e6bcace5 2021-06-22 stsp
114 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
115 e6bcace5 2021-06-22 stsp if (m == NULL)
116 b7e0a384 2021-10-15 thomas return got_error_from_errno("calloc");
117 e6bcace5 2021-06-22 stsp
118 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
119 e6bcace5 2021-06-22 stsp
120 cc524d36 2022-05-31 thomas m->path_hash = murmurhash2(path, strlen(path), seed);
121 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
122 e6bcace5 2021-06-22 stsp m->mtime = mtime;
123 e6bcace5 2021-06-22 stsp *new = m;
124 e6bcace5 2021-06-22 stsp return NULL;
125 e6bcace5 2021-06-22 stsp }
126 e6bcace5 2021-06-22 stsp
127 e6bcace5 2021-06-22 stsp static void
128 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
129 e6bcace5 2021-06-22 stsp {
130 e6bcace5 2021-06-22 stsp if (meta == NULL)
131 e6bcace5 2021-06-22 stsp return;
132 d23a6c95 2022-05-31 thomas meta->path_hash = 0;
133 3b6ceab7 2022-01-10 thomas free(meta->delta_buf);
134 3b6ceab7 2022-01-10 thomas meta->delta_buf = NULL;
135 f9c2e8e5 2022-02-13 thomas free(meta->base_obj_id);
136 f9c2e8e5 2022-02-13 thomas meta->base_obj_id = NULL;
137 6c0788af 2022-05-31 thomas meta->reused_delta_offset = 0;
138 e6bcace5 2021-06-22 stsp }
139 e6bcace5 2021-06-22 stsp
140 e6bcace5 2021-06-22 stsp static void
141 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
142 e6bcace5 2021-06-22 stsp {
143 e6bcace5 2021-06-22 stsp int i;
144 e6bcace5 2021-06-22 stsp
145 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
146 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
147 e6bcace5 2021-06-22 stsp free(meta);
148 e6bcace5 2021-06-22 stsp }
149 e6bcace5 2021-06-22 stsp
150 e6bcace5 2021-06-22 stsp static int
151 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
152 e6bcace5 2021-06-22 stsp {
153 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
154 e6bcace5 2021-06-22 stsp
155 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
156 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
157 e6bcace5 2021-06-22 stsp
158 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
159 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
160 d23a6c95 2022-05-31 thomas if (a->path_hash < b->path_hash)
161 d23a6c95 2022-05-31 thomas return -1;
162 d23a6c95 2022-05-31 thomas if (a->path_hash > b->path_hash)
163 d23a6c95 2022-05-31 thomas return 1;
164 f6b43367 2022-05-01 thomas if (a->mtime < b->mtime)
165 f6b43367 2022-05-01 thomas return -1;
166 f6b43367 2022-05-01 thomas if (a->mtime > b->mtime)
167 f6b43367 2022-05-01 thomas return 1;
168 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
169 e6bcace5 2021-06-22 stsp }
170 e6bcace5 2021-06-22 stsp
171 3b6ceab7 2022-01-10 thomas static off_t
172 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
173 e6bcace5 2021-06-22 stsp {
174 3b6ceab7 2022-01-10 thomas int i;
175 3b6ceab7 2022-01-10 thomas off_t size = 32;
176 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
177 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
178 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
179 e6bcace5 2021-06-22 stsp else
180 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
181 e6bcace5 2021-06-22 stsp }
182 e6bcace5 2021-06-22 stsp return size;
183 3b6ceab7 2022-01-10 thomas }
184 3b6ceab7 2022-01-10 thomas
185 3b6ceab7 2022-01-10 thomas static const struct got_error *
186 3b6ceab7 2022-01-10 thomas append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
187 3b6ceab7 2022-01-10 thomas {
188 3b6ceab7 2022-01-10 thomas char *n;
189 3b6ceab7 2022-01-10 thomas
190 3b6ceab7 2022-01-10 thomas if (*len + nseg >= *sz) {
191 3b6ceab7 2022-01-10 thomas while (*len + nseg >= *sz)
192 3b6ceab7 2022-01-10 thomas *sz += *sz / 2;
193 3b6ceab7 2022-01-10 thomas n = realloc(*p, *sz);
194 3b6ceab7 2022-01-10 thomas if (n == NULL)
195 3b6ceab7 2022-01-10 thomas return got_error_from_errno("realloc");
196 3b6ceab7 2022-01-10 thomas *p = n;
197 3b6ceab7 2022-01-10 thomas }
198 3b6ceab7 2022-01-10 thomas memcpy(*p + *len, seg, nseg);
199 3b6ceab7 2022-01-10 thomas *len += nseg;
200 3b6ceab7 2022-01-10 thomas return NULL;
201 3b6ceab7 2022-01-10 thomas }
202 3b6ceab7 2022-01-10 thomas
203 3b6ceab7 2022-01-10 thomas static const struct got_error *
204 3b6ceab7 2022-01-10 thomas encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
205 3b6ceab7 2022-01-10 thomas struct got_delta_instruction *deltas, int ndeltas,
206 3b6ceab7 2022-01-10 thomas off_t delta_size, off_t base_size)
207 3b6ceab7 2022-01-10 thomas {
208 3b6ceab7 2022-01-10 thomas const struct got_error *err;
209 3b6ceab7 2022-01-10 thomas unsigned char buf[16], *bp;
210 3b6ceab7 2022-01-10 thomas int i, j;
211 9249e7e3 2022-05-12 thomas size_t len = 0, compressed_len;
212 9249e7e3 2022-05-12 thomas off_t bufsize = delta_size;
213 3b6ceab7 2022-01-10 thomas off_t n;
214 3b6ceab7 2022-01-10 thomas struct got_delta_instruction *d;
215 9249e7e3 2022-05-12 thomas uint8_t *delta_buf;
216 3b6ceab7 2022-01-10 thomas
217 9249e7e3 2022-05-12 thomas delta_buf = malloc(bufsize);
218 9249e7e3 2022-05-12 thomas if (delta_buf == NULL)
219 9249e7e3 2022-05-12 thomas return got_error_from_errno("malloc");
220 3b6ceab7 2022-01-10 thomas
221 3b6ceab7 2022-01-10 thomas /* base object size */
222 3b6ceab7 2022-01-10 thomas buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
223 3b6ceab7 2022-01-10 thomas n = base_size >> GOT_DELTA_SIZE_SHIFT;
224 3b6ceab7 2022-01-10 thomas for (i = 1; n > 0; i++) {
225 3b6ceab7 2022-01-10 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
226 3b6ceab7 2022-01-10 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
227 3b6ceab7 2022-01-10 thomas n >>= GOT_DELTA_SIZE_SHIFT;
228 3b6ceab7 2022-01-10 thomas }
229 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize, buf, i);
230 3b6ceab7 2022-01-10 thomas if (err)
231 9249e7e3 2022-05-12 thomas goto done;
232 3b6ceab7 2022-01-10 thomas
233 3b6ceab7 2022-01-10 thomas /* target object size */
234 3b6ceab7 2022-01-10 thomas buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
235 3b6ceab7 2022-01-10 thomas n = o->size >> GOT_DELTA_SIZE_SHIFT;
236 3b6ceab7 2022-01-10 thomas for (i = 1; n > 0; i++) {
237 3b6ceab7 2022-01-10 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
238 3b6ceab7 2022-01-10 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
239 3b6ceab7 2022-01-10 thomas n >>= GOT_DELTA_SIZE_SHIFT;
240 3b6ceab7 2022-01-10 thomas }
241 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize, buf, i);
242 3b6ceab7 2022-01-10 thomas if (err)
243 9249e7e3 2022-05-12 thomas goto done;
244 3b6ceab7 2022-01-10 thomas
245 3b6ceab7 2022-01-10 thomas for (j = 0; j < ndeltas; j++) {
246 3b6ceab7 2022-01-10 thomas d = &deltas[j];
247 3b6ceab7 2022-01-10 thomas if (d->copy) {
248 3b6ceab7 2022-01-10 thomas n = d->offset;
249 3b6ceab7 2022-01-10 thomas bp = &buf[1];
250 3b6ceab7 2022-01-10 thomas buf[0] = GOT_DELTA_BASE_COPY;
251 3b6ceab7 2022-01-10 thomas for (i = 0; i < 4; i++) {
252 3b6ceab7 2022-01-10 thomas /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
253 3b6ceab7 2022-01-10 thomas buf[0] |= 1 << i;
254 3b6ceab7 2022-01-10 thomas *bp++ = n & 0xff;
255 3b6ceab7 2022-01-10 thomas n >>= 8;
256 3b6ceab7 2022-01-10 thomas if (n == 0)
257 3b6ceab7 2022-01-10 thomas break;
258 3b6ceab7 2022-01-10 thomas }
259 3b6ceab7 2022-01-10 thomas
260 3b6ceab7 2022-01-10 thomas n = d->len;
261 3b6ceab7 2022-01-10 thomas if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
262 3b6ceab7 2022-01-10 thomas /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
263 3b6ceab7 2022-01-10 thomas for (i = 0; i < 3 && n > 0; i++) {
264 3b6ceab7 2022-01-10 thomas buf[0] |= 1 << (i + 4);
265 3b6ceab7 2022-01-10 thomas *bp++ = n & 0xff;
266 3b6ceab7 2022-01-10 thomas n >>= 8;
267 3b6ceab7 2022-01-10 thomas }
268 3b6ceab7 2022-01-10 thomas }
269 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize,
270 3b6ceab7 2022-01-10 thomas buf, bp - buf);
271 3b6ceab7 2022-01-10 thomas if (err)
272 9249e7e3 2022-05-12 thomas goto done;
273 3b6ceab7 2022-01-10 thomas } else if (o->f == NULL) {
274 3b6ceab7 2022-01-10 thomas n = 0;
275 3b6ceab7 2022-01-10 thomas while (n != d->len) {
276 3b6ceab7 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
277 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize,
278 3b6ceab7 2022-01-10 thomas buf, 1);
279 3b6ceab7 2022-01-10 thomas if (err)
280 9249e7e3 2022-05-12 thomas goto done;
281 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize,
282 3b6ceab7 2022-01-10 thomas o->data + o->hdrlen + d->offset + n,
283 3b6ceab7 2022-01-10 thomas buf[0]);
284 3b6ceab7 2022-01-10 thomas if (err)
285 9249e7e3 2022-05-12 thomas goto done;
286 3b6ceab7 2022-01-10 thomas n += buf[0];
287 3b6ceab7 2022-01-10 thomas }
288 3b6ceab7 2022-01-10 thomas } else {
289 3b6ceab7 2022-01-10 thomas char content[128];
290 3b6ceab7 2022-01-10 thomas size_t r;
291 9249e7e3 2022-05-12 thomas if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
292 9249e7e3 2022-05-12 thomas err = got_error_from_errno("fseeko");
293 9249e7e3 2022-05-12 thomas goto done;
294 9249e7e3 2022-05-12 thomas }
295 3b6ceab7 2022-01-10 thomas n = 0;
296 3b6ceab7 2022-01-10 thomas while (n != d->len) {
297 3b6ceab7 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
298 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize,
299 3b6ceab7 2022-01-10 thomas buf, 1);
300 3b6ceab7 2022-01-10 thomas if (err)
301 9249e7e3 2022-05-12 thomas goto done;
302 3b6ceab7 2022-01-10 thomas r = fread(content, 1, buf[0], o->f);
303 9249e7e3 2022-05-12 thomas if (r != buf[0]) {
304 9249e7e3 2022-05-12 thomas err = got_ferror(o->f, GOT_ERR_IO);
305 9249e7e3 2022-05-12 thomas goto done;
306 9249e7e3 2022-05-12 thomas }
307 9249e7e3 2022-05-12 thomas err = append(&delta_buf, &len, &bufsize,
308 3b6ceab7 2022-01-10 thomas content, buf[0]);
309 3b6ceab7 2022-01-10 thomas if (err)
310 9249e7e3 2022-05-12 thomas goto done;
311 3b6ceab7 2022-01-10 thomas n += buf[0];
312 3b6ceab7 2022-01-10 thomas }
313 3b6ceab7 2022-01-10 thomas }
314 3b6ceab7 2022-01-10 thomas }
315 3b6ceab7 2022-01-10 thomas
316 9249e7e3 2022-05-12 thomas err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
317 9249e7e3 2022-05-12 thomas NULL, NULL, delta_buf, 0, len);
318 9249e7e3 2022-05-12 thomas if (err)
319 9249e7e3 2022-05-12 thomas goto done;
320 9249e7e3 2022-05-12 thomas
321 3b6ceab7 2022-01-10 thomas m->delta_len = len;
322 9249e7e3 2022-05-12 thomas m->delta_compressed_len = compressed_len;
323 9249e7e3 2022-05-12 thomas done:
324 9249e7e3 2022-05-12 thomas free(delta_buf);
325 9249e7e3 2022-05-12 thomas return err;
326 e6bcace5 2021-06-22 stsp }
327 e6bcace5 2021-06-22 stsp
328 b79280dd 2021-10-15 thomas static const struct got_error *
329 b79280dd 2021-10-15 thomas encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
330 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas, int ndeltas,
331 d0772de9 2021-10-15 thomas off_t base_size, FILE *f)
332 d0772de9 2021-10-15 thomas {
333 9249e7e3 2022-05-12 thomas const struct got_error *err;
334 d0772de9 2021-10-15 thomas unsigned char buf[16], *bp;
335 d0772de9 2021-10-15 thomas int i, j;
336 d0772de9 2021-10-15 thomas off_t n;
337 9249e7e3 2022-05-12 thomas struct got_deflate_buf zb;
338 d0772de9 2021-10-15 thomas struct got_delta_instruction *d;
339 9249e7e3 2022-05-12 thomas off_t delta_len = 0, compressed_len = 0;
340 d0772de9 2021-10-15 thomas
341 9249e7e3 2022-05-12 thomas err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
342 9249e7e3 2022-05-12 thomas if (err)
343 9249e7e3 2022-05-12 thomas return err;
344 9249e7e3 2022-05-12 thomas
345 d0772de9 2021-10-15 thomas /* base object size */
346 d0772de9 2021-10-15 thomas buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
347 d0772de9 2021-10-15 thomas n = base_size >> GOT_DELTA_SIZE_SHIFT;
348 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
349 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
350 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
351 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
352 d0772de9 2021-10-15 thomas }
353 d0772de9 2021-10-15 thomas
354 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
355 9249e7e3 2022-05-12 thomas buf, 0, i, f, NULL);
356 9249e7e3 2022-05-12 thomas if (err)
357 9249e7e3 2022-05-12 thomas goto done;
358 9249e7e3 2022-05-12 thomas delta_len += i;
359 9249e7e3 2022-05-12 thomas
360 d0772de9 2021-10-15 thomas /* target object size */
361 d0772de9 2021-10-15 thomas buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
362 d0772de9 2021-10-15 thomas n = o->size >> GOT_DELTA_SIZE_SHIFT;
363 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
364 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
365 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
366 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
367 d0772de9 2021-10-15 thomas }
368 d0772de9 2021-10-15 thomas
369 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
370 9249e7e3 2022-05-12 thomas buf, 0, i, f, NULL);
371 9249e7e3 2022-05-12 thomas if (err)
372 9249e7e3 2022-05-12 thomas goto done;
373 9249e7e3 2022-05-12 thomas delta_len += i;
374 9249e7e3 2022-05-12 thomas
375 d0772de9 2021-10-15 thomas for (j = 0; j < ndeltas; j++) {
376 d0772de9 2021-10-15 thomas d = &deltas[j];
377 d0772de9 2021-10-15 thomas if (d->copy) {
378 d0772de9 2021-10-15 thomas n = d->offset;
379 d0772de9 2021-10-15 thomas bp = &buf[1];
380 d0772de9 2021-10-15 thomas buf[0] = GOT_DELTA_BASE_COPY;
381 d0772de9 2021-10-15 thomas for (i = 0; i < 4; i++) {
382 d0772de9 2021-10-15 thomas /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
383 d0772de9 2021-10-15 thomas buf[0] |= 1 << i;
384 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
385 d0772de9 2021-10-15 thomas n >>= 8;
386 d0772de9 2021-10-15 thomas if (n == 0)
387 d0772de9 2021-10-15 thomas break;
388 d0772de9 2021-10-15 thomas }
389 d0772de9 2021-10-15 thomas n = d->len;
390 d0772de9 2021-10-15 thomas if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
391 d0772de9 2021-10-15 thomas /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
392 d0772de9 2021-10-15 thomas for (i = 0; i < 3 && n > 0; i++) {
393 d0772de9 2021-10-15 thomas buf[0] |= 1 << (i + 4);
394 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
395 d0772de9 2021-10-15 thomas n >>= 8;
396 d0772de9 2021-10-15 thomas }
397 d0772de9 2021-10-15 thomas }
398 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb,
399 9249e7e3 2022-05-12 thomas &compressed_len, buf, 0, bp - buf, f, NULL);
400 9249e7e3 2022-05-12 thomas if (err)
401 9249e7e3 2022-05-12 thomas goto done;
402 9249e7e3 2022-05-12 thomas delta_len += (bp - buf);
403 2b0ae357 2022-01-10 thomas } else if (o->f == NULL) {
404 2b0ae357 2022-01-10 thomas n = 0;
405 2b0ae357 2022-01-10 thomas while (n != d->len) {
406 2b0ae357 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
407 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb,
408 9249e7e3 2022-05-12 thomas &compressed_len, buf, 0, 1, f, NULL);
409 9249e7e3 2022-05-12 thomas if (err)
410 9249e7e3 2022-05-12 thomas goto done;
411 9249e7e3 2022-05-12 thomas delta_len++;
412 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb,
413 9249e7e3 2022-05-12 thomas &compressed_len,
414 9249e7e3 2022-05-12 thomas o->data + o->hdrlen + d->offset + n, 0,
415 9249e7e3 2022-05-12 thomas buf[0], f, NULL);
416 9249e7e3 2022-05-12 thomas if (err)
417 9249e7e3 2022-05-12 thomas goto done;
418 9249e7e3 2022-05-12 thomas delta_len += buf[0];
419 2b0ae357 2022-01-10 thomas n += buf[0];
420 2b0ae357 2022-01-10 thomas }
421 d0772de9 2021-10-15 thomas } else {
422 d0772de9 2021-10-15 thomas char content[128];
423 d0772de9 2021-10-15 thomas size_t r;
424 9249e7e3 2022-05-12 thomas if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
425 9249e7e3 2022-05-12 thomas err = got_error_from_errno("fseeko");
426 9249e7e3 2022-05-12 thomas goto done;
427 9249e7e3 2022-05-12 thomas }
428 d0772de9 2021-10-15 thomas n = 0;
429 d0772de9 2021-10-15 thomas while (n != d->len) {
430 d0772de9 2021-10-15 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
431 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb,
432 9249e7e3 2022-05-12 thomas &compressed_len, buf, 0, 1, f, NULL);
433 9249e7e3 2022-05-12 thomas if (err)
434 9249e7e3 2022-05-12 thomas goto done;
435 9249e7e3 2022-05-12 thomas delta_len++;
436 d0772de9 2021-10-15 thomas r = fread(content, 1, buf[0], o->f);
437 9249e7e3 2022-05-12 thomas if (r != buf[0]) {
438 9249e7e3 2022-05-12 thomas err = got_ferror(o->f, GOT_ERR_IO);
439 9249e7e3 2022-05-12 thomas goto done;
440 9249e7e3 2022-05-12 thomas }
441 9249e7e3 2022-05-12 thomas err = got_deflate_append_to_file_mmap(&zb,
442 9249e7e3 2022-05-12 thomas &compressed_len, content, 0, buf[0], f,
443 9249e7e3 2022-05-12 thomas NULL);
444 9249e7e3 2022-05-12 thomas if (err)
445 9249e7e3 2022-05-12 thomas goto done;
446 9249e7e3 2022-05-12 thomas delta_len += buf[0];
447 d0772de9 2021-10-15 thomas n += buf[0];
448 d0772de9 2021-10-15 thomas }
449 d0772de9 2021-10-15 thomas }
450 d0772de9 2021-10-15 thomas }
451 e6bcace5 2021-06-22 stsp
452 9249e7e3 2022-05-12 thomas err = got_deflate_flush(&zb, f, NULL, &compressed_len);
453 9249e7e3 2022-05-12 thomas if (err)
454 9249e7e3 2022-05-12 thomas goto done;
455 9249e7e3 2022-05-12 thomas
456 9249e7e3 2022-05-12 thomas /* sanity check */
457 9249e7e3 2022-05-12 thomas if (compressed_len != ftello(f) - m->delta_offset) {
458 9249e7e3 2022-05-12 thomas err = got_error(GOT_ERR_COMPRESSION);
459 9249e7e3 2022-05-12 thomas goto done;
460 9249e7e3 2022-05-12 thomas }
461 9249e7e3 2022-05-12 thomas
462 9249e7e3 2022-05-12 thomas m->delta_len = delta_len;
463 9249e7e3 2022-05-12 thomas m->delta_compressed_len = compressed_len;
464 9249e7e3 2022-05-12 thomas done:
465 9249e7e3 2022-05-12 thomas got_deflate_end(&zb);
466 9249e7e3 2022-05-12 thomas return err;
467 d0772de9 2021-10-15 thomas }
468 31ba2236 2022-01-05 thomas
469 31ba2236 2022-01-05 thomas static const struct got_error *
470 31ba2236 2022-01-05 thomas report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
471 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
472 85220b0e 2022-03-22 thomas off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
473 85220b0e 2022-03-22 thomas int nobj_written)
474 31ba2236 2022-01-05 thomas {
475 31ba2236 2022-01-05 thomas const struct got_error *err;
476 31ba2236 2022-01-05 thomas int elapsed;
477 31ba2236 2022-01-05 thomas
478 31ba2236 2022-01-05 thomas if (progress_cb == NULL)
479 31ba2236 2022-01-05 thomas return NULL;
480 d0772de9 2021-10-15 thomas
481 31ba2236 2022-01-05 thomas err = got_ratelimit_check(&elapsed, rl);
482 31ba2236 2022-01-05 thomas if (err || !elapsed)
483 31ba2236 2022-01-05 thomas return err;
484 d0772de9 2021-10-15 thomas
485 85220b0e 2022-03-22 thomas return progress_cb(progress_arg, ncolored, nfound, ntrees,
486 85220b0e 2022-03-22 thomas packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
487 31ba2236 2022-01-05 thomas }
488 31ba2236 2022-01-05 thomas
489 e6bcace5 2021-06-22 stsp static const struct got_error *
490 f9c2e8e5 2022-02-13 thomas add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
491 f9c2e8e5 2022-02-13 thomas {
492 f9c2e8e5 2022-02-13 thomas if (v->nmeta == v->metasz){
493 f9c2e8e5 2022-02-13 thomas size_t newsize = 2 * v->metasz;
494 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **new;
495 f9c2e8e5 2022-02-13 thomas new = reallocarray(v->meta, newsize, sizeof(*new));
496 f9c2e8e5 2022-02-13 thomas if (new == NULL)
497 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("reallocarray");
498 f9c2e8e5 2022-02-13 thomas v->meta = new;
499 f9c2e8e5 2022-02-13 thomas v->metasz = newsize;
500 f9c2e8e5 2022-02-13 thomas }
501 f9c2e8e5 2022-02-13 thomas
502 f9c2e8e5 2022-02-13 thomas v->meta[v->nmeta++] = m;
503 f9c2e8e5 2022-02-13 thomas return NULL;
504 f9c2e8e5 2022-02-13 thomas }
505 f9c2e8e5 2022-02-13 thomas
506 f9c2e8e5 2022-02-13 thomas static const struct got_error *
507 f9c2e8e5 2022-02-13 thomas find_pack_for_reuse(struct got_packidx **best_packidx,
508 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
509 f9c2e8e5 2022-02-13 thomas {
510 de47d040 2022-03-22 thomas const struct got_error *err = NULL;
511 f9c2e8e5 2022-02-13 thomas struct got_pathlist_entry *pe;
512 f9c2e8e5 2022-02-13 thomas const char *best_packidx_path = NULL;
513 f9c2e8e5 2022-02-13 thomas int nobj_max = 0;
514 f9c2e8e5 2022-02-13 thomas
515 f9c2e8e5 2022-02-13 thomas *best_packidx = NULL;
516 f9c2e8e5 2022-02-13 thomas
517 de47d040 2022-03-22 thomas TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
518 f9c2e8e5 2022-02-13 thomas const char *path_packidx = pe->path;
519 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx;
520 f9c2e8e5 2022-02-13 thomas int nobj;
521 f9c2e8e5 2022-02-13 thomas
522 f9c2e8e5 2022-02-13 thomas err = got_repo_get_packidx(&packidx, path_packidx, repo);
523 f9c2e8e5 2022-02-13 thomas if (err)
524 f9c2e8e5 2022-02-13 thomas break;
525 f9c2e8e5 2022-02-13 thomas
526 f9c2e8e5 2022-02-13 thomas nobj = be32toh(packidx->hdr.fanout_table[0xff]);
527 f9c2e8e5 2022-02-13 thomas if (nobj > nobj_max) {
528 f9c2e8e5 2022-02-13 thomas best_packidx_path = path_packidx;
529 f9c2e8e5 2022-02-13 thomas nobj_max = nobj;
530 f9c2e8e5 2022-02-13 thomas }
531 f9c2e8e5 2022-02-13 thomas }
532 f9c2e8e5 2022-02-13 thomas
533 f9c2e8e5 2022-02-13 thomas if (best_packidx_path) {
534 f9c2e8e5 2022-02-13 thomas err = got_repo_get_packidx(best_packidx, best_packidx_path,
535 f9c2e8e5 2022-02-13 thomas repo);
536 f9c2e8e5 2022-02-13 thomas }
537 f9c2e8e5 2022-02-13 thomas
538 f9c2e8e5 2022-02-13 thomas return err;
539 f9c2e8e5 2022-02-13 thomas }
540 f9c2e8e5 2022-02-13 thomas
541 7d0d4920 2022-05-12 thomas struct send_id_arg {
542 7d0d4920 2022-05-12 thomas struct imsgbuf *ibuf;
543 7d0d4920 2022-05-12 thomas struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
544 7d0d4920 2022-05-12 thomas size_t nids;
545 f9c2e8e5 2022-02-13 thomas };
546 f9c2e8e5 2022-02-13 thomas
547 f9c2e8e5 2022-02-13 thomas static const struct got_error *
548 7d0d4920 2022-05-12 thomas send_id(struct got_object_id *id, void *data, void *arg)
549 f9c2e8e5 2022-02-13 thomas {
550 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
551 7d0d4920 2022-05-12 thomas struct send_id_arg *a = arg;
552 f9c2e8e5 2022-02-13 thomas
553 7d0d4920 2022-05-12 thomas a->ids[a->nids++] = id;
554 7d0d4920 2022-05-12 thomas
555 7d0d4920 2022-05-12 thomas if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
556 7d0d4920 2022-05-12 thomas err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
557 f9c2e8e5 2022-02-13 thomas if (err)
558 f9c2e8e5 2022-02-13 thomas return err;
559 7d0d4920 2022-05-12 thomas a->nids = 0;
560 f9c2e8e5 2022-02-13 thomas }
561 f9c2e8e5 2022-02-13 thomas
562 7d0d4920 2022-05-12 thomas return NULL;
563 ec2b23c5 2022-07-01 thomas }
564 ec2b23c5 2022-07-01 thomas
565 ec2b23c5 2022-07-01 thomas static const struct got_error *
566 ec2b23c5 2022-07-01 thomas send_idset(struct imsgbuf *ibuf, struct got_object_idset *idset)
567 ec2b23c5 2022-07-01 thomas {
568 ec2b23c5 2022-07-01 thomas const struct got_error *err;
569 ec2b23c5 2022-07-01 thomas struct send_id_arg sia;
570 ec2b23c5 2022-07-01 thomas
571 ec2b23c5 2022-07-01 thomas memset(&sia, 0, sizeof(sia));
572 ec2b23c5 2022-07-01 thomas sia.ibuf = ibuf;
573 ec2b23c5 2022-07-01 thomas err = got_object_idset_for_each(idset, send_id, &sia);
574 ec2b23c5 2022-07-01 thomas if (err)
575 ec2b23c5 2022-07-01 thomas return err;
576 ec2b23c5 2022-07-01 thomas
577 ec2b23c5 2022-07-01 thomas if (sia.nids > 0) {
578 ec2b23c5 2022-07-01 thomas err = got_privsep_send_object_idlist(ibuf, sia.ids, sia.nids);
579 ec2b23c5 2022-07-01 thomas if (err)
580 ec2b23c5 2022-07-01 thomas return err;
581 ec2b23c5 2022-07-01 thomas }
582 ec2b23c5 2022-07-01 thomas
583 ec2b23c5 2022-07-01 thomas return got_privsep_send_object_idlist_done(ibuf);
584 7d0d4920 2022-05-12 thomas }
585 f9c2e8e5 2022-02-13 thomas
586 ec2b23c5 2022-07-01 thomas
587 7d0d4920 2022-05-12 thomas static const struct got_error *
588 7d0d4920 2022-05-12 thomas recv_reused_delta(struct got_imsg_reused_delta *delta,
589 7d0d4920 2022-05-12 thomas struct got_object_idset *idset, struct got_pack_metavec *v)
590 7d0d4920 2022-05-12 thomas {
591 7d0d4920 2022-05-12 thomas struct got_pack_meta *m, *base;
592 f9c2e8e5 2022-02-13 thomas
593 7d0d4920 2022-05-12 thomas if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
594 7d0d4920 2022-05-12 thomas delta->delta_offset +
595 7d0d4920 2022-05-12 thomas delta->delta_compressed_size < delta->delta_offset)
596 7d0d4920 2022-05-12 thomas return got_error(GOT_ERR_BAD_PACKFILE);
597 f9c2e8e5 2022-02-13 thomas
598 7d0d4920 2022-05-12 thomas m = got_object_idset_get(idset, &delta->id);
599 7d0d4920 2022-05-12 thomas if (m == NULL)
600 7d0d4920 2022-05-12 thomas return got_error(GOT_ERR_NO_OBJ);
601 7d0d4920 2022-05-12 thomas
602 7d0d4920 2022-05-12 thomas base = got_object_idset_get(idset, &delta->base_id);
603 7d0d4920 2022-05-12 thomas if (base == NULL)
604 7d0d4920 2022-05-12 thomas return got_error(GOT_ERR_NO_OBJ);
605 7d0d4920 2022-05-12 thomas
606 7d0d4920 2022-05-12 thomas m->delta_len = delta->delta_size;
607 7d0d4920 2022-05-12 thomas m->delta_compressed_len = delta->delta_compressed_size;
608 7d0d4920 2022-05-12 thomas m->delta_offset = delta->delta_out_offset;
609 7d0d4920 2022-05-12 thomas m->prev = base;
610 7d0d4920 2022-05-12 thomas m->size = delta->result_size;
611 7d0d4920 2022-05-12 thomas m->reused_delta_offset = delta->delta_offset;
612 7d0d4920 2022-05-12 thomas m->base_obj_id = got_object_id_dup(&delta->base_id);
613 7d0d4920 2022-05-12 thomas if (m->base_obj_id == NULL)
614 7d0d4920 2022-05-12 thomas return got_error_from_errno("got_object_id_dup");
615 7d0d4920 2022-05-12 thomas
616 7d0d4920 2022-05-12 thomas return add_meta(m, v);
617 68036464 2022-06-26 thomas }
618 68036464 2022-06-26 thomas
619 68036464 2022-06-26 thomas static const struct got_error *
620 ec2b23c5 2022-07-01 thomas cache_pack_for_packidx(struct got_pack **pack, struct got_packidx *packidx,
621 ec2b23c5 2022-07-01 thomas struct got_repository *repo)
622 68036464 2022-06-26 thomas {
623 ec2b23c5 2022-07-01 thomas const struct got_error *err;
624 68036464 2022-06-26 thomas char *path_packfile = NULL;
625 68036464 2022-06-26 thomas
626 68036464 2022-06-26 thomas err = got_packidx_get_packfile_path(&path_packfile,
627 68036464 2022-06-26 thomas packidx->path_packidx);
628 68036464 2022-06-26 thomas if (err)
629 68036464 2022-06-26 thomas return err;
630 68036464 2022-06-26 thomas
631 68036464 2022-06-26 thomas *pack = got_repo_get_cached_pack(repo, path_packfile);
632 68036464 2022-06-26 thomas if (*pack == NULL) {
633 68036464 2022-06-26 thomas err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
634 68036464 2022-06-26 thomas if (err)
635 68036464 2022-06-26 thomas goto done;
636 68036464 2022-06-26 thomas }
637 68036464 2022-06-26 thomas if ((*pack)->privsep_child == NULL) {
638 68036464 2022-06-26 thomas err = got_pack_start_privsep_child(*pack, packidx);
639 68036464 2022-06-26 thomas if (err)
640 68036464 2022-06-26 thomas goto done;
641 68036464 2022-06-26 thomas }
642 ec2b23c5 2022-07-01 thomas done:
643 ec2b23c5 2022-07-01 thomas free(path_packfile);
644 ec2b23c5 2022-07-01 thomas return err;
645 ec2b23c5 2022-07-01 thomas }
646 68036464 2022-06-26 thomas
647 ec2b23c5 2022-07-01 thomas static const struct got_error *
648 ec2b23c5 2022-07-01 thomas prepare_delta_reuse(struct got_pack *pack, struct got_packidx *packidx,
649 ec2b23c5 2022-07-01 thomas int delta_outfd, struct got_repository *repo)
650 ec2b23c5 2022-07-01 thomas {
651 ec2b23c5 2022-07-01 thomas const struct got_error *err = NULL;
652 ec2b23c5 2022-07-01 thomas
653 ec2b23c5 2022-07-01 thomas if (!pack->child_has_delta_outfd) {
654 68036464 2022-06-26 thomas int outfd_child;
655 68036464 2022-06-26 thomas outfd_child = dup(delta_outfd);
656 68036464 2022-06-26 thomas if (outfd_child == -1) {
657 68036464 2022-06-26 thomas err = got_error_from_errno("dup");
658 68036464 2022-06-26 thomas goto done;
659 68036464 2022-06-26 thomas }
660 68036464 2022-06-26 thomas err = got_privsep_send_raw_delta_outfd(
661 ec2b23c5 2022-07-01 thomas pack->privsep_child->ibuf, outfd_child);
662 68036464 2022-06-26 thomas if (err)
663 68036464 2022-06-26 thomas goto done;
664 ec2b23c5 2022-07-01 thomas pack->child_has_delta_outfd = 1;
665 68036464 2022-06-26 thomas }
666 68036464 2022-06-26 thomas
667 ec2b23c5 2022-07-01 thomas err = got_privsep_send_delta_reuse_req(pack->privsep_child->ibuf);
668 68036464 2022-06-26 thomas done:
669 68036464 2022-06-26 thomas return err;
670 f9c2e8e5 2022-02-13 thomas }
671 f9c2e8e5 2022-02-13 thomas
672 68036464 2022-06-26 thomas
673 f9c2e8e5 2022-02-13 thomas static const struct got_error *
674 f9c2e8e5 2022-02-13 thomas search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
675 85220b0e 2022-03-22 thomas int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
676 85220b0e 2022-03-22 thomas struct got_repository *repo,
677 f9c2e8e5 2022-02-13 thomas got_pack_progress_cb progress_cb, void *progress_arg,
678 f9c2e8e5 2022-02-13 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
679 f9c2e8e5 2022-02-13 thomas {
680 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
681 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx;
682 f9c2e8e5 2022-02-13 thomas struct got_pack *pack;
683 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
684 7d0d4920 2022-05-12 thomas size_t ndeltas, i;
685 f9c2e8e5 2022-02-13 thomas
686 f9c2e8e5 2022-02-13 thomas err = find_pack_for_reuse(&packidx, repo);
687 f9c2e8e5 2022-02-13 thomas if (err)
688 f9c2e8e5 2022-02-13 thomas return err;
689 f9c2e8e5 2022-02-13 thomas
690 f9c2e8e5 2022-02-13 thomas if (packidx == NULL)
691 f9c2e8e5 2022-02-13 thomas return NULL;
692 f9c2e8e5 2022-02-13 thomas
693 ec2b23c5 2022-07-01 thomas err = cache_pack_for_packidx(&pack, packidx, repo);
694 f9c2e8e5 2022-02-13 thomas if (err)
695 f9c2e8e5 2022-02-13 thomas return err;
696 f9c2e8e5 2022-02-13 thomas
697 ec2b23c5 2022-07-01 thomas err = prepare_delta_reuse(pack, packidx, delta_cache_fd, repo);
698 7d0d4920 2022-05-12 thomas if (err)
699 7d0d4920 2022-05-12 thomas return err;
700 ec2b23c5 2022-07-01 thomas
701 ec2b23c5 2022-07-01 thomas err = send_idset(pack->privsep_child->ibuf, idset);
702 7d0d4920 2022-05-12 thomas if (err)
703 7d0d4920 2022-05-12 thomas return err;
704 f9c2e8e5 2022-02-13 thomas
705 7d0d4920 2022-05-12 thomas for (;;) {
706 7d0d4920 2022-05-12 thomas int done = 0;
707 7d0d4920 2022-05-12 thomas
708 7d0d4920 2022-05-12 thomas if (cancel_cb) {
709 7d0d4920 2022-05-12 thomas err = (*cancel_cb)(cancel_arg);
710 7d0d4920 2022-05-12 thomas if (err)
711 7d0d4920 2022-05-12 thomas break;
712 7d0d4920 2022-05-12 thomas }
713 7d0d4920 2022-05-12 thomas
714 7d0d4920 2022-05-12 thomas err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
715 7d0d4920 2022-05-12 thomas pack->privsep_child->ibuf);
716 7d0d4920 2022-05-12 thomas if (err || done)
717 7d0d4920 2022-05-12 thomas break;
718 7d0d4920 2022-05-12 thomas
719 7d0d4920 2022-05-12 thomas for (i = 0; i < ndeltas; i++) {
720 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta *delta = &deltas[i];
721 7d0d4920 2022-05-12 thomas err = recv_reused_delta(delta, idset, v);
722 7d0d4920 2022-05-12 thomas if (err)
723 7d0d4920 2022-05-12 thomas goto done;
724 7d0d4920 2022-05-12 thomas }
725 7d0d4920 2022-05-12 thomas
726 7d0d4920 2022-05-12 thomas err = report_progress(progress_cb, progress_arg, rl,
727 7d0d4920 2022-05-12 thomas ncolored, nfound, ntrees, 0L, ncommits,
728 7d0d4920 2022-05-12 thomas got_object_idset_num_elements(idset), v->nmeta, 0);
729 7d0d4920 2022-05-12 thomas if (err)
730 7d0d4920 2022-05-12 thomas break;
731 7d0d4920 2022-05-12 thomas }
732 f9c2e8e5 2022-02-13 thomas done:
733 f9c2e8e5 2022-02-13 thomas return err;
734 f9c2e8e5 2022-02-13 thomas }
735 f9c2e8e5 2022-02-13 thomas
736 f9c2e8e5 2022-02-13 thomas static const struct got_error *
737 85220b0e 2022-03-22 thomas pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
738 85220b0e 2022-03-22 thomas int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
739 85220b0e 2022-03-22 thomas struct got_repository *repo,
740 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
741 31ba2236 2022-01-05 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
742 e6bcace5 2021-06-22 stsp {
743 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
744 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
745 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
746 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
747 3b6ceab7 2022-01-10 thomas int i, j, ndeltas, best_ndeltas;
748 3b6ceab7 2022-01-10 thomas off_t size, best_size;
749 959daf23 2021-10-29 thomas const int max_base_candidates = 3;
750 510885f7 2022-01-10 thomas size_t delta_memsize = 0;
751 1569c56d 2022-05-31 thomas const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
752 ecf9545f 2021-10-15 thomas int outfd = -1;
753 cc524d36 2022-05-31 thomas uint32_t delta_seed;
754 cc524d36 2022-05-31 thomas
755 cc524d36 2022-05-31 thomas delta_seed = arc4random();
756 e6bcace5 2021-06-22 stsp
757 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
758 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
759 e6bcace5 2021-06-22 stsp if (cancel_cb) {
760 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
761 e6bcace5 2021-06-22 stsp if (err)
762 e6bcace5 2021-06-22 stsp break;
763 e6bcace5 2021-06-22 stsp }
764 31ba2236 2022-01-05 thomas err = report_progress(progress_cb, progress_arg, rl,
765 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
766 85220b0e 2022-03-22 thomas nreused + i, 0);
767 31ba2236 2022-01-05 thomas if (err)
768 31ba2236 2022-01-05 thomas goto done;
769 e6bcace5 2021-06-22 stsp m = meta[i];
770 e6bcace5 2021-06-22 stsp
771 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
772 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
773 e6bcace5 2021-06-22 stsp continue;
774 e6bcace5 2021-06-22 stsp
775 f8bb1d3e 2021-10-17 thomas err = got_object_raw_open(&raw, &outfd, repo, &m->id);
776 e6bcace5 2021-06-22 stsp if (err)
777 e6bcace5 2021-06-22 stsp goto done;
778 ab6186ae 2021-10-15 thomas m->size = raw->size;
779 e6bcace5 2021-06-22 stsp
780 2b0ae357 2022-01-10 thomas if (raw->f == NULL) {
781 2b0ae357 2022-01-10 thomas err = got_deltify_init_mem(&m->dtab, raw->data,
782 cc524d36 2022-05-31 thomas raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
783 2b0ae357 2022-01-10 thomas } else {
784 2b0ae357 2022-01-10 thomas err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
785 cc524d36 2022-05-31 thomas raw->size + raw->hdrlen, delta_seed);
786 2b0ae357 2022-01-10 thomas }
787 e6bcace5 2021-06-22 stsp if (err)
788 e6bcace5 2021-06-22 stsp goto done;
789 e6bcace5 2021-06-22 stsp
790 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
791 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
792 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
793 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
794 e6bcace5 2021-06-22 stsp n->dtab = NULL;
795 e6bcace5 2021-06-22 stsp }
796 e6bcace5 2021-06-22 stsp
797 b79280dd 2021-10-15 thomas best_size = raw->size;
798 b79280dd 2021-10-15 thomas best_ndeltas = 0;
799 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
800 e6bcace5 2021-06-22 stsp if (cancel_cb) {
801 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
802 e6bcace5 2021-06-22 stsp if (err)
803 e6bcace5 2021-06-22 stsp goto done;
804 e6bcace5 2021-06-22 stsp }
805 e6bcace5 2021-06-22 stsp base = meta[j];
806 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
807 62a05ae9 2021-10-29 thomas if (base->nchain >= 128 ||
808 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
809 e6bcace5 2021-06-22 stsp continue;
810 e6bcace5 2021-06-22 stsp
811 8ab9215c 2021-10-15 thomas err = got_object_raw_open(&base_raw, &outfd, repo,
812 f8bb1d3e 2021-10-17 thomas &base->id);
813 e6bcace5 2021-06-22 stsp if (err)
814 e6bcace5 2021-06-22 stsp goto done;
815 f9c2e8e5 2022-02-13 thomas
816 2b0ae357 2022-01-10 thomas if (raw->f == NULL && base_raw->f == NULL) {
817 2b0ae357 2022-01-10 thomas err = got_deltify_mem_mem(&deltas, &ndeltas,
818 2b0ae357 2022-01-10 thomas raw->data, raw->hdrlen,
819 cc524d36 2022-05-31 thomas raw->size + raw->hdrlen, delta_seed,
820 2b0ae357 2022-01-10 thomas base->dtab, base_raw->data,
821 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
822 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
823 2b0ae357 2022-01-10 thomas } else if (raw->f == NULL) {
824 2b0ae357 2022-01-10 thomas err = got_deltify_mem_file(&deltas, &ndeltas,
825 2b0ae357 2022-01-10 thomas raw->data, raw->hdrlen,
826 cc524d36 2022-05-31 thomas raw->size + raw->hdrlen, delta_seed,
827 2b0ae357 2022-01-10 thomas base->dtab, base_raw->f,
828 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
829 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
830 2b0ae357 2022-01-10 thomas } else if (base_raw->f == NULL) {
831 2b0ae357 2022-01-10 thomas err = got_deltify_file_mem(&deltas, &ndeltas,
832 2b0ae357 2022-01-10 thomas raw->f, raw->hdrlen,
833 cc524d36 2022-05-31 thomas raw->size + raw->hdrlen, delta_seed,
834 2b0ae357 2022-01-10 thomas base->dtab, base_raw->data,
835 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
836 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
837 2b0ae357 2022-01-10 thomas } else {
838 2b0ae357 2022-01-10 thomas err = got_deltify(&deltas, &ndeltas,
839 2b0ae357 2022-01-10 thomas raw->f, raw->hdrlen,
840 cc524d36 2022-05-31 thomas raw->size + raw->hdrlen, delta_seed,
841 2b0ae357 2022-01-10 thomas base->dtab, base_raw->f, base_raw->hdrlen,
842 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
843 2b0ae357 2022-01-10 thomas }
844 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
845 e6bcace5 2021-06-22 stsp base_raw = NULL;
846 e6bcace5 2021-06-22 stsp if (err)
847 e6bcace5 2021-06-22 stsp goto done;
848 e6bcace5 2021-06-22 stsp
849 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
850 b79280dd 2021-10-15 thomas if (size + 32 < best_size){
851 e6bcace5 2021-06-22 stsp /*
852 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
853 e6bcace5 2021-06-22 stsp * replace it.
854 e6bcace5 2021-06-22 stsp */
855 b79280dd 2021-10-15 thomas best_size = size;
856 b79280dd 2021-10-15 thomas free(best_deltas);
857 b79280dd 2021-10-15 thomas best_deltas = deltas;
858 b79280dd 2021-10-15 thomas best_ndeltas = ndeltas;
859 b79280dd 2021-10-15 thomas deltas = NULL;
860 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
861 e6bcace5 2021-06-22 stsp m->prev = base;
862 e6bcace5 2021-06-22 stsp m->head = base->head;
863 e6bcace5 2021-06-22 stsp if (m->head == NULL)
864 e6bcace5 2021-06-22 stsp m->head = base;
865 e6bcace5 2021-06-22 stsp } else {
866 e6bcace5 2021-06-22 stsp free(deltas);
867 e6bcace5 2021-06-22 stsp deltas = NULL;
868 e6bcace5 2021-06-22 stsp ndeltas = 0;
869 e6bcace5 2021-06-22 stsp }
870 e6bcace5 2021-06-22 stsp }
871 e6bcace5 2021-06-22 stsp
872 b79280dd 2021-10-15 thomas if (best_ndeltas > 0) {
873 510885f7 2022-01-10 thomas if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
874 510885f7 2022-01-10 thomas delta_memsize + best_size <= max_delta_memsize) {
875 510885f7 2022-01-10 thomas delta_memsize += best_size;
876 3b6ceab7 2022-01-10 thomas err = encode_delta_in_mem(m, raw, best_deltas,
877 3b6ceab7 2022-01-10 thomas best_ndeltas, best_size, m->prev->size);
878 3b6ceab7 2022-01-10 thomas } else {
879 3b6ceab7 2022-01-10 thomas m->delta_offset = ftello(delta_cache);
880 3b6ceab7 2022-01-10 thomas err = encode_delta(m, raw, best_deltas,
881 3b6ceab7 2022-01-10 thomas best_ndeltas, m->prev->size, delta_cache);
882 3b6ceab7 2022-01-10 thomas }
883 b79280dd 2021-10-15 thomas free(best_deltas);
884 b79280dd 2021-10-15 thomas best_deltas = NULL;
885 b79280dd 2021-10-15 thomas best_ndeltas = 0;
886 b79280dd 2021-10-15 thomas if (err)
887 b79280dd 2021-10-15 thomas goto done;
888 b79280dd 2021-10-15 thomas }
889 b79280dd 2021-10-15 thomas
890 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
891 e6bcace5 2021-06-22 stsp raw = NULL;
892 e6bcace5 2021-06-22 stsp }
893 e6bcace5 2021-06-22 stsp done:
894 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
895 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
896 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
897 e6bcace5 2021-06-22 stsp }
898 e6bcace5 2021-06-22 stsp if (raw)
899 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
900 e6bcace5 2021-06-22 stsp if (base_raw)
901 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
902 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
903 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
904 b79280dd 2021-10-15 thomas free(deltas);
905 b79280dd 2021-10-15 thomas free(best_deltas);
906 e6bcace5 2021-06-22 stsp return err;
907 e6bcace5 2021-06-22 stsp }
908 e6bcace5 2021-06-22 stsp
909 e6bcace5 2021-06-22 stsp static const struct got_error *
910 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
911 05118f5a 2021-06-22 stsp struct got_repository *repo)
912 05118f5a 2021-06-22 stsp {
913 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
914 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
915 05118f5a 2021-06-22 stsp int idx;
916 05118f5a 2021-06-22 stsp
917 05118f5a 2021-06-22 stsp *found = 0;
918 05118f5a 2021-06-22 stsp
919 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
920 05118f5a 2021-06-22 stsp if (err == NULL)
921 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
922 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
923 05118f5a 2021-06-22 stsp err = NULL;
924 05118f5a 2021-06-22 stsp return err;
925 05118f5a 2021-06-22 stsp }
926 05118f5a 2021-06-22 stsp
927 05118f5a 2021-06-22 stsp static const struct got_error *
928 f9c2e8e5 2022-02-13 thomas add_object(int want_meta, struct got_object_idset *idset,
929 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
930 cc524d36 2022-05-31 thomas time_t mtime, uint32_t seed, int loose_obj_only,
931 cc524d36 2022-05-31 thomas struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
932 78a00876 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
933 78a00876 2022-03-22 thomas struct got_ratelimit *rl)
934 e6bcace5 2021-06-22 stsp {
935 e6bcace5 2021-06-22 stsp const struct got_error *err;
936 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m = NULL;
937 e6bcace5 2021-06-22 stsp
938 05118f5a 2021-06-22 stsp if (loose_obj_only) {
939 05118f5a 2021-06-22 stsp int is_packed;
940 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
941 05118f5a 2021-06-22 stsp if (err)
942 05118f5a 2021-06-22 stsp return err;
943 a605f678 2022-03-22 thomas if (is_packed && want_meta)
944 05118f5a 2021-06-22 stsp return NULL;
945 05118f5a 2021-06-22 stsp }
946 05118f5a 2021-06-22 stsp
947 f9c2e8e5 2022-02-13 thomas if (want_meta) {
948 cc524d36 2022-05-31 thomas err = alloc_meta(&m, id, path, obj_type, mtime, seed);
949 78a00876 2022-03-22 thomas if (err)
950 78a00876 2022-03-22 thomas return err;
951 78a00876 2022-03-22 thomas
952 78a00876 2022-03-22 thomas (*nfound)++;
953 78a00876 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
954 78a00876 2022-03-22 thomas *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
955 ee7b409c 2022-04-16 thomas if (err) {
956 ee7b409c 2022-04-16 thomas clear_meta(m);
957 ee7b409c 2022-04-16 thomas free(m);
958 f9c2e8e5 2022-02-13 thomas return err;
959 ee7b409c 2022-04-16 thomas }
960 e6bcace5 2021-06-22 stsp }
961 e6bcace5 2021-06-22 stsp
962 ee7b409c 2022-04-16 thomas err = got_object_idset_add(idset, id, m);
963 ee7b409c 2022-04-16 thomas if (err) {
964 ee7b409c 2022-04-16 thomas clear_meta(m);
965 ee7b409c 2022-04-16 thomas free(m);
966 ee7b409c 2022-04-16 thomas }
967 ee7b409c 2022-04-16 thomas return err;
968 e6bcace5 2021-06-22 stsp }
969 e6bcace5 2021-06-22 stsp
970 e6bcace5 2021-06-22 stsp static const struct got_error *
971 f9c2e8e5 2022-02-13 thomas load_tree_entries(struct got_object_id_queue *ids, int want_meta,
972 7afbdbad 2022-05-12 thomas struct got_object_idset *idset, struct got_object_idset *idset_exclude,
973 63915ee5 2022-06-23 thomas struct got_tree_object *tree,
974 cc524d36 2022-05-31 thomas const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
975 85220b0e 2022-03-22 thomas int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
976 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
977 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
978 e6bcace5 2021-06-22 stsp {
979 e6bcace5 2021-06-22 stsp const struct got_error *err;
980 e6bcace5 2021-06-22 stsp char *p = NULL;
981 e6bcace5 2021-06-22 stsp int i;
982 85220b0e 2022-03-22 thomas
983 85220b0e 2022-03-22 thomas (*ntrees)++;
984 85220b0e 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
985 85220b0e 2022-03-22 thomas *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
986 e6bcace5 2021-06-22 stsp if (err)
987 e6bcace5 2021-06-22 stsp return err;
988 e6bcace5 2021-06-22 stsp
989 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
990 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
991 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
992 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
993 e6bcace5 2021-06-22 stsp
994 e6bcace5 2021-06-22 stsp if (cancel_cb) {
995 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
996 e6bcace5 2021-06-22 stsp if (err)
997 e6bcace5 2021-06-22 stsp break;
998 e6bcace5 2021-06-22 stsp }
999 e6bcace5 2021-06-22 stsp
1000 08511b5e 2021-09-24 thomas if (got_object_tree_entry_is_submodule(e) ||
1001 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset, id) ||
1002 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset_exclude, id))
1003 13280750 2022-06-13 thomas continue;
1004 63915ee5 2022-06-23 thomas
1005 63915ee5 2022-06-23 thomas /*
1006 63915ee5 2022-06-23 thomas * If got-read-pack is crawling trees for us then
1007 63915ee5 2022-06-23 thomas * we are only here to collect blob IDs.
1008 63915ee5 2022-06-23 thomas */
1009 63915ee5 2022-06-23 thomas if (ids == NULL && S_ISDIR(mode))
1010 63915ee5 2022-06-23 thomas continue;
1011 63915ee5 2022-06-23 thomas
1012 63915ee5 2022-06-23 thomas if (asprintf(&p, "%s%s%s", dpath,
1013 63915ee5 2022-06-23 thomas got_path_is_root_dir(dpath) ? "" : "/",
1014 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
1015 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
1016 e6bcace5 2021-06-22 stsp break;
1017 e6bcace5 2021-06-22 stsp }
1018 e6bcace5 2021-06-22 stsp
1019 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
1020 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1021 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1022 e6bcace5 2021-06-22 stsp if (err)
1023 e6bcace5 2021-06-22 stsp break;
1024 eee114b4 2022-05-31 thomas qid->data = p;
1025 eee114b4 2022-05-31 thomas p = NULL;
1026 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
1027 08511b5e 2021-09-24 thomas } else if (S_ISREG(mode) || S_ISLNK(mode)) {
1028 7afbdbad 2022-05-12 thomas err = add_object(want_meta,
1029 7afbdbad 2022-05-12 thomas want_meta ? idset : idset_exclude, id, p,
1030 cc524d36 2022-05-31 thomas GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
1031 cc524d36 2022-05-31 thomas repo, ncolored, nfound, ntrees,
1032 78a00876 2022-03-22 thomas progress_cb, progress_arg, rl);
1033 85220b0e 2022-03-22 thomas if (err)
1034 85220b0e 2022-03-22 thomas break;
1035 eee114b4 2022-05-31 thomas free(p);
1036 eee114b4 2022-05-31 thomas p = NULL;
1037 eee114b4 2022-05-31 thomas } else {
1038 eee114b4 2022-05-31 thomas free(p);
1039 eee114b4 2022-05-31 thomas p = NULL;
1040 e6bcace5 2021-06-22 stsp }
1041 e6bcace5 2021-06-22 stsp }
1042 e6bcace5 2021-06-22 stsp
1043 e6bcace5 2021-06-22 stsp free(p);
1044 e6bcace5 2021-06-22 stsp return err;
1045 e6bcace5 2021-06-22 stsp }
1046 e6bcace5 2021-06-22 stsp
1047 e6bcace5 2021-06-22 stsp static const struct got_error *
1048 f9c2e8e5 2022-02-13 thomas load_tree(int want_meta, struct got_object_idset *idset,
1049 7afbdbad 2022-05-12 thomas struct got_object_idset *idset_exclude,
1050 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
1051 cc524d36 2022-05-31 thomas uint32_t seed, struct got_repository *repo, int loose_obj_only,
1052 85220b0e 2022-03-22 thomas int *ncolored, int *nfound, int *ntrees,
1053 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1054 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1055 e6bcace5 2021-06-22 stsp {
1056 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1057 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
1058 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1059 63915ee5 2022-06-23 thomas struct got_tree_object *tree = NULL;
1060 e6bcace5 2021-06-22 stsp
1061 7afbdbad 2022-05-12 thomas if (got_object_idset_contains(idset, tree_id) ||
1062 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset_exclude, tree_id))
1063 e6bcace5 2021-06-22 stsp return NULL;
1064 e6bcace5 2021-06-22 stsp
1065 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
1066 e6bcace5 2021-06-22 stsp if (err)
1067 eee114b4 2022-05-31 thomas return err;
1068 eee114b4 2022-05-31 thomas qid->data = strdup(dpath);
1069 eee114b4 2022-05-31 thomas if (qid->data == NULL) {
1070 eee114b4 2022-05-31 thomas err = got_error_from_errno("strdup");
1071 eee114b4 2022-05-31 thomas got_object_qid_free(qid);
1072 e6bcace5 2021-06-22 stsp return err;
1073 eee114b4 2022-05-31 thomas }
1074 e6bcace5 2021-06-22 stsp
1075 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
1076 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1077 e6bcace5 2021-06-22 stsp
1078 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
1079 eee114b4 2022-05-31 thomas const char *path;
1080 e6bcace5 2021-06-22 stsp if (cancel_cb) {
1081 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
1082 e6bcace5 2021-06-22 stsp if (err)
1083 e6bcace5 2021-06-22 stsp break;
1084 e6bcace5 2021-06-22 stsp }
1085 e6bcace5 2021-06-22 stsp
1086 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
1087 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
1088 eee114b4 2022-05-31 thomas path = qid->data;
1089 e6bcace5 2021-06-22 stsp
1090 7afbdbad 2022-05-12 thomas if (got_object_idset_contains(idset, &qid->id) ||
1091 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset_exclude, &qid->id)) {
1092 eee114b4 2022-05-31 thomas free(qid->data);
1093 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1094 e6bcace5 2021-06-22 stsp continue;
1095 e6bcace5 2021-06-22 stsp }
1096 e6bcace5 2021-06-22 stsp
1097 7afbdbad 2022-05-12 thomas err = add_object(want_meta, want_meta ? idset : idset_exclude,
1098 eee114b4 2022-05-31 thomas &qid->id, path, GOT_OBJ_TYPE_TREE,
1099 cc524d36 2022-05-31 thomas mtime, seed, loose_obj_only, repo,
1100 78a00876 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1101 e6bcace5 2021-06-22 stsp if (err) {
1102 eee114b4 2022-05-31 thomas free(qid->data);
1103 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1104 e6bcace5 2021-06-22 stsp break;
1105 e6bcace5 2021-06-22 stsp }
1106 e6bcace5 2021-06-22 stsp
1107 63915ee5 2022-06-23 thomas err = got_object_open_as_tree(&tree, repo, &qid->id);
1108 63915ee5 2022-06-23 thomas if (err) {
1109 63915ee5 2022-06-23 thomas free(qid->data);
1110 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1111 63915ee5 2022-06-23 thomas break;
1112 63915ee5 2022-06-23 thomas }
1113 63915ee5 2022-06-23 thomas
1114 7afbdbad 2022-05-12 thomas err = load_tree_entries(&tree_ids, want_meta, idset,
1115 63915ee5 2022-06-23 thomas idset_exclude, tree, path, mtime, seed, repo,
1116 63915ee5 2022-06-23 thomas loose_obj_only, ncolored, nfound, ntrees,
1117 63915ee5 2022-06-23 thomas progress_cb, progress_arg, rl,
1118 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1119 eee114b4 2022-05-31 thomas free(qid->data);
1120 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1121 e6bcace5 2021-06-22 stsp if (err)
1122 e6bcace5 2021-06-22 stsp break;
1123 63915ee5 2022-06-23 thomas
1124 63915ee5 2022-06-23 thomas got_object_tree_close(tree);
1125 63915ee5 2022-06-23 thomas tree = NULL;
1126 e6bcace5 2021-06-22 stsp }
1127 e6bcace5 2021-06-22 stsp
1128 eee114b4 2022-05-31 thomas STAILQ_FOREACH(qid, &tree_ids, entry)
1129 eee114b4 2022-05-31 thomas free(qid->data);
1130 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
1131 63915ee5 2022-06-23 thomas if (tree)
1132 63915ee5 2022-06-23 thomas got_object_tree_close(tree);
1133 e6bcace5 2021-06-22 stsp return err;
1134 e6bcace5 2021-06-22 stsp }
1135 e6bcace5 2021-06-22 stsp
1136 e6bcace5 2021-06-22 stsp static const struct got_error *
1137 f9c2e8e5 2022-02-13 thomas load_commit(int want_meta, struct got_object_idset *idset,
1138 7afbdbad 2022-05-12 thomas struct got_object_idset *idset_exclude,
1139 cc524d36 2022-05-31 thomas struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1140 cc524d36 2022-05-31 thomas int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1141 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1142 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1143 e6bcace5 2021-06-22 stsp {
1144 e6bcace5 2021-06-22 stsp const struct got_error *err;
1145 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1146 e6bcace5 2021-06-22 stsp
1147 7afbdbad 2022-05-12 thomas if (got_object_idset_contains(idset, id) ||
1148 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset_exclude, id))
1149 e6bcace5 2021-06-22 stsp return NULL;
1150 05118f5a 2021-06-22 stsp
1151 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1152 05118f5a 2021-06-22 stsp int is_packed;
1153 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1154 05118f5a 2021-06-22 stsp if (err)
1155 05118f5a 2021-06-22 stsp return err;
1156 a605f678 2022-03-22 thomas if (is_packed && want_meta)
1157 05118f5a 2021-06-22 stsp return NULL;
1158 05118f5a 2021-06-22 stsp }
1159 e6bcace5 2021-06-22 stsp
1160 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1161 e6bcace5 2021-06-22 stsp if (err)
1162 e6bcace5 2021-06-22 stsp return err;
1163 e6bcace5 2021-06-22 stsp
1164 7afbdbad 2022-05-12 thomas err = add_object(want_meta, want_meta ? idset : idset_exclude,
1165 7afbdbad 2022-05-12 thomas id, "", GOT_OBJ_TYPE_COMMIT,
1166 cc524d36 2022-05-31 thomas got_object_commit_get_committer_time(commit), seed,
1167 78a00876 2022-03-22 thomas loose_obj_only, repo,
1168 78a00876 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1169 e6bcace5 2021-06-22 stsp if (err)
1170 e6bcace5 2021-06-22 stsp goto done;
1171 85220b0e 2022-03-22 thomas
1172 7afbdbad 2022-05-12 thomas err = load_tree(want_meta, idset, idset_exclude,
1173 7afbdbad 2022-05-12 thomas got_object_commit_get_tree_id(commit),
1174 cc524d36 2022-05-31 thomas "", got_object_commit_get_committer_time(commit), seed,
1175 85220b0e 2022-03-22 thomas repo, loose_obj_only, ncolored, nfound, ntrees,
1176 85220b0e 2022-03-22 thomas progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1177 e6bcace5 2021-06-22 stsp done:
1178 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1179 e6bcace5 2021-06-22 stsp return err;
1180 e6bcace5 2021-06-22 stsp }
1181 e6bcace5 2021-06-22 stsp
1182 05118f5a 2021-06-22 stsp static const struct got_error *
1183 f9c2e8e5 2022-02-13 thomas load_tag(int want_meta, struct got_object_idset *idset,
1184 7afbdbad 2022-05-12 thomas struct got_object_idset *idset_exclude,
1185 cc524d36 2022-05-31 thomas struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1186 cc524d36 2022-05-31 thomas int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1187 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1188 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1189 05118f5a 2021-06-22 stsp {
1190 05118f5a 2021-06-22 stsp const struct got_error *err;
1191 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
1192 05118f5a 2021-06-22 stsp
1193 7afbdbad 2022-05-12 thomas if (got_object_idset_contains(idset, id) ||
1194 7afbdbad 2022-05-12 thomas got_object_idset_contains(idset_exclude, id))
1195 05118f5a 2021-06-22 stsp return NULL;
1196 05118f5a 2021-06-22 stsp
1197 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1198 05118f5a 2021-06-22 stsp int is_packed;
1199 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1200 05118f5a 2021-06-22 stsp if (err)
1201 05118f5a 2021-06-22 stsp return err;
1202 a605f678 2022-03-22 thomas if (is_packed && want_meta)
1203 05118f5a 2021-06-22 stsp return NULL;
1204 05118f5a 2021-06-22 stsp }
1205 05118f5a 2021-06-22 stsp
1206 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1207 05118f5a 2021-06-22 stsp if (err)
1208 05118f5a 2021-06-22 stsp return err;
1209 05118f5a 2021-06-22 stsp
1210 7afbdbad 2022-05-12 thomas err = add_object(want_meta, want_meta ? idset : idset_exclude,
1211 7afbdbad 2022-05-12 thomas id, "", GOT_OBJ_TYPE_TAG,
1212 cc524d36 2022-05-31 thomas got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1213 78a00876 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1214 05118f5a 2021-06-22 stsp if (err)
1215 05118f5a 2021-06-22 stsp goto done;
1216 05118f5a 2021-06-22 stsp
1217 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1218 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1219 7afbdbad 2022-05-12 thomas err = load_commit(want_meta, idset, idset_exclude,
1220 cc524d36 2022-05-31 thomas got_object_tag_get_object_id(tag), repo, seed,
1221 cc524d36 2022-05-31 thomas loose_obj_only, ncolored, nfound, ntrees,
1222 cc524d36 2022-05-31 thomas progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1223 05118f5a 2021-06-22 stsp break;
1224 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1225 7afbdbad 2022-05-12 thomas err = load_tree(want_meta, idset, idset_exclude,
1226 f9c2e8e5 2022-02-13 thomas got_object_tag_get_object_id(tag), "",
1227 cc524d36 2022-05-31 thomas got_object_tag_get_tagger_time(tag), seed, repo,
1228 cc524d36 2022-05-31 thomas loose_obj_only, ncolored, nfound, ntrees,
1229 cc524d36 2022-05-31 thomas progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1230 05118f5a 2021-06-22 stsp break;
1231 05118f5a 2021-06-22 stsp default:
1232 05118f5a 2021-06-22 stsp break;
1233 05118f5a 2021-06-22 stsp }
1234 05118f5a 2021-06-22 stsp
1235 05118f5a 2021-06-22 stsp done:
1236 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1237 05118f5a 2021-06-22 stsp return err;
1238 05118f5a 2021-06-22 stsp }
1239 05118f5a 2021-06-22 stsp
1240 e6bcace5 2021-06-22 stsp enum findtwixt_color {
1241 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
1242 e6bcace5 2021-06-22 stsp COLOR_DROP,
1243 5fe7b5c1 2022-04-16 thomas COLOR_SKIP,
1244 ec2b23c5 2022-07-01 thomas COLOR_MAX,
1245 e6bcace5 2021-06-22 stsp };
1246 e6bcace5 2021-06-22 stsp
1247 e6bcace5 2021-06-22 stsp static const struct got_error *
1248 ec2b23c5 2022-07-01 thomas paint_commit(struct got_object_qid *qid, intptr_t color)
1249 e6bcace5 2021-06-22 stsp {
1250 ec2b23c5 2022-07-01 thomas if (color < 0 || color >= COLOR_MAX)
1251 5fe7b5c1 2022-04-16 thomas return got_error(GOT_ERR_RANGE);
1252 e6bcace5 2021-06-22 stsp
1253 ec2b23c5 2022-07-01 thomas qid->data = (void *)color;
1254 e6bcace5 2021-06-22 stsp return NULL;
1255 e6bcace5 2021-06-22 stsp }
1256 e6bcace5 2021-06-22 stsp
1257 e6bcace5 2021-06-22 stsp static const struct got_error *
1258 5fe7b5c1 2022-04-16 thomas queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1259 ec2b23c5 2022-07-01 thomas intptr_t color, struct got_repository *repo)
1260 e6bcace5 2021-06-22 stsp {
1261 5fe7b5c1 2022-04-16 thomas const struct got_error *err;
1262 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1263 e6bcace5 2021-06-22 stsp
1264 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1265 e6bcace5 2021-06-22 stsp if (err)
1266 e6bcace5 2021-06-22 stsp return err;
1267 e6bcace5 2021-06-22 stsp
1268 5fe7b5c1 2022-04-16 thomas STAILQ_INSERT_TAIL(ids, qid, entry);
1269 5fe7b5c1 2022-04-16 thomas return paint_commit(qid, color);
1270 e6bcace5 2021-06-22 stsp }
1271 e6bcace5 2021-06-22 stsp
1272 e6bcace5 2021-06-22 stsp struct append_id_arg {
1273 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1274 e6bcace5 2021-06-22 stsp int idx;
1275 5fe7b5c1 2022-04-16 thomas struct got_object_idset *drop;
1276 5fe7b5c1 2022-04-16 thomas struct got_object_idset *skip;
1277 e6bcace5 2021-06-22 stsp };
1278 e6bcace5 2021-06-22 stsp
1279 e6bcace5 2021-06-22 stsp static const struct got_error *
1280 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1281 e6bcace5 2021-06-22 stsp {
1282 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1283 e6bcace5 2021-06-22 stsp
1284 5fe7b5c1 2022-04-16 thomas if (got_object_idset_contains(a->skip, id) ||
1285 5fe7b5c1 2022-04-16 thomas got_object_idset_contains(a->drop, id))
1286 5fe7b5c1 2022-04-16 thomas return NULL;
1287 5fe7b5c1 2022-04-16 thomas
1288 5fe7b5c1 2022-04-16 thomas a->array[++a->idx] = got_object_id_dup(id);
1289 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1290 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1291 e6bcace5 2021-06-22 stsp
1292 e6bcace5 2021-06-22 stsp return NULL;
1293 a3a0e34e 2022-04-16 thomas }
1294 a3a0e34e 2022-04-16 thomas
1295 a3a0e34e 2022-04-16 thomas static const struct got_error *
1296 ec2b23c5 2022-07-01 thomas queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1297 a3a0e34e 2022-04-16 thomas struct got_object_id_queue *ids, struct got_repository *repo)
1298 a3a0e34e 2022-04-16 thomas {
1299 a3a0e34e 2022-04-16 thomas const struct got_error *err;
1300 a3a0e34e 2022-04-16 thomas struct got_tag_object *tag = NULL;
1301 a3a0e34e 2022-04-16 thomas int obj_type;
1302 a3a0e34e 2022-04-16 thomas
1303 a3a0e34e 2022-04-16 thomas err = got_object_get_type(&obj_type, repo, id);
1304 a3a0e34e 2022-04-16 thomas if (err)
1305 a3a0e34e 2022-04-16 thomas return err;
1306 a3a0e34e 2022-04-16 thomas
1307 a3a0e34e 2022-04-16 thomas if (obj_type == GOT_OBJ_TYPE_TAG) {
1308 a3a0e34e 2022-04-16 thomas err = got_object_open_as_tag(&tag, repo, id);
1309 a3a0e34e 2022-04-16 thomas if (err)
1310 a3a0e34e 2022-04-16 thomas return err;
1311 a3a0e34e 2022-04-16 thomas obj_type = got_object_tag_get_object_type(tag);
1312 a3a0e34e 2022-04-16 thomas id = got_object_tag_get_object_id(tag);
1313 a3a0e34e 2022-04-16 thomas }
1314 a3a0e34e 2022-04-16 thomas
1315 a3a0e34e 2022-04-16 thomas if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1316 a3a0e34e 2022-04-16 thomas err = queue_commit_id(ids, id, color, repo);
1317 a3a0e34e 2022-04-16 thomas if (err)
1318 a3a0e34e 2022-04-16 thomas goto done;
1319 a3a0e34e 2022-04-16 thomas }
1320 a3a0e34e 2022-04-16 thomas done:
1321 a3a0e34e 2022-04-16 thomas if (tag)
1322 a3a0e34e 2022-04-16 thomas got_object_tag_close(tag);
1323 a3a0e34e 2022-04-16 thomas return err;
1324 e6bcace5 2021-06-22 stsp }
1325 e6bcace5 2021-06-22 stsp
1326 ec2b23c5 2022-07-01 thomas struct recv_painted_commit_arg {
1327 ec2b23c5 2022-07-01 thomas int *ncolored;
1328 ec2b23c5 2022-07-01 thomas int *nqueued;
1329 ec2b23c5 2022-07-01 thomas int *nskip;
1330 ec2b23c5 2022-07-01 thomas struct got_object_id_queue *ids;
1331 ec2b23c5 2022-07-01 thomas struct got_object_idset *keep;
1332 ec2b23c5 2022-07-01 thomas struct got_object_idset *drop;
1333 ec2b23c5 2022-07-01 thomas struct got_object_idset *skip;
1334 ec2b23c5 2022-07-01 thomas got_pack_progress_cb progress_cb;
1335 ec2b23c5 2022-07-01 thomas void *progress_arg;
1336 ec2b23c5 2022-07-01 thomas struct got_ratelimit *rl;
1337 ec2b23c5 2022-07-01 thomas got_cancel_cb cancel_cb;
1338 ec2b23c5 2022-07-01 thomas void *cancel_arg;
1339 ec2b23c5 2022-07-01 thomas };
1340 ec2b23c5 2022-07-01 thomas
1341 e6bcace5 2021-06-22 stsp static const struct got_error *
1342 ec2b23c5 2022-07-01 thomas recv_painted_commit(void *arg, struct got_object_id *id, intptr_t color)
1343 ec2b23c5 2022-07-01 thomas {
1344 ec2b23c5 2022-07-01 thomas const struct got_error *err = NULL;
1345 ec2b23c5 2022-07-01 thomas struct recv_painted_commit_arg *a = arg;
1346 ec2b23c5 2022-07-01 thomas struct got_object_qid *qid, *tmp;
1347 ec2b23c5 2022-07-01 thomas
1348 ec2b23c5 2022-07-01 thomas if (a->cancel_cb) {
1349 ec2b23c5 2022-07-01 thomas err = a->cancel_cb(a->cancel_arg);
1350 ec2b23c5 2022-07-01 thomas if (err)
1351 ec2b23c5 2022-07-01 thomas return err;
1352 ec2b23c5 2022-07-01 thomas }
1353 ec2b23c5 2022-07-01 thomas
1354 ec2b23c5 2022-07-01 thomas switch (color) {
1355 ec2b23c5 2022-07-01 thomas case COLOR_KEEP:
1356 ec2b23c5 2022-07-01 thomas err = got_object_idset_add(a->keep, id, NULL);
1357 ec2b23c5 2022-07-01 thomas if (err)
1358 ec2b23c5 2022-07-01 thomas return err;
1359 ec2b23c5 2022-07-01 thomas (*a->ncolored)++;
1360 ec2b23c5 2022-07-01 thomas break;
1361 ec2b23c5 2022-07-01 thomas case COLOR_DROP:
1362 ec2b23c5 2022-07-01 thomas err = got_object_idset_add(a->drop, id, NULL);
1363 ec2b23c5 2022-07-01 thomas if (err)
1364 ec2b23c5 2022-07-01 thomas return err;
1365 ec2b23c5 2022-07-01 thomas (*a->ncolored)++;
1366 ec2b23c5 2022-07-01 thomas break;
1367 ec2b23c5 2022-07-01 thomas case COLOR_SKIP:
1368 ec2b23c5 2022-07-01 thomas err = got_object_idset_add(a->skip, id, NULL);
1369 ec2b23c5 2022-07-01 thomas if (err)
1370 ec2b23c5 2022-07-01 thomas return err;
1371 ec2b23c5 2022-07-01 thomas break;
1372 ec2b23c5 2022-07-01 thomas default:
1373 ec2b23c5 2022-07-01 thomas /* should not happen */
1374 ec2b23c5 2022-07-01 thomas return got_error_fmt(GOT_ERR_NOT_IMPL,
1375 ec2b23c5 2022-07-01 thomas "%s invalid commit color %d", __func__, color);
1376 ec2b23c5 2022-07-01 thomas }
1377 ec2b23c5 2022-07-01 thomas
1378 ec2b23c5 2022-07-01 thomas STAILQ_FOREACH_SAFE(qid, a->ids, entry, tmp) {
1379 ec2b23c5 2022-07-01 thomas if (got_object_id_cmp(&qid->id, id) != 0)
1380 ec2b23c5 2022-07-01 thomas continue;
1381 ec2b23c5 2022-07-01 thomas STAILQ_REMOVE(a->ids, qid, got_object_qid, entry);
1382 ec2b23c5 2022-07-01 thomas color = (intptr_t)qid->data;
1383 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1384 ec2b23c5 2022-07-01 thomas (*a->nqueued)--;
1385 ec2b23c5 2022-07-01 thomas if (color == COLOR_SKIP)
1386 ec2b23c5 2022-07-01 thomas (*a->nskip)--;
1387 ec2b23c5 2022-07-01 thomas break;
1388 ec2b23c5 2022-07-01 thomas }
1389 ec2b23c5 2022-07-01 thomas
1390 ec2b23c5 2022-07-01 thomas return report_progress(a->progress_cb, a->progress_arg, a->rl,
1391 ec2b23c5 2022-07-01 thomas *a->ncolored, 0, 0, 0L, 0, 0, 0, 0);
1392 ec2b23c5 2022-07-01 thomas }
1393 ec2b23c5 2022-07-01 thomas
1394 ec2b23c5 2022-07-01 thomas static const struct got_error *
1395 ec2b23c5 2022-07-01 thomas paint_packed_commits(struct got_pack *pack, struct got_object_id *id,
1396 ec2b23c5 2022-07-01 thomas int idx, intptr_t color, int *ncolored, int *nqueued, int *nskip,
1397 ec2b23c5 2022-07-01 thomas struct got_object_id_queue *ids,
1398 ec2b23c5 2022-07-01 thomas struct got_object_idset *keep, struct got_object_idset *drop,
1399 ec2b23c5 2022-07-01 thomas struct got_object_idset *skip, struct got_repository *repo,
1400 ec2b23c5 2022-07-01 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1401 ec2b23c5 2022-07-01 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1402 ec2b23c5 2022-07-01 thomas {
1403 ec2b23c5 2022-07-01 thomas const struct got_error *err = NULL;
1404 ec2b23c5 2022-07-01 thomas struct got_object_id_queue next_ids;
1405 ec2b23c5 2022-07-01 thomas struct got_object_qid *qid, *tmp;
1406 ec2b23c5 2022-07-01 thomas struct recv_painted_commit_arg arg;
1407 ec2b23c5 2022-07-01 thomas
1408 ec2b23c5 2022-07-01 thomas STAILQ_INIT(&next_ids);
1409 ec2b23c5 2022-07-01 thomas
1410 ec2b23c5 2022-07-01 thomas err = got_privsep_send_painting_request(pack->privsep_child->ibuf,
1411 ec2b23c5 2022-07-01 thomas idx, id, color);
1412 ec2b23c5 2022-07-01 thomas if (err)
1413 ec2b23c5 2022-07-01 thomas return err;
1414 ec2b23c5 2022-07-01 thomas
1415 ec2b23c5 2022-07-01 thomas arg.ncolored = ncolored;
1416 ec2b23c5 2022-07-01 thomas arg.nqueued = nqueued;
1417 ec2b23c5 2022-07-01 thomas arg.nskip = nskip;
1418 ec2b23c5 2022-07-01 thomas arg.ids = ids;
1419 ec2b23c5 2022-07-01 thomas arg.keep = keep;
1420 ec2b23c5 2022-07-01 thomas arg.drop = drop;
1421 ec2b23c5 2022-07-01 thomas arg.skip = skip;
1422 ec2b23c5 2022-07-01 thomas arg.progress_cb = progress_cb;
1423 ec2b23c5 2022-07-01 thomas arg.progress_arg = progress_arg;
1424 ec2b23c5 2022-07-01 thomas arg.rl = rl;
1425 ec2b23c5 2022-07-01 thomas arg.cancel_cb = cancel_cb;
1426 ec2b23c5 2022-07-01 thomas arg.cancel_arg = cancel_arg;
1427 ec2b23c5 2022-07-01 thomas err = got_privsep_recv_painted_commits(&next_ids,
1428 ec2b23c5 2022-07-01 thomas recv_painted_commit, &arg, pack->privsep_child->ibuf);
1429 ec2b23c5 2022-07-01 thomas if (err)
1430 ec2b23c5 2022-07-01 thomas return err;
1431 ec2b23c5 2022-07-01 thomas
1432 ec2b23c5 2022-07-01 thomas STAILQ_FOREACH_SAFE(qid, &next_ids, entry, tmp) {
1433 ec2b23c5 2022-07-01 thomas struct got_object_qid *old_id;
1434 ec2b23c5 2022-07-01 thomas intptr_t qcolor, ocolor;
1435 ec2b23c5 2022-07-01 thomas STAILQ_FOREACH(old_id, ids, entry) {
1436 ec2b23c5 2022-07-01 thomas if (got_object_id_cmp(&qid->id, &old_id->id))
1437 ec2b23c5 2022-07-01 thomas continue;
1438 ec2b23c5 2022-07-01 thomas qcolor = (intptr_t)qid->data;
1439 ec2b23c5 2022-07-01 thomas ocolor = (intptr_t)old_id->data;
1440 ec2b23c5 2022-07-01 thomas STAILQ_REMOVE(&next_ids, qid, got_object_qid, entry);
1441 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1442 ec2b23c5 2022-07-01 thomas qid = NULL;
1443 ec2b23c5 2022-07-01 thomas if (qcolor != ocolor) {
1444 ec2b23c5 2022-07-01 thomas paint_commit(old_id, qcolor);
1445 ec2b23c5 2022-07-01 thomas if (ocolor == COLOR_SKIP)
1446 ec2b23c5 2022-07-01 thomas (*nskip)--;
1447 ec2b23c5 2022-07-01 thomas else if (qcolor == COLOR_SKIP)
1448 ec2b23c5 2022-07-01 thomas (*nskip)++;
1449 ec2b23c5 2022-07-01 thomas }
1450 ec2b23c5 2022-07-01 thomas break;
1451 ec2b23c5 2022-07-01 thomas }
1452 ec2b23c5 2022-07-01 thomas }
1453 ec2b23c5 2022-07-01 thomas while (!STAILQ_EMPTY(&next_ids)) {
1454 ec2b23c5 2022-07-01 thomas qid = STAILQ_FIRST(&next_ids);
1455 ec2b23c5 2022-07-01 thomas STAILQ_REMOVE_HEAD(&next_ids, entry);
1456 ec2b23c5 2022-07-01 thomas paint_commit(qid, color);
1457 ec2b23c5 2022-07-01 thomas STAILQ_INSERT_TAIL(ids, qid, entry);
1458 ec2b23c5 2022-07-01 thomas (*nqueued)++;
1459 ec2b23c5 2022-07-01 thomas if (color == COLOR_SKIP)
1460 ec2b23c5 2022-07-01 thomas (*nskip)++;
1461 ec2b23c5 2022-07-01 thomas }
1462 ec2b23c5 2022-07-01 thomas
1463 ec2b23c5 2022-07-01 thomas return err;
1464 ec2b23c5 2022-07-01 thomas }
1465 ec2b23c5 2022-07-01 thomas
1466 ec2b23c5 2022-07-01 thomas static const struct got_error *
1467 ec2b23c5 2022-07-01 thomas find_pack_for_commit_painting(struct got_packidx **best_packidx,
1468 ec2b23c5 2022-07-01 thomas struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1469 ec2b23c5 2022-07-01 thomas {
1470 ec2b23c5 2022-07-01 thomas const struct got_error *err = NULL;
1471 ec2b23c5 2022-07-01 thomas struct got_pathlist_entry *pe;
1472 ec2b23c5 2022-07-01 thomas const char *best_packidx_path = NULL;
1473 ec2b23c5 2022-07-01 thomas int nobj_max = 0;
1474 ec2b23c5 2022-07-01 thomas int ncommits_max = 0;
1475 ec2b23c5 2022-07-01 thomas
1476 ec2b23c5 2022-07-01 thomas *best_packidx = NULL;
1477 ec2b23c5 2022-07-01 thomas
1478 ec2b23c5 2022-07-01 thomas /*
1479 ec2b23c5 2022-07-01 thomas * Find the largest pack which contains at least some of the
1480 ec2b23c5 2022-07-01 thomas * commits we are interested in.
1481 ec2b23c5 2022-07-01 thomas */
1482 ec2b23c5 2022-07-01 thomas TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1483 ec2b23c5 2022-07-01 thomas const char *path_packidx = pe->path;
1484 ec2b23c5 2022-07-01 thomas struct got_packidx *packidx;
1485 ec2b23c5 2022-07-01 thomas int nobj, idx, ncommits = 0;
1486 ec2b23c5 2022-07-01 thomas struct got_object_qid *qid;
1487 ec2b23c5 2022-07-01 thomas
1488 ec2b23c5 2022-07-01 thomas err = got_repo_get_packidx(&packidx, path_packidx, repo);
1489 ec2b23c5 2022-07-01 thomas if (err)
1490 ec2b23c5 2022-07-01 thomas break;
1491 ec2b23c5 2022-07-01 thomas
1492 ec2b23c5 2022-07-01 thomas nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1493 ec2b23c5 2022-07-01 thomas if (nobj <= nobj_max)
1494 ec2b23c5 2022-07-01 thomas continue;
1495 ec2b23c5 2022-07-01 thomas
1496 ec2b23c5 2022-07-01 thomas STAILQ_FOREACH(qid, ids, entry) {
1497 ec2b23c5 2022-07-01 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1498 ec2b23c5 2022-07-01 thomas if (idx != -1)
1499 ec2b23c5 2022-07-01 thomas ncommits++;
1500 ec2b23c5 2022-07-01 thomas }
1501 ec2b23c5 2022-07-01 thomas if (ncommits > ncommits_max) {
1502 ec2b23c5 2022-07-01 thomas best_packidx_path = path_packidx;
1503 ec2b23c5 2022-07-01 thomas nobj_max = nobj;
1504 ec2b23c5 2022-07-01 thomas ncommits_max = ncommits;
1505 ec2b23c5 2022-07-01 thomas }
1506 ec2b23c5 2022-07-01 thomas }
1507 ec2b23c5 2022-07-01 thomas
1508 ec2b23c5 2022-07-01 thomas if (best_packidx_path && err == NULL) {
1509 ec2b23c5 2022-07-01 thomas err = got_repo_get_packidx(best_packidx, best_packidx_path,
1510 ec2b23c5 2022-07-01 thomas repo);
1511 ec2b23c5 2022-07-01 thomas }
1512 ec2b23c5 2022-07-01 thomas
1513 ec2b23c5 2022-07-01 thomas return err;
1514 ec2b23c5 2022-07-01 thomas }
1515 ec2b23c5 2022-07-01 thomas
1516 ec2b23c5 2022-07-01 thomas static const struct got_error *
1517 5fe7b5c1 2022-04-16 thomas paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1518 e9371be6 2022-04-16 thomas struct got_object_idset *keep, struct got_object_idset *drop,
1519 5fe7b5c1 2022-04-16 thomas struct got_object_idset *skip, struct got_repository *repo,
1520 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1521 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1522 e6bcace5 2021-06-22 stsp {
1523 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1524 e9371be6 2022-04-16 thomas struct got_commit_object *commit = NULL;
1525 ec2b23c5 2022-07-01 thomas struct got_packidx *packidx = NULL;
1526 ec2b23c5 2022-07-01 thomas struct got_pack *pack = NULL;
1527 5fe7b5c1 2022-04-16 thomas const struct got_object_id_queue *parents;
1528 ec2b23c5 2022-07-01 thomas struct got_object_qid *qid = NULL;
1529 5fe7b5c1 2022-04-16 thomas int nqueued = nids, nskip = 0;
1530 ec2b23c5 2022-07-01 thomas int idx;
1531 e6bcace5 2021-06-22 stsp
1532 5fe7b5c1 2022-04-16 thomas while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1533 ec2b23c5 2022-07-01 thomas intptr_t color;
1534 dd111f75 2022-04-16 thomas
1535 21a0da85 2022-04-16 thomas if (cancel_cb) {
1536 21a0da85 2022-04-16 thomas err = cancel_cb(cancel_arg);
1537 21a0da85 2022-04-16 thomas if (err)
1538 e9371be6 2022-04-16 thomas break;
1539 21a0da85 2022-04-16 thomas }
1540 21a0da85 2022-04-16 thomas
1541 e9371be6 2022-04-16 thomas qid = STAILQ_FIRST(ids);
1542 5fe7b5c1 2022-04-16 thomas STAILQ_REMOVE_HEAD(ids, entry);
1543 5fe7b5c1 2022-04-16 thomas nqueued--;
1544 ec2b23c5 2022-07-01 thomas color = (intptr_t)qid->data;
1545 5fe7b5c1 2022-04-16 thomas if (color == COLOR_SKIP)
1546 5fe7b5c1 2022-04-16 thomas nskip--;
1547 e6bcace5 2021-06-22 stsp
1548 ec242592 2022-04-22 thomas if (got_object_idset_contains(skip, &qid->id)) {
1549 5fe7b5c1 2022-04-16 thomas got_object_qid_free(qid);
1550 ec2b23c5 2022-07-01 thomas qid = NULL;
1551 5fe7b5c1 2022-04-16 thomas continue;
1552 5fe7b5c1 2022-04-16 thomas }
1553 ec2b23c5 2022-07-01 thomas if (color == COLOR_KEEP &&
1554 ec2b23c5 2022-07-01 thomas got_object_idset_contains(keep, &qid->id)) {
1555 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1556 ec2b23c5 2022-07-01 thomas qid = NULL;
1557 ec2b23c5 2022-07-01 thomas continue;
1558 ec2b23c5 2022-07-01 thomas }
1559 ec2b23c5 2022-07-01 thomas if (color == COLOR_DROP &&
1560 ec2b23c5 2022-07-01 thomas got_object_idset_contains(drop, &qid->id)) {
1561 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1562 ec2b23c5 2022-07-01 thomas qid = NULL;
1563 ec2b23c5 2022-07-01 thomas continue;
1564 ec2b23c5 2022-07-01 thomas }
1565 85220b0e 2022-03-22 thomas
1566 ec2b23c5 2022-07-01 thomas /* Pinned pack may have moved to different cache slot. */
1567 ec2b23c5 2022-07-01 thomas pack = got_repo_get_pinned_pack(repo);
1568 ec2b23c5 2022-07-01 thomas
1569 ec2b23c5 2022-07-01 thomas if (packidx && pack) {
1570 ec2b23c5 2022-07-01 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1571 ec2b23c5 2022-07-01 thomas if (idx != -1) {
1572 ec2b23c5 2022-07-01 thomas err = paint_packed_commits(pack, &qid->id,
1573 ec2b23c5 2022-07-01 thomas idx, color, ncolored, &nqueued, &nskip,
1574 ec2b23c5 2022-07-01 thomas ids, keep, drop, skip, repo,
1575 ec2b23c5 2022-07-01 thomas progress_cb, progress_arg, rl,
1576 ec2b23c5 2022-07-01 thomas cancel_cb, cancel_arg);
1577 ec2b23c5 2022-07-01 thomas if (err)
1578 ec2b23c5 2022-07-01 thomas break;
1579 5fe7b5c1 2022-04-16 thomas got_object_qid_free(qid);
1580 ec2b23c5 2022-07-01 thomas qid = NULL;
1581 5fe7b5c1 2022-04-16 thomas continue;
1582 5fe7b5c1 2022-04-16 thomas }
1583 ec2b23c5 2022-07-01 thomas }
1584 ec2b23c5 2022-07-01 thomas
1585 ec2b23c5 2022-07-01 thomas switch (color) {
1586 ec2b23c5 2022-07-01 thomas case COLOR_KEEP:
1587 ec242592 2022-04-22 thomas if (got_object_idset_contains(drop, &qid->id)) {
1588 5fe7b5c1 2022-04-16 thomas err = paint_commit(qid, COLOR_SKIP);
1589 5fe7b5c1 2022-04-16 thomas if (err)
1590 5fe7b5c1 2022-04-16 thomas goto done;
1591 5fe7b5c1 2022-04-16 thomas } else
1592 5fe7b5c1 2022-04-16 thomas (*ncolored)++;
1593 ec242592 2022-04-22 thomas err = got_object_idset_add(keep, &qid->id, NULL);
1594 5fe7b5c1 2022-04-16 thomas if (err)
1595 5fe7b5c1 2022-04-16 thomas goto done;
1596 5fe7b5c1 2022-04-16 thomas break;
1597 5fe7b5c1 2022-04-16 thomas case COLOR_DROP:
1598 ec242592 2022-04-22 thomas if (got_object_idset_contains(keep, &qid->id)) {
1599 5fe7b5c1 2022-04-16 thomas err = paint_commit(qid, COLOR_SKIP);
1600 5fe7b5c1 2022-04-16 thomas if (err)
1601 5fe7b5c1 2022-04-16 thomas goto done;
1602 5fe7b5c1 2022-04-16 thomas } else
1603 5fe7b5c1 2022-04-16 thomas (*ncolored)++;
1604 ec242592 2022-04-22 thomas err = got_object_idset_add(drop, &qid->id, NULL);
1605 5fe7b5c1 2022-04-16 thomas if (err)
1606 5fe7b5c1 2022-04-16 thomas goto done;
1607 5fe7b5c1 2022-04-16 thomas break;
1608 5fe7b5c1 2022-04-16 thomas case COLOR_SKIP:
1609 ec242592 2022-04-22 thomas if (!got_object_idset_contains(skip, &qid->id)) {
1610 ec242592 2022-04-22 thomas err = got_object_idset_add(skip, &qid->id,
1611 ec242592 2022-04-22 thomas NULL);
1612 5fe7b5c1 2022-04-16 thomas if (err)
1613 5fe7b5c1 2022-04-16 thomas goto done;
1614 5fe7b5c1 2022-04-16 thomas }
1615 5fe7b5c1 2022-04-16 thomas break;
1616 5fe7b5c1 2022-04-16 thomas default:
1617 5fe7b5c1 2022-04-16 thomas /* should not happen */
1618 5fe7b5c1 2022-04-16 thomas err = got_error_fmt(GOT_ERR_NOT_IMPL,
1619 5fe7b5c1 2022-04-16 thomas "%s invalid commit color %d", __func__, color);
1620 5fe7b5c1 2022-04-16 thomas goto done;
1621 5fe7b5c1 2022-04-16 thomas }
1622 5fe7b5c1 2022-04-16 thomas
1623 85220b0e 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
1624 85220b0e 2022-03-22 thomas *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1625 85220b0e 2022-03-22 thomas if (err)
1626 e9371be6 2022-04-16 thomas break;
1627 e6bcace5 2021-06-22 stsp
1628 ec242592 2022-04-22 thomas err = got_object_open_as_commit(&commit, repo, &qid->id);
1629 5fe7b5c1 2022-04-16 thomas if (err)
1630 5fe7b5c1 2022-04-16 thomas break;
1631 e6bcace5 2021-06-22 stsp
1632 5fe7b5c1 2022-04-16 thomas parents = got_object_commit_get_parent_ids(commit);
1633 5fe7b5c1 2022-04-16 thomas if (parents) {
1634 5fe7b5c1 2022-04-16 thomas struct got_object_qid *pid;
1635 ec2b23c5 2022-07-01 thomas color = (intptr_t)qid->data;
1636 5fe7b5c1 2022-04-16 thomas STAILQ_FOREACH(pid, parents, entry) {
1637 ec2b23c5 2022-07-01 thomas err = queue_commit_id(ids, &pid->id,
1638 ec2b23c5 2022-07-01 thomas color, repo);
1639 5fe7b5c1 2022-04-16 thomas if (err)
1640 5fe7b5c1 2022-04-16 thomas break;
1641 5fe7b5c1 2022-04-16 thomas nqueued++;
1642 5fe7b5c1 2022-04-16 thomas if (color == COLOR_SKIP)
1643 5fe7b5c1 2022-04-16 thomas nskip++;
1644 e6bcace5 2021-06-22 stsp }
1645 e6bcace5 2021-06-22 stsp }
1646 e6bcace5 2021-06-22 stsp
1647 ec2b23c5 2022-07-01 thomas if (pack == NULL && (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
1648 ec2b23c5 2022-07-01 thomas if (packidx == NULL) {
1649 ec2b23c5 2022-07-01 thomas err = find_pack_for_commit_painting(&packidx,
1650 ec2b23c5 2022-07-01 thomas ids, nqueued, repo);
1651 ec2b23c5 2022-07-01 thomas if (err)
1652 ec2b23c5 2022-07-01 thomas goto done;
1653 ec2b23c5 2022-07-01 thomas }
1654 ec2b23c5 2022-07-01 thomas if (packidx != NULL) {
1655 ec2b23c5 2022-07-01 thomas err = cache_pack_for_packidx(&pack, packidx,
1656 ec2b23c5 2022-07-01 thomas repo);
1657 ec2b23c5 2022-07-01 thomas if (err)
1658 ec2b23c5 2022-07-01 thomas goto done;
1659 ec2b23c5 2022-07-01 thomas err = got_privsep_init_commit_painting(
1660 ec2b23c5 2022-07-01 thomas pack->privsep_child->ibuf);
1661 ec2b23c5 2022-07-01 thomas if (err)
1662 ec2b23c5 2022-07-01 thomas goto done;
1663 ec2b23c5 2022-07-01 thomas err = send_idset(pack->privsep_child->ibuf,
1664 ec2b23c5 2022-07-01 thomas keep);
1665 ec2b23c5 2022-07-01 thomas if (err)
1666 ec2b23c5 2022-07-01 thomas goto done;
1667 ec2b23c5 2022-07-01 thomas err = send_idset(pack->privsep_child->ibuf, drop);
1668 ec2b23c5 2022-07-01 thomas if (err)
1669 ec2b23c5 2022-07-01 thomas goto done;
1670 ec2b23c5 2022-07-01 thomas err = send_idset(pack->privsep_child->ibuf, skip);
1671 ec2b23c5 2022-07-01 thomas if (err)
1672 ec2b23c5 2022-07-01 thomas goto done;
1673 ec2b23c5 2022-07-01 thomas err = got_repo_pin_pack(repo, packidx, pack);
1674 ec2b23c5 2022-07-01 thomas if (err)
1675 ec2b23c5 2022-07-01 thomas goto done;
1676 ec2b23c5 2022-07-01 thomas }
1677 ec2b23c5 2022-07-01 thomas }
1678 ec2b23c5 2022-07-01 thomas
1679 5fe7b5c1 2022-04-16 thomas got_object_commit_close(commit);
1680 5fe7b5c1 2022-04-16 thomas commit = NULL;
1681 ec2b23c5 2022-07-01 thomas
1682 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1683 ec2b23c5 2022-07-01 thomas qid = NULL;
1684 e6bcace5 2021-06-22 stsp }
1685 5fe7b5c1 2022-04-16 thomas done:
1686 ec2b23c5 2022-07-01 thomas if (pack) {
1687 ec2b23c5 2022-07-01 thomas const struct got_error *pack_err;
1688 ec2b23c5 2022-07-01 thomas pack_err = got_privsep_send_painting_commits_done(
1689 ec2b23c5 2022-07-01 thomas pack->privsep_child->ibuf);
1690 ec2b23c5 2022-07-01 thomas if (err == NULL)
1691 ec2b23c5 2022-07-01 thomas err = pack_err;
1692 ec2b23c5 2022-07-01 thomas }
1693 e9371be6 2022-04-16 thomas if (commit)
1694 e9371be6 2022-04-16 thomas got_object_commit_close(commit);
1695 ec2b23c5 2022-07-01 thomas got_object_qid_free(qid);
1696 ec2b23c5 2022-07-01 thomas got_repo_unpin_pack(repo);
1697 e9371be6 2022-04-16 thomas return err;
1698 e9371be6 2022-04-16 thomas }
1699 e9371be6 2022-04-16 thomas
1700 e9371be6 2022-04-16 thomas static const struct got_error *
1701 e9371be6 2022-04-16 thomas findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1702 e9371be6 2022-04-16 thomas struct got_object_id **head, int nhead,
1703 e9371be6 2022-04-16 thomas struct got_object_id **tail, int ntail,
1704 e9371be6 2022-04-16 thomas struct got_repository *repo,
1705 e9371be6 2022-04-16 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1706 e9371be6 2022-04-16 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1707 e9371be6 2022-04-16 thomas {
1708 e9371be6 2022-04-16 thomas const struct got_error *err = NULL;
1709 e9371be6 2022-04-16 thomas struct got_object_id_queue ids;
1710 5fe7b5c1 2022-04-16 thomas struct got_object_idset *keep, *drop, *skip = NULL;
1711 e9371be6 2022-04-16 thomas int i, nkeep;
1712 e9371be6 2022-04-16 thomas
1713 e9371be6 2022-04-16 thomas STAILQ_INIT(&ids);
1714 e9371be6 2022-04-16 thomas *res = NULL;
1715 e9371be6 2022-04-16 thomas *nres = 0;
1716 e9371be6 2022-04-16 thomas *ncolored = 0;
1717 e9371be6 2022-04-16 thomas
1718 e9371be6 2022-04-16 thomas keep = got_object_idset_alloc();
1719 e9371be6 2022-04-16 thomas if (keep == NULL)
1720 e9371be6 2022-04-16 thomas return got_error_from_errno("got_object_idset_alloc");
1721 e9371be6 2022-04-16 thomas
1722 e9371be6 2022-04-16 thomas drop = got_object_idset_alloc();
1723 e9371be6 2022-04-16 thomas if (drop == NULL) {
1724 e9371be6 2022-04-16 thomas err = got_error_from_errno("got_object_idset_alloc");
1725 e9371be6 2022-04-16 thomas goto done;
1726 e9371be6 2022-04-16 thomas }
1727 e9371be6 2022-04-16 thomas
1728 5fe7b5c1 2022-04-16 thomas skip = got_object_idset_alloc();
1729 5fe7b5c1 2022-04-16 thomas if (skip == NULL) {
1730 5fe7b5c1 2022-04-16 thomas err = got_error_from_errno("got_object_idset_alloc");
1731 5fe7b5c1 2022-04-16 thomas goto done;
1732 5fe7b5c1 2022-04-16 thomas }
1733 5fe7b5c1 2022-04-16 thomas
1734 e9371be6 2022-04-16 thomas for (i = 0; i < nhead; i++) {
1735 e9371be6 2022-04-16 thomas struct got_object_id *id = head[i];
1736 e9371be6 2022-04-16 thomas if (id == NULL)
1737 e9371be6 2022-04-16 thomas continue;
1738 fc457d24 2022-04-16 thomas err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1739 e9371be6 2022-04-16 thomas if (err)
1740 e9371be6 2022-04-16 thomas goto done;
1741 e9371be6 2022-04-16 thomas }
1742 e9371be6 2022-04-16 thomas
1743 e9371be6 2022-04-16 thomas for (i = 0; i < ntail; i++) {
1744 e9371be6 2022-04-16 thomas struct got_object_id *id = tail[i];
1745 e9371be6 2022-04-16 thomas if (id == NULL)
1746 e9371be6 2022-04-16 thomas continue;
1747 e9371be6 2022-04-16 thomas err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1748 e9371be6 2022-04-16 thomas if (err)
1749 e9371be6 2022-04-16 thomas goto done;
1750 e9371be6 2022-04-16 thomas }
1751 e9371be6 2022-04-16 thomas
1752 5fe7b5c1 2022-04-16 thomas err = paint_commits(ncolored, &ids, nhead + ntail,
1753 5fe7b5c1 2022-04-16 thomas keep, drop, skip, repo, progress_cb, progress_arg, rl,
1754 5fe7b5c1 2022-04-16 thomas cancel_cb, cancel_arg);
1755 e9371be6 2022-04-16 thomas if (err)
1756 e9371be6 2022-04-16 thomas goto done;
1757 e9371be6 2022-04-16 thomas
1758 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1759 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1760 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1761 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1762 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1763 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1764 e6bcace5 2021-06-22 stsp goto done;
1765 e6bcace5 2021-06-22 stsp }
1766 5fe7b5c1 2022-04-16 thomas arg.idx = -1;
1767 5fe7b5c1 2022-04-16 thomas arg.skip = skip;
1768 5fe7b5c1 2022-04-16 thomas arg.drop = drop;
1769 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1770 e6bcace5 2021-06-22 stsp if (err) {
1771 e6bcace5 2021-06-22 stsp free(arg.array);
1772 e6bcace5 2021-06-22 stsp goto done;
1773 e6bcace5 2021-06-22 stsp }
1774 e6bcace5 2021-06-22 stsp *res = arg.array;
1775 5fe7b5c1 2022-04-16 thomas *nres = arg.idx + 1;
1776 e6bcace5 2021-06-22 stsp }
1777 e6bcace5 2021-06-22 stsp done:
1778 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1779 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1780 5fe7b5c1 2022-04-16 thomas if (skip)
1781 5fe7b5c1 2022-04-16 thomas got_object_idset_free(skip);
1782 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1783 63915ee5 2022-06-23 thomas return err;
1784 63915ee5 2022-06-23 thomas }
1785 63915ee5 2022-06-23 thomas
1786 63915ee5 2022-06-23 thomas struct load_packed_obj_arg {
1787 63915ee5 2022-06-23 thomas /* output parameters: */
1788 63915ee5 2022-06-23 thomas struct got_object_id *id;
1789 63915ee5 2022-06-23 thomas char *dpath;
1790 63915ee5 2022-06-23 thomas time_t mtime;
1791 63915ee5 2022-06-23 thomas
1792 63915ee5 2022-06-23 thomas /* input parameters: */
1793 63915ee5 2022-06-23 thomas uint32_t seed;
1794 63915ee5 2022-06-23 thomas int want_meta;
1795 63915ee5 2022-06-23 thomas struct got_object_idset *idset;
1796 63915ee5 2022-06-23 thomas struct got_object_idset *idset_exclude;
1797 63915ee5 2022-06-23 thomas int loose_obj_only;
1798 63915ee5 2022-06-23 thomas int *ncolored;
1799 63915ee5 2022-06-23 thomas int *nfound;
1800 63915ee5 2022-06-23 thomas int *ntrees;
1801 63915ee5 2022-06-23 thomas got_pack_progress_cb progress_cb;
1802 63915ee5 2022-06-23 thomas void *progress_arg;
1803 63915ee5 2022-06-23 thomas struct got_ratelimit *rl;
1804 63915ee5 2022-06-23 thomas got_cancel_cb cancel_cb;
1805 63915ee5 2022-06-23 thomas void *cancel_arg;
1806 63915ee5 2022-06-23 thomas };
1807 63915ee5 2022-06-23 thomas
1808 63915ee5 2022-06-23 thomas static const struct got_error *
1809 63915ee5 2022-06-23 thomas load_packed_commit_id(void *arg, time_t mtime, struct got_object_id *id,
1810 63915ee5 2022-06-23 thomas struct got_repository *repo)
1811 63915ee5 2022-06-23 thomas {
1812 63915ee5 2022-06-23 thomas struct load_packed_obj_arg *a = arg;
1813 63915ee5 2022-06-23 thomas
1814 63915ee5 2022-06-23 thomas if (got_object_idset_contains(a->idset, id) ||
1815 63915ee5 2022-06-23 thomas got_object_idset_contains(a->idset_exclude, id))
1816 63915ee5 2022-06-23 thomas return NULL;
1817 63915ee5 2022-06-23 thomas
1818 63915ee5 2022-06-23 thomas return add_object(a->want_meta,
1819 63915ee5 2022-06-23 thomas a->want_meta ? a->idset : a->idset_exclude,
1820 63915ee5 2022-06-23 thomas id, "", GOT_OBJ_TYPE_COMMIT, mtime, a->seed, a->loose_obj_only,
1821 63915ee5 2022-06-23 thomas repo, a->ncolored, a->nfound, a->ntrees,
1822 63915ee5 2022-06-23 thomas a->progress_cb, a->progress_arg, a->rl);
1823 63915ee5 2022-06-23 thomas }
1824 63915ee5 2022-06-23 thomas
1825 63915ee5 2022-06-23 thomas static const struct got_error *
1826 63915ee5 2022-06-23 thomas load_packed_tree_ids(void *arg, struct got_tree_object *tree, time_t mtime,
1827 63915ee5 2022-06-23 thomas struct got_object_id *id, const char *dpath, struct got_repository *repo)
1828 63915ee5 2022-06-23 thomas {
1829 63915ee5 2022-06-23 thomas const struct got_error *err;
1830 63915ee5 2022-06-23 thomas struct load_packed_obj_arg *a = arg;
1831 63915ee5 2022-06-23 thomas const char *relpath;
1832 63915ee5 2022-06-23 thomas
1833 63915ee5 2022-06-23 thomas /*
1834 63915ee5 2022-06-23 thomas * When we receive a tree's ID and path but not the tree itself,
1835 63915ee5 2022-06-23 thomas * this tree object was not found in the pack file. This is the
1836 63915ee5 2022-06-23 thomas * last time we are being called for this optimized traversal.
1837 63915ee5 2022-06-23 thomas * Return from here and switch to loading objects the slow way.
1838 63915ee5 2022-06-23 thomas */
1839 63915ee5 2022-06-23 thomas if (tree == NULL) {
1840 63915ee5 2022-06-23 thomas free(a->id);
1841 63915ee5 2022-06-23 thomas a->id = got_object_id_dup(id);
1842 63915ee5 2022-06-23 thomas if (a->id == NULL) {
1843 63915ee5 2022-06-23 thomas err = got_error_from_errno("got_object_id_dup");
1844 63915ee5 2022-06-23 thomas free(a->dpath);
1845 63915ee5 2022-06-23 thomas a->dpath = NULL;
1846 63915ee5 2022-06-23 thomas return err;
1847 63915ee5 2022-06-23 thomas }
1848 63915ee5 2022-06-23 thomas
1849 63915ee5 2022-06-23 thomas free(a->dpath);
1850 63915ee5 2022-06-23 thomas a->dpath = strdup(dpath);
1851 63915ee5 2022-06-23 thomas if (a->dpath == NULL) {
1852 63915ee5 2022-06-23 thomas err = got_error_from_errno("strdup");
1853 63915ee5 2022-06-23 thomas free(a->id);
1854 63915ee5 2022-06-23 thomas a->id = NULL;
1855 63915ee5 2022-06-23 thomas return err;
1856 63915ee5 2022-06-23 thomas }
1857 63915ee5 2022-06-23 thomas
1858 63915ee5 2022-06-23 thomas a->mtime = mtime;
1859 63915ee5 2022-06-23 thomas return NULL;
1860 63915ee5 2022-06-23 thomas }
1861 63915ee5 2022-06-23 thomas
1862 63915ee5 2022-06-23 thomas if (got_object_idset_contains(a->idset, id) ||
1863 63915ee5 2022-06-23 thomas got_object_idset_contains(a->idset_exclude, id))
1864 63915ee5 2022-06-23 thomas return NULL;
1865 63915ee5 2022-06-23 thomas
1866 63915ee5 2022-06-23 thomas relpath = dpath;
1867 63915ee5 2022-06-23 thomas while (relpath[0] == '/')
1868 63915ee5 2022-06-23 thomas relpath++;
1869 63915ee5 2022-06-23 thomas
1870 63915ee5 2022-06-23 thomas err = add_object(a->want_meta,
1871 63915ee5 2022-06-23 thomas a->want_meta ? a->idset : a->idset_exclude,
1872 63915ee5 2022-06-23 thomas id, relpath, GOT_OBJ_TYPE_TREE, mtime, a->seed,
1873 63915ee5 2022-06-23 thomas a->loose_obj_only, repo, a->ncolored, a->nfound, a->ntrees,
1874 63915ee5 2022-06-23 thomas a->progress_cb, a->progress_arg, a->rl);
1875 63915ee5 2022-06-23 thomas if (err)
1876 63915ee5 2022-06-23 thomas return err;
1877 63915ee5 2022-06-23 thomas
1878 63915ee5 2022-06-23 thomas return load_tree_entries(NULL, a->want_meta, a->idset,
1879 63915ee5 2022-06-23 thomas a->idset_exclude, tree, dpath, mtime, a->seed, repo,
1880 63915ee5 2022-06-23 thomas a->loose_obj_only, a->ncolored, a->nfound, a->ntrees,
1881 63915ee5 2022-06-23 thomas a->progress_cb, a->progress_arg, a->rl,
1882 63915ee5 2022-06-23 thomas a->cancel_cb, a->cancel_arg);
1883 63915ee5 2022-06-23 thomas }
1884 63915ee5 2022-06-23 thomas
1885 63915ee5 2022-06-23 thomas static const struct got_error *
1886 e71f1e62 2022-06-23 thomas load_packed_object_ids(int *found_all_objects,
1887 e71f1e62 2022-06-23 thomas struct got_object_id **ours, int nours,
1888 63915ee5 2022-06-23 thomas struct got_object_id **theirs, int ntheirs,
1889 63915ee5 2022-06-23 thomas int want_meta, uint32_t seed, struct got_object_idset *idset,
1890 63915ee5 2022-06-23 thomas struct got_object_idset *idset_exclude, int loose_obj_only,
1891 63915ee5 2022-06-23 thomas struct got_repository *repo, struct got_packidx *packidx,
1892 63915ee5 2022-06-23 thomas int *ncolored, int *nfound, int *ntrees,
1893 63915ee5 2022-06-23 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1894 63915ee5 2022-06-23 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1895 63915ee5 2022-06-23 thomas {
1896 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1897 63915ee5 2022-06-23 thomas struct load_packed_obj_arg lpa;
1898 63915ee5 2022-06-23 thomas
1899 63915ee5 2022-06-23 thomas memset(&lpa, 0, sizeof(lpa));
1900 63915ee5 2022-06-23 thomas lpa.seed = seed;
1901 63915ee5 2022-06-23 thomas lpa.want_meta = want_meta;
1902 63915ee5 2022-06-23 thomas lpa.idset = idset;
1903 63915ee5 2022-06-23 thomas lpa.idset_exclude = idset_exclude;
1904 63915ee5 2022-06-23 thomas lpa.loose_obj_only = loose_obj_only;
1905 63915ee5 2022-06-23 thomas lpa.ncolored = ncolored;
1906 63915ee5 2022-06-23 thomas lpa.nfound = nfound;
1907 63915ee5 2022-06-23 thomas lpa.ntrees = ntrees;
1908 63915ee5 2022-06-23 thomas lpa.progress_cb = progress_cb;
1909 63915ee5 2022-06-23 thomas lpa.progress_arg = progress_arg;
1910 63915ee5 2022-06-23 thomas lpa.rl = rl;
1911 63915ee5 2022-06-23 thomas lpa.cancel_cb = cancel_cb;
1912 63915ee5 2022-06-23 thomas lpa.cancel_arg = cancel_arg;
1913 63915ee5 2022-06-23 thomas
1914 63915ee5 2022-06-23 thomas /* Attempt to load objects via got-read-pack, as far as possible. */
1915 e71f1e62 2022-06-23 thomas err = got_object_enumerate(found_all_objects, load_packed_commit_id,
1916 63915ee5 2022-06-23 thomas load_packed_tree_ids, &lpa, ours, nours, theirs, ntheirs,
1917 63915ee5 2022-06-23 thomas packidx, repo);
1918 63915ee5 2022-06-23 thomas if (err)
1919 63915ee5 2022-06-23 thomas return err;
1920 63915ee5 2022-06-23 thomas
1921 63915ee5 2022-06-23 thomas if (lpa.id == NULL)
1922 63915ee5 2022-06-23 thomas return NULL;
1923 63915ee5 2022-06-23 thomas
1924 63915ee5 2022-06-23 thomas /*
1925 63915ee5 2022-06-23 thomas * An incomplete tree hierarchy was present in the pack file
1926 e71f1e62 2022-06-23 thomas * and caused loading to be aborted.
1927 63915ee5 2022-06-23 thomas * Continue loading trees the slow way.
1928 63915ee5 2022-06-23 thomas */
1929 63915ee5 2022-06-23 thomas err = load_tree(want_meta, idset, idset_exclude,
1930 63915ee5 2022-06-23 thomas lpa.id, lpa.dpath, lpa.mtime, seed, repo, loose_obj_only,
1931 63915ee5 2022-06-23 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1932 63915ee5 2022-06-23 thomas cancel_cb, cancel_arg);
1933 63915ee5 2022-06-23 thomas free(lpa.id);
1934 63915ee5 2022-06-23 thomas free(lpa.dpath);
1935 e6bcace5 2021-06-22 stsp return err;
1936 e6bcace5 2021-06-22 stsp }
1937 e6bcace5 2021-06-22 stsp
1938 e6bcace5 2021-06-22 stsp static const struct got_error *
1939 63915ee5 2022-06-23 thomas find_pack_for_enumeration(struct got_packidx **best_packidx,
1940 63915ee5 2022-06-23 thomas struct got_object_id **ids, int nids, struct got_repository *repo)
1941 63915ee5 2022-06-23 thomas {
1942 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1943 63915ee5 2022-06-23 thomas struct got_pathlist_entry *pe;
1944 63915ee5 2022-06-23 thomas const char *best_packidx_path = NULL;
1945 63915ee5 2022-06-23 thomas int nobj_max = 0;
1946 63915ee5 2022-06-23 thomas int ncommits_max = 0;
1947 63915ee5 2022-06-23 thomas
1948 63915ee5 2022-06-23 thomas *best_packidx = NULL;
1949 63915ee5 2022-06-23 thomas
1950 63915ee5 2022-06-23 thomas /*
1951 63915ee5 2022-06-23 thomas * Find the largest pack which contains at least some of the
1952 63915ee5 2022-06-23 thomas * commits and tags we are interested in.
1953 63915ee5 2022-06-23 thomas */
1954 63915ee5 2022-06-23 thomas TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1955 63915ee5 2022-06-23 thomas const char *path_packidx = pe->path;
1956 63915ee5 2022-06-23 thomas struct got_packidx *packidx;
1957 63915ee5 2022-06-23 thomas int nobj, i, idx, ncommits = 0;
1958 63915ee5 2022-06-23 thomas
1959 63915ee5 2022-06-23 thomas err = got_repo_get_packidx(&packidx, path_packidx, repo);
1960 63915ee5 2022-06-23 thomas if (err)
1961 63915ee5 2022-06-23 thomas break;
1962 63915ee5 2022-06-23 thomas
1963 63915ee5 2022-06-23 thomas nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1964 63915ee5 2022-06-23 thomas if (nobj <= nobj_max)
1965 63915ee5 2022-06-23 thomas continue;
1966 63915ee5 2022-06-23 thomas
1967 63915ee5 2022-06-23 thomas for (i = 0; i < nids; i++) {
1968 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, ids[i]);
1969 63915ee5 2022-06-23 thomas if (idx != -1)
1970 63915ee5 2022-06-23 thomas ncommits++;
1971 63915ee5 2022-06-23 thomas }
1972 63915ee5 2022-06-23 thomas if (ncommits > ncommits_max) {
1973 63915ee5 2022-06-23 thomas best_packidx_path = path_packidx;
1974 63915ee5 2022-06-23 thomas nobj_max = nobj;
1975 63915ee5 2022-06-23 thomas ncommits_max = ncommits;
1976 63915ee5 2022-06-23 thomas }
1977 63915ee5 2022-06-23 thomas }
1978 63915ee5 2022-06-23 thomas
1979 eee80a61 2022-06-23 thomas if (best_packidx_path && err == NULL) {
1980 63915ee5 2022-06-23 thomas err = got_repo_get_packidx(best_packidx, best_packidx_path,
1981 63915ee5 2022-06-23 thomas repo);
1982 63915ee5 2022-06-23 thomas }
1983 63915ee5 2022-06-23 thomas
1984 63915ee5 2022-06-23 thomas return err;
1985 63915ee5 2022-06-23 thomas }
1986 63915ee5 2022-06-23 thomas
1987 63915ee5 2022-06-23 thomas static const struct got_error *
1988 85220b0e 2022-03-22 thomas load_object_ids(int *ncolored, int *nfound, int *ntrees,
1989 85220b0e 2022-03-22 thomas struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1990 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1991 cc524d36 2022-05-31 thomas uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1992 cc524d36 2022-05-31 thomas void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1993 cc524d36 2022-05-31 thomas void *cancel_arg)
1994 e6bcace5 2021-06-22 stsp {
1995 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1996 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1997 63915ee5 2022-06-23 thomas struct got_packidx *packidx = NULL;
1998 e71f1e62 2022-06-23 thomas int i, nobj = 0, obj_type, found_all_objects = 0;
1999 7afbdbad 2022-05-12 thomas struct got_object_idset *idset_exclude;
2000 7afbdbad 2022-05-12 thomas
2001 7afbdbad 2022-05-12 thomas idset_exclude = got_object_idset_alloc();
2002 7afbdbad 2022-05-12 thomas if (idset_exclude == NULL)
2003 7afbdbad 2022-05-12 thomas return got_error_from_errno("got_object_idset_alloc");
2004 e6bcace5 2021-06-22 stsp
2005 85220b0e 2022-03-22 thomas *ncolored = 0;
2006 85220b0e 2022-03-22 thomas *nfound = 0;
2007 85220b0e 2022-03-22 thomas *ntrees = 0;
2008 85220b0e 2022-03-22 thomas
2009 85220b0e 2022-03-22 thomas err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
2010 85220b0e 2022-03-22 thomas repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2011 ae2b0251 2022-05-12 thomas if (err)
2012 e6bcace5 2021-06-22 stsp goto done;
2013 63915ee5 2022-06-23 thomas
2014 63915ee5 2022-06-23 thomas err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
2015 63915ee5 2022-06-23 thomas if (err)
2016 63915ee5 2022-06-23 thomas goto done;
2017 63915ee5 2022-06-23 thomas if (packidx) {
2018 e71f1e62 2022-06-23 thomas err = load_packed_object_ids(&found_all_objects,
2019 e71f1e62 2022-06-23 thomas theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
2020 e71f1e62 2022-06-23 thomas loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
2021 e71f1e62 2022-06-23 thomas progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2022 63915ee5 2022-06-23 thomas if (err)
2023 63915ee5 2022-06-23 thomas goto done;
2024 63915ee5 2022-06-23 thomas }
2025 13280750 2022-06-13 thomas
2026 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
2027 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
2028 05118f5a 2021-06-22 stsp if (id == NULL)
2029 05118f5a 2021-06-22 stsp continue;
2030 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
2031 05118f5a 2021-06-22 stsp if (err)
2032 05118f5a 2021-06-22 stsp return err;
2033 8eef4276 2022-04-16 thomas if (obj_type == GOT_OBJ_TYPE_COMMIT) {
2034 e71f1e62 2022-06-23 thomas if (!found_all_objects) {
2035 e71f1e62 2022-06-23 thomas err = load_commit(0, idset, idset_exclude,
2036 e71f1e62 2022-06-23 thomas id, repo, seed, loose_obj_only,
2037 e71f1e62 2022-06-23 thomas ncolored, nfound, ntrees,
2038 e71f1e62 2022-06-23 thomas progress_cb, progress_arg, rl,
2039 e71f1e62 2022-06-23 thomas cancel_cb, cancel_arg);
2040 e71f1e62 2022-06-23 thomas if (err)
2041 e71f1e62 2022-06-23 thomas goto done;
2042 e71f1e62 2022-06-23 thomas }
2043 8eef4276 2022-04-16 thomas } else if (obj_type == GOT_OBJ_TYPE_TAG) {
2044 7afbdbad 2022-05-12 thomas err = load_tag(0, idset, idset_exclude, id, repo,
2045 cc524d36 2022-05-31 thomas seed, loose_obj_only, ncolored, nfound, ntrees,
2046 8eef4276 2022-04-16 thomas progress_cb, progress_arg, rl,
2047 8eef4276 2022-04-16 thomas cancel_cb, cancel_arg);
2048 8eef4276 2022-04-16 thomas if (err)
2049 8eef4276 2022-04-16 thomas goto done;
2050 8eef4276 2022-04-16 thomas }
2051 05118f5a 2021-06-22 stsp }
2052 05118f5a 2021-06-22 stsp
2053 e71f1e62 2022-06-23 thomas found_all_objects = 0;
2054 63915ee5 2022-06-23 thomas err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
2055 63915ee5 2022-06-23 thomas if (err)
2056 63915ee5 2022-06-23 thomas goto done;
2057 63915ee5 2022-06-23 thomas if (packidx) {
2058 e71f1e62 2022-06-23 thomas err = load_packed_object_ids(&found_all_objects, ids,
2059 e71f1e62 2022-06-23 thomas nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
2060 e71f1e62 2022-06-23 thomas loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
2061 63915ee5 2022-06-23 thomas progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2062 63915ee5 2022-06-23 thomas if (err)
2063 63915ee5 2022-06-23 thomas goto done;
2064 63915ee5 2022-06-23 thomas }
2065 63915ee5 2022-06-23 thomas
2066 e71f1e62 2022-06-23 thomas if (!found_all_objects) {
2067 e71f1e62 2022-06-23 thomas for (i = 0; i < nobj; i++) {
2068 e71f1e62 2022-06-23 thomas err = load_commit(1, idset, idset_exclude, ids[i],
2069 e71f1e62 2022-06-23 thomas repo, seed, loose_obj_only, ncolored, nfound,
2070 e71f1e62 2022-06-23 thomas ntrees, progress_cb, progress_arg, rl,
2071 e71f1e62 2022-06-23 thomas cancel_cb, cancel_arg);
2072 e71f1e62 2022-06-23 thomas if (err)
2073 e71f1e62 2022-06-23 thomas goto done;
2074 e71f1e62 2022-06-23 thomas }
2075 eca70f98 2021-09-03 stsp }
2076 eca70f98 2021-09-03 stsp
2077 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
2078 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
2079 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m;
2080 05118f5a 2021-06-22 stsp if (id == NULL)
2081 05118f5a 2021-06-22 stsp continue;
2082 f9c2e8e5 2022-02-13 thomas m = got_object_idset_get(idset, id);
2083 f9c2e8e5 2022-02-13 thomas if (m == NULL) {
2084 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
2085 07165b17 2021-07-01 stsp if (err)
2086 07165b17 2021-07-01 stsp goto done;
2087 07165b17 2021-07-01 stsp } else
2088 f9c2e8e5 2022-02-13 thomas obj_type = m->obj_type;
2089 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
2090 05118f5a 2021-06-22 stsp continue;
2091 7afbdbad 2022-05-12 thomas err = load_tag(1, idset, idset_exclude, id, repo,
2092 cc524d36 2022-05-31 thomas seed, loose_obj_only, ncolored, nfound, ntrees,
2093 7afbdbad 2022-05-12 thomas progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2094 05118f5a 2021-06-22 stsp if (err)
2095 e6bcace5 2021-06-22 stsp goto done;
2096 e6bcace5 2021-06-22 stsp }
2097 e6bcace5 2021-06-22 stsp done:
2098 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
2099 e6bcace5 2021-06-22 stsp free(ids[i]);
2100 e6bcace5 2021-06-22 stsp }
2101 e6bcace5 2021-06-22 stsp free(ids);
2102 7afbdbad 2022-05-12 thomas got_object_idset_free(idset_exclude);
2103 e6bcace5 2021-06-22 stsp return err;
2104 e6bcace5 2021-06-22 stsp }
2105 e6bcace5 2021-06-22 stsp
2106 ef20f542 2022-06-26 thomas static const struct got_error *
2107 0c6f49ba 2022-07-01 thomas hwrite(FILE *f, const void *buf, off_t len, SHA1_CTX *ctx)
2108 e6bcace5 2021-06-22 stsp {
2109 e6bcace5 2021-06-22 stsp size_t n;
2110 e6bcace5 2021-06-22 stsp
2111 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
2112 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
2113 e6bcace5 2021-06-22 stsp if (n != len)
2114 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
2115 9249e7e3 2022-05-12 thomas return NULL;
2116 9249e7e3 2022-05-12 thomas }
2117 9249e7e3 2022-05-12 thomas
2118 ef20f542 2022-06-26 thomas static const struct got_error *
2119 9249e7e3 2022-05-12 thomas hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
2120 9249e7e3 2022-05-12 thomas {
2121 9249e7e3 2022-05-12 thomas unsigned char buf[65536];
2122 9249e7e3 2022-05-12 thomas off_t remain = len;
2123 9249e7e3 2022-05-12 thomas size_t n;
2124 9249e7e3 2022-05-12 thomas
2125 9249e7e3 2022-05-12 thomas while (remain > 0) {
2126 9249e7e3 2022-05-12 thomas size_t copylen = MIN(sizeof(buf), remain);
2127 9249e7e3 2022-05-12 thomas n = fread(buf, 1, copylen, fsrc);
2128 9249e7e3 2022-05-12 thomas if (n != copylen)
2129 9249e7e3 2022-05-12 thomas return got_ferror(fsrc, GOT_ERR_IO);
2130 9249e7e3 2022-05-12 thomas SHA1Update(ctx, buf, copylen);
2131 9249e7e3 2022-05-12 thomas n = fwrite(buf, 1, copylen, fdst);
2132 9249e7e3 2022-05-12 thomas if (n != copylen)
2133 9249e7e3 2022-05-12 thomas return got_ferror(fdst, GOT_ERR_IO);
2134 9249e7e3 2022-05-12 thomas remain -= copylen;
2135 9249e7e3 2022-05-12 thomas }
2136 9249e7e3 2022-05-12 thomas
2137 e6bcace5 2021-06-22 stsp return NULL;
2138 e6bcace5 2021-06-22 stsp }
2139 d93dda9a 2022-05-12 thomas
2140 ef20f542 2022-06-26 thomas static const struct got_error *
2141 d93dda9a 2022-05-12 thomas hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
2142 d93dda9a 2022-05-12 thomas FILE *fdst, off_t len, SHA1_CTX *ctx)
2143 d93dda9a 2022-05-12 thomas {
2144 d93dda9a 2022-05-12 thomas size_t n;
2145 e6bcace5 2021-06-22 stsp
2146 d93dda9a 2022-05-12 thomas if (src_offset + len > src_size)
2147 d93dda9a 2022-05-12 thomas return got_error(GOT_ERR_RANGE);
2148 d93dda9a 2022-05-12 thomas
2149 d93dda9a 2022-05-12 thomas SHA1Update(ctx, src + src_offset, len);
2150 d93dda9a 2022-05-12 thomas n = fwrite(src + src_offset, 1, len, fdst);
2151 d93dda9a 2022-05-12 thomas if (n != len)
2152 d93dda9a 2022-05-12 thomas return got_ferror(fdst, GOT_ERR_IO);
2153 d93dda9a 2022-05-12 thomas
2154 d93dda9a 2022-05-12 thomas return NULL;
2155 d93dda9a 2022-05-12 thomas }
2156 d93dda9a 2022-05-12 thomas
2157 e6bcace5 2021-06-22 stsp static void
2158 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
2159 e6bcace5 2021-06-22 stsp {
2160 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
2161 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
2162 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
2163 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
2164 e6bcace5 2021-06-22 stsp }
2165 e6bcace5 2021-06-22 stsp
2166 e6bcace5 2021-06-22 stsp static int
2167 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
2168 e6bcace5 2021-06-22 stsp {
2169 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
2170 e6bcace5 2021-06-22 stsp
2171 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
2172 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
2173 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
2174 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
2175 f6b43367 2022-05-01 thomas if (bhd->mtime < ahd->mtime)
2176 f6b43367 2022-05-01 thomas return -1;
2177 f6b43367 2022-05-01 thomas if (bhd->mtime > ahd->mtime)
2178 f6b43367 2022-05-01 thomas return 1;
2179 f6b43367 2022-05-01 thomas if (bhd < ahd)
2180 f6b43367 2022-05-01 thomas return -1;
2181 f6b43367 2022-05-01 thomas if (bhd > ahd)
2182 f6b43367 2022-05-01 thomas return 1;
2183 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
2184 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
2185 f6b43367 2022-05-01 thomas if (a->mtime < b->mtime)
2186 f6b43367 2022-05-01 thomas return -1;
2187 f6b43367 2022-05-01 thomas if (a->mtime > b->mtime)
2188 f6b43367 2022-05-01 thomas return 1;
2189 f6b43367 2022-05-01 thomas return got_object_id_cmp(&a->id, &b->id);
2190 e6bcace5 2021-06-22 stsp }
2191 e6bcace5 2021-06-22 stsp
2192 f9c2e8e5 2022-02-13 thomas static int
2193 f9c2e8e5 2022-02-13 thomas reuse_write_order_cmp(const void *pa, const void *pb)
2194 f9c2e8e5 2022-02-13 thomas {
2195 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *a, *b;
2196 f9c2e8e5 2022-02-13 thomas
2197 f9c2e8e5 2022-02-13 thomas a = *(struct got_pack_meta **)pa;
2198 f9c2e8e5 2022-02-13 thomas b = *(struct got_pack_meta **)pb;
2199 f9c2e8e5 2022-02-13 thomas
2200 f9c2e8e5 2022-02-13 thomas if (a->reused_delta_offset < b->reused_delta_offset)
2201 f9c2e8e5 2022-02-13 thomas return -1;
2202 f9c2e8e5 2022-02-13 thomas if (a->reused_delta_offset > b->reused_delta_offset)
2203 f9c2e8e5 2022-02-13 thomas return 1;
2204 f9c2e8e5 2022-02-13 thomas return 0;
2205 f9c2e8e5 2022-02-13 thomas }
2206 f9c2e8e5 2022-02-13 thomas
2207 e6bcace5 2021-06-22 stsp static const struct got_error *
2208 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
2209 e6bcace5 2021-06-22 stsp {
2210 e6bcace5 2021-06-22 stsp size_t i;
2211 e6bcace5 2021-06-22 stsp
2212 e6bcace5 2021-06-22 stsp *hdrlen = 0;
2213 e6bcace5 2021-06-22 stsp
2214 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
2215 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
2216 e6bcace5 2021-06-22 stsp len >>= 4;
2217 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
2218 e6bcace5 2021-06-22 stsp if (i >= bufsize)
2219 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
2220 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
2221 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
2222 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
2223 e6bcace5 2021-06-22 stsp }
2224 e6bcace5 2021-06-22 stsp
2225 e6bcace5 2021-06-22 stsp *hdrlen = i;
2226 e6bcace5 2021-06-22 stsp return NULL;
2227 e6bcace5 2021-06-22 stsp }
2228 e6bcace5 2021-06-22 stsp
2229 e6bcace5 2021-06-22 stsp static int
2230 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
2231 e6bcace5 2021-06-22 stsp {
2232 e6bcace5 2021-06-22 stsp int i, j;
2233 e6bcace5 2021-06-22 stsp char rbuf[8];
2234 e6bcace5 2021-06-22 stsp
2235 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
2236 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
2237 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
2238 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
2239 e6bcace5 2021-06-22 stsp }
2240 e6bcace5 2021-06-22 stsp
2241 e6bcace5 2021-06-22 stsp j = 0;
2242 e6bcace5 2021-06-22 stsp while (i > 0)
2243 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
2244 e6bcace5 2021-06-22 stsp return j;
2245 e6bcace5 2021-06-22 stsp }
2246 e6bcace5 2021-06-22 stsp
2247 e6bcace5 2021-06-22 stsp static const struct got_error *
2248 3b6ceab7 2022-01-10 thomas deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
2249 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m)
2250 3b6ceab7 2022-01-10 thomas {
2251 3b6ceab7 2022-01-10 thomas const struct got_error *err;
2252 3b6ceab7 2022-01-10 thomas char buf[32];
2253 3b6ceab7 2022-01-10 thomas int nh;
2254 3b6ceab7 2022-01-10 thomas
2255 f9c2e8e5 2022-02-13 thomas if (m->prev->off != 0) {
2256 3b6ceab7 2022-01-10 thomas err = packhdr(&nh, buf, sizeof(buf),
2257 3b6ceab7 2022-01-10 thomas GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
2258 3b6ceab7 2022-01-10 thomas if (err)
2259 3b6ceab7 2022-01-10 thomas return err;
2260 3b6ceab7 2022-01-10 thomas nh += packoff(buf + nh, m->off - m->prev->off);
2261 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, buf, nh, ctx);
2262 3b6ceab7 2022-01-10 thomas if (err)
2263 3b6ceab7 2022-01-10 thomas return err;
2264 3b6ceab7 2022-01-10 thomas *packfile_size += nh;
2265 3b6ceab7 2022-01-10 thomas } else {
2266 3b6ceab7 2022-01-10 thomas err = packhdr(&nh, buf, sizeof(buf),
2267 3b6ceab7 2022-01-10 thomas GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
2268 3b6ceab7 2022-01-10 thomas if (err)
2269 3b6ceab7 2022-01-10 thomas return err;
2270 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, buf, nh, ctx);
2271 3b6ceab7 2022-01-10 thomas if (err)
2272 3b6ceab7 2022-01-10 thomas return err;
2273 3b6ceab7 2022-01-10 thomas *packfile_size += nh;
2274 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, m->prev->id.sha1,
2275 3b6ceab7 2022-01-10 thomas sizeof(m->prev->id.sha1), ctx);
2276 3b6ceab7 2022-01-10 thomas if (err)
2277 3b6ceab7 2022-01-10 thomas return err;
2278 3b6ceab7 2022-01-10 thomas *packfile_size += sizeof(m->prev->id.sha1);
2279 3b6ceab7 2022-01-10 thomas }
2280 3b6ceab7 2022-01-10 thomas
2281 3b6ceab7 2022-01-10 thomas return NULL;
2282 3b6ceab7 2022-01-10 thomas }
2283 3b6ceab7 2022-01-10 thomas
2284 3b6ceab7 2022-01-10 thomas static const struct got_error *
2285 f9c2e8e5 2022-02-13 thomas write_packed_object(off_t *packfile_size, FILE *packfile,
2286 d93dda9a 2022-05-12 thomas FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
2287 d93dda9a 2022-05-12 thomas struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
2288 d93dda9a 2022-05-12 thomas struct got_repository *repo)
2289 f9c2e8e5 2022-02-13 thomas {
2290 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
2291 f9c2e8e5 2022-02-13 thomas struct got_deflate_checksum csum;
2292 f9c2e8e5 2022-02-13 thomas char buf[32];
2293 f9c2e8e5 2022-02-13 thomas int nh;
2294 f9c2e8e5 2022-02-13 thomas struct got_raw_object *raw = NULL;
2295 f9c2e8e5 2022-02-13 thomas off_t outlen;
2296 f9c2e8e5 2022-02-13 thomas
2297 f9c2e8e5 2022-02-13 thomas csum.output_sha1 = ctx;
2298 f9c2e8e5 2022-02-13 thomas csum.output_crc = NULL;
2299 f9c2e8e5 2022-02-13 thomas
2300 f9c2e8e5 2022-02-13 thomas m->off = ftello(packfile);
2301 f9c2e8e5 2022-02-13 thomas if (m->delta_len == 0) {
2302 f9c2e8e5 2022-02-13 thomas err = got_object_raw_open(&raw, outfd, repo, &m->id);
2303 f9c2e8e5 2022-02-13 thomas if (err)
2304 f9c2e8e5 2022-02-13 thomas goto done;
2305 f9c2e8e5 2022-02-13 thomas err = packhdr(&nh, buf, sizeof(buf),
2306 f9c2e8e5 2022-02-13 thomas m->obj_type, raw->size);
2307 f9c2e8e5 2022-02-13 thomas if (err)
2308 f9c2e8e5 2022-02-13 thomas goto done;
2309 f9c2e8e5 2022-02-13 thomas err = hwrite(packfile, buf, nh, ctx);
2310 f9c2e8e5 2022-02-13 thomas if (err)
2311 f9c2e8e5 2022-02-13 thomas goto done;
2312 f9c2e8e5 2022-02-13 thomas *packfile_size += nh;
2313 f9c2e8e5 2022-02-13 thomas if (raw->f == NULL) {
2314 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file_mmap(&outlen,
2315 f9c2e8e5 2022-02-13 thomas raw->data + raw->hdrlen, 0, raw->size,
2316 f9c2e8e5 2022-02-13 thomas packfile, &csum);
2317 f9c2e8e5 2022-02-13 thomas if (err)
2318 f9c2e8e5 2022-02-13 thomas goto done;
2319 f9c2e8e5 2022-02-13 thomas } else {
2320 f9c2e8e5 2022-02-13 thomas if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
2321 f9c2e8e5 2022-02-13 thomas == -1) {
2322 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
2323 f9c2e8e5 2022-02-13 thomas goto done;
2324 f9c2e8e5 2022-02-13 thomas }
2325 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file(&outlen, raw->f,
2326 f9c2e8e5 2022-02-13 thomas raw->size, packfile, &csum);
2327 f9c2e8e5 2022-02-13 thomas if (err)
2328 f9c2e8e5 2022-02-13 thomas goto done;
2329 f9c2e8e5 2022-02-13 thomas }
2330 f9c2e8e5 2022-02-13 thomas *packfile_size += outlen;
2331 f9c2e8e5 2022-02-13 thomas got_object_raw_close(raw);
2332 f9c2e8e5 2022-02-13 thomas raw = NULL;
2333 f9c2e8e5 2022-02-13 thomas } else if (m->delta_buf) {
2334 f9c2e8e5 2022-02-13 thomas err = deltahdr(packfile_size, ctx, packfile, m);
2335 f9c2e8e5 2022-02-13 thomas if (err)
2336 f9c2e8e5 2022-02-13 thomas goto done;
2337 9249e7e3 2022-05-12 thomas err = hwrite(packfile, m->delta_buf,
2338 9249e7e3 2022-05-12 thomas m->delta_compressed_len, ctx);
2339 f9c2e8e5 2022-02-13 thomas if (err)
2340 f9c2e8e5 2022-02-13 thomas goto done;
2341 9249e7e3 2022-05-12 thomas *packfile_size += m->delta_compressed_len;
2342 f9c2e8e5 2022-02-13 thomas free(m->delta_buf);
2343 f9c2e8e5 2022-02-13 thomas m->delta_buf = NULL;
2344 d93dda9a 2022-05-12 thomas } else if (delta_cache_map) {
2345 d93dda9a 2022-05-12 thomas err = deltahdr(packfile_size, ctx, packfile, m);
2346 d93dda9a 2022-05-12 thomas if (err)
2347 d93dda9a 2022-05-12 thomas goto done;
2348 d93dda9a 2022-05-12 thomas err = hcopy_mmap(delta_cache_map, m->delta_offset,
2349 d93dda9a 2022-05-12 thomas delta_cache_size, packfile, m->delta_compressed_len,
2350 d93dda9a 2022-05-12 thomas ctx);
2351 d93dda9a 2022-05-12 thomas if (err)
2352 d93dda9a 2022-05-12 thomas goto done;
2353 d93dda9a 2022-05-12 thomas *packfile_size += m->delta_compressed_len;
2354 f9c2e8e5 2022-02-13 thomas } else {
2355 f9c2e8e5 2022-02-13 thomas if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
2356 f9c2e8e5 2022-02-13 thomas == -1) {
2357 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
2358 f9c2e8e5 2022-02-13 thomas goto done;
2359 f9c2e8e5 2022-02-13 thomas }
2360 f9c2e8e5 2022-02-13 thomas err = deltahdr(packfile_size, ctx, packfile, m);
2361 f9c2e8e5 2022-02-13 thomas if (err)
2362 f9c2e8e5 2022-02-13 thomas goto done;
2363 9249e7e3 2022-05-12 thomas err = hcopy(delta_cache, packfile,
2364 9249e7e3 2022-05-12 thomas m->delta_compressed_len, ctx);
2365 f9c2e8e5 2022-02-13 thomas if (err)
2366 f9c2e8e5 2022-02-13 thomas goto done;
2367 9249e7e3 2022-05-12 thomas *packfile_size += m->delta_compressed_len;
2368 f9c2e8e5 2022-02-13 thomas }
2369 f9c2e8e5 2022-02-13 thomas done:
2370 f9c2e8e5 2022-02-13 thomas if (raw)
2371 f9c2e8e5 2022-02-13 thomas got_object_raw_close(raw);
2372 f9c2e8e5 2022-02-13 thomas return err;
2373 f9c2e8e5 2022-02-13 thomas }
2374 f9c2e8e5 2022-02-13 thomas
2375 f9c2e8e5 2022-02-13 thomas static const struct got_error *
2376 b79280dd 2021-10-15 thomas genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
2377 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **deltify, int ndeltify,
2378 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **reuse, int nreuse,
2379 85220b0e 2022-03-22 thomas int ncolored, int nfound, int ntrees, int nours,
2380 85220b0e 2022-03-22 thomas struct got_repository *repo,
2381 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
2382 31ba2236 2022-01-05 thomas struct got_ratelimit *rl,
2383 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2384 e6bcace5 2021-06-22 stsp {
2385 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
2386 f9c2e8e5 2022-02-13 thomas int i;
2387 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
2388 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
2389 e4d8ab47 2021-10-15 thomas char buf[32];
2390 e8f02263 2022-01-23 thomas size_t n;
2391 f9c2e8e5 2022-02-13 thomas off_t packfile_size = 0;
2392 ecf9545f 2021-10-15 thomas int outfd = -1;
2393 d93dda9a 2022-05-12 thomas int delta_cache_fd = -1;
2394 d93dda9a 2022-05-12 thomas uint8_t *delta_cache_map = NULL;
2395 d93dda9a 2022-05-12 thomas size_t delta_cache_size = 0;
2396 e6bcace5 2021-06-22 stsp
2397 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
2398 e6bcace5 2021-06-22 stsp
2399 d93dda9a 2022-05-12 thomas #ifndef GOT_PACK_NO_MMAP
2400 d93dda9a 2022-05-12 thomas delta_cache_fd = dup(fileno(delta_cache));
2401 d93dda9a 2022-05-12 thomas if (delta_cache_fd != -1) {
2402 d93dda9a 2022-05-12 thomas struct stat sb;
2403 d93dda9a 2022-05-12 thomas if (fstat(delta_cache_fd, &sb) == -1) {
2404 d93dda9a 2022-05-12 thomas err = got_error_from_errno("fstat");
2405 d93dda9a 2022-05-12 thomas goto done;
2406 d93dda9a 2022-05-12 thomas }
2407 d93dda9a 2022-05-12 thomas if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
2408 d93dda9a 2022-05-12 thomas delta_cache_map = mmap(NULL, sb.st_size,
2409 d93dda9a 2022-05-12 thomas PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
2410 d93dda9a 2022-05-12 thomas if (delta_cache_map == MAP_FAILED) {
2411 d93dda9a 2022-05-12 thomas if (errno != ENOMEM) {
2412 d93dda9a 2022-05-12 thomas err = got_error_from_errno("mmap");
2413 d93dda9a 2022-05-12 thomas goto done;
2414 d93dda9a 2022-05-12 thomas }
2415 d93dda9a 2022-05-12 thomas delta_cache_map = NULL; /* fallback on stdio */
2416 d93dda9a 2022-05-12 thomas } else
2417 d93dda9a 2022-05-12 thomas delta_cache_size = (size_t)sb.st_size;
2418 d93dda9a 2022-05-12 thomas }
2419 d93dda9a 2022-05-12 thomas }
2420 d93dda9a 2022-05-12 thomas #endif
2421 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
2422 e6bcace5 2021-06-22 stsp if (err)
2423 d93dda9a 2022-05-12 thomas goto done;
2424 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
2425 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
2426 e6bcace5 2021-06-22 stsp if (err)
2427 e6bcace5 2021-06-22 stsp goto done;
2428 f9c2e8e5 2022-02-13 thomas putbe32(buf, ndeltify + nreuse);
2429 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
2430 e6bcace5 2021-06-22 stsp if (err)
2431 e6bcace5 2021-06-22 stsp goto done;
2432 f9c2e8e5 2022-02-13 thomas
2433 f9c2e8e5 2022-02-13 thomas qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
2434 f9c2e8e5 2022-02-13 thomas write_order_cmp);
2435 f9c2e8e5 2022-02-13 thomas for (i = 0; i < ndeltify; i++) {
2436 31ba2236 2022-01-05 thomas err = report_progress(progress_cb, progress_arg, rl,
2437 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, packfile_size, nours,
2438 85220b0e 2022-03-22 thomas ndeltify + nreuse, ndeltify + nreuse, i);
2439 31ba2236 2022-01-05 thomas if (err)
2440 31ba2236 2022-01-05 thomas goto done;
2441 f9c2e8e5 2022-02-13 thomas m = deltify[i];
2442 f9c2e8e5 2022-02-13 thomas err = write_packed_object(&packfile_size, packfile,
2443 d93dda9a 2022-05-12 thomas delta_cache, delta_cache_map, delta_cache_size,
2444 d93dda9a 2022-05-12 thomas m, &outfd, &ctx, repo);
2445 f9c2e8e5 2022-02-13 thomas if (err)
2446 f9c2e8e5 2022-02-13 thomas goto done;
2447 e6bcace5 2021-06-22 stsp }
2448 f9c2e8e5 2022-02-13 thomas
2449 f9c2e8e5 2022-02-13 thomas qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
2450 f9c2e8e5 2022-02-13 thomas reuse_write_order_cmp);
2451 f9c2e8e5 2022-02-13 thomas for (i = 0; i < nreuse; i++) {
2452 f9c2e8e5 2022-02-13 thomas err = report_progress(progress_cb, progress_arg, rl,
2453 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, packfile_size, nours,
2454 85220b0e 2022-03-22 thomas ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
2455 f9c2e8e5 2022-02-13 thomas if (err)
2456 f9c2e8e5 2022-02-13 thomas goto done;
2457 f9c2e8e5 2022-02-13 thomas m = reuse[i];
2458 f9c2e8e5 2022-02-13 thomas err = write_packed_object(&packfile_size, packfile,
2459 d93dda9a 2022-05-12 thomas delta_cache, delta_cache_map, delta_cache_size,
2460 d93dda9a 2022-05-12 thomas m, &outfd, &ctx, repo);
2461 f9c2e8e5 2022-02-13 thomas if (err)
2462 f9c2e8e5 2022-02-13 thomas goto done;
2463 f9c2e8e5 2022-02-13 thomas }
2464 f9c2e8e5 2022-02-13 thomas
2465 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
2466 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
2467 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
2468 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
2469 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
2470 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
2471 31ba2236 2022-01-05 thomas if (progress_cb) {
2472 85220b0e 2022-03-22 thomas err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2473 85220b0e 2022-03-22 thomas packfile_size, nours, ndeltify + nreuse,
2474 85220b0e 2022-03-22 thomas ndeltify + nreuse, ndeltify + nreuse);
2475 31ba2236 2022-01-05 thomas if (err)
2476 31ba2236 2022-01-05 thomas goto done;
2477 31ba2236 2022-01-05 thomas }
2478 e6bcace5 2021-06-22 stsp done:
2479 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
2480 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
2481 d93dda9a 2022-05-12 thomas if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
2482 d93dda9a 2022-05-12 thomas err = got_error_from_errno("munmap");
2483 d93dda9a 2022-05-12 thomas if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2484 d93dda9a 2022-05-12 thomas err = got_error_from_errno("close");
2485 e6bcace5 2021-06-22 stsp return err;
2486 f9c2e8e5 2022-02-13 thomas }
2487 f9c2e8e5 2022-02-13 thomas
2488 f9c2e8e5 2022-02-13 thomas static const struct got_error *
2489 f9c2e8e5 2022-02-13 thomas add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
2490 f9c2e8e5 2022-02-13 thomas {
2491 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m = data;
2492 f9c2e8e5 2022-02-13 thomas struct got_pack_metavec *v = arg;
2493 f291ef1f 2022-05-12 thomas
2494 6c0788af 2022-05-31 thomas if (m->reused_delta_offset != 0)
2495 f291ef1f 2022-05-12 thomas return NULL;
2496 f9c2e8e5 2022-02-13 thomas
2497 f9c2e8e5 2022-02-13 thomas return add_meta(m, v);
2498 f9c2e8e5 2022-02-13 thomas }
2499 f9c2e8e5 2022-02-13 thomas
2500 e6bcace5 2021-06-22 stsp const struct got_error *
2501 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
2502 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
2503 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
2504 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
2505 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
2506 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2507 e6bcace5 2021-06-22 stsp {
2508 e6bcace5 2021-06-22 stsp const struct got_error *err;
2509 f9c2e8e5 2022-02-13 thomas int delta_cache_fd = -1;
2510 b79280dd 2021-10-15 thomas FILE *delta_cache = NULL;
2511 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset;
2512 31ba2236 2022-01-05 thomas struct got_ratelimit rl;
2513 f9c2e8e5 2022-02-13 thomas struct got_pack_metavec deltify, reuse;
2514 85220b0e 2022-03-22 thomas int ncolored = 0, nfound = 0, ntrees = 0;
2515 f291ef1f 2022-05-12 thomas size_t ndeltify;
2516 cc524d36 2022-05-31 thomas uint32_t seed;
2517 e6bcace5 2021-06-22 stsp
2518 cc524d36 2022-05-31 thomas seed = arc4random();
2519 cc524d36 2022-05-31 thomas
2520 f9c2e8e5 2022-02-13 thomas memset(&deltify, 0, sizeof(deltify));
2521 f9c2e8e5 2022-02-13 thomas memset(&reuse, 0, sizeof(reuse));
2522 f9c2e8e5 2022-02-13 thomas
2523 31ba2236 2022-01-05 thomas got_ratelimit_init(&rl, 0, 500);
2524 31ba2236 2022-01-05 thomas
2525 f9c2e8e5 2022-02-13 thomas idset = got_object_idset_alloc();
2526 f9c2e8e5 2022-02-13 thomas if (idset == NULL)
2527 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("got_object_idset_alloc");
2528 f9c2e8e5 2022-02-13 thomas
2529 85220b0e 2022-03-22 thomas err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
2530 cc524d36 2022-05-31 thomas ntheirs, ours, nours, repo, seed, loose_obj_only,
2531 85220b0e 2022-03-22 thomas progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
2532 e6bcace5 2021-06-22 stsp if (err)
2533 20c6bdb6 2022-05-19 thomas goto done;
2534 e6bcace5 2021-06-22 stsp
2535 f9a643e8 2022-02-13 thomas if (progress_cb) {
2536 85220b0e 2022-03-22 thomas err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2537 85220b0e 2022-03-22 thomas 0L, nours, got_object_idset_num_elements(idset), 0, 0);
2538 f9a643e8 2022-02-13 thomas if (err)
2539 f9a643e8 2022-02-13 thomas goto done;
2540 f9a643e8 2022-02-13 thomas }
2541 f9a643e8 2022-02-13 thomas
2542 f9c2e8e5 2022-02-13 thomas if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
2543 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
2544 05118f5a 2021-06-22 stsp goto done;
2545 05118f5a 2021-06-22 stsp }
2546 b79280dd 2021-10-15 thomas
2547 f9c2e8e5 2022-02-13 thomas delta_cache_fd = got_opentempfd();
2548 f9c2e8e5 2022-02-13 thomas if (delta_cache_fd == -1) {
2549 b79280dd 2021-10-15 thomas err = got_error_from_errno("got_opentemp");
2550 b79280dd 2021-10-15 thomas goto done;
2551 b79280dd 2021-10-15 thomas }
2552 b79280dd 2021-10-15 thomas
2553 f9c2e8e5 2022-02-13 thomas reuse.metasz = 64;
2554 f9c2e8e5 2022-02-13 thomas reuse.meta = calloc(reuse.metasz,
2555 f9c2e8e5 2022-02-13 thomas sizeof(struct got_pack_meta *));
2556 f9c2e8e5 2022-02-13 thomas if (reuse.meta == NULL) {
2557 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("calloc");
2558 f9c2e8e5 2022-02-13 thomas goto done;
2559 f9c2e8e5 2022-02-13 thomas }
2560 f9c2e8e5 2022-02-13 thomas
2561 85220b0e 2022-03-22 thomas err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
2562 85220b0e 2022-03-22 thomas ntrees, nours, repo, progress_cb, progress_arg, &rl,
2563 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
2564 f9c2e8e5 2022-02-13 thomas if (err)
2565 f9c2e8e5 2022-02-13 thomas goto done;
2566 f9c2e8e5 2022-02-13 thomas
2567 f9c2e8e5 2022-02-13 thomas delta_cache = fdopen(delta_cache_fd, "a+");
2568 f9c2e8e5 2022-02-13 thomas if (delta_cache == NULL) {
2569 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fdopen");
2570 f9c2e8e5 2022-02-13 thomas goto done;
2571 f9c2e8e5 2022-02-13 thomas }
2572 f9c2e8e5 2022-02-13 thomas delta_cache_fd = -1;
2573 f9c2e8e5 2022-02-13 thomas
2574 f9c2e8e5 2022-02-13 thomas if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
2575 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
2576 f9c2e8e5 2022-02-13 thomas goto done;
2577 f9c2e8e5 2022-02-13 thomas }
2578 f9c2e8e5 2022-02-13 thomas
2579 f291ef1f 2022-05-12 thomas ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
2580 f291ef1f 2022-05-12 thomas if (ndeltify > 0) {
2581 f291ef1f 2022-05-12 thomas deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
2582 f291ef1f 2022-05-12 thomas if (deltify.meta == NULL) {
2583 f291ef1f 2022-05-12 thomas err = got_error_from_errno("calloc");
2584 f291ef1f 2022-05-12 thomas goto done;
2585 f291ef1f 2022-05-12 thomas }
2586 f291ef1f 2022-05-12 thomas deltify.metasz = ndeltify;
2587 f9c2e8e5 2022-02-13 thomas
2588 f291ef1f 2022-05-12 thomas err = got_object_idset_for_each(idset, add_meta_idset_cb,
2589 f291ef1f 2022-05-12 thomas &deltify);
2590 f8a36e22 2021-08-26 stsp if (err)
2591 f8a36e22 2021-08-26 stsp goto done;
2592 f291ef1f 2022-05-12 thomas if (deltify.nmeta > 0) {
2593 f291ef1f 2022-05-12 thomas err = pick_deltas(deltify.meta, deltify.nmeta,
2594 f291ef1f 2022-05-12 thomas ncolored, nfound, ntrees, nours, reuse.nmeta,
2595 f291ef1f 2022-05-12 thomas delta_cache, repo, progress_cb, progress_arg, &rl,
2596 f291ef1f 2022-05-12 thomas cancel_cb, cancel_arg);
2597 f291ef1f 2022-05-12 thomas if (err)
2598 f291ef1f 2022-05-12 thomas goto done;
2599 f291ef1f 2022-05-12 thomas }
2600 f8a36e22 2021-08-26 stsp }
2601 e6bcace5 2021-06-22 stsp
2602 9249e7e3 2022-05-12 thomas if (fflush(delta_cache) == EOF) {
2603 9249e7e3 2022-05-12 thomas err = got_error_from_errno("fflush");
2604 9249e7e3 2022-05-12 thomas goto done;
2605 9249e7e3 2022-05-12 thomas }
2606 f9c2e8e5 2022-02-13 thomas err = genpack(packsha1, packfile, delta_cache, deltify.meta,
2607 85220b0e 2022-03-22 thomas deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
2608 85220b0e 2022-03-22 thomas nours, repo, progress_cb, progress_arg, &rl,
2609 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
2610 e6bcace5 2021-06-22 stsp if (err)
2611 e6bcace5 2021-06-22 stsp goto done;
2612 e6bcace5 2021-06-22 stsp done:
2613 f9c2e8e5 2022-02-13 thomas free_nmeta(deltify.meta, deltify.nmeta);
2614 f9c2e8e5 2022-02-13 thomas free_nmeta(reuse.meta, reuse.nmeta);
2615 f9c2e8e5 2022-02-13 thomas got_object_idset_free(idset);
2616 f9c2e8e5 2022-02-13 thomas if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2617 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("close");
2618 b79280dd 2021-10-15 thomas if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2619 b79280dd 2021-10-15 thomas err = got_error_from_errno("fclose");
2620 e6bcace5 2021-06-22 stsp return err;
2621 e6bcace5 2021-06-22 stsp }