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