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 cfe41121 2021-10-16 thomas #include <sys/queue.h>
20 cfe41121 2021-10-16 thomas #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 e6bcace5 2021-06-22 stsp
24 08736cf9 2021-06-23 stsp #include <stdint.h>
25 e6bcace5 2021-06-22 stsp #include <stdio.h>
26 e6bcace5 2021-06-22 stsp #include <stdlib.h>
27 e6bcace5 2021-06-22 stsp #include <string.h>
28 e6bcace5 2021-06-22 stsp #include <limits.h>
29 e6bcace5 2021-06-22 stsp #include <zlib.h>
30 b347007e 2021-10-15 thomas
31 b347007e 2021-10-15 thomas #if defined(__FreeBSD__)
32 b347007e 2021-10-15 thomas #include <unistd.h>
33 b347007e 2021-10-15 thomas #endif
34 e6bcace5 2021-06-22 stsp
35 e6bcace5 2021-06-22 stsp #include "got_error.h"
36 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
37 e6bcace5 2021-06-22 stsp #include "got_object.h"
38 0be8fa4c 2021-10-15 thomas #include "got_path.h"
39 05118f5a 2021-06-22 stsp #include "got_reference.h"
40 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
41 e4d8ab47 2021-10-15 thomas #include "got_opentemp.h"
42 e6bcace5 2021-06-22 stsp
43 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
44 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
45 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
46 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
47 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
48 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
49 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
50 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
51 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
52 e6bcace5 2021-06-22 stsp
53 b79280dd 2021-10-15 thomas #ifndef MIN
54 b79280dd 2021-10-15 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
55 b79280dd 2021-10-15 thomas #endif
56 b79280dd 2021-10-15 thomas
57 e6bcace5 2021-06-22 stsp #ifndef MAX
58 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
59 e6bcace5 2021-06-22 stsp #endif
60 e6bcace5 2021-06-22 stsp
61 e6bcace5 2021-06-22 stsp struct got_pack_meta {
62 e6bcace5 2021-06-22 stsp struct got_object_id id;
63 e6bcace5 2021-06-22 stsp char *path;
64 e6bcace5 2021-06-22 stsp int obj_type;
65 ab6186ae 2021-10-15 thomas off_t size;
66 e6bcace5 2021-06-22 stsp time_t mtime;
67 e6bcace5 2021-06-22 stsp
68 e6bcace5 2021-06-22 stsp /* The best delta we picked */
69 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
70 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
71 b79280dd 2021-10-15 thomas off_t delta_offset; /* offset in delta cache file */
72 b79280dd 2021-10-15 thomas off_t delta_len; /* length in delta cache file */
73 e6bcace5 2021-06-22 stsp int nchain;
74 e6bcace5 2021-06-22 stsp
75 e6bcace5 2021-06-22 stsp /* Only used for delta window */
76 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
77 e6bcace5 2021-06-22 stsp
78 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
79 e6bcace5 2021-06-22 stsp off_t off;
80 e6bcace5 2021-06-22 stsp };
81 e6bcace5 2021-06-22 stsp
82 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
83 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
84 e6bcace5 2021-06-22 stsp int nmeta;
85 e6bcace5 2021-06-22 stsp int metasz;
86 e6bcace5 2021-06-22 stsp };
87 e6bcace5 2021-06-22 stsp
88 e6bcace5 2021-06-22 stsp static const struct got_error *
89 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
90 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
91 e6bcace5 2021-06-22 stsp {
92 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
93 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
94 e6bcace5 2021-06-22 stsp
95 e6bcace5 2021-06-22 stsp *new = NULL;
96 e6bcace5 2021-06-22 stsp
97 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
98 e6bcace5 2021-06-22 stsp if (m == NULL)
99 b7e0a384 2021-10-15 thomas return got_error_from_errno("calloc");
100 e6bcace5 2021-06-22 stsp
101 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
102 e6bcace5 2021-06-22 stsp
103 e6bcace5 2021-06-22 stsp m->path = strdup(path);
104 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
105 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
106 e6bcace5 2021-06-22 stsp free(m);
107 e6bcace5 2021-06-22 stsp return err;
108 e6bcace5 2021-06-22 stsp }
109 e6bcace5 2021-06-22 stsp
110 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
111 e6bcace5 2021-06-22 stsp m->mtime = mtime;
112 e6bcace5 2021-06-22 stsp *new = m;
113 e6bcace5 2021-06-22 stsp return NULL;
114 e6bcace5 2021-06-22 stsp }
115 e6bcace5 2021-06-22 stsp
116 e6bcace5 2021-06-22 stsp static void
117 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
118 e6bcace5 2021-06-22 stsp {
119 e6bcace5 2021-06-22 stsp if (meta == NULL)
120 e6bcace5 2021-06-22 stsp return;
121 e6bcace5 2021-06-22 stsp free(meta->path);
122 e6bcace5 2021-06-22 stsp meta->path = NULL;
123 e6bcace5 2021-06-22 stsp }
124 e6bcace5 2021-06-22 stsp
125 e6bcace5 2021-06-22 stsp static void
126 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
127 e6bcace5 2021-06-22 stsp {
128 e6bcace5 2021-06-22 stsp int i;
129 e6bcace5 2021-06-22 stsp
130 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
131 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
132 e6bcace5 2021-06-22 stsp free(meta);
133 e6bcace5 2021-06-22 stsp }
134 e6bcace5 2021-06-22 stsp
135 e6bcace5 2021-06-22 stsp static int
136 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
137 e6bcace5 2021-06-22 stsp {
138 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
139 e6bcace5 2021-06-22 stsp int cmp;
140 e6bcace5 2021-06-22 stsp
141 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
142 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
143 e6bcace5 2021-06-22 stsp
144 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
145 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
146 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
147 e6bcace5 2021-06-22 stsp if (cmp != 0)
148 e6bcace5 2021-06-22 stsp return cmp;
149 e6bcace5 2021-06-22 stsp if (a->mtime != b->mtime)
150 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
151 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
152 e6bcace5 2021-06-22 stsp }
153 e6bcace5 2021-06-22 stsp
154 e6bcace5 2021-06-22 stsp static int
155 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
156 e6bcace5 2021-06-22 stsp {
157 e6bcace5 2021-06-22 stsp int i, size = 32;
158 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
159 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
160 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
161 e6bcace5 2021-06-22 stsp else
162 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
163 e6bcace5 2021-06-22 stsp }
164 e6bcace5 2021-06-22 stsp return size;
165 e6bcace5 2021-06-22 stsp }
166 e6bcace5 2021-06-22 stsp
167 b79280dd 2021-10-15 thomas static const struct got_error *
168 b79280dd 2021-10-15 thomas encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
169 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas, int ndeltas,
170 d0772de9 2021-10-15 thomas off_t base_size, FILE *f)
171 d0772de9 2021-10-15 thomas {
172 d0772de9 2021-10-15 thomas unsigned char buf[16], *bp;
173 d0772de9 2021-10-15 thomas int i, j;
174 d0772de9 2021-10-15 thomas off_t n;
175 d0772de9 2021-10-15 thomas size_t w;
176 d0772de9 2021-10-15 thomas struct got_delta_instruction *d;
177 d0772de9 2021-10-15 thomas
178 d0772de9 2021-10-15 thomas /* base object size */
179 d0772de9 2021-10-15 thomas buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
180 d0772de9 2021-10-15 thomas n = base_size >> GOT_DELTA_SIZE_SHIFT;
181 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
182 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
183 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
184 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
185 d0772de9 2021-10-15 thomas }
186 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, i, f);
187 d0772de9 2021-10-15 thomas if (w != i)
188 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
189 d0772de9 2021-10-15 thomas
190 d0772de9 2021-10-15 thomas /* target object size */
191 d0772de9 2021-10-15 thomas buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
192 d0772de9 2021-10-15 thomas n = o->size >> GOT_DELTA_SIZE_SHIFT;
193 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
194 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
195 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
196 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
197 d0772de9 2021-10-15 thomas }
198 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, i, f);
199 d0772de9 2021-10-15 thomas if (w != i)
200 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
201 d0772de9 2021-10-15 thomas
202 d0772de9 2021-10-15 thomas for (j = 0; j < ndeltas; j++) {
203 d0772de9 2021-10-15 thomas d = &deltas[j];
204 d0772de9 2021-10-15 thomas if (d->copy) {
205 d0772de9 2021-10-15 thomas n = d->offset;
206 d0772de9 2021-10-15 thomas bp = &buf[1];
207 d0772de9 2021-10-15 thomas buf[0] = GOT_DELTA_BASE_COPY;
208 d0772de9 2021-10-15 thomas for (i = 0; i < 4; i++) {
209 d0772de9 2021-10-15 thomas /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
210 d0772de9 2021-10-15 thomas buf[0] |= 1 << i;
211 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
212 d0772de9 2021-10-15 thomas n >>= 8;
213 d0772de9 2021-10-15 thomas if (n == 0)
214 d0772de9 2021-10-15 thomas break;
215 d0772de9 2021-10-15 thomas }
216 d0772de9 2021-10-15 thomas
217 d0772de9 2021-10-15 thomas n = d->len;
218 d0772de9 2021-10-15 thomas if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
219 d0772de9 2021-10-15 thomas /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
220 d0772de9 2021-10-15 thomas for (i = 0; i < 3 && n > 0; i++) {
221 d0772de9 2021-10-15 thomas buf[0] |= 1 << (i + 4);
222 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
223 d0772de9 2021-10-15 thomas n >>= 8;
224 d0772de9 2021-10-15 thomas }
225 d0772de9 2021-10-15 thomas }
226 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, bp - buf, f);
227 d0772de9 2021-10-15 thomas if (w != bp - buf)
228 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
229 d0772de9 2021-10-15 thomas } else {
230 d0772de9 2021-10-15 thomas char content[128];
231 d0772de9 2021-10-15 thomas size_t r;
232 d0772de9 2021-10-15 thomas if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
233 d0772de9 2021-10-15 thomas return got_error_from_errno("fseeko");
234 d0772de9 2021-10-15 thomas n = 0;
235 d0772de9 2021-10-15 thomas while (n != d->len) {
236 d0772de9 2021-10-15 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
237 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, 1, f);
238 d0772de9 2021-10-15 thomas if (w != 1)
239 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
240 d0772de9 2021-10-15 thomas r = fread(content, 1, buf[0], o->f);
241 d0772de9 2021-10-15 thomas if (r != buf[0])
242 d0772de9 2021-10-15 thomas return got_ferror(o->f, GOT_ERR_IO);
243 d0772de9 2021-10-15 thomas w = fwrite(content, 1, buf[0], f);
244 d0772de9 2021-10-15 thomas if (w != buf[0])
245 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
246 d0772de9 2021-10-15 thomas n += buf[0];
247 d0772de9 2021-10-15 thomas }
248 d0772de9 2021-10-15 thomas }
249 d0772de9 2021-10-15 thomas }
250 e6bcace5 2021-06-22 stsp
251 d0772de9 2021-10-15 thomas return NULL;
252 d0772de9 2021-10-15 thomas }
253 d0772de9 2021-10-15 thomas
254 d0772de9 2021-10-15 thomas
255 e6bcace5 2021-06-22 stsp static const struct got_error *
256 05118f5a 2021-06-22 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
257 b79280dd 2021-10-15 thomas FILE *delta_cache, struct got_repository *repo,
258 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
259 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
260 e6bcace5 2021-06-22 stsp {
261 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
262 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
263 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
264 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
265 b79280dd 2021-10-15 thomas int i, j, size, best_size, ndeltas, best_ndeltas;
266 e6bcace5 2021-06-22 stsp const int max_base_candidates = 10;
267 ecf9545f 2021-10-15 thomas int outfd = -1;
268 e6bcace5 2021-06-22 stsp
269 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
270 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
271 e6bcace5 2021-06-22 stsp if (cancel_cb) {
272 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
273 e6bcace5 2021-06-22 stsp if (err)
274 e6bcace5 2021-06-22 stsp break;
275 e6bcace5 2021-06-22 stsp }
276 05118f5a 2021-06-22 stsp if (progress_cb) {
277 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
278 05118f5a 2021-06-22 stsp if (err)
279 05118f5a 2021-06-22 stsp goto done;
280 05118f5a 2021-06-22 stsp }
281 e6bcace5 2021-06-22 stsp m = meta[i];
282 e6bcace5 2021-06-22 stsp
283 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
284 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
285 e6bcace5 2021-06-22 stsp continue;
286 e6bcace5 2021-06-22 stsp
287 c565dfd3 2021-10-16 thomas err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
288 e6bcace5 2021-06-22 stsp if (err)
289 e6bcace5 2021-06-22 stsp goto done;
290 ab6186ae 2021-10-15 thomas m->size = raw->size;
291 e6bcace5 2021-06-22 stsp
292 e6bcace5 2021-06-22 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
293 e6bcace5 2021-06-22 stsp raw->size + raw->hdrlen);
294 e6bcace5 2021-06-22 stsp if (err)
295 e6bcace5 2021-06-22 stsp goto done;
296 e6bcace5 2021-06-22 stsp
297 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
298 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
299 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
300 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
301 e6bcace5 2021-06-22 stsp n->dtab = NULL;
302 e6bcace5 2021-06-22 stsp }
303 e6bcace5 2021-06-22 stsp
304 b79280dd 2021-10-15 thomas best_size = raw->size;
305 b79280dd 2021-10-15 thomas best_ndeltas = 0;
306 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
307 e6bcace5 2021-06-22 stsp if (cancel_cb) {
308 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
309 e6bcace5 2021-06-22 stsp if (err)
310 e6bcace5 2021-06-22 stsp goto done;
311 e6bcace5 2021-06-22 stsp }
312 e6bcace5 2021-06-22 stsp base = meta[j];
313 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
314 33acf1a2 2021-10-15 thomas if (base->nchain >= 32 ||
315 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
316 e6bcace5 2021-06-22 stsp continue;
317 e6bcace5 2021-06-22 stsp
318 8ab9215c 2021-10-15 thomas err = got_object_raw_open(&base_raw, &outfd, repo,
319 c565dfd3 2021-10-16 thomas &base->id, 8192);
320 e6bcace5 2021-06-22 stsp if (err)
321 e6bcace5 2021-06-22 stsp goto done;
322 e6bcace5 2021-06-22 stsp err = got_deltify(&deltas, &ndeltas,
323 e6bcace5 2021-06-22 stsp raw->f, raw->hdrlen, raw->size + raw->hdrlen,
324 e6bcace5 2021-06-22 stsp base->dtab, base_raw->f, base_raw->hdrlen,
325 e6bcace5 2021-06-22 stsp base_raw->size + base_raw->hdrlen);
326 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
327 e6bcace5 2021-06-22 stsp base_raw = NULL;
328 e6bcace5 2021-06-22 stsp if (err)
329 e6bcace5 2021-06-22 stsp goto done;
330 e6bcace5 2021-06-22 stsp
331 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
332 b79280dd 2021-10-15 thomas if (size + 32 < best_size){
333 e6bcace5 2021-06-22 stsp /*
334 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
335 e6bcace5 2021-06-22 stsp * replace it.
336 e6bcace5 2021-06-22 stsp */
337 b79280dd 2021-10-15 thomas best_size = size;
338 b79280dd 2021-10-15 thomas free(best_deltas);
339 b79280dd 2021-10-15 thomas best_deltas = deltas;
340 b79280dd 2021-10-15 thomas best_ndeltas = ndeltas;
341 b79280dd 2021-10-15 thomas deltas = NULL;
342 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
343 e6bcace5 2021-06-22 stsp m->prev = base;
344 e6bcace5 2021-06-22 stsp m->head = base->head;
345 e6bcace5 2021-06-22 stsp if (m->head == NULL)
346 e6bcace5 2021-06-22 stsp m->head = base;
347 e6bcace5 2021-06-22 stsp } else {
348 e6bcace5 2021-06-22 stsp free(deltas);
349 e6bcace5 2021-06-22 stsp deltas = NULL;
350 e6bcace5 2021-06-22 stsp ndeltas = 0;
351 e6bcace5 2021-06-22 stsp }
352 e6bcace5 2021-06-22 stsp }
353 e6bcace5 2021-06-22 stsp
354 b79280dd 2021-10-15 thomas if (best_ndeltas > 0) {
355 b79280dd 2021-10-15 thomas m->delta_offset = ftello(delta_cache);
356 b79280dd 2021-10-15 thomas err = encode_delta(m, raw, best_deltas,
357 b79280dd 2021-10-15 thomas best_ndeltas, m->prev->size, delta_cache);
358 b79280dd 2021-10-15 thomas free(best_deltas);
359 b79280dd 2021-10-15 thomas best_deltas = NULL;
360 b79280dd 2021-10-15 thomas best_ndeltas = 0;
361 b79280dd 2021-10-15 thomas if (err)
362 b79280dd 2021-10-15 thomas goto done;
363 b79280dd 2021-10-15 thomas m->delta_len = ftello(delta_cache) - m->delta_offset;
364 b79280dd 2021-10-15 thomas }
365 b79280dd 2021-10-15 thomas
366 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
367 e6bcace5 2021-06-22 stsp raw = NULL;
368 e6bcace5 2021-06-22 stsp }
369 e6bcace5 2021-06-22 stsp done:
370 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
371 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
372 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
373 e6bcace5 2021-06-22 stsp }
374 e6bcace5 2021-06-22 stsp if (raw)
375 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
376 e6bcace5 2021-06-22 stsp if (base_raw)
377 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
378 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
379 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
380 b79280dd 2021-10-15 thomas free(deltas);
381 b79280dd 2021-10-15 thomas free(best_deltas);
382 e6bcace5 2021-06-22 stsp return err;
383 e6bcace5 2021-06-22 stsp }
384 e6bcace5 2021-06-22 stsp
385 e6bcace5 2021-06-22 stsp static const struct got_error *
386 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
387 05118f5a 2021-06-22 stsp struct got_repository *repo)
388 05118f5a 2021-06-22 stsp {
389 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
390 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
391 05118f5a 2021-06-22 stsp int idx;
392 05118f5a 2021-06-22 stsp
393 05118f5a 2021-06-22 stsp *found = 0;
394 05118f5a 2021-06-22 stsp
395 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
396 05118f5a 2021-06-22 stsp if (err == NULL)
397 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
398 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
399 05118f5a 2021-06-22 stsp err = NULL;
400 05118f5a 2021-06-22 stsp return err;
401 05118f5a 2021-06-22 stsp }
402 07165b17 2021-07-01 stsp
403 07165b17 2021-07-01 stsp static const int obj_types[] = {
404 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_ANY,
405 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_COMMIT,
406 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TREE,
407 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_BLOB,
408 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TAG,
409 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_OFFSET_DELTA,
410 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_REF_DELTA
411 07165b17 2021-07-01 stsp };
412 05118f5a 2021-06-22 stsp
413 05118f5a 2021-06-22 stsp static const struct got_error *
414 e6bcace5 2021-06-22 stsp add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
415 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
416 05118f5a 2021-06-22 stsp time_t mtime, int loose_obj_only, struct got_repository *repo)
417 e6bcace5 2021-06-22 stsp {
418 e6bcace5 2021-06-22 stsp const struct got_error *err;
419 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
420 e6bcace5 2021-06-22 stsp
421 05118f5a 2021-06-22 stsp if (loose_obj_only) {
422 05118f5a 2021-06-22 stsp int is_packed;
423 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
424 05118f5a 2021-06-22 stsp if (err)
425 05118f5a 2021-06-22 stsp return err;
426 05118f5a 2021-06-22 stsp if (is_packed)
427 05118f5a 2021-06-22 stsp return NULL;
428 05118f5a 2021-06-22 stsp }
429 05118f5a 2021-06-22 stsp
430 07165b17 2021-07-01 stsp err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
431 e6bcace5 2021-06-22 stsp if (err)
432 e6bcace5 2021-06-22 stsp return err;
433 e6bcace5 2021-06-22 stsp
434 e6bcace5 2021-06-22 stsp if (v == NULL)
435 e6bcace5 2021-06-22 stsp return NULL;
436 e6bcace5 2021-06-22 stsp
437 e6bcace5 2021-06-22 stsp err = alloc_meta(&m, id, path, obj_type, mtime);
438 e6bcace5 2021-06-22 stsp if (err)
439 e6bcace5 2021-06-22 stsp goto done;
440 e6bcace5 2021-06-22 stsp
441 e6bcace5 2021-06-22 stsp if (v->nmeta == v->metasz){
442 e6bcace5 2021-06-22 stsp size_t newsize = 2 * v->metasz;
443 e6bcace5 2021-06-22 stsp struct got_pack_meta **new;
444 e6bcace5 2021-06-22 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
445 e6bcace5 2021-06-22 stsp if (new == NULL) {
446 e6bcace5 2021-06-22 stsp err = got_error_from_errno("reallocarray");
447 e6bcace5 2021-06-22 stsp goto done;
448 e6bcace5 2021-06-22 stsp }
449 e6bcace5 2021-06-22 stsp v->meta = new;
450 e6bcace5 2021-06-22 stsp v->metasz = newsize;
451 e6bcace5 2021-06-22 stsp }
452 e6bcace5 2021-06-22 stsp done:
453 e6bcace5 2021-06-22 stsp if (err) {
454 e6bcace5 2021-06-22 stsp clear_meta(m);
455 e6bcace5 2021-06-22 stsp free(m);
456 e6bcace5 2021-06-22 stsp } else
457 e6bcace5 2021-06-22 stsp v->meta[v->nmeta++] = m;
458 e6bcace5 2021-06-22 stsp
459 e6bcace5 2021-06-22 stsp return err;
460 e6bcace5 2021-06-22 stsp }
461 e6bcace5 2021-06-22 stsp
462 e6bcace5 2021-06-22 stsp static const struct got_error *
463 e6bcace5 2021-06-22 stsp load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
464 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
465 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
466 05118f5a 2021-06-22 stsp int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
467 e6bcace5 2021-06-22 stsp {
468 e6bcace5 2021-06-22 stsp const struct got_error *err;
469 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
470 e6bcace5 2021-06-22 stsp char *p = NULL;
471 e6bcace5 2021-06-22 stsp int i;
472 e6bcace5 2021-06-22 stsp
473 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
474 e6bcace5 2021-06-22 stsp if (err)
475 e6bcace5 2021-06-22 stsp return err;
476 e6bcace5 2021-06-22 stsp
477 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
478 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
479 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
480 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
481 e6bcace5 2021-06-22 stsp
482 e6bcace5 2021-06-22 stsp if (cancel_cb) {
483 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
484 e6bcace5 2021-06-22 stsp if (err)
485 e6bcace5 2021-06-22 stsp break;
486 e6bcace5 2021-06-22 stsp }
487 e6bcace5 2021-06-22 stsp
488 08511b5e 2021-09-24 thomas if (got_object_tree_entry_is_submodule(e) ||
489 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
490 e6bcace5 2021-06-22 stsp continue;
491 e6bcace5 2021-06-22 stsp
492 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
493 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
494 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
495 e6bcace5 2021-06-22 stsp break;
496 e6bcace5 2021-06-22 stsp }
497 e6bcace5 2021-06-22 stsp
498 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
499 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
500 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
501 e6bcace5 2021-06-22 stsp if (err)
502 e6bcace5 2021-06-22 stsp break;
503 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
504 08511b5e 2021-09-24 thomas } else if (S_ISREG(mode) || S_ISLNK(mode)) {
505 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
506 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
507 e6bcace5 2021-06-22 stsp if (err)
508 e6bcace5 2021-06-22 stsp break;
509 e6bcace5 2021-06-22 stsp }
510 e6bcace5 2021-06-22 stsp free(p);
511 e6bcace5 2021-06-22 stsp p = NULL;
512 e6bcace5 2021-06-22 stsp }
513 e6bcace5 2021-06-22 stsp
514 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
515 e6bcace5 2021-06-22 stsp free(p);
516 e6bcace5 2021-06-22 stsp return err;
517 e6bcace5 2021-06-22 stsp }
518 e6bcace5 2021-06-22 stsp
519 e6bcace5 2021-06-22 stsp static const struct got_error *
520 e6bcace5 2021-06-22 stsp load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
521 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
522 05118f5a 2021-06-22 stsp int loose_obj_only, struct got_repository *repo,
523 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
524 e6bcace5 2021-06-22 stsp {
525 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
526 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
527 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
528 e6bcace5 2021-06-22 stsp
529 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
530 e6bcace5 2021-06-22 stsp return NULL;
531 e6bcace5 2021-06-22 stsp
532 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
533 e6bcace5 2021-06-22 stsp if (err)
534 e6bcace5 2021-06-22 stsp return err;
535 e6bcace5 2021-06-22 stsp
536 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
537 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
538 e6bcace5 2021-06-22 stsp
539 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
540 e6bcace5 2021-06-22 stsp if (cancel_cb) {
541 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
542 e6bcace5 2021-06-22 stsp if (err)
543 e6bcace5 2021-06-22 stsp break;
544 e6bcace5 2021-06-22 stsp }
545 e6bcace5 2021-06-22 stsp
546 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
547 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
548 e6bcace5 2021-06-22 stsp
549 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
550 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
551 e6bcace5 2021-06-22 stsp continue;
552 e6bcace5 2021-06-22 stsp }
553 e6bcace5 2021-06-22 stsp
554 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
555 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
556 e6bcace5 2021-06-22 stsp if (err) {
557 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
558 e6bcace5 2021-06-22 stsp break;
559 e6bcace5 2021-06-22 stsp }
560 e6bcace5 2021-06-22 stsp
561 e6bcace5 2021-06-22 stsp err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
562 05118f5a 2021-06-22 stsp mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
563 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
564 e6bcace5 2021-06-22 stsp if (err)
565 e6bcace5 2021-06-22 stsp break;
566 e6bcace5 2021-06-22 stsp }
567 e6bcace5 2021-06-22 stsp
568 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
569 e6bcace5 2021-06-22 stsp return err;
570 e6bcace5 2021-06-22 stsp }
571 e6bcace5 2021-06-22 stsp
572 e6bcace5 2021-06-22 stsp static const struct got_error *
573 e6bcace5 2021-06-22 stsp load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
574 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
575 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
576 e6bcace5 2021-06-22 stsp {
577 e6bcace5 2021-06-22 stsp const struct got_error *err;
578 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
579 e6bcace5 2021-06-22 stsp
580 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
581 e6bcace5 2021-06-22 stsp return NULL;
582 05118f5a 2021-06-22 stsp
583 05118f5a 2021-06-22 stsp if (loose_obj_only) {
584 05118f5a 2021-06-22 stsp int is_packed;
585 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
586 05118f5a 2021-06-22 stsp if (err)
587 05118f5a 2021-06-22 stsp return err;
588 05118f5a 2021-06-22 stsp if (is_packed)
589 05118f5a 2021-06-22 stsp return NULL;
590 05118f5a 2021-06-22 stsp }
591 e6bcace5 2021-06-22 stsp
592 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
593 e6bcace5 2021-06-22 stsp if (err)
594 e6bcace5 2021-06-22 stsp return err;
595 e6bcace5 2021-06-22 stsp
596 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
597 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
598 05118f5a 2021-06-22 stsp loose_obj_only, repo);
599 e6bcace5 2021-06-22 stsp if (err)
600 e6bcace5 2021-06-22 stsp goto done;
601 e6bcace5 2021-06-22 stsp
602 e6bcace5 2021-06-22 stsp err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
603 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
604 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
605 e6bcace5 2021-06-22 stsp done:
606 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
607 e6bcace5 2021-06-22 stsp return err;
608 e6bcace5 2021-06-22 stsp }
609 e6bcace5 2021-06-22 stsp
610 05118f5a 2021-06-22 stsp static const struct got_error *
611 05118f5a 2021-06-22 stsp load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
612 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
613 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
614 05118f5a 2021-06-22 stsp {
615 05118f5a 2021-06-22 stsp const struct got_error *err;
616 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
617 05118f5a 2021-06-22 stsp
618 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
619 05118f5a 2021-06-22 stsp return NULL;
620 05118f5a 2021-06-22 stsp
621 05118f5a 2021-06-22 stsp if (loose_obj_only) {
622 05118f5a 2021-06-22 stsp int is_packed;
623 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
624 05118f5a 2021-06-22 stsp if (err)
625 05118f5a 2021-06-22 stsp return err;
626 05118f5a 2021-06-22 stsp if (is_packed)
627 05118f5a 2021-06-22 stsp return NULL;
628 05118f5a 2021-06-22 stsp }
629 05118f5a 2021-06-22 stsp
630 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
631 05118f5a 2021-06-22 stsp if (err)
632 05118f5a 2021-06-22 stsp return err;
633 05118f5a 2021-06-22 stsp
634 05118f5a 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
635 05118f5a 2021-06-22 stsp got_object_tag_get_tagger_time(tag),
636 05118f5a 2021-06-22 stsp loose_obj_only, repo);
637 05118f5a 2021-06-22 stsp if (err)
638 05118f5a 2021-06-22 stsp goto done;
639 05118f5a 2021-06-22 stsp
640 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
641 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
642 26960ff7 2021-09-14 stsp err = load_commit(v, idset,
643 05118f5a 2021-06-22 stsp got_object_tag_get_object_id(tag), repo,
644 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
645 05118f5a 2021-06-22 stsp break;
646 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
647 05118f5a 2021-06-22 stsp err = load_tree(v, idset, got_object_tag_get_object_id(tag),
648 05118f5a 2021-06-22 stsp "", got_object_tag_get_tagger_time(tag),
649 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
650 05118f5a 2021-06-22 stsp break;
651 05118f5a 2021-06-22 stsp default:
652 05118f5a 2021-06-22 stsp break;
653 05118f5a 2021-06-22 stsp }
654 05118f5a 2021-06-22 stsp
655 05118f5a 2021-06-22 stsp done:
656 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
657 05118f5a 2021-06-22 stsp return err;
658 05118f5a 2021-06-22 stsp }
659 05118f5a 2021-06-22 stsp
660 e6bcace5 2021-06-22 stsp enum findtwixt_color {
661 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
662 e6bcace5 2021-06-22 stsp COLOR_DROP,
663 e6bcace5 2021-06-22 stsp COLOR_BLANK,
664 e6bcace5 2021-06-22 stsp };
665 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
666 e6bcace5 2021-06-22 stsp COLOR_KEEP,
667 e6bcace5 2021-06-22 stsp COLOR_DROP,
668 e6bcace5 2021-06-22 stsp COLOR_BLANK
669 e6bcace5 2021-06-22 stsp };
670 e6bcace5 2021-06-22 stsp
671 e6bcace5 2021-06-22 stsp static const struct got_error *
672 e6bcace5 2021-06-22 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
673 e6bcace5 2021-06-22 stsp int color, struct got_repository *repo)
674 e6bcace5 2021-06-22 stsp {
675 e6bcace5 2021-06-22 stsp const struct got_error *err;
676 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
677 e6bcace5 2021-06-22 stsp
678 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
679 e6bcace5 2021-06-22 stsp if (err)
680 e6bcace5 2021-06-22 stsp return err;
681 e6bcace5 2021-06-22 stsp
682 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
683 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
684 e6bcace5 2021-06-22 stsp return NULL;
685 e6bcace5 2021-06-22 stsp }
686 e6bcace5 2021-06-22 stsp
687 e6bcace5 2021-06-22 stsp static const struct got_error *
688 e6bcace5 2021-06-22 stsp drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
689 e6bcace5 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo,
690 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
691 e6bcace5 2021-06-22 stsp {
692 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
693 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
694 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
695 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
696 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
697 e6bcace5 2021-06-22 stsp
698 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
699 e6bcace5 2021-06-22 stsp
700 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
701 e6bcace5 2021-06-22 stsp if (err)
702 e6bcace5 2021-06-22 stsp return err;
703 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&ids, qid, entry);
704 e6bcace5 2021-06-22 stsp
705 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
706 e6bcace5 2021-06-22 stsp if (cancel_cb) {
707 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
708 e6bcace5 2021-06-22 stsp if (err)
709 e6bcace5 2021-06-22 stsp break;
710 e6bcace5 2021-06-22 stsp }
711 e6bcace5 2021-06-22 stsp
712 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
713 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
714 e6bcace5 2021-06-22 stsp
715 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id)) {
716 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
717 e6bcace5 2021-06-22 stsp continue;
718 e6bcace5 2021-06-22 stsp }
719 e6bcace5 2021-06-22 stsp
720 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, qid->id,
721 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
722 e6bcace5 2021-06-22 stsp if (err) {
723 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
724 e6bcace5 2021-06-22 stsp break;
725 e6bcace5 2021-06-22 stsp }
726 e6bcace5 2021-06-22 stsp
727 e6bcace5 2021-06-22 stsp if (!got_object_idset_contains(keep, qid->id)) {
728 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
729 e6bcace5 2021-06-22 stsp continue;
730 e6bcace5 2021-06-22 stsp }
731 e6bcace5 2021-06-22 stsp
732 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
733 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
734 e6bcace5 2021-06-22 stsp if (err)
735 e6bcace5 2021-06-22 stsp break;
736 e6bcace5 2021-06-22 stsp
737 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
738 e6bcace5 2021-06-22 stsp if (parents) {
739 e6bcace5 2021-06-22 stsp err = got_object_id_queue_copy(parents, &ids);
740 e6bcace5 2021-06-22 stsp if (err) {
741 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
742 e6bcace5 2021-06-22 stsp break;
743 e6bcace5 2021-06-22 stsp }
744 e6bcace5 2021-06-22 stsp }
745 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
746 e6bcace5 2021-06-22 stsp }
747 e6bcace5 2021-06-22 stsp
748 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
749 e6bcace5 2021-06-22 stsp return err;
750 e6bcace5 2021-06-22 stsp }
751 e6bcace5 2021-06-22 stsp
752 e6bcace5 2021-06-22 stsp struct append_id_arg {
753 e6bcace5 2021-06-22 stsp struct got_object_id **array;
754 e6bcace5 2021-06-22 stsp int idx;
755 e6bcace5 2021-06-22 stsp };
756 e6bcace5 2021-06-22 stsp
757 e6bcace5 2021-06-22 stsp static const struct got_error *
758 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
759 e6bcace5 2021-06-22 stsp {
760 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
761 e6bcace5 2021-06-22 stsp
762 e6bcace5 2021-06-22 stsp a->array[a->idx] = got_object_id_dup(id);
763 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
764 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
765 e6bcace5 2021-06-22 stsp
766 e6bcace5 2021-06-22 stsp a->idx++;
767 e6bcace5 2021-06-22 stsp return NULL;
768 e6bcace5 2021-06-22 stsp }
769 e6bcace5 2021-06-22 stsp
770 e6bcace5 2021-06-22 stsp static const struct got_error *
771 e6bcace5 2021-06-22 stsp findtwixt(struct got_object_id ***res, int *nres,
772 e6bcace5 2021-06-22 stsp struct got_object_id **head, int nhead,
773 e6bcace5 2021-06-22 stsp struct got_object_id **tail, int ntail,
774 e6bcace5 2021-06-22 stsp struct got_repository *repo,
775 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
776 e6bcace5 2021-06-22 stsp {
777 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
778 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
779 e6bcace5 2021-06-22 stsp struct got_object_idset *keep, *drop;
780 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
781 05118f5a 2021-06-22 stsp int i, ncolor, nkeep, obj_type;
782 e6bcace5 2021-06-22 stsp
783 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
784 e6bcace5 2021-06-22 stsp *res = NULL;
785 e6bcace5 2021-06-22 stsp *nres = 0;
786 e6bcace5 2021-06-22 stsp
787 e6bcace5 2021-06-22 stsp keep = got_object_idset_alloc();
788 e6bcace5 2021-06-22 stsp if (keep == NULL)
789 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
790 e6bcace5 2021-06-22 stsp
791 e6bcace5 2021-06-22 stsp drop = got_object_idset_alloc();
792 e6bcace5 2021-06-22 stsp if (drop == NULL) {
793 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_idset_alloc");
794 e6bcace5 2021-06-22 stsp goto done;
795 e6bcace5 2021-06-22 stsp }
796 e6bcace5 2021-06-22 stsp
797 05118f5a 2021-06-22 stsp for (i = 0; i < nhead; i++) {
798 05118f5a 2021-06-22 stsp struct got_object_id *id = head[i];
799 05118f5a 2021-06-22 stsp if (id == NULL)
800 05118f5a 2021-06-22 stsp continue;
801 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
802 05118f5a 2021-06-22 stsp if (err)
803 05118f5a 2021-06-22 stsp return err;
804 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
805 05118f5a 2021-06-22 stsp continue;
806 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
807 05118f5a 2021-06-22 stsp if (err)
808 05118f5a 2021-06-22 stsp goto done;
809 e6bcace5 2021-06-22 stsp }
810 05118f5a 2021-06-22 stsp for (i = 0; i < ntail; i++) {
811 05118f5a 2021-06-22 stsp struct got_object_id *id = tail[i];
812 05118f5a 2021-06-22 stsp if (id == NULL)
813 05118f5a 2021-06-22 stsp continue;
814 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
815 05118f5a 2021-06-22 stsp if (err)
816 05118f5a 2021-06-22 stsp return err;
817 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
818 05118f5a 2021-06-22 stsp continue;
819 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_DROP, repo);
820 05118f5a 2021-06-22 stsp if (err)
821 05118f5a 2021-06-22 stsp goto done;
822 e6bcace5 2021-06-22 stsp }
823 e6bcace5 2021-06-22 stsp
824 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
825 e6bcace5 2021-06-22 stsp int qcolor;
826 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
827 e6bcace5 2021-06-22 stsp qcolor = *((int *)qid->data);
828 e6bcace5 2021-06-22 stsp
829 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id))
830 e6bcace5 2021-06-22 stsp ncolor = COLOR_DROP;
831 e6bcace5 2021-06-22 stsp else if (got_object_idset_contains(keep, qid->id))
832 e6bcace5 2021-06-22 stsp ncolor = COLOR_KEEP;
833 e6bcace5 2021-06-22 stsp else
834 e6bcace5 2021-06-22 stsp ncolor = COLOR_BLANK;
835 e6bcace5 2021-06-22 stsp
836 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
837 e6bcace5 2021-06-22 stsp qcolor == COLOR_KEEP)) {
838 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
839 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
840 e6bcace5 2021-06-22 stsp continue;
841 e6bcace5 2021-06-22 stsp }
842 e6bcace5 2021-06-22 stsp
843 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
844 e6bcace5 2021-06-22 stsp err = drop_commit(keep, drop, qid->id, repo,
845 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
846 e6bcace5 2021-06-22 stsp if (err)
847 e6bcace5 2021-06-22 stsp goto done;
848 e6bcace5 2021-06-22 stsp } else if (ncolor == COLOR_BLANK) {
849 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
850 e6bcace5 2021-06-22 stsp struct got_object_id *id;
851 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
852 e6bcace5 2021-06-22 stsp struct got_object_qid *pid;
853 e6bcace5 2021-06-22 stsp
854 e6bcace5 2021-06-22 stsp id = got_object_id_dup(qid->id);
855 e6bcace5 2021-06-22 stsp if (id == NULL) {
856 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
857 e6bcace5 2021-06-22 stsp goto done;
858 e6bcace5 2021-06-22 stsp }
859 e6bcace5 2021-06-22 stsp if (qcolor == COLOR_KEEP)
860 07165b17 2021-07-01 stsp err = got_object_idset_add(keep, id,
861 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
862 e6bcace5 2021-06-22 stsp else
863 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, id,
864 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
865 e6bcace5 2021-06-22 stsp if (err) {
866 e6bcace5 2021-06-22 stsp free(id);
867 e6bcace5 2021-06-22 stsp goto done;
868 e6bcace5 2021-06-22 stsp }
869 e6bcace5 2021-06-22 stsp
870 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
871 e6bcace5 2021-06-22 stsp if (err) {
872 e6bcace5 2021-06-22 stsp free(id);
873 e6bcace5 2021-06-22 stsp goto done;
874 e6bcace5 2021-06-22 stsp }
875 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
876 e6bcace5 2021-06-22 stsp if (parents) {
877 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parents, entry) {
878 e6bcace5 2021-06-22 stsp err = queue_commit_id(&ids, pid->id,
879 e6bcace5 2021-06-22 stsp qcolor, repo);
880 e6bcace5 2021-06-22 stsp if (err) {
881 e6bcace5 2021-06-22 stsp free(id);
882 e6bcace5 2021-06-22 stsp goto done;
883 e6bcace5 2021-06-22 stsp }
884 e6bcace5 2021-06-22 stsp }
885 e6bcace5 2021-06-22 stsp }
886 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
887 e6bcace5 2021-06-22 stsp commit = NULL;
888 e6bcace5 2021-06-22 stsp } else {
889 e6bcace5 2021-06-22 stsp /* should not happen */
890 e6bcace5 2021-06-22 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
891 e6bcace5 2021-06-22 stsp "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
892 e6bcace5 2021-06-22 stsp goto done;
893 e6bcace5 2021-06-22 stsp }
894 e6bcace5 2021-06-22 stsp
895 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
896 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
897 e6bcace5 2021-06-22 stsp }
898 e6bcace5 2021-06-22 stsp
899 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
900 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
901 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
902 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
903 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
904 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
905 e6bcace5 2021-06-22 stsp goto done;
906 e6bcace5 2021-06-22 stsp }
907 e6bcace5 2021-06-22 stsp arg.idx = 0;
908 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
909 e6bcace5 2021-06-22 stsp if (err) {
910 e6bcace5 2021-06-22 stsp free(arg.array);
911 e6bcace5 2021-06-22 stsp goto done;
912 e6bcace5 2021-06-22 stsp }
913 e6bcace5 2021-06-22 stsp *res = arg.array;
914 e6bcace5 2021-06-22 stsp *nres = nkeep;
915 e6bcace5 2021-06-22 stsp }
916 e6bcace5 2021-06-22 stsp done:
917 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
918 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
919 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
920 e6bcace5 2021-06-22 stsp return err;
921 e6bcace5 2021-06-22 stsp }
922 e6bcace5 2021-06-22 stsp
923 e6bcace5 2021-06-22 stsp static const struct got_error *
924 e6bcace5 2021-06-22 stsp read_meta(struct got_pack_meta ***meta, int *nmeta,
925 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
926 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
927 05118f5a 2021-06-22 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
928 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
929 e6bcace5 2021-06-22 stsp {
930 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
931 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
932 e6bcace5 2021-06-22 stsp struct got_object_idset *idset;
933 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
934 e6bcace5 2021-06-22 stsp struct got_pack_metavec v;
935 e6bcace5 2021-06-22 stsp
936 e6bcace5 2021-06-22 stsp *meta = NULL;
937 e6bcace5 2021-06-22 stsp *nmeta = 0;
938 e6bcace5 2021-06-22 stsp
939 e6bcace5 2021-06-22 stsp idset = got_object_idset_alloc();
940 e6bcace5 2021-06-22 stsp if (idset == NULL)
941 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
942 e6bcace5 2021-06-22 stsp
943 e6bcace5 2021-06-22 stsp v.nmeta = 0;
944 e6bcace5 2021-06-22 stsp v.metasz = 64;
945 e6bcace5 2021-06-22 stsp v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
946 e6bcace5 2021-06-22 stsp if (v.meta == NULL) {
947 b7e0a384 2021-10-15 thomas err = got_error_from_errno("calloc");
948 e6bcace5 2021-06-22 stsp goto done;
949 e6bcace5 2021-06-22 stsp }
950 e6bcace5 2021-06-22 stsp
951 e6bcace5 2021-06-22 stsp err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
952 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
953 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
954 e6bcace5 2021-06-22 stsp goto done;
955 e6bcace5 2021-06-22 stsp
956 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
957 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
958 05118f5a 2021-06-22 stsp if (id == NULL)
959 05118f5a 2021-06-22 stsp continue;
960 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
961 05118f5a 2021-06-22 stsp if (err)
962 05118f5a 2021-06-22 stsp return err;
963 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
964 05118f5a 2021-06-22 stsp continue;
965 05118f5a 2021-06-22 stsp err = load_commit(NULL, idset, id, repo,
966 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
967 05118f5a 2021-06-22 stsp if (err)
968 05118f5a 2021-06-22 stsp goto done;
969 05118f5a 2021-06-22 stsp if (progress_cb) {
970 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
971 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
972 05118f5a 2021-06-22 stsp if (err)
973 05118f5a 2021-06-22 stsp goto done;
974 05118f5a 2021-06-22 stsp }
975 05118f5a 2021-06-22 stsp }
976 05118f5a 2021-06-22 stsp
977 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
978 f4a2ff2d 2021-07-01 stsp struct got_object_id *id = theirs[i];
979 07165b17 2021-07-01 stsp int *cached_type;
980 05118f5a 2021-06-22 stsp if (id == NULL)
981 05118f5a 2021-06-22 stsp continue;
982 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
983 07165b17 2021-07-01 stsp if (cached_type == NULL) {
984 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
985 07165b17 2021-07-01 stsp if (err)
986 07165b17 2021-07-01 stsp goto done;
987 07165b17 2021-07-01 stsp } else
988 07165b17 2021-07-01 stsp obj_type = *cached_type;
989 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
990 05118f5a 2021-06-22 stsp continue;
991 05118f5a 2021-06-22 stsp err = load_tag(NULL, idset, id, repo,
992 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
993 05118f5a 2021-06-22 stsp if (err)
994 05118f5a 2021-06-22 stsp goto done;
995 05118f5a 2021-06-22 stsp if (progress_cb) {
996 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
997 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
998 05118f5a 2021-06-22 stsp if (err)
999 05118f5a 2021-06-22 stsp goto done;
1000 05118f5a 2021-06-22 stsp }
1001 05118f5a 2021-06-22 stsp }
1002 05118f5a 2021-06-22 stsp
1003 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
1004 eca70f98 2021-09-03 stsp err = load_commit(&v, idset, ids[i], repo,
1005 eca70f98 2021-09-03 stsp loose_obj_only, cancel_cb, cancel_arg);
1006 eca70f98 2021-09-03 stsp if (err)
1007 eca70f98 2021-09-03 stsp goto done;
1008 eca70f98 2021-09-03 stsp if (progress_cb) {
1009 eca70f98 2021-09-03 stsp err = progress_cb(progress_arg, 0L, nours,
1010 eca70f98 2021-09-03 stsp v.nmeta, 0, 0);
1011 eca70f98 2021-09-03 stsp if (err)
1012 eca70f98 2021-09-03 stsp goto done;
1013 eca70f98 2021-09-03 stsp }
1014 eca70f98 2021-09-03 stsp }
1015 eca70f98 2021-09-03 stsp
1016 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1017 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1018 07165b17 2021-07-01 stsp int *cached_type;
1019 05118f5a 2021-06-22 stsp if (id == NULL)
1020 05118f5a 2021-06-22 stsp continue;
1021 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
1022 07165b17 2021-07-01 stsp if (cached_type == NULL) {
1023 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1024 07165b17 2021-07-01 stsp if (err)
1025 07165b17 2021-07-01 stsp goto done;
1026 07165b17 2021-07-01 stsp } else
1027 07165b17 2021-07-01 stsp obj_type = *cached_type;
1028 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1029 05118f5a 2021-06-22 stsp continue;
1030 05118f5a 2021-06-22 stsp err = load_tag(&v, idset, id, repo,
1031 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
1032 05118f5a 2021-06-22 stsp if (err)
1033 e6bcace5 2021-06-22 stsp goto done;
1034 05118f5a 2021-06-22 stsp if (progress_cb) {
1035 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
1036 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
1037 05118f5a 2021-06-22 stsp if (err)
1038 05118f5a 2021-06-22 stsp goto done;
1039 05118f5a 2021-06-22 stsp }
1040 e6bcace5 2021-06-22 stsp }
1041 05118f5a 2021-06-22 stsp
1042 e6bcace5 2021-06-22 stsp done:
1043 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
1044 e6bcace5 2021-06-22 stsp free(ids[i]);
1045 e6bcace5 2021-06-22 stsp }
1046 e6bcace5 2021-06-22 stsp free(ids);
1047 e6bcace5 2021-06-22 stsp got_object_idset_free(idset);
1048 e6bcace5 2021-06-22 stsp if (err == NULL) {
1049 e6bcace5 2021-06-22 stsp *meta = v.meta;
1050 e6bcace5 2021-06-22 stsp *nmeta = v.nmeta;
1051 e6bcace5 2021-06-22 stsp } else
1052 e6bcace5 2021-06-22 stsp free(v.meta);
1053 e6bcace5 2021-06-22 stsp
1054 e6bcace5 2021-06-22 stsp return err;
1055 e6bcace5 2021-06-22 stsp }
1056 e6bcace5 2021-06-22 stsp
1057 e6bcace5 2021-06-22 stsp const struct got_error *
1058 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1059 e6bcace5 2021-06-22 stsp {
1060 e6bcace5 2021-06-22 stsp size_t n;
1061 e6bcace5 2021-06-22 stsp
1062 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1063 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
1064 e6bcace5 2021-06-22 stsp if (n != len)
1065 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
1066 e6bcace5 2021-06-22 stsp return NULL;
1067 e6bcace5 2021-06-22 stsp }
1068 e6bcace5 2021-06-22 stsp
1069 e6bcace5 2021-06-22 stsp static void
1070 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1071 e6bcace5 2021-06-22 stsp {
1072 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1073 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1074 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1075 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1076 e6bcace5 2021-06-22 stsp }
1077 e6bcace5 2021-06-22 stsp
1078 e6bcace5 2021-06-22 stsp static int
1079 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1080 e6bcace5 2021-06-22 stsp {
1081 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1082 e6bcace5 2021-06-22 stsp
1083 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1084 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1085 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1086 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1087 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
1088 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
1089 e6bcace5 2021-06-22 stsp if (ahd != bhd)
1090 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
1091 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1092 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1093 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
1094 e6bcace5 2021-06-22 stsp }
1095 e6bcace5 2021-06-22 stsp
1096 e6bcace5 2021-06-22 stsp static const struct got_error *
1097 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1098 e6bcace5 2021-06-22 stsp {
1099 e6bcace5 2021-06-22 stsp size_t i;
1100 e6bcace5 2021-06-22 stsp
1101 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1102 e6bcace5 2021-06-22 stsp
1103 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1104 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1105 e6bcace5 2021-06-22 stsp len >>= 4;
1106 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1107 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1108 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1109 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1110 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1111 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1112 e6bcace5 2021-06-22 stsp }
1113 e6bcace5 2021-06-22 stsp
1114 e6bcace5 2021-06-22 stsp *hdrlen = i;
1115 e6bcace5 2021-06-22 stsp return NULL;
1116 e6bcace5 2021-06-22 stsp }
1117 e6bcace5 2021-06-22 stsp
1118 e6bcace5 2021-06-22 stsp static int
1119 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1120 e6bcace5 2021-06-22 stsp {
1121 e6bcace5 2021-06-22 stsp int i, j;
1122 e6bcace5 2021-06-22 stsp char rbuf[8];
1123 e6bcace5 2021-06-22 stsp
1124 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1125 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1126 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1127 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1128 e6bcace5 2021-06-22 stsp }
1129 e6bcace5 2021-06-22 stsp
1130 e6bcace5 2021-06-22 stsp j = 0;
1131 e6bcace5 2021-06-22 stsp while (i > 0)
1132 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1133 e6bcace5 2021-06-22 stsp return j;
1134 e6bcace5 2021-06-22 stsp }
1135 e6bcace5 2021-06-22 stsp
1136 e6bcace5 2021-06-22 stsp static const struct got_error *
1137 b79280dd 2021-10-15 thomas genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1138 05118f5a 2021-06-22 stsp struct got_pack_meta **meta, int nmeta, int nours,
1139 05118f5a 2021-06-22 stsp int use_offset_deltas, struct got_repository *repo,
1140 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1141 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1142 e6bcace5 2021-06-22 stsp {
1143 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1144 e4d8ab47 2021-10-15 thomas int i, nh;
1145 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1146 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1147 ab6186ae 2021-10-15 thomas struct got_raw_object *raw = NULL;
1148 e4d8ab47 2021-10-15 thomas FILE *delta_file = NULL;
1149 e4d8ab47 2021-10-15 thomas char buf[32];
1150 e6bcace5 2021-06-22 stsp size_t outlen, n;
1151 e6bcace5 2021-06-22 stsp struct got_deflate_checksum csum;
1152 05118f5a 2021-06-22 stsp off_t packfile_size = 0;
1153 ecf9545f 2021-10-15 thomas int outfd = -1;
1154 e6bcace5 2021-06-22 stsp
1155 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1156 e6bcace5 2021-06-22 stsp csum.output_sha1 = &ctx;
1157 e6bcace5 2021-06-22 stsp csum.output_crc = NULL;
1158 e6bcace5 2021-06-22 stsp
1159 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1160 e6bcace5 2021-06-22 stsp if (err)
1161 e6bcace5 2021-06-22 stsp return err;
1162 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1163 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1164 e6bcace5 2021-06-22 stsp if (err)
1165 e6bcace5 2021-06-22 stsp goto done;
1166 e6bcace5 2021-06-22 stsp putbe32(buf, nmeta);
1167 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1168 e6bcace5 2021-06-22 stsp if (err)
1169 e6bcace5 2021-06-22 stsp goto done;
1170 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1171 05118f5a 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
1172 05118f5a 2021-06-22 stsp if (progress_cb) {
1173 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1174 05118f5a 2021-06-22 stsp nmeta, nmeta, i);
1175 05118f5a 2021-06-22 stsp if (err)
1176 05118f5a 2021-06-22 stsp goto done;
1177 05118f5a 2021-06-22 stsp }
1178 e6bcace5 2021-06-22 stsp m = meta[i];
1179 e6bcace5 2021-06-22 stsp m->off = ftello(packfile);
1180 c565dfd3 2021-10-16 thomas err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
1181 e6bcace5 2021-06-22 stsp if (err)
1182 e6bcace5 2021-06-22 stsp goto done;
1183 c565dfd3 2021-10-16 thomas if (m->deltas == NULL) {
1184 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1185 e6bcace5 2021-06-22 stsp m->obj_type, raw->size);
1186 e6bcace5 2021-06-22 stsp if (err)
1187 e6bcace5 2021-06-22 stsp goto done;
1188 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1189 e6bcace5 2021-06-22 stsp if (err)
1190 e6bcace5 2021-06-22 stsp goto done;
1191 05118f5a 2021-06-22 stsp packfile_size += nh;
1192 e6bcace5 2021-06-22 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1193 e6bcace5 2021-06-22 stsp err = got_error_from_errno("fseeko");
1194 e6bcace5 2021-06-22 stsp goto done;
1195 e6bcace5 2021-06-22 stsp }
1196 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, raw->f, packfile,
1197 e6bcace5 2021-06-22 stsp &csum);
1198 e6bcace5 2021-06-22 stsp if (err)
1199 e6bcace5 2021-06-22 stsp goto done;
1200 05118f5a 2021-06-22 stsp packfile_size += outlen;
1201 e6bcace5 2021-06-22 stsp } else {
1202 b79280dd 2021-10-15 thomas off_t remain;
1203 e4d8ab47 2021-10-15 thomas if (delta_file == NULL) {
1204 e4d8ab47 2021-10-15 thomas delta_file = got_opentemp();
1205 e4d8ab47 2021-10-15 thomas if (delta_file == NULL) {
1206 e4d8ab47 2021-10-15 thomas err = got_error_from_errno(
1207 e4d8ab47 2021-10-15 thomas "got_opentemp");
1208 e4d8ab47 2021-10-15 thomas goto done;
1209 e4d8ab47 2021-10-15 thomas }
1210 e4d8ab47 2021-10-15 thomas }
1211 e4d8ab47 2021-10-15 thomas if (ftruncate(fileno(delta_file), 0L) == -1) {
1212 e4d8ab47 2021-10-15 thomas err = got_error_from_errno("ftruncate");
1213 e4d8ab47 2021-10-15 thomas goto done;
1214 e4d8ab47 2021-10-15 thomas }
1215 e4d8ab47 2021-10-15 thomas if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1216 e4d8ab47 2021-10-15 thomas err = got_error_from_errno("fseeko");
1217 e4d8ab47 2021-10-15 thomas goto done;
1218 e4d8ab47 2021-10-15 thomas }
1219 b79280dd 2021-10-15 thomas if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1220 b79280dd 2021-10-15 thomas == -1) {
1221 e4d8ab47 2021-10-15 thomas err = got_error_from_errno("fseeko");
1222 e4d8ab47 2021-10-15 thomas goto done;
1223 e4d8ab47 2021-10-15 thomas }
1224 b79280dd 2021-10-15 thomas remain = m->delta_len;
1225 b79280dd 2021-10-15 thomas while (remain > 0) {
1226 b79280dd 2021-10-15 thomas char delta_buf[8192];
1227 b79280dd 2021-10-15 thomas size_t r, w, n;
1228 b79280dd 2021-10-15 thomas n = MIN(remain, sizeof(delta_buf));
1229 b79280dd 2021-10-15 thomas r = fread(delta_buf, 1, n, delta_cache);
1230 b79280dd 2021-10-15 thomas if (r != n) {
1231 b79280dd 2021-10-15 thomas err = got_ferror(delta_cache,
1232 b79280dd 2021-10-15 thomas GOT_ERR_IO);
1233 b79280dd 2021-10-15 thomas goto done;
1234 b79280dd 2021-10-15 thomas }
1235 b79280dd 2021-10-15 thomas w = fwrite(delta_buf, 1, n, delta_file);
1236 b79280dd 2021-10-15 thomas if (w != n) {
1237 b79280dd 2021-10-15 thomas err = got_ferror(delta_file,
1238 b79280dd 2021-10-15 thomas GOT_ERR_IO);
1239 b79280dd 2021-10-15 thomas goto done;
1240 b79280dd 2021-10-15 thomas }
1241 b79280dd 2021-10-15 thomas remain -= n;
1242 b79280dd 2021-10-15 thomas }
1243 e6bcace5 2021-06-22 stsp if (use_offset_deltas && m->prev->off != 0) {
1244 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1245 b79280dd 2021-10-15 thomas GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1246 e6bcace5 2021-06-22 stsp if (err)
1247 e6bcace5 2021-06-22 stsp goto done;
1248 e6bcace5 2021-06-22 stsp nh += packoff(buf + nh,
1249 e6bcace5 2021-06-22 stsp m->off - m->prev->off);
1250 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1251 e6bcace5 2021-06-22 stsp if (err)
1252 e6bcace5 2021-06-22 stsp goto done;
1253 05118f5a 2021-06-22 stsp packfile_size += nh;
1254 e6bcace5 2021-06-22 stsp } else {
1255 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1256 b79280dd 2021-10-15 thomas GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1257 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1258 e6bcace5 2021-06-22 stsp if (err)
1259 e6bcace5 2021-06-22 stsp goto done;
1260 05118f5a 2021-06-22 stsp packfile_size += nh;
1261 e6bcace5 2021-06-22 stsp err = hwrite(packfile, m->prev->id.sha1,
1262 e6bcace5 2021-06-22 stsp sizeof(m->prev->id.sha1), &ctx);
1263 05118f5a 2021-06-22 stsp packfile_size += sizeof(m->prev->id.sha1);
1264 e6bcace5 2021-06-22 stsp if (err)
1265 e6bcace5 2021-06-22 stsp goto done;
1266 e6bcace5 2021-06-22 stsp }
1267 b79280dd 2021-10-15 thomas if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1268 b79280dd 2021-10-15 thomas err = got_error_from_errno("fseeko");
1269 b79280dd 2021-10-15 thomas goto done;
1270 b79280dd 2021-10-15 thomas }
1271 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, delta_file,
1272 e6bcace5 2021-06-22 stsp packfile, &csum);
1273 e6bcace5 2021-06-22 stsp if (err)
1274 e6bcace5 2021-06-22 stsp goto done;
1275 05118f5a 2021-06-22 stsp packfile_size += outlen;
1276 e6bcace5 2021-06-22 stsp }
1277 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
1278 e6bcace5 2021-06-22 stsp raw = NULL;
1279 e6bcace5 2021-06-22 stsp }
1280 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1281 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1282 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1283 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1284 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1285 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1286 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1287 05118f5a 2021-06-22 stsp nmeta, nmeta, nmeta);
1288 05118f5a 2021-06-22 stsp if (err)
1289 05118f5a 2021-06-22 stsp goto done;
1290 e6bcace5 2021-06-22 stsp done:
1291 e4d8ab47 2021-10-15 thomas if (delta_file && fclose(delta_file) == EOF && err == NULL)
1292 e4d8ab47 2021-10-15 thomas err = got_error_from_errno("fclose");
1293 e4d8ab47 2021-10-15 thomas if (raw)
1294 e4d8ab47 2021-10-15 thomas got_object_raw_close(raw);
1295 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
1296 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
1297 e6bcace5 2021-06-22 stsp return err;
1298 e6bcace5 2021-06-22 stsp }
1299 e6bcace5 2021-06-22 stsp
1300 e6bcace5 2021-06-22 stsp const struct got_error *
1301 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1302 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1303 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1304 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1305 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1306 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1307 e6bcace5 2021-06-22 stsp {
1308 e6bcace5 2021-06-22 stsp const struct got_error *err;
1309 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
1310 e6bcace5 2021-06-22 stsp int nmeta;
1311 b79280dd 2021-10-15 thomas FILE *delta_cache = NULL;
1312 e6bcace5 2021-06-22 stsp
1313 e6bcace5 2021-06-22 stsp err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1314 05118f5a 2021-06-22 stsp loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1315 e6bcace5 2021-06-22 stsp if (err)
1316 e6bcace5 2021-06-22 stsp return err;
1317 e6bcace5 2021-06-22 stsp
1318 f8a36e22 2021-08-26 stsp if (nmeta == 0 && !allow_empty) {
1319 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1320 05118f5a 2021-06-22 stsp goto done;
1321 05118f5a 2021-06-22 stsp }
1322 b79280dd 2021-10-15 thomas
1323 b79280dd 2021-10-15 thomas delta_cache = got_opentemp();
1324 b79280dd 2021-10-15 thomas if (delta_cache == NULL) {
1325 b79280dd 2021-10-15 thomas err = got_error_from_errno("got_opentemp");
1326 b79280dd 2021-10-15 thomas goto done;
1327 b79280dd 2021-10-15 thomas }
1328 b79280dd 2021-10-15 thomas
1329 f8a36e22 2021-08-26 stsp if (nmeta > 0) {
1330 b79280dd 2021-10-15 thomas err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1331 f8a36e22 2021-08-26 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1332 f8a36e22 2021-08-26 stsp if (err)
1333 f8a36e22 2021-08-26 stsp goto done;
1334 b79280dd 2021-10-15 thomas if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1335 b79280dd 2021-10-15 thomas err = got_error_from_errno("fseeko");
1336 b79280dd 2021-10-15 thomas goto done;
1337 b79280dd 2021-10-15 thomas }
1338 f8a36e22 2021-08-26 stsp }
1339 e6bcace5 2021-06-22 stsp
1340 b79280dd 2021-10-15 thomas err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1341 b79280dd 2021-10-15 thomas repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1342 e6bcace5 2021-06-22 stsp if (err)
1343 e6bcace5 2021-06-22 stsp goto done;
1344 e6bcace5 2021-06-22 stsp done:
1345 e6bcace5 2021-06-22 stsp free_nmeta(meta, nmeta);
1346 b79280dd 2021-10-15 thomas if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1347 b79280dd 2021-10-15 thomas err = got_error_from_errno("fclose");
1348 e6bcace5 2021-06-22 stsp return err;
1349 e6bcace5 2021-06-22 stsp }