Blame


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