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