Blame


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