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