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