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