Blame


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