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