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"
40 e6bcace5 2021-06-22 stsp #include "got_lib_deltify.h"
41 e6bcace5 2021-06-22 stsp #include "got_lib_delta.h"
42 e6bcace5 2021-06-22 stsp #include "got_lib_object.h"
43 e6bcace5 2021-06-22 stsp #include "got_lib_object_idset.h"
44 05118f5a 2021-06-22 stsp #include "got_lib_object_cache.h"
45 e6bcace5 2021-06-22 stsp #include "got_lib_deflate.h"
46 e6bcace5 2021-06-22 stsp #include "got_lib_pack.h"
47 05118f5a 2021-06-22 stsp #include "got_lib_privsep.h"
48 05118f5a 2021-06-22 stsp #include "got_lib_repository.h"
51 e6bcace5 2021-06-22 stsp #define MAX(_a,_b) ((_a) > (_b) ? (_a) : (_b))
54 e6bcace5 2021-06-22 stsp struct got_pack_meta {
55 e6bcace5 2021-06-22 stsp struct got_object_id id;
57 e6bcace5 2021-06-22 stsp int obj_type;
58 e6bcace5 2021-06-22 stsp time_t mtime;
60 e6bcace5 2021-06-22 stsp /* The best delta we picked */
61 e6bcace5 2021-06-22 stsp struct got_pack_meta *head;
62 e6bcace5 2021-06-22 stsp struct got_pack_meta *prev;
63 e6bcace5 2021-06-22 stsp struct got_delta_instruction *deltas;
64 e6bcace5 2021-06-22 stsp int ndeltas;
67 e6bcace5 2021-06-22 stsp /* Only used for delta window */
68 e6bcace5 2021-06-22 stsp struct got_delta_table *dtab;
70 e6bcace5 2021-06-22 stsp /* Only used for writing offset deltas */
74 e6bcace5 2021-06-22 stsp struct got_pack_metavec {
75 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
80 e6bcace5 2021-06-22 stsp static const struct got_error *
81 e6bcace5 2021-06-22 stsp alloc_meta(struct got_pack_meta **new, struct got_object_id *id,
82 e6bcace5 2021-06-22 stsp const char *path, int obj_type, time_t mtime)
84 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
85 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
87 e6bcace5 2021-06-22 stsp *new = NULL;
89 e6bcace5 2021-06-22 stsp m = calloc(1, sizeof(*m));
90 e6bcace5 2021-06-22 stsp if (m == NULL)
91 1d19226a 2021-10-13 stsp return got_error_from_errno("calloc");
93 e6bcace5 2021-06-22 stsp memcpy(&m->id, id, sizeof(m->id));
95 e6bcace5 2021-06-22 stsp m->path = strdup(path);
96 e6bcace5 2021-06-22 stsp if (m->path == NULL) {
97 e6bcace5 2021-06-22 stsp err = got_error_from_errno("strdup");
102 e6bcace5 2021-06-22 stsp m->obj_type = obj_type;
103 e6bcace5 2021-06-22 stsp m->mtime = mtime;
105 e6bcace5 2021-06-22 stsp return NULL;
108 e6bcace5 2021-06-22 stsp static void
109 e6bcace5 2021-06-22 stsp clear_meta(struct got_pack_meta *meta)
111 e6bcace5 2021-06-22 stsp if (meta == NULL)
113 e6bcace5 2021-06-22 stsp free(meta->deltas);
114 e6bcace5 2021-06-22 stsp meta->deltas = NULL;
115 e6bcace5 2021-06-22 stsp free(meta->path);
116 e6bcace5 2021-06-22 stsp meta->path = NULL;
119 e6bcace5 2021-06-22 stsp static void
120 e6bcace5 2021-06-22 stsp free_nmeta(struct got_pack_meta **meta, int nmeta)
124 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++)
125 e6bcace5 2021-06-22 stsp clear_meta(meta[i]);
126 e6bcace5 2021-06-22 stsp free(meta);
130 e6bcace5 2021-06-22 stsp delta_order_cmp(const void *pa, const void *pb)
132 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b;
135 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
136 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
138 e6bcace5 2021-06-22 stsp if (a->obj_type != b->obj_type)
139 e6bcace5 2021-06-22 stsp return a->obj_type - b->obj_type;
140 e6bcace5 2021-06-22 stsp cmp = strcmp(a->path, b->path);
141 e6bcace5 2021-06-22 stsp if (cmp != 0)
142 e6bcace5 2021-06-22 stsp return cmp;
143 e6bcace5 2021-06-22 stsp if (a->mtime != b->mtime)
144 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
145 e6bcace5 2021-06-22 stsp return got_object_id_cmp(&a->id, &b->id);
149 e6bcace5 2021-06-22 stsp delta_size(struct got_delta_instruction *deltas, int ndeltas)
151 e6bcace5 2021-06-22 stsp int i, size = 32;
152 e6bcace5 2021-06-22 stsp for (i = 0; i < ndeltas; i++) {
153 e6bcace5 2021-06-22 stsp if (deltas[i].copy)
154 e6bcace5 2021-06-22 stsp size += GOT_DELTA_SIZE_SHIFT;
156 e6bcace5 2021-06-22 stsp size += deltas[i].len + 1;
158 e6bcace5 2021-06-22 stsp return size;
162 e6bcace5 2021-06-22 stsp static const struct got_error *
163 05118f5a 2021-06-22 stsp pick_deltas(struct got_pack_meta **meta, int nmeta, int nours,
164 05118f5a 2021-06-22 stsp struct got_repository *repo,
165 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
166 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
168 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
169 e6bcace5 2021-06-22 stsp struct got_pack_meta *m = NULL, *base = NULL;
170 e6bcace5 2021-06-22 stsp struct got_raw_object *raw = NULL, *base_raw = NULL;
171 e6bcace5 2021-06-22 stsp struct got_delta_instruction *deltas;
172 05118f5a 2021-06-22 stsp int i, j, size, ndeltas, best;
173 e6bcace5 2021-06-22 stsp const int max_base_candidates = 10;
175 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), delta_order_cmp);
176 e6bcace5 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
177 e6bcace5 2021-06-22 stsp if (cancel_cb) {
178 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
182 05118f5a 2021-06-22 stsp if (progress_cb) {
183 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours, nmeta, i, 0);
187 e6bcace5 2021-06-22 stsp m = meta[i];
188 e6bcace5 2021-06-22 stsp m->deltas = NULL;
189 e6bcace5 2021-06-22 stsp m->ndeltas = 0;
191 e6bcace5 2021-06-22 stsp if (m->obj_type == GOT_OBJ_TYPE_COMMIT ||
192 e6bcace5 2021-06-22 stsp m->obj_type == GOT_OBJ_TYPE_TAG)
195 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&raw, repo, &m->id, 8192);
199 e6bcace5 2021-06-22 stsp err = got_deltify_init(&m->dtab, raw->f, raw->hdrlen,
200 e6bcace5 2021-06-22 stsp raw->size + raw->hdrlen);
204 e6bcace5 2021-06-22 stsp if (i > max_base_candidates) {
205 e6bcace5 2021-06-22 stsp struct got_pack_meta *n = NULL;
206 e6bcace5 2021-06-22 stsp n = meta[i - (max_base_candidates + 1)];
207 e6bcace5 2021-06-22 stsp got_deltify_free(n->dtab);
208 e6bcace5 2021-06-22 stsp n->dtab = NULL;
211 e6bcace5 2021-06-22 stsp best = raw->size;
212 e6bcace5 2021-06-22 stsp for (j = MAX(0, i - max_base_candidates); j < i; j++) {
213 e6bcace5 2021-06-22 stsp if (cancel_cb) {
214 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
218 e6bcace5 2021-06-22 stsp base = meta[j];
219 e6bcace5 2021-06-22 stsp /* long chains make unpacking slow, avoid such bases */
220 e6bcace5 2021-06-22 stsp if (base->nchain >= 128 ||
221 e6bcace5 2021-06-22 stsp base->obj_type != m->obj_type)
224 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&base_raw, repo, &base->id,
228 e6bcace5 2021-06-22 stsp err = got_deltify(&deltas, &ndeltas,
229 e6bcace5 2021-06-22 stsp raw->f, raw->hdrlen, raw->size + raw->hdrlen,
230 e6bcace5 2021-06-22 stsp base->dtab, base_raw->f, base_raw->hdrlen,
231 e6bcace5 2021-06-22 stsp base_raw->size + base_raw->hdrlen);
232 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
233 e6bcace5 2021-06-22 stsp base_raw = NULL;
237 e6bcace5 2021-06-22 stsp size = delta_size(deltas, ndeltas);
238 e6bcace5 2021-06-22 stsp if (size + 32 < best){
240 e6bcace5 2021-06-22 stsp * if we already picked a best delta,
241 e6bcace5 2021-06-22 stsp * replace it.
243 e6bcace5 2021-06-22 stsp free(m->deltas);
244 e6bcace5 2021-06-22 stsp best = size;
245 e6bcace5 2021-06-22 stsp m->deltas = deltas;
246 e6bcace5 2021-06-22 stsp m->ndeltas = ndeltas;
247 e6bcace5 2021-06-22 stsp m->nchain = base->nchain + 1;
248 e6bcace5 2021-06-22 stsp m->prev = base;
249 e6bcace5 2021-06-22 stsp m->head = base->head;
250 e6bcace5 2021-06-22 stsp if (m->head == NULL)
251 e6bcace5 2021-06-22 stsp m->head = base;
253 e6bcace5 2021-06-22 stsp free(deltas);
254 e6bcace5 2021-06-22 stsp deltas = NULL;
255 e6bcace5 2021-06-22 stsp ndeltas = 0;
259 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
260 e6bcace5 2021-06-22 stsp raw = NULL;
263 e6bcace5 2021-06-22 stsp for (i = MAX(0, nmeta - max_base_candidates); i < nmeta; i++) {
264 e6bcace5 2021-06-22 stsp got_deltify_free(meta[i]->dtab);
265 e6bcace5 2021-06-22 stsp meta[i]->dtab = NULL;
268 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
269 e6bcace5 2021-06-22 stsp if (base_raw)
270 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
271 e6bcace5 2021-06-22 stsp return err;
274 e6bcace5 2021-06-22 stsp static const struct got_error *
275 05118f5a 2021-06-22 stsp search_packidx(int *found, struct got_object_id *id,
276 05118f5a 2021-06-22 stsp struct got_repository *repo)
278 05118f5a 2021-06-22 stsp const struct got_error *err = NULL;
279 05118f5a 2021-06-22 stsp struct got_packidx *packidx = NULL;
282 05118f5a 2021-06-22 stsp *found = 0;
284 05118f5a 2021-06-22 stsp err = got_repo_search_packidx(&packidx, &idx, repo, id);
285 05118f5a 2021-06-22 stsp if (err == NULL)
286 05118f5a 2021-06-22 stsp *found = 1; /* object is already packed */
287 05118f5a 2021-06-22 stsp else if (err->code == GOT_ERR_NO_OBJ)
288 05118f5a 2021-06-22 stsp err = NULL;
289 05118f5a 2021-06-22 stsp return err;
292 07165b17 2021-07-01 stsp static const int obj_types[] = {
293 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_ANY,
294 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_COMMIT,
295 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TREE,
296 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_BLOB,
297 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_TAG,
298 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_OFFSET_DELTA,
299 07165b17 2021-07-01 stsp GOT_OBJ_TYPE_REF_DELTA
302 05118f5a 2021-06-22 stsp static const struct got_error *
303 e6bcace5 2021-06-22 stsp add_meta(struct got_pack_metavec *v, struct got_object_idset *idset,
304 e6bcace5 2021-06-22 stsp struct got_object_id *id, const char *path, int obj_type,
305 05118f5a 2021-06-22 stsp time_t mtime, int loose_obj_only, struct got_repository *repo)
307 e6bcace5 2021-06-22 stsp const struct got_error *err;
308 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
310 05118f5a 2021-06-22 stsp if (loose_obj_only) {
311 05118f5a 2021-06-22 stsp int is_packed;
312 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
314 05118f5a 2021-06-22 stsp return err;
315 05118f5a 2021-06-22 stsp if (is_packed)
316 05118f5a 2021-06-22 stsp return NULL;
319 07165b17 2021-07-01 stsp err = got_object_idset_add(idset, id, (void *)&obj_types[obj_type]);
321 e6bcace5 2021-06-22 stsp return err;
323 e6bcace5 2021-06-22 stsp if (v == NULL)
324 e6bcace5 2021-06-22 stsp return NULL;
326 e6bcace5 2021-06-22 stsp err = alloc_meta(&m, id, path, obj_type, mtime);
330 e6bcace5 2021-06-22 stsp if (v->nmeta == v->metasz){
331 e6bcace5 2021-06-22 stsp size_t newsize = 2 * v->metasz;
332 e6bcace5 2021-06-22 stsp struct got_pack_meta **new;
333 e6bcace5 2021-06-22 stsp new = reallocarray(v->meta, newsize, sizeof(*new));
334 e6bcace5 2021-06-22 stsp if (new == NULL) {
335 e6bcace5 2021-06-22 stsp err = got_error_from_errno("reallocarray");
338 e6bcace5 2021-06-22 stsp v->meta = new;
339 e6bcace5 2021-06-22 stsp v->metasz = newsize;
343 e6bcace5 2021-06-22 stsp clear_meta(m);
346 e6bcace5 2021-06-22 stsp v->meta[v->nmeta++] = m;
348 e6bcace5 2021-06-22 stsp return err;
351 e6bcace5 2021-06-22 stsp static const struct got_error *
352 e6bcace5 2021-06-22 stsp load_tree_entries(struct got_object_id_queue *ids, struct got_pack_metavec *v,
353 e6bcace5 2021-06-22 stsp struct got_object_idset *idset, struct got_object_id *tree_id,
354 e6bcace5 2021-06-22 stsp const char *dpath, time_t mtime, struct got_repository *repo,
355 05118f5a 2021-06-22 stsp int loose_obj_only, got_cancel_cb cancel_cb, void *cancel_arg)
357 e6bcace5 2021-06-22 stsp const struct got_error *err;
358 e6bcace5 2021-06-22 stsp struct got_tree_object *tree;
359 e6bcace5 2021-06-22 stsp char *p = NULL;
362 e6bcace5 2021-06-22 stsp err = got_object_open_as_tree(&tree, repo, tree_id);
364 e6bcace5 2021-06-22 stsp return err;
366 e6bcace5 2021-06-22 stsp for (i = 0; i < got_object_tree_get_nentries(tree); i++) {
367 e6bcace5 2021-06-22 stsp struct got_tree_entry *e = got_object_tree_get_entry(tree, i);
368 e6bcace5 2021-06-22 stsp struct got_object_id *id = got_tree_entry_get_id(e);
369 e6bcace5 2021-06-22 stsp mode_t mode = got_tree_entry_get_mode(e);
371 e6bcace5 2021-06-22 stsp if (cancel_cb) {
372 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
377 3af9de88 2021-09-22 stsp if (got_object_tree_entry_is_submodule(e) ||
378 e6bcace5 2021-06-22 stsp got_object_idset_contains(idset, id))
381 e6bcace5 2021-06-22 stsp if (asprintf(&p, "%s%s%s", dpath, dpath[0] != '\0' ? "/" : "",
382 e6bcace5 2021-06-22 stsp got_tree_entry_get_name(e)) == -1) {
383 e6bcace5 2021-06-22 stsp err = got_error_from_errno("asprintf");
387 e6bcace5 2021-06-22 stsp if (S_ISDIR(mode)) {
388 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
389 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
392 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
393 3af9de88 2021-09-22 stsp } else if (S_ISREG(mode) || S_ISLNK(mode)) {
394 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, p, GOT_OBJ_TYPE_BLOB,
395 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
403 e6bcace5 2021-06-22 stsp got_object_tree_close(tree);
405 e6bcace5 2021-06-22 stsp return err;
408 e6bcace5 2021-06-22 stsp static const struct got_error *
409 e6bcace5 2021-06-22 stsp load_tree(struct got_pack_metavec *v, struct got_object_idset *idset,
410 e6bcace5 2021-06-22 stsp struct got_object_id *tree_id, const char *dpath, time_t mtime,
411 05118f5a 2021-06-22 stsp int loose_obj_only, struct got_repository *repo,
412 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
414 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
415 e6bcace5 2021-06-22 stsp struct got_object_id_queue tree_ids;
416 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
418 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, tree_id))
419 e6bcace5 2021-06-22 stsp return NULL;
421 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, tree_id);
423 e6bcace5 2021-06-22 stsp return err;
425 dbdddfee 2021-06-23 naddy STAILQ_INIT(&tree_ids);
426 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(&tree_ids, qid, entry);
428 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&tree_ids)) {
429 e6bcace5 2021-06-22 stsp if (cancel_cb) {
430 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
435 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&tree_ids);
436 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&tree_ids, entry);
438 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, qid->id)) {
439 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
443 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, qid->id, dpath, GOT_OBJ_TYPE_TREE,
444 05118f5a 2021-06-22 stsp mtime, loose_obj_only, repo);
446 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
450 e6bcace5 2021-06-22 stsp err = load_tree_entries(&tree_ids, v, idset, qid->id, dpath,
451 05118f5a 2021-06-22 stsp mtime, repo, loose_obj_only, cancel_cb, cancel_arg);
452 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
457 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&tree_ids);
458 e6bcace5 2021-06-22 stsp return err;
461 e6bcace5 2021-06-22 stsp static const struct got_error *
462 e6bcace5 2021-06-22 stsp load_commit(struct got_pack_metavec *v, struct got_object_idset *idset,
463 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
464 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
466 e6bcace5 2021-06-22 stsp const struct got_error *err;
467 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
469 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(idset, id))
470 e6bcace5 2021-06-22 stsp return NULL;
472 05118f5a 2021-06-22 stsp if (loose_obj_only) {
473 05118f5a 2021-06-22 stsp int is_packed;
474 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
476 05118f5a 2021-06-22 stsp return err;
477 05118f5a 2021-06-22 stsp if (is_packed)
478 05118f5a 2021-06-22 stsp return NULL;
481 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
483 e6bcace5 2021-06-22 stsp return err;
485 e6bcace5 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_COMMIT,
486 05118f5a 2021-06-22 stsp got_object_commit_get_committer_time(commit),
487 05118f5a 2021-06-22 stsp loose_obj_only, repo);
491 e6bcace5 2021-06-22 stsp err = load_tree(v, idset, got_object_commit_get_tree_id(commit),
492 05118f5a 2021-06-22 stsp "", got_object_commit_get_committer_time(commit),
493 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
495 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
496 e6bcace5 2021-06-22 stsp return err;
499 05118f5a 2021-06-22 stsp static const struct got_error *
500 05118f5a 2021-06-22 stsp load_tag(struct got_pack_metavec *v, struct got_object_idset *idset,
501 05118f5a 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo, int loose_obj_only,
502 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
504 05118f5a 2021-06-22 stsp const struct got_error *err;
505 05118f5a 2021-06-22 stsp struct got_tag_object *tag = NULL;
507 05118f5a 2021-06-22 stsp if (got_object_idset_contains(idset, id))
508 05118f5a 2021-06-22 stsp return NULL;
510 05118f5a 2021-06-22 stsp if (loose_obj_only) {
511 05118f5a 2021-06-22 stsp int is_packed;
512 05118f5a 2021-06-22 stsp err = search_packidx(&is_packed, id, repo);
514 05118f5a 2021-06-22 stsp return err;
515 05118f5a 2021-06-22 stsp if (is_packed)
516 05118f5a 2021-06-22 stsp return NULL;
519 05118f5a 2021-06-22 stsp err = got_object_open_as_tag(&tag, repo, id);
521 05118f5a 2021-06-22 stsp return err;
523 05118f5a 2021-06-22 stsp err = add_meta(v, idset, id, "", GOT_OBJ_TYPE_TAG,
524 05118f5a 2021-06-22 stsp got_object_tag_get_tagger_time(tag),
525 05118f5a 2021-06-22 stsp loose_obj_only, repo);
529 05118f5a 2021-06-22 stsp switch (got_object_tag_get_object_type(tag)) {
530 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_COMMIT:
531 26960ff7 2021-09-14 stsp err = load_commit(v, idset,
532 05118f5a 2021-06-22 stsp got_object_tag_get_object_id(tag), repo,
533 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
535 05118f5a 2021-06-22 stsp case GOT_OBJ_TYPE_TREE:
536 05118f5a 2021-06-22 stsp err = load_tree(v, idset, got_object_tag_get_object_id(tag),
537 05118f5a 2021-06-22 stsp "", got_object_tag_get_tagger_time(tag),
538 05118f5a 2021-06-22 stsp loose_obj_only, repo, cancel_cb, cancel_arg);
545 05118f5a 2021-06-22 stsp got_object_tag_close(tag);
546 05118f5a 2021-06-22 stsp return err;
549 e6bcace5 2021-06-22 stsp enum findtwixt_color {
550 e6bcace5 2021-06-22 stsp COLOR_KEEP = 0,
551 e6bcace5 2021-06-22 stsp COLOR_DROP,
552 e6bcace5 2021-06-22 stsp COLOR_BLANK,
554 e6bcace5 2021-06-22 stsp static const int findtwixt_colors[] = {
555 e6bcace5 2021-06-22 stsp COLOR_KEEP,
556 e6bcace5 2021-06-22 stsp COLOR_DROP,
557 e6bcace5 2021-06-22 stsp COLOR_BLANK
560 e6bcace5 2021-06-22 stsp static const struct got_error *
561 e6bcace5 2021-06-22 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
562 e6bcace5 2021-06-22 stsp int color, struct got_repository *repo)
564 e6bcace5 2021-06-22 stsp const struct got_error *err;
565 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
567 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
569 e6bcace5 2021-06-22 stsp return err;
571 dbdddfee 2021-06-23 naddy STAILQ_INSERT_TAIL(ids, qid, entry);
572 e6bcace5 2021-06-22 stsp qid->data = (void *)&findtwixt_colors[color];
573 e6bcace5 2021-06-22 stsp return NULL;
576 e6bcace5 2021-06-22 stsp static const struct got_error *
577 e6bcace5 2021-06-22 stsp drop_commit(struct got_object_idset *keep, struct got_object_idset *drop,
578 e6bcace5 2021-06-22 stsp struct got_object_id *id, struct got_repository *repo,
579 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
581 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
582 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
583 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
584 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
585 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
587 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
589 e6bcace5 2021-06-22 stsp err = got_object_qid_alloc(&qid, id);
591 e6bcace5 2021-06-22 stsp return err;
592 dbdddfee 2021-06-23 naddy STAILQ_INSERT_HEAD(&ids, qid, entry);
594 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
595 e6bcace5 2021-06-22 stsp if (cancel_cb) {
596 e6bcace5 2021-06-22 stsp err = (*cancel_cb)(cancel_arg);
601 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
602 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
604 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id)) {
605 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
609 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, qid->id,
610 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
612 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
616 e6bcace5 2021-06-22 stsp if (!got_object_idset_contains(keep, qid->id)) {
617 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
621 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, qid->id);
622 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
626 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
627 e6bcace5 2021-06-22 stsp if (parents) {
628 e6bcace5 2021-06-22 stsp err = got_object_id_queue_copy(parents, &ids);
630 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
634 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
637 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
638 e6bcace5 2021-06-22 stsp return err;
641 e6bcace5 2021-06-22 stsp struct append_id_arg {
642 e6bcace5 2021-06-22 stsp struct got_object_id **array;
646 e6bcace5 2021-06-22 stsp static const struct got_error *
647 e6bcace5 2021-06-22 stsp append_id(struct got_object_id *id, void *data, void *arg)
649 e6bcace5 2021-06-22 stsp struct append_id_arg *a = arg;
651 e6bcace5 2021-06-22 stsp a->array[a->idx] = got_object_id_dup(id);
652 e6bcace5 2021-06-22 stsp if (a->array[a->idx] == NULL)
653 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_id_dup");
656 e6bcace5 2021-06-22 stsp return NULL;
659 e6bcace5 2021-06-22 stsp static const struct got_error *
660 e6bcace5 2021-06-22 stsp findtwixt(struct got_object_id ***res, int *nres,
661 e6bcace5 2021-06-22 stsp struct got_object_id **head, int nhead,
662 e6bcace5 2021-06-22 stsp struct got_object_id **tail, int ntail,
663 e6bcace5 2021-06-22 stsp struct got_repository *repo,
664 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
666 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
667 e6bcace5 2021-06-22 stsp struct got_object_id_queue ids;
668 e6bcace5 2021-06-22 stsp struct got_object_idset *keep, *drop;
669 e6bcace5 2021-06-22 stsp struct got_object_qid *qid;
670 05118f5a 2021-06-22 stsp int i, ncolor, nkeep, obj_type;
672 dbdddfee 2021-06-23 naddy STAILQ_INIT(&ids);
673 e6bcace5 2021-06-22 stsp *res = NULL;
676 e6bcace5 2021-06-22 stsp keep = got_object_idset_alloc();
677 e6bcace5 2021-06-22 stsp if (keep == NULL)
678 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
680 e6bcace5 2021-06-22 stsp drop = got_object_idset_alloc();
681 e6bcace5 2021-06-22 stsp if (drop == NULL) {
682 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_idset_alloc");
686 05118f5a 2021-06-22 stsp for (i = 0; i < nhead; i++) {
687 05118f5a 2021-06-22 stsp struct got_object_id *id = head[i];
688 05118f5a 2021-06-22 stsp if (id == NULL)
690 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
692 05118f5a 2021-06-22 stsp return err;
693 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
695 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_KEEP, repo);
699 05118f5a 2021-06-22 stsp for (i = 0; i < ntail; i++) {
700 05118f5a 2021-06-22 stsp struct got_object_id *id = tail[i];
701 05118f5a 2021-06-22 stsp if (id == NULL)
703 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
705 05118f5a 2021-06-22 stsp return err;
706 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
708 05118f5a 2021-06-22 stsp err = queue_commit_id(&ids, id, COLOR_DROP, repo);
713 dbdddfee 2021-06-23 naddy while (!STAILQ_EMPTY(&ids)) {
714 e6bcace5 2021-06-22 stsp int qcolor;
715 dbdddfee 2021-06-23 naddy qid = STAILQ_FIRST(&ids);
716 e6bcace5 2021-06-22 stsp qcolor = *((int *)qid->data);
718 e6bcace5 2021-06-22 stsp if (got_object_idset_contains(drop, qid->id))
719 e6bcace5 2021-06-22 stsp ncolor = COLOR_DROP;
720 e6bcace5 2021-06-22 stsp else if (got_object_idset_contains(keep, qid->id))
721 e6bcace5 2021-06-22 stsp ncolor = COLOR_KEEP;
723 e6bcace5 2021-06-22 stsp ncolor = COLOR_BLANK;
725 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_DROP || (ncolor == COLOR_KEEP &&
726 e6bcace5 2021-06-22 stsp qcolor == COLOR_KEEP)) {
727 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
728 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
732 e6bcace5 2021-06-22 stsp if (ncolor == COLOR_KEEP && qcolor == COLOR_DROP) {
733 e6bcace5 2021-06-22 stsp err = drop_commit(keep, drop, qid->id, repo,
734 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
737 e6bcace5 2021-06-22 stsp } else if (ncolor == COLOR_BLANK) {
738 e6bcace5 2021-06-22 stsp struct got_commit_object *commit;
739 e6bcace5 2021-06-22 stsp struct got_object_id *id;
740 e6bcace5 2021-06-22 stsp const struct got_object_id_queue *parents;
741 e6bcace5 2021-06-22 stsp struct got_object_qid *pid;
743 e6bcace5 2021-06-22 stsp id = got_object_id_dup(qid->id);
744 e6bcace5 2021-06-22 stsp if (id == NULL) {
745 e6bcace5 2021-06-22 stsp err = got_error_from_errno("got_object_id_dup");
748 e6bcace5 2021-06-22 stsp if (qcolor == COLOR_KEEP)
749 07165b17 2021-07-01 stsp err = got_object_idset_add(keep, id,
750 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
752 07165b17 2021-07-01 stsp err = got_object_idset_add(drop, id,
753 07165b17 2021-07-01 stsp (void *)&obj_types[GOT_OBJ_TYPE_COMMIT]);
759 e6bcace5 2021-06-22 stsp err = got_object_open_as_commit(&commit, repo, id);
764 e6bcace5 2021-06-22 stsp parents = got_object_commit_get_parent_ids(commit);
765 e6bcace5 2021-06-22 stsp if (parents) {
766 dbdddfee 2021-06-23 naddy STAILQ_FOREACH(pid, parents, entry) {
767 e6bcace5 2021-06-22 stsp err = queue_commit_id(&ids, pid->id,
768 e6bcace5 2021-06-22 stsp qcolor, repo);
775 e6bcace5 2021-06-22 stsp got_object_commit_close(commit);
776 e6bcace5 2021-06-22 stsp commit = NULL;
778 e6bcace5 2021-06-22 stsp /* should not happen */
779 e6bcace5 2021-06-22 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
780 e6bcace5 2021-06-22 stsp "%s ncolor=%d qcolor=%d", __func__, ncolor, qcolor);
784 dbdddfee 2021-06-23 naddy STAILQ_REMOVE_HEAD(&ids, entry);
785 e6bcace5 2021-06-22 stsp got_object_qid_free(qid);
788 e6bcace5 2021-06-22 stsp nkeep = got_object_idset_num_elements(keep);
789 e6bcace5 2021-06-22 stsp if (nkeep > 0) {
790 e6bcace5 2021-06-22 stsp struct append_id_arg arg;
791 e6bcace5 2021-06-22 stsp arg.array = calloc(nkeep, sizeof(struct got_object_id *));
792 e6bcace5 2021-06-22 stsp if (arg.array == NULL) {
793 e6bcace5 2021-06-22 stsp err = got_error_from_errno("calloc");
796 e6bcace5 2021-06-22 stsp arg.idx = 0;
797 e6bcace5 2021-06-22 stsp err = got_object_idset_for_each(keep, append_id, &arg);
799 e6bcace5 2021-06-22 stsp free(arg.array);
802 e6bcace5 2021-06-22 stsp *res = arg.array;
803 e6bcace5 2021-06-22 stsp *nres = nkeep;
806 e6bcace5 2021-06-22 stsp got_object_idset_free(keep);
807 e6bcace5 2021-06-22 stsp got_object_idset_free(drop);
808 e6bcace5 2021-06-22 stsp got_object_id_queue_free(&ids);
809 e6bcace5 2021-06-22 stsp return err;
812 e6bcace5 2021-06-22 stsp static const struct got_error *
813 e6bcace5 2021-06-22 stsp read_meta(struct got_pack_meta ***meta, int *nmeta,
814 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
815 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours, struct got_repository *repo,
816 05118f5a 2021-06-22 stsp int loose_obj_only, got_pack_progress_cb progress_cb, void *progress_arg,
817 e6bcace5 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
819 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
820 e6bcace5 2021-06-22 stsp struct got_object_id **ids = NULL;
821 e6bcace5 2021-06-22 stsp struct got_object_idset *idset;
822 05118f5a 2021-06-22 stsp int i, nobj = 0, obj_type;
823 e6bcace5 2021-06-22 stsp struct got_pack_metavec v;
825 e6bcace5 2021-06-22 stsp *meta = NULL;
826 e6bcace5 2021-06-22 stsp *nmeta = 0;
828 e6bcace5 2021-06-22 stsp idset = got_object_idset_alloc();
829 e6bcace5 2021-06-22 stsp if (idset == NULL)
830 e6bcace5 2021-06-22 stsp return got_error_from_errno("got_object_idset_alloc");
832 e6bcace5 2021-06-22 stsp v.nmeta = 0;
833 e6bcace5 2021-06-22 stsp v.metasz = 64;
834 e6bcace5 2021-06-22 stsp v.meta = calloc(v.metasz, sizeof(struct got_pack_meta *));
835 e6bcace5 2021-06-22 stsp if (v.meta == NULL) {
836 1d19226a 2021-10-13 stsp err = got_error_from_errno("calloc");
840 e6bcace5 2021-06-22 stsp err = findtwixt(&ids, &nobj, ours, nours, theirs, ntheirs, repo,
841 e6bcace5 2021-06-22 stsp cancel_cb, cancel_arg);
842 e6bcace5 2021-06-22 stsp if (err || nobj == 0)
845 e6bcace5 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
846 05118f5a 2021-06-22 stsp struct got_object_id *id = theirs[i];
847 05118f5a 2021-06-22 stsp if (id == NULL)
849 05118f5a 2021-06-22 stsp err = got_object_get_type(&obj_type, repo, id);
851 05118f5a 2021-06-22 stsp return err;
852 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_COMMIT)
854 05118f5a 2021-06-22 stsp err = load_commit(NULL, idset, id, repo,
855 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
858 05118f5a 2021-06-22 stsp if (progress_cb) {
859 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
860 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
866 05118f5a 2021-06-22 stsp for (i = 0; i < ntheirs; i++) {
867 f4a2ff2d 2021-07-01 stsp struct got_object_id *id = theirs[i];
868 07165b17 2021-07-01 stsp int *cached_type;
869 05118f5a 2021-06-22 stsp if (id == NULL)
871 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
872 07165b17 2021-07-01 stsp if (cached_type == NULL) {
873 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
877 07165b17 2021-07-01 stsp obj_type = *cached_type;
878 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
880 05118f5a 2021-06-22 stsp err = load_tag(NULL, idset, id, repo,
881 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
884 05118f5a 2021-06-22 stsp if (progress_cb) {
885 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
886 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
892 eca70f98 2021-09-03 stsp for (i = 0; i < nobj; i++) {
893 eca70f98 2021-09-03 stsp err = load_commit(&v, idset, ids[i], repo,
894 eca70f98 2021-09-03 stsp loose_obj_only, cancel_cb, cancel_arg);
897 eca70f98 2021-09-03 stsp if (progress_cb) {
898 eca70f98 2021-09-03 stsp err = progress_cb(progress_arg, 0L, nours,
899 eca70f98 2021-09-03 stsp v.nmeta, 0, 0);
905 05118f5a 2021-06-22 stsp for (i = 0; i < nours; i++) {
906 05118f5a 2021-06-22 stsp struct got_object_id *id = ours[i];
907 07165b17 2021-07-01 stsp int *cached_type;
908 05118f5a 2021-06-22 stsp if (id == NULL)
910 07165b17 2021-07-01 stsp cached_type = got_object_idset_get(idset, id);
911 07165b17 2021-07-01 stsp if (cached_type == NULL) {
912 07165b17 2021-07-01 stsp err = got_object_get_type(&obj_type, repo, id);
916 07165b17 2021-07-01 stsp obj_type = *cached_type;
917 05118f5a 2021-06-22 stsp if (obj_type != GOT_OBJ_TYPE_TAG)
919 05118f5a 2021-06-22 stsp err = load_tag(&v, idset, id, repo,
920 05118f5a 2021-06-22 stsp loose_obj_only, cancel_cb, cancel_arg);
923 05118f5a 2021-06-22 stsp if (progress_cb) {
924 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, 0L, nours,
925 05118f5a 2021-06-22 stsp v.nmeta, 0, 0);
932 e6bcace5 2021-06-22 stsp for (i = 0; i < nobj; i++) {
933 e6bcace5 2021-06-22 stsp free(ids[i]);
936 e6bcace5 2021-06-22 stsp got_object_idset_free(idset);
937 e6bcace5 2021-06-22 stsp if (err == NULL) {
938 e6bcace5 2021-06-22 stsp *meta = v.meta;
939 e6bcace5 2021-06-22 stsp *nmeta = v.nmeta;
941 e6bcace5 2021-06-22 stsp free(v.meta);
943 e6bcace5 2021-06-22 stsp return err;
946 e6bcace5 2021-06-22 stsp const struct got_error *
947 e6bcace5 2021-06-22 stsp hwrite(FILE *f, void *buf, int len, SHA1_CTX *ctx)
951 e6bcace5 2021-06-22 stsp SHA1Update(ctx, buf, len);
952 e6bcace5 2021-06-22 stsp n = fwrite(buf, 1, len, f);
953 e6bcace5 2021-06-22 stsp if (n != len)
954 e6bcace5 2021-06-22 stsp return got_ferror(f, GOT_ERR_IO);
955 e6bcace5 2021-06-22 stsp return NULL;
958 e6bcace5 2021-06-22 stsp static void
959 e6bcace5 2021-06-22 stsp putbe32(char *b, uint32_t n)
961 e6bcace5 2021-06-22 stsp b[0] = n >> 24;
962 e6bcace5 2021-06-22 stsp b[1] = n >> 16;
963 e6bcace5 2021-06-22 stsp b[2] = n >> 8;
964 e6bcace5 2021-06-22 stsp b[3] = n >> 0;
968 e6bcace5 2021-06-22 stsp write_order_cmp(const void *pa, const void *pb)
970 e6bcace5 2021-06-22 stsp struct got_pack_meta *a, *b, *ahd, *bhd;
972 e6bcace5 2021-06-22 stsp a = *(struct got_pack_meta **)pa;
973 e6bcace5 2021-06-22 stsp b = *(struct got_pack_meta **)pb;
974 e6bcace5 2021-06-22 stsp ahd = (a->head == NULL) ? a : a->head;
975 e6bcace5 2021-06-22 stsp bhd = (b->head == NULL) ? b : b->head;
976 e6bcace5 2021-06-22 stsp if (ahd->mtime != bhd->mtime)
977 e6bcace5 2021-06-22 stsp return bhd->mtime - ahd->mtime;
978 e6bcace5 2021-06-22 stsp if (ahd != bhd)
979 e6bcace5 2021-06-22 stsp return (uintptr_t)bhd - (uintptr_t)ahd;
980 e6bcace5 2021-06-22 stsp if (a->nchain != b->nchain)
981 e6bcace5 2021-06-22 stsp return a->nchain - b->nchain;
982 e6bcace5 2021-06-22 stsp return a->mtime - b->mtime;
985 e6bcace5 2021-06-22 stsp static const struct got_error *
986 e6bcace5 2021-06-22 stsp packhdr(int *hdrlen, char *hdr, size_t bufsize, int obj_type, size_t len)
990 e6bcace5 2021-06-22 stsp *hdrlen = 0;
992 e6bcace5 2021-06-22 stsp hdr[0] = obj_type << 4;
993 e6bcace5 2021-06-22 stsp hdr[0] |= len & 0xf;
995 e6bcace5 2021-06-22 stsp for (i = 1; len != 0; i++){
996 e6bcace5 2021-06-22 stsp if (i >= bufsize)
997 e6bcace5 2021-06-22 stsp return got_error(GOT_ERR_NO_SPACE);
998 e6bcace5 2021-06-22 stsp hdr[i - 1] |= GOT_DELTA_SIZE_MORE;
999 e6bcace5 2021-06-22 stsp hdr[i] = len & GOT_DELTA_SIZE_VAL_MASK;
1000 e6bcace5 2021-06-22 stsp len >>= GOT_DELTA_SIZE_SHIFT;
1003 e6bcace5 2021-06-22 stsp *hdrlen = i;
1004 e6bcace5 2021-06-22 stsp return NULL;
1007 e6bcace5 2021-06-22 stsp static const struct got_error *
1008 e6bcace5 2021-06-22 stsp append(char **p, int *len, int *sz, void *seg, int nseg)
1012 e6bcace5 2021-06-22 stsp if (*len + nseg >= *sz) {
1013 e6bcace5 2021-06-22 stsp while (*len + nseg >= *sz)
1014 e6bcace5 2021-06-22 stsp *sz += *sz / 2;
1015 e6bcace5 2021-06-22 stsp n = realloc(*p, *sz);
1016 e6bcace5 2021-06-22 stsp if (n == NULL)
1017 e6bcace5 2021-06-22 stsp return got_error_from_errno("realloc");
1020 e6bcace5 2021-06-22 stsp memcpy(*p + *len, seg, nseg);
1021 e6bcace5 2021-06-22 stsp *len += nseg;
1022 e6bcace5 2021-06-22 stsp return NULL;
1026 e6bcace5 2021-06-22 stsp static const struct got_error *
1027 e6bcace5 2021-06-22 stsp encodedelta(int *nd, struct got_pack_meta *m, struct got_raw_object *o,
1028 e6bcace5 2021-06-22 stsp off_t base_size, char **pp)
1030 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1032 e6bcace5 2021-06-22 stsp unsigned char buf[16], *bp;
1033 e6bcace5 2021-06-22 stsp int len, sz, i, j;
1035 e6bcace5 2021-06-22 stsp struct got_delta_instruction *d;
1037 e6bcace5 2021-06-22 stsp *pp = NULL;
1042 e6bcace5 2021-06-22 stsp p = malloc(sz);
1043 e6bcace5 2021-06-22 stsp if (p == NULL)
1044 e6bcace5 2021-06-22 stsp return got_error_from_errno("malloc");
1046 e6bcace5 2021-06-22 stsp /* base object size */
1047 e6bcace5 2021-06-22 stsp buf[0] = base_size & GOT_DELTA_SIZE_VAL_MASK;
1048 e6bcace5 2021-06-22 stsp n = base_size >> GOT_DELTA_SIZE_SHIFT;
1049 e6bcace5 2021-06-22 stsp for (i = 1; n > 0; i++) {
1050 e6bcace5 2021-06-22 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1051 e6bcace5 2021-06-22 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1052 e6bcace5 2021-06-22 stsp n >>= GOT_DELTA_SIZE_SHIFT;
1054 e6bcace5 2021-06-22 stsp err = append(&p, &len, &sz, buf, i);
1056 e6bcace5 2021-06-22 stsp return err;
1058 e6bcace5 2021-06-22 stsp /* target object size */
1059 e6bcace5 2021-06-22 stsp buf[0] = o->size & GOT_DELTA_SIZE_VAL_MASK;
1060 e6bcace5 2021-06-22 stsp n = o->size >> GOT_DELTA_SIZE_SHIFT;
1061 e6bcace5 2021-06-22 stsp for (i = 1; n > 0; i++) {
1062 e6bcace5 2021-06-22 stsp buf[i - 1] |= GOT_DELTA_SIZE_MORE;
1063 e6bcace5 2021-06-22 stsp buf[i] = n & GOT_DELTA_SIZE_VAL_MASK;
1064 e6bcace5 2021-06-22 stsp n >>= GOT_DELTA_SIZE_SHIFT;
1066 e6bcace5 2021-06-22 stsp err = append(&p, &len, &sz, buf, i);
1068 e6bcace5 2021-06-22 stsp return err;
1069 e6bcace5 2021-06-22 stsp for (j = 0; j < m->ndeltas; j++) {
1070 e6bcace5 2021-06-22 stsp d = &m->deltas[j];
1071 e6bcace5 2021-06-22 stsp if (d->copy) {
1072 e6bcace5 2021-06-22 stsp n = d->offset;
1073 e6bcace5 2021-06-22 stsp bp = &buf[1];
1074 e6bcace5 2021-06-22 stsp buf[0] = GOT_DELTA_BASE_COPY;
1075 e6bcace5 2021-06-22 stsp for (i = 0; i < 4; i++) {
1076 e6bcace5 2021-06-22 stsp /* DELTA_COPY_OFF1 ... DELTA_COPY_OFF4 */
1077 e6bcace5 2021-06-22 stsp buf[0] |= 1 << i;
1078 e6bcace5 2021-06-22 stsp *bp++ = n & 0xff;
1080 e6bcace5 2021-06-22 stsp if (n == 0)
1084 e6bcace5 2021-06-22 stsp n = d->len;
1085 e6bcace5 2021-06-22 stsp if (n != GOT_DELTA_COPY_DEFAULT_LEN) {
1086 e6bcace5 2021-06-22 stsp /* DELTA_COPY_LEN1 ... DELTA_COPY_LEN3 */
1087 e6bcace5 2021-06-22 stsp for (i = 0; i < 3 && n > 0; i++) {
1088 e6bcace5 2021-06-22 stsp buf[0] |= 1 << (i + 4);
1089 e6bcace5 2021-06-22 stsp *bp++ = n & 0xff;
1093 e6bcace5 2021-06-22 stsp err = append(&p, &len, &sz, buf, bp - buf);
1095 e6bcace5 2021-06-22 stsp return err;
1097 e6bcace5 2021-06-22 stsp char content[128];
1099 e6bcace5 2021-06-22 stsp if (fseeko(o->f, o->hdrlen + d->offset, SEEK_SET) == -1)
1100 e6bcace5 2021-06-22 stsp return got_error_from_errno("fseeko");
1102 e6bcace5 2021-06-22 stsp while (n != d->len) {
1103 e6bcace5 2021-06-22 stsp buf[0] = (d->len - n < 127) ? d->len - n : 127;
1104 e6bcace5 2021-06-22 stsp err = append(&p, &len, &sz, buf, 1);
1106 e6bcace5 2021-06-22 stsp return err;
1107 e6bcace5 2021-06-22 stsp r = fread(content, 1, buf[0], o->f);
1108 e6bcace5 2021-06-22 stsp if (r != buf[0])
1109 e6bcace5 2021-06-22 stsp return got_ferror(o->f, GOT_ERR_IO);
1110 e6bcace5 2021-06-22 stsp err = append(&p, &len, &sz, content, buf[0]);
1112 e6bcace5 2021-06-22 stsp return err;
1113 e6bcace5 2021-06-22 stsp n += buf[0];
1118 e6bcace5 2021-06-22 stsp *nd = len;
1119 e6bcace5 2021-06-22 stsp return NULL;
1122 e6bcace5 2021-06-22 stsp static int
1123 e6bcace5 2021-06-22 stsp packoff(char *hdr, off_t off)
1126 e6bcace5 2021-06-22 stsp char rbuf[8];
1128 e6bcace5 2021-06-22 stsp rbuf[0] = off & GOT_DELTA_SIZE_VAL_MASK;
1129 e6bcace5 2021-06-22 stsp for (i = 1; (off >>= GOT_DELTA_SIZE_SHIFT) != 0; i++) {
1130 e6bcace5 2021-06-22 stsp rbuf[i] = (--off & GOT_DELTA_SIZE_VAL_MASK) |
1131 e6bcace5 2021-06-22 stsp GOT_DELTA_SIZE_MORE;
1135 e6bcace5 2021-06-22 stsp while (i > 0)
1136 e6bcace5 2021-06-22 stsp hdr[j++] = rbuf[--i];
1140 e6bcace5 2021-06-22 stsp static const struct got_error *
1141 e6bcace5 2021-06-22 stsp genpack(uint8_t *pack_sha1, FILE *packfile,
1142 05118f5a 2021-06-22 stsp struct got_pack_meta **meta, int nmeta, int nours,
1143 05118f5a 2021-06-22 stsp int use_offset_deltas, struct got_repository *repo,
1144 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1145 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1147 e6bcace5 2021-06-22 stsp const struct got_error *err = NULL;
1148 05118f5a 2021-06-22 stsp int i, nh, nd;
1149 e6bcace5 2021-06-22 stsp SHA1_CTX ctx;
1150 e6bcace5 2021-06-22 stsp struct got_pack_meta *m;
1151 e6bcace5 2021-06-22 stsp struct got_raw_object *raw;
1152 e6bcace5 2021-06-22 stsp char *p = NULL, buf[32];
1153 e6bcace5 2021-06-22 stsp size_t outlen, n;
1154 e6bcace5 2021-06-22 stsp struct got_deflate_checksum csum;
1155 05118f5a 2021-06-22 stsp off_t packfile_size = 0;
1157 e6bcace5 2021-06-22 stsp SHA1Init(&ctx);
1158 e6bcace5 2021-06-22 stsp csum.output_sha1 = &ctx;
1159 e6bcace5 2021-06-22 stsp csum.output_crc = NULL;
1161 e6bcace5 2021-06-22 stsp err = hwrite(packfile, "PACK", 4, &ctx);
1163 e6bcace5 2021-06-22 stsp return err;
1164 e6bcace5 2021-06-22 stsp putbe32(buf, GOT_PACKFILE_VERSION);
1165 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1167 e6bcace5 2021-06-22 stsp goto done;
1168 e6bcace5 2021-06-22 stsp putbe32(buf, nmeta);
1169 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, 4, &ctx);
1171 e6bcace5 2021-06-22 stsp goto done;
1172 e6bcace5 2021-06-22 stsp qsort(meta, nmeta, sizeof(struct got_pack_meta *), write_order_cmp);
1173 05118f5a 2021-06-22 stsp for (i = 0; i < nmeta; i++) {
1174 05118f5a 2021-06-22 stsp if (progress_cb) {
1175 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1176 05118f5a 2021-06-22 stsp nmeta, nmeta, i);
1178 05118f5a 2021-06-22 stsp goto done;
1180 e6bcace5 2021-06-22 stsp m = meta[i];
1181 e6bcace5 2021-06-22 stsp m->off = ftello(packfile);
1182 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&raw, repo, &m->id, 8192);
1184 e6bcace5 2021-06-22 stsp goto done;
1185 e6bcace5 2021-06-22 stsp if (m->deltas == NULL) {
1186 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1187 e6bcace5 2021-06-22 stsp m->obj_type, raw->size);
1189 e6bcace5 2021-06-22 stsp goto done;
1190 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1192 e6bcace5 2021-06-22 stsp goto done;
1193 05118f5a 2021-06-22 stsp packfile_size += nh;
1194 e6bcace5 2021-06-22 stsp if (fseeko(raw->f, raw->hdrlen, SEEK_SET) == -1) {
1195 e6bcace5 2021-06-22 stsp err = got_error_from_errno("fseeko");
1196 e6bcace5 2021-06-22 stsp goto done;
1198 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, raw->f, packfile,
1201 e6bcace5 2021-06-22 stsp goto done;
1202 05118f5a 2021-06-22 stsp packfile_size += outlen;
1204 e6bcace5 2021-06-22 stsp FILE *delta_file;
1205 e6bcace5 2021-06-22 stsp struct got_raw_object *base_raw;
1206 e6bcace5 2021-06-22 stsp err = got_object_raw_open(&base_raw, repo,
1207 e6bcace5 2021-06-22 stsp &m->prev->id, 8192);
1209 e6bcace5 2021-06-22 stsp goto done;
1210 e6bcace5 2021-06-22 stsp err = encodedelta(&nd, m, raw, base_raw->size, &p);
1212 e6bcace5 2021-06-22 stsp goto done;
1213 e6bcace5 2021-06-22 stsp got_object_raw_close(base_raw);
1214 e6bcace5 2021-06-22 stsp if (use_offset_deltas && m->prev->off != 0) {
1215 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1216 e6bcace5 2021-06-22 stsp GOT_OBJ_TYPE_OFFSET_DELTA, nd);
1218 e6bcace5 2021-06-22 stsp goto done;
1219 e6bcace5 2021-06-22 stsp nh += packoff(buf + nh,
1220 e6bcace5 2021-06-22 stsp m->off - m->prev->off);
1221 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1223 e6bcace5 2021-06-22 stsp goto done;
1224 05118f5a 2021-06-22 stsp packfile_size += nh;
1226 e6bcace5 2021-06-22 stsp err = packhdr(&nh, buf, sizeof(buf),
1227 e6bcace5 2021-06-22 stsp GOT_OBJ_TYPE_REF_DELTA, nd);
1228 e6bcace5 2021-06-22 stsp err = hwrite(packfile, buf, nh, &ctx);
1230 e6bcace5 2021-06-22 stsp goto done;
1231 05118f5a 2021-06-22 stsp packfile_size += nh;
1232 e6bcace5 2021-06-22 stsp err = hwrite(packfile, m->prev->id.sha1,
1233 e6bcace5 2021-06-22 stsp sizeof(m->prev->id.sha1), &ctx);
1234 05118f5a 2021-06-22 stsp packfile_size += sizeof(m->prev->id.sha1);
1236 e6bcace5 2021-06-22 stsp goto done;
1238 e6bcace5 2021-06-22 stsp /* XXX need got_deflate_from_mem() */
1239 e6bcace5 2021-06-22 stsp delta_file = fmemopen(p, nd, "r");
1240 e6bcace5 2021-06-22 stsp if (delta_file == NULL) {
1241 e6bcace5 2021-06-22 stsp err = got_error_from_errno("fmemopen");
1242 e6bcace5 2021-06-22 stsp goto done;
1244 e6bcace5 2021-06-22 stsp err = got_deflate_to_file(&outlen, delta_file,
1245 e6bcace5 2021-06-22 stsp packfile, &csum);
1246 e6bcace5 2021-06-22 stsp fclose(delta_file);
1248 e6bcace5 2021-06-22 stsp goto done;
1249 05118f5a 2021-06-22 stsp packfile_size += outlen;
1253 e6bcace5 2021-06-22 stsp got_object_raw_close(raw);
1254 e6bcace5 2021-06-22 stsp raw = NULL;
1256 e6bcace5 2021-06-22 stsp SHA1Final(pack_sha1, &ctx);
1257 e6bcace5 2021-06-22 stsp n = fwrite(pack_sha1, 1, SHA1_DIGEST_LENGTH, packfile);
1258 e6bcace5 2021-06-22 stsp if (n != SHA1_DIGEST_LENGTH)
1259 e6bcace5 2021-06-22 stsp err = got_ferror(packfile, GOT_ERR_IO);
1260 05118f5a 2021-06-22 stsp packfile_size += SHA1_DIGEST_LENGTH;
1261 dc7edd42 2021-08-22 stsp packfile_size += sizeof(struct got_packfile_hdr);
1262 05118f5a 2021-06-22 stsp err = progress_cb(progress_arg, packfile_size, nours,
1263 05118f5a 2021-06-22 stsp nmeta, nmeta, nmeta);
1265 05118f5a 2021-06-22 stsp goto done;
1268 e6bcace5 2021-06-22 stsp return err;
1271 e6bcace5 2021-06-22 stsp const struct got_error *
1272 e6bcace5 2021-06-22 stsp got_pack_create(uint8_t *packsha1, FILE *packfile,
1273 e6bcace5 2021-06-22 stsp struct got_object_id **theirs, int ntheirs,
1274 e6bcace5 2021-06-22 stsp struct got_object_id **ours, int nours,
1275 f8a36e22 2021-08-26 stsp struct got_repository *repo, int loose_obj_only, int allow_empty,
1276 05118f5a 2021-06-22 stsp got_pack_progress_cb progress_cb, void *progress_arg,
1277 05118f5a 2021-06-22 stsp got_cancel_cb cancel_cb, void *cancel_arg)
1279 e6bcace5 2021-06-22 stsp const struct got_error *err;
1280 e6bcace5 2021-06-22 stsp struct got_pack_meta **meta;
1281 e6bcace5 2021-06-22 stsp int nmeta;
1283 e6bcace5 2021-06-22 stsp err = read_meta(&meta, &nmeta, theirs, ntheirs, ours, nours, repo,
1284 05118f5a 2021-06-22 stsp loose_obj_only, progress_cb, progress_arg, cancel_cb, cancel_arg);
1286 e6bcace5 2021-06-22 stsp return err;
1288 f8a36e22 2021-08-26 stsp if (nmeta == 0 && !allow_empty) {
1289 05118f5a 2021-06-22 stsp err = got_error(GOT_ERR_CANNOT_PACK);
1290 05118f5a 2021-06-22 stsp goto done;
1292 f8a36e22 2021-08-26 stsp if (nmeta > 0) {
1293 f8a36e22 2021-08-26 stsp err = pick_deltas(meta, nmeta, nours, repo,
1294 f8a36e22 2021-08-26 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1296 f8a36e22 2021-08-26 stsp goto done;
1299 05118f5a 2021-06-22 stsp err = genpack(packsha1, packfile, meta, nmeta, nours, 1, repo,
1300 05118f5a 2021-06-22 stsp progress_cb, progress_arg, cancel_cb, cancel_arg);
1302 e6bcace5 2021-06-22 stsp goto done;
1304 e6bcace5 2021-06-22 stsp free_nmeta(meta, nmeta);
1305 e6bcace5 2021-06-22 stsp return err;