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