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