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