Blame


1 876c234b 2018-09-10 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 876c234b 2018-09-10 stsp *
4 876c234b 2018-09-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 876c234b 2018-09-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 876c234b 2018-09-10 stsp * copyright notice and this permission notice appear in all copies.
7 876c234b 2018-09-10 stsp *
8 876c234b 2018-09-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 876c234b 2018-09-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 876c234b 2018-09-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 876c234b 2018-09-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 876c234b 2018-09-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 876c234b 2018-09-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 876c234b 2018-09-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 876c234b 2018-09-10 stsp */
16 876c234b 2018-09-10 stsp
17 63915ee5 2022-06-23 thomas #include <sys/stat.h>
18 876c234b 2018-09-10 stsp #include <sys/types.h>
19 876c234b 2018-09-10 stsp #include <sys/uio.h>
20 876c234b 2018-09-10 stsp #include <sys/time.h>
21 876c234b 2018-09-10 stsp #include <sys/mman.h>
22 876c234b 2018-09-10 stsp
23 876c234b 2018-09-10 stsp #include <limits.h>
24 99437157 2018-11-11 stsp #include <signal.h>
25 876c234b 2018-09-10 stsp #include <stdint.h>
26 876c234b 2018-09-10 stsp #include <stdio.h>
27 876c234b 2018-09-10 stsp #include <stdlib.h>
28 876c234b 2018-09-10 stsp #include <string.h>
29 81a12da5 2020-09-09 naddy #include <unistd.h>
30 876c234b 2018-09-10 stsp #include <zlib.h>
31 dd038bc6 2021-09-21 thomas.ad
32 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
33 876c234b 2018-09-10 stsp
34 876c234b 2018-09-10 stsp #include "got_error.h"
35 876c234b 2018-09-10 stsp #include "got_object.h"
36 3022d272 2019-11-14 stsp #include "got_path.h"
37 876c234b 2018-09-10 stsp
38 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
39 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
40 876c234b 2018-09-10 stsp #include "got_lib_object.h"
41 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
42 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
43 7d0d4920 2022-05-12 thomas #include "got_lib_object_idset.h"
44 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
45 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
46 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 8f1c06eb 2021-09-25 thomas.ad 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 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
180 78e7b7b8 2022-05-19 thomas open_tree(uint8_t **buf, struct got_parsed_tree_entry **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 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry *entries = NULL;
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 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 78e7b7b8 2022-05-19 thomas err = got_privsep_send_tree(ibuf, entries, nentries);
241 78e7b7b8 2022-05-19 thomas 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 f9c2e8e5 2022-02-13 thomas receive_tempfile(FILE **f, const char *mode, 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
296 bc1f382f 2022-01-05 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
297 bc1f382f 2022-01-05 thomas if (datalen != 0)
298 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
299 bc1f382f 2022-01-05 thomas
300 bc1f382f 2022-01-05 thomas if (imsg->fd == -1)
301 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
302 bc1f382f 2022-01-05 thomas
303 f9c2e8e5 2022-02-13 thomas *f = fdopen(imsg->fd, mode);
304 bc1f382f 2022-01-05 thomas if (*f == NULL)
305 bc1f382f 2022-01-05 thomas return got_error_from_errno("fdopen");
306 bc1f382f 2022-01-05 thomas imsg->fd = -1;
307 bc1f382f 2022-01-05 thomas
308 bc1f382f 2022-01-05 thomas return NULL;
309 3840f4c9 2018-09-12 stsp }
310 55da3778 2018-09-10 stsp
311 3840f4c9 2018-09-12 stsp static const struct got_error *
312 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
313 bc1f382f 2022-01-05 thomas struct got_packidx *packidx, struct got_object_cache *objcache,
314 bc1f382f 2022-01-05 thomas FILE *basefile, FILE *accumfile)
315 3840f4c9 2018-09-12 stsp {
316 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
317 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
318 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
319 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
320 ebc55e2d 2018-12-24 stsp struct got_object_id id;
321 ebc55e2d 2018-12-24 stsp size_t datalen;
322 ac544f8c 2019-01-13 stsp uint64_t blob_size;
323 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
324 3840f4c9 2018-09-12 stsp
325 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
326 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
327 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
328 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
329 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
330 ebc55e2d 2018-12-24 stsp
331 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
332 704b89c4 2019-05-23 stsp if (obj) {
333 704b89c4 2019-05-23 stsp obj->refcnt++;
334 704b89c4 2019-05-23 stsp } else {
335 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
336 704b89c4 2019-05-23 stsp objcache);
337 704b89c4 2019-05-23 stsp if (err)
338 704b89c4 2019-05-23 stsp return err;
339 704b89c4 2019-05-23 stsp }
340 3840f4c9 2018-09-12 stsp
341 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
342 3840f4c9 2018-09-12 stsp if (err)
343 ac544f8c 2019-01-13 stsp goto done;
344 3840f4c9 2018-09-12 stsp
345 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
346 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
347 ac544f8c 2019-01-13 stsp if (err)
348 ac544f8c 2019-01-13 stsp goto done;
349 ac544f8c 2019-01-13 stsp } else
350 ac544f8c 2019-01-13 stsp blob_size = obj->size;
351 ac544f8c 2019-01-13 stsp
352 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
353 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
354 ac544f8c 2019-01-13 stsp obj, pack);
355 ac544f8c 2019-01-13 stsp else
356 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
357 ac544f8c 2019-01-13 stsp accumfile);
358 3840f4c9 2018-09-12 stsp if (err)
359 55da3778 2018-09-10 stsp goto done;
360 55da3778 2018-09-10 stsp
361 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
362 55da3778 2018-09-10 stsp done:
363 ac544f8c 2019-01-13 stsp free(buf);
364 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
365 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
366 cb5e38fd 2019-05-23 stsp got_object_close(obj);
367 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
368 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
369 55da3778 2018-09-10 stsp
370 55da3778 2018-09-10 stsp return err;
371 876c234b 2018-09-10 stsp }
372 876c234b 2018-09-10 stsp
373 876c234b 2018-09-10 stsp static const struct got_error *
374 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
375 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
376 f4a881ce 2018-11-17 stsp {
377 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
378 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
379 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
380 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
381 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
382 f4a881ce 2018-11-17 stsp size_t len;
383 268f7291 2018-12-24 stsp struct got_object_id id;
384 268f7291 2018-12-24 stsp size_t datalen;
385 f4a881ce 2018-11-17 stsp
386 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
387 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
388 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
389 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
390 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
391 268f7291 2018-12-24 stsp
392 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
393 704b89c4 2019-05-23 stsp if (obj) {
394 704b89c4 2019-05-23 stsp obj->refcnt++;
395 704b89c4 2019-05-23 stsp } else {
396 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
397 704b89c4 2019-05-23 stsp objcache);
398 704b89c4 2019-05-23 stsp if (err)
399 704b89c4 2019-05-23 stsp return err;
400 704b89c4 2019-05-23 stsp }
401 f4a881ce 2018-11-17 stsp
402 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
403 f4a881ce 2018-11-17 stsp if (err)
404 cb5e38fd 2019-05-23 stsp goto done;
405 f4a881ce 2018-11-17 stsp
406 f4a881ce 2018-11-17 stsp obj->size = len;
407 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
408 0ae4af15 2019-02-01 stsp if (err)
409 cb5e38fd 2019-05-23 stsp goto done;
410 f4a881ce 2018-11-17 stsp
411 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
412 cb5e38fd 2019-05-23 stsp done:
413 cb5e38fd 2019-05-23 stsp free(buf);
414 cb5e38fd 2019-05-23 stsp got_object_close(obj);
415 cb5e38fd 2019-05-23 stsp if (tag)
416 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
417 ca6e02ac 2020-01-07 stsp if (err) {
418 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
419 ca6e02ac 2020-01-07 stsp err = NULL;
420 ca6e02ac 2020-01-07 stsp else
421 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
422 ca6e02ac 2020-01-07 stsp }
423 ca6e02ac 2020-01-07 stsp
424 ca6e02ac 2020-01-07 stsp return err;
425 ca6e02ac 2020-01-07 stsp }
426 ca6e02ac 2020-01-07 stsp
427 ca6e02ac 2020-01-07 stsp static struct got_parsed_tree_entry *
428 78e7b7b8 2022-05-19 thomas find_entry_by_name(struct got_parsed_tree_entry *entries, int nentries,
429 ca6e02ac 2020-01-07 stsp const char *name, size_t len)
430 ca6e02ac 2020-01-07 stsp {
431 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry *pte;
432 78e7b7b8 2022-05-19 thomas int cmp, i;
433 ca6e02ac 2020-01-07 stsp
434 ca6e02ac 2020-01-07 stsp /* Note that tree entries are sorted in strncmp() order. */
435 78e7b7b8 2022-05-19 thomas for (i = 0; i < nentries; i++) {
436 78e7b7b8 2022-05-19 thomas pte = &entries[i];
437 78e7b7b8 2022-05-19 thomas cmp = strncmp(pte->name, 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 78e7b7b8 2022-05-19 thomas if (pte->name[len] == '\0')
443 78e7b7b8 2022-05-19 thomas return pte;
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 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
449 ca6e02ac 2020-01-07 stsp tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
450 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry **entries1, int *nentries1,
451 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry **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 78e7b7b8 2022-05-19 thomas 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 78e7b7b8 2022-05-19 thomas 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 78e7b7b8 2022-05-19 thomas 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 78e7b7b8 2022-05-19 thomas 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 struct ibuf *wbuf;
555 01bb5a15 2021-09-25 thomas.ad size_t i;
556 e70bf110 2020-03-22 stsp
557 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
558 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
559 e70bf110 2020-03-22 stsp ncommits * SHA1_DIGEST_LENGTH);
560 e70bf110 2020-03-22 stsp if (wbuf == NULL)
561 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
562 e70bf110 2020-03-22 stsp
563 e9f1a409 2022-05-19 thomas if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1)
564 e9f1a409 2022-05-19 thomas return got_error_from_errno("imsg_add TRAVERSED_COMMITS");
565 e9f1a409 2022-05-19 thomas
566 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
567 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
568 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
569 e9f1a409 2022-05-19 thomas return got_error_from_errno(
570 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
571 e70bf110 2020-03-22 stsp }
572 e70bf110 2020-03-22 stsp }
573 e70bf110 2020-03-22 stsp
574 e70bf110 2020-03-22 stsp wbuf->fd = -1;
575 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
576 e70bf110 2020-03-22 stsp
577 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
578 e70bf110 2020-03-22 stsp }
579 e70bf110 2020-03-22 stsp
580 e70bf110 2020-03-22 stsp static const struct got_error *
581 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
582 e70bf110 2020-03-22 stsp {
583 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
584 e70bf110 2020-03-22 stsp NULL, 0) == -1)
585 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
586 e70bf110 2020-03-22 stsp
587 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
588 e70bf110 2020-03-22 stsp }
589 3dfecf3e 2022-06-13 thomas
590 e70bf110 2020-03-22 stsp static const struct got_error *
591 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
592 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
593 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
594 ca6e02ac 2020-01-07 stsp {
595 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
596 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
597 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
598 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
599 78e7b7b8 2022-05-19 thomas struct got_parsed_tree_entry *entries = NULL, *pentries = NULL;
600 ca6e02ac 2020-01-07 stsp int nentries = 0, pnentries = 0;
601 ca6e02ac 2020-01-07 stsp struct got_object_id id;
602 ca6e02ac 2020-01-07 stsp size_t datalen, path_len;
603 ca6e02ac 2020-01-07 stsp char *path = NULL;
604 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
605 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
606 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
607 ca6e02ac 2020-01-07 stsp
608 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
609 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(iobj))
610 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
611 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
612 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
613 ca6e02ac 2020-01-07 stsp
614 ca6e02ac 2020-01-07 stsp path_len = datalen - sizeof(iobj) - 1;
615 ca6e02ac 2020-01-07 stsp if (path_len < 0)
616 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
617 ca6e02ac 2020-01-07 stsp if (path_len > 0) {
618 ca6e02ac 2020-01-07 stsp path = imsg->data + sizeof(iobj);
619 ca6e02ac 2020-01-07 stsp if (path[path_len] != '\0')
620 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
621 ca6e02ac 2020-01-07 stsp }
622 ca6e02ac 2020-01-07 stsp
623 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
624 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
625 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
626 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
627 ca6e02ac 2020-01-07 stsp
628 ca6e02ac 2020-01-07 stsp do {
629 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
630 ca6e02ac 2020-01-07 stsp int idx;
631 ca6e02ac 2020-01-07 stsp
632 ca6e02ac 2020-01-07 stsp if (sigint_received) {
633 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
634 ca6e02ac 2020-01-07 stsp goto done;
635 ca6e02ac 2020-01-07 stsp }
636 ca6e02ac 2020-01-07 stsp
637 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
638 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
639 ca6e02ac 2020-01-07 stsp if (idx == -1)
640 ca6e02ac 2020-01-07 stsp break;
641 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
642 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
643 ca6e02ac 2020-01-07 stsp if (err) {
644 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
645 ca6e02ac 2020-01-07 stsp goto done;
646 ca6e02ac 2020-01-07 stsp err = NULL;
647 ca6e02ac 2020-01-07 stsp break;
648 ca6e02ac 2020-01-07 stsp }
649 ca6e02ac 2020-01-07 stsp }
650 ca6e02ac 2020-01-07 stsp
651 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
652 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
653 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
654 e70bf110 2020-03-22 stsp ibuf);
655 ca6e02ac 2020-01-07 stsp if (err)
656 ca6e02ac 2020-01-07 stsp goto done;
657 ca6e02ac 2020-01-07 stsp ncommits = 0;
658 ca6e02ac 2020-01-07 stsp }
659 ca6e02ac 2020-01-07 stsp ncommits++;
660 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
661 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
662 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
663 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
664 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
665 ca6e02ac 2020-01-07 stsp if (new == NULL) {
666 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
667 ca6e02ac 2020-01-07 stsp goto done;
668 ca6e02ac 2020-01-07 stsp }
669 ca6e02ac 2020-01-07 stsp commit_ids = new;
670 ca6e02ac 2020-01-07 stsp }
671 ca6e02ac 2020-01-07 stsp memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
672 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
673 ca6e02ac 2020-01-07 stsp
674 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
675 ca6e02ac 2020-01-07 stsp if (pid == NULL)
676 ca6e02ac 2020-01-07 stsp break;
677 ca6e02ac 2020-01-07 stsp
678 ec242592 2022-04-22 thomas idx = got_packidx_get_object_idx(packidx, &pid->id);
679 ca6e02ac 2020-01-07 stsp if (idx == -1)
680 ca6e02ac 2020-01-07 stsp break;
681 ca6e02ac 2020-01-07 stsp
682 ec242592 2022-04-22 thomas err = open_commit(&pcommit, pack, packidx, idx, &pid->id,
683 ca6e02ac 2020-01-07 stsp objcache);
684 ca6e02ac 2020-01-07 stsp if (err) {
685 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
686 ca6e02ac 2020-01-07 stsp goto done;
687 ca6e02ac 2020-01-07 stsp err = NULL;
688 ca6e02ac 2020-01-07 stsp break;
689 ca6e02ac 2020-01-07 stsp }
690 ca6e02ac 2020-01-07 stsp
691 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
692 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
693 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
694 ca6e02ac 2020-01-07 stsp changed = 1;
695 ca6e02ac 2020-01-07 stsp break;
696 ca6e02ac 2020-01-07 stsp }
697 ca6e02ac 2020-01-07 stsp } else {
698 ca6e02ac 2020-01-07 stsp int pidx;
699 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
700 ca6e02ac 2020-01-07 stsp
701 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
702 ca6e02ac 2020-01-07 stsp commit->tree_id);
703 ca6e02ac 2020-01-07 stsp if (idx == -1)
704 ca6e02ac 2020-01-07 stsp break;
705 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
706 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
707 ca6e02ac 2020-01-07 stsp if (pidx == -1)
708 ca6e02ac 2020-01-07 stsp break;
709 ca6e02ac 2020-01-07 stsp
710 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack,
711 ca6e02ac 2020-01-07 stsp packidx, idx, commit->tree_id, objcache);
712 ca6e02ac 2020-01-07 stsp if (err)
713 ca6e02ac 2020-01-07 stsp goto done;
714 ca6e02ac 2020-01-07 stsp err = open_tree(&pbuf, &pentries, &pnentries, pack,
715 ca6e02ac 2020-01-07 stsp packidx, pidx, pcommit->tree_id, objcache);
716 ca6e02ac 2020-01-07 stsp if (err) {
717 ca6e02ac 2020-01-07 stsp free(buf);
718 ca6e02ac 2020-01-07 stsp goto done;
719 ca6e02ac 2020-01-07 stsp }
720 ca6e02ac 2020-01-07 stsp
721 ca6e02ac 2020-01-07 stsp err = tree_path_changed(&changed, &buf, &pbuf,
722 ca6e02ac 2020-01-07 stsp &entries, &nentries, &pentries, &pnentries, path,
723 ca6e02ac 2020-01-07 stsp pack, packidx, ibuf, objcache);
724 ca6e02ac 2020-01-07 stsp
725 78e7b7b8 2022-05-19 thomas free(entries);
726 78e7b7b8 2022-05-19 thomas entries = NULL;
727 ca6e02ac 2020-01-07 stsp nentries = 0;
728 ca6e02ac 2020-01-07 stsp free(buf);
729 78e7b7b8 2022-05-19 thomas free(pentries);
730 78e7b7b8 2022-05-19 thomas pentries = NULL;
731 ca6e02ac 2020-01-07 stsp pnentries = 0;
732 ca6e02ac 2020-01-07 stsp free(pbuf);
733 ca6e02ac 2020-01-07 stsp if (err) {
734 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
735 ca6e02ac 2020-01-07 stsp goto done;
736 ca6e02ac 2020-01-07 stsp err = NULL;
737 ca6e02ac 2020-01-07 stsp break;
738 ca6e02ac 2020-01-07 stsp }
739 ca6e02ac 2020-01-07 stsp }
740 ca6e02ac 2020-01-07 stsp
741 ca6e02ac 2020-01-07 stsp if (!changed) {
742 ec242592 2022-04-22 thomas memcpy(id.sha1, pid->id.sha1, SHA1_DIGEST_LENGTH);
743 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
744 ca6e02ac 2020-01-07 stsp commit = pcommit;
745 ca6e02ac 2020-01-07 stsp pcommit = NULL;
746 ca6e02ac 2020-01-07 stsp }
747 ca6e02ac 2020-01-07 stsp } while (!changed);
748 ca6e02ac 2020-01-07 stsp
749 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
750 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
751 ca6e02ac 2020-01-07 stsp if (err)
752 ca6e02ac 2020-01-07 stsp goto done;
753 ca6e02ac 2020-01-07 stsp
754 ca6e02ac 2020-01-07 stsp if (changed) {
755 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
756 ca6e02ac 2020-01-07 stsp if (err)
757 ca6e02ac 2020-01-07 stsp goto done;
758 ca6e02ac 2020-01-07 stsp }
759 ca6e02ac 2020-01-07 stsp }
760 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
761 ca6e02ac 2020-01-07 stsp done:
762 ca6e02ac 2020-01-07 stsp free(commit_ids);
763 ca6e02ac 2020-01-07 stsp if (commit)
764 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
765 ca6e02ac 2020-01-07 stsp if (pcommit)
766 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
767 78e7b7b8 2022-05-19 thomas free(entries);
768 78e7b7b8 2022-05-19 thomas free(pentries);
769 f4a881ce 2018-11-17 stsp if (err) {
770 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
771 f4a881ce 2018-11-17 stsp err = NULL;
772 f4a881ce 2018-11-17 stsp else
773 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
774 f4a881ce 2018-11-17 stsp }
775 f4a881ce 2018-11-17 stsp
776 f4a881ce 2018-11-17 stsp return err;
777 f4a881ce 2018-11-17 stsp }
778 f4a881ce 2018-11-17 stsp
779 f4a881ce 2018-11-17 stsp static const struct got_error *
780 48b4f239 2021-12-31 thomas raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
781 48b4f239 2021-12-31 thomas struct got_pack *pack, struct got_packidx *packidx,
782 bc1f382f 2022-01-05 thomas struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
783 59d1e4a0 2021-03-10 stsp {
784 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
785 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
786 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
787 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
788 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
789 59d1e4a0 2021-03-10 stsp struct got_object *obj;
790 59d1e4a0 2021-03-10 stsp struct got_object_id id;
791 59d1e4a0 2021-03-10 stsp size_t datalen;
792 59d1e4a0 2021-03-10 stsp
793 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
794 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
795 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
796 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
797 59d1e4a0 2021-03-10 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
798 59d1e4a0 2021-03-10 stsp
799 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
800 59d1e4a0 2021-03-10 stsp if (obj) {
801 59d1e4a0 2021-03-10 stsp obj->refcnt++;
802 59d1e4a0 2021-03-10 stsp } else {
803 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
804 59d1e4a0 2021-03-10 stsp objcache);
805 59d1e4a0 2021-03-10 stsp if (err)
806 59d1e4a0 2021-03-10 stsp return err;
807 59d1e4a0 2021-03-10 stsp }
808 59d1e4a0 2021-03-10 stsp
809 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
810 59d1e4a0 2021-03-10 stsp if (err)
811 59d1e4a0 2021-03-10 stsp return err;
812 59d1e4a0 2021-03-10 stsp
813 59d1e4a0 2021-03-10 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
814 59d1e4a0 2021-03-10 stsp err = got_pack_get_max_delta_object_size(&size, obj, pack);
815 59d1e4a0 2021-03-10 stsp if (err)
816 59d1e4a0 2021-03-10 stsp goto done;
817 59d1e4a0 2021-03-10 stsp } else
818 59d1e4a0 2021-03-10 stsp size = obj->size;
819 59d1e4a0 2021-03-10 stsp
820 59d1e4a0 2021-03-10 stsp if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
821 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
822 59d1e4a0 2021-03-10 stsp obj, pack);
823 59d1e4a0 2021-03-10 stsp else
824 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
825 59d1e4a0 2021-03-10 stsp accumfile);
826 59d1e4a0 2021-03-10 stsp if (err)
827 59d1e4a0 2021-03-10 stsp goto done;
828 59d1e4a0 2021-03-10 stsp
829 40e3cb72 2021-06-22 stsp err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
830 59d1e4a0 2021-03-10 stsp done:
831 59d1e4a0 2021-03-10 stsp free(buf);
832 59d1e4a0 2021-03-10 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
833 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
834 59d1e4a0 2021-03-10 stsp got_object_close(obj);
835 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
836 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
837 59d1e4a0 2021-03-10 stsp
838 59d1e4a0 2021-03-10 stsp return err;
839 59d1e4a0 2021-03-10 stsp }
840 f9c2e8e5 2022-02-13 thomas
841 f9c2e8e5 2022-02-13 thomas static const struct got_error *
842 f9c2e8e5 2022-02-13 thomas get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
843 f9c2e8e5 2022-02-13 thomas off_t base_offset)
844 f9c2e8e5 2022-02-13 thomas {
845 f9c2e8e5 2022-02-13 thomas const struct got_error *err;
846 f9c2e8e5 2022-02-13 thomas int idx;
847 59d1e4a0 2021-03-10 stsp
848 f9c2e8e5 2022-02-13 thomas err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
849 f9c2e8e5 2022-02-13 thomas if (err)
850 f9c2e8e5 2022-02-13 thomas return err;
851 f9c2e8e5 2022-02-13 thomas if (idx == -1)
852 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_BAD_PACKIDX);
853 59d1e4a0 2021-03-10 stsp
854 f9c2e8e5 2022-02-13 thomas return got_packidx_get_object_id(base_id, packidx, idx);
855 f9c2e8e5 2022-02-13 thomas }
856 59d1e4a0 2021-03-10 stsp
857 59d1e4a0 2021-03-10 stsp static const struct got_error *
858 f9c2e8e5 2022-02-13 thomas raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
859 f9c2e8e5 2022-02-13 thomas FILE *delta_outfile, struct got_pack *pack,
860 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx)
861 f9c2e8e5 2022-02-13 thomas {
862 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
863 f9c2e8e5 2022-02-13 thomas struct got_imsg_raw_delta_request req;
864 9249e7e3 2022-05-12 thomas size_t datalen, delta_size, delta_compressed_size;
865 f9c2e8e5 2022-02-13 thomas off_t delta_offset;
866 f9c2e8e5 2022-02-13 thomas uint8_t *delta_buf = NULL;
867 f9c2e8e5 2022-02-13 thomas struct got_object_id id, base_id;
868 f9c2e8e5 2022-02-13 thomas off_t base_offset, delta_out_offset = 0;
869 f9c2e8e5 2022-02-13 thomas uint64_t base_size = 0, result_size = 0;
870 f9c2e8e5 2022-02-13 thomas size_t w;
871 f9c2e8e5 2022-02-13 thomas
872 f9c2e8e5 2022-02-13 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
873 f9c2e8e5 2022-02-13 thomas if (datalen != sizeof(req))
874 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
875 f9c2e8e5 2022-02-13 thomas memcpy(&req, imsg->data, sizeof(req));
876 f9c2e8e5 2022-02-13 thomas memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
877 f9c2e8e5 2022-02-13 thomas
878 f9c2e8e5 2022-02-13 thomas imsg->fd = -1;
879 f9c2e8e5 2022-02-13 thomas
880 f9c2e8e5 2022-02-13 thomas err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
881 9249e7e3 2022-05-12 thomas &delta_compressed_size, &delta_offset, &base_offset, &base_id,
882 9249e7e3 2022-05-12 thomas &base_size, &result_size, pack, packidx, req.idx);
883 f9c2e8e5 2022-02-13 thomas if (err)
884 f9c2e8e5 2022-02-13 thomas goto done;
885 f9c2e8e5 2022-02-13 thomas
886 f9c2e8e5 2022-02-13 thomas /*
887 f9c2e8e5 2022-02-13 thomas * If this is an offset delta we must determine the base
888 f9c2e8e5 2022-02-13 thomas * object ID ourselves.
889 f9c2e8e5 2022-02-13 thomas */
890 f9c2e8e5 2022-02-13 thomas if (base_offset != 0) {
891 f9c2e8e5 2022-02-13 thomas err = get_base_object_id(&base_id, packidx, base_offset);
892 f9c2e8e5 2022-02-13 thomas if (err)
893 f9c2e8e5 2022-02-13 thomas goto done;
894 f9c2e8e5 2022-02-13 thomas }
895 f9c2e8e5 2022-02-13 thomas
896 f9c2e8e5 2022-02-13 thomas delta_out_offset = ftello(delta_outfile);
897 9249e7e3 2022-05-12 thomas w = fwrite(delta_buf, 1, delta_compressed_size, delta_outfile);
898 9249e7e3 2022-05-12 thomas if (w != delta_compressed_size) {
899 f9c2e8e5 2022-02-13 thomas err = got_ferror(delta_outfile, GOT_ERR_IO);
900 f9c2e8e5 2022-02-13 thomas goto done;
901 f9c2e8e5 2022-02-13 thomas }
902 f9c2e8e5 2022-02-13 thomas if (fflush(delta_outfile) == -1) {
903 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fflush");
904 f9c2e8e5 2022-02-13 thomas goto done;
905 f9c2e8e5 2022-02-13 thomas }
906 f9c2e8e5 2022-02-13 thomas
907 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
908 9249e7e3 2022-05-12 thomas delta_size, delta_compressed_size, delta_offset, delta_out_offset,
909 9249e7e3 2022-05-12 thomas &base_id);
910 f9c2e8e5 2022-02-13 thomas done:
911 f9c2e8e5 2022-02-13 thomas free(delta_buf);
912 f9c2e8e5 2022-02-13 thomas return err;
913 f9c2e8e5 2022-02-13 thomas }
914 7d0d4920 2022-05-12 thomas
915 7d0d4920 2022-05-12 thomas struct search_deltas_arg {
916 7d0d4920 2022-05-12 thomas struct imsgbuf *ibuf;
917 7d0d4920 2022-05-12 thomas struct got_packidx *packidx;
918 7d0d4920 2022-05-12 thomas struct got_pack *pack;
919 7d0d4920 2022-05-12 thomas struct got_object_idset *idset;
920 7d0d4920 2022-05-12 thomas FILE *delta_outfile;
921 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta deltas[GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS];
922 7d0d4920 2022-05-12 thomas size_t ndeltas;
923 7d0d4920 2022-05-12 thomas };
924 f9c2e8e5 2022-02-13 thomas
925 f9c2e8e5 2022-02-13 thomas static const struct got_error *
926 7d0d4920 2022-05-12 thomas search_delta_for_object(struct got_object_id *id, void *data, void *arg)
927 7d0d4920 2022-05-12 thomas {
928 7d0d4920 2022-05-12 thomas const struct got_error *err;
929 7d0d4920 2022-05-12 thomas struct search_deltas_arg *a = arg;
930 7d0d4920 2022-05-12 thomas int obj_idx;
931 7d0d4920 2022-05-12 thomas uint8_t *delta_buf = NULL;
932 7d0d4920 2022-05-12 thomas uint64_t base_size, result_size;
933 7d0d4920 2022-05-12 thomas size_t delta_size, delta_compressed_size;
934 7d0d4920 2022-05-12 thomas off_t delta_offset, base_offset;
935 7d0d4920 2022-05-12 thomas struct got_object_id base_id;
936 7d0d4920 2022-05-12 thomas
937 7d0d4920 2022-05-12 thomas if (sigint_received)
938 7d0d4920 2022-05-12 thomas return got_error(GOT_ERR_CANCELLED);
939 7d0d4920 2022-05-12 thomas
940 7d0d4920 2022-05-12 thomas obj_idx = got_packidx_get_object_idx(a->packidx, id);
941 7d0d4920 2022-05-12 thomas if (obj_idx == -1)
942 7d0d4920 2022-05-12 thomas return NULL; /* object not present in our pack file */
943 7d0d4920 2022-05-12 thomas
944 7d0d4920 2022-05-12 thomas err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
945 7d0d4920 2022-05-12 thomas &delta_compressed_size, &delta_offset, &base_offset, &base_id,
946 7d0d4920 2022-05-12 thomas &base_size, &result_size, a->pack, a->packidx, obj_idx);
947 7d0d4920 2022-05-12 thomas if (err) {
948 7d0d4920 2022-05-12 thomas if (err->code == GOT_ERR_OBJ_TYPE)
949 7d0d4920 2022-05-12 thomas return NULL; /* object not stored as a delta */
950 7d0d4920 2022-05-12 thomas return err;
951 7d0d4920 2022-05-12 thomas }
952 7d0d4920 2022-05-12 thomas
953 7d0d4920 2022-05-12 thomas /*
954 7d0d4920 2022-05-12 thomas * If this is an offset delta we must determine the base
955 7d0d4920 2022-05-12 thomas * object ID ourselves.
956 7d0d4920 2022-05-12 thomas */
957 7d0d4920 2022-05-12 thomas if (base_offset != 0) {
958 7d0d4920 2022-05-12 thomas err = get_base_object_id(&base_id, a->packidx, base_offset);
959 7d0d4920 2022-05-12 thomas if (err)
960 7d0d4920 2022-05-12 thomas goto done;
961 7d0d4920 2022-05-12 thomas }
962 7d0d4920 2022-05-12 thomas
963 7d0d4920 2022-05-12 thomas if (got_object_idset_contains(a->idset, &base_id)) {
964 7d0d4920 2022-05-12 thomas struct got_imsg_reused_delta *delta;
965 7d0d4920 2022-05-12 thomas off_t delta_out_offset = ftello(a->delta_outfile);
966 7d0d4920 2022-05-12 thomas size_t w;
967 7d0d4920 2022-05-12 thomas
968 7d0d4920 2022-05-12 thomas w = fwrite(delta_buf, 1, delta_compressed_size,
969 7d0d4920 2022-05-12 thomas a->delta_outfile);
970 7d0d4920 2022-05-12 thomas if (w != delta_compressed_size) {
971 7d0d4920 2022-05-12 thomas err = got_ferror(a->delta_outfile, GOT_ERR_IO);
972 7d0d4920 2022-05-12 thomas goto done;
973 7d0d4920 2022-05-12 thomas }
974 7d0d4920 2022-05-12 thomas
975 7d0d4920 2022-05-12 thomas delta = &a->deltas[a->ndeltas++];
976 7d0d4920 2022-05-12 thomas memcpy(&delta->id, id, sizeof(delta->id));
977 7d0d4920 2022-05-12 thomas memcpy(&delta->base_id, &base_id, sizeof(delta->base_id));
978 7d0d4920 2022-05-12 thomas delta->base_size = base_size;
979 7d0d4920 2022-05-12 thomas delta->result_size = result_size;
980 7d0d4920 2022-05-12 thomas delta->delta_size = delta_size;
981 7d0d4920 2022-05-12 thomas delta->delta_compressed_size = delta_compressed_size;
982 7d0d4920 2022-05-12 thomas delta->delta_offset = delta_offset;
983 7d0d4920 2022-05-12 thomas delta->delta_out_offset = delta_out_offset;
984 7d0d4920 2022-05-12 thomas
985 7d0d4920 2022-05-12 thomas if (a->ndeltas >= GOT_IMSG_REUSED_DELTAS_MAX_NDELTAS) {
986 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas(a->ibuf,
987 7d0d4920 2022-05-12 thomas a->deltas, a->ndeltas);
988 7d0d4920 2022-05-12 thomas if (err)
989 7d0d4920 2022-05-12 thomas goto done;
990 7d0d4920 2022-05-12 thomas a->ndeltas = 0;
991 7d0d4920 2022-05-12 thomas }
992 7d0d4920 2022-05-12 thomas }
993 7d0d4920 2022-05-12 thomas done:
994 7d0d4920 2022-05-12 thomas free(delta_buf);
995 7d0d4920 2022-05-12 thomas return err;
996 7d0d4920 2022-05-12 thomas }
997 7d0d4920 2022-05-12 thomas
998 7d0d4920 2022-05-12 thomas static const struct got_error *
999 7d0d4920 2022-05-12 thomas recv_object_ids(struct got_object_idset *idset, struct imsgbuf *ibuf)
1000 7d0d4920 2022-05-12 thomas {
1001 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
1002 7d0d4920 2022-05-12 thomas int done = 0;
1003 7d0d4920 2022-05-12 thomas struct got_object_id *ids;
1004 7d0d4920 2022-05-12 thomas size_t nids, i;
1005 7d0d4920 2022-05-12 thomas
1006 7d0d4920 2022-05-12 thomas for (;;) {
1007 7d0d4920 2022-05-12 thomas err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1008 7d0d4920 2022-05-12 thomas if (err || done)
1009 7d0d4920 2022-05-12 thomas break;
1010 7d0d4920 2022-05-12 thomas for (i = 0; i < nids; i++) {
1011 7d0d4920 2022-05-12 thomas err = got_object_idset_add(idset, &ids[i], NULL);
1012 7d0d4920 2022-05-12 thomas if (err) {
1013 7d0d4920 2022-05-12 thomas free(ids);
1014 7d0d4920 2022-05-12 thomas return err;
1015 7d0d4920 2022-05-12 thomas }
1016 7d0d4920 2022-05-12 thomas }
1017 7d0d4920 2022-05-12 thomas free(ids);
1018 63915ee5 2022-06-23 thomas }
1019 63915ee5 2022-06-23 thomas
1020 63915ee5 2022-06-23 thomas return err;
1021 63915ee5 2022-06-23 thomas }
1022 63915ee5 2022-06-23 thomas
1023 63915ee5 2022-06-23 thomas static const struct got_error *
1024 63915ee5 2022-06-23 thomas recv_object_id_queue(struct got_object_id_queue *queue, struct imsgbuf *ibuf)
1025 63915ee5 2022-06-23 thomas {
1026 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1027 63915ee5 2022-06-23 thomas int done = 0;
1028 63915ee5 2022-06-23 thomas struct got_object_qid *qid;
1029 63915ee5 2022-06-23 thomas struct got_object_id *ids;
1030 63915ee5 2022-06-23 thomas size_t nids, i;
1031 63915ee5 2022-06-23 thomas
1032 63915ee5 2022-06-23 thomas for (;;) {
1033 63915ee5 2022-06-23 thomas err = got_privsep_recv_object_idlist(&done, &ids, &nids, ibuf);
1034 63915ee5 2022-06-23 thomas if (err || done)
1035 63915ee5 2022-06-23 thomas break;
1036 63915ee5 2022-06-23 thomas for (i = 0; i < nids; i++) {
1037 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1038 63915ee5 2022-06-23 thomas if (err)
1039 63915ee5 2022-06-23 thomas return err;
1040 63915ee5 2022-06-23 thomas memcpy(&qid->id, &ids[i], sizeof(qid->id));
1041 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(queue, qid, entry);
1042 63915ee5 2022-06-23 thomas }
1043 13280750 2022-06-13 thomas }
1044 13280750 2022-06-13 thomas
1045 13280750 2022-06-13 thomas return err;
1046 13280750 2022-06-13 thomas }
1047 13280750 2022-06-13 thomas
1048 13280750 2022-06-13 thomas static const struct got_error *
1049 7d0d4920 2022-05-12 thomas delta_reuse_request(struct imsg *imsg, struct imsgbuf *ibuf,
1050 7d0d4920 2022-05-12 thomas FILE *delta_outfile, struct got_pack *pack, struct got_packidx *packidx)
1051 7d0d4920 2022-05-12 thomas {
1052 7d0d4920 2022-05-12 thomas const struct got_error *err = NULL;
1053 7d0d4920 2022-05-12 thomas struct got_object_idset *idset;
1054 7d0d4920 2022-05-12 thomas struct search_deltas_arg sda;
1055 7d0d4920 2022-05-12 thomas
1056 7d0d4920 2022-05-12 thomas idset = got_object_idset_alloc();
1057 7d0d4920 2022-05-12 thomas if (idset == NULL)
1058 7d0d4920 2022-05-12 thomas return got_error_from_errno("got_object_idset_alloc");
1059 7d0d4920 2022-05-12 thomas
1060 7d0d4920 2022-05-12 thomas err = recv_object_ids(idset, ibuf);
1061 7d0d4920 2022-05-12 thomas if (err)
1062 7d0d4920 2022-05-12 thomas return err;
1063 7d0d4920 2022-05-12 thomas
1064 7d0d4920 2022-05-12 thomas memset(&sda, 0, sizeof(sda));
1065 7d0d4920 2022-05-12 thomas sda.ibuf = ibuf;
1066 7d0d4920 2022-05-12 thomas sda.idset = idset;
1067 7d0d4920 2022-05-12 thomas sda.pack = pack;
1068 7d0d4920 2022-05-12 thomas sda.packidx = packidx;
1069 7d0d4920 2022-05-12 thomas sda.delta_outfile = delta_outfile;
1070 7d0d4920 2022-05-12 thomas err = got_object_idset_for_each(idset, search_delta_for_object, &sda);
1071 7d0d4920 2022-05-12 thomas if (err)
1072 7d0d4920 2022-05-12 thomas goto done;
1073 7d0d4920 2022-05-12 thomas
1074 7d0d4920 2022-05-12 thomas if (sda.ndeltas > 0) {
1075 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas(ibuf, sda.deltas,
1076 7d0d4920 2022-05-12 thomas sda.ndeltas);
1077 7d0d4920 2022-05-12 thomas if (err)
1078 7d0d4920 2022-05-12 thomas goto done;
1079 7d0d4920 2022-05-12 thomas }
1080 7d0d4920 2022-05-12 thomas
1081 7d0d4920 2022-05-12 thomas if (fflush(delta_outfile) == -1) {
1082 7d0d4920 2022-05-12 thomas err = got_error_from_errno("fflush");
1083 7d0d4920 2022-05-12 thomas goto done;
1084 7d0d4920 2022-05-12 thomas }
1085 7d0d4920 2022-05-12 thomas
1086 7d0d4920 2022-05-12 thomas err = got_privsep_send_reused_deltas_done(ibuf);
1087 7d0d4920 2022-05-12 thomas done:
1088 7d0d4920 2022-05-12 thomas got_object_idset_free(idset);
1089 7d0d4920 2022-05-12 thomas return err;
1090 7d0d4920 2022-05-12 thomas }
1091 7d0d4920 2022-05-12 thomas
1092 7d0d4920 2022-05-12 thomas static const struct got_error *
1093 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
1094 876c234b 2018-09-10 stsp {
1095 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1096 876c234b 2018-09-10 stsp struct imsg imsg;
1097 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
1098 876c234b 2018-09-10 stsp size_t datalen;
1099 876c234b 2018-09-10 stsp struct got_packidx *p;
1100 876c234b 2018-09-10 stsp
1101 876c234b 2018-09-10 stsp *packidx = NULL;
1102 876c234b 2018-09-10 stsp
1103 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1104 876c234b 2018-09-10 stsp if (err)
1105 876c234b 2018-09-10 stsp return err;
1106 876c234b 2018-09-10 stsp
1107 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
1108 876c234b 2018-09-10 stsp if (p == NULL) {
1109 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1110 876c234b 2018-09-10 stsp goto done;
1111 876c234b 2018-09-10 stsp }
1112 876c234b 2018-09-10 stsp
1113 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
1114 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1115 876c234b 2018-09-10 stsp goto done;
1116 876c234b 2018-09-10 stsp }
1117 876c234b 2018-09-10 stsp
1118 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1119 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1120 876c234b 2018-09-10 stsp goto done;
1121 876c234b 2018-09-10 stsp }
1122 876c234b 2018-09-10 stsp
1123 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1124 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
1125 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1126 876c234b 2018-09-10 stsp goto done;
1127 876c234b 2018-09-10 stsp }
1128 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
1129 876c234b 2018-09-10 stsp
1130 876c234b 2018-09-10 stsp p->len = ipackidx.len;
1131 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
1132 876c234b 2018-09-10 stsp if (p->fd == -1) {
1133 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1134 56bef47a 2018-09-15 stsp goto done;
1135 56bef47a 2018-09-15 stsp }
1136 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
1137 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1138 876c234b 2018-09-10 stsp goto done;
1139 876c234b 2018-09-10 stsp }
1140 876c234b 2018-09-10 stsp
1141 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1142 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
1143 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
1144 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
1145 876c234b 2018-09-10 stsp #endif
1146 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
1147 876c234b 2018-09-10 stsp done:
1148 876c234b 2018-09-10 stsp if (err) {
1149 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1150 876c234b 2018-09-10 stsp close(imsg.fd);
1151 876c234b 2018-09-10 stsp got_packidx_close(p);
1152 876c234b 2018-09-10 stsp } else
1153 876c234b 2018-09-10 stsp *packidx = p;
1154 876c234b 2018-09-10 stsp imsg_free(&imsg);
1155 63915ee5 2022-06-23 thomas return err;
1156 63915ee5 2022-06-23 thomas }
1157 63915ee5 2022-06-23 thomas
1158 63915ee5 2022-06-23 thomas static const struct got_error *
1159 63915ee5 2022-06-23 thomas send_tree_enumeration_done(struct imsgbuf *ibuf)
1160 63915ee5 2022-06-23 thomas {
1161 63915ee5 2022-06-23 thomas if (imsg_compose(ibuf, GOT_IMSG_TREE_ENUMERATION_DONE, 0, 0, -1,
1162 63915ee5 2022-06-23 thomas NULL, 0) == -1)
1163 63915ee5 2022-06-23 thomas return got_error_from_errno("imsg_compose TREE_ENUMERATION_DONE");
1164 63915ee5 2022-06-23 thomas
1165 63915ee5 2022-06-23 thomas return got_privsep_flush_imsg(ibuf);
1166 63915ee5 2022-06-23 thomas }
1167 63915ee5 2022-06-23 thomas
1168 63915ee5 2022-06-23 thomas struct enumerated_tree {
1169 63915ee5 2022-06-23 thomas struct got_object_id id;
1170 63915ee5 2022-06-23 thomas char *path;
1171 63915ee5 2022-06-23 thomas uint8_t *buf;
1172 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *entries;
1173 63915ee5 2022-06-23 thomas int nentries;
1174 63915ee5 2022-06-23 thomas };
1175 63915ee5 2022-06-23 thomas
1176 63915ee5 2022-06-23 thomas static const struct got_error *
1177 63915ee5 2022-06-23 thomas enumerate_tree(int *have_all_entries, struct imsgbuf *ibuf, size_t *totlen,
1178 63915ee5 2022-06-23 thomas struct got_object_id *tree_id,
1179 63915ee5 2022-06-23 thomas const char *path, struct got_pack *pack, struct got_packidx *packidx,
1180 63915ee5 2022-06-23 thomas struct got_object_cache *objcache, struct got_object_idset *idset,
1181 63915ee5 2022-06-23 thomas struct enumerated_tree **trees, size_t *nalloc, size_t *ntrees)
1182 63915ee5 2022-06-23 thomas {
1183 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1184 63915ee5 2022-06-23 thomas struct got_object_id_queue ids;
1185 63915ee5 2022-06-23 thomas struct got_object_qid *qid;
1186 63915ee5 2022-06-23 thomas uint8_t *buf = NULL;
1187 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *entries = NULL;
1188 63915ee5 2022-06-23 thomas int nentries = 0, i;
1189 63915ee5 2022-06-23 thomas struct enumerated_tree *tree;
1190 63915ee5 2022-06-23 thomas
1191 63915ee5 2022-06-23 thomas *ntrees = 0;
1192 63915ee5 2022-06-23 thomas *have_all_entries = 1;
1193 63915ee5 2022-06-23 thomas STAILQ_INIT(&ids);
1194 63915ee5 2022-06-23 thomas
1195 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1196 63915ee5 2022-06-23 thomas if (err)
1197 63915ee5 2022-06-23 thomas return err;
1198 63915ee5 2022-06-23 thomas memcpy(&qid->id.sha1, tree_id, SHA1_DIGEST_LENGTH);
1199 63915ee5 2022-06-23 thomas qid->data = strdup(path);
1200 63915ee5 2022-06-23 thomas if (qid->data == NULL) {
1201 63915ee5 2022-06-23 thomas err = got_error_from_errno("strdup");
1202 63915ee5 2022-06-23 thomas goto done;
1203 63915ee5 2022-06-23 thomas }
1204 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&ids, qid, entry);
1205 63915ee5 2022-06-23 thomas qid = NULL;
1206 63915ee5 2022-06-23 thomas
1207 63915ee5 2022-06-23 thomas /* Traverse the tree hierarchy, gather tree object IDs and paths. */
1208 63915ee5 2022-06-23 thomas do {
1209 63915ee5 2022-06-23 thomas const char *path;
1210 63915ee5 2022-06-23 thomas int idx, i;
1211 63915ee5 2022-06-23 thomas
1212 63915ee5 2022-06-23 thomas if (sigint_received) {
1213 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_CANCELLED);
1214 63915ee5 2022-06-23 thomas goto done;
1215 63915ee5 2022-06-23 thomas }
1216 63915ee5 2022-06-23 thomas
1217 63915ee5 2022-06-23 thomas qid = STAILQ_FIRST(&ids);
1218 63915ee5 2022-06-23 thomas STAILQ_REMOVE_HEAD(&ids, entry);
1219 63915ee5 2022-06-23 thomas path = qid->data;
1220 63915ee5 2022-06-23 thomas
1221 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1222 63915ee5 2022-06-23 thomas if (idx == -1) {
1223 63915ee5 2022-06-23 thomas *have_all_entries = 0;
1224 63915ee5 2022-06-23 thomas break;
1225 63915ee5 2022-06-23 thomas }
1226 63915ee5 2022-06-23 thomas
1227 63915ee5 2022-06-23 thomas err = open_tree(&buf, &entries, &nentries,
1228 63915ee5 2022-06-23 thomas pack, packidx, idx, &qid->id, objcache);
1229 63915ee5 2022-06-23 thomas if (err) {
1230 63915ee5 2022-06-23 thomas if (err->code != GOT_ERR_NO_OBJ)
1231 63915ee5 2022-06-23 thomas goto done;
1232 63915ee5 2022-06-23 thomas }
1233 63915ee5 2022-06-23 thomas
1234 63915ee5 2022-06-23 thomas err = got_object_idset_add(idset, &qid->id, NULL);
1235 63915ee5 2022-06-23 thomas if (err)
1236 63915ee5 2022-06-23 thomas goto done;
1237 63915ee5 2022-06-23 thomas
1238 63915ee5 2022-06-23 thomas for (i = 0; i < nentries; i++) {
1239 63915ee5 2022-06-23 thomas struct got_object_qid *eqid = NULL;
1240 63915ee5 2022-06-23 thomas struct got_parsed_tree_entry *pte = &entries[i];
1241 63915ee5 2022-06-23 thomas char *p;
1242 63915ee5 2022-06-23 thomas
1243 63915ee5 2022-06-23 thomas if (!S_ISDIR(pte->mode))
1244 63915ee5 2022-06-23 thomas continue;
1245 63915ee5 2022-06-23 thomas
1246 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&eqid);
1247 63915ee5 2022-06-23 thomas if (err)
1248 63915ee5 2022-06-23 thomas goto done;
1249 63915ee5 2022-06-23 thomas memcpy(eqid->id.sha1, pte->id, sizeof(eqid->id.sha1));
1250 63915ee5 2022-06-23 thomas
1251 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &eqid->id)) {
1252 63915ee5 2022-06-23 thomas got_object_qid_free(eqid);
1253 63915ee5 2022-06-23 thomas continue;
1254 63915ee5 2022-06-23 thomas }
1255 63915ee5 2022-06-23 thomas
1256 63915ee5 2022-06-23 thomas if (asprintf(&p, "%s%s%s", path,
1257 63915ee5 2022-06-23 thomas got_path_is_root_dir(path) ? "" : "/",
1258 63915ee5 2022-06-23 thomas pte->name) == -1) {
1259 63915ee5 2022-06-23 thomas err = got_error_from_errno("asprintf");
1260 63915ee5 2022-06-23 thomas got_object_qid_free(eqid);
1261 63915ee5 2022-06-23 thomas goto done;
1262 63915ee5 2022-06-23 thomas }
1263 63915ee5 2022-06-23 thomas eqid->data = p;
1264 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&ids, eqid, entry);
1265 63915ee5 2022-06-23 thomas }
1266 63915ee5 2022-06-23 thomas
1267 63915ee5 2022-06-23 thomas if (*ntrees >= *nalloc) {
1268 63915ee5 2022-06-23 thomas struct enumerated_tree *new;
1269 63915ee5 2022-06-23 thomas new = recallocarray(*trees, *nalloc, *nalloc + 16,
1270 63915ee5 2022-06-23 thomas sizeof(*new));
1271 63915ee5 2022-06-23 thomas if (new == NULL) {
1272 63915ee5 2022-06-23 thomas err = got_error_from_errno("malloc");
1273 63915ee5 2022-06-23 thomas goto done;
1274 63915ee5 2022-06-23 thomas }
1275 63915ee5 2022-06-23 thomas *trees = new;
1276 63915ee5 2022-06-23 thomas *nalloc += 16;
1277 63915ee5 2022-06-23 thomas }
1278 63915ee5 2022-06-23 thomas tree = &(*trees)[*ntrees];
1279 63915ee5 2022-06-23 thomas (*ntrees)++;
1280 63915ee5 2022-06-23 thomas memcpy(&tree->id, &qid->id, sizeof(tree->id));
1281 63915ee5 2022-06-23 thomas tree->path = qid->data;
1282 63915ee5 2022-06-23 thomas tree->buf = buf;
1283 63915ee5 2022-06-23 thomas buf = NULL;
1284 63915ee5 2022-06-23 thomas tree->entries = entries;
1285 63915ee5 2022-06-23 thomas entries = NULL;
1286 63915ee5 2022-06-23 thomas tree->nentries = nentries;
1287 63915ee5 2022-06-23 thomas
1288 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1289 63915ee5 2022-06-23 thomas qid = NULL;
1290 63915ee5 2022-06-23 thomas } while (!STAILQ_EMPTY(&ids));
1291 63915ee5 2022-06-23 thomas
1292 63915ee5 2022-06-23 thomas if (*have_all_entries) {
1293 63915ee5 2022-06-23 thomas int i;
1294 63915ee5 2022-06-23 thomas /*
1295 63915ee5 2022-06-23 thomas * We have managed to traverse all entries in the hierarchy.
1296 63915ee5 2022-06-23 thomas * Tell the main process what we have found.
1297 63915ee5 2022-06-23 thomas */
1298 63915ee5 2022-06-23 thomas for (i = 0; i < *ntrees; i++) {
1299 63915ee5 2022-06-23 thomas tree = &(*trees)[i];
1300 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(totlen,
1301 63915ee5 2022-06-23 thomas ibuf, &tree->id, tree->path, tree->entries,
1302 63915ee5 2022-06-23 thomas tree->nentries);
1303 63915ee5 2022-06-23 thomas if (err)
1304 63915ee5 2022-06-23 thomas goto done;
1305 63915ee5 2022-06-23 thomas free(tree->buf);
1306 63915ee5 2022-06-23 thomas tree->buf = NULL;
1307 63915ee5 2022-06-23 thomas free(tree->path);
1308 63915ee5 2022-06-23 thomas tree->path = NULL;
1309 63915ee5 2022-06-23 thomas free(tree->entries);
1310 63915ee5 2022-06-23 thomas tree->entries = NULL;
1311 63915ee5 2022-06-23 thomas }
1312 63915ee5 2022-06-23 thomas *ntrees = 0; /* don't loop again below to free memory */
1313 63915ee5 2022-06-23 thomas
1314 63915ee5 2022-06-23 thomas err = send_tree_enumeration_done(ibuf);
1315 63915ee5 2022-06-23 thomas } else {
1316 63915ee5 2022-06-23 thomas /*
1317 63915ee5 2022-06-23 thomas * We can only load fully packed tree hierarchies on
1318 63915ee5 2022-06-23 thomas * behalf of the main process, otherwise the main process
1319 63915ee5 2022-06-23 thomas * gets a wrong idea about which tree objects have
1320 63915ee5 2022-06-23 thomas * already been traversed.
1321 63915ee5 2022-06-23 thomas * Indicate a missing entry for the root of this tree.
1322 63915ee5 2022-06-23 thomas * The main process should continue by loading this
1323 63915ee5 2022-06-23 thomas * entire tree the slow way.
1324 63915ee5 2022-06-23 thomas */
1325 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(totlen, ibuf,
1326 63915ee5 2022-06-23 thomas tree_id, "/", NULL, -1);
1327 63915ee5 2022-06-23 thomas if (err)
1328 63915ee5 2022-06-23 thomas goto done;
1329 63915ee5 2022-06-23 thomas }
1330 63915ee5 2022-06-23 thomas done:
1331 63915ee5 2022-06-23 thomas free(buf);
1332 63915ee5 2022-06-23 thomas free(entries);
1333 63915ee5 2022-06-23 thomas for (i = 0; i < *ntrees; i++) {
1334 63915ee5 2022-06-23 thomas tree = &(*trees)[i];
1335 63915ee5 2022-06-23 thomas free(tree->buf);
1336 63915ee5 2022-06-23 thomas tree->buf = NULL;
1337 63915ee5 2022-06-23 thomas free(tree->path);
1338 63915ee5 2022-06-23 thomas tree->path = NULL;
1339 63915ee5 2022-06-23 thomas free(tree->entries);
1340 63915ee5 2022-06-23 thomas tree->entries = NULL;
1341 63915ee5 2022-06-23 thomas }
1342 63915ee5 2022-06-23 thomas if (qid)
1343 63915ee5 2022-06-23 thomas free(qid->data);
1344 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1345 63915ee5 2022-06-23 thomas got_object_id_queue_free(&ids);
1346 63915ee5 2022-06-23 thomas if (err) {
1347 63915ee5 2022-06-23 thomas if (err->code == GOT_ERR_PRIVSEP_PIPE)
1348 63915ee5 2022-06-23 thomas err = NULL;
1349 63915ee5 2022-06-23 thomas else
1350 63915ee5 2022-06-23 thomas got_privsep_send_error(ibuf, err);
1351 63915ee5 2022-06-23 thomas }
1352 63915ee5 2022-06-23 thomas
1353 13280750 2022-06-13 thomas return err;
1354 13280750 2022-06-13 thomas }
1355 13280750 2022-06-13 thomas
1356 13280750 2022-06-13 thomas static const struct got_error *
1357 63915ee5 2022-06-23 thomas enumeration_request(struct imsg *imsg, struct imsgbuf *ibuf,
1358 63915ee5 2022-06-23 thomas struct got_pack *pack, struct got_packidx *packidx,
1359 63915ee5 2022-06-23 thomas struct got_object_cache *objcache)
1360 63915ee5 2022-06-23 thomas {
1361 63915ee5 2022-06-23 thomas const struct got_error *err = NULL;
1362 63915ee5 2022-06-23 thomas struct got_object_id_queue commit_ids;
1363 63915ee5 2022-06-23 thomas const struct got_object_id_queue *parents = NULL;
1364 63915ee5 2022-06-23 thomas struct got_object_qid *qid = NULL;
1365 63915ee5 2022-06-23 thomas struct got_object *obj = NULL;
1366 63915ee5 2022-06-23 thomas struct got_commit_object *commit = NULL;
1367 63915ee5 2022-06-23 thomas struct got_object_id *tree_id = NULL;
1368 63915ee5 2022-06-23 thomas size_t totlen = 0;
1369 63915ee5 2022-06-23 thomas struct got_object_idset *idset;
1370 63915ee5 2022-06-23 thomas int i, idx, have_all_entries = 1;
1371 63915ee5 2022-06-23 thomas struct enumerated_tree *trees = NULL;
1372 63915ee5 2022-06-23 thomas size_t ntrees = 0, nalloc = 16;
1373 63915ee5 2022-06-23 thomas
1374 63915ee5 2022-06-23 thomas STAILQ_INIT(&commit_ids);
1375 63915ee5 2022-06-23 thomas
1376 93088cca 2022-06-23 thomas trees = calloc(nalloc, sizeof(*trees));
1377 63915ee5 2022-06-23 thomas if (trees == NULL)
1378 63915ee5 2022-06-23 thomas return got_error_from_errno("calloc");
1379 63915ee5 2022-06-23 thomas
1380 63915ee5 2022-06-23 thomas idset = got_object_idset_alloc();
1381 63915ee5 2022-06-23 thomas if (idset == NULL) {
1382 63915ee5 2022-06-23 thomas err = got_error_from_errno("got_object_idset_alloc");
1383 63915ee5 2022-06-23 thomas goto done;
1384 63915ee5 2022-06-23 thomas }
1385 63915ee5 2022-06-23 thomas
1386 63915ee5 2022-06-23 thomas err = recv_object_id_queue(&commit_ids, ibuf);
1387 63915ee5 2022-06-23 thomas if (err)
1388 63915ee5 2022-06-23 thomas goto done;
1389 63915ee5 2022-06-23 thomas
1390 ef53e23c 2022-06-23 thomas if (STAILQ_EMPTY(&commit_ids)) {
1391 ef53e23c 2022-06-23 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1392 ef53e23c 2022-06-23 thomas goto done;
1393 ef53e23c 2022-06-23 thomas }
1394 ef53e23c 2022-06-23 thomas
1395 63915ee5 2022-06-23 thomas err = recv_object_ids(idset, ibuf);
1396 63915ee5 2022-06-23 thomas if (err)
1397 63915ee5 2022-06-23 thomas goto done;
1398 63915ee5 2022-06-23 thomas
1399 63915ee5 2022-06-23 thomas while (!STAILQ_EMPTY(&commit_ids)) {
1400 63915ee5 2022-06-23 thomas if (sigint_received) {
1401 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_CANCELLED);
1402 63915ee5 2022-06-23 thomas goto done;
1403 63915ee5 2022-06-23 thomas }
1404 63915ee5 2022-06-23 thomas
1405 63915ee5 2022-06-23 thomas qid = STAILQ_FIRST(&commit_ids);
1406 63915ee5 2022-06-23 thomas STAILQ_REMOVE_HEAD(&commit_ids, entry);
1407 63915ee5 2022-06-23 thomas
1408 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &qid->id)) {
1409 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1410 63915ee5 2022-06-23 thomas qid = NULL;
1411 63915ee5 2022-06-23 thomas continue;
1412 63915ee5 2022-06-23 thomas }
1413 63915ee5 2022-06-23 thomas
1414 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &qid->id);
1415 e71f1e62 2022-06-23 thomas if (idx == -1) {
1416 e71f1e62 2022-06-23 thomas have_all_entries = 0;
1417 63915ee5 2022-06-23 thomas break;
1418 e71f1e62 2022-06-23 thomas }
1419 63915ee5 2022-06-23 thomas
1420 63915ee5 2022-06-23 thomas err = open_object(&obj, pack, packidx, idx, &qid->id,
1421 63915ee5 2022-06-23 thomas objcache);
1422 63915ee5 2022-06-23 thomas if (err)
1423 63915ee5 2022-06-23 thomas goto done;
1424 63915ee5 2022-06-23 thomas if (obj->type == GOT_OBJ_TYPE_TAG) {
1425 63915ee5 2022-06-23 thomas struct got_tag_object *tag;
1426 63915ee5 2022-06-23 thomas uint8_t *buf;
1427 63915ee5 2022-06-23 thomas size_t len;
1428 63915ee5 2022-06-23 thomas err = got_packfile_extract_object_to_mem(&buf,
1429 63915ee5 2022-06-23 thomas &len, obj, pack);
1430 63915ee5 2022-06-23 thomas if (err)
1431 63915ee5 2022-06-23 thomas goto done;
1432 63915ee5 2022-06-23 thomas obj->size = len;
1433 63915ee5 2022-06-23 thomas err = got_object_parse_tag(&tag, buf, len);
1434 63915ee5 2022-06-23 thomas if (err) {
1435 63915ee5 2022-06-23 thomas free(buf);
1436 63915ee5 2022-06-23 thomas goto done;
1437 63915ee5 2022-06-23 thomas }
1438 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, &tag->id);
1439 e71f1e62 2022-06-23 thomas if (idx == -1) {
1440 e71f1e62 2022-06-23 thomas have_all_entries = 0;
1441 63915ee5 2022-06-23 thomas break;
1442 e71f1e62 2022-06-23 thomas }
1443 63915ee5 2022-06-23 thomas err = open_commit(&commit, pack, packidx, idx,
1444 63915ee5 2022-06-23 thomas &tag->id, objcache);
1445 63915ee5 2022-06-23 thomas got_object_tag_close(tag);
1446 63915ee5 2022-06-23 thomas free(buf);
1447 63915ee5 2022-06-23 thomas if (err)
1448 63915ee5 2022-06-23 thomas goto done;
1449 63915ee5 2022-06-23 thomas } else if (obj->type == GOT_OBJ_TYPE_COMMIT) {
1450 63915ee5 2022-06-23 thomas err = open_commit(&commit, pack, packidx, idx,
1451 63915ee5 2022-06-23 thomas &qid->id, objcache);
1452 63915ee5 2022-06-23 thomas if (err)
1453 63915ee5 2022-06-23 thomas goto done;
1454 63915ee5 2022-06-23 thomas } else {
1455 63915ee5 2022-06-23 thomas err = got_error(GOT_ERR_OBJ_TYPE);
1456 63915ee5 2022-06-23 thomas goto done;
1457 63915ee5 2022-06-23 thomas }
1458 63915ee5 2022-06-23 thomas got_object_close(obj);
1459 63915ee5 2022-06-23 thomas obj = NULL;
1460 63915ee5 2022-06-23 thomas
1461 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_commit(ibuf, &qid->id,
1462 63915ee5 2022-06-23 thomas got_object_commit_get_committer_time(commit));
1463 63915ee5 2022-06-23 thomas if (err)
1464 63915ee5 2022-06-23 thomas goto done;
1465 63915ee5 2022-06-23 thomas
1466 63915ee5 2022-06-23 thomas tree_id = got_object_commit_get_tree_id(commit);
1467 63915ee5 2022-06-23 thomas idx = got_packidx_get_object_idx(packidx, tree_id);
1468 63915ee5 2022-06-23 thomas if (idx == -1) {
1469 e71f1e62 2022-06-23 thomas have_all_entries = 0;
1470 63915ee5 2022-06-23 thomas err = got_privsep_send_enumerated_tree(&totlen, ibuf,
1471 63915ee5 2022-06-23 thomas tree_id, "/", NULL, -1);
1472 63915ee5 2022-06-23 thomas if (err)
1473 63915ee5 2022-06-23 thomas goto done;
1474 63915ee5 2022-06-23 thomas break;
1475 63915ee5 2022-06-23 thomas }
1476 63915ee5 2022-06-23 thomas
1477 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, tree_id)) {
1478 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1479 63915ee5 2022-06-23 thomas qid = NULL;
1480 63915ee5 2022-06-23 thomas continue;
1481 63915ee5 2022-06-23 thomas }
1482 63915ee5 2022-06-23 thomas
1483 63915ee5 2022-06-23 thomas err = enumerate_tree(&have_all_entries, ibuf, &totlen, tree_id, "/",
1484 63915ee5 2022-06-23 thomas pack, packidx, objcache, idset, &trees, &nalloc, &ntrees);
1485 63915ee5 2022-06-23 thomas if (err)
1486 63915ee5 2022-06-23 thomas goto done;
1487 63915ee5 2022-06-23 thomas
1488 63915ee5 2022-06-23 thomas if (!have_all_entries)
1489 63915ee5 2022-06-23 thomas break;
1490 63915ee5 2022-06-23 thomas
1491 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1492 63915ee5 2022-06-23 thomas qid = NULL;
1493 63915ee5 2022-06-23 thomas
1494 63915ee5 2022-06-23 thomas parents = got_object_commit_get_parent_ids(commit);
1495 63915ee5 2022-06-23 thomas if (parents) {
1496 63915ee5 2022-06-23 thomas struct got_object_qid *pid;
1497 63915ee5 2022-06-23 thomas STAILQ_FOREACH(pid, parents, entry) {
1498 63915ee5 2022-06-23 thomas if (got_object_idset_contains(idset, &pid->id))
1499 63915ee5 2022-06-23 thomas continue;
1500 63915ee5 2022-06-23 thomas err = got_object_qid_alloc_partial(&qid);
1501 63915ee5 2022-06-23 thomas if (err)
1502 63915ee5 2022-06-23 thomas goto done;
1503 63915ee5 2022-06-23 thomas memcpy(&qid->id, &pid->id, sizeof(qid->id));
1504 63915ee5 2022-06-23 thomas STAILQ_INSERT_TAIL(&commit_ids, qid, entry);
1505 63915ee5 2022-06-23 thomas qid = NULL;
1506 63915ee5 2022-06-23 thomas }
1507 63915ee5 2022-06-23 thomas }
1508 63915ee5 2022-06-23 thomas
1509 63915ee5 2022-06-23 thomas got_object_commit_close(commit);
1510 63915ee5 2022-06-23 thomas commit = NULL;
1511 63915ee5 2022-06-23 thomas }
1512 63915ee5 2022-06-23 thomas
1513 63915ee5 2022-06-23 thomas if (have_all_entries) {
1514 63915ee5 2022-06-23 thomas err = got_privsep_send_object_enumeration_done(ibuf);
1515 e71f1e62 2022-06-23 thomas if (err)
1516 e71f1e62 2022-06-23 thomas goto done;
1517 e71f1e62 2022-06-23 thomas } else {
1518 e71f1e62 2022-06-23 thomas err = got_privsep_send_object_enumeration_incomplete(ibuf);
1519 63915ee5 2022-06-23 thomas if (err)
1520 63915ee5 2022-06-23 thomas goto done;
1521 63915ee5 2022-06-23 thomas }
1522 63915ee5 2022-06-23 thomas done:
1523 63915ee5 2022-06-23 thomas if (obj)
1524 63915ee5 2022-06-23 thomas got_object_close(obj);
1525 63915ee5 2022-06-23 thomas if (commit)
1526 63915ee5 2022-06-23 thomas got_object_commit_close(commit);
1527 63915ee5 2022-06-23 thomas got_object_qid_free(qid);
1528 63915ee5 2022-06-23 thomas got_object_id_queue_free(&commit_ids);
1529 63915ee5 2022-06-23 thomas if (idset)
1530 63915ee5 2022-06-23 thomas got_object_idset_free(idset);
1531 63915ee5 2022-06-23 thomas for (i = 0; i < ntrees; i++) {
1532 63915ee5 2022-06-23 thomas struct enumerated_tree *tree = &trees[i];
1533 63915ee5 2022-06-23 thomas free(tree->buf);
1534 63915ee5 2022-06-23 thomas free(tree->path);
1535 63915ee5 2022-06-23 thomas free(tree->entries);
1536 63915ee5 2022-06-23 thomas }
1537 63915ee5 2022-06-23 thomas free(trees);
1538 63915ee5 2022-06-23 thomas return err;
1539 63915ee5 2022-06-23 thomas }
1540 63915ee5 2022-06-23 thomas
1541 63915ee5 2022-06-23 thomas static const struct got_error *
1542 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
1543 876c234b 2018-09-10 stsp {
1544 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1545 876c234b 2018-09-10 stsp struct imsg imsg;
1546 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
1547 876c234b 2018-09-10 stsp size_t datalen;
1548 876c234b 2018-09-10 stsp struct got_pack *pack;
1549 876c234b 2018-09-10 stsp
1550 876c234b 2018-09-10 stsp *packp = NULL;
1551 876c234b 2018-09-10 stsp
1552 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1553 876c234b 2018-09-10 stsp if (err)
1554 876c234b 2018-09-10 stsp return err;
1555 876c234b 2018-09-10 stsp
1556 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
1557 876c234b 2018-09-10 stsp if (pack == NULL) {
1558 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1559 876c234b 2018-09-10 stsp goto done;
1560 876c234b 2018-09-10 stsp }
1561 876c234b 2018-09-10 stsp
1562 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
1563 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1564 876c234b 2018-09-10 stsp goto done;
1565 876c234b 2018-09-10 stsp }
1566 876c234b 2018-09-10 stsp
1567 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1568 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1569 876c234b 2018-09-10 stsp goto done;
1570 876c234b 2018-09-10 stsp }
1571 876c234b 2018-09-10 stsp
1572 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1573 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
1574 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1575 876c234b 2018-09-10 stsp goto done;
1576 876c234b 2018-09-10 stsp }
1577 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
1578 876c234b 2018-09-10 stsp
1579 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
1580 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
1581 876c234b 2018-09-10 stsp if (pack->fd == -1) {
1582 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1583 876c234b 2018-09-10 stsp goto done;
1584 876c234b 2018-09-10 stsp }
1585 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1586 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1587 56bef47a 2018-09-15 stsp goto done;
1588 56bef47a 2018-09-15 stsp }
1589 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
1590 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
1591 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1592 ab2f42e7 2019-11-10 stsp goto done;
1593 ab2f42e7 2019-11-10 stsp }
1594 ab2f42e7 2019-11-10 stsp
1595 a5061f77 2022-06-13 thomas err = got_delta_cache_alloc(&pack->delta_cache);
1596 a5061f77 2022-06-13 thomas if (err)
1597 876c234b 2018-09-10 stsp goto done;
1598 876c234b 2018-09-10 stsp
1599 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1600 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1601 876c234b 2018-09-10 stsp pack->fd, 0);
1602 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
1603 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
1604 876c234b 2018-09-10 stsp #endif
1605 876c234b 2018-09-10 stsp done:
1606 876c234b 2018-09-10 stsp if (err) {
1607 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1608 876c234b 2018-09-10 stsp close(imsg.fd);
1609 876c234b 2018-09-10 stsp free(pack);
1610 876c234b 2018-09-10 stsp } else
1611 876c234b 2018-09-10 stsp *packp = pack;
1612 876c234b 2018-09-10 stsp imsg_free(&imsg);
1613 876c234b 2018-09-10 stsp return err;
1614 876c234b 2018-09-10 stsp }
1615 876c234b 2018-09-10 stsp
1616 876c234b 2018-09-10 stsp int
1617 876c234b 2018-09-10 stsp main(int argc, char *argv[])
1618 876c234b 2018-09-10 stsp {
1619 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1620 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
1621 876c234b 2018-09-10 stsp struct imsg imsg;
1622 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
1623 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
1624 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1625 f9c2e8e5 2022-02-13 thomas FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1626 876c234b 2018-09-10 stsp
1627 876c234b 2018-09-10 stsp //static int attached;
1628 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1629 876c234b 2018-09-10 stsp
1630 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1631 99437157 2018-11-11 stsp
1632 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1633 876c234b 2018-09-10 stsp
1634 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1635 c59b3346 2018-09-11 stsp if (err) {
1636 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1637 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1638 c59b3346 2018-09-11 stsp return 1;
1639 c59b3346 2018-09-11 stsp }
1640 c59b3346 2018-09-11 stsp
1641 2ff12563 2018-09-15 stsp #ifndef PROFILE
1642 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1643 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1644 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1645 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1646 876c234b 2018-09-10 stsp return 1;
1647 876c234b 2018-09-10 stsp }
1648 97799ccd 2022-02-06 thomas
1649 97799ccd 2022-02-06 thomas /* revoke fs access */
1650 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
1651 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
1652 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
1653 97799ccd 2022-02-06 thomas return 1;
1654 97799ccd 2022-02-06 thomas }
1655 2ff12563 2018-09-15 stsp #endif
1656 876c234b 2018-09-10 stsp
1657 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1658 876c234b 2018-09-10 stsp if (err) {
1659 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1660 876c234b 2018-09-10 stsp return 1;
1661 876c234b 2018-09-10 stsp }
1662 876c234b 2018-09-10 stsp
1663 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1664 876c234b 2018-09-10 stsp if (err) {
1665 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1666 876c234b 2018-09-10 stsp return 1;
1667 876c234b 2018-09-10 stsp }
1668 876c234b 2018-09-10 stsp
1669 656b1f76 2019-05-11 jcs for (;;) {
1670 876c234b 2018-09-10 stsp imsg.fd = -1;
1671 99437157 2018-11-11 stsp
1672 99437157 2018-11-11 stsp if (sigint_received) {
1673 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1674 99437157 2018-11-11 stsp break;
1675 99437157 2018-11-11 stsp }
1676 876c234b 2018-09-10 stsp
1677 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1678 876c234b 2018-09-10 stsp if (err) {
1679 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1680 876c234b 2018-09-10 stsp err = NULL;
1681 876c234b 2018-09-10 stsp break;
1682 876c234b 2018-09-10 stsp }
1683 876c234b 2018-09-10 stsp
1684 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1685 876c234b 2018-09-10 stsp break;
1686 876c234b 2018-09-10 stsp
1687 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1688 bc1f382f 2022-01-05 thomas case GOT_IMSG_TMPFD:
1689 f9c2e8e5 2022-02-13 thomas if (basefile == NULL) {
1690 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&basefile, "w+",
1691 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1692 f9c2e8e5 2022-02-13 thomas } else if (accumfile == NULL) {
1693 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&accumfile, "w+",
1694 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1695 f9c2e8e5 2022-02-13 thomas } else
1696 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1697 bc1f382f 2022-01-05 thomas break;
1698 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
1699 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
1700 c59b3346 2018-09-11 stsp &objcache);
1701 876c234b 2018-09-10 stsp break;
1702 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1703 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
1704 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1705 bc1f382f 2022-01-05 thomas break;
1706 bc1f382f 2022-01-05 thomas }
1707 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
1708 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
1709 59d1e4a0 2021-03-10 stsp break;
1710 f9c2e8e5 2022-02-13 thomas case GOT_IMSG_RAW_DELTA_OUTFD:
1711 f9c2e8e5 2022-02-13 thomas if (delta_outfile != NULL) {
1712 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1713 f9c2e8e5 2022-02-13 thomas break;
1714 f9c2e8e5 2022-02-13 thomas }
1715 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&delta_outfile, "w",
1716 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1717 f9c2e8e5 2022-02-13 thomas break;
1718 f9c2e8e5 2022-02-13 thomas case GOT_IMSG_RAW_DELTA_REQUEST:
1719 f9c2e8e5 2022-02-13 thomas if (delta_outfile == NULL) {
1720 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1721 f9c2e8e5 2022-02-13 thomas break;
1722 f9c2e8e5 2022-02-13 thomas }
1723 f9c2e8e5 2022-02-13 thomas err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1724 f9c2e8e5 2022-02-13 thomas pack, packidx);
1725 f9c2e8e5 2022-02-13 thomas break;
1726 7d0d4920 2022-05-12 thomas case GOT_IMSG_DELTA_REUSE_REQUEST:
1727 7d0d4920 2022-05-12 thomas if (delta_outfile == NULL) {
1728 7d0d4920 2022-05-12 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1729 7d0d4920 2022-05-12 thomas break;
1730 7d0d4920 2022-05-12 thomas }
1731 7d0d4920 2022-05-12 thomas err = delta_reuse_request(&imsg, &ibuf,
1732 7d0d4920 2022-05-12 thomas delta_outfile, pack, packidx);
1733 7d0d4920 2022-05-12 thomas break;
1734 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
1735 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
1736 7762fe12 2018-11-05 stsp &objcache);
1737 7762fe12 2018-11-05 stsp break;
1738 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
1739 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
1740 62d463ca 2020-10-20 naddy &objcache);
1741 876c234b 2018-09-10 stsp break;
1742 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
1743 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
1744 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1745 bc1f382f 2022-01-05 thomas break;
1746 bc1f382f 2022-01-05 thomas }
1747 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
1748 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
1749 876c234b 2018-09-10 stsp break;
1750 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
1751 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
1752 62d463ca 2020-10-20 naddy &objcache);
1753 f4a881ce 2018-11-17 stsp break;
1754 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1755 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
1756 ca6e02ac 2020-01-07 stsp packidx, &objcache);
1757 ca6e02ac 2020-01-07 stsp break;
1758 63915ee5 2022-06-23 thomas case GOT_IMSG_OBJECT_ENUMERATION_REQUEST:
1759 63915ee5 2022-06-23 thomas err = enumeration_request(&imsg, &ibuf, pack,
1760 63915ee5 2022-06-23 thomas packidx, &objcache);
1761 63915ee5 2022-06-23 thomas break;
1762 876c234b 2018-09-10 stsp default:
1763 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1764 876c234b 2018-09-10 stsp break;
1765 876c234b 2018-09-10 stsp }
1766 876c234b 2018-09-10 stsp
1767 08578a35 2021-01-22 stsp if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1768 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1769 876c234b 2018-09-10 stsp imsg_free(&imsg);
1770 99437157 2018-11-11 stsp if (err)
1771 876c234b 2018-09-10 stsp break;
1772 876c234b 2018-09-10 stsp }
1773 876c234b 2018-09-10 stsp
1774 c59b3346 2018-09-11 stsp if (packidx)
1775 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
1776 c59b3346 2018-09-11 stsp if (pack)
1777 c59b3346 2018-09-11 stsp got_pack_close(pack);
1778 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
1779 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
1780 bc1f382f 2022-01-05 thomas if (basefile && fclose(basefile) == EOF && err == NULL)
1781 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
1782 bc1f382f 2022-01-05 thomas if (accumfile && fclose(accumfile) == EOF && err == NULL)
1783 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
1784 f9c2e8e5 2022-02-13 thomas if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1785 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fclose");
1786 99437157 2018-11-11 stsp if (err) {
1787 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1788 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1789 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
1790 80d5f134 2018-11-11 stsp }
1791 99437157 2018-11-11 stsp }
1792 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1793 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1794 876c234b 2018-09-10 stsp return err ? 1 : 0;
1795 876c234b 2018-09-10 stsp }