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