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