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