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