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 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>
24 08736cf9 2021-06-23 stsp #include <stdint.h>
25 05118f5a 2021-06-22 stsp #include <imsg.h>
26 e6bcace5 2021-06-22 stsp #include <stdio.h>
27 e6bcace5 2021-06-22 stsp #include <stdlib.h>
28 e6bcace5 2021-06-22 stsp #include <string.h>
29 e6bcace5 2021-06-22 stsp #include <sha1.h>
30 e6bcace5 2021-06-22 stsp #include <limits.h>
31 e6bcace5 2021-06-22 stsp #include <zlib.h>
33 e6bcace5 2021-06-22 stsp #include "got_error.h"
34 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
35 e6bcace5 2021-06-22 stsp #include "got_object.h"
36 f8b19efd 2021-10-13 stsp #include "got_path.h"
37 05118f5a 2021-06-22 stsp #include "got_reference.h"
38 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
39 08347b73 2021-10-14 stsp #include "got_opentemp.h"
41 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
42 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
43 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
44 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
45 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
46 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
47 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
48 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
49 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
52 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
55 e6bcace5 2021-06-22 stsp struct got_pack_meta {
56 e6bcace5 2021-06-22 stsp struct got_object_id id;
58 e6bcace5 2021-06-22 stsp int obj_type;
60 e6bcace5 2021-06-22 stsp time_t mtime;
62 e6bcace5 2021-06-22 stsp /* The best delta we picked */
63 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
64 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
65 e6bcace5 2021-06-22 stsp struct got_delta_instruction *deltas;
66 e6bcace5 2021-06-22 stsp int ndeltas;
69 e6bcace5 2021-06-22 stsp /* Only used for delta window */
70 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
72 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
76 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
77 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
82 e6bcace5 2021-06-22 stsp static const struct got_error *
83 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
84 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
86 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
87 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
89 e6bcace5 2021-06-22 stsp *new = NULL;
91 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
92 e6bcace5 2021-06-22 stsp if (m == NULL)
93 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
95 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
97 e6bcace5 2021-06-22 stsp m->path = strdup(path);
98 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
99 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
101 e6bcace5 2021-06-22 stsp return err;
104 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
105 e6bcace5 2021-06-22 stsp m->mtime = mtime;
107 e6bcace5 2021-06-22 stsp return NULL;
110 e6bcace5 2021-06-22 stsp static void
111 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
113 e6bcace5 2021-06-22 stsp if (meta == NULL)
115 e6bcace5 2021-06-22 stsp free(meta->deltas);
116 e6bcace5 2021-06-22 stsp meta->deltas = NULL;
117 e6bcace5 2021-06-22 stsp free(meta->path);
118 e6bcace5 2021-06-22 stsp meta->path = NULL;
121 e6bcace5 2021-06-22 stsp static void
122 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
126 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
127 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
128 e6bcace5 2021-06-22 stsp free(meta);
132 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
134 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
137 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
138 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
140 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
141 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
142 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
143 e6bcace5 2021-06-22 stsp if (cmp != 0)
144 e6bcace5 2021-06-22 stsp return cmp;
145 e6bcace5 2021-06-22 stsp if (a->mtime != b->mtime)
146 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
147 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
151 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
153 e6bcace5 2021-06-22 stsp int i, size = 32;
154 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
155 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
156 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
158 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
160 e6bcace5 2021-06-22 stsp return size;
164 e6bcace5 2021-06-22 stsp static const struct got_error *
165 05118f5a 2021-06-22 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
166 05118f5a 2021-06-22 stsp struct got_repository *repo,
167 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
168 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
170 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
171 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
172 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
173 e6bcace5 2021-06-22 stsp struct got_delta_instruction *deltas;
174 05118f5a 2021-06-22 stsp int i, j, size, ndeltas, best;
175 e6bcace5 2021-06-22 stsp const int max_base_candidates = 10;
176 cc7a354a 2021-10-15 stsp int outfd = -1;
178 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
179 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
180 e6bcace5 2021-06-22 stsp if (cancel_cb) {
181 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
185 05118f5a 2021-06-22 stsp if (progress_cb) {
186 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
190 e6bcace5 2021-06-22 stsp m = meta[i];
191 e6bcace5 2021-06-22 stsp m->deltas = NULL;
192 e6bcace5 2021-06-22 stsp m->ndeltas = 0;
194 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
195 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
198 cc7a354a 2021-10-15 stsp if (outfd == -1) {
199 cc7a354a 2021-10-15 stsp outfd = got_opentempfd();
200 cc7a354a 2021-10-15 stsp if (outfd == -1) {
201 cc7a354a 2021-10-15 stsp err = got_error_from_errno("got_opentempfd");
205 cc7a354a 2021-10-15 stsp err = got_object_raw_open(&raw, outfd, repo, &m->id, 8192);
208 cc7a354a 2021-10-15 stsp if (raw->data == NULL)
209 cc7a354a 2021-10-15 stsp outfd = -1; /* outfd is now raw->f */
210 600b755e 2021-10-14 stsp m->size = raw->size;
212 e6bcace5 2021-06-22 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
213 e6bcace5 2021-06-22 stsp raw->size + raw->hdrlen);
217 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
218 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
219 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
220 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
221 e6bcace5 2021-06-22 stsp n->dtab = NULL;
224 e6bcace5 2021-06-22 stsp best = raw->size;
225 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
226 e6bcace5 2021-06-22 stsp if (cancel_cb) {
227 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
231 e6bcace5 2021-06-22 stsp base = meta[j];
232 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
233 e6bcace5 2021-06-22 stsp if (base->nchain >= 128 ||
234 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
237 cc7a354a 2021-10-15 stsp if (outfd == -1) {
238 cc7a354a 2021-10-15 stsp outfd = got_opentempfd();
239 cc7a354a 2021-10-15 stsp if (outfd == -1) {
240 cc7a354a 2021-10-15 stsp err = got_error_from_errno(
241 cc7a354a 2021-10-15 stsp "got_opentempfd");
245 cc7a354a 2021-10-15 stsp err = got_object_raw_open(&base_raw, outfd, repo,
246 cc7a354a 2021-10-15 stsp &base->id, 8192);
249 cc7a354a 2021-10-15 stsp if (base_raw->data == NULL)
250 cc7a354a 2021-10-15 stsp outfd = -1; /* outfd is now base_raw->f */
251 e6bcace5 2021-06-22 stsp err = got_deltify(&deltas, &ndeltas,
252 e6bcace5 2021-06-22 stsp raw->f, raw->hdrlen, raw->size + raw->hdrlen,
253 e6bcace5 2021-06-22 stsp base->dtab, base_raw->f, base_raw->hdrlen,
254 e6bcace5 2021-06-22 stsp base_raw->size + base_raw->hdrlen);
255 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
256 e6bcace5 2021-06-22 stsp base_raw = NULL;
260 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
261 e6bcace5 2021-06-22 stsp if (size + 32 < best){
263 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
264 e6bcace5 2021-06-22 stsp * replace it.
266 e6bcace5 2021-06-22 stsp free(m->deltas);
267 e6bcace5 2021-06-22 stsp best = size;
268 e6bcace5 2021-06-22 stsp m->deltas = deltas;
269 e6bcace5 2021-06-22 stsp m->ndeltas = ndeltas;
270 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
271 e6bcace5 2021-06-22 stsp m->prev = base;
272 e6bcace5 2021-06-22 stsp m->head = base->head;
273 e6bcace5 2021-06-22 stsp if (m->head == NULL)
274 e6bcace5 2021-06-22 stsp m->head = base;
276 e6bcace5 2021-06-22 stsp free(deltas);
277 e6bcace5 2021-06-22 stsp deltas = NULL;
278 e6bcace5 2021-06-22 stsp ndeltas = 0;
282 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
283 e6bcace5 2021-06-22 stsp raw = NULL;
286 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
287 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
288 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
291 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
292 e6bcace5 2021-06-22 stsp if (base_raw)
293 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
294 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
295 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
296 e6bcace5 2021-06-22 stsp return err;
299 e6bcace5 2021-06-22 stsp static const struct got_error *
300 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
301 05118f5a 2021-06-22 stsp struct got_repository *repo)
303 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
304 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
307 05118f5a 2021-06-22 stsp *found = 0;
309 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
310 05118f5a 2021-06-22 stsp if (err == NULL)
311 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
312 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
313 05118f5a 2021-06-22 stsp err = NULL;
314 05118f5a 2021-06-22 stsp return err;
317 07165b17 2021-07-01 stsp static const int obj_types[] = {
318 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_ANY,
319 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_COMMIT,
320 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TREE,
321 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_BLOB,
322 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TAG,
323 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_OFFSET_DELTA,
324 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_REF_DELTA
327 05118f5a 2021-06-22 stsp static const struct got_error *
328 e6bcace5 2021-06-22 stsp add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
329 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
330 05118f5a 2021-06-22 stsp time_t mtime, int loose_obj_only, struct got_repository *repo)
332 e6bcace5 2021-06-22 stsp const struct got_error *err;
333 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
335 05118f5a 2021-06-22 stsp if (loose_obj_only) {
336 05118f5a 2021-06-22 stsp int is_packed;
337 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
339 05118f5a 2021-06-22 stsp return err;
340 05118f5a 2021-06-22 stsp if (is_packed)
341 05118f5a 2021-06-22 stsp return NULL;
344 07165b17 2021-07-01 stsp err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
346 e6bcace5 2021-06-22 stsp return err;
348 e6bcace5 2021-06-22 stsp if (v == NULL)
349 e6bcace5 2021-06-22 stsp return NULL;
351 e6bcace5 2021-06-22 stsp err = alloc_meta(&m, id, path, obj_type, mtime);
355 e6bcace5 2021-06-22 stsp if (v->nmeta == v->metasz){
356 e6bcace5 2021-06-22 stsp size_t newsize = 2 * v->metasz;
357 e6bcace5 2021-06-22 stsp struct got_pack_meta **new;
358 e6bcace5 2021-06-22 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
359 e6bcace5 2021-06-22 stsp if (new == NULL) {
360 e6bcace5 2021-06-22 stsp err = got_error_from_errno("reallocarray");
363 e6bcace5 2021-06-22 stsp v->meta = new;
364 e6bcace5 2021-06-22 stsp v->metasz = newsize;
368 e6bcace5 2021-06-22 stsp clear_meta(m);
371 e6bcace5 2021-06-22 stsp v->meta[v->nmeta++] = m;
373 e6bcace5 2021-06-22 stsp return err;
376 e6bcace5 2021-06-22 stsp static const struct got_error *
377 e6bcace5 2021-06-22 stsp load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
378 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
379 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
380 05118f5a 2021-06-22 stsp int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
382 e6bcace5 2021-06-22 stsp const struct got_error *err;
383 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
384 e6bcace5 2021-06-22 stsp char *p = NULL;
387 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
389 e6bcace5 2021-06-22 stsp return err;
391 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
392 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
393 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
394 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
396 e6bcace5 2021-06-22 stsp if (cancel_cb) {
397 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
402 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
403 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
406 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
407 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
408 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
412 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
413 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
414 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
417 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
418 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
419 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
420 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
428 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
430 e6bcace5 2021-06-22 stsp return err;
433 e6bcace5 2021-06-22 stsp static const struct got_error *
434 e6bcace5 2021-06-22 stsp load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
435 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
436 05118f5a 2021-06-22 stsp int loose_obj_only, struct got_repository *repo,
437 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
439 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
440 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
441 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
443 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
444 e6bcace5 2021-06-22 stsp return NULL;
446 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
448 e6bcace5 2021-06-22 stsp return err;
450 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
451 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
453 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
454 e6bcace5 2021-06-22 stsp if (cancel_cb) {
455 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
460 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
461 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
463 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
464 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
468 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
469 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
471 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
475 e6bcace5 2021-06-22 stsp err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
476 05118f5a 2021-06-22 stsp mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
477 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
482 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
483 e6bcace5 2021-06-22 stsp return err;
486 e6bcace5 2021-06-22 stsp static const struct got_error *
487 e6bcace5 2021-06-22 stsp load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
488 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
489 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
491 e6bcace5 2021-06-22 stsp const struct got_error *err;
492 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
494 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
495 e6bcace5 2021-06-22 stsp return NULL;
497 05118f5a 2021-06-22 stsp if (loose_obj_only) {
498 05118f5a 2021-06-22 stsp int is_packed;
499 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
501 05118f5a 2021-06-22 stsp return err;
502 05118f5a 2021-06-22 stsp if (is_packed)
503 05118f5a 2021-06-22 stsp return NULL;
506 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
508 e6bcace5 2021-06-22 stsp return err;
510 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
511 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
512 05118f5a 2021-06-22 stsp loose_obj_only, repo);
516 e6bcace5 2021-06-22 stsp err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
517 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
518 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
520 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
521 e6bcace5 2021-06-22 stsp return err;
524 05118f5a 2021-06-22 stsp static const struct got_error *
525 05118f5a 2021-06-22 stsp load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
526 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
527 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
529 05118f5a 2021-06-22 stsp const struct got_error *err;
530 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
532 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
533 05118f5a 2021-06-22 stsp return NULL;
535 05118f5a 2021-06-22 stsp if (loose_obj_only) {
536 05118f5a 2021-06-22 stsp int is_packed;
537 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
539 05118f5a 2021-06-22 stsp return err;
540 05118f5a 2021-06-22 stsp if (is_packed)
541 05118f5a 2021-06-22 stsp return NULL;
544 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
546 05118f5a 2021-06-22 stsp return err;
548 05118f5a 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
549 05118f5a 2021-06-22 stsp got_object_tag_get_tagger_time(tag),
550 05118f5a 2021-06-22 stsp loose_obj_only, repo);
554 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
555 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
556 26960ff7 2021-09-14 stsp err = load_commit(v, idset,
557 05118f5a 2021-06-22 stsp got_object_tag_get_object_id(tag), repo,
558 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
560 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
561 05118f5a 2021-06-22 stsp err = load_tree(v, idset, got_object_tag_get_object_id(tag),
562 05118f5a 2021-06-22 stsp "", got_object_tag_get_tagger_time(tag),
563 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
570 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
571 05118f5a 2021-06-22 stsp return err;
574 e6bcace5 2021-06-22 stsp enum findtwixt_color {
575 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
576 e6bcace5 2021-06-22 stsp COLOR_DROP,
577 e6bcace5 2021-06-22 stsp COLOR_BLANK,
579 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
580 e6bcace5 2021-06-22 stsp COLOR_KEEP,
581 e6bcace5 2021-06-22 stsp COLOR_DROP,
582 e6bcace5 2021-06-22 stsp COLOR_BLANK
585 e6bcace5 2021-06-22 stsp static const struct got_error *
586 e6bcace5 2021-06-22 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
587 e6bcace5 2021-06-22 stsp int color, struct got_repository *repo)
589 e6bcace5 2021-06-22 stsp const struct got_error *err;
590 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
592 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
594 e6bcace5 2021-06-22 stsp return err;
596 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
597 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
598 e6bcace5 2021-06-22 stsp return NULL;
601 e6bcace5 2021-06-22 stsp static const struct got_error *
602 e6bcace5 2021-06-22 stsp drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
603 e6bcace5 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo,
604 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
606 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
607 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
608 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
609 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
610 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
612 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
614 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
616 e6bcace5 2021-06-22 stsp return err;
617 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&ids, qid, entry);
619 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
620 e6bcace5 2021-06-22 stsp if (cancel_cb) {
621 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
626 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
627 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
629 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id)) {
630 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
634 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, qid->id,
635 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
637 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
641 e6bcace5 2021-06-22 stsp if (!got_object_idset_contains(keep, qid->id)) {
642 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
646 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
647 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
651 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
652 e6bcace5 2021-06-22 stsp if (parents) {
653 e6bcace5 2021-06-22 stsp err = got_object_id_queue_copy(parents, &ids);
655 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
659 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
662 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
663 e6bcace5 2021-06-22 stsp return err;
666 e6bcace5 2021-06-22 stsp struct append_id_arg {
667 e6bcace5 2021-06-22 stsp struct got_object_id **array;
671 e6bcace5 2021-06-22 stsp static const struct got_error *
672 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
674 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
676 e6bcace5 2021-06-22 stsp a->array[a->idx] = got_object_id_dup(id);
677 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
678 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
681 e6bcace5 2021-06-22 stsp return NULL;
684 e6bcace5 2021-06-22 stsp static const struct got_error *
685 e6bcace5 2021-06-22 stsp findtwixt(struct got_object_id ***res, int *nres,
686 e6bcace5 2021-06-22 stsp struct got_object_id **head, int nhead,
687 e6bcace5 2021-06-22 stsp struct got_object_id **tail, int ntail,
688 e6bcace5 2021-06-22 stsp struct got_repository *repo,
689 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
691 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
692 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
693 e6bcace5 2021-06-22 stsp struct got_object_idset *keep, *drop;
694 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
695 05118f5a 2021-06-22 stsp int i, ncolor, nkeep, obj_type;
697 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
698 e6bcace5 2021-06-22 stsp *res = NULL;
701 e6bcace5 2021-06-22 stsp keep = got_object_idset_alloc();
702 e6bcace5 2021-06-22 stsp if (keep == NULL)
703 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
705 e6bcace5 2021-06-22 stsp drop = got_object_idset_alloc();
706 e6bcace5 2021-06-22 stsp if (drop == NULL) {
707 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_idset_alloc");
711 05118f5a 2021-06-22 stsp for (i = 0; i < nhead; i++) {
712 05118f5a 2021-06-22 stsp struct got_object_id *id = head[i];
713 05118f5a 2021-06-22 stsp if (id == NULL)
715 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
717 05118f5a 2021-06-22 stsp return err;
718 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
720 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
724 05118f5a 2021-06-22 stsp for (i = 0; i < ntail; i++) {
725 05118f5a 2021-06-22 stsp struct got_object_id *id = tail[i];
726 05118f5a 2021-06-22 stsp if (id == NULL)
728 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
730 05118f5a 2021-06-22 stsp return err;
731 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
733 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_DROP, repo);
738 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
739 e6bcace5 2021-06-22 stsp int qcolor;
740 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
741 e6bcace5 2021-06-22 stsp qcolor = *((int *)qid->data);
743 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id))
744 e6bcace5 2021-06-22 stsp ncolor = COLOR_DROP;
745 e6bcace5 2021-06-22 stsp else if (got_object_idset_contains(keep, qid->id))
746 e6bcace5 2021-06-22 stsp ncolor = COLOR_KEEP;
748 e6bcace5 2021-06-22 stsp ncolor = COLOR_BLANK;
750 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
751 e6bcace5 2021-06-22 stsp qcolor == COLOR_KEEP)) {
752 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
753 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
757 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
758 e6bcace5 2021-06-22 stsp err = drop_commit(keep, drop, qid->id, repo,
759 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
762 e6bcace5 2021-06-22 stsp } else if (ncolor == COLOR_BLANK) {
763 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
764 e6bcace5 2021-06-22 stsp struct got_object_id *id;
765 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
766 e6bcace5 2021-06-22 stsp struct got_object_qid *pid;
768 e6bcace5 2021-06-22 stsp id = got_object_id_dup(qid->id);
769 e6bcace5 2021-06-22 stsp if (id == NULL) {
770 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
773 e6bcace5 2021-06-22 stsp if (qcolor == COLOR_KEEP)
774 07165b17 2021-07-01 stsp err = got_object_idset_add(keep, id,
775 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
777 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, id,
778 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
784 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
789 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
790 e6bcace5 2021-06-22 stsp if (parents) {
791 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parents, entry) {
792 e6bcace5 2021-06-22 stsp err = queue_commit_id(&ids, pid->id,
793 e6bcace5 2021-06-22 stsp qcolor, repo);
800 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
801 e6bcace5 2021-06-22 stsp commit = NULL;
803 e6bcace5 2021-06-22 stsp /* should not happen */
804 e6bcace5 2021-06-22 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
805 e6bcace5 2021-06-22 stsp "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
809 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
810 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
813 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
814 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
815 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
816 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
817 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
818 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
821 e6bcace5 2021-06-22 stsp arg.idx = 0;
822 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
824 e6bcace5 2021-06-22 stsp free(arg.array);
827 e6bcace5 2021-06-22 stsp *res = arg.array;
828 e6bcace5 2021-06-22 stsp *nres = nkeep;
831 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
832 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
833 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
834 e6bcace5 2021-06-22 stsp return err;
837 e6bcace5 2021-06-22 stsp static const struct got_error *
838 e6bcace5 2021-06-22 stsp read_meta(struct got_pack_meta ***meta, int *nmeta,
839 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
840 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
841 05118f5a 2021-06-22 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
842 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
844 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
845 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
846 e6bcace5 2021-06-22 stsp struct got_object_idset *idset;
847 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
848 e6bcace5 2021-06-22 stsp struct got_pack_metavec v;
850 e6bcace5 2021-06-22 stsp *meta = NULL;
851 e6bcace5 2021-06-22 stsp *nmeta = 0;
853 e6bcace5 2021-06-22 stsp idset = got_object_idset_alloc();
854 e6bcace5 2021-06-22 stsp if (idset == NULL)
855 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
857 e6bcace5 2021-06-22 stsp v.nmeta = 0;
858 e6bcace5 2021-06-22 stsp v.metasz = 64;
859 e6bcace5 2021-06-22 stsp v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
860 e6bcace5 2021-06-22 stsp if (v.meta == NULL) {
861 1d19226a 2021-10-13 stsp err = got_error_from_errno("calloc");
865 e6bcace5 2021-06-22 stsp err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
866 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
867 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
870 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
871 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
872 05118f5a 2021-06-22 stsp if (id == NULL)
874 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
876 05118f5a 2021-06-22 stsp return err;
877 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
879 05118f5a 2021-06-22 stsp err = load_commit(NULL, idset, id, repo,
880 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
883 05118f5a 2021-06-22 stsp if (progress_cb) {
884 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
885 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
891 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
892 f4a2ff2d 2021-07-01 stsp struct got_object_id *id = theirs[i];
893 07165b17 2021-07-01 stsp int *cached_type;
894 05118f5a 2021-06-22 stsp if (id == NULL)
896 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
897 07165b17 2021-07-01 stsp if (cached_type == NULL) {
898 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
902 07165b17 2021-07-01 stsp obj_type = *cached_type;
903 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
905 05118f5a 2021-06-22 stsp err = load_tag(NULL, idset, id, repo,
906 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
909 05118f5a 2021-06-22 stsp if (progress_cb) {
910 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
911 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
917 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
918 eca70f98 2021-09-03 stsp err = load_commit(&v, idset, ids[i], repo,
919 eca70f98 2021-09-03 stsp loose_obj_only, cancel_cb, cancel_arg);
922 eca70f98 2021-09-03 stsp if (progress_cb) {
923 eca70f98 2021-09-03 stsp err = progress_cb(progress_arg, 0L, nours,
924 eca70f98 2021-09-03 stsp v.nmeta, 0, 0);
930 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
931 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
932 07165b17 2021-07-01 stsp int *cached_type;
933 05118f5a 2021-06-22 stsp if (id == NULL)
935 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
936 07165b17 2021-07-01 stsp if (cached_type == NULL) {
937 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
941 07165b17 2021-07-01 stsp obj_type = *cached_type;
942 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
944 05118f5a 2021-06-22 stsp err = load_tag(&v, idset, id, repo,
945 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
948 05118f5a 2021-06-22 stsp if (progress_cb) {
949 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
950 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
957 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
958 e6bcace5 2021-06-22 stsp free(ids[i]);
961 e6bcace5 2021-06-22 stsp got_object_idset_free(idset);
962 e6bcace5 2021-06-22 stsp if (err == NULL) {
963 e6bcace5 2021-06-22 stsp *meta = v.meta;
964 e6bcace5 2021-06-22 stsp *nmeta = v.nmeta;
966 e6bcace5 2021-06-22 stsp free(v.meta);
968 e6bcace5 2021-06-22 stsp return err;
971 e6bcace5 2021-06-22 stsp const struct got_error *
972 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
976 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
977 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
978 e6bcace5 2021-06-22 stsp if (n != len)
979 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
980 e6bcace5 2021-06-22 stsp return NULL;
983 e6bcace5 2021-06-22 stsp static void
984 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
986 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
987 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
988 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
989 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
993 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
995 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
997 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
998 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
999 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
1000 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1001 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
1002 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
1003 e6bcace5 2021-06-22 stsp if (ahd != bhd)
1004 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
1005 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1006 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1007 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
1010 e6bcace5 2021-06-22 stsp static const struct got_error *
1011 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1015 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1017 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1018 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1019 e6bcace5 2021-06-22 stsp len >>= 4;
1020 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1021 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1022 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1023 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1024 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1025 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1028 e6bcace5 2021-06-22 stsp *hdrlen = i;
1029 e6bcace5 2021-06-22 stsp return NULL;
1032 e6bcace5 2021-06-22 stsp static const struct got_error *
1033 08347b73 2021-10-14 stsp encodedelta(struct got_pack_meta *m, struct got_raw_object *o,
1034 08347b73 2021-10-14 stsp off_t base_size, FILE *f)
1036 e6bcace5 2021-06-22 stsp unsigned char buf[16], *bp;
1040 e6bcace5 2021-06-22 stsp struct got_delta_instruction *d;
1042 e6bcace5 2021-06-22 stsp /* base object size */
1043 e6bcace5 2021-06-22 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1044 e6bcace5 2021-06-22 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
1045 e6bcace5 2021-06-22 stsp for (i = 1; n > 0; i++) {
1046 e6bcace5 2021-06-22 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1047 e6bcace5 2021-06-22 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1048 e6bcace5 2021-06-22 stsp n >>= GOT_DELTA_SIZE_SHIFT;
1050 08347b73 2021-10-14 stsp w = fwrite(buf, 1, i, f);
1051 08347b73 2021-10-14 stsp if (w != i)
1052 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1054 e6bcace5 2021-06-22 stsp /* target object size */
1055 e6bcace5 2021-06-22 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1056 e6bcace5 2021-06-22 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
1057 e6bcace5 2021-06-22 stsp for (i = 1; n > 0; i++) {
1058 e6bcace5 2021-06-22 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1059 e6bcace5 2021-06-22 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1060 e6bcace5 2021-06-22 stsp n >>= GOT_DELTA_SIZE_SHIFT;
1062 08347b73 2021-10-14 stsp w = fwrite(buf, 1, i, f);
1063 08347b73 2021-10-14 stsp if (w != i)
1064 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1066 e6bcace5 2021-06-22 stsp for (j = 0; j < m->ndeltas; j++) {
1067 e6bcace5 2021-06-22 stsp d = &m->deltas[j];
1068 e6bcace5 2021-06-22 stsp if (d->copy) {
1069 e6bcace5 2021-06-22 stsp n = d->offset;
1070 e6bcace5 2021-06-22 stsp bp = &buf[1];
1071 e6bcace5 2021-06-22 stsp buf[0] = GOT_DELTA_BASE_COPY;
1072 e6bcace5 2021-06-22 stsp for (i = 0; i < 4; i++) {
1073 e6bcace5 2021-06-22 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1074 e6bcace5 2021-06-22 stsp buf[0] |= 1 << i;
1075 e6bcace5 2021-06-22 stsp *bp++ = n & 0xff;
1077 e6bcace5 2021-06-22 stsp if (n == 0)
1081 e6bcace5 2021-06-22 stsp n = d->len;
1082 e6bcace5 2021-06-22 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1083 e6bcace5 2021-06-22 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1084 e6bcace5 2021-06-22 stsp for (i = 0; i < 3 && n > 0; i++) {
1085 e6bcace5 2021-06-22 stsp buf[0] |= 1 << (i + 4);
1086 e6bcace5 2021-06-22 stsp *bp++ = n & 0xff;
1090 08347b73 2021-10-14 stsp w = fwrite(buf, 1, bp - buf, f);
1091 08347b73 2021-10-14 stsp if (w != bp - buf)
1092 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1094 e6bcace5 2021-06-22 stsp char content[128];
1096 e6bcace5 2021-06-22 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1097 e6bcace5 2021-06-22 stsp return got_error_from_errno("fseeko");
1099 e6bcace5 2021-06-22 stsp while (n != d->len) {
1100 e6bcace5 2021-06-22 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
1101 08347b73 2021-10-14 stsp w = fwrite(buf, 1, 1, f);
1102 08347b73 2021-10-14 stsp if (w != 1)
1103 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1104 e6bcace5 2021-06-22 stsp r = fread(content, 1, buf[0], o->f);
1105 e6bcace5 2021-06-22 stsp if (r != buf[0])
1106 e6bcace5 2021-06-22 stsp return got_ferror(o->f, GOT_ERR_IO);
1107 08347b73 2021-10-14 stsp w = fwrite(content, 1, buf[0], f);
1108 08347b73 2021-10-14 stsp if (w != buf[0])
1109 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1110 e6bcace5 2021-06-22 stsp n += buf[0];
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 e6bcace5 2021-06-22 stsp genpack(uint8_t *pack_sha1, FILE *packfile,
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 08347b73 2021-10-14 stsp int i, nh;
1146 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1147 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1148 600b755e 2021-10-14 stsp struct got_raw_object *raw = NULL;
1149 08347b73 2021-10-14 stsp FILE *delta_file = NULL;
1150 08347b73 2021-10-14 stsp char buf[32];
1151 e6bcace5 2021-06-22 stsp size_t outlen, n;
1152 e6bcace5 2021-06-22 stsp struct got_deflate_checksum csum;
1153 05118f5a 2021-06-22 stsp off_t packfile_size = 0;
1154 cc7a354a 2021-10-15 stsp int outfd = -1;
1156 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1157 e6bcace5 2021-06-22 stsp csum.output_sha1 = &ctx;
1158 e6bcace5 2021-06-22 stsp csum.output_crc = NULL;
1160 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1162 e6bcace5 2021-06-22 stsp return err;
1163 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1164 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1166 e6bcace5 2021-06-22 stsp goto done;
1167 e6bcace5 2021-06-22 stsp putbe32(buf, nmeta);
1168 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1170 e6bcace5 2021-06-22 stsp goto done;
1171 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1172 05118f5a 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
1173 05118f5a 2021-06-22 stsp if (progress_cb) {
1174 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1175 05118f5a 2021-06-22 stsp nmeta, nmeta, i);
1177 05118f5a 2021-06-22 stsp goto done;
1179 e6bcace5 2021-06-22 stsp m = meta[i];
1180 e6bcace5 2021-06-22 stsp m->off = ftello(packfile);
1181 cc7a354a 2021-10-15 stsp if (outfd == -1) {
1182 cc7a354a 2021-10-15 stsp outfd = got_opentempfd();
1183 cc7a354a 2021-10-15 stsp if (outfd == -1) {
1184 cc7a354a 2021-10-15 stsp err = got_error_from_errno("got_opentempfd");
1185 cc7a354a 2021-10-15 stsp goto done;
1188 cc7a354a 2021-10-15 stsp err = got_object_raw_open(&raw, outfd, repo, &m->id, 8192);
1190 e6bcace5 2021-06-22 stsp goto done;
1191 cc7a354a 2021-10-15 stsp if (raw->data == NULL)
1192 cc7a354a 2021-10-15 stsp outfd = -1; /* outfd is now raw->f */
1193 e6bcace5 2021-06-22 stsp if (m->deltas == NULL) {
1194 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1195 e6bcace5 2021-06-22 stsp m->obj_type, raw->size);
1197 e6bcace5 2021-06-22 stsp goto done;
1198 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1200 e6bcace5 2021-06-22 stsp goto done;
1201 05118f5a 2021-06-22 stsp packfile_size += nh;
1202 e6bcace5 2021-06-22 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1203 e6bcace5 2021-06-22 stsp err = got_error_from_errno("fseeko");
1204 e6bcace5 2021-06-22 stsp goto done;
1206 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, raw->f, packfile,
1209 e6bcace5 2021-06-22 stsp goto done;
1210 05118f5a 2021-06-22 stsp packfile_size += outlen;
1212 08347b73 2021-10-14 stsp if (delta_file == NULL) {
1213 08347b73 2021-10-14 stsp delta_file = got_opentemp();
1214 08347b73 2021-10-14 stsp if (delta_file == NULL) {
1215 08347b73 2021-10-14 stsp err = got_error_from_errno(
1216 08347b73 2021-10-14 stsp "got_opentemp");
1217 08347b73 2021-10-14 stsp goto done;
1220 08347b73 2021-10-14 stsp if (ftruncate(fileno(delta_file), 0L) == -1) {
1221 08347b73 2021-10-14 stsp err = got_error_from_errno("ftruncate");
1222 08347b73 2021-10-14 stsp goto done;
1224 08347b73 2021-10-14 stsp if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1225 08347b73 2021-10-14 stsp err = got_error_from_errno("fseeko");
1226 08347b73 2021-10-14 stsp goto done;
1228 600b755e 2021-10-14 stsp err = encodedelta(m, raw, m->prev->size, delta_file);
1230 e6bcace5 2021-06-22 stsp goto done;
1231 08347b73 2021-10-14 stsp nd = ftello(delta_file);
1232 08347b73 2021-10-14 stsp if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1233 08347b73 2021-10-14 stsp err = got_error_from_errno("fseeko");
1234 08347b73 2021-10-14 stsp goto done;
1236 e6bcace5 2021-06-22 stsp if (use_offset_deltas && m->prev->off != 0) {
1237 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1238 e6bcace5 2021-06-22 stsp GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1240 e6bcace5 2021-06-22 stsp goto done;
1241 e6bcace5 2021-06-22 stsp nh += packoff(buf + nh,
1242 e6bcace5 2021-06-22 stsp m->off - m->prev->off);
1243 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1245 e6bcace5 2021-06-22 stsp goto done;
1246 05118f5a 2021-06-22 stsp packfile_size += nh;
1248 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1249 e6bcace5 2021-06-22 stsp GOT_OBJ_TYPE_REF_DELTA, nd);
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;
1254 e6bcace5 2021-06-22 stsp err = hwrite(packfile, m->prev->id.sha1,
1255 e6bcace5 2021-06-22 stsp sizeof(m->prev->id.sha1), &ctx);
1256 05118f5a 2021-06-22 stsp packfile_size += sizeof(m->prev->id.sha1);
1258 e6bcace5 2021-06-22 stsp goto done;
1260 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, delta_file,
1261 e6bcace5 2021-06-22 stsp packfile, &csum);
1263 e6bcace5 2021-06-22 stsp goto done;
1264 05118f5a 2021-06-22 stsp packfile_size += outlen;
1266 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
1267 e6bcace5 2021-06-22 stsp raw = NULL;
1269 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1270 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1271 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1272 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1273 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1274 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1275 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1276 05118f5a 2021-06-22 stsp nmeta, nmeta, nmeta);
1278 05118f5a 2021-06-22 stsp goto done;
1280 08347b73 2021-10-14 stsp if (delta_file && fclose(delta_file) == EOF && err == NULL)
1281 08347b73 2021-10-14 stsp err = got_error_from_errno("fclose");
1283 08347b73 2021-10-14 stsp got_object_raw_close(raw);
1284 cc7a354a 2021-10-15 stsp if (outfd != -1 && close(outfd) == -1 && err == NULL)
1285 cc7a354a 2021-10-15 stsp err = got_error_from_errno("close");
1286 e6bcace5 2021-06-22 stsp return err;
1289 e6bcace5 2021-06-22 stsp const struct got_error *
1290 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1291 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1292 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1293 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1294 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1295 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1297 e6bcace5 2021-06-22 stsp const struct got_error *err;
1298 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
1299 e6bcace5 2021-06-22 stsp int nmeta;
1301 e6bcace5 2021-06-22 stsp err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1302 05118f5a 2021-06-22 stsp loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1304 e6bcace5 2021-06-22 stsp return err;
1306 f8a36e22 2021-08-26 stsp if (nmeta == 0 && !allow_empty) {
1307 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1308 05118f5a 2021-06-22 stsp goto done;
1310 f8a36e22 2021-08-26 stsp if (nmeta > 0) {
1311 f8a36e22 2021-08-26 stsp err = pick_deltas(meta, nmeta, nours, repo,
1312 f8a36e22 2021-08-26 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1314 f8a36e22 2021-08-26 stsp goto done;
1317 05118f5a 2021-06-22 stsp err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1318 05118f5a 2021-06-22 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1320 e6bcace5 2021-06-22 stsp goto done;
1322 e6bcace5 2021-06-22 stsp free_nmeta(meta, nmeta);
1323 e6bcace5 2021-06-22 stsp return err;