Blame


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