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 5060d5a1 2022-01-10 stsp unsigned char *delta_buf; /* if not encoded in delta cache file */
73 74881701 2021-10-15 stsp off_t delta_offset; /* offset in delta cache file */
74 5060d5a1 2022-01-10 stsp 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 1d19226a 2021-10-13 stsp 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 5060d5a1 2022-01-10 stsp free(meta->delta_buf);
126 5060d5a1 2022-01-10 stsp 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 5060d5a1 2022-01-10 stsp 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 5060d5a1 2022-01-10 stsp int i;
162 5060d5a1 2022-01-10 stsp 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 5060d5a1 2022-01-10 stsp }
171 5060d5a1 2022-01-10 stsp
172 5060d5a1 2022-01-10 stsp static const struct got_error *
173 5060d5a1 2022-01-10 stsp append(unsigned char **p, size_t *len, off_t *sz, void *seg, int nseg)
174 5060d5a1 2022-01-10 stsp {
175 5060d5a1 2022-01-10 stsp char *n;
176 5060d5a1 2022-01-10 stsp
177 5060d5a1 2022-01-10 stsp if (*len + nseg >= *sz) {
178 5060d5a1 2022-01-10 stsp while (*len + nseg >= *sz)
179 5060d5a1 2022-01-10 stsp *sz += *sz / 2;
180 5060d5a1 2022-01-10 stsp n = realloc(*p, *sz);
181 5060d5a1 2022-01-10 stsp if (n == NULL)
182 5060d5a1 2022-01-10 stsp return got_error_from_errno("realloc");
183 5060d5a1 2022-01-10 stsp *p = n;
184 5060d5a1 2022-01-10 stsp }
185 5060d5a1 2022-01-10 stsp memcpy(*p + *len, seg, nseg);
186 5060d5a1 2022-01-10 stsp *len += nseg;
187 5060d5a1 2022-01-10 stsp return NULL;
188 5060d5a1 2022-01-10 stsp }
189 5060d5a1 2022-01-10 stsp
190 5060d5a1 2022-01-10 stsp static const struct got_error *
191 5060d5a1 2022-01-10 stsp encode_delta_in_mem(struct got_pack_meta *m, struct got_raw_object *o,
192 5060d5a1 2022-01-10 stsp struct got_delta_instruction *deltas, int ndeltas,
193 5060d5a1 2022-01-10 stsp off_t delta_size, off_t base_size)
194 5060d5a1 2022-01-10 stsp {
195 5060d5a1 2022-01-10 stsp const struct got_error *err;
196 5060d5a1 2022-01-10 stsp unsigned char buf[16], *bp;
197 5060d5a1 2022-01-10 stsp int i, j;
198 5060d5a1 2022-01-10 stsp size_t len = 0;
199 5060d5a1 2022-01-10 stsp off_t n;
200 5060d5a1 2022-01-10 stsp struct got_delta_instruction *d;
201 5060d5a1 2022-01-10 stsp
202 5060d5a1 2022-01-10 stsp m->delta_buf = malloc(delta_size);
203 5060d5a1 2022-01-10 stsp if (m->delta_buf == NULL)
204 5060d5a1 2022-01-10 stsp return got_error_from_errno("calloc");
205 5060d5a1 2022-01-10 stsp
206 5060d5a1 2022-01-10 stsp /* base object size */
207 5060d5a1 2022-01-10 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
208 5060d5a1 2022-01-10 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
209 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
210 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
211 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
212 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
213 5060d5a1 2022-01-10 stsp }
214 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size, buf, i);
215 5060d5a1 2022-01-10 stsp if (err)
216 5060d5a1 2022-01-10 stsp return err;
217 5060d5a1 2022-01-10 stsp
218 5060d5a1 2022-01-10 stsp /* target object size */
219 5060d5a1 2022-01-10 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
220 5060d5a1 2022-01-10 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
221 5060d5a1 2022-01-10 stsp for (i = 1; n > 0; i++) {
222 5060d5a1 2022-01-10 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
223 5060d5a1 2022-01-10 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
224 5060d5a1 2022-01-10 stsp n >>= GOT_DELTA_SIZE_SHIFT;
225 5060d5a1 2022-01-10 stsp }
226 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size, buf, i);
227 5060d5a1 2022-01-10 stsp if (err)
228 5060d5a1 2022-01-10 stsp return err;
229 5060d5a1 2022-01-10 stsp
230 5060d5a1 2022-01-10 stsp for (j = 0; j < ndeltas; j++) {
231 5060d5a1 2022-01-10 stsp d = &deltas[j];
232 5060d5a1 2022-01-10 stsp if (d->copy) {
233 5060d5a1 2022-01-10 stsp n = d->offset;
234 5060d5a1 2022-01-10 stsp bp = &buf[1];
235 5060d5a1 2022-01-10 stsp buf[0] = GOT_DELTA_BASE_COPY;
236 5060d5a1 2022-01-10 stsp for (i = 0; i < 4; i++) {
237 5060d5a1 2022-01-10 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
238 5060d5a1 2022-01-10 stsp buf[0] |= 1 << i;
239 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
240 5060d5a1 2022-01-10 stsp n >>= 8;
241 5060d5a1 2022-01-10 stsp if (n == 0)
242 5060d5a1 2022-01-10 stsp break;
243 5060d5a1 2022-01-10 stsp }
244 5060d5a1 2022-01-10 stsp
245 5060d5a1 2022-01-10 stsp n = d->len;
246 5060d5a1 2022-01-10 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
247 5060d5a1 2022-01-10 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
248 5060d5a1 2022-01-10 stsp for (i = 0; i < 3 && n > 0; i++) {
249 5060d5a1 2022-01-10 stsp buf[0] |= 1 << (i + 4);
250 5060d5a1 2022-01-10 stsp *bp++ = n & 0xff;
251 5060d5a1 2022-01-10 stsp n >>= 8;
252 5060d5a1 2022-01-10 stsp }
253 5060d5a1 2022-01-10 stsp }
254 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
255 5060d5a1 2022-01-10 stsp buf, bp - buf);
256 5060d5a1 2022-01-10 stsp if (err)
257 5060d5a1 2022-01-10 stsp return err;
258 5060d5a1 2022-01-10 stsp } else if (o->f == NULL) {
259 5060d5a1 2022-01-10 stsp n = 0;
260 5060d5a1 2022-01-10 stsp while (n != d->len) {
261 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
262 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
263 5060d5a1 2022-01-10 stsp buf, 1);
264 5060d5a1 2022-01-10 stsp if (err)
265 5060d5a1 2022-01-10 stsp return err;
266 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
267 5060d5a1 2022-01-10 stsp o->data + o->hdrlen + d->offset + n,
268 5060d5a1 2022-01-10 stsp buf[0]);
269 5060d5a1 2022-01-10 stsp if (err)
270 5060d5a1 2022-01-10 stsp return err;
271 5060d5a1 2022-01-10 stsp n += buf[0];
272 5060d5a1 2022-01-10 stsp }
273 5060d5a1 2022-01-10 stsp } else {
274 5060d5a1 2022-01-10 stsp char content[128];
275 5060d5a1 2022-01-10 stsp size_t r;
276 5060d5a1 2022-01-10 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
277 5060d5a1 2022-01-10 stsp return got_error_from_errno("fseeko");
278 5060d5a1 2022-01-10 stsp n = 0;
279 5060d5a1 2022-01-10 stsp while (n != d->len) {
280 5060d5a1 2022-01-10 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
281 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
282 5060d5a1 2022-01-10 stsp buf, 1);
283 5060d5a1 2022-01-10 stsp if (err)
284 5060d5a1 2022-01-10 stsp return err;
285 5060d5a1 2022-01-10 stsp r = fread(content, 1, buf[0], o->f);
286 5060d5a1 2022-01-10 stsp if (r != buf[0])
287 5060d5a1 2022-01-10 stsp return got_ferror(o->f, GOT_ERR_IO);
288 5060d5a1 2022-01-10 stsp err = append(&m->delta_buf, &len, &delta_size,
289 5060d5a1 2022-01-10 stsp content, buf[0]);
290 5060d5a1 2022-01-10 stsp if (err)
291 5060d5a1 2022-01-10 stsp return err;
292 5060d5a1 2022-01-10 stsp n += buf[0];
293 5060d5a1 2022-01-10 stsp }
294 5060d5a1 2022-01-10 stsp }
295 5060d5a1 2022-01-10 stsp }
296 5060d5a1 2022-01-10 stsp
297 5060d5a1 2022-01-10 stsp m->delta_len = len;
298 5060d5a1 2022-01-10 stsp return NULL;
299 e6bcace5 2021-06-22 stsp }
300 e6bcace5 2021-06-22 stsp
301 74881701 2021-10-15 stsp static const struct got_error *
302 74881701 2021-10-15 stsp encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
303 74881701 2021-10-15 stsp struct got_delta_instruction *deltas, int ndeltas,
304 a319ca8c 2021-10-15 stsp off_t base_size, FILE *f)
305 a319ca8c 2021-10-15 stsp {
306 a319ca8c 2021-10-15 stsp unsigned char buf[16], *bp;
307 a319ca8c 2021-10-15 stsp int i, j;
308 a319ca8c 2021-10-15 stsp off_t n;
309 a319ca8c 2021-10-15 stsp size_t w;
310 a319ca8c 2021-10-15 stsp struct got_delta_instruction *d;
311 a319ca8c 2021-10-15 stsp
312 a319ca8c 2021-10-15 stsp /* base object size */
313 a319ca8c 2021-10-15 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
314 a319ca8c 2021-10-15 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
315 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
316 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
317 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
318 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
319 a319ca8c 2021-10-15 stsp }
320 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, i, f);
321 a319ca8c 2021-10-15 stsp if (w != i)
322 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
323 a319ca8c 2021-10-15 stsp
324 a319ca8c 2021-10-15 stsp /* target object size */
325 a319ca8c 2021-10-15 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
326 a319ca8c 2021-10-15 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
327 a319ca8c 2021-10-15 stsp for (i = 1; n > 0; i++) {
328 a319ca8c 2021-10-15 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
329 a319ca8c 2021-10-15 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
330 a319ca8c 2021-10-15 stsp n >>= GOT_DELTA_SIZE_SHIFT;
331 a319ca8c 2021-10-15 stsp }
332 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, i, f);
333 a319ca8c 2021-10-15 stsp if (w != i)
334 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
335 a319ca8c 2021-10-15 stsp
336 a319ca8c 2021-10-15 stsp for (j = 0; j < ndeltas; j++) {
337 a319ca8c 2021-10-15 stsp d = &deltas[j];
338 a319ca8c 2021-10-15 stsp if (d->copy) {
339 a319ca8c 2021-10-15 stsp n = d->offset;
340 a319ca8c 2021-10-15 stsp bp = &buf[1];
341 a319ca8c 2021-10-15 stsp buf[0] = GOT_DELTA_BASE_COPY;
342 a319ca8c 2021-10-15 stsp for (i = 0; i < 4; i++) {
343 a319ca8c 2021-10-15 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
344 a319ca8c 2021-10-15 stsp buf[0] |= 1 << i;
345 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
346 a319ca8c 2021-10-15 stsp n >>= 8;
347 a319ca8c 2021-10-15 stsp if (n == 0)
348 a319ca8c 2021-10-15 stsp break;
349 a319ca8c 2021-10-15 stsp }
350 a319ca8c 2021-10-15 stsp
351 a319ca8c 2021-10-15 stsp n = d->len;
352 a319ca8c 2021-10-15 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
353 a319ca8c 2021-10-15 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
354 a319ca8c 2021-10-15 stsp for (i = 0; i < 3 && n > 0; i++) {
355 a319ca8c 2021-10-15 stsp buf[0] |= 1 << (i + 4);
356 a319ca8c 2021-10-15 stsp *bp++ = n & 0xff;
357 a319ca8c 2021-10-15 stsp n >>= 8;
358 a319ca8c 2021-10-15 stsp }
359 a319ca8c 2021-10-15 stsp }
360 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, bp - buf, f);
361 a319ca8c 2021-10-15 stsp if (w != bp - buf)
362 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
363 64a8571e 2022-01-07 stsp } else if (o->f == NULL) {
364 64a8571e 2022-01-07 stsp n = 0;
365 64a8571e 2022-01-07 stsp while (n != d->len) {
366 64a8571e 2022-01-07 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
367 64a8571e 2022-01-07 stsp w = fwrite(buf, 1, 1, f);
368 64a8571e 2022-01-07 stsp if (w != 1)
369 64a8571e 2022-01-07 stsp return got_ferror(f, GOT_ERR_IO);
370 64a8571e 2022-01-07 stsp w = fwrite(o->data + o->hdrlen + d->offset + n,
371 64a8571e 2022-01-07 stsp 1, buf[0], f);
372 64a8571e 2022-01-07 stsp if (w != buf[0])
373 64a8571e 2022-01-07 stsp return got_ferror(f, GOT_ERR_IO);
374 64a8571e 2022-01-07 stsp n += buf[0];
375 64a8571e 2022-01-07 stsp }
376 a319ca8c 2021-10-15 stsp } else {
377 a319ca8c 2021-10-15 stsp char content[128];
378 a319ca8c 2021-10-15 stsp size_t r;
379 a319ca8c 2021-10-15 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
380 a319ca8c 2021-10-15 stsp return got_error_from_errno("fseeko");
381 a319ca8c 2021-10-15 stsp n = 0;
382 a319ca8c 2021-10-15 stsp while (n != d->len) {
383 a319ca8c 2021-10-15 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
384 a319ca8c 2021-10-15 stsp w = fwrite(buf, 1, 1, f);
385 a319ca8c 2021-10-15 stsp if (w != 1)
386 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
387 a319ca8c 2021-10-15 stsp r = fread(content, 1, buf[0], o->f);
388 a319ca8c 2021-10-15 stsp if (r != buf[0])
389 a319ca8c 2021-10-15 stsp return got_ferror(o->f, GOT_ERR_IO);
390 a319ca8c 2021-10-15 stsp w = fwrite(content, 1, buf[0], f);
391 a319ca8c 2021-10-15 stsp if (w != buf[0])
392 a319ca8c 2021-10-15 stsp return got_ferror(f, GOT_ERR_IO);
393 a319ca8c 2021-10-15 stsp n += buf[0];
394 a319ca8c 2021-10-15 stsp }
395 a319ca8c 2021-10-15 stsp }
396 a319ca8c 2021-10-15 stsp }
397 e6bcace5 2021-06-22 stsp
398 5060d5a1 2022-01-10 stsp m->delta_len = ftello(f) - m->delta_offset;
399 a319ca8c 2021-10-15 stsp return NULL;
400 a319ca8c 2021-10-15 stsp }
401 211cfef0 2022-01-05 stsp
402 211cfef0 2022-01-05 stsp static const struct got_error *
403 211cfef0 2022-01-05 stsp report_progress(got_pack_progress_cb progress_cb, void *progress_arg,
404 211cfef0 2022-01-05 stsp struct got_ratelimit *rl, off_t packfile_size, int ncommits,
405 211cfef0 2022-01-05 stsp int nobj_total, int obj_deltify, int nobj_written)
406 211cfef0 2022-01-05 stsp {
407 211cfef0 2022-01-05 stsp const struct got_error *err;
408 211cfef0 2022-01-05 stsp int elapsed;
409 211cfef0 2022-01-05 stsp
410 211cfef0 2022-01-05 stsp if (progress_cb == NULL)
411 211cfef0 2022-01-05 stsp return NULL;
412 211cfef0 2022-01-05 stsp
413 211cfef0 2022-01-05 stsp err = got_ratelimit_check(&elapsed, rl);
414 211cfef0 2022-01-05 stsp if (err || !elapsed)
415 211cfef0 2022-01-05 stsp return err;
416 a319ca8c 2021-10-15 stsp
417 211cfef0 2022-01-05 stsp return progress_cb(progress_arg, packfile_size, ncommits,
418 211cfef0 2022-01-05 stsp nobj_total, obj_deltify, nobj_written);
419 211cfef0 2022-01-05 stsp }
420 a319ca8c 2021-10-15 stsp
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 74881701 2021-10-15 stsp FILE *delta_cache, struct got_repository *repo,
424 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
425 211cfef0 2022-01-05 stsp 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 74881701 2021-10-15 stsp struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
431 5060d5a1 2022-01-10 stsp int i, j, ndeltas, best_ndeltas;
432 5060d5a1 2022-01-10 stsp off_t size, best_size;
433 4f4d853e 2021-10-24 stsp const int max_base_candidates = 3;
434 402a5ec1 2022-01-10 stsp size_t delta_memsize = 0;
435 402a5ec1 2022-01-10 stsp const size_t max_delta_memsize = 25 * GOT_DELTA_RESULT_SIZE_CACHED_MAX;
436 cc7a354a 2021-10-15 stsp 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 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
446 211cfef0 2022-01-05 stsp 0L, nours, nmeta, i, 0);
447 211cfef0 2022-01-05 stsp if (err)
448 211cfef0 2022-01-05 stsp 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 94dac27c 2021-10-15 stsp 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 600b755e 2021-10-14 stsp m->size = raw->size;
459 e6bcace5 2021-06-22 stsp
460 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
461 64a8571e 2022-01-07 stsp err = got_deltify_init_mem(&m->dtab, raw->data,
462 64a8571e 2022-01-07 stsp raw->hdrlen, raw->size + raw->hdrlen);
463 64a8571e 2022-01-07 stsp } else {
464 64a8571e 2022-01-07 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
465 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen);
466 64a8571e 2022-01-07 stsp }
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 74881701 2021-10-15 stsp best_size = raw->size;
478 74881701 2021-10-15 stsp 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 22edbce7 2021-10-24 stsp 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 d3c116bf 2021-10-15 stsp err = got_object_raw_open(&base_raw, &outfd, repo,
492 94dac27c 2021-10-15 stsp &base->id);
493 e6bcace5 2021-06-22 stsp if (err)
494 e6bcace5 2021-06-22 stsp goto done;
495 64a8571e 2022-01-07 stsp if (raw->f == NULL && base_raw->f == NULL) {
496 64a8571e 2022-01-07 stsp err = got_deltify_mem_mem(&deltas, &ndeltas,
497 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
498 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
499 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
500 64a8571e 2022-01-07 stsp base_raw->hdrlen,
501 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
502 64a8571e 2022-01-07 stsp } else if (raw->f == NULL) {
503 64a8571e 2022-01-07 stsp err = got_deltify_mem_file(&deltas, &ndeltas,
504 64a8571e 2022-01-07 stsp raw->data, raw->hdrlen,
505 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
506 64a8571e 2022-01-07 stsp base->dtab, base_raw->f,
507 64a8571e 2022-01-07 stsp base_raw->hdrlen,
508 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
509 64a8571e 2022-01-07 stsp } else if (base_raw->f == NULL) {
510 64a8571e 2022-01-07 stsp err = got_deltify_file_mem(&deltas, &ndeltas,
511 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
512 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
513 64a8571e 2022-01-07 stsp base->dtab, base_raw->data,
514 64a8571e 2022-01-07 stsp base_raw->hdrlen,
515 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
516 64a8571e 2022-01-07 stsp } else {
517 64a8571e 2022-01-07 stsp err = got_deltify(&deltas, &ndeltas,
518 64a8571e 2022-01-07 stsp raw->f, raw->hdrlen,
519 64a8571e 2022-01-07 stsp raw->size + raw->hdrlen,
520 64a8571e 2022-01-07 stsp base->dtab, base_raw->f, base_raw->hdrlen,
521 64a8571e 2022-01-07 stsp base_raw->size + base_raw->hdrlen);
522 64a8571e 2022-01-07 stsp }
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 74881701 2021-10-15 stsp 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 74881701 2021-10-15 stsp best_size = size;
535 74881701 2021-10-15 stsp free(best_deltas);
536 74881701 2021-10-15 stsp best_deltas = deltas;
537 74881701 2021-10-15 stsp best_ndeltas = ndeltas;
538 74881701 2021-10-15 stsp 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 74881701 2021-10-15 stsp if (best_ndeltas > 0) {
552 402a5ec1 2022-01-10 stsp if (best_size <= GOT_DELTA_RESULT_SIZE_CACHED_MAX &&
553 402a5ec1 2022-01-10 stsp delta_memsize + best_size <= max_delta_memsize) {
554 402a5ec1 2022-01-10 stsp delta_memsize += best_size;
555 5060d5a1 2022-01-10 stsp err = encode_delta_in_mem(m, raw, best_deltas,
556 5060d5a1 2022-01-10 stsp best_ndeltas, best_size, m->prev->size);
557 5060d5a1 2022-01-10 stsp } else {
558 5060d5a1 2022-01-10 stsp m->delta_offset = ftello(delta_cache);
559 5060d5a1 2022-01-10 stsp err = encode_delta(m, raw, best_deltas,
560 5060d5a1 2022-01-10 stsp best_ndeltas, m->prev->size, delta_cache);
561 5060d5a1 2022-01-10 stsp }
562 74881701 2021-10-15 stsp free(best_deltas);
563 74881701 2021-10-15 stsp best_deltas = NULL;
564 74881701 2021-10-15 stsp best_ndeltas = 0;
565 74881701 2021-10-15 stsp if (err)
566 74881701 2021-10-15 stsp goto done;
567 74881701 2021-10-15 stsp }
568 74881701 2021-10-15 stsp
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 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
582 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
583 74881701 2021-10-15 stsp free(deltas);
584 74881701 2021-10-15 stsp 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 3af9de88 2021-09-22 stsp 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 3af9de88 2021-09-22 stsp } 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 211cfef0 2022-01-05 stsp 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 1d19226a 2021-10-13 stsp 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 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1173 211cfef0 2022-01-05 stsp 0L, nours, v.nmeta, 0, 0);
1174 211cfef0 2022-01-05 stsp if (err)
1175 211cfef0 2022-01-05 stsp 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 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1197 211cfef0 2022-01-05 stsp 0L, nours, v.nmeta, 0, 0);
1198 211cfef0 2022-01-05 stsp if (err)
1199 211cfef0 2022-01-05 stsp 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 211cfef0 2022-01-05 stsp if (err)
1208 211cfef0 2022-01-05 stsp goto done;
1209 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1210 211cfef0 2022-01-05 stsp 0L, nours, v.nmeta, 0, 0);
1211 211cfef0 2022-01-05 stsp if (err)
1212 211cfef0 2022-01-05 stsp 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 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1234 211cfef0 2022-01-05 stsp 0L, nours, v.nmeta, 0, 0);
1235 211cfef0 2022-01-05 stsp if (err)
1236 211cfef0 2022-01-05 stsp goto done;
1237 e6bcace5 2021-06-22 stsp }
1238 05118f5a 2021-06-22 stsp
1239 211cfef0 2022-01-05 stsp if (progress_cb) {
1240 211cfef0 2022-01-05 stsp err = progress_cb(progress_arg, 0L, nours, v.nmeta, 0, 0);
1241 211cfef0 2022-01-05 stsp if (err)
1242 211cfef0 2022-01-05 stsp goto done;
1243 211cfef0 2022-01-05 stsp }
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 5060d5a1 2022-01-10 stsp deltahdr(off_t *packfile_size, SHA1_CTX *ctx, FILE *packfile,
1340 5060d5a1 2022-01-10 stsp struct got_pack_meta *m, int use_offset_deltas)
1341 5060d5a1 2022-01-10 stsp {
1342 5060d5a1 2022-01-10 stsp const struct got_error *err;
1343 5060d5a1 2022-01-10 stsp char buf[32];
1344 5060d5a1 2022-01-10 stsp int nh;
1345 5060d5a1 2022-01-10 stsp
1346 5060d5a1 2022-01-10 stsp if (use_offset_deltas && m->prev->off != 0) {
1347 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1348 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1349 5060d5a1 2022-01-10 stsp if (err)
1350 5060d5a1 2022-01-10 stsp return err;
1351 5060d5a1 2022-01-10 stsp nh += packoff(buf + nh, m->off - m->prev->off);
1352 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1353 5060d5a1 2022-01-10 stsp if (err)
1354 5060d5a1 2022-01-10 stsp return err;
1355 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1356 5060d5a1 2022-01-10 stsp } else {
1357 5060d5a1 2022-01-10 stsp err = packhdr(&nh, buf, sizeof(buf),
1358 5060d5a1 2022-01-10 stsp GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1359 5060d5a1 2022-01-10 stsp if (err)
1360 5060d5a1 2022-01-10 stsp return err;
1361 5060d5a1 2022-01-10 stsp err = hwrite(packfile, buf, nh, ctx);
1362 5060d5a1 2022-01-10 stsp if (err)
1363 5060d5a1 2022-01-10 stsp return err;
1364 5060d5a1 2022-01-10 stsp *packfile_size += nh;
1365 5060d5a1 2022-01-10 stsp err = hwrite(packfile, m->prev->id.sha1,
1366 5060d5a1 2022-01-10 stsp sizeof(m->prev->id.sha1), ctx);
1367 5060d5a1 2022-01-10 stsp if (err)
1368 5060d5a1 2022-01-10 stsp return err;
1369 5060d5a1 2022-01-10 stsp *packfile_size += sizeof(m->prev->id.sha1);
1370 5060d5a1 2022-01-10 stsp }
1371 5060d5a1 2022-01-10 stsp
1372 5060d5a1 2022-01-10 stsp return NULL;
1373 5060d5a1 2022-01-10 stsp }
1374 5060d5a1 2022-01-10 stsp
1375 5060d5a1 2022-01-10 stsp static const struct got_error *
1376 74881701 2021-10-15 stsp 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 211cfef0 2022-01-05 stsp 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 08347b73 2021-10-14 stsp int i, nh;
1385 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1386 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1387 600b755e 2021-10-14 stsp struct got_raw_object *raw = NULL;
1388 08347b73 2021-10-14 stsp char buf[32];
1389 72840534 2022-01-19 stsp size_t n;
1390 e6bcace5 2021-06-22 stsp struct got_deflate_checksum csum;
1391 72840534 2022-01-19 stsp off_t outlen, packfile_size = 0;
1392 cc7a354a 2021-10-15 stsp int outfd = -1;
1393 e6bcace5 2021-06-22 stsp
1394 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1395 e6bcace5 2021-06-22 stsp csum.output_sha1 = &ctx;
1396 e6bcace5 2021-06-22 stsp csum.output_crc = NULL;
1397 e6bcace5 2021-06-22 stsp
1398 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1399 e6bcace5 2021-06-22 stsp if (err)
1400 e6bcace5 2021-06-22 stsp return err;
1401 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1402 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1403 e6bcace5 2021-06-22 stsp if (err)
1404 e6bcace5 2021-06-22 stsp goto done;
1405 e6bcace5 2021-06-22 stsp putbe32(buf, nmeta);
1406 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1407 e6bcace5 2021-06-22 stsp if (err)
1408 e6bcace5 2021-06-22 stsp goto done;
1409 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1410 05118f5a 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
1411 211cfef0 2022-01-05 stsp err = report_progress(progress_cb, progress_arg, rl,
1412 211cfef0 2022-01-05 stsp packfile_size, nours, nmeta, nmeta, i);
1413 211cfef0 2022-01-05 stsp if (err)
1414 211cfef0 2022-01-05 stsp goto done;
1415 e6bcace5 2021-06-22 stsp m = meta[i];
1416 e6bcace5 2021-06-22 stsp m->off = ftello(packfile);
1417 74881701 2021-10-15 stsp if (m->delta_len == 0) {
1418 59b21794 2022-01-07 stsp err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1419 59b21794 2022-01-07 stsp if (err)
1420 59b21794 2022-01-07 stsp goto done;
1421 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1422 e6bcace5 2021-06-22 stsp m->obj_type, raw->size);
1423 e6bcace5 2021-06-22 stsp if (err)
1424 e6bcace5 2021-06-22 stsp goto done;
1425 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1426 e6bcace5 2021-06-22 stsp if (err)
1427 e6bcace5 2021-06-22 stsp goto done;
1428 05118f5a 2021-06-22 stsp packfile_size += nh;
1429 64a8571e 2022-01-07 stsp if (raw->f == NULL) {
1430 64a8571e 2022-01-07 stsp err = got_deflate_to_file_mmap(&outlen,
1431 64a8571e 2022-01-07 stsp raw->data + raw->hdrlen, 0, raw->size,
1432 64a8571e 2022-01-07 stsp packfile, &csum);
1433 64a8571e 2022-01-07 stsp if (err)
1434 64a8571e 2022-01-07 stsp goto done;
1435 64a8571e 2022-01-07 stsp } else {
1436 64a8571e 2022-01-07 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET)
1437 64a8571e 2022-01-07 stsp == -1) {
1438 64a8571e 2022-01-07 stsp err = got_error_from_errno("fseeko");
1439 64a8571e 2022-01-07 stsp goto done;
1440 64a8571e 2022-01-07 stsp }
1441 64a8571e 2022-01-07 stsp err = got_deflate_to_file(&outlen, raw->f,
1442 72840534 2022-01-19 stsp raw->size, packfile, &csum);
1443 64a8571e 2022-01-07 stsp if (err)
1444 64a8571e 2022-01-07 stsp goto done;
1445 e6bcace5 2021-06-22 stsp }
1446 05118f5a 2021-06-22 stsp packfile_size += outlen;
1447 59b21794 2022-01-07 stsp got_object_raw_close(raw);
1448 59b21794 2022-01-07 stsp raw = NULL;
1449 5060d5a1 2022-01-10 stsp } else if (m->delta_buf) {
1450 5060d5a1 2022-01-10 stsp err = deltahdr(&packfile_size, &ctx, packfile,
1451 5060d5a1 2022-01-10 stsp m, use_offset_deltas);
1452 5060d5a1 2022-01-10 stsp if (err)
1453 5060d5a1 2022-01-10 stsp goto done;
1454 5060d5a1 2022-01-10 stsp err = got_deflate_to_file_mmap(&outlen,
1455 5060d5a1 2022-01-10 stsp m->delta_buf, 0, m->delta_len, packfile, &csum);
1456 5060d5a1 2022-01-10 stsp if (err)
1457 5060d5a1 2022-01-10 stsp goto done;
1458 5060d5a1 2022-01-10 stsp packfile_size += outlen;
1459 5060d5a1 2022-01-10 stsp free(m->delta_buf);
1460 5060d5a1 2022-01-10 stsp m->delta_buf = NULL;
1461 e6bcace5 2021-06-22 stsp } else {
1462 74881701 2021-10-15 stsp if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1463 74881701 2021-10-15 stsp == -1) {
1464 08347b73 2021-10-14 stsp err = got_error_from_errno("fseeko");
1465 08347b73 2021-10-14 stsp goto done;
1466 08347b73 2021-10-14 stsp }
1467 5060d5a1 2022-01-10 stsp err = deltahdr(&packfile_size, &ctx, packfile,
1468 5060d5a1 2022-01-10 stsp m, use_offset_deltas);
1469 5060d5a1 2022-01-10 stsp if (err)
1470 5060d5a1 2022-01-10 stsp goto done;
1471 72840534 2022-01-19 stsp err = got_deflate_to_file(&outlen, delta_cache,
1472 72840534 2022-01-19 stsp m->delta_len, packfile, &csum);
1473 e6bcace5 2021-06-22 stsp if (err)
1474 e6bcace5 2021-06-22 stsp goto done;
1475 05118f5a 2021-06-22 stsp packfile_size += outlen;
1476 e6bcace5 2021-06-22 stsp }
1477 e6bcace5 2021-06-22 stsp }
1478 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1479 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1480 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1481 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1482 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1483 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1484 211cfef0 2022-01-05 stsp if (progress_cb) {
1485 211cfef0 2022-01-05 stsp err = progress_cb(progress_arg, packfile_size, nours,
1486 211cfef0 2022-01-05 stsp nmeta, nmeta, nmeta);
1487 211cfef0 2022-01-05 stsp if (err)
1488 211cfef0 2022-01-05 stsp goto done;
1489 211cfef0 2022-01-05 stsp }
1490 e6bcace5 2021-06-22 stsp done:
1491 08347b73 2021-10-14 stsp if (raw)
1492 08347b73 2021-10-14 stsp got_object_raw_close(raw);
1493 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
1494 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
1495 e6bcace5 2021-06-22 stsp return err;
1496 e6bcace5 2021-06-22 stsp }
1497 e6bcace5 2021-06-22 stsp
1498 e6bcace5 2021-06-22 stsp const struct got_error *
1499 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1500 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1501 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1502 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1503 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1504 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1505 e6bcace5 2021-06-22 stsp {
1506 e6bcace5 2021-06-22 stsp const struct got_error *err;
1507 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
1508 e6bcace5 2021-06-22 stsp int nmeta;
1509 74881701 2021-10-15 stsp FILE *delta_cache = NULL;
1510 211cfef0 2022-01-05 stsp struct got_ratelimit rl;
1511 e6bcace5 2021-06-22 stsp
1512 211cfef0 2022-01-05 stsp got_ratelimit_init(&rl, 0, 500);
1513 211cfef0 2022-01-05 stsp
1514 e6bcace5 2021-06-22 stsp err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1515 211cfef0 2022-01-05 stsp loose_obj_only, progress_cb, progress_arg, &rl,
1516 211cfef0 2022-01-05 stsp cancel_cb, cancel_arg);
1517 e6bcace5 2021-06-22 stsp if (err)
1518 e6bcace5 2021-06-22 stsp return err;
1519 e6bcace5 2021-06-22 stsp
1520 f8a36e22 2021-08-26 stsp if (nmeta == 0 && !allow_empty) {
1521 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1522 05118f5a 2021-06-22 stsp goto done;
1523 05118f5a 2021-06-22 stsp }
1524 74881701 2021-10-15 stsp
1525 74881701 2021-10-15 stsp delta_cache = got_opentemp();
1526 74881701 2021-10-15 stsp if (delta_cache == NULL) {
1527 74881701 2021-10-15 stsp err = got_error_from_errno("got_opentemp");
1528 74881701 2021-10-15 stsp goto done;
1529 74881701 2021-10-15 stsp }
1530 74881701 2021-10-15 stsp
1531 f8a36e22 2021-08-26 stsp if (nmeta > 0) {
1532 74881701 2021-10-15 stsp err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1533 211cfef0 2022-01-05 stsp progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1534 f8a36e22 2021-08-26 stsp if (err)
1535 f8a36e22 2021-08-26 stsp goto done;
1536 74881701 2021-10-15 stsp if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1537 74881701 2021-10-15 stsp err = got_error_from_errno("fseeko");
1538 74881701 2021-10-15 stsp goto done;
1539 74881701 2021-10-15 stsp }
1540 f8a36e22 2021-08-26 stsp }
1541 e6bcace5 2021-06-22 stsp
1542 74881701 2021-10-15 stsp err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1543 211cfef0 2022-01-05 stsp repo, progress_cb, progress_arg, &rl, cancel_cb, cancel_arg);
1544 e6bcace5 2021-06-22 stsp if (err)
1545 e6bcace5 2021-06-22 stsp goto done;
1546 e6bcace5 2021-06-22 stsp done:
1547 e6bcace5 2021-06-22 stsp free_nmeta(meta, nmeta);
1548 74881701 2021-10-15 stsp if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1549 74881701 2021-10-15 stsp err = got_error_from_errno("fclose");
1550 e6bcace5 2021-06-22 stsp return err;
1551 e6bcace5 2021-06-22 stsp }