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 0be8fa4c 2021-10-15 thomas #include <sys/queue.h>
20 0be8fa4c 2021-10-15 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 e6bcace5 2021-06-22 stsp #include "got_error.h"
32 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
33 e6bcace5 2021-06-22 stsp #include "got_object.h"
34 0be8fa4c 2021-10-15 thomas #include "got_path.h"
35 05118f5a 2021-06-22 stsp #include "got_reference.h"
36 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
37 e4d8ab47 2021-10-15 thomas #include "got_opentemp.h"
39 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
40 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
41 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
42 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
43 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
44 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
45 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
46 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
47 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
49 b79280dd 2021-10-15 thomas #ifndef MIN
50 b79280dd 2021-10-15 thomas #define MIN(_a,_b) ((_a) < (_b) ? (_a) : (_b))
54 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
57 e6bcace5 2021-06-22 stsp struct got_pack_meta {
58 e6bcace5 2021-06-22 stsp struct got_object_id id;
60 e6bcace5 2021-06-22 stsp int obj_type;
61 ab6186ae 2021-10-15 thomas off_t size;
62 e6bcace5 2021-06-22 stsp time_t mtime;
64 e6bcace5 2021-06-22 stsp /* The best delta we picked */
65 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
66 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
67 b79280dd 2021-10-15 thomas off_t delta_offset; /* offset in delta cache file */
68 b79280dd 2021-10-15 thomas off_t delta_len; /* length in delta cache file */
71 e6bcace5 2021-06-22 stsp /* Only used for delta window */
72 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
74 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
78 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
79 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
84 e6bcace5 2021-06-22 stsp static const struct got_error *
85 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
86 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
88 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
89 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
91 e6bcace5 2021-06-22 stsp *new = NULL;
93 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
94 e6bcace5 2021-06-22 stsp if (m == NULL)
95 b7e0a384 2021-10-15 thomas return got_error_from_errno("calloc");
97 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
99 e6bcace5 2021-06-22 stsp m->path = strdup(path);
100 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
101 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
103 e6bcace5 2021-06-22 stsp return err;
106 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
107 e6bcace5 2021-06-22 stsp m->mtime = mtime;
109 e6bcace5 2021-06-22 stsp return NULL;
112 e6bcace5 2021-06-22 stsp static void
113 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
115 e6bcace5 2021-06-22 stsp if (meta == 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;
163 b79280dd 2021-10-15 thomas static const struct got_error *
164 b79280dd 2021-10-15 thomas encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
165 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas, int ndeltas,
166 b79280dd 2021-10-15 thomas off_t base_size, FILE *f);
168 e6bcace5 2021-06-22 stsp static const struct got_error *
169 05118f5a 2021-06-22 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
170 b79280dd 2021-10-15 thomas FILE *delta_cache, struct got_repository *repo,
171 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
172 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
174 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
175 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
176 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
177 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas = NULL, *best_deltas = NULL;
178 b79280dd 2021-10-15 thomas int i, j, size, best_size, ndeltas, best_ndeltas;
179 e6bcace5 2021-06-22 stsp const int max_base_candidates = 10;
180 ecf9545f 2021-10-15 thomas int outfd = -1;
182 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
183 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
184 e6bcace5 2021-06-22 stsp if (cancel_cb) {
185 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
189 05118f5a 2021-06-22 stsp if (progress_cb) {
190 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
194 e6bcace5 2021-06-22 stsp m = meta[i];
196 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
197 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
200 31f4c1e6 2021-10-15 thomas err = got_object_raw_open(&raw, &outfd, repo, &m->id);
203 ab6186ae 2021-10-15 thomas m->size = raw->size;
205 e6bcace5 2021-06-22 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
206 e6bcace5 2021-06-22 stsp raw->size + raw->hdrlen);
210 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
211 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
212 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
213 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
214 e6bcace5 2021-06-22 stsp n->dtab = NULL;
217 b79280dd 2021-10-15 thomas best_size = raw->size;
218 b79280dd 2021-10-15 thomas best_ndeltas = 0;
219 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
220 e6bcace5 2021-06-22 stsp if (cancel_cb) {
221 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
225 e6bcace5 2021-06-22 stsp base = meta[j];
226 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
227 33acf1a2 2021-10-15 thomas if (base->nchain >= 32 ||
228 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
231 8ab9215c 2021-10-15 thomas err = got_object_raw_open(&base_raw, &outfd, repo,
232 31f4c1e6 2021-10-15 thomas &base->id);
235 e6bcace5 2021-06-22 stsp err = got_deltify(&deltas, &ndeltas,
236 e6bcace5 2021-06-22 stsp raw->f, raw->hdrlen, raw->size + raw->hdrlen,
237 e6bcace5 2021-06-22 stsp base->dtab, base_raw->f, base_raw->hdrlen,
238 e6bcace5 2021-06-22 stsp base_raw->size + base_raw->hdrlen);
239 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
240 e6bcace5 2021-06-22 stsp base_raw = NULL;
244 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
245 b79280dd 2021-10-15 thomas if (size + 32 < best_size){
247 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
248 e6bcace5 2021-06-22 stsp * replace it.
250 b79280dd 2021-10-15 thomas best_size = size;
251 b79280dd 2021-10-15 thomas free(best_deltas);
252 b79280dd 2021-10-15 thomas best_deltas = deltas;
253 b79280dd 2021-10-15 thomas best_ndeltas = ndeltas;
254 b79280dd 2021-10-15 thomas deltas = NULL;
255 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
256 e6bcace5 2021-06-22 stsp m->prev = base;
257 e6bcace5 2021-06-22 stsp m->head = base->head;
258 e6bcace5 2021-06-22 stsp if (m->head == NULL)
259 e6bcace5 2021-06-22 stsp m->head = base;
261 e6bcace5 2021-06-22 stsp free(deltas);
262 e6bcace5 2021-06-22 stsp deltas = NULL;
263 e6bcace5 2021-06-22 stsp ndeltas = 0;
267 b79280dd 2021-10-15 thomas if (best_ndeltas > 0) {
268 b79280dd 2021-10-15 thomas m->delta_offset = ftello(delta_cache);
269 b79280dd 2021-10-15 thomas err = encode_delta(m, raw, best_deltas,
270 b79280dd 2021-10-15 thomas best_ndeltas, m->prev->size, delta_cache);
271 b79280dd 2021-10-15 thomas free(best_deltas);
272 b79280dd 2021-10-15 thomas best_deltas = NULL;
273 b79280dd 2021-10-15 thomas best_ndeltas = 0;
275 b79280dd 2021-10-15 thomas goto done;
276 b79280dd 2021-10-15 thomas m->delta_len = ftello(delta_cache) - m->delta_offset;
279 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
280 e6bcace5 2021-06-22 stsp raw = NULL;
283 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
284 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
285 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
288 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
289 e6bcace5 2021-06-22 stsp if (base_raw)
290 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
291 ecf9545f 2021-10-15 thomas if (outfd != -1 && close(outfd) == -1 && err == NULL)
292 ecf9545f 2021-10-15 thomas err = got_error_from_errno("close");
293 b79280dd 2021-10-15 thomas free(deltas);
294 b79280dd 2021-10-15 thomas free(best_deltas);
295 e6bcace5 2021-06-22 stsp return err;
298 e6bcace5 2021-06-22 stsp static const struct got_error *
299 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
300 05118f5a 2021-06-22 stsp struct got_repository *repo)
302 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
303 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
306 05118f5a 2021-06-22 stsp *found = 0;
308 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
309 05118f5a 2021-06-22 stsp if (err == NULL)
310 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
311 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
312 05118f5a 2021-06-22 stsp err = NULL;
313 05118f5a 2021-06-22 stsp return err;
316 07165b17 2021-07-01 stsp static const int obj_types[] = {
317 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_ANY,
318 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_COMMIT,
319 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TREE,
320 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_BLOB,
321 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TAG,
322 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_OFFSET_DELTA,
323 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_REF_DELTA
326 05118f5a 2021-06-22 stsp static const struct got_error *
327 e6bcace5 2021-06-22 stsp add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
328 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
329 05118f5a 2021-06-22 stsp time_t mtime, int loose_obj_only, struct got_repository *repo)
331 e6bcace5 2021-06-22 stsp const struct got_error *err;
332 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
334 05118f5a 2021-06-22 stsp if (loose_obj_only) {
335 05118f5a 2021-06-22 stsp int is_packed;
336 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
338 05118f5a 2021-06-22 stsp return err;
339 05118f5a 2021-06-22 stsp if (is_packed)
340 05118f5a 2021-06-22 stsp return NULL;
343 07165b17 2021-07-01 stsp err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
345 e6bcace5 2021-06-22 stsp return err;
347 e6bcace5 2021-06-22 stsp if (v == NULL)
348 e6bcace5 2021-06-22 stsp return NULL;
350 e6bcace5 2021-06-22 stsp err = alloc_meta(&m, id, path, obj_type, mtime);
354 e6bcace5 2021-06-22 stsp if (v->nmeta == v->metasz){
355 e6bcace5 2021-06-22 stsp size_t newsize = 2 * v->metasz;
356 e6bcace5 2021-06-22 stsp struct got_pack_meta **new;
357 e6bcace5 2021-06-22 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
358 e6bcace5 2021-06-22 stsp if (new == NULL) {
359 e6bcace5 2021-06-22 stsp err = got_error_from_errno("reallocarray");
362 e6bcace5 2021-06-22 stsp v->meta = new;
363 e6bcace5 2021-06-22 stsp v->metasz = newsize;
367 e6bcace5 2021-06-22 stsp clear_meta(m);
370 e6bcace5 2021-06-22 stsp v->meta[v->nmeta++] = m;
372 e6bcace5 2021-06-22 stsp return err;
375 e6bcace5 2021-06-22 stsp static const struct got_error *
376 e6bcace5 2021-06-22 stsp load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
377 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
378 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
379 05118f5a 2021-06-22 stsp int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
381 e6bcace5 2021-06-22 stsp const struct got_error *err;
382 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
383 e6bcace5 2021-06-22 stsp char *p = NULL;
386 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
388 e6bcace5 2021-06-22 stsp return err;
390 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
391 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
392 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
393 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
395 e6bcace5 2021-06-22 stsp if (cancel_cb) {
396 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
401 08511b5e 2021-09-24 thomas if (got_object_tree_entry_is_submodule(e) ||
402 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
405 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
406 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
407 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
411 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
412 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
413 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
416 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
417 08511b5e 2021-09-24 thomas } else if (S_ISREG(mode) || S_ISLNK(mode)) {
418 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
419 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
427 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
429 e6bcace5 2021-06-22 stsp return err;
432 e6bcace5 2021-06-22 stsp static const struct got_error *
433 e6bcace5 2021-06-22 stsp load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
434 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
435 05118f5a 2021-06-22 stsp int loose_obj_only, struct got_repository *repo,
436 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
438 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
439 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
440 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
442 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
443 e6bcace5 2021-06-22 stsp return NULL;
445 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
447 e6bcace5 2021-06-22 stsp return err;
449 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
450 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
452 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
453 e6bcace5 2021-06-22 stsp if (cancel_cb) {
454 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
459 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
460 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
462 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
463 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
467 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
468 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
470 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
474 e6bcace5 2021-06-22 stsp err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
475 05118f5a 2021-06-22 stsp mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
476 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
481 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
482 e6bcace5 2021-06-22 stsp return err;
485 e6bcace5 2021-06-22 stsp static const struct got_error *
486 e6bcace5 2021-06-22 stsp load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
487 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
488 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
490 e6bcace5 2021-06-22 stsp const struct got_error *err;
491 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
493 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
494 e6bcace5 2021-06-22 stsp return NULL;
496 05118f5a 2021-06-22 stsp if (loose_obj_only) {
497 05118f5a 2021-06-22 stsp int is_packed;
498 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
500 05118f5a 2021-06-22 stsp return err;
501 05118f5a 2021-06-22 stsp if (is_packed)
502 05118f5a 2021-06-22 stsp return NULL;
505 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
507 e6bcace5 2021-06-22 stsp return err;
509 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
510 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
511 05118f5a 2021-06-22 stsp loose_obj_only, repo);
515 e6bcace5 2021-06-22 stsp err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
516 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
517 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
519 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
520 e6bcace5 2021-06-22 stsp return err;
523 05118f5a 2021-06-22 stsp static const struct got_error *
524 05118f5a 2021-06-22 stsp load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
525 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
526 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
528 05118f5a 2021-06-22 stsp const struct got_error *err;
529 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
531 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
532 05118f5a 2021-06-22 stsp return NULL;
534 05118f5a 2021-06-22 stsp if (loose_obj_only) {
535 05118f5a 2021-06-22 stsp int is_packed;
536 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
538 05118f5a 2021-06-22 stsp return err;
539 05118f5a 2021-06-22 stsp if (is_packed)
540 05118f5a 2021-06-22 stsp return NULL;
543 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
545 05118f5a 2021-06-22 stsp return err;
547 05118f5a 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
548 05118f5a 2021-06-22 stsp got_object_tag_get_tagger_time(tag),
549 05118f5a 2021-06-22 stsp loose_obj_only, repo);
553 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
554 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
555 26960ff7 2021-09-14 stsp err = load_commit(v, idset,
556 05118f5a 2021-06-22 stsp got_object_tag_get_object_id(tag), repo,
557 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
559 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
560 05118f5a 2021-06-22 stsp err = load_tree(v, idset, got_object_tag_get_object_id(tag),
561 05118f5a 2021-06-22 stsp "", got_object_tag_get_tagger_time(tag),
562 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
569 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
570 05118f5a 2021-06-22 stsp return err;
573 e6bcace5 2021-06-22 stsp enum findtwixt_color {
574 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
575 e6bcace5 2021-06-22 stsp COLOR_DROP,
576 e6bcace5 2021-06-22 stsp COLOR_BLANK,
578 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
579 e6bcace5 2021-06-22 stsp COLOR_KEEP,
580 e6bcace5 2021-06-22 stsp COLOR_DROP,
581 e6bcace5 2021-06-22 stsp COLOR_BLANK
584 e6bcace5 2021-06-22 stsp static const struct got_error *
585 e6bcace5 2021-06-22 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
586 e6bcace5 2021-06-22 stsp int color, struct got_repository *repo)
588 e6bcace5 2021-06-22 stsp const struct got_error *err;
589 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
591 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
593 e6bcace5 2021-06-22 stsp return err;
595 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
596 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
597 e6bcace5 2021-06-22 stsp return NULL;
600 e6bcace5 2021-06-22 stsp static const struct got_error *
601 e6bcace5 2021-06-22 stsp drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
602 e6bcace5 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo,
603 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
605 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
606 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
607 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
608 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
609 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
611 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
613 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
615 e6bcace5 2021-06-22 stsp return err;
616 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&ids, qid, entry);
618 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
619 e6bcace5 2021-06-22 stsp if (cancel_cb) {
620 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
625 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
626 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
628 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id)) {
629 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
633 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, qid->id,
634 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
636 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
640 e6bcace5 2021-06-22 stsp if (!got_object_idset_contains(keep, qid->id)) {
641 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
645 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
646 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
650 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
651 e6bcace5 2021-06-22 stsp if (parents) {
652 e6bcace5 2021-06-22 stsp err = got_object_id_queue_copy(parents, &ids);
654 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
658 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
661 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
662 e6bcace5 2021-06-22 stsp return err;
665 e6bcace5 2021-06-22 stsp struct append_id_arg {
666 e6bcace5 2021-06-22 stsp struct got_object_id **array;
670 e6bcace5 2021-06-22 stsp static const struct got_error *
671 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
673 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
675 e6bcace5 2021-06-22 stsp a->array[a->idx] = got_object_id_dup(id);
676 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
677 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
680 e6bcace5 2021-06-22 stsp return NULL;
683 e6bcace5 2021-06-22 stsp static const struct got_error *
684 e6bcace5 2021-06-22 stsp findtwixt(struct got_object_id ***res, int *nres,
685 e6bcace5 2021-06-22 stsp struct got_object_id **head, int nhead,
686 e6bcace5 2021-06-22 stsp struct got_object_id **tail, int ntail,
687 e6bcace5 2021-06-22 stsp struct got_repository *repo,
688 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
690 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
691 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
692 e6bcace5 2021-06-22 stsp struct got_object_idset *keep, *drop;
693 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
694 05118f5a 2021-06-22 stsp int i, ncolor, nkeep, obj_type;
696 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
697 e6bcace5 2021-06-22 stsp *res = NULL;
700 e6bcace5 2021-06-22 stsp keep = got_object_idset_alloc();
701 e6bcace5 2021-06-22 stsp if (keep == NULL)
702 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
704 e6bcace5 2021-06-22 stsp drop = got_object_idset_alloc();
705 e6bcace5 2021-06-22 stsp if (drop == NULL) {
706 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_idset_alloc");
710 05118f5a 2021-06-22 stsp for (i = 0; i < nhead; i++) {
711 05118f5a 2021-06-22 stsp struct got_object_id *id = head[i];
712 05118f5a 2021-06-22 stsp if (id == NULL)
714 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
716 05118f5a 2021-06-22 stsp return err;
717 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
719 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
723 05118f5a 2021-06-22 stsp for (i = 0; i < ntail; i++) {
724 05118f5a 2021-06-22 stsp struct got_object_id *id = tail[i];
725 05118f5a 2021-06-22 stsp if (id == NULL)
727 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
729 05118f5a 2021-06-22 stsp return err;
730 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
732 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_DROP, repo);
737 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
738 e6bcace5 2021-06-22 stsp int qcolor;
739 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
740 e6bcace5 2021-06-22 stsp qcolor = *((int *)qid->data);
742 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id))
743 e6bcace5 2021-06-22 stsp ncolor = COLOR_DROP;
744 e6bcace5 2021-06-22 stsp else if (got_object_idset_contains(keep, qid->id))
745 e6bcace5 2021-06-22 stsp ncolor = COLOR_KEEP;
747 e6bcace5 2021-06-22 stsp ncolor = COLOR_BLANK;
749 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
750 e6bcace5 2021-06-22 stsp qcolor == COLOR_KEEP)) {
751 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
752 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
756 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
757 e6bcace5 2021-06-22 stsp err = drop_commit(keep, drop, qid->id, repo,
758 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
761 e6bcace5 2021-06-22 stsp } else if (ncolor == COLOR_BLANK) {
762 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
763 e6bcace5 2021-06-22 stsp struct got_object_id *id;
764 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
765 e6bcace5 2021-06-22 stsp struct got_object_qid *pid;
767 e6bcace5 2021-06-22 stsp id = got_object_id_dup(qid->id);
768 e6bcace5 2021-06-22 stsp if (id == NULL) {
769 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
772 e6bcace5 2021-06-22 stsp if (qcolor == COLOR_KEEP)
773 07165b17 2021-07-01 stsp err = got_object_idset_add(keep, id,
774 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
776 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, id,
777 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
783 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
788 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
789 e6bcace5 2021-06-22 stsp if (parents) {
790 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parents, entry) {
791 e6bcace5 2021-06-22 stsp err = queue_commit_id(&ids, pid->id,
792 e6bcace5 2021-06-22 stsp qcolor, repo);
799 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
800 e6bcace5 2021-06-22 stsp commit = NULL;
802 e6bcace5 2021-06-22 stsp /* should not happen */
803 e6bcace5 2021-06-22 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
804 e6bcace5 2021-06-22 stsp "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
808 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
809 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
812 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
813 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
814 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
815 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
816 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
817 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
820 e6bcace5 2021-06-22 stsp arg.idx = 0;
821 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
823 e6bcace5 2021-06-22 stsp free(arg.array);
826 e6bcace5 2021-06-22 stsp *res = arg.array;
827 e6bcace5 2021-06-22 stsp *nres = nkeep;
830 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
831 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
832 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
833 e6bcace5 2021-06-22 stsp return err;
836 e6bcace5 2021-06-22 stsp static const struct got_error *
837 e6bcace5 2021-06-22 stsp read_meta(struct got_pack_meta ***meta, int *nmeta,
838 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
839 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
840 05118f5a 2021-06-22 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
841 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
843 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
844 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
845 e6bcace5 2021-06-22 stsp struct got_object_idset *idset;
846 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
847 e6bcace5 2021-06-22 stsp struct got_pack_metavec v;
849 e6bcace5 2021-06-22 stsp *meta = NULL;
850 e6bcace5 2021-06-22 stsp *nmeta = 0;
852 e6bcace5 2021-06-22 stsp idset = got_object_idset_alloc();
853 e6bcace5 2021-06-22 stsp if (idset == NULL)
854 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
856 e6bcace5 2021-06-22 stsp v.nmeta = 0;
857 e6bcace5 2021-06-22 stsp v.metasz = 64;
858 e6bcace5 2021-06-22 stsp v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
859 e6bcace5 2021-06-22 stsp if (v.meta == NULL) {
860 b7e0a384 2021-10-15 thomas err = got_error_from_errno("calloc");
864 e6bcace5 2021-06-22 stsp err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
865 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
866 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
869 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
870 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
871 05118f5a 2021-06-22 stsp if (id == NULL)
873 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
875 05118f5a 2021-06-22 stsp return err;
876 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
878 05118f5a 2021-06-22 stsp err = load_commit(NULL, idset, id, repo,
879 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
882 05118f5a 2021-06-22 stsp if (progress_cb) {
883 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
884 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
890 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
891 f4a2ff2d 2021-07-01 stsp struct got_object_id *id = theirs[i];
892 07165b17 2021-07-01 stsp int *cached_type;
893 05118f5a 2021-06-22 stsp if (id == NULL)
895 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
896 07165b17 2021-07-01 stsp if (cached_type == NULL) {
897 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
901 07165b17 2021-07-01 stsp obj_type = *cached_type;
902 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
904 05118f5a 2021-06-22 stsp err = load_tag(NULL, idset, id, repo,
905 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
908 05118f5a 2021-06-22 stsp if (progress_cb) {
909 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
910 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
916 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
917 eca70f98 2021-09-03 stsp err = load_commit(&v, idset, ids[i], repo,
918 eca70f98 2021-09-03 stsp loose_obj_only, cancel_cb, cancel_arg);
921 eca70f98 2021-09-03 stsp if (progress_cb) {
922 eca70f98 2021-09-03 stsp err = progress_cb(progress_arg, 0L, nours,
923 eca70f98 2021-09-03 stsp v.nmeta, 0, 0);
929 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
930 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
931 07165b17 2021-07-01 stsp int *cached_type;
932 05118f5a 2021-06-22 stsp if (id == NULL)
934 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
935 07165b17 2021-07-01 stsp if (cached_type == NULL) {
936 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
940 07165b17 2021-07-01 stsp obj_type = *cached_type;
941 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
943 05118f5a 2021-06-22 stsp err = load_tag(&v, idset, id, repo,
944 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
947 05118f5a 2021-06-22 stsp if (progress_cb) {
948 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
949 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
956 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
957 e6bcace5 2021-06-22 stsp free(ids[i]);
960 e6bcace5 2021-06-22 stsp got_object_idset_free(idset);
961 e6bcace5 2021-06-22 stsp if (err == NULL) {
962 e6bcace5 2021-06-22 stsp *meta = v.meta;
963 e6bcace5 2021-06-22 stsp *nmeta = v.nmeta;
965 e6bcace5 2021-06-22 stsp free(v.meta);
967 e6bcace5 2021-06-22 stsp return err;
970 e6bcace5 2021-06-22 stsp const struct got_error *
971 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
975 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
976 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
977 e6bcace5 2021-06-22 stsp if (n != len)
978 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
979 e6bcace5 2021-06-22 stsp return NULL;
982 e6bcace5 2021-06-22 stsp static void
983 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
985 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
986 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
987 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
988 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
992 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
994 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
996 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
997 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
998 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
999 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
1000 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
1001 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
1002 e6bcace5 2021-06-22 stsp if (ahd != bhd)
1003 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
1004 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
1005 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
1006 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
1009 e6bcace5 2021-06-22 stsp static const struct got_error *
1010 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
1014 e6bcace5 2021-06-22 stsp *hdrlen = 0;
1016 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
1017 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
1018 e6bcace5 2021-06-22 stsp len >>= 4;
1019 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
1020 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1021 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1022 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1023 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1024 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1027 e6bcace5 2021-06-22 stsp *hdrlen = i;
1028 e6bcace5 2021-06-22 stsp return NULL;
1031 e6bcace5 2021-06-22 stsp static const struct got_error *
1032 b79280dd 2021-10-15 thomas encode_delta(struct got_pack_meta *m, struct got_raw_object *o,
1033 b79280dd 2021-10-15 thomas struct got_delta_instruction *deltas, int ndeltas,
1034 e4d8ab47 2021-10-15 thomas off_t base_size, FILE *f)
1036 e6bcace5 2021-06-22 stsp unsigned char buf[16], *bp;
1037 e4d8ab47 2021-10-15 thomas int i, j;
1039 e4d8ab47 2021-10-15 thomas size_t w;
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 e4d8ab47 2021-10-15 thomas w = fwrite(buf, 1, i, f);
1051 e4d8ab47 2021-10-15 thomas if (w != i)
1052 e4d8ab47 2021-10-15 thomas 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 e4d8ab47 2021-10-15 thomas w = fwrite(buf, 1, i, f);
1063 e4d8ab47 2021-10-15 thomas if (w != i)
1064 e4d8ab47 2021-10-15 thomas return got_ferror(f, GOT_ERR_IO);
1066 b79280dd 2021-10-15 thomas for (j = 0; j < ndeltas; j++) {
1067 b79280dd 2021-10-15 thomas d = &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 e4d8ab47 2021-10-15 thomas w = fwrite(buf, 1, bp - buf, f);
1091 e4d8ab47 2021-10-15 thomas if (w != bp - buf)
1092 e4d8ab47 2021-10-15 thomas 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 e4d8ab47 2021-10-15 thomas w = fwrite(buf, 1, 1, f);
1102 e4d8ab47 2021-10-15 thomas if (w != 1)
1103 e4d8ab47 2021-10-15 thomas 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 e4d8ab47 2021-10-15 thomas w = fwrite(content, 1, buf[0], f);
1108 e4d8ab47 2021-10-15 thomas if (w != buf[0])
1109 e4d8ab47 2021-10-15 thomas 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 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 31f4c1e6 2021-10-15 thomas err = got_object_raw_open(&raw, &outfd, repo, &m->id);
1182 e6bcace5 2021-06-22 stsp goto done;
1183 b79280dd 2021-10-15 thomas if (m->delta_len == 0) {
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;