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