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/queue.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 <imsg.h>
27 876c234b 2018-09-10 stsp #include <stdio.h>
28 876c234b 2018-09-10 stsp #include <stdlib.h>
29 876c234b 2018-09-10 stsp #include <string.h>
30 876c234b 2018-09-10 stsp #include <sha1.h>
31 81a12da5 2020-09-09 naddy #include <unistd.h>
32 876c234b 2018-09-10 stsp #include <zlib.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 fae7e038 2022-05-07 stsp #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 876c234b 2018-09-10 stsp
47 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
48 99437157 2018-11-11 stsp
49 99437157 2018-11-11 stsp static void
50 99437157 2018-11-11 stsp catch_sigint(int signo)
51 99437157 2018-11-11 stsp {
52 99437157 2018-11-11 stsp sigint_received = 1;
53 99437157 2018-11-11 stsp }
54 99437157 2018-11-11 stsp
55 876c234b 2018-09-10 stsp static const struct got_error *
56 704b89c4 2019-05-23 stsp open_object(struct got_object **obj, struct got_pack *pack,
57 704b89c4 2019-05-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id,
58 704b89c4 2019-05-23 stsp struct got_object_cache *objcache)
59 704b89c4 2019-05-23 stsp {
60 704b89c4 2019-05-23 stsp const struct got_error *err;
61 704b89c4 2019-05-23 stsp
62 704b89c4 2019-05-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
63 704b89c4 2019-05-23 stsp if (err)
64 704b89c4 2019-05-23 stsp return err;
65 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
66 704b89c4 2019-05-23 stsp
67 704b89c4 2019-05-23 stsp err = got_object_cache_add(objcache, id, *obj);
68 79c99a64 2019-05-23 stsp if (err) {
69 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
70 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
71 79c99a64 2019-05-23 stsp err = NULL;
72 704b89c4 2019-05-23 stsp return err;
73 79c99a64 2019-05-23 stsp }
74 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
75 704b89c4 2019-05-23 stsp return NULL;
76 704b89c4 2019-05-23 stsp }
77 704b89c4 2019-05-23 stsp
78 704b89c4 2019-05-23 stsp static const struct got_error *
79 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
80 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
81 876c234b 2018-09-10 stsp {
82 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
83 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
84 876c234b 2018-09-10 stsp struct got_object *obj;
85 106807b4 2018-09-15 stsp struct got_object_id id;
86 876c234b 2018-09-10 stsp size_t datalen;
87 876c234b 2018-09-10 stsp
88 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
89 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
90 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
91 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
92 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
93 876c234b 2018-09-10 stsp
94 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
95 704b89c4 2019-05-23 stsp if (obj) {
96 704b89c4 2019-05-23 stsp obj->refcnt++;
97 704b89c4 2019-05-23 stsp } else {
98 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
99 704b89c4 2019-05-23 stsp objcache);
100 704b89c4 2019-05-23 stsp if (err)
101 704b89c4 2019-05-23 stsp goto done;
102 704b89c4 2019-05-23 stsp }
103 876c234b 2018-09-10 stsp
104 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
105 c59b3346 2018-09-11 stsp done:
106 876c234b 2018-09-10 stsp got_object_close(obj);
107 876c234b 2018-09-10 stsp return err;
108 876c234b 2018-09-10 stsp }
109 876c234b 2018-09-10 stsp
110 50127d69 2021-09-25 stsp static const struct got_error *
111 ca6e02ac 2020-01-07 stsp open_commit(struct got_commit_object **commit, struct got_pack *pack,
112 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
113 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
114 876c234b 2018-09-10 stsp {
115 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
116 cb5e38fd 2019-05-23 stsp struct got_object *obj = NULL;
117 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
118 cfd633c2 2018-09-10 stsp size_t len;
119 cfd633c2 2018-09-10 stsp
120 ca6e02ac 2020-01-07 stsp *commit = NULL;
121 1785f84a 2018-12-23 stsp
122 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
123 704b89c4 2019-05-23 stsp if (obj) {
124 704b89c4 2019-05-23 stsp obj->refcnt++;
125 704b89c4 2019-05-23 stsp } else {
126 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
127 704b89c4 2019-05-23 stsp objcache);
128 704b89c4 2019-05-23 stsp if (err)
129 704b89c4 2019-05-23 stsp return err;
130 704b89c4 2019-05-23 stsp }
131 cfd633c2 2018-09-10 stsp
132 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
133 cfd633c2 2018-09-10 stsp if (err)
134 cb5e38fd 2019-05-23 stsp goto done;
135 cfd633c2 2018-09-10 stsp
136 cfd633c2 2018-09-10 stsp obj->size = len;
137 ca6e02ac 2020-01-07 stsp
138 ca6e02ac 2020-01-07 stsp err = got_object_parse_commit(commit, buf, len);
139 ca6e02ac 2020-01-07 stsp done:
140 ca6e02ac 2020-01-07 stsp got_object_close(obj);
141 ca6e02ac 2020-01-07 stsp free(buf);
142 ca6e02ac 2020-01-07 stsp return err;
143 ca6e02ac 2020-01-07 stsp }
144 ca6e02ac 2020-01-07 stsp
145 ca6e02ac 2020-01-07 stsp static const struct got_error *
146 ca6e02ac 2020-01-07 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
147 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
148 ca6e02ac 2020-01-07 stsp {
149 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
150 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
151 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL;
152 ca6e02ac 2020-01-07 stsp struct got_object_id id;
153 ca6e02ac 2020-01-07 stsp size_t datalen;
154 ca6e02ac 2020-01-07 stsp
155 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
156 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(iobj))
157 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
158 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
159 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
160 ca6e02ac 2020-01-07 stsp
161 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
162 cb5e38fd 2019-05-23 stsp if (err)
163 cb5e38fd 2019-05-23 stsp goto done;
164 cfd633c2 2018-09-10 stsp
165 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
166 cb5e38fd 2019-05-23 stsp done:
167 cb5e38fd 2019-05-23 stsp if (commit)
168 cb5e38fd 2019-05-23 stsp got_object_commit_close(commit);
169 7762fe12 2018-11-05 stsp if (err) {
170 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
171 7762fe12 2018-11-05 stsp err = NULL;
172 7762fe12 2018-11-05 stsp else
173 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
174 7762fe12 2018-11-05 stsp }
175 7762fe12 2018-11-05 stsp
176 7762fe12 2018-11-05 stsp return err;
177 7762fe12 2018-11-05 stsp }
178 7762fe12 2018-11-05 stsp
179 50127d69 2021-09-25 stsp static const struct got_error *
180 ca6e02ac 2020-01-07 stsp open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
181 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
182 ca6e02ac 2020-01-07 stsp struct got_object_id *id, struct got_object_cache *objcache)
183 ca6e02ac 2020-01-07 stsp {
184 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
185 ca6e02ac 2020-01-07 stsp struct got_object *obj = NULL;
186 ca6e02ac 2020-01-07 stsp size_t len;
187 ca6e02ac 2020-01-07 stsp
188 ca6e02ac 2020-01-07 stsp *buf = NULL;
189 ca6e02ac 2020-01-07 stsp *nentries = 0;
190 ca6e02ac 2020-01-07 stsp
191 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
192 ca6e02ac 2020-01-07 stsp if (obj) {
193 ca6e02ac 2020-01-07 stsp obj->refcnt++;
194 ca6e02ac 2020-01-07 stsp } else {
195 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
196 ca6e02ac 2020-01-07 stsp objcache);
197 ca6e02ac 2020-01-07 stsp if (err)
198 ca6e02ac 2020-01-07 stsp return err;
199 ca6e02ac 2020-01-07 stsp }
200 ca6e02ac 2020-01-07 stsp
201 ca6e02ac 2020-01-07 stsp err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
202 ca6e02ac 2020-01-07 stsp if (err)
203 ca6e02ac 2020-01-07 stsp goto done;
204 ca6e02ac 2020-01-07 stsp
205 ca6e02ac 2020-01-07 stsp obj->size = len;
206 ca6e02ac 2020-01-07 stsp
207 ca6e02ac 2020-01-07 stsp err = got_object_parse_tree(entries, nentries, *buf, len);
208 ca6e02ac 2020-01-07 stsp done:
209 ca6e02ac 2020-01-07 stsp got_object_close(obj);
210 ca6e02ac 2020-01-07 stsp if (err) {
211 ca6e02ac 2020-01-07 stsp free(*buf);
212 ca6e02ac 2020-01-07 stsp *buf = NULL;
213 ca6e02ac 2020-01-07 stsp }
214 ca6e02ac 2020-01-07 stsp return err;
215 ca6e02ac 2020-01-07 stsp }
216 ca6e02ac 2020-01-07 stsp
217 7762fe12 2018-11-05 stsp static const struct got_error *
218 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
219 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
220 876c234b 2018-09-10 stsp {
221 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
222 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
223 3022d272 2019-11-14 stsp struct got_pathlist_head entries;
224 3022d272 2019-11-14 stsp int nentries = 0;
225 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
226 13c729f7 2018-12-24 stsp struct got_object_id id;
227 13c729f7 2018-12-24 stsp size_t datalen;
228 e7885405 2018-09-10 stsp
229 3022d272 2019-11-14 stsp TAILQ_INIT(&entries);
230 3022d272 2019-11-14 stsp
231 13c729f7 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
232 13c729f7 2018-12-24 stsp if (datalen != sizeof(iobj))
233 13c729f7 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
234 13c729f7 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
235 13c729f7 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
236 13c729f7 2018-12-24 stsp
237 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
238 62d463ca 2020-10-20 naddy &id, objcache);
239 e7885405 2018-09-10 stsp if (err)
240 ca6e02ac 2020-01-07 stsp return err;
241 e7885405 2018-09-10 stsp
242 3022d272 2019-11-14 stsp err = got_privsep_send_tree(ibuf, &entries, nentries);
243 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_free(&entries);
244 cb5e38fd 2019-05-23 stsp free(buf);
245 e7885405 2018-09-10 stsp if (err) {
246 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
247 e7885405 2018-09-10 stsp err = NULL;
248 e7885405 2018-09-10 stsp else
249 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
250 e7885405 2018-09-10 stsp }
251 e7885405 2018-09-10 stsp
252 e7885405 2018-09-10 stsp return err;
253 876c234b 2018-09-10 stsp }
254 876c234b 2018-09-10 stsp
255 876c234b 2018-09-10 stsp static const struct got_error *
256 030daac8 2021-09-25 stsp receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
257 876c234b 2018-09-10 stsp {
258 3840f4c9 2018-09-12 stsp const struct got_error *err;
259 3840f4c9 2018-09-12 stsp struct imsg imsg;
260 55da3778 2018-09-10 stsp size_t datalen;
261 55da3778 2018-09-10 stsp
262 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
263 55da3778 2018-09-10 stsp if (err)
264 55da3778 2018-09-10 stsp return err;
265 55da3778 2018-09-10 stsp
266 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
267 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
268 55da3778 2018-09-10 stsp goto done;
269 55da3778 2018-09-10 stsp }
270 55da3778 2018-09-10 stsp
271 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
272 55da3778 2018-09-10 stsp if (datalen != 0) {
273 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
274 55da3778 2018-09-10 stsp goto done;
275 55da3778 2018-09-10 stsp }
276 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
277 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
278 55da3778 2018-09-10 stsp goto done;
279 55da3778 2018-09-10 stsp }
280 55da3778 2018-09-10 stsp
281 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
282 3840f4c9 2018-09-12 stsp if (*f == NULL) {
283 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
284 3a6ce05a 2019-02-11 stsp close(imsg.fd);
285 55da3778 2018-09-10 stsp goto done;
286 55da3778 2018-09-10 stsp }
287 3840f4c9 2018-09-12 stsp done:
288 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
289 3840f4c9 2018-09-12 stsp return err;
290 db696021 2022-01-04 stsp }
291 db696021 2022-01-04 stsp
292 db696021 2022-01-04 stsp static const struct got_error *
293 67fd6849 2022-02-13 stsp receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
294 db696021 2022-01-04 stsp struct imsgbuf *ibuf)
295 db696021 2022-01-04 stsp {
296 db696021 2022-01-04 stsp size_t datalen;
297 db696021 2022-01-04 stsp
298 db696021 2022-01-04 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
299 db696021 2022-01-04 stsp if (datalen != 0)
300 db696021 2022-01-04 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
301 db696021 2022-01-04 stsp
302 db696021 2022-01-04 stsp if (imsg->fd == -1)
303 db696021 2022-01-04 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
304 db696021 2022-01-04 stsp
305 67fd6849 2022-02-13 stsp *f = fdopen(imsg->fd, mode);
306 db696021 2022-01-04 stsp if (*f == NULL)
307 db696021 2022-01-04 stsp return got_error_from_errno("fdopen");
308 db696021 2022-01-04 stsp imsg->fd = -1;
309 db696021 2022-01-04 stsp
310 db696021 2022-01-04 stsp return NULL;
311 3840f4c9 2018-09-12 stsp }
312 55da3778 2018-09-10 stsp
313 3840f4c9 2018-09-12 stsp static const struct got_error *
314 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
315 db696021 2022-01-04 stsp struct got_packidx *packidx, struct got_object_cache *objcache,
316 db696021 2022-01-04 stsp FILE *basefile, FILE *accumfile)
317 3840f4c9 2018-09-12 stsp {
318 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
319 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
320 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
321 db696021 2022-01-04 stsp FILE *outfile = NULL;
322 ebc55e2d 2018-12-24 stsp struct got_object_id id;
323 ebc55e2d 2018-12-24 stsp size_t datalen;
324 ac544f8c 2019-01-13 stsp uint64_t blob_size;
325 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
326 3840f4c9 2018-09-12 stsp
327 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
328 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
329 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
330 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
331 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
332 ebc55e2d 2018-12-24 stsp
333 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
334 704b89c4 2019-05-23 stsp if (obj) {
335 704b89c4 2019-05-23 stsp obj->refcnt++;
336 704b89c4 2019-05-23 stsp } else {
337 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
338 704b89c4 2019-05-23 stsp objcache);
339 704b89c4 2019-05-23 stsp if (err)
340 704b89c4 2019-05-23 stsp return err;
341 704b89c4 2019-05-23 stsp }
342 3840f4c9 2018-09-12 stsp
343 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
344 3840f4c9 2018-09-12 stsp if (err)
345 ac544f8c 2019-01-13 stsp goto done;
346 3840f4c9 2018-09-12 stsp
347 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
348 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
349 ac544f8c 2019-01-13 stsp if (err)
350 ac544f8c 2019-01-13 stsp goto done;
351 ac544f8c 2019-01-13 stsp } else
352 ac544f8c 2019-01-13 stsp blob_size = obj->size;
353 ac544f8c 2019-01-13 stsp
354 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
355 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
356 ac544f8c 2019-01-13 stsp obj, pack);
357 ac544f8c 2019-01-13 stsp else
358 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
359 ac544f8c 2019-01-13 stsp accumfile);
360 3840f4c9 2018-09-12 stsp if (err)
361 55da3778 2018-09-10 stsp goto done;
362 55da3778 2018-09-10 stsp
363 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
364 55da3778 2018-09-10 stsp done:
365 ac544f8c 2019-01-13 stsp free(buf);
366 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
367 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
368 cb5e38fd 2019-05-23 stsp got_object_close(obj);
369 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
370 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
371 55da3778 2018-09-10 stsp
372 55da3778 2018-09-10 stsp return err;
373 876c234b 2018-09-10 stsp }
374 876c234b 2018-09-10 stsp
375 876c234b 2018-09-10 stsp static const struct got_error *
376 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
377 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
378 f4a881ce 2018-11-17 stsp {
379 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
380 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
381 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
382 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
383 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
384 f4a881ce 2018-11-17 stsp size_t len;
385 268f7291 2018-12-24 stsp struct got_object_id id;
386 268f7291 2018-12-24 stsp size_t datalen;
387 f4a881ce 2018-11-17 stsp
388 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
389 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
390 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
391 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
392 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
393 268f7291 2018-12-24 stsp
394 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
395 704b89c4 2019-05-23 stsp if (obj) {
396 704b89c4 2019-05-23 stsp obj->refcnt++;
397 704b89c4 2019-05-23 stsp } else {
398 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
399 704b89c4 2019-05-23 stsp objcache);
400 704b89c4 2019-05-23 stsp if (err)
401 704b89c4 2019-05-23 stsp return err;
402 704b89c4 2019-05-23 stsp }
403 f4a881ce 2018-11-17 stsp
404 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
405 f4a881ce 2018-11-17 stsp if (err)
406 cb5e38fd 2019-05-23 stsp goto done;
407 f4a881ce 2018-11-17 stsp
408 f4a881ce 2018-11-17 stsp obj->size = len;
409 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
410 0ae4af15 2019-02-01 stsp if (err)
411 cb5e38fd 2019-05-23 stsp goto done;
412 f4a881ce 2018-11-17 stsp
413 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
414 cb5e38fd 2019-05-23 stsp done:
415 cb5e38fd 2019-05-23 stsp free(buf);
416 cb5e38fd 2019-05-23 stsp got_object_close(obj);
417 cb5e38fd 2019-05-23 stsp if (tag)
418 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
419 ca6e02ac 2020-01-07 stsp if (err) {
420 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
421 ca6e02ac 2020-01-07 stsp err = NULL;
422 ca6e02ac 2020-01-07 stsp else
423 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
424 ca6e02ac 2020-01-07 stsp }
425 ca6e02ac 2020-01-07 stsp
426 ca6e02ac 2020-01-07 stsp return err;
427 ca6e02ac 2020-01-07 stsp }
428 ca6e02ac 2020-01-07 stsp
429 ca6e02ac 2020-01-07 stsp static struct got_parsed_tree_entry *
430 ca6e02ac 2020-01-07 stsp find_entry_by_name(struct got_pathlist_head *entries, int nentries,
431 ca6e02ac 2020-01-07 stsp const char *name, size_t len)
432 ca6e02ac 2020-01-07 stsp {
433 ca6e02ac 2020-01-07 stsp struct got_pathlist_entry *pe;
434 ca6e02ac 2020-01-07 stsp
435 ca6e02ac 2020-01-07 stsp /* Note that tree entries are sorted in strncmp() order. */
436 ca6e02ac 2020-01-07 stsp TAILQ_FOREACH(pe, entries, entry) {
437 ca6e02ac 2020-01-07 stsp int cmp = strncmp(pe->path, name, len);
438 ca6e02ac 2020-01-07 stsp if (cmp < 0)
439 ca6e02ac 2020-01-07 stsp continue;
440 ca6e02ac 2020-01-07 stsp if (cmp > 0)
441 ca6e02ac 2020-01-07 stsp break;
442 ca6e02ac 2020-01-07 stsp if (pe->path[len] == '\0')
443 ca6e02ac 2020-01-07 stsp return (struct got_parsed_tree_entry *)pe->data;
444 ca6e02ac 2020-01-07 stsp }
445 ca6e02ac 2020-01-07 stsp return NULL;
446 ca6e02ac 2020-01-07 stsp }
447 ca6e02ac 2020-01-07 stsp
448 50127d69 2021-09-25 stsp static const struct got_error *
449 ca6e02ac 2020-01-07 stsp tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
450 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries1, int *nentries1,
451 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries2, int *nentries2,
452 ca6e02ac 2020-01-07 stsp const char *path, struct got_pack *pack, struct got_packidx *packidx,
453 ca6e02ac 2020-01-07 stsp struct imsgbuf *ibuf, struct got_object_cache *objcache)
454 ca6e02ac 2020-01-07 stsp {
455 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
456 ca6e02ac 2020-01-07 stsp struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
457 ca6e02ac 2020-01-07 stsp const char *seg, *s;
458 ca6e02ac 2020-01-07 stsp size_t seglen;
459 ca6e02ac 2020-01-07 stsp
460 ca6e02ac 2020-01-07 stsp *changed = 0;
461 ca6e02ac 2020-01-07 stsp
462 ca6e02ac 2020-01-07 stsp /* We not do support comparing the root path. */
463 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
464 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
465 ca6e02ac 2020-01-07 stsp
466 ca6e02ac 2020-01-07 stsp s = path;
467 61a7d79f 2020-02-29 stsp while (*s == '/')
468 61a7d79f 2020-02-29 stsp s++;
469 ca6e02ac 2020-01-07 stsp seg = s;
470 ca6e02ac 2020-01-07 stsp seglen = 0;
471 ca6e02ac 2020-01-07 stsp while (*s) {
472 ca6e02ac 2020-01-07 stsp if (*s != '/') {
473 ca6e02ac 2020-01-07 stsp s++;
474 ca6e02ac 2020-01-07 stsp seglen++;
475 ca6e02ac 2020-01-07 stsp if (*s)
476 ca6e02ac 2020-01-07 stsp continue;
477 ca6e02ac 2020-01-07 stsp }
478 ca6e02ac 2020-01-07 stsp
479 ca6e02ac 2020-01-07 stsp pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
480 ca6e02ac 2020-01-07 stsp if (pte1 == NULL) {
481 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_NO_OBJ);
482 ca6e02ac 2020-01-07 stsp break;
483 ca6e02ac 2020-01-07 stsp }
484 ca6e02ac 2020-01-07 stsp
485 ca6e02ac 2020-01-07 stsp pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
486 ca6e02ac 2020-01-07 stsp if (pte2 == NULL) {
487 ca6e02ac 2020-01-07 stsp *changed = 1;
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 if (pte1->mode != pte2->mode) {
492 ca6e02ac 2020-01-07 stsp *changed = 1;
493 ca6e02ac 2020-01-07 stsp break;
494 ca6e02ac 2020-01-07 stsp }
495 ca6e02ac 2020-01-07 stsp
496 ca6e02ac 2020-01-07 stsp if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
497 ca6e02ac 2020-01-07 stsp *changed = 0;
498 ca6e02ac 2020-01-07 stsp break;
499 ca6e02ac 2020-01-07 stsp }
500 ca6e02ac 2020-01-07 stsp
501 ca6e02ac 2020-01-07 stsp if (*s == '\0') { /* final path element */
502 ca6e02ac 2020-01-07 stsp *changed = 1;
503 ca6e02ac 2020-01-07 stsp break;
504 ca6e02ac 2020-01-07 stsp }
505 ca6e02ac 2020-01-07 stsp
506 ca6e02ac 2020-01-07 stsp seg = s + 1;
507 ca6e02ac 2020-01-07 stsp s++;
508 ca6e02ac 2020-01-07 stsp seglen = 0;
509 ca6e02ac 2020-01-07 stsp if (*s) {
510 ca6e02ac 2020-01-07 stsp struct got_object_id id1, id2;
511 ca6e02ac 2020-01-07 stsp int idx;
512 ca6e02ac 2020-01-07 stsp
513 ded8fbb8 2020-04-19 stsp memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
514 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id1);
515 ca6e02ac 2020-01-07 stsp if (idx == -1) {
516 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id1);
517 ca6e02ac 2020-01-07 stsp break;
518 ca6e02ac 2020-01-07 stsp }
519 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries1);
520 ca6e02ac 2020-01-07 stsp *nentries1 = 0;
521 ca6e02ac 2020-01-07 stsp free(*buf1);
522 ca6e02ac 2020-01-07 stsp *buf1 = NULL;
523 ca6e02ac 2020-01-07 stsp err = open_tree(buf1, entries1, nentries1, pack,
524 ca6e02ac 2020-01-07 stsp packidx, idx, &id1, objcache);
525 ca6e02ac 2020-01-07 stsp pte1 = NULL;
526 ca6e02ac 2020-01-07 stsp if (err)
527 ca6e02ac 2020-01-07 stsp break;
528 ca6e02ac 2020-01-07 stsp
529 ded8fbb8 2020-04-19 stsp memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
530 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id2);
531 ca6e02ac 2020-01-07 stsp if (idx == -1) {
532 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id2);
533 ca6e02ac 2020-01-07 stsp break;
534 ca6e02ac 2020-01-07 stsp }
535 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries2);
536 ca6e02ac 2020-01-07 stsp *nentries2 = 0;
537 ca6e02ac 2020-01-07 stsp free(*buf2);
538 ca6e02ac 2020-01-07 stsp *buf2 = NULL;
539 ca6e02ac 2020-01-07 stsp err = open_tree(buf2, entries2, nentries2, pack,
540 ca6e02ac 2020-01-07 stsp packidx, idx, &id2, objcache);
541 ca6e02ac 2020-01-07 stsp pte2 = NULL;
542 ca6e02ac 2020-01-07 stsp if (err)
543 ca6e02ac 2020-01-07 stsp break;
544 ca6e02ac 2020-01-07 stsp }
545 ca6e02ac 2020-01-07 stsp }
546 ca6e02ac 2020-01-07 stsp
547 ca6e02ac 2020-01-07 stsp return err;
548 ca6e02ac 2020-01-07 stsp }
549 ca6e02ac 2020-01-07 stsp
550 ca6e02ac 2020-01-07 stsp static const struct got_error *
551 e70bf110 2020-03-22 stsp send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
552 e70bf110 2020-03-22 stsp struct imsgbuf *ibuf)
553 e70bf110 2020-03-22 stsp {
554 e70bf110 2020-03-22 stsp const struct got_error *err;
555 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
556 030daac8 2021-09-25 stsp size_t i;
557 e70bf110 2020-03-22 stsp
558 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
559 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
560 e70bf110 2020-03-22 stsp ncommits * SHA1_DIGEST_LENGTH);
561 e70bf110 2020-03-22 stsp if (wbuf == NULL)
562 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
563 e70bf110 2020-03-22 stsp
564 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
565 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
566 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
567 e70bf110 2020-03-22 stsp return err;
568 e70bf110 2020-03-22 stsp }
569 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
570 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
571 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
572 e70bf110 2020-03-22 stsp err = got_error_from_errno(
573 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
574 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
575 e70bf110 2020-03-22 stsp return err;
576 e70bf110 2020-03-22 stsp }
577 e70bf110 2020-03-22 stsp }
578 e70bf110 2020-03-22 stsp
579 e70bf110 2020-03-22 stsp wbuf->fd = -1;
580 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
581 e70bf110 2020-03-22 stsp
582 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
583 e70bf110 2020-03-22 stsp }
584 e70bf110 2020-03-22 stsp
585 e70bf110 2020-03-22 stsp static const struct got_error *
586 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
587 e70bf110 2020-03-22 stsp {
588 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
589 e70bf110 2020-03-22 stsp NULL, 0) == -1)
590 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
591 e70bf110 2020-03-22 stsp
592 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
593 e70bf110 2020-03-22 stsp }
594 e70bf110 2020-03-22 stsp
595 e70bf110 2020-03-22 stsp
596 e70bf110 2020-03-22 stsp static const struct got_error *
597 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
598 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
599 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
600 ca6e02ac 2020-01-07 stsp {
601 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
602 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
603 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
604 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
605 ca6e02ac 2020-01-07 stsp struct got_pathlist_head entries, pentries;
606 ca6e02ac 2020-01-07 stsp int nentries = 0, pnentries = 0;
607 ca6e02ac 2020-01-07 stsp struct got_object_id id;
608 ca6e02ac 2020-01-07 stsp size_t datalen, path_len;
609 ca6e02ac 2020-01-07 stsp char *path = NULL;
610 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
611 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
612 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
613 ca6e02ac 2020-01-07 stsp
614 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&entries);
615 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&pentries);
616 ca6e02ac 2020-01-07 stsp
617 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
618 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(iobj))
619 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
620 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
621 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
622 ca6e02ac 2020-01-07 stsp
623 ca6e02ac 2020-01-07 stsp path_len = datalen - sizeof(iobj) - 1;
624 ca6e02ac 2020-01-07 stsp if (path_len < 0)
625 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
626 ca6e02ac 2020-01-07 stsp if (path_len > 0) {
627 ca6e02ac 2020-01-07 stsp path = imsg->data + sizeof(iobj);
628 ca6e02ac 2020-01-07 stsp if (path[path_len] != '\0')
629 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
630 ca6e02ac 2020-01-07 stsp }
631 ca6e02ac 2020-01-07 stsp
632 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
633 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
634 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
635 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
636 ca6e02ac 2020-01-07 stsp
637 ca6e02ac 2020-01-07 stsp do {
638 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
639 ca6e02ac 2020-01-07 stsp int idx;
640 ca6e02ac 2020-01-07 stsp
641 ca6e02ac 2020-01-07 stsp if (sigint_received) {
642 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
643 ca6e02ac 2020-01-07 stsp goto done;
644 ca6e02ac 2020-01-07 stsp }
645 ca6e02ac 2020-01-07 stsp
646 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
647 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
648 ca6e02ac 2020-01-07 stsp if (idx == -1)
649 ca6e02ac 2020-01-07 stsp break;
650 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
651 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
652 ca6e02ac 2020-01-07 stsp if (err) {
653 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
654 ca6e02ac 2020-01-07 stsp goto done;
655 ca6e02ac 2020-01-07 stsp err = NULL;
656 ca6e02ac 2020-01-07 stsp break;
657 ca6e02ac 2020-01-07 stsp }
658 ca6e02ac 2020-01-07 stsp }
659 ca6e02ac 2020-01-07 stsp
660 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
661 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
662 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
663 e70bf110 2020-03-22 stsp ibuf);
664 ca6e02ac 2020-01-07 stsp if (err)
665 ca6e02ac 2020-01-07 stsp goto done;
666 ca6e02ac 2020-01-07 stsp ncommits = 0;
667 ca6e02ac 2020-01-07 stsp }
668 ca6e02ac 2020-01-07 stsp ncommits++;
669 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
670 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
671 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
672 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
673 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
674 ca6e02ac 2020-01-07 stsp if (new == NULL) {
675 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
676 ca6e02ac 2020-01-07 stsp goto done;
677 ca6e02ac 2020-01-07 stsp }
678 ca6e02ac 2020-01-07 stsp commit_ids = new;
679 ca6e02ac 2020-01-07 stsp }
680 ca6e02ac 2020-01-07 stsp memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
681 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
682 ca6e02ac 2020-01-07 stsp
683 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
684 ca6e02ac 2020-01-07 stsp if (pid == NULL)
685 ca6e02ac 2020-01-07 stsp break;
686 ca6e02ac 2020-01-07 stsp
687 d7b5a0e8 2022-04-20 stsp idx = got_packidx_get_object_idx(packidx, &pid->id);
688 ca6e02ac 2020-01-07 stsp if (idx == -1)
689 ca6e02ac 2020-01-07 stsp break;
690 ca6e02ac 2020-01-07 stsp
691 d7b5a0e8 2022-04-20 stsp err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
692 ca6e02ac 2020-01-07 stsp objcache);
693 ca6e02ac 2020-01-07 stsp if (err) {
694 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
695 ca6e02ac 2020-01-07 stsp goto done;
696 ca6e02ac 2020-01-07 stsp err = NULL;
697 ca6e02ac 2020-01-07 stsp break;
698 ca6e02ac 2020-01-07 stsp }
699 ca6e02ac 2020-01-07 stsp
700 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
701 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
702 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
703 ca6e02ac 2020-01-07 stsp changed = 1;
704 ca6e02ac 2020-01-07 stsp break;
705 ca6e02ac 2020-01-07 stsp }
706 ca6e02ac 2020-01-07 stsp } else {
707 ca6e02ac 2020-01-07 stsp int pidx;
708 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
709 ca6e02ac 2020-01-07 stsp
710 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
711 ca6e02ac 2020-01-07 stsp commit->tree_id);
712 ca6e02ac 2020-01-07 stsp if (idx == -1)
713 ca6e02ac 2020-01-07 stsp break;
714 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
715 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
716 ca6e02ac 2020-01-07 stsp if (pidx == -1)
717 ca6e02ac 2020-01-07 stsp break;
718 ca6e02ac 2020-01-07 stsp
719 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack,
720 ca6e02ac 2020-01-07 stsp packidx, idx, commit->tree_id, objcache);
721 ca6e02ac 2020-01-07 stsp if (err)
722 ca6e02ac 2020-01-07 stsp goto done;
723 ca6e02ac 2020-01-07 stsp err = open_tree(&pbuf, &pentries, &pnentries, pack,
724 ca6e02ac 2020-01-07 stsp packidx, pidx, pcommit->tree_id, objcache);
725 ca6e02ac 2020-01-07 stsp if (err) {
726 ca6e02ac 2020-01-07 stsp free(buf);
727 ca6e02ac 2020-01-07 stsp goto done;
728 ca6e02ac 2020-01-07 stsp }
729 ca6e02ac 2020-01-07 stsp
730 ca6e02ac 2020-01-07 stsp err = tree_path_changed(&changed, &buf, &pbuf,
731 ca6e02ac 2020-01-07 stsp &entries, &nentries, &pentries, &pnentries, path,
732 ca6e02ac 2020-01-07 stsp pack, packidx, ibuf, objcache);
733 ca6e02ac 2020-01-07 stsp
734 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
735 ca6e02ac 2020-01-07 stsp nentries = 0;
736 ca6e02ac 2020-01-07 stsp free(buf);
737 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
738 ca6e02ac 2020-01-07 stsp pnentries = 0;
739 ca6e02ac 2020-01-07 stsp free(pbuf);
740 ca6e02ac 2020-01-07 stsp if (err) {
741 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
742 ca6e02ac 2020-01-07 stsp goto done;
743 ca6e02ac 2020-01-07 stsp err = NULL;
744 ca6e02ac 2020-01-07 stsp break;
745 ca6e02ac 2020-01-07 stsp }
746 ca6e02ac 2020-01-07 stsp }
747 ca6e02ac 2020-01-07 stsp
748 ca6e02ac 2020-01-07 stsp if (!changed) {
749 d7b5a0e8 2022-04-20 stsp memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
750 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
751 ca6e02ac 2020-01-07 stsp commit = pcommit;
752 ca6e02ac 2020-01-07 stsp pcommit = NULL;
753 ca6e02ac 2020-01-07 stsp }
754 ca6e02ac 2020-01-07 stsp } while (!changed);
755 ca6e02ac 2020-01-07 stsp
756 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
757 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
758 ca6e02ac 2020-01-07 stsp if (err)
759 ca6e02ac 2020-01-07 stsp goto done;
760 ca6e02ac 2020-01-07 stsp
761 ca6e02ac 2020-01-07 stsp if (changed) {
762 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
763 ca6e02ac 2020-01-07 stsp if (err)
764 ca6e02ac 2020-01-07 stsp goto done;
765 ca6e02ac 2020-01-07 stsp }
766 ca6e02ac 2020-01-07 stsp }
767 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
768 ca6e02ac 2020-01-07 stsp done:
769 ca6e02ac 2020-01-07 stsp free(commit_ids);
770 ca6e02ac 2020-01-07 stsp if (commit)
771 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
772 ca6e02ac 2020-01-07 stsp if (pcommit)
773 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
774 ca6e02ac 2020-01-07 stsp if (nentries != 0)
775 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
776 ca6e02ac 2020-01-07 stsp if (pnentries != 0)
777 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
778 f4a881ce 2018-11-17 stsp if (err) {
779 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
780 f4a881ce 2018-11-17 stsp err = NULL;
781 f4a881ce 2018-11-17 stsp else
782 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
783 f4a881ce 2018-11-17 stsp }
784 f4a881ce 2018-11-17 stsp
785 f4a881ce 2018-11-17 stsp return err;
786 f4a881ce 2018-11-17 stsp }
787 f4a881ce 2018-11-17 stsp
788 f4a881ce 2018-11-17 stsp static const struct got_error *
789 c0df5966 2021-12-31 stsp raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
790 c0df5966 2021-12-31 stsp struct got_pack *pack, struct got_packidx *packidx,
791 db696021 2022-01-04 stsp struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
792 59d1e4a0 2021-03-10 stsp {
793 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
794 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
795 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
796 db696021 2022-01-04 stsp FILE *outfile = NULL;
797 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
798 59d1e4a0 2021-03-10 stsp struct got_object *obj;
799 59d1e4a0 2021-03-10 stsp struct got_object_id id;
800 59d1e4a0 2021-03-10 stsp size_t datalen;
801 59d1e4a0 2021-03-10 stsp
802 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
803 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
804 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
805 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
806 59d1e4a0 2021-03-10 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
807 59d1e4a0 2021-03-10 stsp
808 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
809 59d1e4a0 2021-03-10 stsp if (obj) {
810 59d1e4a0 2021-03-10 stsp obj->refcnt++;
811 59d1e4a0 2021-03-10 stsp } else {
812 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
813 59d1e4a0 2021-03-10 stsp objcache);
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
818 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
819 59d1e4a0 2021-03-10 stsp if (err)
820 59d1e4a0 2021-03-10 stsp return err;
821 59d1e4a0 2021-03-10 stsp
822 59d1e4a0 2021-03-10 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
823 59d1e4a0 2021-03-10 stsp err = got_pack_get_max_delta_object_size(&size, obj, pack);
824 59d1e4a0 2021-03-10 stsp if (err)
825 59d1e4a0 2021-03-10 stsp goto done;
826 59d1e4a0 2021-03-10 stsp } else
827 59d1e4a0 2021-03-10 stsp size = obj->size;
828 59d1e4a0 2021-03-10 stsp
829 59d1e4a0 2021-03-10 stsp if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
830 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
831 59d1e4a0 2021-03-10 stsp obj, pack);
832 59d1e4a0 2021-03-10 stsp else
833 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
834 59d1e4a0 2021-03-10 stsp accumfile);
835 59d1e4a0 2021-03-10 stsp if (err)
836 59d1e4a0 2021-03-10 stsp goto done;
837 59d1e4a0 2021-03-10 stsp
838 40e3cb72 2021-06-22 stsp err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
839 59d1e4a0 2021-03-10 stsp done:
840 59d1e4a0 2021-03-10 stsp free(buf);
841 59d1e4a0 2021-03-10 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
842 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
843 59d1e4a0 2021-03-10 stsp got_object_close(obj);
844 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
845 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
846 59d1e4a0 2021-03-10 stsp
847 59d1e4a0 2021-03-10 stsp return err;
848 59d1e4a0 2021-03-10 stsp }
849 67fd6849 2022-02-13 stsp
850 67fd6849 2022-02-13 stsp static const struct got_error *
851 67fd6849 2022-02-13 stsp get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
852 67fd6849 2022-02-13 stsp off_t base_offset)
853 67fd6849 2022-02-13 stsp {
854 67fd6849 2022-02-13 stsp const struct got_error *err;
855 67fd6849 2022-02-13 stsp int idx;
856 59d1e4a0 2021-03-10 stsp
857 67fd6849 2022-02-13 stsp err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
858 67fd6849 2022-02-13 stsp if (err)
859 67fd6849 2022-02-13 stsp return err;
860 67fd6849 2022-02-13 stsp if (idx == -1)
861 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKIDX);
862 59d1e4a0 2021-03-10 stsp
863 67fd6849 2022-02-13 stsp return got_packidx_get_object_id(base_id, packidx, idx);
864 67fd6849 2022-02-13 stsp }
865 59d1e4a0 2021-03-10 stsp
866 59d1e4a0 2021-03-10 stsp static const struct got_error *
867 67fd6849 2022-02-13 stsp raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
868 67fd6849 2022-02-13 stsp FILE *delta_outfile, struct got_pack *pack,
869 67fd6849 2022-02-13 stsp struct got_packidx *packidx)
870 67fd6849 2022-02-13 stsp {
871 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
872 67fd6849 2022-02-13 stsp struct got_imsg_raw_delta_request req;
873 2d9e6abf 2022-05-04 stsp size_t datalen, delta_size, delta_compressed_size;
874 67fd6849 2022-02-13 stsp off_t delta_offset;
875 67fd6849 2022-02-13 stsp uint8_t *delta_buf = NULL;
876 67fd6849 2022-02-13 stsp struct got_object_id id, base_id;
877 67fd6849 2022-02-13 stsp off_t base_offset, delta_out_offset = 0;
878 67fd6849 2022-02-13 stsp uint64_t base_size = 0, result_size = 0;
879 67fd6849 2022-02-13 stsp size_t w;
880 67fd6849 2022-02-13 stsp
881 67fd6849 2022-02-13 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
882 67fd6849 2022-02-13 stsp if (datalen != sizeof(req))
883 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
884 67fd6849 2022-02-13 stsp memcpy(&req, imsg->data, sizeof(req));
885 67fd6849 2022-02-13 stsp memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
886 67fd6849 2022-02-13 stsp
887 67fd6849 2022-02-13 stsp imsg->fd = -1;
888 67fd6849 2022-02-13 stsp
889 67fd6849 2022-02-13 stsp err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
890 2d9e6abf 2022-05-04 stsp &delta_compressed_size, &delta_offset, &base_offset, &base_id,
891 2d9e6abf 2022-05-04 stsp &base_size, &result_size, pack, packidx, req.idx);
892 67fd6849 2022-02-13 stsp if (err)
893 67fd6849 2022-02-13 stsp goto done;
894 67fd6849 2022-02-13 stsp
895 67fd6849 2022-02-13 stsp /*
896 67fd6849 2022-02-13 stsp * If this is an offset delta we must determine the base
897 67fd6849 2022-02-13 stsp * object ID ourselves.
898 67fd6849 2022-02-13 stsp */
899 67fd6849 2022-02-13 stsp if (base_offset != 0) {
900 67fd6849 2022-02-13 stsp err = get_base_object_id(&base_id, packidx, base_offset);
901 67fd6849 2022-02-13 stsp if (err)
902 67fd6849 2022-02-13 stsp goto done;
903 67fd6849 2022-02-13 stsp }
904 67fd6849 2022-02-13 stsp
905 67fd6849 2022-02-13 stsp delta_out_offset = ftello(delta_outfile);
906 2d9e6abf 2022-05-04 stsp w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
907 2d9e6abf 2022-05-04 stsp if (w != delta_compressed_size) {
908 67fd6849 2022-02-13 stsp err = got_ferror(delta_outfile, GOT_ERR_IO);
909 67fd6849 2022-02-13 stsp goto done;
910 67fd6849 2022-02-13 stsp }
911 67fd6849 2022-02-13 stsp if (fflush(delta_outfile) == -1) {
912 67fd6849 2022-02-13 stsp err = got_error_from_errno("fflush");
913 67fd6849 2022-02-13 stsp goto done;
914 67fd6849 2022-02-13 stsp }
915 67fd6849 2022-02-13 stsp
916 67fd6849 2022-02-13 stsp err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
917 2d9e6abf 2022-05-04 stsp delta_size, delta_compressed_size, delta_offset, delta_out_offset,
918 2d9e6abf 2022-05-04 stsp &base_id);
919 67fd6849 2022-02-13 stsp done:
920 67fd6849 2022-02-13 stsp free(delta_buf);
921 67fd6849 2022-02-13 stsp return err;
922 67fd6849 2022-02-13 stsp }
923 fae7e038 2022-05-07 stsp
924 fae7e038 2022-05-07 stsp struct search_deltas_arg {
925 fae7e038 2022-05-07 stsp struct imsgbuf *ibuf;
926 fae7e038 2022-05-07 stsp struct got_packidx *packidx;
927 fae7e038 2022-05-07 stsp struct got_pack *pack;
928 fae7e038 2022-05-07 stsp struct got_object_idset *idset;
929 fae7e038 2022-05-07 stsp FILE *delta_outfile;
930 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
931 fae7e038 2022-05-07 stsp size_t ndeltas;
932 fae7e038 2022-05-07 stsp };
933 67fd6849 2022-02-13 stsp
934 67fd6849 2022-02-13 stsp static const struct got_error *
935 fae7e038 2022-05-07 stsp search_delta_for_object(struct got_object_id *id, void *data, void *arg)
936 fae7e038 2022-05-07 stsp {
937 fae7e038 2022-05-07 stsp const struct got_error *err;
938 fae7e038 2022-05-07 stsp struct search_deltas_arg *a = arg;
939 fae7e038 2022-05-07 stsp int obj_idx;
940 fae7e038 2022-05-07 stsp uint8_t *delta_buf = NULL;
941 fae7e038 2022-05-07 stsp uint64_t base_size, result_size;
942 fae7e038 2022-05-07 stsp size_t delta_size, delta_compressed_size;
943 fae7e038 2022-05-07 stsp off_t delta_offset, base_offset;
944 fae7e038 2022-05-07 stsp struct got_object_id base_id;
945 fae7e038 2022-05-07 stsp
946 fae7e038 2022-05-07 stsp if (sigint_received)
947 fae7e038 2022-05-07 stsp return got_error(GOT_ERR_CANCELLED);
948 fae7e038 2022-05-07 stsp
949 fae7e038 2022-05-07 stsp obj_idx = got_packidx_get_object_idx(a->packidx, id);
950 fae7e038 2022-05-07 stsp if (obj_idx == -1)
951 fae7e038 2022-05-07 stsp return NULL; /* object not present in our pack file */
952 fae7e038 2022-05-07 stsp
953 fae7e038 2022-05-07 stsp err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
954 fae7e038 2022-05-07 stsp &delta_compressed_size, &delta_offset, &base_offset, &base_id,
955 fae7e038 2022-05-07 stsp &base_size, &result_size, a->pack, a->packidx, obj_idx);
956 fae7e038 2022-05-07 stsp if (err) {
957 fae7e038 2022-05-07 stsp if (err->code == GOT_ERR_OBJ_TYPE)
958 fae7e038 2022-05-07 stsp return NULL; /* object not stored as a delta */
959 fae7e038 2022-05-07 stsp return err;
960 fae7e038 2022-05-07 stsp }
961 fae7e038 2022-05-07 stsp
962 fae7e038 2022-05-07 stsp /*
963 fae7e038 2022-05-07 stsp * If this is an offset delta we must determine the base
964 fae7e038 2022-05-07 stsp * object ID ourselves.
965 fae7e038 2022-05-07 stsp */
966 fae7e038 2022-05-07 stsp if (base_offset != 0) {
967 fae7e038 2022-05-07 stsp err = get_base_object_id(&base_id, a->packidx, base_offset);
968 fae7e038 2022-05-07 stsp if (err)
969 fae7e038 2022-05-07 stsp goto done;
970 fae7e038 2022-05-07 stsp }
971 fae7e038 2022-05-07 stsp
972 fae7e038 2022-05-07 stsp if (got_object_idset_contains(a->idset, &base_id)) {
973 fae7e038 2022-05-07 stsp struct got_imsg_reused_delta *delta;
974 fae7e038 2022-05-07 stsp off_t delta_out_offset = ftello(a->delta_outfile);
975 fae7e038 2022-05-07 stsp size_t w;
976 fae7e038 2022-05-07 stsp
977 fae7e038 2022-05-07 stsp w = fwrite(delta_buf, 1, delta_compressed_size,
978 fae7e038 2022-05-07 stsp a->delta_outfile);
979 fae7e038 2022-05-07 stsp if (w != delta_compressed_size) {
980 fae7e038 2022-05-07 stsp err = got_ferror(a->delta_outfile, GOT_ERR_IO);
981 fae7e038 2022-05-07 stsp goto done;
982 fae7e038 2022-05-07 stsp }
983 fae7e038 2022-05-07 stsp
984 fae7e038 2022-05-07 stsp delta = &a->deltas[a->ndeltas++];
985 fae7e038 2022-05-07 stsp memcpy(&delta->id, id, sizeof(delta->id));
986 fae7e038 2022-05-07 stsp memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
987 fae7e038 2022-05-07 stsp delta->base_size = base_size;
988 fae7e038 2022-05-07 stsp delta->result_size = result_size;
989 fae7e038 2022-05-07 stsp delta->delta_size = delta_size;
990 fae7e038 2022-05-07 stsp delta->delta_compressed_size = delta_compressed_size;
991 fae7e038 2022-05-07 stsp delta->delta_offset = delta_offset;
992 fae7e038 2022-05-07 stsp delta->delta_out_offset = delta_out_offset;
993 fae7e038 2022-05-07 stsp
994 fae7e038 2022-05-07 stsp if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
995 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas(a->ibuf,
996 fae7e038 2022-05-07 stsp a->deltas, a->ndeltas);
997 fae7e038 2022-05-07 stsp if (err)
998 fae7e038 2022-05-07 stsp goto done;
999 fae7e038 2022-05-07 stsp a->ndeltas = 0;
1000 fae7e038 2022-05-07 stsp }
1001 fae7e038 2022-05-07 stsp }
1002 fae7e038 2022-05-07 stsp done:
1003 fae7e038 2022-05-07 stsp free(delta_buf);
1004 fae7e038 2022-05-07 stsp return err;
1005 fae7e038 2022-05-07 stsp }
1006 fae7e038 2022-05-07 stsp
1007 fae7e038 2022-05-07 stsp static const struct got_error *
1008 fae7e038 2022-05-07 stsp recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1009 fae7e038 2022-05-07 stsp {
1010 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
1011 fae7e038 2022-05-07 stsp int done = 0;
1012 fae7e038 2022-05-07 stsp struct got_object_id *ids;
1013 fae7e038 2022-05-07 stsp size_t nids, i;
1014 fae7e038 2022-05-07 stsp
1015 fae7e038 2022-05-07 stsp for (;;) {
1016 fae7e038 2022-05-07 stsp err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1017 fae7e038 2022-05-07 stsp if (err || done)
1018 fae7e038 2022-05-07 stsp break;
1019 fae7e038 2022-05-07 stsp for (i = 0; i < nids; i++) {
1020 fae7e038 2022-05-07 stsp err = got_object_idset_add(idset, &ids[i], NULL);
1021 fae7e038 2022-05-07 stsp if (err) {
1022 fae7e038 2022-05-07 stsp free(ids);
1023 fae7e038 2022-05-07 stsp return err;
1024 fae7e038 2022-05-07 stsp }
1025 fae7e038 2022-05-07 stsp }
1026 fae7e038 2022-05-07 stsp free(ids);
1027 fae7e038 2022-05-07 stsp }
1028 fae7e038 2022-05-07 stsp
1029 fae7e038 2022-05-07 stsp return err;
1030 fae7e038 2022-05-07 stsp }
1031 fae7e038 2022-05-07 stsp
1032 fae7e038 2022-05-07 stsp static const struct got_error *
1033 fae7e038 2022-05-07 stsp delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1034 fae7e038 2022-05-07 stsp FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1035 fae7e038 2022-05-07 stsp {
1036 fae7e038 2022-05-07 stsp const struct got_error *err = NULL;
1037 fae7e038 2022-05-07 stsp struct got_object_idset *idset;
1038 fae7e038 2022-05-07 stsp struct search_deltas_arg sda;
1039 fae7e038 2022-05-07 stsp
1040 fae7e038 2022-05-07 stsp idset = got_object_idset_alloc();
1041 fae7e038 2022-05-07 stsp if (idset == NULL)
1042 fae7e038 2022-05-07 stsp return got_error_from_errno("got_object_idset_alloc");
1043 fae7e038 2022-05-07 stsp
1044 fae7e038 2022-05-07 stsp err = recv_object_ids(idset, ibuf);
1045 fae7e038 2022-05-07 stsp if (err)
1046 fae7e038 2022-05-07 stsp return err;
1047 fae7e038 2022-05-07 stsp
1048 fae7e038 2022-05-07 stsp memset(&sda, 0, sizeof(sda));
1049 fae7e038 2022-05-07 stsp sda.ibuf = ibuf;
1050 fae7e038 2022-05-07 stsp sda.idset = idset;
1051 fae7e038 2022-05-07 stsp sda.pack = pack;
1052 fae7e038 2022-05-07 stsp sda.packidx = packidx;
1053 fae7e038 2022-05-07 stsp sda.delta_outfile = delta_outfile;
1054 fae7e038 2022-05-07 stsp err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1055 fae7e038 2022-05-07 stsp if (err)
1056 fae7e038 2022-05-07 stsp goto done;
1057 fae7e038 2022-05-07 stsp
1058 fae7e038 2022-05-07 stsp if (sda.ndeltas > 0) {
1059 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1060 fae7e038 2022-05-07 stsp sda.ndeltas);
1061 fae7e038 2022-05-07 stsp if (err)
1062 fae7e038 2022-05-07 stsp goto done;
1063 fae7e038 2022-05-07 stsp }
1064 fae7e038 2022-05-07 stsp
1065 fae7e038 2022-05-07 stsp if (fflush(delta_outfile) == -1) {
1066 fae7e038 2022-05-07 stsp err = got_error_from_errno("fflush");
1067 fae7e038 2022-05-07 stsp goto done;
1068 fae7e038 2022-05-07 stsp }
1069 fae7e038 2022-05-07 stsp
1070 fae7e038 2022-05-07 stsp err = got_privsep_send_reused_deltas_done(ibuf);
1071 fae7e038 2022-05-07 stsp done:
1072 fae7e038 2022-05-07 stsp got_object_idset_free(idset);
1073 fae7e038 2022-05-07 stsp return err;
1074 fae7e038 2022-05-07 stsp }
1075 fae7e038 2022-05-07 stsp
1076 fae7e038 2022-05-07 stsp static const struct got_error *
1077 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1078 876c234b 2018-09-10 stsp {
1079 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1080 876c234b 2018-09-10 stsp struct imsg imsg;
1081 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1082 876c234b 2018-09-10 stsp size_t datalen;
1083 876c234b 2018-09-10 stsp struct got_packidx *p;
1084 876c234b 2018-09-10 stsp
1085 876c234b 2018-09-10 stsp *packidx = NULL;
1086 876c234b 2018-09-10 stsp
1087 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1088 876c234b 2018-09-10 stsp if (err)
1089 876c234b 2018-09-10 stsp return err;
1090 876c234b 2018-09-10 stsp
1091 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
1092 876c234b 2018-09-10 stsp if (p == NULL) {
1093 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1094 876c234b 2018-09-10 stsp goto done;
1095 876c234b 2018-09-10 stsp }
1096 876c234b 2018-09-10 stsp
1097 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1098 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1099 876c234b 2018-09-10 stsp goto done;
1100 876c234b 2018-09-10 stsp }
1101 876c234b 2018-09-10 stsp
1102 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1103 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1104 876c234b 2018-09-10 stsp goto done;
1105 876c234b 2018-09-10 stsp }
1106 876c234b 2018-09-10 stsp
1107 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1108 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
1109 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1110 876c234b 2018-09-10 stsp goto done;
1111 876c234b 2018-09-10 stsp }
1112 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1113 876c234b 2018-09-10 stsp
1114 876c234b 2018-09-10 stsp p->len = ipackidx.len;
1115 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
1116 876c234b 2018-09-10 stsp if (p->fd == -1) {
1117 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1118 56bef47a 2018-09-15 stsp goto done;
1119 56bef47a 2018-09-15 stsp }
1120 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
1121 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1122 876c234b 2018-09-10 stsp goto done;
1123 876c234b 2018-09-10 stsp }
1124 876c234b 2018-09-10 stsp
1125 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1126 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1127 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
1128 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
1129 876c234b 2018-09-10 stsp #endif
1130 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1131 876c234b 2018-09-10 stsp done:
1132 876c234b 2018-09-10 stsp if (err) {
1133 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1134 876c234b 2018-09-10 stsp close(imsg.fd);
1135 876c234b 2018-09-10 stsp got_packidx_close(p);
1136 876c234b 2018-09-10 stsp } else
1137 876c234b 2018-09-10 stsp *packidx = p;
1138 876c234b 2018-09-10 stsp imsg_free(&imsg);
1139 876c234b 2018-09-10 stsp return err;
1140 876c234b 2018-09-10 stsp }
1141 876c234b 2018-09-10 stsp
1142 876c234b 2018-09-10 stsp static const struct got_error *
1143 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1144 876c234b 2018-09-10 stsp {
1145 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1146 876c234b 2018-09-10 stsp struct imsg imsg;
1147 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1148 876c234b 2018-09-10 stsp size_t datalen;
1149 876c234b 2018-09-10 stsp struct got_pack *pack;
1150 876c234b 2018-09-10 stsp
1151 876c234b 2018-09-10 stsp *packp = NULL;
1152 876c234b 2018-09-10 stsp
1153 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1154 876c234b 2018-09-10 stsp if (err)
1155 876c234b 2018-09-10 stsp return err;
1156 876c234b 2018-09-10 stsp
1157 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
1158 876c234b 2018-09-10 stsp if (pack == NULL) {
1159 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1160 876c234b 2018-09-10 stsp goto done;
1161 876c234b 2018-09-10 stsp }
1162 876c234b 2018-09-10 stsp
1163 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
1164 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1165 876c234b 2018-09-10 stsp goto done;
1166 876c234b 2018-09-10 stsp }
1167 876c234b 2018-09-10 stsp
1168 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1169 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1170 876c234b 2018-09-10 stsp goto done;
1171 876c234b 2018-09-10 stsp }
1172 876c234b 2018-09-10 stsp
1173 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1174 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
1175 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1176 876c234b 2018-09-10 stsp goto done;
1177 876c234b 2018-09-10 stsp }
1178 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
1179 876c234b 2018-09-10 stsp
1180 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
1181 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
1182 876c234b 2018-09-10 stsp if (pack->fd == -1) {
1183 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1184 876c234b 2018-09-10 stsp goto done;
1185 876c234b 2018-09-10 stsp }
1186 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1187 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1188 56bef47a 2018-09-15 stsp goto done;
1189 56bef47a 2018-09-15 stsp }
1190 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
1191 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
1192 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1193 ab2f42e7 2019-11-10 stsp goto done;
1194 ab2f42e7 2019-11-10 stsp }
1195 ab2f42e7 2019-11-10 stsp
1196 ab2f42e7 2019-11-10 stsp pack->delta_cache = got_delta_cache_alloc(100,
1197 ab2f42e7 2019-11-10 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
1198 ab2f42e7 2019-11-10 stsp if (pack->delta_cache == NULL) {
1199 ab2f42e7 2019-11-10 stsp err = got_error_from_errno("got_delta_cache_alloc");
1200 876c234b 2018-09-10 stsp goto done;
1201 876c234b 2018-09-10 stsp }
1202 876c234b 2018-09-10 stsp
1203 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1204 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1205 876c234b 2018-09-10 stsp pack->fd, 0);
1206 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
1207 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
1208 876c234b 2018-09-10 stsp #endif
1209 876c234b 2018-09-10 stsp done:
1210 876c234b 2018-09-10 stsp if (err) {
1211 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1212 876c234b 2018-09-10 stsp close(imsg.fd);
1213 876c234b 2018-09-10 stsp free(pack);
1214 876c234b 2018-09-10 stsp } else
1215 876c234b 2018-09-10 stsp *packp = pack;
1216 876c234b 2018-09-10 stsp imsg_free(&imsg);
1217 876c234b 2018-09-10 stsp return err;
1218 876c234b 2018-09-10 stsp }
1219 876c234b 2018-09-10 stsp
1220 876c234b 2018-09-10 stsp int
1221 876c234b 2018-09-10 stsp main(int argc, char *argv[])
1222 876c234b 2018-09-10 stsp {
1223 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1224 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
1225 876c234b 2018-09-10 stsp struct imsg imsg;
1226 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
1227 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
1228 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1229 67fd6849 2022-02-13 stsp FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1230 876c234b 2018-09-10 stsp
1231 876c234b 2018-09-10 stsp //static int attached;
1232 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1233 876c234b 2018-09-10 stsp
1234 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1235 99437157 2018-11-11 stsp
1236 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1237 876c234b 2018-09-10 stsp
1238 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1239 c59b3346 2018-09-11 stsp if (err) {
1240 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1241 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1242 c59b3346 2018-09-11 stsp return 1;
1243 c59b3346 2018-09-11 stsp }
1244 c59b3346 2018-09-11 stsp
1245 2ff12563 2018-09-15 stsp #ifndef PROFILE
1246 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1247 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1248 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1249 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1250 876c234b 2018-09-10 stsp return 1;
1251 876c234b 2018-09-10 stsp }
1252 2ff12563 2018-09-15 stsp #endif
1253 876c234b 2018-09-10 stsp
1254 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1255 876c234b 2018-09-10 stsp if (err) {
1256 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1257 876c234b 2018-09-10 stsp return 1;
1258 876c234b 2018-09-10 stsp }
1259 876c234b 2018-09-10 stsp
1260 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1261 876c234b 2018-09-10 stsp if (err) {
1262 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1263 876c234b 2018-09-10 stsp return 1;
1264 876c234b 2018-09-10 stsp }
1265 876c234b 2018-09-10 stsp
1266 656b1f76 2019-05-11 jcs for (;;) {
1267 876c234b 2018-09-10 stsp imsg.fd = -1;
1268 99437157 2018-11-11 stsp
1269 99437157 2018-11-11 stsp if (sigint_received) {
1270 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1271 99437157 2018-11-11 stsp break;
1272 99437157 2018-11-11 stsp }
1273 876c234b 2018-09-10 stsp
1274 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1275 876c234b 2018-09-10 stsp if (err) {
1276 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1277 876c234b 2018-09-10 stsp err = NULL;
1278 876c234b 2018-09-10 stsp break;
1279 876c234b 2018-09-10 stsp }
1280 876c234b 2018-09-10 stsp
1281 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1282 876c234b 2018-09-10 stsp break;
1283 876c234b 2018-09-10 stsp
1284 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1285 db696021 2022-01-04 stsp case GOT_IMSG_TMPFD:
1286 67fd6849 2022-02-13 stsp if (basefile == NULL) {
1287 67fd6849 2022-02-13 stsp err = receive_tempfile(&basefile, "w+",
1288 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1289 67fd6849 2022-02-13 stsp } else if (accumfile == NULL) {
1290 67fd6849 2022-02-13 stsp err = receive_tempfile(&accumfile, "w+",
1291 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1292 67fd6849 2022-02-13 stsp } else
1293 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1294 db696021 2022-01-04 stsp break;
1295 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
1296 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
1297 c59b3346 2018-09-11 stsp &objcache);
1298 876c234b 2018-09-10 stsp break;
1299 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1300 db696021 2022-01-04 stsp if (basefile == NULL || accumfile == NULL) {
1301 db696021 2022-01-04 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1302 db696021 2022-01-04 stsp break;
1303 db696021 2022-01-04 stsp }
1304 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
1305 db696021 2022-01-04 stsp &objcache, basefile, accumfile);
1306 59d1e4a0 2021-03-10 stsp break;
1307 67fd6849 2022-02-13 stsp case GOT_IMSG_RAW_DELTA_OUTFD:
1308 67fd6849 2022-02-13 stsp if (delta_outfile != NULL) {
1309 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1310 67fd6849 2022-02-13 stsp break;
1311 67fd6849 2022-02-13 stsp }
1312 67fd6849 2022-02-13 stsp err = receive_tempfile(&delta_outfile, "w",
1313 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1314 67fd6849 2022-02-13 stsp break;
1315 67fd6849 2022-02-13 stsp case GOT_IMSG_RAW_DELTA_REQUEST:
1316 67fd6849 2022-02-13 stsp if (delta_outfile == NULL) {
1317 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1318 67fd6849 2022-02-13 stsp break;
1319 67fd6849 2022-02-13 stsp }
1320 67fd6849 2022-02-13 stsp err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1321 67fd6849 2022-02-13 stsp pack, packidx);
1322 67fd6849 2022-02-13 stsp break;
1323 fae7e038 2022-05-07 stsp case GOT_IMSG_DELTA_REUSE_REQUEST:
1324 fae7e038 2022-05-07 stsp if (delta_outfile == NULL) {
1325 fae7e038 2022-05-07 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1326 fae7e038 2022-05-07 stsp break;
1327 fae7e038 2022-05-07 stsp }
1328 fae7e038 2022-05-07 stsp err = delta_reuse_request(&imsg, &ibuf,
1329 fae7e038 2022-05-07 stsp delta_outfile, pack, packidx);
1330 fae7e038 2022-05-07 stsp break;
1331 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
1332 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
1333 7762fe12 2018-11-05 stsp &objcache);
1334 7762fe12 2018-11-05 stsp break;
1335 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
1336 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
1337 62d463ca 2020-10-20 naddy &objcache);
1338 876c234b 2018-09-10 stsp break;
1339 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
1340 db696021 2022-01-04 stsp if (basefile == NULL || accumfile == NULL) {
1341 db696021 2022-01-04 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1342 db696021 2022-01-04 stsp break;
1343 db696021 2022-01-04 stsp }
1344 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
1345 db696021 2022-01-04 stsp &objcache, basefile, accumfile);
1346 876c234b 2018-09-10 stsp break;
1347 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
1348 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
1349 62d463ca 2020-10-20 naddy &objcache);
1350 f4a881ce 2018-11-17 stsp break;
1351 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1352 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
1353 ca6e02ac 2020-01-07 stsp packidx, &objcache);
1354 ca6e02ac 2020-01-07 stsp break;
1355 876c234b 2018-09-10 stsp default:
1356 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1357 876c234b 2018-09-10 stsp break;
1358 876c234b 2018-09-10 stsp }
1359 876c234b 2018-09-10 stsp
1360 08578a35 2021-01-22 stsp if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1361 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1362 876c234b 2018-09-10 stsp imsg_free(&imsg);
1363 99437157 2018-11-11 stsp if (err)
1364 876c234b 2018-09-10 stsp break;
1365 876c234b 2018-09-10 stsp }
1366 876c234b 2018-09-10 stsp
1367 c59b3346 2018-09-11 stsp if (packidx)
1368 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
1369 c59b3346 2018-09-11 stsp if (pack)
1370 c59b3346 2018-09-11 stsp got_pack_close(pack);
1371 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
1372 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
1373 db696021 2022-01-04 stsp if (basefile && fclose(basefile) == EOF && err == NULL)
1374 db696021 2022-01-04 stsp err = got_error_from_errno("fclose");
1375 db696021 2022-01-04 stsp if (accumfile && fclose(accumfile) == EOF && err == NULL)
1376 db696021 2022-01-04 stsp err = got_error_from_errno("fclose");
1377 67fd6849 2022-02-13 stsp if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1378 67fd6849 2022-02-13 stsp err = got_error_from_errno("fclose");
1379 99437157 2018-11-11 stsp if (err) {
1380 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1381 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1382 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
1383 80d5f134 2018-11-11 stsp }
1384 99437157 2018-11-11 stsp }
1385 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1386 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1387 876c234b 2018-09-10 stsp return err ? 1 : 0;
1388 876c234b 2018-09-10 stsp }