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