Blame


1 097b408a 2022-10-17 thomas /*
2 097b408a 2022-10-17 thomas * Copyright (c) 2020 Ori Bernstein
3 097b408a 2022-10-17 thomas * Copyright (c) 2021, 2022 Stefan Sperling <stsp@openbsd.org>
4 097b408a 2022-10-17 thomas *
5 097b408a 2022-10-17 thomas * Permission to use, copy, modify, and distribute this software for any
6 097b408a 2022-10-17 thomas * purpose with or without fee is hereby granted, provided that the above
7 097b408a 2022-10-17 thomas * copyright notice and this permission notice appear in all copies.
8 097b408a 2022-10-17 thomas *
9 097b408a 2022-10-17 thomas * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 097b408a 2022-10-17 thomas * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 097b408a 2022-10-17 thomas * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 097b408a 2022-10-17 thomas * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 097b408a 2022-10-17 thomas * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 097b408a 2022-10-17 thomas * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 097b408a 2022-10-17 thomas * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 097b408a 2022-10-17 thomas */
17 097b408a 2022-10-17 thomas
18 097b408a 2022-10-17 thomas #include <sys/types.h>
19 097b408a 2022-10-17 thomas #include <sys/queue.h>
20 097b408a 2022-10-17 thomas #include <sys/uio.h>
21 097b408a 2022-10-17 thomas
22 097b408a 2022-10-17 thomas #include <limits.h>
23 097b408a 2022-10-17 thomas #include <stdio.h>
24 097b408a 2022-10-17 thomas #include <stdint.h>
25 097b408a 2022-10-17 thomas #include <stdlib.h>
26 097b408a 2022-10-17 thomas #include <string.h>
27 abd46894 2022-10-18 thomas #include <time.h>
28 097b408a 2022-10-17 thomas #include <imsg.h>
29 097b408a 2022-10-17 thomas #include <inttypes.h>
30 097b408a 2022-10-17 thomas #include <unistd.h>
31 097b408a 2022-10-17 thomas
32 4fccd2fe 2023-03-08 thomas #include "got_compat.h"
33 097b408a 2022-10-17 thomas #include "got_error.h"
34 097b408a 2022-10-17 thomas #include "got_cancel.h"
35 097b408a 2022-10-17 thomas #include "got_object.h"
36 097b408a 2022-10-17 thomas #include "got_reference.h"
37 097b408a 2022-10-17 thomas #include "got_repository_admin.h"
38 097b408a 2022-10-17 thomas #include "got_path.h"
39 097b408a 2022-10-17 thomas
40 097b408a 2022-10-17 thomas #include "got_lib_delta.h"
41 097b408a 2022-10-17 thomas #include "got_lib_object.h"
42 097b408a 2022-10-17 thomas #include "got_lib_object_cache.h"
43 097b408a 2022-10-17 thomas #include "got_lib_object_idset.h"
44 097b408a 2022-10-17 thomas #include "got_lib_privsep.h"
45 abd46894 2022-10-18 thomas #include "got_lib_ratelimit.h"
46 097b408a 2022-10-17 thomas #include "got_lib_pack.h"
47 097b408a 2022-10-17 thomas #include "got_lib_pack_create.h"
48 097b408a 2022-10-17 thomas #include "got_lib_repository.h"
49 097b408a 2022-10-17 thomas
50 097b408a 2022-10-17 thomas struct send_id_arg {
51 097b408a 2022-10-17 thomas struct imsgbuf *ibuf;
52 097b408a 2022-10-17 thomas struct got_object_id *ids[GOT_IMSG_OBJ_ID_LIST_MAX_NIDS];
53 097b408a 2022-10-17 thomas size_t nids;
54 097b408a 2022-10-17 thomas };
55 097b408a 2022-10-17 thomas
56 097b408a 2022-10-17 thomas static const struct got_error *
57 097b408a 2022-10-17 thomas send_id(struct got_object_id *id, void *data, void *arg)
58 097b408a 2022-10-17 thomas {
59 097b408a 2022-10-17 thomas const struct got_error *err = NULL;
60 097b408a 2022-10-17 thomas struct send_id_arg *a = arg;
61 097b408a 2022-10-17 thomas
62 097b408a 2022-10-17 thomas a->ids[a->nids++] = id;
63 097b408a 2022-10-17 thomas
64 097b408a 2022-10-17 thomas if (a->nids >= GOT_IMSG_OBJ_ID_LIST_MAX_NIDS) {
65 097b408a 2022-10-17 thomas err = got_privsep_send_object_idlist(a->ibuf, a->ids, a->nids);
66 097b408a 2022-10-17 thomas if (err)
67 097b408a 2022-10-17 thomas return err;
68 097b408a 2022-10-17 thomas a->nids = 0;
69 097b408a 2022-10-17 thomas }
70 097b408a 2022-10-17 thomas
71 097b408a 2022-10-17 thomas return NULL;
72 097b408a 2022-10-17 thomas }
73 097b408a 2022-10-17 thomas
74 097b408a 2022-10-17 thomas static const struct got_error *
75 097b408a 2022-10-17 thomas send_idset(struct imsgbuf *ibuf, struct got_object_idset *idset)
76 097b408a 2022-10-17 thomas {
77 097b408a 2022-10-17 thomas const struct got_error *err;
78 097b408a 2022-10-17 thomas struct send_id_arg sia;
79 097b408a 2022-10-17 thomas
80 097b408a 2022-10-17 thomas memset(&sia, 0, sizeof(sia));
81 097b408a 2022-10-17 thomas sia.ibuf = ibuf;
82 097b408a 2022-10-17 thomas err = got_object_idset_for_each(idset, send_id, &sia);
83 097b408a 2022-10-17 thomas if (err)
84 097b408a 2022-10-17 thomas return err;
85 097b408a 2022-10-17 thomas
86 097b408a 2022-10-17 thomas if (sia.nids > 0) {
87 097b408a 2022-10-17 thomas err = got_privsep_send_object_idlist(ibuf, sia.ids, sia.nids);
88 097b408a 2022-10-17 thomas if (err)
89 097b408a 2022-10-17 thomas return err;
90 097b408a 2022-10-17 thomas }
91 097b408a 2022-10-17 thomas
92 097b408a 2022-10-17 thomas return got_privsep_send_object_idlist_done(ibuf);
93 097b408a 2022-10-17 thomas }
94 097b408a 2022-10-17 thomas
95 097b408a 2022-10-17 thomas static const struct got_error *
96 097b408a 2022-10-17 thomas recv_reused_delta(struct got_imsg_reused_delta *delta,
97 097b408a 2022-10-17 thomas struct got_object_idset *idset, struct got_pack_metavec *v)
98 097b408a 2022-10-17 thomas {
99 097b408a 2022-10-17 thomas struct got_pack_meta *m, *base;
100 097b408a 2022-10-17 thomas
101 097b408a 2022-10-17 thomas if (delta->delta_offset + delta->delta_size < delta->delta_offset ||
102 097b408a 2022-10-17 thomas delta->delta_offset +
103 097b408a 2022-10-17 thomas delta->delta_compressed_size < delta->delta_offset)
104 097b408a 2022-10-17 thomas return got_error(GOT_ERR_BAD_PACKFILE);
105 097b408a 2022-10-17 thomas
106 097b408a 2022-10-17 thomas m = got_object_idset_get(idset, &delta->id);
107 097b408a 2022-10-17 thomas if (m == NULL)
108 097b408a 2022-10-17 thomas return got_error(GOT_ERR_NO_OBJ);
109 097b408a 2022-10-17 thomas
110 097b408a 2022-10-17 thomas base = got_object_idset_get(idset, &delta->base_id);
111 097b408a 2022-10-17 thomas if (base == NULL)
112 097b408a 2022-10-17 thomas return got_error(GOT_ERR_NO_OBJ);
113 097b408a 2022-10-17 thomas
114 097b408a 2022-10-17 thomas m->delta_len = delta->delta_size;
115 097b408a 2022-10-17 thomas m->delta_compressed_len = delta->delta_compressed_size;
116 c44c7d6e 2022-12-04 thomas m->delta_offset = 0;
117 097b408a 2022-10-17 thomas m->prev = base;
118 097b408a 2022-10-17 thomas m->size = delta->result_size;
119 097b408a 2022-10-17 thomas m->reused_delta_offset = delta->delta_offset;
120 097b408a 2022-10-17 thomas m->base_obj_id = got_object_id_dup(&delta->base_id);
121 097b408a 2022-10-17 thomas if (m->base_obj_id == NULL)
122 097b408a 2022-10-17 thomas return got_error_from_errno("got_object_id_dup");
123 097b408a 2022-10-17 thomas
124 097b408a 2022-10-17 thomas return got_pack_add_meta(m, v);
125 097b408a 2022-10-17 thomas }
126 097b408a 2022-10-17 thomas
127 097b408a 2022-10-17 thomas const struct got_error *
128 c44c7d6e 2022-12-04 thomas got_pack_search_deltas(struct got_packidx **packidx, struct got_pack **pack,
129 c44c7d6e 2022-12-04 thomas struct got_pack_metavec *v, struct got_object_idset *idset,
130 097b408a 2022-10-17 thomas int ncolored, int nfound, int ntrees, int ncommits,
131 097b408a 2022-10-17 thomas struct got_repository *repo,
132 097b408a 2022-10-17 thomas got_pack_progress_cb progress_cb, void *progress_arg,
133 097b408a 2022-10-17 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
134 097b408a 2022-10-17 thomas {
135 097b408a 2022-10-17 thomas const struct got_error *err = NULL;
136 097b408a 2022-10-17 thomas struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
137 097b408a 2022-10-17 thomas size_t ndeltas, i;
138 097b408a 2022-10-17 thomas
139 c44c7d6e 2022-12-04 thomas *packidx = NULL;
140 c44c7d6e 2022-12-04 thomas *pack = NULL;
141 097b408a 2022-10-17 thomas
142 c44c7d6e 2022-12-04 thomas err = got_pack_find_pack_for_reuse(packidx, repo);
143 c44c7d6e 2022-12-04 thomas if (err)
144 c44c7d6e 2022-12-04 thomas return err;
145 c44c7d6e 2022-12-04 thomas
146 c44c7d6e 2022-12-04 thomas if (*packidx == NULL)
147 097b408a 2022-10-17 thomas return NULL;
148 097b408a 2022-10-17 thomas
149 c44c7d6e 2022-12-04 thomas err = got_pack_cache_pack_for_packidx(pack, *packidx, repo);
150 097b408a 2022-10-17 thomas if (err)
151 c44c7d6e 2022-12-04 thomas goto done;
152 097b408a 2022-10-17 thomas
153 c44c7d6e 2022-12-04 thomas if ((*pack)->privsep_child == NULL) {
154 c44c7d6e 2022-12-04 thomas err = got_pack_start_privsep_child(*pack, *packidx);
155 097b408a 2022-10-17 thomas if (err)
156 c44c7d6e 2022-12-04 thomas goto done;
157 097b408a 2022-10-17 thomas }
158 097b408a 2022-10-17 thomas
159 c44c7d6e 2022-12-04 thomas err = got_privsep_send_delta_reuse_req((*pack)->privsep_child->ibuf);
160 097b408a 2022-10-17 thomas if (err)
161 c44c7d6e 2022-12-04 thomas goto done;
162 097b408a 2022-10-17 thomas
163 c44c7d6e 2022-12-04 thomas err = send_idset((*pack)->privsep_child->ibuf, idset);
164 097b408a 2022-10-17 thomas if (err)
165 c44c7d6e 2022-12-04 thomas goto done;
166 097b408a 2022-10-17 thomas
167 097b408a 2022-10-17 thomas for (;;) {
168 097b408a 2022-10-17 thomas int done = 0;
169 097b408a 2022-10-17 thomas
170 097b408a 2022-10-17 thomas if (cancel_cb) {
171 097b408a 2022-10-17 thomas err = (*cancel_cb)(cancel_arg);
172 097b408a 2022-10-17 thomas if (err)
173 097b408a 2022-10-17 thomas break;
174 097b408a 2022-10-17 thomas }
175 097b408a 2022-10-17 thomas
176 097b408a 2022-10-17 thomas err = got_privsep_recv_reused_deltas(&done, deltas, &ndeltas,
177 c44c7d6e 2022-12-04 thomas (*pack)->privsep_child->ibuf);
178 097b408a 2022-10-17 thomas if (err || done)
179 097b408a 2022-10-17 thomas break;
180 097b408a 2022-10-17 thomas
181 097b408a 2022-10-17 thomas for (i = 0; i < ndeltas; i++) {
182 097b408a 2022-10-17 thomas struct got_imsg_reused_delta *delta = &deltas[i];
183 097b408a 2022-10-17 thomas err = recv_reused_delta(delta, idset, v);
184 097b408a 2022-10-17 thomas if (err)
185 097b408a 2022-10-17 thomas goto done;
186 097b408a 2022-10-17 thomas }
187 097b408a 2022-10-17 thomas
188 097b408a 2022-10-17 thomas err = got_pack_report_progress(progress_cb, progress_arg, rl,
189 097b408a 2022-10-17 thomas ncolored, nfound, ntrees, 0L, ncommits,
190 097b408a 2022-10-17 thomas got_object_idset_num_elements(idset), v->nmeta, 0);
191 097b408a 2022-10-17 thomas if (err)
192 097b408a 2022-10-17 thomas break;
193 097b408a 2022-10-17 thomas }
194 097b408a 2022-10-17 thomas done:
195 097b408a 2022-10-17 thomas return err;
196 097b408a 2022-10-17 thomas }
197 097b408a 2022-10-17 thomas
198 097b408a 2022-10-17 thomas struct recv_painted_commit_arg {
199 097b408a 2022-10-17 thomas int *ncolored;
200 097b408a 2022-10-17 thomas int *nqueued;
201 097b408a 2022-10-17 thomas int *nskip;
202 097b408a 2022-10-17 thomas struct got_object_id_queue *ids;
203 097b408a 2022-10-17 thomas struct got_object_idset *keep;
204 097b408a 2022-10-17 thomas struct got_object_idset *drop;
205 097b408a 2022-10-17 thomas struct got_object_idset *skip;
206 097b408a 2022-10-17 thomas got_pack_progress_cb progress_cb;
207 097b408a 2022-10-17 thomas void *progress_arg;
208 097b408a 2022-10-17 thomas struct got_ratelimit *rl;
209 097b408a 2022-10-17 thomas got_cancel_cb cancel_cb;
210 097b408a 2022-10-17 thomas void *cancel_arg;
211 097b408a 2022-10-17 thomas };
212 097b408a 2022-10-17 thomas
213 097b408a 2022-10-17 thomas static const struct got_error *
214 097b408a 2022-10-17 thomas recv_painted_commit(void *arg, struct got_object_id *id, intptr_t color)
215 097b408a 2022-10-17 thomas {
216 097b408a 2022-10-17 thomas const struct got_error *err = NULL;
217 097b408a 2022-10-17 thomas struct recv_painted_commit_arg *a = arg;
218 097b408a 2022-10-17 thomas struct got_object_qid *qid, *tmp;
219 097b408a 2022-10-17 thomas
220 097b408a 2022-10-17 thomas if (a->cancel_cb) {
221 097b408a 2022-10-17 thomas err = a->cancel_cb(a->cancel_arg);
222 097b408a 2022-10-17 thomas if (err)
223 097b408a 2022-10-17 thomas return err;
224 097b408a 2022-10-17 thomas }
225 097b408a 2022-10-17 thomas
226 097b408a 2022-10-17 thomas switch (color) {
227 097b408a 2022-10-17 thomas case COLOR_KEEP:
228 097b408a 2022-10-17 thomas err = got_object_idset_add(a->keep, id, NULL);
229 097b408a 2022-10-17 thomas if (err)
230 097b408a 2022-10-17 thomas return err;
231 097b408a 2022-10-17 thomas (*a->ncolored)++;
232 097b408a 2022-10-17 thomas break;
233 097b408a 2022-10-17 thomas case COLOR_DROP:
234 097b408a 2022-10-17 thomas err = got_object_idset_add(a->drop, id, NULL);
235 097b408a 2022-10-17 thomas if (err)
236 097b408a 2022-10-17 thomas return err;
237 097b408a 2022-10-17 thomas (*a->ncolored)++;
238 097b408a 2022-10-17 thomas break;
239 097b408a 2022-10-17 thomas case COLOR_SKIP:
240 097b408a 2022-10-17 thomas err = got_object_idset_add(a->skip, id, NULL);
241 097b408a 2022-10-17 thomas if (err)
242 097b408a 2022-10-17 thomas return err;
243 097b408a 2022-10-17 thomas break;
244 097b408a 2022-10-17 thomas default:
245 097b408a 2022-10-17 thomas /* should not happen */
246 097b408a 2022-10-17 thomas return got_error_fmt(GOT_ERR_NOT_IMPL,
247 097b408a 2022-10-17 thomas "%s invalid commit color %"PRIdPTR, __func__, color);
248 097b408a 2022-10-17 thomas }
249 097b408a 2022-10-17 thomas
250 097b408a 2022-10-17 thomas STAILQ_FOREACH_SAFE(qid, a->ids, entry, tmp) {
251 097b408a 2022-10-17 thomas if (got_object_id_cmp(&qid->id, id) != 0)
252 097b408a 2022-10-17 thomas continue;
253 097b408a 2022-10-17 thomas STAILQ_REMOVE(a->ids, qid, got_object_qid, entry);
254 097b408a 2022-10-17 thomas color = (intptr_t)qid->data;
255 097b408a 2022-10-17 thomas got_object_qid_free(qid);
256 097b408a 2022-10-17 thomas (*a->nqueued)--;
257 097b408a 2022-10-17 thomas if (color == COLOR_SKIP)
258 097b408a 2022-10-17 thomas (*a->nskip)--;
259 097b408a 2022-10-17 thomas break;
260 097b408a 2022-10-17 thomas }
261 097b408a 2022-10-17 thomas
262 097b408a 2022-10-17 thomas return got_pack_report_progress(a->progress_cb, a->progress_arg, a->rl,
263 097b408a 2022-10-17 thomas *a->ncolored, 0, 0, 0L, 0, 0, 0, 0);
264 097b408a 2022-10-17 thomas }
265 097b408a 2022-10-17 thomas
266 097b408a 2022-10-17 thomas static const struct got_error *
267 097b408a 2022-10-17 thomas paint_packed_commits(struct got_pack *pack, struct got_object_id *id,
268 097b408a 2022-10-17 thomas int idx, intptr_t color, int *ncolored, int *nqueued, int *nskip,
269 097b408a 2022-10-17 thomas struct got_object_id_queue *ids,
270 097b408a 2022-10-17 thomas struct got_object_idset *keep, struct got_object_idset *drop,
271 097b408a 2022-10-17 thomas struct got_object_idset *skip, struct got_repository *repo,
272 097b408a 2022-10-17 thomas got_pack_progress_cb progress_cb, void *progress_arg,
273 097b408a 2022-10-17 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
274 097b408a 2022-10-17 thomas {
275 097b408a 2022-10-17 thomas const struct got_error *err = NULL;
276 097b408a 2022-10-17 thomas struct got_object_id_queue next_ids;
277 097b408a 2022-10-17 thomas struct got_object_qid *qid, *tmp;
278 097b408a 2022-10-17 thomas struct recv_painted_commit_arg arg;
279 097b408a 2022-10-17 thomas
280 097b408a 2022-10-17 thomas STAILQ_INIT(&next_ids);
281 097b408a 2022-10-17 thomas
282 097b408a 2022-10-17 thomas err = got_privsep_send_painting_request(pack->privsep_child->ibuf,
283 097b408a 2022-10-17 thomas idx, id, color);
284 097b408a 2022-10-17 thomas if (err)
285 097b408a 2022-10-17 thomas return err;
286 097b408a 2022-10-17 thomas
287 097b408a 2022-10-17 thomas arg.ncolored = ncolored;
288 097b408a 2022-10-17 thomas arg.nqueued = nqueued;
289 097b408a 2022-10-17 thomas arg.nskip = nskip;
290 097b408a 2022-10-17 thomas arg.ids = ids;
291 097b408a 2022-10-17 thomas arg.keep = keep;
292 097b408a 2022-10-17 thomas arg.drop = drop;
293 097b408a 2022-10-17 thomas arg.skip = skip;
294 097b408a 2022-10-17 thomas arg.progress_cb = progress_cb;
295 097b408a 2022-10-17 thomas arg.progress_arg = progress_arg;
296 097b408a 2022-10-17 thomas arg.rl = rl;
297 097b408a 2022-10-17 thomas arg.cancel_cb = cancel_cb;
298 097b408a 2022-10-17 thomas arg.cancel_arg = cancel_arg;
299 097b408a 2022-10-17 thomas err = got_privsep_recv_painted_commits(&next_ids,
300 097b408a 2022-10-17 thomas recv_painted_commit, &arg, pack->privsep_child->ibuf);
301 097b408a 2022-10-17 thomas if (err)
302 097b408a 2022-10-17 thomas return err;
303 097b408a 2022-10-17 thomas
304 097b408a 2022-10-17 thomas STAILQ_FOREACH_SAFE(qid, &next_ids, entry, tmp) {
305 097b408a 2022-10-17 thomas struct got_object_qid *old_id;
306 097b408a 2022-10-17 thomas intptr_t qcolor, ocolor;
307 097b408a 2022-10-17 thomas STAILQ_FOREACH(old_id, ids, entry) {
308 097b408a 2022-10-17 thomas if (got_object_id_cmp(&qid->id, &old_id->id))
309 097b408a 2022-10-17 thomas continue;
310 097b408a 2022-10-17 thomas qcolor = (intptr_t)qid->data;
311 097b408a 2022-10-17 thomas ocolor = (intptr_t)old_id->data;
312 097b408a 2022-10-17 thomas STAILQ_REMOVE(&next_ids, qid, got_object_qid, entry);
313 097b408a 2022-10-17 thomas got_object_qid_free(qid);
314 097b408a 2022-10-17 thomas qid = NULL;
315 097b408a 2022-10-17 thomas if (qcolor != ocolor) {
316 097b408a 2022-10-17 thomas got_pack_paint_commit(old_id, qcolor);
317 097b408a 2022-10-17 thomas if (ocolor == COLOR_SKIP)
318 097b408a 2022-10-17 thomas (*nskip)--;
319 097b408a 2022-10-17 thomas else if (qcolor == COLOR_SKIP)
320 097b408a 2022-10-17 thomas (*nskip)++;
321 097b408a 2022-10-17 thomas }
322 097b408a 2022-10-17 thomas break;
323 097b408a 2022-10-17 thomas }
324 097b408a 2022-10-17 thomas }
325 097b408a 2022-10-17 thomas while (!STAILQ_EMPTY(&next_ids)) {
326 097b408a 2022-10-17 thomas qid = STAILQ_FIRST(&next_ids);
327 097b408a 2022-10-17 thomas STAILQ_REMOVE_HEAD(&next_ids, entry);
328 097b408a 2022-10-17 thomas got_pack_paint_commit(qid, color);
329 097b408a 2022-10-17 thomas STAILQ_INSERT_TAIL(ids, qid, entry);
330 097b408a 2022-10-17 thomas (*nqueued)++;
331 097b408a 2022-10-17 thomas if (color == COLOR_SKIP)
332 097b408a 2022-10-17 thomas (*nskip)++;
333 097b408a 2022-10-17 thomas }
334 097b408a 2022-10-17 thomas
335 097b408a 2022-10-17 thomas return err;
336 097b408a 2022-10-17 thomas }
337 097b408a 2022-10-17 thomas
338 097b408a 2022-10-17 thomas const struct got_error *
339 097b408a 2022-10-17 thomas got_pack_paint_commits(int *ncolored, struct got_object_id_queue *ids, int nids,
340 097b408a 2022-10-17 thomas struct got_object_idset *keep, struct got_object_idset *drop,
341 097b408a 2022-10-17 thomas struct got_object_idset *skip, struct got_repository *repo,
342 097b408a 2022-10-17 thomas got_pack_progress_cb progress_cb, void *progress_arg,
343 097b408a 2022-10-17 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
344 097b408a 2022-10-17 thomas {
345 097b408a 2022-10-17 thomas const struct got_error *err = NULL;
346 097b408a 2022-10-17 thomas struct got_commit_object *commit = NULL;
347 097b408a 2022-10-17 thomas struct got_packidx *packidx = NULL;
348 097b408a 2022-10-17 thomas struct got_pack *pack = NULL;
349 097b408a 2022-10-17 thomas const struct got_object_id_queue *parents;
350 097b408a 2022-10-17 thomas struct got_object_qid *qid = NULL;
351 097b408a 2022-10-17 thomas int nqueued = nids, nskip = 0;
352 097b408a 2022-10-17 thomas int idx;
353 097b408a 2022-10-17 thomas
354 097b408a 2022-10-17 thomas while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
355 097b408a 2022-10-17 thomas intptr_t color;
356 097b408a 2022-10-17 thomas
357 097b408a 2022-10-17 thomas if (cancel_cb) {
358 097b408a 2022-10-17 thomas err = cancel_cb(cancel_arg);
359 097b408a 2022-10-17 thomas if (err)
360 097b408a 2022-10-17 thomas break;
361 097b408a 2022-10-17 thomas }
362 097b408a 2022-10-17 thomas
363 097b408a 2022-10-17 thomas qid = STAILQ_FIRST(ids);
364 097b408a 2022-10-17 thomas STAILQ_REMOVE_HEAD(ids, entry);
365 097b408a 2022-10-17 thomas nqueued--;
366 097b408a 2022-10-17 thomas color = (intptr_t)qid->data;
367 097b408a 2022-10-17 thomas if (color == COLOR_SKIP)
368 097b408a 2022-10-17 thomas nskip--;
369 097b408a 2022-10-17 thomas
370 097b408a 2022-10-17 thomas if (got_object_idset_contains(skip, &qid->id)) {
371 097b408a 2022-10-17 thomas got_object_qid_free(qid);
372 097b408a 2022-10-17 thomas qid = NULL;
373 097b408a 2022-10-17 thomas continue;
374 097b408a 2022-10-17 thomas }
375 097b408a 2022-10-17 thomas if (color == COLOR_KEEP &&
376 097b408a 2022-10-17 thomas got_object_idset_contains(keep, &qid->id)) {
377 097b408a 2022-10-17 thomas got_object_qid_free(qid);
378 097b408a 2022-10-17 thomas qid = NULL;
379 097b408a 2022-10-17 thomas continue;
380 097b408a 2022-10-17 thomas }
381 097b408a 2022-10-17 thomas if (color == COLOR_DROP &&
382 097b408a 2022-10-17 thomas got_object_idset_contains(drop, &qid->id)) {
383 097b408a 2022-10-17 thomas got_object_qid_free(qid);
384 097b408a 2022-10-17 thomas qid = NULL;
385 097b408a 2022-10-17 thomas continue;
386 097b408a 2022-10-17 thomas }
387 097b408a 2022-10-17 thomas
388 097b408a 2022-10-17 thomas /* Pinned pack may have moved to different cache slot. */
389 097b408a 2022-10-17 thomas pack = got_repo_get_pinned_pack(repo);
390 097b408a 2022-10-17 thomas
391 097b408a 2022-10-17 thomas if (packidx && pack) {
392 097b408a 2022-10-17 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
393 097b408a 2022-10-17 thomas if (idx != -1) {
394 097b408a 2022-10-17 thomas err = paint_packed_commits(pack, &qid->id,
395 097b408a 2022-10-17 thomas idx, color, ncolored, &nqueued, &nskip,
396 097b408a 2022-10-17 thomas ids, keep, drop, skip, repo,
397 097b408a 2022-10-17 thomas progress_cb, progress_arg, rl,
398 097b408a 2022-10-17 thomas cancel_cb, cancel_arg);
399 097b408a 2022-10-17 thomas if (err)
400 097b408a 2022-10-17 thomas break;
401 097b408a 2022-10-17 thomas got_object_qid_free(qid);
402 097b408a 2022-10-17 thomas qid = NULL;
403 097b408a 2022-10-17 thomas continue;
404 097b408a 2022-10-17 thomas }
405 097b408a 2022-10-17 thomas }
406 097b408a 2022-10-17 thomas
407 097b408a 2022-10-17 thomas switch (color) {
408 097b408a 2022-10-17 thomas case COLOR_KEEP:
409 097b408a 2022-10-17 thomas if (got_object_idset_contains(drop, &qid->id)) {
410 097b408a 2022-10-17 thomas err = got_pack_paint_commit(qid, COLOR_SKIP);
411 097b408a 2022-10-17 thomas if (err)
412 097b408a 2022-10-17 thomas goto done;
413 097b408a 2022-10-17 thomas } else
414 097b408a 2022-10-17 thomas (*ncolored)++;
415 097b408a 2022-10-17 thomas err = got_object_idset_add(keep, &qid->id, NULL);
416 097b408a 2022-10-17 thomas if (err)
417 097b408a 2022-10-17 thomas goto done;
418 097b408a 2022-10-17 thomas break;
419 097b408a 2022-10-17 thomas case COLOR_DROP:
420 097b408a 2022-10-17 thomas if (got_object_idset_contains(keep, &qid->id)) {
421 097b408a 2022-10-17 thomas err = got_pack_paint_commit(qid, COLOR_SKIP);
422 097b408a 2022-10-17 thomas if (err)
423 097b408a 2022-10-17 thomas goto done;
424 097b408a 2022-10-17 thomas } else
425 097b408a 2022-10-17 thomas (*ncolored)++;
426 097b408a 2022-10-17 thomas err = got_object_idset_add(drop, &qid->id, NULL);
427 097b408a 2022-10-17 thomas if (err)
428 097b408a 2022-10-17 thomas goto done;
429 097b408a 2022-10-17 thomas break;
430 097b408a 2022-10-17 thomas case COLOR_SKIP:
431 097b408a 2022-10-17 thomas if (!got_object_idset_contains(skip, &qid->id)) {
432 097b408a 2022-10-17 thomas err = got_object_idset_add(skip, &qid->id,
433 097b408a 2022-10-17 thomas NULL);
434 097b408a 2022-10-17 thomas if (err)
435 097b408a 2022-10-17 thomas goto done;
436 097b408a 2022-10-17 thomas }
437 097b408a 2022-10-17 thomas break;
438 097b408a 2022-10-17 thomas default:
439 097b408a 2022-10-17 thomas /* should not happen */
440 097b408a 2022-10-17 thomas err = got_error_fmt(GOT_ERR_NOT_IMPL,
441 097b408a 2022-10-17 thomas "%s invalid commit color %"PRIdPTR, __func__,
442 097b408a 2022-10-17 thomas color);
443 097b408a 2022-10-17 thomas goto done;
444 097b408a 2022-10-17 thomas }
445 097b408a 2022-10-17 thomas
446 097b408a 2022-10-17 thomas err = got_pack_report_progress(progress_cb, progress_arg, rl,
447 097b408a 2022-10-17 thomas *ncolored, 0, 0, 0L, 0, 0, 0, 0);
448 097b408a 2022-10-17 thomas if (err)
449 097b408a 2022-10-17 thomas break;
450 097b408a 2022-10-17 thomas
451 097b408a 2022-10-17 thomas err = got_object_open_as_commit(&commit, repo, &qid->id);
452 097b408a 2022-10-17 thomas if (err)
453 097b408a 2022-10-17 thomas break;
454 097b408a 2022-10-17 thomas
455 097b408a 2022-10-17 thomas parents = got_object_commit_get_parent_ids(commit);
456 097b408a 2022-10-17 thomas if (parents) {
457 097b408a 2022-10-17 thomas struct got_object_qid *pid;
458 097b408a 2022-10-17 thomas color = (intptr_t)qid->data;
459 097b408a 2022-10-17 thomas STAILQ_FOREACH(pid, parents, entry) {
460 097b408a 2022-10-17 thomas err = got_pack_queue_commit_id(ids, &pid->id,
461 097b408a 2022-10-17 thomas color, repo);
462 097b408a 2022-10-17 thomas if (err)
463 097b408a 2022-10-17 thomas break;
464 097b408a 2022-10-17 thomas nqueued++;
465 097b408a 2022-10-17 thomas if (color == COLOR_SKIP)
466 097b408a 2022-10-17 thomas nskip++;
467 097b408a 2022-10-17 thomas }
468 097b408a 2022-10-17 thomas }
469 097b408a 2022-10-17 thomas
470 097b408a 2022-10-17 thomas if (pack == NULL && (commit->flags & GOT_COMMIT_FLAG_PACKED)) {
471 097b408a 2022-10-17 thomas if (packidx == NULL) {
472 097b408a 2022-10-17 thomas err = got_pack_find_pack_for_commit_painting(
473 097b408a 2022-10-17 thomas &packidx, ids, nqueued, repo);
474 097b408a 2022-10-17 thomas if (err)
475 097b408a 2022-10-17 thomas goto done;
476 097b408a 2022-10-17 thomas }
477 097b408a 2022-10-17 thomas if (packidx != NULL) {
478 097b408a 2022-10-17 thomas err = got_pack_cache_pack_for_packidx(&pack,
479 097b408a 2022-10-17 thomas packidx, repo);
480 097b408a 2022-10-17 thomas if (err)
481 097b408a 2022-10-17 thomas goto done;
482 097b408a 2022-10-17 thomas if (pack->privsep_child == NULL) {
483 097b408a 2022-10-17 thomas err = got_pack_start_privsep_child(
484 097b408a 2022-10-17 thomas pack, packidx);
485 097b408a 2022-10-17 thomas if (err)
486 097b408a 2022-10-17 thomas goto done;
487 097b408a 2022-10-17 thomas }
488 097b408a 2022-10-17 thomas err = got_privsep_init_commit_painting(
489 097b408a 2022-10-17 thomas pack->privsep_child->ibuf);
490 097b408a 2022-10-17 thomas if (err)
491 097b408a 2022-10-17 thomas goto done;
492 097b408a 2022-10-17 thomas err = send_idset(pack->privsep_child->ibuf,
493 097b408a 2022-10-17 thomas keep);
494 097b408a 2022-10-17 thomas if (err)
495 097b408a 2022-10-17 thomas goto done;
496 097b408a 2022-10-17 thomas err = send_idset(pack->privsep_child->ibuf, drop);
497 097b408a 2022-10-17 thomas if (err)
498 097b408a 2022-10-17 thomas goto done;
499 097b408a 2022-10-17 thomas err = send_idset(pack->privsep_child->ibuf, skip);
500 097b408a 2022-10-17 thomas if (err)
501 097b408a 2022-10-17 thomas goto done;
502 097b408a 2022-10-17 thomas err = got_repo_pin_pack(repo, packidx, pack);
503 097b408a 2022-10-17 thomas if (err)
504 097b408a 2022-10-17 thomas goto done;
505 097b408a 2022-10-17 thomas }
506 097b408a 2022-10-17 thomas }
507 097b408a 2022-10-17 thomas
508 097b408a 2022-10-17 thomas got_object_commit_close(commit);
509 097b408a 2022-10-17 thomas commit = NULL;
510 097b408a 2022-10-17 thomas
511 097b408a 2022-10-17 thomas got_object_qid_free(qid);
512 097b408a 2022-10-17 thomas qid = NULL;
513 097b408a 2022-10-17 thomas }
514 097b408a 2022-10-17 thomas done:
515 097b408a 2022-10-17 thomas if (pack) {
516 097b408a 2022-10-17 thomas const struct got_error *pack_err;
517 097b408a 2022-10-17 thomas pack_err = got_privsep_send_painting_commits_done(
518 097b408a 2022-10-17 thomas pack->privsep_child->ibuf);
519 097b408a 2022-10-17 thomas if (err == NULL)
520 097b408a 2022-10-17 thomas err = pack_err;
521 097b408a 2022-10-17 thomas }
522 097b408a 2022-10-17 thomas if (commit)
523 097b408a 2022-10-17 thomas got_object_commit_close(commit);
524 097b408a 2022-10-17 thomas got_object_qid_free(qid);
525 097b408a 2022-10-17 thomas got_repo_unpin_pack(repo);
526 097b408a 2022-10-17 thomas return err;
527 097b408a 2022-10-17 thomas }
528 097b408a 2022-10-17 thomas
529 097b408a 2022-10-17 thomas struct load_packed_obj_arg {
530 097b408a 2022-10-17 thomas /* output parameters: */
531 097b408a 2022-10-17 thomas struct got_object_id *id;
532 097b408a 2022-10-17 thomas char *dpath;
533 097b408a 2022-10-17 thomas time_t mtime;
534 097b408a 2022-10-17 thomas
535 097b408a 2022-10-17 thomas /* input parameters: */
536 097b408a 2022-10-17 thomas uint32_t seed;
537 097b408a 2022-10-17 thomas int want_meta;
538 097b408a 2022-10-17 thomas struct got_object_idset *idset;
539 097b408a 2022-10-17 thomas struct got_object_idset *idset_exclude;
540 097b408a 2022-10-17 thomas int loose_obj_only;
541 097b408a 2022-10-17 thomas int *ncolored;
542 097b408a 2022-10-17 thomas int *nfound;
543 097b408a 2022-10-17 thomas int *ntrees;
544 097b408a 2022-10-17 thomas got_pack_progress_cb progress_cb;
545 097b408a 2022-10-17 thomas void *progress_arg;
546 097b408a 2022-10-17 thomas struct got_ratelimit *rl;
547 097b408a 2022-10-17 thomas got_cancel_cb cancel_cb;
548 097b408a 2022-10-17 thomas void *cancel_arg;
549 097b408a 2022-10-17 thomas };
550 097b408a 2022-10-17 thomas
551 097b408a 2022-10-17 thomas static const struct got_error *
552 097b408a 2022-10-17 thomas load_packed_commit_id(void *arg, time_t mtime, struct got_object_id *id,
553 097b408a 2022-10-17 thomas struct got_repository *repo)
554 097b408a 2022-10-17 thomas {
555 097b408a 2022-10-17 thomas struct load_packed_obj_arg *a = arg;
556 097b408a 2022-10-17 thomas
557 097b408a 2022-10-17 thomas if (got_object_idset_contains(a->idset, id) ||
558 097b408a 2022-10-17 thomas got_object_idset_contains(a->idset_exclude, id))
559 097b408a 2022-10-17 thomas return NULL;
560 097b408a 2022-10-17 thomas
561 097b408a 2022-10-17 thomas return got_pack_add_object(a->want_meta,
562 097b408a 2022-10-17 thomas a->want_meta ? a->idset : a->idset_exclude,
563 097b408a 2022-10-17 thomas id, "", GOT_OBJ_TYPE_COMMIT, mtime, a->seed, a->loose_obj_only,
564 097b408a 2022-10-17 thomas repo, a->ncolored, a->nfound, a->ntrees,
565 097b408a 2022-10-17 thomas a->progress_cb, a->progress_arg, a->rl);
566 097b408a 2022-10-17 thomas }
567 097b408a 2022-10-17 thomas
568 097b408a 2022-10-17 thomas static const struct got_error *
569 097b408a 2022-10-17 thomas load_packed_tree_ids(void *arg, struct got_tree_object *tree, time_t mtime,
570 097b408a 2022-10-17 thomas struct got_object_id *id, const char *dpath, struct got_repository *repo)
571 097b408a 2022-10-17 thomas {
572 097b408a 2022-10-17 thomas const struct got_error *err;
573 097b408a 2022-10-17 thomas struct load_packed_obj_arg *a = arg;
574 097b408a 2022-10-17 thomas const char *relpath;
575 097b408a 2022-10-17 thomas
576 097b408a 2022-10-17 thomas /*
577 097b408a 2022-10-17 thomas * When we receive a tree's ID and path but not the tree itself,
578 097b408a 2022-10-17 thomas * this tree object was not found in the pack file. This is the
579 097b408a 2022-10-17 thomas * last time we are being called for this optimized traversal.
580 097b408a 2022-10-17 thomas * Return from here and switch to loading objects the slow way.
581 097b408a 2022-10-17 thomas */
582 097b408a 2022-10-17 thomas if (tree == NULL) {
583 097b408a 2022-10-17 thomas free(a->id);
584 097b408a 2022-10-17 thomas a->id = got_object_id_dup(id);
585 097b408a 2022-10-17 thomas if (a->id == NULL) {
586 097b408a 2022-10-17 thomas err = got_error_from_errno("got_object_id_dup");
587 097b408a 2022-10-17 thomas free(a->dpath);
588 097b408a 2022-10-17 thomas a->dpath = NULL;
589 097b408a 2022-10-17 thomas return err;
590 097b408a 2022-10-17 thomas }
591 097b408a 2022-10-17 thomas
592 097b408a 2022-10-17 thomas free(a->dpath);
593 097b408a 2022-10-17 thomas a->dpath = strdup(dpath);
594 097b408a 2022-10-17 thomas if (a->dpath == NULL) {
595 097b408a 2022-10-17 thomas err = got_error_from_errno("strdup");
596 097b408a 2022-10-17 thomas free(a->id);
597 097b408a 2022-10-17 thomas a->id = NULL;
598 097b408a 2022-10-17 thomas return err;
599 097b408a 2022-10-17 thomas }
600 097b408a 2022-10-17 thomas
601 097b408a 2022-10-17 thomas a->mtime = mtime;
602 097b408a 2022-10-17 thomas return NULL;
603 097b408a 2022-10-17 thomas }
604 097b408a 2022-10-17 thomas
605 097b408a 2022-10-17 thomas if (got_object_idset_contains(a->idset, id) ||
606 097b408a 2022-10-17 thomas got_object_idset_contains(a->idset_exclude, id))
607 097b408a 2022-10-17 thomas return NULL;
608 097b408a 2022-10-17 thomas
609 097b408a 2022-10-17 thomas relpath = dpath;
610 097b408a 2022-10-17 thomas while (relpath[0] == '/')
611 097b408a 2022-10-17 thomas relpath++;
612 097b408a 2022-10-17 thomas
613 097b408a 2022-10-17 thomas err = got_pack_add_object(a->want_meta,
614 097b408a 2022-10-17 thomas a->want_meta ? a->idset : a->idset_exclude,
615 097b408a 2022-10-17 thomas id, relpath, GOT_OBJ_TYPE_TREE, mtime, a->seed,
616 097b408a 2022-10-17 thomas a->loose_obj_only, repo, a->ncolored, a->nfound, a->ntrees,
617 097b408a 2022-10-17 thomas a->progress_cb, a->progress_arg, a->rl);
618 097b408a 2022-10-17 thomas if (err)
619 097b408a 2022-10-17 thomas return err;
620 097b408a 2022-10-17 thomas
621 097b408a 2022-10-17 thomas return got_pack_load_tree_entries(NULL, a->want_meta, a->idset,
622 097b408a 2022-10-17 thomas a->idset_exclude, tree, dpath, mtime, a->seed, repo,
623 097b408a 2022-10-17 thomas a->loose_obj_only, a->ncolored, a->nfound, a->ntrees,
624 097b408a 2022-10-17 thomas a->progress_cb, a->progress_arg, a->rl,
625 097b408a 2022-10-17 thomas a->cancel_cb, a->cancel_arg);
626 097b408a 2022-10-17 thomas }
627 097b408a 2022-10-17 thomas
628 097b408a 2022-10-17 thomas const struct got_error *
629 097b408a 2022-10-17 thomas got_pack_load_packed_object_ids(int *found_all_objects,
630 097b408a 2022-10-17 thomas struct got_object_id **ours, int nours,
631 097b408a 2022-10-17 thomas struct got_object_id **theirs, int ntheirs,
632 097b408a 2022-10-17 thomas int want_meta, uint32_t seed, struct got_object_idset *idset,
633 097b408a 2022-10-17 thomas struct got_object_idset *idset_exclude, int loose_obj_only,
634 097b408a 2022-10-17 thomas struct got_repository *repo, struct got_packidx *packidx,
635 097b408a 2022-10-17 thomas int *ncolored, int *nfound, int *ntrees,
636 097b408a 2022-10-17 thomas got_pack_progress_cb progress_cb, void *progress_arg,
637 097b408a 2022-10-17 thomas struct got_ratelimit *rl, got_cancel_cb cancel_cb, void *cancel_arg)
638 097b408a 2022-10-17 thomas {
639 097b408a 2022-10-17 thomas const struct got_error *err = NULL;
640 097b408a 2022-10-17 thomas struct load_packed_obj_arg lpa;
641 097b408a 2022-10-17 thomas
642 097b408a 2022-10-17 thomas memset(&lpa, 0, sizeof(lpa));
643 097b408a 2022-10-17 thomas lpa.seed = seed;
644 097b408a 2022-10-17 thomas lpa.want_meta = want_meta;
645 097b408a 2022-10-17 thomas lpa.idset = idset;
646 097b408a 2022-10-17 thomas lpa.idset_exclude = idset_exclude;
647 097b408a 2022-10-17 thomas lpa.loose_obj_only = loose_obj_only;
648 097b408a 2022-10-17 thomas lpa.ncolored = ncolored;
649 097b408a 2022-10-17 thomas lpa.nfound = nfound;
650 097b408a 2022-10-17 thomas lpa.ntrees = ntrees;
651 097b408a 2022-10-17 thomas lpa.progress_cb = progress_cb;
652 097b408a 2022-10-17 thomas lpa.progress_arg = progress_arg;
653 097b408a 2022-10-17 thomas lpa.rl = rl;
654 097b408a 2022-10-17 thomas lpa.cancel_cb = cancel_cb;
655 097b408a 2022-10-17 thomas lpa.cancel_arg = cancel_arg;
656 097b408a 2022-10-17 thomas
657 097b408a 2022-10-17 thomas /* Attempt to load objects via got-read-pack, as far as possible. */
658 097b408a 2022-10-17 thomas err = got_object_enumerate(found_all_objects, load_packed_commit_id,
659 097b408a 2022-10-17 thomas load_packed_tree_ids, &lpa, ours, nours, theirs, ntheirs,
660 097b408a 2022-10-17 thomas packidx, repo);
661 097b408a 2022-10-17 thomas if (err)
662 097b408a 2022-10-17 thomas return err;
663 097b408a 2022-10-17 thomas
664 097b408a 2022-10-17 thomas if (lpa.id == NULL)
665 097b408a 2022-10-17 thomas return NULL;
666 097b408a 2022-10-17 thomas
667 097b408a 2022-10-17 thomas /*
668 097b408a 2022-10-17 thomas * An incomplete tree hierarchy was present in the pack file
669 097b408a 2022-10-17 thomas * and caused loading to be aborted.
670 097b408a 2022-10-17 thomas * Continue loading trees the slow way.
671 097b408a 2022-10-17 thomas */
672 097b408a 2022-10-17 thomas err = got_pack_load_tree(want_meta, idset, idset_exclude,
673 097b408a 2022-10-17 thomas lpa.id, lpa.dpath, lpa.mtime, seed, repo, loose_obj_only,
674 097b408a 2022-10-17 thomas ncolored, nfound, ntrees, progress_cb, progress_arg, rl,
675 097b408a 2022-10-17 thomas cancel_cb, cancel_arg);
676 097b408a 2022-10-17 thomas free(lpa.id);
677 097b408a 2022-10-17 thomas free(lpa.dpath);
678 097b408a 2022-10-17 thomas return err;
679 097b408a 2022-10-17 thomas }