2 e6bcace5 2021-06-22 stsp * Copyright (c) 2020 Ori Bernstein
3 e6bcace5 2021-06-22 stsp * Copyright (c) 2021 Stefan Sperling <stsp@openbsd.org>
5 e6bcace5 2021-06-22 stsp * Permission to use, copy, modify, and distribute this software for any
6 e6bcace5 2021-06-22 stsp * purpose with or without fee is hereby granted, provided that the above
7 e6bcace5 2021-06-22 stsp * copyright notice and this permission notice appear in all copies.
9 e6bcace5 2021-06-22 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 e6bcace5 2021-06-22 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 e6bcace5 2021-06-22 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 e6bcace5 2021-06-22 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 e6bcace5 2021-06-22 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 e6bcace5 2021-06-22 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 e6bcace5 2021-06-22 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 08736cf9 2021-06-23 stsp #include <sys/types.h>
19 e6bcace5 2021-06-22 stsp #include <sys/queue.h>
20 f8b19efd 2021-10-13 stsp #include <sys/tree.h>
21 08736cf9 2021-06-23 stsp #include <sys/uio.h>
22 e6bcace5 2021-06-22 stsp #include <sys/stat.h>
24 08736cf9 2021-06-23 stsp #include <stdint.h>
25 05118f5a 2021-06-22 stsp #include <imsg.h>
26 e6bcace5 2021-06-22 stsp #include <stdio.h>
27 e6bcace5 2021-06-22 stsp #include <stdlib.h>
28 e6bcace5 2021-06-22 stsp #include <string.h>
29 e6bcace5 2021-06-22 stsp #include <sha1.h>
30 e6bcace5 2021-06-22 stsp #include <limits.h>
31 e6bcace5 2021-06-22 stsp #include <zlib.h>
33 e6bcace5 2021-06-22 stsp #include "got_error.h"
34 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
35 e6bcace5 2021-06-22 stsp #include "got_object.h"
36 f8b19efd 2021-10-13 stsp #include "got_path.h"
37 05118f5a 2021-06-22 stsp #include "got_reference.h"
38 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
39 08347b73 2021-10-14 stsp #include "got_opentemp.h"
41 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
42 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
43 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
44 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
45 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
46 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
47 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
48 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
49 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
52 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
55 e6bcace5 2021-06-22 stsp struct got_pack_meta {
56 e6bcace5 2021-06-22 stsp struct got_object_id id;
58 e6bcace5 2021-06-22 stsp int obj_type;
60 e6bcace5 2021-06-22 stsp time_t mtime;
62 e6bcace5 2021-06-22 stsp /* The best delta we picked */
63 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
64 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
65 e6bcace5 2021-06-22 stsp struct got_delta_instruction *deltas;
66 e6bcace5 2021-06-22 stsp int ndeltas;
69 e6bcace5 2021-06-22 stsp /* Only used for delta window */
70 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
72 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
76 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
77 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
82 e6bcace5 2021-06-22 stsp static const struct got_error *
83 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
84 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
86 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
87 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
89 e6bcace5 2021-06-22 stsp *new = NULL;
91 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
92 e6bcace5 2021-06-22 stsp if (m == NULL)
93 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
95 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
97 e6bcace5 2021-06-22 stsp m->path = strdup(path);
98 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
99 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
101 e6bcace5 2021-06-22 stsp return err;
104 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
105 e6bcace5 2021-06-22 stsp m->mtime = mtime;
107 e6bcace5 2021-06-22 stsp return NULL;
110 e6bcace5 2021-06-22 stsp static void
111 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
113 e6bcace5 2021-06-22 stsp if (meta == NULL)
115 e6bcace5 2021-06-22 stsp free(meta->deltas);
116 e6bcace5 2021-06-22 stsp meta->deltas = NULL;
117 e6bcace5 2021-06-22 stsp free(meta->path);
118 e6bcace5 2021-06-22 stsp meta->path = NULL;
121 e6bcace5 2021-06-22 stsp static void
122 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
126 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
127 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
128 e6bcace5 2021-06-22 stsp free(meta);
132 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
134 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
137 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
138 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
140 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
141 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
142 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
143 e6bcace5 2021-06-22 stsp if (cmp != 0)
144 e6bcace5 2021-06-22 stsp return cmp;
145 e6bcace5 2021-06-22 stsp if (a->mtime != b->mtime)
146 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
147 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
151 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
153 e6bcace5 2021-06-22 stsp int i, size = 32;
154 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
155 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
156 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
158 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
160 e6bcace5 2021-06-22 stsp return size;
164 e6bcace5 2021-06-22 stsp static const struct got_error *
165 05118f5a 2021-06-22 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
166 05118f5a 2021-06-22 stsp struct got_repository *repo,
167 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
168 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
170 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
171 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
172 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
173 e6bcace5 2021-06-22 stsp struct got_delta_instruction *deltas;
174 05118f5a 2021-06-22 stsp int i, j, size, ndeltas, best;
175 e6bcace5 2021-06-22 stsp const int max_base_candidates = 10;
177 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
178 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
179 e6bcace5 2021-06-22 stsp if (cancel_cb) {
180 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
184 05118f5a 2021-06-22 stsp if (progress_cb) {
185 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
189 e6bcace5 2021-06-22 stsp m = meta[i];
190 e6bcace5 2021-06-22 stsp m->deltas = NULL;
191 e6bcace5 2021-06-22 stsp m->ndeltas = 0;
193 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
194 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
197 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&raw, repo, &m->id, 8192);
200 600b755e 2021-10-14 stsp m->size = raw->size;
202 e6bcace5 2021-06-22 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
203 e6bcace5 2021-06-22 stsp raw->size + raw->hdrlen);
207 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
208 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
209 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
210 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
211 e6bcace5 2021-06-22 stsp n->dtab = NULL;
214 e6bcace5 2021-06-22 stsp best = raw->size;
215 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
216 e6bcace5 2021-06-22 stsp if (cancel_cb) {
217 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
221 e6bcace5 2021-06-22 stsp base = meta[j];
222 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
223 e6bcace5 2021-06-22 stsp if (base->nchain >= 128 ||
224 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
227 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&base_raw, repo, &base->id,
231 e6bcace5 2021-06-22 stsp err = got_deltify(&deltas, &ndeltas,
232 e6bcace5 2021-06-22 stsp raw->f, raw->hdrlen, raw->size + raw->hdrlen,
233 e6bcace5 2021-06-22 stsp base->dtab, base_raw->f, base_raw->hdrlen,
234 e6bcace5 2021-06-22 stsp base_raw->size + base_raw->hdrlen);
235 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
236 e6bcace5 2021-06-22 stsp base_raw = NULL;
240 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
241 e6bcace5 2021-06-22 stsp if (size + 32 < best){
243 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
244 e6bcace5 2021-06-22 stsp * replace it.
246 e6bcace5 2021-06-22 stsp free(m->deltas);
247 e6bcace5 2021-06-22 stsp best = size;
248 e6bcace5 2021-06-22 stsp m->deltas = deltas;
249 e6bcace5 2021-06-22 stsp m->ndeltas = ndeltas;
250 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
251 e6bcace5 2021-06-22 stsp m->prev = base;
252 e6bcace5 2021-06-22 stsp m->head = base->head;
253 e6bcace5 2021-06-22 stsp if (m->head == NULL)
254 e6bcace5 2021-06-22 stsp m->head = base;
256 e6bcace5 2021-06-22 stsp free(deltas);
257 e6bcace5 2021-06-22 stsp deltas = NULL;
258 e6bcace5 2021-06-22 stsp ndeltas = 0;
262 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
263 e6bcace5 2021-06-22 stsp raw = NULL;
266 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
267 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
268 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
271 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
272 e6bcace5 2021-06-22 stsp if (base_raw)
273 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
274 e6bcace5 2021-06-22 stsp return err;
277 e6bcace5 2021-06-22 stsp static const struct got_error *
278 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
279 05118f5a 2021-06-22 stsp struct got_repository *repo)
281 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
282 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
285 05118f5a 2021-06-22 stsp *found = 0;
287 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
288 05118f5a 2021-06-22 stsp if (err == NULL)
289 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
290 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
291 05118f5a 2021-06-22 stsp err = NULL;
292 05118f5a 2021-06-22 stsp return err;
295 07165b17 2021-07-01 stsp static const int obj_types[] = {
296 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_ANY,
297 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_COMMIT,
298 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TREE,
299 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_BLOB,
300 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TAG,
301 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_OFFSET_DELTA,
302 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_REF_DELTA
305 05118f5a 2021-06-22 stsp static const struct got_error *
306 e6bcace5 2021-06-22 stsp add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
307 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
308 05118f5a 2021-06-22 stsp time_t mtime, int loose_obj_only, struct got_repository *repo)
310 e6bcace5 2021-06-22 stsp const struct got_error *err;
311 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
313 05118f5a 2021-06-22 stsp if (loose_obj_only) {
314 05118f5a 2021-06-22 stsp int is_packed;
315 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
317 05118f5a 2021-06-22 stsp return err;
318 05118f5a 2021-06-22 stsp if (is_packed)
319 05118f5a 2021-06-22 stsp return NULL;
322 07165b17 2021-07-01 stsp err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
324 e6bcace5 2021-06-22 stsp return err;
326 e6bcace5 2021-06-22 stsp if (v == NULL)
327 e6bcace5 2021-06-22 stsp return NULL;
329 e6bcace5 2021-06-22 stsp err = alloc_meta(&m, id, path, obj_type, mtime);
333 e6bcace5 2021-06-22 stsp if (v->nmeta == v->metasz){
334 e6bcace5 2021-06-22 stsp size_t newsize = 2 * v->metasz;
335 e6bcace5 2021-06-22 stsp struct got_pack_meta **new;
336 e6bcace5 2021-06-22 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
337 e6bcace5 2021-06-22 stsp if (new == NULL) {
338 e6bcace5 2021-06-22 stsp err = got_error_from_errno("reallocarray");
341 e6bcace5 2021-06-22 stsp v->meta = new;
342 e6bcace5 2021-06-22 stsp v->metasz = newsize;
346 e6bcace5 2021-06-22 stsp clear_meta(m);
349 e6bcace5 2021-06-22 stsp v->meta[v->nmeta++] = m;
351 e6bcace5 2021-06-22 stsp return err;
354 e6bcace5 2021-06-22 stsp static const struct got_error *
355 e6bcace5 2021-06-22 stsp load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
356 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
357 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
358 05118f5a 2021-06-22 stsp int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
360 e6bcace5 2021-06-22 stsp const struct got_error *err;
361 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
362 e6bcace5 2021-06-22 stsp char *p = NULL;
365 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
367 e6bcace5 2021-06-22 stsp return err;
369 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
370 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
371 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
372 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
374 e6bcace5 2021-06-22 stsp if (cancel_cb) {
375 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
380 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
381 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
384 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
385 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
386 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
390 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
391 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
392 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
395 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
396 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
397 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
398 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
406 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
408 e6bcace5 2021-06-22 stsp return err;
411 e6bcace5 2021-06-22 stsp static const struct got_error *
412 e6bcace5 2021-06-22 stsp load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
413 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
414 05118f5a 2021-06-22 stsp int loose_obj_only, struct got_repository *repo,
415 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
417 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
418 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
419 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
421 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
422 e6bcace5 2021-06-22 stsp return NULL;
424 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
426 e6bcace5 2021-06-22 stsp return err;
428 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
429 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
431 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
432 e6bcace5 2021-06-22 stsp if (cancel_cb) {
433 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
438 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
439 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
441 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
442 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
446 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
447 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
449 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
453 e6bcace5 2021-06-22 stsp err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
454 05118f5a 2021-06-22 stsp mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
455 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
460 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
461 e6bcace5 2021-06-22 stsp return err;
464 e6bcace5 2021-06-22 stsp static const struct got_error *
465 e6bcace5 2021-06-22 stsp load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
466 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
467 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
469 e6bcace5 2021-06-22 stsp const struct got_error *err;
470 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
472 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
473 e6bcace5 2021-06-22 stsp return NULL;
475 05118f5a 2021-06-22 stsp if (loose_obj_only) {
476 05118f5a 2021-06-22 stsp int is_packed;
477 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
479 05118f5a 2021-06-22 stsp return err;
480 05118f5a 2021-06-22 stsp if (is_packed)
481 05118f5a 2021-06-22 stsp return NULL;
484 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
486 e6bcace5 2021-06-22 stsp return err;
488 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
489 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
490 05118f5a 2021-06-22 stsp loose_obj_only, repo);
494 e6bcace5 2021-06-22 stsp err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
495 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
496 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
498 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
499 e6bcace5 2021-06-22 stsp return err;
502 05118f5a 2021-06-22 stsp static const struct got_error *
503 05118f5a 2021-06-22 stsp load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
504 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
505 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
507 05118f5a 2021-06-22 stsp const struct got_error *err;
508 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
510 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
511 05118f5a 2021-06-22 stsp return NULL;
513 05118f5a 2021-06-22 stsp if (loose_obj_only) {
514 05118f5a 2021-06-22 stsp int is_packed;
515 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
517 05118f5a 2021-06-22 stsp return err;
518 05118f5a 2021-06-22 stsp if (is_packed)
519 05118f5a 2021-06-22 stsp return NULL;
522 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
524 05118f5a 2021-06-22 stsp return err;
526 05118f5a 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
527 05118f5a 2021-06-22 stsp got_object_tag_get_tagger_time(tag),
528 05118f5a 2021-06-22 stsp loose_obj_only, repo);
532 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
533 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
534 26960ff7 2021-09-14 stsp err = load_commit(v, idset,
535 05118f5a 2021-06-22 stsp got_object_tag_get_object_id(tag), repo,
536 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
538 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
539 05118f5a 2021-06-22 stsp err = load_tree(v, idset, got_object_tag_get_object_id(tag),
540 05118f5a 2021-06-22 stsp "", got_object_tag_get_tagger_time(tag),
541 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
548 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
549 05118f5a 2021-06-22 stsp return err;
552 e6bcace5 2021-06-22 stsp enum findtwixt_color {
553 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
554 e6bcace5 2021-06-22 stsp COLOR_DROP,
555 e6bcace5 2021-06-22 stsp COLOR_BLANK,
557 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
558 e6bcace5 2021-06-22 stsp COLOR_KEEP,
559 e6bcace5 2021-06-22 stsp COLOR_DROP,
560 e6bcace5 2021-06-22 stsp COLOR_BLANK
563 e6bcace5 2021-06-22 stsp static const struct got_error *
564 e6bcace5 2021-06-22 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
565 e6bcace5 2021-06-22 stsp int color, struct got_repository *repo)
567 e6bcace5 2021-06-22 stsp const struct got_error *err;
568 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
570 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
572 e6bcace5 2021-06-22 stsp return err;
574 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
575 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
576 e6bcace5 2021-06-22 stsp return NULL;
579 e6bcace5 2021-06-22 stsp static const struct got_error *
580 e6bcace5 2021-06-22 stsp drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
581 e6bcace5 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo,
582 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
584 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
585 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
586 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
587 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
588 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
590 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
592 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
594 e6bcace5 2021-06-22 stsp return err;
595 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&ids, qid, entry);
597 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
598 e6bcace5 2021-06-22 stsp if (cancel_cb) {
599 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
604 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
605 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
607 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id)) {
608 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
612 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, qid->id,
613 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
615 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
619 e6bcace5 2021-06-22 stsp if (!got_object_idset_contains(keep, qid->id)) {
620 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
624 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
625 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
629 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
630 e6bcace5 2021-06-22 stsp if (parents) {
631 e6bcace5 2021-06-22 stsp err = got_object_id_queue_copy(parents, &ids);
633 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
637 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
640 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
641 e6bcace5 2021-06-22 stsp return err;
644 e6bcace5 2021-06-22 stsp struct append_id_arg {
645 e6bcace5 2021-06-22 stsp struct got_object_id **array;
649 e6bcace5 2021-06-22 stsp static const struct got_error *
650 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
652 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
654 e6bcace5 2021-06-22 stsp a->array[a->idx] = got_object_id_dup(id);
655 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
656 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
659 e6bcace5 2021-06-22 stsp return NULL;
662 e6bcace5 2021-06-22 stsp static const struct got_error *
663 e6bcace5 2021-06-22 stsp findtwixt(struct got_object_id ***res, int *nres,
664 e6bcace5 2021-06-22 stsp struct got_object_id **head, int nhead,
665 e6bcace5 2021-06-22 stsp struct got_object_id **tail, int ntail,
666 e6bcace5 2021-06-22 stsp struct got_repository *repo,
667 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
669 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
670 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
671 e6bcace5 2021-06-22 stsp struct got_object_idset *keep, *drop;
672 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
673 05118f5a 2021-06-22 stsp int i, ncolor, nkeep, obj_type;
675 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
676 e6bcace5 2021-06-22 stsp *res = NULL;
679 e6bcace5 2021-06-22 stsp keep = got_object_idset_alloc();
680 e6bcace5 2021-06-22 stsp if (keep == NULL)
681 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
683 e6bcace5 2021-06-22 stsp drop = got_object_idset_alloc();
684 e6bcace5 2021-06-22 stsp if (drop == NULL) {
685 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_idset_alloc");
689 05118f5a 2021-06-22 stsp for (i = 0; i < nhead; i++) {
690 05118f5a 2021-06-22 stsp struct got_object_id *id = head[i];
691 05118f5a 2021-06-22 stsp if (id == NULL)
693 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
695 05118f5a 2021-06-22 stsp return err;
696 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
698 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
702 05118f5a 2021-06-22 stsp for (i = 0; i < ntail; i++) {
703 05118f5a 2021-06-22 stsp struct got_object_id *id = tail[i];
704 05118f5a 2021-06-22 stsp if (id == NULL)
706 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
708 05118f5a 2021-06-22 stsp return err;
709 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
711 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_DROP, repo);
716 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
717 e6bcace5 2021-06-22 stsp int qcolor;
718 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
719 e6bcace5 2021-06-22 stsp qcolor = *((int *)qid->data);
721 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id))
722 e6bcace5 2021-06-22 stsp ncolor = COLOR_DROP;
723 e6bcace5 2021-06-22 stsp else if (got_object_idset_contains(keep, qid->id))
724 e6bcace5 2021-06-22 stsp ncolor = COLOR_KEEP;
726 e6bcace5 2021-06-22 stsp ncolor = COLOR_BLANK;
728 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
729 e6bcace5 2021-06-22 stsp qcolor == COLOR_KEEP)) {
730 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
731 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
735 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
736 e6bcace5 2021-06-22 stsp err = drop_commit(keep, drop, qid->id, repo,
737 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
740 e6bcace5 2021-06-22 stsp } else if (ncolor == COLOR_BLANK) {
741 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
742 e6bcace5 2021-06-22 stsp struct got_object_id *id;
743 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
744 e6bcace5 2021-06-22 stsp struct got_object_qid *pid;
746 e6bcace5 2021-06-22 stsp id = got_object_id_dup(qid->id);
747 e6bcace5 2021-06-22 stsp if (id == NULL) {
748 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
751 e6bcace5 2021-06-22 stsp if (qcolor == COLOR_KEEP)
752 07165b17 2021-07-01 stsp err = got_object_idset_add(keep, id,
753 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
755 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, id,
756 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
762 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
767 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
768 e6bcace5 2021-06-22 stsp if (parents) {
769 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parents, entry) {
770 e6bcace5 2021-06-22 stsp err = queue_commit_id(&ids, pid->id,
771 e6bcace5 2021-06-22 stsp qcolor, repo);
778 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
779 e6bcace5 2021-06-22 stsp commit = NULL;
781 e6bcace5 2021-06-22 stsp /* should not happen */
782 e6bcace5 2021-06-22 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
783 e6bcace5 2021-06-22 stsp "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
787 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
788 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
791 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
792 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
793 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
794 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
795 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
796 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
799 e6bcace5 2021-06-22 stsp arg.idx = 0;
800 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
802 e6bcace5 2021-06-22 stsp free(arg.array);
805 e6bcace5 2021-06-22 stsp *res = arg.array;
806 e6bcace5 2021-06-22 stsp *nres = nkeep;
809 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
810 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
811 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
812 e6bcace5 2021-06-22 stsp return err;
815 e6bcace5 2021-06-22 stsp static const struct got_error *
816 e6bcace5 2021-06-22 stsp read_meta(struct got_pack_meta ***meta, int *nmeta,
817 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
818 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
819 05118f5a 2021-06-22 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
820 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
822 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
823 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
824 e6bcace5 2021-06-22 stsp struct got_object_idset *idset;
825 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
826 e6bcace5 2021-06-22 stsp struct got_pack_metavec v;
828 e6bcace5 2021-06-22 stsp *meta = NULL;
829 e6bcace5 2021-06-22 stsp *nmeta = 0;
831 e6bcace5 2021-06-22 stsp idset = got_object_idset_alloc();
832 e6bcace5 2021-06-22 stsp if (idset == NULL)
833 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
835 e6bcace5 2021-06-22 stsp v.nmeta = 0;
836 e6bcace5 2021-06-22 stsp v.metasz = 64;
837 e6bcace5 2021-06-22 stsp v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
838 e6bcace5 2021-06-22 stsp if (v.meta == NULL) {
839 1d19226a 2021-10-13 stsp err = got_error_from_errno("calloc");
843 e6bcace5 2021-06-22 stsp err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
844 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
845 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
848 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
849 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
850 05118f5a 2021-06-22 stsp if (id == NULL)
852 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
854 05118f5a 2021-06-22 stsp return err;
855 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
857 05118f5a 2021-06-22 stsp err = load_commit(NULL, idset, id, repo,
858 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
861 05118f5a 2021-06-22 stsp if (progress_cb) {
862 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
863 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
869 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
870 f4a2ff2d 2021-07-01 stsp struct got_object_id *id = theirs[i];
871 07165b17 2021-07-01 stsp int *cached_type;
872 05118f5a 2021-06-22 stsp if (id == NULL)
874 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
875 07165b17 2021-07-01 stsp if (cached_type == NULL) {
876 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
880 07165b17 2021-07-01 stsp obj_type = *cached_type;
881 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
883 05118f5a 2021-06-22 stsp err = load_tag(NULL, idset, id, repo,
884 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
887 05118f5a 2021-06-22 stsp if (progress_cb) {
888 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
889 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
895 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
896 eca70f98 2021-09-03 stsp err = load_commit(&v, idset, ids[i], repo,
897 eca70f98 2021-09-03 stsp loose_obj_only, cancel_cb, cancel_arg);
900 eca70f98 2021-09-03 stsp if (progress_cb) {
901 eca70f98 2021-09-03 stsp err = progress_cb(progress_arg, 0L, nours,
902 eca70f98 2021-09-03 stsp v.nmeta, 0, 0);
908 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
909 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
910 07165b17 2021-07-01 stsp int *cached_type;
911 05118f5a 2021-06-22 stsp if (id == NULL)
913 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
914 07165b17 2021-07-01 stsp if (cached_type == NULL) {
915 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
919 07165b17 2021-07-01 stsp obj_type = *cached_type;
920 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
922 05118f5a 2021-06-22 stsp err = load_tag(&v, idset, id, repo,
923 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
926 05118f5a 2021-06-22 stsp if (progress_cb) {
927 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
928 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
935 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
936 e6bcace5 2021-06-22 stsp free(ids[i]);
939 e6bcace5 2021-06-22 stsp got_object_idset_free(idset);
940 e6bcace5 2021-06-22 stsp if (err == NULL) {
941 e6bcace5 2021-06-22 stsp *meta = v.meta;
942 e6bcace5 2021-06-22 stsp *nmeta = v.nmeta;
944 e6bcace5 2021-06-22 stsp free(v.meta);
946 e6bcace5 2021-06-22 stsp return err;
949 e6bcace5 2021-06-22 stsp const struct got_error *
950 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
954 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
955 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
956 e6bcace5 2021-06-22 stsp if (n != len)
957 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
958 e6bcace5 2021-06-22 stsp return NULL;
961 e6bcace5 2021-06-22 stsp static void
962 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
964 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
965 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
966 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
967 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
971 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
973 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
975 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
976 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
977 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
978 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
979 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
980 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
981 e6bcace5 2021-06-22 stsp if (ahd != bhd)
982 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
983 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
984 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
985 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
988 e6bcace5 2021-06-22 stsp static const struct got_error *
989 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
993 e6bcace5 2021-06-22 stsp *hdrlen = 0;
995 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
996 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
998 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
999 e6bcace5 2021-06-22 stsp if (i >= bufsize)
1000 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
1001 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
1002 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1003 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1006 e6bcace5 2021-06-22 stsp *hdrlen = i;
1007 e6bcace5 2021-06-22 stsp return NULL;
1010 e6bcace5 2021-06-22 stsp static const struct got_error *
1011 08347b73 2021-10-14 stsp encodedelta(struct got_pack_meta *m, struct got_raw_object *o,
1012 08347b73 2021-10-14 stsp off_t base_size, FILE *f)
1014 e6bcace5 2021-06-22 stsp unsigned char buf[16], *bp;
1018 e6bcace5 2021-06-22 stsp struct got_delta_instruction *d;
1020 e6bcace5 2021-06-22 stsp /* base object size */
1021 e6bcace5 2021-06-22 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1022 e6bcace5 2021-06-22 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
1023 e6bcace5 2021-06-22 stsp for (i = 1; n > 0; i++) {
1024 e6bcace5 2021-06-22 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1025 e6bcace5 2021-06-22 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1026 e6bcace5 2021-06-22 stsp n >>= GOT_DELTA_SIZE_SHIFT;
1028 08347b73 2021-10-14 stsp w = fwrite(buf, 1, i, f);
1029 08347b73 2021-10-14 stsp if (w != i)
1030 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1032 e6bcace5 2021-06-22 stsp /* target object size */
1033 e6bcace5 2021-06-22 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1034 e6bcace5 2021-06-22 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
1035 e6bcace5 2021-06-22 stsp for (i = 1; n > 0; i++) {
1036 e6bcace5 2021-06-22 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1037 e6bcace5 2021-06-22 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1038 e6bcace5 2021-06-22 stsp n >>= GOT_DELTA_SIZE_SHIFT;
1040 08347b73 2021-10-14 stsp w = fwrite(buf, 1, i, f);
1041 08347b73 2021-10-14 stsp if (w != i)
1042 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1044 e6bcace5 2021-06-22 stsp for (j = 0; j < m->ndeltas; j++) {
1045 e6bcace5 2021-06-22 stsp d = &m->deltas[j];
1046 e6bcace5 2021-06-22 stsp if (d->copy) {
1047 e6bcace5 2021-06-22 stsp n = d->offset;
1048 e6bcace5 2021-06-22 stsp bp = &buf[1];
1049 e6bcace5 2021-06-22 stsp buf[0] = GOT_DELTA_BASE_COPY;
1050 e6bcace5 2021-06-22 stsp for (i = 0; i < 4; i++) {
1051 e6bcace5 2021-06-22 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1052 e6bcace5 2021-06-22 stsp buf[0] |= 1 << i;
1053 e6bcace5 2021-06-22 stsp *bp++ = n & 0xff;
1055 e6bcace5 2021-06-22 stsp if (n == 0)
1059 e6bcace5 2021-06-22 stsp n = d->len;
1060 e6bcace5 2021-06-22 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1061 e6bcace5 2021-06-22 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1062 e6bcace5 2021-06-22 stsp for (i = 0; i < 3 && n > 0; i++) {
1063 e6bcace5 2021-06-22 stsp buf[0] |= 1 << (i + 4);
1064 e6bcace5 2021-06-22 stsp *bp++ = n & 0xff;
1068 08347b73 2021-10-14 stsp w = fwrite(buf, 1, bp - buf, f);
1069 08347b73 2021-10-14 stsp if (w != bp - buf)
1070 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1072 e6bcace5 2021-06-22 stsp char content[128];
1074 e6bcace5 2021-06-22 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1075 e6bcace5 2021-06-22 stsp return got_error_from_errno("fseeko");
1077 e6bcace5 2021-06-22 stsp while (n != d->len) {
1078 e6bcace5 2021-06-22 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
1079 08347b73 2021-10-14 stsp w = fwrite(buf, 1, 1, f);
1080 08347b73 2021-10-14 stsp if (w != 1)
1081 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1082 e6bcace5 2021-06-22 stsp r = fread(content, 1, buf[0], o->f);
1083 e6bcace5 2021-06-22 stsp if (r != buf[0])
1084 e6bcace5 2021-06-22 stsp return got_ferror(o->f, GOT_ERR_IO);
1085 08347b73 2021-10-14 stsp w = fwrite(content, 1, buf[0], f);
1086 08347b73 2021-10-14 stsp if (w != buf[0])
1087 08347b73 2021-10-14 stsp return got_ferror(f, GOT_ERR_IO);
1088 e6bcace5 2021-06-22 stsp n += buf[0];
1093 e6bcace5 2021-06-22 stsp return NULL;
1096 e6bcace5 2021-06-22 stsp static int
1097 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1100 e6bcace5 2021-06-22 stsp char rbuf[8];
1102 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1103 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1104 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1105 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1109 e6bcace5 2021-06-22 stsp while (i > 0)
1110 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1114 e6bcace5 2021-06-22 stsp static const struct got_error *
1115 e6bcace5 2021-06-22 stsp genpack(uint8_t *pack_sha1, FILE *packfile,
1116 05118f5a 2021-06-22 stsp struct got_pack_meta **meta, int nmeta, int nours,
1117 05118f5a 2021-06-22 stsp int use_offset_deltas, struct got_repository *repo,
1118 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1119 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1121 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1122 08347b73 2021-10-14 stsp int i, nh;
1124 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1125 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1126 600b755e 2021-10-14 stsp struct got_raw_object *raw = NULL;
1127 08347b73 2021-10-14 stsp FILE *delta_file = NULL;
1128 08347b73 2021-10-14 stsp char buf[32];
1129 e6bcace5 2021-06-22 stsp size_t outlen, n;
1130 e6bcace5 2021-06-22 stsp struct got_deflate_checksum csum;
1131 05118f5a 2021-06-22 stsp off_t packfile_size = 0;
1133 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1134 e6bcace5 2021-06-22 stsp csum.output_sha1 = &ctx;
1135 e6bcace5 2021-06-22 stsp csum.output_crc = NULL;
1137 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1139 e6bcace5 2021-06-22 stsp return err;
1140 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1141 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1143 e6bcace5 2021-06-22 stsp goto done;
1144 e6bcace5 2021-06-22 stsp putbe32(buf, nmeta);
1145 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1147 e6bcace5 2021-06-22 stsp goto done;
1148 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1149 05118f5a 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
1150 05118f5a 2021-06-22 stsp if (progress_cb) {
1151 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1152 05118f5a 2021-06-22 stsp nmeta, nmeta, i);
1154 05118f5a 2021-06-22 stsp goto done;
1156 e6bcace5 2021-06-22 stsp m = meta[i];
1157 e6bcace5 2021-06-22 stsp m->off = ftello(packfile);
1158 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&raw, repo, &m->id, 8192);
1160 e6bcace5 2021-06-22 stsp goto done;
1161 e6bcace5 2021-06-22 stsp if (m->deltas == NULL) {
1162 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1163 e6bcace5 2021-06-22 stsp m->obj_type, raw->size);
1165 e6bcace5 2021-06-22 stsp goto done;
1166 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1168 e6bcace5 2021-06-22 stsp goto done;
1169 05118f5a 2021-06-22 stsp packfile_size += nh;
1170 e6bcace5 2021-06-22 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1171 e6bcace5 2021-06-22 stsp err = got_error_from_errno("fseeko");
1172 e6bcace5 2021-06-22 stsp goto done;
1174 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, raw->f, packfile,
1177 e6bcace5 2021-06-22 stsp goto done;
1178 05118f5a 2021-06-22 stsp packfile_size += outlen;
1180 08347b73 2021-10-14 stsp if (delta_file == NULL) {
1181 08347b73 2021-10-14 stsp delta_file = got_opentemp();
1182 08347b73 2021-10-14 stsp if (delta_file == NULL) {
1183 08347b73 2021-10-14 stsp err = got_error_from_errno(
1184 08347b73 2021-10-14 stsp "got_opentemp");
1185 08347b73 2021-10-14 stsp goto done;
1188 08347b73 2021-10-14 stsp if (ftruncate(fileno(delta_file), 0L) == -1) {
1189 08347b73 2021-10-14 stsp err = got_error_from_errno("ftruncate");
1190 08347b73 2021-10-14 stsp goto done;
1192 08347b73 2021-10-14 stsp if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1193 08347b73 2021-10-14 stsp err = got_error_from_errno("fseeko");
1194 08347b73 2021-10-14 stsp goto done;
1196 600b755e 2021-10-14 stsp err = encodedelta(m, raw, m->prev->size, delta_file);
1198 e6bcace5 2021-06-22 stsp goto done;
1199 08347b73 2021-10-14 stsp nd = ftello(delta_file);
1200 08347b73 2021-10-14 stsp if (fseeko(delta_file, 0L, SEEK_SET) == -1) {
1201 08347b73 2021-10-14 stsp err = got_error_from_errno("fseeko");
1202 08347b73 2021-10-14 stsp goto done;
1204 e6bcace5 2021-06-22 stsp if (use_offset_deltas && m->prev->off != 0) {
1205 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1206 e6bcace5 2021-06-22 stsp GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1208 e6bcace5 2021-06-22 stsp goto done;
1209 e6bcace5 2021-06-22 stsp nh += packoff(buf + nh,
1210 e6bcace5 2021-06-22 stsp m->off - m->prev->off);
1211 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1213 e6bcace5 2021-06-22 stsp goto done;
1214 05118f5a 2021-06-22 stsp packfile_size += nh;
1216 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1217 e6bcace5 2021-06-22 stsp GOT_OBJ_TYPE_REF_DELTA, nd);
1218 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1220 e6bcace5 2021-06-22 stsp goto done;
1221 05118f5a 2021-06-22 stsp packfile_size += nh;
1222 e6bcace5 2021-06-22 stsp err = hwrite(packfile, m->prev->id.sha1,
1223 e6bcace5 2021-06-22 stsp sizeof(m->prev->id.sha1), &ctx);
1224 05118f5a 2021-06-22 stsp packfile_size += sizeof(m->prev->id.sha1);
1226 e6bcace5 2021-06-22 stsp goto done;
1228 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, delta_file,
1229 e6bcace5 2021-06-22 stsp packfile, &csum);
1231 e6bcace5 2021-06-22 stsp goto done;
1232 05118f5a 2021-06-22 stsp packfile_size += outlen;
1234 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
1235 e6bcace5 2021-06-22 stsp raw = NULL;
1237 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1238 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1239 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1240 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1241 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1242 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1243 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1244 05118f5a 2021-06-22 stsp nmeta, nmeta, nmeta);
1246 05118f5a 2021-06-22 stsp goto done;
1248 08347b73 2021-10-14 stsp if (delta_file && fclose(delta_file) == EOF && err == NULL)
1249 08347b73 2021-10-14 stsp err = got_error_from_errno("fclose");
1251 08347b73 2021-10-14 stsp got_object_raw_close(raw);
1252 e6bcace5 2021-06-22 stsp return err;
1255 e6bcace5 2021-06-22 stsp const struct got_error *
1256 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1257 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1258 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1259 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1260 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1261 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1263 e6bcace5 2021-06-22 stsp const struct got_error *err;
1264 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
1265 e6bcace5 2021-06-22 stsp int nmeta;
1267 e6bcace5 2021-06-22 stsp err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1268 05118f5a 2021-06-22 stsp loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1270 e6bcace5 2021-06-22 stsp return err;
1272 f8a36e22 2021-08-26 stsp if (nmeta == 0 && !allow_empty) {
1273 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1274 05118f5a 2021-06-22 stsp goto done;
1276 f8a36e22 2021-08-26 stsp if (nmeta > 0) {
1277 f8a36e22 2021-08-26 stsp err = pick_deltas(meta, nmeta, nours, repo,
1278 f8a36e22 2021-08-26 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1280 f8a36e22 2021-08-26 stsp goto done;
1283 05118f5a 2021-06-22 stsp err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1284 05118f5a 2021-06-22 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1286 e6bcace5 2021-06-22 stsp goto done;
1288 e6bcace5 2021-06-22 stsp free_nmeta(meta, nmeta);
1289 e6bcace5 2021-06-22 stsp return err;