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;
59 e6bcace5 2021-06-22 stsp time_t mtime;
61 e6bcace5 2021-06-22 stsp /* The best delta we picked */
62 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
63 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
64 e6bcace5 2021-06-22 stsp struct got_delta_instruction *deltas;
65 e6bcace5 2021-06-22 stsp int ndeltas;
68 e6bcace5 2021-06-22 stsp /* Only used for delta window */
69 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
71 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
75 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
76 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
81 e6bcace5 2021-06-22 stsp static const struct got_error *
82 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
83 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
85 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
86 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
88 e6bcace5 2021-06-22 stsp *new = NULL;
90 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
91 e6bcace5 2021-06-22 stsp if (m == NULL)
92 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
94 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
96 e6bcace5 2021-06-22 stsp m->path = strdup(path);
97 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
98 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
100 e6bcace5 2021-06-22 stsp return err;
103 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
104 e6bcace5 2021-06-22 stsp m->mtime = mtime;
106 e6bcace5 2021-06-22 stsp return NULL;
109 e6bcace5 2021-06-22 stsp static void
110 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
112 e6bcace5 2021-06-22 stsp if (meta == NULL)
114 e6bcace5 2021-06-22 stsp free(meta->deltas);
115 e6bcace5 2021-06-22 stsp meta->deltas = NULL;
116 e6bcace5 2021-06-22 stsp free(meta->path);
117 e6bcace5 2021-06-22 stsp meta->path = NULL;
120 e6bcace5 2021-06-22 stsp static void
121 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
125 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
126 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
127 e6bcace5 2021-06-22 stsp free(meta);
131 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
133 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
136 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
137 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
139 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
140 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
141 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
142 e6bcace5 2021-06-22 stsp if (cmp != 0)
143 e6bcace5 2021-06-22 stsp return cmp;
144 e6bcace5 2021-06-22 stsp if (a->mtime != b->mtime)
145 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
146 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
150 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
152 e6bcace5 2021-06-22 stsp int i, size = 32;
153 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
154 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
155 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
157 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
159 e6bcace5 2021-06-22 stsp return size;
163 e6bcace5 2021-06-22 stsp static const struct got_error *
164 05118f5a 2021-06-22 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
165 05118f5a 2021-06-22 stsp struct got_repository *repo,
166 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
167 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
169 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
170 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
171 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
172 e6bcace5 2021-06-22 stsp struct got_delta_instruction *deltas;
173 05118f5a 2021-06-22 stsp int i, j, size, ndeltas, best;
174 e6bcace5 2021-06-22 stsp const int max_base_candidates = 10;
176 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
177 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
178 e6bcace5 2021-06-22 stsp if (cancel_cb) {
179 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
183 05118f5a 2021-06-22 stsp if (progress_cb) {
184 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
188 e6bcace5 2021-06-22 stsp m = meta[i];
189 e6bcace5 2021-06-22 stsp m->deltas = NULL;
190 e6bcace5 2021-06-22 stsp m->ndeltas = 0;
192 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
193 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
196 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&raw, repo, &m->id, 8192);
200 e6bcace5 2021-06-22 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
201 e6bcace5 2021-06-22 stsp raw->size + raw->hdrlen);
205 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
206 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
207 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
208 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
209 e6bcace5 2021-06-22 stsp n->dtab = NULL;
212 e6bcace5 2021-06-22 stsp best = raw->size;
213 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
214 e6bcace5 2021-06-22 stsp if (cancel_cb) {
215 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
219 e6bcace5 2021-06-22 stsp base = meta[j];
220 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
221 e6bcace5 2021-06-22 stsp if (base->nchain >= 128 ||
222 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
225 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&base_raw, repo, &base->id,
229 e6bcace5 2021-06-22 stsp err = got_deltify(&deltas, &ndeltas,
230 e6bcace5 2021-06-22 stsp raw->f, raw->hdrlen, raw->size + raw->hdrlen,
231 e6bcace5 2021-06-22 stsp base->dtab, base_raw->f, base_raw->hdrlen,
232 e6bcace5 2021-06-22 stsp base_raw->size + base_raw->hdrlen);
233 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
234 e6bcace5 2021-06-22 stsp base_raw = NULL;
238 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
239 e6bcace5 2021-06-22 stsp if (size + 32 < best){
241 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
242 e6bcace5 2021-06-22 stsp * replace it.
244 e6bcace5 2021-06-22 stsp free(m->deltas);
245 e6bcace5 2021-06-22 stsp best = size;
246 e6bcace5 2021-06-22 stsp m->deltas = deltas;
247 e6bcace5 2021-06-22 stsp m->ndeltas = ndeltas;
248 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
249 e6bcace5 2021-06-22 stsp m->prev = base;
250 e6bcace5 2021-06-22 stsp m->head = base->head;
251 e6bcace5 2021-06-22 stsp if (m->head == NULL)
252 e6bcace5 2021-06-22 stsp m->head = base;
254 e6bcace5 2021-06-22 stsp free(deltas);
255 e6bcace5 2021-06-22 stsp deltas = NULL;
256 e6bcace5 2021-06-22 stsp ndeltas = 0;
260 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
261 e6bcace5 2021-06-22 stsp raw = NULL;
264 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
265 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
266 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
269 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
270 e6bcace5 2021-06-22 stsp if (base_raw)
271 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
272 e6bcace5 2021-06-22 stsp return err;
275 e6bcace5 2021-06-22 stsp static const struct got_error *
276 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
277 05118f5a 2021-06-22 stsp struct got_repository *repo)
279 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
280 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
283 05118f5a 2021-06-22 stsp *found = 0;
285 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
286 05118f5a 2021-06-22 stsp if (err == NULL)
287 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
288 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
289 05118f5a 2021-06-22 stsp err = NULL;
290 05118f5a 2021-06-22 stsp return err;
293 07165b17 2021-07-01 stsp static const int obj_types[] = {
294 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_ANY,
295 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_COMMIT,
296 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TREE,
297 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_BLOB,
298 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TAG,
299 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_OFFSET_DELTA,
300 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_REF_DELTA
303 05118f5a 2021-06-22 stsp static const struct got_error *
304 e6bcace5 2021-06-22 stsp add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
305 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
306 05118f5a 2021-06-22 stsp time_t mtime, int loose_obj_only, struct got_repository *repo)
308 e6bcace5 2021-06-22 stsp const struct got_error *err;
309 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
311 05118f5a 2021-06-22 stsp if (loose_obj_only) {
312 05118f5a 2021-06-22 stsp int is_packed;
313 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
315 05118f5a 2021-06-22 stsp return err;
316 05118f5a 2021-06-22 stsp if (is_packed)
317 05118f5a 2021-06-22 stsp return NULL;
320 07165b17 2021-07-01 stsp err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
322 e6bcace5 2021-06-22 stsp return err;
324 e6bcace5 2021-06-22 stsp if (v == NULL)
325 e6bcace5 2021-06-22 stsp return NULL;
327 e6bcace5 2021-06-22 stsp err = alloc_meta(&m, id, path, obj_type, mtime);
331 e6bcace5 2021-06-22 stsp if (v->nmeta == v->metasz){
332 e6bcace5 2021-06-22 stsp size_t newsize = 2 * v->metasz;
333 e6bcace5 2021-06-22 stsp struct got_pack_meta **new;
334 e6bcace5 2021-06-22 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
335 e6bcace5 2021-06-22 stsp if (new == NULL) {
336 e6bcace5 2021-06-22 stsp err = got_error_from_errno("reallocarray");
339 e6bcace5 2021-06-22 stsp v->meta = new;
340 e6bcace5 2021-06-22 stsp v->metasz = newsize;
344 e6bcace5 2021-06-22 stsp clear_meta(m);
347 e6bcace5 2021-06-22 stsp v->meta[v->nmeta++] = m;
349 e6bcace5 2021-06-22 stsp return err;
352 e6bcace5 2021-06-22 stsp static const struct got_error *
353 e6bcace5 2021-06-22 stsp load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
354 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
355 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
356 05118f5a 2021-06-22 stsp int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
358 e6bcace5 2021-06-22 stsp const struct got_error *err;
359 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
360 e6bcace5 2021-06-22 stsp char *p = NULL;
363 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
365 e6bcace5 2021-06-22 stsp return err;
367 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
368 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
369 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
370 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
372 e6bcace5 2021-06-22 stsp if (cancel_cb) {
373 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
378 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
379 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
382 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
383 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
384 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
388 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
389 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
390 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
393 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
394 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
395 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
396 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
404 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
406 e6bcace5 2021-06-22 stsp return err;
409 e6bcace5 2021-06-22 stsp static const struct got_error *
410 e6bcace5 2021-06-22 stsp load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
411 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
412 05118f5a 2021-06-22 stsp int loose_obj_only, struct got_repository *repo,
413 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
415 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
416 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
417 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
419 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
420 e6bcace5 2021-06-22 stsp return NULL;
422 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
424 e6bcace5 2021-06-22 stsp return err;
426 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
427 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
429 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
430 e6bcace5 2021-06-22 stsp if (cancel_cb) {
431 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
436 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
437 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
439 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
440 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
444 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
445 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
447 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
451 e6bcace5 2021-06-22 stsp err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
452 05118f5a 2021-06-22 stsp mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
453 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
458 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
459 e6bcace5 2021-06-22 stsp return err;
462 e6bcace5 2021-06-22 stsp static const struct got_error *
463 e6bcace5 2021-06-22 stsp load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
464 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
465 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
467 e6bcace5 2021-06-22 stsp const struct got_error *err;
468 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
470 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
471 e6bcace5 2021-06-22 stsp return NULL;
473 05118f5a 2021-06-22 stsp if (loose_obj_only) {
474 05118f5a 2021-06-22 stsp int is_packed;
475 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
477 05118f5a 2021-06-22 stsp return err;
478 05118f5a 2021-06-22 stsp if (is_packed)
479 05118f5a 2021-06-22 stsp return NULL;
482 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
484 e6bcace5 2021-06-22 stsp return err;
486 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
487 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
488 05118f5a 2021-06-22 stsp loose_obj_only, repo);
492 e6bcace5 2021-06-22 stsp err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
493 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
494 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
496 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
497 e6bcace5 2021-06-22 stsp return err;
500 05118f5a 2021-06-22 stsp static const struct got_error *
501 05118f5a 2021-06-22 stsp load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
502 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
503 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
505 05118f5a 2021-06-22 stsp const struct got_error *err;
506 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
508 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
509 05118f5a 2021-06-22 stsp return NULL;
511 05118f5a 2021-06-22 stsp if (loose_obj_only) {
512 05118f5a 2021-06-22 stsp int is_packed;
513 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
515 05118f5a 2021-06-22 stsp return err;
516 05118f5a 2021-06-22 stsp if (is_packed)
517 05118f5a 2021-06-22 stsp return NULL;
520 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
522 05118f5a 2021-06-22 stsp return err;
524 05118f5a 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
525 05118f5a 2021-06-22 stsp got_object_tag_get_tagger_time(tag),
526 05118f5a 2021-06-22 stsp loose_obj_only, repo);
530 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
531 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
532 26960ff7 2021-09-14 stsp err = load_commit(v, idset,
533 05118f5a 2021-06-22 stsp got_object_tag_get_object_id(tag), repo,
534 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
536 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
537 05118f5a 2021-06-22 stsp err = load_tree(v, idset, got_object_tag_get_object_id(tag),
538 05118f5a 2021-06-22 stsp "", got_object_tag_get_tagger_time(tag),
539 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
546 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
547 05118f5a 2021-06-22 stsp return err;
550 e6bcace5 2021-06-22 stsp enum findtwixt_color {
551 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
552 e6bcace5 2021-06-22 stsp COLOR_DROP,
553 e6bcace5 2021-06-22 stsp COLOR_BLANK,
555 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
556 e6bcace5 2021-06-22 stsp COLOR_KEEP,
557 e6bcace5 2021-06-22 stsp COLOR_DROP,
558 e6bcace5 2021-06-22 stsp COLOR_BLANK
561 e6bcace5 2021-06-22 stsp static const struct got_error *
562 e6bcace5 2021-06-22 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
563 e6bcace5 2021-06-22 stsp int color, struct got_repository *repo)
565 e6bcace5 2021-06-22 stsp const struct got_error *err;
566 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
568 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
570 e6bcace5 2021-06-22 stsp return err;
572 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
573 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
574 e6bcace5 2021-06-22 stsp return NULL;
577 e6bcace5 2021-06-22 stsp static const struct got_error *
578 e6bcace5 2021-06-22 stsp drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
579 e6bcace5 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo,
580 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
582 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
583 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
584 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
585 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
586 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
588 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
590 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
592 e6bcace5 2021-06-22 stsp return err;
593 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&ids, qid, entry);
595 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
596 e6bcace5 2021-06-22 stsp if (cancel_cb) {
597 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
602 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
603 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
605 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id)) {
606 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
610 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, qid->id,
611 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
613 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
617 e6bcace5 2021-06-22 stsp if (!got_object_idset_contains(keep, qid->id)) {
618 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
622 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
623 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
627 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
628 e6bcace5 2021-06-22 stsp if (parents) {
629 e6bcace5 2021-06-22 stsp err = got_object_id_queue_copy(parents, &ids);
631 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
635 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
638 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
639 e6bcace5 2021-06-22 stsp return err;
642 e6bcace5 2021-06-22 stsp struct append_id_arg {
643 e6bcace5 2021-06-22 stsp struct got_object_id **array;
647 e6bcace5 2021-06-22 stsp static const struct got_error *
648 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
650 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
652 e6bcace5 2021-06-22 stsp a->array[a->idx] = got_object_id_dup(id);
653 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
654 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
657 e6bcace5 2021-06-22 stsp return NULL;
660 e6bcace5 2021-06-22 stsp static const struct got_error *
661 e6bcace5 2021-06-22 stsp findtwixt(struct got_object_id ***res, int *nres,
662 e6bcace5 2021-06-22 stsp struct got_object_id **head, int nhead,
663 e6bcace5 2021-06-22 stsp struct got_object_id **tail, int ntail,
664 e6bcace5 2021-06-22 stsp struct got_repository *repo,
665 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
667 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
668 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
669 e6bcace5 2021-06-22 stsp struct got_object_idset *keep, *drop;
670 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
671 05118f5a 2021-06-22 stsp int i, ncolor, nkeep, obj_type;
673 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
674 e6bcace5 2021-06-22 stsp *res = NULL;
677 e6bcace5 2021-06-22 stsp keep = got_object_idset_alloc();
678 e6bcace5 2021-06-22 stsp if (keep == NULL)
679 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
681 e6bcace5 2021-06-22 stsp drop = got_object_idset_alloc();
682 e6bcace5 2021-06-22 stsp if (drop == NULL) {
683 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_idset_alloc");
687 05118f5a 2021-06-22 stsp for (i = 0; i < nhead; i++) {
688 05118f5a 2021-06-22 stsp struct got_object_id *id = head[i];
689 05118f5a 2021-06-22 stsp if (id == NULL)
691 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
693 05118f5a 2021-06-22 stsp return err;
694 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
696 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
700 05118f5a 2021-06-22 stsp for (i = 0; i < ntail; i++) {
701 05118f5a 2021-06-22 stsp struct got_object_id *id = tail[i];
702 05118f5a 2021-06-22 stsp if (id == NULL)
704 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
706 05118f5a 2021-06-22 stsp return err;
707 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
709 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_DROP, repo);
714 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
715 e6bcace5 2021-06-22 stsp int qcolor;
716 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
717 e6bcace5 2021-06-22 stsp qcolor = *((int *)qid->data);
719 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id))
720 e6bcace5 2021-06-22 stsp ncolor = COLOR_DROP;
721 e6bcace5 2021-06-22 stsp else if (got_object_idset_contains(keep, qid->id))
722 e6bcace5 2021-06-22 stsp ncolor = COLOR_KEEP;
724 e6bcace5 2021-06-22 stsp ncolor = COLOR_BLANK;
726 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
727 e6bcace5 2021-06-22 stsp qcolor == COLOR_KEEP)) {
728 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
729 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
733 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
734 e6bcace5 2021-06-22 stsp err = drop_commit(keep, drop, qid->id, repo,
735 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
738 e6bcace5 2021-06-22 stsp } else if (ncolor == COLOR_BLANK) {
739 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
740 e6bcace5 2021-06-22 stsp struct got_object_id *id;
741 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
742 e6bcace5 2021-06-22 stsp struct got_object_qid *pid;
744 e6bcace5 2021-06-22 stsp id = got_object_id_dup(qid->id);
745 e6bcace5 2021-06-22 stsp if (id == NULL) {
746 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
749 e6bcace5 2021-06-22 stsp if (qcolor == COLOR_KEEP)
750 07165b17 2021-07-01 stsp err = got_object_idset_add(keep, id,
751 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
753 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, id,
754 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
760 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
765 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
766 e6bcace5 2021-06-22 stsp if (parents) {
767 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parents, entry) {
768 e6bcace5 2021-06-22 stsp err = queue_commit_id(&ids, pid->id,
769 e6bcace5 2021-06-22 stsp qcolor, repo);
776 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
777 e6bcace5 2021-06-22 stsp commit = NULL;
779 e6bcace5 2021-06-22 stsp /* should not happen */
780 e6bcace5 2021-06-22 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
781 e6bcace5 2021-06-22 stsp "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
785 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
786 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
789 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
790 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
791 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
792 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
793 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
794 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
797 e6bcace5 2021-06-22 stsp arg.idx = 0;
798 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
800 e6bcace5 2021-06-22 stsp free(arg.array);
803 e6bcace5 2021-06-22 stsp *res = arg.array;
804 e6bcace5 2021-06-22 stsp *nres = nkeep;
807 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
808 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
809 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
810 e6bcace5 2021-06-22 stsp return err;
813 e6bcace5 2021-06-22 stsp static const struct got_error *
814 e6bcace5 2021-06-22 stsp read_meta(struct got_pack_meta ***meta, int *nmeta,
815 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
816 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
817 05118f5a 2021-06-22 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
818 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
820 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
821 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
822 e6bcace5 2021-06-22 stsp struct got_object_idset *idset;
823 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
824 e6bcace5 2021-06-22 stsp struct got_pack_metavec v;
826 e6bcace5 2021-06-22 stsp *meta = NULL;
827 e6bcace5 2021-06-22 stsp *nmeta = 0;
829 e6bcace5 2021-06-22 stsp idset = got_object_idset_alloc();
830 e6bcace5 2021-06-22 stsp if (idset == NULL)
831 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
833 e6bcace5 2021-06-22 stsp v.nmeta = 0;
834 e6bcace5 2021-06-22 stsp v.metasz = 64;
835 e6bcace5 2021-06-22 stsp v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
836 e6bcace5 2021-06-22 stsp if (v.meta == NULL) {
837 1d19226a 2021-10-13 stsp err = got_error_from_errno("calloc");
841 e6bcace5 2021-06-22 stsp err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
842 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
843 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
846 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
847 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
848 05118f5a 2021-06-22 stsp if (id == NULL)
850 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
852 05118f5a 2021-06-22 stsp return err;
853 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
855 05118f5a 2021-06-22 stsp err = load_commit(NULL, idset, id, repo,
856 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
859 05118f5a 2021-06-22 stsp if (progress_cb) {
860 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
861 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
867 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
868 f4a2ff2d 2021-07-01 stsp struct got_object_id *id = theirs[i];
869 07165b17 2021-07-01 stsp int *cached_type;
870 05118f5a 2021-06-22 stsp if (id == NULL)
872 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
873 07165b17 2021-07-01 stsp if (cached_type == NULL) {
874 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
878 07165b17 2021-07-01 stsp obj_type = *cached_type;
879 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
881 05118f5a 2021-06-22 stsp err = load_tag(NULL, idset, id, repo,
882 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
885 05118f5a 2021-06-22 stsp if (progress_cb) {
886 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
887 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
893 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
894 eca70f98 2021-09-03 stsp err = load_commit(&v, idset, ids[i], repo,
895 eca70f98 2021-09-03 stsp loose_obj_only, cancel_cb, cancel_arg);
898 eca70f98 2021-09-03 stsp if (progress_cb) {
899 eca70f98 2021-09-03 stsp err = progress_cb(progress_arg, 0L, nours,
900 eca70f98 2021-09-03 stsp v.nmeta, 0, 0);
906 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
907 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
908 07165b17 2021-07-01 stsp int *cached_type;
909 05118f5a 2021-06-22 stsp if (id == NULL)
911 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
912 07165b17 2021-07-01 stsp if (cached_type == NULL) {
913 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
917 07165b17 2021-07-01 stsp obj_type = *cached_type;
918 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
920 05118f5a 2021-06-22 stsp err = load_tag(&v, idset, id, repo,
921 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
924 05118f5a 2021-06-22 stsp if (progress_cb) {
925 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
926 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
933 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
934 e6bcace5 2021-06-22 stsp free(ids[i]);
937 e6bcace5 2021-06-22 stsp got_object_idset_free(idset);
938 e6bcace5 2021-06-22 stsp if (err == NULL) {
939 e6bcace5 2021-06-22 stsp *meta = v.meta;
940 e6bcace5 2021-06-22 stsp *nmeta = v.nmeta;
942 e6bcace5 2021-06-22 stsp free(v.meta);
944 e6bcace5 2021-06-22 stsp return err;
947 e6bcace5 2021-06-22 stsp const struct got_error *
948 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
952 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
953 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
954 e6bcace5 2021-06-22 stsp if (n != len)
955 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
956 e6bcace5 2021-06-22 stsp return NULL;
959 e6bcace5 2021-06-22 stsp static void
960 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
962 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
963 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
964 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
965 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
969 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
971 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
973 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
974 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
975 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
976 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
977 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
978 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
979 e6bcace5 2021-06-22 stsp if (ahd != bhd)
980 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
981 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
982 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
983 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
986 e6bcace5 2021-06-22 stsp static const struct got_error *
987 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
991 e6bcace5 2021-06-22 stsp *hdrlen = 0;
993 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
994 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
996 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
997 e6bcace5 2021-06-22 stsp if (i >= bufsize)
998 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
999 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1000 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1001 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1004 e6bcace5 2021-06-22 stsp *hdrlen = i;
1005 e6bcace5 2021-06-22 stsp return NULL;
1008 e6bcace5 2021-06-22 stsp static const struct got_error *
1009 08347b73 2021-10-14 stsp encodedelta(struct got_pack_meta *m, struct got_raw_object *o,
1010 08347b73 2021-10-14 stsp off_t base_size, FILE *f)
1012 e6bcace5 2021-06-22 stsp unsigned char buf[16], *bp;
1016 e6bcace5 2021-06-22 stsp struct got_delta_instruction *d;
1018 e6bcace5 2021-06-22 stsp /* base object size */
1019 e6bcace5 2021-06-22 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1020 e6bcace5 2021-06-22 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
1021 e6bcace5 2021-06-22 stsp for (i = 1; n > 0; i++) {
1022 e6bcace5 2021-06-22 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1023 e6bcace5 2021-06-22 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1024 e6bcace5 2021-06-22 stsp n >>= GOT_DELTA_SIZE_SHIFT;
1026 08347b73 2021-10-14 stsp w = fwrite(buf, 1, i, f);
1027 08347b73 2021-10-14 stsp if (w != i)
1028 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1030 e6bcace5 2021-06-22 stsp /* target object size */
1031 e6bcace5 2021-06-22 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1032 e6bcace5 2021-06-22 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
1033 e6bcace5 2021-06-22 stsp for (i = 1; n > 0; i++) {
1034 e6bcace5 2021-06-22 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1035 e6bcace5 2021-06-22 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1036 e6bcace5 2021-06-22 stsp n >>= GOT_DELTA_SIZE_SHIFT;
1038 08347b73 2021-10-14 stsp w = fwrite(buf, 1, i, f);
1039 08347b73 2021-10-14 stsp if (w != i)
1040 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1042 e6bcace5 2021-06-22 stsp for (j = 0; j < m->ndeltas; j++) {
1043 e6bcace5 2021-06-22 stsp d = &m->deltas[j];
1044 e6bcace5 2021-06-22 stsp if (d->copy) {
1045 e6bcace5 2021-06-22 stsp n = d->offset;
1046 e6bcace5 2021-06-22 stsp bp = &buf[1];
1047 e6bcace5 2021-06-22 stsp buf[0] = GOT_DELTA_BASE_COPY;
1048 e6bcace5 2021-06-22 stsp for (i = 0; i < 4; i++) {
1049 e6bcace5 2021-06-22 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1050 e6bcace5 2021-06-22 stsp buf[0] |= 1 << i;
1051 e6bcace5 2021-06-22 stsp *bp++ = n & 0xff;
1053 e6bcace5 2021-06-22 stsp if (n == 0)
1057 e6bcace5 2021-06-22 stsp n = d->len;
1058 e6bcace5 2021-06-22 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1059 e6bcace5 2021-06-22 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1060 e6bcace5 2021-06-22 stsp for (i = 0; i < 3 && n > 0; i++) {
1061 e6bcace5 2021-06-22 stsp buf[0] |= 1 << (i + 4);
1062 e6bcace5 2021-06-22 stsp *bp++ = n & 0xff;
1066 08347b73 2021-10-14 stsp w = fwrite(buf, 1, bp - buf, f);
1067 08347b73 2021-10-14 stsp if (w != bp - buf)
1068 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1070 e6bcace5 2021-06-22 stsp char content[128];
1072 e6bcace5 2021-06-22 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1073 e6bcace5 2021-06-22 stsp return got_error_from_errno("fseeko");
1075 e6bcace5 2021-06-22 stsp while (n != d->len) {
1076 e6bcace5 2021-06-22 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
1077 08347b73 2021-10-14 stsp w = fwrite(buf, 1, 1, f);
1078 08347b73 2021-10-14 stsp if (w != 1)
1079 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1080 e6bcace5 2021-06-22 stsp r = fread(content, 1, buf[0], o->f);
1081 e6bcace5 2021-06-22 stsp if (r != buf[0])
1082 e6bcace5 2021-06-22 stsp return got_ferror(o->f, GOT_ERR_IO);
1083 08347b73 2021-10-14 stsp w = fwrite(content, 1, buf[0], f);
1084 08347b73 2021-10-14 stsp if (w != buf[0])
1085 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1086 e6bcace5 2021-06-22 stsp n += buf[0];
1091 e6bcace5 2021-06-22 stsp return NULL;
1094 e6bcace5 2021-06-22 stsp static int
1095 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1098 e6bcace5 2021-06-22 stsp char rbuf[8];
1100 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1101 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1102 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1103 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1107 e6bcace5 2021-06-22 stsp while (i > 0)
1108 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1112 e6bcace5 2021-06-22 stsp static const struct got_error *
1113 e6bcace5 2021-06-22 stsp genpack(uint8_t *pack_sha1, FILE *packfile,
1114 05118f5a 2021-06-22 stsp struct got_pack_meta **meta, int nmeta, int nours,
1115 05118f5a 2021-06-22 stsp int use_offset_deltas, struct got_repository *repo,
1116 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1117 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1119 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1120 08347b73 2021-10-14 stsp int i, nh;
1122 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1123 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1124 08347b73 2021-10-14 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
1125 08347b73 2021-10-14 stsp FILE *delta_file = NULL;
1126 08347b73 2021-10-14 stsp char buf[32];
1127 e6bcace5 2021-06-22 stsp size_t outlen, n;
1128 e6bcace5 2021-06-22 stsp struct got_deflate_checksum csum;
1129 05118f5a 2021-06-22 stsp off_t packfile_size = 0;
1131 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1132 e6bcace5 2021-06-22 stsp csum.output_sha1 = &ctx;
1133 e6bcace5 2021-06-22 stsp csum.output_crc = NULL;
1135 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1137 e6bcace5 2021-06-22 stsp return err;
1138 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1139 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1141 e6bcace5 2021-06-22 stsp goto done;
1142 e6bcace5 2021-06-22 stsp putbe32(buf, nmeta);
1143 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1145 e6bcace5 2021-06-22 stsp goto done;
1146 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1147 05118f5a 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
1148 05118f5a 2021-06-22 stsp if (progress_cb) {
1149 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1150 05118f5a 2021-06-22 stsp nmeta, nmeta, i);
1152 05118f5a 2021-06-22 stsp goto done;
1154 e6bcace5 2021-06-22 stsp m = meta[i];
1155 e6bcace5 2021-06-22 stsp m->off = ftello(packfile);
1156 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&raw, repo, &m->id, 8192);
1158 e6bcace5 2021-06-22 stsp goto done;
1159 e6bcace5 2021-06-22 stsp if (m->deltas == NULL) {
1160 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1161 e6bcace5 2021-06-22 stsp m->obj_type, raw->size);
1163 e6bcace5 2021-06-22 stsp goto done;
1164 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1166 e6bcace5 2021-06-22 stsp goto done;
1167 05118f5a 2021-06-22 stsp packfile_size += nh;
1168 e6bcace5 2021-06-22 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1169 e6bcace5 2021-06-22 stsp err = got_error_from_errno("fseeko");
1170 e6bcace5 2021-06-22 stsp goto done;
1172 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, raw->f, packfile,
1175 e6bcace5 2021-06-22 stsp goto done;
1176 05118f5a 2021-06-22 stsp packfile_size += outlen;
1178 08347b73 2021-10-14 stsp if (delta_file == NULL) {
1179 08347b73 2021-10-14 stsp delta_file = got_opentemp();
1180 08347b73 2021-10-14 stsp if (delta_file == NULL) {
1181 08347b73 2021-10-14 stsp err = got_error_from_errno(
1182 08347b73 2021-10-14 stsp "got_opentemp");
1183 08347b73 2021-10-14 stsp goto done;
1186 08347b73 2021-10-14 stsp if (ftruncate(fileno(delta_file), 0L) == -1) {
1187 08347b73 2021-10-14 stsp err = got_error_from_errno("ftruncate");
1188 08347b73 2021-10-14 stsp goto done;
1190 08347b73 2021-10-14 stsp if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1191 08347b73 2021-10-14 stsp err = got_error_from_errno("fseeko");
1192 08347b73 2021-10-14 stsp goto done;
1194 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&base_raw, repo,
1195 e6bcace5 2021-06-22 stsp &m->prev->id, 8192);
1197 e6bcace5 2021-06-22 stsp goto done;
1198 08347b73 2021-10-14 stsp err = encodedelta(m, raw, base_raw->size, delta_file);
1200 e6bcace5 2021-06-22 stsp goto done;
1201 08347b73 2021-10-14 stsp nd = ftello(delta_file);
1202 08347b73 2021-10-14 stsp if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1203 08347b73 2021-10-14 stsp err = got_error_from_errno("fseeko");
1204 08347b73 2021-10-14 stsp goto done;
1206 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
1207 08347b73 2021-10-14 stsp base_raw = NULL;
1208 e6bcace5 2021-06-22 stsp if (use_offset_deltas && m->prev->off != 0) {
1209 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1210 e6bcace5 2021-06-22 stsp GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1212 e6bcace5 2021-06-22 stsp goto done;
1213 e6bcace5 2021-06-22 stsp nh += packoff(buf + nh,
1214 e6bcace5 2021-06-22 stsp m->off - m->prev->off);
1215 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1217 e6bcace5 2021-06-22 stsp goto done;
1218 05118f5a 2021-06-22 stsp packfile_size += nh;
1220 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1221 e6bcace5 2021-06-22 stsp GOT_OBJ_TYPE_REF_DELTA, nd);
1222 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1224 e6bcace5 2021-06-22 stsp goto done;
1225 05118f5a 2021-06-22 stsp packfile_size += nh;
1226 e6bcace5 2021-06-22 stsp err = hwrite(packfile, m->prev->id.sha1,
1227 e6bcace5 2021-06-22 stsp sizeof(m->prev->id.sha1), &ctx);
1228 05118f5a 2021-06-22 stsp packfile_size += sizeof(m->prev->id.sha1);
1230 e6bcace5 2021-06-22 stsp goto done;
1232 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, delta_file,
1233 e6bcace5 2021-06-22 stsp packfile, &csum);
1235 e6bcace5 2021-06-22 stsp goto done;
1236 05118f5a 2021-06-22 stsp packfile_size += outlen;
1238 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
1239 e6bcace5 2021-06-22 stsp raw = NULL;
1241 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1242 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1243 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1244 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1245 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1246 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1247 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1248 05118f5a 2021-06-22 stsp nmeta, nmeta, nmeta);
1250 05118f5a 2021-06-22 stsp goto done;
1252 08347b73 2021-10-14 stsp if (delta_file && fclose(delta_file) == EOF && err == NULL)
1253 08347b73 2021-10-14 stsp err = got_error_from_errno("fclose");
1255 08347b73 2021-10-14 stsp got_object_raw_close(raw);
1256 08347b73 2021-10-14 stsp if (base_raw)
1257 08347b73 2021-10-14 stsp got_object_raw_close(base_raw);
1258 e6bcace5 2021-06-22 stsp return err;
1261 e6bcace5 2021-06-22 stsp const struct got_error *
1262 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1263 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1264 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1265 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1266 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1267 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1269 e6bcace5 2021-06-22 stsp const struct got_error *err;
1270 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
1271 e6bcace5 2021-06-22 stsp int nmeta;
1273 e6bcace5 2021-06-22 stsp err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1274 05118f5a 2021-06-22 stsp loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1276 e6bcace5 2021-06-22 stsp return err;
1278 f8a36e22 2021-08-26 stsp if (nmeta == 0 && !allow_empty) {
1279 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1280 05118f5a 2021-06-22 stsp goto done;
1282 f8a36e22 2021-08-26 stsp if (nmeta > 0) {
1283 f8a36e22 2021-08-26 stsp err = pick_deltas(meta, nmeta, nours, repo,
1284 f8a36e22 2021-08-26 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1286 f8a36e22 2021-08-26 stsp goto done;
1289 05118f5a 2021-06-22 stsp err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1290 05118f5a 2021-06-22 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1292 e6bcace5 2021-06-22 stsp goto done;
1294 e6bcace5 2021-06-22 stsp free_nmeta(meta, nmeta);
1295 e6bcace5 2021-06-22 stsp return err;