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