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>
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.
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.
18 08736cf9 2021-06-23 stsp #include <sys/types.h>
19 cfe41121 2021-10-16 thomas #include <sys/queue.h>
20 cfe41121 2021-10-16 thomas #include <sys/tree.h>
21 08736cf9 2021-06-23 stsp #include <sys/uio.h>
22 e6bcace5 2021-06-22 stsp #include <sys/stat.h>
24 08736cf9 2021-06-23 stsp #include <stdint.h>
25 e6bcace5 2021-06-22 stsp #include <stdio.h>
26 e6bcace5 2021-06-22 stsp #include <stdlib.h>
27 e6bcace5 2021-06-22 stsp #include <string.h>
28 e6bcace5 2021-06-22 stsp #include <limits.h>
29 e6bcace5 2021-06-22 stsp #include <zlib.h>
31 b347007e 2021-10-15 thomas #if defined(__FreeBSD__)
32 b347007e 2021-10-15 thomas #include <unistd.h>
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"
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"
53 b79280dd 2021-10-15 thomas #ifndef MIN
54 b79280dd 2021-10-15 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
58 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
61 e6bcace5 2021-06-22 stsp struct got_pack_meta {
62 e6bcace5 2021-06-22 stsp struct got_object_id id;
64 e6bcace5 2021-06-22 stsp int obj_type;
65 ab6186ae 2021-10-15 thomas off_t size;
66 e6bcace5 2021-06-22 stsp time_t mtime;
68 e6bcace5 2021-06-22 stsp /* The best delta we picked */
69 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
70 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
71 b79280dd 2021-10-15 thomas off_t delta_offset; /* offset in delta cache file */
72 b79280dd 2021-10-15 thomas off_t delta_len; /* length in delta cache file */
75 e6bcace5 2021-06-22 stsp /* Only used for delta window */
76 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
78 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
82 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
83 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
88 e6bcace5 2021-06-22 stsp static const struct got_error *
89 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
90 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
92 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
93 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
95 e6bcace5 2021-06-22 stsp *new = NULL;
97 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
98 e6bcace5 2021-06-22 stsp if (m == NULL)
99 b7e0a384 2021-10-15 thomas return got_error_from_errno("calloc");
101 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
103 e6bcace5 2021-06-22 stsp m->path = strdup(path);
104 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
105 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
107 e6bcace5 2021-06-22 stsp return err;
110 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
111 e6bcace5 2021-06-22 stsp m->mtime = mtime;
113 e6bcace5 2021-06-22 stsp return NULL;
116 e6bcace5 2021-06-22 stsp static void
117 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
119 e6bcace5 2021-06-22 stsp if (meta == NULL)
121 e6bcace5 2021-06-22 stsp free(meta->path);
122 e6bcace5 2021-06-22 stsp meta->path = NULL;
125 e6bcace5 2021-06-22 stsp static void
126 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
130 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
131 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
132 e6bcace5 2021-06-22 stsp free(meta);
136 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
138 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
141 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
142 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
144 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
145 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
146 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
147 e6bcace5 2021-06-22 stsp if (cmp != 0)
148 e6bcace5 2021-06-22 stsp return cmp;
149 e6bcace5 2021-06-22 stsp if (a->mtime != b->mtime)
150 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
151 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
155 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
157 e6bcace5 2021-06-22 stsp int i, size = 32;
158 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
159 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
160 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
162 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
164 e6bcace5 2021-06-22 stsp return size;
167 b79280dd 2021-10-15 thomas static const struct got_error *
168 b79280dd 2021-10-15 thomas encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
169 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas, int ndeltas,
170 d0772de9 2021-10-15 thomas off_t base_size, FILE *f)
172 d0772de9 2021-10-15 thomas unsigned char buf[16], *bp;
173 d0772de9 2021-10-15 thomas int i, j;
175 d0772de9 2021-10-15 thomas size_t w;
176 d0772de9 2021-10-15 thomas struct got_delta_instruction *d;
178 d0772de9 2021-10-15 thomas /* base object size */
179 d0772de9 2021-10-15 thomas buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
180 d0772de9 2021-10-15 thomas n = base_size >> GOT_DELTA_SIZE_SHIFT;
181 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
182 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
183 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
184 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
186 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, i, f);
187 d0772de9 2021-10-15 thomas if (w != i)
188 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
190 d0772de9 2021-10-15 thomas /* target object size */
191 d0772de9 2021-10-15 thomas buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
192 d0772de9 2021-10-15 thomas n = o->size >> GOT_DELTA_SIZE_SHIFT;
193 d0772de9 2021-10-15 thomas for (i = 1; n > 0; i++) {
194 d0772de9 2021-10-15 thomas buf[i - 1] |= GOT_DELTA_SIZE_MORE;
195 d0772de9 2021-10-15 thomas buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
196 d0772de9 2021-10-15 thomas n >>= GOT_DELTA_SIZE_SHIFT;
198 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, i, f);
199 d0772de9 2021-10-15 thomas if (w != i)
200 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
202 d0772de9 2021-10-15 thomas for (j = 0; j < ndeltas; j++) {
203 d0772de9 2021-10-15 thomas d = &deltas[j];
204 d0772de9 2021-10-15 thomas if (d->copy) {
205 d0772de9 2021-10-15 thomas n = d->offset;
206 d0772de9 2021-10-15 thomas bp = &buf[1];
207 d0772de9 2021-10-15 thomas buf[0] = GOT_DELTA_BASE_COPY;
208 d0772de9 2021-10-15 thomas for (i = 0; i < 4; i++) {
209 d0772de9 2021-10-15 thomas /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
210 d0772de9 2021-10-15 thomas buf[0] |= 1 << i;
211 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
213 d0772de9 2021-10-15 thomas if (n == 0)
217 d0772de9 2021-10-15 thomas n = d->len;
218 d0772de9 2021-10-15 thomas if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
219 d0772de9 2021-10-15 thomas /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
220 d0772de9 2021-10-15 thomas for (i = 0; i < 3 && n > 0; i++) {
221 d0772de9 2021-10-15 thomas buf[0] |= 1 << (i + 4);
222 d0772de9 2021-10-15 thomas *bp++ = n & 0xff;
226 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, bp - buf, f);
227 d0772de9 2021-10-15 thomas if (w != bp - buf)
228 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
230 d0772de9 2021-10-15 thomas char content[128];
231 d0772de9 2021-10-15 thomas size_t r;
232 d0772de9 2021-10-15 thomas if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
233 d0772de9 2021-10-15 thomas return got_error_from_errno("fseeko");
235 d0772de9 2021-10-15 thomas while (n != d->len) {
236 d0772de9 2021-10-15 thomas buf[0] = (d->len - n < 127) ? d->len - n : 127;
237 d0772de9 2021-10-15 thomas w = fwrite(buf, 1, 1, f);
238 d0772de9 2021-10-15 thomas if (w != 1)
239 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
240 d0772de9 2021-10-15 thomas r = fread(content, 1, buf[0], o->f);
241 d0772de9 2021-10-15 thomas if (r != buf[0])
242 d0772de9 2021-10-15 thomas return got_ferror(o->f, GOT_ERR_IO);
243 d0772de9 2021-10-15 thomas w = fwrite(content, 1, buf[0], f);
244 d0772de9 2021-10-15 thomas if (w != buf[0])
245 d0772de9 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
246 d0772de9 2021-10-15 thomas n += buf[0];
251 d0772de9 2021-10-15 thomas return NULL;
255 e6bcace5 2021-06-22 stsp static const struct got_error *
256 05118f5a 2021-06-22 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
257 b79280dd 2021-10-15 thomas FILE *delta_cache, struct got_repository *repo,
258 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
259 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
261 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
262 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
263 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
264 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
265 b79280dd 2021-10-15 thomas int i, j, size, best_size, ndeltas, best_ndeltas;
266 e6bcace5 2021-06-22 stsp const int max_base_candidates = 10;
267 ecf9545f 2021-10-15 thomas int outfd = -1;
269 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
270 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
271 e6bcace5 2021-06-22 stsp if (cancel_cb) {
272 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
276 05118f5a 2021-06-22 stsp if (progress_cb) {
277 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
281 e6bcace5 2021-06-22 stsp m = meta[i];
283 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
284 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
287 c565dfd3 2021-10-16 thomas err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
290 ab6186ae 2021-10-15 thomas m->size = raw->size;
292 e6bcace5 2021-06-22 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
293 e6bcace5 2021-06-22 stsp raw->size + raw->hdrlen);
297 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
298 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
299 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
300 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
301 e6bcace5 2021-06-22 stsp n->dtab = NULL;
304 b79280dd 2021-10-15 thomas best_size = raw->size;
305 b79280dd 2021-10-15 thomas best_ndeltas = 0;
306 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
307 e6bcace5 2021-06-22 stsp if (cancel_cb) {
308 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
312 e6bcace5 2021-06-22 stsp base = meta[j];
313 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
314 33acf1a2 2021-10-15 thomas if (base->nchain >= 32 ||
315 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
318 8ab9215c 2021-10-15 thomas err = got_object_raw_open(&base_raw, &outfd, repo,
319 c565dfd3 2021-10-16 thomas &base->id, 8192);
322 e6bcace5 2021-06-22 stsp err = got_deltify(&deltas, &ndeltas,
323 e6bcace5 2021-06-22 stsp raw->f, raw->hdrlen, raw->size + raw->hdrlen,
324 e6bcace5 2021-06-22 stsp base->dtab, base_raw->f, base_raw->hdrlen,
325 e6bcace5 2021-06-22 stsp base_raw->size + base_raw->hdrlen);
326 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
327 e6bcace5 2021-06-22 stsp base_raw = NULL;
331 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
332 b79280dd 2021-10-15 thomas if (size + 32 < best_size){
334 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
335 e6bcace5 2021-06-22 stsp * replace it.
337 b79280dd 2021-10-15 thomas best_size = size;
338 b79280dd 2021-10-15 thomas free(best_deltas);
339 b79280dd 2021-10-15 thomas best_deltas = deltas;
340 b79280dd 2021-10-15 thomas best_ndeltas = ndeltas;
341 b79280dd 2021-10-15 thomas deltas = NULL;
342 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
343 e6bcace5 2021-06-22 stsp m->prev = base;
344 e6bcace5 2021-06-22 stsp m->head = base->head;
345 e6bcace5 2021-06-22 stsp if (m->head == NULL)
346 e6bcace5 2021-06-22 stsp m->head = base;
348 e6bcace5 2021-06-22 stsp free(deltas);
349 e6bcace5 2021-06-22 stsp deltas = NULL;
350 e6bcace5 2021-06-22 stsp ndeltas = 0;
354 b79280dd 2021-10-15 thomas if (best_ndeltas > 0) {
355 b79280dd 2021-10-15 thomas m->delta_offset = ftello(delta_cache);
356 b79280dd 2021-10-15 thomas err = encode_delta(m, raw, best_deltas,
357 b79280dd 2021-10-15 thomas best_ndeltas, m->prev->size, delta_cache);
358 b79280dd 2021-10-15 thomas free(best_deltas);
359 b79280dd 2021-10-15 thomas best_deltas = NULL;
360 b79280dd 2021-10-15 thomas best_ndeltas = 0;
362 b79280dd 2021-10-15 thomas goto done;
363 b79280dd 2021-10-15 thomas m->delta_len = ftello(delta_cache) - m->delta_offset;
366 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
367 e6bcace5 2021-06-22 stsp raw = NULL;
370 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
371 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
372 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
375 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
376 e6bcace5 2021-06-22 stsp if (base_raw)
377 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
378 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
379 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
380 b79280dd 2021-10-15 thomas free(deltas);
381 b79280dd 2021-10-15 thomas free(best_deltas);
382 e6bcace5 2021-06-22 stsp return err;
385 e6bcace5 2021-06-22 stsp static const struct got_error *
386 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
387 05118f5a 2021-06-22 stsp struct got_repository *repo)
389 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
390 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
393 05118f5a 2021-06-22 stsp *found = 0;
395 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
396 05118f5a 2021-06-22 stsp if (err == NULL)
397 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
398 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
399 05118f5a 2021-06-22 stsp err = NULL;
400 05118f5a 2021-06-22 stsp return err;
403 07165b17 2021-07-01 stsp static const int obj_types[] = {
404 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_ANY,
405 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_COMMIT,
406 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TREE,
407 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_BLOB,
408 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TAG,
409 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_OFFSET_DELTA,
410 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_REF_DELTA
413 05118f5a 2021-06-22 stsp static const struct got_error *
414 e6bcace5 2021-06-22 stsp add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
415 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
416 05118f5a 2021-06-22 stsp time_t mtime, int loose_obj_only, struct got_repository *repo)
418 e6bcace5 2021-06-22 stsp const struct got_error *err;
419 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
421 05118f5a 2021-06-22 stsp if (loose_obj_only) {
422 05118f5a 2021-06-22 stsp int is_packed;
423 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
425 05118f5a 2021-06-22 stsp return err;
426 05118f5a 2021-06-22 stsp if (is_packed)
427 05118f5a 2021-06-22 stsp return NULL;
430 07165b17 2021-07-01 stsp err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
432 e6bcace5 2021-06-22 stsp return err;
434 e6bcace5 2021-06-22 stsp if (v == NULL)
435 e6bcace5 2021-06-22 stsp return NULL;
437 e6bcace5 2021-06-22 stsp err = alloc_meta(&m, id, path, obj_type, mtime);
441 e6bcace5 2021-06-22 stsp if (v->nmeta == v->metasz){
442 e6bcace5 2021-06-22 stsp size_t newsize = 2 * v->metasz;
443 e6bcace5 2021-06-22 stsp struct got_pack_meta **new;
444 e6bcace5 2021-06-22 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
445 e6bcace5 2021-06-22 stsp if (new == NULL) {
446 e6bcace5 2021-06-22 stsp err = got_error_from_errno("reallocarray");
449 e6bcace5 2021-06-22 stsp v->meta = new;
450 e6bcace5 2021-06-22 stsp v->metasz = newsize;
454 e6bcace5 2021-06-22 stsp clear_meta(m);
457 e6bcace5 2021-06-22 stsp v->meta[v->nmeta++] = m;
459 e6bcace5 2021-06-22 stsp return err;
462 e6bcace5 2021-06-22 stsp static const struct got_error *
463 e6bcace5 2021-06-22 stsp load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
464 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
465 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
466 05118f5a 2021-06-22 stsp int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
468 e6bcace5 2021-06-22 stsp const struct got_error *err;
469 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
470 e6bcace5 2021-06-22 stsp char *p = NULL;
473 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
475 e6bcace5 2021-06-22 stsp return err;
477 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
478 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
479 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
480 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
482 e6bcace5 2021-06-22 stsp if (cancel_cb) {
483 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
488 08511b5e 2021-09-24 thomas if (got_object_tree_entry_is_submodule(e) ||
489 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
492 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
493 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
494 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
498 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
499 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
500 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
503 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
504 08511b5e 2021-09-24 thomas } else if (S_ISREG(mode) || S_ISLNK(mode)) {
505 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
506 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
514 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
516 e6bcace5 2021-06-22 stsp return err;
519 e6bcace5 2021-06-22 stsp static const struct got_error *
520 e6bcace5 2021-06-22 stsp load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
521 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
522 05118f5a 2021-06-22 stsp int loose_obj_only, struct got_repository *repo,
523 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
525 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
526 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
527 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
529 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
530 e6bcace5 2021-06-22 stsp return NULL;
532 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
534 e6bcace5 2021-06-22 stsp return err;
536 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
537 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
539 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
540 e6bcace5 2021-06-22 stsp if (cancel_cb) {
541 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
546 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
547 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
549 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
550 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
554 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
555 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
557 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
561 e6bcace5 2021-06-22 stsp err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
562 05118f5a 2021-06-22 stsp mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
563 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
568 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
569 e6bcace5 2021-06-22 stsp return err;
572 e6bcace5 2021-06-22 stsp static const struct got_error *
573 e6bcace5 2021-06-22 stsp load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
574 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
575 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
577 e6bcace5 2021-06-22 stsp const struct got_error *err;
578 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
580 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
581 e6bcace5 2021-06-22 stsp return NULL;
583 05118f5a 2021-06-22 stsp if (loose_obj_only) {
584 05118f5a 2021-06-22 stsp int is_packed;
585 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
587 05118f5a 2021-06-22 stsp return err;
588 05118f5a 2021-06-22 stsp if (is_packed)
589 05118f5a 2021-06-22 stsp return NULL;
592 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
594 e6bcace5 2021-06-22 stsp return err;
596 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
597 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
598 05118f5a 2021-06-22 stsp loose_obj_only, repo);
602 e6bcace5 2021-06-22 stsp err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
603 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
604 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
606 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
607 e6bcace5 2021-06-22 stsp return err;
610 05118f5a 2021-06-22 stsp static const struct got_error *
611 05118f5a 2021-06-22 stsp load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
612 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
613 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
615 05118f5a 2021-06-22 stsp const struct got_error *err;
616 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
618 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
619 05118f5a 2021-06-22 stsp return NULL;
621 05118f5a 2021-06-22 stsp if (loose_obj_only) {
622 05118f5a 2021-06-22 stsp int is_packed;
623 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
625 05118f5a 2021-06-22 stsp return err;
626 05118f5a 2021-06-22 stsp if (is_packed)
627 05118f5a 2021-06-22 stsp return NULL;
630 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
632 05118f5a 2021-06-22 stsp return err;
634 05118f5a 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
635 05118f5a 2021-06-22 stsp got_object_tag_get_tagger_time(tag),
636 05118f5a 2021-06-22 stsp loose_obj_only, repo);
640 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
641 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
642 26960ff7 2021-09-14 stsp err = load_commit(v, idset,
643 05118f5a 2021-06-22 stsp got_object_tag_get_object_id(tag), repo,
644 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
646 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
647 05118f5a 2021-06-22 stsp err = load_tree(v, idset, got_object_tag_get_object_id(tag),
648 05118f5a 2021-06-22 stsp "", got_object_tag_get_tagger_time(tag),
649 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
656 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
657 05118f5a 2021-06-22 stsp return err;
660 e6bcace5 2021-06-22 stsp enum findtwixt_color {
661 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
662 e6bcace5 2021-06-22 stsp COLOR_DROP,
663 e6bcace5 2021-06-22 stsp COLOR_BLANK,
665 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
666 e6bcace5 2021-06-22 stsp COLOR_KEEP,
667 e6bcace5 2021-06-22 stsp COLOR_DROP,
668 e6bcace5 2021-06-22 stsp COLOR_BLANK
671 e6bcace5 2021-06-22 stsp static const struct got_error *
672 e6bcace5 2021-06-22 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
673 e6bcace5 2021-06-22 stsp int color, struct got_repository *repo)
675 e6bcace5 2021-06-22 stsp const struct got_error *err;
676 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
678 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
680 e6bcace5 2021-06-22 stsp return err;
682 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
683 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
684 e6bcace5 2021-06-22 stsp return NULL;
687 e6bcace5 2021-06-22 stsp static const struct got_error *
688 e6bcace5 2021-06-22 stsp drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
689 e6bcace5 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo,
690 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
692 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
693 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
694 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
695 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
696 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
698 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
700 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
702 e6bcace5 2021-06-22 stsp return err;
703 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&ids, qid, entry);
705 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
706 e6bcace5 2021-06-22 stsp if (cancel_cb) {
707 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
712 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
713 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
715 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id)) {
716 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
720 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, qid->id,
721 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
723 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
727 e6bcace5 2021-06-22 stsp if (!got_object_idset_contains(keep, qid->id)) {
728 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
732 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
733 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
737 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
738 e6bcace5 2021-06-22 stsp if (parents) {
739 e6bcace5 2021-06-22 stsp err = got_object_id_queue_copy(parents, &ids);
741 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
745 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
748 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
749 e6bcace5 2021-06-22 stsp return err;
752 e6bcace5 2021-06-22 stsp struct append_id_arg {
753 e6bcace5 2021-06-22 stsp struct got_object_id **array;
757 e6bcace5 2021-06-22 stsp static const struct got_error *
758 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
760 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
762 e6bcace5 2021-06-22 stsp a->array[a->idx] = got_object_id_dup(id);
763 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
764 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
767 e6bcace5 2021-06-22 stsp return NULL;
770 e6bcace5 2021-06-22 stsp static const struct got_error *
771 e6bcace5 2021-06-22 stsp findtwixt(struct got_object_id ***res, int *nres,
772 e6bcace5 2021-06-22 stsp struct got_object_id **head, int nhead,
773 e6bcace5 2021-06-22 stsp struct got_object_id **tail, int ntail,
774 e6bcace5 2021-06-22 stsp struct got_repository *repo,
775 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
777 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
778 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
779 e6bcace5 2021-06-22 stsp struct got_object_idset *keep, *drop;
780 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
781 05118f5a 2021-06-22 stsp int i, ncolor, nkeep, obj_type;
783 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
784 e6bcace5 2021-06-22 stsp *res = NULL;
787 e6bcace5 2021-06-22 stsp keep = got_object_idset_alloc();
788 e6bcace5 2021-06-22 stsp if (keep == NULL)
789 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
791 e6bcace5 2021-06-22 stsp drop = got_object_idset_alloc();
792 e6bcace5 2021-06-22 stsp if (drop == NULL) {
793 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_idset_alloc");
797 05118f5a 2021-06-22 stsp for (i = 0; i < nhead; i++) {
798 05118f5a 2021-06-22 stsp struct got_object_id *id = head[i];
799 05118f5a 2021-06-22 stsp if (id == NULL)
801 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
803 05118f5a 2021-06-22 stsp return err;
804 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
806 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
810 05118f5a 2021-06-22 stsp for (i = 0; i < ntail; i++) {
811 05118f5a 2021-06-22 stsp struct got_object_id *id = tail[i];
812 05118f5a 2021-06-22 stsp if (id == NULL)
814 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
816 05118f5a 2021-06-22 stsp return err;
817 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
819 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_DROP, repo);
824 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
825 e6bcace5 2021-06-22 stsp int qcolor;
826 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
827 e6bcace5 2021-06-22 stsp qcolor = *((int *)qid->data);
829 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id))
830 e6bcace5 2021-06-22 stsp ncolor = COLOR_DROP;
831 e6bcace5 2021-06-22 stsp else if (got_object_idset_contains(keep, qid->id))
832 e6bcace5 2021-06-22 stsp ncolor = COLOR_KEEP;
834 e6bcace5 2021-06-22 stsp ncolor = COLOR_BLANK;
836 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
837 e6bcace5 2021-06-22 stsp qcolor == COLOR_KEEP)) {
838 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
839 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
843 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
844 e6bcace5 2021-06-22 stsp err = drop_commit(keep, drop, qid->id, repo,
845 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
848 e6bcace5 2021-06-22 stsp } else if (ncolor == COLOR_BLANK) {
849 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
850 e6bcace5 2021-06-22 stsp struct got_object_id *id;
851 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
852 e6bcace5 2021-06-22 stsp struct got_object_qid *pid;
854 e6bcace5 2021-06-22 stsp id = got_object_id_dup(qid->id);
855 e6bcace5 2021-06-22 stsp if (id == NULL) {
856 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
859 e6bcace5 2021-06-22 stsp if (qcolor == COLOR_KEEP)
860 07165b17 2021-07-01 stsp err = got_object_idset_add(keep, id,
861 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
863 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, id,
864 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
870 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
875 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
876 e6bcace5 2021-06-22 stsp if (parents) {
877 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parents, entry) {
878 e6bcace5 2021-06-22 stsp err = queue_commit_id(&ids, pid->id,
879 e6bcace5 2021-06-22 stsp qcolor, repo);
886 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
887 e6bcace5 2021-06-22 stsp commit = NULL;
889 e6bcace5 2021-06-22 stsp /* should not happen */
890 e6bcace5 2021-06-22 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
891 e6bcace5 2021-06-22 stsp "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
895 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
896 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
899 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
900 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
901 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
902 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
903 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
904 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
907 e6bcace5 2021-06-22 stsp arg.idx = 0;
908 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
910 e6bcace5 2021-06-22 stsp free(arg.array);
913 e6bcace5 2021-06-22 stsp *res = arg.array;
914 e6bcace5 2021-06-22 stsp *nres = nkeep;
917 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
918 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
919 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
920 e6bcace5 2021-06-22 stsp return err;
923 e6bcace5 2021-06-22 stsp static const struct got_error *
924 e6bcace5 2021-06-22 stsp read_meta(struct got_pack_meta ***meta, int *nmeta,
925 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
926 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
927 05118f5a 2021-06-22 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
928 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
930 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
931 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
932 e6bcace5 2021-06-22 stsp struct got_object_idset *idset;
933 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
934 e6bcace5 2021-06-22 stsp struct got_pack_metavec v;
936 e6bcace5 2021-06-22 stsp *meta = NULL;
937 e6bcace5 2021-06-22 stsp *nmeta = 0;
939 e6bcace5 2021-06-22 stsp idset = got_object_idset_alloc();
940 e6bcace5 2021-06-22 stsp if (idset == NULL)
941 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
943 e6bcace5 2021-06-22 stsp v.nmeta = 0;
944 e6bcace5 2021-06-22 stsp v.metasz = 64;
945 e6bcace5 2021-06-22 stsp v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
946 e6bcace5 2021-06-22 stsp if (v.meta == NULL) {
947 b7e0a384 2021-10-15 thomas err = got_error_from_errno("calloc");
951 e6bcace5 2021-06-22 stsp err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
952 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
953 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
956 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
957 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
958 05118f5a 2021-06-22 stsp if (id == NULL)
960 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
962 05118f5a 2021-06-22 stsp return err;
963 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
965 05118f5a 2021-06-22 stsp err = load_commit(NULL, idset, id, repo,
966 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
969 05118f5a 2021-06-22 stsp if (progress_cb) {
970 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
971 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
977 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
978 f4a2ff2d 2021-07-01 stsp struct got_object_id *id = theirs[i];
979 07165b17 2021-07-01 stsp int *cached_type;
980 05118f5a 2021-06-22 stsp if (id == NULL)
982 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
983 07165b17 2021-07-01 stsp if (cached_type == NULL) {
984 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
988 07165b17 2021-07-01 stsp obj_type = *cached_type;
989 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
991 05118f5a 2021-06-22 stsp err = load_tag(NULL, idset, id, repo,
992 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
995 05118f5a 2021-06-22 stsp if (progress_cb) {
996 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
997 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
1003 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
1004 eca70f98 2021-09-03 stsp err = load_commit(&v, idset, ids[i], repo,
1005 eca70f98 2021-09-03 stsp loose_obj_only, cancel_cb, cancel_arg);
1007 eca70f98 2021-09-03 stsp goto done;
1008 eca70f98 2021-09-03 stsp if (progress_cb) {
1009 eca70f98 2021-09-03 stsp err = progress_cb(progress_arg, 0L, nours,
1010 eca70f98 2021-09-03 stsp v.nmeta, 0, 0);
1012 eca70f98 2021-09-03 stsp goto done;
1016 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
1017 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
1018 07165b17 2021-07-01 stsp int *cached_type;
1019 05118f5a 2021-06-22 stsp if (id == NULL)
1021 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
1022 07165b17 2021-07-01 stsp if (cached_type == NULL) {
1023 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
1025 07165b17 2021-07-01 stsp goto done;
1027 07165b17 2021-07-01 stsp obj_type = *cached_type;
1028 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
1030 05118f5a 2021-06-22 stsp err = load_tag(&v, idset, id, repo,
1031 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
1033 e6bcace5 2021-06-22 stsp goto done;
1034 05118f5a 2021-06-22 stsp if (progress_cb) {
1035 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
1036 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
1038 05118f5a 2021-06-22 stsp goto done;
1043 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
1044 e6bcace5 2021-06-22 stsp free(ids[i]);
1046 e6bcace5 2021-06-22 stsp free(ids);
1047 e6bcace5 2021-06-22 stsp got_object_idset_free(idset);
1048 e6bcace5 2021-06-22 stsp if (err == NULL) {
1049 e6bcace5 2021-06-22 stsp *meta = v.meta;
1050 e6bcace5 2021-06-22 stsp *nmeta = v.nmeta;
1052 e6bcace5 2021-06-22 stsp free(v.meta);
1054 e6bcace5 2021-06-22 stsp return err;
1057 e6bcace5 2021-06-22 stsp const struct got_error *
1058 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
1062 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
1063 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
1064 e6bcace5 2021-06-22 stsp if (n != len)
1065 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
1066 e6bcace5 2021-06-22 stsp return NULL;
1069 e6bcace5 2021-06-22 stsp static void
1070 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
1072 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
1073 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
1074 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
1075 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
1078 e6bcace5 2021-06-22 stsp static int
1079 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
1081 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
1083 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
1084 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
1085 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1086 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1087 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
1088 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
1089 e6bcace5 2021-06-22 stsp if (ahd != bhd)
1090 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
1091 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1092 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1093 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
1096 e6bcace5 2021-06-22 stsp static const struct got_error *
1097 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1101 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1103 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1104 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1105 e6bcace5 2021-06-22 stsp len >>= 4;
1106 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1107 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1108 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1109 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1110 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1111 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1114 e6bcace5 2021-06-22 stsp *hdrlen = i;
1115 e6bcace5 2021-06-22 stsp return NULL;
1118 e6bcace5 2021-06-22 stsp static int
1119 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1122 e6bcace5 2021-06-22 stsp char rbuf[8];
1124 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1125 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1126 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1127 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1131 e6bcace5 2021-06-22 stsp while (i > 0)
1132 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1136 e6bcace5 2021-06-22 stsp static const struct got_error *
1137 b79280dd 2021-10-15 thomas genpack(uint8_t *pack_sha1, FILE *packfile, FILE *delta_cache,
1138 05118f5a 2021-06-22 stsp struct got_pack_meta **meta, int nmeta, int nours,
1139 05118f5a 2021-06-22 stsp int use_offset_deltas, struct got_repository *repo,
1140 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1141 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1143 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1144 e4d8ab47 2021-10-15 thomas int i, nh;
1145 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1146 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1147 ab6186ae 2021-10-15 thomas struct got_raw_object *raw = NULL;
1148 e4d8ab47 2021-10-15 thomas FILE *delta_file = NULL;
1149 e4d8ab47 2021-10-15 thomas char buf[32];
1150 e6bcace5 2021-06-22 stsp size_t outlen, n;
1151 e6bcace5 2021-06-22 stsp struct got_deflate_checksum csum;
1152 05118f5a 2021-06-22 stsp off_t packfile_size = 0;
1153 ecf9545f 2021-10-15 thomas int outfd = -1;
1155 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1156 e6bcace5 2021-06-22 stsp csum.output_sha1 = &ctx;
1157 e6bcace5 2021-06-22 stsp csum.output_crc = NULL;
1159 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1161 e6bcace5 2021-06-22 stsp return err;
1162 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1163 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1165 e6bcace5 2021-06-22 stsp goto done;
1166 e6bcace5 2021-06-22 stsp putbe32(buf, nmeta);
1167 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1169 e6bcace5 2021-06-22 stsp goto done;
1170 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1171 05118f5a 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
1172 05118f5a 2021-06-22 stsp if (progress_cb) {
1173 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1174 05118f5a 2021-06-22 stsp nmeta, nmeta, i);
1176 05118f5a 2021-06-22 stsp goto done;
1178 e6bcace5 2021-06-22 stsp m = meta[i];
1179 e6bcace5 2021-06-22 stsp m->off = ftello(packfile);
1180 c565dfd3 2021-10-16 thomas err = got_object_raw_open(&raw, &outfd, repo, &m->id, 8192);
1182 e6bcace5 2021-06-22 stsp goto done;
1183 c565dfd3 2021-10-16 thomas if (m->deltas == NULL) {
1184 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1185 e6bcace5 2021-06-22 stsp m->obj_type, raw->size);
1187 e6bcace5 2021-06-22 stsp goto done;
1188 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1190 e6bcace5 2021-06-22 stsp goto done;
1191 05118f5a 2021-06-22 stsp packfile_size += nh;
1192 e6bcace5 2021-06-22 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1193 e6bcace5 2021-06-22 stsp err = got_error_from_errno("fseeko");
1194 e6bcace5 2021-06-22 stsp goto done;
1196 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, raw->f, packfile,
1199 e6bcace5 2021-06-22 stsp goto done;
1200 05118f5a 2021-06-22 stsp packfile_size += outlen;
1202 b79280dd 2021-10-15 thomas off_t remain;
1203 e4d8ab47 2021-10-15 thomas if (delta_file == NULL) {
1204 e4d8ab47 2021-10-15 thomas delta_file = got_opentemp();
1205 e4d8ab47 2021-10-15 thomas if (delta_file == NULL) {
1206 e4d8ab47 2021-10-15 thomas err = got_error_from_errno(
1207 e4d8ab47 2021-10-15 thomas "got_opentemp");
1208 e4d8ab47 2021-10-15 thomas goto done;
1211 e4d8ab47 2021-10-15 thomas if (ftruncate(fileno(delta_file), 0L) == -1) {
1212 e4d8ab47 2021-10-15 thomas err = got_error_from_errno("ftruncate");
1213 e4d8ab47 2021-10-15 thomas goto done;
1215 e4d8ab47 2021-10-15 thomas if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1216 e4d8ab47 2021-10-15 thomas err = got_error_from_errno("fseeko");
1217 e4d8ab47 2021-10-15 thomas goto done;
1219 b79280dd 2021-10-15 thomas if (fseeko(delta_cache, m->delta_offset, SEEK_SET)
1220 b79280dd 2021-10-15 thomas == -1) {
1221 e4d8ab47 2021-10-15 thomas err = got_error_from_errno("fseeko");
1222 e4d8ab47 2021-10-15 thomas goto done;
1224 b79280dd 2021-10-15 thomas remain = m->delta_len;
1225 b79280dd 2021-10-15 thomas while (remain > 0) {
1226 b79280dd 2021-10-15 thomas char delta_buf[8192];
1227 b79280dd 2021-10-15 thomas size_t r, w, n;
1228 b79280dd 2021-10-15 thomas n = MIN(remain, sizeof(delta_buf));
1229 b79280dd 2021-10-15 thomas r = fread(delta_buf, 1, n, delta_cache);
1230 b79280dd 2021-10-15 thomas if (r != n) {
1231 b79280dd 2021-10-15 thomas err = got_ferror(delta_cache,
1232 b79280dd 2021-10-15 thomas GOT_ERR_IO);
1233 b79280dd 2021-10-15 thomas goto done;
1235 b79280dd 2021-10-15 thomas w = fwrite(delta_buf, 1, n, delta_file);
1236 b79280dd 2021-10-15 thomas if (w != n) {
1237 b79280dd 2021-10-15 thomas err = got_ferror(delta_file,
1238 b79280dd 2021-10-15 thomas GOT_ERR_IO);
1239 b79280dd 2021-10-15 thomas goto done;
1241 b79280dd 2021-10-15 thomas remain -= n;
1243 e6bcace5 2021-06-22 stsp if (use_offset_deltas && m->prev->off != 0) {
1244 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1245 b79280dd 2021-10-15 thomas GOT_OBJ_TYPE_OFFSET_DELTA, m->delta_len);
1247 e6bcace5 2021-06-22 stsp goto done;
1248 e6bcace5 2021-06-22 stsp nh += packoff(buf + nh,
1249 e6bcace5 2021-06-22 stsp m->off - m->prev->off);
1250 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1252 e6bcace5 2021-06-22 stsp goto done;
1253 05118f5a 2021-06-22 stsp packfile_size += nh;
1255 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1256 b79280dd 2021-10-15 thomas GOT_OBJ_TYPE_REF_DELTA, m->delta_len);
1257 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1259 e6bcace5 2021-06-22 stsp goto done;
1260 05118f5a 2021-06-22 stsp packfile_size += nh;
1261 e6bcace5 2021-06-22 stsp err = hwrite(packfile, m->prev->id.sha1,
1262 e6bcace5 2021-06-22 stsp sizeof(m->prev->id.sha1), &ctx);
1263 05118f5a 2021-06-22 stsp packfile_size += sizeof(m->prev->id.sha1);
1265 e6bcace5 2021-06-22 stsp goto done;
1267 b79280dd 2021-10-15 thomas if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1268 b79280dd 2021-10-15 thomas err = got_error_from_errno("fseeko");
1269 b79280dd 2021-10-15 thomas goto done;
1271 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, delta_file,
1272 e6bcace5 2021-06-22 stsp packfile, &csum);
1274 e6bcace5 2021-06-22 stsp goto done;
1275 05118f5a 2021-06-22 stsp packfile_size += outlen;
1277 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
1278 e6bcace5 2021-06-22 stsp raw = NULL;
1280 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1281 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1282 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1283 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1284 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1285 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1286 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1287 05118f5a 2021-06-22 stsp nmeta, nmeta, nmeta);
1289 05118f5a 2021-06-22 stsp goto done;
1291 e4d8ab47 2021-10-15 thomas if (delta_file && fclose(delta_file) == EOF && err == NULL)
1292 e4d8ab47 2021-10-15 thomas err = got_error_from_errno("fclose");
1293 e4d8ab47 2021-10-15 thomas if (raw)
1294 e4d8ab47 2021-10-15 thomas got_object_raw_close(raw);
1295 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
1296 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
1297 e6bcace5 2021-06-22 stsp return err;
1300 e6bcace5 2021-06-22 stsp const struct got_error *
1301 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1302 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1303 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1304 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1305 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1306 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1308 e6bcace5 2021-06-22 stsp const struct got_error *err;
1309 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
1310 e6bcace5 2021-06-22 stsp int nmeta;
1311 b79280dd 2021-10-15 thomas FILE *delta_cache = NULL;
1313 e6bcace5 2021-06-22 stsp err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1314 05118f5a 2021-06-22 stsp loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1316 e6bcace5 2021-06-22 stsp return err;
1318 f8a36e22 2021-08-26 stsp if (nmeta == 0 && !allow_empty) {
1319 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1320 05118f5a 2021-06-22 stsp goto done;
1323 b79280dd 2021-10-15 thomas delta_cache = got_opentemp();
1324 b79280dd 2021-10-15 thomas if (delta_cache == NULL) {
1325 b79280dd 2021-10-15 thomas err = got_error_from_errno("got_opentemp");
1326 b79280dd 2021-10-15 thomas goto done;
1329 f8a36e22 2021-08-26 stsp if (nmeta > 0) {
1330 b79280dd 2021-10-15 thomas err = pick_deltas(meta, nmeta, nours, delta_cache, repo,
1331 f8a36e22 2021-08-26 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1333 f8a36e22 2021-08-26 stsp goto done;
1334 b79280dd 2021-10-15 thomas if (fseeko(delta_cache, 0L, SEEK_SET) == -1) {
1335 b79280dd 2021-10-15 thomas err = got_error_from_errno("fseeko");
1336 b79280dd 2021-10-15 thomas goto done;
1340 b79280dd 2021-10-15 thomas err = genpack(packsha1, packfile, delta_cache, meta, nmeta, nours, 1,
1341 b79280dd 2021-10-15 thomas repo, progress_cb, progress_arg, cancel_cb, cancel_arg);
1343 e6bcace5 2021-06-22 stsp goto done;
1345 e6bcace5 2021-06-22 stsp free_nmeta(meta, nmeta);
1346 b79280dd 2021-10-15 thomas if (delta_cache && fclose(delta_cache) == EOF && err == NULL)
1347 b79280dd 2021-10-15 thomas err = got_error_from_errno("fclose");
1348 e6bcace5 2021-06-22 stsp return err;