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 876c234b 2018-09-10 stsp #include <sys/types.h>
18 876c234b 2018-09-10 stsp #include <sys/uio.h>
19 876c234b 2018-09-10 stsp #include <sys/time.h>
20 876c234b 2018-09-10 stsp #include <sys/mman.h>
21 876c234b 2018-09-10 stsp
22 876c234b 2018-09-10 stsp #include <limits.h>
23 99437157 2018-11-11 stsp #include <signal.h>
24 876c234b 2018-09-10 stsp #include <stdint.h>
25 876c234b 2018-09-10 stsp #include <stdio.h>
26 876c234b 2018-09-10 stsp #include <stdlib.h>
27 876c234b 2018-09-10 stsp #include <string.h>
28 81a12da5 2020-09-09 naddy #include <unistd.h>
29 876c234b 2018-09-10 stsp #include <zlib.h>
30 dd038bc6 2021-09-21 thomas.ad
31 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
32 876c234b 2018-09-10 stsp
33 876c234b 2018-09-10 stsp #include "got_error.h"
34 876c234b 2018-09-10 stsp #include "got_object.h"
35 3022d272 2019-11-14 stsp #include "got_path.h"
36 876c234b 2018-09-10 stsp
37 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
38 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
39 876c234b 2018-09-10 stsp #include "got_lib_object.h"
40 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
41 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
42 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
43 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
44 876c234b 2018-09-10 stsp
45 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
46 99437157 2018-11-11 stsp
47 99437157 2018-11-11 stsp static void
48 99437157 2018-11-11 stsp catch_sigint(int signo)
49 99437157 2018-11-11 stsp {
50 99437157 2018-11-11 stsp sigint_received = 1;
51 99437157 2018-11-11 stsp }
52 99437157 2018-11-11 stsp
53 876c234b 2018-09-10 stsp static const struct got_error *
54 704b89c4 2019-05-23 stsp open_object(struct got_object **obj, struct got_pack *pack,
55 704b89c4 2019-05-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id,
56 704b89c4 2019-05-23 stsp struct got_object_cache *objcache)
57 704b89c4 2019-05-23 stsp {
58 704b89c4 2019-05-23 stsp const struct got_error *err;
59 704b89c4 2019-05-23 stsp
60 704b89c4 2019-05-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
61 704b89c4 2019-05-23 stsp if (err)
62 704b89c4 2019-05-23 stsp return err;
63 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
64 704b89c4 2019-05-23 stsp
65 704b89c4 2019-05-23 stsp err = got_object_cache_add(objcache, id, *obj);
66 79c99a64 2019-05-23 stsp if (err) {
67 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
68 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
69 79c99a64 2019-05-23 stsp err = NULL;
70 704b89c4 2019-05-23 stsp return err;
71 79c99a64 2019-05-23 stsp }
72 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
73 704b89c4 2019-05-23 stsp return NULL;
74 704b89c4 2019-05-23 stsp }
75 704b89c4 2019-05-23 stsp
76 704b89c4 2019-05-23 stsp static const struct got_error *
77 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
78 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
79 876c234b 2018-09-10 stsp {
80 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
81 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
82 876c234b 2018-09-10 stsp struct got_object *obj;
83 106807b4 2018-09-15 stsp struct got_object_id id;
84 876c234b 2018-09-10 stsp size_t datalen;
85 876c234b 2018-09-10 stsp
86 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
87 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
88 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
89 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
90 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
91 876c234b 2018-09-10 stsp
92 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
93 704b89c4 2019-05-23 stsp if (obj) {
94 704b89c4 2019-05-23 stsp obj->refcnt++;
95 704b89c4 2019-05-23 stsp } else {
96 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
97 704b89c4 2019-05-23 stsp objcache);
98 704b89c4 2019-05-23 stsp if (err)
99 704b89c4 2019-05-23 stsp goto done;
100 704b89c4 2019-05-23 stsp }
101 876c234b 2018-09-10 stsp
102 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
103 c59b3346 2018-09-11 stsp done:
104 876c234b 2018-09-10 stsp got_object_close(obj);
105 876c234b 2018-09-10 stsp return err;
106 876c234b 2018-09-10 stsp }
107 876c234b 2018-09-10 stsp
108 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
109 ca6e02ac 2020-01-07 stsp open_commit(struct got_commit_object **commit, struct got_pack *pack,
110 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
111 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
112 876c234b 2018-09-10 stsp {
113 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
114 cb5e38fd 2019-05-23 stsp struct got_object *obj = NULL;
115 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
116 cfd633c2 2018-09-10 stsp size_t len;
117 cfd633c2 2018-09-10 stsp
118 ca6e02ac 2020-01-07 stsp *commit = NULL;
119 1785f84a 2018-12-23 stsp
120 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
121 704b89c4 2019-05-23 stsp if (obj) {
122 704b89c4 2019-05-23 stsp obj->refcnt++;
123 704b89c4 2019-05-23 stsp } else {
124 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
125 704b89c4 2019-05-23 stsp objcache);
126 704b89c4 2019-05-23 stsp if (err)
127 704b89c4 2019-05-23 stsp return err;
128 704b89c4 2019-05-23 stsp }
129 cfd633c2 2018-09-10 stsp
130 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
131 cfd633c2 2018-09-10 stsp if (err)
132 cb5e38fd 2019-05-23 stsp goto done;
133 cfd633c2 2018-09-10 stsp
134 cfd633c2 2018-09-10 stsp obj->size = len;
135 ca6e02ac 2020-01-07 stsp
136 ca6e02ac 2020-01-07 stsp err = got_object_parse_commit(commit, buf, len);
137 ca6e02ac 2020-01-07 stsp done:
138 ca6e02ac 2020-01-07 stsp got_object_close(obj);
139 ca6e02ac 2020-01-07 stsp free(buf);
140 ca6e02ac 2020-01-07 stsp return err;
141 ca6e02ac 2020-01-07 stsp }
142 ca6e02ac 2020-01-07 stsp
143 ca6e02ac 2020-01-07 stsp static const struct got_error *
144 ca6e02ac 2020-01-07 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
145 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
146 ca6e02ac 2020-01-07 stsp {
147 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
148 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
149 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL;
150 ca6e02ac 2020-01-07 stsp struct got_object_id id;
151 ca6e02ac 2020-01-07 stsp size_t datalen;
152 ca6e02ac 2020-01-07 stsp
153 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
154 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(iobj))
155 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
156 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
157 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
158 ca6e02ac 2020-01-07 stsp
159 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
160 cb5e38fd 2019-05-23 stsp if (err)
161 cb5e38fd 2019-05-23 stsp goto done;
162 cfd633c2 2018-09-10 stsp
163 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
164 cb5e38fd 2019-05-23 stsp done:
165 cb5e38fd 2019-05-23 stsp if (commit)
166 cb5e38fd 2019-05-23 stsp got_object_commit_close(commit);
167 7762fe12 2018-11-05 stsp if (err) {
168 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
169 7762fe12 2018-11-05 stsp err = NULL;
170 7762fe12 2018-11-05 stsp else
171 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
172 7762fe12 2018-11-05 stsp }
173 7762fe12 2018-11-05 stsp
174 7762fe12 2018-11-05 stsp return err;
175 7762fe12 2018-11-05 stsp }
176 7762fe12 2018-11-05 stsp
177 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
178 ca6e02ac 2020-01-07 stsp open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
179 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
180 ca6e02ac 2020-01-07 stsp struct got_object_id *id, struct got_object_cache *objcache)
181 ca6e02ac 2020-01-07 stsp {
182 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
183 ca6e02ac 2020-01-07 stsp struct got_object *obj = NULL;
184 ca6e02ac 2020-01-07 stsp size_t len;
185 ca6e02ac 2020-01-07 stsp
186 ca6e02ac 2020-01-07 stsp *buf = NULL;
187 ca6e02ac 2020-01-07 stsp *nentries = 0;
188 ca6e02ac 2020-01-07 stsp
189 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
190 ca6e02ac 2020-01-07 stsp if (obj) {
191 ca6e02ac 2020-01-07 stsp obj->refcnt++;
192 ca6e02ac 2020-01-07 stsp } else {
193 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
194 ca6e02ac 2020-01-07 stsp objcache);
195 ca6e02ac 2020-01-07 stsp if (err)
196 ca6e02ac 2020-01-07 stsp return err;
197 ca6e02ac 2020-01-07 stsp }
198 ca6e02ac 2020-01-07 stsp
199 ca6e02ac 2020-01-07 stsp err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
200 ca6e02ac 2020-01-07 stsp if (err)
201 ca6e02ac 2020-01-07 stsp goto done;
202 ca6e02ac 2020-01-07 stsp
203 ca6e02ac 2020-01-07 stsp obj->size = len;
204 ca6e02ac 2020-01-07 stsp
205 ca6e02ac 2020-01-07 stsp err = got_object_parse_tree(entries, nentries, *buf, len);
206 ca6e02ac 2020-01-07 stsp done:
207 ca6e02ac 2020-01-07 stsp got_object_close(obj);
208 ca6e02ac 2020-01-07 stsp if (err) {
209 ca6e02ac 2020-01-07 stsp free(*buf);
210 ca6e02ac 2020-01-07 stsp *buf = NULL;
211 ca6e02ac 2020-01-07 stsp }
212 ca6e02ac 2020-01-07 stsp return err;
213 ca6e02ac 2020-01-07 stsp }
214 ca6e02ac 2020-01-07 stsp
215 7762fe12 2018-11-05 stsp static const struct got_error *
216 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
217 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
218 876c234b 2018-09-10 stsp {
219 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
220 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
221 3022d272 2019-11-14 stsp struct got_pathlist_head entries;
222 3022d272 2019-11-14 stsp int nentries = 0;
223 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
224 13c729f7 2018-12-24 stsp struct got_object_id id;
225 13c729f7 2018-12-24 stsp size_t datalen;
226 e7885405 2018-09-10 stsp
227 3022d272 2019-11-14 stsp TAILQ_INIT(&entries);
228 3022d272 2019-11-14 stsp
229 13c729f7 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
230 13c729f7 2018-12-24 stsp if (datalen != sizeof(iobj))
231 13c729f7 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
232 13c729f7 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
233 13c729f7 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
234 13c729f7 2018-12-24 stsp
235 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
236 62d463ca 2020-10-20 naddy &id, objcache);
237 e7885405 2018-09-10 stsp if (err)
238 ca6e02ac 2020-01-07 stsp return err;
239 e7885405 2018-09-10 stsp
240 3022d272 2019-11-14 stsp err = got_privsep_send_tree(ibuf, &entries, nentries);
241 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_free(&entries);
242 cb5e38fd 2019-05-23 stsp free(buf);
243 e7885405 2018-09-10 stsp if (err) {
244 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
245 e7885405 2018-09-10 stsp err = NULL;
246 e7885405 2018-09-10 stsp else
247 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
248 e7885405 2018-09-10 stsp }
249 e7885405 2018-09-10 stsp
250 e7885405 2018-09-10 stsp return err;
251 876c234b 2018-09-10 stsp }
252 876c234b 2018-09-10 stsp
253 876c234b 2018-09-10 stsp static const struct got_error *
254 01bb5a15 2021-09-25 thomas.ad receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
255 876c234b 2018-09-10 stsp {
256 3840f4c9 2018-09-12 stsp const struct got_error *err;
257 3840f4c9 2018-09-12 stsp struct imsg imsg;
258 55da3778 2018-09-10 stsp size_t datalen;
259 55da3778 2018-09-10 stsp
260 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
261 55da3778 2018-09-10 stsp if (err)
262 55da3778 2018-09-10 stsp return err;
263 55da3778 2018-09-10 stsp
264 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
265 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
266 55da3778 2018-09-10 stsp goto done;
267 55da3778 2018-09-10 stsp }
268 55da3778 2018-09-10 stsp
269 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
270 55da3778 2018-09-10 stsp if (datalen != 0) {
271 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
272 55da3778 2018-09-10 stsp goto done;
273 55da3778 2018-09-10 stsp }
274 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
275 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
276 55da3778 2018-09-10 stsp goto done;
277 55da3778 2018-09-10 stsp }
278 55da3778 2018-09-10 stsp
279 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
280 3840f4c9 2018-09-12 stsp if (*f == NULL) {
281 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
282 3a6ce05a 2019-02-11 stsp close(imsg.fd);
283 55da3778 2018-09-10 stsp goto done;
284 55da3778 2018-09-10 stsp }
285 3840f4c9 2018-09-12 stsp done:
286 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
287 3840f4c9 2018-09-12 stsp return err;
288 bc1f382f 2022-01-05 thomas }
289 bc1f382f 2022-01-05 thomas
290 bc1f382f 2022-01-05 thomas static const struct got_error *
291 bc1f382f 2022-01-05 thomas receive_tempfile(FILE **basefile, FILE **accumfile, struct imsg *imsg,
292 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf)
293 bc1f382f 2022-01-05 thomas {
294 bc1f382f 2022-01-05 thomas size_t datalen;
295 bc1f382f 2022-01-05 thomas FILE **f;
296 bc1f382f 2022-01-05 thomas
297 bc1f382f 2022-01-05 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
298 bc1f382f 2022-01-05 thomas if (datalen != 0)
299 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
300 bc1f382f 2022-01-05 thomas
301 bc1f382f 2022-01-05 thomas if (imsg->fd == -1)
302 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
303 bc1f382f 2022-01-05 thomas
304 bc1f382f 2022-01-05 thomas if (*basefile == NULL)
305 bc1f382f 2022-01-05 thomas f = basefile;
306 bc1f382f 2022-01-05 thomas else if (*accumfile == NULL)
307 bc1f382f 2022-01-05 thomas f = accumfile;
308 bc1f382f 2022-01-05 thomas else
309 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_MSG);
310 bc1f382f 2022-01-05 thomas
311 bc1f382f 2022-01-05 thomas *f = fdopen(imsg->fd, "w+");
312 bc1f382f 2022-01-05 thomas if (*f == NULL)
313 bc1f382f 2022-01-05 thomas return got_error_from_errno("fdopen");
314 bc1f382f 2022-01-05 thomas imsg->fd = -1;
315 bc1f382f 2022-01-05 thomas
316 bc1f382f 2022-01-05 thomas return NULL;
317 3840f4c9 2018-09-12 stsp }
318 55da3778 2018-09-10 stsp
319 3840f4c9 2018-09-12 stsp static const struct got_error *
320 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
321 bc1f382f 2022-01-05 thomas struct got_packidx *packidx, struct got_object_cache *objcache,
322 bc1f382f 2022-01-05 thomas FILE *basefile, FILE *accumfile)
323 3840f4c9 2018-09-12 stsp {
324 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
325 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
326 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
327 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
328 ebc55e2d 2018-12-24 stsp struct got_object_id id;
329 ebc55e2d 2018-12-24 stsp size_t datalen;
330 ac544f8c 2019-01-13 stsp uint64_t blob_size;
331 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
332 3840f4c9 2018-09-12 stsp
333 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
334 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
335 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
336 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
337 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
338 ebc55e2d 2018-12-24 stsp
339 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
340 704b89c4 2019-05-23 stsp if (obj) {
341 704b89c4 2019-05-23 stsp obj->refcnt++;
342 704b89c4 2019-05-23 stsp } else {
343 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
344 704b89c4 2019-05-23 stsp objcache);
345 704b89c4 2019-05-23 stsp if (err)
346 704b89c4 2019-05-23 stsp return err;
347 704b89c4 2019-05-23 stsp }
348 3840f4c9 2018-09-12 stsp
349 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
350 3840f4c9 2018-09-12 stsp if (err)
351 ac544f8c 2019-01-13 stsp goto done;
352 3840f4c9 2018-09-12 stsp
353 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
354 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
355 ac544f8c 2019-01-13 stsp if (err)
356 ac544f8c 2019-01-13 stsp goto done;
357 ac544f8c 2019-01-13 stsp } else
358 ac544f8c 2019-01-13 stsp blob_size = obj->size;
359 ac544f8c 2019-01-13 stsp
360 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
361 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
362 ac544f8c 2019-01-13 stsp obj, pack);
363 ac544f8c 2019-01-13 stsp else
364 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
365 ac544f8c 2019-01-13 stsp accumfile);
366 3840f4c9 2018-09-12 stsp if (err)
367 55da3778 2018-09-10 stsp goto done;
368 55da3778 2018-09-10 stsp
369 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
370 55da3778 2018-09-10 stsp done:
371 ac544f8c 2019-01-13 stsp free(buf);
372 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
373 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
374 cb5e38fd 2019-05-23 stsp got_object_close(obj);
375 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
376 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
377 55da3778 2018-09-10 stsp
378 55da3778 2018-09-10 stsp return err;
379 876c234b 2018-09-10 stsp }
380 876c234b 2018-09-10 stsp
381 876c234b 2018-09-10 stsp static const struct got_error *
382 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
383 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
384 f4a881ce 2018-11-17 stsp {
385 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
386 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
387 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
388 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
389 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
390 f4a881ce 2018-11-17 stsp size_t len;
391 268f7291 2018-12-24 stsp struct got_object_id id;
392 268f7291 2018-12-24 stsp size_t datalen;
393 f4a881ce 2018-11-17 stsp
394 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
395 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
396 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
397 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
398 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
399 268f7291 2018-12-24 stsp
400 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
401 704b89c4 2019-05-23 stsp if (obj) {
402 704b89c4 2019-05-23 stsp obj->refcnt++;
403 704b89c4 2019-05-23 stsp } else {
404 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
405 704b89c4 2019-05-23 stsp objcache);
406 704b89c4 2019-05-23 stsp if (err)
407 704b89c4 2019-05-23 stsp return err;
408 704b89c4 2019-05-23 stsp }
409 f4a881ce 2018-11-17 stsp
410 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
411 f4a881ce 2018-11-17 stsp if (err)
412 cb5e38fd 2019-05-23 stsp goto done;
413 f4a881ce 2018-11-17 stsp
414 f4a881ce 2018-11-17 stsp obj->size = len;
415 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
416 0ae4af15 2019-02-01 stsp if (err)
417 cb5e38fd 2019-05-23 stsp goto done;
418 f4a881ce 2018-11-17 stsp
419 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
420 cb5e38fd 2019-05-23 stsp done:
421 cb5e38fd 2019-05-23 stsp free(buf);
422 cb5e38fd 2019-05-23 stsp got_object_close(obj);
423 cb5e38fd 2019-05-23 stsp if (tag)
424 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
425 ca6e02ac 2020-01-07 stsp if (err) {
426 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
427 ca6e02ac 2020-01-07 stsp err = NULL;
428 ca6e02ac 2020-01-07 stsp else
429 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
430 ca6e02ac 2020-01-07 stsp }
431 ca6e02ac 2020-01-07 stsp
432 ca6e02ac 2020-01-07 stsp return err;
433 ca6e02ac 2020-01-07 stsp }
434 ca6e02ac 2020-01-07 stsp
435 ca6e02ac 2020-01-07 stsp static struct got_parsed_tree_entry *
436 ca6e02ac 2020-01-07 stsp find_entry_by_name(struct got_pathlist_head *entries, int nentries,
437 ca6e02ac 2020-01-07 stsp const char *name, size_t len)
438 ca6e02ac 2020-01-07 stsp {
439 ca6e02ac 2020-01-07 stsp struct got_pathlist_entry *pe;
440 ca6e02ac 2020-01-07 stsp
441 ca6e02ac 2020-01-07 stsp /* Note that tree entries are sorted in strncmp() order. */
442 ca6e02ac 2020-01-07 stsp TAILQ_FOREACH(pe, entries, entry) {
443 ca6e02ac 2020-01-07 stsp int cmp = strncmp(pe->path, name, len);
444 ca6e02ac 2020-01-07 stsp if (cmp < 0)
445 ca6e02ac 2020-01-07 stsp continue;
446 ca6e02ac 2020-01-07 stsp if (cmp > 0)
447 ca6e02ac 2020-01-07 stsp break;
448 ca6e02ac 2020-01-07 stsp if (pe->path[len] == '\0')
449 ca6e02ac 2020-01-07 stsp return (struct got_parsed_tree_entry *)pe->data;
450 ca6e02ac 2020-01-07 stsp }
451 ca6e02ac 2020-01-07 stsp return NULL;
452 ca6e02ac 2020-01-07 stsp }
453 ca6e02ac 2020-01-07 stsp
454 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
455 ca6e02ac 2020-01-07 stsp tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
456 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries1, int *nentries1,
457 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries2, int *nentries2,
458 ca6e02ac 2020-01-07 stsp const char *path, struct got_pack *pack, struct got_packidx *packidx,
459 ca6e02ac 2020-01-07 stsp struct imsgbuf *ibuf, struct got_object_cache *objcache)
460 ca6e02ac 2020-01-07 stsp {
461 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
462 ca6e02ac 2020-01-07 stsp struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
463 ca6e02ac 2020-01-07 stsp const char *seg, *s;
464 ca6e02ac 2020-01-07 stsp size_t seglen;
465 ca6e02ac 2020-01-07 stsp
466 ca6e02ac 2020-01-07 stsp *changed = 0;
467 ca6e02ac 2020-01-07 stsp
468 ca6e02ac 2020-01-07 stsp /* We not do support comparing the root path. */
469 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
470 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
471 ca6e02ac 2020-01-07 stsp
472 ca6e02ac 2020-01-07 stsp s = path;
473 61a7d79f 2020-02-29 stsp while (*s == '/')
474 61a7d79f 2020-02-29 stsp s++;
475 ca6e02ac 2020-01-07 stsp seg = s;
476 ca6e02ac 2020-01-07 stsp seglen = 0;
477 ca6e02ac 2020-01-07 stsp while (*s) {
478 ca6e02ac 2020-01-07 stsp if (*s != '/') {
479 ca6e02ac 2020-01-07 stsp s++;
480 ca6e02ac 2020-01-07 stsp seglen++;
481 ca6e02ac 2020-01-07 stsp if (*s)
482 ca6e02ac 2020-01-07 stsp continue;
483 ca6e02ac 2020-01-07 stsp }
484 ca6e02ac 2020-01-07 stsp
485 ca6e02ac 2020-01-07 stsp pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
486 ca6e02ac 2020-01-07 stsp if (pte1 == NULL) {
487 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_NO_OBJ);
488 ca6e02ac 2020-01-07 stsp break;
489 ca6e02ac 2020-01-07 stsp }
490 ca6e02ac 2020-01-07 stsp
491 ca6e02ac 2020-01-07 stsp pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
492 ca6e02ac 2020-01-07 stsp if (pte2 == NULL) {
493 ca6e02ac 2020-01-07 stsp *changed = 1;
494 ca6e02ac 2020-01-07 stsp break;
495 ca6e02ac 2020-01-07 stsp }
496 ca6e02ac 2020-01-07 stsp
497 ca6e02ac 2020-01-07 stsp if (pte1->mode != pte2->mode) {
498 ca6e02ac 2020-01-07 stsp *changed = 1;
499 ca6e02ac 2020-01-07 stsp break;
500 ca6e02ac 2020-01-07 stsp }
501 ca6e02ac 2020-01-07 stsp
502 ca6e02ac 2020-01-07 stsp if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
503 ca6e02ac 2020-01-07 stsp *changed = 0;
504 ca6e02ac 2020-01-07 stsp break;
505 ca6e02ac 2020-01-07 stsp }
506 ca6e02ac 2020-01-07 stsp
507 ca6e02ac 2020-01-07 stsp if (*s == '\0') { /* final path element */
508 ca6e02ac 2020-01-07 stsp *changed = 1;
509 ca6e02ac 2020-01-07 stsp break;
510 ca6e02ac 2020-01-07 stsp }
511 ca6e02ac 2020-01-07 stsp
512 ca6e02ac 2020-01-07 stsp seg = s + 1;
513 ca6e02ac 2020-01-07 stsp s++;
514 ca6e02ac 2020-01-07 stsp seglen = 0;
515 ca6e02ac 2020-01-07 stsp if (*s) {
516 ca6e02ac 2020-01-07 stsp struct got_object_id id1, id2;
517 ca6e02ac 2020-01-07 stsp int idx;
518 ca6e02ac 2020-01-07 stsp
519 ded8fbb8 2020-04-19 stsp memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
520 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id1);
521 ca6e02ac 2020-01-07 stsp if (idx == -1) {
522 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id1);
523 ca6e02ac 2020-01-07 stsp break;
524 ca6e02ac 2020-01-07 stsp }
525 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries1);
526 ca6e02ac 2020-01-07 stsp *nentries1 = 0;
527 ca6e02ac 2020-01-07 stsp free(*buf1);
528 ca6e02ac 2020-01-07 stsp *buf1 = NULL;
529 ca6e02ac 2020-01-07 stsp err = open_tree(buf1, entries1, nentries1, pack,
530 ca6e02ac 2020-01-07 stsp packidx, idx, &id1, objcache);
531 ca6e02ac 2020-01-07 stsp pte1 = NULL;
532 ca6e02ac 2020-01-07 stsp if (err)
533 ca6e02ac 2020-01-07 stsp break;
534 ca6e02ac 2020-01-07 stsp
535 ded8fbb8 2020-04-19 stsp memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
536 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id2);
537 ca6e02ac 2020-01-07 stsp if (idx == -1) {
538 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id2);
539 ca6e02ac 2020-01-07 stsp break;
540 ca6e02ac 2020-01-07 stsp }
541 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries2);
542 ca6e02ac 2020-01-07 stsp *nentries2 = 0;
543 ca6e02ac 2020-01-07 stsp free(*buf2);
544 ca6e02ac 2020-01-07 stsp *buf2 = NULL;
545 ca6e02ac 2020-01-07 stsp err = open_tree(buf2, entries2, nentries2, pack,
546 ca6e02ac 2020-01-07 stsp packidx, idx, &id2, objcache);
547 ca6e02ac 2020-01-07 stsp pte2 = NULL;
548 ca6e02ac 2020-01-07 stsp if (err)
549 ca6e02ac 2020-01-07 stsp break;
550 ca6e02ac 2020-01-07 stsp }
551 ca6e02ac 2020-01-07 stsp }
552 ca6e02ac 2020-01-07 stsp
553 ca6e02ac 2020-01-07 stsp return err;
554 ca6e02ac 2020-01-07 stsp }
555 ca6e02ac 2020-01-07 stsp
556 ca6e02ac 2020-01-07 stsp static const struct got_error *
557 e70bf110 2020-03-22 stsp send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
558 e70bf110 2020-03-22 stsp struct imsgbuf *ibuf)
559 e70bf110 2020-03-22 stsp {
560 e70bf110 2020-03-22 stsp const struct got_error *err;
561 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
562 01bb5a15 2021-09-25 thomas.ad size_t i;
563 e70bf110 2020-03-22 stsp
564 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
565 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
566 e70bf110 2020-03-22 stsp ncommits * SHA1_DIGEST_LENGTH);
567 e70bf110 2020-03-22 stsp if (wbuf == NULL)
568 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
569 e70bf110 2020-03-22 stsp
570 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
571 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
572 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
573 e70bf110 2020-03-22 stsp return err;
574 e70bf110 2020-03-22 stsp }
575 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
576 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
577 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
578 e70bf110 2020-03-22 stsp err = got_error_from_errno(
579 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
580 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
581 e70bf110 2020-03-22 stsp return err;
582 e70bf110 2020-03-22 stsp }
583 e70bf110 2020-03-22 stsp }
584 e70bf110 2020-03-22 stsp
585 e70bf110 2020-03-22 stsp wbuf->fd = -1;
586 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
587 e70bf110 2020-03-22 stsp
588 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
589 e70bf110 2020-03-22 stsp }
590 e70bf110 2020-03-22 stsp
591 e70bf110 2020-03-22 stsp static const struct got_error *
592 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
593 e70bf110 2020-03-22 stsp {
594 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
595 e70bf110 2020-03-22 stsp NULL, 0) == -1)
596 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
597 e70bf110 2020-03-22 stsp
598 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
599 e70bf110 2020-03-22 stsp }
600 e70bf110 2020-03-22 stsp
601 e70bf110 2020-03-22 stsp
602 e70bf110 2020-03-22 stsp static const struct got_error *
603 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
604 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
605 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
606 ca6e02ac 2020-01-07 stsp {
607 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
608 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
609 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
610 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
611 ca6e02ac 2020-01-07 stsp struct got_pathlist_head entries, pentries;
612 ca6e02ac 2020-01-07 stsp int nentries = 0, pnentries = 0;
613 ca6e02ac 2020-01-07 stsp struct got_object_id id;
614 ca6e02ac 2020-01-07 stsp size_t datalen, path_len;
615 ca6e02ac 2020-01-07 stsp char *path = NULL;
616 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
617 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
618 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
619 ca6e02ac 2020-01-07 stsp
620 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&entries);
621 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&pentries);
622 ca6e02ac 2020-01-07 stsp
623 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
624 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(iobj))
625 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
626 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
627 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
628 ca6e02ac 2020-01-07 stsp
629 ca6e02ac 2020-01-07 stsp path_len = datalen - sizeof(iobj) - 1;
630 ca6e02ac 2020-01-07 stsp if (path_len < 0)
631 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
632 ca6e02ac 2020-01-07 stsp if (path_len > 0) {
633 ca6e02ac 2020-01-07 stsp path = imsg->data + sizeof(iobj);
634 ca6e02ac 2020-01-07 stsp if (path[path_len] != '\0')
635 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
636 ca6e02ac 2020-01-07 stsp }
637 ca6e02ac 2020-01-07 stsp
638 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
639 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
640 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
641 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
642 ca6e02ac 2020-01-07 stsp
643 ca6e02ac 2020-01-07 stsp do {
644 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
645 ca6e02ac 2020-01-07 stsp int idx;
646 ca6e02ac 2020-01-07 stsp
647 ca6e02ac 2020-01-07 stsp if (sigint_received) {
648 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
649 ca6e02ac 2020-01-07 stsp goto done;
650 ca6e02ac 2020-01-07 stsp }
651 ca6e02ac 2020-01-07 stsp
652 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
653 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
654 ca6e02ac 2020-01-07 stsp if (idx == -1)
655 ca6e02ac 2020-01-07 stsp break;
656 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
657 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
658 ca6e02ac 2020-01-07 stsp if (err) {
659 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
660 ca6e02ac 2020-01-07 stsp goto done;
661 ca6e02ac 2020-01-07 stsp err = NULL;
662 ca6e02ac 2020-01-07 stsp break;
663 ca6e02ac 2020-01-07 stsp }
664 ca6e02ac 2020-01-07 stsp }
665 ca6e02ac 2020-01-07 stsp
666 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
667 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
668 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
669 e70bf110 2020-03-22 stsp ibuf);
670 ca6e02ac 2020-01-07 stsp if (err)
671 ca6e02ac 2020-01-07 stsp goto done;
672 ca6e02ac 2020-01-07 stsp ncommits = 0;
673 ca6e02ac 2020-01-07 stsp }
674 ca6e02ac 2020-01-07 stsp ncommits++;
675 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
676 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
677 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
678 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
679 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
680 ca6e02ac 2020-01-07 stsp if (new == NULL) {
681 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
682 ca6e02ac 2020-01-07 stsp goto done;
683 ca6e02ac 2020-01-07 stsp }
684 ca6e02ac 2020-01-07 stsp commit_ids = new;
685 ca6e02ac 2020-01-07 stsp }
686 ca6e02ac 2020-01-07 stsp memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
687 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
688 ca6e02ac 2020-01-07 stsp
689 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
690 ca6e02ac 2020-01-07 stsp if (pid == NULL)
691 ca6e02ac 2020-01-07 stsp break;
692 ca6e02ac 2020-01-07 stsp
693 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, pid->id);
694 ca6e02ac 2020-01-07 stsp if (idx == -1)
695 ca6e02ac 2020-01-07 stsp break;
696 ca6e02ac 2020-01-07 stsp
697 ca6e02ac 2020-01-07 stsp err = open_commit(&pcommit, pack, packidx, idx, pid->id,
698 ca6e02ac 2020-01-07 stsp objcache);
699 ca6e02ac 2020-01-07 stsp if (err) {
700 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
701 ca6e02ac 2020-01-07 stsp goto done;
702 ca6e02ac 2020-01-07 stsp err = NULL;
703 ca6e02ac 2020-01-07 stsp break;
704 ca6e02ac 2020-01-07 stsp }
705 ca6e02ac 2020-01-07 stsp
706 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
707 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
708 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
709 ca6e02ac 2020-01-07 stsp changed = 1;
710 ca6e02ac 2020-01-07 stsp break;
711 ca6e02ac 2020-01-07 stsp }
712 ca6e02ac 2020-01-07 stsp } else {
713 ca6e02ac 2020-01-07 stsp int pidx;
714 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
715 ca6e02ac 2020-01-07 stsp
716 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
717 ca6e02ac 2020-01-07 stsp commit->tree_id);
718 ca6e02ac 2020-01-07 stsp if (idx == -1)
719 ca6e02ac 2020-01-07 stsp break;
720 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
721 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
722 ca6e02ac 2020-01-07 stsp if (pidx == -1)
723 ca6e02ac 2020-01-07 stsp break;
724 ca6e02ac 2020-01-07 stsp
725 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack,
726 ca6e02ac 2020-01-07 stsp packidx, idx, commit->tree_id, objcache);
727 ca6e02ac 2020-01-07 stsp if (err)
728 ca6e02ac 2020-01-07 stsp goto done;
729 ca6e02ac 2020-01-07 stsp err = open_tree(&pbuf, &pentries, &pnentries, pack,
730 ca6e02ac 2020-01-07 stsp packidx, pidx, pcommit->tree_id, objcache);
731 ca6e02ac 2020-01-07 stsp if (err) {
732 ca6e02ac 2020-01-07 stsp free(buf);
733 ca6e02ac 2020-01-07 stsp goto done;
734 ca6e02ac 2020-01-07 stsp }
735 ca6e02ac 2020-01-07 stsp
736 ca6e02ac 2020-01-07 stsp err = tree_path_changed(&changed, &buf, &pbuf,
737 ca6e02ac 2020-01-07 stsp &entries, &nentries, &pentries, &pnentries, path,
738 ca6e02ac 2020-01-07 stsp pack, packidx, ibuf, objcache);
739 ca6e02ac 2020-01-07 stsp
740 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
741 ca6e02ac 2020-01-07 stsp nentries = 0;
742 ca6e02ac 2020-01-07 stsp free(buf);
743 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
744 ca6e02ac 2020-01-07 stsp pnentries = 0;
745 ca6e02ac 2020-01-07 stsp free(pbuf);
746 ca6e02ac 2020-01-07 stsp if (err) {
747 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
748 ca6e02ac 2020-01-07 stsp goto done;
749 ca6e02ac 2020-01-07 stsp err = NULL;
750 ca6e02ac 2020-01-07 stsp break;
751 ca6e02ac 2020-01-07 stsp }
752 ca6e02ac 2020-01-07 stsp }
753 ca6e02ac 2020-01-07 stsp
754 ca6e02ac 2020-01-07 stsp if (!changed) {
755 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
756 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
757 ca6e02ac 2020-01-07 stsp commit = pcommit;
758 ca6e02ac 2020-01-07 stsp pcommit = NULL;
759 ca6e02ac 2020-01-07 stsp }
760 ca6e02ac 2020-01-07 stsp } while (!changed);
761 ca6e02ac 2020-01-07 stsp
762 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
763 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
764 ca6e02ac 2020-01-07 stsp if (err)
765 ca6e02ac 2020-01-07 stsp goto done;
766 ca6e02ac 2020-01-07 stsp
767 ca6e02ac 2020-01-07 stsp if (changed) {
768 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
769 ca6e02ac 2020-01-07 stsp if (err)
770 ca6e02ac 2020-01-07 stsp goto done;
771 ca6e02ac 2020-01-07 stsp }
772 ca6e02ac 2020-01-07 stsp }
773 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
774 ca6e02ac 2020-01-07 stsp done:
775 ca6e02ac 2020-01-07 stsp free(commit_ids);
776 ca6e02ac 2020-01-07 stsp if (commit)
777 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
778 ca6e02ac 2020-01-07 stsp if (pcommit)
779 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
780 ca6e02ac 2020-01-07 stsp if (nentries != 0)
781 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
782 ca6e02ac 2020-01-07 stsp if (pnentries != 0)
783 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
784 f4a881ce 2018-11-17 stsp if (err) {
785 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
786 f4a881ce 2018-11-17 stsp err = NULL;
787 f4a881ce 2018-11-17 stsp else
788 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
789 f4a881ce 2018-11-17 stsp }
790 f4a881ce 2018-11-17 stsp
791 f4a881ce 2018-11-17 stsp return err;
792 f4a881ce 2018-11-17 stsp }
793 f4a881ce 2018-11-17 stsp
794 f4a881ce 2018-11-17 stsp static const struct got_error *
795 48b4f239 2021-12-31 thomas raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
796 48b4f239 2021-12-31 thomas struct got_pack *pack, struct got_packidx *packidx,
797 bc1f382f 2022-01-05 thomas struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
798 59d1e4a0 2021-03-10 stsp {
799 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
800 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
801 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
802 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
803 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
804 59d1e4a0 2021-03-10 stsp struct got_object *obj;
805 59d1e4a0 2021-03-10 stsp struct got_object_id id;
806 59d1e4a0 2021-03-10 stsp size_t datalen;
807 59d1e4a0 2021-03-10 stsp
808 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
809 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
810 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
811 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
812 59d1e4a0 2021-03-10 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
813 59d1e4a0 2021-03-10 stsp
814 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
815 59d1e4a0 2021-03-10 stsp if (obj) {
816 59d1e4a0 2021-03-10 stsp obj->refcnt++;
817 59d1e4a0 2021-03-10 stsp } else {
818 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
819 59d1e4a0 2021-03-10 stsp objcache);
820 59d1e4a0 2021-03-10 stsp if (err)
821 59d1e4a0 2021-03-10 stsp return err;
822 59d1e4a0 2021-03-10 stsp }
823 59d1e4a0 2021-03-10 stsp
824 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
825 59d1e4a0 2021-03-10 stsp if (err)
826 59d1e4a0 2021-03-10 stsp return err;
827 59d1e4a0 2021-03-10 stsp
828 59d1e4a0 2021-03-10 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
829 59d1e4a0 2021-03-10 stsp err = got_pack_get_max_delta_object_size(&size, obj, pack);
830 59d1e4a0 2021-03-10 stsp if (err)
831 59d1e4a0 2021-03-10 stsp goto done;
832 59d1e4a0 2021-03-10 stsp } else
833 59d1e4a0 2021-03-10 stsp size = obj->size;
834 59d1e4a0 2021-03-10 stsp
835 59d1e4a0 2021-03-10 stsp if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
836 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
837 59d1e4a0 2021-03-10 stsp obj, pack);
838 59d1e4a0 2021-03-10 stsp else
839 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
840 59d1e4a0 2021-03-10 stsp accumfile);
841 59d1e4a0 2021-03-10 stsp if (err)
842 59d1e4a0 2021-03-10 stsp goto done;
843 59d1e4a0 2021-03-10 stsp
844 40e3cb72 2021-06-22 stsp err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
845 59d1e4a0 2021-03-10 stsp done:
846 59d1e4a0 2021-03-10 stsp free(buf);
847 59d1e4a0 2021-03-10 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
848 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
849 59d1e4a0 2021-03-10 stsp got_object_close(obj);
850 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
851 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
852 59d1e4a0 2021-03-10 stsp
853 59d1e4a0 2021-03-10 stsp return err;
854 59d1e4a0 2021-03-10 stsp }
855 59d1e4a0 2021-03-10 stsp
856 59d1e4a0 2021-03-10 stsp
857 59d1e4a0 2021-03-10 stsp
858 59d1e4a0 2021-03-10 stsp static const struct got_error *
859 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
860 876c234b 2018-09-10 stsp {
861 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
862 876c234b 2018-09-10 stsp struct imsg imsg;
863 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
864 876c234b 2018-09-10 stsp size_t datalen;
865 876c234b 2018-09-10 stsp struct got_packidx *p;
866 876c234b 2018-09-10 stsp
867 876c234b 2018-09-10 stsp *packidx = NULL;
868 876c234b 2018-09-10 stsp
869 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
870 876c234b 2018-09-10 stsp if (err)
871 876c234b 2018-09-10 stsp return err;
872 876c234b 2018-09-10 stsp
873 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
874 876c234b 2018-09-10 stsp if (p == NULL) {
875 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
876 876c234b 2018-09-10 stsp goto done;
877 876c234b 2018-09-10 stsp }
878 876c234b 2018-09-10 stsp
879 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
880 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
881 876c234b 2018-09-10 stsp goto done;
882 876c234b 2018-09-10 stsp }
883 876c234b 2018-09-10 stsp
884 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
885 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
886 876c234b 2018-09-10 stsp goto done;
887 876c234b 2018-09-10 stsp }
888 876c234b 2018-09-10 stsp
889 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
890 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
891 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
892 876c234b 2018-09-10 stsp goto done;
893 876c234b 2018-09-10 stsp }
894 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
895 876c234b 2018-09-10 stsp
896 876c234b 2018-09-10 stsp p->len = ipackidx.len;
897 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
898 876c234b 2018-09-10 stsp if (p->fd == -1) {
899 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
900 56bef47a 2018-09-15 stsp goto done;
901 56bef47a 2018-09-15 stsp }
902 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
903 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
904 876c234b 2018-09-10 stsp goto done;
905 876c234b 2018-09-10 stsp }
906 876c234b 2018-09-10 stsp
907 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
908 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
909 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
910 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
911 876c234b 2018-09-10 stsp #endif
912 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
913 876c234b 2018-09-10 stsp done:
914 876c234b 2018-09-10 stsp if (err) {
915 876c234b 2018-09-10 stsp if (imsg.fd != -1)
916 876c234b 2018-09-10 stsp close(imsg.fd);
917 876c234b 2018-09-10 stsp got_packidx_close(p);
918 876c234b 2018-09-10 stsp } else
919 876c234b 2018-09-10 stsp *packidx = p;
920 876c234b 2018-09-10 stsp imsg_free(&imsg);
921 876c234b 2018-09-10 stsp return err;
922 876c234b 2018-09-10 stsp }
923 876c234b 2018-09-10 stsp
924 876c234b 2018-09-10 stsp static const struct got_error *
925 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
926 876c234b 2018-09-10 stsp {
927 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
928 876c234b 2018-09-10 stsp struct imsg imsg;
929 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
930 876c234b 2018-09-10 stsp size_t datalen;
931 876c234b 2018-09-10 stsp struct got_pack *pack;
932 876c234b 2018-09-10 stsp
933 876c234b 2018-09-10 stsp *packp = NULL;
934 876c234b 2018-09-10 stsp
935 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
936 876c234b 2018-09-10 stsp if (err)
937 876c234b 2018-09-10 stsp return err;
938 876c234b 2018-09-10 stsp
939 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
940 876c234b 2018-09-10 stsp if (pack == NULL) {
941 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
942 876c234b 2018-09-10 stsp goto done;
943 876c234b 2018-09-10 stsp }
944 876c234b 2018-09-10 stsp
945 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
946 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
947 876c234b 2018-09-10 stsp goto done;
948 876c234b 2018-09-10 stsp }
949 876c234b 2018-09-10 stsp
950 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
951 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
952 876c234b 2018-09-10 stsp goto done;
953 876c234b 2018-09-10 stsp }
954 876c234b 2018-09-10 stsp
955 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
956 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
957 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
958 876c234b 2018-09-10 stsp goto done;
959 876c234b 2018-09-10 stsp }
960 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
961 876c234b 2018-09-10 stsp
962 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
963 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
964 876c234b 2018-09-10 stsp if (pack->fd == -1) {
965 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
966 876c234b 2018-09-10 stsp goto done;
967 876c234b 2018-09-10 stsp }
968 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
969 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
970 56bef47a 2018-09-15 stsp goto done;
971 56bef47a 2018-09-15 stsp }
972 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
973 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
974 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
975 ab2f42e7 2019-11-10 stsp goto done;
976 ab2f42e7 2019-11-10 stsp }
977 ab2f42e7 2019-11-10 stsp
978 ab2f42e7 2019-11-10 stsp pack->delta_cache = got_delta_cache_alloc(100,
979 ab2f42e7 2019-11-10 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
980 ab2f42e7 2019-11-10 stsp if (pack->delta_cache == NULL) {
981 ab2f42e7 2019-11-10 stsp err = got_error_from_errno("got_delta_cache_alloc");
982 876c234b 2018-09-10 stsp goto done;
983 876c234b 2018-09-10 stsp }
984 876c234b 2018-09-10 stsp
985 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
986 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
987 876c234b 2018-09-10 stsp pack->fd, 0);
988 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
989 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
990 876c234b 2018-09-10 stsp #endif
991 876c234b 2018-09-10 stsp done:
992 876c234b 2018-09-10 stsp if (err) {
993 876c234b 2018-09-10 stsp if (imsg.fd != -1)
994 876c234b 2018-09-10 stsp close(imsg.fd);
995 876c234b 2018-09-10 stsp free(pack);
996 876c234b 2018-09-10 stsp } else
997 876c234b 2018-09-10 stsp *packp = pack;
998 876c234b 2018-09-10 stsp imsg_free(&imsg);
999 876c234b 2018-09-10 stsp return err;
1000 876c234b 2018-09-10 stsp }
1001 876c234b 2018-09-10 stsp
1002 876c234b 2018-09-10 stsp int
1003 876c234b 2018-09-10 stsp main(int argc, char *argv[])
1004 876c234b 2018-09-10 stsp {
1005 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1006 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
1007 876c234b 2018-09-10 stsp struct imsg imsg;
1008 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
1009 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
1010 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1011 bc1f382f 2022-01-05 thomas FILE *basefile = NULL, *accumfile = NULL;
1012 876c234b 2018-09-10 stsp
1013 876c234b 2018-09-10 stsp //static int attached;
1014 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1015 876c234b 2018-09-10 stsp
1016 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1017 99437157 2018-11-11 stsp
1018 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1019 876c234b 2018-09-10 stsp
1020 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1021 c59b3346 2018-09-11 stsp if (err) {
1022 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1023 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1024 c59b3346 2018-09-11 stsp return 1;
1025 c59b3346 2018-09-11 stsp }
1026 c59b3346 2018-09-11 stsp
1027 2ff12563 2018-09-15 stsp #ifndef PROFILE
1028 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1029 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1030 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1031 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1032 876c234b 2018-09-10 stsp return 1;
1033 876c234b 2018-09-10 stsp }
1034 2ff12563 2018-09-15 stsp #endif
1035 876c234b 2018-09-10 stsp
1036 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1037 876c234b 2018-09-10 stsp if (err) {
1038 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1039 876c234b 2018-09-10 stsp return 1;
1040 876c234b 2018-09-10 stsp }
1041 876c234b 2018-09-10 stsp
1042 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1043 876c234b 2018-09-10 stsp if (err) {
1044 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1045 876c234b 2018-09-10 stsp return 1;
1046 876c234b 2018-09-10 stsp }
1047 876c234b 2018-09-10 stsp
1048 656b1f76 2019-05-11 jcs for (;;) {
1049 876c234b 2018-09-10 stsp imsg.fd = -1;
1050 99437157 2018-11-11 stsp
1051 99437157 2018-11-11 stsp if (sigint_received) {
1052 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1053 99437157 2018-11-11 stsp break;
1054 99437157 2018-11-11 stsp }
1055 876c234b 2018-09-10 stsp
1056 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1057 876c234b 2018-09-10 stsp if (err) {
1058 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1059 876c234b 2018-09-10 stsp err = NULL;
1060 876c234b 2018-09-10 stsp break;
1061 876c234b 2018-09-10 stsp }
1062 876c234b 2018-09-10 stsp
1063 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1064 876c234b 2018-09-10 stsp break;
1065 876c234b 2018-09-10 stsp
1066 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1067 bc1f382f 2022-01-05 thomas case GOT_IMSG_TMPFD:
1068 bc1f382f 2022-01-05 thomas err = receive_tempfile(&basefile, &accumfile,
1069 bc1f382f 2022-01-05 thomas &imsg, &ibuf);
1070 bc1f382f 2022-01-05 thomas break;
1071 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
1072 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
1073 c59b3346 2018-09-11 stsp &objcache);
1074 876c234b 2018-09-10 stsp break;
1075 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1076 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
1077 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1078 bc1f382f 2022-01-05 thomas break;
1079 bc1f382f 2022-01-05 thomas }
1080 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
1081 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
1082 59d1e4a0 2021-03-10 stsp break;
1083 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
1084 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
1085 7762fe12 2018-11-05 stsp &objcache);
1086 7762fe12 2018-11-05 stsp break;
1087 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
1088 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
1089 62d463ca 2020-10-20 naddy &objcache);
1090 876c234b 2018-09-10 stsp break;
1091 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
1092 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
1093 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1094 bc1f382f 2022-01-05 thomas break;
1095 bc1f382f 2022-01-05 thomas }
1096 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
1097 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
1098 876c234b 2018-09-10 stsp break;
1099 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
1100 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
1101 62d463ca 2020-10-20 naddy &objcache);
1102 f4a881ce 2018-11-17 stsp break;
1103 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1104 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
1105 ca6e02ac 2020-01-07 stsp packidx, &objcache);
1106 ca6e02ac 2020-01-07 stsp break;
1107 876c234b 2018-09-10 stsp default:
1108 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1109 876c234b 2018-09-10 stsp break;
1110 876c234b 2018-09-10 stsp }
1111 876c234b 2018-09-10 stsp
1112 08578a35 2021-01-22 stsp if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1113 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1114 876c234b 2018-09-10 stsp imsg_free(&imsg);
1115 99437157 2018-11-11 stsp if (err)
1116 876c234b 2018-09-10 stsp break;
1117 876c234b 2018-09-10 stsp }
1118 876c234b 2018-09-10 stsp
1119 c59b3346 2018-09-11 stsp if (packidx)
1120 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
1121 c59b3346 2018-09-11 stsp if (pack)
1122 c59b3346 2018-09-11 stsp got_pack_close(pack);
1123 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
1124 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
1125 bc1f382f 2022-01-05 thomas if (basefile && fclose(basefile) == EOF && err == NULL)
1126 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
1127 bc1f382f 2022-01-05 thomas if (accumfile && fclose(accumfile) == EOF && err == NULL)
1128 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
1129 99437157 2018-11-11 stsp if (err) {
1130 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1131 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1132 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
1133 80d5f134 2018-11-11 stsp }
1134 99437157 2018-11-11 stsp }
1135 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1136 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1137 876c234b 2018-09-10 stsp return err ? 1 : 0;
1138 876c234b 2018-09-10 stsp }