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