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 756050ac 2022-07-19 op #include <inttypes.h>
31 13b2bc37 2022-10-23 stsp #include <poll.h>
32 e6bcace5 2021-06-22 stsp #include <stdio.h>
33 e6bcace5 2021-06-22 stsp #include <stdlib.h>
34 e6bcace5 2021-06-22 stsp #include <string.h>
35 e6bcace5 2021-06-22 stsp #include <sha1.h>
36 211cfef0 2022-01-05 stsp #include <time.h>
37 bfc73a47 2022-03-19 naddy #include <unistd.h>
38 e6bcace5 2021-06-22 stsp #include <limits.h>
39 e6bcace5 2021-06-22 stsp #include <zlib.h>
41 e6bcace5 2021-06-22 stsp #include "got_error.h"
42 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
43 e6bcace5 2021-06-22 stsp #include "got_object.h"
44 f8b19efd 2021-10-13 stsp #include "got_path.h"
45 05118f5a 2021-06-22 stsp #include "got_reference.h"
46 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
50 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
51 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
52 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
53 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
54 cae60ab8 2022-10-18 stsp #include "got_lib_ratelimit.h"
55 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
56 17cfdba6 2022-05-20 op #include "got_lib_pack_create.h"
57 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
58 2d9e6abf 2022-05-04 stsp #include "got_lib_inflate.h"
59 13b2bc37 2022-10-23 stsp #include "got_lib_poll.h"
61 f8174ca5 2022-05-20 stsp #include "murmurhash2.h"
64 74881701 2021-10-15 stsp #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
68 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
71 70f8f24d 2022-04-14 stsp #ifndef nitems
72 70f8f24d 2022-04-14 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
75 e6bcace5 2021-06-22 stsp static const struct got_error *
76 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
77 d6a28ffe 2022-05-20 op const char *path, int obj_type, time_t mtime, uint32_t seed)
79 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
81 e6bcace5 2021-06-22 stsp *new = NULL;
83 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
84 e6bcace5 2021-06-22 stsp if (m == NULL)
85 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
87 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
89 d6a28ffe 2022-05-20 op m->path_hash = murmurhash2(path, strlen(path), seed);
90 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
91 e6bcace5 2021-06-22 stsp m->mtime = mtime;
93 e6bcace5 2021-06-22 stsp return NULL;
97 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
99 e6bcace5 2021-06-22 stsp if (meta == NULL)
101 f8174ca5 2022-05-20 stsp meta->path_hash = 0;
102 5060d5a1 2022-01-10 stsp free(meta->delta_buf);
103 5060d5a1 2022-01-10 stsp meta->delta_buf = NULL;
104 67fd6849 2022-02-13 stsp free(meta->base_obj_id);
105 67fd6849 2022-02-13 stsp meta->base_obj_id = NULL;
106 411cbec1 2022-05-20 stsp meta->reused_delta_offset = 0;
109 e6bcace5 2021-06-22 stsp static void
110 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
114 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
115 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
116 e6bcace5 2021-06-22 stsp free(meta);
120 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
122 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
124 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
125 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
127 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
128 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
129 f8174ca5 2022-05-20 stsp if (a->path_hash < b->path_hash)
131 f8174ca5 2022-05-20 stsp if (a->path_hash > b->path_hash)
133 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
135 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
137 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
140 5060d5a1 2022-01-10 stsp static off_t
141 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
144 5060d5a1 2022-01-10 stsp off_t size = 32;
145 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
146 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
147 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
149 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
151 e6bcace5 2021-06-22 stsp return size;
154 5060d5a1 2022-01-10 stsp static const struct got_error *
155 5060d5a1 2022-01-10 stsp append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
159 5060d5a1 2022-01-10 stsp if (*len + nseg >= *sz) {
160 5060d5a1 2022-01-10 stsp while (*len + nseg >= *sz)
161 5060d5a1 2022-01-10 stsp *sz += *sz / 2;
162 5060d5a1 2022-01-10 stsp n = realloc(*p, *sz);
163 5060d5a1 2022-01-10 stsp if (n == NULL)
164 5060d5a1 2022-01-10 stsp return got_error_from_errno("realloc");
167 5060d5a1 2022-01-10 stsp memcpy(*p + *len, seg, nseg);
168 5060d5a1 2022-01-10 stsp *len += nseg;
169 5060d5a1 2022-01-10 stsp return NULL;
172 5060d5a1 2022-01-10 stsp static const struct got_error *
173 5060d5a1 2022-01-10 stsp encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
174 5060d5a1 2022-01-10 stsp struct got_delta_instruction *deltas, int ndeltas,
175 5060d5a1 2022-01-10 stsp off_t delta_size, off_t base_size)
177 5060d5a1 2022-01-10 stsp const struct got_error *err;
178 5060d5a1 2022-01-10 stsp unsigned char buf[16], *bp;
180 2d9e6abf 2022-05-04 stsp size_t len = 0, compressed_len;
181 2d9e6abf 2022-05-04 stsp off_t bufsize = delta_size;
183 5060d5a1 2022-01-10 stsp struct got_delta_instruction *d;
184 2d9e6abf 2022-05-04 stsp uint8_t *delta_buf;
186 2d9e6abf 2022-05-04 stsp delta_buf = malloc(bufsize);
187 2d9e6abf 2022-05-04 stsp if (delta_buf == NULL)
188 2d9e6abf 2022-05-04 stsp return got_error_from_errno("malloc");
190 5060d5a1 2022-01-10 stsp /* base object size */
191 5060d5a1 2022-01-10 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
192 5060d5a1 2022-01-10 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
193 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
194 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
195 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
196 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
198 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
202 5060d5a1 2022-01-10 stsp /* target object size */
203 5060d5a1 2022-01-10 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
204 5060d5a1 2022-01-10 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
205 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
206 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
207 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
208 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
210 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize, buf, i);
214 5060d5a1 2022-01-10 stsp for (j = 0; j < ndeltas; j++) {
215 5060d5a1 2022-01-10 stsp d = &deltas[j];
216 5060d5a1 2022-01-10 stsp if (d->copy) {
217 5060d5a1 2022-01-10 stsp n = d->offset;
218 5060d5a1 2022-01-10 stsp bp = &buf[1];
219 5060d5a1 2022-01-10 stsp buf[0] = GOT_DELTA_BASE_COPY;
220 5060d5a1 2022-01-10 stsp for (i = 0; i < 4; i++) {
221 5060d5a1 2022-01-10 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
222 5060d5a1 2022-01-10 stsp buf[0] |= 1 << i;
223 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
225 5060d5a1 2022-01-10 stsp if (n == 0)
229 5060d5a1 2022-01-10 stsp n = d->len;
230 5060d5a1 2022-01-10 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
231 5060d5a1 2022-01-10 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
232 5060d5a1 2022-01-10 stsp for (i = 0; i < 3 && n > 0; i++) {
233 5060d5a1 2022-01-10 stsp buf[0] |= 1 << (i + 4);
234 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
238 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
239 5060d5a1 2022-01-10 stsp buf, bp - buf);
242 5060d5a1 2022-01-10 stsp } else if (o->f == NULL) {
244 5060d5a1 2022-01-10 stsp while (n != d->len) {
245 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
246 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
250 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
251 5060d5a1 2022-01-10 stsp o->data + o->hdrlen + d->offset + n,
255 5060d5a1 2022-01-10 stsp n += buf[0];
258 5060d5a1 2022-01-10 stsp char content[128];
260 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
261 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
265 5060d5a1 2022-01-10 stsp while (n != d->len) {
266 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
267 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
271 5060d5a1 2022-01-10 stsp r = fread(content, 1, buf[0], o->f);
272 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
273 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
276 2d9e6abf 2022-05-04 stsp err = append(&delta_buf, &len, &bufsize,
277 5060d5a1 2022-01-10 stsp content, buf[0]);
280 5060d5a1 2022-01-10 stsp n += buf[0];
285 2d9e6abf 2022-05-04 stsp err = got_deflate_to_mem_mmap(&m->delta_buf, &compressed_len,
286 2d9e6abf 2022-05-04 stsp NULL, NULL, delta_buf, 0, len);
290 5060d5a1 2022-01-10 stsp m->delta_len = len;
291 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
293 2d9e6abf 2022-05-04 stsp free(delta_buf);
294 2d9e6abf 2022-05-04 stsp return err;
297 74881701 2021-10-15 stsp static const struct got_error *
298 74881701 2021-10-15 stsp encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
299 74881701 2021-10-15 stsp struct got_delta_instruction *deltas, int ndeltas,
300 a319ca8c 2021-10-15 stsp off_t base_size, FILE *f)
302 2d9e6abf 2022-05-04 stsp const struct got_error *err;
303 a319ca8c 2021-10-15 stsp unsigned char buf[16], *bp;
306 2d9e6abf 2022-05-04 stsp struct got_deflate_buf zb;
307 a319ca8c 2021-10-15 stsp struct got_delta_instruction *d;
308 2d9e6abf 2022-05-04 stsp off_t delta_len = 0, compressed_len = 0;
310 2d9e6abf 2022-05-04 stsp err = got_deflate_init(&zb, NULL, GOT_DEFLATE_BUFSIZE);
312 2d9e6abf 2022-05-04 stsp return err;
314 a319ca8c 2021-10-15 stsp /* base object size */
315 a319ca8c 2021-10-15 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
316 a319ca8c 2021-10-15 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
317 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
318 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
319 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
320 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
323 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
324 2d9e6abf 2022-05-04 stsp buf, 0, i, f, NULL);
327 2d9e6abf 2022-05-04 stsp delta_len += i;
329 a319ca8c 2021-10-15 stsp /* target object size */
330 a319ca8c 2021-10-15 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
331 a319ca8c 2021-10-15 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
332 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
333 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
334 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
335 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
338 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb, &compressed_len,
339 2d9e6abf 2022-05-04 stsp buf, 0, i, f, NULL);
342 2d9e6abf 2022-05-04 stsp delta_len += i;
344 a319ca8c 2021-10-15 stsp for (j = 0; j < ndeltas; j++) {
345 a319ca8c 2021-10-15 stsp d = &deltas[j];
346 a319ca8c 2021-10-15 stsp if (d->copy) {
347 a319ca8c 2021-10-15 stsp n = d->offset;
348 a319ca8c 2021-10-15 stsp bp = &buf[1];
349 a319ca8c 2021-10-15 stsp buf[0] = GOT_DELTA_BASE_COPY;
350 a319ca8c 2021-10-15 stsp for (i = 0; i < 4; i++) {
351 a319ca8c 2021-10-15 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
352 a319ca8c 2021-10-15 stsp buf[0] |= 1 << i;
353 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
355 a319ca8c 2021-10-15 stsp if (n == 0)
358 a319ca8c 2021-10-15 stsp n = d->len;
359 a319ca8c 2021-10-15 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
360 a319ca8c 2021-10-15 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
361 a319ca8c 2021-10-15 stsp for (i = 0; i < 3 && n > 0; i++) {
362 a319ca8c 2021-10-15 stsp buf[0] |= 1 << (i + 4);
363 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
367 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
368 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, bp - buf, f, NULL);
371 2d9e6abf 2022-05-04 stsp delta_len += (bp - buf);
372 64a8571e 2022-01-07 stsp } else if (o->f == NULL) {
374 64a8571e 2022-01-07 stsp while (n != d->len) {
375 64a8571e 2022-01-07 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
376 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
377 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
380 2d9e6abf 2022-05-04 stsp delta_len++;
381 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
382 2d9e6abf 2022-05-04 stsp &compressed_len,
383 2d9e6abf 2022-05-04 stsp o->data + o->hdrlen + d->offset + n, 0,
384 2d9e6abf 2022-05-04 stsp buf[0], f, NULL);
387 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
388 64a8571e 2022-01-07 stsp n += buf[0];
391 a319ca8c 2021-10-15 stsp char content[128];
393 2d9e6abf 2022-05-04 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1) {
394 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fseeko");
398 a319ca8c 2021-10-15 stsp while (n != d->len) {
399 a319ca8c 2021-10-15 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
400 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
401 2d9e6abf 2022-05-04 stsp &compressed_len, buf, 0, 1, f, NULL);
404 2d9e6abf 2022-05-04 stsp delta_len++;
405 a319ca8c 2021-10-15 stsp r = fread(content, 1, buf[0], o->f);
406 2d9e6abf 2022-05-04 stsp if (r != buf[0]) {
407 2d9e6abf 2022-05-04 stsp err = got_ferror(o->f, GOT_ERR_IO);
410 2d9e6abf 2022-05-04 stsp err = got_deflate_append_to_file_mmap(&zb,
411 2d9e6abf 2022-05-04 stsp &compressed_len, content, 0, buf[0], f,
415 2d9e6abf 2022-05-04 stsp delta_len += buf[0];
416 a319ca8c 2021-10-15 stsp n += buf[0];
421 2d9e6abf 2022-05-04 stsp err = got_deflate_flush(&zb, f, NULL, &compressed_len);
425 2d9e6abf 2022-05-04 stsp /* sanity check */
426 2d9e6abf 2022-05-04 stsp if (compressed_len != ftello(f) - m->delta_offset) {
427 2d9e6abf 2022-05-04 stsp err = got_error(GOT_ERR_COMPRESSION);
431 2d9e6abf 2022-05-04 stsp m->delta_len = delta_len;
432 2d9e6abf 2022-05-04 stsp m->delta_compressed_len = compressed_len;
434 2d9e6abf 2022-05-04 stsp got_deflate_end(&zb);
435 2d9e6abf 2022-05-04 stsp return err;
438 301e83b3 2022-10-16 stsp const struct got_error *
439 301e83b3 2022-10-16 stsp got_pack_report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
440 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, int ncolored, int nfound, int ntrees,
441 b8af7c06 2022-03-15 stsp off_t packfile_size, int ncommits, int nobj_total, int obj_deltify,
442 b8af7c06 2022-03-15 stsp int nobj_written)
444 211cfef0 2022-01-05 stsp const struct got_error *err;
445 211cfef0 2022-01-05 stsp int elapsed;
447 211cfef0 2022-01-05 stsp if (progress_cb == NULL)
448 211cfef0 2022-01-05 stsp return NULL;
450 211cfef0 2022-01-05 stsp err = got_ratelimit_check(&elapsed, rl);
451 211cfef0 2022-01-05 stsp if (err || !elapsed)
452 211cfef0 2022-01-05 stsp return err;
454 b8af7c06 2022-03-15 stsp return progress_cb(progress_arg, ncolored, nfound, ntrees,
455 b8af7c06 2022-03-15 stsp packfile_size, ncommits, nobj_total, obj_deltify, nobj_written);
458 301e83b3 2022-10-16 stsp const struct got_error *
459 301e83b3 2022-10-16 stsp got_pack_add_meta(struct got_pack_meta *m, struct got_pack_metavec *v)
461 67fd6849 2022-02-13 stsp if (v->nmeta == v->metasz){
462 67fd6849 2022-02-13 stsp size_t newsize = 2 * v->metasz;
463 67fd6849 2022-02-13 stsp struct got_pack_meta **new;
464 67fd6849 2022-02-13 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
465 67fd6849 2022-02-13 stsp if (new == NULL)
466 67fd6849 2022-02-13 stsp return got_error_from_errno("reallocarray");
467 67fd6849 2022-02-13 stsp v->meta = new;
468 5e91dae4 2022-08-30 stsp v->metasz = newsize;
471 67fd6849 2022-02-13 stsp v->meta[v->nmeta++] = m;
472 67fd6849 2022-02-13 stsp return NULL;
475 301e83b3 2022-10-16 stsp const struct got_error *
476 301e83b3 2022-10-16 stsp got_pack_find_pack_for_reuse(struct got_packidx **best_packidx,
477 67fd6849 2022-02-13 stsp struct got_repository *repo)
479 9b576444 2022-03-14 stsp const struct got_error *err = NULL;
480 67fd6849 2022-02-13 stsp struct got_pathlist_entry *pe;
481 67fd6849 2022-02-13 stsp const char *best_packidx_path = NULL;
482 67fd6849 2022-02-13 stsp int nobj_max = 0;
484 67fd6849 2022-02-13 stsp *best_packidx = NULL;
486 9b576444 2022-03-14 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
487 67fd6849 2022-02-13 stsp const char *path_packidx = pe->path;
488 67fd6849 2022-02-13 stsp struct got_packidx *packidx;
491 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
495 67fd6849 2022-02-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
496 67fd6849 2022-02-13 stsp if (nobj > nobj_max) {
497 67fd6849 2022-02-13 stsp best_packidx_path = path_packidx;
498 67fd6849 2022-02-13 stsp nobj_max = nobj;
502 67fd6849 2022-02-13 stsp if (best_packidx_path) {
503 67fd6849 2022-02-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
507 67fd6849 2022-02-13 stsp return err;
510 301e83b3 2022-10-16 stsp const struct got_error *
511 301e83b3 2022-10-16 stsp got_pack_cache_pack_for_packidx(struct got_pack **pack,
512 301e83b3 2022-10-16 stsp struct got_packidx *packidx, struct got_repository *repo)
514 61af9b21 2022-06-28 stsp const struct got_error *err;
515 3d589bee 2022-06-25 stsp char *path_packfile = NULL;
517 3d589bee 2022-06-25 stsp err = got_packidx_get_packfile_path(&path_packfile,
518 3d589bee 2022-06-25 stsp packidx->path_packidx);
520 3d589bee 2022-06-25 stsp return err;
522 3d589bee 2022-06-25 stsp *pack = got_repo_get_cached_pack(repo, path_packfile);
523 3d589bee 2022-06-25 stsp if (*pack == NULL) {
524 3d589bee 2022-06-25 stsp err = got_repo_cache_pack(pack, repo, path_packfile, packidx);
529 61af9b21 2022-06-28 stsp free(path_packfile);
530 67fd6849 2022-02-13 stsp return err;
533 67fd6849 2022-02-13 stsp static const struct got_error *
534 b8af7c06 2022-03-15 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int ncolored,
535 b8af7c06 2022-03-15 stsp int nfound, int ntrees, int ncommits, int nreused, FILE *delta_cache,
536 b8af7c06 2022-03-15 stsp struct got_repository *repo,
537 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
538 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
540 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
541 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
542 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
543 74881701 2021-10-15 stsp struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
544 5060d5a1 2022-01-10 stsp int i, j, ndeltas, best_ndeltas;
545 5060d5a1 2022-01-10 stsp off_t size, best_size;
546 4f4d853e 2021-10-24 stsp const int max_base_candidates = 3;
547 402a5ec1 2022-01-10 stsp size_t delta_memsize = 0;
548 adb4bbb2 2022-05-20 stsp const size_t max_delta_memsize = 4 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
549 cc7a354a 2021-10-15 stsp int outfd = -1;
550 d6a28ffe 2022-05-20 op uint32_t delta_seed;
552 d6a28ffe 2022-05-20 op delta_seed = arc4random();
554 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
555 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
556 e6bcace5 2021-06-22 stsp if (cancel_cb) {
557 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
561 301e83b3 2022-10-16 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
562 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, 0L, ncommits, nreused + nmeta,
563 b8af7c06 2022-03-15 stsp nreused + i, 0);
566 e6bcace5 2021-06-22 stsp m = meta[i];
568 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
569 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
572 94dac27c 2021-10-15 stsp err = got_object_raw_open(&raw, &outfd, repo, &m->id);
575 600b755e 2021-10-14 stsp m->size = raw->size;
577 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
578 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&m->dtab, raw->data,
579 d6a28ffe 2022-05-20 op raw->hdrlen, raw->size + raw->hdrlen, delta_seed);
581 64a8571e 2022-01-07 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
582 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed);
587 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
588 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
589 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
590 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
591 e6bcace5 2021-06-22 stsp n->dtab = NULL;
594 74881701 2021-10-15 stsp best_size = raw->size;
595 74881701 2021-10-15 stsp best_ndeltas = 0;
596 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
597 e6bcace5 2021-06-22 stsp if (cancel_cb) {
598 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
602 e6bcace5 2021-06-22 stsp base = meta[j];
603 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
604 22edbce7 2021-10-24 stsp if (base->nchain >= 128 ||
605 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
608 d3c116bf 2021-10-15 stsp err = got_object_raw_open(&base_raw, &outfd, repo,
609 94dac27c 2021-10-15 stsp &base->id);
613 64a8571e 2022-01-07 stsp if (raw->f == NULL && base_raw->f == NULL) {
614 64a8571e 2022-01-07 stsp err = got_deltify_mem_mem(&deltas, &ndeltas,
615 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
616 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
617 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
618 64a8571e 2022-01-07 stsp base_raw->hdrlen,
619 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
620 64a8571e 2022-01-07 stsp } else if (raw->f == NULL) {
621 64a8571e 2022-01-07 stsp err = got_deltify_mem_file(&deltas, &ndeltas,
622 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
623 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
624 64a8571e 2022-01-07 stsp base->dtab, base_raw->f,
625 64a8571e 2022-01-07 stsp base_raw->hdrlen,
626 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
627 64a8571e 2022-01-07 stsp } else if (base_raw->f == NULL) {
628 64a8571e 2022-01-07 stsp err = got_deltify_file_mem(&deltas, &ndeltas,
629 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
630 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
631 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
632 64a8571e 2022-01-07 stsp base_raw->hdrlen,
633 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
635 64a8571e 2022-01-07 stsp err = got_deltify(&deltas, &ndeltas,
636 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
637 d6a28ffe 2022-05-20 op raw->size + raw->hdrlen, delta_seed,
638 64a8571e 2022-01-07 stsp base->dtab, base_raw->f, base_raw->hdrlen,
639 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
641 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
642 e6bcace5 2021-06-22 stsp base_raw = NULL;
646 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
647 74881701 2021-10-15 stsp if (size + 32 < best_size){
649 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
650 e6bcace5 2021-06-22 stsp * replace it.
652 74881701 2021-10-15 stsp best_size = size;
653 74881701 2021-10-15 stsp free(best_deltas);
654 74881701 2021-10-15 stsp best_deltas = deltas;
655 74881701 2021-10-15 stsp best_ndeltas = ndeltas;
656 74881701 2021-10-15 stsp deltas = NULL;
657 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
658 e6bcace5 2021-06-22 stsp m->prev = base;
659 e6bcace5 2021-06-22 stsp m->head = base->head;
660 e6bcace5 2021-06-22 stsp if (m->head == NULL)
661 e6bcace5 2021-06-22 stsp m->head = base;
663 e6bcace5 2021-06-22 stsp free(deltas);
664 e6bcace5 2021-06-22 stsp deltas = NULL;
665 e6bcace5 2021-06-22 stsp ndeltas = 0;
669 74881701 2021-10-15 stsp if (best_ndeltas > 0) {
670 402a5ec1 2022-01-10 stsp if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
671 402a5ec1 2022-01-10 stsp delta_memsize + best_size <= max_delta_memsize) {
672 402a5ec1 2022-01-10 stsp delta_memsize += best_size;
673 5060d5a1 2022-01-10 stsp err = encode_delta_in_mem(m, raw, best_deltas,
674 5060d5a1 2022-01-10 stsp best_ndeltas, best_size, m->prev->size);
676 5060d5a1 2022-01-10 stsp m->delta_offset = ftello(delta_cache);
677 5060d5a1 2022-01-10 stsp err = encode_delta(m, raw, best_deltas,
678 5060d5a1 2022-01-10 stsp best_ndeltas, m->prev->size, delta_cache);
680 74881701 2021-10-15 stsp free(best_deltas);
681 74881701 2021-10-15 stsp best_deltas = NULL;
682 74881701 2021-10-15 stsp best_ndeltas = 0;
687 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
688 e6bcace5 2021-06-22 stsp raw = NULL;
691 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
692 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
693 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
696 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
697 e6bcace5 2021-06-22 stsp if (base_raw)
698 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
699 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
700 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
701 74881701 2021-10-15 stsp free(deltas);
702 74881701 2021-10-15 stsp free(best_deltas);
703 e6bcace5 2021-06-22 stsp return err;
706 e6bcace5 2021-06-22 stsp static const struct got_error *
707 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
708 05118f5a 2021-06-22 stsp struct got_repository *repo)
710 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
711 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
714 05118f5a 2021-06-22 stsp *found = 0;
716 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
717 05118f5a 2021-06-22 stsp if (err == NULL)
718 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
719 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
720 05118f5a 2021-06-22 stsp err = NULL;
721 05118f5a 2021-06-22 stsp return err;
724 301e83b3 2022-10-16 stsp const struct got_error *
725 301e83b3 2022-10-16 stsp got_pack_add_object(int want_meta, struct got_object_idset *idset,
726 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
727 d6a28ffe 2022-05-20 op time_t mtime, uint32_t seed, int loose_obj_only,
728 d6a28ffe 2022-05-20 op struct got_repository *repo, int *ncolored, int *nfound, int *ntrees,
729 6863cbf9 2022-03-21 stsp got_pack_progress_cb progress_cb, void *progress_arg,
730 6863cbf9 2022-03-21 stsp struct got_ratelimit *rl)
732 e6bcace5 2021-06-22 stsp const struct got_error *err;
733 67fd6849 2022-02-13 stsp struct got_pack_meta *m = NULL;
735 05118f5a 2021-06-22 stsp if (loose_obj_only) {
736 05118f5a 2021-06-22 stsp int is_packed;
737 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
739 05118f5a 2021-06-22 stsp return err;
740 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
741 05118f5a 2021-06-22 stsp return NULL;
744 67fd6849 2022-02-13 stsp if (want_meta) {
745 d6a28ffe 2022-05-20 op err = alloc_meta(&m, id, path, obj_type, mtime, seed);
747 6863cbf9 2022-03-21 stsp return err;
749 6863cbf9 2022-03-21 stsp (*nfound)++;
750 301e83b3 2022-10-16 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
751 6863cbf9 2022-03-21 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
753 bb6672b6 2022-04-14 tb clear_meta(m);
755 67fd6849 2022-02-13 stsp return err;
759 bb6672b6 2022-04-14 tb err = got_object_idset_add(idset, id, m);
761 bb6672b6 2022-04-14 tb clear_meta(m);
767 301e83b3 2022-10-16 stsp const struct got_error *
768 301e83b3 2022-10-16 stsp got_pack_load_tree_entries(struct got_object_id_queue *ids, int want_meta,
769 2f8438b0 2022-05-04 stsp struct got_object_idset *idset, struct got_object_idset *idset_exclude,
770 0ab4c957 2022-06-13 stsp struct got_tree_object *tree,
771 d6a28ffe 2022-05-20 op const char *dpath, time_t mtime, uint32_t seed, struct got_repository *repo,
772 b8af7c06 2022-03-15 stsp int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
773 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
774 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, 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 char *p = NULL;
780 b8af7c06 2022-03-15 stsp (*ntrees)++;
781 301e83b3 2022-10-16 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
782 b8af7c06 2022-03-15 stsp *ncolored, *nfound, *ntrees, 0L, 0, 0, 0, 0);
784 e6bcace5 2021-06-22 stsp return err;
786 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
787 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
788 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
789 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
791 e6bcace5 2021-06-22 stsp if (cancel_cb) {
792 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
797 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
798 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset, id) ||
799 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
803 0ab4c957 2022-06-13 stsp * If got-read-pack is crawling trees for us then
804 0ab4c957 2022-06-13 stsp * we are only here to collect blob IDs.
806 0ab4c957 2022-06-13 stsp if (ids == NULL && S_ISDIR(mode))
809 0ab4c957 2022-06-13 stsp if (asprintf(&p, "%s%s%s", dpath,
810 0ab4c957 2022-06-13 stsp got_path_is_root_dir(dpath) ? "" : "/",
811 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
812 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
816 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
817 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
818 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
821 3e6ceea0 2022-05-20 stsp qid->data = p;
823 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
824 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
825 301e83b3 2022-10-16 stsp err = got_pack_add_object(want_meta,
826 2f8438b0 2022-05-04 stsp want_meta ? idset : idset_exclude, id, p,
827 d6a28ffe 2022-05-20 op GOT_OBJ_TYPE_BLOB, mtime, seed, loose_obj_only,
828 d6a28ffe 2022-05-20 op repo, ncolored, nfound, ntrees,
829 6863cbf9 2022-03-21 stsp progress_cb, progress_arg, rl);
841 e6bcace5 2021-06-22 stsp return err;
844 301e83b3 2022-10-16 stsp const struct got_error *
845 301e83b3 2022-10-16 stsp got_pack_load_tree(int want_meta, struct got_object_idset *idset,
846 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
847 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
848 d6a28ffe 2022-05-20 op uint32_t seed, struct got_repository *repo, int loose_obj_only,
849 b8af7c06 2022-03-15 stsp int *ncolored, int *nfound, int *ntrees,
850 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
851 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
853 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
854 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
855 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
856 0ab4c957 2022-06-13 stsp struct got_tree_object *tree = NULL;
858 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, tree_id) ||
859 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, tree_id))
860 e6bcace5 2021-06-22 stsp return NULL;
862 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
864 3e6ceea0 2022-05-20 stsp return err;
865 3e6ceea0 2022-05-20 stsp qid->data = strdup(dpath);
866 3e6ceea0 2022-05-20 stsp if (qid->data == NULL) {
867 3e6ceea0 2022-05-20 stsp err = got_error_from_errno("strdup");
868 3e6ceea0 2022-05-20 stsp got_object_qid_free(qid);
869 e6bcace5 2021-06-22 stsp return err;
872 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
873 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
875 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
876 3e6ceea0 2022-05-20 stsp const char *path;
877 e6bcace5 2021-06-22 stsp if (cancel_cb) {
878 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
883 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
884 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
885 3e6ceea0 2022-05-20 stsp path = qid->data;
887 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, &qid->id) ||
888 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, &qid->id)) {
889 3e6ceea0 2022-05-20 stsp free(qid->data);
890 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
894 301e83b3 2022-10-16 stsp err = got_pack_add_object(want_meta,
895 301e83b3 2022-10-16 stsp want_meta ? idset : idset_exclude,
896 3e6ceea0 2022-05-20 stsp &qid->id, path, GOT_OBJ_TYPE_TREE,
897 d6a28ffe 2022-05-20 op mtime, seed, loose_obj_only, repo,
898 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
900 3e6ceea0 2022-05-20 stsp free(qid->data);
901 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
905 0ab4c957 2022-06-13 stsp err = got_object_open_as_tree(&tree, repo, &qid->id);
907 0ab4c957 2022-06-13 stsp free(qid->data);
908 0ab4c957 2022-06-13 stsp got_object_qid_free(qid);
912 301e83b3 2022-10-16 stsp err = got_pack_load_tree_entries(&tree_ids, want_meta, idset,
913 0ab4c957 2022-06-13 stsp idset_exclude, tree, path, mtime, seed, repo,
914 0ab4c957 2022-06-13 stsp loose_obj_only, ncolored, nfound, ntrees,
915 0ab4c957 2022-06-13 stsp progress_cb, progress_arg, rl,
916 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
917 3e6ceea0 2022-05-20 stsp free(qid->data);
918 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
922 0ab4c957 2022-06-13 stsp got_object_tree_close(tree);
923 0ab4c957 2022-06-13 stsp tree = NULL;
926 3e6ceea0 2022-05-20 stsp STAILQ_FOREACH(qid, &tree_ids, entry)
927 3e6ceea0 2022-05-20 stsp free(qid->data);
928 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
930 0ab4c957 2022-06-13 stsp got_object_tree_close(tree);
931 e6bcace5 2021-06-22 stsp return err;
934 e6bcace5 2021-06-22 stsp static const struct got_error *
935 67fd6849 2022-02-13 stsp load_commit(int want_meta, struct got_object_idset *idset,
936 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
937 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
938 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
939 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
940 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
942 e6bcace5 2021-06-22 stsp const struct got_error *err;
943 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
945 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
946 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
947 e6bcace5 2021-06-22 stsp return NULL;
949 05118f5a 2021-06-22 stsp if (loose_obj_only) {
950 05118f5a 2021-06-22 stsp int is_packed;
951 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
953 05118f5a 2021-06-22 stsp return err;
954 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
955 05118f5a 2021-06-22 stsp return NULL;
958 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
960 e6bcace5 2021-06-22 stsp return err;
962 301e83b3 2022-10-16 stsp err = got_pack_add_object(want_meta,
963 301e83b3 2022-10-16 stsp want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_COMMIT,
964 d6a28ffe 2022-05-20 op got_object_commit_get_committer_time(commit), seed,
965 6863cbf9 2022-03-21 stsp loose_obj_only, repo,
966 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
970 301e83b3 2022-10-16 stsp err = got_pack_load_tree(want_meta, idset, idset_exclude,
971 2f8438b0 2022-05-04 stsp got_object_commit_get_tree_id(commit),
972 d6a28ffe 2022-05-20 op "", got_object_commit_get_committer_time(commit), seed,
973 b8af7c06 2022-03-15 stsp repo, loose_obj_only, ncolored, nfound, ntrees,
974 b8af7c06 2022-03-15 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
976 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
977 e6bcace5 2021-06-22 stsp return err;
980 05118f5a 2021-06-22 stsp static const struct got_error *
981 67fd6849 2022-02-13 stsp load_tag(int want_meta, struct got_object_idset *idset,
982 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude,
983 d6a28ffe 2022-05-20 op struct got_object_id *id, struct got_repository *repo, uint32_t seed,
984 d6a28ffe 2022-05-20 op int loose_obj_only, int *ncolored, int *nfound, int *ntrees,
985 b8af7c06 2022-03-15 stsp got_pack_progress_cb progress_cb, void *progress_arg,
986 b8af7c06 2022-03-15 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
988 05118f5a 2021-06-22 stsp const struct got_error *err;
989 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
991 2f8438b0 2022-05-04 stsp if (got_object_idset_contains(idset, id) ||
992 2f8438b0 2022-05-04 stsp got_object_idset_contains(idset_exclude, id))
993 05118f5a 2021-06-22 stsp return NULL;
995 05118f5a 2021-06-22 stsp if (loose_obj_only) {
996 05118f5a 2021-06-22 stsp int is_packed;
997 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
999 05118f5a 2021-06-22 stsp return err;
1000 cdeb891a 2022-03-21 stsp if (is_packed && want_meta)
1001 05118f5a 2021-06-22 stsp return NULL;
1004 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
1006 05118f5a 2021-06-22 stsp return err;
1008 301e83b3 2022-10-16 stsp err = got_pack_add_object(want_meta,
1009 301e83b3 2022-10-16 stsp want_meta ? idset : idset_exclude, id, "", GOT_OBJ_TYPE_TAG,
1010 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, loose_obj_only, repo,
1011 6863cbf9 2022-03-21 stsp ncolored, nfound, ntrees, progress_cb, progress_arg, rl);
1013 05118f5a 2021-06-22 stsp goto done;
1015 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
1016 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
1017 2f8438b0 2022-05-04 stsp err = load_commit(want_meta, idset, idset_exclude,
1018 d6a28ffe 2022-05-20 op got_object_tag_get_object_id(tag), repo, seed,
1019 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1020 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1022 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
1023 301e83b3 2022-10-16 stsp err = got_pack_load_tree(want_meta, idset, idset_exclude,
1024 67fd6849 2022-02-13 stsp got_object_tag_get_object_id(tag), "",
1025 d6a28ffe 2022-05-20 op got_object_tag_get_tagger_time(tag), seed, repo,
1026 d6a28ffe 2022-05-20 op loose_obj_only, ncolored, nfound, ntrees,
1027 d6a28ffe 2022-05-20 op progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1034 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
1035 05118f5a 2021-06-22 stsp return err;
1038 301e83b3 2022-10-16 stsp const struct got_error *
1039 301e83b3 2022-10-16 stsp got_pack_paint_commit(struct got_object_qid *qid, intptr_t color)
1041 61af9b21 2022-06-28 stsp if (color < 0 || color >= COLOR_MAX)
1042 70f8f24d 2022-04-14 stsp return got_error(GOT_ERR_RANGE);
1044 61af9b21 2022-06-28 stsp qid->data = (void *)color;
1045 e6bcace5 2021-06-22 stsp return NULL;
1048 301e83b3 2022-10-16 stsp const struct got_error *
1049 301e83b3 2022-10-16 stsp got_pack_queue_commit_id(struct got_object_id_queue *ids,
1050 301e83b3 2022-10-16 stsp struct got_object_id *id, intptr_t color, struct got_repository *repo)
1052 70f8f24d 2022-04-14 stsp const struct got_error *err;
1053 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
1055 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
1057 e6bcace5 2021-06-22 stsp return err;
1059 70f8f24d 2022-04-14 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
1060 301e83b3 2022-10-16 stsp return got_pack_paint_commit(qid, color);
1063 e6bcace5 2021-06-22 stsp struct append_id_arg {
1064 e6bcace5 2021-06-22 stsp struct got_object_id **array;
1066 70f8f24d 2022-04-14 stsp struct got_object_idset *drop;
1067 70f8f24d 2022-04-14 stsp struct got_object_idset *skip;
1070 e6bcace5 2021-06-22 stsp static const struct got_error *
1071 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
1073 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
1075 70f8f24d 2022-04-14 stsp if (got_object_idset_contains(a->skip, id) ||
1076 70f8f24d 2022-04-14 stsp got_object_idset_contains(a->drop, id))
1077 70f8f24d 2022-04-14 stsp return NULL;
1079 70f8f24d 2022-04-14 stsp a->array[++a->idx] = got_object_id_dup(id);
1080 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
1081 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
1083 e6bcace5 2021-06-22 stsp return NULL;
1086 29e0594f 2022-04-09 stsp static const struct got_error *
1087 61af9b21 2022-06-28 stsp queue_commit_or_tag_id(struct got_object_id *id, intptr_t color,
1088 29e0594f 2022-04-09 stsp struct got_object_id_queue *ids, struct got_repository *repo)
1090 29e0594f 2022-04-09 stsp const struct got_error *err;
1091 29e0594f 2022-04-09 stsp struct got_tag_object *tag = NULL;
1092 29e0594f 2022-04-09 stsp int obj_type;
1094 29e0594f 2022-04-09 stsp err = got_object_get_type(&obj_type, repo, id);
1096 29e0594f 2022-04-09 stsp return err;
1098 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_TAG) {
1099 29e0594f 2022-04-09 stsp err = got_object_open_as_tag(&tag, repo, id);
1101 29e0594f 2022-04-09 stsp return err;
1102 29e0594f 2022-04-09 stsp obj_type = got_object_tag_get_object_type(tag);
1103 29e0594f 2022-04-09 stsp id = got_object_tag_get_object_id(tag);
1106 29e0594f 2022-04-09 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1107 301e83b3 2022-10-16 stsp err = got_pack_queue_commit_id(ids, id, color, repo);
1109 29e0594f 2022-04-09 stsp goto done;
1113 29e0594f 2022-04-09 stsp got_object_tag_close(tag);
1114 61af9b21 2022-06-28 stsp return err;
1117 301e83b3 2022-10-16 stsp const struct got_error *
1118 301e83b3 2022-10-16 stsp got_pack_find_pack_for_commit_painting(struct got_packidx **best_packidx,
1119 61af9b21 2022-06-28 stsp struct got_object_id_queue *ids, int nids, struct got_repository *repo)
1121 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
1122 61af9b21 2022-06-28 stsp struct got_pathlist_entry *pe;
1123 61af9b21 2022-06-28 stsp const char *best_packidx_path = NULL;
1124 61af9b21 2022-06-28 stsp int nobj_max = 0;
1125 61af9b21 2022-06-28 stsp int ncommits_max = 0;
1127 61af9b21 2022-06-28 stsp *best_packidx = NULL;
1130 61af9b21 2022-06-28 stsp * Find the largest pack which contains at least some of the
1131 61af9b21 2022-06-28 stsp * commits we are interested in.
1133 61af9b21 2022-06-28 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1134 61af9b21 2022-06-28 stsp const char *path_packidx = pe->path;
1135 61af9b21 2022-06-28 stsp struct got_packidx *packidx;
1136 61af9b21 2022-06-28 stsp int nobj, idx, ncommits = 0;
1137 61af9b21 2022-06-28 stsp struct got_object_qid *qid;
1139 61af9b21 2022-06-28 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
1143 61af9b21 2022-06-28 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1144 61af9b21 2022-06-28 stsp if (nobj <= nobj_max)
1147 61af9b21 2022-06-28 stsp STAILQ_FOREACH(qid, ids, entry) {
1148 61af9b21 2022-06-28 stsp idx = got_packidx_get_object_idx(packidx, &qid->id);
1149 61af9b21 2022-06-28 stsp if (idx != -1)
1150 61af9b21 2022-06-28 stsp ncommits++;
1152 61af9b21 2022-06-28 stsp if (ncommits > ncommits_max) {
1153 61af9b21 2022-06-28 stsp best_packidx_path = path_packidx;
1154 61af9b21 2022-06-28 stsp nobj_max = nobj;
1155 61af9b21 2022-06-28 stsp ncommits_max = ncommits;
1159 61af9b21 2022-06-28 stsp if (best_packidx_path && err == NULL) {
1160 61af9b21 2022-06-28 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
1164 61af9b21 2022-06-28 stsp return err;
1167 61af9b21 2022-06-28 stsp static const struct got_error *
1168 14dbbf48 2022-04-10 stsp findtwixt(struct got_object_id ***res, int *nres, int *ncolored,
1169 14dbbf48 2022-04-10 stsp struct got_object_id **head, int nhead,
1170 14dbbf48 2022-04-10 stsp struct got_object_id **tail, int ntail,
1171 14dbbf48 2022-04-10 stsp struct got_repository *repo,
1172 14dbbf48 2022-04-10 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1173 14dbbf48 2022-04-10 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1175 14dbbf48 2022-04-10 stsp const struct got_error *err = NULL;
1176 14dbbf48 2022-04-10 stsp struct got_object_id_queue ids;
1177 70f8f24d 2022-04-14 stsp struct got_object_idset *keep, *drop, *skip = NULL;
1178 14dbbf48 2022-04-10 stsp int i, nkeep;
1180 14dbbf48 2022-04-10 stsp STAILQ_INIT(&ids);
1181 14dbbf48 2022-04-10 stsp *res = NULL;
1182 14dbbf48 2022-04-10 stsp *nres = 0;
1183 14dbbf48 2022-04-10 stsp *ncolored = 0;
1185 14dbbf48 2022-04-10 stsp keep = got_object_idset_alloc();
1186 14dbbf48 2022-04-10 stsp if (keep == NULL)
1187 14dbbf48 2022-04-10 stsp return got_error_from_errno("got_object_idset_alloc");
1189 14dbbf48 2022-04-10 stsp drop = got_object_idset_alloc();
1190 14dbbf48 2022-04-10 stsp if (drop == NULL) {
1191 14dbbf48 2022-04-10 stsp err = got_error_from_errno("got_object_idset_alloc");
1192 14dbbf48 2022-04-10 stsp goto done;
1195 70f8f24d 2022-04-14 stsp skip = got_object_idset_alloc();
1196 70f8f24d 2022-04-14 stsp if (skip == NULL) {
1197 70f8f24d 2022-04-14 stsp err = got_error_from_errno("got_object_idset_alloc");
1198 70f8f24d 2022-04-14 stsp goto done;
1201 14dbbf48 2022-04-10 stsp for (i = 0; i < nhead; i++) {
1202 14dbbf48 2022-04-10 stsp struct got_object_id *id = head[i];
1203 14dbbf48 2022-04-10 stsp if (id == NULL)
1205 fbafdecf 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_KEEP, &ids, repo);
1207 14dbbf48 2022-04-10 stsp goto done;
1210 14dbbf48 2022-04-10 stsp for (i = 0; i < ntail; i++) {
1211 14dbbf48 2022-04-10 stsp struct got_object_id *id = tail[i];
1212 14dbbf48 2022-04-10 stsp if (id == NULL)
1214 14dbbf48 2022-04-10 stsp err = queue_commit_or_tag_id(id, COLOR_DROP, &ids, repo);
1216 14dbbf48 2022-04-10 stsp goto done;
1219 301e83b3 2022-10-16 stsp err = got_pack_paint_commits(ncolored, &ids, nhead + ntail,
1220 70f8f24d 2022-04-14 stsp keep, drop, skip, repo, progress_cb, progress_arg, rl,
1221 70f8f24d 2022-04-14 stsp cancel_cb, cancel_arg);
1223 14dbbf48 2022-04-10 stsp goto done;
1225 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
1226 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
1227 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
1228 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
1229 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
1230 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
1231 e6bcace5 2021-06-22 stsp goto done;
1233 70f8f24d 2022-04-14 stsp arg.idx = -1;
1234 70f8f24d 2022-04-14 stsp arg.skip = skip;
1235 70f8f24d 2022-04-14 stsp arg.drop = drop;
1236 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
1237 e6bcace5 2021-06-22 stsp if (err) {
1238 e6bcace5 2021-06-22 stsp free(arg.array);
1239 e6bcace5 2021-06-22 stsp goto done;
1241 e6bcace5 2021-06-22 stsp *res = arg.array;
1242 70f8f24d 2022-04-14 stsp *nres = arg.idx + 1;
1245 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
1246 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
1248 70f8f24d 2022-04-14 stsp got_object_idset_free(skip);
1249 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
1250 e6bcace5 2021-06-22 stsp return err;
1253 e6bcace5 2021-06-22 stsp static const struct got_error *
1254 0ab4c957 2022-06-13 stsp find_pack_for_enumeration(struct got_packidx **best_packidx,
1255 0ab4c957 2022-06-13 stsp struct got_object_id **ids, int nids, struct got_repository *repo)
1257 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1258 0ab4c957 2022-06-13 stsp struct got_pathlist_entry *pe;
1259 0ab4c957 2022-06-13 stsp const char *best_packidx_path = NULL;
1260 0ab4c957 2022-06-13 stsp int nobj_max = 0;
1261 0ab4c957 2022-06-13 stsp int ncommits_max = 0;
1263 0ab4c957 2022-06-13 stsp *best_packidx = NULL;
1266 0ab4c957 2022-06-13 stsp * Find the largest pack which contains at least some of the
1267 0ab4c957 2022-06-13 stsp * commits and tags we are interested in.
1269 0ab4c957 2022-06-13 stsp TAILQ_FOREACH(pe, &repo->packidx_paths, entry) {
1270 0ab4c957 2022-06-13 stsp const char *path_packidx = pe->path;
1271 0ab4c957 2022-06-13 stsp struct got_packidx *packidx;
1272 0ab4c957 2022-06-13 stsp int nobj, i, idx, ncommits = 0;
1274 0ab4c957 2022-06-13 stsp err = got_repo_get_packidx(&packidx, path_packidx, repo);
1278 0ab4c957 2022-06-13 stsp nobj = be32toh(packidx->hdr.fanout_table[0xff]);
1279 0ab4c957 2022-06-13 stsp if (nobj <= nobj_max)
1282 0ab4c957 2022-06-13 stsp for (i = 0; i < nids; i++) {
1283 0ab4c957 2022-06-13 stsp idx = got_packidx_get_object_idx(packidx, ids[i]);
1284 0ab4c957 2022-06-13 stsp if (idx != -1)
1285 0ab4c957 2022-06-13 stsp ncommits++;
1287 0ab4c957 2022-06-13 stsp if (ncommits > ncommits_max) {
1288 0ab4c957 2022-06-13 stsp best_packidx_path = path_packidx;
1289 0ab4c957 2022-06-13 stsp nobj_max = nobj;
1290 0ab4c957 2022-06-13 stsp ncommits_max = ncommits;
1294 eb7b30a1 2022-06-13 stsp if (best_packidx_path && err == NULL) {
1295 0ab4c957 2022-06-13 stsp err = got_repo_get_packidx(best_packidx, best_packidx_path,
1299 0ab4c957 2022-06-13 stsp return err;
1302 0ab4c957 2022-06-13 stsp static const struct got_error *
1303 b8af7c06 2022-03-15 stsp load_object_ids(int *ncolored, int *nfound, int *ntrees,
1304 b8af7c06 2022-03-15 stsp struct got_object_idset *idset, struct got_object_id **theirs, int ntheirs,
1305 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
1306 d6a28ffe 2022-05-20 op uint32_t seed, int loose_obj_only, got_pack_progress_cb progress_cb,
1307 d6a28ffe 2022-05-20 op void *progress_arg, struct got_ratelimit *rl, got_cancel_cb cancel_cb,
1308 d6a28ffe 2022-05-20 op void *cancel_arg)
1310 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1311 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
1312 0ab4c957 2022-06-13 stsp struct got_packidx *packidx = NULL;
1313 db9b9b1c 2022-06-14 stsp int i, nobj = 0, obj_type, found_all_objects = 0;
1314 2f8438b0 2022-05-04 stsp struct got_object_idset *idset_exclude;
1316 2f8438b0 2022-05-04 stsp idset_exclude = got_object_idset_alloc();
1317 2f8438b0 2022-05-04 stsp if (idset_exclude == NULL)
1318 2f8438b0 2022-05-04 stsp return got_error_from_errno("got_object_idset_alloc");
1320 b8af7c06 2022-03-15 stsp *ncolored = 0;
1321 b8af7c06 2022-03-15 stsp *nfound = 0;
1322 b8af7c06 2022-03-15 stsp *ntrees = 0;
1324 b8af7c06 2022-03-15 stsp err = findtwixt(&ids, &nobj, ncolored, ours, nours, theirs, ntheirs,
1325 b8af7c06 2022-03-15 stsp repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1327 e6bcace5 2021-06-22 stsp goto done;
1329 0ab4c957 2022-06-13 stsp err = find_pack_for_enumeration(&packidx, theirs, ntheirs, repo);
1331 0ab4c957 2022-06-13 stsp goto done;
1332 0ab4c957 2022-06-13 stsp if (packidx) {
1333 301e83b3 2022-10-16 stsp err = got_pack_load_packed_object_ids(&found_all_objects,
1334 db9b9b1c 2022-06-14 stsp theirs, ntheirs, NULL, 0, 0, seed, idset, idset_exclude,
1335 db9b9b1c 2022-06-14 stsp loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1336 db9b9b1c 2022-06-14 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1338 0ab4c957 2022-06-13 stsp goto done;
1341 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
1342 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
1343 05118f5a 2021-06-22 stsp if (id == NULL)
1345 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
1347 05118f5a 2021-06-22 stsp return err;
1348 9d34261e 2022-04-07 stsp if (obj_type == GOT_OBJ_TYPE_COMMIT) {
1349 db9b9b1c 2022-06-14 stsp if (!found_all_objects) {
1350 db9b9b1c 2022-06-14 stsp err = load_commit(0, idset, idset_exclude,
1351 db9b9b1c 2022-06-14 stsp id, repo, seed, loose_obj_only,
1352 db9b9b1c 2022-06-14 stsp ncolored, nfound, ntrees,
1353 db9b9b1c 2022-06-14 stsp progress_cb, progress_arg, rl,
1354 db9b9b1c 2022-06-14 stsp cancel_cb, cancel_arg);
1356 db9b9b1c 2022-06-14 stsp goto done;
1358 9d34261e 2022-04-07 stsp } else if (obj_type == GOT_OBJ_TYPE_TAG) {
1359 2f8438b0 2022-05-04 stsp err = load_tag(0, idset, idset_exclude, id, repo,
1360 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
1361 9d34261e 2022-04-07 stsp progress_cb, progress_arg, rl,
1362 9d34261e 2022-04-07 stsp cancel_cb, cancel_arg);
1364 9d34261e 2022-04-07 stsp goto done;
1368 db9b9b1c 2022-06-14 stsp found_all_objects = 0;
1369 0ab4c957 2022-06-13 stsp err = find_pack_for_enumeration(&packidx, ids, nobj, repo);
1371 0ab4c957 2022-06-13 stsp goto done;
1372 0ab4c957 2022-06-13 stsp if (packidx) {
1373 301e83b3 2022-10-16 stsp err = got_pack_load_packed_object_ids(&found_all_objects, ids,
1374 db9b9b1c 2022-06-14 stsp nobj, theirs, ntheirs, 1, seed, idset, idset_exclude,
1375 db9b9b1c 2022-06-14 stsp loose_obj_only, repo, packidx, ncolored, nfound, ntrees,
1376 0ab4c957 2022-06-13 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1378 0ab4c957 2022-06-13 stsp goto done;
1381 db9b9b1c 2022-06-14 stsp if (!found_all_objects) {
1382 db9b9b1c 2022-06-14 stsp for (i = 0; i < nobj; i++) {
1383 db9b9b1c 2022-06-14 stsp err = load_commit(1, idset, idset_exclude, ids[i],
1384 db9b9b1c 2022-06-14 stsp repo, seed, loose_obj_only, ncolored, nfound,
1385 db9b9b1c 2022-06-14 stsp ntrees, progress_cb, progress_arg, rl,
1386 db9b9b1c 2022-06-14 stsp cancel_cb, cancel_arg);
1388 db9b9b1c 2022-06-14 stsp goto done;
1392 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1393 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1394 67fd6849 2022-02-13 stsp struct got_pack_meta *m;
1395 05118f5a 2021-06-22 stsp if (id == NULL)
1397 67fd6849 2022-02-13 stsp m = got_object_idset_get(idset, id);
1398 67fd6849 2022-02-13 stsp if (m == NULL) {
1399 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1401 07165b17 2021-07-01 stsp goto done;
1403 67fd6849 2022-02-13 stsp obj_type = m->obj_type;
1404 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1406 2f8438b0 2022-05-04 stsp err = load_tag(1, idset, idset_exclude, id, repo,
1407 d6a28ffe 2022-05-20 op seed, loose_obj_only, ncolored, nfound, ntrees,
1408 2f8438b0 2022-05-04 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1410 e6bcace5 2021-06-22 stsp goto done;
1413 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
1414 e6bcace5 2021-06-22 stsp free(ids[i]);
1416 e6bcace5 2021-06-22 stsp free(ids);
1417 2f8438b0 2022-05-04 stsp got_object_idset_free(idset_exclude);
1418 e6bcace5 2021-06-22 stsp return err;
1421 336075a4 2022-06-25 op static const struct got_error *
1422 894e4711 2022-10-15 stsp hwrite(int fd, const void *buf, off_t len, SHA1_CTX *ctx)
1424 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1425 13b2bc37 2022-10-23 stsp return got_poll_write_full(fd, buf, len);
1428 336075a4 2022-06-25 op static const struct got_error *
1429 894e4711 2022-10-15 stsp hcopy(FILE *fsrc, int fd_dst, off_t len, SHA1_CTX *ctx)
1431 13b2bc37 2022-10-23 stsp const struct got_error *err;
1432 2d9e6abf 2022-05-04 stsp unsigned char buf[65536];
1433 2d9e6abf 2022-05-04 stsp off_t remain = len;
1436 2d9e6abf 2022-05-04 stsp while (remain > 0) {
1437 2d9e6abf 2022-05-04 stsp size_t copylen = MIN(sizeof(buf), remain);
1438 2d9e6abf 2022-05-04 stsp n = fread(buf, 1, copylen, fsrc);
1439 2d9e6abf 2022-05-04 stsp if (n != copylen)
1440 2d9e6abf 2022-05-04 stsp return got_ferror(fsrc, GOT_ERR_IO);
1441 2d9e6abf 2022-05-04 stsp SHA1Update(ctx, buf, copylen);
1442 13b2bc37 2022-10-23 stsp err = got_poll_write_full(fd_dst, buf, copylen);
1444 13b2bc37 2022-10-23 stsp return err;
1445 2d9e6abf 2022-05-04 stsp remain -= copylen;
1448 e6bcace5 2021-06-22 stsp return NULL;
1451 336075a4 2022-06-25 op static const struct got_error *
1452 e93fb944 2022-05-10 stsp hcopy_mmap(uint8_t *src, off_t src_offset, size_t src_size,
1453 894e4711 2022-10-15 stsp int fd, off_t len, SHA1_CTX *ctx)
1455 e93fb944 2022-05-10 stsp if (src_offset + len > src_size)
1456 e93fb944 2022-05-10 stsp return got_error(GOT_ERR_RANGE);
1458 e93fb944 2022-05-10 stsp SHA1Update(ctx, src + src_offset, len);
1459 13b2bc37 2022-10-23 stsp return got_poll_write_full(fd, src + src_offset, len);
1462 e6bcace5 2021-06-22 stsp static void
1463 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1465 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1466 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1467 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1468 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1471 e6bcace5 2021-06-22 stsp static int
1472 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1474 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1476 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1477 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1478 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1479 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1480 611e8e31 2022-05-01 stsp if (bhd->mtime < ahd->mtime)
1481 611e8e31 2022-05-01 stsp return -1;
1482 611e8e31 2022-05-01 stsp if (bhd->mtime > ahd->mtime)
1484 611e8e31 2022-05-01 stsp if (bhd < ahd)
1485 611e8e31 2022-05-01 stsp return -1;
1486 611e8e31 2022-05-01 stsp if (bhd > ahd)
1488 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1489 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1490 611e8e31 2022-05-01 stsp if (a->mtime < b->mtime)
1491 611e8e31 2022-05-01 stsp return -1;
1492 611e8e31 2022-05-01 stsp if (a->mtime > b->mtime)
1494 611e8e31 2022-05-01 stsp return got_object_id_cmp(&a->id, &b->id);
1497 67fd6849 2022-02-13 stsp static int
1498 67fd6849 2022-02-13 stsp reuse_write_order_cmp(const void *pa, const void *pb)
1500 67fd6849 2022-02-13 stsp struct got_pack_meta *a, *b;
1502 67fd6849 2022-02-13 stsp a = *(struct got_pack_meta **)pa;
1503 67fd6849 2022-02-13 stsp b = *(struct got_pack_meta **)pb;
1505 67fd6849 2022-02-13 stsp if (a->reused_delta_offset < b->reused_delta_offset)
1506 67fd6849 2022-02-13 stsp return -1;
1507 67fd6849 2022-02-13 stsp if (a->reused_delta_offset > b->reused_delta_offset)
1512 e6bcace5 2021-06-22 stsp static const struct got_error *
1513 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1517 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1519 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1520 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1521 e6bcace5 2021-06-22 stsp len >>= 4;
1522 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1523 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1524 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1525 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1526 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1527 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1530 e6bcace5 2021-06-22 stsp *hdrlen = i;
1531 e6bcace5 2021-06-22 stsp return NULL;
1534 e6bcace5 2021-06-22 stsp static int
1535 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1538 e6bcace5 2021-06-22 stsp char rbuf[8];
1540 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1541 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1542 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1543 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1547 e6bcace5 2021-06-22 stsp while (i > 0)
1548 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1552 e6bcace5 2021-06-22 stsp static const struct got_error *
1553 894e4711 2022-10-15 stsp deltahdr(off_t *packfile_size, SHA1_CTX *ctx, int packfd,
1554 67fd6849 2022-02-13 stsp struct got_pack_meta *m)
1556 5060d5a1 2022-01-10 stsp const struct got_error *err;
1557 5060d5a1 2022-01-10 stsp char buf[32];
1560 67fd6849 2022-02-13 stsp if (m->prev->off != 0) {
1561 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1562 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1564 5060d5a1 2022-01-10 stsp return err;
1565 5060d5a1 2022-01-10 stsp nh += packoff(buf + nh, m->off - m->prev->off);
1566 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, nh, ctx);
1568 5060d5a1 2022-01-10 stsp return err;
1569 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1571 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1572 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1574 5060d5a1 2022-01-10 stsp return err;
1575 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, nh, ctx);
1577 5060d5a1 2022-01-10 stsp return err;
1578 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1579 894e4711 2022-10-15 stsp err = hwrite(packfd, m->prev->id.sha1,
1580 5060d5a1 2022-01-10 stsp sizeof(m->prev->id.sha1), ctx);
1582 5060d5a1 2022-01-10 stsp return err;
1583 5060d5a1 2022-01-10 stsp *packfile_size += sizeof(m->prev->id.sha1);
1586 5060d5a1 2022-01-10 stsp return NULL;
1589 5060d5a1 2022-01-10 stsp static const struct got_error *
1590 894e4711 2022-10-15 stsp write_packed_object(off_t *packfile_size, int packfd,
1591 e93fb944 2022-05-10 stsp FILE *delta_cache, uint8_t *delta_cache_map, size_t delta_cache_size,
1592 e93fb944 2022-05-10 stsp struct got_pack_meta *m, int *outfd, SHA1_CTX *ctx,
1593 e93fb944 2022-05-10 stsp struct got_repository *repo)
1595 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
1596 67fd6849 2022-02-13 stsp struct got_deflate_checksum csum;
1597 67fd6849 2022-02-13 stsp char buf[32];
1599 67fd6849 2022-02-13 stsp struct got_raw_object *raw = NULL;
1600 24b7de1c 2022-12-03 stsp off_t outlen, delta_offset;
1602 67fd6849 2022-02-13 stsp csum.output_sha1 = ctx;
1603 67fd6849 2022-02-13 stsp csum.output_crc = NULL;
1605 24b7de1c 2022-12-03 stsp if (m->reused_delta_offset)
1606 24b7de1c 2022-12-03 stsp delta_offset = m->reused_delta_offset;
1608 24b7de1c 2022-12-03 stsp delta_offset = m->delta_offset;
1610 894e4711 2022-10-15 stsp m->off = *packfile_size;
1611 67fd6849 2022-02-13 stsp if (m->delta_len == 0) {
1612 67fd6849 2022-02-13 stsp err = got_object_raw_open(&raw, outfd, repo, &m->id);
1614 67fd6849 2022-02-13 stsp goto done;
1615 67fd6849 2022-02-13 stsp err = packhdr(&nh, buf, sizeof(buf),
1616 67fd6849 2022-02-13 stsp m->obj_type, raw->size);
1618 67fd6849 2022-02-13 stsp goto done;
1619 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, nh, ctx);
1621 67fd6849 2022-02-13 stsp goto done;
1622 67fd6849 2022-02-13 stsp *packfile_size += nh;
1623 67fd6849 2022-02-13 stsp if (raw->f == NULL) {
1624 894e4711 2022-10-15 stsp err = got_deflate_to_fd_mmap(&outlen,
1625 67fd6849 2022-02-13 stsp raw->data + raw->hdrlen, 0, raw->size,
1626 894e4711 2022-10-15 stsp packfd, &csum);
1628 67fd6849 2022-02-13 stsp goto done;
1630 67fd6849 2022-02-13 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1632 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1633 67fd6849 2022-02-13 stsp goto done;
1635 894e4711 2022-10-15 stsp err = got_deflate_to_fd(&outlen, raw->f,
1636 894e4711 2022-10-15 stsp raw->size, packfd, &csum);
1638 67fd6849 2022-02-13 stsp goto done;
1640 67fd6849 2022-02-13 stsp *packfile_size += outlen;
1641 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
1642 67fd6849 2022-02-13 stsp raw = NULL;
1643 67fd6849 2022-02-13 stsp } else if (m->delta_buf) {
1644 894e4711 2022-10-15 stsp err = deltahdr(packfile_size, ctx, packfd, m);
1646 67fd6849 2022-02-13 stsp goto done;
1647 894e4711 2022-10-15 stsp err = hwrite(packfd, m->delta_buf,
1648 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
1650 67fd6849 2022-02-13 stsp goto done;
1651 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
1652 67fd6849 2022-02-13 stsp free(m->delta_buf);
1653 67fd6849 2022-02-13 stsp m->delta_buf = NULL;
1654 e93fb944 2022-05-10 stsp } else if (delta_cache_map) {
1655 894e4711 2022-10-15 stsp err = deltahdr(packfile_size, ctx, packfd, m);
1657 e93fb944 2022-05-10 stsp goto done;
1658 24b7de1c 2022-12-03 stsp err = hcopy_mmap(delta_cache_map, delta_offset,
1659 894e4711 2022-10-15 stsp delta_cache_size, packfd, m->delta_compressed_len,
1662 e93fb944 2022-05-10 stsp goto done;
1663 e93fb944 2022-05-10 stsp *packfile_size += m->delta_compressed_len;
1665 24b7de1c 2022-12-03 stsp if (fseeko(delta_cache, delta_offset, SEEK_SET) == -1) {
1666 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1667 67fd6849 2022-02-13 stsp goto done;
1669 894e4711 2022-10-15 stsp err = deltahdr(packfile_size, ctx, packfd, m);
1671 67fd6849 2022-02-13 stsp goto done;
1672 894e4711 2022-10-15 stsp err = hcopy(delta_cache, packfd,
1673 2d9e6abf 2022-05-04 stsp m->delta_compressed_len, ctx);
1675 67fd6849 2022-02-13 stsp goto done;
1676 2d9e6abf 2022-05-04 stsp *packfile_size += m->delta_compressed_len;
1680 67fd6849 2022-02-13 stsp got_object_raw_close(raw);
1681 67fd6849 2022-02-13 stsp return err;
1684 67fd6849 2022-02-13 stsp static const struct got_error *
1685 24b7de1c 2022-12-03 stsp genpack(uint8_t *pack_sha1, int packfd, struct got_pack *reuse_pack,
1686 24b7de1c 2022-12-03 stsp FILE *delta_cache, struct got_pack_meta **deltify, int ndeltify,
1687 67fd6849 2022-02-13 stsp struct got_pack_meta **reuse, int nreuse,
1688 b8af7c06 2022-03-15 stsp int ncolored, int nfound, int ntrees, int nours,
1689 b8af7c06 2022-03-15 stsp struct got_repository *repo,
1690 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1691 211cfef0 2022-01-05 stsp struct got_ratelimit *rl,
1692 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1694 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1696 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1697 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1698 08347b73 2021-10-14 stsp char buf[32];
1699 67fd6849 2022-02-13 stsp off_t packfile_size = 0;
1700 cc7a354a 2021-10-15 stsp int outfd = -1;
1701 e93fb944 2022-05-10 stsp int delta_cache_fd = -1;
1702 e93fb944 2022-05-10 stsp uint8_t *delta_cache_map = NULL;
1703 e93fb944 2022-05-10 stsp size_t delta_cache_size = 0;
1704 24b7de1c 2022-12-03 stsp FILE *packfile = NULL;
1706 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1708 e93fb944 2022-05-10 stsp #ifndef GOT_PACK_NO_MMAP
1709 e93fb944 2022-05-10 stsp delta_cache_fd = dup(fileno(delta_cache));
1710 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1) {
1711 e93fb944 2022-05-10 stsp struct stat sb;
1712 e93fb944 2022-05-10 stsp if (fstat(delta_cache_fd, &sb) == -1) {
1713 e93fb944 2022-05-10 stsp err = got_error_from_errno("fstat");
1714 e93fb944 2022-05-10 stsp goto done;
1716 e93fb944 2022-05-10 stsp if (sb.st_size > 0 && sb.st_size <= SIZE_MAX) {
1717 e93fb944 2022-05-10 stsp delta_cache_map = mmap(NULL, sb.st_size,
1718 e93fb944 2022-05-10 stsp PROT_READ, MAP_PRIVATE, delta_cache_fd, 0);
1719 e93fb944 2022-05-10 stsp if (delta_cache_map == MAP_FAILED) {
1720 e93fb944 2022-05-10 stsp if (errno != ENOMEM) {
1721 e93fb944 2022-05-10 stsp err = got_error_from_errno("mmap");
1722 e93fb944 2022-05-10 stsp goto done;
1724 e93fb944 2022-05-10 stsp delta_cache_map = NULL; /* fallback on stdio */
1726 e93fb944 2022-05-10 stsp delta_cache_size = (size_t)sb.st_size;
1730 894e4711 2022-10-15 stsp err = hwrite(packfd, "PACK", 4, &ctx);
1732 e93fb944 2022-05-10 stsp goto done;
1733 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1734 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, 4, &ctx);
1736 e6bcace5 2021-06-22 stsp goto done;
1737 67fd6849 2022-02-13 stsp putbe32(buf, ndeltify + nreuse);
1738 894e4711 2022-10-15 stsp err = hwrite(packfd, buf, 4, &ctx);
1740 e6bcace5 2021-06-22 stsp goto done;
1742 67fd6849 2022-02-13 stsp qsort(deltify, ndeltify, sizeof(struct got_pack_meta *),
1743 67fd6849 2022-02-13 stsp write_order_cmp);
1744 67fd6849 2022-02-13 stsp for (i = 0; i < ndeltify; i++) {
1745 301e83b3 2022-10-16 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
1746 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
1747 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, i);
1749 211cfef0 2022-01-05 stsp goto done;
1750 67fd6849 2022-02-13 stsp m = deltify[i];
1751 894e4711 2022-10-15 stsp err = write_packed_object(&packfile_size, packfd,
1752 e93fb944 2022-05-10 stsp delta_cache, delta_cache_map, delta_cache_size,
1753 e93fb944 2022-05-10 stsp m, &outfd, &ctx, repo);
1755 67fd6849 2022-02-13 stsp goto done;
1758 67fd6849 2022-02-13 stsp qsort(reuse, nreuse, sizeof(struct got_pack_meta *),
1759 67fd6849 2022-02-13 stsp reuse_write_order_cmp);
1760 24b7de1c 2022-12-03 stsp if (nreuse > 0 && reuse_pack->map == NULL) {
1761 24b7de1c 2022-12-03 stsp int fd = dup(reuse_pack->fd);
1762 24b7de1c 2022-12-03 stsp if (fd == -1) {
1763 24b7de1c 2022-12-03 stsp err = got_error_from_errno("dup");
1764 24b7de1c 2022-12-03 stsp goto done;
1766 24b7de1c 2022-12-03 stsp packfile = fdopen(fd, "r");
1767 24b7de1c 2022-12-03 stsp if (packfile == NULL) {
1768 24b7de1c 2022-12-03 stsp err = got_error_from_errno("fdopen");
1769 24b7de1c 2022-12-03 stsp close(fd);
1770 24b7de1c 2022-12-03 stsp goto done;
1773 67fd6849 2022-02-13 stsp for (i = 0; i < nreuse; i++) {
1774 301e83b3 2022-10-16 stsp err = got_pack_report_progress(progress_cb, progress_arg, rl,
1775 b8af7c06 2022-03-15 stsp ncolored, nfound, ntrees, packfile_size, nours,
1776 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse, ndeltify + i);
1778 67fd6849 2022-02-13 stsp goto done;
1779 67fd6849 2022-02-13 stsp m = reuse[i];
1780 894e4711 2022-10-15 stsp err = write_packed_object(&packfile_size, packfd,
1781 24b7de1c 2022-12-03 stsp packfile, reuse_pack->map, reuse_pack->filesize,
1782 e93fb944 2022-05-10 stsp m, &outfd, &ctx, repo);
1784 67fd6849 2022-02-13 stsp goto done;
1787 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1788 13b2bc37 2022-10-23 stsp err = got_poll_write_full(packfd, pack_sha1, SHA1_DIGEST_LENGTH);
1790 894e4711 2022-10-15 stsp goto done;
1791 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1792 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1793 211cfef0 2022-01-05 stsp if (progress_cb) {
1794 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1795 b8af7c06 2022-03-15 stsp packfile_size, nours, ndeltify + nreuse,
1796 b8af7c06 2022-03-15 stsp ndeltify + nreuse, ndeltify + nreuse);
1798 211cfef0 2022-01-05 stsp goto done;
1801 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
1802 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
1803 e93fb944 2022-05-10 stsp if (delta_cache_map && munmap(delta_cache_map, delta_cache_size) == -1)
1804 e93fb944 2022-05-10 stsp err = got_error_from_errno("munmap");
1805 e93fb944 2022-05-10 stsp if (delta_cache_fd != -1 && close(delta_cache_fd) == -1 && err == NULL)
1806 e93fb944 2022-05-10 stsp err = got_error_from_errno("close");
1807 24b7de1c 2022-12-03 stsp if (packfile && fclose(packfile) == EOF && err == NULL)
1808 24b7de1c 2022-12-03 stsp err = got_error_from_errno("fclose");
1809 e6bcace5 2021-06-22 stsp return err;
1812 67fd6849 2022-02-13 stsp static const struct got_error *
1813 67fd6849 2022-02-13 stsp add_meta_idset_cb(struct got_object_id *id, void *data, void *arg)
1815 67fd6849 2022-02-13 stsp struct got_pack_meta *m = data;
1816 67fd6849 2022-02-13 stsp struct got_pack_metavec *v = arg;
1818 411cbec1 2022-05-20 stsp if (m->reused_delta_offset != 0)
1819 f5e78e05 2022-05-04 stsp return NULL;
1821 301e83b3 2022-10-16 stsp return got_pack_add_meta(m, v);
1824 e6bcace5 2021-06-22 stsp const struct got_error *
1825 a32780aa 2022-10-15 stsp got_pack_create(uint8_t *packsha1, int packfd, FILE *delta_cache,
1826 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1827 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1828 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1829 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1830 cae60ab8 2022-10-18 stsp struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
1832 e6bcace5 2021-06-22 stsp const struct got_error *err;
1833 67fd6849 2022-02-13 stsp struct got_object_idset *idset;
1834 24b7de1c 2022-12-03 stsp struct got_packidx *reuse_packidx = NULL;
1835 24b7de1c 2022-12-03 stsp struct got_pack *reuse_pack = NULL;
1836 67fd6849 2022-02-13 stsp struct got_pack_metavec deltify, reuse;
1837 b8af7c06 2022-03-15 stsp int ncolored = 0, nfound = 0, ntrees = 0;
1838 f5e78e05 2022-05-04 stsp size_t ndeltify;
1839 d6a28ffe 2022-05-20 op uint32_t seed;
1841 d6a28ffe 2022-05-20 op seed = arc4random();
1843 67fd6849 2022-02-13 stsp memset(&deltify, 0, sizeof(deltify));
1844 67fd6849 2022-02-13 stsp memset(&reuse, 0, sizeof(reuse));
1846 67fd6849 2022-02-13 stsp idset = got_object_idset_alloc();
1847 67fd6849 2022-02-13 stsp if (idset == NULL)
1848 67fd6849 2022-02-13 stsp return got_error_from_errno("got_object_idset_alloc");
1850 b8af7c06 2022-03-15 stsp err = load_object_ids(&ncolored, &nfound, &ntrees, idset, theirs,
1851 d6a28ffe 2022-05-20 op ntheirs, ours, nours, repo, seed, loose_obj_only,
1852 cae60ab8 2022-10-18 stsp progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1854 17259bfa 2022-05-19 stsp goto done;
1856 28526235 2022-02-13 stsp if (progress_cb) {
1857 b8af7c06 2022-03-15 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1858 b8af7c06 2022-03-15 stsp 0L, nours, got_object_idset_num_elements(idset), 0, 0);
1860 28526235 2022-02-13 stsp goto done;
1863 67fd6849 2022-02-13 stsp if (got_object_idset_num_elements(idset) == 0 && !allow_empty) {
1864 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1865 05118f5a 2021-06-22 stsp goto done;
1868 67fd6849 2022-02-13 stsp reuse.metasz = 64;
1869 67fd6849 2022-02-13 stsp reuse.meta = calloc(reuse.metasz,
1870 67fd6849 2022-02-13 stsp sizeof(struct got_pack_meta *));
1871 67fd6849 2022-02-13 stsp if (reuse.meta == NULL) {
1872 67fd6849 2022-02-13 stsp err = got_error_from_errno("calloc");
1873 67fd6849 2022-02-13 stsp goto done;
1876 24b7de1c 2022-12-03 stsp err = got_pack_search_deltas(&reuse_packidx, &reuse_pack,
1877 24b7de1c 2022-12-03 stsp &reuse, idset, ncolored, nfound, ntrees, nours,
1878 cae60ab8 2022-10-18 stsp repo, progress_cb, progress_arg, rl, cancel_cb, cancel_arg);
1880 67fd6849 2022-02-13 stsp goto done;
1882 24b7de1c 2022-12-03 stsp if (reuse_packidx && reuse_pack) {
1883 24b7de1c 2022-12-03 stsp err = got_repo_pin_pack(repo, reuse_packidx, reuse_pack);
1885 24b7de1c 2022-12-03 stsp goto done;
1888 67fd6849 2022-02-13 stsp if (fseeko(delta_cache, 0L, SEEK_END) == -1) {
1889 67fd6849 2022-02-13 stsp err = got_error_from_errno("fseeko");
1890 67fd6849 2022-02-13 stsp goto done;
1893 f5e78e05 2022-05-04 stsp ndeltify = got_object_idset_num_elements(idset) - reuse.nmeta;
1894 f5e78e05 2022-05-04 stsp if (ndeltify > 0) {
1895 f5e78e05 2022-05-04 stsp deltify.meta = calloc(ndeltify, sizeof(struct got_pack_meta *));
1896 f5e78e05 2022-05-04 stsp if (deltify.meta == NULL) {
1897 f5e78e05 2022-05-04 stsp err = got_error_from_errno("calloc");
1898 f5e78e05 2022-05-04 stsp goto done;
1900 f5e78e05 2022-05-04 stsp deltify.metasz = ndeltify;
1902 f5e78e05 2022-05-04 stsp err = got_object_idset_for_each(idset, add_meta_idset_cb,
1903 f5e78e05 2022-05-04 stsp &deltify);
1905 f8a36e22 2021-08-26 stsp goto done;
1906 f5e78e05 2022-05-04 stsp if (deltify.nmeta > 0) {
1907 f5e78e05 2022-05-04 stsp err = pick_deltas(deltify.meta, deltify.nmeta,
1908 f5e78e05 2022-05-04 stsp ncolored, nfound, ntrees, nours, reuse.nmeta,
1909 cae60ab8 2022-10-18 stsp delta_cache, repo, progress_cb, progress_arg, rl,
1910 f5e78e05 2022-05-04 stsp cancel_cb, cancel_arg);
1912 f5e78e05 2022-05-04 stsp goto done;
1916 2d9e6abf 2022-05-04 stsp if (fflush(delta_cache) == EOF) {
1917 2d9e6abf 2022-05-04 stsp err = got_error_from_errno("fflush");
1918 2d9e6abf 2022-05-04 stsp goto done;
1921 13b2bc37 2022-10-23 stsp if (progress_cb) {
1923 13b2bc37 2022-10-23 stsp * Report a 1-byte packfile write to indicate we are about
1924 13b2bc37 2022-10-23 stsp * to start sending packfile data. gotd(8) needs this.
1926 13b2bc37 2022-10-23 stsp err = progress_cb(progress_arg, ncolored, nfound, ntrees,
1927 13b2bc37 2022-10-23 stsp 1 /* packfile_size */, nours,
1928 13b2bc37 2022-10-23 stsp got_object_idset_num_elements(idset),
1929 13b2bc37 2022-10-23 stsp deltify.nmeta + reuse.nmeta, 0);
1931 13b2bc37 2022-10-23 stsp goto done;
1934 24b7de1c 2022-12-03 stsp err = genpack(packsha1, packfd, reuse_pack, delta_cache, deltify.meta,
1935 b8af7c06 2022-03-15 stsp deltify.nmeta, reuse.meta, reuse.nmeta, ncolored, nfound, ntrees,
1936 cae60ab8 2022-10-18 stsp nours, repo, progress_cb, progress_arg, rl,
1937 b8af7c06 2022-03-15 stsp cancel_cb, cancel_arg);
1939 e6bcace5 2021-06-22 stsp goto done;
1941 67fd6849 2022-02-13 stsp free_nmeta(deltify.meta, deltify.nmeta);
1942 67fd6849 2022-02-13 stsp free_nmeta(reuse.meta, reuse.nmeta);
1943 67fd6849 2022-02-13 stsp got_object_idset_free(idset);
1944 24b7de1c 2022-12-03 stsp got_repo_unpin_pack(repo);
1945 e6bcace5 2021-06-22 stsp return err;