Blame


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