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>
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.
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.
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>
25 08736cf9 2021-06-23 stsp #include <stdint.h>
26 05118f5a 2021-06-22 stsp #include <imsg.h>
27 e6bcace5 2021-06-22 stsp #include <stdio.h>
28 e6bcace5 2021-06-22 stsp #include <stdlib.h>
29 e6bcace5 2021-06-22 stsp #include <string.h>
30 e6bcace5 2021-06-22 stsp #include <sha1.h>
31 211cfef0 2022-01-05 stsp #include <time.h>
32 e6bcace5 2021-06-22 stsp #include <limits.h>
33 e6bcace5 2021-06-22 stsp #include <zlib.h>
35 e6bcace5 2021-06-22 stsp #include "got_error.h"
36 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
37 e6bcace5 2021-06-22 stsp #include "got_object.h"
38 f8b19efd 2021-10-13 stsp #include "got_path.h"
39 05118f5a 2021-06-22 stsp #include "got_reference.h"
40 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
41 08347b73 2021-10-14 stsp #include "got_opentemp.h"
43 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
44 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
45 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
46 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
47 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
50 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
52 211cfef0 2022-01-05 stsp #include "got_lib_ratelimit.h"
55 74881701 2021-10-15 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
59 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
62 e6bcace5 2021-06-22 stsp struct got_pack_meta {
63 e6bcace5 2021-06-22 stsp struct got_object_id id;
65 e6bcace5 2021-06-22 stsp int obj_type;
67 e6bcace5 2021-06-22 stsp time_t mtime;
69 e6bcace5 2021-06-22 stsp /* The best delta we picked */
70 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
71 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
72 5060d5a1 2022-01-10 stsp unsigned char *delta_buf; /* if not encoded in delta cache file */
73 74881701 2021-10-15 stsp off_t delta_offset; /* offset in delta cache file */
74 5060d5a1 2022-01-10 stsp off_t delta_len; /* encoded delta length */
77 e6bcace5 2021-06-22 stsp /* Only used for delta window */
78 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
80 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
84 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
85 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
90 e6bcace5 2021-06-22 stsp static const struct got_error *
91 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
92 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
94 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
95 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
97 e6bcace5 2021-06-22 stsp *new = NULL;
99 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
100 e6bcace5 2021-06-22 stsp if (m == NULL)
101 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
103 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
105 e6bcace5 2021-06-22 stsp m->path = strdup(path);
106 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
107 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
109 e6bcace5 2021-06-22 stsp return err;
112 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
113 e6bcace5 2021-06-22 stsp m->mtime = mtime;
115 e6bcace5 2021-06-22 stsp return NULL;
118 e6bcace5 2021-06-22 stsp static void
119 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
121 e6bcace5 2021-06-22 stsp if (meta == NULL)
123 e6bcace5 2021-06-22 stsp free(meta->path);
124 e6bcace5 2021-06-22 stsp meta->path = NULL;
125 5060d5a1 2022-01-10 stsp free(meta->delta_buf);
126 5060d5a1 2022-01-10 stsp meta->delta_buf = NULL;
129 e6bcace5 2021-06-22 stsp static void
130 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
134 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
135 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
136 e6bcace5 2021-06-22 stsp free(meta);
140 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
142 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
145 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
146 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
148 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
149 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
150 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
151 e6bcace5 2021-06-22 stsp if (cmp != 0)
152 e6bcace5 2021-06-22 stsp return cmp;
153 e6bcace5 2021-06-22 stsp if (a->mtime != b->mtime)
154 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
155 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
158 5060d5a1 2022-01-10 stsp static off_t
159 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
162 5060d5a1 2022-01-10 stsp off_t size = 32;
163 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
164 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
165 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
167 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
169 e6bcace5 2021-06-22 stsp return size;
172 5060d5a1 2022-01-10 stsp static const struct got_error *
173 5060d5a1 2022-01-10 stsp append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
177 5060d5a1 2022-01-10 stsp if (*len + nseg >= *sz) {
178 5060d5a1 2022-01-10 stsp while (*len + nseg >= *sz)
179 5060d5a1 2022-01-10 stsp *sz += *sz / 2;
180 5060d5a1 2022-01-10 stsp n = realloc(*p, *sz);
181 5060d5a1 2022-01-10 stsp if (n == NULL)
182 5060d5a1 2022-01-10 stsp return got_error_from_errno("realloc");
185 5060d5a1 2022-01-10 stsp memcpy(*p + *len, seg, nseg);
186 5060d5a1 2022-01-10 stsp *len += nseg;
187 5060d5a1 2022-01-10 stsp return NULL;
190 5060d5a1 2022-01-10 stsp static const struct got_error *
191 5060d5a1 2022-01-10 stsp encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
192 5060d5a1 2022-01-10 stsp struct got_delta_instruction *deltas, int ndeltas,
193 5060d5a1 2022-01-10 stsp off_t delta_size, off_t base_size)
195 5060d5a1 2022-01-10 stsp const struct got_error *err;
196 5060d5a1 2022-01-10 stsp unsigned char buf[16], *bp;
198 5060d5a1 2022-01-10 stsp size_t len = 0;
200 5060d5a1 2022-01-10 stsp struct got_delta_instruction *d;
202 5060d5a1 2022-01-10 stsp m->delta_buf = malloc(delta_size);
203 5060d5a1 2022-01-10 stsp if (m->delta_buf == NULL)
204 5060d5a1 2022-01-10 stsp return got_error_from_errno("calloc");
206 5060d5a1 2022-01-10 stsp /* base object size */
207 5060d5a1 2022-01-10 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
208 5060d5a1 2022-01-10 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
209 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
210 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
211 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
212 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
214 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size, buf, i);
216 5060d5a1 2022-01-10 stsp return err;
218 5060d5a1 2022-01-10 stsp /* target object size */
219 5060d5a1 2022-01-10 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
220 5060d5a1 2022-01-10 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
221 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
222 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
223 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
224 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
226 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size, buf, i);
228 5060d5a1 2022-01-10 stsp return err;
230 5060d5a1 2022-01-10 stsp for (j = 0; j < ndeltas; j++) {
231 5060d5a1 2022-01-10 stsp d = &deltas[j];
232 5060d5a1 2022-01-10 stsp if (d->copy) {
233 5060d5a1 2022-01-10 stsp n = d->offset;
234 5060d5a1 2022-01-10 stsp bp = &buf[1];
235 5060d5a1 2022-01-10 stsp buf[0] = GOT_DELTA_BASE_COPY;
236 5060d5a1 2022-01-10 stsp for (i = 0; i < 4; i++) {
237 5060d5a1 2022-01-10 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
238 5060d5a1 2022-01-10 stsp buf[0] |= 1 << i;
239 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
241 5060d5a1 2022-01-10 stsp if (n == 0)
245 5060d5a1 2022-01-10 stsp n = d->len;
246 5060d5a1 2022-01-10 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
247 5060d5a1 2022-01-10 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
248 5060d5a1 2022-01-10 stsp for (i = 0; i < 3 && n > 0; i++) {
249 5060d5a1 2022-01-10 stsp buf[0] |= 1 << (i + 4);
250 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
254 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
255 5060d5a1 2022-01-10 stsp buf, bp - buf);
257 5060d5a1 2022-01-10 stsp return err;
258 5060d5a1 2022-01-10 stsp } else if (o->f == NULL) {
260 5060d5a1 2022-01-10 stsp while (n != d->len) {
261 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
262 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
265 5060d5a1 2022-01-10 stsp return err;
266 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
267 5060d5a1 2022-01-10 stsp o->data + o->hdrlen + d->offset + n,
270 5060d5a1 2022-01-10 stsp return err;
271 5060d5a1 2022-01-10 stsp n += buf[0];
274 5060d5a1 2022-01-10 stsp char content[128];
276 5060d5a1 2022-01-10 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
277 5060d5a1 2022-01-10 stsp return got_error_from_errno("fseeko");
279 5060d5a1 2022-01-10 stsp while (n != d->len) {
280 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
281 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
284 5060d5a1 2022-01-10 stsp return err;
285 5060d5a1 2022-01-10 stsp r = fread(content, 1, buf[0], o->f);
286 5060d5a1 2022-01-10 stsp if (r != buf[0])
287 5060d5a1 2022-01-10 stsp return got_ferror(o->f, GOT_ERR_IO);
288 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
289 5060d5a1 2022-01-10 stsp content, buf[0]);
291 5060d5a1 2022-01-10 stsp return err;
292 5060d5a1 2022-01-10 stsp n += buf[0];
297 5060d5a1 2022-01-10 stsp m->delta_len = len;
298 5060d5a1 2022-01-10 stsp return NULL;
301 74881701 2021-10-15 stsp static const struct got_error *
302 74881701 2021-10-15 stsp encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
303 74881701 2021-10-15 stsp struct got_delta_instruction *deltas, int ndeltas,
304 a319ca8c 2021-10-15 stsp off_t base_size, FILE *f)
306 a319ca8c 2021-10-15 stsp unsigned char buf[16], *bp;
310 a319ca8c 2021-10-15 stsp struct got_delta_instruction *d;
312 a319ca8c 2021-10-15 stsp /* base object size */
313 a319ca8c 2021-10-15 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
314 a319ca8c 2021-10-15 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
315 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
316 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
317 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
318 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
320 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, i, f);
321 a319ca8c 2021-10-15 stsp if (w != i)
322 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
324 a319ca8c 2021-10-15 stsp /* target object size */
325 a319ca8c 2021-10-15 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
326 a319ca8c 2021-10-15 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
327 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
328 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
329 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
330 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
332 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, i, f);
333 a319ca8c 2021-10-15 stsp if (w != i)
334 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
336 a319ca8c 2021-10-15 stsp for (j = 0; j < ndeltas; j++) {
337 a319ca8c 2021-10-15 stsp d = &deltas[j];
338 a319ca8c 2021-10-15 stsp if (d->copy) {
339 a319ca8c 2021-10-15 stsp n = d->offset;
340 a319ca8c 2021-10-15 stsp bp = &buf[1];
341 a319ca8c 2021-10-15 stsp buf[0] = GOT_DELTA_BASE_COPY;
342 a319ca8c 2021-10-15 stsp for (i = 0; i < 4; i++) {
343 a319ca8c 2021-10-15 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
344 a319ca8c 2021-10-15 stsp buf[0] |= 1 << i;
345 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
347 a319ca8c 2021-10-15 stsp if (n == 0)
351 a319ca8c 2021-10-15 stsp n = d->len;
352 a319ca8c 2021-10-15 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
353 a319ca8c 2021-10-15 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
354 a319ca8c 2021-10-15 stsp for (i = 0; i < 3 && n > 0; i++) {
355 a319ca8c 2021-10-15 stsp buf[0] |= 1 << (i + 4);
356 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
360 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, bp - buf, f);
361 a319ca8c 2021-10-15 stsp if (w != bp - buf)
362 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
363 64a8571e 2022-01-07 stsp } else if (o->f == NULL) {
365 64a8571e 2022-01-07 stsp while (n != d->len) {
366 64a8571e 2022-01-07 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
367 64a8571e 2022-01-07 stsp w = fwrite(buf, 1, 1, f);
368 64a8571e 2022-01-07 stsp if (w != 1)
369 64a8571e 2022-01-07 stsp return got_ferror(f, GOT_ERR_IO);
370 64a8571e 2022-01-07 stsp w = fwrite(o->data + o->hdrlen + d->offset + n,
371 64a8571e 2022-01-07 stsp 1, buf[0], f);
372 64a8571e 2022-01-07 stsp if (w != buf[0])
373 64a8571e 2022-01-07 stsp return got_ferror(f, GOT_ERR_IO);
374 64a8571e 2022-01-07 stsp n += buf[0];
377 a319ca8c 2021-10-15 stsp char content[128];
379 a319ca8c 2021-10-15 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
380 a319ca8c 2021-10-15 stsp return got_error_from_errno("fseeko");
382 a319ca8c 2021-10-15 stsp while (n != d->len) {
383 a319ca8c 2021-10-15 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
384 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, 1, f);
385 a319ca8c 2021-10-15 stsp if (w != 1)
386 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
387 a319ca8c 2021-10-15 stsp r = fread(content, 1, buf[0], o->f);
388 a319ca8c 2021-10-15 stsp if (r != buf[0])
389 a319ca8c 2021-10-15 stsp return got_ferror(o->f, GOT_ERR_IO);
390 a319ca8c 2021-10-15 stsp w = fwrite(content, 1, buf[0], f);
391 a319ca8c 2021-10-15 stsp if (w != buf[0])
392 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
393 a319ca8c 2021-10-15 stsp n += buf[0];
398 5060d5a1 2022-01-10 stsp m->delta_len = ftello(f) - m->delta_offset;
399 a319ca8c 2021-10-15 stsp return NULL;
402 211cfef0 2022-01-05 stsp static const struct got_error *
403 211cfef0 2022-01-05 stsp report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
404 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, off_t packfile_size, int ncommits,
405 211cfef0 2022-01-05 stsp int nobj_total, int obj_deltify, int nobj_written)
407 211cfef0 2022-01-05 stsp const struct got_error *err;
408 211cfef0 2022-01-05 stsp int elapsed;
410 211cfef0 2022-01-05 stsp if (progress_cb == NULL)
411 211cfef0 2022-01-05 stsp return NULL;
413 211cfef0 2022-01-05 stsp err = got_ratelimit_check(&elapsed, rl);
414 211cfef0 2022-01-05 stsp if (err || !elapsed)
415 211cfef0 2022-01-05 stsp return err;
417 211cfef0 2022-01-05 stsp return progress_cb(progress_arg, packfile_size, ncommits,
418 211cfef0 2022-01-05 stsp nobj_total, obj_deltify, nobj_written);
421 e6bcace5 2021-06-22 stsp static const struct got_error *
422 05118f5a 2021-06-22 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
423 74881701 2021-10-15 stsp FILE *delta_cache, struct got_repository *repo,
424 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
425 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
427 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
428 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
429 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
430 74881701 2021-10-15 stsp struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
431 5060d5a1 2022-01-10 stsp int i, j, ndeltas, best_ndeltas;
432 5060d5a1 2022-01-10 stsp off_t size, best_size;
433 4f4d853e 2021-10-24 stsp const int max_base_candidates = 3;
434 cc7a354a 2021-10-15 stsp int outfd = -1;
436 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
437 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
438 e6bcace5 2021-06-22 stsp if (cancel_cb) {
439 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
443 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
444 211cfef0 2022-01-05 stsp 0L, nours, nmeta, i, 0);
447 e6bcace5 2021-06-22 stsp m = meta[i];
449 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
450 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
453 94dac27c 2021-10-15 stsp err = got_object_raw_open(&raw, &outfd, repo, &m->id);
456 600b755e 2021-10-14 stsp m->size = raw->size;
458 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
459 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&m->dtab, raw->data,
460 64a8571e 2022-01-07 stsp raw->hdrlen, raw->size + raw->hdrlen);
462 64a8571e 2022-01-07 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
463 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen);
468 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
469 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
470 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
471 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
472 e6bcace5 2021-06-22 stsp n->dtab = NULL;
475 74881701 2021-10-15 stsp best_size = raw->size;
476 74881701 2021-10-15 stsp best_ndeltas = 0;
477 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
478 e6bcace5 2021-06-22 stsp if (cancel_cb) {
479 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
483 e6bcace5 2021-06-22 stsp base = meta[j];
484 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
485 22edbce7 2021-10-24 stsp if (base->nchain >= 128 ||
486 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
489 d3c116bf 2021-10-15 stsp err = got_object_raw_open(&base_raw, &outfd, repo,
490 94dac27c 2021-10-15 stsp &base->id);
493 64a8571e 2022-01-07 stsp if (raw->f == NULL && base_raw->f == NULL) {
494 64a8571e 2022-01-07 stsp err = got_deltify_mem_mem(&deltas, &ndeltas,
495 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
496 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
497 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
498 64a8571e 2022-01-07 stsp base_raw->hdrlen,
499 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
500 64a8571e 2022-01-07 stsp } else if (raw->f == NULL) {
501 64a8571e 2022-01-07 stsp err = got_deltify_mem_file(&deltas, &ndeltas,
502 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
503 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
504 64a8571e 2022-01-07 stsp base->dtab, base_raw->f,
505 64a8571e 2022-01-07 stsp base_raw->hdrlen,
506 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
507 64a8571e 2022-01-07 stsp } else if (base_raw->f == NULL) {
508 64a8571e 2022-01-07 stsp err = got_deltify_file_mem(&deltas, &ndeltas,
509 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
510 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
511 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
512 64a8571e 2022-01-07 stsp base_raw->hdrlen,
513 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
515 64a8571e 2022-01-07 stsp err = got_deltify(&deltas, &ndeltas,
516 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
517 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
518 64a8571e 2022-01-07 stsp base->dtab, base_raw->f, base_raw->hdrlen,
519 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
521 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
522 e6bcace5 2021-06-22 stsp base_raw = NULL;
526 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
527 74881701 2021-10-15 stsp if (size + 32 < best_size){
529 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
530 e6bcace5 2021-06-22 stsp * replace it.
532 74881701 2021-10-15 stsp best_size = size;
533 74881701 2021-10-15 stsp free(best_deltas);
534 74881701 2021-10-15 stsp best_deltas = deltas;
535 74881701 2021-10-15 stsp best_ndeltas = ndeltas;
536 74881701 2021-10-15 stsp deltas = NULL;
537 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
538 e6bcace5 2021-06-22 stsp m->prev = base;
539 e6bcace5 2021-06-22 stsp m->head = base->head;
540 e6bcace5 2021-06-22 stsp if (m->head == NULL)
541 e6bcace5 2021-06-22 stsp m->head = base;
543 e6bcace5 2021-06-22 stsp free(deltas);
544 e6bcace5 2021-06-22 stsp deltas = NULL;
545 e6bcace5 2021-06-22 stsp ndeltas = 0;
549 74881701 2021-10-15 stsp if (best_ndeltas > 0) {
550 5060d5a1 2022-01-10 stsp if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX) {
551 5060d5a1 2022-01-10 stsp err = encode_delta_in_mem(m, raw, best_deltas,
552 5060d5a1 2022-01-10 stsp best_ndeltas, best_size, m->prev->size);
554 5060d5a1 2022-01-10 stsp m->delta_offset = ftello(delta_cache);
555 5060d5a1 2022-01-10 stsp err = encode_delta(m, raw, best_deltas,
556 5060d5a1 2022-01-10 stsp best_ndeltas, m->prev->size, delta_cache);
558 74881701 2021-10-15 stsp free(best_deltas);
559 74881701 2021-10-15 stsp best_deltas = NULL;
560 74881701 2021-10-15 stsp best_ndeltas = 0;
565 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
566 e6bcace5 2021-06-22 stsp raw = NULL;
569 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
570 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
571 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
574 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
575 e6bcace5 2021-06-22 stsp if (base_raw)
576 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
577 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
578 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
579 74881701 2021-10-15 stsp free(deltas);
580 74881701 2021-10-15 stsp free(best_deltas);
581 e6bcace5 2021-06-22 stsp return err;
584 e6bcace5 2021-06-22 stsp static const struct got_error *
585 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
586 05118f5a 2021-06-22 stsp struct got_repository *repo)
588 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
589 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
592 05118f5a 2021-06-22 stsp *found = 0;
594 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
595 05118f5a 2021-06-22 stsp if (err == NULL)
596 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
597 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
598 05118f5a 2021-06-22 stsp err = NULL;
599 05118f5a 2021-06-22 stsp return err;
602 07165b17 2021-07-01 stsp static const int obj_types[] = {
603 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_ANY,
604 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_COMMIT,
605 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TREE,
606 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_BLOB,
607 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TAG,
608 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_OFFSET_DELTA,
609 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_REF_DELTA
612 05118f5a 2021-06-22 stsp static const struct got_error *
613 e6bcace5 2021-06-22 stsp add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
614 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
615 05118f5a 2021-06-22 stsp time_t mtime, int loose_obj_only, struct got_repository *repo)
617 e6bcace5 2021-06-22 stsp const struct got_error *err;
618 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
620 05118f5a 2021-06-22 stsp if (loose_obj_only) {
621 05118f5a 2021-06-22 stsp int is_packed;
622 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
624 05118f5a 2021-06-22 stsp return err;
625 05118f5a 2021-06-22 stsp if (is_packed)
626 05118f5a 2021-06-22 stsp return NULL;
629 07165b17 2021-07-01 stsp err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
631 e6bcace5 2021-06-22 stsp return err;
633 e6bcace5 2021-06-22 stsp if (v == NULL)
634 e6bcace5 2021-06-22 stsp return NULL;
636 e6bcace5 2021-06-22 stsp err = alloc_meta(&m, id, path, obj_type, mtime);
640 e6bcace5 2021-06-22 stsp if (v->nmeta == v->metasz){
641 e6bcace5 2021-06-22 stsp size_t newsize = 2 * v->metasz;
642 e6bcace5 2021-06-22 stsp struct got_pack_meta **new;
643 e6bcace5 2021-06-22 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
644 e6bcace5 2021-06-22 stsp if (new == NULL) {
645 e6bcace5 2021-06-22 stsp err = got_error_from_errno("reallocarray");
648 e6bcace5 2021-06-22 stsp v->meta = new;
649 e6bcace5 2021-06-22 stsp v->metasz = newsize;
653 e6bcace5 2021-06-22 stsp clear_meta(m);
656 e6bcace5 2021-06-22 stsp v->meta[v->nmeta++] = m;
658 e6bcace5 2021-06-22 stsp return err;
661 e6bcace5 2021-06-22 stsp static const struct got_error *
662 e6bcace5 2021-06-22 stsp load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
663 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
664 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
665 05118f5a 2021-06-22 stsp int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
667 e6bcace5 2021-06-22 stsp const struct got_error *err;
668 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
669 e6bcace5 2021-06-22 stsp char *p = NULL;
672 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
674 e6bcace5 2021-06-22 stsp return err;
676 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
677 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
678 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
679 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
681 e6bcace5 2021-06-22 stsp if (cancel_cb) {
682 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
687 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
688 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
691 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
692 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
693 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
697 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
698 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
699 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
702 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
703 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
704 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
705 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
713 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
715 e6bcace5 2021-06-22 stsp return err;
718 e6bcace5 2021-06-22 stsp static const struct got_error *
719 e6bcace5 2021-06-22 stsp load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
720 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
721 05118f5a 2021-06-22 stsp int loose_obj_only, struct got_repository *repo,
722 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
724 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
725 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
726 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
728 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
729 e6bcace5 2021-06-22 stsp return NULL;
731 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
733 e6bcace5 2021-06-22 stsp return err;
735 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
736 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
738 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
739 e6bcace5 2021-06-22 stsp if (cancel_cb) {
740 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
745 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
746 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
748 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
749 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
753 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
754 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
756 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
760 e6bcace5 2021-06-22 stsp err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
761 05118f5a 2021-06-22 stsp mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
762 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
767 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
768 e6bcace5 2021-06-22 stsp return err;
771 e6bcace5 2021-06-22 stsp static const struct got_error *
772 e6bcace5 2021-06-22 stsp load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
773 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
774 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
776 e6bcace5 2021-06-22 stsp const struct got_error *err;
777 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
779 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
780 e6bcace5 2021-06-22 stsp return NULL;
782 05118f5a 2021-06-22 stsp if (loose_obj_only) {
783 05118f5a 2021-06-22 stsp int is_packed;
784 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
786 05118f5a 2021-06-22 stsp return err;
787 05118f5a 2021-06-22 stsp if (is_packed)
788 05118f5a 2021-06-22 stsp return NULL;
791 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
793 e6bcace5 2021-06-22 stsp return err;
795 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
796 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
797 05118f5a 2021-06-22 stsp loose_obj_only, repo);
801 e6bcace5 2021-06-22 stsp err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
802 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
803 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
805 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
806 e6bcace5 2021-06-22 stsp return err;
809 05118f5a 2021-06-22 stsp static const struct got_error *
810 05118f5a 2021-06-22 stsp load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
811 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
812 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
814 05118f5a 2021-06-22 stsp const struct got_error *err;
815 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
817 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
818 05118f5a 2021-06-22 stsp return NULL;
820 05118f5a 2021-06-22 stsp if (loose_obj_only) {
821 05118f5a 2021-06-22 stsp int is_packed;
822 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
824 05118f5a 2021-06-22 stsp return err;
825 05118f5a 2021-06-22 stsp if (is_packed)
826 05118f5a 2021-06-22 stsp return NULL;
829 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
831 05118f5a 2021-06-22 stsp return err;
833 05118f5a 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
834 05118f5a 2021-06-22 stsp got_object_tag_get_tagger_time(tag),
835 05118f5a 2021-06-22 stsp loose_obj_only, repo);
839 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
840 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
841 26960ff7 2021-09-14 stsp err = load_commit(v, idset,
842 05118f5a 2021-06-22 stsp got_object_tag_get_object_id(tag), repo,
843 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
845 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
846 05118f5a 2021-06-22 stsp err = load_tree(v, idset, got_object_tag_get_object_id(tag),
847 05118f5a 2021-06-22 stsp "", got_object_tag_get_tagger_time(tag),
848 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
855 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
856 05118f5a 2021-06-22 stsp return err;
859 e6bcace5 2021-06-22 stsp enum findtwixt_color {
860 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
861 e6bcace5 2021-06-22 stsp COLOR_DROP,
862 e6bcace5 2021-06-22 stsp COLOR_BLANK,
864 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
865 e6bcace5 2021-06-22 stsp COLOR_KEEP,
866 e6bcace5 2021-06-22 stsp COLOR_DROP,
867 e6bcace5 2021-06-22 stsp COLOR_BLANK
870 e6bcace5 2021-06-22 stsp static const struct got_error *
871 e6bcace5 2021-06-22 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
872 e6bcace5 2021-06-22 stsp int color, struct got_repository *repo)
874 e6bcace5 2021-06-22 stsp const struct got_error *err;
875 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
877 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
879 e6bcace5 2021-06-22 stsp return err;
881 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
882 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
883 e6bcace5 2021-06-22 stsp return NULL;
886 e6bcace5 2021-06-22 stsp static const struct got_error *
887 e6bcace5 2021-06-22 stsp drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
888 e6bcace5 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo,
889 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
891 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
892 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
893 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
894 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
895 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
897 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
899 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
901 e6bcace5 2021-06-22 stsp return err;
902 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&ids, qid, entry);
904 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
905 e6bcace5 2021-06-22 stsp if (cancel_cb) {
906 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
911 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
912 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
914 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id)) {
915 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
919 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, qid->id,
920 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
922 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
926 e6bcace5 2021-06-22 stsp if (!got_object_idset_contains(keep, qid->id)) {
927 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
931 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
932 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
936 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
937 e6bcace5 2021-06-22 stsp if (parents) {
938 e6bcace5 2021-06-22 stsp err = got_object_id_queue_copy(parents, &ids);
940 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
944 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
947 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
948 e6bcace5 2021-06-22 stsp return err;
951 e6bcace5 2021-06-22 stsp struct append_id_arg {
952 e6bcace5 2021-06-22 stsp struct got_object_id **array;
956 e6bcace5 2021-06-22 stsp static const struct got_error *
957 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
959 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
961 e6bcace5 2021-06-22 stsp a->array[a->idx] = got_object_id_dup(id);
962 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
963 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
966 e6bcace5 2021-06-22 stsp return NULL;
969 e6bcace5 2021-06-22 stsp static const struct got_error *
970 e6bcace5 2021-06-22 stsp findtwixt(struct got_object_id ***res, int *nres,
971 e6bcace5 2021-06-22 stsp struct got_object_id **head, int nhead,
972 e6bcace5 2021-06-22 stsp struct got_object_id **tail, int ntail,
973 e6bcace5 2021-06-22 stsp struct got_repository *repo,
974 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
976 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
977 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
978 e6bcace5 2021-06-22 stsp struct got_object_idset *keep, *drop;
979 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
980 05118f5a 2021-06-22 stsp int i, ncolor, nkeep, obj_type;
982 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
983 e6bcace5 2021-06-22 stsp *res = NULL;
986 e6bcace5 2021-06-22 stsp keep = got_object_idset_alloc();
987 e6bcace5 2021-06-22 stsp if (keep == NULL)
988 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
990 e6bcace5 2021-06-22 stsp drop = got_object_idset_alloc();
991 e6bcace5 2021-06-22 stsp if (drop == NULL) {
992 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_idset_alloc");
996 05118f5a 2021-06-22 stsp for (i = 0; i < nhead; i++) {
997 05118f5a 2021-06-22 stsp struct got_object_id *id = head[i];
998 05118f5a 2021-06-22 stsp if (id == NULL)
1000 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1002 05118f5a 2021-06-22 stsp return err;
1003 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
1005 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
1007 05118f5a 2021-06-22 stsp goto done;
1009 05118f5a 2021-06-22 stsp for (i = 0; i < ntail; i++) {
1010 05118f5a 2021-06-22 stsp struct got_object_id *id = tail[i];
1011 05118f5a 2021-06-22 stsp if (id == NULL)
1013 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1015 05118f5a 2021-06-22 stsp return err;
1016 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
1018 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_DROP, repo);
1020 05118f5a 2021-06-22 stsp goto done;
1023 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
1024 e6bcace5 2021-06-22 stsp int qcolor;
1025 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
1026 e6bcace5 2021-06-22 stsp qcolor = *((int *)qid->data);
1028 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id))
1029 e6bcace5 2021-06-22 stsp ncolor = COLOR_DROP;
1030 e6bcace5 2021-06-22 stsp else if (got_object_idset_contains(keep, qid->id))
1031 e6bcace5 2021-06-22 stsp ncolor = COLOR_KEEP;
1033 e6bcace5 2021-06-22 stsp ncolor = COLOR_BLANK;
1035 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
1036 e6bcace5 2021-06-22 stsp qcolor == COLOR_KEEP)) {
1037 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
1038 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1042 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
1043 e6bcace5 2021-06-22 stsp err = drop_commit(keep, drop, qid->id, repo,
1044 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
1046 e6bcace5 2021-06-22 stsp goto done;
1047 e6bcace5 2021-06-22 stsp } else if (ncolor == COLOR_BLANK) {
1048 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1049 e6bcace5 2021-06-22 stsp struct got_object_id *id;
1050 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
1051 e6bcace5 2021-06-22 stsp struct got_object_qid *pid;
1053 e6bcace5 2021-06-22 stsp id = got_object_id_dup(qid->id);
1054 e6bcace5 2021-06-22 stsp if (id == NULL) {
1055 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
1056 e6bcace5 2021-06-22 stsp goto done;
1058 e6bcace5 2021-06-22 stsp if (qcolor == COLOR_KEEP)
1059 07165b17 2021-07-01 stsp err = got_object_idset_add(keep, id,
1060 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1062 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, id,
1063 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
1064 e6bcace5 2021-06-22 stsp if (err) {
1066 e6bcace5 2021-06-22 stsp goto done;
1069 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1070 e6bcace5 2021-06-22 stsp if (err) {
1072 e6bcace5 2021-06-22 stsp goto done;
1074 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
1075 e6bcace5 2021-06-22 stsp if (parents) {
1076 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parents, entry) {
1077 e6bcace5 2021-06-22 stsp err = queue_commit_id(&ids, pid->id,
1078 e6bcace5 2021-06-22 stsp qcolor, repo);
1079 e6bcace5 2021-06-22 stsp if (err) {
1081 e6bcace5 2021-06-22 stsp goto done;
1085 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1086 e6bcace5 2021-06-22 stsp commit = NULL;
1088 e6bcace5 2021-06-22 stsp /* should not happen */
1089 e6bcace5 2021-06-22 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
1090 e6bcace5 2021-06-22 stsp "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
1091 e6bcace5 2021-06-22 stsp goto done;
1094 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
1095 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1098 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1099 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1100 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1101 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1102 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1103 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1104 e6bcace5 2021-06-22 stsp goto done;
1106 e6bcace5 2021-06-22 stsp arg.idx = 0;
1107 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1108 e6bcace5 2021-06-22 stsp if (err) {
1109 e6bcace5 2021-06-22 stsp free(arg.array);
1110 e6bcace5 2021-06-22 stsp goto done;
1112 e6bcace5 2021-06-22 stsp *res = arg.array;
1113 e6bcace5 2021-06-22 stsp *nres = nkeep;
1116 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1117 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1118 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1119 e6bcace5 2021-06-22 stsp return err;
1122 e6bcace5 2021-06-22 stsp static const struct got_error *
1123 e6bcace5 2021-06-22 stsp read_meta(struct got_pack_meta ***meta, int *nmeta,
1124 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1125 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1126 05118f5a 2021-06-22 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
1127 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1129 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1130 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1131 e6bcace5 2021-06-22 stsp struct got_object_idset *idset;
1132 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
1133 e6bcace5 2021-06-22 stsp struct got_pack_metavec v;
1135 e6bcace5 2021-06-22 stsp *meta = NULL;
1136 e6bcace5 2021-06-22 stsp *nmeta = 0;
1138 e6bcace5 2021-06-22 stsp idset = got_object_idset_alloc();
1139 e6bcace5 2021-06-22 stsp if (idset == NULL)
1140 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
1142 e6bcace5 2021-06-22 stsp v.nmeta = 0;
1143 e6bcace5 2021-06-22 stsp v.metasz = 64;
1144 e6bcace5 2021-06-22 stsp v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
1145 e6bcace5 2021-06-22 stsp if (v.meta == NULL) {
1146 1d19226a 2021-10-13 stsp err = got_error_from_errno("calloc");
1147 e6bcace5 2021-06-22 stsp goto done;
1150 e6bcace5 2021-06-22 stsp err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
1151 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
1152 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
1153 e6bcace5 2021-06-22 stsp goto done;
1155 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1156 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
1157 05118f5a 2021-06-22 stsp if (id == NULL)
1159 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1161 05118f5a 2021-06-22 stsp return err;
1162 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
1164 05118f5a 2021-06-22 stsp err = load_commit(NULL, idset, id, repo,
1165 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
1167 05118f5a 2021-06-22 stsp goto done;
1168 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1169 211cfef0 2022-01-05 stsp 0L, nours, v.nmeta, 0, 0);
1171 211cfef0 2022-01-05 stsp goto done;
1174 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1175 f4a2ff2d 2021-07-01 stsp struct got_object_id *id = theirs[i];
1176 07165b17 2021-07-01 stsp int *cached_type;
1177 05118f5a 2021-06-22 stsp if (id == NULL)
1179 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
1180 07165b17 2021-07-01 stsp if (cached_type == NULL) {
1181 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1183 07165b17 2021-07-01 stsp goto done;
1185 07165b17 2021-07-01 stsp obj_type = *cached_type;
1186 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1188 05118f5a 2021-06-22 stsp err = load_tag(NULL, idset, id, repo,
1189 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
1191 05118f5a 2021-06-22 stsp goto done;
1192 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1193 211cfef0 2022-01-05 stsp 0L, nours, v.nmeta, 0, 0);
1195 211cfef0 2022-01-05 stsp goto done;
1198 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
1199 eca70f98 2021-09-03 stsp err = load_commit(&v, idset, ids[i], repo,
1200 eca70f98 2021-09-03 stsp loose_obj_only, cancel_cb, cancel_arg);
1202 eca70f98 2021-09-03 stsp goto done;
1204 211cfef0 2022-01-05 stsp goto done;
1205 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1206 211cfef0 2022-01-05 stsp 0L, nours, v.nmeta, 0, 0);
1208 211cfef0 2022-01-05 stsp goto done;
1211 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1212 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1213 07165b17 2021-07-01 stsp int *cached_type;
1214 05118f5a 2021-06-22 stsp if (id == NULL)
1216 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
1217 07165b17 2021-07-01 stsp if (cached_type == NULL) {
1218 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1220 07165b17 2021-07-01 stsp goto done;
1222 07165b17 2021-07-01 stsp obj_type = *cached_type;
1223 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1225 05118f5a 2021-06-22 stsp err = load_tag(&v, idset, id, repo,
1226 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
1228 e6bcace5 2021-06-22 stsp goto done;
1229 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1230 211cfef0 2022-01-05 stsp 0L, nours, v.nmeta, 0, 0);
1232 211cfef0 2022-01-05 stsp goto done;
1235 211cfef0 2022-01-05 stsp if (progress_cb) {
1236 211cfef0 2022-01-05 stsp err = progress_cb(progress_arg, 0L, nours, v.nmeta, 0, 0);
1238 211cfef0 2022-01-05 stsp goto done;
1241 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
1242 e6bcace5 2021-06-22 stsp free(ids[i]);
1244 e6bcace5 2021-06-22 stsp free(ids);
1245 e6bcace5 2021-06-22 stsp got_object_idset_free(idset);
1246 e6bcace5 2021-06-22 stsp if (err == NULL) {
1247 e6bcace5 2021-06-22 stsp *meta = v.meta;
1248 e6bcace5 2021-06-22 stsp *nmeta = v.nmeta;
1250 e6bcace5 2021-06-22 stsp free(v.meta);
1252 e6bcace5 2021-06-22 stsp return err;
1255 e6bcace5 2021-06-22 stsp const struct got_error *
1256 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1260 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1261 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
1262 e6bcace5 2021-06-22 stsp if (n != len)
1263 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
1264 e6bcace5 2021-06-22 stsp return NULL;
1267 e6bcace5 2021-06-22 stsp static void
1268 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1270 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1271 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1272 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1273 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1276 e6bcace5 2021-06-22 stsp static int
1277 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1279 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1281 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1282 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1283 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1284 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1285 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
1286 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
1287 e6bcace5 2021-06-22 stsp if (ahd != bhd)
1288 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
1289 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1290 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1291 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
1294 e6bcace5 2021-06-22 stsp static const struct got_error *
1295 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1299 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1301 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1302 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1303 e6bcace5 2021-06-22 stsp len >>= 4;
1304 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1305 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1306 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1307 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1308 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1309 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1312 e6bcace5 2021-06-22 stsp *hdrlen = i;
1313 e6bcace5 2021-06-22 stsp return NULL;
1316 e6bcace5 2021-06-22 stsp static int
1317 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1320 e6bcace5 2021-06-22 stsp char rbuf[8];
1322 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1323 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1324 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1325 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1329 e6bcace5 2021-06-22 stsp while (i > 0)
1330 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1334 e6bcace5 2021-06-22 stsp static const struct got_error *
1335 5060d5a1 2022-01-10 stsp deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1336 5060d5a1 2022-01-10 stsp struct got_pack_meta *m, int use_offset_deltas)
1338 5060d5a1 2022-01-10 stsp const struct got_error *err;
1339 5060d5a1 2022-01-10 stsp char buf[32];
1342 5060d5a1 2022-01-10 stsp if (use_offset_deltas && m->prev->off != 0) {
1343 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1344 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1346 5060d5a1 2022-01-10 stsp return err;
1347 5060d5a1 2022-01-10 stsp nh += packoff(buf + nh, m->off - m->prev->off);
1348 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1350 5060d5a1 2022-01-10 stsp return err;
1351 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1353 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1354 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1356 5060d5a1 2022-01-10 stsp return err;
1357 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1359 5060d5a1 2022-01-10 stsp return err;
1360 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1361 5060d5a1 2022-01-10 stsp err = hwrite(packfile, m->prev->id.sha1,
1362 5060d5a1 2022-01-10 stsp sizeof(m->prev->id.sha1), ctx);
1364 5060d5a1 2022-01-10 stsp return err;
1365 5060d5a1 2022-01-10 stsp *packfile_size += sizeof(m->prev->id.sha1);
1368 5060d5a1 2022-01-10 stsp return NULL;
1371 5060d5a1 2022-01-10 stsp static const struct got_error *
1372 74881701 2021-10-15 stsp genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1373 05118f5a 2021-06-22 stsp struct got_pack_meta **meta, int nmeta, int nours,
1374 05118f5a 2021-06-22 stsp int use_offset_deltas, struct got_repository *repo,
1375 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1376 211cfef0 2022-01-05 stsp struct got_ratelimit *rl,
1377 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1379 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1380 08347b73 2021-10-14 stsp int i, nh;
1381 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1382 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1383 600b755e 2021-10-14 stsp struct got_raw_object *raw = NULL;
1384 08347b73 2021-10-14 stsp FILE *delta_file = NULL;
1385 08347b73 2021-10-14 stsp char buf[32];
1386 e6bcace5 2021-06-22 stsp size_t outlen, n;
1387 e6bcace5 2021-06-22 stsp struct got_deflate_checksum csum;
1388 05118f5a 2021-06-22 stsp off_t packfile_size = 0;
1389 cc7a354a 2021-10-15 stsp int outfd = -1;
1391 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1392 e6bcace5 2021-06-22 stsp csum.output_sha1 = &ctx;
1393 e6bcace5 2021-06-22 stsp csum.output_crc = NULL;
1395 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1397 e6bcace5 2021-06-22 stsp return err;
1398 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1399 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1401 e6bcace5 2021-06-22 stsp goto done;
1402 e6bcace5 2021-06-22 stsp putbe32(buf, nmeta);
1403 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1405 e6bcace5 2021-06-22 stsp goto done;
1406 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1407 05118f5a 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
1408 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1409 211cfef0 2022-01-05 stsp packfile_size, nours, nmeta, nmeta, i);
1411 211cfef0 2022-01-05 stsp goto done;
1412 e6bcace5 2021-06-22 stsp m = meta[i];
1413 e6bcace5 2021-06-22 stsp m->off = ftello(packfile);
1414 74881701 2021-10-15 stsp if (m->delta_len == 0) {
1415 59b21794 2022-01-07 stsp err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1417 59b21794 2022-01-07 stsp goto done;
1418 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1419 e6bcace5 2021-06-22 stsp m->obj_type, raw->size);
1421 e6bcace5 2021-06-22 stsp goto done;
1422 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1424 e6bcace5 2021-06-22 stsp goto done;
1425 05118f5a 2021-06-22 stsp packfile_size += nh;
1426 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
1427 64a8571e 2022-01-07 stsp err = got_deflate_to_file_mmap(&outlen,
1428 64a8571e 2022-01-07 stsp raw->data + raw->hdrlen, 0, raw->size,
1429 64a8571e 2022-01-07 stsp packfile, &csum);
1431 64a8571e 2022-01-07 stsp goto done;
1433 64a8571e 2022-01-07 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1435 64a8571e 2022-01-07 stsp err = got_error_from_errno("fseeko");
1436 64a8571e 2022-01-07 stsp goto done;
1438 64a8571e 2022-01-07 stsp err = got_deflate_to_file(&outlen, raw->f,
1439 64a8571e 2022-01-07 stsp packfile, &csum);
1441 64a8571e 2022-01-07 stsp goto done;
1443 05118f5a 2021-06-22 stsp packfile_size += outlen;
1444 59b21794 2022-01-07 stsp got_object_raw_close(raw);
1445 59b21794 2022-01-07 stsp raw = NULL;
1446 5060d5a1 2022-01-10 stsp } else if (m->delta_buf) {
1447 5060d5a1 2022-01-10 stsp err = deltahdr(&packfile_size, &ctx, packfile,
1448 5060d5a1 2022-01-10 stsp m, use_offset_deltas);
1450 5060d5a1 2022-01-10 stsp goto done;
1451 5060d5a1 2022-01-10 stsp err = got_deflate_to_file_mmap(&outlen,
1452 5060d5a1 2022-01-10 stsp m->delta_buf, 0, m->delta_len, packfile, &csum);
1454 5060d5a1 2022-01-10 stsp goto done;
1455 5060d5a1 2022-01-10 stsp packfile_size += outlen;
1456 5060d5a1 2022-01-10 stsp free(m->delta_buf);
1457 5060d5a1 2022-01-10 stsp m->delta_buf = NULL;
1459 74881701 2021-10-15 stsp off_t remain;
1460 08347b73 2021-10-14 stsp if (delta_file == NULL) {
1461 08347b73 2021-10-14 stsp delta_file = got_opentemp();
1462 08347b73 2021-10-14 stsp if (delta_file == NULL) {
1463 08347b73 2021-10-14 stsp err = got_error_from_errno(
1464 08347b73 2021-10-14 stsp "got_opentemp");
1465 08347b73 2021-10-14 stsp goto done;
1468 08347b73 2021-10-14 stsp if (ftruncate(fileno(delta_file), 0L) == -1) {
1469 08347b73 2021-10-14 stsp err = got_error_from_errno("ftruncate");
1470 08347b73 2021-10-14 stsp goto done;
1472 08347b73 2021-10-14 stsp if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1473 08347b73 2021-10-14 stsp err = got_error_from_errno("fseeko");
1474 08347b73 2021-10-14 stsp goto done;
1476 74881701 2021-10-15 stsp if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1478 08347b73 2021-10-14 stsp err = got_error_from_errno("fseeko");
1479 08347b73 2021-10-14 stsp goto done;
1481 74881701 2021-10-15 stsp remain = m->delta_len;
1482 74881701 2021-10-15 stsp while (remain > 0) {
1483 74881701 2021-10-15 stsp char delta_buf[8192];
1484 74881701 2021-10-15 stsp size_t r, w, n;
1485 74881701 2021-10-15 stsp n = MIN(remain, sizeof(delta_buf));
1486 74881701 2021-10-15 stsp r = fread(delta_buf, 1, n, delta_cache);
1487 74881701 2021-10-15 stsp if (r != n) {
1488 74881701 2021-10-15 stsp err = got_ferror(delta_cache,
1489 74881701 2021-10-15 stsp GOT_ERR_IO);
1490 74881701 2021-10-15 stsp goto done;
1492 74881701 2021-10-15 stsp w = fwrite(delta_buf, 1, n, delta_file);
1493 74881701 2021-10-15 stsp if (w != n) {
1494 74881701 2021-10-15 stsp err = got_ferror(delta_file,
1495 74881701 2021-10-15 stsp GOT_ERR_IO);
1496 74881701 2021-10-15 stsp goto done;
1498 74881701 2021-10-15 stsp remain -= n;
1500 5060d5a1 2022-01-10 stsp err = deltahdr(&packfile_size, &ctx, packfile,
1501 5060d5a1 2022-01-10 stsp m, use_offset_deltas);
1503 5060d5a1 2022-01-10 stsp goto done;
1504 74881701 2021-10-15 stsp if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1505 74881701 2021-10-15 stsp err = got_error_from_errno("fseeko");
1506 74881701 2021-10-15 stsp goto done;
1508 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, delta_file,
1509 e6bcace5 2021-06-22 stsp packfile, &csum);
1511 e6bcace5 2021-06-22 stsp goto done;
1512 05118f5a 2021-06-22 stsp packfile_size += outlen;
1515 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1516 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1517 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1518 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1519 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1520 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1521 211cfef0 2022-01-05 stsp if (progress_cb) {
1522 211cfef0 2022-01-05 stsp err = progress_cb(progress_arg, packfile_size, nours,
1523 211cfef0 2022-01-05 stsp nmeta, nmeta, nmeta);
1525 211cfef0 2022-01-05 stsp goto done;
1528 08347b73 2021-10-14 stsp if (delta_file && fclose(delta_file) == EOF && err == NULL)
1529 08347b73 2021-10-14 stsp err = got_error_from_errno("fclose");
1531 08347b73 2021-10-14 stsp got_object_raw_close(raw);
1532 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
1533 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
1534 e6bcace5 2021-06-22 stsp return err;
1537 e6bcace5 2021-06-22 stsp const struct got_error *
1538 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1539 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1540 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1541 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1542 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1543 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1545 e6bcace5 2021-06-22 stsp const struct got_error *err;
1546 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
1547 e6bcace5 2021-06-22 stsp int nmeta;
1548 74881701 2021-10-15 stsp FILE *delta_cache = NULL;
1549 211cfef0 2022-01-05 stsp struct got_ratelimit rl;
1551 211cfef0 2022-01-05 stsp got_ratelimit_init(&rl, 0, 500);
1553 e6bcace5 2021-06-22 stsp err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1554 211cfef0 2022-01-05 stsp loose_obj_only, progress_cb, progress_arg, &rl,
1555 211cfef0 2022-01-05 stsp cancel_cb, cancel_arg);
1557 e6bcace5 2021-06-22 stsp return err;
1559 f8a36e22 2021-08-26 stsp if (nmeta == 0 && !allow_empty) {
1560 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1561 05118f5a 2021-06-22 stsp goto done;
1564 74881701 2021-10-15 stsp delta_cache = got_opentemp();
1565 74881701 2021-10-15 stsp if (delta_cache == NULL) {
1566 74881701 2021-10-15 stsp err = got_error_from_errno("got_opentemp");
1567 74881701 2021-10-15 stsp goto done;
1570 f8a36e22 2021-08-26 stsp if (nmeta > 0) {
1571 74881701 2021-10-15 stsp err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1572 211cfef0 2022-01-05 stsp progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1574 f8a36e22 2021-08-26 stsp goto done;
1575 74881701 2021-10-15 stsp if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1576 74881701 2021-10-15 stsp err = got_error_from_errno("fseeko");
1577 74881701 2021-10-15 stsp goto done;
1581 74881701 2021-10-15 stsp err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1582 211cfef0 2022-01-05 stsp repo, progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1584 e6bcace5 2021-06-22 stsp goto done;
1586 e6bcace5 2021-06-22 stsp free_nmeta(meta, nmeta);
1587 74881701 2021-10-15 stsp if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1588 74881701 2021-10-15 stsp err = got_error_from_errno("fclose");
1589 e6bcace5 2021-06-22 stsp return err;