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 e6bcace5 2021-06-22 stsp #include <sys/queue.h>
19 e6bcace5 2021-06-22 stsp #include <sys/stat.h>
21 e6bcace5 2021-06-22 stsp #include <inttypes.h>
22 05118f5a 2021-06-22 stsp #include <imsg.h>
23 e6bcace5 2021-06-22 stsp #include <stdio.h>
24 e6bcace5 2021-06-22 stsp #include <stdlib.h>
25 e6bcace5 2021-06-22 stsp #include <string.h>
26 e6bcace5 2021-06-22 stsp #include <sha1.h>
27 e6bcace5 2021-06-22 stsp #include <limits.h>
28 e6bcace5 2021-06-22 stsp #include <zlib.h>
30 e6bcace5 2021-06-22 stsp #include "got_error.h"
31 e6bcace5 2021-06-22 stsp #include "got_cancel.h"
32 e6bcace5 2021-06-22 stsp #include "got_object.h"
33 05118f5a 2021-06-22 stsp #include "got_reference.h"
34 05118f5a 2021-06-22 stsp #include "got_repository_admin.h"
36 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
37 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
38 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
39 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
40 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
41 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
42 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
43 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
44 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
47 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
50 e6bcace5 2021-06-22 stsp struct got_pack_meta {
51 e6bcace5 2021-06-22 stsp struct got_object_id id;
53 e6bcace5 2021-06-22 stsp int obj_type;
54 e6bcace5 2021-06-22 stsp time_t mtime;
56 e6bcace5 2021-06-22 stsp /* The best delta we picked */
57 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
58 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
59 e6bcace5 2021-06-22 stsp struct got_delta_instruction *deltas;
60 e6bcace5 2021-06-22 stsp int ndeltas;
63 e6bcace5 2021-06-22 stsp /* Only used for delta window */
64 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
66 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
70 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
71 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
76 e6bcace5 2021-06-22 stsp static const struct got_error *
77 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
78 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
80 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
81 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
83 e6bcace5 2021-06-22 stsp *new = NULL;
85 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
86 e6bcace5 2021-06-22 stsp if (m == NULL)
87 e6bcace5 2021-06-22 stsp return got_error_from_errno("malloc");
89 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
91 e6bcace5 2021-06-22 stsp m->path = strdup(path);
92 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
93 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
98 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
99 e6bcace5 2021-06-22 stsp m->mtime = mtime;
101 e6bcace5 2021-06-22 stsp return NULL;
104 e6bcace5 2021-06-22 stsp static void
105 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
107 e6bcace5 2021-06-22 stsp if (meta == NULL)
109 e6bcace5 2021-06-22 stsp free(meta->deltas);
110 e6bcace5 2021-06-22 stsp meta->deltas = NULL;
111 e6bcace5 2021-06-22 stsp free(meta->path);
112 e6bcace5 2021-06-22 stsp meta->path = NULL;
115 e6bcace5 2021-06-22 stsp static void
116 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
120 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
121 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
122 e6bcace5 2021-06-22 stsp free(meta);
126 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
128 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
131 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
132 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
134 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
135 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
136 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
137 e6bcace5 2021-06-22 stsp if (cmp != 0)
138 e6bcace5 2021-06-22 stsp return cmp;
139 e6bcace5 2021-06-22 stsp if (a->mtime != b->mtime)
140 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
141 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
145 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
147 e6bcace5 2021-06-22 stsp int i, size = 32;
148 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
149 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
150 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
152 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
154 e6bcace5 2021-06-22 stsp return size;
158 e6bcace5 2021-06-22 stsp static const struct got_error *
159 05118f5a 2021-06-22 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
160 05118f5a 2021-06-22 stsp struct got_repository *repo,
161 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
162 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
164 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
165 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
166 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
167 e6bcace5 2021-06-22 stsp struct got_delta_instruction *deltas;
168 05118f5a 2021-06-22 stsp int i, j, size, ndeltas, best;
169 e6bcace5 2021-06-22 stsp const int max_base_candidates = 10;
171 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
172 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
173 e6bcace5 2021-06-22 stsp if (cancel_cb) {
174 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
178 05118f5a 2021-06-22 stsp if (progress_cb) {
179 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
183 e6bcace5 2021-06-22 stsp m = meta[i];
184 e6bcace5 2021-06-22 stsp m->deltas = NULL;
185 e6bcace5 2021-06-22 stsp m->ndeltas = 0;
187 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
188 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
191 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&raw, repo, &m->id, 8192);
195 e6bcace5 2021-06-22 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
196 e6bcace5 2021-06-22 stsp raw->size + raw->hdrlen);
200 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
201 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
202 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
203 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
204 e6bcace5 2021-06-22 stsp n->dtab = NULL;
207 e6bcace5 2021-06-22 stsp best = raw->size;
208 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
209 e6bcace5 2021-06-22 stsp if (cancel_cb) {
210 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
214 e6bcace5 2021-06-22 stsp base = meta[j];
215 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
216 e6bcace5 2021-06-22 stsp if (base->nchain >= 128 ||
217 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
220 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&base_raw, repo, &base->id,
224 e6bcace5 2021-06-22 stsp err = got_deltify(&deltas, &ndeltas,
225 e6bcace5 2021-06-22 stsp raw->f, raw->hdrlen, raw->size + raw->hdrlen,
226 e6bcace5 2021-06-22 stsp base->dtab, base_raw->f, base_raw->hdrlen,
227 e6bcace5 2021-06-22 stsp base_raw->size + base_raw->hdrlen);
228 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
229 e6bcace5 2021-06-22 stsp base_raw = NULL;
233 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
234 e6bcace5 2021-06-22 stsp if (size + 32 < best){
236 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
237 e6bcace5 2021-06-22 stsp * replace it.
239 e6bcace5 2021-06-22 stsp free(m->deltas);
240 e6bcace5 2021-06-22 stsp best = size;
241 e6bcace5 2021-06-22 stsp m->deltas = deltas;
242 e6bcace5 2021-06-22 stsp m->ndeltas = ndeltas;
243 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
244 e6bcace5 2021-06-22 stsp m->prev = base;
245 e6bcace5 2021-06-22 stsp m->head = base->head;
246 e6bcace5 2021-06-22 stsp if (m->head == NULL)
247 e6bcace5 2021-06-22 stsp m->head = base;
249 e6bcace5 2021-06-22 stsp free(deltas);
250 e6bcace5 2021-06-22 stsp deltas = NULL;
251 e6bcace5 2021-06-22 stsp ndeltas = 0;
255 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
256 e6bcace5 2021-06-22 stsp raw = NULL;
259 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
260 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
261 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
264 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
265 e6bcace5 2021-06-22 stsp if (base_raw)
266 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
267 e6bcace5 2021-06-22 stsp return err;
270 e6bcace5 2021-06-22 stsp static const struct got_error *
271 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
272 05118f5a 2021-06-22 stsp struct got_repository *repo)
274 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
275 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
278 05118f5a 2021-06-22 stsp *found = 0;
280 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
281 05118f5a 2021-06-22 stsp if (err == NULL)
282 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
283 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
284 05118f5a 2021-06-22 stsp err = NULL;
285 05118f5a 2021-06-22 stsp return err;
288 05118f5a 2021-06-22 stsp static const struct got_error *
289 e6bcace5 2021-06-22 stsp add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
290 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
291 05118f5a 2021-06-22 stsp time_t mtime, int loose_obj_only, struct got_repository *repo)
293 e6bcace5 2021-06-22 stsp const struct got_error *err;
294 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
296 05118f5a 2021-06-22 stsp if (loose_obj_only) {
297 05118f5a 2021-06-22 stsp int is_packed;
298 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
300 05118f5a 2021-06-22 stsp return err;
301 05118f5a 2021-06-22 stsp if (is_packed)
302 05118f5a 2021-06-22 stsp return NULL;
305 e6bcace5 2021-06-22 stsp err = got_object_idset_add(idset, id, NULL);
307 e6bcace5 2021-06-22 stsp return err;
309 e6bcace5 2021-06-22 stsp if (v == NULL)
310 e6bcace5 2021-06-22 stsp return NULL;
312 e6bcace5 2021-06-22 stsp err = alloc_meta(&m, id, path, obj_type, mtime);
316 e6bcace5 2021-06-22 stsp if (v->nmeta == v->metasz){
317 e6bcace5 2021-06-22 stsp size_t newsize = 2 * v->metasz;
318 e6bcace5 2021-06-22 stsp struct got_pack_meta **new;
319 e6bcace5 2021-06-22 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
320 e6bcace5 2021-06-22 stsp if (new == NULL) {
321 e6bcace5 2021-06-22 stsp err = got_error_from_errno("reallocarray");
324 e6bcace5 2021-06-22 stsp v->meta = new;
325 e6bcace5 2021-06-22 stsp v->metasz = newsize;
329 e6bcace5 2021-06-22 stsp clear_meta(m);
332 e6bcace5 2021-06-22 stsp v->meta[v->nmeta++] = m;
334 e6bcace5 2021-06-22 stsp return err;
337 e6bcace5 2021-06-22 stsp static const struct got_error *
338 e6bcace5 2021-06-22 stsp load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
339 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
340 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
341 05118f5a 2021-06-22 stsp int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
343 e6bcace5 2021-06-22 stsp const struct got_error *err;
344 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
345 e6bcace5 2021-06-22 stsp char *p = NULL;
348 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
350 e6bcace5 2021-06-22 stsp return err;
352 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
353 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
354 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
355 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
357 e6bcace5 2021-06-22 stsp if (cancel_cb) {
358 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
363 e6bcace5 2021-06-22 stsp if (got_object_tree_entry_is_symlink(e) ||
364 e6bcace5 2021-06-22 stsp got_object_tree_entry_is_submodule(e) ||
365 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
368 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
369 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
370 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
374 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
375 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
376 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
379 e6bcace5 2021-06-22 stsp SIMPLEQ_INSERT_TAIL(ids, qid, entry);
380 e6bcace5 2021-06-22 stsp } else if (S_ISREG(mode)) {
381 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
382 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
390 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
392 e6bcace5 2021-06-22 stsp return err;
395 e6bcace5 2021-06-22 stsp static const struct got_error *
396 e6bcace5 2021-06-22 stsp load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
397 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
398 05118f5a 2021-06-22 stsp int loose_obj_only, struct got_repository *repo,
399 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
401 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
402 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
403 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
405 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
406 e6bcace5 2021-06-22 stsp return NULL;
408 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
410 e6bcace5 2021-06-22 stsp return err;
412 e6bcace5 2021-06-22 stsp SIMPLEQ_INIT(&tree_ids);
413 e6bcace5 2021-06-22 stsp SIMPLEQ_INSERT_TAIL(&tree_ids, qid, entry);
415 e6bcace5 2021-06-22 stsp while (!SIMPLEQ_EMPTY(&tree_ids)) {
416 e6bcace5 2021-06-22 stsp if (cancel_cb) {
417 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
422 e6bcace5 2021-06-22 stsp qid = SIMPLEQ_FIRST(&tree_ids);
423 e6bcace5 2021-06-22 stsp SIMPLEQ_REMOVE_HEAD(&tree_ids, entry);
425 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
426 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
430 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
431 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
433 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
437 e6bcace5 2021-06-22 stsp err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
438 05118f5a 2021-06-22 stsp mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
439 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
444 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
445 e6bcace5 2021-06-22 stsp return err;
448 e6bcace5 2021-06-22 stsp static const struct got_error *
449 e6bcace5 2021-06-22 stsp load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
450 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
451 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
453 e6bcace5 2021-06-22 stsp const struct got_error *err;
454 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
456 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
457 e6bcace5 2021-06-22 stsp return NULL;
459 05118f5a 2021-06-22 stsp if (loose_obj_only) {
460 05118f5a 2021-06-22 stsp int is_packed;
461 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
463 05118f5a 2021-06-22 stsp return err;
464 05118f5a 2021-06-22 stsp if (is_packed)
465 05118f5a 2021-06-22 stsp return NULL;
468 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
470 e6bcace5 2021-06-22 stsp return err;
472 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
473 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
474 05118f5a 2021-06-22 stsp loose_obj_only, repo);
478 e6bcace5 2021-06-22 stsp err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
479 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
480 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
482 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
483 e6bcace5 2021-06-22 stsp return err;
486 05118f5a 2021-06-22 stsp static const struct got_error *
487 05118f5a 2021-06-22 stsp load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
488 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
489 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
491 05118f5a 2021-06-22 stsp const struct got_error *err;
492 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
494 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
495 05118f5a 2021-06-22 stsp return NULL;
497 05118f5a 2021-06-22 stsp if (loose_obj_only) {
498 05118f5a 2021-06-22 stsp int is_packed;
499 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
501 05118f5a 2021-06-22 stsp return err;
502 05118f5a 2021-06-22 stsp if (is_packed)
503 05118f5a 2021-06-22 stsp return NULL;
506 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
508 05118f5a 2021-06-22 stsp return err;
510 05118f5a 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
511 05118f5a 2021-06-22 stsp got_object_tag_get_tagger_time(tag),
512 05118f5a 2021-06-22 stsp loose_obj_only, repo);
516 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
517 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
518 05118f5a 2021-06-22 stsp err = load_commit(NULL, idset,
519 05118f5a 2021-06-22 stsp got_object_tag_get_object_id(tag), repo,
520 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
522 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
523 05118f5a 2021-06-22 stsp err = load_tree(v, idset, got_object_tag_get_object_id(tag),
524 05118f5a 2021-06-22 stsp "", got_object_tag_get_tagger_time(tag),
525 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
532 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
533 05118f5a 2021-06-22 stsp return err;
536 e6bcace5 2021-06-22 stsp enum findtwixt_color {
537 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
538 e6bcace5 2021-06-22 stsp COLOR_DROP,
539 e6bcace5 2021-06-22 stsp COLOR_BLANK,
541 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
542 e6bcace5 2021-06-22 stsp COLOR_KEEP,
543 e6bcace5 2021-06-22 stsp COLOR_DROP,
544 e6bcace5 2021-06-22 stsp COLOR_BLANK
547 e6bcace5 2021-06-22 stsp static const struct got_error *
548 e6bcace5 2021-06-22 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
549 e6bcace5 2021-06-22 stsp int color, struct got_repository *repo)
551 e6bcace5 2021-06-22 stsp const struct got_error *err;
552 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
554 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
556 e6bcace5 2021-06-22 stsp return err;
558 e6bcace5 2021-06-22 stsp SIMPLEQ_INSERT_TAIL(ids, qid, entry);
559 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
560 e6bcace5 2021-06-22 stsp return NULL;
563 e6bcace5 2021-06-22 stsp static const struct got_error *
564 e6bcace5 2021-06-22 stsp drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
565 e6bcace5 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo,
566 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
568 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
569 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
570 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
571 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
572 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
574 e6bcace5 2021-06-22 stsp SIMPLEQ_INIT(&ids);
576 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
578 e6bcace5 2021-06-22 stsp return err;
579 e6bcace5 2021-06-22 stsp SIMPLEQ_INSERT_HEAD(&ids, qid, entry);
581 e6bcace5 2021-06-22 stsp while (!SIMPLEQ_EMPTY(&ids)) {
582 e6bcace5 2021-06-22 stsp if (cancel_cb) {
583 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
588 e6bcace5 2021-06-22 stsp qid = SIMPLEQ_FIRST(&ids);
589 e6bcace5 2021-06-22 stsp SIMPLEQ_REMOVE_HEAD(&ids, entry);
591 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id)) {
592 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
596 e6bcace5 2021-06-22 stsp err = got_object_idset_add(drop, qid->id, NULL);
598 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
602 e6bcace5 2021-06-22 stsp if (!got_object_idset_contains(keep, qid->id)) {
603 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
607 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
608 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
612 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
613 e6bcace5 2021-06-22 stsp if (parents) {
614 e6bcace5 2021-06-22 stsp err = got_object_id_queue_copy(parents, &ids);
616 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
620 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
623 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
624 e6bcace5 2021-06-22 stsp return err;
627 e6bcace5 2021-06-22 stsp struct append_id_arg {
628 e6bcace5 2021-06-22 stsp struct got_object_id **array;
632 e6bcace5 2021-06-22 stsp static const struct got_error *
633 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
635 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
637 e6bcace5 2021-06-22 stsp a->array[a->idx] = got_object_id_dup(id);
638 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
639 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
642 e6bcace5 2021-06-22 stsp return NULL;
645 e6bcace5 2021-06-22 stsp static const struct got_error *
646 e6bcace5 2021-06-22 stsp findtwixt(struct got_object_id ***res, int *nres,
647 e6bcace5 2021-06-22 stsp struct got_object_id **head, int nhead,
648 e6bcace5 2021-06-22 stsp struct got_object_id **tail, int ntail,
649 e6bcace5 2021-06-22 stsp struct got_repository *repo,
650 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
652 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
653 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
654 e6bcace5 2021-06-22 stsp struct got_object_idset *keep, *drop;
655 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
656 05118f5a 2021-06-22 stsp int i, ncolor, nkeep, obj_type;
658 e6bcace5 2021-06-22 stsp SIMPLEQ_INIT(&ids);
659 e6bcace5 2021-06-22 stsp *res = NULL;
662 e6bcace5 2021-06-22 stsp keep = got_object_idset_alloc();
663 e6bcace5 2021-06-22 stsp if (keep == NULL)
664 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
666 e6bcace5 2021-06-22 stsp drop = got_object_idset_alloc();
667 e6bcace5 2021-06-22 stsp if (drop == NULL) {
668 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_idset_alloc");
672 05118f5a 2021-06-22 stsp for (i = 0; i < nhead; i++) {
673 05118f5a 2021-06-22 stsp struct got_object_id *id = head[i];
674 05118f5a 2021-06-22 stsp if (id == NULL)
676 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
678 05118f5a 2021-06-22 stsp return err;
679 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
681 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
685 05118f5a 2021-06-22 stsp for (i = 0; i < ntail; i++) {
686 05118f5a 2021-06-22 stsp struct got_object_id *id = tail[i];
687 05118f5a 2021-06-22 stsp if (id == NULL)
689 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
691 05118f5a 2021-06-22 stsp return err;
692 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
694 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_DROP, repo);
699 e6bcace5 2021-06-22 stsp while (!SIMPLEQ_EMPTY(&ids)) {
700 e6bcace5 2021-06-22 stsp int qcolor;
701 e6bcace5 2021-06-22 stsp qid = SIMPLEQ_FIRST(&ids);
702 e6bcace5 2021-06-22 stsp qcolor = *((int *)qid->data);
704 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id))
705 e6bcace5 2021-06-22 stsp ncolor = COLOR_DROP;
706 e6bcace5 2021-06-22 stsp else if (got_object_idset_contains(keep, qid->id))
707 e6bcace5 2021-06-22 stsp ncolor = COLOR_KEEP;
709 e6bcace5 2021-06-22 stsp ncolor = COLOR_BLANK;
711 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
712 e6bcace5 2021-06-22 stsp qcolor == COLOR_KEEP)) {
713 e6bcace5 2021-06-22 stsp SIMPLEQ_REMOVE_HEAD(&ids, entry);
714 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
718 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
719 e6bcace5 2021-06-22 stsp err = drop_commit(keep, drop, qid->id, repo,
720 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
723 e6bcace5 2021-06-22 stsp } else if (ncolor == COLOR_BLANK) {
724 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
725 e6bcace5 2021-06-22 stsp struct got_object_id *id;
726 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
727 e6bcace5 2021-06-22 stsp struct got_object_qid *pid;
729 e6bcace5 2021-06-22 stsp id = got_object_id_dup(qid->id);
730 e6bcace5 2021-06-22 stsp if (id == NULL) {
731 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
734 e6bcace5 2021-06-22 stsp if (qcolor == COLOR_KEEP)
735 e6bcace5 2021-06-22 stsp err = got_object_idset_add(keep, id, NULL);
737 e6bcace5 2021-06-22 stsp err = got_object_idset_add(drop, id, NULL);
743 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
748 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
749 e6bcace5 2021-06-22 stsp if (parents) {
750 e6bcace5 2021-06-22 stsp SIMPLEQ_FOREACH(pid, parents, entry) {
751 e6bcace5 2021-06-22 stsp err = queue_commit_id(&ids, pid->id,
752 e6bcace5 2021-06-22 stsp qcolor, repo);
759 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
760 e6bcace5 2021-06-22 stsp commit = NULL;
762 e6bcace5 2021-06-22 stsp /* should not happen */
763 e6bcace5 2021-06-22 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
764 e6bcace5 2021-06-22 stsp "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
768 e6bcace5 2021-06-22 stsp SIMPLEQ_REMOVE_HEAD(&ids, entry);
769 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
772 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
773 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
774 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
775 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
776 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
777 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
780 e6bcace5 2021-06-22 stsp arg.idx = 0;
781 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
783 e6bcace5 2021-06-22 stsp free(arg.array);
786 e6bcace5 2021-06-22 stsp *res = arg.array;
787 e6bcace5 2021-06-22 stsp *nres = nkeep;
790 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
791 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
792 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
793 e6bcace5 2021-06-22 stsp return err;
796 e6bcace5 2021-06-22 stsp static const struct got_error *
797 e6bcace5 2021-06-22 stsp read_meta(struct got_pack_meta ***meta, int *nmeta,
798 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
799 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
800 05118f5a 2021-06-22 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
801 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
803 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
804 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
805 e6bcace5 2021-06-22 stsp struct got_object_idset *idset;
806 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
807 e6bcace5 2021-06-22 stsp struct got_pack_metavec v;
809 e6bcace5 2021-06-22 stsp *meta = NULL;
810 e6bcace5 2021-06-22 stsp *nmeta = 0;
812 e6bcace5 2021-06-22 stsp idset = got_object_idset_alloc();
813 e6bcace5 2021-06-22 stsp if (idset == NULL)
814 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
816 e6bcace5 2021-06-22 stsp v.nmeta = 0;
817 e6bcace5 2021-06-22 stsp v.metasz = 64;
818 e6bcace5 2021-06-22 stsp v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
819 e6bcace5 2021-06-22 stsp if (v.meta == NULL) {
820 e6bcace5 2021-06-22 stsp err = got_error_from_errno("reallocarray");
824 e6bcace5 2021-06-22 stsp err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
825 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
826 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
829 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
830 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
831 05118f5a 2021-06-22 stsp if (id == NULL)
833 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
835 05118f5a 2021-06-22 stsp return err;
836 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
838 05118f5a 2021-06-22 stsp err = load_commit(NULL, idset, id, repo,
839 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
842 05118f5a 2021-06-22 stsp if (progress_cb) {
843 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
844 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
850 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
851 e6bcace5 2021-06-22 stsp err = load_commit(&v, idset, ids[i], repo,
852 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
855 05118f5a 2021-06-22 stsp if (progress_cb) {
856 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
857 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
863 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
864 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
865 05118f5a 2021-06-22 stsp if (id == NULL)
867 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
869 05118f5a 2021-06-22 stsp return err;
870 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
872 05118f5a 2021-06-22 stsp err = load_tag(NULL, idset, id, repo,
873 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
876 05118f5a 2021-06-22 stsp if (progress_cb) {
877 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
878 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
884 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
885 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
886 05118f5a 2021-06-22 stsp if (id == NULL)
888 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
890 05118f5a 2021-06-22 stsp return err;
891 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
893 05118f5a 2021-06-22 stsp err = load_tag(&v, idset, id, repo,
894 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
897 05118f5a 2021-06-22 stsp if (progress_cb) {
898 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
899 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
906 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
907 e6bcace5 2021-06-22 stsp free(ids[i]);
910 e6bcace5 2021-06-22 stsp got_object_idset_free(idset);
911 e6bcace5 2021-06-22 stsp if (err == NULL) {
912 e6bcace5 2021-06-22 stsp *meta = v.meta;
913 e6bcace5 2021-06-22 stsp *nmeta = v.nmeta;
915 e6bcace5 2021-06-22 stsp free(v.meta);
917 e6bcace5 2021-06-22 stsp return err;
920 e6bcace5 2021-06-22 stsp const struct got_error *
921 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
925 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
926 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
927 e6bcace5 2021-06-22 stsp if (n != len)
928 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
929 e6bcace5 2021-06-22 stsp return NULL;
932 e6bcace5 2021-06-22 stsp static void
933 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
935 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
936 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
937 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
938 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
942 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
944 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
946 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
947 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
948 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
949 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
950 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
951 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
952 e6bcace5 2021-06-22 stsp if (ahd != bhd)
953 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
954 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
955 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
956 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
959 e6bcace5 2021-06-22 stsp static const struct got_error *
960 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
964 e6bcace5 2021-06-22 stsp *hdrlen = 0;
966 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
967 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
969 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
970 e6bcace5 2021-06-22 stsp if (i >= bufsize)
971 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
972 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
973 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
974 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
977 e6bcace5 2021-06-22 stsp *hdrlen = i;
978 e6bcace5 2021-06-22 stsp return NULL;
981 e6bcace5 2021-06-22 stsp static const struct got_error *
982 e6bcace5 2021-06-22 stsp append(char **p, int *len, int *sz, void *seg, int nseg)
986 e6bcace5 2021-06-22 stsp if (*len + nseg >= *sz) {
987 e6bcace5 2021-06-22 stsp while (*len + nseg >= *sz)
988 e6bcace5 2021-06-22 stsp *sz += *sz / 2;
989 e6bcace5 2021-06-22 stsp n = realloc(*p, *sz);
990 e6bcace5 2021-06-22 stsp if (n == NULL)
991 e6bcace5 2021-06-22 stsp return got_error_from_errno("realloc");
994 e6bcace5 2021-06-22 stsp memcpy(*p + *len, seg, nseg);
995 e6bcace5 2021-06-22 stsp *len += nseg;
996 e6bcace5 2021-06-22 stsp return NULL;
1000 e6bcace5 2021-06-22 stsp static const struct got_error *
1001 e6bcace5 2021-06-22 stsp encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1002 e6bcace5 2021-06-22 stsp off_t base_size, char **pp)
1004 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1006 e6bcace5 2021-06-22 stsp unsigned char buf[16], *bp;
1007 e6bcace5 2021-06-22 stsp int len, sz, i, j;
1009 e6bcace5 2021-06-22 stsp struct got_delta_instruction *d;
1011 e6bcace5 2021-06-22 stsp *pp = NULL;
1016 e6bcace5 2021-06-22 stsp p = malloc(sz);
1017 e6bcace5 2021-06-22 stsp if (p == NULL)
1018 e6bcace5 2021-06-22 stsp return got_error_from_errno("malloc");
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 e6bcace5 2021-06-22 stsp err = append(&p, &len, &sz, buf, i);
1030 e6bcace5 2021-06-22 stsp return err;
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 e6bcace5 2021-06-22 stsp err = append(&p, &len, &sz, buf, i);
1042 e6bcace5 2021-06-22 stsp return err;
1043 e6bcace5 2021-06-22 stsp for (j = 0; j < m->ndeltas; j++) {
1044 e6bcace5 2021-06-22 stsp d = &m->deltas[j];
1045 e6bcace5 2021-06-22 stsp if (d->copy) {
1046 e6bcace5 2021-06-22 stsp n = d->offset;
1047 e6bcace5 2021-06-22 stsp bp = &buf[1];
1048 e6bcace5 2021-06-22 stsp buf[0] = GOT_DELTA_BASE_COPY;
1049 e6bcace5 2021-06-22 stsp for (i = 0; i < 4; i++) {
1050 e6bcace5 2021-06-22 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1051 e6bcace5 2021-06-22 stsp buf[0] |= 1 << i;
1052 e6bcace5 2021-06-22 stsp *bp++ = n & 0xff;
1054 e6bcace5 2021-06-22 stsp if (n == 0)
1058 e6bcace5 2021-06-22 stsp n = d->len;
1059 e6bcace5 2021-06-22 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1060 e6bcace5 2021-06-22 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1061 e6bcace5 2021-06-22 stsp for (i = 0; i < 3 && n > 0; i++) {
1062 e6bcace5 2021-06-22 stsp buf[0] |= 1 << (i + 4);
1063 e6bcace5 2021-06-22 stsp *bp++ = n & 0xff;
1067 e6bcace5 2021-06-22 stsp err = append(&p, &len, &sz, buf, bp - buf);
1069 e6bcace5 2021-06-22 stsp return err;
1071 e6bcace5 2021-06-22 stsp char content[128];
1073 e6bcace5 2021-06-22 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1074 e6bcace5 2021-06-22 stsp return got_error_from_errno("fseeko");
1076 e6bcace5 2021-06-22 stsp while (n != d->len) {
1077 e6bcace5 2021-06-22 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
1078 e6bcace5 2021-06-22 stsp err = append(&p, &len, &sz, buf, 1);
1080 e6bcace5 2021-06-22 stsp return err;
1081 e6bcace5 2021-06-22 stsp r = fread(content, 1, buf[0], o->f);
1082 e6bcace5 2021-06-22 stsp if (r != buf[0])
1083 e6bcace5 2021-06-22 stsp return got_ferror(o->f, GOT_ERR_IO);
1084 e6bcace5 2021-06-22 stsp err = append(&p, &len, &sz, content, buf[0]);
1086 e6bcace5 2021-06-22 stsp return err;
1087 e6bcace5 2021-06-22 stsp n += buf[0];
1092 e6bcace5 2021-06-22 stsp *nd = len;
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 05118f5a 2021-06-22 stsp int i, nh, nd;
1123 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1124 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1125 e6bcace5 2021-06-22 stsp struct got_raw_object *raw;
1126 e6bcace5 2021-06-22 stsp char *p = NULL, 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 e6bcace5 2021-06-22 stsp FILE *delta_file;
1179 e6bcace5 2021-06-22 stsp struct got_raw_object *base_raw;
1180 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&base_raw, repo,
1181 e6bcace5 2021-06-22 stsp &m->prev->id, 8192);
1183 e6bcace5 2021-06-22 stsp goto done;
1184 e6bcace5 2021-06-22 stsp err = encodedelta(&nd, m, raw, base_raw->size, &p);
1186 e6bcace5 2021-06-22 stsp goto done;
1187 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
1188 e6bcace5 2021-06-22 stsp if (use_offset_deltas && m->prev->off != 0) {
1189 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1190 e6bcace5 2021-06-22 stsp GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1192 e6bcace5 2021-06-22 stsp goto done;
1193 e6bcace5 2021-06-22 stsp nh += packoff(buf + nh,
1194 e6bcace5 2021-06-22 stsp m->off - m->prev->off);
1195 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1197 e6bcace5 2021-06-22 stsp goto done;
1198 05118f5a 2021-06-22 stsp packfile_size += nh;
1200 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1201 e6bcace5 2021-06-22 stsp GOT_OBJ_TYPE_REF_DELTA, nd);
1202 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1204 e6bcace5 2021-06-22 stsp goto done;
1205 05118f5a 2021-06-22 stsp packfile_size += nh;
1206 e6bcace5 2021-06-22 stsp err = hwrite(packfile, m->prev->id.sha1,
1207 e6bcace5 2021-06-22 stsp sizeof(m->prev->id.sha1), &ctx);
1208 05118f5a 2021-06-22 stsp packfile_size += sizeof(m->prev->id.sha1);
1210 e6bcace5 2021-06-22 stsp goto done;
1212 e6bcace5 2021-06-22 stsp /* XXX need got_deflate_from_mem() */
1213 e6bcace5 2021-06-22 stsp delta_file = fmemopen(p, nd, "r");
1214 e6bcace5 2021-06-22 stsp if (delta_file == NULL) {
1215 e6bcace5 2021-06-22 stsp err = got_error_from_errno("fmemopen");
1216 e6bcace5 2021-06-22 stsp goto done;
1218 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, delta_file,
1219 e6bcace5 2021-06-22 stsp packfile, &csum);
1220 e6bcace5 2021-06-22 stsp fclose(delta_file);
1222 e6bcace5 2021-06-22 stsp goto done;
1223 05118f5a 2021-06-22 stsp packfile_size += outlen;
1227 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
1228 e6bcace5 2021-06-22 stsp raw = NULL;
1230 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1231 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1232 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1233 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1234 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1235 05118f5a 2021-06-22 stsp packfile_size += 16; /* pack file header */
1236 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1237 05118f5a 2021-06-22 stsp nmeta, nmeta, nmeta);
1239 05118f5a 2021-06-22 stsp goto done;
1242 e6bcace5 2021-06-22 stsp return err;
1245 e6bcace5 2021-06-22 stsp const struct got_error *
1246 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1247 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1248 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1249 05118f5a 2021-06-22 stsp struct got_repository *repo, int loose_obj_only,
1250 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1251 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1253 e6bcace5 2021-06-22 stsp const struct got_error *err;
1254 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
1255 e6bcace5 2021-06-22 stsp int nmeta;
1257 e6bcace5 2021-06-22 stsp err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1258 05118f5a 2021-06-22 stsp loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1260 e6bcace5 2021-06-22 stsp return err;
1262 05118f5a 2021-06-22 stsp if (nmeta == 0) {
1263 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1264 05118f5a 2021-06-22 stsp goto done;
1267 05118f5a 2021-06-22 stsp err = pick_deltas(meta, nmeta, nours, repo,
1268 05118f5a 2021-06-22 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1270 e6bcace5 2021-06-22 stsp goto done;
1272 05118f5a 2021-06-22 stsp err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1273 05118f5a 2021-06-22 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1275 e6bcace5 2021-06-22 stsp goto done;
1277 e6bcace5 2021-06-22 stsp free_nmeta(meta, nmeta);
1278 e6bcace5 2021-06-22 stsp return err;