Blame


1 876c234b 2018-09-10 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 876c234b 2018-09-10 stsp *
4 876c234b 2018-09-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 876c234b 2018-09-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 876c234b 2018-09-10 stsp * copyright notice and this permission notice appear in all copies.
7 876c234b 2018-09-10 stsp *
8 876c234b 2018-09-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 876c234b 2018-09-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 876c234b 2018-09-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 876c234b 2018-09-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 876c234b 2018-09-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 876c234b 2018-09-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 876c234b 2018-09-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 876c234b 2018-09-10 stsp */
16 876c234b 2018-09-10 stsp
17 0ab4c957 2022-06-13 stsp #include <sys/stat.h>
18 876c234b 2018-09-10 stsp #include <sys/types.h>
19 876c234b 2018-09-10 stsp #include <sys/queue.h>
20 876c234b 2018-09-10 stsp #include <sys/uio.h>
21 876c234b 2018-09-10 stsp #include <sys/time.h>
22 876c234b 2018-09-10 stsp #include <sys/mman.h>
23 876c234b 2018-09-10 stsp
24 756050ac 2022-07-19 op #include <inttypes.h>
25 876c234b 2018-09-10 stsp #include <limits.h>
26 99437157 2018-11-11 stsp #include <signal.h>
27 876c234b 2018-09-10 stsp #include <stdint.h>
28 876c234b 2018-09-10 stsp #include <imsg.h>
29 876c234b 2018-09-10 stsp #include <stdio.h>
30 876c234b 2018-09-10 stsp #include <stdlib.h>
31 876c234b 2018-09-10 stsp #include <string.h>
32 876c234b 2018-09-10 stsp #include <sha1.h>
33 5822e79e 2023-02-23 op #include <sha2.h>
34 81a12da5 2020-09-09 naddy #include <unistd.h>
35 876c234b 2018-09-10 stsp #include <zlib.h>
36 876c234b 2018-09-10 stsp
37 876c234b 2018-09-10 stsp #include "got_error.h"
38 876c234b 2018-09-10 stsp #include "got_object.h"
39 3022d272 2019-11-14 stsp #include "got_path.h"
40 876c234b 2018-09-10 stsp
41 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
42 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
43 876c234b 2018-09-10 stsp #include "got_lib_object.h"
44 2f43cd69 2023-04-14 stsp #include "got_lib_object_qid.h"
45 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
46 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
47 fae7e038 2022-05-07 stsp #include "got_lib_object_idset.h"
48 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
49 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
50 61af9b21 2022-06-28 stsp
51 61af9b21 2022-06-28 stsp #ifndef nitems
52 61af9b21 2022-06-28 stsp #define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
53 61af9b21 2022-06-28 stsp #endif
54 876c234b 2018-09-10 stsp
55 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
56 99437157 2018-11-11 stsp
57 99437157 2018-11-11 stsp static void
58 99437157 2018-11-11 stsp catch_sigint(int signo)
59 99437157 2018-11-11 stsp {
60 99437157 2018-11-11 stsp sigint_received = 1;
61 99437157 2018-11-11 stsp }
62 99437157 2018-11-11 stsp
63 876c234b 2018-09-10 stsp static const struct got_error *
64 704b89c4 2019-05-23 stsp open_object(struct got_object **obj, struct got_pack *pack,
65 704b89c4 2019-05-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id,
66 704b89c4 2019-05-23 stsp struct got_object_cache *objcache)
67 704b89c4 2019-05-23 stsp {
68 704b89c4 2019-05-23 stsp const struct got_error *err;
69 704b89c4 2019-05-23 stsp
70 704b89c4 2019-05-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
71 704b89c4 2019-05-23 stsp if (err)
72 704b89c4 2019-05-23 stsp return err;
73 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
74 704b89c4 2019-05-23 stsp
75 704b89c4 2019-05-23 stsp err = got_object_cache_add(objcache, id, *obj);
76 79c99a64 2019-05-23 stsp if (err) {
77 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
78 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
79 79c99a64 2019-05-23 stsp err = NULL;
80 704b89c4 2019-05-23 stsp return err;
81 79c99a64 2019-05-23 stsp }
82 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
83 704b89c4 2019-05-23 stsp return NULL;
84 704b89c4 2019-05-23 stsp }
85 704b89c4 2019-05-23 stsp
86 704b89c4 2019-05-23 stsp static const struct got_error *
87 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
88 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
89 876c234b 2018-09-10 stsp {
90 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
91 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
92 876c234b 2018-09-10 stsp struct got_object *obj;
93 106807b4 2018-09-15 stsp struct got_object_id id;
94 876c234b 2018-09-10 stsp size_t datalen;
95 876c234b 2018-09-10 stsp
96 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
97 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
98 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
99 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
100 265df21f 2023-01-31 op memcpy(&id, &iobj.id, sizeof(id));
101 876c234b 2018-09-10 stsp
102 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
103 704b89c4 2019-05-23 stsp if (obj) {
104 704b89c4 2019-05-23 stsp obj->refcnt++;
105 704b89c4 2019-05-23 stsp } else {
106 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
107 704b89c4 2019-05-23 stsp objcache);
108 704b89c4 2019-05-23 stsp if (err)
109 704b89c4 2019-05-23 stsp goto done;
110 704b89c4 2019-05-23 stsp }
111 876c234b 2018-09-10 stsp
112 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
113 c59b3346 2018-09-11 stsp done:
114 876c234b 2018-09-10 stsp got_object_close(obj);
115 876c234b 2018-09-10 stsp return err;
116 876c234b 2018-09-10 stsp }
117 876c234b 2018-09-10 stsp
118 50127d69 2021-09-25 stsp static const struct got_error *
119 ca6e02ac 2020-01-07 stsp open_commit(struct got_commit_object **commit, struct got_pack *pack,
120 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
121 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
122 876c234b 2018-09-10 stsp {
123 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
124 cb5e38fd 2019-05-23 stsp struct got_object *obj = NULL;
125 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
126 cfd633c2 2018-09-10 stsp size_t len;
127 cfd633c2 2018-09-10 stsp
128 ca6e02ac 2020-01-07 stsp *commit = NULL;
129 1785f84a 2018-12-23 stsp
130 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
131 704b89c4 2019-05-23 stsp if (obj) {
132 704b89c4 2019-05-23 stsp obj->refcnt++;
133 704b89c4 2019-05-23 stsp } else {
134 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
135 704b89c4 2019-05-23 stsp objcache);
136 704b89c4 2019-05-23 stsp if (err)
137 704b89c4 2019-05-23 stsp return err;
138 704b89c4 2019-05-23 stsp }
139 cfd633c2 2018-09-10 stsp
140 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
141 cfd633c2 2018-09-10 stsp if (err)
142 cb5e38fd 2019-05-23 stsp goto done;
143 cfd633c2 2018-09-10 stsp
144 cfd633c2 2018-09-10 stsp obj->size = len;
145 ca6e02ac 2020-01-07 stsp
146 ca6e02ac 2020-01-07 stsp err = got_object_parse_commit(commit, buf, len);
147 ca6e02ac 2020-01-07 stsp done:
148 ca6e02ac 2020-01-07 stsp got_object_close(obj);
149 ca6e02ac 2020-01-07 stsp free(buf);
150 ca6e02ac 2020-01-07 stsp return err;
151 ca6e02ac 2020-01-07 stsp }
152 ca6e02ac 2020-01-07 stsp
153 ca6e02ac 2020-01-07 stsp static const struct got_error *
154 ca6e02ac 2020-01-07 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
155 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
156 ca6e02ac 2020-01-07 stsp {
157 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
158 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
159 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL;
160 ca6e02ac 2020-01-07 stsp struct got_object_id id;
161 ca6e02ac 2020-01-07 stsp size_t datalen;
162 ca6e02ac 2020-01-07 stsp
163 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
164 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(iobj))
165 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
166 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
167 265df21f 2023-01-31 op memcpy(&id, &iobj.id, sizeof(id));
168 ca6e02ac 2020-01-07 stsp
169 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
170 cb5e38fd 2019-05-23 stsp if (err)
171 cb5e38fd 2019-05-23 stsp goto done;
172 cfd633c2 2018-09-10 stsp
173 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
174 cb5e38fd 2019-05-23 stsp done:
175 cb5e38fd 2019-05-23 stsp if (commit)
176 cb5e38fd 2019-05-23 stsp got_object_commit_close(commit);
177 7762fe12 2018-11-05 stsp if (err) {
178 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
179 7762fe12 2018-11-05 stsp err = NULL;
180 7762fe12 2018-11-05 stsp else
181 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
182 7762fe12 2018-11-05 stsp }
183 7762fe12 2018-11-05 stsp
184 7762fe12 2018-11-05 stsp return err;
185 7762fe12 2018-11-05 stsp }
186 7762fe12 2018-11-05 stsp
187 50127d69 2021-09-25 stsp static const struct got_error *
188 cdfe5501 2023-04-24 stsp open_tree(uint8_t **buf, size_t *len,
189 cdfe5501 2023-04-24 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
190 cdfe5501 2023-04-24 stsp struct got_object_id *id, struct got_object_cache *objcache)
191 ca6e02ac 2020-01-07 stsp {
192 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
193 ca6e02ac 2020-01-07 stsp struct got_object *obj = NULL;
194 cdfe5501 2023-04-24 stsp int cached = 0;
195 ca6e02ac 2020-01-07 stsp
196 ca6e02ac 2020-01-07 stsp *buf = NULL;
197 cdfe5501 2023-04-24 stsp *len = 0;
198 ca6e02ac 2020-01-07 stsp
199 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
200 ca6e02ac 2020-01-07 stsp if (obj) {
201 ca6e02ac 2020-01-07 stsp obj->refcnt++;
202 cdfe5501 2023-04-24 stsp cached = 1;
203 ca6e02ac 2020-01-07 stsp } else {
204 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
205 ca6e02ac 2020-01-07 stsp objcache);
206 ca6e02ac 2020-01-07 stsp if (err)
207 ca6e02ac 2020-01-07 stsp return err;
208 ca6e02ac 2020-01-07 stsp }
209 ca6e02ac 2020-01-07 stsp
210 cdfe5501 2023-04-24 stsp err = got_packfile_extract_object_to_mem(buf, len, obj, pack);
211 ca6e02ac 2020-01-07 stsp if (err)
212 ca6e02ac 2020-01-07 stsp goto done;
213 ca6e02ac 2020-01-07 stsp
214 cdfe5501 2023-04-24 stsp if (!cached)
215 cdfe5501 2023-04-24 stsp obj->size = *len;
216 ca6e02ac 2020-01-07 stsp done:
217 ca6e02ac 2020-01-07 stsp got_object_close(obj);
218 ca6e02ac 2020-01-07 stsp if (err) {
219 ca6e02ac 2020-01-07 stsp free(*buf);
220 ca6e02ac 2020-01-07 stsp *buf = NULL;
221 ca6e02ac 2020-01-07 stsp }
222 ca6e02ac 2020-01-07 stsp return err;
223 ca6e02ac 2020-01-07 stsp }
224 ca6e02ac 2020-01-07 stsp
225 7762fe12 2018-11-05 stsp static const struct got_error *
226 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
227 d294b1dc 2022-10-18 stsp struct got_packidx *packidx, struct got_object_cache *objcache,
228 d294b1dc 2022-10-18 stsp struct got_parsed_tree_entry **entries, size_t *nentries,
229 d294b1dc 2022-10-18 stsp size_t *nentries_alloc)
230 876c234b 2018-09-10 stsp {
231 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
232 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
233 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
234 cdfe5501 2023-04-24 stsp size_t len = 0;
235 13c729f7 2018-12-24 stsp struct got_object_id id;
236 13c729f7 2018-12-24 stsp size_t datalen;
237 e7885405 2018-09-10 stsp
238 13c729f7 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
239 13c729f7 2018-12-24 stsp if (datalen != sizeof(iobj))
240 13c729f7 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
241 13c729f7 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
242 265df21f 2023-01-31 op memcpy(&id, &iobj.id, sizeof(id));
243 13c729f7 2018-12-24 stsp
244 cdfe5501 2023-04-24 stsp err = open_tree(&buf, &len, pack, packidx, iobj.idx, &id, objcache);
245 e7885405 2018-09-10 stsp if (err)
246 ca6e02ac 2020-01-07 stsp return err;
247 e7885405 2018-09-10 stsp
248 cdfe5501 2023-04-24 stsp err = got_object_parse_tree(entries, nentries, nentries_alloc,
249 cdfe5501 2023-04-24 stsp buf, len);
250 cdfe5501 2023-04-24 stsp if (err)
251 cdfe5501 2023-04-24 stsp goto done;
252 cdfe5501 2023-04-24 stsp
253 d294b1dc 2022-10-18 stsp err = got_privsep_send_tree(ibuf, *entries, *nentries);
254 e7885405 2018-09-10 stsp if (err) {
255 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
256 e7885405 2018-09-10 stsp err = NULL;
257 e7885405 2018-09-10 stsp else
258 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
259 e7885405 2018-09-10 stsp }
260 cdfe5501 2023-04-24 stsp done:
261 cdfe5501 2023-04-24 stsp free(buf);
262 e7885405 2018-09-10 stsp return err;
263 876c234b 2018-09-10 stsp }
264 876c234b 2018-09-10 stsp
265 876c234b 2018-09-10 stsp static const struct got_error *
266 030daac8 2021-09-25 stsp receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
267 876c234b 2018-09-10 stsp {
268 3840f4c9 2018-09-12 stsp const struct got_error *err;
269 3840f4c9 2018-09-12 stsp struct imsg imsg;
270 55da3778 2018-09-10 stsp size_t datalen;
271 55da3778 2018-09-10 stsp
272 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
273 55da3778 2018-09-10 stsp if (err)
274 55da3778 2018-09-10 stsp return err;
275 55da3778 2018-09-10 stsp
276 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
277 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
278 55da3778 2018-09-10 stsp goto done;
279 55da3778 2018-09-10 stsp }
280 55da3778 2018-09-10 stsp
281 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
282 55da3778 2018-09-10 stsp if (datalen != 0) {
283 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
284 55da3778 2018-09-10 stsp goto done;
285 55da3778 2018-09-10 stsp }
286 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
287 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
288 55da3778 2018-09-10 stsp goto done;
289 55da3778 2018-09-10 stsp }
290 55da3778 2018-09-10 stsp
291 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
292 3840f4c9 2018-09-12 stsp if (*f == NULL) {
293 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
294 3a6ce05a 2019-02-11 stsp close(imsg.fd);
295 55da3778 2018-09-10 stsp goto done;
296 55da3778 2018-09-10 stsp }
297 3840f4c9 2018-09-12 stsp done:
298 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
299 3840f4c9 2018-09-12 stsp return err;
300 db696021 2022-01-04 stsp }
301 db696021 2022-01-04 stsp
302 db696021 2022-01-04 stsp static const struct got_error *
303 67fd6849 2022-02-13 stsp receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
304 db696021 2022-01-04 stsp struct imsgbuf *ibuf)
305 db696021 2022-01-04 stsp {
306 db696021 2022-01-04 stsp size_t datalen;
307 db696021 2022-01-04 stsp
308 db696021 2022-01-04 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
309 db696021 2022-01-04 stsp if (datalen != 0)
310 db696021 2022-01-04 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
311 db696021 2022-01-04 stsp
312 db696021 2022-01-04 stsp if (imsg->fd == -1)
313 db696021 2022-01-04 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
314 db696021 2022-01-04 stsp
315 67fd6849 2022-02-13 stsp *f = fdopen(imsg->fd, mode);
316 db696021 2022-01-04 stsp if (*f == NULL)
317 db696021 2022-01-04 stsp return got_error_from_errno("fdopen");
318 db696021 2022-01-04 stsp imsg->fd = -1;
319 db696021 2022-01-04 stsp
320 db696021 2022-01-04 stsp return NULL;
321 3840f4c9 2018-09-12 stsp }
322 55da3778 2018-09-10 stsp
323 3840f4c9 2018-09-12 stsp static const struct got_error *
324 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
325 db696021 2022-01-04 stsp struct got_packidx *packidx, struct got_object_cache *objcache,
326 db696021 2022-01-04 stsp FILE *basefile, FILE *accumfile)
327 3840f4c9 2018-09-12 stsp {
328 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
329 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
330 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
331 db696021 2022-01-04 stsp FILE *outfile = NULL;
332 ebc55e2d 2018-12-24 stsp struct got_object_id id;
333 ebc55e2d 2018-12-24 stsp size_t datalen;
334 ac544f8c 2019-01-13 stsp uint64_t blob_size;
335 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
336 3840f4c9 2018-09-12 stsp
337 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
338 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
339 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
340 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
341 265df21f 2023-01-31 op memcpy(&id, &iobj.id, sizeof(id));
342 ebc55e2d 2018-12-24 stsp
343 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
344 704b89c4 2019-05-23 stsp if (obj) {
345 704b89c4 2019-05-23 stsp obj->refcnt++;
346 704b89c4 2019-05-23 stsp } else {
347 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
348 704b89c4 2019-05-23 stsp objcache);
349 704b89c4 2019-05-23 stsp if (err)
350 704b89c4 2019-05-23 stsp return err;
351 704b89c4 2019-05-23 stsp }
352 3840f4c9 2018-09-12 stsp
353 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
354 3840f4c9 2018-09-12 stsp if (err)
355 ac544f8c 2019-01-13 stsp goto done;
356 3840f4c9 2018-09-12 stsp
357 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
358 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
359 ac544f8c 2019-01-13 stsp if (err)
360 ac544f8c 2019-01-13 stsp goto done;
361 ac544f8c 2019-01-13 stsp } else
362 ac544f8c 2019-01-13 stsp blob_size = obj->size;
363 ac544f8c 2019-01-13 stsp
364 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
365 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
366 ac544f8c 2019-01-13 stsp obj, pack);
367 ac544f8c 2019-01-13 stsp else
368 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
369 ac544f8c 2019-01-13 stsp accumfile);
370 3840f4c9 2018-09-12 stsp if (err)
371 55da3778 2018-09-10 stsp goto done;
372 55da3778 2018-09-10 stsp
373 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
374 55da3778 2018-09-10 stsp done:
375 ac544f8c 2019-01-13 stsp free(buf);
376 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
377 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
378 cb5e38fd 2019-05-23 stsp got_object_close(obj);
379 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
380 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
381 55da3778 2018-09-10 stsp
382 55da3778 2018-09-10 stsp return err;
383 876c234b 2018-09-10 stsp }
384 876c234b 2018-09-10 stsp
385 876c234b 2018-09-10 stsp static const struct got_error *
386 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
387 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
388 f4a881ce 2018-11-17 stsp {
389 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
390 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
391 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
392 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
393 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
394 f4a881ce 2018-11-17 stsp size_t len;
395 268f7291 2018-12-24 stsp struct got_object_id id;
396 268f7291 2018-12-24 stsp size_t datalen;
397 f4a881ce 2018-11-17 stsp
398 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
399 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
400 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
401 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
402 265df21f 2023-01-31 op memcpy(&id, &iobj.id, sizeof(id));
403 268f7291 2018-12-24 stsp
404 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
405 704b89c4 2019-05-23 stsp if (obj) {
406 704b89c4 2019-05-23 stsp obj->refcnt++;
407 704b89c4 2019-05-23 stsp } else {
408 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
409 704b89c4 2019-05-23 stsp objcache);
410 704b89c4 2019-05-23 stsp if (err)
411 704b89c4 2019-05-23 stsp return err;
412 704b89c4 2019-05-23 stsp }
413 f4a881ce 2018-11-17 stsp
414 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
415 f4a881ce 2018-11-17 stsp if (err)
416 cb5e38fd 2019-05-23 stsp goto done;
417 f4a881ce 2018-11-17 stsp
418 f4a881ce 2018-11-17 stsp obj->size = len;
419 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
420 0ae4af15 2019-02-01 stsp if (err)
421 cb5e38fd 2019-05-23 stsp goto done;
422 f4a881ce 2018-11-17 stsp
423 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
424 cb5e38fd 2019-05-23 stsp done:
425 cb5e38fd 2019-05-23 stsp free(buf);
426 cb5e38fd 2019-05-23 stsp got_object_close(obj);
427 cb5e38fd 2019-05-23 stsp if (tag)
428 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
429 ca6e02ac 2020-01-07 stsp if (err) {
430 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
431 ca6e02ac 2020-01-07 stsp err = NULL;
432 ca6e02ac 2020-01-07 stsp else
433 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
434 ca6e02ac 2020-01-07 stsp }
435 ca6e02ac 2020-01-07 stsp
436 ca6e02ac 2020-01-07 stsp return err;
437 ca6e02ac 2020-01-07 stsp }
438 ca6e02ac 2020-01-07 stsp
439 50127d69 2021-09-25 stsp static const struct got_error *
440 cdfe5501 2023-04-24 stsp tree_path_changed(int *changed, uint8_t **buf1, size_t *len1,
441 cdfe5501 2023-04-24 stsp uint8_t **buf2, size_t *len2, const char *path,
442 cdfe5501 2023-04-24 stsp struct got_pack *pack, struct got_packidx *packidx,
443 ca6e02ac 2020-01-07 stsp struct imsgbuf *ibuf, struct got_object_cache *objcache)
444 ca6e02ac 2020-01-07 stsp {
445 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
446 cdfe5501 2023-04-24 stsp struct got_parsed_tree_entry pte1, pte2;
447 ca6e02ac 2020-01-07 stsp const char *seg, *s;
448 ca6e02ac 2020-01-07 stsp size_t seglen;
449 cdfe5501 2023-04-24 stsp size_t remain1 = *len1, remain2 = *len2, elen;
450 cdfe5501 2023-04-24 stsp uint8_t *next_entry1 = *buf1;
451 cdfe5501 2023-04-24 stsp uint8_t *next_entry2 = *buf2;
452 ca6e02ac 2020-01-07 stsp
453 cdfe5501 2023-04-24 stsp memset(&pte1, 0, sizeof(pte1));
454 cdfe5501 2023-04-24 stsp memset(&pte2, 0, sizeof(pte2));
455 cdfe5501 2023-04-24 stsp
456 ca6e02ac 2020-01-07 stsp *changed = 0;
457 ca6e02ac 2020-01-07 stsp
458 ca6e02ac 2020-01-07 stsp /* We not do support comparing the root path. */
459 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
460 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
461 ca6e02ac 2020-01-07 stsp
462 ca6e02ac 2020-01-07 stsp s = path;
463 61a7d79f 2020-02-29 stsp while (*s == '/')
464 61a7d79f 2020-02-29 stsp s++;
465 ca6e02ac 2020-01-07 stsp seg = s;
466 ca6e02ac 2020-01-07 stsp seglen = 0;
467 ca6e02ac 2020-01-07 stsp while (*s) {
468 ca6e02ac 2020-01-07 stsp if (*s != '/') {
469 ca6e02ac 2020-01-07 stsp s++;
470 ca6e02ac 2020-01-07 stsp seglen++;
471 ca6e02ac 2020-01-07 stsp if (*s)
472 ca6e02ac 2020-01-07 stsp continue;
473 ca6e02ac 2020-01-07 stsp }
474 ca6e02ac 2020-01-07 stsp
475 cdfe5501 2023-04-24 stsp /*
476 cdfe5501 2023-04-24 stsp * As an optimization we compare entries in on-disk order
477 cdfe5501 2023-04-24 stsp * rather than in got_path_cmp() order. We only need to
478 cdfe5501 2023-04-24 stsp * find out if any entries differ. Parsing all entries and
479 cdfe5501 2023-04-24 stsp * sorting them slows us down significantly when tree objects
480 cdfe5501 2023-04-24 stsp * have thousands of entries. We can assume that on-disk entry
481 cdfe5501 2023-04-24 stsp * ordering is stable, as per got_object_tree_create() and
482 cdfe5501 2023-04-24 stsp * sort_tree_entries_the_way_git_likes_it(). Other orderings
483 cdfe5501 2023-04-24 stsp * are incompatible with Git and would yield false positives
484 cdfe5501 2023-04-24 stsp * here, too.
485 cdfe5501 2023-04-24 stsp */
486 cdfe5501 2023-04-24 stsp while (remain1 > 0) {
487 cdfe5501 2023-04-24 stsp err = got_object_parse_tree_entry(&pte1, &elen,
488 cdfe5501 2023-04-24 stsp next_entry1, remain1);
489 cdfe5501 2023-04-24 stsp if (err)
490 cdfe5501 2023-04-24 stsp return err;
491 cdfe5501 2023-04-24 stsp next_entry1 += elen;
492 cdfe5501 2023-04-24 stsp remain1 -= elen;
493 cdfe5501 2023-04-24 stsp if (strncmp(pte1.name, seg, seglen) != 0 ||
494 cdfe5501 2023-04-24 stsp pte1.name[seglen] != '\0') {
495 cdfe5501 2023-04-24 stsp memset(&pte1, 0, sizeof(pte1));
496 cdfe5501 2023-04-24 stsp continue;
497 cdfe5501 2023-04-24 stsp } else
498 cdfe5501 2023-04-24 stsp break;
499 cdfe5501 2023-04-24 stsp }
500 cdfe5501 2023-04-24 stsp if (pte1.name == NULL) {
501 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_NO_OBJ);
502 ca6e02ac 2020-01-07 stsp break;
503 ca6e02ac 2020-01-07 stsp }
504 ca6e02ac 2020-01-07 stsp
505 cdfe5501 2023-04-24 stsp if (remain2 == 0) {
506 ca6e02ac 2020-01-07 stsp *changed = 1;
507 ca6e02ac 2020-01-07 stsp break;
508 ca6e02ac 2020-01-07 stsp }
509 ca6e02ac 2020-01-07 stsp
510 cdfe5501 2023-04-24 stsp while (remain2 > 0) {
511 cdfe5501 2023-04-24 stsp err = got_object_parse_tree_entry(&pte2, &elen,
512 cdfe5501 2023-04-24 stsp next_entry2, remain2);
513 cdfe5501 2023-04-24 stsp if (err)
514 cdfe5501 2023-04-24 stsp return err;
515 cdfe5501 2023-04-24 stsp next_entry2 += elen;
516 cdfe5501 2023-04-24 stsp remain2 -= elen;
517 cdfe5501 2023-04-24 stsp if (strncmp(pte2.name, seg, seglen) != 0 ||
518 cdfe5501 2023-04-24 stsp pte2.name[seglen] != '\0') {
519 cdfe5501 2023-04-24 stsp memset(&pte2, 0, sizeof(pte2));
520 cdfe5501 2023-04-24 stsp continue;
521 cdfe5501 2023-04-24 stsp } else
522 cdfe5501 2023-04-24 stsp break;
523 cdfe5501 2023-04-24 stsp }
524 cdfe5501 2023-04-24 stsp
525 cdfe5501 2023-04-24 stsp if (pte2.name == NULL) {
526 ca6e02ac 2020-01-07 stsp *changed = 1;
527 ca6e02ac 2020-01-07 stsp break;
528 ca6e02ac 2020-01-07 stsp }
529 ca6e02ac 2020-01-07 stsp
530 cdfe5501 2023-04-24 stsp if (pte1.mode != pte2.mode) {
531 cdfe5501 2023-04-24 stsp *changed = 1;
532 cdfe5501 2023-04-24 stsp break;
533 cdfe5501 2023-04-24 stsp }
534 cdfe5501 2023-04-24 stsp
535 cdfe5501 2023-04-24 stsp if (memcmp(pte1.id, pte2.id, SHA1_DIGEST_LENGTH) == 0) {
536 ca6e02ac 2020-01-07 stsp *changed = 0;
537 ca6e02ac 2020-01-07 stsp break;
538 ca6e02ac 2020-01-07 stsp }
539 ca6e02ac 2020-01-07 stsp
540 ca6e02ac 2020-01-07 stsp if (*s == '\0') { /* final path element */
541 ca6e02ac 2020-01-07 stsp *changed = 1;
542 ca6e02ac 2020-01-07 stsp break;
543 ca6e02ac 2020-01-07 stsp }
544 ca6e02ac 2020-01-07 stsp
545 ca6e02ac 2020-01-07 stsp seg = s + 1;
546 ca6e02ac 2020-01-07 stsp s++;
547 ca6e02ac 2020-01-07 stsp seglen = 0;
548 ca6e02ac 2020-01-07 stsp if (*s) {
549 ca6e02ac 2020-01-07 stsp struct got_object_id id1, id2;
550 ca6e02ac 2020-01-07 stsp int idx;
551 ca6e02ac 2020-01-07 stsp
552 cdfe5501 2023-04-24 stsp memcpy(id1.sha1, pte1.id, SHA1_DIGEST_LENGTH);
553 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id1);
554 ca6e02ac 2020-01-07 stsp if (idx == -1) {
555 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id1);
556 ca6e02ac 2020-01-07 stsp break;
557 ca6e02ac 2020-01-07 stsp }
558 ca6e02ac 2020-01-07 stsp free(*buf1);
559 ca6e02ac 2020-01-07 stsp *buf1 = NULL;
560 cdfe5501 2023-04-24 stsp err = open_tree(buf1, len1, pack, packidx, idx, &id1,
561 d294b1dc 2022-10-18 stsp objcache);
562 cdfe5501 2023-04-24 stsp memset(&pte1, 0, sizeof(pte1));
563 ca6e02ac 2020-01-07 stsp if (err)
564 ca6e02ac 2020-01-07 stsp break;
565 cdfe5501 2023-04-24 stsp next_entry1 = *buf1;
566 cdfe5501 2023-04-24 stsp remain1 = *len1;
567 ca6e02ac 2020-01-07 stsp
568 cdfe5501 2023-04-24 stsp memcpy(id2.sha1, pte2.id, SHA1_DIGEST_LENGTH);
569 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id2);
570 ca6e02ac 2020-01-07 stsp if (idx == -1) {
571 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id2);
572 ca6e02ac 2020-01-07 stsp break;
573 ca6e02ac 2020-01-07 stsp }
574 ca6e02ac 2020-01-07 stsp free(*buf2);
575 ca6e02ac 2020-01-07 stsp *buf2 = NULL;
576 cdfe5501 2023-04-24 stsp err = open_tree(buf2, len2, pack, packidx, idx, &id2,
577 7b771fb6 2023-02-13 op objcache);
578 cdfe5501 2023-04-24 stsp memset(&pte2, 0, sizeof(pte2));
579 ca6e02ac 2020-01-07 stsp if (err)
580 ca6e02ac 2020-01-07 stsp break;
581 cdfe5501 2023-04-24 stsp next_entry2 = *buf2;
582 cdfe5501 2023-04-24 stsp remain2 = *len2;
583 ca6e02ac 2020-01-07 stsp }
584 ca6e02ac 2020-01-07 stsp }
585 ca6e02ac 2020-01-07 stsp
586 ca6e02ac 2020-01-07 stsp return err;
587 ca6e02ac 2020-01-07 stsp }
588 ca6e02ac 2020-01-07 stsp
589 ca6e02ac 2020-01-07 stsp static const struct got_error *
590 e70bf110 2020-03-22 stsp send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
591 e70bf110 2020-03-22 stsp struct imsgbuf *ibuf)
592 e70bf110 2020-03-22 stsp {
593 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
594 030daac8 2021-09-25 stsp size_t i;
595 e70bf110 2020-03-22 stsp
596 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
597 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
598 076fbedc 2023-02-19 op ncommits * sizeof(commit_ids[0]));
599 e70bf110 2020-03-22 stsp if (wbuf == NULL)
600 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
601 e70bf110 2020-03-22 stsp
602 1453347d 2022-05-19 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
603 1453347d 2022-05-19 stsp return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
604 1453347d 2022-05-19 stsp
605 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
606 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
607 076fbedc 2023-02-19 op if (imsg_add(wbuf, id, sizeof(*id)) == -1) {
608 1453347d 2022-05-19 stsp return got_error_from_errno(
609 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
610 e70bf110 2020-03-22 stsp }
611 e70bf110 2020-03-22 stsp }
612 e70bf110 2020-03-22 stsp
613 e70bf110 2020-03-22 stsp wbuf->fd = -1;
614 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
615 e70bf110 2020-03-22 stsp
616 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
617 e70bf110 2020-03-22 stsp }
618 e70bf110 2020-03-22 stsp
619 e70bf110 2020-03-22 stsp static const struct got_error *
620 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
621 e70bf110 2020-03-22 stsp {
622 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
623 e70bf110 2020-03-22 stsp NULL, 0) == -1)
624 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
625 e70bf110 2020-03-22 stsp
626 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
627 e70bf110 2020-03-22 stsp }
628 e44d9391 2022-06-07 stsp
629 e70bf110 2020-03-22 stsp static const struct got_error *
630 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
631 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
632 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
633 ca6e02ac 2020-01-07 stsp {
634 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
635 47cb6f8b 2023-02-26 op struct got_imsg_commit_traversal_request ctreq;
636 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
637 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
638 ca6e02ac 2020-01-07 stsp struct got_object_id id;
639 47cb6f8b 2023-02-26 op size_t datalen;
640 ca6e02ac 2020-01-07 stsp char *path = NULL;
641 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
642 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
643 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
644 ca6e02ac 2020-01-07 stsp
645 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
646 47cb6f8b 2023-02-26 op if (datalen < sizeof(ctreq))
647 47cb6f8b 2023-02-26 op return got_error(GOT_ERR_PRIVSEP_LEN);
648 47cb6f8b 2023-02-26 op memcpy(&ctreq, imsg->data, sizeof(ctreq));
649 47cb6f8b 2023-02-26 op memcpy(&id, &ctreq.iobj.id, sizeof(id));
650 ca6e02ac 2020-01-07 stsp
651 47cb6f8b 2023-02-26 op if (datalen != sizeof(ctreq) + ctreq.path_len)
652 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
653 47cb6f8b 2023-02-26 op if (ctreq.path_len == 0)
654 47cb6f8b 2023-02-26 op return got_error(GOT_ERR_PRIVSEP_LEN);
655 ca6e02ac 2020-01-07 stsp
656 47cb6f8b 2023-02-26 op path = strndup(imsg->data + sizeof(ctreq), ctreq.path_len);
657 47cb6f8b 2023-02-26 op if (path == NULL)
658 47cb6f8b 2023-02-26 op return got_error_from_errno("strndup");
659 47cb6f8b 2023-02-26 op
660 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
661 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
662 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
663 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
664 ca6e02ac 2020-01-07 stsp
665 ca6e02ac 2020-01-07 stsp do {
666 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
667 ca6e02ac 2020-01-07 stsp int idx;
668 ca6e02ac 2020-01-07 stsp
669 ca6e02ac 2020-01-07 stsp if (sigint_received) {
670 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
671 ca6e02ac 2020-01-07 stsp goto done;
672 ca6e02ac 2020-01-07 stsp }
673 ca6e02ac 2020-01-07 stsp
674 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
675 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
676 ca6e02ac 2020-01-07 stsp if (idx == -1)
677 ca6e02ac 2020-01-07 stsp break;
678 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
679 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
680 ca6e02ac 2020-01-07 stsp if (err) {
681 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
682 ca6e02ac 2020-01-07 stsp goto done;
683 ca6e02ac 2020-01-07 stsp err = NULL;
684 ca6e02ac 2020-01-07 stsp break;
685 ca6e02ac 2020-01-07 stsp }
686 ca6e02ac 2020-01-07 stsp }
687 ca6e02ac 2020-01-07 stsp
688 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
689 076fbedc 2023-02-19 op ncommits * sizeof(commit_ids[0]) >= max_datalen) {
690 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
691 e70bf110 2020-03-22 stsp ibuf);
692 ca6e02ac 2020-01-07 stsp if (err)
693 ca6e02ac 2020-01-07 stsp goto done;
694 ca6e02ac 2020-01-07 stsp ncommits = 0;
695 ca6e02ac 2020-01-07 stsp }
696 ca6e02ac 2020-01-07 stsp ncommits++;
697 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
698 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
699 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
700 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
701 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
702 ca6e02ac 2020-01-07 stsp if (new == NULL) {
703 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
704 ca6e02ac 2020-01-07 stsp goto done;
705 ca6e02ac 2020-01-07 stsp }
706 ca6e02ac 2020-01-07 stsp commit_ids = new;
707 ca6e02ac 2020-01-07 stsp }
708 b685c8da 2023-02-19 op memcpy(&commit_ids[ncommits - 1], &id, sizeof(id));
709 ca6e02ac 2020-01-07 stsp
710 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
711 ca6e02ac 2020-01-07 stsp if (pid == NULL)
712 ca6e02ac 2020-01-07 stsp break;
713 ca6e02ac 2020-01-07 stsp
714 d7b5a0e8 2022-04-20 stsp idx = got_packidx_get_object_idx(packidx, &pid->id);
715 ca6e02ac 2020-01-07 stsp if (idx == -1)
716 ca6e02ac 2020-01-07 stsp break;
717 ca6e02ac 2020-01-07 stsp
718 d7b5a0e8 2022-04-20 stsp err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
719 ca6e02ac 2020-01-07 stsp objcache);
720 ca6e02ac 2020-01-07 stsp if (err) {
721 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
722 ca6e02ac 2020-01-07 stsp goto done;
723 ca6e02ac 2020-01-07 stsp err = NULL;
724 ca6e02ac 2020-01-07 stsp break;
725 ca6e02ac 2020-01-07 stsp }
726 ca6e02ac 2020-01-07 stsp
727 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
728 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
729 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
730 ca6e02ac 2020-01-07 stsp changed = 1;
731 ca6e02ac 2020-01-07 stsp break;
732 ca6e02ac 2020-01-07 stsp }
733 ca6e02ac 2020-01-07 stsp } else {
734 ca6e02ac 2020-01-07 stsp int pidx;
735 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
736 cdfe5501 2023-04-24 stsp size_t len = 0, plen = 0;
737 ca6e02ac 2020-01-07 stsp
738 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
739 ca6e02ac 2020-01-07 stsp commit->tree_id);
740 ca6e02ac 2020-01-07 stsp if (idx == -1)
741 ca6e02ac 2020-01-07 stsp break;
742 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
743 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
744 ca6e02ac 2020-01-07 stsp if (pidx == -1)
745 ca6e02ac 2020-01-07 stsp break;
746 ca6e02ac 2020-01-07 stsp
747 cdfe5501 2023-04-24 stsp err = open_tree(&buf, &len, pack, packidx, idx,
748 d294b1dc 2022-10-18 stsp commit->tree_id, objcache);
749 ca6e02ac 2020-01-07 stsp if (err)
750 ca6e02ac 2020-01-07 stsp goto done;
751 cdfe5501 2023-04-24 stsp
752 cdfe5501 2023-04-24 stsp err = open_tree(&pbuf, &plen, pack, packidx, pidx,
753 d294b1dc 2022-10-18 stsp pcommit->tree_id, objcache);
754 ca6e02ac 2020-01-07 stsp if (err) {
755 ca6e02ac 2020-01-07 stsp free(buf);
756 ca6e02ac 2020-01-07 stsp goto done;
757 ca6e02ac 2020-01-07 stsp }
758 ca6e02ac 2020-01-07 stsp
759 cdfe5501 2023-04-24 stsp err = tree_path_changed(&changed, &buf, &len,
760 cdfe5501 2023-04-24 stsp &pbuf, &plen, path, pack, packidx, ibuf,
761 cdfe5501 2023-04-24 stsp objcache);
762 ca6e02ac 2020-01-07 stsp
763 ca6e02ac 2020-01-07 stsp free(buf);
764 ca6e02ac 2020-01-07 stsp free(pbuf);
765 ca6e02ac 2020-01-07 stsp if (err) {
766 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
767 ca6e02ac 2020-01-07 stsp goto done;
768 ca6e02ac 2020-01-07 stsp err = NULL;
769 ca6e02ac 2020-01-07 stsp break;
770 ca6e02ac 2020-01-07 stsp }
771 ca6e02ac 2020-01-07 stsp }
772 ca6e02ac 2020-01-07 stsp
773 ca6e02ac 2020-01-07 stsp if (!changed) {
774 b685c8da 2023-02-19 op memcpy(&id, &pid->id, sizeof(id));
775 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
776 ca6e02ac 2020-01-07 stsp commit = pcommit;
777 ca6e02ac 2020-01-07 stsp pcommit = NULL;
778 ca6e02ac 2020-01-07 stsp }
779 ca6e02ac 2020-01-07 stsp } while (!changed);
780 ca6e02ac 2020-01-07 stsp
781 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
782 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
783 ca6e02ac 2020-01-07 stsp if (err)
784 ca6e02ac 2020-01-07 stsp goto done;
785 ca6e02ac 2020-01-07 stsp
786 ca6e02ac 2020-01-07 stsp if (changed) {
787 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
788 ca6e02ac 2020-01-07 stsp if (err)
789 ca6e02ac 2020-01-07 stsp goto done;
790 ca6e02ac 2020-01-07 stsp }
791 ca6e02ac 2020-01-07 stsp }
792 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
793 ca6e02ac 2020-01-07 stsp done:
794 47cb6f8b 2023-02-26 op free(path);
795 ca6e02ac 2020-01-07 stsp free(commit_ids);
796 ca6e02ac 2020-01-07 stsp if (commit)
797 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
798 ca6e02ac 2020-01-07 stsp if (pcommit)
799 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
800 f4a881ce 2018-11-17 stsp if (err) {
801 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
802 f4a881ce 2018-11-17 stsp err = NULL;
803 f4a881ce 2018-11-17 stsp else
804 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
805 f4a881ce 2018-11-17 stsp }
806 f4a881ce 2018-11-17 stsp
807 f4a881ce 2018-11-17 stsp return err;
808 f4a881ce 2018-11-17 stsp }
809 f4a881ce 2018-11-17 stsp
810 f4a881ce 2018-11-17 stsp static const struct got_error *
811 c0df5966 2021-12-31 stsp raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
812 c0df5966 2021-12-31 stsp struct got_pack *pack, struct got_packidx *packidx,
813 db696021 2022-01-04 stsp struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
814 59d1e4a0 2021-03-10 stsp {
815 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
816 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
817 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
818 db696021 2022-01-04 stsp FILE *outfile = NULL;
819 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
820 59d1e4a0 2021-03-10 stsp struct got_object *obj;
821 59d1e4a0 2021-03-10 stsp struct got_object_id id;
822 59d1e4a0 2021-03-10 stsp size_t datalen;
823 59d1e4a0 2021-03-10 stsp
824 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
825 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
826 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
827 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
828 265df21f 2023-01-31 op memcpy(&id, &iobj.id, sizeof(id));
829 59d1e4a0 2021-03-10 stsp
830 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
831 59d1e4a0 2021-03-10 stsp if (obj) {
832 59d1e4a0 2021-03-10 stsp obj->refcnt++;
833 59d1e4a0 2021-03-10 stsp } else {
834 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
835 59d1e4a0 2021-03-10 stsp objcache);
836 59d1e4a0 2021-03-10 stsp if (err)
837 59d1e4a0 2021-03-10 stsp return err;
838 59d1e4a0 2021-03-10 stsp }
839 59d1e4a0 2021-03-10 stsp
840 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
841 59d1e4a0 2021-03-10 stsp if (err)
842 59d1e4a0 2021-03-10 stsp return err;
843 59d1e4a0 2021-03-10 stsp
844 59d1e4a0 2021-03-10 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
845 59d1e4a0 2021-03-10 stsp err = got_pack_get_max_delta_object_size(&size, obj, pack);
846 59d1e4a0 2021-03-10 stsp if (err)
847 59d1e4a0 2021-03-10 stsp goto done;
848 59d1e4a0 2021-03-10 stsp } else
849 59d1e4a0 2021-03-10 stsp size = obj->size;
850 59d1e4a0 2021-03-10 stsp
851 59d1e4a0 2021-03-10 stsp if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
852 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
853 59d1e4a0 2021-03-10 stsp obj, pack);
854 59d1e4a0 2021-03-10 stsp else
855 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
856 59d1e4a0 2021-03-10 stsp accumfile);
857 59d1e4a0 2021-03-10 stsp if (err)
858 59d1e4a0 2021-03-10 stsp goto done;
859 59d1e4a0 2021-03-10 stsp
860 40e3cb72 2021-06-22 stsp err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
861 59d1e4a0 2021-03-10 stsp done:
862 59d1e4a0 2021-03-10 stsp free(buf);
863 59d1e4a0 2021-03-10 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
864 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
865 59d1e4a0 2021-03-10 stsp got_object_close(obj);
866 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
867 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
868 59d1e4a0 2021-03-10 stsp
869 59d1e4a0 2021-03-10 stsp return err;
870 59d1e4a0 2021-03-10 stsp }
871 67fd6849 2022-02-13 stsp
872 67fd6849 2022-02-13 stsp static const struct got_error *
873 67fd6849 2022-02-13 stsp get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
874 67fd6849 2022-02-13 stsp off_t base_offset)
875 67fd6849 2022-02-13 stsp {
876 67fd6849 2022-02-13 stsp const struct got_error *err;
877 67fd6849 2022-02-13 stsp int idx;
878 59d1e4a0 2021-03-10 stsp
879 5e91dae4 2022-08-30 stsp err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
880 67fd6849 2022-02-13 stsp if (err)
881 67fd6849 2022-02-13 stsp return err;
882 67fd6849 2022-02-13 stsp if (idx == -1)
883 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKIDX);
884 59d1e4a0 2021-03-10 stsp
885 67fd6849 2022-02-13 stsp return got_packidx_get_object_id(base_id, packidx, idx);
886 67fd6849 2022-02-13 stsp }
887 59d1e4a0 2021-03-10 stsp
888 59d1e4a0 2021-03-10 stsp static const struct got_error *
889 67fd6849 2022-02-13 stsp raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
890 67fd6849 2022-02-13 stsp FILE *delta_outfile, struct got_pack *pack,
891 67fd6849 2022-02-13 stsp struct got_packidx *packidx)
892 67fd6849 2022-02-13 stsp {
893 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
894 67fd6849 2022-02-13 stsp struct got_imsg_raw_delta_request req;
895 2d9e6abf 2022-05-04 stsp size_t datalen, delta_size, delta_compressed_size;
896 24b7de1c 2022-12-03 stsp off_t delta_offset, delta_data_offset;
897 67fd6849 2022-02-13 stsp uint8_t *delta_buf = NULL;
898 67fd6849 2022-02-13 stsp struct got_object_id id, base_id;
899 67fd6849 2022-02-13 stsp off_t base_offset, delta_out_offset = 0;
900 67fd6849 2022-02-13 stsp uint64_t base_size = 0, result_size = 0;
901 67fd6849 2022-02-13 stsp size_t w;
902 67fd6849 2022-02-13 stsp
903 67fd6849 2022-02-13 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
904 67fd6849 2022-02-13 stsp if (datalen != sizeof(req))
905 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
906 67fd6849 2022-02-13 stsp memcpy(&req, imsg->data, sizeof(req));
907 babd9f5d 2023-01-31 op memcpy(&id, &req.id, sizeof(id));
908 67fd6849 2022-02-13 stsp
909 67fd6849 2022-02-13 stsp imsg->fd = -1;
910 67fd6849 2022-02-13 stsp
911 67fd6849 2022-02-13 stsp err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
912 24b7de1c 2022-12-03 stsp &delta_compressed_size, &delta_offset, &delta_data_offset,
913 24b7de1c 2022-12-03 stsp &base_offset, &base_id, &base_size, &result_size,
914 24b7de1c 2022-12-03 stsp pack, packidx, req.idx);
915 67fd6849 2022-02-13 stsp if (err)
916 67fd6849 2022-02-13 stsp goto done;
917 67fd6849 2022-02-13 stsp
918 67fd6849 2022-02-13 stsp /*
919 67fd6849 2022-02-13 stsp * If this is an offset delta we must determine the base
920 67fd6849 2022-02-13 stsp * object ID ourselves.
921 67fd6849 2022-02-13 stsp */
922 67fd6849 2022-02-13 stsp if (base_offset != 0) {
923 67fd6849 2022-02-13 stsp err = get_base_object_id(&base_id, packidx, base_offset);
924 67fd6849 2022-02-13 stsp if (err)
925 67fd6849 2022-02-13 stsp goto done;
926 67fd6849 2022-02-13 stsp }
927 67fd6849 2022-02-13 stsp
928 67fd6849 2022-02-13 stsp delta_out_offset = ftello(delta_outfile);
929 2d9e6abf 2022-05-04 stsp w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
930 2d9e6abf 2022-05-04 stsp if (w != delta_compressed_size) {
931 67fd6849 2022-02-13 stsp err = got_ferror(delta_outfile, GOT_ERR_IO);
932 67fd6849 2022-02-13 stsp goto done;
933 67fd6849 2022-02-13 stsp }
934 67fd6849 2022-02-13 stsp if (fflush(delta_outfile) == -1) {
935 67fd6849 2022-02-13 stsp err = got_error_from_errno("fflush");
936 67fd6849 2022-02-13 stsp goto done;
937 67fd6849 2022-02-13 stsp }
938 67fd6849 2022-02-13 stsp
939 67fd6849 2022-02-13 stsp err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
940 2d9e6abf 2022-05-04 stsp delta_size, delta_compressed_size, delta_offset, delta_out_offset,
941 2d9e6abf 2022-05-04 stsp &base_id);
942 67fd6849 2022-02-13 stsp done:
943 67fd6849 2022-02-13 stsp free(delta_buf);
944 67fd6849 2022-02-13 stsp return err;
945 67fd6849 2022-02-13 stsp }
946 fae7e038 2022-05-07 stsp
947 fae7e038 2022-05-07 stsp struct search_deltas_arg {
948 fae7e038 2022-05-07 stsp struct imsgbuf *ibuf;
949 fae7e038 2022-05-07 stsp struct got_packidx *packidx;
950 fae7e038 2022-05-07 stsp struct got_pack *pack;
951 fae7e038 2022-05-07 stsp struct got_object_idset *idset;
952 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
953 fae7e038 2022-05-07 stsp size_t ndeltas;
954 fae7e038 2022-05-07 stsp };
955 67fd6849 2022-02-13 stsp
956 67fd6849 2022-02-13 stsp static const struct got_error *
957 fae7e038 2022-05-07 stsp search_delta_for_object(struct got_object_id *id, void *data, void *arg)
958 fae7e038 2022-05-07 stsp {
959 fae7e038 2022-05-07 stsp const struct got_error *err;
960 fae7e038 2022-05-07 stsp struct search_deltas_arg *a = arg;
961 fae7e038 2022-05-07 stsp int obj_idx;
962 fae7e038 2022-05-07 stsp uint8_t *delta_buf = NULL;
963 fae7e038 2022-05-07 stsp uint64_t base_size, result_size;
964 fae7e038 2022-05-07 stsp size_t delta_size, delta_compressed_size;
965 24b7de1c 2022-12-03 stsp off_t delta_offset, delta_data_offset, base_offset;
966 fae7e038 2022-05-07 stsp struct got_object_id base_id;
967 fae7e038 2022-05-07 stsp
968 fae7e038 2022-05-07 stsp if (sigint_received)
969 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_CANCELLED);
970 fae7e038 2022-05-07 stsp
971 fae7e038 2022-05-07 stsp obj_idx = got_packidx_get_object_idx(a->packidx, id);
972 fae7e038 2022-05-07 stsp if (obj_idx == -1)
973 fae7e038 2022-05-07 stsp return NULL; /* object not present in our pack file */
974 fae7e038 2022-05-07 stsp
975 fae7e038 2022-05-07 stsp err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
976 24b7de1c 2022-12-03 stsp &delta_compressed_size, &delta_offset, &delta_data_offset,
977 24b7de1c 2022-12-03 stsp &base_offset, &base_id, &base_size, &result_size,
978 24b7de1c 2022-12-03 stsp a->pack, a->packidx, obj_idx);
979 fae7e038 2022-05-07 stsp if (err) {
980 fae7e038 2022-05-07 stsp if (err->code == GOT_ERR_OBJ_TYPE)
981 fae7e038 2022-05-07 stsp return NULL; /* object not stored as a delta */
982 fae7e038 2022-05-07 stsp return err;
983 fae7e038 2022-05-07 stsp }
984 fae7e038 2022-05-07 stsp
985 fae7e038 2022-05-07 stsp /*
986 fae7e038 2022-05-07 stsp * If this is an offset delta we must determine the base
987 fae7e038 2022-05-07 stsp * object ID ourselves.
988 fae7e038 2022-05-07 stsp */
989 fae7e038 2022-05-07 stsp if (base_offset != 0) {
990 fae7e038 2022-05-07 stsp err = get_base_object_id(&base_id, a->packidx, base_offset);
991 fae7e038 2022-05-07 stsp if (err)
992 fae7e038 2022-05-07 stsp goto done;
993 fae7e038 2022-05-07 stsp }
994 fae7e038 2022-05-07 stsp
995 fae7e038 2022-05-07 stsp if (got_object_idset_contains(a->idset, &base_id)) {
996 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta *delta;
997 fae7e038 2022-05-07 stsp
998 fae7e038 2022-05-07 stsp delta = &a->deltas[a->ndeltas++];
999 fae7e038 2022-05-07 stsp memcpy(&delta->id, id, sizeof(delta->id));
1000 fae7e038 2022-05-07 stsp memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
1001 fae7e038 2022-05-07 stsp delta->base_size = base_size;
1002 fae7e038 2022-05-07 stsp delta->result_size = result_size;
1003 fae7e038 2022-05-07 stsp delta->delta_size = delta_size;
1004 fae7e038 2022-05-07 stsp delta->delta_compressed_size = delta_compressed_size;
1005 24b7de1c 2022-12-03 stsp delta->delta_offset = delta_data_offset;
1006 fae7e038 2022-05-07 stsp
1007 fae7e038 2022-05-07 stsp if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
1008 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas(a->ibuf,
1009 fae7e038 2022-05-07 stsp a->deltas, a->ndeltas);
1010 fae7e038 2022-05-07 stsp if (err)
1011 fae7e038 2022-05-07 stsp goto done;
1012 fae7e038 2022-05-07 stsp a->ndeltas = 0;
1013 fae7e038 2022-05-07 stsp }
1014 fae7e038 2022-05-07 stsp }
1015 fae7e038 2022-05-07 stsp done:
1016 fae7e038 2022-05-07 stsp free(delta_buf);
1017 fae7e038 2022-05-07 stsp return err;
1018 fae7e038 2022-05-07 stsp }
1019 fae7e038 2022-05-07 stsp
1020 fae7e038 2022-05-07 stsp static const struct got_error *
1021 fae7e038 2022-05-07 stsp recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1022 fae7e038 2022-05-07 stsp {
1023 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
1024 fae7e038 2022-05-07 stsp int done = 0;
1025 fae7e038 2022-05-07 stsp struct got_object_id *ids;
1026 fae7e038 2022-05-07 stsp size_t nids, i;
1027 fae7e038 2022-05-07 stsp
1028 fae7e038 2022-05-07 stsp for (;;) {
1029 fae7e038 2022-05-07 stsp err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1030 fae7e038 2022-05-07 stsp if (err || done)
1031 fae7e038 2022-05-07 stsp break;
1032 fae7e038 2022-05-07 stsp for (i = 0; i < nids; i++) {
1033 fae7e038 2022-05-07 stsp err = got_object_idset_add(idset, &ids[i], NULL);
1034 fae7e038 2022-05-07 stsp if (err) {
1035 fae7e038 2022-05-07 stsp free(ids);
1036 fae7e038 2022-05-07 stsp return err;
1037 fae7e038 2022-05-07 stsp }
1038 fae7e038 2022-05-07 stsp }
1039 fae7e038 2022-05-07 stsp free(ids);
1040 0ab4c957 2022-06-13 stsp }
1041 0ab4c957 2022-06-13 stsp
1042 0ab4c957 2022-06-13 stsp return err;
1043 0ab4c957 2022-06-13 stsp }
1044 0ab4c957 2022-06-13 stsp
1045 0ab4c957 2022-06-13 stsp static const struct got_error *
1046 0a618912 2023-01-27 stsp recv_object_id_queue(struct got_object_id_queue *queue,
1047 0a618912 2023-01-27 stsp struct got_object_idset *queued_ids, struct imsgbuf *ibuf)
1048 0ab4c957 2022-06-13 stsp {
1049 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1050 0ab4c957 2022-06-13 stsp int done = 0;
1051 0ab4c957 2022-06-13 stsp struct got_object_qid *qid;
1052 0ab4c957 2022-06-13 stsp struct got_object_id *ids;
1053 0ab4c957 2022-06-13 stsp size_t nids, i;
1054 0ab4c957 2022-06-13 stsp
1055 0ab4c957 2022-06-13 stsp for (;;) {
1056 0ab4c957 2022-06-13 stsp err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1057 0ab4c957 2022-06-13 stsp if (err || done)
1058 0ab4c957 2022-06-13 stsp break;
1059 0ab4c957 2022-06-13 stsp for (i = 0; i < nids; i++) {
1060 0ab4c957 2022-06-13 stsp err = got_object_qid_alloc_partial(&qid);
1061 0ab4c957 2022-06-13 stsp if (err)
1062 0ab4c957 2022-06-13 stsp return err;
1063 0ab4c957 2022-06-13 stsp memcpy(&qid->id, &ids[i], sizeof(qid->id));
1064 0ab4c957 2022-06-13 stsp STAILQ_INSERT_TAIL(queue, qid, entry);
1065 0a618912 2023-01-27 stsp err = got_object_idset_add(queued_ids, &qid->id, NULL);
1066 0a618912 2023-01-27 stsp if (err)
1067 0a618912 2023-01-27 stsp return err;
1068 0ab4c957 2022-06-13 stsp }
1069 cee6a7ea 2022-06-07 stsp }
1070 cee6a7ea 2022-06-07 stsp
1071 cee6a7ea 2022-06-07 stsp return err;
1072 cee6a7ea 2022-06-07 stsp }
1073 cee6a7ea 2022-06-07 stsp
1074 cee6a7ea 2022-06-07 stsp static const struct got_error *
1075 fae7e038 2022-05-07 stsp delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1076 24b7de1c 2022-12-03 stsp struct got_pack *pack, struct got_packidx *packidx)
1077 fae7e038 2022-05-07 stsp {
1078 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
1079 fae7e038 2022-05-07 stsp struct got_object_idset *idset;
1080 fae7e038 2022-05-07 stsp struct search_deltas_arg sda;
1081 fae7e038 2022-05-07 stsp
1082 fae7e038 2022-05-07 stsp idset = got_object_idset_alloc();
1083 fae7e038 2022-05-07 stsp if (idset == NULL)
1084 fae7e038 2022-05-07 stsp return got_error_from_errno("got_object_idset_alloc");
1085 fae7e038 2022-05-07 stsp
1086 fae7e038 2022-05-07 stsp err = recv_object_ids(idset, ibuf);
1087 fae7e038 2022-05-07 stsp if (err)
1088 fae7e038 2022-05-07 stsp return err;
1089 fae7e038 2022-05-07 stsp
1090 fae7e038 2022-05-07 stsp memset(&sda, 0, sizeof(sda));
1091 fae7e038 2022-05-07 stsp sda.ibuf = ibuf;
1092 fae7e038 2022-05-07 stsp sda.idset = idset;
1093 fae7e038 2022-05-07 stsp sda.pack = pack;
1094 fae7e038 2022-05-07 stsp sda.packidx = packidx;
1095 fae7e038 2022-05-07 stsp err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1096 fae7e038 2022-05-07 stsp if (err)
1097 fae7e038 2022-05-07 stsp goto done;
1098 fae7e038 2022-05-07 stsp
1099 fae7e038 2022-05-07 stsp if (sda.ndeltas > 0) {
1100 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1101 fae7e038 2022-05-07 stsp sda.ndeltas);
1102 fae7e038 2022-05-07 stsp if (err)
1103 fae7e038 2022-05-07 stsp goto done;
1104 fae7e038 2022-05-07 stsp }
1105 fae7e038 2022-05-07 stsp
1106 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas_done(ibuf);
1107 fae7e038 2022-05-07 stsp done:
1108 fae7e038 2022-05-07 stsp got_object_idset_free(idset);
1109 fae7e038 2022-05-07 stsp return err;
1110 fae7e038 2022-05-07 stsp }
1111 fae7e038 2022-05-07 stsp
1112 fae7e038 2022-05-07 stsp static const struct got_error *
1113 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1114 876c234b 2018-09-10 stsp {
1115 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1116 876c234b 2018-09-10 stsp struct imsg imsg;
1117 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1118 876c234b 2018-09-10 stsp size_t datalen;
1119 876c234b 2018-09-10 stsp struct got_packidx *p;
1120 876c234b 2018-09-10 stsp
1121 876c234b 2018-09-10 stsp *packidx = NULL;
1122 876c234b 2018-09-10 stsp
1123 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1124 876c234b 2018-09-10 stsp if (err)
1125 876c234b 2018-09-10 stsp return err;
1126 876c234b 2018-09-10 stsp
1127 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
1128 876c234b 2018-09-10 stsp if (p == NULL) {
1129 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1130 876c234b 2018-09-10 stsp goto done;
1131 876c234b 2018-09-10 stsp }
1132 876c234b 2018-09-10 stsp
1133 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1134 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1135 876c234b 2018-09-10 stsp goto done;
1136 876c234b 2018-09-10 stsp }
1137 876c234b 2018-09-10 stsp
1138 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1139 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1140 876c234b 2018-09-10 stsp goto done;
1141 876c234b 2018-09-10 stsp }
1142 876c234b 2018-09-10 stsp
1143 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1144 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
1145 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1146 876c234b 2018-09-10 stsp goto done;
1147 876c234b 2018-09-10 stsp }
1148 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1149 876c234b 2018-09-10 stsp
1150 876c234b 2018-09-10 stsp p->len = ipackidx.len;
1151 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
1152 876c234b 2018-09-10 stsp if (p->fd == -1) {
1153 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1154 56bef47a 2018-09-15 stsp goto done;
1155 56bef47a 2018-09-15 stsp }
1156 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
1157 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1158 876c234b 2018-09-10 stsp goto done;
1159 876c234b 2018-09-10 stsp }
1160 876c234b 2018-09-10 stsp
1161 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1162 1c28a361 2022-10-25 op if (p->len > 0 && p->len <= SIZE_MAX) {
1163 1c28a361 2022-10-25 op p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1164 1c28a361 2022-10-25 op if (p->map == MAP_FAILED)
1165 1c28a361 2022-10-25 op p->map = NULL; /* fall back to read(2) */
1166 1c28a361 2022-10-25 op }
1167 876c234b 2018-09-10 stsp #endif
1168 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1169 876c234b 2018-09-10 stsp done:
1170 876c234b 2018-09-10 stsp if (err) {
1171 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1172 876c234b 2018-09-10 stsp close(imsg.fd);
1173 876c234b 2018-09-10 stsp got_packidx_close(p);
1174 876c234b 2018-09-10 stsp } else
1175 876c234b 2018-09-10 stsp *packidx = p;
1176 876c234b 2018-09-10 stsp imsg_free(&imsg);
1177 0ab4c957 2022-06-13 stsp return err;
1178 0ab4c957 2022-06-13 stsp }
1179 0ab4c957 2022-06-13 stsp
1180 0ab4c957 2022-06-13 stsp static const struct got_error *
1181 0ab4c957 2022-06-13 stsp send_tree_enumeration_done(struct imsgbuf *ibuf)
1182 0ab4c957 2022-06-13 stsp {
1183 0ab4c957 2022-06-13 stsp if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1184 0ab4c957 2022-06-13 stsp NULL, 0) == -1)
1185 0ab4c957 2022-06-13 stsp return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1186 0ab4c957 2022-06-13 stsp
1187 0ab4c957 2022-06-13 stsp return got_privsep_flush_imsg(ibuf);
1188 0ab4c957 2022-06-13 stsp }
1189 0ab4c957 2022-06-13 stsp
1190 0ab4c957 2022-06-13 stsp struct enumerated_tree {
1191 0ab4c957 2022-06-13 stsp struct got_object_id id;
1192 0ab4c957 2022-06-13 stsp char *path;
1193 0ab4c957 2022-06-13 stsp uint8_t *buf;
1194 0ab4c957 2022-06-13 stsp struct got_parsed_tree_entry *entries;
1195 0ab4c957 2022-06-13 stsp int nentries;
1196 0ab4c957 2022-06-13 stsp };
1197 0ab4c957 2022-06-13 stsp
1198 0ab4c957 2022-06-13 stsp static const struct got_error *
1199 0ab4c957 2022-06-13 stsp enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1200 0ab4c957 2022-06-13 stsp struct got_object_id *tree_id,
1201 0ab4c957 2022-06-13 stsp const char *path, struct got_pack *pack, struct got_packidx *packidx,
1202 0ab4c957 2022-06-13 stsp struct got_object_cache *objcache, struct got_object_idset *idset,
1203 0ab4c957 2022-06-13 stsp struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1204 0ab4c957 2022-06-13 stsp {
1205 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1206 0ab4c957 2022-06-13 stsp struct got_object_id_queue ids;
1207 0ab4c957 2022-06-13 stsp struct got_object_qid *qid;
1208 0ab4c957 2022-06-13 stsp uint8_t *buf = NULL;
1209 cdfe5501 2023-04-24 stsp size_t len = 0;
1210 0ab4c957 2022-06-13 stsp struct got_parsed_tree_entry *entries = NULL;
1211 d294b1dc 2022-10-18 stsp size_t nentries = 0, nentries_alloc = 0, i;
1212 0ab4c957 2022-06-13 stsp struct enumerated_tree *tree;
1213 0ab4c957 2022-06-13 stsp
1214 0ab4c957 2022-06-13 stsp *ntrees = 0;
1215 0ab4c957 2022-06-13 stsp *have_all_entries = 1;
1216 0ab4c957 2022-06-13 stsp STAILQ_INIT(&ids);
1217 0ab4c957 2022-06-13 stsp
1218 0ab4c957 2022-06-13 stsp err = got_object_qid_alloc_partial(&qid);
1219 0ab4c957 2022-06-13 stsp if (err)
1220 0ab4c957 2022-06-13 stsp return err;
1221 076fbedc 2023-02-19 op memcpy(&qid->id, tree_id, sizeof(*tree_id));
1222 0ab4c957 2022-06-13 stsp qid->data = strdup(path);
1223 0ab4c957 2022-06-13 stsp if (qid->data == NULL) {
1224 0ab4c957 2022-06-13 stsp err = got_error_from_errno("strdup");
1225 0ab4c957 2022-06-13 stsp goto done;
1226 0ab4c957 2022-06-13 stsp }
1227 0ab4c957 2022-06-13 stsp STAILQ_INSERT_TAIL(&ids, qid, entry);
1228 0ab4c957 2022-06-13 stsp qid = NULL;
1229 0ab4c957 2022-06-13 stsp
1230 0ab4c957 2022-06-13 stsp /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1231 0ab4c957 2022-06-13 stsp do {
1232 0ab4c957 2022-06-13 stsp const char *path;
1233 0ab4c957 2022-06-13 stsp int idx, i;
1234 0ab4c957 2022-06-13 stsp
1235 0ab4c957 2022-06-13 stsp if (sigint_received) {
1236 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_CANCELLED);
1237 0ab4c957 2022-06-13 stsp goto done;
1238 0ab4c957 2022-06-13 stsp }
1239 0ab4c957 2022-06-13 stsp
1240 0ab4c957 2022-06-13 stsp qid = STAILQ_FIRST(&ids);
1241 0ab4c957 2022-06-13 stsp STAILQ_REMOVE_HEAD(&ids, entry);
1242 0ab4c957 2022-06-13 stsp path = qid->data;
1243 0ab4c957 2022-06-13 stsp
1244 0ab4c957 2022-06-13 stsp idx = got_packidx_get_object_idx(packidx, &qid->id);
1245 0ab4c957 2022-06-13 stsp if (idx == -1) {
1246 0ab4c957 2022-06-13 stsp *have_all_entries = 0;
1247 0ab4c957 2022-06-13 stsp break;
1248 0ab4c957 2022-06-13 stsp }
1249 0ab4c957 2022-06-13 stsp
1250 cdfe5501 2023-04-24 stsp err = open_tree(&buf, &len, pack, packidx, idx, &qid->id,
1251 cdfe5501 2023-04-24 stsp objcache);
1252 0ab4c957 2022-06-13 stsp if (err) {
1253 0ab4c957 2022-06-13 stsp if (err->code != GOT_ERR_NO_OBJ)
1254 0ab4c957 2022-06-13 stsp goto done;
1255 0ab4c957 2022-06-13 stsp }
1256 0ab4c957 2022-06-13 stsp
1257 cdfe5501 2023-04-24 stsp err = got_object_parse_tree(&entries, &nentries,
1258 cdfe5501 2023-04-24 stsp &nentries_alloc, buf, len);
1259 cdfe5501 2023-04-24 stsp if (err)
1260 cdfe5501 2023-04-24 stsp goto done;
1261 cdfe5501 2023-04-24 stsp
1262 0ab4c957 2022-06-13 stsp err = got_object_idset_add(idset, &qid->id, NULL);
1263 0ab4c957 2022-06-13 stsp if (err)
1264 0ab4c957 2022-06-13 stsp goto done;
1265 0ab4c957 2022-06-13 stsp
1266 0ab4c957 2022-06-13 stsp for (i = 0; i < nentries; i++) {
1267 0ab4c957 2022-06-13 stsp struct got_object_qid *eqid = NULL;
1268 0ab4c957 2022-06-13 stsp struct got_parsed_tree_entry *pte = &entries[i];
1269 0ab4c957 2022-06-13 stsp char *p;
1270 0ab4c957 2022-06-13 stsp
1271 0ab4c957 2022-06-13 stsp if (!S_ISDIR(pte->mode))
1272 0ab4c957 2022-06-13 stsp continue;
1273 0ab4c957 2022-06-13 stsp
1274 0ab4c957 2022-06-13 stsp err = got_object_qid_alloc_partial(&eqid);
1275 0ab4c957 2022-06-13 stsp if (err)
1276 0ab4c957 2022-06-13 stsp goto done;
1277 0ab4c957 2022-06-13 stsp memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1278 0ab4c957 2022-06-13 stsp
1279 0ab4c957 2022-06-13 stsp if (got_object_idset_contains(idset, &eqid->id)) {
1280 0ab4c957 2022-06-13 stsp got_object_qid_free(eqid);
1281 0ab4c957 2022-06-13 stsp continue;
1282 0ab4c957 2022-06-13 stsp }
1283 0ab4c957 2022-06-13 stsp
1284 0ab4c957 2022-06-13 stsp if (asprintf(&p, "%s%s%s", path,
1285 0ab4c957 2022-06-13 stsp got_path_is_root_dir(path) ? "" : "/",
1286 0ab4c957 2022-06-13 stsp pte->name) == -1) {
1287 0ab4c957 2022-06-13 stsp err = got_error_from_errno("asprintf");
1288 0ab4c957 2022-06-13 stsp got_object_qid_free(eqid);
1289 0ab4c957 2022-06-13 stsp goto done;
1290 0ab4c957 2022-06-13 stsp }
1291 0ab4c957 2022-06-13 stsp eqid->data = p;
1292 0ab4c957 2022-06-13 stsp STAILQ_INSERT_TAIL(&ids, eqid, entry);
1293 0ab4c957 2022-06-13 stsp }
1294 0ab4c957 2022-06-13 stsp
1295 0ab4c957 2022-06-13 stsp if (*ntrees >= *nalloc) {
1296 0ab4c957 2022-06-13 stsp struct enumerated_tree *new;
1297 0ab4c957 2022-06-13 stsp new = recallocarray(*trees, *nalloc, *nalloc + 16,
1298 0ab4c957 2022-06-13 stsp sizeof(*new));
1299 0ab4c957 2022-06-13 stsp if (new == NULL) {
1300 0ab4c957 2022-06-13 stsp err = got_error_from_errno("malloc");
1301 0ab4c957 2022-06-13 stsp goto done;
1302 0ab4c957 2022-06-13 stsp }
1303 0ab4c957 2022-06-13 stsp *trees = new;
1304 0ab4c957 2022-06-13 stsp *nalloc += 16;
1305 0ab4c957 2022-06-13 stsp }
1306 0ab4c957 2022-06-13 stsp tree = &(*trees)[*ntrees];
1307 0ab4c957 2022-06-13 stsp (*ntrees)++;
1308 0ab4c957 2022-06-13 stsp memcpy(&tree->id, &qid->id, sizeof(tree->id));
1309 0ab4c957 2022-06-13 stsp tree->path = qid->data;
1310 0ab4c957 2022-06-13 stsp tree->buf = buf;
1311 0ab4c957 2022-06-13 stsp buf = NULL;
1312 0ab4c957 2022-06-13 stsp tree->entries = entries;
1313 0ab4c957 2022-06-13 stsp entries = NULL;
1314 d294b1dc 2022-10-18 stsp nentries_alloc = 0;
1315 0ab4c957 2022-06-13 stsp tree->nentries = nentries;
1316 d294b1dc 2022-10-18 stsp nentries = 0;
1317 0ab4c957 2022-06-13 stsp
1318 0ab4c957 2022-06-13 stsp got_object_qid_free(qid);
1319 0ab4c957 2022-06-13 stsp qid = NULL;
1320 0ab4c957 2022-06-13 stsp } while (!STAILQ_EMPTY(&ids));
1321 0ab4c957 2022-06-13 stsp
1322 0ab4c957 2022-06-13 stsp if (*have_all_entries) {
1323 0ab4c957 2022-06-13 stsp int i;
1324 0ab4c957 2022-06-13 stsp /*
1325 0ab4c957 2022-06-13 stsp * We have managed to traverse all entries in the hierarchy.
1326 0ab4c957 2022-06-13 stsp * Tell the main process what we have found.
1327 0ab4c957 2022-06-13 stsp */
1328 0ab4c957 2022-06-13 stsp for (i = 0; i < *ntrees; i++) {
1329 0ab4c957 2022-06-13 stsp tree = &(*trees)[i];
1330 0ab4c957 2022-06-13 stsp err = got_privsep_send_enumerated_tree(totlen,
1331 0ab4c957 2022-06-13 stsp ibuf, &tree->id, tree->path, tree->entries,
1332 0ab4c957 2022-06-13 stsp tree->nentries);
1333 0ab4c957 2022-06-13 stsp if (err)
1334 0ab4c957 2022-06-13 stsp goto done;
1335 0ab4c957 2022-06-13 stsp free(tree->buf);
1336 0ab4c957 2022-06-13 stsp tree->buf = NULL;
1337 0ab4c957 2022-06-13 stsp free(tree->path);
1338 0ab4c957 2022-06-13 stsp tree->path = NULL;
1339 0ab4c957 2022-06-13 stsp free(tree->entries);
1340 0ab4c957 2022-06-13 stsp tree->entries = NULL;
1341 0ab4c957 2022-06-13 stsp }
1342 0ab4c957 2022-06-13 stsp *ntrees = 0; /* don't loop again below to free memory */
1343 0ab4c957 2022-06-13 stsp
1344 0ab4c957 2022-06-13 stsp err = send_tree_enumeration_done(ibuf);
1345 0ab4c957 2022-06-13 stsp } else {
1346 0ab4c957 2022-06-13 stsp /*
1347 0ab4c957 2022-06-13 stsp * We can only load fully packed tree hierarchies on
1348 0ab4c957 2022-06-13 stsp * behalf of the main process, otherwise the main process
1349 0ab4c957 2022-06-13 stsp * gets a wrong idea about which tree objects have
1350 0ab4c957 2022-06-13 stsp * already been traversed.
1351 0ab4c957 2022-06-13 stsp * Indicate a missing entry for the root of this tree.
1352 0ab4c957 2022-06-13 stsp * The main process should continue by loading this
1353 0ab4c957 2022-06-13 stsp * entire tree the slow way.
1354 0ab4c957 2022-06-13 stsp */
1355 0ab4c957 2022-06-13 stsp err = got_privsep_send_enumerated_tree(totlen, ibuf,
1356 0ab4c957 2022-06-13 stsp tree_id, "/", NULL, -1);
1357 0ab4c957 2022-06-13 stsp if (err)
1358 0ab4c957 2022-06-13 stsp goto done;
1359 0ab4c957 2022-06-13 stsp }
1360 0ab4c957 2022-06-13 stsp done:
1361 0ab4c957 2022-06-13 stsp free(buf);
1362 0ab4c957 2022-06-13 stsp free(entries);
1363 0ab4c957 2022-06-13 stsp for (i = 0; i < *ntrees; i++) {
1364 0ab4c957 2022-06-13 stsp tree = &(*trees)[i];
1365 0ab4c957 2022-06-13 stsp free(tree->buf);
1366 0ab4c957 2022-06-13 stsp tree->buf = NULL;
1367 0ab4c957 2022-06-13 stsp free(tree->path);
1368 0ab4c957 2022-06-13 stsp tree->path = NULL;
1369 0ab4c957 2022-06-13 stsp free(tree->entries);
1370 0ab4c957 2022-06-13 stsp tree->entries = NULL;
1371 0ab4c957 2022-06-13 stsp }
1372 0ab4c957 2022-06-13 stsp if (qid)
1373 0ab4c957 2022-06-13 stsp free(qid->data);
1374 0ab4c957 2022-06-13 stsp got_object_qid_free(qid);
1375 0ab4c957 2022-06-13 stsp got_object_id_queue_free(&ids);
1376 0ab4c957 2022-06-13 stsp if (err) {
1377 0ab4c957 2022-06-13 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1378 0ab4c957 2022-06-13 stsp err = NULL;
1379 0ab4c957 2022-06-13 stsp else
1380 0ab4c957 2022-06-13 stsp got_privsep_send_error(ibuf, err);
1381 0ab4c957 2022-06-13 stsp }
1382 0ab4c957 2022-06-13 stsp
1383 cee6a7ea 2022-06-07 stsp return err;
1384 cee6a7ea 2022-06-07 stsp }
1385 cee6a7ea 2022-06-07 stsp
1386 cee6a7ea 2022-06-07 stsp static const struct got_error *
1387 0ab4c957 2022-06-13 stsp enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1388 0ab4c957 2022-06-13 stsp struct got_pack *pack, struct got_packidx *packidx,
1389 0ab4c957 2022-06-13 stsp struct got_object_cache *objcache)
1390 0ab4c957 2022-06-13 stsp {
1391 0ab4c957 2022-06-13 stsp const struct got_error *err = NULL;
1392 0ab4c957 2022-06-13 stsp struct got_object_id_queue commit_ids;
1393 0ab4c957 2022-06-13 stsp const struct got_object_id_queue *parents = NULL;
1394 0ab4c957 2022-06-13 stsp struct got_object_qid *qid = NULL;
1395 0ab4c957 2022-06-13 stsp struct got_object *obj = NULL;
1396 0ab4c957 2022-06-13 stsp struct got_commit_object *commit = NULL;
1397 0ab4c957 2022-06-13 stsp struct got_object_id *tree_id = NULL;
1398 0ab4c957 2022-06-13 stsp size_t totlen = 0;
1399 0a618912 2023-01-27 stsp struct got_object_idset *idset, *queued_ids = NULL;
1400 0ab4c957 2022-06-13 stsp int i, idx, have_all_entries = 1;
1401 0ab4c957 2022-06-13 stsp struct enumerated_tree *trees = NULL;
1402 0ab4c957 2022-06-13 stsp size_t ntrees = 0, nalloc = 16;
1403 0ab4c957 2022-06-13 stsp
1404 0ab4c957 2022-06-13 stsp STAILQ_INIT(&commit_ids);
1405 0ab4c957 2022-06-13 stsp
1406 ffe3518f 2022-06-14 stsp trees = calloc(nalloc, sizeof(*trees));
1407 0ab4c957 2022-06-13 stsp if (trees == NULL)
1408 0ab4c957 2022-06-13 stsp return got_error_from_errno("calloc");
1409 0ab4c957 2022-06-13 stsp
1410 0ab4c957 2022-06-13 stsp idset = got_object_idset_alloc();
1411 0ab4c957 2022-06-13 stsp if (idset == NULL) {
1412 0ab4c957 2022-06-13 stsp err = got_error_from_errno("got_object_idset_alloc");
1413 0ab4c957 2022-06-13 stsp goto done;
1414 0ab4c957 2022-06-13 stsp }
1415 0ab4c957 2022-06-13 stsp
1416 0a618912 2023-01-27 stsp queued_ids = got_object_idset_alloc();
1417 0a618912 2023-01-27 stsp if (queued_ids == NULL) {
1418 0a618912 2023-01-27 stsp err = got_error_from_errno("got_object_idset_alloc");
1419 0a618912 2023-01-27 stsp goto done;
1420 0a618912 2023-01-27 stsp }
1421 0a618912 2023-01-27 stsp
1422 0a618912 2023-01-27 stsp err = recv_object_id_queue(&commit_ids, queued_ids, ibuf);
1423 0ab4c957 2022-06-13 stsp if (err)
1424 0ab4c957 2022-06-13 stsp goto done;
1425 0ab4c957 2022-06-13 stsp
1426 a5e587e0 2022-06-14 stsp if (STAILQ_EMPTY(&commit_ids)) {
1427 a5e587e0 2022-06-14 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1428 a5e587e0 2022-06-14 stsp goto done;
1429 a5e587e0 2022-06-14 stsp }
1430 a5e587e0 2022-06-14 stsp
1431 0ab4c957 2022-06-13 stsp err = recv_object_ids(idset, ibuf);
1432 0ab4c957 2022-06-13 stsp if (err)
1433 0ab4c957 2022-06-13 stsp goto done;
1434 0ab4c957 2022-06-13 stsp
1435 0ab4c957 2022-06-13 stsp while (!STAILQ_EMPTY(&commit_ids)) {
1436 0ab4c957 2022-06-13 stsp if (sigint_received) {
1437 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_CANCELLED);
1438 0ab4c957 2022-06-13 stsp goto done;
1439 0ab4c957 2022-06-13 stsp }
1440 0ab4c957 2022-06-13 stsp
1441 0ab4c957 2022-06-13 stsp qid = STAILQ_FIRST(&commit_ids);
1442 0ab4c957 2022-06-13 stsp STAILQ_REMOVE_HEAD(&commit_ids, entry);
1443 0ab4c957 2022-06-13 stsp
1444 0ab4c957 2022-06-13 stsp if (got_object_idset_contains(idset, &qid->id)) {
1445 0ab4c957 2022-06-13 stsp got_object_qid_free(qid);
1446 0ab4c957 2022-06-13 stsp qid = NULL;
1447 0ab4c957 2022-06-13 stsp continue;
1448 0ab4c957 2022-06-13 stsp }
1449 0ab4c957 2022-06-13 stsp
1450 0ab4c957 2022-06-13 stsp idx = got_packidx_get_object_idx(packidx, &qid->id);
1451 db9b9b1c 2022-06-14 stsp if (idx == -1) {
1452 db9b9b1c 2022-06-14 stsp have_all_entries = 0;
1453 0ab4c957 2022-06-13 stsp break;
1454 db9b9b1c 2022-06-14 stsp }
1455 0ab4c957 2022-06-13 stsp
1456 0ab4c957 2022-06-13 stsp err = open_object(&obj, pack, packidx, idx, &qid->id,
1457 0ab4c957 2022-06-13 stsp objcache);
1458 0ab4c957 2022-06-13 stsp if (err)
1459 0ab4c957 2022-06-13 stsp goto done;
1460 0ab4c957 2022-06-13 stsp if (obj->type == GOT_OBJ_TYPE_TAG) {
1461 0ab4c957 2022-06-13 stsp struct got_tag_object *tag;
1462 0ab4c957 2022-06-13 stsp uint8_t *buf;
1463 0ab4c957 2022-06-13 stsp size_t len;
1464 0ab4c957 2022-06-13 stsp err = got_packfile_extract_object_to_mem(&buf,
1465 0ab4c957 2022-06-13 stsp &len, obj, pack);
1466 0ab4c957 2022-06-13 stsp if (err)
1467 0ab4c957 2022-06-13 stsp goto done;
1468 0ab4c957 2022-06-13 stsp obj->size = len;
1469 0ab4c957 2022-06-13 stsp err = got_object_parse_tag(&tag, buf, len);
1470 0ab4c957 2022-06-13 stsp if (err) {
1471 0ab4c957 2022-06-13 stsp free(buf);
1472 0ab4c957 2022-06-13 stsp goto done;
1473 0ab4c957 2022-06-13 stsp }
1474 0ab4c957 2022-06-13 stsp idx = got_packidx_get_object_idx(packidx, &tag->id);
1475 db9b9b1c 2022-06-14 stsp if (idx == -1) {
1476 db9b9b1c 2022-06-14 stsp have_all_entries = 0;
1477 0ab4c957 2022-06-13 stsp break;
1478 db9b9b1c 2022-06-14 stsp }
1479 0ab4c957 2022-06-13 stsp err = open_commit(&commit, pack, packidx, idx,
1480 0ab4c957 2022-06-13 stsp &tag->id, objcache);
1481 0ab4c957 2022-06-13 stsp got_object_tag_close(tag);
1482 0ab4c957 2022-06-13 stsp free(buf);
1483 0ab4c957 2022-06-13 stsp if (err)
1484 0ab4c957 2022-06-13 stsp goto done;
1485 0ab4c957 2022-06-13 stsp } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1486 0ab4c957 2022-06-13 stsp err = open_commit(&commit, pack, packidx, idx,
1487 0ab4c957 2022-06-13 stsp &qid->id, objcache);
1488 0ab4c957 2022-06-13 stsp if (err)
1489 0ab4c957 2022-06-13 stsp goto done;
1490 0ab4c957 2022-06-13 stsp } else {
1491 0ab4c957 2022-06-13 stsp err = got_error(GOT_ERR_OBJ_TYPE);
1492 0ab4c957 2022-06-13 stsp goto done;
1493 0ab4c957 2022-06-13 stsp }
1494 0ab4c957 2022-06-13 stsp got_object_close(obj);
1495 0ab4c957 2022-06-13 stsp obj = NULL;
1496 0ab4c957 2022-06-13 stsp
1497 0ab4c957 2022-06-13 stsp err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1498 0ab4c957 2022-06-13 stsp got_object_commit_get_committer_time(commit));
1499 0ab4c957 2022-06-13 stsp if (err)
1500 0ab4c957 2022-06-13 stsp goto done;
1501 0ab4c957 2022-06-13 stsp
1502 0ab4c957 2022-06-13 stsp tree_id = got_object_commit_get_tree_id(commit);
1503 0ab4c957 2022-06-13 stsp idx = got_packidx_get_object_idx(packidx, tree_id);
1504 0ab4c957 2022-06-13 stsp if (idx == -1) {
1505 db9b9b1c 2022-06-14 stsp have_all_entries = 0;
1506 0ab4c957 2022-06-13 stsp err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1507 0ab4c957 2022-06-13 stsp tree_id, "/", NULL, -1);
1508 0ab4c957 2022-06-13 stsp if (err)
1509 0ab4c957 2022-06-13 stsp goto done;
1510 0ab4c957 2022-06-13 stsp break;
1511 0ab4c957 2022-06-13 stsp }
1512 0ab4c957 2022-06-13 stsp
1513 0ab4c957 2022-06-13 stsp if (got_object_idset_contains(idset, tree_id)) {
1514 0ab4c957 2022-06-13 stsp got_object_qid_free(qid);
1515 0ab4c957 2022-06-13 stsp qid = NULL;
1516 e1380e28 2023-01-27 stsp err = send_tree_enumeration_done(ibuf);
1517 e1380e28 2023-01-27 stsp if (err)
1518 e1380e28 2023-01-27 stsp goto done;
1519 0ab4c957 2022-06-13 stsp continue;
1520 0ab4c957 2022-06-13 stsp }
1521 0ab4c957 2022-06-13 stsp
1522 89a34d6e 2022-06-18 stsp err = enumerate_tree(&have_all_entries, ibuf, &totlen,
1523 89a34d6e 2022-06-18 stsp tree_id, "/", pack, packidx, objcache, idset,
1524 89a34d6e 2022-06-18 stsp &trees, &nalloc, &ntrees);
1525 0ab4c957 2022-06-13 stsp if (err)
1526 0ab4c957 2022-06-13 stsp goto done;
1527 0ab4c957 2022-06-13 stsp
1528 0ab4c957 2022-06-13 stsp if (!have_all_entries)
1529 0ab4c957 2022-06-13 stsp break;
1530 0ab4c957 2022-06-13 stsp
1531 0ab4c957 2022-06-13 stsp got_object_qid_free(qid);
1532 0ab4c957 2022-06-13 stsp qid = NULL;
1533 0ab4c957 2022-06-13 stsp
1534 0ab4c957 2022-06-13 stsp parents = got_object_commit_get_parent_ids(commit);
1535 0ab4c957 2022-06-13 stsp if (parents) {
1536 0ab4c957 2022-06-13 stsp struct got_object_qid *pid;
1537 0ab4c957 2022-06-13 stsp STAILQ_FOREACH(pid, parents, entry) {
1538 0ab4c957 2022-06-13 stsp if (got_object_idset_contains(idset, &pid->id))
1539 0ab4c957 2022-06-13 stsp continue;
1540 0a618912 2023-01-27 stsp if (got_object_idset_contains(queued_ids, &pid->id))
1541 0a618912 2023-01-27 stsp continue;
1542 0ab4c957 2022-06-13 stsp err = got_object_qid_alloc_partial(&qid);
1543 0ab4c957 2022-06-13 stsp if (err)
1544 0ab4c957 2022-06-13 stsp goto done;
1545 0ab4c957 2022-06-13 stsp memcpy(&qid->id, &pid->id, sizeof(qid->id));
1546 0ab4c957 2022-06-13 stsp STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1547 0ab4c957 2022-06-13 stsp qid = NULL;
1548 0ab4c957 2022-06-13 stsp }
1549 0ab4c957 2022-06-13 stsp }
1550 0ab4c957 2022-06-13 stsp
1551 0ab4c957 2022-06-13 stsp got_object_commit_close(commit);
1552 0ab4c957 2022-06-13 stsp commit = NULL;
1553 0ab4c957 2022-06-13 stsp }
1554 0ab4c957 2022-06-13 stsp
1555 0ab4c957 2022-06-13 stsp if (have_all_entries) {
1556 0ab4c957 2022-06-13 stsp err = got_privsep_send_object_enumeration_done(ibuf);
1557 db9b9b1c 2022-06-14 stsp if (err)
1558 db9b9b1c 2022-06-14 stsp goto done;
1559 db9b9b1c 2022-06-14 stsp } else {
1560 db9b9b1c 2022-06-14 stsp err = got_privsep_send_object_enumeration_incomplete(ibuf);
1561 0ab4c957 2022-06-13 stsp if (err)
1562 0ab4c957 2022-06-13 stsp goto done;
1563 0ab4c957 2022-06-13 stsp }
1564 0ab4c957 2022-06-13 stsp done:
1565 0ab4c957 2022-06-13 stsp if (obj)
1566 0ab4c957 2022-06-13 stsp got_object_close(obj);
1567 0ab4c957 2022-06-13 stsp if (commit)
1568 0ab4c957 2022-06-13 stsp got_object_commit_close(commit);
1569 0ab4c957 2022-06-13 stsp got_object_qid_free(qid);
1570 0ab4c957 2022-06-13 stsp got_object_id_queue_free(&commit_ids);
1571 0ab4c957 2022-06-13 stsp if (idset)
1572 0ab4c957 2022-06-13 stsp got_object_idset_free(idset);
1573 0a618912 2023-01-27 stsp if (queued_ids)
1574 0a618912 2023-01-27 stsp got_object_idset_free(queued_ids);
1575 0ab4c957 2022-06-13 stsp for (i = 0; i < ntrees; i++) {
1576 0ab4c957 2022-06-13 stsp struct enumerated_tree *tree = &trees[i];
1577 0ab4c957 2022-06-13 stsp free(tree->buf);
1578 0ab4c957 2022-06-13 stsp free(tree->path);
1579 0ab4c957 2022-06-13 stsp free(tree->entries);
1580 0ab4c957 2022-06-13 stsp }
1581 0ab4c957 2022-06-13 stsp free(trees);
1582 0ab4c957 2022-06-13 stsp return err;
1583 0ab4c957 2022-06-13 stsp }
1584 0ab4c957 2022-06-13 stsp
1585 61af9b21 2022-06-28 stsp enum findtwixt_color {
1586 61af9b21 2022-06-28 stsp COLOR_KEEP = 0,
1587 61af9b21 2022-06-28 stsp COLOR_DROP,
1588 61af9b21 2022-06-28 stsp COLOR_SKIP,
1589 61af9b21 2022-06-28 stsp COLOR_MAX,
1590 61af9b21 2022-06-28 stsp };
1591 61af9b21 2022-06-28 stsp
1592 0ab4c957 2022-06-13 stsp static const struct got_error *
1593 61af9b21 2022-06-28 stsp paint_commit(struct got_object_qid *qid, intptr_t color)
1594 61af9b21 2022-06-28 stsp {
1595 61af9b21 2022-06-28 stsp if (color < 0 || color >= COLOR_MAX)
1596 61af9b21 2022-06-28 stsp return got_error(GOT_ERR_RANGE);
1597 61af9b21 2022-06-28 stsp
1598 61af9b21 2022-06-28 stsp qid->data = (void *)color;
1599 61af9b21 2022-06-28 stsp return NULL;
1600 61af9b21 2022-06-28 stsp }
1601 61af9b21 2022-06-28 stsp
1602 61af9b21 2022-06-28 stsp static const struct got_error *
1603 61af9b21 2022-06-28 stsp queue_commit_id(struct got_object_id_queue *ids, struct got_object_id *id,
1604 61af9b21 2022-06-28 stsp intptr_t color)
1605 61af9b21 2022-06-28 stsp {
1606 61af9b21 2022-06-28 stsp const struct got_error *err;
1607 61af9b21 2022-06-28 stsp struct got_object_qid *qid;
1608 61af9b21 2022-06-28 stsp
1609 61af9b21 2022-06-28 stsp err = got_object_qid_alloc_partial(&qid);
1610 61af9b21 2022-06-28 stsp if (err)
1611 61af9b21 2022-06-28 stsp return err;
1612 61af9b21 2022-06-28 stsp
1613 61af9b21 2022-06-28 stsp memcpy(&qid->id, id, sizeof(qid->id));
1614 61af9b21 2022-06-28 stsp STAILQ_INSERT_TAIL(ids, qid, entry);
1615 61af9b21 2022-06-28 stsp return paint_commit(qid, color);
1616 61af9b21 2022-06-28 stsp }
1617 61af9b21 2022-06-28 stsp
1618 61af9b21 2022-06-28 stsp static const struct got_error *
1619 61af9b21 2022-06-28 stsp paint_commits(struct got_object_id_queue *ids, int *nids,
1620 61af9b21 2022-06-28 stsp struct got_object_idset *keep, struct got_object_idset *drop,
1621 61af9b21 2022-06-28 stsp struct got_object_idset *skip, struct got_pack *pack,
1622 61af9b21 2022-06-28 stsp struct got_packidx *packidx, struct imsgbuf *ibuf,
1623 61af9b21 2022-06-28 stsp struct got_object_cache *objcache)
1624 61af9b21 2022-06-28 stsp {
1625 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
1626 61af9b21 2022-06-28 stsp struct got_commit_object *commit = NULL;
1627 61af9b21 2022-06-28 stsp struct got_object_id_queue painted;
1628 61af9b21 2022-06-28 stsp const struct got_object_id_queue *parents;
1629 61af9b21 2022-06-28 stsp struct got_object_qid *qid = NULL;
1630 61af9b21 2022-06-28 stsp int nqueued = *nids, nskip = 0, npainted = 0;
1631 61af9b21 2022-06-28 stsp
1632 61af9b21 2022-06-28 stsp STAILQ_INIT(&painted);
1633 61af9b21 2022-06-28 stsp
1634 61af9b21 2022-06-28 stsp while (!STAILQ_EMPTY(ids) && nskip != nqueued) {
1635 61af9b21 2022-06-28 stsp int idx;
1636 61af9b21 2022-06-28 stsp intptr_t color;
1637 61af9b21 2022-06-28 stsp
1638 61af9b21 2022-06-28 stsp if (sigint_received) {
1639 61af9b21 2022-06-28 stsp err = got_error(GOT_ERR_CANCELLED);
1640 61af9b21 2022-06-28 stsp goto done;
1641 61af9b21 2022-06-28 stsp }
1642 61af9b21 2022-06-28 stsp
1643 61af9b21 2022-06-28 stsp qid = STAILQ_FIRST(ids);
1644 61af9b21 2022-06-28 stsp idx = got_packidx_get_object_idx(packidx, &qid->id);
1645 61af9b21 2022-06-28 stsp if (idx == -1) {
1646 61af9b21 2022-06-28 stsp qid = NULL;
1647 61af9b21 2022-06-28 stsp break;
1648 61af9b21 2022-06-28 stsp }
1649 61af9b21 2022-06-28 stsp
1650 61af9b21 2022-06-28 stsp STAILQ_REMOVE_HEAD(ids, entry);
1651 61af9b21 2022-06-28 stsp nqueued--;
1652 61af9b21 2022-06-28 stsp color = (intptr_t)qid->data;
1653 61af9b21 2022-06-28 stsp if (color == COLOR_SKIP)
1654 61af9b21 2022-06-28 stsp nskip--;
1655 61af9b21 2022-06-28 stsp
1656 61af9b21 2022-06-28 stsp if (got_object_idset_contains(skip, &qid->id)) {
1657 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1658 61af9b21 2022-06-28 stsp qid = NULL;
1659 61af9b21 2022-06-28 stsp continue;
1660 61af9b21 2022-06-28 stsp }
1661 61af9b21 2022-06-28 stsp
1662 61af9b21 2022-06-28 stsp switch (color) {
1663 61af9b21 2022-06-28 stsp case COLOR_KEEP:
1664 61af9b21 2022-06-28 stsp if (got_object_idset_contains(keep, &qid->id)) {
1665 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1666 61af9b21 2022-06-28 stsp qid = NULL;
1667 61af9b21 2022-06-28 stsp continue;
1668 61af9b21 2022-06-28 stsp }
1669 61af9b21 2022-06-28 stsp if (got_object_idset_contains(drop, &qid->id)) {
1670 61af9b21 2022-06-28 stsp err = paint_commit(qid, COLOR_SKIP);
1671 61af9b21 2022-06-28 stsp if (err)
1672 61af9b21 2022-06-28 stsp goto done;
1673 61af9b21 2022-06-28 stsp }
1674 61af9b21 2022-06-28 stsp err = got_object_idset_add(keep, &qid->id, NULL);
1675 61af9b21 2022-06-28 stsp if (err)
1676 61af9b21 2022-06-28 stsp goto done;
1677 61af9b21 2022-06-28 stsp break;
1678 61af9b21 2022-06-28 stsp case COLOR_DROP:
1679 61af9b21 2022-06-28 stsp if (got_object_idset_contains(drop, &qid->id)) {
1680 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1681 61af9b21 2022-06-28 stsp qid = NULL;
1682 61af9b21 2022-06-28 stsp continue;
1683 61af9b21 2022-06-28 stsp }
1684 61af9b21 2022-06-28 stsp if (got_object_idset_contains(keep, &qid->id)) {
1685 61af9b21 2022-06-28 stsp err = paint_commit(qid, COLOR_SKIP);
1686 61af9b21 2022-06-28 stsp if (err)
1687 61af9b21 2022-06-28 stsp goto done;
1688 61af9b21 2022-06-28 stsp }
1689 61af9b21 2022-06-28 stsp err = got_object_idset_add(drop, &qid->id, NULL);
1690 61af9b21 2022-06-28 stsp if (err)
1691 61af9b21 2022-06-28 stsp goto done;
1692 61af9b21 2022-06-28 stsp break;
1693 61af9b21 2022-06-28 stsp case COLOR_SKIP:
1694 61af9b21 2022-06-28 stsp if (!got_object_idset_contains(skip, &qid->id)) {
1695 61af9b21 2022-06-28 stsp err = got_object_idset_add(skip, &qid->id,
1696 61af9b21 2022-06-28 stsp NULL);
1697 61af9b21 2022-06-28 stsp if (err)
1698 61af9b21 2022-06-28 stsp goto done;
1699 61af9b21 2022-06-28 stsp }
1700 61af9b21 2022-06-28 stsp break;
1701 61af9b21 2022-06-28 stsp default:
1702 61af9b21 2022-06-28 stsp /* should not happen */
1703 61af9b21 2022-06-28 stsp err = got_error_fmt(GOT_ERR_NOT_IMPL,
1704 756050ac 2022-07-19 op "%s invalid commit color %"PRIdPTR, __func__,
1705 756050ac 2022-07-19 op color);
1706 61af9b21 2022-06-28 stsp goto done;
1707 61af9b21 2022-06-28 stsp }
1708 61af9b21 2022-06-28 stsp
1709 61af9b21 2022-06-28 stsp err = open_commit(&commit, pack, packidx, idx, &qid->id,
1710 61af9b21 2022-06-28 stsp objcache);
1711 61af9b21 2022-06-28 stsp if (err)
1712 61af9b21 2022-06-28 stsp goto done;
1713 61af9b21 2022-06-28 stsp
1714 61af9b21 2022-06-28 stsp parents = got_object_commit_get_parent_ids(commit);
1715 61af9b21 2022-06-28 stsp if (parents) {
1716 61af9b21 2022-06-28 stsp struct got_object_qid *pid;
1717 61af9b21 2022-06-28 stsp color = (intptr_t)qid->data;
1718 61af9b21 2022-06-28 stsp STAILQ_FOREACH(pid, parents, entry) {
1719 61af9b21 2022-06-28 stsp err = queue_commit_id(ids, &pid->id, color);
1720 61af9b21 2022-06-28 stsp if (err)
1721 61af9b21 2022-06-28 stsp goto done;
1722 61af9b21 2022-06-28 stsp nqueued++;
1723 61af9b21 2022-06-28 stsp if (color == COLOR_SKIP)
1724 61af9b21 2022-06-28 stsp nskip++;
1725 61af9b21 2022-06-28 stsp }
1726 61af9b21 2022-06-28 stsp }
1727 61af9b21 2022-06-28 stsp
1728 61af9b21 2022-06-28 stsp got_object_commit_close(commit);
1729 61af9b21 2022-06-28 stsp commit = NULL;
1730 61af9b21 2022-06-28 stsp
1731 61af9b21 2022-06-28 stsp STAILQ_INSERT_TAIL(&painted, qid, entry);
1732 61af9b21 2022-06-28 stsp qid = NULL;
1733 61af9b21 2022-06-28 stsp npainted++;
1734 61af9b21 2022-06-28 stsp
1735 61af9b21 2022-06-28 stsp err = got_privsep_send_painted_commits(ibuf, &painted,
1736 61af9b21 2022-06-28 stsp &npainted, 1, 0);
1737 61af9b21 2022-06-28 stsp if (err)
1738 61af9b21 2022-06-28 stsp goto done;
1739 61af9b21 2022-06-28 stsp }
1740 61af9b21 2022-06-28 stsp
1741 61af9b21 2022-06-28 stsp err = got_privsep_send_painted_commits(ibuf, &painted, &npainted, 1, 1);
1742 61af9b21 2022-06-28 stsp if (err)
1743 61af9b21 2022-06-28 stsp goto done;
1744 61af9b21 2022-06-28 stsp
1745 61af9b21 2022-06-28 stsp *nids = nqueued;
1746 61af9b21 2022-06-28 stsp done:
1747 61af9b21 2022-06-28 stsp if (commit)
1748 61af9b21 2022-06-28 stsp got_object_commit_close(commit);
1749 61af9b21 2022-06-28 stsp got_object_qid_free(qid);
1750 61af9b21 2022-06-28 stsp return err;
1751 61af9b21 2022-06-28 stsp }
1752 61af9b21 2022-06-28 stsp
1753 61af9b21 2022-06-28 stsp static void
1754 61af9b21 2022-06-28 stsp commit_painting_free(struct got_object_idset **keep,
1755 61af9b21 2022-06-28 stsp struct got_object_idset **drop,
1756 61af9b21 2022-06-28 stsp struct got_object_idset **skip)
1757 61af9b21 2022-06-28 stsp {
1758 61af9b21 2022-06-28 stsp if (*keep) {
1759 61af9b21 2022-06-28 stsp got_object_idset_free(*keep);
1760 61af9b21 2022-06-28 stsp *keep = NULL;
1761 61af9b21 2022-06-28 stsp }
1762 61af9b21 2022-06-28 stsp if (*drop) {
1763 61af9b21 2022-06-28 stsp got_object_idset_free(*drop);
1764 61af9b21 2022-06-28 stsp *drop = NULL;
1765 61af9b21 2022-06-28 stsp }
1766 61af9b21 2022-06-28 stsp if (*skip) {
1767 5e91dae4 2022-08-30 stsp got_object_idset_free(*skip);
1768 61af9b21 2022-06-28 stsp *skip = NULL;
1769 61af9b21 2022-06-28 stsp }
1770 61af9b21 2022-06-28 stsp }
1771 61af9b21 2022-06-28 stsp
1772 61af9b21 2022-06-28 stsp static const struct got_error *
1773 61af9b21 2022-06-28 stsp commit_painting_init(struct imsgbuf *ibuf, struct got_object_idset **keep,
1774 61af9b21 2022-06-28 stsp struct got_object_idset **drop, struct got_object_idset **skip)
1775 61af9b21 2022-06-28 stsp {
1776 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
1777 61af9b21 2022-06-28 stsp
1778 61af9b21 2022-06-28 stsp *keep = got_object_idset_alloc();
1779 61af9b21 2022-06-28 stsp if (*keep == NULL) {
1780 61af9b21 2022-06-28 stsp err = got_error_from_errno("got_object_idset_alloc");
1781 61af9b21 2022-06-28 stsp goto done;
1782 61af9b21 2022-06-28 stsp }
1783 61af9b21 2022-06-28 stsp *drop = got_object_idset_alloc();
1784 61af9b21 2022-06-28 stsp if (*drop == NULL) {
1785 61af9b21 2022-06-28 stsp err = got_error_from_errno("got_object_idset_alloc");
1786 61af9b21 2022-06-28 stsp goto done;
1787 61af9b21 2022-06-28 stsp }
1788 61af9b21 2022-06-28 stsp *skip = got_object_idset_alloc();
1789 61af9b21 2022-06-28 stsp if (*skip == NULL) {
1790 61af9b21 2022-06-28 stsp err = got_error_from_errno("got_object_idset_alloc");
1791 61af9b21 2022-06-28 stsp goto done;
1792 61af9b21 2022-06-28 stsp }
1793 61af9b21 2022-06-28 stsp
1794 61af9b21 2022-06-28 stsp err = recv_object_ids(*keep, ibuf);
1795 61af9b21 2022-06-28 stsp if (err)
1796 61af9b21 2022-06-28 stsp goto done;
1797 61af9b21 2022-06-28 stsp err = recv_object_ids(*drop, ibuf);
1798 61af9b21 2022-06-28 stsp if (err)
1799 61af9b21 2022-06-28 stsp goto done;
1800 61af9b21 2022-06-28 stsp err = recv_object_ids(*skip, ibuf);
1801 61af9b21 2022-06-28 stsp if (err)
1802 61af9b21 2022-06-28 stsp goto done;
1803 61af9b21 2022-06-28 stsp
1804 61af9b21 2022-06-28 stsp done:
1805 61af9b21 2022-06-28 stsp if (err)
1806 61af9b21 2022-06-28 stsp commit_painting_free(keep, drop, skip);
1807 61af9b21 2022-06-28 stsp
1808 61af9b21 2022-06-28 stsp return err;
1809 61af9b21 2022-06-28 stsp }
1810 61af9b21 2022-06-28 stsp
1811 61af9b21 2022-06-28 stsp static const struct got_error *
1812 61af9b21 2022-06-28 stsp commit_painting_request(struct imsg *imsg, struct imsgbuf *ibuf,
1813 61af9b21 2022-06-28 stsp struct got_pack *pack, struct got_packidx *packidx,
1814 61af9b21 2022-06-28 stsp struct got_object_cache *objcache, struct got_object_idset *keep,
1815 61af9b21 2022-06-28 stsp struct got_object_idset *drop, struct got_object_idset *skip)
1816 61af9b21 2022-06-28 stsp {
1817 61af9b21 2022-06-28 stsp const struct got_error *err = NULL;
1818 61af9b21 2022-06-28 stsp struct got_imsg_commit_painting_request ireq;
1819 61af9b21 2022-06-28 stsp struct got_object_id id;
1820 61af9b21 2022-06-28 stsp size_t datalen;
1821 61af9b21 2022-06-28 stsp struct got_object_id_queue ids;
1822 61af9b21 2022-06-28 stsp int nids = 0;
1823 61af9b21 2022-06-28 stsp
1824 61af9b21 2022-06-28 stsp STAILQ_INIT(&ids);
1825 61af9b21 2022-06-28 stsp
1826 61af9b21 2022-06-28 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
1827 61af9b21 2022-06-28 stsp if (datalen != sizeof(ireq))
1828 61af9b21 2022-06-28 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
1829 61af9b21 2022-06-28 stsp memcpy(&ireq, imsg->data, sizeof(ireq));
1830 076fbedc 2023-02-19 op memcpy(&id, &ireq.id, sizeof(id));
1831 61af9b21 2022-06-28 stsp
1832 61af9b21 2022-06-28 stsp err = queue_commit_id(&ids, &id, ireq.color);
1833 61af9b21 2022-06-28 stsp if (err)
1834 61af9b21 2022-06-28 stsp return err;
1835 61af9b21 2022-06-28 stsp nids = 1;
1836 61af9b21 2022-06-28 stsp
1837 61af9b21 2022-06-28 stsp err = paint_commits(&ids, &nids, keep, drop, skip,
1838 61af9b21 2022-06-28 stsp pack, packidx, ibuf, objcache);
1839 61af9b21 2022-06-28 stsp if (err)
1840 61af9b21 2022-06-28 stsp goto done;
1841 61af9b21 2022-06-28 stsp
1842 61af9b21 2022-06-28 stsp err = got_privsep_send_painted_commits(ibuf, &ids, &nids, 0, 1);
1843 61af9b21 2022-06-28 stsp if (err)
1844 61af9b21 2022-06-28 stsp goto done;
1845 61af9b21 2022-06-28 stsp
1846 61af9b21 2022-06-28 stsp err = got_privsep_send_painting_commits_done(ibuf);
1847 61af9b21 2022-06-28 stsp done:
1848 61af9b21 2022-06-28 stsp got_object_id_queue_free(&ids);
1849 61af9b21 2022-06-28 stsp return err;
1850 61af9b21 2022-06-28 stsp }
1851 61af9b21 2022-06-28 stsp
1852 61af9b21 2022-06-28 stsp static const struct got_error *
1853 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1854 876c234b 2018-09-10 stsp {
1855 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1856 876c234b 2018-09-10 stsp struct imsg imsg;
1857 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1858 876c234b 2018-09-10 stsp size_t datalen;
1859 876c234b 2018-09-10 stsp struct got_pack *pack;
1860 876c234b 2018-09-10 stsp
1861 876c234b 2018-09-10 stsp *packp = NULL;
1862 876c234b 2018-09-10 stsp
1863 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1864 876c234b 2018-09-10 stsp if (err)
1865 876c234b 2018-09-10 stsp return err;
1866 876c234b 2018-09-10 stsp
1867 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
1868 876c234b 2018-09-10 stsp if (pack == NULL) {
1869 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1870 876c234b 2018-09-10 stsp goto done;
1871 876c234b 2018-09-10 stsp }
1872 876c234b 2018-09-10 stsp
1873 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
1874 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1875 876c234b 2018-09-10 stsp goto done;
1876 876c234b 2018-09-10 stsp }
1877 876c234b 2018-09-10 stsp
1878 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1879 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1880 876c234b 2018-09-10 stsp goto done;
1881 876c234b 2018-09-10 stsp }
1882 876c234b 2018-09-10 stsp
1883 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1884 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
1885 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1886 876c234b 2018-09-10 stsp goto done;
1887 876c234b 2018-09-10 stsp }
1888 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
1889 876c234b 2018-09-10 stsp
1890 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
1891 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
1892 876c234b 2018-09-10 stsp if (pack->fd == -1) {
1893 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1894 876c234b 2018-09-10 stsp goto done;
1895 876c234b 2018-09-10 stsp }
1896 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1897 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1898 56bef47a 2018-09-15 stsp goto done;
1899 56bef47a 2018-09-15 stsp }
1900 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
1901 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
1902 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1903 ab2f42e7 2019-11-10 stsp goto done;
1904 ab2f42e7 2019-11-10 stsp }
1905 ab2f42e7 2019-11-10 stsp
1906 dac5c75e 2022-06-04 stsp err = got_delta_cache_alloc(&pack->delta_cache);
1907 dac5c75e 2022-06-04 stsp if (err)
1908 876c234b 2018-09-10 stsp goto done;
1909 876c234b 2018-09-10 stsp
1910 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1911 1c28a361 2022-10-25 op if (pack->filesize > 0 && pack->filesize <= SIZE_MAX) {
1912 1c28a361 2022-10-25 op pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1913 1c28a361 2022-10-25 op pack->fd, 0);
1914 1c28a361 2022-10-25 op if (pack->map == MAP_FAILED)
1915 1c28a361 2022-10-25 op pack->map = NULL; /* fall back to read(2) */
1916 1c28a361 2022-10-25 op }
1917 876c234b 2018-09-10 stsp #endif
1918 876c234b 2018-09-10 stsp done:
1919 876c234b 2018-09-10 stsp if (err) {
1920 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1921 876c234b 2018-09-10 stsp close(imsg.fd);
1922 876c234b 2018-09-10 stsp free(pack);
1923 876c234b 2018-09-10 stsp } else
1924 876c234b 2018-09-10 stsp *packp = pack;
1925 876c234b 2018-09-10 stsp imsg_free(&imsg);
1926 876c234b 2018-09-10 stsp return err;
1927 876c234b 2018-09-10 stsp }
1928 876c234b 2018-09-10 stsp
1929 876c234b 2018-09-10 stsp int
1930 876c234b 2018-09-10 stsp main(int argc, char *argv[])
1931 876c234b 2018-09-10 stsp {
1932 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1933 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
1934 876c234b 2018-09-10 stsp struct imsg imsg;
1935 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
1936 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
1937 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1938 67fd6849 2022-02-13 stsp FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1939 61af9b21 2022-06-28 stsp struct got_object_idset *keep = NULL, *drop = NULL, *skip = NULL;
1940 d294b1dc 2022-10-18 stsp struct got_parsed_tree_entry *entries = NULL;
1941 d294b1dc 2022-10-18 stsp size_t nentries = 0, nentries_alloc = 0;
1942 876c234b 2018-09-10 stsp
1943 876c234b 2018-09-10 stsp //static int attached;
1944 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1945 876c234b 2018-09-10 stsp
1946 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1947 99437157 2018-11-11 stsp
1948 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1949 876c234b 2018-09-10 stsp
1950 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1951 c59b3346 2018-09-11 stsp if (err) {
1952 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1953 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1954 c59b3346 2018-09-11 stsp return 1;
1955 c59b3346 2018-09-11 stsp }
1956 c59b3346 2018-09-11 stsp
1957 2ff12563 2018-09-15 stsp #ifndef PROFILE
1958 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1959 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1960 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1961 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1962 876c234b 2018-09-10 stsp return 1;
1963 876c234b 2018-09-10 stsp }
1964 2ff12563 2018-09-15 stsp #endif
1965 876c234b 2018-09-10 stsp
1966 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1967 876c234b 2018-09-10 stsp if (err) {
1968 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1969 876c234b 2018-09-10 stsp return 1;
1970 876c234b 2018-09-10 stsp }
1971 876c234b 2018-09-10 stsp
1972 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1973 876c234b 2018-09-10 stsp if (err) {
1974 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1975 876c234b 2018-09-10 stsp return 1;
1976 876c234b 2018-09-10 stsp }
1977 876c234b 2018-09-10 stsp
1978 656b1f76 2019-05-11 jcs for (;;) {
1979 876c234b 2018-09-10 stsp imsg.fd = -1;
1980 99437157 2018-11-11 stsp
1981 99437157 2018-11-11 stsp if (sigint_received) {
1982 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1983 99437157 2018-11-11 stsp break;
1984 99437157 2018-11-11 stsp }
1985 876c234b 2018-09-10 stsp
1986 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1987 876c234b 2018-09-10 stsp if (err) {
1988 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1989 876c234b 2018-09-10 stsp err = NULL;
1990 876c234b 2018-09-10 stsp break;
1991 876c234b 2018-09-10 stsp }
1992 876c234b 2018-09-10 stsp
1993 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1994 876c234b 2018-09-10 stsp break;
1995 876c234b 2018-09-10 stsp
1996 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1997 db696021 2022-01-04 stsp case GOT_IMSG_TMPFD:
1998 67fd6849 2022-02-13 stsp if (basefile == NULL) {
1999 67fd6849 2022-02-13 stsp err = receive_tempfile(&basefile, "w+",
2000 67fd6849 2022-02-13 stsp &imsg, &ibuf);
2001 67fd6849 2022-02-13 stsp } else if (accumfile == NULL) {
2002 67fd6849 2022-02-13 stsp err = receive_tempfile(&accumfile, "w+",
2003 67fd6849 2022-02-13 stsp &imsg, &ibuf);
2004 67fd6849 2022-02-13 stsp } else
2005 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2006 db696021 2022-01-04 stsp break;
2007 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
2008 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
2009 c59b3346 2018-09-11 stsp &objcache);
2010 876c234b 2018-09-10 stsp break;
2011 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
2012 db696021 2022-01-04 stsp if (basefile == NULL || accumfile == NULL) {
2013 db696021 2022-01-04 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2014 db696021 2022-01-04 stsp break;
2015 db696021 2022-01-04 stsp }
2016 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
2017 db696021 2022-01-04 stsp &objcache, basefile, accumfile);
2018 59d1e4a0 2021-03-10 stsp break;
2019 67fd6849 2022-02-13 stsp case GOT_IMSG_RAW_DELTA_OUTFD:
2020 67fd6849 2022-02-13 stsp if (delta_outfile != NULL) {
2021 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2022 67fd6849 2022-02-13 stsp break;
2023 67fd6849 2022-02-13 stsp }
2024 67fd6849 2022-02-13 stsp err = receive_tempfile(&delta_outfile, "w",
2025 67fd6849 2022-02-13 stsp &imsg, &ibuf);
2026 67fd6849 2022-02-13 stsp break;
2027 67fd6849 2022-02-13 stsp case GOT_IMSG_RAW_DELTA_REQUEST:
2028 67fd6849 2022-02-13 stsp if (delta_outfile == NULL) {
2029 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
2030 67fd6849 2022-02-13 stsp break;
2031 67fd6849 2022-02-13 stsp }
2032 67fd6849 2022-02-13 stsp err = raw_delta_request(&imsg, &ibuf, delta_outfile,
2033 67fd6849 2022-02-13 stsp pack, packidx);
2034 67fd6849 2022-02-13 stsp break;
2035 fae7e038 2022-05-07 stsp case GOT_IMSG_DELTA_REUSE_REQUEST:
2036 24b7de1c 2022-12-03 stsp err = delta_reuse_request(&imsg, &ibuf, pack, packidx);
2037 fae7e038 2022-05-07 stsp break;
2038 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
2039 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
2040 7762fe12 2018-11-05 stsp &objcache);
2041 7762fe12 2018-11-05 stsp break;
2042 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
2043 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
2044 d294b1dc 2022-10-18 stsp &objcache, &entries, &nentries, &nentries_alloc);
2045 876c234b 2018-09-10 stsp break;
2046 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
2047 db696021 2022-01-04 stsp if (basefile == NULL || accumfile == NULL) {
2048 db696021 2022-01-04 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2049 db696021 2022-01-04 stsp break;
2050 db696021 2022-01-04 stsp }
2051 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
2052 db696021 2022-01-04 stsp &objcache, basefile, accumfile);
2053 876c234b 2018-09-10 stsp break;
2054 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
2055 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
2056 62d463ca 2020-10-20 naddy &objcache);
2057 f4a881ce 2018-11-17 stsp break;
2058 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
2059 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
2060 ca6e02ac 2020-01-07 stsp packidx, &objcache);
2061 ca6e02ac 2020-01-07 stsp break;
2062 0ab4c957 2022-06-13 stsp case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
2063 0ab4c957 2022-06-13 stsp err = enumeration_request(&imsg, &ibuf, pack,
2064 0ab4c957 2022-06-13 stsp packidx, &objcache);
2065 0ab4c957 2022-06-13 stsp break;
2066 61af9b21 2022-06-28 stsp case GOT_IMSG_COMMIT_PAINTING_INIT:
2067 61af9b21 2022-06-28 stsp commit_painting_free(&keep, &drop, &skip);
2068 61af9b21 2022-06-28 stsp err = commit_painting_init(&ibuf, &keep, &drop, &skip);
2069 61af9b21 2022-06-28 stsp break;
2070 61af9b21 2022-06-28 stsp case GOT_IMSG_COMMIT_PAINTING_REQUEST:
2071 61af9b21 2022-06-28 stsp if (keep == NULL || drop == NULL || skip == NULL) {
2072 61af9b21 2022-06-28 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2073 61af9b21 2022-06-28 stsp break;
2074 61af9b21 2022-06-28 stsp }
2075 61af9b21 2022-06-28 stsp err = commit_painting_request(&imsg, &ibuf, pack,
2076 61af9b21 2022-06-28 stsp packidx, &objcache, keep, drop, skip);
2077 61af9b21 2022-06-28 stsp break;
2078 61af9b21 2022-06-28 stsp case GOT_IMSG_COMMIT_PAINTING_DONE:
2079 61af9b21 2022-06-28 stsp commit_painting_free(&keep, &drop, &skip);
2080 61af9b21 2022-06-28 stsp break;
2081 876c234b 2018-09-10 stsp default:
2082 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
2083 876c234b 2018-09-10 stsp break;
2084 876c234b 2018-09-10 stsp }
2085 876c234b 2018-09-10 stsp
2086 08578a35 2021-01-22 stsp if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
2087 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
2088 876c234b 2018-09-10 stsp imsg_free(&imsg);
2089 99437157 2018-11-11 stsp if (err)
2090 876c234b 2018-09-10 stsp break;
2091 876c234b 2018-09-10 stsp }
2092 876c234b 2018-09-10 stsp
2093 d294b1dc 2022-10-18 stsp free(entries);
2094 61af9b21 2022-06-28 stsp commit_painting_free(&keep, &drop, &skip);
2095 c59b3346 2018-09-11 stsp if (packidx)
2096 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
2097 c59b3346 2018-09-11 stsp if (pack)
2098 c59b3346 2018-09-11 stsp got_pack_close(pack);
2099 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
2100 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
2101 db696021 2022-01-04 stsp if (basefile && fclose(basefile) == EOF && err == NULL)
2102 db696021 2022-01-04 stsp err = got_error_from_errno("fclose");
2103 db696021 2022-01-04 stsp if (accumfile && fclose(accumfile) == EOF && err == NULL)
2104 db696021 2022-01-04 stsp err = got_error_from_errno("fclose");
2105 67fd6849 2022-02-13 stsp if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
2106 67fd6849 2022-02-13 stsp err = got_error_from_errno("fclose");
2107 99437157 2018-11-11 stsp if (err) {
2108 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
2109 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
2110 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
2111 80d5f134 2018-11-11 stsp }
2112 99437157 2018-11-11 stsp }
2113 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
2114 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
2115 876c234b 2018-09-10 stsp return err ? 1 : 0;
2116 876c234b 2018-09-10 stsp }