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 e6bcace5 2021-06-22 stsp
23 08736cf9 2021-06-23 stsp #include <stdint.h>
24 e6bcace5 2021-06-22 stsp #include <stdio.h>
25 e6bcace5 2021-06-22 stsp #include <stdlib.h>
26 e6bcace5 2021-06-22 stsp #include <string.h>
27 31ba2236 2022-01-05 thomas #include <time.h>
28 e6bcace5 2021-06-22 stsp #include <limits.h>
29 e6bcace5 2021-06-22 stsp #include <zlib.h>
30 b347007e 2021-10-15 thomas
31 b347007e 2021-10-15 thomas #if defined(__FreeBSD__)
32 b347007e 2021-10-15 thomas #include <unistd.h>
33 b347007e 2021-10-15 thomas #endif
34 e6bcace5 2021-06-22 stsp
35 e6bcace5 2021-06-22 stsp #include "got_error.h"
36 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
37 e6bcace5 2021-06-22 stsp #include "got_object.h"
38 0be8fa4c 2021-10-15 thomas #include "got_path.h"
39 05118f5a 2021-06-22 stsp #include "got_reference.h"
40 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
41 e4d8ab47 2021-10-15 thomas #include "got_opentemp.h"
42 e6bcace5 2021-06-22 stsp
43 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
44 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
45 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
46 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
47 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
50 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
52 31ba2236 2022-01-05 thomas #include "got_lib_ratelimit.h"
53 e6bcace5 2021-06-22 stsp
54 b79280dd 2021-10-15 thomas #ifndef MIN
55 b79280dd 2021-10-15 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
56 b79280dd 2021-10-15 thomas #endif
57 b79280dd 2021-10-15 thomas
58 e6bcace5 2021-06-22 stsp #ifndef MAX
59 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
60 e6bcace5 2021-06-22 stsp #endif
61 e6bcace5 2021-06-22 stsp
62 e6bcace5 2021-06-22 stsp struct got_pack_meta {
63 e6bcace5 2021-06-22 stsp struct got_object_id id;
64 e6bcace5 2021-06-22 stsp char *path;
65 e6bcace5 2021-06-22 stsp int obj_type;
66 ab6186ae 2021-10-15 thomas off_t size;
67 e6bcace5 2021-06-22 stsp time_t mtime;
68 e6bcace5 2021-06-22 stsp
69 e6bcace5 2021-06-22 stsp /* The best delta we picked */
70 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
71 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
72 3b6ceab7 2022-01-10 thomas unsigned char *delta_buf; /* if not encoded in delta cache file */
73 b79280dd 2021-10-15 thomas off_t delta_offset; /* offset in delta cache file */
74 3b6ceab7 2022-01-10 thomas off_t delta_len; /* encoded delta length */
75 e6bcace5 2021-06-22 stsp int nchain;
76 e6bcace5 2021-06-22 stsp
77 f9c2e8e5 2022-02-13 thomas int have_reused_delta;
78 f9c2e8e5 2022-02-13 thomas off_t reused_delta_offset; /* offset of delta in reused pack file */
79 f9c2e8e5 2022-02-13 thomas struct got_object_id *base_obj_id;
80 f9c2e8e5 2022-02-13 thomas
81 e6bcace5 2021-06-22 stsp /* Only used for delta window */
82 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
83 e6bcace5 2021-06-22 stsp
84 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
85 e6bcace5 2021-06-22 stsp off_t off;
86 e6bcace5 2021-06-22 stsp };
87 e6bcace5 2021-06-22 stsp
88 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
89 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
90 e6bcace5 2021-06-22 stsp int nmeta;
91 e6bcace5 2021-06-22 stsp int metasz;
92 e6bcace5 2021-06-22 stsp };
93 e6bcace5 2021-06-22 stsp
94 e6bcace5 2021-06-22 stsp static const struct got_error *
95 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
96 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
97 e6bcace5 2021-06-22 stsp {
98 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
99 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
100 e6bcace5 2021-06-22 stsp
101 e6bcace5 2021-06-22 stsp *new = NULL;
102 e6bcace5 2021-06-22 stsp
103 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
104 e6bcace5 2021-06-22 stsp if (m == NULL)
105 b7e0a384 2021-10-15 thomas return got_error_from_errno("calloc");
106 e6bcace5 2021-06-22 stsp
107 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
108 e6bcace5 2021-06-22 stsp
109 e6bcace5 2021-06-22 stsp m->path = strdup(path);
110 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
111 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
112 e6bcace5 2021-06-22 stsp free(m);
113 e6bcace5 2021-06-22 stsp return err;
114 e6bcace5 2021-06-22 stsp }
115 e6bcace5 2021-06-22 stsp
116 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
117 e6bcace5 2021-06-22 stsp m->mtime = mtime;
118 e6bcace5 2021-06-22 stsp *new = m;
119 e6bcace5 2021-06-22 stsp return NULL;
120 e6bcace5 2021-06-22 stsp }
121 e6bcace5 2021-06-22 stsp
122 e6bcace5 2021-06-22 stsp static void
123 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
124 e6bcace5 2021-06-22 stsp {
125 e6bcace5 2021-06-22 stsp if (meta == NULL)
126 e6bcace5 2021-06-22 stsp return;
127 e6bcace5 2021-06-22 stsp free(meta->path);
128 e6bcace5 2021-06-22 stsp meta->path = NULL;
129 3b6ceab7 2022-01-10 thomas free(meta->delta_buf);
130 3b6ceab7 2022-01-10 thomas meta->delta_buf = NULL;
131 f9c2e8e5 2022-02-13 thomas free(meta->base_obj_id);
132 f9c2e8e5 2022-02-13 thomas meta->base_obj_id = NULL;
133 e6bcace5 2021-06-22 stsp }
134 e6bcace5 2021-06-22 stsp
135 e6bcace5 2021-06-22 stsp static void
136 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
137 e6bcace5 2021-06-22 stsp {
138 e6bcace5 2021-06-22 stsp int i;
139 e6bcace5 2021-06-22 stsp
140 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
141 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
142 e6bcace5 2021-06-22 stsp free(meta);
143 e6bcace5 2021-06-22 stsp }
144 e6bcace5 2021-06-22 stsp
145 e6bcace5 2021-06-22 stsp static int
146 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
147 e6bcace5 2021-06-22 stsp {
148 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
149 e6bcace5 2021-06-22 stsp int cmp;
150 e6bcace5 2021-06-22 stsp
151 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
152 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
153 e6bcace5 2021-06-22 stsp
154 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
155 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
156 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
157 e6bcace5 2021-06-22 stsp if (cmp != 0)
158 e6bcace5 2021-06-22 stsp return cmp;
159 e6bcace5 2021-06-22 stsp if (a->mtime != b->mtime)
160 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
161 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
162 e6bcace5 2021-06-22 stsp }
163 e6bcace5 2021-06-22 stsp
164 3b6ceab7 2022-01-10 thomas static off_t
165 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
166 e6bcace5 2021-06-22 stsp {
167 3b6ceab7 2022-01-10 thomas int i;
168 3b6ceab7 2022-01-10 thomas off_t size = 32;
169 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
170 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
171 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
172 e6bcace5 2021-06-22 stsp else
173 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
174 e6bcace5 2021-06-22 stsp }
175 e6bcace5 2021-06-22 stsp return size;
176 3b6ceab7 2022-01-10 thomas }
177 3b6ceab7 2022-01-10 thomas
178 3b6ceab7 2022-01-10 thomas static const struct got_error *
179 3b6ceab7 2022-01-10 thomas append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
180 3b6ceab7 2022-01-10 thomas {
181 3b6ceab7 2022-01-10 thomas char *n;
182 3b6ceab7 2022-01-10 thomas
183 3b6ceab7 2022-01-10 thomas if (*len + nseg >= *sz) {
184 3b6ceab7 2022-01-10 thomas while (*len + nseg >= *sz)
185 3b6ceab7 2022-01-10 thomas *sz += *sz / 2;
186 3b6ceab7 2022-01-10 thomas n = realloc(*p, *sz);
187 3b6ceab7 2022-01-10 thomas if (n == NULL)
188 3b6ceab7 2022-01-10 thomas return got_error_from_errno("realloc");
189 3b6ceab7 2022-01-10 thomas *p = n;
190 3b6ceab7 2022-01-10 thomas }
191 3b6ceab7 2022-01-10 thomas memcpy(*p + *len, seg, nseg);
192 3b6ceab7 2022-01-10 thomas *len += nseg;
193 3b6ceab7 2022-01-10 thomas return NULL;
194 3b6ceab7 2022-01-10 thomas }
195 3b6ceab7 2022-01-10 thomas
196 3b6ceab7 2022-01-10 thomas static const struct got_error *
197 3b6ceab7 2022-01-10 thomas encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
198 3b6ceab7 2022-01-10 thomas struct got_delta_instruction *deltas, int ndeltas,
199 3b6ceab7 2022-01-10 thomas off_t delta_size, off_t base_size)
200 3b6ceab7 2022-01-10 thomas {
201 3b6ceab7 2022-01-10 thomas const struct got_error *err;
202 3b6ceab7 2022-01-10 thomas unsigned char buf[16], *bp;
203 3b6ceab7 2022-01-10 thomas int i, j;
204 3b6ceab7 2022-01-10 thomas size_t len = 0;
205 3b6ceab7 2022-01-10 thomas off_t n;
206 3b6ceab7 2022-01-10 thomas struct got_delta_instruction *d;
207 3b6ceab7 2022-01-10 thomas
208 3b6ceab7 2022-01-10 thomas m->delta_buf = malloc(delta_size);
209 3b6ceab7 2022-01-10 thomas if (m->delta_buf == NULL)
210 3b6ceab7 2022-01-10 thomas return got_error_from_errno("calloc");
211 3b6ceab7 2022-01-10 thomas
212 3b6ceab7 2022-01-10 thomas /* base object size */
213 3b6ceab7 2022-01-10 thomas buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
214 3b6ceab7 2022-01-10 thomas n = base_size >> GOT_DELTA_SIZE_SHIFT;
215 3b6ceab7 2022-01-10 thomas for (i = 1; n > 0; i++) {
216 3b6ceab7 2022-01-10 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
217 3b6ceab7 2022-01-10 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
218 3b6ceab7 2022-01-10 thomas n >>= GOT_DELTA_SIZE_SHIFT;
219 3b6ceab7 2022-01-10 thomas }
220 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size, buf, i);
221 3b6ceab7 2022-01-10 thomas if (err)
222 3b6ceab7 2022-01-10 thomas return err;
223 3b6ceab7 2022-01-10 thomas
224 3b6ceab7 2022-01-10 thomas /* target object size */
225 3b6ceab7 2022-01-10 thomas buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
226 3b6ceab7 2022-01-10 thomas n = o->size >> GOT_DELTA_SIZE_SHIFT;
227 3b6ceab7 2022-01-10 thomas for (i = 1; n > 0; i++) {
228 3b6ceab7 2022-01-10 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
229 3b6ceab7 2022-01-10 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
230 3b6ceab7 2022-01-10 thomas n >>= GOT_DELTA_SIZE_SHIFT;
231 3b6ceab7 2022-01-10 thomas }
232 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size, buf, i);
233 3b6ceab7 2022-01-10 thomas if (err)
234 3b6ceab7 2022-01-10 thomas return err;
235 3b6ceab7 2022-01-10 thomas
236 3b6ceab7 2022-01-10 thomas for (j = 0; j < ndeltas; j++) {
237 3b6ceab7 2022-01-10 thomas d = &deltas[j];
238 3b6ceab7 2022-01-10 thomas if (d->copy) {
239 3b6ceab7 2022-01-10 thomas n = d->offset;
240 3b6ceab7 2022-01-10 thomas bp = &buf[1];
241 3b6ceab7 2022-01-10 thomas buf[0] = GOT_DELTA_BASE_COPY;
242 3b6ceab7 2022-01-10 thomas for (i = 0; i < 4; i++) {
243 3b6ceab7 2022-01-10 thomas /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
244 3b6ceab7 2022-01-10 thomas buf[0] |= 1 << i;
245 3b6ceab7 2022-01-10 thomas *bp++ = n & 0xff;
246 3b6ceab7 2022-01-10 thomas n >>= 8;
247 3b6ceab7 2022-01-10 thomas if (n == 0)
248 3b6ceab7 2022-01-10 thomas break;
249 3b6ceab7 2022-01-10 thomas }
250 3b6ceab7 2022-01-10 thomas
251 3b6ceab7 2022-01-10 thomas n = d->len;
252 3b6ceab7 2022-01-10 thomas if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
253 3b6ceab7 2022-01-10 thomas /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
254 3b6ceab7 2022-01-10 thomas for (i = 0; i < 3 && n > 0; i++) {
255 3b6ceab7 2022-01-10 thomas buf[0] |= 1 << (i + 4);
256 3b6ceab7 2022-01-10 thomas *bp++ = n & 0xff;
257 3b6ceab7 2022-01-10 thomas n >>= 8;
258 3b6ceab7 2022-01-10 thomas }
259 3b6ceab7 2022-01-10 thomas }
260 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size,
261 3b6ceab7 2022-01-10 thomas buf, bp - buf);
262 3b6ceab7 2022-01-10 thomas if (err)
263 3b6ceab7 2022-01-10 thomas return err;
264 3b6ceab7 2022-01-10 thomas } else if (o->f == NULL) {
265 3b6ceab7 2022-01-10 thomas n = 0;
266 3b6ceab7 2022-01-10 thomas while (n != d->len) {
267 3b6ceab7 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
268 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size,
269 3b6ceab7 2022-01-10 thomas buf, 1);
270 3b6ceab7 2022-01-10 thomas if (err)
271 3b6ceab7 2022-01-10 thomas return err;
272 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size,
273 3b6ceab7 2022-01-10 thomas o->data + o->hdrlen + d->offset + n,
274 3b6ceab7 2022-01-10 thomas buf[0]);
275 3b6ceab7 2022-01-10 thomas if (err)
276 3b6ceab7 2022-01-10 thomas return err;
277 3b6ceab7 2022-01-10 thomas n += buf[0];
278 3b6ceab7 2022-01-10 thomas }
279 3b6ceab7 2022-01-10 thomas } else {
280 3b6ceab7 2022-01-10 thomas char content[128];
281 3b6ceab7 2022-01-10 thomas size_t r;
282 3b6ceab7 2022-01-10 thomas if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
283 3b6ceab7 2022-01-10 thomas return got_error_from_errno("fseeko");
284 3b6ceab7 2022-01-10 thomas n = 0;
285 3b6ceab7 2022-01-10 thomas while (n != d->len) {
286 3b6ceab7 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
287 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size,
288 3b6ceab7 2022-01-10 thomas buf, 1);
289 3b6ceab7 2022-01-10 thomas if (err)
290 3b6ceab7 2022-01-10 thomas return err;
291 3b6ceab7 2022-01-10 thomas r = fread(content, 1, buf[0], o->f);
292 3b6ceab7 2022-01-10 thomas if (r != buf[0])
293 3b6ceab7 2022-01-10 thomas return got_ferror(o->f, GOT_ERR_IO);
294 3b6ceab7 2022-01-10 thomas err = append(&m->delta_buf, &len, &delta_size,
295 3b6ceab7 2022-01-10 thomas content, buf[0]);
296 3b6ceab7 2022-01-10 thomas if (err)
297 3b6ceab7 2022-01-10 thomas return err;
298 3b6ceab7 2022-01-10 thomas n += buf[0];
299 3b6ceab7 2022-01-10 thomas }
300 3b6ceab7 2022-01-10 thomas }
301 3b6ceab7 2022-01-10 thomas }
302 3b6ceab7 2022-01-10 thomas
303 3b6ceab7 2022-01-10 thomas m->delta_len = len;
304 3b6ceab7 2022-01-10 thomas return NULL;
305 e6bcace5 2021-06-22 stsp }
306 e6bcace5 2021-06-22 stsp
307 b79280dd 2021-10-15 thomas static const struct got_error *
308 b79280dd 2021-10-15 thomas encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
309 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas, int ndeltas,
310 d0772de9 2021-10-15 thomas off_t base_size, FILE *f)
311 d0772de9 2021-10-15 thomas {
312 d0772de9 2021-10-15 thomas unsigned char buf[16], *bp;
313 d0772de9 2021-10-15 thomas int i, j;
314 d0772de9 2021-10-15 thomas off_t n;
315 d0772de9 2021-10-15 thomas size_t w;
316 d0772de9 2021-10-15 thomas struct got_delta_instruction *d;
317 d0772de9 2021-10-15 thomas
318 d0772de9 2021-10-15 thomas /* base object size */
319 d0772de9 2021-10-15 thomas buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
320 d0772de9 2021-10-15 thomas n = base_size >> GOT_DELTA_SIZE_SHIFT;
321 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
322 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
323 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
324 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
325 d0772de9 2021-10-15 thomas }
326 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, i, f);
327 d0772de9 2021-10-15 thomas if (w != i)
328 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
329 d0772de9 2021-10-15 thomas
330 d0772de9 2021-10-15 thomas /* target object size */
331 d0772de9 2021-10-15 thomas buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
332 d0772de9 2021-10-15 thomas n = o->size >> GOT_DELTA_SIZE_SHIFT;
333 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
334 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
335 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
336 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
337 d0772de9 2021-10-15 thomas }
338 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, i, f);
339 d0772de9 2021-10-15 thomas if (w != i)
340 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
341 d0772de9 2021-10-15 thomas
342 d0772de9 2021-10-15 thomas for (j = 0; j < ndeltas; j++) {
343 d0772de9 2021-10-15 thomas d = &deltas[j];
344 d0772de9 2021-10-15 thomas if (d->copy) {
345 d0772de9 2021-10-15 thomas n = d->offset;
346 d0772de9 2021-10-15 thomas bp = &buf[1];
347 d0772de9 2021-10-15 thomas buf[0] = GOT_DELTA_BASE_COPY;
348 d0772de9 2021-10-15 thomas for (i = 0; i < 4; i++) {
349 d0772de9 2021-10-15 thomas /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
350 d0772de9 2021-10-15 thomas buf[0] |= 1 << i;
351 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
352 d0772de9 2021-10-15 thomas n >>= 8;
353 d0772de9 2021-10-15 thomas if (n == 0)
354 d0772de9 2021-10-15 thomas break;
355 d0772de9 2021-10-15 thomas }
356 d0772de9 2021-10-15 thomas
357 d0772de9 2021-10-15 thomas n = d->len;
358 d0772de9 2021-10-15 thomas if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
359 d0772de9 2021-10-15 thomas /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
360 d0772de9 2021-10-15 thomas for (i = 0; i < 3 && n > 0; i++) {
361 d0772de9 2021-10-15 thomas buf[0] |= 1 << (i + 4);
362 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
363 d0772de9 2021-10-15 thomas n >>= 8;
364 d0772de9 2021-10-15 thomas }
365 d0772de9 2021-10-15 thomas }
366 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, bp - buf, f);
367 d0772de9 2021-10-15 thomas if (w != bp - buf)
368 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
369 2b0ae357 2022-01-10 thomas } else if (o->f == NULL) {
370 2b0ae357 2022-01-10 thomas n = 0;
371 2b0ae357 2022-01-10 thomas while (n != d->len) {
372 2b0ae357 2022-01-10 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
373 2b0ae357 2022-01-10 thomas w = fwrite(buf, 1, 1, f);
374 2b0ae357 2022-01-10 thomas if (w != 1)
375 2b0ae357 2022-01-10 thomas return got_ferror(f, GOT_ERR_IO);
376 2b0ae357 2022-01-10 thomas w = fwrite(o->data + o->hdrlen + d->offset + n,
377 2b0ae357 2022-01-10 thomas 1, buf[0], f);
378 2b0ae357 2022-01-10 thomas if (w != buf[0])
379 2b0ae357 2022-01-10 thomas return got_ferror(f, GOT_ERR_IO);
380 2b0ae357 2022-01-10 thomas n += buf[0];
381 2b0ae357 2022-01-10 thomas }
382 d0772de9 2021-10-15 thomas } else {
383 d0772de9 2021-10-15 thomas char content[128];
384 d0772de9 2021-10-15 thomas size_t r;
385 d0772de9 2021-10-15 thomas if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
386 d0772de9 2021-10-15 thomas return got_error_from_errno("fseeko");
387 d0772de9 2021-10-15 thomas n = 0;
388 d0772de9 2021-10-15 thomas while (n != d->len) {
389 d0772de9 2021-10-15 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
390 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, 1, f);
391 d0772de9 2021-10-15 thomas if (w != 1)
392 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
393 d0772de9 2021-10-15 thomas r = fread(content, 1, buf[0], o->f);
394 d0772de9 2021-10-15 thomas if (r != buf[0])
395 d0772de9 2021-10-15 thomas return got_ferror(o->f, GOT_ERR_IO);
396 d0772de9 2021-10-15 thomas w = fwrite(content, 1, buf[0], f);
397 d0772de9 2021-10-15 thomas if (w != buf[0])
398 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
399 d0772de9 2021-10-15 thomas n += buf[0];
400 d0772de9 2021-10-15 thomas }
401 d0772de9 2021-10-15 thomas }
402 d0772de9 2021-10-15 thomas }
403 e6bcace5 2021-06-22 stsp
404 3b6ceab7 2022-01-10 thomas m->delta_len = ftello(f) - m->delta_offset;
405 d0772de9 2021-10-15 thomas return NULL;
406 d0772de9 2021-10-15 thomas }
407 31ba2236 2022-01-05 thomas
408 31ba2236 2022-01-05 thomas static const struct got_error *
409 31ba2236 2022-01-05 thomas report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
410 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
411 85220b0e 2022-03-22 thomas off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
412 85220b0e 2022-03-22 thomas int nobj_written)
413 31ba2236 2022-01-05 thomas {
414 31ba2236 2022-01-05 thomas const struct got_error *err;
415 31ba2236 2022-01-05 thomas int elapsed;
416 31ba2236 2022-01-05 thomas
417 31ba2236 2022-01-05 thomas if (progress_cb == NULL)
418 31ba2236 2022-01-05 thomas return NULL;
419 d0772de9 2021-10-15 thomas
420 31ba2236 2022-01-05 thomas err = got_ratelimit_check(&elapsed, rl);
421 31ba2236 2022-01-05 thomas if (err || !elapsed)
422 31ba2236 2022-01-05 thomas return err;
423 d0772de9 2021-10-15 thomas
424 85220b0e 2022-03-22 thomas return progress_cb(progress_arg, ncolored, nfound, ntrees,
425 85220b0e 2022-03-22 thomas packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
426 31ba2236 2022-01-05 thomas }
427 31ba2236 2022-01-05 thomas
428 e6bcace5 2021-06-22 stsp static const struct got_error *
429 f9c2e8e5 2022-02-13 thomas add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
430 f9c2e8e5 2022-02-13 thomas {
431 f9c2e8e5 2022-02-13 thomas if (v->nmeta == v->metasz){
432 f9c2e8e5 2022-02-13 thomas size_t newsize = 2 * v->metasz;
433 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **new;
434 f9c2e8e5 2022-02-13 thomas new = reallocarray(v->meta, newsize, sizeof(*new));
435 f9c2e8e5 2022-02-13 thomas if (new == NULL)
436 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("reallocarray");
437 f9c2e8e5 2022-02-13 thomas v->meta = new;
438 f9c2e8e5 2022-02-13 thomas v->metasz = newsize;
439 f9c2e8e5 2022-02-13 thomas }
440 f9c2e8e5 2022-02-13 thomas
441 f9c2e8e5 2022-02-13 thomas v->meta[v->nmeta++] = m;
442 f9c2e8e5 2022-02-13 thomas return NULL;
443 f9c2e8e5 2022-02-13 thomas }
444 f9c2e8e5 2022-02-13 thomas
445 f9c2e8e5 2022-02-13 thomas static const struct got_error *
446 f9c2e8e5 2022-02-13 thomas reuse_delta(int idx, struct got_pack_meta *m, struct got_pack_metavec *v,
447 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset, struct got_pack *pack,
448 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx, int delta_cache_fd,
449 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
450 f9c2e8e5 2022-02-13 thomas {
451 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
452 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *base = NULL;
453 f9c2e8e5 2022-02-13 thomas struct got_object_id *base_obj_id = NULL;
454 f9c2e8e5 2022-02-13 thomas off_t delta_len = 0, delta_offset = 0, delta_cache_offset = 0;
455 f9c2e8e5 2022-02-13 thomas uint64_t base_size, result_size;
456 f9c2e8e5 2022-02-13 thomas
457 f9c2e8e5 2022-02-13 thomas if (m->have_reused_delta)
458 f9c2e8e5 2022-02-13 thomas return NULL;
459 f9c2e8e5 2022-02-13 thomas
460 f9c2e8e5 2022-02-13 thomas err = got_object_read_raw_delta(&base_size, &result_size, &delta_len,
461 f9c2e8e5 2022-02-13 thomas &delta_offset, &delta_cache_offset, &base_obj_id, delta_cache_fd,
462 f9c2e8e5 2022-02-13 thomas packidx, idx, &m->id, repo);
463 f9c2e8e5 2022-02-13 thomas if (err)
464 f9c2e8e5 2022-02-13 thomas return err;
465 f9c2e8e5 2022-02-13 thomas
466 f9c2e8e5 2022-02-13 thomas if (delta_offset + delta_len < delta_offset)
467 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_BAD_PACKFILE);
468 f9c2e8e5 2022-02-13 thomas
469 f9c2e8e5 2022-02-13 thomas base = got_object_idset_get(idset, base_obj_id);
470 f9c2e8e5 2022-02-13 thomas if (base == NULL)
471 f9c2e8e5 2022-02-13 thomas goto done;
472 f9c2e8e5 2022-02-13 thomas
473 f9c2e8e5 2022-02-13 thomas m->delta_len = delta_len;
474 f9c2e8e5 2022-02-13 thomas m->delta_offset = delta_cache_offset;
475 f9c2e8e5 2022-02-13 thomas m->prev = base;
476 f9c2e8e5 2022-02-13 thomas m->size = result_size;
477 f9c2e8e5 2022-02-13 thomas m->have_reused_delta = 1;
478 f9c2e8e5 2022-02-13 thomas m->reused_delta_offset = delta_offset;
479 f9c2e8e5 2022-02-13 thomas m->base_obj_id = base_obj_id;
480 f9c2e8e5 2022-02-13 thomas base_obj_id = NULL;
481 f9c2e8e5 2022-02-13 thomas err = add_meta(m, v);
482 f9c2e8e5 2022-02-13 thomas done:
483 f9c2e8e5 2022-02-13 thomas free(base_obj_id);
484 f9c2e8e5 2022-02-13 thomas return err;
485 f9c2e8e5 2022-02-13 thomas }
486 f9c2e8e5 2022-02-13 thomas
487 f9c2e8e5 2022-02-13 thomas static const struct got_error *
488 f9c2e8e5 2022-02-13 thomas find_pack_for_reuse(struct got_packidx **best_packidx,
489 f9c2e8e5 2022-02-13 thomas struct got_repository *repo)
490 f9c2e8e5 2022-02-13 thomas {
491 de47d040 2022-03-22 thomas const struct got_error *err = NULL;
492 f9c2e8e5 2022-02-13 thomas struct got_pathlist_entry *pe;
493 f9c2e8e5 2022-02-13 thomas const char *best_packidx_path = NULL;
494 f9c2e8e5 2022-02-13 thomas int nobj_max = 0;
495 f9c2e8e5 2022-02-13 thomas
496 f9c2e8e5 2022-02-13 thomas *best_packidx = NULL;
497 f9c2e8e5 2022-02-13 thomas
498 de47d040 2022-03-22 thomas TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
499 f9c2e8e5 2022-02-13 thomas const char *path_packidx = pe->path;
500 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx;
501 f9c2e8e5 2022-02-13 thomas int nobj;
502 f9c2e8e5 2022-02-13 thomas
503 f9c2e8e5 2022-02-13 thomas err = got_repo_get_packidx(&packidx, path_packidx, repo);
504 f9c2e8e5 2022-02-13 thomas if (err)
505 f9c2e8e5 2022-02-13 thomas break;
506 f9c2e8e5 2022-02-13 thomas
507 f9c2e8e5 2022-02-13 thomas nobj = be32toh(packidx->hdr.fanout_table[0xff]);
508 f9c2e8e5 2022-02-13 thomas if (nobj > nobj_max) {
509 f9c2e8e5 2022-02-13 thomas best_packidx_path = path_packidx;
510 f9c2e8e5 2022-02-13 thomas nobj_max = nobj;
511 f9c2e8e5 2022-02-13 thomas }
512 f9c2e8e5 2022-02-13 thomas }
513 f9c2e8e5 2022-02-13 thomas
514 f9c2e8e5 2022-02-13 thomas if (best_packidx_path) {
515 f9c2e8e5 2022-02-13 thomas err = got_repo_get_packidx(best_packidx, best_packidx_path,
516 f9c2e8e5 2022-02-13 thomas repo);
517 f9c2e8e5 2022-02-13 thomas }
518 f9c2e8e5 2022-02-13 thomas
519 f9c2e8e5 2022-02-13 thomas return err;
520 f9c2e8e5 2022-02-13 thomas }
521 f9c2e8e5 2022-02-13 thomas
522 f9c2e8e5 2022-02-13 thomas struct search_deltas_arg {
523 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx;
524 f9c2e8e5 2022-02-13 thomas struct got_pack *pack;
525 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset;
526 f9c2e8e5 2022-02-13 thomas struct got_pack_metavec *v;
527 f9c2e8e5 2022-02-13 thomas int delta_cache_fd;
528 f9c2e8e5 2022-02-13 thomas struct got_repository *repo;
529 f9c2e8e5 2022-02-13 thomas got_pack_progress_cb progress_cb;
530 f9c2e8e5 2022-02-13 thomas void *progress_arg;
531 f9c2e8e5 2022-02-13 thomas struct got_ratelimit *rl;
532 f9c2e8e5 2022-02-13 thomas got_cancel_cb cancel_cb;
533 f9c2e8e5 2022-02-13 thomas void *cancel_arg;
534 85220b0e 2022-03-22 thomas int ncolored;
535 85220b0e 2022-03-22 thomas int nfound;
536 85220b0e 2022-03-22 thomas int ntrees;
537 f9c2e8e5 2022-02-13 thomas int ncommits;
538 f9c2e8e5 2022-02-13 thomas };
539 f9c2e8e5 2022-02-13 thomas
540 f9c2e8e5 2022-02-13 thomas static const struct got_error *
541 f9c2e8e5 2022-02-13 thomas search_delta_for_object(struct got_object_id *id, void *data, void *arg)
542 f9c2e8e5 2022-02-13 thomas {
543 f9c2e8e5 2022-02-13 thomas const struct got_error *err;
544 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m = data;
545 f9c2e8e5 2022-02-13 thomas struct search_deltas_arg *a = arg;
546 f9c2e8e5 2022-02-13 thomas int obj_idx;
547 f9c2e8e5 2022-02-13 thomas struct got_object *obj = NULL;
548 f9c2e8e5 2022-02-13 thomas
549 f9c2e8e5 2022-02-13 thomas if (a->cancel_cb) {
550 f9c2e8e5 2022-02-13 thomas err = (*a->cancel_cb)(a->cancel_arg);
551 f9c2e8e5 2022-02-13 thomas if (err)
552 f9c2e8e5 2022-02-13 thomas return err;
553 f9c2e8e5 2022-02-13 thomas }
554 f9c2e8e5 2022-02-13 thomas
555 f9c2e8e5 2022-02-13 thomas if (!got_repo_check_packidx_bloom_filter(a->repo,
556 f9c2e8e5 2022-02-13 thomas a->packidx->path_packidx, id))
557 f9c2e8e5 2022-02-13 thomas return NULL;
558 f9c2e8e5 2022-02-13 thomas
559 f9c2e8e5 2022-02-13 thomas obj_idx = got_packidx_get_object_idx(a->packidx, id);
560 f9c2e8e5 2022-02-13 thomas if (obj_idx == -1)
561 f9c2e8e5 2022-02-13 thomas return NULL;
562 f9c2e8e5 2022-02-13 thomas
563 f9c2e8e5 2022-02-13 thomas /* TODO:
564 f9c2e8e5 2022-02-13 thomas * Opening and closing an object just to check its flags
565 f9c2e8e5 2022-02-13 thomas * is a bit expensive. We could have an imsg which requests
566 f9c2e8e5 2022-02-13 thomas * plain type/size information for an object without doing
567 f9c2e8e5 2022-02-13 thomas * work such as traversing the object's entire delta chain
568 f9c2e8e5 2022-02-13 thomas * to find the base object type, and other such info which
569 f9c2e8e5 2022-02-13 thomas * we don't really need here.
570 f9c2e8e5 2022-02-13 thomas */
571 f9c2e8e5 2022-02-13 thomas err = got_object_open_from_packfile(&obj, &m->id, a->pack,
572 f9c2e8e5 2022-02-13 thomas a->packidx, obj_idx, a->repo);
573 f9c2e8e5 2022-02-13 thomas if (err)
574 f9c2e8e5 2022-02-13 thomas return err;
575 f9c2e8e5 2022-02-13 thomas
576 f9c2e8e5 2022-02-13 thomas if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
577 f9c2e8e5 2022-02-13 thomas reuse_delta(obj_idx, m, a->v, a->idset, a->pack, a->packidx,
578 f9c2e8e5 2022-02-13 thomas a->delta_cache_fd, a->repo);
579 f9c2e8e5 2022-02-13 thomas if (err)
580 f9c2e8e5 2022-02-13 thomas goto done;
581 f9c2e8e5 2022-02-13 thomas err = report_progress(a->progress_cb, a->progress_arg, a->rl,
582 85220b0e 2022-03-22 thomas a->ncolored, a->nfound, a->ntrees, 0L, a->ncommits,
583 85220b0e 2022-03-22 thomas got_object_idset_num_elements(a->idset), a->v->nmeta, 0);
584 f9c2e8e5 2022-02-13 thomas }
585 f9c2e8e5 2022-02-13 thomas done:
586 f9c2e8e5 2022-02-13 thomas got_object_close(obj);
587 f9c2e8e5 2022-02-13 thomas return err;
588 f9c2e8e5 2022-02-13 thomas }
589 f9c2e8e5 2022-02-13 thomas
590 f9c2e8e5 2022-02-13 thomas static const struct got_error *
591 f9c2e8e5 2022-02-13 thomas search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
592 85220b0e 2022-03-22 thomas int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
593 85220b0e 2022-03-22 thomas struct got_repository *repo,
594 f9c2e8e5 2022-02-13 thomas got_pack_progress_cb progress_cb, void *progress_arg,
595 f9c2e8e5 2022-02-13 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
596 f9c2e8e5 2022-02-13 thomas {
597 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
598 f9c2e8e5 2022-02-13 thomas char *path_packfile = NULL;
599 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx;
600 f9c2e8e5 2022-02-13 thomas struct got_pack *pack;
601 f9c2e8e5 2022-02-13 thomas struct search_deltas_arg sda;
602 f9c2e8e5 2022-02-13 thomas
603 f9c2e8e5 2022-02-13 thomas err = find_pack_for_reuse(&packidx, repo);
604 f9c2e8e5 2022-02-13 thomas if (err)
605 f9c2e8e5 2022-02-13 thomas return err;
606 f9c2e8e5 2022-02-13 thomas
607 f9c2e8e5 2022-02-13 thomas if (packidx == NULL)
608 f9c2e8e5 2022-02-13 thomas return NULL;
609 f9c2e8e5 2022-02-13 thomas
610 f9c2e8e5 2022-02-13 thomas err = got_packidx_get_packfile_path(&path_packfile,
611 f9c2e8e5 2022-02-13 thomas packidx->path_packidx);
612 f9c2e8e5 2022-02-13 thomas if (err)
613 f9c2e8e5 2022-02-13 thomas return err;
614 f9c2e8e5 2022-02-13 thomas
615 f9c2e8e5 2022-02-13 thomas pack = got_repo_get_cached_pack(repo, path_packfile);
616 f9c2e8e5 2022-02-13 thomas if (pack == NULL) {
617 f9c2e8e5 2022-02-13 thomas err = got_repo_cache_pack(&pack, repo, path_packfile, packidx);
618 f9c2e8e5 2022-02-13 thomas if (err)
619 f9c2e8e5 2022-02-13 thomas goto done;
620 f9c2e8e5 2022-02-13 thomas }
621 f9c2e8e5 2022-02-13 thomas
622 f9c2e8e5 2022-02-13 thomas sda.packidx = packidx;
623 f9c2e8e5 2022-02-13 thomas sda.pack = pack;
624 f9c2e8e5 2022-02-13 thomas sda.idset = idset;
625 f9c2e8e5 2022-02-13 thomas sda.v = v;
626 f9c2e8e5 2022-02-13 thomas sda.delta_cache_fd = delta_cache_fd;
627 f9c2e8e5 2022-02-13 thomas sda.repo = repo;
628 f9c2e8e5 2022-02-13 thomas sda.progress_cb = progress_cb;
629 f9c2e8e5 2022-02-13 thomas sda.progress_arg = progress_arg;
630 f9c2e8e5 2022-02-13 thomas sda.rl = rl;
631 f9c2e8e5 2022-02-13 thomas sda.cancel_cb = cancel_cb;
632 f9c2e8e5 2022-02-13 thomas sda.cancel_arg = cancel_arg;
633 85220b0e 2022-03-22 thomas sda.ncolored = ncolored;
634 85220b0e 2022-03-22 thomas sda.nfound = nfound;
635 85220b0e 2022-03-22 thomas sda.ntrees = ntrees;
636 f9c2e8e5 2022-02-13 thomas sda.ncommits = ncommits;
637 f9c2e8e5 2022-02-13 thomas err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
638 f9c2e8e5 2022-02-13 thomas done:
639 f9c2e8e5 2022-02-13 thomas free(path_packfile);
640 f9c2e8e5 2022-02-13 thomas return err;
641 f9c2e8e5 2022-02-13 thomas }
642 f9c2e8e5 2022-02-13 thomas
643 f9c2e8e5 2022-02-13 thomas static const struct got_error *
644 85220b0e 2022-03-22 thomas pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
645 85220b0e 2022-03-22 thomas int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
646 85220b0e 2022-03-22 thomas struct got_repository *repo,
647 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
648 31ba2236 2022-01-05 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
649 e6bcace5 2021-06-22 stsp {
650 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
651 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
652 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
653 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
654 3b6ceab7 2022-01-10 thomas int i, j, ndeltas, best_ndeltas;
655 3b6ceab7 2022-01-10 thomas off_t size, best_size;
656 959daf23 2021-10-29 thomas const int max_base_candidates = 3;
657 510885f7 2022-01-10 thomas size_t delta_memsize = 0;
658 510885f7 2022-01-10 thomas const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
659 ecf9545f 2021-10-15 thomas int outfd = -1;
660 e6bcace5 2021-06-22 stsp
661 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
662 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
663 e6bcace5 2021-06-22 stsp if (cancel_cb) {
664 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
665 e6bcace5 2021-06-22 stsp if (err)
666 e6bcace5 2021-06-22 stsp break;
667 e6bcace5 2021-06-22 stsp }
668 31ba2236 2022-01-05 thomas err = report_progress(progress_cb, progress_arg, rl,
669 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
670 85220b0e 2022-03-22 thomas nreused + i, 0);
671 31ba2236 2022-01-05 thomas if (err)
672 31ba2236 2022-01-05 thomas goto done;
673 e6bcace5 2021-06-22 stsp m = meta[i];
674 e6bcace5 2021-06-22 stsp
675 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
676 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
677 e6bcace5 2021-06-22 stsp continue;
678 e6bcace5 2021-06-22 stsp
679 f8bb1d3e 2021-10-17 thomas err = got_object_raw_open(&raw, &outfd, repo, &m->id);
680 e6bcace5 2021-06-22 stsp if (err)
681 e6bcace5 2021-06-22 stsp goto done;
682 ab6186ae 2021-10-15 thomas m->size = raw->size;
683 e6bcace5 2021-06-22 stsp
684 2b0ae357 2022-01-10 thomas if (raw->f == NULL) {
685 2b0ae357 2022-01-10 thomas err = got_deltify_init_mem(&m->dtab, raw->data,
686 2b0ae357 2022-01-10 thomas raw->hdrlen, raw->size + raw->hdrlen);
687 2b0ae357 2022-01-10 thomas } else {
688 2b0ae357 2022-01-10 thomas err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
689 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen);
690 2b0ae357 2022-01-10 thomas }
691 e6bcace5 2021-06-22 stsp if (err)
692 e6bcace5 2021-06-22 stsp goto done;
693 e6bcace5 2021-06-22 stsp
694 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
695 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
696 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
697 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
698 e6bcace5 2021-06-22 stsp n->dtab = NULL;
699 e6bcace5 2021-06-22 stsp }
700 e6bcace5 2021-06-22 stsp
701 b79280dd 2021-10-15 thomas best_size = raw->size;
702 b79280dd 2021-10-15 thomas best_ndeltas = 0;
703 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
704 e6bcace5 2021-06-22 stsp if (cancel_cb) {
705 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
706 e6bcace5 2021-06-22 stsp if (err)
707 e6bcace5 2021-06-22 stsp goto done;
708 e6bcace5 2021-06-22 stsp }
709 e6bcace5 2021-06-22 stsp base = meta[j];
710 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
711 62a05ae9 2021-10-29 thomas if (base->nchain >= 128 ||
712 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
713 e6bcace5 2021-06-22 stsp continue;
714 e6bcace5 2021-06-22 stsp
715 8ab9215c 2021-10-15 thomas err = got_object_raw_open(&base_raw, &outfd, repo,
716 f8bb1d3e 2021-10-17 thomas &base->id);
717 e6bcace5 2021-06-22 stsp if (err)
718 e6bcace5 2021-06-22 stsp goto done;
719 f9c2e8e5 2022-02-13 thomas
720 2b0ae357 2022-01-10 thomas if (raw->f == NULL && base_raw->f == NULL) {
721 2b0ae357 2022-01-10 thomas err = got_deltify_mem_mem(&deltas, &ndeltas,
722 2b0ae357 2022-01-10 thomas raw->data, raw->hdrlen,
723 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
724 2b0ae357 2022-01-10 thomas base->dtab, base_raw->data,
725 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
726 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
727 2b0ae357 2022-01-10 thomas } else if (raw->f == NULL) {
728 2b0ae357 2022-01-10 thomas err = got_deltify_mem_file(&deltas, &ndeltas,
729 2b0ae357 2022-01-10 thomas raw->data, raw->hdrlen,
730 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
731 2b0ae357 2022-01-10 thomas base->dtab, base_raw->f,
732 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
733 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
734 2b0ae357 2022-01-10 thomas } else if (base_raw->f == NULL) {
735 2b0ae357 2022-01-10 thomas err = got_deltify_file_mem(&deltas, &ndeltas,
736 2b0ae357 2022-01-10 thomas raw->f, raw->hdrlen,
737 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
738 2b0ae357 2022-01-10 thomas base->dtab, base_raw->data,
739 2b0ae357 2022-01-10 thomas base_raw->hdrlen,
740 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
741 2b0ae357 2022-01-10 thomas } else {
742 2b0ae357 2022-01-10 thomas err = got_deltify(&deltas, &ndeltas,
743 2b0ae357 2022-01-10 thomas raw->f, raw->hdrlen,
744 2b0ae357 2022-01-10 thomas raw->size + raw->hdrlen,
745 2b0ae357 2022-01-10 thomas base->dtab, base_raw->f, base_raw->hdrlen,
746 2b0ae357 2022-01-10 thomas base_raw->size + base_raw->hdrlen);
747 2b0ae357 2022-01-10 thomas }
748 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
749 e6bcace5 2021-06-22 stsp base_raw = NULL;
750 e6bcace5 2021-06-22 stsp if (err)
751 e6bcace5 2021-06-22 stsp goto done;
752 e6bcace5 2021-06-22 stsp
753 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
754 b79280dd 2021-10-15 thomas if (size + 32 < best_size){
755 e6bcace5 2021-06-22 stsp /*
756 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
757 e6bcace5 2021-06-22 stsp * replace it.
758 e6bcace5 2021-06-22 stsp */
759 b79280dd 2021-10-15 thomas best_size = size;
760 b79280dd 2021-10-15 thomas free(best_deltas);
761 b79280dd 2021-10-15 thomas best_deltas = deltas;
762 b79280dd 2021-10-15 thomas best_ndeltas = ndeltas;
763 b79280dd 2021-10-15 thomas deltas = NULL;
764 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
765 e6bcace5 2021-06-22 stsp m->prev = base;
766 e6bcace5 2021-06-22 stsp m->head = base->head;
767 e6bcace5 2021-06-22 stsp if (m->head == NULL)
768 e6bcace5 2021-06-22 stsp m->head = base;
769 e6bcace5 2021-06-22 stsp } else {
770 e6bcace5 2021-06-22 stsp free(deltas);
771 e6bcace5 2021-06-22 stsp deltas = NULL;
772 e6bcace5 2021-06-22 stsp ndeltas = 0;
773 e6bcace5 2021-06-22 stsp }
774 e6bcace5 2021-06-22 stsp }
775 e6bcace5 2021-06-22 stsp
776 b79280dd 2021-10-15 thomas if (best_ndeltas > 0) {
777 510885f7 2022-01-10 thomas if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
778 510885f7 2022-01-10 thomas delta_memsize + best_size <= max_delta_memsize) {
779 510885f7 2022-01-10 thomas delta_memsize += best_size;
780 3b6ceab7 2022-01-10 thomas err = encode_delta_in_mem(m, raw, best_deltas,
781 3b6ceab7 2022-01-10 thomas best_ndeltas, best_size, m->prev->size);
782 3b6ceab7 2022-01-10 thomas } else {
783 3b6ceab7 2022-01-10 thomas m->delta_offset = ftello(delta_cache);
784 f9c2e8e5 2022-02-13 thomas /*
785 f9c2e8e5 2022-02-13 thomas * TODO:
786 f9c2e8e5 2022-02-13 thomas * Storing compressed delta data in the delta
787 f9c2e8e5 2022-02-13 thomas * cache file would probably be more efficient
788 f9c2e8e5 2022-02-13 thomas * than writing uncompressed delta data here
789 f9c2e8e5 2022-02-13 thomas * and compressing it while writing the pack
790 f9c2e8e5 2022-02-13 thomas * file. This would also allow for reusing
791 f9c2e8e5 2022-02-13 thomas * deltas in their compressed form.
792 f9c2e8e5 2022-02-13 thomas */
793 3b6ceab7 2022-01-10 thomas err = encode_delta(m, raw, best_deltas,
794 3b6ceab7 2022-01-10 thomas best_ndeltas, m->prev->size, delta_cache);
795 3b6ceab7 2022-01-10 thomas }
796 b79280dd 2021-10-15 thomas free(best_deltas);
797 b79280dd 2021-10-15 thomas best_deltas = NULL;
798 b79280dd 2021-10-15 thomas best_ndeltas = 0;
799 b79280dd 2021-10-15 thomas if (err)
800 b79280dd 2021-10-15 thomas goto done;
801 b79280dd 2021-10-15 thomas }
802 b79280dd 2021-10-15 thomas
803 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
804 e6bcace5 2021-06-22 stsp raw = NULL;
805 e6bcace5 2021-06-22 stsp }
806 e6bcace5 2021-06-22 stsp done:
807 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
808 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
809 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
810 e6bcace5 2021-06-22 stsp }
811 e6bcace5 2021-06-22 stsp if (raw)
812 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
813 e6bcace5 2021-06-22 stsp if (base_raw)
814 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
815 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
816 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
817 b79280dd 2021-10-15 thomas free(deltas);
818 b79280dd 2021-10-15 thomas free(best_deltas);
819 e6bcace5 2021-06-22 stsp return err;
820 e6bcace5 2021-06-22 stsp }
821 e6bcace5 2021-06-22 stsp
822 e6bcace5 2021-06-22 stsp static const struct got_error *
823 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
824 05118f5a 2021-06-22 stsp struct got_repository *repo)
825 05118f5a 2021-06-22 stsp {
826 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
827 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
828 05118f5a 2021-06-22 stsp int idx;
829 05118f5a 2021-06-22 stsp
830 05118f5a 2021-06-22 stsp *found = 0;
831 05118f5a 2021-06-22 stsp
832 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
833 05118f5a 2021-06-22 stsp if (err == NULL)
834 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
835 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
836 05118f5a 2021-06-22 stsp err = NULL;
837 05118f5a 2021-06-22 stsp return err;
838 05118f5a 2021-06-22 stsp }
839 07165b17 2021-07-01 stsp
840 07165b17 2021-07-01 stsp static const int obj_types[] = {
841 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_ANY,
842 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_COMMIT,
843 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TREE,
844 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_BLOB,
845 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TAG,
846 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_OFFSET_DELTA,
847 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_REF_DELTA
848 07165b17 2021-07-01 stsp };
849 05118f5a 2021-06-22 stsp
850 05118f5a 2021-06-22 stsp static const struct got_error *
851 f9c2e8e5 2022-02-13 thomas add_object(int want_meta, struct got_object_idset *idset,
852 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
853 05118f5a 2021-06-22 stsp time_t mtime, int loose_obj_only, struct got_repository *repo)
854 e6bcace5 2021-06-22 stsp {
855 e6bcace5 2021-06-22 stsp const struct got_error *err;
856 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m = NULL;
857 e6bcace5 2021-06-22 stsp
858 05118f5a 2021-06-22 stsp if (loose_obj_only) {
859 05118f5a 2021-06-22 stsp int is_packed;
860 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
861 05118f5a 2021-06-22 stsp if (err)
862 05118f5a 2021-06-22 stsp return err;
863 05118f5a 2021-06-22 stsp if (is_packed)
864 05118f5a 2021-06-22 stsp return NULL;
865 05118f5a 2021-06-22 stsp }
866 05118f5a 2021-06-22 stsp
867 f9c2e8e5 2022-02-13 thomas if (want_meta) {
868 f9c2e8e5 2022-02-13 thomas err = alloc_meta(&m, id, path, obj_type, mtime);
869 f9c2e8e5 2022-02-13 thomas if (err)
870 f9c2e8e5 2022-02-13 thomas return err;
871 e6bcace5 2021-06-22 stsp }
872 e6bcace5 2021-06-22 stsp
873 f9c2e8e5 2022-02-13 thomas return got_object_idset_add(idset, id, m);
874 e6bcace5 2021-06-22 stsp }
875 e6bcace5 2021-06-22 stsp
876 e6bcace5 2021-06-22 stsp static const struct got_error *
877 f9c2e8e5 2022-02-13 thomas load_tree_entries(struct got_object_id_queue *ids, int want_meta,
878 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
879 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
880 85220b0e 2022-03-22 thomas int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
881 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
882 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
883 e6bcace5 2021-06-22 stsp {
884 e6bcace5 2021-06-22 stsp const struct got_error *err;
885 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
886 e6bcace5 2021-06-22 stsp char *p = NULL;
887 e6bcace5 2021-06-22 stsp int i;
888 e6bcace5 2021-06-22 stsp
889 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
890 85220b0e 2022-03-22 thomas if (err)
891 85220b0e 2022-03-22 thomas return err;
892 85220b0e 2022-03-22 thomas
893 85220b0e 2022-03-22 thomas (*ntrees)++;
894 85220b0e 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
895 85220b0e 2022-03-22 thomas *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
896 e6bcace5 2021-06-22 stsp if (err)
897 e6bcace5 2021-06-22 stsp return err;
898 e6bcace5 2021-06-22 stsp
899 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
900 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
901 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
902 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
903 e6bcace5 2021-06-22 stsp
904 e6bcace5 2021-06-22 stsp if (cancel_cb) {
905 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
906 e6bcace5 2021-06-22 stsp if (err)
907 e6bcace5 2021-06-22 stsp break;
908 e6bcace5 2021-06-22 stsp }
909 e6bcace5 2021-06-22 stsp
910 08511b5e 2021-09-24 thomas if (got_object_tree_entry_is_submodule(e) ||
911 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
912 e6bcace5 2021-06-22 stsp continue;
913 e6bcace5 2021-06-22 stsp
914 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
915 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
916 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
917 e6bcace5 2021-06-22 stsp break;
918 e6bcace5 2021-06-22 stsp }
919 e6bcace5 2021-06-22 stsp
920 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
921 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
922 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
923 e6bcace5 2021-06-22 stsp if (err)
924 e6bcace5 2021-06-22 stsp break;
925 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
926 08511b5e 2021-09-24 thomas } else if (S_ISREG(mode) || S_ISLNK(mode)) {
927 f9c2e8e5 2022-02-13 thomas err = add_object(want_meta, idset, id, p,
928 f9c2e8e5 2022-02-13 thomas GOT_OBJ_TYPE_BLOB, mtime, loose_obj_only, repo);
929 e6bcace5 2021-06-22 stsp if (err)
930 e6bcace5 2021-06-22 stsp break;
931 85220b0e 2022-03-22 thomas (*nfound)++;
932 85220b0e 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
933 85220b0e 2022-03-22 thomas *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
934 85220b0e 2022-03-22 thomas if (err)
935 85220b0e 2022-03-22 thomas break;
936 e6bcace5 2021-06-22 stsp }
937 e6bcace5 2021-06-22 stsp free(p);
938 e6bcace5 2021-06-22 stsp p = NULL;
939 e6bcace5 2021-06-22 stsp }
940 e6bcace5 2021-06-22 stsp
941 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
942 e6bcace5 2021-06-22 stsp free(p);
943 e6bcace5 2021-06-22 stsp return err;
944 e6bcace5 2021-06-22 stsp }
945 e6bcace5 2021-06-22 stsp
946 e6bcace5 2021-06-22 stsp static const struct got_error *
947 f9c2e8e5 2022-02-13 thomas load_tree(int want_meta, struct got_object_idset *idset,
948 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
949 85220b0e 2022-03-22 thomas struct got_repository *repo, int loose_obj_only,
950 85220b0e 2022-03-22 thomas int *ncolored, int *nfound, int *ntrees,
951 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
952 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
953 e6bcace5 2021-06-22 stsp {
954 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
955 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
956 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
957 e6bcace5 2021-06-22 stsp
958 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
959 e6bcace5 2021-06-22 stsp return NULL;
960 e6bcace5 2021-06-22 stsp
961 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
962 e6bcace5 2021-06-22 stsp if (err)
963 e6bcace5 2021-06-22 stsp return err;
964 e6bcace5 2021-06-22 stsp
965 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
966 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
967 e6bcace5 2021-06-22 stsp
968 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
969 e6bcace5 2021-06-22 stsp if (cancel_cb) {
970 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
971 e6bcace5 2021-06-22 stsp if (err)
972 e6bcace5 2021-06-22 stsp break;
973 e6bcace5 2021-06-22 stsp }
974 e6bcace5 2021-06-22 stsp
975 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
976 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
977 e6bcace5 2021-06-22 stsp
978 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
979 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
980 e6bcace5 2021-06-22 stsp continue;
981 e6bcace5 2021-06-22 stsp }
982 e6bcace5 2021-06-22 stsp
983 f9c2e8e5 2022-02-13 thomas err = add_object(want_meta, idset, qid->id, dpath,
984 f9c2e8e5 2022-02-13 thomas GOT_OBJ_TYPE_TREE, mtime, loose_obj_only, repo);
985 e6bcace5 2021-06-22 stsp if (err) {
986 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
987 e6bcace5 2021-06-22 stsp break;
988 e6bcace5 2021-06-22 stsp }
989 e6bcace5 2021-06-22 stsp
990 85220b0e 2022-03-22 thomas (*nfound)++;
991 85220b0e 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
992 85220b0e 2022-03-22 thomas *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
993 85220b0e 2022-03-22 thomas if (err)
994 85220b0e 2022-03-22 thomas break;
995 85220b0e 2022-03-22 thomas
996 f9c2e8e5 2022-02-13 thomas err = load_tree_entries(&tree_ids, want_meta, idset, qid->id,
997 85220b0e 2022-03-22 thomas dpath, mtime, repo, loose_obj_only, ncolored, nfound,
998 85220b0e 2022-03-22 thomas ntrees, progress_cb, progress_arg, rl,
999 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1000 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1001 e6bcace5 2021-06-22 stsp if (err)
1002 e6bcace5 2021-06-22 stsp break;
1003 e6bcace5 2021-06-22 stsp }
1004 e6bcace5 2021-06-22 stsp
1005 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
1006 e6bcace5 2021-06-22 stsp return err;
1007 e6bcace5 2021-06-22 stsp }
1008 e6bcace5 2021-06-22 stsp
1009 e6bcace5 2021-06-22 stsp static const struct got_error *
1010 f9c2e8e5 2022-02-13 thomas load_commit(int want_meta, struct got_object_idset *idset,
1011 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1012 85220b0e 2022-03-22 thomas int *ncolored, int *nfound, int *ntrees,
1013 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1014 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1015 e6bcace5 2021-06-22 stsp {
1016 e6bcace5 2021-06-22 stsp const struct got_error *err;
1017 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1018 e6bcace5 2021-06-22 stsp
1019 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
1020 e6bcace5 2021-06-22 stsp return NULL;
1021 05118f5a 2021-06-22 stsp
1022 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1023 05118f5a 2021-06-22 stsp int is_packed;
1024 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1025 05118f5a 2021-06-22 stsp if (err)
1026 05118f5a 2021-06-22 stsp return err;
1027 05118f5a 2021-06-22 stsp if (is_packed)
1028 05118f5a 2021-06-22 stsp return NULL;
1029 05118f5a 2021-06-22 stsp }
1030 e6bcace5 2021-06-22 stsp
1031 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1032 e6bcace5 2021-06-22 stsp if (err)
1033 e6bcace5 2021-06-22 stsp return err;
1034 e6bcace5 2021-06-22 stsp
1035 f9c2e8e5 2022-02-13 thomas err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_COMMIT,
1036 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
1037 05118f5a 2021-06-22 stsp loose_obj_only, repo);
1038 e6bcace5 2021-06-22 stsp if (err)
1039 e6bcace5 2021-06-22 stsp goto done;
1040 e6bcace5 2021-06-22 stsp
1041 85220b0e 2022-03-22 thomas (*nfound)++;
1042 85220b0e 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
1043 85220b0e 2022-03-22 thomas *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
1044 85220b0e 2022-03-22 thomas if (err)
1045 85220b0e 2022-03-22 thomas goto done;
1046 85220b0e 2022-03-22 thomas
1047 f9c2e8e5 2022-02-13 thomas err = load_tree(want_meta, idset, got_object_commit_get_tree_id(commit),
1048 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
1049 85220b0e 2022-03-22 thomas repo, loose_obj_only, ncolored, nfound, ntrees,
1050 85220b0e 2022-03-22 thomas progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1051 e6bcace5 2021-06-22 stsp done:
1052 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1053 e6bcace5 2021-06-22 stsp return err;
1054 e6bcace5 2021-06-22 stsp }
1055 e6bcace5 2021-06-22 stsp
1056 05118f5a 2021-06-22 stsp static const struct got_error *
1057 f9c2e8e5 2022-02-13 thomas load_tag(int want_meta, struct got_object_idset *idset,
1058 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
1059 85220b0e 2022-03-22 thomas int *ncolored, int *nfound, int *ntrees,
1060 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1061 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1062 05118f5a 2021-06-22 stsp {
1063 05118f5a 2021-06-22 stsp const struct got_error *err;
1064 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
1065 05118f5a 2021-06-22 stsp
1066 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
1067 05118f5a 2021-06-22 stsp return NULL;
1068 05118f5a 2021-06-22 stsp
1069 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1070 05118f5a 2021-06-22 stsp int is_packed;
1071 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1072 05118f5a 2021-06-22 stsp if (err)
1073 05118f5a 2021-06-22 stsp return err;
1074 05118f5a 2021-06-22 stsp if (is_packed)
1075 05118f5a 2021-06-22 stsp return NULL;
1076 05118f5a 2021-06-22 stsp }
1077 05118f5a 2021-06-22 stsp
1078 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1079 05118f5a 2021-06-22 stsp if (err)
1080 05118f5a 2021-06-22 stsp return err;
1081 05118f5a 2021-06-22 stsp
1082 f9c2e8e5 2022-02-13 thomas err = add_object(want_meta, idset, id, "", GOT_OBJ_TYPE_TAG,
1083 05118f5a 2021-06-22 stsp got_object_tag_get_tagger_time(tag),
1084 05118f5a 2021-06-22 stsp loose_obj_only, repo);
1085 05118f5a 2021-06-22 stsp if (err)
1086 05118f5a 2021-06-22 stsp goto done;
1087 05118f5a 2021-06-22 stsp
1088 85220b0e 2022-03-22 thomas (*nfound)++;
1089 85220b0e 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
1090 85220b0e 2022-03-22 thomas *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
1091 85220b0e 2022-03-22 thomas if (err)
1092 85220b0e 2022-03-22 thomas goto done;
1093 85220b0e 2022-03-22 thomas
1094 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1095 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1096 f9c2e8e5 2022-02-13 thomas err = load_commit(want_meta, idset,
1097 85220b0e 2022-03-22 thomas got_object_tag_get_object_id(tag), repo, loose_obj_only,
1098 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1099 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1100 05118f5a 2021-06-22 stsp break;
1101 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1102 f9c2e8e5 2022-02-13 thomas err = load_tree(want_meta, idset,
1103 f9c2e8e5 2022-02-13 thomas got_object_tag_get_object_id(tag), "",
1104 85220b0e 2022-03-22 thomas got_object_tag_get_tagger_time(tag), repo, loose_obj_only,
1105 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1106 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1107 05118f5a 2021-06-22 stsp break;
1108 05118f5a 2021-06-22 stsp default:
1109 05118f5a 2021-06-22 stsp break;
1110 05118f5a 2021-06-22 stsp }
1111 05118f5a 2021-06-22 stsp
1112 05118f5a 2021-06-22 stsp done:
1113 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1114 05118f5a 2021-06-22 stsp return err;
1115 05118f5a 2021-06-22 stsp }
1116 05118f5a 2021-06-22 stsp
1117 e6bcace5 2021-06-22 stsp enum findtwixt_color {
1118 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
1119 e6bcace5 2021-06-22 stsp COLOR_DROP,
1120 e6bcace5 2021-06-22 stsp COLOR_BLANK,
1121 e6bcace5 2021-06-22 stsp };
1122 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
1123 e6bcace5 2021-06-22 stsp COLOR_KEEP,
1124 e6bcace5 2021-06-22 stsp COLOR_DROP,
1125 e6bcace5 2021-06-22 stsp COLOR_BLANK
1126 e6bcace5 2021-06-22 stsp };
1127 e6bcace5 2021-06-22 stsp
1128 e6bcace5 2021-06-22 stsp static const struct got_error *
1129 e6bcace5 2021-06-22 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1130 e6bcace5 2021-06-22 stsp int color, struct got_repository *repo)
1131 e6bcace5 2021-06-22 stsp {
1132 e6bcace5 2021-06-22 stsp const struct got_error *err;
1133 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1134 e6bcace5 2021-06-22 stsp
1135 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1136 e6bcace5 2021-06-22 stsp if (err)
1137 e6bcace5 2021-06-22 stsp return err;
1138 e6bcace5 2021-06-22 stsp
1139 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
1140 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
1141 e6bcace5 2021-06-22 stsp return NULL;
1142 e6bcace5 2021-06-22 stsp }
1143 e6bcace5 2021-06-22 stsp
1144 e6bcace5 2021-06-22 stsp static const struct got_error *
1145 e6bcace5 2021-06-22 stsp drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
1146 e6bcace5 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo,
1147 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1148 e6bcace5 2021-06-22 stsp {
1149 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1150 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1151 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
1152 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
1153 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1154 e6bcace5 2021-06-22 stsp
1155 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
1156 e6bcace5 2021-06-22 stsp
1157 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1158 e6bcace5 2021-06-22 stsp if (err)
1159 e6bcace5 2021-06-22 stsp return err;
1160 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&ids, qid, entry);
1161 e6bcace5 2021-06-22 stsp
1162 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
1163 e6bcace5 2021-06-22 stsp if (cancel_cb) {
1164 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
1165 e6bcace5 2021-06-22 stsp if (err)
1166 e6bcace5 2021-06-22 stsp break;
1167 e6bcace5 2021-06-22 stsp }
1168 e6bcace5 2021-06-22 stsp
1169 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
1170 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
1171 e6bcace5 2021-06-22 stsp
1172 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id)) {
1173 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1174 e6bcace5 2021-06-22 stsp continue;
1175 e6bcace5 2021-06-22 stsp }
1176 e6bcace5 2021-06-22 stsp
1177 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, qid->id,
1178 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1179 e6bcace5 2021-06-22 stsp if (err) {
1180 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1181 e6bcace5 2021-06-22 stsp break;
1182 e6bcace5 2021-06-22 stsp }
1183 e6bcace5 2021-06-22 stsp
1184 e6bcace5 2021-06-22 stsp if (!got_object_idset_contains(keep, qid->id)) {
1185 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1186 e6bcace5 2021-06-22 stsp continue;
1187 e6bcace5 2021-06-22 stsp }
1188 e6bcace5 2021-06-22 stsp
1189 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
1190 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1191 e6bcace5 2021-06-22 stsp if (err)
1192 e6bcace5 2021-06-22 stsp break;
1193 e6bcace5 2021-06-22 stsp
1194 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
1195 e6bcace5 2021-06-22 stsp if (parents) {
1196 e6bcace5 2021-06-22 stsp err = got_object_id_queue_copy(parents, &ids);
1197 e6bcace5 2021-06-22 stsp if (err) {
1198 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1199 e6bcace5 2021-06-22 stsp break;
1200 e6bcace5 2021-06-22 stsp }
1201 e6bcace5 2021-06-22 stsp }
1202 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1203 e6bcace5 2021-06-22 stsp }
1204 e6bcace5 2021-06-22 stsp
1205 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1206 e6bcace5 2021-06-22 stsp return err;
1207 e6bcace5 2021-06-22 stsp }
1208 e6bcace5 2021-06-22 stsp
1209 e6bcace5 2021-06-22 stsp struct append_id_arg {
1210 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1211 e6bcace5 2021-06-22 stsp int idx;
1212 e6bcace5 2021-06-22 stsp };
1213 e6bcace5 2021-06-22 stsp
1214 e6bcace5 2021-06-22 stsp static const struct got_error *
1215 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1216 e6bcace5 2021-06-22 stsp {
1217 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1218 e6bcace5 2021-06-22 stsp
1219 e6bcace5 2021-06-22 stsp a->array[a->idx] = got_object_id_dup(id);
1220 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1221 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1222 e6bcace5 2021-06-22 stsp
1223 e6bcace5 2021-06-22 stsp a->idx++;
1224 e6bcace5 2021-06-22 stsp return NULL;
1225 e6bcace5 2021-06-22 stsp }
1226 e6bcace5 2021-06-22 stsp
1227 e6bcace5 2021-06-22 stsp static const struct got_error *
1228 85220b0e 2022-03-22 thomas findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1229 e6bcace5 2021-06-22 stsp struct got_object_id **head, int nhead,
1230 e6bcace5 2021-06-22 stsp struct got_object_id **tail, int ntail,
1231 e6bcace5 2021-06-22 stsp struct got_repository *repo,
1232 85220b0e 2022-03-22 thomas got_pack_progress_cb progress_cb, void *progress_arg,
1233 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1234 e6bcace5 2021-06-22 stsp {
1235 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1236 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
1237 e6bcace5 2021-06-22 stsp struct got_object_idset *keep, *drop;
1238 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1239 05118f5a 2021-06-22 stsp int i, ncolor, nkeep, obj_type;
1240 e6bcace5 2021-06-22 stsp
1241 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
1242 e6bcace5 2021-06-22 stsp *res = NULL;
1243 e6bcace5 2021-06-22 stsp *nres = 0;
1244 85220b0e 2022-03-22 thomas *ncolored = 0;
1245 e6bcace5 2021-06-22 stsp
1246 e6bcace5 2021-06-22 stsp keep = got_object_idset_alloc();
1247 e6bcace5 2021-06-22 stsp if (keep == NULL)
1248 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
1249 e6bcace5 2021-06-22 stsp
1250 e6bcace5 2021-06-22 stsp drop = got_object_idset_alloc();
1251 e6bcace5 2021-06-22 stsp if (drop == NULL) {
1252 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_idset_alloc");
1253 e6bcace5 2021-06-22 stsp goto done;
1254 e6bcace5 2021-06-22 stsp }
1255 e6bcace5 2021-06-22 stsp
1256 05118f5a 2021-06-22 stsp for (i = 0; i < nhead; i++) {
1257 05118f5a 2021-06-22 stsp struct got_object_id *id = head[i];
1258 05118f5a 2021-06-22 stsp if (id == NULL)
1259 05118f5a 2021-06-22 stsp continue;
1260 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1261 05118f5a 2021-06-22 stsp if (err)
1262 05118f5a 2021-06-22 stsp return err;
1263 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
1264 05118f5a 2021-06-22 stsp continue;
1265 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1266 05118f5a 2021-06-22 stsp if (err)
1267 05118f5a 2021-06-22 stsp goto done;
1268 e6bcace5 2021-06-22 stsp }
1269 05118f5a 2021-06-22 stsp for (i = 0; i < ntail; i++) {
1270 05118f5a 2021-06-22 stsp struct got_object_id *id = tail[i];
1271 05118f5a 2021-06-22 stsp if (id == NULL)
1272 05118f5a 2021-06-22 stsp continue;
1273 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1274 05118f5a 2021-06-22 stsp if (err)
1275 05118f5a 2021-06-22 stsp return err;
1276 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
1277 05118f5a 2021-06-22 stsp continue;
1278 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1279 05118f5a 2021-06-22 stsp if (err)
1280 05118f5a 2021-06-22 stsp goto done;
1281 e6bcace5 2021-06-22 stsp }
1282 e6bcace5 2021-06-22 stsp
1283 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
1284 e6bcace5 2021-06-22 stsp int qcolor;
1285 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
1286 e6bcace5 2021-06-22 stsp qcolor = *((int *)qid->data);
1287 e6bcace5 2021-06-22 stsp
1288 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id))
1289 e6bcace5 2021-06-22 stsp ncolor = COLOR_DROP;
1290 e6bcace5 2021-06-22 stsp else if (got_object_idset_contains(keep, qid->id))
1291 e6bcace5 2021-06-22 stsp ncolor = COLOR_KEEP;
1292 e6bcace5 2021-06-22 stsp else
1293 e6bcace5 2021-06-22 stsp ncolor = COLOR_BLANK;
1294 85220b0e 2022-03-22 thomas
1295 85220b0e 2022-03-22 thomas (*ncolored)++;
1296 85220b0e 2022-03-22 thomas err = report_progress(progress_cb, progress_arg, rl,
1297 85220b0e 2022-03-22 thomas *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1298 85220b0e 2022-03-22 thomas if (err)
1299 85220b0e 2022-03-22 thomas goto done;
1300 e6bcace5 2021-06-22 stsp
1301 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1302 e6bcace5 2021-06-22 stsp qcolor == COLOR_KEEP)) {
1303 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
1304 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1305 e6bcace5 2021-06-22 stsp continue;
1306 e6bcace5 2021-06-22 stsp }
1307 e6bcace5 2021-06-22 stsp
1308 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1309 e6bcace5 2021-06-22 stsp err = drop_commit(keep, drop, qid->id, repo,
1310 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
1311 e6bcace5 2021-06-22 stsp if (err)
1312 e6bcace5 2021-06-22 stsp goto done;
1313 e6bcace5 2021-06-22 stsp } else if (ncolor == COLOR_BLANK) {
1314 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1315 e6bcace5 2021-06-22 stsp struct got_object_id *id;
1316 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
1317 e6bcace5 2021-06-22 stsp struct got_object_qid *pid;
1318 e6bcace5 2021-06-22 stsp
1319 e6bcace5 2021-06-22 stsp id = got_object_id_dup(qid->id);
1320 e6bcace5 2021-06-22 stsp if (id == NULL) {
1321 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
1322 e6bcace5 2021-06-22 stsp goto done;
1323 e6bcace5 2021-06-22 stsp }
1324 e6bcace5 2021-06-22 stsp if (qcolor == COLOR_KEEP)
1325 07165b17 2021-07-01 stsp err = got_object_idset_add(keep, id,
1326 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1327 e6bcace5 2021-06-22 stsp else
1328 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, id,
1329 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1330 e6bcace5 2021-06-22 stsp if (err) {
1331 e6bcace5 2021-06-22 stsp free(id);
1332 e6bcace5 2021-06-22 stsp goto done;
1333 e6bcace5 2021-06-22 stsp }
1334 e6bcace5 2021-06-22 stsp
1335 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1336 e6bcace5 2021-06-22 stsp if (err) {
1337 e6bcace5 2021-06-22 stsp free(id);
1338 e6bcace5 2021-06-22 stsp goto done;
1339 e6bcace5 2021-06-22 stsp }
1340 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
1341 e6bcace5 2021-06-22 stsp if (parents) {
1342 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parents, entry) {
1343 e6bcace5 2021-06-22 stsp err = queue_commit_id(&ids, pid->id,
1344 e6bcace5 2021-06-22 stsp qcolor, repo);
1345 e6bcace5 2021-06-22 stsp if (err) {
1346 e6bcace5 2021-06-22 stsp free(id);
1347 e6bcace5 2021-06-22 stsp goto done;
1348 e6bcace5 2021-06-22 stsp }
1349 e6bcace5 2021-06-22 stsp }
1350 e6bcace5 2021-06-22 stsp }
1351 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1352 e6bcace5 2021-06-22 stsp commit = NULL;
1353 e6bcace5 2021-06-22 stsp } else {
1354 e6bcace5 2021-06-22 stsp /* should not happen */
1355 e6bcace5 2021-06-22 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
1356 e6bcace5 2021-06-22 stsp "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1357 e6bcace5 2021-06-22 stsp goto done;
1358 e6bcace5 2021-06-22 stsp }
1359 e6bcace5 2021-06-22 stsp
1360 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
1361 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1362 e6bcace5 2021-06-22 stsp }
1363 e6bcace5 2021-06-22 stsp
1364 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1365 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1366 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1367 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1368 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1369 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1370 e6bcace5 2021-06-22 stsp goto done;
1371 e6bcace5 2021-06-22 stsp }
1372 e6bcace5 2021-06-22 stsp arg.idx = 0;
1373 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1374 e6bcace5 2021-06-22 stsp if (err) {
1375 e6bcace5 2021-06-22 stsp free(arg.array);
1376 e6bcace5 2021-06-22 stsp goto done;
1377 e6bcace5 2021-06-22 stsp }
1378 e6bcace5 2021-06-22 stsp *res = arg.array;
1379 e6bcace5 2021-06-22 stsp *nres = nkeep;
1380 e6bcace5 2021-06-22 stsp }
1381 e6bcace5 2021-06-22 stsp done:
1382 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1383 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1384 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1385 e6bcace5 2021-06-22 stsp return err;
1386 e6bcace5 2021-06-22 stsp }
1387 e6bcace5 2021-06-22 stsp
1388 e6bcace5 2021-06-22 stsp static const struct got_error *
1389 85220b0e 2022-03-22 thomas load_object_ids(int *ncolored, int *nfound, int *ntrees,
1390 85220b0e 2022-03-22 thomas struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1391 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1392 85220b0e 2022-03-22 thomas int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1393 85220b0e 2022-03-22 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1394 e6bcace5 2021-06-22 stsp {
1395 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1396 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1397 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
1398 e6bcace5 2021-06-22 stsp
1399 85220b0e 2022-03-22 thomas *ncolored = 0;
1400 85220b0e 2022-03-22 thomas *nfound = 0;
1401 85220b0e 2022-03-22 thomas *ntrees = 0;
1402 85220b0e 2022-03-22 thomas
1403 85220b0e 2022-03-22 thomas err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1404 85220b0e 2022-03-22 thomas repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1405 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
1406 e6bcace5 2021-06-22 stsp goto done;
1407 e6bcace5 2021-06-22 stsp
1408 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1409 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
1410 05118f5a 2021-06-22 stsp if (id == NULL)
1411 05118f5a 2021-06-22 stsp continue;
1412 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1413 05118f5a 2021-06-22 stsp if (err)
1414 05118f5a 2021-06-22 stsp return err;
1415 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
1416 05118f5a 2021-06-22 stsp continue;
1417 85220b0e 2022-03-22 thomas err = load_commit(0, idset, id, repo, loose_obj_only,
1418 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1419 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1420 05118f5a 2021-06-22 stsp if (err)
1421 05118f5a 2021-06-22 stsp goto done;
1422 05118f5a 2021-06-22 stsp }
1423 05118f5a 2021-06-22 stsp
1424 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1425 f4a2ff2d 2021-07-01 stsp struct got_object_id *id = theirs[i];
1426 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m;
1427 05118f5a 2021-06-22 stsp if (id == NULL)
1428 05118f5a 2021-06-22 stsp continue;
1429 f9c2e8e5 2022-02-13 thomas m = got_object_idset_get(idset, id);
1430 f9c2e8e5 2022-02-13 thomas if (m == NULL) {
1431 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1432 07165b17 2021-07-01 stsp if (err)
1433 07165b17 2021-07-01 stsp goto done;
1434 07165b17 2021-07-01 stsp } else
1435 f9c2e8e5 2022-02-13 thomas obj_type = m->obj_type;
1436 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1437 05118f5a 2021-06-22 stsp continue;
1438 85220b0e 2022-03-22 thomas err = load_tag(0, idset, id, repo, loose_obj_only,
1439 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1440 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1441 05118f5a 2021-06-22 stsp if (err)
1442 05118f5a 2021-06-22 stsp goto done;
1443 05118f5a 2021-06-22 stsp }
1444 05118f5a 2021-06-22 stsp
1445 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
1446 85220b0e 2022-03-22 thomas err = load_commit(1, idset, ids[i], repo, loose_obj_only,
1447 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1448 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1449 eca70f98 2021-09-03 stsp if (err)
1450 eca70f98 2021-09-03 stsp goto done;
1451 eca70f98 2021-09-03 stsp }
1452 eca70f98 2021-09-03 stsp
1453 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1454 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1455 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m;
1456 05118f5a 2021-06-22 stsp if (id == NULL)
1457 05118f5a 2021-06-22 stsp continue;
1458 f9c2e8e5 2022-02-13 thomas m = got_object_idset_get(idset, id);
1459 f9c2e8e5 2022-02-13 thomas if (m == NULL) {
1460 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1461 07165b17 2021-07-01 stsp if (err)
1462 07165b17 2021-07-01 stsp goto done;
1463 07165b17 2021-07-01 stsp } else
1464 f9c2e8e5 2022-02-13 thomas obj_type = m->obj_type;
1465 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1466 05118f5a 2021-06-22 stsp continue;
1467 85220b0e 2022-03-22 thomas err = load_tag(1, idset, id, repo, loose_obj_only,
1468 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1469 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1470 05118f5a 2021-06-22 stsp if (err)
1471 e6bcace5 2021-06-22 stsp goto done;
1472 e6bcace5 2021-06-22 stsp }
1473 e6bcace5 2021-06-22 stsp done:
1474 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
1475 e6bcace5 2021-06-22 stsp free(ids[i]);
1476 e6bcace5 2021-06-22 stsp }
1477 e6bcace5 2021-06-22 stsp free(ids);
1478 e6bcace5 2021-06-22 stsp return err;
1479 e6bcace5 2021-06-22 stsp }
1480 e6bcace5 2021-06-22 stsp
1481 e6bcace5 2021-06-22 stsp const struct got_error *
1482 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1483 e6bcace5 2021-06-22 stsp {
1484 e6bcace5 2021-06-22 stsp size_t n;
1485 e6bcace5 2021-06-22 stsp
1486 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1487 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
1488 e6bcace5 2021-06-22 stsp if (n != len)
1489 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
1490 e6bcace5 2021-06-22 stsp return NULL;
1491 e6bcace5 2021-06-22 stsp }
1492 e6bcace5 2021-06-22 stsp
1493 e6bcace5 2021-06-22 stsp static void
1494 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1495 e6bcace5 2021-06-22 stsp {
1496 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1497 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1498 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1499 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1500 e6bcace5 2021-06-22 stsp }
1501 e6bcace5 2021-06-22 stsp
1502 e6bcace5 2021-06-22 stsp static int
1503 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1504 e6bcace5 2021-06-22 stsp {
1505 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1506 e6bcace5 2021-06-22 stsp
1507 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1508 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1509 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1510 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1511 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
1512 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
1513 e6bcace5 2021-06-22 stsp if (ahd != bhd)
1514 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
1515 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1516 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1517 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
1518 e6bcace5 2021-06-22 stsp }
1519 e6bcace5 2021-06-22 stsp
1520 f9c2e8e5 2022-02-13 thomas static int
1521 f9c2e8e5 2022-02-13 thomas reuse_write_order_cmp(const void *pa, const void *pb)
1522 f9c2e8e5 2022-02-13 thomas {
1523 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *a, *b;
1524 f9c2e8e5 2022-02-13 thomas
1525 f9c2e8e5 2022-02-13 thomas a = *(struct got_pack_meta **)pa;
1526 f9c2e8e5 2022-02-13 thomas b = *(struct got_pack_meta **)pb;
1527 f9c2e8e5 2022-02-13 thomas
1528 f9c2e8e5 2022-02-13 thomas if (a->reused_delta_offset < b->reused_delta_offset)
1529 f9c2e8e5 2022-02-13 thomas return -1;
1530 f9c2e8e5 2022-02-13 thomas if (a->reused_delta_offset > b->reused_delta_offset)
1531 f9c2e8e5 2022-02-13 thomas return 1;
1532 f9c2e8e5 2022-02-13 thomas return 0;
1533 f9c2e8e5 2022-02-13 thomas }
1534 f9c2e8e5 2022-02-13 thomas
1535 e6bcace5 2021-06-22 stsp static const struct got_error *
1536 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1537 e6bcace5 2021-06-22 stsp {
1538 e6bcace5 2021-06-22 stsp size_t i;
1539 e6bcace5 2021-06-22 stsp
1540 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1541 e6bcace5 2021-06-22 stsp
1542 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1543 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1544 e6bcace5 2021-06-22 stsp len >>= 4;
1545 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1546 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1547 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1548 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1549 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1550 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1551 e6bcace5 2021-06-22 stsp }
1552 e6bcace5 2021-06-22 stsp
1553 e6bcace5 2021-06-22 stsp *hdrlen = i;
1554 e6bcace5 2021-06-22 stsp return NULL;
1555 e6bcace5 2021-06-22 stsp }
1556 e6bcace5 2021-06-22 stsp
1557 e6bcace5 2021-06-22 stsp static int
1558 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1559 e6bcace5 2021-06-22 stsp {
1560 e6bcace5 2021-06-22 stsp int i, j;
1561 e6bcace5 2021-06-22 stsp char rbuf[8];
1562 e6bcace5 2021-06-22 stsp
1563 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1564 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1565 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1566 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1567 e6bcace5 2021-06-22 stsp }
1568 e6bcace5 2021-06-22 stsp
1569 e6bcace5 2021-06-22 stsp j = 0;
1570 e6bcace5 2021-06-22 stsp while (i > 0)
1571 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1572 e6bcace5 2021-06-22 stsp return j;
1573 e6bcace5 2021-06-22 stsp }
1574 e6bcace5 2021-06-22 stsp
1575 e6bcace5 2021-06-22 stsp static const struct got_error *
1576 3b6ceab7 2022-01-10 thomas deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1577 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m)
1578 3b6ceab7 2022-01-10 thomas {
1579 3b6ceab7 2022-01-10 thomas const struct got_error *err;
1580 3b6ceab7 2022-01-10 thomas char buf[32];
1581 3b6ceab7 2022-01-10 thomas int nh;
1582 3b6ceab7 2022-01-10 thomas
1583 f9c2e8e5 2022-02-13 thomas if (m->prev->off != 0) {
1584 3b6ceab7 2022-01-10 thomas err = packhdr(&nh, buf, sizeof(buf),
1585 3b6ceab7 2022-01-10 thomas GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1586 3b6ceab7 2022-01-10 thomas if (err)
1587 3b6ceab7 2022-01-10 thomas return err;
1588 3b6ceab7 2022-01-10 thomas nh += packoff(buf + nh, m->off - m->prev->off);
1589 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, buf, nh, ctx);
1590 3b6ceab7 2022-01-10 thomas if (err)
1591 3b6ceab7 2022-01-10 thomas return err;
1592 3b6ceab7 2022-01-10 thomas *packfile_size += nh;
1593 3b6ceab7 2022-01-10 thomas } else {
1594 3b6ceab7 2022-01-10 thomas err = packhdr(&nh, buf, sizeof(buf),
1595 3b6ceab7 2022-01-10 thomas GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1596 3b6ceab7 2022-01-10 thomas if (err)
1597 3b6ceab7 2022-01-10 thomas return err;
1598 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, buf, nh, ctx);
1599 3b6ceab7 2022-01-10 thomas if (err)
1600 3b6ceab7 2022-01-10 thomas return err;
1601 3b6ceab7 2022-01-10 thomas *packfile_size += nh;
1602 3b6ceab7 2022-01-10 thomas err = hwrite(packfile, m->prev->id.sha1,
1603 3b6ceab7 2022-01-10 thomas sizeof(m->prev->id.sha1), ctx);
1604 3b6ceab7 2022-01-10 thomas if (err)
1605 3b6ceab7 2022-01-10 thomas return err;
1606 3b6ceab7 2022-01-10 thomas *packfile_size += sizeof(m->prev->id.sha1);
1607 3b6ceab7 2022-01-10 thomas }
1608 3b6ceab7 2022-01-10 thomas
1609 3b6ceab7 2022-01-10 thomas return NULL;
1610 3b6ceab7 2022-01-10 thomas }
1611 3b6ceab7 2022-01-10 thomas
1612 3b6ceab7 2022-01-10 thomas static const struct got_error *
1613 f9c2e8e5 2022-02-13 thomas write_packed_object(off_t *packfile_size, FILE *packfile,
1614 f9c2e8e5 2022-02-13 thomas FILE *delta_cache, struct got_pack_meta *m, int *outfd,
1615 f9c2e8e5 2022-02-13 thomas SHA1_CTX *ctx, struct got_repository *repo)
1616 f9c2e8e5 2022-02-13 thomas {
1617 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
1618 f9c2e8e5 2022-02-13 thomas struct got_deflate_checksum csum;
1619 f9c2e8e5 2022-02-13 thomas char buf[32];
1620 f9c2e8e5 2022-02-13 thomas int nh;
1621 f9c2e8e5 2022-02-13 thomas struct got_raw_object *raw = NULL;
1622 f9c2e8e5 2022-02-13 thomas off_t outlen;
1623 f9c2e8e5 2022-02-13 thomas
1624 f9c2e8e5 2022-02-13 thomas csum.output_sha1 = ctx;
1625 f9c2e8e5 2022-02-13 thomas csum.output_crc = NULL;
1626 f9c2e8e5 2022-02-13 thomas
1627 f9c2e8e5 2022-02-13 thomas m->off = ftello(packfile);
1628 f9c2e8e5 2022-02-13 thomas if (m->delta_len == 0) {
1629 f9c2e8e5 2022-02-13 thomas err = got_object_raw_open(&raw, outfd, repo, &m->id);
1630 f9c2e8e5 2022-02-13 thomas if (err)
1631 f9c2e8e5 2022-02-13 thomas goto done;
1632 f9c2e8e5 2022-02-13 thomas err = packhdr(&nh, buf, sizeof(buf),
1633 f9c2e8e5 2022-02-13 thomas m->obj_type, raw->size);
1634 f9c2e8e5 2022-02-13 thomas if (err)
1635 f9c2e8e5 2022-02-13 thomas goto done;
1636 f9c2e8e5 2022-02-13 thomas err = hwrite(packfile, buf, nh, ctx);
1637 f9c2e8e5 2022-02-13 thomas if (err)
1638 f9c2e8e5 2022-02-13 thomas goto done;
1639 f9c2e8e5 2022-02-13 thomas *packfile_size += nh;
1640 f9c2e8e5 2022-02-13 thomas if (raw->f == NULL) {
1641 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file_mmap(&outlen,
1642 f9c2e8e5 2022-02-13 thomas raw->data + raw->hdrlen, 0, raw->size,
1643 f9c2e8e5 2022-02-13 thomas packfile, &csum);
1644 f9c2e8e5 2022-02-13 thomas if (err)
1645 f9c2e8e5 2022-02-13 thomas goto done;
1646 f9c2e8e5 2022-02-13 thomas } else {
1647 f9c2e8e5 2022-02-13 thomas if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1648 f9c2e8e5 2022-02-13 thomas == -1) {
1649 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
1650 f9c2e8e5 2022-02-13 thomas goto done;
1651 f9c2e8e5 2022-02-13 thomas }
1652 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file(&outlen, raw->f,
1653 f9c2e8e5 2022-02-13 thomas raw->size, packfile, &csum);
1654 f9c2e8e5 2022-02-13 thomas if (err)
1655 f9c2e8e5 2022-02-13 thomas goto done;
1656 f9c2e8e5 2022-02-13 thomas }
1657 f9c2e8e5 2022-02-13 thomas *packfile_size += outlen;
1658 f9c2e8e5 2022-02-13 thomas got_object_raw_close(raw);
1659 f9c2e8e5 2022-02-13 thomas raw = NULL;
1660 f9c2e8e5 2022-02-13 thomas } else if (m->delta_buf) {
1661 f9c2e8e5 2022-02-13 thomas err = deltahdr(packfile_size, ctx, packfile, m);
1662 f9c2e8e5 2022-02-13 thomas if (err)
1663 f9c2e8e5 2022-02-13 thomas goto done;
1664 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file_mmap(&outlen,
1665 f9c2e8e5 2022-02-13 thomas m->delta_buf, 0, m->delta_len, packfile, &csum);
1666 f9c2e8e5 2022-02-13 thomas if (err)
1667 f9c2e8e5 2022-02-13 thomas goto done;
1668 f9c2e8e5 2022-02-13 thomas *packfile_size += outlen;
1669 f9c2e8e5 2022-02-13 thomas free(m->delta_buf);
1670 f9c2e8e5 2022-02-13 thomas m->delta_buf = NULL;
1671 f9c2e8e5 2022-02-13 thomas } else {
1672 f9c2e8e5 2022-02-13 thomas if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1673 f9c2e8e5 2022-02-13 thomas == -1) {
1674 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
1675 f9c2e8e5 2022-02-13 thomas goto done;
1676 f9c2e8e5 2022-02-13 thomas }
1677 f9c2e8e5 2022-02-13 thomas err = deltahdr(packfile_size, ctx, packfile, m);
1678 f9c2e8e5 2022-02-13 thomas if (err)
1679 f9c2e8e5 2022-02-13 thomas goto done;
1680 f9c2e8e5 2022-02-13 thomas err = got_deflate_to_file(&outlen, delta_cache,
1681 f9c2e8e5 2022-02-13 thomas m->delta_len, packfile, &csum);
1682 f9c2e8e5 2022-02-13 thomas if (err)
1683 f9c2e8e5 2022-02-13 thomas goto done;
1684 f9c2e8e5 2022-02-13 thomas *packfile_size += outlen;
1685 f9c2e8e5 2022-02-13 thomas }
1686 f9c2e8e5 2022-02-13 thomas done:
1687 f9c2e8e5 2022-02-13 thomas if (raw)
1688 f9c2e8e5 2022-02-13 thomas got_object_raw_close(raw);
1689 f9c2e8e5 2022-02-13 thomas return err;
1690 f9c2e8e5 2022-02-13 thomas }
1691 f9c2e8e5 2022-02-13 thomas
1692 f9c2e8e5 2022-02-13 thomas static const struct got_error *
1693 b79280dd 2021-10-15 thomas genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1694 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **deltify, int ndeltify,
1695 f9c2e8e5 2022-02-13 thomas struct got_pack_meta **reuse, int nreuse,
1696 85220b0e 2022-03-22 thomas int ncolored, int nfound, int ntrees, int nours,
1697 85220b0e 2022-03-22 thomas struct got_repository *repo,
1698 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1699 31ba2236 2022-01-05 thomas struct got_ratelimit *rl,
1700 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1701 e6bcace5 2021-06-22 stsp {
1702 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1703 f9c2e8e5 2022-02-13 thomas int i;
1704 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1705 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1706 e4d8ab47 2021-10-15 thomas char buf[32];
1707 e8f02263 2022-01-23 thomas size_t n;
1708 f9c2e8e5 2022-02-13 thomas off_t packfile_size = 0;
1709 ecf9545f 2021-10-15 thomas int outfd = -1;
1710 e6bcace5 2021-06-22 stsp
1711 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1712 e6bcace5 2021-06-22 stsp
1713 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1714 e6bcace5 2021-06-22 stsp if (err)
1715 e6bcace5 2021-06-22 stsp return err;
1716 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1717 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1718 e6bcace5 2021-06-22 stsp if (err)
1719 e6bcace5 2021-06-22 stsp goto done;
1720 f9c2e8e5 2022-02-13 thomas putbe32(buf, ndeltify + nreuse);
1721 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1722 e6bcace5 2021-06-22 stsp if (err)
1723 e6bcace5 2021-06-22 stsp goto done;
1724 f9c2e8e5 2022-02-13 thomas
1725 f9c2e8e5 2022-02-13 thomas qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1726 f9c2e8e5 2022-02-13 thomas write_order_cmp);
1727 f9c2e8e5 2022-02-13 thomas for (i = 0; i < ndeltify; i++) {
1728 31ba2236 2022-01-05 thomas err = report_progress(progress_cb, progress_arg, rl,
1729 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, packfile_size, nours,
1730 85220b0e 2022-03-22 thomas ndeltify + nreuse, ndeltify + nreuse, i);
1731 31ba2236 2022-01-05 thomas if (err)
1732 31ba2236 2022-01-05 thomas goto done;
1733 f9c2e8e5 2022-02-13 thomas m = deltify[i];
1734 f9c2e8e5 2022-02-13 thomas err = write_packed_object(&packfile_size, packfile,
1735 f9c2e8e5 2022-02-13 thomas delta_cache, m, &outfd, &ctx, repo);
1736 f9c2e8e5 2022-02-13 thomas if (err)
1737 f9c2e8e5 2022-02-13 thomas goto done;
1738 e6bcace5 2021-06-22 stsp }
1739 f9c2e8e5 2022-02-13 thomas
1740 f9c2e8e5 2022-02-13 thomas qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1741 f9c2e8e5 2022-02-13 thomas reuse_write_order_cmp);
1742 f9c2e8e5 2022-02-13 thomas for (i = 0; i < nreuse; i++) {
1743 f9c2e8e5 2022-02-13 thomas err = report_progress(progress_cb, progress_arg, rl,
1744 85220b0e 2022-03-22 thomas ncolored, nfound, ntrees, packfile_size, nours,
1745 85220b0e 2022-03-22 thomas ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1746 f9c2e8e5 2022-02-13 thomas if (err)
1747 f9c2e8e5 2022-02-13 thomas goto done;
1748 f9c2e8e5 2022-02-13 thomas m = reuse[i];
1749 f9c2e8e5 2022-02-13 thomas err = write_packed_object(&packfile_size, packfile,
1750 f9c2e8e5 2022-02-13 thomas delta_cache, m, &outfd, &ctx, repo);
1751 f9c2e8e5 2022-02-13 thomas if (err)
1752 f9c2e8e5 2022-02-13 thomas goto done;
1753 f9c2e8e5 2022-02-13 thomas }
1754 f9c2e8e5 2022-02-13 thomas
1755 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1756 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1757 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1758 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1759 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1760 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1761 31ba2236 2022-01-05 thomas if (progress_cb) {
1762 85220b0e 2022-03-22 thomas err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1763 85220b0e 2022-03-22 thomas packfile_size, nours, ndeltify + nreuse,
1764 85220b0e 2022-03-22 thomas ndeltify + nreuse, ndeltify + nreuse);
1765 31ba2236 2022-01-05 thomas if (err)
1766 31ba2236 2022-01-05 thomas goto done;
1767 31ba2236 2022-01-05 thomas }
1768 e6bcace5 2021-06-22 stsp done:
1769 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
1770 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
1771 e6bcace5 2021-06-22 stsp return err;
1772 e6bcace5 2021-06-22 stsp }
1773 e6bcace5 2021-06-22 stsp
1774 f9c2e8e5 2022-02-13 thomas static const struct got_error *
1775 f9c2e8e5 2022-02-13 thomas remove_unused_object(struct got_object_idset_element *entry, void *arg)
1776 f9c2e8e5 2022-02-13 thomas {
1777 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset = arg;
1778 f9c2e8e5 2022-02-13 thomas
1779 f9c2e8e5 2022-02-13 thomas if (got_object_idset_get_element_data(entry) == NULL)
1780 f9c2e8e5 2022-02-13 thomas got_object_idset_remove_element(idset, entry);
1781 f9c2e8e5 2022-02-13 thomas
1782 f9c2e8e5 2022-02-13 thomas return NULL;
1783 f9c2e8e5 2022-02-13 thomas }
1784 f9c2e8e5 2022-02-13 thomas
1785 f9c2e8e5 2022-02-13 thomas static const struct got_error *
1786 f9c2e8e5 2022-02-13 thomas remove_reused_object(struct got_object_idset_element *entry, void *arg)
1787 f9c2e8e5 2022-02-13 thomas {
1788 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset = arg;
1789 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m;
1790 f9c2e8e5 2022-02-13 thomas
1791 f9c2e8e5 2022-02-13 thomas m = got_object_idset_get_element_data(entry);
1792 f9c2e8e5 2022-02-13 thomas if (m->have_reused_delta)
1793 f9c2e8e5 2022-02-13 thomas got_object_idset_remove_element(idset, entry);
1794 f9c2e8e5 2022-02-13 thomas
1795 f9c2e8e5 2022-02-13 thomas return NULL;
1796 f9c2e8e5 2022-02-13 thomas }
1797 f9c2e8e5 2022-02-13 thomas
1798 f9c2e8e5 2022-02-13 thomas static const struct got_error *
1799 f9c2e8e5 2022-02-13 thomas add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1800 f9c2e8e5 2022-02-13 thomas {
1801 f9c2e8e5 2022-02-13 thomas struct got_pack_meta *m = data;
1802 f9c2e8e5 2022-02-13 thomas struct got_pack_metavec *v = arg;
1803 f9c2e8e5 2022-02-13 thomas
1804 f9c2e8e5 2022-02-13 thomas return add_meta(m, v);
1805 f9c2e8e5 2022-02-13 thomas }
1806 f9c2e8e5 2022-02-13 thomas
1807 e6bcace5 2021-06-22 stsp const struct got_error *
1808 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1809 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1810 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1811 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1812 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1813 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1814 e6bcace5 2021-06-22 stsp {
1815 e6bcace5 2021-06-22 stsp const struct got_error *err;
1816 f9c2e8e5 2022-02-13 thomas int delta_cache_fd = -1;
1817 b79280dd 2021-10-15 thomas FILE *delta_cache = NULL;
1818 f9c2e8e5 2022-02-13 thomas struct got_object_idset *idset;
1819 31ba2236 2022-01-05 thomas struct got_ratelimit rl;
1820 f9c2e8e5 2022-02-13 thomas struct got_pack_metavec deltify, reuse;
1821 85220b0e 2022-03-22 thomas int ncolored = 0, nfound = 0, ntrees = 0;
1822 e6bcace5 2021-06-22 stsp
1823 f9c2e8e5 2022-02-13 thomas memset(&deltify, 0, sizeof(deltify));
1824 f9c2e8e5 2022-02-13 thomas memset(&reuse, 0, sizeof(reuse));
1825 f9c2e8e5 2022-02-13 thomas
1826 31ba2236 2022-01-05 thomas got_ratelimit_init(&rl, 0, 500);
1827 31ba2236 2022-01-05 thomas
1828 f9c2e8e5 2022-02-13 thomas idset = got_object_idset_alloc();
1829 f9c2e8e5 2022-02-13 thomas if (idset == NULL)
1830 f9c2e8e5 2022-02-13 thomas return got_error_from_errno("got_object_idset_alloc");
1831 f9c2e8e5 2022-02-13 thomas
1832 85220b0e 2022-03-22 thomas err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1833 85220b0e 2022-03-22 thomas ntheirs, ours, nours, repo, loose_obj_only,
1834 85220b0e 2022-03-22 thomas progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1835 e6bcace5 2021-06-22 stsp if (err)
1836 e6bcace5 2021-06-22 stsp return err;
1837 e6bcace5 2021-06-22 stsp
1838 f9c2e8e5 2022-02-13 thomas err = got_object_idset_for_each_element(idset,
1839 f9c2e8e5 2022-02-13 thomas remove_unused_object, idset);
1840 f9c2e8e5 2022-02-13 thomas if (err)
1841 f9c2e8e5 2022-02-13 thomas goto done;
1842 f9c2e8e5 2022-02-13 thomas
1843 f9a643e8 2022-02-13 thomas if (progress_cb) {
1844 85220b0e 2022-03-22 thomas err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1845 85220b0e 2022-03-22 thomas 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1846 f9a643e8 2022-02-13 thomas if (err)
1847 f9a643e8 2022-02-13 thomas goto done;
1848 f9a643e8 2022-02-13 thomas }
1849 f9a643e8 2022-02-13 thomas
1850 f9c2e8e5 2022-02-13 thomas if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1851 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1852 05118f5a 2021-06-22 stsp goto done;
1853 05118f5a 2021-06-22 stsp }
1854 b79280dd 2021-10-15 thomas
1855 f9c2e8e5 2022-02-13 thomas delta_cache_fd = got_opentempfd();
1856 f9c2e8e5 2022-02-13 thomas if (delta_cache_fd == -1) {
1857 b79280dd 2021-10-15 thomas err = got_error_from_errno("got_opentemp");
1858 b79280dd 2021-10-15 thomas goto done;
1859 b79280dd 2021-10-15 thomas }
1860 b79280dd 2021-10-15 thomas
1861 f9c2e8e5 2022-02-13 thomas reuse.metasz = 64;
1862 f9c2e8e5 2022-02-13 thomas reuse.meta = calloc(reuse.metasz,
1863 f9c2e8e5 2022-02-13 thomas sizeof(struct got_pack_meta *));
1864 f9c2e8e5 2022-02-13 thomas if (reuse.meta == NULL) {
1865 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("calloc");
1866 f9c2e8e5 2022-02-13 thomas goto done;
1867 f9c2e8e5 2022-02-13 thomas }
1868 f9c2e8e5 2022-02-13 thomas
1869 85220b0e 2022-03-22 thomas err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1870 85220b0e 2022-03-22 thomas ntrees, nours, repo, progress_cb, progress_arg, &rl,
1871 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1872 f9c2e8e5 2022-02-13 thomas if (err)
1873 f9c2e8e5 2022-02-13 thomas goto done;
1874 f9c2e8e5 2022-02-13 thomas if (reuse.nmeta > 0) {
1875 f9c2e8e5 2022-02-13 thomas err = got_object_idset_for_each_element(idset,
1876 f9c2e8e5 2022-02-13 thomas remove_reused_object, idset);
1877 f9c2e8e5 2022-02-13 thomas if (err)
1878 f9c2e8e5 2022-02-13 thomas goto done;
1879 f9c2e8e5 2022-02-13 thomas }
1880 f9c2e8e5 2022-02-13 thomas
1881 f9c2e8e5 2022-02-13 thomas delta_cache = fdopen(delta_cache_fd, "a+");
1882 f9c2e8e5 2022-02-13 thomas if (delta_cache == NULL) {
1883 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fdopen");
1884 f9c2e8e5 2022-02-13 thomas goto done;
1885 f9c2e8e5 2022-02-13 thomas }
1886 f9c2e8e5 2022-02-13 thomas delta_cache_fd = -1;
1887 f9c2e8e5 2022-02-13 thomas
1888 f9c2e8e5 2022-02-13 thomas if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1889 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fseeko");
1890 f9c2e8e5 2022-02-13 thomas goto done;
1891 f9c2e8e5 2022-02-13 thomas }
1892 f9c2e8e5 2022-02-13 thomas
1893 f9c2e8e5 2022-02-13 thomas deltify.meta = calloc(got_object_idset_num_elements(idset),
1894 f9c2e8e5 2022-02-13 thomas sizeof(struct got_pack_meta *));
1895 f9c2e8e5 2022-02-13 thomas if (deltify.meta == NULL) {
1896 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("calloc");
1897 f9c2e8e5 2022-02-13 thomas goto done;
1898 f9c2e8e5 2022-02-13 thomas }
1899 f9c2e8e5 2022-02-13 thomas deltify.metasz = got_object_idset_num_elements(idset);
1900 f9c2e8e5 2022-02-13 thomas
1901 f9c2e8e5 2022-02-13 thomas err = got_object_idset_for_each(idset, add_meta_idset_cb, &deltify);
1902 f9c2e8e5 2022-02-13 thomas if (err)
1903 f9c2e8e5 2022-02-13 thomas goto done;
1904 f9c2e8e5 2022-02-13 thomas if (deltify.nmeta > 0) {
1905 85220b0e 2022-03-22 thomas err = pick_deltas(deltify.meta, deltify.nmeta, ncolored,
1906 85220b0e 2022-03-22 thomas nfound, ntrees, nours, reuse.nmeta, delta_cache, repo,
1907 31ba2236 2022-01-05 thomas progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1908 f8a36e22 2021-08-26 stsp if (err)
1909 f8a36e22 2021-08-26 stsp goto done;
1910 b79280dd 2021-10-15 thomas if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1911 b79280dd 2021-10-15 thomas err = got_error_from_errno("fseeko");
1912 b79280dd 2021-10-15 thomas goto done;
1913 b79280dd 2021-10-15 thomas }
1914 f8a36e22 2021-08-26 stsp }
1915 e6bcace5 2021-06-22 stsp
1916 f9c2e8e5 2022-02-13 thomas err = genpack(packsha1, packfile, delta_cache, deltify.meta,
1917 85220b0e 2022-03-22 thomas deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1918 85220b0e 2022-03-22 thomas nours, repo, progress_cb, progress_arg, &rl,
1919 85220b0e 2022-03-22 thomas cancel_cb, cancel_arg);
1920 e6bcace5 2021-06-22 stsp if (err)
1921 e6bcace5 2021-06-22 stsp goto done;
1922 e6bcace5 2021-06-22 stsp done:
1923 f9c2e8e5 2022-02-13 thomas free_nmeta(deltify.meta, deltify.nmeta);
1924 f9c2e8e5 2022-02-13 thomas free_nmeta(reuse.meta, reuse.nmeta);
1925 f9c2e8e5 2022-02-13 thomas got_object_idset_free(idset);
1926 f9c2e8e5 2022-02-13 thomas if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1927 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("close");
1928 b79280dd 2021-10-15 thomas if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1929 b79280dd 2021-10-15 thomas err = got_error_from_errno("fclose");
1930 e6bcace5 2021-06-22 stsp return err;
1931 e6bcace5 2021-06-22 stsp }