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