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