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 e6bcace5 2021-06-22 stsp #include <stdio.h>
31 e6bcace5 2021-06-22 stsp #include <stdlib.h>
32 e6bcace5 2021-06-22 stsp #include <string.h>
33 e6bcace5 2021-06-22 stsp #include <sha1.h>
34 211cfef0 2022-01-05 stsp #include <time.h>
35 bfc73a47 2022-03-19 naddy #include <unistd.h>
36 e6bcace5 2021-06-22 stsp #include <limits.h>
37 e6bcace5 2021-06-22 stsp #include <zlib.h>
38 e6bcace5 2021-06-22 stsp
39 e6bcace5 2021-06-22 stsp #include "got_error.h"
40 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
41 e6bcace5 2021-06-22 stsp #include "got_object.h"
42 f8b19efd 2021-10-13 stsp #include "got_path.h"
43 05118f5a 2021-06-22 stsp #include "got_reference.h"
44 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
45 08347b73 2021-10-14 stsp #include "got_opentemp.h"
46 e6bcace5 2021-06-22 stsp
47 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
50 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
52 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
53 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
54 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 67fd6849 2022-02-13 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 fae7e038 2022-05-07 stsp }
564 67fd6849 2022-02-13 stsp
565 fae7e038 2022-05-07 stsp static const struct got_error *
566 fae7e038 2022-05-07 stsp recv_reused_delta(struct got_imsg_reused_delta *delta,
567 fae7e038 2022-05-07 stsp struct got_object_idset *idset, struct got_pack_metavec *v)
568 fae7e038 2022-05-07 stsp {
569 fae7e038 2022-05-07 stsp struct got_pack_meta *m, *base;
570 67fd6849 2022-02-13 stsp
571 fae7e038 2022-05-07 stsp if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
572 fae7e038 2022-05-07 stsp delta->delta_offset +
573 fae7e038 2022-05-07 stsp delta->delta_compressed_size < delta->delta_offset)
574 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_BAD_PACKFILE);
575 67fd6849 2022-02-13 stsp
576 fae7e038 2022-05-07 stsp m = got_object_idset_get(idset, &delta->id);
577 fae7e038 2022-05-07 stsp if (m == NULL)
578 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_OBJ);
579 fae7e038 2022-05-07 stsp
580 fae7e038 2022-05-07 stsp base = got_object_idset_get(idset, &delta->base_id);
581 fae7e038 2022-05-07 stsp if (base == NULL)
582 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_OBJ);
583 fae7e038 2022-05-07 stsp
584 fae7e038 2022-05-07 stsp m->delta_len = delta->delta_size;
585 fae7e038 2022-05-07 stsp m->delta_compressed_len = delta->delta_compressed_size;
586 fae7e038 2022-05-07 stsp m->delta_offset = delta->delta_out_offset;
587 fae7e038 2022-05-07 stsp m->prev = base;
588 fae7e038 2022-05-07 stsp m->size = delta->result_size;
589 fae7e038 2022-05-07 stsp m->reused_delta_offset = delta->delta_offset;
590 fae7e038 2022-05-07 stsp m->base_obj_id = got_object_id_dup(&delta->base_id);
591 fae7e038 2022-05-07 stsp if (m->base_obj_id == NULL)
592 fae7e038 2022-05-07 stsp return got_error_from_errno("got_object_id_dup");
593 fae7e038 2022-05-07 stsp
594 fae7e038 2022-05-07 stsp return add_meta(m, v);
595 3d589bee 2022-06-25 stsp }
596 3d589bee 2022-06-25 stsp
597 3d589bee 2022-06-25 stsp static const struct got_error *
598 3d589bee 2022-06-25 stsp prepare_delta_reuse(struct got_pack **pack, struct got_packidx *packidx,
599 3d589bee 2022-06-25 stsp int delta_outfd, struct got_repository *repo)
600 3d589bee 2022-06-25 stsp {
601 3d589bee 2022-06-25 stsp const struct got_error *err = NULL;
602 3d589bee 2022-06-25 stsp char *path_packfile = NULL;
603 3d589bee 2022-06-25 stsp
604 3d589bee 2022-06-25 stsp err = got_packidx_get_packfile_path(&path_packfile,
605 3d589bee 2022-06-25 stsp packidx->path_packidx);
606 3d589bee 2022-06-25 stsp if (err)
607 3d589bee 2022-06-25 stsp return err;
608 3d589bee 2022-06-25 stsp
609 3d589bee 2022-06-25 stsp *pack = got_repo_get_cached_pack(repo, path_packfile);
610 3d589bee 2022-06-25 stsp if (*pack == NULL) {
611 3d589bee 2022-06-25 stsp err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
612 3d589bee 2022-06-25 stsp if (err)
613 3d589bee 2022-06-25 stsp goto done;
614 3d589bee 2022-06-25 stsp }
615 3d589bee 2022-06-25 stsp if ((*pack)->privsep_child == NULL) {
616 3d589bee 2022-06-25 stsp err = got_pack_start_privsep_child(*pack, packidx);
617 3d589bee 2022-06-25 stsp if (err)
618 3d589bee 2022-06-25 stsp goto done;
619 3d589bee 2022-06-25 stsp }
620 3d589bee 2022-06-25 stsp
621 3d589bee 2022-06-25 stsp if (!(*pack)->child_has_delta_outfd) {
622 3d589bee 2022-06-25 stsp int outfd_child;
623 3d589bee 2022-06-25 stsp outfd_child = dup(delta_outfd);
624 3d589bee 2022-06-25 stsp if (outfd_child == -1) {
625 3d589bee 2022-06-25 stsp err = got_error_from_errno("dup");
626 3d589bee 2022-06-25 stsp goto done;
627 3d589bee 2022-06-25 stsp }
628 3d589bee 2022-06-25 stsp err = got_privsep_send_raw_delta_outfd(
629 3d589bee 2022-06-25 stsp (*pack)->privsep_child->ibuf, outfd_child);
630 3d589bee 2022-06-25 stsp if (err)
631 3d589bee 2022-06-25 stsp goto done;
632 3d589bee 2022-06-25 stsp (*pack)->child_has_delta_outfd = 1;
633 3d589bee 2022-06-25 stsp }
634 3d589bee 2022-06-25 stsp
635 3d589bee 2022-06-25 stsp err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
636 3d589bee 2022-06-25 stsp done:
637 3d589bee 2022-06-25 stsp free(path_packfile);
638 3d589bee 2022-06-25 stsp return err;
639 67fd6849 2022-02-13 stsp }
640 67fd6849 2022-02-13 stsp
641 3d589bee 2022-06-25 stsp
642 67fd6849 2022-02-13 stsp static const struct got_error *
643 67fd6849 2022-02-13 stsp search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
644 b8af7c06 2022-03-15 stsp int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
645 b8af7c06 2022-03-15 stsp struct got_repository *repo,
646 67fd6849 2022-02-13 stsp got_pack_progress_cb progress_cb, void *progress_arg,
647 67fd6849 2022-02-13 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
648 67fd6849 2022-02-13 stsp {
649 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
650 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
651 67fd6849 2022-02-13 stsp struct got_pack *pack;
652 fae7e038 2022-05-07 stsp struct send_id_arg sia;
653 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
654 fae7e038 2022-05-07 stsp size_t ndeltas, i;
655 67fd6849 2022-02-13 stsp
656 67fd6849 2022-02-13 stsp err = find_pack_for_reuse(&packidx, repo);
657 67fd6849 2022-02-13 stsp if (err)
658 67fd6849 2022-02-13 stsp return err;
659 67fd6849 2022-02-13 stsp
660 67fd6849 2022-02-13 stsp if (packidx == NULL)
661 67fd6849 2022-02-13 stsp return NULL;
662 67fd6849 2022-02-13 stsp
663 3d589bee 2022-06-25 stsp err = prepare_delta_reuse(&pack, packidx, delta_cache_fd, repo);
664 67fd6849 2022-02-13 stsp if (err)
665 67fd6849 2022-02-13 stsp return err;
666 67fd6849 2022-02-13 stsp
667 fae7e038 2022-05-07 stsp memset(&sia, 0, sizeof(sia));
668 fae7e038 2022-05-07 stsp sia.ibuf = pack->privsep_child->ibuf;
669 fae7e038 2022-05-07 stsp err = got_object_idset_for_each(idset, send_id, &sia);
670 fae7e038 2022-05-07 stsp if (err)
671 fae7e038 2022-05-07 stsp return err;
672 fae7e038 2022-05-07 stsp if (sia.nids > 0) {
673 fae7e038 2022-05-07 stsp err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
674 fae7e038 2022-05-07 stsp sia.ids, sia.nids);
675 fae7e038 2022-05-07 stsp if (err)
676 fae7e038 2022-05-07 stsp return err;
677 67fd6849 2022-02-13 stsp }
678 fae7e038 2022-05-07 stsp err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
679 fae7e038 2022-05-07 stsp if (err)
680 fae7e038 2022-05-07 stsp return err;
681 67fd6849 2022-02-13 stsp
682 fae7e038 2022-05-07 stsp for (;;) {
683 fae7e038 2022-05-07 stsp int done = 0;
684 fae7e038 2022-05-07 stsp
685 fae7e038 2022-05-07 stsp if (cancel_cb) {
686 fae7e038 2022-05-07 stsp err = (*cancel_cb)(cancel_arg);
687 fae7e038 2022-05-07 stsp if (err)
688 fae7e038 2022-05-07 stsp break;
689 fae7e038 2022-05-07 stsp }
690 fae7e038 2022-05-07 stsp
691 fae7e038 2022-05-07 stsp err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
692 fae7e038 2022-05-07 stsp pack->privsep_child->ibuf);
693 fae7e038 2022-05-07 stsp if (err || done)
694 fae7e038 2022-05-07 stsp break;
695 fae7e038 2022-05-07 stsp
696 fae7e038 2022-05-07 stsp for (i = 0; i < ndeltas; i++) {
697 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta *delta = &deltas[i];
698 fae7e038 2022-05-07 stsp err = recv_reused_delta(delta, idset, v);
699 fae7e038 2022-05-07 stsp if (err)
700 fae7e038 2022-05-07 stsp goto done;
701 fae7e038 2022-05-07 stsp }
702 fae7e038 2022-05-07 stsp
703 fae7e038 2022-05-07 stsp err = report_progress(progress_cb, progress_arg, rl,
704 fae7e038 2022-05-07 stsp ncolored, nfound, ntrees, 0L, ncommits,
705 fae7e038 2022-05-07 stsp got_object_idset_num_elements(idset), v->nmeta, 0);
706 fae7e038 2022-05-07 stsp if (err)
707 fae7e038 2022-05-07 stsp break;
708 fae7e038 2022-05-07 stsp }
709 67fd6849 2022-02-13 stsp done:
710 67fd6849 2022-02-13 stsp return err;
711 67fd6849 2022-02-13 stsp }
712 67fd6849 2022-02-13 stsp
713 67fd6849 2022-02-13 stsp static const struct got_error *
714 b8af7c06 2022-03-15 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
715 b8af7c06 2022-03-15 stsp int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
716 b8af7c06 2022-03-15 stsp struct got_repository *repo,
717 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
718 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
719 e6bcace5 2021-06-22 stsp {
720 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
721 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
722 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
723 74881701 2021-10-15 stsp struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
724 5060d5a1 2022-01-10 stsp int i, j, ndeltas, best_ndeltas;
725 5060d5a1 2022-01-10 stsp off_t size, best_size;
726 4f4d853e 2021-10-24 stsp const int max_base_candidates = 3;
727 402a5ec1 2022-01-10 stsp size_t delta_memsize = 0;
728 adb4bbb2 2022-05-20 stsp const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
729 cc7a354a 2021-10-15 stsp int outfd = -1;
730 d6a28ffe 2022-05-20 op uint32_t delta_seed;
731 d6a28ffe 2022-05-20 op
732 d6a28ffe 2022-05-20 op delta_seed = arc4random();
733 e6bcace5 2021-06-22 stsp
734 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
735 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
736 e6bcace5 2021-06-22 stsp if (cancel_cb) {
737 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
738 e6bcace5 2021-06-22 stsp if (err)
739 e6bcace5 2021-06-22 stsp break;
740 e6bcace5 2021-06-22 stsp }
741 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
742 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
743 b8af7c06 2022-03-15 stsp nreused + i, 0);
744 211cfef0 2022-01-05 stsp if (err)
745 211cfef0 2022-01-05 stsp goto done;
746 e6bcace5 2021-06-22 stsp m = meta[i];
747 e6bcace5 2021-06-22 stsp
748 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
749 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
750 e6bcace5 2021-06-22 stsp continue;
751 e6bcace5 2021-06-22 stsp
752 94dac27c 2021-10-15 stsp err = got_object_raw_open(&raw, &outfd, repo, &m->id);
753 e6bcace5 2021-06-22 stsp if (err)
754 e6bcace5 2021-06-22 stsp goto done;
755 600b755e 2021-10-14 stsp m->size = raw->size;
756 e6bcace5 2021-06-22 stsp
757 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
758 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&m->dtab, raw->data,
759 d6a28ffe 2022-05-20 op raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
760 64a8571e 2022-01-07 stsp } else {
761 64a8571e 2022-01-07 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
762 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed);
763 64a8571e 2022-01-07 stsp }
764 e6bcace5 2021-06-22 stsp if (err)
765 e6bcace5 2021-06-22 stsp goto done;
766 e6bcace5 2021-06-22 stsp
767 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
768 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
769 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
770 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
771 e6bcace5 2021-06-22 stsp n->dtab = NULL;
772 e6bcace5 2021-06-22 stsp }
773 e6bcace5 2021-06-22 stsp
774 74881701 2021-10-15 stsp best_size = raw->size;
775 74881701 2021-10-15 stsp best_ndeltas = 0;
776 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
777 e6bcace5 2021-06-22 stsp if (cancel_cb) {
778 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
779 e6bcace5 2021-06-22 stsp if (err)
780 e6bcace5 2021-06-22 stsp goto done;
781 e6bcace5 2021-06-22 stsp }
782 e6bcace5 2021-06-22 stsp base = meta[j];
783 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
784 22edbce7 2021-10-24 stsp if (base->nchain >= 128 ||
785 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
786 e6bcace5 2021-06-22 stsp continue;
787 e6bcace5 2021-06-22 stsp
788 d3c116bf 2021-10-15 stsp err = got_object_raw_open(&base_raw, &outfd, repo,
789 94dac27c 2021-10-15 stsp &base->id);
790 e6bcace5 2021-06-22 stsp if (err)
791 e6bcace5 2021-06-22 stsp goto done;
792 67fd6849 2022-02-13 stsp
793 64a8571e 2022-01-07 stsp if (raw->f == NULL && base_raw->f == NULL) {
794 64a8571e 2022-01-07 stsp err = got_deltify_mem_mem(&deltas, &ndeltas,
795 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
796 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
797 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
798 64a8571e 2022-01-07 stsp base_raw->hdrlen,
799 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
800 64a8571e 2022-01-07 stsp } else if (raw->f == NULL) {
801 64a8571e 2022-01-07 stsp err = got_deltify_mem_file(&deltas, &ndeltas,
802 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
803 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
804 64a8571e 2022-01-07 stsp base->dtab, base_raw->f,
805 64a8571e 2022-01-07 stsp base_raw->hdrlen,
806 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
807 64a8571e 2022-01-07 stsp } else if (base_raw->f == NULL) {
808 64a8571e 2022-01-07 stsp err = got_deltify_file_mem(&deltas, &ndeltas,
809 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
810 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
811 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
812 64a8571e 2022-01-07 stsp base_raw->hdrlen,
813 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
814 64a8571e 2022-01-07 stsp } else {
815 64a8571e 2022-01-07 stsp err = got_deltify(&deltas, &ndeltas,
816 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
817 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
818 64a8571e 2022-01-07 stsp base->dtab, base_raw->f, base_raw->hdrlen,
819 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
820 64a8571e 2022-01-07 stsp }
821 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
822 e6bcace5 2021-06-22 stsp base_raw = NULL;
823 e6bcace5 2021-06-22 stsp if (err)
824 e6bcace5 2021-06-22 stsp goto done;
825 e6bcace5 2021-06-22 stsp
826 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
827 74881701 2021-10-15 stsp if (size + 32 < best_size){
828 e6bcace5 2021-06-22 stsp /*
829 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
830 e6bcace5 2021-06-22 stsp * replace it.
831 e6bcace5 2021-06-22 stsp */
832 74881701 2021-10-15 stsp best_size = size;
833 74881701 2021-10-15 stsp free(best_deltas);
834 74881701 2021-10-15 stsp best_deltas = deltas;
835 74881701 2021-10-15 stsp best_ndeltas = ndeltas;
836 74881701 2021-10-15 stsp deltas = NULL;
837 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
838 e6bcace5 2021-06-22 stsp m->prev = base;
839 e6bcace5 2021-06-22 stsp m->head = base->head;
840 e6bcace5 2021-06-22 stsp if (m->head == NULL)
841 e6bcace5 2021-06-22 stsp m->head = base;
842 e6bcace5 2021-06-22 stsp } else {
843 e6bcace5 2021-06-22 stsp free(deltas);
844 e6bcace5 2021-06-22 stsp deltas = NULL;
845 e6bcace5 2021-06-22 stsp ndeltas = 0;
846 e6bcace5 2021-06-22 stsp }
847 e6bcace5 2021-06-22 stsp }
848 e6bcace5 2021-06-22 stsp
849 74881701 2021-10-15 stsp if (best_ndeltas > 0) {
850 402a5ec1 2022-01-10 stsp if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
851 402a5ec1 2022-01-10 stsp delta_memsize + best_size <= max_delta_memsize) {
852 402a5ec1 2022-01-10 stsp delta_memsize += best_size;
853 5060d5a1 2022-01-10 stsp err = encode_delta_in_mem(m, raw, best_deltas,
854 5060d5a1 2022-01-10 stsp best_ndeltas, best_size, m->prev->size);
855 5060d5a1 2022-01-10 stsp } else {
856 5060d5a1 2022-01-10 stsp m->delta_offset = ftello(delta_cache);
857 5060d5a1 2022-01-10 stsp err = encode_delta(m, raw, best_deltas,
858 5060d5a1 2022-01-10 stsp best_ndeltas, m->prev->size, delta_cache);
859 5060d5a1 2022-01-10 stsp }
860 74881701 2021-10-15 stsp free(best_deltas);
861 74881701 2021-10-15 stsp best_deltas = NULL;
862 74881701 2021-10-15 stsp best_ndeltas = 0;
863 74881701 2021-10-15 stsp if (err)
864 74881701 2021-10-15 stsp goto done;
865 74881701 2021-10-15 stsp }
866 74881701 2021-10-15 stsp
867 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
868 e6bcace5 2021-06-22 stsp raw = NULL;
869 e6bcace5 2021-06-22 stsp }
870 e6bcace5 2021-06-22 stsp done:
871 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
872 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
873 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
874 e6bcace5 2021-06-22 stsp }
875 e6bcace5 2021-06-22 stsp if (raw)
876 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
877 e6bcace5 2021-06-22 stsp if (base_raw)
878 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
879 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
880 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
881 74881701 2021-10-15 stsp free(deltas);
882 74881701 2021-10-15 stsp free(best_deltas);
883 e6bcace5 2021-06-22 stsp return err;
884 e6bcace5 2021-06-22 stsp }
885 e6bcace5 2021-06-22 stsp
886 e6bcace5 2021-06-22 stsp static const struct got_error *
887 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
888 05118f5a 2021-06-22 stsp struct got_repository *repo)
889 05118f5a 2021-06-22 stsp {
890 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
891 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
892 05118f5a 2021-06-22 stsp int idx;
893 05118f5a 2021-06-22 stsp
894 05118f5a 2021-06-22 stsp *found = 0;
895 05118f5a 2021-06-22 stsp
896 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
897 05118f5a 2021-06-22 stsp if (err == NULL)
898 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
899 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
900 05118f5a 2021-06-22 stsp err = NULL;
901 05118f5a 2021-06-22 stsp return err;
902 05118f5a 2021-06-22 stsp }
903 05118f5a 2021-06-22 stsp
904 05118f5a 2021-06-22 stsp static const struct got_error *
905 67fd6849 2022-02-13 stsp add_object(int want_meta, struct got_object_idset *idset,
906 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
907 d6a28ffe 2022-05-20 op time_t mtime, uint32_t seed, int loose_obj_only,
908 d6a28ffe 2022-05-20 op struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
909 6863cbf9 2022-03-21 stsp got_pack_progress_cb progress_cb, void *progress_arg,
910 6863cbf9 2022-03-21 stsp struct got_ratelimit *rl)
911 e6bcace5 2021-06-22 stsp {
912 e6bcace5 2021-06-22 stsp const struct got_error *err;
913 67fd6849 2022-02-13 stsp struct got_pack_meta *m = NULL;
914 e6bcace5 2021-06-22 stsp
915 05118f5a 2021-06-22 stsp if (loose_obj_only) {
916 05118f5a 2021-06-22 stsp int is_packed;
917 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
918 05118f5a 2021-06-22 stsp if (err)
919 05118f5a 2021-06-22 stsp return err;
920 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
921 05118f5a 2021-06-22 stsp return NULL;
922 05118f5a 2021-06-22 stsp }
923 05118f5a 2021-06-22 stsp
924 67fd6849 2022-02-13 stsp if (want_meta) {
925 d6a28ffe 2022-05-20 op err = alloc_meta(&m, id, path, obj_type, mtime, seed);
926 6863cbf9 2022-03-21 stsp if (err)
927 6863cbf9 2022-03-21 stsp return err;
928 6863cbf9 2022-03-21 stsp
929 6863cbf9 2022-03-21 stsp (*nfound)++;
930 6863cbf9 2022-03-21 stsp err = report_progress(progress_cb, progress_arg, rl,
931 6863cbf9 2022-03-21 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
932 bb6672b6 2022-04-14 tb if (err) {
933 bb6672b6 2022-04-14 tb clear_meta(m);
934 bb6672b6 2022-04-14 tb free(m);
935 67fd6849 2022-02-13 stsp return err;
936 bb6672b6 2022-04-14 tb }
937 e6bcace5 2021-06-22 stsp }
938 e6bcace5 2021-06-22 stsp
939 bb6672b6 2022-04-14 tb err = got_object_idset_add(idset, id, m);
940 bb6672b6 2022-04-14 tb if (err) {
941 bb6672b6 2022-04-14 tb clear_meta(m);
942 bb6672b6 2022-04-14 tb free(m);
943 bb6672b6 2022-04-14 tb }
944 bb6672b6 2022-04-14 tb return err;
945 e6bcace5 2021-06-22 stsp }
946 e6bcace5 2021-06-22 stsp
947 e6bcace5 2021-06-22 stsp static const struct got_error *
948 67fd6849 2022-02-13 stsp load_tree_entries(struct got_object_id_queue *ids, int want_meta,
949 2f8438b0 2022-05-04 stsp struct got_object_idset *idset, struct got_object_idset *idset_exclude,
950 0ab4c957 2022-06-13 stsp struct got_tree_object *tree,
951 d6a28ffe 2022-05-20 op const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
952 b8af7c06 2022-03-15 stsp int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
953 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
954 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
955 e6bcace5 2021-06-22 stsp {
956 e6bcace5 2021-06-22 stsp const struct got_error *err;
957 e6bcace5 2021-06-22 stsp char *p = NULL;
958 e6bcace5 2021-06-22 stsp int i;
959 b8af7c06 2022-03-15 stsp
960 b8af7c06 2022-03-15 stsp (*ntrees)++;
961 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
962 b8af7c06 2022-03-15 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
963 e6bcace5 2021-06-22 stsp if (err)
964 e6bcace5 2021-06-22 stsp return err;
965 e6bcace5 2021-06-22 stsp
966 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
967 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
968 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
969 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
970 e6bcace5 2021-06-22 stsp
971 e6bcace5 2021-06-22 stsp if (cancel_cb) {
972 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
973 e6bcace5 2021-06-22 stsp if (err)
974 e6bcace5 2021-06-22 stsp break;
975 e6bcace5 2021-06-22 stsp }
976 e6bcace5 2021-06-22 stsp
977 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
978 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset, id) ||
979 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
980 cee6a7ea 2022-06-07 stsp continue;
981 0ab4c957 2022-06-13 stsp
982 0ab4c957 2022-06-13 stsp /*
983 0ab4c957 2022-06-13 stsp * If got-read-pack is crawling trees for us then
984 0ab4c957 2022-06-13 stsp * we are only here to collect blob IDs.
985 0ab4c957 2022-06-13 stsp */
986 0ab4c957 2022-06-13 stsp if (ids == NULL && S_ISDIR(mode))
987 0ab4c957 2022-06-13 stsp continue;
988 0ab4c957 2022-06-13 stsp
989 0ab4c957 2022-06-13 stsp if (asprintf(&p, "%s%s%s", dpath,
990 0ab4c957 2022-06-13 stsp got_path_is_root_dir(dpath) ? "" : "/",
991 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
992 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
993 e6bcace5 2021-06-22 stsp break;
994 e6bcace5 2021-06-22 stsp }
995 e6bcace5 2021-06-22 stsp
996 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
997 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
998 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
999 e6bcace5 2021-06-22 stsp if (err)
1000 e6bcace5 2021-06-22 stsp break;
1001 3e6ceea0 2022-05-20 stsp qid->data = p;
1002 3e6ceea0 2022-05-20 stsp p = NULL;
1003 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
1004 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
1005 2f8438b0 2022-05-04 stsp err = add_object(want_meta,
1006 2f8438b0 2022-05-04 stsp want_meta ? idset : idset_exclude, id, p,
1007 d6a28ffe 2022-05-20 op GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
1008 d6a28ffe 2022-05-20 op repo, ncolored, nfound, ntrees,
1009 6863cbf9 2022-03-21 stsp progress_cb, progress_arg, rl);
1010 b8af7c06 2022-03-15 stsp if (err)
1011 b8af7c06 2022-03-15 stsp break;
1012 3e6ceea0 2022-05-20 stsp free(p);
1013 3e6ceea0 2022-05-20 stsp p = NULL;
1014 3e6ceea0 2022-05-20 stsp } else {
1015 3e6ceea0 2022-05-20 stsp free(p);
1016 3e6ceea0 2022-05-20 stsp p = NULL;
1017 e6bcace5 2021-06-22 stsp }
1018 e6bcace5 2021-06-22 stsp }
1019 e6bcace5 2021-06-22 stsp
1020 e6bcace5 2021-06-22 stsp free(p);
1021 e6bcace5 2021-06-22 stsp return err;
1022 e6bcace5 2021-06-22 stsp }
1023 e6bcace5 2021-06-22 stsp
1024 e6bcace5 2021-06-22 stsp static const struct got_error *
1025 67fd6849 2022-02-13 stsp load_tree(int want_meta, struct got_object_idset *idset,
1026 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1027 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
1028 d6a28ffe 2022-05-20 op uint32_t seed, struct got_repository *repo, int loose_obj_only,
1029 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
1030 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1031 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1032 e6bcace5 2021-06-22 stsp {
1033 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1034 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
1035 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1036 0ab4c957 2022-06-13 stsp struct got_tree_object *tree = NULL;
1037 e6bcace5 2021-06-22 stsp
1038 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, tree_id) ||
1039 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, tree_id))
1040 e6bcace5 2021-06-22 stsp return NULL;
1041 e6bcace5 2021-06-22 stsp
1042 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
1043 e6bcace5 2021-06-22 stsp if (err)
1044 3e6ceea0 2022-05-20 stsp return err;
1045 3e6ceea0 2022-05-20 stsp qid->data = strdup(dpath);
1046 3e6ceea0 2022-05-20 stsp if (qid->data == NULL) {
1047 3e6ceea0 2022-05-20 stsp err = got_error_from_errno("strdup");
1048 3e6ceea0 2022-05-20 stsp got_object_qid_free(qid);
1049 e6bcace5 2021-06-22 stsp return err;
1050 3e6ceea0 2022-05-20 stsp }
1051 e6bcace5 2021-06-22 stsp
1052 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
1053 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1054 e6bcace5 2021-06-22 stsp
1055 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
1056 3e6ceea0 2022-05-20 stsp const char *path;
1057 e6bcace5 2021-06-22 stsp if (cancel_cb) {
1058 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
1059 e6bcace5 2021-06-22 stsp if (err)
1060 e6bcace5 2021-06-22 stsp break;
1061 e6bcace5 2021-06-22 stsp }
1062 e6bcace5 2021-06-22 stsp
1063 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
1064 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
1065 3e6ceea0 2022-05-20 stsp path = qid->data;
1066 e6bcace5 2021-06-22 stsp
1067 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, &qid->id) ||
1068 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, &qid->id)) {
1069 3e6ceea0 2022-05-20 stsp free(qid->data);
1070 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1071 e6bcace5 2021-06-22 stsp continue;
1072 e6bcace5 2021-06-22 stsp }
1073 e6bcace5 2021-06-22 stsp
1074 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1075 3e6ceea0 2022-05-20 stsp &qid->id, path, GOT_OBJ_TYPE_TREE,
1076 d6a28ffe 2022-05-20 op mtime, seed, loose_obj_only, repo,
1077 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1078 e6bcace5 2021-06-22 stsp if (err) {
1079 3e6ceea0 2022-05-20 stsp free(qid->data);
1080 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1081 e6bcace5 2021-06-22 stsp break;
1082 e6bcace5 2021-06-22 stsp }
1083 e6bcace5 2021-06-22 stsp
1084 0ab4c957 2022-06-13 stsp err = got_object_open_as_tree(&tree, repo, &qid->id);
1085 0ab4c957 2022-06-13 stsp if (err) {
1086 0ab4c957 2022-06-13 stsp free(qid->data);
1087 0ab4c957 2022-06-13 stsp got_object_qid_free(qid);
1088 0ab4c957 2022-06-13 stsp break;
1089 0ab4c957 2022-06-13 stsp }
1090 0ab4c957 2022-06-13 stsp
1091 2f8438b0 2022-05-04 stsp err = load_tree_entries(&tree_ids, want_meta, idset,
1092 0ab4c957 2022-06-13 stsp idset_exclude, tree, path, mtime, seed, repo,
1093 0ab4c957 2022-06-13 stsp loose_obj_only, ncolored, nfound, ntrees,
1094 0ab4c957 2022-06-13 stsp progress_cb, progress_arg, rl,
1095 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1096 3e6ceea0 2022-05-20 stsp free(qid->data);
1097 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1098 e6bcace5 2021-06-22 stsp if (err)
1099 e6bcace5 2021-06-22 stsp break;
1100 0ab4c957 2022-06-13 stsp
1101 0ab4c957 2022-06-13 stsp got_object_tree_close(tree);
1102 0ab4c957 2022-06-13 stsp tree = NULL;
1103 e6bcace5 2021-06-22 stsp }
1104 e6bcace5 2021-06-22 stsp
1105 3e6ceea0 2022-05-20 stsp STAILQ_FOREACH(qid, &tree_ids, entry)
1106 3e6ceea0 2022-05-20 stsp free(qid->data);
1107 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
1108 0ab4c957 2022-06-13 stsp if (tree)
1109 0ab4c957 2022-06-13 stsp got_object_tree_close(tree);
1110 e6bcace5 2021-06-22 stsp return err;
1111 e6bcace5 2021-06-22 stsp }
1112 e6bcace5 2021-06-22 stsp
1113 e6bcace5 2021-06-22 stsp static const struct got_error *
1114 67fd6849 2022-02-13 stsp load_commit(int want_meta, struct got_object_idset *idset,
1115 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1116 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1117 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1118 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1119 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1120 e6bcace5 2021-06-22 stsp {
1121 e6bcace5 2021-06-22 stsp const struct got_error *err;
1122 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1123 e6bcace5 2021-06-22 stsp
1124 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
1125 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1126 e6bcace5 2021-06-22 stsp return NULL;
1127 05118f5a 2021-06-22 stsp
1128 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1129 05118f5a 2021-06-22 stsp int is_packed;
1130 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1131 05118f5a 2021-06-22 stsp if (err)
1132 05118f5a 2021-06-22 stsp return err;
1133 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1134 05118f5a 2021-06-22 stsp return NULL;
1135 05118f5a 2021-06-22 stsp }
1136 e6bcace5 2021-06-22 stsp
1137 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1138 e6bcace5 2021-06-22 stsp if (err)
1139 e6bcace5 2021-06-22 stsp return err;
1140 e6bcace5 2021-06-22 stsp
1141 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1142 2f8438b0 2022-05-04 stsp id, "", GOT_OBJ_TYPE_COMMIT,
1143 d6a28ffe 2022-05-20 op got_object_commit_get_committer_time(commit), seed,
1144 6863cbf9 2022-03-21 stsp loose_obj_only, repo,
1145 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1146 e6bcace5 2021-06-22 stsp if (err)
1147 e6bcace5 2021-06-22 stsp goto done;
1148 b8af7c06 2022-03-15 stsp
1149 2f8438b0 2022-05-04 stsp err = load_tree(want_meta, idset, idset_exclude,
1150 2f8438b0 2022-05-04 stsp got_object_commit_get_tree_id(commit),
1151 d6a28ffe 2022-05-20 op "", got_object_commit_get_committer_time(commit), seed,
1152 b8af7c06 2022-03-15 stsp repo, loose_obj_only, ncolored, nfound, ntrees,
1153 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1154 e6bcace5 2021-06-22 stsp done:
1155 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1156 e6bcace5 2021-06-22 stsp return err;
1157 e6bcace5 2021-06-22 stsp }
1158 e6bcace5 2021-06-22 stsp
1159 05118f5a 2021-06-22 stsp static const struct got_error *
1160 67fd6849 2022-02-13 stsp load_tag(int want_meta, struct got_object_idset *idset,
1161 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1162 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1163 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1164 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1165 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1166 05118f5a 2021-06-22 stsp {
1167 05118f5a 2021-06-22 stsp const struct got_error *err;
1168 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
1169 05118f5a 2021-06-22 stsp
1170 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
1171 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1172 05118f5a 2021-06-22 stsp return NULL;
1173 05118f5a 2021-06-22 stsp
1174 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1175 05118f5a 2021-06-22 stsp int is_packed;
1176 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1177 05118f5a 2021-06-22 stsp if (err)
1178 05118f5a 2021-06-22 stsp return err;
1179 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1180 05118f5a 2021-06-22 stsp return NULL;
1181 05118f5a 2021-06-22 stsp }
1182 05118f5a 2021-06-22 stsp
1183 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1184 05118f5a 2021-06-22 stsp if (err)
1185 05118f5a 2021-06-22 stsp return err;
1186 05118f5a 2021-06-22 stsp
1187 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1188 2f8438b0 2022-05-04 stsp id, "", GOT_OBJ_TYPE_TAG,
1189 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1190 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1191 05118f5a 2021-06-22 stsp if (err)
1192 05118f5a 2021-06-22 stsp goto done;
1193 05118f5a 2021-06-22 stsp
1194 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1195 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1196 2f8438b0 2022-05-04 stsp err = load_commit(want_meta, idset, idset_exclude,
1197 d6a28ffe 2022-05-20 op got_object_tag_get_object_id(tag), repo, seed,
1198 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1199 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1200 05118f5a 2021-06-22 stsp break;
1201 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1202 2f8438b0 2022-05-04 stsp err = load_tree(want_meta, idset, idset_exclude,
1203 67fd6849 2022-02-13 stsp got_object_tag_get_object_id(tag), "",
1204 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, repo,
1205 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1206 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1207 05118f5a 2021-06-22 stsp break;
1208 05118f5a 2021-06-22 stsp default:
1209 05118f5a 2021-06-22 stsp break;
1210 05118f5a 2021-06-22 stsp }
1211 05118f5a 2021-06-22 stsp
1212 05118f5a 2021-06-22 stsp done:
1213 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1214 05118f5a 2021-06-22 stsp return err;
1215 05118f5a 2021-06-22 stsp }
1216 05118f5a 2021-06-22 stsp
1217 e6bcace5 2021-06-22 stsp enum findtwixt_color {
1218 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
1219 e6bcace5 2021-06-22 stsp COLOR_DROP,
1220 e6bcace5 2021-06-22 stsp COLOR_BLANK,
1221 70f8f24d 2022-04-14 stsp COLOR_SKIP,
1222 e6bcace5 2021-06-22 stsp };
1223 70f8f24d 2022-04-14 stsp
1224 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
1225 e6bcace5 2021-06-22 stsp COLOR_KEEP,
1226 e6bcace5 2021-06-22 stsp COLOR_DROP,
1227 70f8f24d 2022-04-14 stsp COLOR_BLANK,
1228 70f8f24d 2022-04-14 stsp COLOR_SKIP,
1229 e6bcace5 2021-06-22 stsp };
1230 e6bcace5 2021-06-22 stsp
1231 e6bcace5 2021-06-22 stsp static const struct got_error *
1232 70f8f24d 2022-04-14 stsp paint_commit(struct got_object_qid *qid, int color)
1233 e6bcace5 2021-06-22 stsp {
1234 70f8f24d 2022-04-14 stsp if (color < 0 || color >= nitems(findtwixt_colors))
1235 70f8f24d 2022-04-14 stsp return got_error(GOT_ERR_RANGE);
1236 e6bcace5 2021-06-22 stsp
1237 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
1238 e6bcace5 2021-06-22 stsp return NULL;
1239 e6bcace5 2021-06-22 stsp }
1240 e6bcace5 2021-06-22 stsp
1241 e6bcace5 2021-06-22 stsp static const struct got_error *
1242 70f8f24d 2022-04-14 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1243 70f8f24d 2022-04-14 stsp int color, struct got_repository *repo)
1244 e6bcace5 2021-06-22 stsp {
1245 70f8f24d 2022-04-14 stsp const struct got_error *err;
1246 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1247 e6bcace5 2021-06-22 stsp
1248 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1249 e6bcace5 2021-06-22 stsp if (err)
1250 e6bcace5 2021-06-22 stsp return err;
1251 e6bcace5 2021-06-22 stsp
1252 70f8f24d 2022-04-14 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
1253 70f8f24d 2022-04-14 stsp return paint_commit(qid, color);
1254 e6bcace5 2021-06-22 stsp }
1255 e6bcace5 2021-06-22 stsp
1256 e6bcace5 2021-06-22 stsp struct append_id_arg {
1257 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1258 e6bcace5 2021-06-22 stsp int idx;
1259 70f8f24d 2022-04-14 stsp struct got_object_idset *drop;
1260 70f8f24d 2022-04-14 stsp struct got_object_idset *skip;
1261 e6bcace5 2021-06-22 stsp };
1262 e6bcace5 2021-06-22 stsp
1263 e6bcace5 2021-06-22 stsp static const struct got_error *
1264 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1265 e6bcace5 2021-06-22 stsp {
1266 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1267 e6bcace5 2021-06-22 stsp
1268 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(a->skip, id) ||
1269 70f8f24d 2022-04-14 stsp got_object_idset_contains(a->drop, id))
1270 70f8f24d 2022-04-14 stsp return NULL;
1271 70f8f24d 2022-04-14 stsp
1272 70f8f24d 2022-04-14 stsp a->array[++a->idx] = got_object_id_dup(id);
1273 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1274 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1275 e6bcace5 2021-06-22 stsp
1276 e6bcace5 2021-06-22 stsp return NULL;
1277 29e0594f 2022-04-09 stsp }
1278 29e0594f 2022-04-09 stsp
1279 29e0594f 2022-04-09 stsp static const struct got_error *
1280 29e0594f 2022-04-09 stsp queue_commit_or_tag_id(struct got_object_id *id, int color,
1281 29e0594f 2022-04-09 stsp struct got_object_id_queue *ids, struct got_repository *repo)
1282 29e0594f 2022-04-09 stsp {
1283 29e0594f 2022-04-09 stsp const struct got_error *err;
1284 29e0594f 2022-04-09 stsp struct got_tag_object *tag = NULL;
1285 29e0594f 2022-04-09 stsp int obj_type;
1286 29e0594f 2022-04-09 stsp
1287 29e0594f 2022-04-09 stsp err = got_object_get_type(&obj_type, repo, id);
1288 29e0594f 2022-04-09 stsp if (err)
1289 29e0594f 2022-04-09 stsp return err;
1290 29e0594f 2022-04-09 stsp
1291 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1292 29e0594f 2022-04-09 stsp err = got_object_open_as_tag(&tag, repo, id);
1293 29e0594f 2022-04-09 stsp if (err)
1294 29e0594f 2022-04-09 stsp return err;
1295 29e0594f 2022-04-09 stsp obj_type = got_object_tag_get_object_type(tag);
1296 29e0594f 2022-04-09 stsp id = got_object_tag_get_object_id(tag);
1297 29e0594f 2022-04-09 stsp }
1298 29e0594f 2022-04-09 stsp
1299 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1300 29e0594f 2022-04-09 stsp err = queue_commit_id(ids, id, color, repo);
1301 29e0594f 2022-04-09 stsp if (err)
1302 29e0594f 2022-04-09 stsp goto done;
1303 29e0594f 2022-04-09 stsp }
1304 29e0594f 2022-04-09 stsp done:
1305 29e0594f 2022-04-09 stsp if (tag)
1306 29e0594f 2022-04-09 stsp got_object_tag_close(tag);
1307 29e0594f 2022-04-09 stsp return err;
1308 e6bcace5 2021-06-22 stsp }
1309 e6bcace5 2021-06-22 stsp
1310 e6bcace5 2021-06-22 stsp static const struct got_error *
1311 70f8f24d 2022-04-14 stsp paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1312 14dbbf48 2022-04-10 stsp struct got_object_idset *keep, struct got_object_idset *drop,
1313 70f8f24d 2022-04-14 stsp struct got_object_idset *skip, struct got_repository *repo,
1314 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1315 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1316 e6bcace5 2021-06-22 stsp {
1317 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1318 14dbbf48 2022-04-10 stsp struct got_commit_object *commit = NULL;
1319 70f8f24d 2022-04-14 stsp const struct got_object_id_queue *parents;
1320 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1321 70f8f24d 2022-04-14 stsp int nqueued = nids, nskip = 0;
1322 e6bcace5 2021-06-22 stsp
1323 70f8f24d 2022-04-14 stsp while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1324 70f8f24d 2022-04-14 stsp int color;
1325 03c03172 2022-04-10 stsp
1326 57bc7b6d 2022-04-10 stsp if (cancel_cb) {
1327 57bc7b6d 2022-04-10 stsp err = cancel_cb(cancel_arg);
1328 57bc7b6d 2022-04-10 stsp if (err)
1329 14dbbf48 2022-04-10 stsp break;
1330 57bc7b6d 2022-04-10 stsp }
1331 57bc7b6d 2022-04-10 stsp
1332 14dbbf48 2022-04-10 stsp qid = STAILQ_FIRST(ids);
1333 70f8f24d 2022-04-14 stsp STAILQ_REMOVE_HEAD(ids, entry);
1334 70f8f24d 2022-04-14 stsp nqueued--;
1335 70f8f24d 2022-04-14 stsp color = *((int *)qid->data);
1336 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1337 70f8f24d 2022-04-14 stsp nskip--;
1338 e6bcace5 2021-06-22 stsp
1339 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(skip, &qid->id)) {
1340 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1341 70f8f24d 2022-04-14 stsp continue;
1342 70f8f24d 2022-04-14 stsp }
1343 b8af7c06 2022-03-15 stsp
1344 70f8f24d 2022-04-14 stsp switch (color) {
1345 70f8f24d 2022-04-14 stsp case COLOR_KEEP:
1346 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(keep, &qid->id)) {
1347 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1348 70f8f24d 2022-04-14 stsp continue;
1349 70f8f24d 2022-04-14 stsp }
1350 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(drop, &qid->id)) {
1351 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1352 70f8f24d 2022-04-14 stsp if (err)
1353 70f8f24d 2022-04-14 stsp goto done;
1354 70f8f24d 2022-04-14 stsp } else
1355 70f8f24d 2022-04-14 stsp (*ncolored)++;
1356 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(keep, &qid->id, NULL);
1357 70f8f24d 2022-04-14 stsp if (err)
1358 70f8f24d 2022-04-14 stsp goto done;
1359 70f8f24d 2022-04-14 stsp break;
1360 70f8f24d 2022-04-14 stsp case COLOR_DROP:
1361 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(drop, &qid->id)) {
1362 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1363 70f8f24d 2022-04-14 stsp continue;
1364 70f8f24d 2022-04-14 stsp }
1365 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(keep, &qid->id)) {
1366 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1367 70f8f24d 2022-04-14 stsp if (err)
1368 70f8f24d 2022-04-14 stsp goto done;
1369 70f8f24d 2022-04-14 stsp } else
1370 70f8f24d 2022-04-14 stsp (*ncolored)++;
1371 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(drop, &qid->id, NULL);
1372 70f8f24d 2022-04-14 stsp if (err)
1373 70f8f24d 2022-04-14 stsp goto done;
1374 70f8f24d 2022-04-14 stsp break;
1375 70f8f24d 2022-04-14 stsp case COLOR_SKIP:
1376 d7b5a0e8 2022-04-20 stsp if (!got_object_idset_contains(skip, &qid->id)) {
1377 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(skip, &qid->id,
1378 d7b5a0e8 2022-04-20 stsp NULL);
1379 70f8f24d 2022-04-14 stsp if (err)
1380 70f8f24d 2022-04-14 stsp goto done;
1381 70f8f24d 2022-04-14 stsp }
1382 70f8f24d 2022-04-14 stsp break;
1383 70f8f24d 2022-04-14 stsp default:
1384 70f8f24d 2022-04-14 stsp /* should not happen */
1385 70f8f24d 2022-04-14 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
1386 70f8f24d 2022-04-14 stsp "%s invalid commit color %d", __func__, color);
1387 70f8f24d 2022-04-14 stsp goto done;
1388 70f8f24d 2022-04-14 stsp }
1389 70f8f24d 2022-04-14 stsp
1390 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
1391 b8af7c06 2022-03-15 stsp *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1392 b8af7c06 2022-03-15 stsp if (err)
1393 14dbbf48 2022-04-10 stsp break;
1394 e6bcace5 2021-06-22 stsp
1395 e6bcace5 2021-06-22 stsp
1396 d7b5a0e8 2022-04-20 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
1397 70f8f24d 2022-04-14 stsp if (err)
1398 70f8f24d 2022-04-14 stsp break;
1399 e6bcace5 2021-06-22 stsp
1400 70f8f24d 2022-04-14 stsp parents = got_object_commit_get_parent_ids(commit);
1401 70f8f24d 2022-04-14 stsp if (parents) {
1402 70f8f24d 2022-04-14 stsp struct got_object_qid *pid;
1403 70f8f24d 2022-04-14 stsp color = *((int *)qid->data);
1404 70f8f24d 2022-04-14 stsp STAILQ_FOREACH(pid, parents, entry) {
1405 d7b5a0e8 2022-04-20 stsp err = queue_commit_id(ids, &pid->id, color,
1406 70f8f24d 2022-04-14 stsp repo);
1407 70f8f24d 2022-04-14 stsp if (err)
1408 70f8f24d 2022-04-14 stsp break;
1409 70f8f24d 2022-04-14 stsp nqueued++;
1410 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1411 70f8f24d 2022-04-14 stsp nskip++;
1412 e6bcace5 2021-06-22 stsp }
1413 e6bcace5 2021-06-22 stsp }
1414 e6bcace5 2021-06-22 stsp
1415 70f8f24d 2022-04-14 stsp got_object_commit_close(commit);
1416 70f8f24d 2022-04-14 stsp commit = NULL;
1417 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1418 e6bcace5 2021-06-22 stsp }
1419 70f8f24d 2022-04-14 stsp done:
1420 14dbbf48 2022-04-10 stsp if (commit)
1421 14dbbf48 2022-04-10 stsp got_object_commit_close(commit);
1422 14dbbf48 2022-04-10 stsp return err;
1423 14dbbf48 2022-04-10 stsp }
1424 14dbbf48 2022-04-10 stsp
1425 14dbbf48 2022-04-10 stsp static const struct got_error *
1426 14dbbf48 2022-04-10 stsp findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1427 14dbbf48 2022-04-10 stsp struct got_object_id **head, int nhead,
1428 14dbbf48 2022-04-10 stsp struct got_object_id **tail, int ntail,
1429 14dbbf48 2022-04-10 stsp struct got_repository *repo,
1430 14dbbf48 2022-04-10 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1431 14dbbf48 2022-04-10 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1432 14dbbf48 2022-04-10 stsp {
1433 14dbbf48 2022-04-10 stsp const struct got_error *err = NULL;
1434 14dbbf48 2022-04-10 stsp struct got_object_id_queue ids;
1435 70f8f24d 2022-04-14 stsp struct got_object_idset *keep, *drop, *skip = NULL;
1436 14dbbf48 2022-04-10 stsp int i, nkeep;
1437 14dbbf48 2022-04-10 stsp
1438 14dbbf48 2022-04-10 stsp STAILQ_INIT(&ids);
1439 14dbbf48 2022-04-10 stsp *res = NULL;
1440 14dbbf48 2022-04-10 stsp *nres = 0;
1441 14dbbf48 2022-04-10 stsp *ncolored = 0;
1442 14dbbf48 2022-04-10 stsp
1443 14dbbf48 2022-04-10 stsp keep = got_object_idset_alloc();
1444 14dbbf48 2022-04-10 stsp if (keep == NULL)
1445 14dbbf48 2022-04-10 stsp return got_error_from_errno("got_object_idset_alloc");
1446 14dbbf48 2022-04-10 stsp
1447 14dbbf48 2022-04-10 stsp drop = got_object_idset_alloc();
1448 14dbbf48 2022-04-10 stsp if (drop == NULL) {
1449 14dbbf48 2022-04-10 stsp err = got_error_from_errno("got_object_idset_alloc");
1450 14dbbf48 2022-04-10 stsp goto done;
1451 14dbbf48 2022-04-10 stsp }
1452 14dbbf48 2022-04-10 stsp
1453 70f8f24d 2022-04-14 stsp skip = got_object_idset_alloc();
1454 70f8f24d 2022-04-14 stsp if (skip == NULL) {
1455 70f8f24d 2022-04-14 stsp err = got_error_from_errno("got_object_idset_alloc");
1456 70f8f24d 2022-04-14 stsp goto done;
1457 70f8f24d 2022-04-14 stsp }
1458 70f8f24d 2022-04-14 stsp
1459 14dbbf48 2022-04-10 stsp for (i = 0; i < nhead; i++) {
1460 14dbbf48 2022-04-10 stsp struct got_object_id *id = head[i];
1461 14dbbf48 2022-04-10 stsp if (id == NULL)
1462 14dbbf48 2022-04-10 stsp continue;
1463 fbafdecf 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1464 14dbbf48 2022-04-10 stsp if (err)
1465 14dbbf48 2022-04-10 stsp goto done;
1466 14dbbf48 2022-04-10 stsp }
1467 14dbbf48 2022-04-10 stsp
1468 14dbbf48 2022-04-10 stsp for (i = 0; i < ntail; i++) {
1469 14dbbf48 2022-04-10 stsp struct got_object_id *id = tail[i];
1470 14dbbf48 2022-04-10 stsp if (id == NULL)
1471 14dbbf48 2022-04-10 stsp continue;
1472 14dbbf48 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1473 14dbbf48 2022-04-10 stsp if (err)
1474 14dbbf48 2022-04-10 stsp goto done;
1475 14dbbf48 2022-04-10 stsp }
1476 14dbbf48 2022-04-10 stsp
1477 70f8f24d 2022-04-14 stsp err = paint_commits(ncolored, &ids, nhead + ntail,
1478 70f8f24d 2022-04-14 stsp keep, drop, skip, repo, progress_cb, progress_arg, rl,
1479 70f8f24d 2022-04-14 stsp cancel_cb, cancel_arg);
1480 14dbbf48 2022-04-10 stsp if (err)
1481 14dbbf48 2022-04-10 stsp goto done;
1482 14dbbf48 2022-04-10 stsp
1483 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1484 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1485 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1486 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1487 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1488 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1489 e6bcace5 2021-06-22 stsp goto done;
1490 e6bcace5 2021-06-22 stsp }
1491 70f8f24d 2022-04-14 stsp arg.idx = -1;
1492 70f8f24d 2022-04-14 stsp arg.skip = skip;
1493 70f8f24d 2022-04-14 stsp arg.drop = drop;
1494 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1495 e6bcace5 2021-06-22 stsp if (err) {
1496 e6bcace5 2021-06-22 stsp free(arg.array);
1497 e6bcace5 2021-06-22 stsp goto done;
1498 e6bcace5 2021-06-22 stsp }
1499 e6bcace5 2021-06-22 stsp *res = arg.array;
1500 70f8f24d 2022-04-14 stsp *nres = arg.idx + 1;
1501 e6bcace5 2021-06-22 stsp }
1502 e6bcace5 2021-06-22 stsp done:
1503 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1504 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1505 70f8f24d 2022-04-14 stsp if (skip)
1506 70f8f24d 2022-04-14 stsp got_object_idset_free(skip);
1507 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1508 0ab4c957 2022-06-13 stsp return err;
1509 0ab4c957 2022-06-13 stsp }
1510 0ab4c957 2022-06-13 stsp
1511 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg {
1512 0ab4c957 2022-06-13 stsp /* output parameters: */
1513 0ab4c957 2022-06-13 stsp struct got_object_id *id;
1514 0ab4c957 2022-06-13 stsp char *dpath;
1515 0ab4c957 2022-06-13 stsp time_t mtime;
1516 0ab4c957 2022-06-13 stsp
1517 0ab4c957 2022-06-13 stsp /* input parameters: */
1518 0ab4c957 2022-06-13 stsp uint32_t seed;
1519 0ab4c957 2022-06-13 stsp int want_meta;
1520 0ab4c957 2022-06-13 stsp struct got_object_idset *idset;
1521 0ab4c957 2022-06-13 stsp struct got_object_idset *idset_exclude;
1522 0ab4c957 2022-06-13 stsp int loose_obj_only;
1523 0ab4c957 2022-06-13 stsp int *ncolored;
1524 0ab4c957 2022-06-13 stsp int *nfound;
1525 0ab4c957 2022-06-13 stsp int *ntrees;
1526 0ab4c957 2022-06-13 stsp got_pack_progress_cb progress_cb;
1527 0ab4c957 2022-06-13 stsp void *progress_arg;
1528 0ab4c957 2022-06-13 stsp struct got_ratelimit *rl;
1529 0ab4c957 2022-06-13 stsp got_cancel_cb cancel_cb;
1530 0ab4c957 2022-06-13 stsp void *cancel_arg;
1531 0ab4c957 2022-06-13 stsp };
1532 0ab4c957 2022-06-13 stsp
1533 0ab4c957 2022-06-13 stsp static const struct got_error *
1534 0ab4c957 2022-06-13 stsp load_packed_commit_id(void *arg, time_t mtime, struct got_object_id *id,
1535 0ab4c957 2022-06-13 stsp struct got_repository *repo)
1536 0ab4c957 2022-06-13 stsp {
1537 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg *a = arg;
1538 0ab4c957 2022-06-13 stsp
1539 0ab4c957 2022-06-13 stsp if (got_object_idset_contains(a->idset, id) ||
1540 0ab4c957 2022-06-13 stsp got_object_idset_contains(a->idset_exclude, id))
1541 0ab4c957 2022-06-13 stsp return NULL;
1542 0ab4c957 2022-06-13 stsp
1543 0ab4c957 2022-06-13 stsp return add_object(a->want_meta,
1544 0ab4c957 2022-06-13 stsp a->want_meta ? a->idset : a->idset_exclude,
1545 0ab4c957 2022-06-13 stsp id, "", GOT_OBJ_TYPE_COMMIT, mtime, a->seed, a->loose_obj_only,
1546 0ab4c957 2022-06-13 stsp repo, a->ncolored, a->nfound, a->ntrees,
1547 0ab4c957 2022-06-13 stsp a->progress_cb, a->progress_arg, a->rl);
1548 0ab4c957 2022-06-13 stsp }
1549 0ab4c957 2022-06-13 stsp
1550 0ab4c957 2022-06-13 stsp static const struct got_error *
1551 0ab4c957 2022-06-13 stsp load_packed_tree_ids(void *arg, struct got_tree_object *tree, time_t mtime,
1552 0ab4c957 2022-06-13 stsp struct got_object_id *id, const char *dpath, struct got_repository *repo)
1553 0ab4c957 2022-06-13 stsp {
1554 0ab4c957 2022-06-13 stsp const struct got_error *err;
1555 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg *a = arg;
1556 0ab4c957 2022-06-13 stsp const char *relpath;
1557 0ab4c957 2022-06-13 stsp
1558 0ab4c957 2022-06-13 stsp /*
1559 0ab4c957 2022-06-13 stsp * When we receive a tree's ID and path but not the tree itself,
1560 0ab4c957 2022-06-13 stsp * this tree object was not found in the pack file. This is the
1561 0ab4c957 2022-06-13 stsp * last time we are being called for this optimized traversal.
1562 0ab4c957 2022-06-13 stsp * Return from here and switch to loading objects the slow way.
1563 0ab4c957 2022-06-13 stsp */
1564 0ab4c957 2022-06-13 stsp if (tree == NULL) {
1565 0ab4c957 2022-06-13 stsp free(a->id);
1566 0ab4c957 2022-06-13 stsp a->id = got_object_id_dup(id);
1567 0ab4c957 2022-06-13 stsp if (a->id == NULL) {
1568 0ab4c957 2022-06-13 stsp err = got_error_from_errno("got_object_id_dup");
1569 0ab4c957 2022-06-13 stsp free(a->dpath);
1570 0ab4c957 2022-06-13 stsp a->dpath = NULL;
1571 0ab4c957 2022-06-13 stsp return err;
1572 0ab4c957 2022-06-13 stsp }
1573 0ab4c957 2022-06-13 stsp
1574 0ab4c957 2022-06-13 stsp free(a->dpath);
1575 0ab4c957 2022-06-13 stsp a->dpath = strdup(dpath);
1576 0ab4c957 2022-06-13 stsp if (a->dpath == NULL) {
1577 0ab4c957 2022-06-13 stsp err = got_error_from_errno("strdup");
1578 0ab4c957 2022-06-13 stsp free(a->id);
1579 0ab4c957 2022-06-13 stsp a->id = NULL;
1580 0ab4c957 2022-06-13 stsp return err;
1581 0ab4c957 2022-06-13 stsp }
1582 0ab4c957 2022-06-13 stsp
1583 0ab4c957 2022-06-13 stsp a->mtime = mtime;
1584 0ab4c957 2022-06-13 stsp return NULL;
1585 0ab4c957 2022-06-13 stsp }
1586 0ab4c957 2022-06-13 stsp
1587 0ab4c957 2022-06-13 stsp if (got_object_idset_contains(a->idset, id) ||
1588 0ab4c957 2022-06-13 stsp got_object_idset_contains(a->idset_exclude, id))
1589 0ab4c957 2022-06-13 stsp return NULL;
1590 0ab4c957 2022-06-13 stsp
1591 0ab4c957 2022-06-13 stsp relpath = dpath;
1592 0ab4c957 2022-06-13 stsp while (relpath[0] == '/')
1593 0ab4c957 2022-06-13 stsp relpath++;
1594 0ab4c957 2022-06-13 stsp
1595 0ab4c957 2022-06-13 stsp err = add_object(a->want_meta,
1596 0ab4c957 2022-06-13 stsp a->want_meta ? a->idset : a->idset_exclude,
1597 0ab4c957 2022-06-13 stsp id, relpath, GOT_OBJ_TYPE_TREE, mtime, a->seed,
1598 0ab4c957 2022-06-13 stsp a->loose_obj_only, repo, a->ncolored, a->nfound, a->ntrees,
1599 0ab4c957 2022-06-13 stsp a->progress_cb, a->progress_arg, a->rl);
1600 0ab4c957 2022-06-13 stsp if (err)
1601 0ab4c957 2022-06-13 stsp return err;
1602 0ab4c957 2022-06-13 stsp
1603 0ab4c957 2022-06-13 stsp return load_tree_entries(NULL, a->want_meta, a->idset,
1604 0ab4c957 2022-06-13 stsp a->idset_exclude, tree, dpath, mtime, a->seed, repo,
1605 0ab4c957 2022-06-13 stsp a->loose_obj_only, a->ncolored, a->nfound, a->ntrees,
1606 0ab4c957 2022-06-13 stsp a->progress_cb, a->progress_arg, a->rl,
1607 0ab4c957 2022-06-13 stsp a->cancel_cb, a->cancel_arg);
1608 0ab4c957 2022-06-13 stsp }
1609 0ab4c957 2022-06-13 stsp
1610 0ab4c957 2022-06-13 stsp static const struct got_error *
1611 db9b9b1c 2022-06-14 stsp load_packed_object_ids(int *found_all_objects,
1612 db9b9b1c 2022-06-14 stsp struct got_object_id **ours, int nours,
1613 0ab4c957 2022-06-13 stsp struct got_object_id **theirs, int ntheirs,
1614 0ab4c957 2022-06-13 stsp int want_meta, uint32_t seed, struct got_object_idset *idset,
1615 0ab4c957 2022-06-13 stsp struct got_object_idset *idset_exclude, int loose_obj_only,
1616 0ab4c957 2022-06-13 stsp struct got_repository *repo, struct got_packidx *packidx,
1617 0ab4c957 2022-06-13 stsp int *ncolored, int *nfound, int *ntrees,
1618 0ab4c957 2022-06-13 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1619 0ab4c957 2022-06-13 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1620 0ab4c957 2022-06-13 stsp {
1621 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1622 0ab4c957 2022-06-13 stsp struct load_packed_obj_arg lpa;
1623 0ab4c957 2022-06-13 stsp
1624 0ab4c957 2022-06-13 stsp memset(&lpa, 0, sizeof(lpa));
1625 0ab4c957 2022-06-13 stsp lpa.seed = seed;
1626 0ab4c957 2022-06-13 stsp lpa.want_meta = want_meta;
1627 0ab4c957 2022-06-13 stsp lpa.idset = idset;
1628 0ab4c957 2022-06-13 stsp lpa.idset_exclude = idset_exclude;
1629 0ab4c957 2022-06-13 stsp lpa.loose_obj_only = loose_obj_only;
1630 0ab4c957 2022-06-13 stsp lpa.ncolored = ncolored;
1631 0ab4c957 2022-06-13 stsp lpa.nfound = nfound;
1632 0ab4c957 2022-06-13 stsp lpa.ntrees = ntrees;
1633 0ab4c957 2022-06-13 stsp lpa.progress_cb = progress_cb;
1634 0ab4c957 2022-06-13 stsp lpa.progress_arg = progress_arg;
1635 0ab4c957 2022-06-13 stsp lpa.rl = rl;
1636 0ab4c957 2022-06-13 stsp lpa.cancel_cb = cancel_cb;
1637 0ab4c957 2022-06-13 stsp lpa.cancel_arg = cancel_arg;
1638 0ab4c957 2022-06-13 stsp
1639 0ab4c957 2022-06-13 stsp /* Attempt to load objects via got-read-pack, as far as possible. */
1640 db9b9b1c 2022-06-14 stsp err = got_object_enumerate(found_all_objects, load_packed_commit_id,
1641 0ab4c957 2022-06-13 stsp load_packed_tree_ids, &lpa, ours, nours, theirs, ntheirs,
1642 0ab4c957 2022-06-13 stsp packidx, repo);
1643 0ab4c957 2022-06-13 stsp if (err)
1644 0ab4c957 2022-06-13 stsp return err;
1645 0ab4c957 2022-06-13 stsp
1646 0ab4c957 2022-06-13 stsp if (lpa.id == NULL)
1647 0ab4c957 2022-06-13 stsp return NULL;
1648 0ab4c957 2022-06-13 stsp
1649 0ab4c957 2022-06-13 stsp /*
1650 0ab4c957 2022-06-13 stsp * An incomplete tree hierarchy was present in the pack file
1651 db9b9b1c 2022-06-14 stsp * and caused loading to be aborted.
1652 0ab4c957 2022-06-13 stsp * Continue loading trees the slow way.
1653 0ab4c957 2022-06-13 stsp */
1654 0ab4c957 2022-06-13 stsp err = load_tree(want_meta, idset, idset_exclude,
1655 0ab4c957 2022-06-13 stsp lpa.id, lpa.dpath, lpa.mtime, seed, repo, loose_obj_only,
1656 0ab4c957 2022-06-13 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
1657 0ab4c957 2022-06-13 stsp cancel_cb, cancel_arg);
1658 0ab4c957 2022-06-13 stsp free(lpa.id);
1659 0ab4c957 2022-06-13 stsp free(lpa.dpath);
1660 e6bcace5 2021-06-22 stsp return err;
1661 e6bcace5 2021-06-22 stsp }
1662 e6bcace5 2021-06-22 stsp
1663 e6bcace5 2021-06-22 stsp static const struct got_error *
1664 0ab4c957 2022-06-13 stsp find_pack_for_enumeration(struct got_packidx **best_packidx,
1665 0ab4c957 2022-06-13 stsp struct got_object_id **ids, int nids, struct got_repository *repo)
1666 0ab4c957 2022-06-13 stsp {
1667 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1668 0ab4c957 2022-06-13 stsp struct got_pathlist_entry *pe;
1669 0ab4c957 2022-06-13 stsp const char *best_packidx_path = NULL;
1670 0ab4c957 2022-06-13 stsp int nobj_max = 0;
1671 0ab4c957 2022-06-13 stsp int ncommits_max = 0;
1672 0ab4c957 2022-06-13 stsp
1673 0ab4c957 2022-06-13 stsp *best_packidx = NULL;
1674 0ab4c957 2022-06-13 stsp
1675 0ab4c957 2022-06-13 stsp /*
1676 0ab4c957 2022-06-13 stsp * Find the largest pack which contains at least some of the
1677 0ab4c957 2022-06-13 stsp * commits and tags we are interested in.
1678 0ab4c957 2022-06-13 stsp */
1679 0ab4c957 2022-06-13 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1680 0ab4c957 2022-06-13 stsp const char *path_packidx = pe->path;
1681 0ab4c957 2022-06-13 stsp struct got_packidx *packidx;
1682 0ab4c957 2022-06-13 stsp int nobj, i, idx, ncommits = 0;
1683 0ab4c957 2022-06-13 stsp
1684 0ab4c957 2022-06-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
1685 0ab4c957 2022-06-13 stsp if (err)
1686 0ab4c957 2022-06-13 stsp break;
1687 0ab4c957 2022-06-13 stsp
1688 0ab4c957 2022-06-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1689 0ab4c957 2022-06-13 stsp if (nobj <= nobj_max)
1690 0ab4c957 2022-06-13 stsp continue;
1691 0ab4c957 2022-06-13 stsp
1692 0ab4c957 2022-06-13 stsp for (i = 0; i < nids; i++) {
1693 0ab4c957 2022-06-13 stsp idx = got_packidx_get_object_idx(packidx, ids[i]);
1694 0ab4c957 2022-06-13 stsp if (idx != -1)
1695 0ab4c957 2022-06-13 stsp ncommits++;
1696 0ab4c957 2022-06-13 stsp }
1697 0ab4c957 2022-06-13 stsp if (ncommits > ncommits_max) {
1698 0ab4c957 2022-06-13 stsp best_packidx_path = path_packidx;
1699 0ab4c957 2022-06-13 stsp nobj_max = nobj;
1700 0ab4c957 2022-06-13 stsp ncommits_max = ncommits;
1701 0ab4c957 2022-06-13 stsp }
1702 0ab4c957 2022-06-13 stsp }
1703 0ab4c957 2022-06-13 stsp
1704 eb7b30a1 2022-06-13 stsp if (best_packidx_path && err == NULL) {
1705 0ab4c957 2022-06-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
1706 0ab4c957 2022-06-13 stsp repo);
1707 0ab4c957 2022-06-13 stsp }
1708 0ab4c957 2022-06-13 stsp
1709 0ab4c957 2022-06-13 stsp return err;
1710 0ab4c957 2022-06-13 stsp }
1711 0ab4c957 2022-06-13 stsp
1712 0ab4c957 2022-06-13 stsp static const struct got_error *
1713 b8af7c06 2022-03-15 stsp load_object_ids(int *ncolored, int *nfound, int *ntrees,
1714 b8af7c06 2022-03-15 stsp struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1715 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1716 d6a28ffe 2022-05-20 op uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1717 d6a28ffe 2022-05-20 op void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1718 d6a28ffe 2022-05-20 op void *cancel_arg)
1719 e6bcace5 2021-06-22 stsp {
1720 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1721 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1722 0ab4c957 2022-06-13 stsp struct got_packidx *packidx = NULL;
1723 db9b9b1c 2022-06-14 stsp int i, nobj = 0, obj_type, found_all_objects = 0;
1724 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude;
1725 2f8438b0 2022-05-04 stsp
1726 2f8438b0 2022-05-04 stsp idset_exclude = got_object_idset_alloc();
1727 2f8438b0 2022-05-04 stsp if (idset_exclude == NULL)
1728 2f8438b0 2022-05-04 stsp return got_error_from_errno("got_object_idset_alloc");
1729 e6bcace5 2021-06-22 stsp
1730 b8af7c06 2022-03-15 stsp *ncolored = 0;
1731 b8af7c06 2022-03-15 stsp *nfound = 0;
1732 b8af7c06 2022-03-15 stsp *ntrees = 0;
1733 b8af7c06 2022-03-15 stsp
1734 b8af7c06 2022-03-15 stsp err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1735 b8af7c06 2022-03-15 stsp repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1736 dc3fe1bf 2022-05-10 stsp if (err)
1737 e6bcace5 2021-06-22 stsp goto done;
1738 0ab4c957 2022-06-13 stsp
1739 0ab4c957 2022-06-13 stsp err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
1740 0ab4c957 2022-06-13 stsp if (err)
1741 0ab4c957 2022-06-13 stsp goto done;
1742 0ab4c957 2022-06-13 stsp if (packidx) {
1743 db9b9b1c 2022-06-14 stsp err = load_packed_object_ids(&found_all_objects,
1744 db9b9b1c 2022-06-14 stsp theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
1745 db9b9b1c 2022-06-14 stsp loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1746 db9b9b1c 2022-06-14 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1747 0ab4c957 2022-06-13 stsp if (err)
1748 0ab4c957 2022-06-13 stsp goto done;
1749 0ab4c957 2022-06-13 stsp }
1750 cee6a7ea 2022-06-07 stsp
1751 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1752 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
1753 05118f5a 2021-06-22 stsp if (id == NULL)
1754 05118f5a 2021-06-22 stsp continue;
1755 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1756 05118f5a 2021-06-22 stsp if (err)
1757 05118f5a 2021-06-22 stsp return err;
1758 9d34261e 2022-04-07 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1759 db9b9b1c 2022-06-14 stsp if (!found_all_objects) {
1760 db9b9b1c 2022-06-14 stsp err = load_commit(0, idset, idset_exclude,
1761 db9b9b1c 2022-06-14 stsp id, repo, seed, loose_obj_only,
1762 db9b9b1c 2022-06-14 stsp ncolored, nfound, ntrees,
1763 db9b9b1c 2022-06-14 stsp progress_cb, progress_arg, rl,
1764 db9b9b1c 2022-06-14 stsp cancel_cb, cancel_arg);
1765 db9b9b1c 2022-06-14 stsp if (err)
1766 db9b9b1c 2022-06-14 stsp goto done;
1767 db9b9b1c 2022-06-14 stsp }
1768 9d34261e 2022-04-07 stsp } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1769 2f8438b0 2022-05-04 stsp err = load_tag(0, idset, idset_exclude, id, repo,
1770 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
1771 9d34261e 2022-04-07 stsp progress_cb, progress_arg, rl,
1772 9d34261e 2022-04-07 stsp cancel_cb, cancel_arg);
1773 9d34261e 2022-04-07 stsp if (err)
1774 9d34261e 2022-04-07 stsp goto done;
1775 9d34261e 2022-04-07 stsp }
1776 05118f5a 2021-06-22 stsp }
1777 05118f5a 2021-06-22 stsp
1778 db9b9b1c 2022-06-14 stsp found_all_objects = 0;
1779 0ab4c957 2022-06-13 stsp err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
1780 0ab4c957 2022-06-13 stsp if (err)
1781 0ab4c957 2022-06-13 stsp goto done;
1782 0ab4c957 2022-06-13 stsp if (packidx) {
1783 db9b9b1c 2022-06-14 stsp err = load_packed_object_ids(&found_all_objects, ids,
1784 db9b9b1c 2022-06-14 stsp nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
1785 db9b9b1c 2022-06-14 stsp loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1786 0ab4c957 2022-06-13 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1787 0ab4c957 2022-06-13 stsp if (err)
1788 0ab4c957 2022-06-13 stsp goto done;
1789 0ab4c957 2022-06-13 stsp }
1790 0ab4c957 2022-06-13 stsp
1791 db9b9b1c 2022-06-14 stsp if (!found_all_objects) {
1792 db9b9b1c 2022-06-14 stsp for (i = 0; i < nobj; i++) {
1793 db9b9b1c 2022-06-14 stsp err = load_commit(1, idset, idset_exclude, ids[i],
1794 db9b9b1c 2022-06-14 stsp repo, seed, loose_obj_only, ncolored, nfound,
1795 db9b9b1c 2022-06-14 stsp ntrees, progress_cb, progress_arg, rl,
1796 db9b9b1c 2022-06-14 stsp cancel_cb, cancel_arg);
1797 db9b9b1c 2022-06-14 stsp if (err)
1798 db9b9b1c 2022-06-14 stsp goto done;
1799 db9b9b1c 2022-06-14 stsp }
1800 eca70f98 2021-09-03 stsp }
1801 eca70f98 2021-09-03 stsp
1802 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1803 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1804 67fd6849 2022-02-13 stsp struct got_pack_meta *m;
1805 05118f5a 2021-06-22 stsp if (id == NULL)
1806 05118f5a 2021-06-22 stsp continue;
1807 67fd6849 2022-02-13 stsp m = got_object_idset_get(idset, id);
1808 67fd6849 2022-02-13 stsp if (m == NULL) {
1809 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1810 07165b17 2021-07-01 stsp if (err)
1811 07165b17 2021-07-01 stsp goto done;
1812 07165b17 2021-07-01 stsp } else
1813 67fd6849 2022-02-13 stsp obj_type = m->obj_type;
1814 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1815 05118f5a 2021-06-22 stsp continue;
1816 2f8438b0 2022-05-04 stsp err = load_tag(1, idset, idset_exclude, id, repo,
1817 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
1818 2f8438b0 2022-05-04 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1819 05118f5a 2021-06-22 stsp if (err)
1820 e6bcace5 2021-06-22 stsp goto done;
1821 e6bcace5 2021-06-22 stsp }
1822 e6bcace5 2021-06-22 stsp done:
1823 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
1824 e6bcace5 2021-06-22 stsp free(ids[i]);
1825 e6bcace5 2021-06-22 stsp }
1826 e6bcace5 2021-06-22 stsp free(ids);
1827 2f8438b0 2022-05-04 stsp got_object_idset_free(idset_exclude);
1828 e6bcace5 2021-06-22 stsp return err;
1829 e6bcace5 2021-06-22 stsp }
1830 e6bcace5 2021-06-22 stsp
1831 336075a4 2022-06-25 op static const struct got_error *
1832 58e31a80 2022-06-27 op hwrite(FILE *f, const void *buf, off_t len, SHA1_CTX *ctx)
1833 e6bcace5 2021-06-22 stsp {
1834 e6bcace5 2021-06-22 stsp size_t n;
1835 e6bcace5 2021-06-22 stsp
1836 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1837 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
1838 e6bcace5 2021-06-22 stsp if (n != len)
1839 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
1840 2d9e6abf 2022-05-04 stsp return NULL;
1841 2d9e6abf 2022-05-04 stsp }
1842 2d9e6abf 2022-05-04 stsp
1843 336075a4 2022-06-25 op static const struct got_error *
1844 2d9e6abf 2022-05-04 stsp hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1845 2d9e6abf 2022-05-04 stsp {
1846 2d9e6abf 2022-05-04 stsp unsigned char buf[65536];
1847 2d9e6abf 2022-05-04 stsp off_t remain = len;
1848 2d9e6abf 2022-05-04 stsp size_t n;
1849 2d9e6abf 2022-05-04 stsp
1850 2d9e6abf 2022-05-04 stsp while (remain > 0) {
1851 2d9e6abf 2022-05-04 stsp size_t copylen = MIN(sizeof(buf), remain);
1852 2d9e6abf 2022-05-04 stsp n = fread(buf, 1, copylen, fsrc);
1853 2d9e6abf 2022-05-04 stsp if (n != copylen)
1854 2d9e6abf 2022-05-04 stsp return got_ferror(fsrc, GOT_ERR_IO);
1855 2d9e6abf 2022-05-04 stsp SHA1Update(ctx, buf, copylen);
1856 2d9e6abf 2022-05-04 stsp n = fwrite(buf, 1, copylen, fdst);
1857 2d9e6abf 2022-05-04 stsp if (n != copylen)
1858 2d9e6abf 2022-05-04 stsp return got_ferror(fdst, GOT_ERR_IO);
1859 2d9e6abf 2022-05-04 stsp remain -= copylen;
1860 2d9e6abf 2022-05-04 stsp }
1861 2d9e6abf 2022-05-04 stsp
1862 e6bcace5 2021-06-22 stsp return NULL;
1863 e6bcace5 2021-06-22 stsp }
1864 e93fb944 2022-05-10 stsp
1865 336075a4 2022-06-25 op static const struct got_error *
1866 e93fb944 2022-05-10 stsp hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1867 e93fb944 2022-05-10 stsp FILE *fdst, off_t len, SHA1_CTX *ctx)
1868 e93fb944 2022-05-10 stsp {
1869 e93fb944 2022-05-10 stsp size_t n;
1870 e6bcace5 2021-06-22 stsp
1871 e93fb944 2022-05-10 stsp if (src_offset + len > src_size)
1872 e93fb944 2022-05-10 stsp return got_error(GOT_ERR_RANGE);
1873 e93fb944 2022-05-10 stsp
1874 e93fb944 2022-05-10 stsp SHA1Update(ctx, src + src_offset, len);
1875 e93fb944 2022-05-10 stsp n = fwrite(src + src_offset, 1, len, fdst);
1876 e93fb944 2022-05-10 stsp if (n != len)
1877 e93fb944 2022-05-10 stsp return got_ferror(fdst, GOT_ERR_IO);
1878 e93fb944 2022-05-10 stsp
1879 e93fb944 2022-05-10 stsp return NULL;
1880 e93fb944 2022-05-10 stsp }
1881 e93fb944 2022-05-10 stsp
1882 e6bcace5 2021-06-22 stsp static void
1883 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1884 e6bcace5 2021-06-22 stsp {
1885 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1886 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1887 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1888 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1889 e6bcace5 2021-06-22 stsp }
1890 e6bcace5 2021-06-22 stsp
1891 e6bcace5 2021-06-22 stsp static int
1892 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1893 e6bcace5 2021-06-22 stsp {
1894 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1895 e6bcace5 2021-06-22 stsp
1896 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1897 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1898 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1899 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1900 611e8e31 2022-05-01 stsp if (bhd->mtime < ahd->mtime)
1901 611e8e31 2022-05-01 stsp return -1;
1902 611e8e31 2022-05-01 stsp if (bhd->mtime > ahd->mtime)
1903 611e8e31 2022-05-01 stsp return 1;
1904 611e8e31 2022-05-01 stsp if (bhd < ahd)
1905 611e8e31 2022-05-01 stsp return -1;
1906 611e8e31 2022-05-01 stsp if (bhd > ahd)
1907 611e8e31 2022-05-01 stsp return 1;
1908 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1909 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1910 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
1911 611e8e31 2022-05-01 stsp return -1;
1912 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
1913 611e8e31 2022-05-01 stsp return 1;
1914 611e8e31 2022-05-01 stsp return got_object_id_cmp(&a->id, &b->id);
1915 e6bcace5 2021-06-22 stsp }
1916 e6bcace5 2021-06-22 stsp
1917 67fd6849 2022-02-13 stsp static int
1918 67fd6849 2022-02-13 stsp reuse_write_order_cmp(const void *pa, const void *pb)
1919 67fd6849 2022-02-13 stsp {
1920 67fd6849 2022-02-13 stsp struct got_pack_meta *a, *b;
1921 67fd6849 2022-02-13 stsp
1922 67fd6849 2022-02-13 stsp a = *(struct got_pack_meta **)pa;
1923 67fd6849 2022-02-13 stsp b = *(struct got_pack_meta **)pb;
1924 67fd6849 2022-02-13 stsp
1925 67fd6849 2022-02-13 stsp if (a->reused_delta_offset < b->reused_delta_offset)
1926 67fd6849 2022-02-13 stsp return -1;
1927 67fd6849 2022-02-13 stsp if (a->reused_delta_offset > b->reused_delta_offset)
1928 67fd6849 2022-02-13 stsp return 1;
1929 67fd6849 2022-02-13 stsp return 0;
1930 67fd6849 2022-02-13 stsp }
1931 67fd6849 2022-02-13 stsp
1932 e6bcace5 2021-06-22 stsp static const struct got_error *
1933 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1934 e6bcace5 2021-06-22 stsp {
1935 e6bcace5 2021-06-22 stsp size_t i;
1936 e6bcace5 2021-06-22 stsp
1937 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1938 e6bcace5 2021-06-22 stsp
1939 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1940 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1941 e6bcace5 2021-06-22 stsp len >>= 4;
1942 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1943 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1944 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1945 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1946 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1947 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1948 e6bcace5 2021-06-22 stsp }
1949 e6bcace5 2021-06-22 stsp
1950 e6bcace5 2021-06-22 stsp *hdrlen = i;
1951 e6bcace5 2021-06-22 stsp return NULL;
1952 e6bcace5 2021-06-22 stsp }
1953 e6bcace5 2021-06-22 stsp
1954 e6bcace5 2021-06-22 stsp static int
1955 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1956 e6bcace5 2021-06-22 stsp {
1957 e6bcace5 2021-06-22 stsp int i, j;
1958 e6bcace5 2021-06-22 stsp char rbuf[8];
1959 e6bcace5 2021-06-22 stsp
1960 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1961 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1962 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1963 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1964 e6bcace5 2021-06-22 stsp }
1965 e6bcace5 2021-06-22 stsp
1966 e6bcace5 2021-06-22 stsp j = 0;
1967 e6bcace5 2021-06-22 stsp while (i > 0)
1968 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1969 e6bcace5 2021-06-22 stsp return j;
1970 e6bcace5 2021-06-22 stsp }
1971 e6bcace5 2021-06-22 stsp
1972 e6bcace5 2021-06-22 stsp static const struct got_error *
1973 5060d5a1 2022-01-10 stsp deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1974 67fd6849 2022-02-13 stsp struct got_pack_meta *m)
1975 5060d5a1 2022-01-10 stsp {
1976 5060d5a1 2022-01-10 stsp const struct got_error *err;
1977 5060d5a1 2022-01-10 stsp char buf[32];
1978 5060d5a1 2022-01-10 stsp int nh;
1979 5060d5a1 2022-01-10 stsp
1980 67fd6849 2022-02-13 stsp if (m->prev->off != 0) {
1981 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1982 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1983 5060d5a1 2022-01-10 stsp if (err)
1984 5060d5a1 2022-01-10 stsp return err;
1985 5060d5a1 2022-01-10 stsp nh += packoff(buf + nh, m->off - m->prev->off);
1986 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1987 5060d5a1 2022-01-10 stsp if (err)
1988 5060d5a1 2022-01-10 stsp return err;
1989 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1990 5060d5a1 2022-01-10 stsp } else {
1991 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1992 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1993 5060d5a1 2022-01-10 stsp if (err)
1994 5060d5a1 2022-01-10 stsp return err;
1995 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1996 5060d5a1 2022-01-10 stsp if (err)
1997 5060d5a1 2022-01-10 stsp return err;
1998 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1999 5060d5a1 2022-01-10 stsp err = hwrite(packfile, m->prev->id.sha1,
2000 5060d5a1 2022-01-10 stsp sizeof(m->prev->id.sha1), ctx);
2001 5060d5a1 2022-01-10 stsp if (err)
2002 5060d5a1 2022-01-10 stsp return err;
2003 5060d5a1 2022-01-10 stsp *packfile_size += sizeof(m->prev->id.sha1);
2004 5060d5a1 2022-01-10 stsp }
2005 5060d5a1 2022-01-10 stsp
2006 5060d5a1 2022-01-10 stsp return NULL;
2007 5060d5a1 2022-01-10 stsp }
2008 5060d5a1 2022-01-10 stsp
2009 5060d5a1 2022-01-10 stsp static const struct got_error *
2010 67fd6849 2022-02-13 stsp write_packed_object(off_t *packfile_size, FILE *packfile,
2011 e93fb944 2022-05-10 stsp FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
2012 e93fb944 2022-05-10 stsp struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
2013 e93fb944 2022-05-10 stsp struct got_repository *repo)
2014 67fd6849 2022-02-13 stsp {
2015 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
2016 67fd6849 2022-02-13 stsp struct got_deflate_checksum csum;
2017 67fd6849 2022-02-13 stsp char buf[32];
2018 67fd6849 2022-02-13 stsp int nh;
2019 67fd6849 2022-02-13 stsp struct got_raw_object *raw = NULL;
2020 67fd6849 2022-02-13 stsp off_t outlen;
2021 67fd6849 2022-02-13 stsp
2022 67fd6849 2022-02-13 stsp csum.output_sha1 = ctx;
2023 67fd6849 2022-02-13 stsp csum.output_crc = NULL;
2024 67fd6849 2022-02-13 stsp
2025 67fd6849 2022-02-13 stsp m->off = ftello(packfile);
2026 67fd6849 2022-02-13 stsp if (m->delta_len == 0) {
2027 67fd6849 2022-02-13 stsp err = got_object_raw_open(&raw, outfd, repo, &m->id);
2028 67fd6849 2022-02-13 stsp if (err)
2029 67fd6849 2022-02-13 stsp goto done;
2030 67fd6849 2022-02-13 stsp err = packhdr(&nh, buf, sizeof(buf),
2031 67fd6849 2022-02-13 stsp m->obj_type, raw->size);
2032 67fd6849 2022-02-13 stsp if (err)
2033 67fd6849 2022-02-13 stsp goto done;
2034 67fd6849 2022-02-13 stsp err = hwrite(packfile, buf, nh, ctx);
2035 67fd6849 2022-02-13 stsp if (err)
2036 67fd6849 2022-02-13 stsp goto done;
2037 67fd6849 2022-02-13 stsp *packfile_size += nh;
2038 67fd6849 2022-02-13 stsp if (raw->f == NULL) {
2039 67fd6849 2022-02-13 stsp err = got_deflate_to_file_mmap(&outlen,
2040 67fd6849 2022-02-13 stsp raw->data + raw->hdrlen, 0, raw->size,
2041 67fd6849 2022-02-13 stsp packfile, &csum);
2042 67fd6849 2022-02-13 stsp if (err)
2043 67fd6849 2022-02-13 stsp goto done;
2044 67fd6849 2022-02-13 stsp } else {
2045 67fd6849 2022-02-13 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
2046 67fd6849 2022-02-13 stsp == -1) {
2047 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
2048 67fd6849 2022-02-13 stsp goto done;
2049 67fd6849 2022-02-13 stsp }
2050 67fd6849 2022-02-13 stsp err = got_deflate_to_file(&outlen, raw->f,
2051 67fd6849 2022-02-13 stsp raw->size, packfile, &csum);
2052 67fd6849 2022-02-13 stsp if (err)
2053 67fd6849 2022-02-13 stsp goto done;
2054 67fd6849 2022-02-13 stsp }
2055 67fd6849 2022-02-13 stsp *packfile_size += outlen;
2056 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
2057 67fd6849 2022-02-13 stsp raw = NULL;
2058 67fd6849 2022-02-13 stsp } else if (m->delta_buf) {
2059 67fd6849 2022-02-13 stsp err = deltahdr(packfile_size, ctx, packfile, m);
2060 67fd6849 2022-02-13 stsp if (err)
2061 67fd6849 2022-02-13 stsp goto done;
2062 2d9e6abf 2022-05-04 stsp err = hwrite(packfile, m->delta_buf,
2063 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
2064 67fd6849 2022-02-13 stsp if (err)
2065 67fd6849 2022-02-13 stsp goto done;
2066 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
2067 67fd6849 2022-02-13 stsp free(m->delta_buf);
2068 67fd6849 2022-02-13 stsp m->delta_buf = NULL;
2069 e93fb944 2022-05-10 stsp } else if (delta_cache_map) {
2070 e93fb944 2022-05-10 stsp err = deltahdr(packfile_size, ctx, packfile, m);
2071 e93fb944 2022-05-10 stsp if (err)
2072 e93fb944 2022-05-10 stsp goto done;
2073 e93fb944 2022-05-10 stsp err = hcopy_mmap(delta_cache_map, m->delta_offset,
2074 e93fb944 2022-05-10 stsp delta_cache_size, packfile, m->delta_compressed_len,
2075 e93fb944 2022-05-10 stsp ctx);
2076 e93fb944 2022-05-10 stsp if (err)
2077 e93fb944 2022-05-10 stsp goto done;
2078 e93fb944 2022-05-10 stsp *packfile_size += m->delta_compressed_len;
2079 67fd6849 2022-02-13 stsp } else {
2080 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
2081 67fd6849 2022-02-13 stsp == -1) {
2082 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
2083 67fd6849 2022-02-13 stsp goto done;
2084 67fd6849 2022-02-13 stsp }
2085 67fd6849 2022-02-13 stsp err = deltahdr(packfile_size, ctx, packfile, m);
2086 67fd6849 2022-02-13 stsp if (err)
2087 67fd6849 2022-02-13 stsp goto done;
2088 2d9e6abf 2022-05-04 stsp err = hcopy(delta_cache, packfile,
2089 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
2090 67fd6849 2022-02-13 stsp if (err)
2091 67fd6849 2022-02-13 stsp goto done;
2092 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
2093 67fd6849 2022-02-13 stsp }
2094 67fd6849 2022-02-13 stsp done:
2095 67fd6849 2022-02-13 stsp if (raw)
2096 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
2097 67fd6849 2022-02-13 stsp return err;
2098 67fd6849 2022-02-13 stsp }
2099 67fd6849 2022-02-13 stsp
2100 67fd6849 2022-02-13 stsp static const struct got_error *
2101 74881701 2021-10-15 stsp genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
2102 67fd6849 2022-02-13 stsp struct got_pack_meta **deltify, int ndeltify,
2103 67fd6849 2022-02-13 stsp struct got_pack_meta **reuse, int nreuse,
2104 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees, int nours,
2105 b8af7c06 2022-03-15 stsp struct got_repository *repo,
2106 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
2107 211cfef0 2022-01-05 stsp struct got_ratelimit *rl,
2108 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2109 e6bcace5 2021-06-22 stsp {
2110 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
2111 67fd6849 2022-02-13 stsp int i;
2112 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
2113 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
2114 08347b73 2021-10-14 stsp char buf[32];
2115 72840534 2022-01-19 stsp size_t n;
2116 67fd6849 2022-02-13 stsp off_t packfile_size = 0;
2117 cc7a354a 2021-10-15 stsp int outfd = -1;
2118 e93fb944 2022-05-10 stsp int delta_cache_fd = -1;
2119 e93fb944 2022-05-10 stsp uint8_t *delta_cache_map = NULL;
2120 e93fb944 2022-05-10 stsp size_t delta_cache_size = 0;
2121 e6bcace5 2021-06-22 stsp
2122 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
2123 e6bcace5 2021-06-22 stsp
2124 e93fb944 2022-05-10 stsp #ifndef GOT_PACK_NO_MMAP
2125 e93fb944 2022-05-10 stsp delta_cache_fd = dup(fileno(delta_cache));
2126 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1) {
2127 e93fb944 2022-05-10 stsp struct stat sb;
2128 e93fb944 2022-05-10 stsp if (fstat(delta_cache_fd, &sb) == -1) {
2129 e93fb944 2022-05-10 stsp err = got_error_from_errno("fstat");
2130 e93fb944 2022-05-10 stsp goto done;
2131 e93fb944 2022-05-10 stsp }
2132 e93fb944 2022-05-10 stsp if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
2133 e93fb944 2022-05-10 stsp delta_cache_map = mmap(NULL, sb.st_size,
2134 e93fb944 2022-05-10 stsp PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
2135 e93fb944 2022-05-10 stsp if (delta_cache_map == MAP_FAILED) {
2136 e93fb944 2022-05-10 stsp if (errno != ENOMEM) {
2137 e93fb944 2022-05-10 stsp err = got_error_from_errno("mmap");
2138 e93fb944 2022-05-10 stsp goto done;
2139 e93fb944 2022-05-10 stsp }
2140 e93fb944 2022-05-10 stsp delta_cache_map = NULL; /* fallback on stdio */
2141 e93fb944 2022-05-10 stsp } else
2142 e93fb944 2022-05-10 stsp delta_cache_size = (size_t)sb.st_size;
2143 e93fb944 2022-05-10 stsp }
2144 e93fb944 2022-05-10 stsp }
2145 e93fb944 2022-05-10 stsp #endif
2146 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
2147 e6bcace5 2021-06-22 stsp if (err)
2148 e93fb944 2022-05-10 stsp goto done;
2149 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
2150 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
2151 e6bcace5 2021-06-22 stsp if (err)
2152 e6bcace5 2021-06-22 stsp goto done;
2153 67fd6849 2022-02-13 stsp putbe32(buf, ndeltify + nreuse);
2154 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
2155 e6bcace5 2021-06-22 stsp if (err)
2156 e6bcace5 2021-06-22 stsp goto done;
2157 67fd6849 2022-02-13 stsp
2158 67fd6849 2022-02-13 stsp qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
2159 67fd6849 2022-02-13 stsp write_order_cmp);
2160 67fd6849 2022-02-13 stsp for (i = 0; i < ndeltify; i++) {
2161 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
2162 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
2163 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, i);
2164 211cfef0 2022-01-05 stsp if (err)
2165 211cfef0 2022-01-05 stsp goto done;
2166 67fd6849 2022-02-13 stsp m = deltify[i];
2167 67fd6849 2022-02-13 stsp err = write_packed_object(&packfile_size, packfile,
2168 e93fb944 2022-05-10 stsp delta_cache, delta_cache_map, delta_cache_size,
2169 e93fb944 2022-05-10 stsp m, &outfd, &ctx, repo);
2170 67fd6849 2022-02-13 stsp if (err)
2171 67fd6849 2022-02-13 stsp goto done;
2172 e6bcace5 2021-06-22 stsp }
2173 67fd6849 2022-02-13 stsp
2174 67fd6849 2022-02-13 stsp qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
2175 67fd6849 2022-02-13 stsp reuse_write_order_cmp);
2176 67fd6849 2022-02-13 stsp for (i = 0; i < nreuse; i++) {
2177 67fd6849 2022-02-13 stsp err = report_progress(progress_cb, progress_arg, rl,
2178 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
2179 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
2180 67fd6849 2022-02-13 stsp if (err)
2181 67fd6849 2022-02-13 stsp goto done;
2182 67fd6849 2022-02-13 stsp m = reuse[i];
2183 67fd6849 2022-02-13 stsp err = write_packed_object(&packfile_size, packfile,
2184 e93fb944 2022-05-10 stsp delta_cache, delta_cache_map, delta_cache_size,
2185 e93fb944 2022-05-10 stsp m, &outfd, &ctx, repo);
2186 67fd6849 2022-02-13 stsp if (err)
2187 67fd6849 2022-02-13 stsp goto done;
2188 67fd6849 2022-02-13 stsp }
2189 67fd6849 2022-02-13 stsp
2190 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
2191 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
2192 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
2193 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
2194 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
2195 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
2196 211cfef0 2022-01-05 stsp if (progress_cb) {
2197 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2198 b8af7c06 2022-03-15 stsp packfile_size, nours, ndeltify + nreuse,
2199 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse);
2200 211cfef0 2022-01-05 stsp if (err)
2201 211cfef0 2022-01-05 stsp goto done;
2202 211cfef0 2022-01-05 stsp }
2203 e6bcace5 2021-06-22 stsp done:
2204 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
2205 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
2206 e93fb944 2022-05-10 stsp if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
2207 e93fb944 2022-05-10 stsp err = got_error_from_errno("munmap");
2208 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2209 e93fb944 2022-05-10 stsp err = got_error_from_errno("close");
2210 e6bcace5 2021-06-22 stsp return err;
2211 67fd6849 2022-02-13 stsp }
2212 67fd6849 2022-02-13 stsp
2213 67fd6849 2022-02-13 stsp static const struct got_error *
2214 67fd6849 2022-02-13 stsp add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
2215 67fd6849 2022-02-13 stsp {
2216 67fd6849 2022-02-13 stsp struct got_pack_meta *m = data;
2217 67fd6849 2022-02-13 stsp struct got_pack_metavec *v = arg;
2218 f5e78e05 2022-05-04 stsp
2219 411cbec1 2022-05-20 stsp if (m->reused_delta_offset != 0)
2220 f5e78e05 2022-05-04 stsp return NULL;
2221 67fd6849 2022-02-13 stsp
2222 67fd6849 2022-02-13 stsp return add_meta(m, v);
2223 67fd6849 2022-02-13 stsp }
2224 67fd6849 2022-02-13 stsp
2225 e6bcace5 2021-06-22 stsp const struct got_error *
2226 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
2227 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
2228 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
2229 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
2230 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
2231 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
2232 e6bcace5 2021-06-22 stsp {
2233 e6bcace5 2021-06-22 stsp const struct got_error *err;
2234 67fd6849 2022-02-13 stsp int delta_cache_fd = -1;
2235 74881701 2021-10-15 stsp FILE *delta_cache = NULL;
2236 67fd6849 2022-02-13 stsp struct got_object_idset *idset;
2237 211cfef0 2022-01-05 stsp struct got_ratelimit rl;
2238 67fd6849 2022-02-13 stsp struct got_pack_metavec deltify, reuse;
2239 b8af7c06 2022-03-15 stsp int ncolored = 0, nfound = 0, ntrees = 0;
2240 f5e78e05 2022-05-04 stsp size_t ndeltify;
2241 d6a28ffe 2022-05-20 op uint32_t seed;
2242 e6bcace5 2021-06-22 stsp
2243 d6a28ffe 2022-05-20 op seed = arc4random();
2244 d6a28ffe 2022-05-20 op
2245 67fd6849 2022-02-13 stsp memset(&deltify, 0, sizeof(deltify));
2246 67fd6849 2022-02-13 stsp memset(&reuse, 0, sizeof(reuse));
2247 67fd6849 2022-02-13 stsp
2248 211cfef0 2022-01-05 stsp got_ratelimit_init(&rl, 0, 500);
2249 211cfef0 2022-01-05 stsp
2250 67fd6849 2022-02-13 stsp idset = got_object_idset_alloc();
2251 67fd6849 2022-02-13 stsp if (idset == NULL)
2252 67fd6849 2022-02-13 stsp return got_error_from_errno("got_object_idset_alloc");
2253 67fd6849 2022-02-13 stsp
2254 b8af7c06 2022-03-15 stsp err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
2255 d6a28ffe 2022-05-20 op ntheirs, ours, nours, repo, seed, loose_obj_only,
2256 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
2257 e6bcace5 2021-06-22 stsp if (err)
2258 17259bfa 2022-05-19 stsp goto done;
2259 e6bcace5 2021-06-22 stsp
2260 28526235 2022-02-13 stsp if (progress_cb) {
2261 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
2262 b8af7c06 2022-03-15 stsp 0L, nours, got_object_idset_num_elements(idset), 0, 0);
2263 28526235 2022-02-13 stsp if (err)
2264 28526235 2022-02-13 stsp goto done;
2265 28526235 2022-02-13 stsp }
2266 28526235 2022-02-13 stsp
2267 67fd6849 2022-02-13 stsp if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
2268 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
2269 05118f5a 2021-06-22 stsp goto done;
2270 05118f5a 2021-06-22 stsp }
2271 74881701 2021-10-15 stsp
2272 67fd6849 2022-02-13 stsp delta_cache_fd = got_opentempfd();
2273 67fd6849 2022-02-13 stsp if (delta_cache_fd == -1) {
2274 74881701 2021-10-15 stsp err = got_error_from_errno("got_opentemp");
2275 74881701 2021-10-15 stsp goto done;
2276 74881701 2021-10-15 stsp }
2277 74881701 2021-10-15 stsp
2278 67fd6849 2022-02-13 stsp reuse.metasz = 64;
2279 67fd6849 2022-02-13 stsp reuse.meta = calloc(reuse.metasz,
2280 67fd6849 2022-02-13 stsp sizeof(struct got_pack_meta *));
2281 67fd6849 2022-02-13 stsp if (reuse.meta == NULL) {
2282 67fd6849 2022-02-13 stsp err = got_error_from_errno("calloc");
2283 67fd6849 2022-02-13 stsp goto done;
2284 67fd6849 2022-02-13 stsp }
2285 67fd6849 2022-02-13 stsp
2286 b8af7c06 2022-03-15 stsp err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
2287 b8af7c06 2022-03-15 stsp ntrees, nours, repo, progress_cb, progress_arg, &rl,
2288 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
2289 67fd6849 2022-02-13 stsp if (err)
2290 67fd6849 2022-02-13 stsp goto done;
2291 67fd6849 2022-02-13 stsp
2292 67fd6849 2022-02-13 stsp delta_cache = fdopen(delta_cache_fd, "a+");
2293 67fd6849 2022-02-13 stsp if (delta_cache == NULL) {
2294 67fd6849 2022-02-13 stsp err = got_error_from_errno("fdopen");
2295 67fd6849 2022-02-13 stsp goto done;
2296 67fd6849 2022-02-13 stsp }
2297 67fd6849 2022-02-13 stsp delta_cache_fd = -1;
2298 67fd6849 2022-02-13 stsp
2299 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
2300 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
2301 67fd6849 2022-02-13 stsp goto done;
2302 67fd6849 2022-02-13 stsp }
2303 67fd6849 2022-02-13 stsp
2304 f5e78e05 2022-05-04 stsp ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
2305 f5e78e05 2022-05-04 stsp if (ndeltify > 0) {
2306 f5e78e05 2022-05-04 stsp deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
2307 f5e78e05 2022-05-04 stsp if (deltify.meta == NULL) {
2308 f5e78e05 2022-05-04 stsp err = got_error_from_errno("calloc");
2309 f5e78e05 2022-05-04 stsp goto done;
2310 f5e78e05 2022-05-04 stsp }
2311 f5e78e05 2022-05-04 stsp deltify.metasz = ndeltify;
2312 67fd6849 2022-02-13 stsp
2313 f5e78e05 2022-05-04 stsp err = got_object_idset_for_each(idset, add_meta_idset_cb,
2314 f5e78e05 2022-05-04 stsp &deltify);
2315 f8a36e22 2021-08-26 stsp if (err)
2316 f8a36e22 2021-08-26 stsp goto done;
2317 f5e78e05 2022-05-04 stsp if (deltify.nmeta > 0) {
2318 f5e78e05 2022-05-04 stsp err = pick_deltas(deltify.meta, deltify.nmeta,
2319 f5e78e05 2022-05-04 stsp ncolored, nfound, ntrees, nours, reuse.nmeta,
2320 f5e78e05 2022-05-04 stsp delta_cache, repo, progress_cb, progress_arg, &rl,
2321 f5e78e05 2022-05-04 stsp cancel_cb, cancel_arg);
2322 f5e78e05 2022-05-04 stsp if (err)
2323 f5e78e05 2022-05-04 stsp goto done;
2324 f5e78e05 2022-05-04 stsp }
2325 f8a36e22 2021-08-26 stsp }
2326 e6bcace5 2021-06-22 stsp
2327 2d9e6abf 2022-05-04 stsp if (fflush(delta_cache) == EOF) {
2328 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fflush");
2329 2d9e6abf 2022-05-04 stsp goto done;
2330 2d9e6abf 2022-05-04 stsp }
2331 67fd6849 2022-02-13 stsp err = genpack(packsha1, packfile, delta_cache, deltify.meta,
2332 b8af7c06 2022-03-15 stsp deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
2333 b8af7c06 2022-03-15 stsp nours, repo, progress_cb, progress_arg, &rl,
2334 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
2335 e6bcace5 2021-06-22 stsp if (err)
2336 e6bcace5 2021-06-22 stsp goto done;
2337 e6bcace5 2021-06-22 stsp done:
2338 67fd6849 2022-02-13 stsp free_nmeta(deltify.meta, deltify.nmeta);
2339 67fd6849 2022-02-13 stsp free_nmeta(reuse.meta, reuse.nmeta);
2340 67fd6849 2022-02-13 stsp got_object_idset_free(idset);
2341 67fd6849 2022-02-13 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2342 67fd6849 2022-02-13 stsp err = got_error_from_errno("close");
2343 74881701 2021-10-15 stsp if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2344 74881701 2021-10-15 stsp err = got_error_from_errno("fclose");
2345 e6bcace5 2021-06-22 stsp return err;
2346 e6bcace5 2021-06-22 stsp }