Blame


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