2 * Copyright (c) 2020 Ori Bernstein
3 * Copyright (c) 2021, 2022 Stefan Sperling <stsp@openbsd.org>
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 #include <sys/types.h>
19 #include <sys/queue.h>
32 #include "got_error.h"
33 #include "got_cancel.h"
34 #include "got_object.h"
35 #include "got_reference.h"
36 #include "got_repository_admin.h"
39 #include "got_lib_delta.h"
40 #include "got_lib_object.h"
41 #include "got_lib_object_cache.h"
42 #include "got_lib_object_idset.h"
43 #include "got_lib_privsep.h"
44 #include "got_lib_ratelimit.h"
45 #include "got_lib_pack.h"
46 #include "got_lib_pack_create.h"
47 #include "got_lib_repository.h"
51 struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
55 static const struct got_error *
56 send_id(struct got_object_id *id, void *data, void *arg)
58 const struct got_error *err = NULL;
59 struct send_id_arg *a = arg;
61 a->ids[a->nids++] = id;
63 if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
64 err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
73 static const struct got_error *
74 send_idset(struct imsgbuf *ibuf, struct got_object_idset *idset)
76 const struct got_error *err;
77 struct send_id_arg sia;
79 memset(&sia, 0, sizeof(sia));
81 err = got_object_idset_for_each(idset, send_id, &sia);
86 err = got_privsep_send_object_idlist(ibuf, sia.ids, sia.nids);
91 return got_privsep_send_object_idlist_done(ibuf);
94 static const struct got_error *
95 recv_reused_delta(struct got_imsg_reused_delta *delta,
96 struct got_object_idset *idset, struct got_pack_metavec *v)
98 struct got_pack_meta *m, *base;
100 if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
101 delta->delta_offset +
102 delta->delta_compressed_size < delta->delta_offset)
103 return got_error(GOT_ERR_BAD_PACKFILE);
105 m = got_object_idset_get(idset, &delta->id);
107 return got_error(GOT_ERR_NO_OBJ);
109 base = got_object_idset_get(idset, &delta->base_id);
111 return got_error(GOT_ERR_NO_OBJ);
113 m->delta_len = delta->delta_size;
114 m->delta_compressed_len = delta->delta_compressed_size;
115 m->delta_offset = delta->delta_out_offset;
117 m->size = delta->result_size;
118 m->reused_delta_offset = delta->delta_offset;
119 m->base_obj_id = got_object_id_dup(&delta->base_id);
120 if (m->base_obj_id == NULL)
121 return got_error_from_errno("got_object_id_dup");
123 return got_pack_add_meta(m, v);
126 static const struct got_error *
127 prepare_delta_reuse(struct got_pack *pack, struct got_packidx *packidx,
128 int delta_outfd, struct got_repository *repo)
130 const struct got_error *err = NULL;
132 if (!pack->child_has_delta_outfd) {
134 outfd_child = dup(delta_outfd);
135 if (outfd_child == -1) {
136 err = got_error_from_errno("dup");
139 err = got_privsep_send_raw_delta_outfd(
140 pack->privsep_child->ibuf, outfd_child);
143 pack->child_has_delta_outfd = 1;
146 err = got_privsep_send_delta_reuse_req(pack->privsep_child->ibuf);
151 const struct got_error *
152 got_pack_search_deltas(struct got_pack_metavec *v,
153 struct got_object_idset *idset, int delta_cache_fd,
154 int ncolored, int nfound, int ntrees, int ncommits,
155 struct got_repository *repo,
156 got_pack_progress_cb progress_cb, void *progress_arg,
157 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
159 const struct got_error *err = NULL;
160 struct got_packidx *packidx;
161 struct got_pack *pack;
162 struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
165 err = got_pack_find_pack_for_reuse(&packidx, repo);
172 err = got_pack_cache_pack_for_packidx(&pack, packidx, repo);
176 if (pack->privsep_child == NULL) {
177 err = got_pack_start_privsep_child(pack, packidx);
182 err = prepare_delta_reuse(pack, packidx, delta_cache_fd, repo);
186 err = send_idset(pack->privsep_child->ibuf, idset);
194 err = (*cancel_cb)(cancel_arg);
199 err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
200 pack->privsep_child->ibuf);
204 for (i = 0; i < ndeltas; i++) {
205 struct got_imsg_reused_delta *delta = &deltas[i];
206 err = recv_reused_delta(delta, idset, v);
211 err = got_pack_report_progress(progress_cb, progress_arg, rl,
212 ncolored, nfound, ntrees, 0L, ncommits,
213 got_object_idset_num_elements(idset), v->nmeta, 0);
221 struct recv_painted_commit_arg {
225 struct got_object_id_queue *ids;
226 struct got_object_idset *keep;
227 struct got_object_idset *drop;
228 struct got_object_idset *skip;
229 got_pack_progress_cb progress_cb;
231 struct got_ratelimit *rl;
232 got_cancel_cb cancel_cb;
236 static const struct got_error *
237 recv_painted_commit(void *arg, struct got_object_id *id, intptr_t color)
239 const struct got_error *err = NULL;
240 struct recv_painted_commit_arg *a = arg;
241 struct got_object_qid *qid, *tmp;
244 err = a->cancel_cb(a->cancel_arg);
251 err = got_object_idset_add(a->keep, id, NULL);
257 err = got_object_idset_add(a->drop, id, NULL);
263 err = got_object_idset_add(a->skip, id, NULL);
268 /* should not happen */
269 return got_error_fmt(GOT_ERR_NOT_IMPL,
270 "%s invalid commit color %"PRIdPTR, __func__, color);
273 STAILQ_FOREACH_SAFE(qid, a->ids, entry, tmp) {
274 if (got_object_id_cmp(&qid->id, id) != 0)
276 STAILQ_REMOVE(a->ids, qid, got_object_qid, entry);
277 color = (intptr_t)qid->data;
278 got_object_qid_free(qid);
280 if (color == COLOR_SKIP)
285 return got_pack_report_progress(a->progress_cb, a->progress_arg, a->rl,
286 *a->ncolored, 0, 0, 0L, 0, 0, 0, 0);
289 static const struct got_error *
290 paint_packed_commits(struct got_pack *pack, struct got_object_id *id,
291 int idx, intptr_t color, int *ncolored, int *nqueued, int *nskip,
292 struct got_object_id_queue *ids,
293 struct got_object_idset *keep, struct got_object_idset *drop,
294 struct got_object_idset *skip, struct got_repository *repo,
295 got_pack_progress_cb progress_cb, void *progress_arg,
296 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
298 const struct got_error *err = NULL;
299 struct got_object_id_queue next_ids;
300 struct got_object_qid *qid, *tmp;
301 struct recv_painted_commit_arg arg;
303 STAILQ_INIT(&next_ids);
305 err = got_privsep_send_painting_request(pack->privsep_child->ibuf,
310 arg.ncolored = ncolored;
311 arg.nqueued = nqueued;
317 arg.progress_cb = progress_cb;
318 arg.progress_arg = progress_arg;
320 arg.cancel_cb = cancel_cb;
321 arg.cancel_arg = cancel_arg;
322 err = got_privsep_recv_painted_commits(&next_ids,
323 recv_painted_commit, &arg, pack->privsep_child->ibuf);
327 STAILQ_FOREACH_SAFE(qid, &next_ids, entry, tmp) {
328 struct got_object_qid *old_id;
329 intptr_t qcolor, ocolor;
330 STAILQ_FOREACH(old_id, ids, entry) {
331 if (got_object_id_cmp(&qid->id, &old_id->id))
333 qcolor = (intptr_t)qid->data;
334 ocolor = (intptr_t)old_id->data;
335 STAILQ_REMOVE(&next_ids, qid, got_object_qid, entry);
336 got_object_qid_free(qid);
338 if (qcolor != ocolor) {
339 got_pack_paint_commit(old_id, qcolor);
340 if (ocolor == COLOR_SKIP)
342 else if (qcolor == COLOR_SKIP)
348 while (!STAILQ_EMPTY(&next_ids)) {
349 qid = STAILQ_FIRST(&next_ids);
350 STAILQ_REMOVE_HEAD(&next_ids, entry);
351 got_pack_paint_commit(qid, color);
352 STAILQ_INSERT_TAIL(ids, qid, entry);
354 if (color == COLOR_SKIP)
361 const struct got_error *
362 got_pack_paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
363 struct got_object_idset *keep, struct got_object_idset *drop,
364 struct got_object_idset *skip, struct got_repository *repo,
365 got_pack_progress_cb progress_cb, void *progress_arg,
366 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
368 const struct got_error *err = NULL;
369 struct got_commit_object *commit = NULL;
370 struct got_packidx *packidx = NULL;
371 struct got_pack *pack = NULL;
372 const struct got_object_id_queue *parents;
373 struct got_object_qid *qid = NULL;
374 int nqueued = nids, nskip = 0;
377 while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
381 err = cancel_cb(cancel_arg);
386 qid = STAILQ_FIRST(ids);
387 STAILQ_REMOVE_HEAD(ids, entry);
389 color = (intptr_t)qid->data;
390 if (color == COLOR_SKIP)
393 if (got_object_idset_contains(skip, &qid->id)) {
394 got_object_qid_free(qid);
398 if (color == COLOR_KEEP &&
399 got_object_idset_contains(keep, &qid->id)) {
400 got_object_qid_free(qid);
404 if (color == COLOR_DROP &&
405 got_object_idset_contains(drop, &qid->id)) {
406 got_object_qid_free(qid);
411 /* Pinned pack may have moved to different cache slot. */
412 pack = got_repo_get_pinned_pack(repo);
414 if (packidx && pack) {
415 idx = got_packidx_get_object_idx(packidx, &qid->id);
417 err = paint_packed_commits(pack, &qid->id,
418 idx, color, ncolored, &nqueued, &nskip,
419 ids, keep, drop, skip, repo,
420 progress_cb, progress_arg, rl,
421 cancel_cb, cancel_arg);
424 got_object_qid_free(qid);
432 if (got_object_idset_contains(drop, &qid->id)) {
433 err = got_pack_paint_commit(qid, COLOR_SKIP);
438 err = got_object_idset_add(keep, &qid->id, NULL);
443 if (got_object_idset_contains(keep, &qid->id)) {
444 err = got_pack_paint_commit(qid, COLOR_SKIP);
449 err = got_object_idset_add(drop, &qid->id, NULL);
454 if (!got_object_idset_contains(skip, &qid->id)) {
455 err = got_object_idset_add(skip, &qid->id,
462 /* should not happen */
463 err = got_error_fmt(GOT_ERR_NOT_IMPL,
464 "%s invalid commit color %"PRIdPTR, __func__,
469 err = got_pack_report_progress(progress_cb, progress_arg, rl,
470 *ncolored, 0, 0, 0L, 0, 0, 0, 0);
474 err = got_object_open_as_commit(&commit, repo, &qid->id);
478 parents = got_object_commit_get_parent_ids(commit);
480 struct got_object_qid *pid;
481 color = (intptr_t)qid->data;
482 STAILQ_FOREACH(pid, parents, entry) {
483 err = got_pack_queue_commit_id(ids, &pid->id,
488 if (color == COLOR_SKIP)
493 if (pack == NULL && (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
494 if (packidx == NULL) {
495 err = got_pack_find_pack_for_commit_painting(
496 &packidx, ids, nqueued, repo);
500 if (packidx != NULL) {
501 err = got_pack_cache_pack_for_packidx(&pack,
505 if (pack->privsep_child == NULL) {
506 err = got_pack_start_privsep_child(
511 err = got_privsep_init_commit_painting(
512 pack->privsep_child->ibuf);
515 err = send_idset(pack->privsep_child->ibuf,
519 err = send_idset(pack->privsep_child->ibuf, drop);
522 err = send_idset(pack->privsep_child->ibuf, skip);
525 err = got_repo_pin_pack(repo, packidx, pack);
531 got_object_commit_close(commit);
534 got_object_qid_free(qid);
539 const struct got_error *pack_err;
540 pack_err = got_privsep_send_painting_commits_done(
541 pack->privsep_child->ibuf);
546 got_object_commit_close(commit);
547 got_object_qid_free(qid);
548 got_repo_unpin_pack(repo);
552 struct load_packed_obj_arg {
553 /* output parameters: */
554 struct got_object_id *id;
558 /* input parameters: */
561 struct got_object_idset *idset;
562 struct got_object_idset *idset_exclude;
567 got_pack_progress_cb progress_cb;
569 struct got_ratelimit *rl;
570 got_cancel_cb cancel_cb;
574 static const struct got_error *
575 load_packed_commit_id(void *arg, time_t mtime, struct got_object_id *id,
576 struct got_repository *repo)
578 struct load_packed_obj_arg *a = arg;
580 if (got_object_idset_contains(a->idset, id) ||
581 got_object_idset_contains(a->idset_exclude, id))
584 return got_pack_add_object(a->want_meta,
585 a->want_meta ? a->idset : a->idset_exclude,
586 id, "", GOT_OBJ_TYPE_COMMIT, mtime, a->seed, a->loose_obj_only,
587 repo, a->ncolored, a->nfound, a->ntrees,
588 a->progress_cb, a->progress_arg, a->rl);
591 static const struct got_error *
592 load_packed_tree_ids(void *arg, struct got_tree_object *tree, time_t mtime,
593 struct got_object_id *id, const char *dpath, struct got_repository *repo)
595 const struct got_error *err;
596 struct load_packed_obj_arg *a = arg;
600 * When we receive a tree's ID and path but not the tree itself,
601 * this tree object was not found in the pack file. This is the
602 * last time we are being called for this optimized traversal.
603 * Return from here and switch to loading objects the slow way.
607 a->id = got_object_id_dup(id);
609 err = got_error_from_errno("got_object_id_dup");
616 a->dpath = strdup(dpath);
617 if (a->dpath == NULL) {
618 err = got_error_from_errno("strdup");
628 if (got_object_idset_contains(a->idset, id) ||
629 got_object_idset_contains(a->idset_exclude, id))
633 while (relpath[0] == '/')
636 err = got_pack_add_object(a->want_meta,
637 a->want_meta ? a->idset : a->idset_exclude,
638 id, relpath, GOT_OBJ_TYPE_TREE, mtime, a->seed,
639 a->loose_obj_only, repo, a->ncolored, a->nfound, a->ntrees,
640 a->progress_cb, a->progress_arg, a->rl);
644 return got_pack_load_tree_entries(NULL, a->want_meta, a->idset,
645 a->idset_exclude, tree, dpath, mtime, a->seed, repo,
646 a->loose_obj_only, a->ncolored, a->nfound, a->ntrees,
647 a->progress_cb, a->progress_arg, a->rl,
648 a->cancel_cb, a->cancel_arg);
651 const struct got_error *
652 got_pack_load_packed_object_ids(int *found_all_objects,
653 struct got_object_id **ours, int nours,
654 struct got_object_id **theirs, int ntheirs,
655 int want_meta, uint32_t seed, struct got_object_idset *idset,
656 struct got_object_idset *idset_exclude, int loose_obj_only,
657 struct got_repository *repo, struct got_packidx *packidx,
658 int *ncolored, int *nfound, int *ntrees,
659 got_pack_progress_cb progress_cb, void *progress_arg,
660 struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
662 const struct got_error *err = NULL;
663 struct load_packed_obj_arg lpa;
665 memset(&lpa, 0, sizeof(lpa));
667 lpa.want_meta = want_meta;
669 lpa.idset_exclude = idset_exclude;
670 lpa.loose_obj_only = loose_obj_only;
671 lpa.ncolored = ncolored;
674 lpa.progress_cb = progress_cb;
675 lpa.progress_arg = progress_arg;
677 lpa.cancel_cb = cancel_cb;
678 lpa.cancel_arg = cancel_arg;
680 /* Attempt to load objects via got-read-pack, as far as possible. */
681 err = got_object_enumerate(found_all_objects, load_packed_commit_id,
682 load_packed_tree_ids, &lpa, ours, nours, theirs, ntheirs,
691 * An incomplete tree hierarchy was present in the pack file
692 * and caused loading to be aborted.
693 * Continue loading trees the slow way.
695 err = got_pack_load_tree(want_meta, idset, idset_exclude,
696 lpa.id, lpa.dpath, lpa.mtime, seed, repo, loose_obj_only,
697 ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
698 cancel_cb, cancel_arg);