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>
24 e93fb944 2022-05-10 stsp #include <sys/mman.h>
26 e3f86256 2022-02-18 naddy #include <endian.h>
27 e93fb944 2022-05-10 stsp #include <errno.h>
28 08736cf9 2021-06-23 stsp #include <stdint.h>
29 05118f5a 2021-06-22 stsp #include <imsg.h>
30 e6bcace5 2021-06-22 stsp #include <stdio.h>
31 e6bcace5 2021-06-22 stsp #include <stdlib.h>
32 e6bcace5 2021-06-22 stsp #include <string.h>
33 e6bcace5 2021-06-22 stsp #include <sha1.h>
34 211cfef0 2022-01-05 stsp #include <time.h>
35 bfc73a47 2022-03-19 naddy #include <unistd.h>
36 e6bcace5 2021-06-22 stsp #include <limits.h>
37 e6bcace5 2021-06-22 stsp #include <zlib.h>
39 e6bcace5 2021-06-22 stsp #include "got_error.h"
40 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
41 e6bcace5 2021-06-22 stsp #include "got_object.h"
42 f8b19efd 2021-10-13 stsp #include "got_path.h"
43 05118f5a 2021-06-22 stsp #include "got_reference.h"
44 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
45 08347b73 2021-10-14 stsp #include "got_opentemp.h"
47 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
50 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
52 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
53 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
54 17cfdba6 2022-05-20 op #include "got_lib_pack_create.h"
55 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
56 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
57 211cfef0 2022-01-05 stsp #include "got_lib_ratelimit.h"
58 2d9e6abf 2022-05-04 stsp #include "got_lib_inflate.h"
60 f8174ca5 2022-05-20 stsp #include "murmurhash2.h"
63 74881701 2021-10-15 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
67 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
70 70f8f24d 2022-04-14 stsp #ifndef nitems
71 70f8f24d 2022-04-14 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
74 e6bcace5 2021-06-22 stsp struct got_pack_meta {
75 e6bcace5 2021-06-22 stsp struct got_object_id id;
76 f8174ca5 2022-05-20 stsp uint32_t path_hash;
77 e6bcace5 2021-06-22 stsp int obj_type;
79 e6bcace5 2021-06-22 stsp time_t mtime;
81 e6bcace5 2021-06-22 stsp /* The best delta we picked */
82 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
83 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
84 2d9e6abf 2022-05-04 stsp unsigned char *delta_buf; /* if encoded in memory (compressed) */
85 2d9e6abf 2022-05-04 stsp off_t delta_offset; /* offset in delta cache file (compressed) */
86 5060d5a1 2022-01-10 stsp off_t delta_len; /* encoded delta length */
87 2d9e6abf 2022-05-04 stsp off_t delta_compressed_len; /* encoded+compressed delta length */
90 67fd6849 2022-02-13 stsp off_t reused_delta_offset; /* offset of delta in reused pack file */
91 67fd6849 2022-02-13 stsp struct got_object_id *base_obj_id;
93 e6bcace5 2021-06-22 stsp /* Only used for delta window */
94 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
96 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
100 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
101 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
103 e6bcace5 2021-06-22 stsp int metasz;
106 e6bcace5 2021-06-22 stsp static const struct got_error *
107 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
108 d6a28ffe 2022-05-20 op const char *path, int obj_type, time_t mtime, uint32_t seed)
110 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
112 e6bcace5 2021-06-22 stsp *new = NULL;
114 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
115 e6bcace5 2021-06-22 stsp if (m == NULL)
116 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
118 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
120 d6a28ffe 2022-05-20 op m->path_hash = murmurhash2(path, strlen(path), seed);
121 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
122 e6bcace5 2021-06-22 stsp m->mtime = mtime;
124 e6bcace5 2021-06-22 stsp return NULL;
127 e6bcace5 2021-06-22 stsp static void
128 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
130 e6bcace5 2021-06-22 stsp if (meta == NULL)
132 f8174ca5 2022-05-20 stsp meta->path_hash = 0;
133 5060d5a1 2022-01-10 stsp free(meta->delta_buf);
134 5060d5a1 2022-01-10 stsp meta->delta_buf = NULL;
135 67fd6849 2022-02-13 stsp free(meta->base_obj_id);
136 67fd6849 2022-02-13 stsp meta->base_obj_id = NULL;
137 411cbec1 2022-05-20 stsp meta->reused_delta_offset = 0;
140 e6bcace5 2021-06-22 stsp static void
141 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
145 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
146 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
147 e6bcace5 2021-06-22 stsp free(meta);
151 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
153 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
155 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
156 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
158 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
159 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
160 f8174ca5 2022-05-20 stsp if (a->path_hash < b->path_hash)
162 f8174ca5 2022-05-20 stsp if (a->path_hash > b->path_hash)
164 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
166 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
168 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
171 5060d5a1 2022-01-10 stsp static off_t
172 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
175 5060d5a1 2022-01-10 stsp off_t size = 32;
176 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
177 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
178 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
180 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
182 e6bcace5 2021-06-22 stsp return size;
185 5060d5a1 2022-01-10 stsp static const struct got_error *
186 5060d5a1 2022-01-10 stsp append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
190 5060d5a1 2022-01-10 stsp if (*len + nseg >= *sz) {
191 5060d5a1 2022-01-10 stsp while (*len + nseg >= *sz)
192 5060d5a1 2022-01-10 stsp *sz += *sz / 2;
193 5060d5a1 2022-01-10 stsp n = realloc(*p, *sz);
194 5060d5a1 2022-01-10 stsp if (n == NULL)
195 5060d5a1 2022-01-10 stsp return got_error_from_errno("realloc");
198 5060d5a1 2022-01-10 stsp memcpy(*p + *len, seg, nseg);
199 5060d5a1 2022-01-10 stsp *len += nseg;
200 5060d5a1 2022-01-10 stsp return NULL;
203 5060d5a1 2022-01-10 stsp static const struct got_error *
204 5060d5a1 2022-01-10 stsp encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
205 5060d5a1 2022-01-10 stsp struct got_delta_instruction *deltas, int ndeltas,
206 5060d5a1 2022-01-10 stsp off_t delta_size, off_t base_size)
208 5060d5a1 2022-01-10 stsp const struct got_error *err;
209 5060d5a1 2022-01-10 stsp unsigned char buf[16], *bp;
211 2d9e6abf 2022-05-04 stsp size_t len = 0, compressed_len;
212 2d9e6abf 2022-05-04 stsp off_t bufsize = delta_size;
214 5060d5a1 2022-01-10 stsp struct got_delta_instruction *d;
215 2d9e6abf 2022-05-04 stsp uint8_t *delta_buf;
217 2d9e6abf 2022-05-04 stsp delta_buf = malloc(bufsize);
218 2d9e6abf 2022-05-04 stsp if (delta_buf == NULL)
219 2d9e6abf 2022-05-04 stsp return got_error_from_errno("malloc");
221 5060d5a1 2022-01-10 stsp /* base object size */
222 5060d5a1 2022-01-10 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
223 5060d5a1 2022-01-10 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
224 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
225 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
226 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
227 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
229 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
233 5060d5a1 2022-01-10 stsp /* target object size */
234 5060d5a1 2022-01-10 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
235 5060d5a1 2022-01-10 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
236 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
237 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
238 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
239 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
241 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
245 5060d5a1 2022-01-10 stsp for (j = 0; j < ndeltas; j++) {
246 5060d5a1 2022-01-10 stsp d = &deltas[j];
247 5060d5a1 2022-01-10 stsp if (d->copy) {
248 5060d5a1 2022-01-10 stsp n = d->offset;
249 5060d5a1 2022-01-10 stsp bp = &buf[1];
250 5060d5a1 2022-01-10 stsp buf[0] = GOT_DELTA_BASE_COPY;
251 5060d5a1 2022-01-10 stsp for (i = 0; i < 4; i++) {
252 5060d5a1 2022-01-10 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
253 5060d5a1 2022-01-10 stsp buf[0] |= 1 << i;
254 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
256 5060d5a1 2022-01-10 stsp if (n == 0)
260 5060d5a1 2022-01-10 stsp n = d->len;
261 5060d5a1 2022-01-10 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
262 5060d5a1 2022-01-10 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
263 5060d5a1 2022-01-10 stsp for (i = 0; i < 3 && n > 0; i++) {
264 5060d5a1 2022-01-10 stsp buf[0] |= 1 << (i + 4);
265 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
269 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
270 5060d5a1 2022-01-10 stsp buf, bp - buf);
273 5060d5a1 2022-01-10 stsp } else if (o->f == NULL) {
275 5060d5a1 2022-01-10 stsp while (n != d->len) {
276 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
277 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
281 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
282 5060d5a1 2022-01-10 stsp o->data + o->hdrlen + d->offset + n,
286 5060d5a1 2022-01-10 stsp n += buf[0];
289 5060d5a1 2022-01-10 stsp char content[128];
291 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
292 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
296 5060d5a1 2022-01-10 stsp while (n != d->len) {
297 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
298 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
302 5060d5a1 2022-01-10 stsp r = fread(content, 1, buf[0], o->f);
303 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
304 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
307 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
308 5060d5a1 2022-01-10 stsp content, buf[0]);
311 5060d5a1 2022-01-10 stsp n += buf[0];
316 2d9e6abf 2022-05-04 stsp err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
317 2d9e6abf 2022-05-04 stsp NULL, NULL, delta_buf, 0, len);
321 5060d5a1 2022-01-10 stsp m->delta_len = len;
322 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
324 2d9e6abf 2022-05-04 stsp free(delta_buf);
325 2d9e6abf 2022-05-04 stsp return err;
328 74881701 2021-10-15 stsp static const struct got_error *
329 74881701 2021-10-15 stsp encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
330 74881701 2021-10-15 stsp struct got_delta_instruction *deltas, int ndeltas,
331 a319ca8c 2021-10-15 stsp off_t base_size, FILE *f)
333 2d9e6abf 2022-05-04 stsp const struct got_error *err;
334 a319ca8c 2021-10-15 stsp unsigned char buf[16], *bp;
337 2d9e6abf 2022-05-04 stsp struct got_deflate_buf zb;
338 a319ca8c 2021-10-15 stsp struct got_delta_instruction *d;
339 2d9e6abf 2022-05-04 stsp off_t delta_len = 0, compressed_len = 0;
341 2d9e6abf 2022-05-04 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
343 2d9e6abf 2022-05-04 stsp return err;
345 a319ca8c 2021-10-15 stsp /* base object size */
346 a319ca8c 2021-10-15 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
347 a319ca8c 2021-10-15 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
348 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
349 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
350 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
351 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
354 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
355 2d9e6abf 2022-05-04 stsp buf, 0, i, f, NULL);
358 2d9e6abf 2022-05-04 stsp delta_len += i;
360 a319ca8c 2021-10-15 stsp /* target object size */
361 a319ca8c 2021-10-15 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
362 a319ca8c 2021-10-15 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
363 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
364 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
365 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
366 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
369 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
370 2d9e6abf 2022-05-04 stsp buf, 0, i, f, NULL);
373 2d9e6abf 2022-05-04 stsp delta_len += i;
375 a319ca8c 2021-10-15 stsp for (j = 0; j < ndeltas; j++) {
376 a319ca8c 2021-10-15 stsp d = &deltas[j];
377 a319ca8c 2021-10-15 stsp if (d->copy) {
378 a319ca8c 2021-10-15 stsp n = d->offset;
379 a319ca8c 2021-10-15 stsp bp = &buf[1];
380 a319ca8c 2021-10-15 stsp buf[0] = GOT_DELTA_BASE_COPY;
381 a319ca8c 2021-10-15 stsp for (i = 0; i < 4; i++) {
382 a319ca8c 2021-10-15 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
383 a319ca8c 2021-10-15 stsp buf[0] |= 1 << i;
384 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
386 a319ca8c 2021-10-15 stsp if (n == 0)
389 a319ca8c 2021-10-15 stsp n = d->len;
390 a319ca8c 2021-10-15 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
391 a319ca8c 2021-10-15 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
392 a319ca8c 2021-10-15 stsp for (i = 0; i < 3 && n > 0; i++) {
393 a319ca8c 2021-10-15 stsp buf[0] |= 1 << (i + 4);
394 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
398 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
399 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, bp - buf, f, NULL);
402 2d9e6abf 2022-05-04 stsp delta_len += (bp - buf);
403 64a8571e 2022-01-07 stsp } else if (o->f == NULL) {
405 64a8571e 2022-01-07 stsp while (n != d->len) {
406 64a8571e 2022-01-07 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
407 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
408 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
411 2d9e6abf 2022-05-04 stsp delta_len++;
412 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
413 2d9e6abf 2022-05-04 stsp &compressed_len,
414 2d9e6abf 2022-05-04 stsp o->data + o->hdrlen + d->offset + n, 0,
415 2d9e6abf 2022-05-04 stsp buf[0], f, NULL);
418 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
419 64a8571e 2022-01-07 stsp n += buf[0];
422 a319ca8c 2021-10-15 stsp char content[128];
424 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
425 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
429 a319ca8c 2021-10-15 stsp while (n != d->len) {
430 a319ca8c 2021-10-15 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
431 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
432 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
435 2d9e6abf 2022-05-04 stsp delta_len++;
436 a319ca8c 2021-10-15 stsp r = fread(content, 1, buf[0], o->f);
437 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
438 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
441 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
442 2d9e6abf 2022-05-04 stsp &compressed_len, content, 0, buf[0], f,
446 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
447 a319ca8c 2021-10-15 stsp n += buf[0];
452 2d9e6abf 2022-05-04 stsp err = got_deflate_flush(&zb, f, NULL, &compressed_len);
456 2d9e6abf 2022-05-04 stsp /* sanity check */
457 2d9e6abf 2022-05-04 stsp if (compressed_len != ftello(f) - m->delta_offset) {
458 2d9e6abf 2022-05-04 stsp err = got_error(GOT_ERR_COMPRESSION);
462 2d9e6abf 2022-05-04 stsp m->delta_len = delta_len;
463 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
465 2d9e6abf 2022-05-04 stsp got_deflate_end(&zb);
466 2d9e6abf 2022-05-04 stsp return err;
469 211cfef0 2022-01-05 stsp static const struct got_error *
470 211cfef0 2022-01-05 stsp report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
471 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
472 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
473 b8af7c06 2022-03-15 stsp int nobj_written)
475 211cfef0 2022-01-05 stsp const struct got_error *err;
476 211cfef0 2022-01-05 stsp int elapsed;
478 211cfef0 2022-01-05 stsp if (progress_cb == NULL)
479 211cfef0 2022-01-05 stsp return NULL;
481 211cfef0 2022-01-05 stsp err = got_ratelimit_check(&elapsed, rl);
482 211cfef0 2022-01-05 stsp if (err || !elapsed)
483 211cfef0 2022-01-05 stsp return err;
485 b8af7c06 2022-03-15 stsp return progress_cb(progress_arg, ncolored, nfound, ntrees,
486 b8af7c06 2022-03-15 stsp packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
489 e6bcace5 2021-06-22 stsp static const struct got_error *
490 67fd6849 2022-02-13 stsp add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
492 67fd6849 2022-02-13 stsp if (v->nmeta == v->metasz){
493 67fd6849 2022-02-13 stsp size_t newsize = 2 * v->metasz;
494 67fd6849 2022-02-13 stsp struct got_pack_meta **new;
495 67fd6849 2022-02-13 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
496 67fd6849 2022-02-13 stsp if (new == NULL)
497 67fd6849 2022-02-13 stsp return got_error_from_errno("reallocarray");
498 67fd6849 2022-02-13 stsp v->meta = new;
499 67fd6849 2022-02-13 stsp v->metasz = newsize;
502 67fd6849 2022-02-13 stsp v->meta[v->nmeta++] = m;
503 67fd6849 2022-02-13 stsp return NULL;
506 67fd6849 2022-02-13 stsp static const struct got_error *
507 67fd6849 2022-02-13 stsp find_pack_for_reuse(struct got_packidx **best_packidx,
508 67fd6849 2022-02-13 stsp struct got_repository *repo)
510 9b576444 2022-03-14 stsp const struct got_error *err = NULL;
511 67fd6849 2022-02-13 stsp struct got_pathlist_entry *pe;
512 67fd6849 2022-02-13 stsp const char *best_packidx_path = NULL;
513 67fd6849 2022-02-13 stsp int nobj_max = 0;
515 67fd6849 2022-02-13 stsp *best_packidx = NULL;
517 9b576444 2022-03-14 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
518 67fd6849 2022-02-13 stsp const char *path_packidx = pe->path;
519 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
522 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
526 67fd6849 2022-02-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
527 67fd6849 2022-02-13 stsp if (nobj > nobj_max) {
528 67fd6849 2022-02-13 stsp best_packidx_path = path_packidx;
529 67fd6849 2022-02-13 stsp nobj_max = nobj;
533 67fd6849 2022-02-13 stsp if (best_packidx_path) {
534 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
538 67fd6849 2022-02-13 stsp return err;
541 fae7e038 2022-05-07 stsp struct send_id_arg {
542 fae7e038 2022-05-07 stsp struct imsgbuf *ibuf;
543 fae7e038 2022-05-07 stsp struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
544 fae7e038 2022-05-07 stsp size_t nids;
547 67fd6849 2022-02-13 stsp static const struct got_error *
548 fae7e038 2022-05-07 stsp send_id(struct got_object_id *id, void *data, void *arg)
550 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
551 fae7e038 2022-05-07 stsp struct send_id_arg *a = arg;
553 fae7e038 2022-05-07 stsp a->ids[a->nids++] = id;
555 fae7e038 2022-05-07 stsp if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
556 fae7e038 2022-05-07 stsp err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
558 67fd6849 2022-02-13 stsp return err;
559 fae7e038 2022-05-07 stsp a->nids = 0;
562 fae7e038 2022-05-07 stsp return NULL;
565 fae7e038 2022-05-07 stsp static const struct got_error *
566 fae7e038 2022-05-07 stsp recv_reused_delta(struct got_imsg_reused_delta *delta,
567 fae7e038 2022-05-07 stsp struct got_object_idset *idset, struct got_pack_metavec *v)
569 fae7e038 2022-05-07 stsp struct got_pack_meta *m, *base;
571 fae7e038 2022-05-07 stsp if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
572 fae7e038 2022-05-07 stsp delta->delta_offset +
573 fae7e038 2022-05-07 stsp delta->delta_compressed_size < delta->delta_offset)
574 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_BAD_PACKFILE);
576 fae7e038 2022-05-07 stsp m = got_object_idset_get(idset, &delta->id);
577 fae7e038 2022-05-07 stsp if (m == NULL)
578 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_OBJ);
580 fae7e038 2022-05-07 stsp base = got_object_idset_get(idset, &delta->base_id);
581 fae7e038 2022-05-07 stsp if (base == NULL)
582 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_NO_OBJ);
584 fae7e038 2022-05-07 stsp m->delta_len = delta->delta_size;
585 fae7e038 2022-05-07 stsp m->delta_compressed_len = delta->delta_compressed_size;
586 fae7e038 2022-05-07 stsp m->delta_offset = delta->delta_out_offset;
587 fae7e038 2022-05-07 stsp m->prev = base;
588 fae7e038 2022-05-07 stsp m->size = delta->result_size;
589 fae7e038 2022-05-07 stsp m->reused_delta_offset = delta->delta_offset;
590 fae7e038 2022-05-07 stsp m->base_obj_id = got_object_id_dup(&delta->base_id);
591 fae7e038 2022-05-07 stsp if (m->base_obj_id == NULL)
592 fae7e038 2022-05-07 stsp return got_error_from_errno("got_object_id_dup");
594 fae7e038 2022-05-07 stsp return add_meta(m, v);
597 67fd6849 2022-02-13 stsp static const struct got_error *
598 67fd6849 2022-02-13 stsp search_deltas(struct got_pack_metavec *v, struct got_object_idset *idset,
599 b8af7c06 2022-03-15 stsp int delta_cache_fd, int ncolored, int nfound, int ntrees, int ncommits,
600 b8af7c06 2022-03-15 stsp struct got_repository *repo,
601 67fd6849 2022-02-13 stsp got_pack_progress_cb progress_cb, void *progress_arg,
602 67fd6849 2022-02-13 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
604 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
605 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
606 67fd6849 2022-02-13 stsp struct got_pack *pack;
607 fae7e038 2022-05-07 stsp struct send_id_arg sia;
608 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
609 fae7e038 2022-05-07 stsp size_t ndeltas, i;
611 67fd6849 2022-02-13 stsp err = find_pack_for_reuse(&packidx, repo);
613 67fd6849 2022-02-13 stsp return err;
615 67fd6849 2022-02-13 stsp if (packidx == NULL)
616 67fd6849 2022-02-13 stsp return NULL;
618 fae7e038 2022-05-07 stsp err = got_object_prepare_delta_reuse(&pack, packidx,
619 fae7e038 2022-05-07 stsp delta_cache_fd, repo);
621 67fd6849 2022-02-13 stsp return err;
623 fae7e038 2022-05-07 stsp memset(&sia, 0, sizeof(sia));
624 fae7e038 2022-05-07 stsp sia.ibuf = pack->privsep_child->ibuf;
625 fae7e038 2022-05-07 stsp err = got_object_idset_for_each(idset, send_id, &sia);
627 fae7e038 2022-05-07 stsp return err;
628 fae7e038 2022-05-07 stsp if (sia.nids > 0) {
629 fae7e038 2022-05-07 stsp err = got_privsep_send_object_idlist(pack->privsep_child->ibuf,
630 fae7e038 2022-05-07 stsp sia.ids, sia.nids);
632 fae7e038 2022-05-07 stsp return err;
634 fae7e038 2022-05-07 stsp err = got_privsep_send_object_idlist_done(pack->privsep_child->ibuf);
636 fae7e038 2022-05-07 stsp return err;
639 fae7e038 2022-05-07 stsp int done = 0;
641 fae7e038 2022-05-07 stsp if (cancel_cb) {
642 fae7e038 2022-05-07 stsp err = (*cancel_cb)(cancel_arg);
647 fae7e038 2022-05-07 stsp err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
648 fae7e038 2022-05-07 stsp pack->privsep_child->ibuf);
649 fae7e038 2022-05-07 stsp if (err || done)
652 fae7e038 2022-05-07 stsp for (i = 0; i < ndeltas; i++) {
653 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta *delta = &deltas[i];
654 fae7e038 2022-05-07 stsp err = recv_reused_delta(delta, idset, v);
659 fae7e038 2022-05-07 stsp err = report_progress(progress_cb, progress_arg, rl,
660 fae7e038 2022-05-07 stsp ncolored, nfound, ntrees, 0L, ncommits,
661 fae7e038 2022-05-07 stsp got_object_idset_num_elements(idset), v->nmeta, 0);
666 67fd6849 2022-02-13 stsp return err;
669 67fd6849 2022-02-13 stsp static const struct got_error *
670 b8af7c06 2022-03-15 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
671 b8af7c06 2022-03-15 stsp int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
672 b8af7c06 2022-03-15 stsp struct got_repository *repo,
673 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
674 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
676 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
677 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
678 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
679 74881701 2021-10-15 stsp struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
680 5060d5a1 2022-01-10 stsp int i, j, ndeltas, best_ndeltas;
681 5060d5a1 2022-01-10 stsp off_t size, best_size;
682 4f4d853e 2021-10-24 stsp const int max_base_candidates = 3;
683 402a5ec1 2022-01-10 stsp size_t delta_memsize = 0;
684 adb4bbb2 2022-05-20 stsp const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
685 cc7a354a 2021-10-15 stsp int outfd = -1;
686 d6a28ffe 2022-05-20 op uint32_t delta_seed;
688 d6a28ffe 2022-05-20 op delta_seed = arc4random();
690 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
691 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
692 e6bcace5 2021-06-22 stsp if (cancel_cb) {
693 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
697 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
698 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
699 b8af7c06 2022-03-15 stsp nreused + i, 0);
702 e6bcace5 2021-06-22 stsp m = meta[i];
704 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
705 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
708 94dac27c 2021-10-15 stsp err = got_object_raw_open(&raw, &outfd, repo, &m->id);
711 600b755e 2021-10-14 stsp m->size = raw->size;
713 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
714 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&m->dtab, raw->data,
715 d6a28ffe 2022-05-20 op raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
717 64a8571e 2022-01-07 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
718 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed);
723 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
724 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
725 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
726 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
727 e6bcace5 2021-06-22 stsp n->dtab = NULL;
730 74881701 2021-10-15 stsp best_size = raw->size;
731 74881701 2021-10-15 stsp best_ndeltas = 0;
732 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
733 e6bcace5 2021-06-22 stsp if (cancel_cb) {
734 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
738 e6bcace5 2021-06-22 stsp base = meta[j];
739 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
740 22edbce7 2021-10-24 stsp if (base->nchain >= 128 ||
741 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
744 d3c116bf 2021-10-15 stsp err = got_object_raw_open(&base_raw, &outfd, repo,
745 94dac27c 2021-10-15 stsp &base->id);
749 64a8571e 2022-01-07 stsp if (raw->f == NULL && base_raw->f == NULL) {
750 64a8571e 2022-01-07 stsp err = got_deltify_mem_mem(&deltas, &ndeltas,
751 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
752 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
753 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
754 64a8571e 2022-01-07 stsp base_raw->hdrlen,
755 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
756 64a8571e 2022-01-07 stsp } else if (raw->f == NULL) {
757 64a8571e 2022-01-07 stsp err = got_deltify_mem_file(&deltas, &ndeltas,
758 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
759 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
760 64a8571e 2022-01-07 stsp base->dtab, base_raw->f,
761 64a8571e 2022-01-07 stsp base_raw->hdrlen,
762 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
763 64a8571e 2022-01-07 stsp } else if (base_raw->f == NULL) {
764 64a8571e 2022-01-07 stsp err = got_deltify_file_mem(&deltas, &ndeltas,
765 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
766 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
767 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
768 64a8571e 2022-01-07 stsp base_raw->hdrlen,
769 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
771 64a8571e 2022-01-07 stsp err = got_deltify(&deltas, &ndeltas,
772 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
773 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
774 64a8571e 2022-01-07 stsp base->dtab, base_raw->f, base_raw->hdrlen,
775 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
777 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
778 e6bcace5 2021-06-22 stsp base_raw = NULL;
782 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
783 74881701 2021-10-15 stsp if (size + 32 < best_size){
785 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
786 e6bcace5 2021-06-22 stsp * replace it.
788 74881701 2021-10-15 stsp best_size = size;
789 74881701 2021-10-15 stsp free(best_deltas);
790 74881701 2021-10-15 stsp best_deltas = deltas;
791 74881701 2021-10-15 stsp best_ndeltas = ndeltas;
792 74881701 2021-10-15 stsp deltas = NULL;
793 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
794 e6bcace5 2021-06-22 stsp m->prev = base;
795 e6bcace5 2021-06-22 stsp m->head = base->head;
796 e6bcace5 2021-06-22 stsp if (m->head == NULL)
797 e6bcace5 2021-06-22 stsp m->head = base;
799 e6bcace5 2021-06-22 stsp free(deltas);
800 e6bcace5 2021-06-22 stsp deltas = NULL;
801 e6bcace5 2021-06-22 stsp ndeltas = 0;
805 74881701 2021-10-15 stsp if (best_ndeltas > 0) {
806 402a5ec1 2022-01-10 stsp if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
807 402a5ec1 2022-01-10 stsp delta_memsize + best_size <= max_delta_memsize) {
808 402a5ec1 2022-01-10 stsp delta_memsize += best_size;
809 5060d5a1 2022-01-10 stsp err = encode_delta_in_mem(m, raw, best_deltas,
810 5060d5a1 2022-01-10 stsp best_ndeltas, best_size, m->prev->size);
812 5060d5a1 2022-01-10 stsp m->delta_offset = ftello(delta_cache);
813 5060d5a1 2022-01-10 stsp err = encode_delta(m, raw, best_deltas,
814 5060d5a1 2022-01-10 stsp best_ndeltas, m->prev->size, delta_cache);
816 74881701 2021-10-15 stsp free(best_deltas);
817 74881701 2021-10-15 stsp best_deltas = NULL;
818 74881701 2021-10-15 stsp best_ndeltas = 0;
823 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
824 e6bcace5 2021-06-22 stsp raw = NULL;
827 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
828 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
829 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
832 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
833 e6bcace5 2021-06-22 stsp if (base_raw)
834 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
835 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
836 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
837 74881701 2021-10-15 stsp free(deltas);
838 74881701 2021-10-15 stsp free(best_deltas);
839 e6bcace5 2021-06-22 stsp return err;
842 e6bcace5 2021-06-22 stsp static const struct got_error *
843 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
844 05118f5a 2021-06-22 stsp struct got_repository *repo)
846 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
847 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
850 05118f5a 2021-06-22 stsp *found = 0;
852 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
853 05118f5a 2021-06-22 stsp if (err == NULL)
854 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
855 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
856 05118f5a 2021-06-22 stsp err = NULL;
857 05118f5a 2021-06-22 stsp return err;
860 05118f5a 2021-06-22 stsp static const struct got_error *
861 67fd6849 2022-02-13 stsp add_object(int want_meta, struct got_object_idset *idset,
862 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
863 d6a28ffe 2022-05-20 op time_t mtime, uint32_t seed, int loose_obj_only,
864 d6a28ffe 2022-05-20 op struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
865 6863cbf9 2022-03-21 stsp got_pack_progress_cb progress_cb, void *progress_arg,
866 6863cbf9 2022-03-21 stsp struct got_ratelimit *rl)
868 e6bcace5 2021-06-22 stsp const struct got_error *err;
869 67fd6849 2022-02-13 stsp struct got_pack_meta *m = NULL;
871 05118f5a 2021-06-22 stsp if (loose_obj_only) {
872 05118f5a 2021-06-22 stsp int is_packed;
873 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
875 05118f5a 2021-06-22 stsp return err;
876 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
877 05118f5a 2021-06-22 stsp return NULL;
880 67fd6849 2022-02-13 stsp if (want_meta) {
881 d6a28ffe 2022-05-20 op err = alloc_meta(&m, id, path, obj_type, mtime, seed);
883 6863cbf9 2022-03-21 stsp return err;
885 6863cbf9 2022-03-21 stsp (*nfound)++;
886 6863cbf9 2022-03-21 stsp err = report_progress(progress_cb, progress_arg, rl,
887 6863cbf9 2022-03-21 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
889 bb6672b6 2022-04-14 tb clear_meta(m);
891 67fd6849 2022-02-13 stsp return err;
895 bb6672b6 2022-04-14 tb err = got_object_idset_add(idset, id, m);
897 bb6672b6 2022-04-14 tb clear_meta(m);
903 e6bcace5 2021-06-22 stsp static const struct got_error *
904 67fd6849 2022-02-13 stsp load_tree_entries(struct got_object_id_queue *ids, int want_meta,
905 2f8438b0 2022-05-04 stsp struct got_object_idset *idset, struct got_object_idset *idset_exclude,
906 2f8438b0 2022-05-04 stsp struct got_object_id *tree_id,
907 d6a28ffe 2022-05-20 op const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
908 b8af7c06 2022-03-15 stsp int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
909 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
910 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
912 e6bcace5 2021-06-22 stsp const struct got_error *err;
913 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
914 e6bcace5 2021-06-22 stsp char *p = NULL;
917 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
919 b8af7c06 2022-03-15 stsp return err;
921 b8af7c06 2022-03-15 stsp (*ntrees)++;
922 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
923 b8af7c06 2022-03-15 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
925 e6bcace5 2021-06-22 stsp return err;
927 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
928 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
929 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
930 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
932 e6bcace5 2021-06-22 stsp if (cancel_cb) {
933 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
938 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
939 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset, id) ||
940 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
943 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
944 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
945 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
949 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
950 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
951 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
954 3e6ceea0 2022-05-20 stsp qid->data = p;
956 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
957 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
958 2f8438b0 2022-05-04 stsp err = add_object(want_meta,
959 2f8438b0 2022-05-04 stsp want_meta ? idset : idset_exclude, id, p,
960 d6a28ffe 2022-05-20 op GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
961 d6a28ffe 2022-05-20 op repo, ncolored, nfound, ntrees,
962 6863cbf9 2022-03-21 stsp progress_cb, progress_arg, rl);
973 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
975 e6bcace5 2021-06-22 stsp return err;
978 e6bcace5 2021-06-22 stsp static const struct got_error *
979 67fd6849 2022-02-13 stsp load_tree(int want_meta, struct got_object_idset *idset,
980 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
981 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
982 d6a28ffe 2022-05-20 op uint32_t seed, struct got_repository *repo, int loose_obj_only,
983 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
984 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
985 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
987 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
988 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
989 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
991 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, tree_id) ||
992 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, tree_id))
993 e6bcace5 2021-06-22 stsp return NULL;
995 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
997 3e6ceea0 2022-05-20 stsp return err;
998 3e6ceea0 2022-05-20 stsp qid->data = strdup(dpath);
999 3e6ceea0 2022-05-20 stsp if (qid->data == NULL) {
1000 3e6ceea0 2022-05-20 stsp err = got_error_from_errno("strdup");
1001 3e6ceea0 2022-05-20 stsp got_object_qid_free(qid);
1002 e6bcace5 2021-06-22 stsp return err;
1005 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
1006 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
1008 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
1009 3e6ceea0 2022-05-20 stsp const char *path;
1010 e6bcace5 2021-06-22 stsp if (cancel_cb) {
1011 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
1016 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
1017 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
1018 3e6ceea0 2022-05-20 stsp path = qid->data;
1020 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, &qid->id) ||
1021 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, &qid->id)) {
1022 3e6ceea0 2022-05-20 stsp free(qid->data);
1023 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1027 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1028 3e6ceea0 2022-05-20 stsp &qid->id, path, GOT_OBJ_TYPE_TREE,
1029 d6a28ffe 2022-05-20 op mtime, seed, loose_obj_only, repo,
1030 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1031 e6bcace5 2021-06-22 stsp if (err) {
1032 3e6ceea0 2022-05-20 stsp free(qid->data);
1033 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1037 2f8438b0 2022-05-04 stsp err = load_tree_entries(&tree_ids, want_meta, idset,
1038 2f8438b0 2022-05-04 stsp idset_exclude, &qid->id,
1039 d6a28ffe 2022-05-20 op path, mtime, seed, repo, loose_obj_only, ncolored, nfound,
1040 b8af7c06 2022-03-15 stsp ntrees, progress_cb, progress_arg, rl,
1041 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1042 3e6ceea0 2022-05-20 stsp free(qid->data);
1043 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1048 3e6ceea0 2022-05-20 stsp STAILQ_FOREACH(qid, &tree_ids, entry)
1049 3e6ceea0 2022-05-20 stsp free(qid->data);
1050 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
1051 e6bcace5 2021-06-22 stsp return err;
1054 e6bcace5 2021-06-22 stsp static const struct got_error *
1055 67fd6849 2022-02-13 stsp load_commit(int want_meta, struct got_object_idset *idset,
1056 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1057 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1058 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1059 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1060 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1062 e6bcace5 2021-06-22 stsp const struct got_error *err;
1063 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
1065 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
1066 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1067 e6bcace5 2021-06-22 stsp return NULL;
1069 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1070 05118f5a 2021-06-22 stsp int is_packed;
1071 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1073 05118f5a 2021-06-22 stsp return err;
1074 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1075 05118f5a 2021-06-22 stsp return NULL;
1078 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
1080 e6bcace5 2021-06-22 stsp return err;
1082 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1083 2f8438b0 2022-05-04 stsp id, "", GOT_OBJ_TYPE_COMMIT,
1084 d6a28ffe 2022-05-20 op got_object_commit_get_committer_time(commit), seed,
1085 6863cbf9 2022-03-21 stsp loose_obj_only, repo,
1086 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1088 e6bcace5 2021-06-22 stsp goto done;
1090 2f8438b0 2022-05-04 stsp err = load_tree(want_meta, idset, idset_exclude,
1091 2f8438b0 2022-05-04 stsp got_object_commit_get_tree_id(commit),
1092 d6a28ffe 2022-05-20 op "", got_object_commit_get_committer_time(commit), seed,
1093 b8af7c06 2022-03-15 stsp repo, loose_obj_only, ncolored, nfound, ntrees,
1094 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1096 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
1097 e6bcace5 2021-06-22 stsp return err;
1100 05118f5a 2021-06-22 stsp static const struct got_error *
1101 67fd6849 2022-02-13 stsp load_tag(int want_meta, struct got_object_idset *idset,
1102 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
1103 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
1104 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
1105 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1106 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1108 05118f5a 2021-06-22 stsp const struct got_error *err;
1109 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
1111 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
1112 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
1113 05118f5a 2021-06-22 stsp return NULL;
1115 05118f5a 2021-06-22 stsp if (loose_obj_only) {
1116 05118f5a 2021-06-22 stsp int is_packed;
1117 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
1119 05118f5a 2021-06-22 stsp return err;
1120 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1121 05118f5a 2021-06-22 stsp return NULL;
1124 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1126 05118f5a 2021-06-22 stsp return err;
1128 2f8438b0 2022-05-04 stsp err = add_object(want_meta, want_meta ? idset : idset_exclude,
1129 2f8438b0 2022-05-04 stsp id, "", GOT_OBJ_TYPE_TAG,
1130 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1131 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1133 05118f5a 2021-06-22 stsp goto done;
1135 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1136 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1137 2f8438b0 2022-05-04 stsp err = load_commit(want_meta, idset, idset_exclude,
1138 d6a28ffe 2022-05-20 op got_object_tag_get_object_id(tag), repo, seed,
1139 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1140 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1142 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1143 2f8438b0 2022-05-04 stsp err = load_tree(want_meta, idset, idset_exclude,
1144 67fd6849 2022-02-13 stsp got_object_tag_get_object_id(tag), "",
1145 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, repo,
1146 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1147 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1154 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1155 05118f5a 2021-06-22 stsp return err;
1158 e6bcace5 2021-06-22 stsp enum findtwixt_color {
1159 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
1160 e6bcace5 2021-06-22 stsp COLOR_DROP,
1161 e6bcace5 2021-06-22 stsp COLOR_BLANK,
1162 70f8f24d 2022-04-14 stsp COLOR_SKIP,
1165 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
1166 e6bcace5 2021-06-22 stsp COLOR_KEEP,
1167 e6bcace5 2021-06-22 stsp COLOR_DROP,
1168 70f8f24d 2022-04-14 stsp COLOR_BLANK,
1169 70f8f24d 2022-04-14 stsp COLOR_SKIP,
1172 e6bcace5 2021-06-22 stsp static const struct got_error *
1173 70f8f24d 2022-04-14 stsp paint_commit(struct got_object_qid *qid, int color)
1175 70f8f24d 2022-04-14 stsp if (color < 0 || color >= nitems(findtwixt_colors))
1176 70f8f24d 2022-04-14 stsp return got_error(GOT_ERR_RANGE);
1178 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
1179 e6bcace5 2021-06-22 stsp return NULL;
1182 e6bcace5 2021-06-22 stsp static const struct got_error *
1183 70f8f24d 2022-04-14 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1184 70f8f24d 2022-04-14 stsp int color, struct got_repository *repo)
1186 70f8f24d 2022-04-14 stsp const struct got_error *err;
1187 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1189 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1191 e6bcace5 2021-06-22 stsp return err;
1193 70f8f24d 2022-04-14 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
1194 70f8f24d 2022-04-14 stsp return paint_commit(qid, color);
1197 e6bcace5 2021-06-22 stsp struct append_id_arg {
1198 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1200 70f8f24d 2022-04-14 stsp struct got_object_idset *drop;
1201 70f8f24d 2022-04-14 stsp struct got_object_idset *skip;
1204 e6bcace5 2021-06-22 stsp static const struct got_error *
1205 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1207 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1209 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(a->skip, id) ||
1210 70f8f24d 2022-04-14 stsp got_object_idset_contains(a->drop, id))
1211 70f8f24d 2022-04-14 stsp return NULL;
1213 70f8f24d 2022-04-14 stsp a->array[++a->idx] = got_object_id_dup(id);
1214 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1215 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1217 e6bcace5 2021-06-22 stsp return NULL;
1220 29e0594f 2022-04-09 stsp static const struct got_error *
1221 29e0594f 2022-04-09 stsp queue_commit_or_tag_id(struct got_object_id *id, int color,
1222 29e0594f 2022-04-09 stsp struct got_object_id_queue *ids, struct got_repository *repo)
1224 29e0594f 2022-04-09 stsp const struct got_error *err;
1225 29e0594f 2022-04-09 stsp struct got_tag_object *tag = NULL;
1226 29e0594f 2022-04-09 stsp int obj_type;
1228 29e0594f 2022-04-09 stsp err = got_object_get_type(&obj_type, repo, id);
1230 29e0594f 2022-04-09 stsp return err;
1232 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1233 29e0594f 2022-04-09 stsp err = got_object_open_as_tag(&tag, repo, id);
1235 29e0594f 2022-04-09 stsp return err;
1236 29e0594f 2022-04-09 stsp obj_type = got_object_tag_get_object_type(tag);
1237 29e0594f 2022-04-09 stsp id = got_object_tag_get_object_id(tag);
1240 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1241 29e0594f 2022-04-09 stsp err = queue_commit_id(ids, id, color, repo);
1243 29e0594f 2022-04-09 stsp goto done;
1247 29e0594f 2022-04-09 stsp got_object_tag_close(tag);
1248 29e0594f 2022-04-09 stsp return err;
1251 e6bcace5 2021-06-22 stsp static const struct got_error *
1252 70f8f24d 2022-04-14 stsp paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
1253 14dbbf48 2022-04-10 stsp struct got_object_idset *keep, struct got_object_idset *drop,
1254 70f8f24d 2022-04-14 stsp struct got_object_idset *skip, struct got_repository *repo,
1255 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1256 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1258 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1259 14dbbf48 2022-04-10 stsp struct got_commit_object *commit = NULL;
1260 70f8f24d 2022-04-14 stsp const struct got_object_id_queue *parents;
1261 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1262 70f8f24d 2022-04-14 stsp int nqueued = nids, nskip = 0;
1264 70f8f24d 2022-04-14 stsp while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1265 70f8f24d 2022-04-14 stsp int color;
1267 57bc7b6d 2022-04-10 stsp if (cancel_cb) {
1268 57bc7b6d 2022-04-10 stsp err = cancel_cb(cancel_arg);
1273 14dbbf48 2022-04-10 stsp qid = STAILQ_FIRST(ids);
1274 70f8f24d 2022-04-14 stsp STAILQ_REMOVE_HEAD(ids, entry);
1275 70f8f24d 2022-04-14 stsp nqueued--;
1276 70f8f24d 2022-04-14 stsp color = *((int *)qid->data);
1277 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1280 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(skip, &qid->id)) {
1281 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1285 70f8f24d 2022-04-14 stsp switch (color) {
1286 70f8f24d 2022-04-14 stsp case COLOR_KEEP:
1287 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(keep, &qid->id)) {
1288 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1291 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(drop, &qid->id)) {
1292 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1294 70f8f24d 2022-04-14 stsp goto done;
1296 70f8f24d 2022-04-14 stsp (*ncolored)++;
1297 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(keep, &qid->id, NULL);
1299 70f8f24d 2022-04-14 stsp goto done;
1301 70f8f24d 2022-04-14 stsp case COLOR_DROP:
1302 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(drop, &qid->id)) {
1303 70f8f24d 2022-04-14 stsp got_object_qid_free(qid);
1306 d7b5a0e8 2022-04-20 stsp if (got_object_idset_contains(keep, &qid->id)) {
1307 70f8f24d 2022-04-14 stsp err = paint_commit(qid, COLOR_SKIP);
1309 70f8f24d 2022-04-14 stsp goto done;
1311 70f8f24d 2022-04-14 stsp (*ncolored)++;
1312 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(drop, &qid->id, NULL);
1314 70f8f24d 2022-04-14 stsp goto done;
1316 70f8f24d 2022-04-14 stsp case COLOR_SKIP:
1317 d7b5a0e8 2022-04-20 stsp if (!got_object_idset_contains(skip, &qid->id)) {
1318 d7b5a0e8 2022-04-20 stsp err = got_object_idset_add(skip, &qid->id,
1321 70f8f24d 2022-04-14 stsp goto done;
1325 70f8f24d 2022-04-14 stsp /* should not happen */
1326 70f8f24d 2022-04-14 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
1327 70f8f24d 2022-04-14 stsp "%s invalid commit color %d", __func__, color);
1328 70f8f24d 2022-04-14 stsp goto done;
1331 b8af7c06 2022-03-15 stsp err = report_progress(progress_cb, progress_arg, rl,
1332 b8af7c06 2022-03-15 stsp *ncolored, 0, 0, 0L, 0, 0, 0, 0);
1337 d7b5a0e8 2022-04-20 stsp err = got_object_open_as_commit(&commit, repo, &qid->id);
1341 70f8f24d 2022-04-14 stsp parents = got_object_commit_get_parent_ids(commit);
1342 70f8f24d 2022-04-14 stsp if (parents) {
1343 70f8f24d 2022-04-14 stsp struct got_object_qid *pid;
1344 70f8f24d 2022-04-14 stsp color = *((int *)qid->data);
1345 70f8f24d 2022-04-14 stsp STAILQ_FOREACH(pid, parents, entry) {
1346 d7b5a0e8 2022-04-20 stsp err = queue_commit_id(ids, &pid->id, color,
1350 70f8f24d 2022-04-14 stsp nqueued++;
1351 70f8f24d 2022-04-14 stsp if (color == COLOR_SKIP)
1356 70f8f24d 2022-04-14 stsp got_object_commit_close(commit);
1357 70f8f24d 2022-04-14 stsp commit = NULL;
1358 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
1361 14dbbf48 2022-04-10 stsp if (commit)
1362 14dbbf48 2022-04-10 stsp got_object_commit_close(commit);
1363 14dbbf48 2022-04-10 stsp return err;
1366 14dbbf48 2022-04-10 stsp static const struct got_error *
1367 14dbbf48 2022-04-10 stsp findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1368 14dbbf48 2022-04-10 stsp struct got_object_id **head, int nhead,
1369 14dbbf48 2022-04-10 stsp struct got_object_id **tail, int ntail,
1370 14dbbf48 2022-04-10 stsp struct got_repository *repo,
1371 14dbbf48 2022-04-10 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1372 14dbbf48 2022-04-10 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1374 14dbbf48 2022-04-10 stsp const struct got_error *err = NULL;
1375 14dbbf48 2022-04-10 stsp struct got_object_id_queue ids;
1376 70f8f24d 2022-04-14 stsp struct got_object_idset *keep, *drop, *skip = NULL;
1377 14dbbf48 2022-04-10 stsp int i, nkeep;
1379 14dbbf48 2022-04-10 stsp STAILQ_INIT(&ids);
1380 14dbbf48 2022-04-10 stsp *res = NULL;
1381 14dbbf48 2022-04-10 stsp *nres = 0;
1382 14dbbf48 2022-04-10 stsp *ncolored = 0;
1384 14dbbf48 2022-04-10 stsp keep = got_object_idset_alloc();
1385 14dbbf48 2022-04-10 stsp if (keep == NULL)
1386 14dbbf48 2022-04-10 stsp return got_error_from_errno("got_object_idset_alloc");
1388 14dbbf48 2022-04-10 stsp drop = got_object_idset_alloc();
1389 14dbbf48 2022-04-10 stsp if (drop == NULL) {
1390 14dbbf48 2022-04-10 stsp err = got_error_from_errno("got_object_idset_alloc");
1391 14dbbf48 2022-04-10 stsp goto done;
1394 70f8f24d 2022-04-14 stsp skip = got_object_idset_alloc();
1395 70f8f24d 2022-04-14 stsp if (skip == NULL) {
1396 70f8f24d 2022-04-14 stsp err = got_error_from_errno("got_object_idset_alloc");
1397 70f8f24d 2022-04-14 stsp goto done;
1400 14dbbf48 2022-04-10 stsp for (i = 0; i < nhead; i++) {
1401 14dbbf48 2022-04-10 stsp struct got_object_id *id = head[i];
1402 14dbbf48 2022-04-10 stsp if (id == NULL)
1404 fbafdecf 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1406 14dbbf48 2022-04-10 stsp goto done;
1409 14dbbf48 2022-04-10 stsp for (i = 0; i < ntail; i++) {
1410 14dbbf48 2022-04-10 stsp struct got_object_id *id = tail[i];
1411 14dbbf48 2022-04-10 stsp if (id == NULL)
1413 14dbbf48 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1415 14dbbf48 2022-04-10 stsp goto done;
1418 70f8f24d 2022-04-14 stsp err = paint_commits(ncolored, &ids, nhead + ntail,
1419 70f8f24d 2022-04-14 stsp keep, drop, skip, repo, progress_cb, progress_arg, rl,
1420 70f8f24d 2022-04-14 stsp cancel_cb, cancel_arg);
1422 14dbbf48 2022-04-10 stsp goto done;
1424 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1425 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1426 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1427 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1428 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1429 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1430 e6bcace5 2021-06-22 stsp goto done;
1432 70f8f24d 2022-04-14 stsp arg.idx = -1;
1433 70f8f24d 2022-04-14 stsp arg.skip = skip;
1434 70f8f24d 2022-04-14 stsp arg.drop = drop;
1435 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1436 e6bcace5 2021-06-22 stsp if (err) {
1437 e6bcace5 2021-06-22 stsp free(arg.array);
1438 e6bcace5 2021-06-22 stsp goto done;
1440 e6bcace5 2021-06-22 stsp *res = arg.array;
1441 70f8f24d 2022-04-14 stsp *nres = arg.idx + 1;
1444 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1445 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1447 70f8f24d 2022-04-14 stsp got_object_idset_free(skip);
1448 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1449 e6bcace5 2021-06-22 stsp return err;
1452 e6bcace5 2021-06-22 stsp static const struct got_error *
1453 b8af7c06 2022-03-15 stsp load_object_ids(int *ncolored, int *nfound, int *ntrees,
1454 b8af7c06 2022-03-15 stsp struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1455 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1456 d6a28ffe 2022-05-20 op uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1457 d6a28ffe 2022-05-20 op void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1458 d6a28ffe 2022-05-20 op void *cancel_arg)
1460 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1461 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1462 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
1463 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude;
1465 2f8438b0 2022-05-04 stsp idset_exclude = got_object_idset_alloc();
1466 2f8438b0 2022-05-04 stsp if (idset_exclude == NULL)
1467 2f8438b0 2022-05-04 stsp return got_error_from_errno("got_object_idset_alloc");
1469 b8af7c06 2022-03-15 stsp *ncolored = 0;
1470 b8af7c06 2022-03-15 stsp *nfound = 0;
1471 b8af7c06 2022-03-15 stsp *ntrees = 0;
1473 b8af7c06 2022-03-15 stsp err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1474 b8af7c06 2022-03-15 stsp repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1476 e6bcace5 2021-06-22 stsp goto done;
1478 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1479 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
1480 05118f5a 2021-06-22 stsp if (id == NULL)
1482 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1484 05118f5a 2021-06-22 stsp return err;
1485 9d34261e 2022-04-07 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1486 2f8438b0 2022-05-04 stsp err = load_commit(0, idset, idset_exclude, id, repo,
1487 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
1488 9d34261e 2022-04-07 stsp progress_cb, progress_arg, rl,
1489 9d34261e 2022-04-07 stsp cancel_cb, cancel_arg);
1491 07165b17 2021-07-01 stsp goto done;
1492 9d34261e 2022-04-07 stsp } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1493 2f8438b0 2022-05-04 stsp err = load_tag(0, idset, idset_exclude, id, repo,
1494 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
1495 9d34261e 2022-04-07 stsp progress_cb, progress_arg, rl,
1496 9d34261e 2022-04-07 stsp cancel_cb, cancel_arg);
1498 9d34261e 2022-04-07 stsp goto done;
1502 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
1503 d6a28ffe 2022-05-20 op err = load_commit(1, idset, idset_exclude, ids[i], repo,
1504 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
1505 2f8438b0 2022-05-04 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1507 eca70f98 2021-09-03 stsp goto done;
1510 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1511 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1512 67fd6849 2022-02-13 stsp struct got_pack_meta *m;
1513 05118f5a 2021-06-22 stsp if (id == NULL)
1515 67fd6849 2022-02-13 stsp m = got_object_idset_get(idset, id);
1516 67fd6849 2022-02-13 stsp if (m == NULL) {
1517 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1519 07165b17 2021-07-01 stsp goto done;
1521 67fd6849 2022-02-13 stsp obj_type = m->obj_type;
1522 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1524 2f8438b0 2022-05-04 stsp err = load_tag(1, idset, idset_exclude, id, repo,
1525 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
1526 2f8438b0 2022-05-04 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1528 e6bcace5 2021-06-22 stsp goto done;
1531 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
1532 e6bcace5 2021-06-22 stsp free(ids[i]);
1534 e6bcace5 2021-06-22 stsp free(ids);
1535 2f8438b0 2022-05-04 stsp got_object_idset_free(idset_exclude);
1536 e6bcace5 2021-06-22 stsp return err;
1539 e6bcace5 2021-06-22 stsp const struct got_error *
1540 2d9e6abf 2022-05-04 stsp hwrite(FILE *f, void *buf, off_t len, SHA1_CTX *ctx)
1544 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1545 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
1546 e6bcace5 2021-06-22 stsp if (n != len)
1547 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
1548 2d9e6abf 2022-05-04 stsp return NULL;
1551 2d9e6abf 2022-05-04 stsp const struct got_error *
1552 2d9e6abf 2022-05-04 stsp hcopy(FILE *fsrc, FILE *fdst, off_t len, SHA1_CTX *ctx)
1554 2d9e6abf 2022-05-04 stsp unsigned char buf[65536];
1555 2d9e6abf 2022-05-04 stsp off_t remain = len;
1558 2d9e6abf 2022-05-04 stsp while (remain > 0) {
1559 2d9e6abf 2022-05-04 stsp size_t copylen = MIN(sizeof(buf), remain);
1560 2d9e6abf 2022-05-04 stsp n = fread(buf, 1, copylen, fsrc);
1561 2d9e6abf 2022-05-04 stsp if (n != copylen)
1562 2d9e6abf 2022-05-04 stsp return got_ferror(fsrc, GOT_ERR_IO);
1563 2d9e6abf 2022-05-04 stsp SHA1Update(ctx, buf, copylen);
1564 2d9e6abf 2022-05-04 stsp n = fwrite(buf, 1, copylen, fdst);
1565 2d9e6abf 2022-05-04 stsp if (n != copylen)
1566 2d9e6abf 2022-05-04 stsp return got_ferror(fdst, GOT_ERR_IO);
1567 2d9e6abf 2022-05-04 stsp remain -= copylen;
1570 e6bcace5 2021-06-22 stsp return NULL;
1573 e93fb944 2022-05-10 stsp const struct got_error *
1574 e93fb944 2022-05-10 stsp hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1575 e93fb944 2022-05-10 stsp FILE *fdst, off_t len, SHA1_CTX *ctx)
1579 e93fb944 2022-05-10 stsp if (src_offset + len > src_size)
1580 e93fb944 2022-05-10 stsp return got_error(GOT_ERR_RANGE);
1582 e93fb944 2022-05-10 stsp SHA1Update(ctx, src + src_offset, len);
1583 e93fb944 2022-05-10 stsp n = fwrite(src + src_offset, 1, len, fdst);
1584 e93fb944 2022-05-10 stsp if (n != len)
1585 e93fb944 2022-05-10 stsp return got_ferror(fdst, GOT_ERR_IO);
1587 e93fb944 2022-05-10 stsp return NULL;
1590 e6bcace5 2021-06-22 stsp static void
1591 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1593 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1594 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1595 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1596 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1599 e6bcace5 2021-06-22 stsp static int
1600 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1602 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1604 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1605 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1606 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1607 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1608 611e8e31 2022-05-01 stsp if (bhd->mtime < ahd->mtime)
1609 611e8e31 2022-05-01 stsp return -1;
1610 611e8e31 2022-05-01 stsp if (bhd->mtime > ahd->mtime)
1612 611e8e31 2022-05-01 stsp if (bhd < ahd)
1613 611e8e31 2022-05-01 stsp return -1;
1614 611e8e31 2022-05-01 stsp if (bhd > ahd)
1616 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1617 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1618 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
1619 611e8e31 2022-05-01 stsp return -1;
1620 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
1622 611e8e31 2022-05-01 stsp return got_object_id_cmp(&a->id, &b->id);
1625 67fd6849 2022-02-13 stsp static int
1626 67fd6849 2022-02-13 stsp reuse_write_order_cmp(const void *pa, const void *pb)
1628 67fd6849 2022-02-13 stsp struct got_pack_meta *a, *b;
1630 67fd6849 2022-02-13 stsp a = *(struct got_pack_meta **)pa;
1631 67fd6849 2022-02-13 stsp b = *(struct got_pack_meta **)pb;
1633 67fd6849 2022-02-13 stsp if (a->reused_delta_offset < b->reused_delta_offset)
1634 67fd6849 2022-02-13 stsp return -1;
1635 67fd6849 2022-02-13 stsp if (a->reused_delta_offset > b->reused_delta_offset)
1640 e6bcace5 2021-06-22 stsp static const struct got_error *
1641 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1645 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1647 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1648 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1649 e6bcace5 2021-06-22 stsp len >>= 4;
1650 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1651 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1652 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1653 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1654 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1655 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1658 e6bcace5 2021-06-22 stsp *hdrlen = i;
1659 e6bcace5 2021-06-22 stsp return NULL;
1662 e6bcace5 2021-06-22 stsp static int
1663 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1666 e6bcace5 2021-06-22 stsp char rbuf[8];
1668 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1669 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1670 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1671 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1675 e6bcace5 2021-06-22 stsp while (i > 0)
1676 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1680 e6bcace5 2021-06-22 stsp static const struct got_error *
1681 5060d5a1 2022-01-10 stsp deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1682 67fd6849 2022-02-13 stsp struct got_pack_meta *m)
1684 5060d5a1 2022-01-10 stsp const struct got_error *err;
1685 5060d5a1 2022-01-10 stsp char buf[32];
1688 67fd6849 2022-02-13 stsp if (m->prev->off != 0) {
1689 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1690 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1692 5060d5a1 2022-01-10 stsp return err;
1693 5060d5a1 2022-01-10 stsp nh += packoff(buf + nh, m->off - m->prev->off);
1694 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1696 5060d5a1 2022-01-10 stsp return err;
1697 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1699 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1700 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1702 5060d5a1 2022-01-10 stsp return err;
1703 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1705 5060d5a1 2022-01-10 stsp return err;
1706 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1707 5060d5a1 2022-01-10 stsp err = hwrite(packfile, m->prev->id.sha1,
1708 5060d5a1 2022-01-10 stsp sizeof(m->prev->id.sha1), ctx);
1710 5060d5a1 2022-01-10 stsp return err;
1711 5060d5a1 2022-01-10 stsp *packfile_size += sizeof(m->prev->id.sha1);
1714 5060d5a1 2022-01-10 stsp return NULL;
1717 5060d5a1 2022-01-10 stsp static const struct got_error *
1718 67fd6849 2022-02-13 stsp write_packed_object(off_t *packfile_size, FILE *packfile,
1719 e93fb944 2022-05-10 stsp FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1720 e93fb944 2022-05-10 stsp struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
1721 e93fb944 2022-05-10 stsp struct got_repository *repo)
1723 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
1724 67fd6849 2022-02-13 stsp struct got_deflate_checksum csum;
1725 67fd6849 2022-02-13 stsp char buf[32];
1727 67fd6849 2022-02-13 stsp struct got_raw_object *raw = NULL;
1728 67fd6849 2022-02-13 stsp off_t outlen;
1730 67fd6849 2022-02-13 stsp csum.output_sha1 = ctx;
1731 67fd6849 2022-02-13 stsp csum.output_crc = NULL;
1733 67fd6849 2022-02-13 stsp m->off = ftello(packfile);
1734 67fd6849 2022-02-13 stsp if (m->delta_len == 0) {
1735 67fd6849 2022-02-13 stsp err = got_object_raw_open(&raw, outfd, repo, &m->id);
1737 67fd6849 2022-02-13 stsp goto done;
1738 67fd6849 2022-02-13 stsp err = packhdr(&nh, buf, sizeof(buf),
1739 67fd6849 2022-02-13 stsp m->obj_type, raw->size);
1741 67fd6849 2022-02-13 stsp goto done;
1742 67fd6849 2022-02-13 stsp err = hwrite(packfile, buf, nh, ctx);
1744 67fd6849 2022-02-13 stsp goto done;
1745 67fd6849 2022-02-13 stsp *packfile_size += nh;
1746 67fd6849 2022-02-13 stsp if (raw->f == NULL) {
1747 67fd6849 2022-02-13 stsp err = got_deflate_to_file_mmap(&outlen,
1748 67fd6849 2022-02-13 stsp raw->data + raw->hdrlen, 0, raw->size,
1749 67fd6849 2022-02-13 stsp packfile, &csum);
1751 67fd6849 2022-02-13 stsp goto done;
1753 67fd6849 2022-02-13 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1755 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1756 67fd6849 2022-02-13 stsp goto done;
1758 67fd6849 2022-02-13 stsp err = got_deflate_to_file(&outlen, raw->f,
1759 67fd6849 2022-02-13 stsp raw->size, packfile, &csum);
1761 67fd6849 2022-02-13 stsp goto done;
1763 67fd6849 2022-02-13 stsp *packfile_size += outlen;
1764 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
1765 67fd6849 2022-02-13 stsp raw = NULL;
1766 67fd6849 2022-02-13 stsp } else if (m->delta_buf) {
1767 67fd6849 2022-02-13 stsp err = deltahdr(packfile_size, ctx, packfile, m);
1769 67fd6849 2022-02-13 stsp goto done;
1770 2d9e6abf 2022-05-04 stsp err = hwrite(packfile, m->delta_buf,
1771 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
1773 67fd6849 2022-02-13 stsp goto done;
1774 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
1775 67fd6849 2022-02-13 stsp free(m->delta_buf);
1776 67fd6849 2022-02-13 stsp m->delta_buf = NULL;
1777 e93fb944 2022-05-10 stsp } else if (delta_cache_map) {
1778 e93fb944 2022-05-10 stsp err = deltahdr(packfile_size, ctx, packfile, m);
1780 e93fb944 2022-05-10 stsp goto done;
1781 e93fb944 2022-05-10 stsp err = hcopy_mmap(delta_cache_map, m->delta_offset,
1782 e93fb944 2022-05-10 stsp delta_cache_size, packfile, m->delta_compressed_len,
1785 e93fb944 2022-05-10 stsp goto done;
1786 e93fb944 2022-05-10 stsp *packfile_size += m->delta_compressed_len;
1788 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1790 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1791 67fd6849 2022-02-13 stsp goto done;
1793 67fd6849 2022-02-13 stsp err = deltahdr(packfile_size, ctx, packfile, m);
1795 67fd6849 2022-02-13 stsp goto done;
1796 2d9e6abf 2022-05-04 stsp err = hcopy(delta_cache, packfile,
1797 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
1799 67fd6849 2022-02-13 stsp goto done;
1800 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
1804 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
1805 67fd6849 2022-02-13 stsp return err;
1808 67fd6849 2022-02-13 stsp static const struct got_error *
1809 74881701 2021-10-15 stsp genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1810 67fd6849 2022-02-13 stsp struct got_pack_meta **deltify, int ndeltify,
1811 67fd6849 2022-02-13 stsp struct got_pack_meta **reuse, int nreuse,
1812 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees, int nours,
1813 b8af7c06 2022-03-15 stsp struct got_repository *repo,
1814 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1815 211cfef0 2022-01-05 stsp struct got_ratelimit *rl,
1816 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1818 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1820 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1821 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1822 08347b73 2021-10-14 stsp char buf[32];
1824 67fd6849 2022-02-13 stsp off_t packfile_size = 0;
1825 cc7a354a 2021-10-15 stsp int outfd = -1;
1826 e93fb944 2022-05-10 stsp int delta_cache_fd = -1;
1827 e93fb944 2022-05-10 stsp uint8_t *delta_cache_map = NULL;
1828 e93fb944 2022-05-10 stsp size_t delta_cache_size = 0;
1830 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1832 e93fb944 2022-05-10 stsp #ifndef GOT_PACK_NO_MMAP
1833 e93fb944 2022-05-10 stsp delta_cache_fd = dup(fileno(delta_cache));
1834 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1) {
1835 e93fb944 2022-05-10 stsp struct stat sb;
1836 e93fb944 2022-05-10 stsp if (fstat(delta_cache_fd, &sb) == -1) {
1837 e93fb944 2022-05-10 stsp err = got_error_from_errno("fstat");
1838 e93fb944 2022-05-10 stsp goto done;
1840 e93fb944 2022-05-10 stsp if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1841 e93fb944 2022-05-10 stsp delta_cache_map = mmap(NULL, sb.st_size,
1842 e93fb944 2022-05-10 stsp PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1843 e93fb944 2022-05-10 stsp if (delta_cache_map == MAP_FAILED) {
1844 e93fb944 2022-05-10 stsp if (errno != ENOMEM) {
1845 e93fb944 2022-05-10 stsp err = got_error_from_errno("mmap");
1846 e93fb944 2022-05-10 stsp goto done;
1848 e93fb944 2022-05-10 stsp delta_cache_map = NULL; /* fallback on stdio */
1850 e93fb944 2022-05-10 stsp delta_cache_size = (size_t)sb.st_size;
1854 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1856 e93fb944 2022-05-10 stsp goto done;
1857 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1858 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1860 e6bcace5 2021-06-22 stsp goto done;
1861 67fd6849 2022-02-13 stsp putbe32(buf, ndeltify + nreuse);
1862 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1864 e6bcace5 2021-06-22 stsp goto done;
1866 67fd6849 2022-02-13 stsp qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1867 67fd6849 2022-02-13 stsp write_order_cmp);
1868 67fd6849 2022-02-13 stsp for (i = 0; i < ndeltify; i++) {
1869 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1870 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
1871 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, i);
1873 211cfef0 2022-01-05 stsp goto done;
1874 67fd6849 2022-02-13 stsp m = deltify[i];
1875 67fd6849 2022-02-13 stsp err = write_packed_object(&packfile_size, packfile,
1876 e93fb944 2022-05-10 stsp delta_cache, delta_cache_map, delta_cache_size,
1877 e93fb944 2022-05-10 stsp m, &outfd, &ctx, repo);
1879 67fd6849 2022-02-13 stsp goto done;
1882 67fd6849 2022-02-13 stsp qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1883 67fd6849 2022-02-13 stsp reuse_write_order_cmp);
1884 67fd6849 2022-02-13 stsp for (i = 0; i < nreuse; i++) {
1885 67fd6849 2022-02-13 stsp err = report_progress(progress_cb, progress_arg, rl,
1886 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
1887 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1889 67fd6849 2022-02-13 stsp goto done;
1890 67fd6849 2022-02-13 stsp m = reuse[i];
1891 67fd6849 2022-02-13 stsp err = write_packed_object(&packfile_size, packfile,
1892 e93fb944 2022-05-10 stsp delta_cache, delta_cache_map, delta_cache_size,
1893 e93fb944 2022-05-10 stsp m, &outfd, &ctx, repo);
1895 67fd6849 2022-02-13 stsp goto done;
1898 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1899 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1900 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1901 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1902 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1903 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1904 211cfef0 2022-01-05 stsp if (progress_cb) {
1905 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1906 b8af7c06 2022-03-15 stsp packfile_size, nours, ndeltify + nreuse,
1907 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse);
1909 211cfef0 2022-01-05 stsp goto done;
1912 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
1913 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
1914 e93fb944 2022-05-10 stsp if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1915 e93fb944 2022-05-10 stsp err = got_error_from_errno("munmap");
1916 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1917 e93fb944 2022-05-10 stsp err = got_error_from_errno("close");
1918 e6bcace5 2021-06-22 stsp return err;
1921 67fd6849 2022-02-13 stsp static const struct got_error *
1922 67fd6849 2022-02-13 stsp add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1924 67fd6849 2022-02-13 stsp struct got_pack_meta *m = data;
1925 67fd6849 2022-02-13 stsp struct got_pack_metavec *v = arg;
1927 411cbec1 2022-05-20 stsp if (m->reused_delta_offset != 0)
1928 f5e78e05 2022-05-04 stsp return NULL;
1930 67fd6849 2022-02-13 stsp return add_meta(m, v);
1933 e6bcace5 2021-06-22 stsp const struct got_error *
1934 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1935 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1936 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1937 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1938 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1939 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1941 e6bcace5 2021-06-22 stsp const struct got_error *err;
1942 67fd6849 2022-02-13 stsp int delta_cache_fd = -1;
1943 74881701 2021-10-15 stsp FILE *delta_cache = NULL;
1944 67fd6849 2022-02-13 stsp struct got_object_idset *idset;
1945 211cfef0 2022-01-05 stsp struct got_ratelimit rl;
1946 67fd6849 2022-02-13 stsp struct got_pack_metavec deltify, reuse;
1947 b8af7c06 2022-03-15 stsp int ncolored = 0, nfound = 0, ntrees = 0;
1948 f5e78e05 2022-05-04 stsp size_t ndeltify;
1949 d6a28ffe 2022-05-20 op uint32_t seed;
1951 d6a28ffe 2022-05-20 op seed = arc4random();
1953 67fd6849 2022-02-13 stsp memset(&deltify, 0, sizeof(deltify));
1954 67fd6849 2022-02-13 stsp memset(&reuse, 0, sizeof(reuse));
1956 211cfef0 2022-01-05 stsp got_ratelimit_init(&rl, 0, 500);
1958 67fd6849 2022-02-13 stsp idset = got_object_idset_alloc();
1959 67fd6849 2022-02-13 stsp if (idset == NULL)
1960 67fd6849 2022-02-13 stsp return got_error_from_errno("got_object_idset_alloc");
1962 b8af7c06 2022-03-15 stsp err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1963 d6a28ffe 2022-05-20 op ntheirs, ours, nours, repo, seed, loose_obj_only,
1964 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1966 17259bfa 2022-05-19 stsp goto done;
1968 28526235 2022-02-13 stsp if (progress_cb) {
1969 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1970 b8af7c06 2022-03-15 stsp 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1972 28526235 2022-02-13 stsp goto done;
1975 67fd6849 2022-02-13 stsp if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1976 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1977 05118f5a 2021-06-22 stsp goto done;
1980 67fd6849 2022-02-13 stsp delta_cache_fd = got_opentempfd();
1981 67fd6849 2022-02-13 stsp if (delta_cache_fd == -1) {
1982 74881701 2021-10-15 stsp err = got_error_from_errno("got_opentemp");
1983 74881701 2021-10-15 stsp goto done;
1986 67fd6849 2022-02-13 stsp reuse.metasz = 64;
1987 67fd6849 2022-02-13 stsp reuse.meta = calloc(reuse.metasz,
1988 67fd6849 2022-02-13 stsp sizeof(struct got_pack_meta *));
1989 67fd6849 2022-02-13 stsp if (reuse.meta == NULL) {
1990 67fd6849 2022-02-13 stsp err = got_error_from_errno("calloc");
1991 67fd6849 2022-02-13 stsp goto done;
1994 b8af7c06 2022-03-15 stsp err = search_deltas(&reuse, idset, delta_cache_fd, ncolored, nfound,
1995 b8af7c06 2022-03-15 stsp ntrees, nours, repo, progress_cb, progress_arg, &rl,
1996 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1998 67fd6849 2022-02-13 stsp goto done;
2000 67fd6849 2022-02-13 stsp delta_cache = fdopen(delta_cache_fd, "a+");
2001 67fd6849 2022-02-13 stsp if (delta_cache == NULL) {
2002 67fd6849 2022-02-13 stsp err = got_error_from_errno("fdopen");
2003 67fd6849 2022-02-13 stsp goto done;
2005 67fd6849 2022-02-13 stsp delta_cache_fd = -1;
2007 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
2008 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
2009 67fd6849 2022-02-13 stsp goto done;
2012 f5e78e05 2022-05-04 stsp ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
2013 f5e78e05 2022-05-04 stsp if (ndeltify > 0) {
2014 f5e78e05 2022-05-04 stsp deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
2015 f5e78e05 2022-05-04 stsp if (deltify.meta == NULL) {
2016 f5e78e05 2022-05-04 stsp err = got_error_from_errno("calloc");
2017 f5e78e05 2022-05-04 stsp goto done;
2019 f5e78e05 2022-05-04 stsp deltify.metasz = ndeltify;
2021 f5e78e05 2022-05-04 stsp err = got_object_idset_for_each(idset, add_meta_idset_cb,
2022 f5e78e05 2022-05-04 stsp &deltify);
2024 f8a36e22 2021-08-26 stsp goto done;
2025 f5e78e05 2022-05-04 stsp if (deltify.nmeta > 0) {
2026 f5e78e05 2022-05-04 stsp err = pick_deltas(deltify.meta, deltify.nmeta,
2027 f5e78e05 2022-05-04 stsp ncolored, nfound, ntrees, nours, reuse.nmeta,
2028 f5e78e05 2022-05-04 stsp delta_cache, repo, progress_cb, progress_arg, &rl,
2029 f5e78e05 2022-05-04 stsp cancel_cb, cancel_arg);
2031 f5e78e05 2022-05-04 stsp goto done;
2035 2d9e6abf 2022-05-04 stsp if (fflush(delta_cache) == EOF) {
2036 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fflush");
2037 2d9e6abf 2022-05-04 stsp goto done;
2039 67fd6849 2022-02-13 stsp err = genpack(packsha1, packfile, delta_cache, deltify.meta,
2040 b8af7c06 2022-03-15 stsp deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
2041 b8af7c06 2022-03-15 stsp nours, repo, progress_cb, progress_arg, &rl,
2042 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
2044 e6bcace5 2021-06-22 stsp goto done;
2046 67fd6849 2022-02-13 stsp free_nmeta(deltify.meta, deltify.nmeta);
2047 67fd6849 2022-02-13 stsp free_nmeta(reuse.meta, reuse.nmeta);
2048 67fd6849 2022-02-13 stsp got_object_idset_free(idset);
2049 67fd6849 2022-02-13 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
2050 67fd6849 2022-02-13 stsp err = got_error_from_errno("close");
2051 74881701 2021-10-15 stsp if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
2052 74881701 2021-10-15 stsp err = got_error_from_errno("fclose");
2053 e6bcace5 2021-06-22 stsp return err;