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