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 e6bcace5 2021-06-22 stsp #include <stdio.h>
32 e6bcace5 2021-06-22 stsp #include <stdlib.h>
33 e6bcace5 2021-06-22 stsp #include <string.h>
34 e6bcace5 2021-06-22 stsp #include <sha1.h>
35 211cfef0 2022-01-05 stsp #include <time.h>
36 bfc73a47 2022-03-19 naddy #include <unistd.h>
37 e6bcace5 2021-06-22 stsp #include <limits.h>
38 e6bcace5 2021-06-22 stsp #include <zlib.h>
39 e6bcace5 2021-06-22 stsp
40 e6bcace5 2021-06-22 stsp #include "got_error.h"
41 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
42 e6bcace5 2021-06-22 stsp #include "got_object.h"
43 f8b19efd 2021-10-13 stsp #include "got_path.h"
44 05118f5a 2021-06-22 stsp #include "got_reference.h"
45 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
46 e6bcace5 2021-06-22 stsp
47 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
50 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
52 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
53 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
54 17cfdba6 2022-05-20 op #include "got_lib_pack_create.h"
55 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
56 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
57 211cfef0 2022-01-05 stsp #include "got_lib_ratelimit.h"
58 2d9e6abf 2022-05-04 stsp #include "got_lib_inflate.h"
59 e6bcace5 2021-06-22 stsp
60 f8174ca5 2022-05-20 stsp #include "murmurhash2.h"
61 f8174ca5 2022-05-20 stsp
62 74881701 2021-10-15 stsp #ifndef MIN
63 74881701 2021-10-15 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
64 74881701 2021-10-15 stsp #endif
65 74881701 2021-10-15 stsp
66 e6bcace5 2021-06-22 stsp #ifndef MAX
67 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
68 e6bcace5 2021-06-22 stsp #endif
69 e6bcace5 2021-06-22 stsp
70 70f8f24d 2022-04-14 stsp #ifndef nitems
71 70f8f24d 2022-04-14 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
72 70f8f24d 2022-04-14 stsp #endif
73 70f8f24d 2022-04-14 stsp
74 e6bcace5 2021-06-22 stsp struct got_pack_meta {
75 e6bcace5 2021-06-22 stsp struct got_object_id id;
76 f8174ca5 2022-05-20 stsp uint32_t path_hash;
77 e6bcace5 2021-06-22 stsp int obj_type;
78 600b755e 2021-10-14 stsp off_t size;
79 e6bcace5 2021-06-22 stsp time_t mtime;
80 e6bcace5 2021-06-22 stsp
81 e6bcace5 2021-06-22 stsp /* The best delta we picked */
82 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
83 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
84 2d9e6abf 2022-05-04 stsp unsigned char *delta_buf; /* if encoded in memory (compressed) */
85 2d9e6abf 2022-05-04 stsp off_t delta_offset; /* offset in delta cache file (compressed) */
86 5060d5a1 2022-01-10 stsp off_t delta_len; /* encoded delta length */
87 2d9e6abf 2022-05-04 stsp off_t delta_compressed_len; /* encoded+compressed delta length */
88 e6bcace5 2021-06-22 stsp int nchain;
89 e6bcace5 2021-06-22 stsp
90 67fd6849 2022-02-13 stsp off_t reused_delta_offset; /* offset of delta in reused pack file */
91 67fd6849 2022-02-13 stsp struct got_object_id *base_obj_id;
92 67fd6849 2022-02-13 stsp
93 e6bcace5 2021-06-22 stsp /* Only used for delta window */
94 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
95 e6bcace5 2021-06-22 stsp
96 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
97 e6bcace5 2021-06-22 stsp off_t off;
98 e6bcace5 2021-06-22 stsp };
99 e6bcace5 2021-06-22 stsp
100 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
101 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
102 e6bcace5 2021-06-22 stsp int nmeta;
103 e6bcace5 2021-06-22 stsp int metasz;
104 e6bcace5 2021-06-22 stsp };
105 e6bcace5 2021-06-22 stsp
106 e6bcace5 2021-06-22 stsp static const struct got_error *
107 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
108 d6a28ffe 2022-05-20 op const char *path, int obj_type, time_t mtime, uint32_t seed)
109 e6bcace5 2021-06-22 stsp {
110 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
111 e6bcace5 2021-06-22 stsp
112 e6bcace5 2021-06-22 stsp *new = NULL;
113 e6bcace5 2021-06-22 stsp
114 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
115 e6bcace5 2021-06-22 stsp if (m == NULL)
116 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
117 e6bcace5 2021-06-22 stsp
118 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
119 e6bcace5 2021-06-22 stsp
120 d6a28ffe 2022-05-20 op m->path_hash = murmurhash2(path, strlen(path), seed);
121 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
122 e6bcace5 2021-06-22 stsp m->mtime = mtime;
123 e6bcace5 2021-06-22 stsp *new = m;
124 e6bcace5 2021-06-22 stsp return NULL;
125 e6bcace5 2021-06-22 stsp }
126 e6bcace5 2021-06-22 stsp
127 e6bcace5 2021-06-22 stsp static void
128 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
129 e6bcace5 2021-06-22 stsp {
130 e6bcace5 2021-06-22 stsp if (meta == NULL)
131 e6bcace5 2021-06-22 stsp return;
132 f8174ca5 2022-05-20 stsp meta->path_hash = 0;
133 5060d5a1 2022-01-10 stsp free(meta->delta_buf);
134 5060d5a1 2022-01-10 stsp meta->delta_buf = NULL;
135 67fd6849 2022-02-13 stsp free(meta->base_obj_id);
136 67fd6849 2022-02-13 stsp meta->base_obj_id = NULL;
137 411cbec1 2022-05-20 stsp meta->reused_delta_offset = 0;
138 e6bcace5 2021-06-22 stsp }
139 e6bcace5 2021-06-22 stsp
140 e6bcace5 2021-06-22 stsp static void
141 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
142 e6bcace5 2021-06-22 stsp {
143 e6bcace5 2021-06-22 stsp int i;
144 e6bcace5 2021-06-22 stsp
145 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
146 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
147 e6bcace5 2021-06-22 stsp free(meta);
148 e6bcace5 2021-06-22 stsp }
149 e6bcace5 2021-06-22 stsp
150 e6bcace5 2021-06-22 stsp static int
151 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
152 e6bcace5 2021-06-22 stsp {
153 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
154 e6bcace5 2021-06-22 stsp
155 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
156 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
157 e6bcace5 2021-06-22 stsp
158 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
159 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
160 f8174ca5 2022-05-20 stsp if (a->path_hash < b->path_hash)
161 f8174ca5 2022-05-20 stsp return -1;
162 f8174ca5 2022-05-20 stsp if (a->path_hash > b->path_hash)
163 f8174ca5 2022-05-20 stsp return 1;
164 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
165 611e8e31 2022-05-01 stsp return -1;
166 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
167 611e8e31 2022-05-01 stsp return 1;
168 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
169 e6bcace5 2021-06-22 stsp }
170 e6bcace5 2021-06-22 stsp
171 5060d5a1 2022-01-10 stsp static off_t
172 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
173 e6bcace5 2021-06-22 stsp {
174 5060d5a1 2022-01-10 stsp int i;
175 5060d5a1 2022-01-10 stsp off_t size = 32;
176 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
177 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
178 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
179 e6bcace5 2021-06-22 stsp else
180 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
181 e6bcace5 2021-06-22 stsp }
182 e6bcace5 2021-06-22 stsp return size;
183 5060d5a1 2022-01-10 stsp }
184 5060d5a1 2022-01-10 stsp
185 5060d5a1 2022-01-10 stsp static const struct got_error *
186 5060d5a1 2022-01-10 stsp append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
187 5060d5a1 2022-01-10 stsp {
188 5060d5a1 2022-01-10 stsp char *n;
189 5060d5a1 2022-01-10 stsp
190 5060d5a1 2022-01-10 stsp if (*len + nseg >= *sz) {
191 5060d5a1 2022-01-10 stsp while (*len + nseg >= *sz)
192 5060d5a1 2022-01-10 stsp *sz += *sz / 2;
193 5060d5a1 2022-01-10 stsp n = realloc(*p, *sz);
194 5060d5a1 2022-01-10 stsp if (n == NULL)
195 5060d5a1 2022-01-10 stsp return got_error_from_errno("realloc");
196 5060d5a1 2022-01-10 stsp *p = n;
197 5060d5a1 2022-01-10 stsp }
198 5060d5a1 2022-01-10 stsp memcpy(*p + *len, seg, nseg);
199 5060d5a1 2022-01-10 stsp *len += nseg;
200 5060d5a1 2022-01-10 stsp return NULL;
201 5060d5a1 2022-01-10 stsp }
202 5060d5a1 2022-01-10 stsp
203 5060d5a1 2022-01-10 stsp static const struct got_error *
204 5060d5a1 2022-01-10 stsp encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
205 5060d5a1 2022-01-10 stsp struct got_delta_instruction *deltas, int ndeltas,
206 5060d5a1 2022-01-10 stsp off_t delta_size, off_t base_size)
207 5060d5a1 2022-01-10 stsp {
208 5060d5a1 2022-01-10 stsp const struct got_error *err;
209 5060d5a1 2022-01-10 stsp unsigned char buf[16], *bp;
210 5060d5a1 2022-01-10 stsp int i, j;
211 2d9e6abf 2022-05-04 stsp size_t len = 0, compressed_len;
212 2d9e6abf 2022-05-04 stsp off_t bufsize = delta_size;
213 5060d5a1 2022-01-10 stsp off_t n;
214 5060d5a1 2022-01-10 stsp struct got_delta_instruction *d;
215 2d9e6abf 2022-05-04 stsp uint8_t *delta_buf;
216 5060d5a1 2022-01-10 stsp
217 2d9e6abf 2022-05-04 stsp delta_buf = malloc(bufsize);
218 2d9e6abf 2022-05-04 stsp if (delta_buf == NULL)
219 2d9e6abf 2022-05-04 stsp return got_error_from_errno("malloc");
220 5060d5a1 2022-01-10 stsp
221 5060d5a1 2022-01-10 stsp /* base object size */
222 5060d5a1 2022-01-10 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
223 5060d5a1 2022-01-10 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
224 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
225 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
226 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
227 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
228 5060d5a1 2022-01-10 stsp }
229 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
230 5060d5a1 2022-01-10 stsp if (err)
231 2d9e6abf 2022-05-04 stsp goto done;
232 5060d5a1 2022-01-10 stsp
233 5060d5a1 2022-01-10 stsp /* target object size */
234 5060d5a1 2022-01-10 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
235 5060d5a1 2022-01-10 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
236 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
237 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
238 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
239 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
240 5060d5a1 2022-01-10 stsp }
241 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
242 5060d5a1 2022-01-10 stsp if (err)
243 2d9e6abf 2022-05-04 stsp goto done;
244 5060d5a1 2022-01-10 stsp
245 5060d5a1 2022-01-10 stsp for (j = 0; j < ndeltas; j++) {
246 5060d5a1 2022-01-10 stsp d = &deltas[j];
247 5060d5a1 2022-01-10 stsp if (d->copy) {
248 5060d5a1 2022-01-10 stsp n = d->offset;
249 5060d5a1 2022-01-10 stsp bp = &buf[1];
250 5060d5a1 2022-01-10 stsp buf[0] = GOT_DELTA_BASE_COPY;
251 5060d5a1 2022-01-10 stsp for (i = 0; i < 4; i++) {
252 5060d5a1 2022-01-10 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
253 5060d5a1 2022-01-10 stsp buf[0] |= 1 << i;
254 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
255 5060d5a1 2022-01-10 stsp n >>= 8;
256 5060d5a1 2022-01-10 stsp if (n == 0)
257 5060d5a1 2022-01-10 stsp break;
258 5060d5a1 2022-01-10 stsp }
259 5060d5a1 2022-01-10 stsp
260 5060d5a1 2022-01-10 stsp n = d->len;
261 5060d5a1 2022-01-10 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
262 5060d5a1 2022-01-10 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
263 5060d5a1 2022-01-10 stsp for (i = 0; i < 3 && n > 0; i++) {
264 5060d5a1 2022-01-10 stsp buf[0] |= 1 << (i + 4);
265 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
266 5060d5a1 2022-01-10 stsp n >>= 8;
267 5060d5a1 2022-01-10 stsp }
268 5060d5a1 2022-01-10 stsp }
269 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
270 5060d5a1 2022-01-10 stsp buf, bp - buf);
271 5060d5a1 2022-01-10 stsp if (err)
272 2d9e6abf 2022-05-04 stsp goto done;
273 5060d5a1 2022-01-10 stsp } else if (o->f == NULL) {
274 5060d5a1 2022-01-10 stsp n = 0;
275 5060d5a1 2022-01-10 stsp while (n != d->len) {
276 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
277 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
278 5060d5a1 2022-01-10 stsp buf, 1);
279 5060d5a1 2022-01-10 stsp if (err)
280 2d9e6abf 2022-05-04 stsp goto done;
281 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
282 5060d5a1 2022-01-10 stsp o->data + o->hdrlen + d->offset + n,
283 5060d5a1 2022-01-10 stsp buf[0]);
284 5060d5a1 2022-01-10 stsp if (err)
285 2d9e6abf 2022-05-04 stsp goto done;
286 5060d5a1 2022-01-10 stsp n += buf[0];
287 5060d5a1 2022-01-10 stsp }
288 5060d5a1 2022-01-10 stsp } else {
289 5060d5a1 2022-01-10 stsp char content[128];
290 5060d5a1 2022-01-10 stsp size_t r;
291 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
292 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
293 2d9e6abf 2022-05-04 stsp goto done;
294 2d9e6abf 2022-05-04 stsp }
295 5060d5a1 2022-01-10 stsp n = 0;
296 5060d5a1 2022-01-10 stsp while (n != d->len) {
297 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
298 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
299 5060d5a1 2022-01-10 stsp buf, 1);
300 5060d5a1 2022-01-10 stsp if (err)
301 2d9e6abf 2022-05-04 stsp goto done;
302 5060d5a1 2022-01-10 stsp r = fread(content, 1, buf[0], o->f);
303 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
304 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
305 2d9e6abf 2022-05-04 stsp goto done;
306 2d9e6abf 2022-05-04 stsp }
307 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
308 5060d5a1 2022-01-10 stsp content, buf[0]);
309 5060d5a1 2022-01-10 stsp if (err)
310 2d9e6abf 2022-05-04 stsp goto done;
311 5060d5a1 2022-01-10 stsp n += buf[0];
312 5060d5a1 2022-01-10 stsp }
313 5060d5a1 2022-01-10 stsp }
314 5060d5a1 2022-01-10 stsp }
315 5060d5a1 2022-01-10 stsp
316 2d9e6abf 2022-05-04 stsp err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
317 2d9e6abf 2022-05-04 stsp NULL, NULL, delta_buf, 0, len);
318 2d9e6abf 2022-05-04 stsp if (err)
319 2d9e6abf 2022-05-04 stsp goto done;
320 2d9e6abf 2022-05-04 stsp
321 5060d5a1 2022-01-10 stsp m->delta_len = len;
322 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
323 2d9e6abf 2022-05-04 stsp done:
324 2d9e6abf 2022-05-04 stsp free(delta_buf);
325 2d9e6abf 2022-05-04 stsp return err;
326 e6bcace5 2021-06-22 stsp }
327 e6bcace5 2021-06-22 stsp
328 74881701 2021-10-15 stsp static const struct got_error *
329 74881701 2021-10-15 stsp encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
330 74881701 2021-10-15 stsp struct got_delta_instruction *deltas, int ndeltas,
331 a319ca8c 2021-10-15 stsp off_t base_size, FILE *f)
332 a319ca8c 2021-10-15 stsp {
333 2d9e6abf 2022-05-04 stsp const struct got_error *err;
334 a319ca8c 2021-10-15 stsp unsigned char buf[16], *bp;
335 a319ca8c 2021-10-15 stsp int i, j;
336 a319ca8c 2021-10-15 stsp off_t n;
337 2d9e6abf 2022-05-04 stsp struct got_deflate_buf zb;
338 a319ca8c 2021-10-15 stsp struct got_delta_instruction *d;
339 2d9e6abf 2022-05-04 stsp off_t delta_len = 0, compressed_len = 0;
340 a319ca8c 2021-10-15 stsp
341 2d9e6abf 2022-05-04 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
342 2d9e6abf 2022-05-04 stsp if (err)
343 2d9e6abf 2022-05-04 stsp return err;
344 2d9e6abf 2022-05-04 stsp
345 a319ca8c 2021-10-15 stsp /* base object size */
346 a319ca8c 2021-10-15 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
347 a319ca8c 2021-10-15 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
348 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
349 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
350 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
351 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
352 a319ca8c 2021-10-15 stsp }
353 a319ca8c 2021-10-15 stsp
354 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
355 2d9e6abf 2022-05-04 stsp buf, 0, i, f, NULL);
356 2d9e6abf 2022-05-04 stsp if (err)
357 2d9e6abf 2022-05-04 stsp goto done;
358 2d9e6abf 2022-05-04 stsp delta_len += i;
359 2d9e6abf 2022-05-04 stsp
360 a319ca8c 2021-10-15 stsp /* target object size */
361 a319ca8c 2021-10-15 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
362 a319ca8c 2021-10-15 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
363 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
364 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
365 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
366 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
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, &compressed_len,
370 2d9e6abf 2022-05-04 stsp buf, 0, i, 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 += i;
374 2d9e6abf 2022-05-04 stsp
375 a319ca8c 2021-10-15 stsp for (j = 0; j < ndeltas; j++) {
376 a319ca8c 2021-10-15 stsp d = &deltas[j];
377 a319ca8c 2021-10-15 stsp if (d->copy) {
378 a319ca8c 2021-10-15 stsp n = d->offset;
379 a319ca8c 2021-10-15 stsp bp = &buf[1];
380 a319ca8c 2021-10-15 stsp buf[0] = GOT_DELTA_BASE_COPY;
381 a319ca8c 2021-10-15 stsp for (i = 0; i < 4; i++) {
382 a319ca8c 2021-10-15 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
383 a319ca8c 2021-10-15 stsp buf[0] |= 1 << i;
384 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
385 a319ca8c 2021-10-15 stsp n >>= 8;
386 a319ca8c 2021-10-15 stsp if (n == 0)
387 a319ca8c 2021-10-15 stsp break;
388 a319ca8c 2021-10-15 stsp }
389 a319ca8c 2021-10-15 stsp n = d->len;
390 a319ca8c 2021-10-15 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
391 a319ca8c 2021-10-15 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
392 a319ca8c 2021-10-15 stsp for (i = 0; i < 3 && n > 0; i++) {
393 a319ca8c 2021-10-15 stsp buf[0] |= 1 << (i + 4);
394 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
395 a319ca8c 2021-10-15 stsp n >>= 8;
396 a319ca8c 2021-10-15 stsp }
397 a319ca8c 2021-10-15 stsp }
398 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
399 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, bp - buf, f, NULL);
400 2d9e6abf 2022-05-04 stsp if (err)
401 2d9e6abf 2022-05-04 stsp goto done;
402 2d9e6abf 2022-05-04 stsp delta_len += (bp - buf);
403 64a8571e 2022-01-07 stsp } else if (o->f == NULL) {
404 64a8571e 2022-01-07 stsp n = 0;
405 64a8571e 2022-01-07 stsp while (n != d->len) {
406 64a8571e 2022-01-07 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
407 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
408 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
409 2d9e6abf 2022-05-04 stsp if (err)
410 2d9e6abf 2022-05-04 stsp goto done;
411 2d9e6abf 2022-05-04 stsp delta_len++;
412 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
413 2d9e6abf 2022-05-04 stsp &compressed_len,
414 2d9e6abf 2022-05-04 stsp o->data + o->hdrlen + d->offset + n, 0,
415 2d9e6abf 2022-05-04 stsp buf[0], f, NULL);
416 2d9e6abf 2022-05-04 stsp if (err)
417 2d9e6abf 2022-05-04 stsp goto done;
418 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
419 64a8571e 2022-01-07 stsp n += buf[0];
420 64a8571e 2022-01-07 stsp }
421 a319ca8c 2021-10-15 stsp } else {
422 a319ca8c 2021-10-15 stsp char content[128];
423 a319ca8c 2021-10-15 stsp size_t r;
424 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
425 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
426 2d9e6abf 2022-05-04 stsp goto done;
427 2d9e6abf 2022-05-04 stsp }
428 a319ca8c 2021-10-15 stsp n = 0;
429 a319ca8c 2021-10-15 stsp while (n != d->len) {
430 a319ca8c 2021-10-15 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
431 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
432 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
433 2d9e6abf 2022-05-04 stsp if (err)
434 2d9e6abf 2022-05-04 stsp goto done;
435 2d9e6abf 2022-05-04 stsp delta_len++;
436 a319ca8c 2021-10-15 stsp r = fread(content, 1, buf[0], o->f);
437 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
438 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
439 2d9e6abf 2022-05-04 stsp goto done;
440 2d9e6abf 2022-05-04 stsp }
441 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
442 2d9e6abf 2022-05-04 stsp &compressed_len, content, 0, buf[0], f,
443 2d9e6abf 2022-05-04 stsp NULL);
444 2d9e6abf 2022-05-04 stsp if (err)
445 2d9e6abf 2022-05-04 stsp goto done;
446 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
447 a319ca8c 2021-10-15 stsp n += buf[0];
448 a319ca8c 2021-10-15 stsp }
449 a319ca8c 2021-10-15 stsp }
450 a319ca8c 2021-10-15 stsp }
451 e6bcace5 2021-06-22 stsp
452 2d9e6abf 2022-05-04 stsp err = got_deflate_flush(&zb, f, NULL, &compressed_len);
453 2d9e6abf 2022-05-04 stsp if (err)
454 2d9e6abf 2022-05-04 stsp goto done;
455 2d9e6abf 2022-05-04 stsp
456 2d9e6abf 2022-05-04 stsp /* sanity check */
457 2d9e6abf 2022-05-04 stsp if (compressed_len != ftello(f) - m->delta_offset) {
458 2d9e6abf 2022-05-04 stsp err = got_error(GOT_ERR_COMPRESSION);
459 2d9e6abf 2022-05-04 stsp goto done;
460 2d9e6abf 2022-05-04 stsp }
461 2d9e6abf 2022-05-04 stsp
462 2d9e6abf 2022-05-04 stsp m->delta_len = delta_len;
463 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
464 2d9e6abf 2022-05-04 stsp done:
465 2d9e6abf 2022-05-04 stsp got_deflate_end(&zb);
466 2d9e6abf 2022-05-04 stsp return err;
467 a319ca8c 2021-10-15 stsp }
468 211cfef0 2022-01-05 stsp
469 211cfef0 2022-01-05 stsp static const struct got_error *
470 211cfef0 2022-01-05 stsp report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
471 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
472 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
473 b8af7c06 2022-03-15 stsp int nobj_written)
474 211cfef0 2022-01-05 stsp {
475 211cfef0 2022-01-05 stsp const struct got_error *err;
476 211cfef0 2022-01-05 stsp int elapsed;
477 211cfef0 2022-01-05 stsp
478 211cfef0 2022-01-05 stsp if (progress_cb == NULL)
479 211cfef0 2022-01-05 stsp return NULL;
480 211cfef0 2022-01-05 stsp
481 211cfef0 2022-01-05 stsp err = got_ratelimit_check(&elapsed, rl);
482 211cfef0 2022-01-05 stsp if (err || !elapsed)
483 211cfef0 2022-01-05 stsp return err;
484 a319ca8c 2021-10-15 stsp
485 b8af7c06 2022-03-15 stsp return progress_cb(progress_arg, ncolored, nfound, ntrees,
486 b8af7c06 2022-03-15 stsp packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
487 211cfef0 2022-01-05 stsp }
488 a319ca8c 2021-10-15 stsp
489 e6bcace5 2021-06-22 stsp static const struct got_error *
490 67fd6849 2022-02-13 stsp add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
491 67fd6849 2022-02-13 stsp {
492 67fd6849 2022-02-13 stsp if (v->nmeta == v->metasz){
493 67fd6849 2022-02-13 stsp size_t newsize = 2 * v->metasz;
494 67fd6849 2022-02-13 stsp struct got_pack_meta **new;
495 67fd6849 2022-02-13 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
496 67fd6849 2022-02-13 stsp if (new == NULL)
497 67fd6849 2022-02-13 stsp return got_error_from_errno("reallocarray");
498 67fd6849 2022-02-13 stsp v->meta = new;
499 5e91dae4 2022-08-30 stsp v->metasz = newsize;
500 67fd6849 2022-02-13 stsp }
501 67fd6849 2022-02-13 stsp
502 67fd6849 2022-02-13 stsp v->meta[v->nmeta++] = m;
503 67fd6849 2022-02-13 stsp return NULL;
504 67fd6849 2022-02-13 stsp }
505 67fd6849 2022-02-13 stsp
506 67fd6849 2022-02-13 stsp static const struct got_error *
507 67fd6849 2022-02-13 stsp find_pack_for_reuse(struct got_packidx **best_packidx,
508 67fd6849 2022-02-13 stsp struct got_repository *repo)
509 67fd6849 2022-02-13 stsp {
510 9b576444 2022-03-14 stsp const struct got_error *err = NULL;
511 67fd6849 2022-02-13 stsp struct got_pathlist_entry *pe;
512 67fd6849 2022-02-13 stsp const char *best_packidx_path = NULL;
513 67fd6849 2022-02-13 stsp int nobj_max = 0;
514 67fd6849 2022-02-13 stsp
515 67fd6849 2022-02-13 stsp *best_packidx = NULL;
516 67fd6849 2022-02-13 stsp
517 9b576444 2022-03-14 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
518 67fd6849 2022-02-13 stsp const char *path_packidx = pe->path;
519 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
520 67fd6849 2022-02-13 stsp int nobj;
521 67fd6849 2022-02-13 stsp
522 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
523 67fd6849 2022-02-13 stsp if (err)
524 67fd6849 2022-02-13 stsp break;
525 67fd6849 2022-02-13 stsp
526 67fd6849 2022-02-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
527 67fd6849 2022-02-13 stsp if (nobj > nobj_max) {
528 67fd6849 2022-02-13 stsp best_packidx_path = path_packidx;
529 67fd6849 2022-02-13 stsp nobj_max = nobj;
530 67fd6849 2022-02-13 stsp }
531 67fd6849 2022-02-13 stsp }
532 67fd6849 2022-02-13 stsp
533 67fd6849 2022-02-13 stsp if (best_packidx_path) {
534 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
535 67fd6849 2022-02-13 stsp repo);
536 67fd6849 2022-02-13 stsp }
537 67fd6849 2022-02-13 stsp
538 67fd6849 2022-02-13 stsp return err;
539 67fd6849 2022-02-13 stsp }
540 67fd6849 2022-02-13 stsp
541 fae7e038 2022-05-07 stsp struct send_id_arg {
542 fae7e038 2022-05-07 stsp struct imsgbuf *ibuf;
543 fae7e038 2022-05-07 stsp struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
544 fae7e038 2022-05-07 stsp size_t nids;
545 67fd6849 2022-02-13 stsp };
546 67fd6849 2022-02-13 stsp
547 67fd6849 2022-02-13 stsp static const struct got_error *
548 fae7e038 2022-05-07 stsp send_id(struct got_object_id *id, void *data, void *arg)
549 67fd6849 2022-02-13 stsp {
550 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
551 fae7e038 2022-05-07 stsp struct send_id_arg *a = arg;
552 67fd6849 2022-02-13 stsp
553 fae7e038 2022-05-07 stsp a->ids[a->nids++] = id;
554 fae7e038 2022-05-07 stsp
555 fae7e038 2022-05-07 stsp if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
556 fae7e038 2022-05-07 stsp err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
557 67fd6849 2022-02-13 stsp if (err)
558 67fd6849 2022-02-13 stsp return err;
559 fae7e038 2022-05-07 stsp a->nids = 0;
560 67fd6849 2022-02-13 stsp }
561 67fd6849 2022-02-13 stsp
562 fae7e038 2022-05-07 stsp return NULL;
563 61af9b21 2022-06-28 stsp }
564 61af9b21 2022-06-28 stsp
565 61af9b21 2022-06-28 stsp static const struct got_error *
566 61af9b21 2022-06-28 stsp send_idset(struct imsgbuf *ibuf, struct got_object_idset *idset)
567 61af9b21 2022-06-28 stsp {
568 61af9b21 2022-06-28 stsp const struct got_error *err;
569 61af9b21 2022-06-28 stsp struct send_id_arg sia;
570 61af9b21 2022-06-28 stsp
571 61af9b21 2022-06-28 stsp memset(&sia, 0, sizeof(sia));
572 61af9b21 2022-06-28 stsp sia.ibuf = ibuf;
573 61af9b21 2022-06-28 stsp err = got_object_idset_for_each(idset, send_id, &sia);
574 61af9b21 2022-06-28 stsp if (err)
575 61af9b21 2022-06-28 stsp return err;
576 61af9b21 2022-06-28 stsp
577 61af9b21 2022-06-28 stsp if (sia.nids > 0) {
578 61af9b21 2022-06-28 stsp err = got_privsep_send_object_idlist(ibuf, sia.ids, sia.nids);
579 61af9b21 2022-06-28 stsp if (err)
580 61af9b21 2022-06-28 stsp return err;
581 61af9b21 2022-06-28 stsp }
582 61af9b21 2022-06-28 stsp
583 61af9b21 2022-06-28 stsp return got_privsep_send_object_idlist_done(ibuf);
584 fae7e038 2022-05-07 stsp }
585 67fd6849 2022-02-13 stsp
586 61af9b21 2022-06-28 stsp
587 fae7e038 2022-05-07 stsp static const struct got_error *
588 fae7e038 2022-05-07 stsp recv_reused_delta(struct got_imsg_reused_delta *delta,
589 fae7e038 2022-05-07 stsp struct got_object_idset *idset, struct got_pack_metavec *v)
590 fae7e038 2022-05-07 stsp {
591 fae7e038 2022-05-07 stsp struct got_pack_meta *m, *base;
592 67fd6849 2022-02-13 stsp
593 fae7e038 2022-05-07 stsp if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
594 fae7e038 2022-05-07 stsp delta->delta_offset +
595 fae7e038 2022-05-07 stsp delta->delta_compressed_size < delta->delta_offset)
596 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_BAD_PACKFILE);
597 67fd6849 2022-02-13 stsp
598 fae7e038 2022-05-07 stsp m = got_object_idset_get(idset, &delta->id);
599 fae7e038 2022-05-07 stsp if (m == NULL)
600 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_OBJ);
601 fae7e038 2022-05-07 stsp
602 fae7e038 2022-05-07 stsp base = got_object_idset_get(idset, &delta->base_id);
603 fae7e038 2022-05-07 stsp if (base == NULL)
604 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_OBJ);
605 fae7e038 2022-05-07 stsp
606 fae7e038 2022-05-07 stsp m->delta_len = delta->delta_size;
607 fae7e038 2022-05-07 stsp m->delta_compressed_len = delta->delta_compressed_size;
608 fae7e038 2022-05-07 stsp m->delta_offset = delta->delta_out_offset;
609 fae7e038 2022-05-07 stsp m->prev = base;
610 fae7e038 2022-05-07 stsp m->size = delta->result_size;
611 fae7e038 2022-05-07 stsp m->reused_delta_offset = delta->delta_offset;
612 fae7e038 2022-05-07 stsp m->base_obj_id = got_object_id_dup(&delta->base_id);
613 fae7e038 2022-05-07 stsp if (m->base_obj_id == NULL)
614 fae7e038 2022-05-07 stsp return got_error_from_errno("got_object_id_dup");
615 fae7e038 2022-05-07 stsp
616 fae7e038 2022-05-07 stsp return add_meta(m, v);
617 3d589bee 2022-06-25 stsp }
618 3d589bee 2022-06-25 stsp
619 3d589bee 2022-06-25 stsp static const struct got_error *
620 61af9b21 2022-06-28 stsp cache_pack_for_packidx(struct got_pack **pack, struct got_packidx *packidx,
621 61af9b21 2022-06-28 stsp struct got_repository *repo)
622 3d589bee 2022-06-25 stsp {
623 61af9b21 2022-06-28 stsp const struct got_error *err;
624 3d589bee 2022-06-25 stsp char *path_packfile = NULL;
625 3d589bee 2022-06-25 stsp
626 3d589bee 2022-06-25 stsp err = got_packidx_get_packfile_path(&path_packfile,
627 3d589bee 2022-06-25 stsp packidx->path_packidx);
628 3d589bee 2022-06-25 stsp if (err)
629 3d589bee 2022-06-25 stsp return err;
630 3d589bee 2022-06-25 stsp
631 3d589bee 2022-06-25 stsp *pack = got_repo_get_cached_pack(repo, path_packfile);
632 3d589bee 2022-06-25 stsp if (*pack == NULL) {
633 3d589bee 2022-06-25 stsp err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
634 3d589bee 2022-06-25 stsp if (err)
635 3d589bee 2022-06-25 stsp goto done;
636 3d589bee 2022-06-25 stsp }
637 3d589bee 2022-06-25 stsp if ((*pack)->privsep_child == NULL) {
638 3d589bee 2022-06-25 stsp err = got_pack_start_privsep_child(*pack, packidx);
639 3d589bee 2022-06-25 stsp if (err)
640 3d589bee 2022-06-25 stsp goto done;
641 3d589bee 2022-06-25 stsp }
642 61af9b21 2022-06-28 stsp done:
643 61af9b21 2022-06-28 stsp free(path_packfile);
644 61af9b21 2022-06-28 stsp return err;
645 61af9b21 2022-06-28 stsp }
646 3d589bee 2022-06-25 stsp
647 61af9b21 2022-06-28 stsp static const struct got_error *
648 61af9b21 2022-06-28 stsp prepare_delta_reuse(struct got_pack *pack, struct got_packidx *packidx,
649 61af9b21 2022-06-28 stsp int delta_outfd, struct got_repository *repo)
650 61af9b21 2022-06-28 stsp {
651 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
652 61af9b21 2022-06-28 stsp
653 61af9b21 2022-06-28 stsp if (!pack->child_has_delta_outfd) {
654 3d589bee 2022-06-25 stsp int outfd_child;
655 3d589bee 2022-06-25 stsp outfd_child = dup(delta_outfd);
656 3d589bee 2022-06-25 stsp if (outfd_child == -1) {
657 3d589bee 2022-06-25 stsp err = got_error_from_errno("dup");
658 3d589bee 2022-06-25 stsp goto done;
659 3d589bee 2022-06-25 stsp }
660 3d589bee 2022-06-25 stsp err = got_privsep_send_raw_delta_outfd(
661 61af9b21 2022-06-28 stsp pack->privsep_child->ibuf, outfd_child);
662 3d589bee 2022-06-25 stsp if (err)
663 3d589bee 2022-06-25 stsp goto done;
664 61af9b21 2022-06-28 stsp pack->child_has_delta_outfd = 1;
665 3d589bee 2022-06-25 stsp }
666 3d589bee 2022-06-25 stsp
667 61af9b21 2022-06-28 stsp err = got_privsep_send_delta_reuse_req(pack->privsep_child->ibuf);
668 3d589bee 2022-06-25 stsp done:
669 3d589bee 2022-06-25 stsp return err;
670 67fd6849 2022-02-13 stsp }
671 67fd6849 2022-02-13 stsp
672 3d589bee 2022-06-25 stsp
673 67fd6849 2022-02-13 stsp static const struct got_error *
674 67fd6849 2022-02-13 stsp search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
675 b8af7c06 2022-03-15 stsp int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
676 b8af7c06 2022-03-15 stsp struct got_repository *repo,
677 67fd6849 2022-02-13 stsp got_pack_progress_cb progress_cb, void *progress_arg,
678 67fd6849 2022-02-13 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
679 67fd6849 2022-02-13 stsp {
680 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
681 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
682 67fd6849 2022-02-13 stsp struct got_pack *pack;
683 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
684 fae7e038 2022-05-07 stsp size_t ndeltas, i;
685 67fd6849 2022-02-13 stsp
686 67fd6849 2022-02-13 stsp err = find_pack_for_reuse(&packidx, repo);
687 67fd6849 2022-02-13 stsp if (err)
688 67fd6849 2022-02-13 stsp return err;
689 67fd6849 2022-02-13 stsp
690 67fd6849 2022-02-13 stsp if (packidx == NULL)
691 67fd6849 2022-02-13 stsp return NULL;
692 67fd6849 2022-02-13 stsp
693 61af9b21 2022-06-28 stsp err = cache_pack_for_packidx(&pack, packidx, repo);
694 67fd6849 2022-02-13 stsp if (err)
695 67fd6849 2022-02-13 stsp return err;
696 67fd6849 2022-02-13 stsp
697 61af9b21 2022-06-28 stsp err = prepare_delta_reuse(pack, packidx, delta_cache_fd, repo);
698 fae7e038 2022-05-07 stsp if (err)
699 fae7e038 2022-05-07 stsp return err;
700 61af9b21 2022-06-28 stsp
701 61af9b21 2022-06-28 stsp err = send_idset(pack->privsep_child->ibuf, idset);
702 fae7e038 2022-05-07 stsp if (err)
703 fae7e038 2022-05-07 stsp return err;
704 67fd6849 2022-02-13 stsp
705 fae7e038 2022-05-07 stsp for (;;) {
706 fae7e038 2022-05-07 stsp int done = 0;
707 fae7e038 2022-05-07 stsp
708 fae7e038 2022-05-07 stsp if (cancel_cb) {
709 fae7e038 2022-05-07 stsp err = (*cancel_cb)(cancel_arg);
710 fae7e038 2022-05-07 stsp if (err)
711 fae7e038 2022-05-07 stsp break;
712 fae7e038 2022-05-07 stsp }
713 fae7e038 2022-05-07 stsp
714 fae7e038 2022-05-07 stsp err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
715 fae7e038 2022-05-07 stsp pack->privsep_child->ibuf);
716 fae7e038 2022-05-07 stsp if (err || done)
717 fae7e038 2022-05-07 stsp break;
718 fae7e038 2022-05-07 stsp
719 fae7e038 2022-05-07 stsp for (i = 0; i < ndeltas; i++) {
720 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta *delta = &deltas[i];
721 fae7e038 2022-05-07 stsp err = recv_reused_delta(delta, idset, v);
722 fae7e038 2022-05-07 stsp if (err)
723 fae7e038 2022-05-07 stsp goto done;
724 fae7e038 2022-05-07 stsp }
725 fae7e038 2022-05-07 stsp
726 fae7e038 2022-05-07 stsp err = report_progress(progress_cb, progress_arg, rl,
727 fae7e038 2022-05-07 stsp ncolored, nfound, ntrees, 0L, ncommits,
728 fae7e038 2022-05-07 stsp got_object_idset_num_elements(idset), v->nmeta, 0);
729 fae7e038 2022-05-07 stsp if (err)
730 fae7e038 2022-05-07 stsp break;
731 fae7e038 2022-05-07 stsp }
732 67fd6849 2022-02-13 stsp done:
733 67fd6849 2022-02-13 stsp return err;
734 67fd6849 2022-02-13 stsp }
735 67fd6849 2022-02-13 stsp
736 67fd6849 2022-02-13 stsp static const struct got_error *
737 b8af7c06 2022-03-15 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
738 b8af7c06 2022-03-15 stsp int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
739 b8af7c06 2022-03-15 stsp struct got_repository *repo,
740 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
741 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
742 e6bcace5 2021-06-22 stsp {
743 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
744 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
745 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
746 74881701 2021-10-15 stsp struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
747 5060d5a1 2022-01-10 stsp int i, j, ndeltas, best_ndeltas;
748 5060d5a1 2022-01-10 stsp off_t size, best_size;
749 4f4d853e 2021-10-24 stsp const int max_base_candidates = 3;
750 402a5ec1 2022-01-10 stsp size_t delta_memsize = 0;
751 adb4bbb2 2022-05-20 stsp const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
752 cc7a354a 2021-10-15 stsp int outfd = -1;
753 d6a28ffe 2022-05-20 op uint32_t delta_seed;
754 d6a28ffe 2022-05-20 op
755 d6a28ffe 2022-05-20 op delta_seed = arc4random();
756 e6bcace5 2021-06-22 stsp
757 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
758 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
759 e6bcace5 2021-06-22 stsp if (cancel_cb) {
760 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
761 e6bcace5 2021-06-22 stsp if (err)
762 e6bcace5 2021-06-22 stsp break;
763 e6bcace5 2021-06-22 stsp }
764 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
765 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
766 b8af7c06 2022-03-15 stsp nreused + i, 0);
767 211cfef0 2022-01-05 stsp if (err)
768 211cfef0 2022-01-05 stsp goto done;
769 e6bcace5 2021-06-22 stsp m = meta[i];
770 e6bcace5 2021-06-22 stsp
771 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
772 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
773 e6bcace5 2021-06-22 stsp continue;
774 e6bcace5 2021-06-22 stsp
775 94dac27c 2021-10-15 stsp err = got_object_raw_open(&raw, &outfd, repo, &m->id);
776 e6bcace5 2021-06-22 stsp if (err)
777 e6bcace5 2021-06-22 stsp goto done;
778 600b755e 2021-10-14 stsp m->size = raw->size;
779 e6bcace5 2021-06-22 stsp
780 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
781 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&m->dtab, raw->data,
782 d6a28ffe 2022-05-20 op raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
783 64a8571e 2022-01-07 stsp } else {
784 64a8571e 2022-01-07 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
785 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed);
786 64a8571e 2022-01-07 stsp }
787 e6bcace5 2021-06-22 stsp if (err)
788 e6bcace5 2021-06-22 stsp goto done;
789 e6bcace5 2021-06-22 stsp
790 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
791 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
792 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
793 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
794 e6bcace5 2021-06-22 stsp n->dtab = NULL;
795 e6bcace5 2021-06-22 stsp }
796 e6bcace5 2021-06-22 stsp
797 74881701 2021-10-15 stsp best_size = raw->size;
798 74881701 2021-10-15 stsp best_ndeltas = 0;
799 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
800 e6bcace5 2021-06-22 stsp if (cancel_cb) {
801 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
802 e6bcace5 2021-06-22 stsp if (err)
803 e6bcace5 2021-06-22 stsp goto done;
804 e6bcace5 2021-06-22 stsp }
805 e6bcace5 2021-06-22 stsp base = meta[j];
806 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
807 22edbce7 2021-10-24 stsp if (base->nchain >= 128 ||
808 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
809 e6bcace5 2021-06-22 stsp continue;
810 e6bcace5 2021-06-22 stsp
811 d3c116bf 2021-10-15 stsp err = got_object_raw_open(&base_raw, &outfd, repo,
812 94dac27c 2021-10-15 stsp &base->id);
813 e6bcace5 2021-06-22 stsp if (err)
814 e6bcace5 2021-06-22 stsp goto done;
815 67fd6849 2022-02-13 stsp
816 64a8571e 2022-01-07 stsp if (raw->f == NULL && base_raw->f == NULL) {
817 64a8571e 2022-01-07 stsp err = got_deltify_mem_mem(&deltas, &ndeltas,
818 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
819 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
820 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
821 64a8571e 2022-01-07 stsp base_raw->hdrlen,
822 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
823 64a8571e 2022-01-07 stsp } else if (raw->f == NULL) {
824 64a8571e 2022-01-07 stsp err = got_deltify_mem_file(&deltas, &ndeltas,
825 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
826 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
827 64a8571e 2022-01-07 stsp base->dtab, base_raw->f,
828 64a8571e 2022-01-07 stsp base_raw->hdrlen,
829 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
830 64a8571e 2022-01-07 stsp } else if (base_raw->f == NULL) {
831 64a8571e 2022-01-07 stsp err = got_deltify_file_mem(&deltas, &ndeltas,
832 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
833 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
834 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
835 64a8571e 2022-01-07 stsp base_raw->hdrlen,
836 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
837 64a8571e 2022-01-07 stsp } else {
838 64a8571e 2022-01-07 stsp err = got_deltify(&deltas, &ndeltas,
839 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
840 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
841 64a8571e 2022-01-07 stsp base->dtab, base_raw->f, base_raw->hdrlen,
842 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
843 64a8571e 2022-01-07 stsp }
844 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
845 e6bcace5 2021-06-22 stsp base_raw = NULL;
846 e6bcace5 2021-06-22 stsp if (err)
847 e6bcace5 2021-06-22 stsp goto done;
848 e6bcace5 2021-06-22 stsp
849 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
850 74881701 2021-10-15 stsp if (size + 32 < best_size){
851 e6bcace5 2021-06-22 stsp /*
852 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
853 e6bcace5 2021-06-22 stsp * replace it.
854 e6bcace5 2021-06-22 stsp */
855 74881701 2021-10-15 stsp best_size = size;
856 74881701 2021-10-15 stsp free(best_deltas);
857 74881701 2021-10-15 stsp best_deltas = deltas;
858 74881701 2021-10-15 stsp best_ndeltas = ndeltas;
859 74881701 2021-10-15 stsp deltas = NULL;
860 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
861 e6bcace5 2021-06-22 stsp m->prev = base;
862 e6bcace5 2021-06-22 stsp m->head = base->head;
863 e6bcace5 2021-06-22 stsp if (m->head == NULL)
864 e6bcace5 2021-06-22 stsp m->head = base;
865 e6bcace5 2021-06-22 stsp } else {
866 e6bcace5 2021-06-22 stsp free(deltas);
867 e6bcace5 2021-06-22 stsp deltas = NULL;
868 e6bcace5 2021-06-22 stsp ndeltas = 0;
869 e6bcace5 2021-06-22 stsp }
870 e6bcace5 2021-06-22 stsp }
871 e6bcace5 2021-06-22 stsp
872 74881701 2021-10-15 stsp if (best_ndeltas > 0) {
873 402a5ec1 2022-01-10 stsp if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
874 402a5ec1 2022-01-10 stsp delta_memsize + best_size <= max_delta_memsize) {
875 402a5ec1 2022-01-10 stsp delta_memsize += best_size;
876 5060d5a1 2022-01-10 stsp err = encode_delta_in_mem(m, raw, best_deltas,
877 5060d5a1 2022-01-10 stsp best_ndeltas, best_size, m->prev->size);
878 5060d5a1 2022-01-10 stsp } else {
879 5060d5a1 2022-01-10 stsp m->delta_offset = ftello(delta_cache);
880 5060d5a1 2022-01-10 stsp err = encode_delta(m, raw, best_deltas,
881 5060d5a1 2022-01-10 stsp best_ndeltas, m->prev->size, delta_cache);
882 5060d5a1 2022-01-10 stsp }
883 74881701 2021-10-15 stsp free(best_deltas);
884 74881701 2021-10-15 stsp best_deltas = NULL;
885 74881701 2021-10-15 stsp best_ndeltas = 0;
886 74881701 2021-10-15 stsp if (err)
887 74881701 2021-10-15 stsp goto done;
888 74881701 2021-10-15 stsp }
889 74881701 2021-10-15 stsp
890 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
891 e6bcace5 2021-06-22 stsp raw = NULL;
892 e6bcace5 2021-06-22 stsp }
893 e6bcace5 2021-06-22 stsp done:
894 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
895 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
896 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
897 e6bcace5 2021-06-22 stsp }
898 e6bcace5 2021-06-22 stsp if (raw)
899 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
900 e6bcace5 2021-06-22 stsp if (base_raw)
901 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
902 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
903 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
904 74881701 2021-10-15 stsp free(deltas);
905 74881701 2021-10-15 stsp free(best_deltas);
906 e6bcace5 2021-06-22 stsp return err;
907 e6bcace5 2021-06-22 stsp }
908 e6bcace5 2021-06-22 stsp
909 e6bcace5 2021-06-22 stsp static const struct got_error *
910 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
911 05118f5a 2021-06-22 stsp struct got_repository *repo)
912 05118f5a 2021-06-22 stsp {
913 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
914 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
915 05118f5a 2021-06-22 stsp int idx;
916 05118f5a 2021-06-22 stsp
917 05118f5a 2021-06-22 stsp *found = 0;
918 05118f5a 2021-06-22 stsp
919 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
920 05118f5a 2021-06-22 stsp if (err == NULL)
921 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
922 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
923 05118f5a 2021-06-22 stsp err = NULL;
924 05118f5a 2021-06-22 stsp return err;
925 05118f5a 2021-06-22 stsp }
926 05118f5a 2021-06-22 stsp
927 05118f5a 2021-06-22 stsp static const struct got_error *
928 67fd6849 2022-02-13 stsp add_object(int want_meta, struct got_object_idset *idset,
929 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
930 d6a28ffe 2022-05-20 op time_t mtime, uint32_t seed, int loose_obj_only,
931 d6a28ffe 2022-05-20 op struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
932 6863cbf9 2022-03-21 stsp got_pack_progress_cb progress_cb, void *progress_arg,
933 6863cbf9 2022-03-21 stsp struct got_ratelimit *rl)
934 e6bcace5 2021-06-22 stsp {
935 e6bcace5 2021-06-22 stsp const struct got_error *err;
936 67fd6849 2022-02-13 stsp struct got_pack_meta *m = NULL;
937 e6bcace5 2021-06-22 stsp
938 05118f5a 2021-06-22 stsp if (loose_obj_only) {
939 05118f5a 2021-06-22 stsp int is_packed;
940 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
941 05118f5a 2021-06-22 stsp if (err)
942 05118f5a 2021-06-22 stsp return err;
943 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
944 05118f5a 2021-06-22 stsp return NULL;
945 05118f5a 2021-06-22 stsp }
946 05118f5a 2021-06-22 stsp
947 67fd6849 2022-02-13 stsp if (want_meta) {
948 d6a28ffe 2022-05-20 op err = alloc_meta(&m, id, path, obj_type, mtime, seed);
949 6863cbf9 2022-03-21 stsp if (err)
950 6863cbf9 2022-03-21 stsp return err;
951 6863cbf9 2022-03-21 stsp
952 6863cbf9 2022-03-21 stsp (*nfound)++;
953 6863cbf9 2022-03-21 stsp err = report_progress(progress_cb, progress_arg, rl,
954 6863cbf9 2022-03-21 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
955 bb6672b6 2022-04-14 tb if (err) {
956 bb6672b6 2022-04-14 tb clear_meta(m);
957 bb6672b6 2022-04-14 tb free(m);
958 67fd6849 2022-02-13 stsp return err;
959 bb6672b6 2022-04-14 tb }
960 e6bcace5 2021-06-22 stsp }
961 e6bcace5 2021-06-22 stsp
962 bb6672b6 2022-04-14 tb err = got_object_idset_add(idset, id, m);
963 bb6672b6 2022-04-14 tb if (err) {
964 bb6672b6 2022-04-14 tb clear_meta(m);
965 bb6672b6 2022-04-14 tb free(m);
966 bb6672b6 2022-04-14 tb }
967 bb6672b6 2022-04-14 tb return err;
968 e6bcace5 2021-06-22 stsp }
969 e6bcace5 2021-06-22 stsp
970 e6bcace5 2021-06-22 stsp static const struct got_error *
971 67fd6849 2022-02-13 stsp load_tree_entries(struct got_object_id_queue *ids, int want_meta,
972 2f8438b0 2022-05-04 stsp struct got_object_idset *idset, struct got_object_idset *idset_exclude,
973 0ab4c957 2022-06-13 stsp struct got_tree_object *tree,
974 d6a28ffe 2022-05-20 op const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
975 b8af7c06 2022-03-15 stsp int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
976 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
977 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
978 e6bcace5 2021-06-22 stsp {
979 e6bcace5 2021-06-22 stsp const struct got_error *err;
980 e6bcace5 2021-06-22 stsp char *p = NULL;
981 e6bcace5 2021-06-22 stsp int i;
982 b8af7c06 2022-03-15 stsp
983 b8af7c06 2022-03-15 stsp (*ntrees)++;
984 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
985 b8af7c06 2022-03-15 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
986 e6bcace5 2021-06-22 stsp if (err)
987 e6bcace5 2021-06-22 stsp return err;
988 e6bcace5 2021-06-22 stsp
989 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
990 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
991 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
992 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
993 e6bcace5 2021-06-22 stsp
994 e6bcace5 2021-06-22 stsp if (cancel_cb) {
995 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
996 e6bcace5 2021-06-22 stsp if (err)
997 e6bcace5 2021-06-22 stsp break;
998 e6bcace5 2021-06-22 stsp }
999 e6bcace5 2021-06-22 stsp
1000 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
1001 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset, id) ||
1002 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1003 cee6a7ea 2022-06-07 stsp continue;
1004 0ab4c957 2022-06-13 stsp
1005 0ab4c957 2022-06-13 stsp /*
1006 0ab4c957 2022-06-13 stsp * If got-read-pack is crawling trees for us then
1007 0ab4c957 2022-06-13 stsp * we are only here to collect blob IDs.
1008 0ab4c957 2022-06-13 stsp */
1009 0ab4c957 2022-06-13 stsp if (ids == NULL && S_ISDIR(mode))
1010 0ab4c957 2022-06-13 stsp continue;
1011 0ab4c957 2022-06-13 stsp
1012 0ab4c957 2022-06-13 stsp if (asprintf(&p, "%s%s%s", dpath,
1013 0ab4c957 2022-06-13 stsp got_path_is_root_dir(dpath) ? "" : "/",
1014 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
1015 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
1016 e6bcace5 2021-06-22 stsp break;
1017 e6bcace5 2021-06-22 stsp }
1018 e6bcace5 2021-06-22 stsp
1019 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
1020 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1021 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1022 e6bcace5 2021-06-22 stsp if (err)
1023 e6bcace5 2021-06-22 stsp break;
1024 3e6ceea0 2022-05-20 stsp qid->data = p;
1025 3e6ceea0 2022-05-20 stsp p = NULL;
1026 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
1027 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
1028 2f8438b0 2022-05-04 stsp err = add_object(want_meta,
1029 2f8438b0 2022-05-04 stsp want_meta ? idset : idset_exclude, id, p,
1030 d6a28ffe 2022-05-20 op GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
1031 d6a28ffe 2022-05-20 op repo, ncolored, nfound, ntrees,
1032 6863cbf9 2022-03-21 stsp progress_cb, progress_arg, rl);
1033 b8af7c06 2022-03-15 stsp if (err)
1034 b8af7c06 2022-03-15 stsp break;
1035 3e6ceea0 2022-05-20 stsp free(p);
1036 3e6ceea0 2022-05-20 stsp p = NULL;
1037 3e6ceea0 2022-05-20 stsp } else {
1038 3e6ceea0 2022-05-20 stsp free(p);
1039 3e6ceea0 2022-05-20 stsp p = NULL;
1040 e6bcace5 2021-06-22 stsp }
1041 e6bcace5 2021-06-22 stsp }
1042 e6bcace5 2021-06-22 stsp
1043 e6bcace5 2021-06-22 stsp free(p);
1044 e6bcace5 2021-06-22 stsp return err;
1045 e6bcace5 2021-06-22 stsp }
1046 e6bcace5 2021-06-22 stsp
1047 e6bcace5 2021-06-22 stsp static const struct got_error *
1048 67fd6849 2022-02-13 stsp load_tree(int want_meta, struct got_object_idset *idset,
1049 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1050 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
1051 d6a28ffe 2022-05-20 op uint32_t seed, struct got_repository *repo, int loose_obj_only,
1052 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
1053 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1054 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1055 e6bcace5 2021-06-22 stsp {
1056 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1057 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
1058 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1059 0ab4c957 2022-06-13 stsp struct got_tree_object *tree = NULL;
1060 e6bcace5 2021-06-22 stsp
1061 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, tree_id) ||
1062 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, tree_id))
1063 e6bcace5 2021-06-22 stsp return NULL;
1064 e6bcace5 2021-06-22 stsp
1065 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
1066 e6bcace5 2021-06-22 stsp if (err)
1067 3e6ceea0 2022-05-20 stsp return err;
1068 3e6ceea0 2022-05-20 stsp qid->data = strdup(dpath);
1069 3e6ceea0 2022-05-20 stsp if (qid->data == NULL) {
1070 3e6ceea0 2022-05-20 stsp err = got_error_from_errno("strdup");
1071 3e6ceea0 2022-05-20 stsp got_object_qid_free(qid);
1072 e6bcace5 2021-06-22 stsp return err;
1073 3e6ceea0 2022-05-20 stsp }
1074 e6bcace5 2021-06-22 stsp
1075 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
1076 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1077 e6bcace5 2021-06-22 stsp
1078 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
1079 3e6ceea0 2022-05-20 stsp const char *path;
1080 e6bcace5 2021-06-22 stsp if (cancel_cb) {
1081 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
1082 e6bcace5 2021-06-22 stsp if (err)
1083 e6bcace5 2021-06-22 stsp break;
1084 e6bcace5 2021-06-22 stsp }
1085 e6bcace5 2021-06-22 stsp
1086 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
1087 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
1088 3e6ceea0 2022-05-20 stsp path = qid->data;
1089 e6bcace5 2021-06-22 stsp
1090 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, &qid->id) ||
1091 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, &qid->id)) {
1092 3e6ceea0 2022-05-20 stsp free(qid->data);
1093 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1094 e6bcace5 2021-06-22 stsp continue;
1095 e6bcace5 2021-06-22 stsp }
1096 e6bcace5 2021-06-22 stsp
1097 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1098 3e6ceea0 2022-05-20 stsp &qid->id, path, GOT_OBJ_TYPE_TREE,
1099 d6a28ffe 2022-05-20 op mtime, seed, loose_obj_only, repo,
1100 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1101 e6bcace5 2021-06-22 stsp if (err) {
1102 3e6ceea0 2022-05-20 stsp free(qid->data);
1103 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1104 e6bcace5 2021-06-22 stsp break;
1105 e6bcace5 2021-06-22 stsp }
1106 e6bcace5 2021-06-22 stsp
1107 0ab4c957 2022-06-13 stsp err = got_object_open_as_tree(&tree, repo, &qid->id);
1108 0ab4c957 2022-06-13 stsp if (err) {
1109 0ab4c957 2022-06-13 stsp free(qid->data);
1110 0ab4c957 2022-06-13 stsp got_object_qid_free(qid);
1111 0ab4c957 2022-06-13 stsp break;
1112 0ab4c957 2022-06-13 stsp }
1113 0ab4c957 2022-06-13 stsp
1114 2f8438b0 2022-05-04 stsp err = load_tree_entries(&tree_ids, want_meta, idset,
1115 0ab4c957 2022-06-13 stsp idset_exclude, tree, path, mtime, seed, repo,
1116 0ab4c957 2022-06-13 stsp loose_obj_only, ncolored, nfound, ntrees,
1117 0ab4c957 2022-06-13 stsp progress_cb, progress_arg, rl,
1118 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1119 3e6ceea0 2022-05-20 stsp free(qid->data);
1120 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1121 e6bcace5 2021-06-22 stsp if (err)
1122 e6bcace5 2021-06-22 stsp break;
1123 0ab4c957 2022-06-13 stsp
1124 0ab4c957 2022-06-13 stsp got_object_tree_close(tree);
1125 0ab4c957 2022-06-13 stsp tree = NULL;
1126 e6bcace5 2021-06-22 stsp }
1127 e6bcace5 2021-06-22 stsp
1128 3e6ceea0 2022-05-20 stsp STAILQ_FOREACH(qid, &tree_ids, entry)
1129 3e6ceea0 2022-05-20 stsp free(qid->data);
1130 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
1131 0ab4c957 2022-06-13 stsp if (tree)
1132 0ab4c957 2022-06-13 stsp got_object_tree_close(tree);
1133 e6bcace5 2021-06-22 stsp return err;
1134 e6bcace5 2021-06-22 stsp }
1135 e6bcace5 2021-06-22 stsp
1136 e6bcace5 2021-06-22 stsp static const struct got_error *
1137 67fd6849 2022-02-13 stsp load_commit(int want_meta, struct got_object_idset *idset,
1138 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1139 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1140 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1141 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1142 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1143 e6bcace5 2021-06-22 stsp {
1144 e6bcace5 2021-06-22 stsp const struct got_error *err;
1145 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1146 e6bcace5 2021-06-22 stsp
1147 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
1148 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1149 e6bcace5 2021-06-22 stsp return NULL;
1150 05118f5a 2021-06-22 stsp
1151 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1152 05118f5a 2021-06-22 stsp int is_packed;
1153 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1154 05118f5a 2021-06-22 stsp if (err)
1155 05118f5a 2021-06-22 stsp return err;
1156 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1157 05118f5a 2021-06-22 stsp return NULL;
1158 05118f5a 2021-06-22 stsp }
1159 e6bcace5 2021-06-22 stsp
1160 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1161 e6bcace5 2021-06-22 stsp if (err)
1162 e6bcace5 2021-06-22 stsp return err;
1163 e6bcace5 2021-06-22 stsp
1164 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1165 2f8438b0 2022-05-04 stsp id, "", GOT_OBJ_TYPE_COMMIT,
1166 d6a28ffe 2022-05-20 op got_object_commit_get_committer_time(commit), seed,
1167 6863cbf9 2022-03-21 stsp loose_obj_only, repo,
1168 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1169 e6bcace5 2021-06-22 stsp if (err)
1170 e6bcace5 2021-06-22 stsp goto done;
1171 b8af7c06 2022-03-15 stsp
1172 2f8438b0 2022-05-04 stsp err = load_tree(want_meta, idset, idset_exclude,
1173 2f8438b0 2022-05-04 stsp got_object_commit_get_tree_id(commit),
1174 d6a28ffe 2022-05-20 op "", got_object_commit_get_committer_time(commit), seed,
1175 b8af7c06 2022-03-15 stsp repo, loose_obj_only, ncolored, nfound, ntrees,
1176 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1177 e6bcace5 2021-06-22 stsp done:
1178 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1179 e6bcace5 2021-06-22 stsp return err;
1180 e6bcace5 2021-06-22 stsp }
1181 e6bcace5 2021-06-22 stsp
1182 05118f5a 2021-06-22 stsp static const struct got_error *
1183 67fd6849 2022-02-13 stsp load_tag(int want_meta, struct got_object_idset *idset,
1184 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1185 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1186 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1187 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1188 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1189 05118f5a 2021-06-22 stsp {
1190 05118f5a 2021-06-22 stsp const struct got_error *err;
1191 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
1192 05118f5a 2021-06-22 stsp
1193 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
1194 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1195 05118f5a 2021-06-22 stsp return NULL;
1196 05118f5a 2021-06-22 stsp
1197 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1198 05118f5a 2021-06-22 stsp int is_packed;
1199 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1200 05118f5a 2021-06-22 stsp if (err)
1201 05118f5a 2021-06-22 stsp return err;
1202 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1203 05118f5a 2021-06-22 stsp return NULL;
1204 05118f5a 2021-06-22 stsp }
1205 05118f5a 2021-06-22 stsp
1206 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1207 05118f5a 2021-06-22 stsp if (err)
1208 05118f5a 2021-06-22 stsp return err;
1209 05118f5a 2021-06-22 stsp
1210 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1211 2f8438b0 2022-05-04 stsp id, "", GOT_OBJ_TYPE_TAG,
1212 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1213 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1214 05118f5a 2021-06-22 stsp if (err)
1215 05118f5a 2021-06-22 stsp goto done;
1216 05118f5a 2021-06-22 stsp
1217 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1218 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1219 2f8438b0 2022-05-04 stsp err = load_commit(want_meta, idset, idset_exclude,
1220 d6a28ffe 2022-05-20 op got_object_tag_get_object_id(tag), repo, seed,
1221 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1222 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1223 05118f5a 2021-06-22 stsp break;
1224 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1225 2f8438b0 2022-05-04 stsp err = load_tree(want_meta, idset, idset_exclude,
1226 67fd6849 2022-02-13 stsp got_object_tag_get_object_id(tag), "",
1227 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, repo,
1228 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1229 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1230 05118f5a 2021-06-22 stsp break;
1231 05118f5a 2021-06-22 stsp default:
1232 05118f5a 2021-06-22 stsp break;
1233 05118f5a 2021-06-22 stsp }
1234 05118f5a 2021-06-22 stsp
1235 05118f5a 2021-06-22 stsp done:
1236 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1237 05118f5a 2021-06-22 stsp return err;
1238 05118f5a 2021-06-22 stsp }
1239 05118f5a 2021-06-22 stsp
1240 e6bcace5 2021-06-22 stsp enum findtwixt_color {
1241 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
1242 e6bcace5 2021-06-22 stsp COLOR_DROP,
1243 70f8f24d 2022-04-14 stsp COLOR_SKIP,
1244 61af9b21 2022-06-28 stsp COLOR_MAX,
1245 e6bcace5 2021-06-22 stsp };
1246 e6bcace5 2021-06-22 stsp
1247 e6bcace5 2021-06-22 stsp static const struct got_error *
1248 61af9b21 2022-06-28 stsp paint_commit(struct got_object_qid *qid, intptr_t color)
1249 e6bcace5 2021-06-22 stsp {
1250 61af9b21 2022-06-28 stsp if (color < 0 || color >= COLOR_MAX)
1251 70f8f24d 2022-04-14 stsp return got_error(GOT_ERR_RANGE);
1252 e6bcace5 2021-06-22 stsp
1253 61af9b21 2022-06-28 stsp qid->data = (void *)color;
1254 e6bcace5 2021-06-22 stsp return NULL;
1255 e6bcace5 2021-06-22 stsp }
1256 e6bcace5 2021-06-22 stsp
1257 e6bcace5 2021-06-22 stsp static const struct got_error *
1258 70f8f24d 2022-04-14 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1259 61af9b21 2022-06-28 stsp intptr_t color, struct got_repository *repo)
1260 e6bcace5 2021-06-22 stsp {
1261 70f8f24d 2022-04-14 stsp const struct got_error *err;
1262 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1263 e6bcace5 2021-06-22 stsp
1264 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1265 e6bcace5 2021-06-22 stsp if (err)
1266 e6bcace5 2021-06-22 stsp return err;
1267 e6bcace5 2021-06-22 stsp
1268 70f8f24d 2022-04-14 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
1269 70f8f24d 2022-04-14 stsp return paint_commit(qid, color);
1270 e6bcace5 2021-06-22 stsp }
1271 e6bcace5 2021-06-22 stsp
1272 e6bcace5 2021-06-22 stsp struct append_id_arg {
1273 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1274 e6bcace5 2021-06-22 stsp int idx;
1275 70f8f24d 2022-04-14 stsp struct got_object_idset *drop;
1276 70f8f24d 2022-04-14 stsp struct got_object_idset *skip;
1277 e6bcace5 2021-06-22 stsp };
1278 e6bcace5 2021-06-22 stsp
1279 e6bcace5 2021-06-22 stsp static const struct got_error *
1280 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1281 e6bcace5 2021-06-22 stsp {
1282 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1283 e6bcace5 2021-06-22 stsp
1284 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(a->skip, id) ||
1285 70f8f24d 2022-04-14 stsp got_object_idset_contains(a->drop, id))
1286 70f8f24d 2022-04-14 stsp return NULL;
1287 70f8f24d 2022-04-14 stsp
1288 70f8f24d 2022-04-14 stsp a->array[++a->idx] = got_object_id_dup(id);
1289 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1290 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1291 e6bcace5 2021-06-22 stsp
1292 e6bcace5 2021-06-22 stsp return NULL;
1293 29e0594f 2022-04-09 stsp }
1294 29e0594f 2022-04-09 stsp
1295 29e0594f 2022-04-09 stsp static const struct got_error *
1296 61af9b21 2022-06-28 stsp queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1297 29e0594f 2022-04-09 stsp struct got_object_id_queue *ids, struct got_repository *repo)
1298 29e0594f 2022-04-09 stsp {
1299 29e0594f 2022-04-09 stsp const struct got_error *err;
1300 29e0594f 2022-04-09 stsp struct got_tag_object *tag = NULL;
1301 29e0594f 2022-04-09 stsp int obj_type;
1302 29e0594f 2022-04-09 stsp
1303 29e0594f 2022-04-09 stsp err = got_object_get_type(&obj_type, repo, id);
1304 29e0594f 2022-04-09 stsp if (err)
1305 29e0594f 2022-04-09 stsp return err;
1306 29e0594f 2022-04-09 stsp
1307 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1308 29e0594f 2022-04-09 stsp err = got_object_open_as_tag(&tag, repo, id);
1309 29e0594f 2022-04-09 stsp if (err)
1310 29e0594f 2022-04-09 stsp return err;
1311 29e0594f 2022-04-09 stsp obj_type = got_object_tag_get_object_type(tag);
1312 29e0594f 2022-04-09 stsp id = got_object_tag_get_object_id(tag);
1313 29e0594f 2022-04-09 stsp }
1314 29e0594f 2022-04-09 stsp
1315 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1316 29e0594f 2022-04-09 stsp err = queue_commit_id(ids, id, color, repo);
1317 29e0594f 2022-04-09 stsp if (err)
1318 29e0594f 2022-04-09 stsp goto done;
1319 29e0594f 2022-04-09 stsp }
1320 29e0594f 2022-04-09 stsp done:
1321 29e0594f 2022-04-09 stsp if (tag)
1322 29e0594f 2022-04-09 stsp got_object_tag_close(tag);
1323 29e0594f 2022-04-09 stsp return err;
1324 e6bcace5 2021-06-22 stsp }
1325 e6bcace5 2021-06-22 stsp
1326 61af9b21 2022-06-28 stsp struct recv_painted_commit_arg {
1327 61af9b21 2022-06-28 stsp int *ncolored;
1328 61af9b21 2022-06-28 stsp int *nqueued;
1329 61af9b21 2022-06-28 stsp int *nskip;
1330 61af9b21 2022-06-28 stsp struct got_object_id_queue *ids;
1331 61af9b21 2022-06-28 stsp struct got_object_idset *keep;
1332 61af9b21 2022-06-28 stsp struct got_object_idset *drop;
1333 61af9b21 2022-06-28 stsp struct got_object_idset *skip;
1334 61af9b21 2022-06-28 stsp got_pack_progress_cb progress_cb;
1335 61af9b21 2022-06-28 stsp void *progress_arg;
1336 61af9b21 2022-06-28 stsp struct got_ratelimit *rl;
1337 61af9b21 2022-06-28 stsp got_cancel_cb cancel_cb;
1338 61af9b21 2022-06-28 stsp void *cancel_arg;
1339 61af9b21 2022-06-28 stsp };
1340 61af9b21 2022-06-28 stsp
1341 e6bcace5 2021-06-22 stsp static const struct got_error *
1342 61af9b21 2022-06-28 stsp recv_painted_commit(void *arg, struct got_object_id *id, intptr_t color)
1343 61af9b21 2022-06-28 stsp {
1344 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
1345 61af9b21 2022-06-28 stsp struct recv_painted_commit_arg *a = arg;
1346 61af9b21 2022-06-28 stsp struct got_object_qid *qid, *tmp;
1347 61af9b21 2022-06-28 stsp
1348 61af9b21 2022-06-28 stsp if (a->cancel_cb) {
1349 61af9b21 2022-06-28 stsp err = a->cancel_cb(a->cancel_arg);
1350 61af9b21 2022-06-28 stsp if (err)
1351 61af9b21 2022-06-28 stsp return err;
1352 61af9b21 2022-06-28 stsp }
1353 61af9b21 2022-06-28 stsp
1354 61af9b21 2022-06-28 stsp switch (color) {
1355 61af9b21 2022-06-28 stsp case COLOR_KEEP:
1356 61af9b21 2022-06-28 stsp err = got_object_idset_add(a->keep, id, NULL);
1357 61af9b21 2022-06-28 stsp if (err)
1358 61af9b21 2022-06-28 stsp return err;
1359 61af9b21 2022-06-28 stsp (*a->ncolored)++;
1360 61af9b21 2022-06-28 stsp break;
1361 61af9b21 2022-06-28 stsp case COLOR_DROP:
1362 61af9b21 2022-06-28 stsp err = got_object_idset_add(a->drop, id, NULL);
1363 61af9b21 2022-06-28 stsp if (err)
1364 61af9b21 2022-06-28 stsp return err;
1365 61af9b21 2022-06-28 stsp (*a->ncolored)++;
1366 61af9b21 2022-06-28 stsp break;
1367 61af9b21 2022-06-28 stsp case COLOR_SKIP:
1368 61af9b21 2022-06-28 stsp err = got_object_idset_add(a->skip, id, NULL);
1369 61af9b21 2022-06-28 stsp if (err)
1370 61af9b21 2022-06-28 stsp return err;
1371 61af9b21 2022-06-28 stsp break;
1372 61af9b21 2022-06-28 stsp default:
1373 61af9b21 2022-06-28 stsp /* should not happen */
1374 61af9b21 2022-06-28 stsp return got_error_fmt(GOT_ERR_NOT_IMPL,
1375 756050ac 2022-07-19 op "%s invalid commit color %"PRIdPTR, __func__, color);
1376 61af9b21 2022-06-28 stsp }
1377 61af9b21 2022-06-28 stsp
1378 61af9b21 2022-06-28 stsp STAILQ_FOREACH_SAFE(qid, a->ids, entry, tmp) {
1379 61af9b21 2022-06-28 stsp if (got_object_id_cmp(&qid->id, id) != 0)
1380 61af9b21 2022-06-28 stsp continue;
1381 61af9b21 2022-06-28 stsp STAILQ_REMOVE(a->ids, qid, got_object_qid, entry);
1382 61af9b21 2022-06-28 stsp color = (intptr_t)qid->data;
1383 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1384 61af9b21 2022-06-28 stsp (*a->nqueued)--;
1385 61af9b21 2022-06-28 stsp if (color == COLOR_SKIP)
1386 61af9b21 2022-06-28 stsp (*a->nskip)--;
1387 61af9b21 2022-06-28 stsp break;
1388 61af9b21 2022-06-28 stsp }
1389 61af9b21 2022-06-28 stsp
1390 61af9b21 2022-06-28 stsp return report_progress(a->progress_cb, a->progress_arg, a->rl,
1391 61af9b21 2022-06-28 stsp *a->ncolored, 0, 0, 0L, 0, 0, 0, 0);
1392 61af9b21 2022-06-28 stsp }
1393 61af9b21 2022-06-28 stsp
1394 61af9b21 2022-06-28 stsp static const struct got_error *
1395 61af9b21 2022-06-28 stsp paint_packed_commits(struct got_pack *pack, struct got_object_id *id,
1396 61af9b21 2022-06-28 stsp int idx, intptr_t color, int *ncolored, int *nqueued, int *nskip,
1397 61af9b21 2022-06-28 stsp struct got_object_id_queue *ids,
1398 61af9b21 2022-06-28 stsp struct got_object_idset *keep, struct got_object_idset *drop,
1399 61af9b21 2022-06-28 stsp struct got_object_idset *skip, struct got_repository *repo,
1400 61af9b21 2022-06-28 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1401 61af9b21 2022-06-28 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1402 61af9b21 2022-06-28 stsp {
1403 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
1404 61af9b21 2022-06-28 stsp struct got_object_id_queue next_ids;
1405 61af9b21 2022-06-28 stsp struct got_object_qid *qid, *tmp;
1406 61af9b21 2022-06-28 stsp struct recv_painted_commit_arg arg;
1407 61af9b21 2022-06-28 stsp
1408 61af9b21 2022-06-28 stsp STAILQ_INIT(&next_ids);
1409 61af9b21 2022-06-28 stsp
1410 61af9b21 2022-06-28 stsp err = got_privsep_send_painting_request(pack->privsep_child->ibuf,
1411 61af9b21 2022-06-28 stsp idx, id, color);
1412 61af9b21 2022-06-28 stsp if (err)
1413 61af9b21 2022-06-28 stsp return err;
1414 61af9b21 2022-06-28 stsp
1415 61af9b21 2022-06-28 stsp arg.ncolored = ncolored;
1416 61af9b21 2022-06-28 stsp arg.nqueued = nqueued;
1417 61af9b21 2022-06-28 stsp arg.nskip = nskip;
1418 61af9b21 2022-06-28 stsp arg.ids = ids;
1419 61af9b21 2022-06-28 stsp arg.keep = keep;
1420 61af9b21 2022-06-28 stsp arg.drop = drop;
1421 61af9b21 2022-06-28 stsp arg.skip = skip;
1422 61af9b21 2022-06-28 stsp arg.progress_cb = progress_cb;
1423 61af9b21 2022-06-28 stsp arg.progress_arg = progress_arg;
1424 61af9b21 2022-06-28 stsp arg.rl = rl;
1425 61af9b21 2022-06-28 stsp arg.cancel_cb = cancel_cb;
1426 61af9b21 2022-06-28 stsp arg.cancel_arg = cancel_arg;
1427 61af9b21 2022-06-28 stsp err = got_privsep_recv_painted_commits(&next_ids,
1428 61af9b21 2022-06-28 stsp recv_painted_commit, &arg, pack->privsep_child->ibuf);
1429 61af9b21 2022-06-28 stsp if (err)
1430 61af9b21 2022-06-28 stsp return err;
1431 61af9b21 2022-06-28 stsp
1432 61af9b21 2022-06-28 stsp STAILQ_FOREACH_SAFE(qid, &next_ids, entry, tmp) {
1433 61af9b21 2022-06-28 stsp struct got_object_qid *old_id;
1434 61af9b21 2022-06-28 stsp intptr_t qcolor, ocolor;
1435 61af9b21 2022-06-28 stsp STAILQ_FOREACH(old_id, ids, entry) {
1436 61af9b21 2022-06-28 stsp if (got_object_id_cmp(&qid->id, &old_id->id))
1437 61af9b21 2022-06-28 stsp continue;
1438 61af9b21 2022-06-28 stsp qcolor = (intptr_t)qid->data;
1439 61af9b21 2022-06-28 stsp ocolor = (intptr_t)old_id->data;
1440 61af9b21 2022-06-28 stsp STAILQ_REMOVE(&next_ids, qid, got_object_qid, entry);
1441 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1442 61af9b21 2022-06-28 stsp qid = NULL;
1443 61af9b21 2022-06-28 stsp if (qcolor != ocolor) {
1444 61af9b21 2022-06-28 stsp paint_commit(old_id, qcolor);
1445 61af9b21 2022-06-28 stsp if (ocolor == COLOR_SKIP)
1446 61af9b21 2022-06-28 stsp (*nskip)--;
1447 61af9b21 2022-06-28 stsp else if (qcolor == COLOR_SKIP)
1448 61af9b21 2022-06-28 stsp (*nskip)++;
1449 61af9b21 2022-06-28 stsp }
1450 61af9b21 2022-06-28 stsp break;
1451 61af9b21 2022-06-28 stsp }
1452 61af9b21 2022-06-28 stsp }
1453 61af9b21 2022-06-28 stsp while (!STAILQ_EMPTY(&next_ids)) {
1454 61af9b21 2022-06-28 stsp qid = STAILQ_FIRST(&next_ids);
1455 61af9b21 2022-06-28 stsp STAILQ_REMOVE_HEAD(&next_ids, entry);
1456 61af9b21 2022-06-28 stsp paint_commit(qid, color);
1457 61af9b21 2022-06-28 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
1458 61af9b21 2022-06-28 stsp (*nqueued)++;
1459 61af9b21 2022-06-28 stsp if (color == COLOR_SKIP)
1460 61af9b21 2022-06-28 stsp (*nskip)++;
1461 61af9b21 2022-06-28 stsp }
1462 61af9b21 2022-06-28 stsp
1463 61af9b21 2022-06-28 stsp return err;
1464 61af9b21 2022-06-28 stsp }
1465 61af9b21 2022-06-28 stsp
1466 61af9b21 2022-06-28 stsp static const struct got_error *
1467 61af9b21 2022-06-28 stsp find_pack_for_commit_painting(struct got_packidx **best_packidx,
1468 61af9b21 2022-06-28 stsp struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1469 61af9b21 2022-06-28 stsp {
1470 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
1471 61af9b21 2022-06-28 stsp struct got_pathlist_entry *pe;
1472 61af9b21 2022-06-28 stsp const char *best_packidx_path = NULL;
1473 61af9b21 2022-06-28 stsp int nobj_max = 0;
1474 61af9b21 2022-06-28 stsp int ncommits_max = 0;
1475 61af9b21 2022-06-28 stsp
1476 61af9b21 2022-06-28 stsp *best_packidx = NULL;
1477 61af9b21 2022-06-28 stsp
1478 61af9b21 2022-06-28 stsp /*
1479 61af9b21 2022-06-28 stsp * Find the largest pack which contains at least some of the
1480 61af9b21 2022-06-28 stsp * commits we are interested in.
1481 61af9b21 2022-06-28 stsp */
1482 61af9b21 2022-06-28 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1483 61af9b21 2022-06-28 stsp const char *path_packidx = pe->path;
1484 61af9b21 2022-06-28 stsp struct got_packidx *packidx;
1485 61af9b21 2022-06-28 stsp int nobj, idx, ncommits = 0;
1486 61af9b21 2022-06-28 stsp struct got_object_qid *qid;
1487 61af9b21 2022-06-28 stsp
1488 61af9b21 2022-06-28 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
1489 61af9b21 2022-06-28 stsp if (err)
1490 61af9b21 2022-06-28 stsp break;
1491 61af9b21 2022-06-28 stsp
1492 61af9b21 2022-06-28 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1493 61af9b21 2022-06-28 stsp if (nobj <= nobj_max)
1494 61af9b21 2022-06-28 stsp continue;
1495 61af9b21 2022-06-28 stsp
1496 61af9b21 2022-06-28 stsp STAILQ_FOREACH(qid, ids, entry) {
1497 61af9b21 2022-06-28 stsp idx = got_packidx_get_object_idx(packidx, &qid->id);
1498 61af9b21 2022-06-28 stsp if (idx != -1)
1499 61af9b21 2022-06-28 stsp ncommits++;
1500 61af9b21 2022-06-28 stsp }
1501 61af9b21 2022-06-28 stsp if (ncommits > ncommits_max) {
1502 61af9b21 2022-06-28 stsp best_packidx_path = path_packidx;
1503 61af9b21 2022-06-28 stsp nobj_max = nobj;
1504 61af9b21 2022-06-28 stsp ncommits_max = ncommits;
1505 61af9b21 2022-06-28 stsp }
1506 61af9b21 2022-06-28 stsp }
1507 61af9b21 2022-06-28 stsp
1508 61af9b21 2022-06-28 stsp if (best_packidx_path && err == NULL) {
1509 61af9b21 2022-06-28 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
1510 61af9b21 2022-06-28 stsp repo);
1511 61af9b21 2022-06-28 stsp }
1512 61af9b21 2022-06-28 stsp
1513 61af9b21 2022-06-28 stsp return err;
1514 61af9b21 2022-06-28 stsp }
1515 61af9b21 2022-06-28 stsp
1516 61af9b21 2022-06-28 stsp static const struct got_error *
1517 70f8f24d 2022-04-14 stsp paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1518 14dbbf48 2022-04-10 stsp struct got_object_idset *keep, struct got_object_idset *drop,
1519 70f8f24d 2022-04-14 stsp struct got_object_idset *skip, struct got_repository *repo,
1520 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1521 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1522 e6bcace5 2021-06-22 stsp {
1523 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1524 14dbbf48 2022-04-10 stsp struct got_commit_object *commit = NULL;
1525 61af9b21 2022-06-28 stsp struct got_packidx *packidx = NULL;
1526 61af9b21 2022-06-28 stsp struct got_pack *pack = NULL;
1527 70f8f24d 2022-04-14 stsp const struct got_object_id_queue *parents;
1528 61af9b21 2022-06-28 stsp struct got_object_qid *qid = NULL;
1529 70f8f24d 2022-04-14 stsp int nqueued = nids, nskip = 0;
1530 61af9b21 2022-06-28 stsp int idx;
1531 e6bcace5 2021-06-22 stsp
1532 70f8f24d 2022-04-14 stsp while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1533 61af9b21 2022-06-28 stsp intptr_t color;
1534 03c03172 2022-04-10 stsp
1535 57bc7b6d 2022-04-10 stsp if (cancel_cb) {
1536 57bc7b6d 2022-04-10 stsp err = cancel_cb(cancel_arg);
1537 57bc7b6d 2022-04-10 stsp if (err)
1538 14dbbf48 2022-04-10 stsp break;
1539 57bc7b6d 2022-04-10 stsp }
1540 57bc7b6d 2022-04-10 stsp
1541 14dbbf48 2022-04-10 stsp qid = STAILQ_FIRST(ids);
1542 70f8f24d 2022-04-14 stsp STAILQ_REMOVE_HEAD(ids, entry);
1543 70f8f24d 2022-04-14 stsp nqueued--;
1544 61af9b21 2022-06-28 stsp color = (intptr_t)qid->data;
1545 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1546 70f8f24d 2022-04-14 stsp nskip--;
1547 e6bcace5 2021-06-22 stsp
1548 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(skip, &qid->id)) {
1549 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1550 61af9b21 2022-06-28 stsp qid = NULL;
1551 70f8f24d 2022-04-14 stsp continue;
1552 70f8f24d 2022-04-14 stsp }
1553 61af9b21 2022-06-28 stsp if (color == COLOR_KEEP &&
1554 61af9b21 2022-06-28 stsp got_object_idset_contains(keep, &qid->id)) {
1555 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1556 61af9b21 2022-06-28 stsp qid = NULL;
1557 61af9b21 2022-06-28 stsp continue;
1558 61af9b21 2022-06-28 stsp }
1559 61af9b21 2022-06-28 stsp if (color == COLOR_DROP &&
1560 61af9b21 2022-06-28 stsp got_object_idset_contains(drop, &qid->id)) {
1561 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1562 61af9b21 2022-06-28 stsp qid = NULL;
1563 61af9b21 2022-06-28 stsp continue;
1564 61af9b21 2022-06-28 stsp }
1565 b8af7c06 2022-03-15 stsp
1566 61af9b21 2022-06-28 stsp /* Pinned pack may have moved to different cache slot. */
1567 61af9b21 2022-06-28 stsp pack = got_repo_get_pinned_pack(repo);
1568 61af9b21 2022-06-28 stsp
1569 61af9b21 2022-06-28 stsp if (packidx && pack) {
1570 61af9b21 2022-06-28 stsp idx = got_packidx_get_object_idx(packidx, &qid->id);
1571 61af9b21 2022-06-28 stsp if (idx != -1) {
1572 61af9b21 2022-06-28 stsp err = paint_packed_commits(pack, &qid->id,
1573 61af9b21 2022-06-28 stsp idx, color, ncolored, &nqueued, &nskip,
1574 61af9b21 2022-06-28 stsp ids, keep, drop, skip, repo,
1575 61af9b21 2022-06-28 stsp progress_cb, progress_arg, rl,
1576 61af9b21 2022-06-28 stsp cancel_cb, cancel_arg);
1577 61af9b21 2022-06-28 stsp if (err)
1578 61af9b21 2022-06-28 stsp break;
1579 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1580 61af9b21 2022-06-28 stsp qid = NULL;
1581 70f8f24d 2022-04-14 stsp continue;
1582 70f8f24d 2022-04-14 stsp }
1583 61af9b21 2022-06-28 stsp }
1584 61af9b21 2022-06-28 stsp
1585 61af9b21 2022-06-28 stsp switch (color) {
1586 61af9b21 2022-06-28 stsp case COLOR_KEEP:
1587 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(drop, &qid->id)) {
1588 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1589 70f8f24d 2022-04-14 stsp if (err)
1590 70f8f24d 2022-04-14 stsp goto done;
1591 70f8f24d 2022-04-14 stsp } else
1592 70f8f24d 2022-04-14 stsp (*ncolored)++;
1593 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(keep, &qid->id, NULL);
1594 70f8f24d 2022-04-14 stsp if (err)
1595 70f8f24d 2022-04-14 stsp goto done;
1596 70f8f24d 2022-04-14 stsp break;
1597 70f8f24d 2022-04-14 stsp case COLOR_DROP:
1598 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(keep, &qid->id)) {
1599 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1600 70f8f24d 2022-04-14 stsp if (err)
1601 70f8f24d 2022-04-14 stsp goto done;
1602 70f8f24d 2022-04-14 stsp } else
1603 70f8f24d 2022-04-14 stsp (*ncolored)++;
1604 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(drop, &qid->id, NULL);
1605 70f8f24d 2022-04-14 stsp if (err)
1606 70f8f24d 2022-04-14 stsp goto done;
1607 70f8f24d 2022-04-14 stsp break;
1608 70f8f24d 2022-04-14 stsp case COLOR_SKIP:
1609 d7b5a0e8 2022-04-20 stsp if (!got_object_idset_contains(skip, &qid->id)) {
1610 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(skip, &qid->id,
1611 d7b5a0e8 2022-04-20 stsp NULL);
1612 70f8f24d 2022-04-14 stsp if (err)
1613 70f8f24d 2022-04-14 stsp goto done;
1614 70f8f24d 2022-04-14 stsp }
1615 70f8f24d 2022-04-14 stsp break;
1616 70f8f24d 2022-04-14 stsp default:
1617 70f8f24d 2022-04-14 stsp /* should not happen */
1618 70f8f24d 2022-04-14 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
1619 756050ac 2022-07-19 op "%s invalid commit color %"PRIdPTR, __func__,
1620 756050ac 2022-07-19 op color);
1621 70f8f24d 2022-04-14 stsp goto done;
1622 70f8f24d 2022-04-14 stsp }
1623 70f8f24d 2022-04-14 stsp
1624 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
1625 b8af7c06 2022-03-15 stsp *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1626 b8af7c06 2022-03-15 stsp if (err)
1627 14dbbf48 2022-04-10 stsp break;
1628 e6bcace5 2021-06-22 stsp
1629 d7b5a0e8 2022-04-20 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
1630 70f8f24d 2022-04-14 stsp if (err)
1631 70f8f24d 2022-04-14 stsp break;
1632 e6bcace5 2021-06-22 stsp
1633 70f8f24d 2022-04-14 stsp parents = got_object_commit_get_parent_ids(commit);
1634 70f8f24d 2022-04-14 stsp if (parents) {
1635 70f8f24d 2022-04-14 stsp struct got_object_qid *pid;
1636 61af9b21 2022-06-28 stsp color = (intptr_t)qid->data;
1637 70f8f24d 2022-04-14 stsp STAILQ_FOREACH(pid, parents, entry) {
1638 61af9b21 2022-06-28 stsp err = queue_commit_id(ids, &pid->id,
1639 61af9b21 2022-06-28 stsp color, repo);
1640 70f8f24d 2022-04-14 stsp if (err)
1641 70f8f24d 2022-04-14 stsp break;
1642 70f8f24d 2022-04-14 stsp nqueued++;
1643 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1644 70f8f24d 2022-04-14 stsp nskip++;
1645 e6bcace5 2021-06-22 stsp }
1646 e6bcace5 2021-06-22 stsp }
1647 e6bcace5 2021-06-22 stsp
1648 61af9b21 2022-06-28 stsp if (pack == NULL && (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
1649 61af9b21 2022-06-28 stsp if (packidx == NULL) {
1650 61af9b21 2022-06-28 stsp err = find_pack_for_commit_painting(&packidx,
1651 61af9b21 2022-06-28 stsp ids, nqueued, repo);
1652 61af9b21 2022-06-28 stsp if (err)
1653 61af9b21 2022-06-28 stsp goto done;
1654 61af9b21 2022-06-28 stsp }
1655 61af9b21 2022-06-28 stsp if (packidx != NULL) {
1656 61af9b21 2022-06-28 stsp err = cache_pack_for_packidx(&pack, packidx,
1657 61af9b21 2022-06-28 stsp repo);
1658 61af9b21 2022-06-28 stsp if (err)
1659 61af9b21 2022-06-28 stsp goto done;
1660 61af9b21 2022-06-28 stsp err = got_privsep_init_commit_painting(
1661 61af9b21 2022-06-28 stsp pack->privsep_child->ibuf);
1662 61af9b21 2022-06-28 stsp if (err)
1663 61af9b21 2022-06-28 stsp goto done;
1664 61af9b21 2022-06-28 stsp err = send_idset(pack->privsep_child->ibuf,
1665 61af9b21 2022-06-28 stsp keep);
1666 61af9b21 2022-06-28 stsp if (err)
1667 61af9b21 2022-06-28 stsp goto done;
1668 61af9b21 2022-06-28 stsp err = send_idset(pack->privsep_child->ibuf, drop);
1669 61af9b21 2022-06-28 stsp if (err)
1670 61af9b21 2022-06-28 stsp goto done;
1671 61af9b21 2022-06-28 stsp err = send_idset(pack->privsep_child->ibuf, skip);
1672 61af9b21 2022-06-28 stsp if (err)
1673 61af9b21 2022-06-28 stsp goto done;
1674 61af9b21 2022-06-28 stsp err = got_repo_pin_pack(repo, packidx, pack);
1675 61af9b21 2022-06-28 stsp if (err)
1676 61af9b21 2022-06-28 stsp goto done;
1677 61af9b21 2022-06-28 stsp }
1678 61af9b21 2022-06-28 stsp }
1679 61af9b21 2022-06-28 stsp
1680 70f8f24d 2022-04-14 stsp got_object_commit_close(commit);
1681 70f8f24d 2022-04-14 stsp commit = NULL;
1682 61af9b21 2022-06-28 stsp
1683 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1684 61af9b21 2022-06-28 stsp qid = NULL;
1685 e6bcace5 2021-06-22 stsp }
1686 70f8f24d 2022-04-14 stsp done:
1687 61af9b21 2022-06-28 stsp if (pack) {
1688 61af9b21 2022-06-28 stsp const struct got_error *pack_err;
1689 61af9b21 2022-06-28 stsp pack_err = got_privsep_send_painting_commits_done(
1690 61af9b21 2022-06-28 stsp pack->privsep_child->ibuf);
1691 61af9b21 2022-06-28 stsp if (err == NULL)
1692 61af9b21 2022-06-28 stsp err = pack_err;
1693 61af9b21 2022-06-28 stsp }
1694 14dbbf48 2022-04-10 stsp if (commit)
1695 14dbbf48 2022-04-10 stsp got_object_commit_close(commit);
1696 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1697 61af9b21 2022-06-28 stsp got_repo_unpin_pack(repo);
1698 14dbbf48 2022-04-10 stsp return err;
1699 14dbbf48 2022-04-10 stsp }
1700 14dbbf48 2022-04-10 stsp
1701 14dbbf48 2022-04-10 stsp static const struct got_error *
1702 14dbbf48 2022-04-10 stsp findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1703 14dbbf48 2022-04-10 stsp struct got_object_id **head, int nhead,
1704 14dbbf48 2022-04-10 stsp struct got_object_id **tail, int ntail,
1705 14dbbf48 2022-04-10 stsp struct got_repository *repo,
1706 14dbbf48 2022-04-10 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1707 14dbbf48 2022-04-10 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1708 14dbbf48 2022-04-10 stsp {
1709 14dbbf48 2022-04-10 stsp const struct got_error *err = NULL;
1710 14dbbf48 2022-04-10 stsp struct got_object_id_queue ids;
1711 70f8f24d 2022-04-14 stsp struct got_object_idset *keep, *drop, *skip = NULL;
1712 14dbbf48 2022-04-10 stsp int i, nkeep;
1713 14dbbf48 2022-04-10 stsp
1714 14dbbf48 2022-04-10 stsp STAILQ_INIT(&ids);
1715 14dbbf48 2022-04-10 stsp *res = NULL;
1716 14dbbf48 2022-04-10 stsp *nres = 0;
1717 14dbbf48 2022-04-10 stsp *ncolored = 0;
1718 14dbbf48 2022-04-10 stsp
1719 14dbbf48 2022-04-10 stsp keep = got_object_idset_alloc();
1720 14dbbf48 2022-04-10 stsp if (keep == NULL)
1721 14dbbf48 2022-04-10 stsp return got_error_from_errno("got_object_idset_alloc");
1722 14dbbf48 2022-04-10 stsp
1723 14dbbf48 2022-04-10 stsp drop = got_object_idset_alloc();
1724 14dbbf48 2022-04-10 stsp if (drop == NULL) {
1725 14dbbf48 2022-04-10 stsp err = got_error_from_errno("got_object_idset_alloc");
1726 14dbbf48 2022-04-10 stsp goto done;
1727 14dbbf48 2022-04-10 stsp }
1728 14dbbf48 2022-04-10 stsp
1729 70f8f24d 2022-04-14 stsp skip = got_object_idset_alloc();
1730 70f8f24d 2022-04-14 stsp if (skip == NULL) {
1731 70f8f24d 2022-04-14 stsp err = got_error_from_errno("got_object_idset_alloc");
1732 70f8f24d 2022-04-14 stsp goto done;
1733 70f8f24d 2022-04-14 stsp }
1734 70f8f24d 2022-04-14 stsp
1735 14dbbf48 2022-04-10 stsp for (i = 0; i < nhead; i++) {
1736 14dbbf48 2022-04-10 stsp struct got_object_id *id = head[i];
1737 14dbbf48 2022-04-10 stsp if (id == NULL)
1738 14dbbf48 2022-04-10 stsp continue;
1739 fbafdecf 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1740 14dbbf48 2022-04-10 stsp if (err)
1741 14dbbf48 2022-04-10 stsp goto done;
1742 5e91dae4 2022-08-30 stsp }
1743 14dbbf48 2022-04-10 stsp
1744 14dbbf48 2022-04-10 stsp for (i = 0; i < ntail; i++) {
1745 14dbbf48 2022-04-10 stsp struct got_object_id *id = tail[i];
1746 14dbbf48 2022-04-10 stsp if (id == NULL)
1747 14dbbf48 2022-04-10 stsp continue;
1748 14dbbf48 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1749 14dbbf48 2022-04-10 stsp if (err)
1750 14dbbf48 2022-04-10 stsp goto done;
1751 14dbbf48 2022-04-10 stsp }
1752 14dbbf48 2022-04-10 stsp
1753 70f8f24d 2022-04-14 stsp err = paint_commits(ncolored, &ids, nhead + ntail,
1754 70f8f24d 2022-04-14 stsp keep, drop, skip, repo, progress_cb, progress_arg, rl,
1755 70f8f24d 2022-04-14 stsp cancel_cb, cancel_arg);
1756 14dbbf48 2022-04-10 stsp if (err)
1757 14dbbf48 2022-04-10 stsp goto done;
1758 14dbbf48 2022-04-10 stsp
1759 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1760 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1761 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1762 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1763 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1764 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1765 e6bcace5 2021-06-22 stsp goto done;
1766 e6bcace5 2021-06-22 stsp }
1767 70f8f24d 2022-04-14 stsp arg.idx = -1;
1768 70f8f24d 2022-04-14 stsp arg.skip = skip;
1769 70f8f24d 2022-04-14 stsp arg.drop = drop;
1770 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1771 e6bcace5 2021-06-22 stsp if (err) {
1772 e6bcace5 2021-06-22 stsp free(arg.array);
1773 e6bcace5 2021-06-22 stsp goto done;
1774 e6bcace5 2021-06-22 stsp }
1775 e6bcace5 2021-06-22 stsp *res = arg.array;
1776 70f8f24d 2022-04-14 stsp *nres = arg.idx + 1;
1777 e6bcace5 2021-06-22 stsp }
1778 e6bcace5 2021-06-22 stsp done:
1779 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1780 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1781 70f8f24d 2022-04-14 stsp if (skip)
1782 70f8f24d 2022-04-14 stsp got_object_idset_free(skip);
1783 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1784 0ab4c957 2022-06-13 stsp return err;
1785 0ab4c957 2022-06-13 stsp }
1786 0ab4c957 2022-06-13 stsp
1787 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg {
1788 0ab4c957 2022-06-13 stsp /* output parameters: */
1789 0ab4c957 2022-06-13 stsp struct got_object_id *id;
1790 0ab4c957 2022-06-13 stsp char *dpath;
1791 0ab4c957 2022-06-13 stsp time_t mtime;
1792 0ab4c957 2022-06-13 stsp
1793 0ab4c957 2022-06-13 stsp /* input parameters: */
1794 0ab4c957 2022-06-13 stsp uint32_t seed;
1795 0ab4c957 2022-06-13 stsp int want_meta;
1796 0ab4c957 2022-06-13 stsp struct got_object_idset *idset;
1797 0ab4c957 2022-06-13 stsp struct got_object_idset *idset_exclude;
1798 0ab4c957 2022-06-13 stsp int loose_obj_only;
1799 0ab4c957 2022-06-13 stsp int *ncolored;
1800 0ab4c957 2022-06-13 stsp int *nfound;
1801 0ab4c957 2022-06-13 stsp int *ntrees;
1802 0ab4c957 2022-06-13 stsp got_pack_progress_cb progress_cb;
1803 0ab4c957 2022-06-13 stsp void *progress_arg;
1804 0ab4c957 2022-06-13 stsp struct got_ratelimit *rl;
1805 0ab4c957 2022-06-13 stsp got_cancel_cb cancel_cb;
1806 0ab4c957 2022-06-13 stsp void *cancel_arg;
1807 0ab4c957 2022-06-13 stsp };
1808 0ab4c957 2022-06-13 stsp
1809 0ab4c957 2022-06-13 stsp static const struct got_error *
1810 0ab4c957 2022-06-13 stsp load_packed_commit_id(void *arg, time_t mtime, struct got_object_id *id,
1811 0ab4c957 2022-06-13 stsp struct got_repository *repo)
1812 0ab4c957 2022-06-13 stsp {
1813 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg *a = arg;
1814 0ab4c957 2022-06-13 stsp
1815 0ab4c957 2022-06-13 stsp if (got_object_idset_contains(a->idset, id) ||
1816 0ab4c957 2022-06-13 stsp got_object_idset_contains(a->idset_exclude, id))
1817 0ab4c957 2022-06-13 stsp return NULL;
1818 0ab4c957 2022-06-13 stsp
1819 0ab4c957 2022-06-13 stsp return add_object(a->want_meta,
1820 0ab4c957 2022-06-13 stsp a->want_meta ? a->idset : a->idset_exclude,
1821 0ab4c957 2022-06-13 stsp id, "", GOT_OBJ_TYPE_COMMIT, mtime, a->seed, a->loose_obj_only,
1822 0ab4c957 2022-06-13 stsp repo, a->ncolored, a->nfound, a->ntrees,
1823 0ab4c957 2022-06-13 stsp a->progress_cb, a->progress_arg, a->rl);
1824 0ab4c957 2022-06-13 stsp }
1825 0ab4c957 2022-06-13 stsp
1826 0ab4c957 2022-06-13 stsp static const struct got_error *
1827 0ab4c957 2022-06-13 stsp load_packed_tree_ids(void *arg, struct got_tree_object *tree, time_t mtime,
1828 0ab4c957 2022-06-13 stsp struct got_object_id *id, const char *dpath, struct got_repository *repo)
1829 0ab4c957 2022-06-13 stsp {
1830 0ab4c957 2022-06-13 stsp const struct got_error *err;
1831 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg *a = arg;
1832 0ab4c957 2022-06-13 stsp const char *relpath;
1833 0ab4c957 2022-06-13 stsp
1834 0ab4c957 2022-06-13 stsp /*
1835 0ab4c957 2022-06-13 stsp * When we receive a tree's ID and path but not the tree itself,
1836 0ab4c957 2022-06-13 stsp * this tree object was not found in the pack file. This is the
1837 0ab4c957 2022-06-13 stsp * last time we are being called for this optimized traversal.
1838 0ab4c957 2022-06-13 stsp * Return from here and switch to loading objects the slow way.
1839 0ab4c957 2022-06-13 stsp */
1840 0ab4c957 2022-06-13 stsp if (tree == NULL) {
1841 0ab4c957 2022-06-13 stsp free(a->id);
1842 0ab4c957 2022-06-13 stsp a->id = got_object_id_dup(id);
1843 0ab4c957 2022-06-13 stsp if (a->id == NULL) {
1844 0ab4c957 2022-06-13 stsp err = got_error_from_errno("got_object_id_dup");
1845 0ab4c957 2022-06-13 stsp free(a->dpath);
1846 0ab4c957 2022-06-13 stsp a->dpath = NULL;
1847 0ab4c957 2022-06-13 stsp return err;
1848 0ab4c957 2022-06-13 stsp }
1849 0ab4c957 2022-06-13 stsp
1850 0ab4c957 2022-06-13 stsp free(a->dpath);
1851 0ab4c957 2022-06-13 stsp a->dpath = strdup(dpath);
1852 0ab4c957 2022-06-13 stsp if (a->dpath == NULL) {
1853 0ab4c957 2022-06-13 stsp err = got_error_from_errno("strdup");
1854 0ab4c957 2022-06-13 stsp free(a->id);
1855 0ab4c957 2022-06-13 stsp a->id = NULL;
1856 0ab4c957 2022-06-13 stsp return err;
1857 0ab4c957 2022-06-13 stsp }
1858 0ab4c957 2022-06-13 stsp
1859 0ab4c957 2022-06-13 stsp a->mtime = mtime;
1860 0ab4c957 2022-06-13 stsp return NULL;
1861 0ab4c957 2022-06-13 stsp }
1862 0ab4c957 2022-06-13 stsp
1863 0ab4c957 2022-06-13 stsp if (got_object_idset_contains(a->idset, id) ||
1864 0ab4c957 2022-06-13 stsp got_object_idset_contains(a->idset_exclude, id))
1865 0ab4c957 2022-06-13 stsp return NULL;
1866 0ab4c957 2022-06-13 stsp
1867 0ab4c957 2022-06-13 stsp relpath = dpath;
1868 0ab4c957 2022-06-13 stsp while (relpath[0] == '/')
1869 0ab4c957 2022-06-13 stsp relpath++;
1870 0ab4c957 2022-06-13 stsp
1871 0ab4c957 2022-06-13 stsp err = add_object(a->want_meta,
1872 0ab4c957 2022-06-13 stsp a->want_meta ? a->idset : a->idset_exclude,
1873 0ab4c957 2022-06-13 stsp id, relpath, GOT_OBJ_TYPE_TREE, mtime, a->seed,
1874 0ab4c957 2022-06-13 stsp a->loose_obj_only, repo, a->ncolored, a->nfound, a->ntrees,
1875 0ab4c957 2022-06-13 stsp a->progress_cb, a->progress_arg, a->rl);
1876 0ab4c957 2022-06-13 stsp if (err)
1877 0ab4c957 2022-06-13 stsp return err;
1878 0ab4c957 2022-06-13 stsp
1879 0ab4c957 2022-06-13 stsp return load_tree_entries(NULL, a->want_meta, a->idset,
1880 0ab4c957 2022-06-13 stsp a->idset_exclude, tree, dpath, mtime, a->seed, repo,
1881 0ab4c957 2022-06-13 stsp a->loose_obj_only, a->ncolored, a->nfound, a->ntrees,
1882 0ab4c957 2022-06-13 stsp a->progress_cb, a->progress_arg, a->rl,
1883 0ab4c957 2022-06-13 stsp a->cancel_cb, a->cancel_arg);
1884 0ab4c957 2022-06-13 stsp }
1885 0ab4c957 2022-06-13 stsp
1886 0ab4c957 2022-06-13 stsp static const struct got_error *
1887 db9b9b1c 2022-06-14 stsp load_packed_object_ids(int *found_all_objects,
1888 db9b9b1c 2022-06-14 stsp struct got_object_id **ours, int nours,
1889 0ab4c957 2022-06-13 stsp struct got_object_id **theirs, int ntheirs,
1890 0ab4c957 2022-06-13 stsp int want_meta, uint32_t seed, struct got_object_idset *idset,
1891 0ab4c957 2022-06-13 stsp struct got_object_idset *idset_exclude, int loose_obj_only,
1892 0ab4c957 2022-06-13 stsp struct got_repository *repo, struct got_packidx *packidx,
1893 0ab4c957 2022-06-13 stsp int *ncolored, int *nfound, int *ntrees,
1894 0ab4c957 2022-06-13 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1895 0ab4c957 2022-06-13 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1896 0ab4c957 2022-06-13 stsp {
1897 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1898 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg lpa;
1899 0ab4c957 2022-06-13 stsp
1900 0ab4c957 2022-06-13 stsp memset(&lpa, 0, sizeof(lpa));
1901 0ab4c957 2022-06-13 stsp lpa.seed = seed;
1902 0ab4c957 2022-06-13 stsp lpa.want_meta = want_meta;
1903 0ab4c957 2022-06-13 stsp lpa.idset = idset;
1904 0ab4c957 2022-06-13 stsp lpa.idset_exclude = idset_exclude;
1905 0ab4c957 2022-06-13 stsp lpa.loose_obj_only = loose_obj_only;
1906 0ab4c957 2022-06-13 stsp lpa.ncolored = ncolored;
1907 0ab4c957 2022-06-13 stsp lpa.nfound = nfound;
1908 0ab4c957 2022-06-13 stsp lpa.ntrees = ntrees;
1909 0ab4c957 2022-06-13 stsp lpa.progress_cb = progress_cb;
1910 0ab4c957 2022-06-13 stsp lpa.progress_arg = progress_arg;
1911 0ab4c957 2022-06-13 stsp lpa.rl = rl;
1912 0ab4c957 2022-06-13 stsp lpa.cancel_cb = cancel_cb;
1913 0ab4c957 2022-06-13 stsp lpa.cancel_arg = cancel_arg;
1914 0ab4c957 2022-06-13 stsp
1915 0ab4c957 2022-06-13 stsp /* Attempt to load objects via got-read-pack, as far as possible. */
1916 db9b9b1c 2022-06-14 stsp err = got_object_enumerate(found_all_objects, load_packed_commit_id,
1917 0ab4c957 2022-06-13 stsp load_packed_tree_ids, &lpa, ours, nours, theirs, ntheirs,
1918 0ab4c957 2022-06-13 stsp packidx, repo);
1919 0ab4c957 2022-06-13 stsp if (err)
1920 0ab4c957 2022-06-13 stsp return err;
1921 0ab4c957 2022-06-13 stsp
1922 0ab4c957 2022-06-13 stsp if (lpa.id == NULL)
1923 0ab4c957 2022-06-13 stsp return NULL;
1924 0ab4c957 2022-06-13 stsp
1925 0ab4c957 2022-06-13 stsp /*
1926 0ab4c957 2022-06-13 stsp * An incomplete tree hierarchy was present in the pack file
1927 db9b9b1c 2022-06-14 stsp * and caused loading to be aborted.
1928 0ab4c957 2022-06-13 stsp * Continue loading trees the slow way.
1929 0ab4c957 2022-06-13 stsp */
1930 0ab4c957 2022-06-13 stsp err = load_tree(want_meta, idset, idset_exclude,
1931 0ab4c957 2022-06-13 stsp lpa.id, lpa.dpath, lpa.mtime, seed, repo, loose_obj_only,
1932 0ab4c957 2022-06-13 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1933 0ab4c957 2022-06-13 stsp cancel_cb, cancel_arg);
1934 0ab4c957 2022-06-13 stsp free(lpa.id);
1935 0ab4c957 2022-06-13 stsp free(lpa.dpath);
1936 e6bcace5 2021-06-22 stsp return err;
1937 e6bcace5 2021-06-22 stsp }
1938 e6bcace5 2021-06-22 stsp
1939 e6bcace5 2021-06-22 stsp static const struct got_error *
1940 0ab4c957 2022-06-13 stsp find_pack_for_enumeration(struct got_packidx **best_packidx,
1941 0ab4c957 2022-06-13 stsp struct got_object_id **ids, int nids, struct got_repository *repo)
1942 0ab4c957 2022-06-13 stsp {
1943 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1944 0ab4c957 2022-06-13 stsp struct got_pathlist_entry *pe;
1945 0ab4c957 2022-06-13 stsp const char *best_packidx_path = NULL;
1946 0ab4c957 2022-06-13 stsp int nobj_max = 0;
1947 0ab4c957 2022-06-13 stsp int ncommits_max = 0;
1948 0ab4c957 2022-06-13 stsp
1949 0ab4c957 2022-06-13 stsp *best_packidx = NULL;
1950 0ab4c957 2022-06-13 stsp
1951 0ab4c957 2022-06-13 stsp /*
1952 0ab4c957 2022-06-13 stsp * Find the largest pack which contains at least some of the
1953 0ab4c957 2022-06-13 stsp * commits and tags we are interested in.
1954 0ab4c957 2022-06-13 stsp */
1955 0ab4c957 2022-06-13 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1956 0ab4c957 2022-06-13 stsp const char *path_packidx = pe->path;
1957 0ab4c957 2022-06-13 stsp struct got_packidx *packidx;
1958 0ab4c957 2022-06-13 stsp int nobj, i, idx, ncommits = 0;
1959 0ab4c957 2022-06-13 stsp
1960 0ab4c957 2022-06-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
1961 0ab4c957 2022-06-13 stsp if (err)
1962 0ab4c957 2022-06-13 stsp break;
1963 0ab4c957 2022-06-13 stsp
1964 0ab4c957 2022-06-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1965 0ab4c957 2022-06-13 stsp if (nobj <= nobj_max)
1966 0ab4c957 2022-06-13 stsp continue;
1967 0ab4c957 2022-06-13 stsp
1968 0ab4c957 2022-06-13 stsp for (i = 0; i < nids; i++) {
1969 0ab4c957 2022-06-13 stsp idx = got_packidx_get_object_idx(packidx, ids[i]);
1970 0ab4c957 2022-06-13 stsp if (idx != -1)
1971 0ab4c957 2022-06-13 stsp ncommits++;
1972 0ab4c957 2022-06-13 stsp }
1973 0ab4c957 2022-06-13 stsp if (ncommits > ncommits_max) {
1974 0ab4c957 2022-06-13 stsp best_packidx_path = path_packidx;
1975 0ab4c957 2022-06-13 stsp nobj_max = nobj;
1976 0ab4c957 2022-06-13 stsp ncommits_max = ncommits;
1977 0ab4c957 2022-06-13 stsp }
1978 0ab4c957 2022-06-13 stsp }
1979 0ab4c957 2022-06-13 stsp
1980 eb7b30a1 2022-06-13 stsp if (best_packidx_path && err == NULL) {
1981 0ab4c957 2022-06-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
1982 0ab4c957 2022-06-13 stsp repo);
1983 0ab4c957 2022-06-13 stsp }
1984 0ab4c957 2022-06-13 stsp
1985 0ab4c957 2022-06-13 stsp return err;
1986 0ab4c957 2022-06-13 stsp }
1987 0ab4c957 2022-06-13 stsp
1988 0ab4c957 2022-06-13 stsp static const struct got_error *
1989 b8af7c06 2022-03-15 stsp load_object_ids(int *ncolored, int *nfound, int *ntrees,
1990 b8af7c06 2022-03-15 stsp struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1991 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1992 d6a28ffe 2022-05-20 op uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1993 d6a28ffe 2022-05-20 op void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1994 d6a28ffe 2022-05-20 op void *cancel_arg)
1995 e6bcace5 2021-06-22 stsp {
1996 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1997 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1998 0ab4c957 2022-06-13 stsp struct got_packidx *packidx = NULL;
1999 db9b9b1c 2022-06-14 stsp int i, nobj = 0, obj_type, found_all_objects = 0;
2000 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude;
2001 2f8438b0 2022-05-04 stsp
2002 2f8438b0 2022-05-04 stsp idset_exclude = got_object_idset_alloc();
2003 2f8438b0 2022-05-04 stsp if (idset_exclude == NULL)
2004 2f8438b0 2022-05-04 stsp return got_error_from_errno("got_object_idset_alloc");
2005 e6bcace5 2021-06-22 stsp
2006 b8af7c06 2022-03-15 stsp *ncolored = 0;
2007 b8af7c06 2022-03-15 stsp *nfound = 0;
2008 b8af7c06 2022-03-15 stsp *ntrees = 0;
2009 b8af7c06 2022-03-15 stsp
2010 b8af7c06 2022-03-15 stsp err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
2011 b8af7c06 2022-03-15 stsp repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2012 dc3fe1bf 2022-05-10 stsp if (err)
2013 e6bcace5 2021-06-22 stsp goto done;
2014 0ab4c957 2022-06-13 stsp
2015 0ab4c957 2022-06-13 stsp err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
2016 0ab4c957 2022-06-13 stsp if (err)
2017 0ab4c957 2022-06-13 stsp goto done;
2018 0ab4c957 2022-06-13 stsp if (packidx) {
2019 db9b9b1c 2022-06-14 stsp err = load_packed_object_ids(&found_all_objects,
2020 db9b9b1c 2022-06-14 stsp theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
2021 db9b9b1c 2022-06-14 stsp loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
2022 db9b9b1c 2022-06-14 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2023 0ab4c957 2022-06-13 stsp if (err)
2024 0ab4c957 2022-06-13 stsp goto done;
2025 0ab4c957 2022-06-13 stsp }
2026 cee6a7ea 2022-06-07 stsp
2027 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
2028 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
2029 05118f5a 2021-06-22 stsp if (id == NULL)
2030 05118f5a 2021-06-22 stsp continue;
2031 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
2032 05118f5a 2021-06-22 stsp if (err)
2033 05118f5a 2021-06-22 stsp return err;
2034 9d34261e 2022-04-07 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
2035 db9b9b1c 2022-06-14 stsp if (!found_all_objects) {
2036 db9b9b1c 2022-06-14 stsp err = load_commit(0, idset, idset_exclude,
2037 db9b9b1c 2022-06-14 stsp id, repo, seed, loose_obj_only,
2038 db9b9b1c 2022-06-14 stsp ncolored, nfound, ntrees,
2039 db9b9b1c 2022-06-14 stsp progress_cb, progress_arg, rl,
2040 db9b9b1c 2022-06-14 stsp cancel_cb, cancel_arg);
2041 db9b9b1c 2022-06-14 stsp if (err)
2042 db9b9b1c 2022-06-14 stsp goto done;
2043 db9b9b1c 2022-06-14 stsp }
2044 9d34261e 2022-04-07 stsp } else if (obj_type == GOT_OBJ_TYPE_TAG) {
2045 2f8438b0 2022-05-04 stsp err = load_tag(0, idset, idset_exclude, id, repo,
2046 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
2047 9d34261e 2022-04-07 stsp progress_cb, progress_arg, rl,
2048 9d34261e 2022-04-07 stsp cancel_cb, cancel_arg);
2049 9d34261e 2022-04-07 stsp if (err)
2050 9d34261e 2022-04-07 stsp goto done;
2051 9d34261e 2022-04-07 stsp }
2052 05118f5a 2021-06-22 stsp }
2053 05118f5a 2021-06-22 stsp
2054 db9b9b1c 2022-06-14 stsp found_all_objects = 0;
2055 0ab4c957 2022-06-13 stsp err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
2056 0ab4c957 2022-06-13 stsp if (err)
2057 0ab4c957 2022-06-13 stsp goto done;
2058 0ab4c957 2022-06-13 stsp if (packidx) {
2059 db9b9b1c 2022-06-14 stsp err = load_packed_object_ids(&found_all_objects, ids,
2060 db9b9b1c 2022-06-14 stsp nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
2061 db9b9b1c 2022-06-14 stsp loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
2062 0ab4c957 2022-06-13 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2063 0ab4c957 2022-06-13 stsp if (err)
2064 0ab4c957 2022-06-13 stsp goto done;
2065 0ab4c957 2022-06-13 stsp }
2066 0ab4c957 2022-06-13 stsp
2067 db9b9b1c 2022-06-14 stsp if (!found_all_objects) {
2068 db9b9b1c 2022-06-14 stsp for (i = 0; i < nobj; i++) {
2069 db9b9b1c 2022-06-14 stsp err = load_commit(1, idset, idset_exclude, ids[i],
2070 db9b9b1c 2022-06-14 stsp repo, seed, loose_obj_only, ncolored, nfound,
2071 db9b9b1c 2022-06-14 stsp ntrees, progress_cb, progress_arg, rl,
2072 db9b9b1c 2022-06-14 stsp cancel_cb, cancel_arg);
2073 db9b9b1c 2022-06-14 stsp if (err)
2074 db9b9b1c 2022-06-14 stsp goto done;
2075 db9b9b1c 2022-06-14 stsp }
2076 eca70f98 2021-09-03 stsp }
2077 eca70f98 2021-09-03 stsp
2078 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
2079 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
2080 67fd6849 2022-02-13 stsp struct got_pack_meta *m;
2081 05118f5a 2021-06-22 stsp if (id == NULL)
2082 05118f5a 2021-06-22 stsp continue;
2083 67fd6849 2022-02-13 stsp m = got_object_idset_get(idset, id);
2084 67fd6849 2022-02-13 stsp if (m == NULL) {
2085 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
2086 07165b17 2021-07-01 stsp if (err)
2087 07165b17 2021-07-01 stsp goto done;
2088 07165b17 2021-07-01 stsp } else
2089 67fd6849 2022-02-13 stsp obj_type = m->obj_type;
2090 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
2091 05118f5a 2021-06-22 stsp continue;
2092 2f8438b0 2022-05-04 stsp err = load_tag(1, idset, idset_exclude, id, repo,
2093 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
2094 2f8438b0 2022-05-04 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
2095 05118f5a 2021-06-22 stsp if (err)
2096 e6bcace5 2021-06-22 stsp goto done;
2097 e6bcace5 2021-06-22 stsp }
2098 e6bcace5 2021-06-22 stsp done:
2099 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
2100 e6bcace5 2021-06-22 stsp free(ids[i]);
2101 e6bcace5 2021-06-22 stsp }
2102 e6bcace5 2021-06-22 stsp free(ids);
2103 2f8438b0 2022-05-04 stsp got_object_idset_free(idset_exclude);
2104 e6bcace5 2021-06-22 stsp return err;
2105 e6bcace5 2021-06-22 stsp }
2106 e6bcace5 2021-06-22 stsp
2107 336075a4 2022-06-25 op static const struct got_error *
2108 894e4711 2022-10-15 stsp hwrite(int fd, const void *buf, off_t len, SHA1_CTX *ctx)
2109 e6bcace5 2021-06-22 stsp {
2110 894e4711 2022-10-15 stsp ssize_t w;
2111 e6bcace5 2021-06-22 stsp
2112 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
2113 894e4711 2022-10-15 stsp w = write(fd, buf, len);
2114 894e4711 2022-10-15 stsp if (w == -1)
2115 894e4711 2022-10-15 stsp return got_error_from_errno("write");
2116 894e4711 2022-10-15 stsp else if (w != len)
2117 894e4711 2022-10-15 stsp return got_error(GOT_ERR_IO);
2118 2d9e6abf 2022-05-04 stsp return NULL;
2119 2d9e6abf 2022-05-04 stsp }
2120 2d9e6abf 2022-05-04 stsp
2121 336075a4 2022-06-25 op static const struct got_error *
2122 894e4711 2022-10-15 stsp hcopy(FILE *fsrc, int fd_dst, off_t len, SHA1_CTX *ctx)
2123 2d9e6abf 2022-05-04 stsp {
2124 2d9e6abf 2022-05-04 stsp unsigned char buf[65536];
2125 2d9e6abf 2022-05-04 stsp off_t remain = len;
2126 2d9e6abf 2022-05-04 stsp size_t n;
2127 894e4711 2022-10-15 stsp ssize_t w;
2128 2d9e6abf 2022-05-04 stsp
2129 2d9e6abf 2022-05-04 stsp while (remain > 0) {
2130 2d9e6abf 2022-05-04 stsp size_t copylen = MIN(sizeof(buf), remain);
2131 2d9e6abf 2022-05-04 stsp n = fread(buf, 1, copylen, fsrc);
2132 2d9e6abf 2022-05-04 stsp if (n != copylen)
2133 2d9e6abf 2022-05-04 stsp return got_ferror(fsrc, GOT_ERR_IO);
2134 2d9e6abf 2022-05-04 stsp SHA1Update(ctx, buf, copylen);
2135 894e4711 2022-10-15 stsp w = write(fd_dst, buf, copylen);
2136 894e4711 2022-10-15 stsp if (w == -1)
2137 894e4711 2022-10-15 stsp return got_error_from_errno("write");
2138 894e4711 2022-10-15 stsp else if (w != copylen)
2139 894e4711 2022-10-15 stsp return got_error(GOT_ERR_IO);
2140 2d9e6abf 2022-05-04 stsp remain -= copylen;
2141 2d9e6abf 2022-05-04 stsp }
2142 2d9e6abf 2022-05-04 stsp
2143 e6bcace5 2021-06-22 stsp return NULL;
2144 e6bcace5 2021-06-22 stsp }
2145 e93fb944 2022-05-10 stsp
2146 336075a4 2022-06-25 op static const struct got_error *
2147 e93fb944 2022-05-10 stsp hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
2148 894e4711 2022-10-15 stsp int fd, off_t len, SHA1_CTX *ctx)
2149 e93fb944 2022-05-10 stsp {
2150 894e4711 2022-10-15 stsp ssize_t w;
2151 e6bcace5 2021-06-22 stsp
2152 e93fb944 2022-05-10 stsp if (src_offset + len > src_size)
2153 e93fb944 2022-05-10 stsp return got_error(GOT_ERR_RANGE);
2154 e93fb944 2022-05-10 stsp
2155 e93fb944 2022-05-10 stsp SHA1Update(ctx, src + src_offset, len);
2156 894e4711 2022-10-15 stsp w = write(fd, src + src_offset, len);
2157 894e4711 2022-10-15 stsp if (w == -1)
2158 894e4711 2022-10-15 stsp return got_error_from_errno("write");
2159 894e4711 2022-10-15 stsp else if (w != len)
2160 894e4711 2022-10-15 stsp return got_error(GOT_ERR_IO);
2161 e93fb944 2022-05-10 stsp return NULL;
2162 e93fb944 2022-05-10 stsp }
2163 e93fb944 2022-05-10 stsp
2164 e6bcace5 2021-06-22 stsp static void
2165 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
2166 e6bcace5 2021-06-22 stsp {
2167 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
2168 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
2169 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
2170 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
2171 e6bcace5 2021-06-22 stsp }
2172 e6bcace5 2021-06-22 stsp
2173 e6bcace5 2021-06-22 stsp static int
2174 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
2175 e6bcace5 2021-06-22 stsp {
2176 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
2177 e6bcace5 2021-06-22 stsp
2178 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
2179 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
2180 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
2181 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
2182 611e8e31 2022-05-01 stsp if (bhd->mtime < ahd->mtime)
2183 611e8e31 2022-05-01 stsp return -1;
2184 611e8e31 2022-05-01 stsp if (bhd->mtime > ahd->mtime)
2185 611e8e31 2022-05-01 stsp return 1;
2186 611e8e31 2022-05-01 stsp if (bhd < ahd)
2187 611e8e31 2022-05-01 stsp return -1;
2188 611e8e31 2022-05-01 stsp if (bhd > ahd)
2189 611e8e31 2022-05-01 stsp return 1;
2190 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
2191 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
2192 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
2193 611e8e31 2022-05-01 stsp return -1;
2194 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
2195 611e8e31 2022-05-01 stsp return 1;
2196 611e8e31 2022-05-01 stsp return got_object_id_cmp(&a->id, &b->id);
2197 e6bcace5 2021-06-22 stsp }
2198 e6bcace5 2021-06-22 stsp
2199 67fd6849 2022-02-13 stsp static int
2200 67fd6849 2022-02-13 stsp reuse_write_order_cmp(const void *pa, const void *pb)
2201 67fd6849 2022-02-13 stsp {
2202 67fd6849 2022-02-13 stsp struct got_pack_meta *a, *b;
2203 67fd6849 2022-02-13 stsp
2204 67fd6849 2022-02-13 stsp a = *(struct got_pack_meta **)pa;
2205 67fd6849 2022-02-13 stsp b = *(struct got_pack_meta **)pb;
2206 67fd6849 2022-02-13 stsp
2207 67fd6849 2022-02-13 stsp if (a->reused_delta_offset < b->reused_delta_offset)
2208 67fd6849 2022-02-13 stsp return -1;
2209 67fd6849 2022-02-13 stsp if (a->reused_delta_offset > b->reused_delta_offset)
2210 67fd6849 2022-02-13 stsp return 1;
2211 67fd6849 2022-02-13 stsp return 0;
2212 67fd6849 2022-02-13 stsp }
2213 67fd6849 2022-02-13 stsp
2214 e6bcace5 2021-06-22 stsp static const struct got_error *
2215 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
2216 e6bcace5 2021-06-22 stsp {
2217 e6bcace5 2021-06-22 stsp size_t i;
2218 e6bcace5 2021-06-22 stsp
2219 e6bcace5 2021-06-22 stsp *hdrlen = 0;
2220 e6bcace5 2021-06-22 stsp
2221 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
2222 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
2223 e6bcace5 2021-06-22 stsp len >>= 4;
2224 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
2225 e6bcace5 2021-06-22 stsp if (i >= bufsize)
2226 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
2227 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
2228 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
2229 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
2230 e6bcace5 2021-06-22 stsp }
2231 e6bcace5 2021-06-22 stsp
2232 e6bcace5 2021-06-22 stsp *hdrlen = i;
2233 e6bcace5 2021-06-22 stsp return NULL;
2234 e6bcace5 2021-06-22 stsp }
2235 e6bcace5 2021-06-22 stsp
2236 e6bcace5 2021-06-22 stsp static int
2237 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
2238 e6bcace5 2021-06-22 stsp {
2239 e6bcace5 2021-06-22 stsp int i, j;
2240 e6bcace5 2021-06-22 stsp char rbuf[8];
2241 e6bcace5 2021-06-22 stsp
2242 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
2243 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
2244 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
2245 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
2246 e6bcace5 2021-06-22 stsp }
2247 e6bcace5 2021-06-22 stsp
2248 e6bcace5 2021-06-22 stsp j = 0;
2249 e6bcace5 2021-06-22 stsp while (i > 0)
2250 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
2251 e6bcace5 2021-06-22 stsp return j;
2252 e6bcace5 2021-06-22 stsp }
2253 e6bcace5 2021-06-22 stsp
2254 e6bcace5 2021-06-22 stsp static const struct got_error *
2255 894e4711 2022-10-15 stsp deltahdr(off_t *packfile_size, SHA1_CTX *ctx, int packfd,
2256 67fd6849 2022-02-13 stsp struct got_pack_meta *m)
2257 5060d5a1 2022-01-10 stsp {
2258 5060d5a1 2022-01-10 stsp const struct got_error *err;
2259 5060d5a1 2022-01-10 stsp char buf[32];
2260 5060d5a1 2022-01-10 stsp int nh;
2261 5060d5a1 2022-01-10 stsp
2262 67fd6849 2022-02-13 stsp if (m->prev->off != 0) {
2263 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
2264 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
2265 5060d5a1 2022-01-10 stsp if (err)
2266 5060d5a1 2022-01-10 stsp return err;
2267 5060d5a1 2022-01-10 stsp nh += packoff(buf + nh, m->off - m->prev->off);
2268 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, nh, ctx);
2269 5060d5a1 2022-01-10 stsp if (err)
2270 5060d5a1 2022-01-10 stsp return err;
2271 5060d5a1 2022-01-10 stsp *packfile_size += nh;
2272 5060d5a1 2022-01-10 stsp } else {
2273 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
2274 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
2275 5060d5a1 2022-01-10 stsp if (err)
2276 5060d5a1 2022-01-10 stsp return err;
2277 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, nh, ctx);
2278 5060d5a1 2022-01-10 stsp if (err)
2279 5060d5a1 2022-01-10 stsp return err;
2280 5060d5a1 2022-01-10 stsp *packfile_size += nh;
2281 894e4711 2022-10-15 stsp err = hwrite(packfd, m->prev->id.sha1,
2282 5060d5a1 2022-01-10 stsp sizeof(m->prev->id.sha1), ctx);
2283 5060d5a1 2022-01-10 stsp if (err)
2284 5060d5a1 2022-01-10 stsp return err;
2285 5060d5a1 2022-01-10 stsp *packfile_size += sizeof(m->prev->id.sha1);
2286 5060d5a1 2022-01-10 stsp }
2287 5060d5a1 2022-01-10 stsp
2288 5060d5a1 2022-01-10 stsp return NULL;
2289 5060d5a1 2022-01-10 stsp }
2290 5060d5a1 2022-01-10 stsp
2291 5060d5a1 2022-01-10 stsp static const struct got_error *
2292 894e4711 2022-10-15 stsp write_packed_object(off_t *packfile_size, int packfd,
2293 e93fb944 2022-05-10 stsp FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
2294 e93fb944 2022-05-10 stsp struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
2295 e93fb944 2022-05-10 stsp struct got_repository *repo)
2296 67fd6849 2022-02-13 stsp {
2297 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
2298 67fd6849 2022-02-13 stsp struct got_deflate_checksum csum;
2299 67fd6849 2022-02-13 stsp char buf[32];
2300 67fd6849 2022-02-13 stsp int nh;
2301 67fd6849 2022-02-13 stsp struct got_raw_object *raw = NULL;
2302 67fd6849 2022-02-13 stsp off_t outlen;
2303 67fd6849 2022-02-13 stsp
2304 67fd6849 2022-02-13 stsp csum.output_sha1 = ctx;
2305 67fd6849 2022-02-13 stsp csum.output_crc = NULL;
2306 67fd6849 2022-02-13 stsp
2307 894e4711 2022-10-15 stsp m->off = *packfile_size;
2308 67fd6849 2022-02-13 stsp if (m->delta_len == 0) {
2309 67fd6849 2022-02-13 stsp err = got_object_raw_open(&raw, outfd, repo, &m->id);
2310 67fd6849 2022-02-13 stsp if (err)
2311 67fd6849 2022-02-13 stsp goto done;
2312 67fd6849 2022-02-13 stsp err = packhdr(&nh, buf, sizeof(buf),
2313 67fd6849 2022-02-13 stsp m->obj_type, raw->size);
2314 67fd6849 2022-02-13 stsp if (err)
2315 67fd6849 2022-02-13 stsp goto done;
2316 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, nh, ctx);
2317 67fd6849 2022-02-13 stsp if (err)
2318 67fd6849 2022-02-13 stsp goto done;
2319 67fd6849 2022-02-13 stsp *packfile_size += nh;
2320 67fd6849 2022-02-13 stsp if (raw->f == NULL) {
2321 894e4711 2022-10-15 stsp err = got_deflate_to_fd_mmap(&outlen,
2322 67fd6849 2022-02-13 stsp raw->data + raw->hdrlen, 0, raw->size,
2323 894e4711 2022-10-15 stsp packfd, &csum);
2324 67fd6849 2022-02-13 stsp if (err)
2325 67fd6849 2022-02-13 stsp goto done;
2326 67fd6849 2022-02-13 stsp } else {
2327 67fd6849 2022-02-13 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
2328 67fd6849 2022-02-13 stsp == -1) {
2329 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
2330 67fd6849 2022-02-13 stsp goto done;
2331 67fd6849 2022-02-13 stsp }
2332 894e4711 2022-10-15 stsp err = got_deflate_to_fd(&outlen, raw->f,
2333 894e4711 2022-10-15 stsp raw->size, packfd, &csum);
2334 67fd6849 2022-02-13 stsp if (err)
2335 67fd6849 2022-02-13 stsp goto done;
2336 67fd6849 2022-02-13 stsp }
2337 67fd6849 2022-02-13 stsp *packfile_size += outlen;
2338 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
2339 67fd6849 2022-02-13 stsp raw = NULL;
2340 67fd6849 2022-02-13 stsp } else if (m->delta_buf) {
2341 894e4711 2022-10-15 stsp err = deltahdr(packfile_size, ctx, packfd, m);
2342 67fd6849 2022-02-13 stsp if (err)
2343 67fd6849 2022-02-13 stsp goto done;
2344 894e4711 2022-10-15 stsp err = hwrite(packfd, m->delta_buf,
2345 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
2346 67fd6849 2022-02-13 stsp if (err)
2347 67fd6849 2022-02-13 stsp goto done;
2348 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
2349 67fd6849 2022-02-13 stsp free(m->delta_buf);
2350 67fd6849 2022-02-13 stsp m->delta_buf = NULL;
2351 e93fb944 2022-05-10 stsp } else if (delta_cache_map) {
2352 894e4711 2022-10-15 stsp err = deltahdr(packfile_size, ctx, packfd, m);
2353 e93fb944 2022-05-10 stsp if (err)
2354 e93fb944 2022-05-10 stsp goto done;
2355 e93fb944 2022-05-10 stsp err = hcopy_mmap(delta_cache_map, m->delta_offset,
2356 894e4711 2022-10-15 stsp delta_cache_size, packfd, m->delta_compressed_len,
2357 e93fb944 2022-05-10 stsp ctx);
2358 e93fb944 2022-05-10 stsp if (err)
2359 e93fb944 2022-05-10 stsp goto done;
2360 e93fb944 2022-05-10 stsp *packfile_size += m->delta_compressed_len;
2361 67fd6849 2022-02-13 stsp } else {
2362 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
2363 67fd6849 2022-02-13 stsp == -1) {
2364 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
2365 67fd6849 2022-02-13 stsp goto done;
2366 67fd6849 2022-02-13 stsp }
2367 894e4711 2022-10-15 stsp err = deltahdr(packfile_size, ctx, packfd, m);
2368 67fd6849 2022-02-13 stsp if (err)
2369 67fd6849 2022-02-13 stsp goto done;
2370 894e4711 2022-10-15 stsp err = hcopy(delta_cache, packfd,
2371 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
2372 67fd6849 2022-02-13 stsp if (err)
2373 67fd6849 2022-02-13 stsp goto done;
2374 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
2375 67fd6849 2022-02-13 stsp }
2376 67fd6849 2022-02-13 stsp done:
2377 67fd6849 2022-02-13 stsp if (raw)
2378 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
2379 67fd6849 2022-02-13 stsp return err;
2380 67fd6849 2022-02-13 stsp }
2381 67fd6849 2022-02-13 stsp
2382 67fd6849 2022-02-13 stsp static const struct got_error *
2383 894e4711 2022-10-15 stsp genpack(uint8_t *pack_sha1, int packfd, FILE *delta_cache,
2384 67fd6849 2022-02-13 stsp struct got_pack_meta **deltify, int ndeltify,
2385 67fd6849 2022-02-13 stsp struct got_pack_meta **reuse, int nreuse,
2386 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees, int nours,
2387 b8af7c06 2022-03-15 stsp struct got_repository *repo,
2388 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
2389 211cfef0 2022-01-05 stsp struct got_ratelimit *rl,
2390 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2391 e6bcace5 2021-06-22 stsp {
2392 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
2393 67fd6849 2022-02-13 stsp int i;
2394 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
2395 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
2396 08347b73 2021-10-14 stsp char buf[32];
2397 894e4711 2022-10-15 stsp ssize_t w;
2398 67fd6849 2022-02-13 stsp off_t packfile_size = 0;
2399 cc7a354a 2021-10-15 stsp int outfd = -1;
2400 e93fb944 2022-05-10 stsp int delta_cache_fd = -1;
2401 e93fb944 2022-05-10 stsp uint8_t *delta_cache_map = NULL;
2402 e93fb944 2022-05-10 stsp size_t delta_cache_size = 0;
2403 e6bcace5 2021-06-22 stsp
2404 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
2405 e6bcace5 2021-06-22 stsp
2406 e93fb944 2022-05-10 stsp #ifndef GOT_PACK_NO_MMAP
2407 e93fb944 2022-05-10 stsp delta_cache_fd = dup(fileno(delta_cache));
2408 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1) {
2409 e93fb944 2022-05-10 stsp struct stat sb;
2410 e93fb944 2022-05-10 stsp if (fstat(delta_cache_fd, &sb) == -1) {
2411 e93fb944 2022-05-10 stsp err = got_error_from_errno("fstat");
2412 e93fb944 2022-05-10 stsp goto done;
2413 e93fb944 2022-05-10 stsp }
2414 e93fb944 2022-05-10 stsp if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
2415 e93fb944 2022-05-10 stsp delta_cache_map = mmap(NULL, sb.st_size,
2416 e93fb944 2022-05-10 stsp PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
2417 e93fb944 2022-05-10 stsp if (delta_cache_map == MAP_FAILED) {
2418 e93fb944 2022-05-10 stsp if (errno != ENOMEM) {
2419 e93fb944 2022-05-10 stsp err = got_error_from_errno("mmap");
2420 e93fb944 2022-05-10 stsp goto done;
2421 e93fb944 2022-05-10 stsp }
2422 e93fb944 2022-05-10 stsp delta_cache_map = NULL; /* fallback on stdio */
2423 e93fb944 2022-05-10 stsp } else
2424 e93fb944 2022-05-10 stsp delta_cache_size = (size_t)sb.st_size;
2425 e93fb944 2022-05-10 stsp }
2426 e93fb944 2022-05-10 stsp }
2427 e93fb944 2022-05-10 stsp #endif
2428 894e4711 2022-10-15 stsp err = hwrite(packfd, "PACK", 4, &ctx);
2429 e6bcace5 2021-06-22 stsp if (err)
2430 e93fb944 2022-05-10 stsp goto done;
2431 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
2432 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, 4, &ctx);
2433 e6bcace5 2021-06-22 stsp if (err)
2434 e6bcace5 2021-06-22 stsp goto done;
2435 67fd6849 2022-02-13 stsp putbe32(buf, ndeltify + nreuse);
2436 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, 4, &ctx);
2437 e6bcace5 2021-06-22 stsp if (err)
2438 e6bcace5 2021-06-22 stsp goto done;
2439 67fd6849 2022-02-13 stsp
2440 67fd6849 2022-02-13 stsp qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
2441 67fd6849 2022-02-13 stsp write_order_cmp);
2442 67fd6849 2022-02-13 stsp for (i = 0; i < ndeltify; i++) {
2443 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
2444 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
2445 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, i);
2446 211cfef0 2022-01-05 stsp if (err)
2447 211cfef0 2022-01-05 stsp goto done;
2448 67fd6849 2022-02-13 stsp m = deltify[i];
2449 894e4711 2022-10-15 stsp err = write_packed_object(&packfile_size, packfd,
2450 e93fb944 2022-05-10 stsp delta_cache, delta_cache_map, delta_cache_size,
2451 e93fb944 2022-05-10 stsp m, &outfd, &ctx, repo);
2452 67fd6849 2022-02-13 stsp if (err)
2453 67fd6849 2022-02-13 stsp goto done;
2454 e6bcace5 2021-06-22 stsp }
2455 67fd6849 2022-02-13 stsp
2456 67fd6849 2022-02-13 stsp qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
2457 67fd6849 2022-02-13 stsp reuse_write_order_cmp);
2458 67fd6849 2022-02-13 stsp for (i = 0; i < nreuse; i++) {
2459 67fd6849 2022-02-13 stsp err = report_progress(progress_cb, progress_arg, rl,
2460 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
2461 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
2462 67fd6849 2022-02-13 stsp if (err)
2463 67fd6849 2022-02-13 stsp goto done;
2464 67fd6849 2022-02-13 stsp m = reuse[i];
2465 894e4711 2022-10-15 stsp err = write_packed_object(&packfile_size, packfd,
2466 e93fb944 2022-05-10 stsp delta_cache, delta_cache_map, delta_cache_size,
2467 e93fb944 2022-05-10 stsp m, &outfd, &ctx, repo);
2468 67fd6849 2022-02-13 stsp if (err)
2469 67fd6849 2022-02-13 stsp goto done;
2470 67fd6849 2022-02-13 stsp }
2471 67fd6849 2022-02-13 stsp
2472 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
2473 894e4711 2022-10-15 stsp w = write(packfd, pack_sha1, SHA1_DIGEST_LENGTH);
2474 894e4711 2022-10-15 stsp if (w == -1)
2475 894e4711 2022-10-15 stsp err = got_error_from_errno("write");
2476 894e4711 2022-10-15 stsp else if (w != SHA1_DIGEST_LENGTH)
2477 894e4711 2022-10-15 stsp err = got_error(GOT_ERR_IO);
2478 894e4711 2022-10-15 stsp if (err)
2479 894e4711 2022-10-15 stsp goto done;
2480 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
2481 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
2482 211cfef0 2022-01-05 stsp if (progress_cb) {
2483 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2484 b8af7c06 2022-03-15 stsp packfile_size, nours, ndeltify + nreuse,
2485 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse);
2486 211cfef0 2022-01-05 stsp if (err)
2487 211cfef0 2022-01-05 stsp goto done;
2488 211cfef0 2022-01-05 stsp }
2489 e6bcace5 2021-06-22 stsp done:
2490 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
2491 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
2492 e93fb944 2022-05-10 stsp if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
2493 e93fb944 2022-05-10 stsp err = got_error_from_errno("munmap");
2494 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2495 e93fb944 2022-05-10 stsp err = got_error_from_errno("close");
2496 e6bcace5 2021-06-22 stsp return err;
2497 67fd6849 2022-02-13 stsp }
2498 67fd6849 2022-02-13 stsp
2499 67fd6849 2022-02-13 stsp static const struct got_error *
2500 67fd6849 2022-02-13 stsp add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
2501 67fd6849 2022-02-13 stsp {
2502 67fd6849 2022-02-13 stsp struct got_pack_meta *m = data;
2503 67fd6849 2022-02-13 stsp struct got_pack_metavec *v = arg;
2504 f5e78e05 2022-05-04 stsp
2505 411cbec1 2022-05-20 stsp if (m->reused_delta_offset != 0)
2506 f5e78e05 2022-05-04 stsp return NULL;
2507 67fd6849 2022-02-13 stsp
2508 67fd6849 2022-02-13 stsp return add_meta(m, v);
2509 67fd6849 2022-02-13 stsp }
2510 67fd6849 2022-02-13 stsp
2511 e6bcace5 2021-06-22 stsp const struct got_error *
2512 a32780aa 2022-10-15 stsp got_pack_create(uint8_t *packsha1, int packfd, FILE *delta_cache,
2513 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
2514 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
2515 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
2516 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
2517 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2518 e6bcace5 2021-06-22 stsp {
2519 e6bcace5 2021-06-22 stsp const struct got_error *err;
2520 67fd6849 2022-02-13 stsp int delta_cache_fd = -1;
2521 67fd6849 2022-02-13 stsp struct got_object_idset *idset;
2522 211cfef0 2022-01-05 stsp struct got_ratelimit rl;
2523 67fd6849 2022-02-13 stsp struct got_pack_metavec deltify, reuse;
2524 b8af7c06 2022-03-15 stsp int ncolored = 0, nfound = 0, ntrees = 0;
2525 f5e78e05 2022-05-04 stsp size_t ndeltify;
2526 d6a28ffe 2022-05-20 op uint32_t seed;
2527 e6bcace5 2021-06-22 stsp
2528 d6a28ffe 2022-05-20 op seed = arc4random();
2529 d6a28ffe 2022-05-20 op
2530 67fd6849 2022-02-13 stsp memset(&deltify, 0, sizeof(deltify));
2531 67fd6849 2022-02-13 stsp memset(&reuse, 0, sizeof(reuse));
2532 67fd6849 2022-02-13 stsp
2533 211cfef0 2022-01-05 stsp got_ratelimit_init(&rl, 0, 500);
2534 211cfef0 2022-01-05 stsp
2535 67fd6849 2022-02-13 stsp idset = got_object_idset_alloc();
2536 67fd6849 2022-02-13 stsp if (idset == NULL)
2537 67fd6849 2022-02-13 stsp return got_error_from_errno("got_object_idset_alloc");
2538 67fd6849 2022-02-13 stsp
2539 b8af7c06 2022-03-15 stsp err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
2540 d6a28ffe 2022-05-20 op ntheirs, ours, nours, repo, seed, loose_obj_only,
2541 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
2542 e6bcace5 2021-06-22 stsp if (err)
2543 17259bfa 2022-05-19 stsp goto done;
2544 e6bcace5 2021-06-22 stsp
2545 28526235 2022-02-13 stsp if (progress_cb) {
2546 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2547 b8af7c06 2022-03-15 stsp 0L, nours, got_object_idset_num_elements(idset), 0, 0);
2548 28526235 2022-02-13 stsp if (err)
2549 28526235 2022-02-13 stsp goto done;
2550 28526235 2022-02-13 stsp }
2551 28526235 2022-02-13 stsp
2552 67fd6849 2022-02-13 stsp if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
2553 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
2554 05118f5a 2021-06-22 stsp goto done;
2555 05118f5a 2021-06-22 stsp }
2556 74881701 2021-10-15 stsp
2557 a32780aa 2022-10-15 stsp delta_cache_fd = dup(fileno(delta_cache));
2558 67fd6849 2022-02-13 stsp if (delta_cache_fd == -1) {
2559 a32780aa 2022-10-15 stsp err = got_error_from_errno("dup");
2560 74881701 2021-10-15 stsp goto done;
2561 74881701 2021-10-15 stsp }
2562 74881701 2021-10-15 stsp
2563 67fd6849 2022-02-13 stsp reuse.metasz = 64;
2564 67fd6849 2022-02-13 stsp reuse.meta = calloc(reuse.metasz,
2565 67fd6849 2022-02-13 stsp sizeof(struct got_pack_meta *));
2566 67fd6849 2022-02-13 stsp if (reuse.meta == NULL) {
2567 67fd6849 2022-02-13 stsp err = got_error_from_errno("calloc");
2568 67fd6849 2022-02-13 stsp goto done;
2569 67fd6849 2022-02-13 stsp }
2570 67fd6849 2022-02-13 stsp
2571 b8af7c06 2022-03-15 stsp err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
2572 b8af7c06 2022-03-15 stsp ntrees, nours, repo, progress_cb, progress_arg, &rl,
2573 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
2574 67fd6849 2022-02-13 stsp if (err)
2575 67fd6849 2022-02-13 stsp goto done;
2576 67fd6849 2022-02-13 stsp
2577 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
2578 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
2579 67fd6849 2022-02-13 stsp goto done;
2580 67fd6849 2022-02-13 stsp }
2581 67fd6849 2022-02-13 stsp
2582 f5e78e05 2022-05-04 stsp ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
2583 f5e78e05 2022-05-04 stsp if (ndeltify > 0) {
2584 f5e78e05 2022-05-04 stsp deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
2585 f5e78e05 2022-05-04 stsp if (deltify.meta == NULL) {
2586 f5e78e05 2022-05-04 stsp err = got_error_from_errno("calloc");
2587 f5e78e05 2022-05-04 stsp goto done;
2588 f5e78e05 2022-05-04 stsp }
2589 f5e78e05 2022-05-04 stsp deltify.metasz = ndeltify;
2590 67fd6849 2022-02-13 stsp
2591 f5e78e05 2022-05-04 stsp err = got_object_idset_for_each(idset, add_meta_idset_cb,
2592 f5e78e05 2022-05-04 stsp &deltify);
2593 f8a36e22 2021-08-26 stsp if (err)
2594 f8a36e22 2021-08-26 stsp goto done;
2595 f5e78e05 2022-05-04 stsp if (deltify.nmeta > 0) {
2596 f5e78e05 2022-05-04 stsp err = pick_deltas(deltify.meta, deltify.nmeta,
2597 f5e78e05 2022-05-04 stsp ncolored, nfound, ntrees, nours, reuse.nmeta,
2598 f5e78e05 2022-05-04 stsp delta_cache, repo, progress_cb, progress_arg, &rl,
2599 f5e78e05 2022-05-04 stsp cancel_cb, cancel_arg);
2600 f5e78e05 2022-05-04 stsp if (err)
2601 f5e78e05 2022-05-04 stsp goto done;
2602 f5e78e05 2022-05-04 stsp }
2603 f8a36e22 2021-08-26 stsp }
2604 e6bcace5 2021-06-22 stsp
2605 2d9e6abf 2022-05-04 stsp if (fflush(delta_cache) == EOF) {
2606 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fflush");
2607 2d9e6abf 2022-05-04 stsp goto done;
2608 2d9e6abf 2022-05-04 stsp }
2609 894e4711 2022-10-15 stsp err = genpack(packsha1, packfd, delta_cache, deltify.meta,
2610 b8af7c06 2022-03-15 stsp deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
2611 b8af7c06 2022-03-15 stsp nours, repo, progress_cb, progress_arg, &rl,
2612 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
2613 e6bcace5 2021-06-22 stsp if (err)
2614 e6bcace5 2021-06-22 stsp goto done;
2615 e6bcace5 2021-06-22 stsp done:
2616 67fd6849 2022-02-13 stsp free_nmeta(deltify.meta, deltify.nmeta);
2617 67fd6849 2022-02-13 stsp free_nmeta(reuse.meta, reuse.nmeta);
2618 67fd6849 2022-02-13 stsp got_object_idset_free(idset);
2619 67fd6849 2022-02-13 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2620 67fd6849 2022-02-13 stsp err = got_error_from_errno("close");
2621 e6bcace5 2021-06-22 stsp return err;
2622 e6bcace5 2021-06-22 stsp }