Blame


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