Blame


1 876c234b 2018-09-10 stsp /*
2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
3 876c234b 2018-09-10 stsp *
4 876c234b 2018-09-10 stsp * Permission to use, copy, modify, and distribute this software for any
5 876c234b 2018-09-10 stsp * purpose with or without fee is hereby granted, provided that the above
6 876c234b 2018-09-10 stsp * copyright notice and this permission notice appear in all copies.
7 876c234b 2018-09-10 stsp *
8 876c234b 2018-09-10 stsp * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 876c234b 2018-09-10 stsp * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 876c234b 2018-09-10 stsp * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 876c234b 2018-09-10 stsp * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 876c234b 2018-09-10 stsp * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 876c234b 2018-09-10 stsp * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 876c234b 2018-09-10 stsp * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 876c234b 2018-09-10 stsp */
16 876c234b 2018-09-10 stsp
17 876c234b 2018-09-10 stsp #include <sys/types.h>
18 876c234b 2018-09-10 stsp #include <sys/queue.h>
19 876c234b 2018-09-10 stsp #include <sys/uio.h>
20 876c234b 2018-09-10 stsp #include <sys/time.h>
21 876c234b 2018-09-10 stsp #include <sys/mman.h>
22 876c234b 2018-09-10 stsp
23 876c234b 2018-09-10 stsp #include <limits.h>
24 99437157 2018-11-11 stsp #include <signal.h>
25 876c234b 2018-09-10 stsp #include <stdint.h>
26 876c234b 2018-09-10 stsp #include <imsg.h>
27 876c234b 2018-09-10 stsp #include <stdio.h>
28 876c234b 2018-09-10 stsp #include <stdlib.h>
29 876c234b 2018-09-10 stsp #include <string.h>
30 876c234b 2018-09-10 stsp #include <sha1.h>
31 81a12da5 2020-09-09 naddy #include <unistd.h>
32 876c234b 2018-09-10 stsp #include <zlib.h>
33 876c234b 2018-09-10 stsp
34 876c234b 2018-09-10 stsp #include "got_error.h"
35 876c234b 2018-09-10 stsp #include "got_object.h"
36 3022d272 2019-11-14 stsp #include "got_path.h"
37 876c234b 2018-09-10 stsp
38 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
39 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
40 876c234b 2018-09-10 stsp #include "got_lib_object.h"
41 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
42 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
43 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
44 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
45 876c234b 2018-09-10 stsp
46 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
47 99437157 2018-11-11 stsp
48 99437157 2018-11-11 stsp static void
49 99437157 2018-11-11 stsp catch_sigint(int signo)
50 99437157 2018-11-11 stsp {
51 99437157 2018-11-11 stsp sigint_received = 1;
52 99437157 2018-11-11 stsp }
53 99437157 2018-11-11 stsp
54 876c234b 2018-09-10 stsp static const struct got_error *
55 704b89c4 2019-05-23 stsp open_object(struct got_object **obj, struct got_pack *pack,
56 704b89c4 2019-05-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id,
57 704b89c4 2019-05-23 stsp struct got_object_cache *objcache)
58 704b89c4 2019-05-23 stsp {
59 704b89c4 2019-05-23 stsp const struct got_error *err;
60 704b89c4 2019-05-23 stsp
61 704b89c4 2019-05-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
62 704b89c4 2019-05-23 stsp if (err)
63 704b89c4 2019-05-23 stsp return err;
64 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
65 704b89c4 2019-05-23 stsp
66 704b89c4 2019-05-23 stsp err = got_object_cache_add(objcache, id, *obj);
67 79c99a64 2019-05-23 stsp if (err) {
68 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
69 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
70 79c99a64 2019-05-23 stsp err = NULL;
71 704b89c4 2019-05-23 stsp return err;
72 79c99a64 2019-05-23 stsp }
73 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
74 704b89c4 2019-05-23 stsp return NULL;
75 704b89c4 2019-05-23 stsp }
76 704b89c4 2019-05-23 stsp
77 704b89c4 2019-05-23 stsp static const struct got_error *
78 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
79 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
80 876c234b 2018-09-10 stsp {
81 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
82 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
83 876c234b 2018-09-10 stsp struct got_object *obj;
84 106807b4 2018-09-15 stsp struct got_object_id id;
85 876c234b 2018-09-10 stsp size_t datalen;
86 876c234b 2018-09-10 stsp
87 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
88 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
89 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
90 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
91 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
92 876c234b 2018-09-10 stsp
93 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
94 704b89c4 2019-05-23 stsp if (obj) {
95 704b89c4 2019-05-23 stsp obj->refcnt++;
96 704b89c4 2019-05-23 stsp } else {
97 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
98 704b89c4 2019-05-23 stsp objcache);
99 704b89c4 2019-05-23 stsp if (err)
100 704b89c4 2019-05-23 stsp goto done;
101 704b89c4 2019-05-23 stsp }
102 876c234b 2018-09-10 stsp
103 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
104 c59b3346 2018-09-11 stsp done:
105 876c234b 2018-09-10 stsp got_object_close(obj);
106 876c234b 2018-09-10 stsp return err;
107 876c234b 2018-09-10 stsp }
108 876c234b 2018-09-10 stsp
109 ca6e02ac 2020-01-07 stsp const struct got_error *
110 ca6e02ac 2020-01-07 stsp open_commit(struct got_commit_object **commit, struct got_pack *pack,
111 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
112 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
113 876c234b 2018-09-10 stsp {
114 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
115 cb5e38fd 2019-05-23 stsp struct got_object *obj = NULL;
116 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
117 cfd633c2 2018-09-10 stsp size_t len;
118 cfd633c2 2018-09-10 stsp
119 ca6e02ac 2020-01-07 stsp *commit = NULL;
120 1785f84a 2018-12-23 stsp
121 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
122 704b89c4 2019-05-23 stsp if (obj) {
123 704b89c4 2019-05-23 stsp obj->refcnt++;
124 704b89c4 2019-05-23 stsp } else {
125 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
126 704b89c4 2019-05-23 stsp objcache);
127 704b89c4 2019-05-23 stsp if (err)
128 704b89c4 2019-05-23 stsp return err;
129 704b89c4 2019-05-23 stsp }
130 cfd633c2 2018-09-10 stsp
131 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
132 cfd633c2 2018-09-10 stsp if (err)
133 cb5e38fd 2019-05-23 stsp goto done;
134 cfd633c2 2018-09-10 stsp
135 cfd633c2 2018-09-10 stsp obj->size = len;
136 ca6e02ac 2020-01-07 stsp
137 ca6e02ac 2020-01-07 stsp err = got_object_parse_commit(commit, buf, len);
138 ca6e02ac 2020-01-07 stsp done:
139 ca6e02ac 2020-01-07 stsp got_object_close(obj);
140 ca6e02ac 2020-01-07 stsp free(buf);
141 ca6e02ac 2020-01-07 stsp return err;
142 ca6e02ac 2020-01-07 stsp }
143 ca6e02ac 2020-01-07 stsp
144 ca6e02ac 2020-01-07 stsp static const struct got_error *
145 ca6e02ac 2020-01-07 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
146 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
147 ca6e02ac 2020-01-07 stsp {
148 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
149 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
150 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL;
151 ca6e02ac 2020-01-07 stsp struct got_object_id id;
152 ca6e02ac 2020-01-07 stsp size_t datalen;
153 ca6e02ac 2020-01-07 stsp
154 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
155 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(iobj))
156 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
157 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
158 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
159 ca6e02ac 2020-01-07 stsp
160 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
161 cb5e38fd 2019-05-23 stsp if (err)
162 cb5e38fd 2019-05-23 stsp goto done;
163 cfd633c2 2018-09-10 stsp
164 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
165 cb5e38fd 2019-05-23 stsp done:
166 cb5e38fd 2019-05-23 stsp if (commit)
167 cb5e38fd 2019-05-23 stsp got_object_commit_close(commit);
168 7762fe12 2018-11-05 stsp if (err) {
169 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
170 7762fe12 2018-11-05 stsp err = NULL;
171 7762fe12 2018-11-05 stsp else
172 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
173 7762fe12 2018-11-05 stsp }
174 7762fe12 2018-11-05 stsp
175 7762fe12 2018-11-05 stsp return err;
176 7762fe12 2018-11-05 stsp }
177 7762fe12 2018-11-05 stsp
178 ca6e02ac 2020-01-07 stsp const struct got_error *
179 ca6e02ac 2020-01-07 stsp open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
180 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
181 ca6e02ac 2020-01-07 stsp struct got_object_id *id, struct got_object_cache *objcache)
182 ca6e02ac 2020-01-07 stsp {
183 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
184 ca6e02ac 2020-01-07 stsp struct got_object *obj = NULL;
185 ca6e02ac 2020-01-07 stsp size_t len;
186 ca6e02ac 2020-01-07 stsp
187 ca6e02ac 2020-01-07 stsp *buf = NULL;
188 ca6e02ac 2020-01-07 stsp *nentries = 0;
189 ca6e02ac 2020-01-07 stsp
190 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
191 ca6e02ac 2020-01-07 stsp if (obj) {
192 ca6e02ac 2020-01-07 stsp obj->refcnt++;
193 ca6e02ac 2020-01-07 stsp } else {
194 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
195 ca6e02ac 2020-01-07 stsp objcache);
196 ca6e02ac 2020-01-07 stsp if (err)
197 ca6e02ac 2020-01-07 stsp return err;
198 ca6e02ac 2020-01-07 stsp }
199 ca6e02ac 2020-01-07 stsp
200 ca6e02ac 2020-01-07 stsp err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
201 ca6e02ac 2020-01-07 stsp if (err)
202 ca6e02ac 2020-01-07 stsp goto done;
203 ca6e02ac 2020-01-07 stsp
204 ca6e02ac 2020-01-07 stsp obj->size = len;
205 ca6e02ac 2020-01-07 stsp
206 ca6e02ac 2020-01-07 stsp err = got_object_parse_tree(entries, nentries, *buf, len);
207 ca6e02ac 2020-01-07 stsp done:
208 ca6e02ac 2020-01-07 stsp got_object_close(obj);
209 ca6e02ac 2020-01-07 stsp if (err) {
210 ca6e02ac 2020-01-07 stsp free(*buf);
211 ca6e02ac 2020-01-07 stsp *buf = NULL;
212 ca6e02ac 2020-01-07 stsp }
213 ca6e02ac 2020-01-07 stsp return err;
214 ca6e02ac 2020-01-07 stsp }
215 ca6e02ac 2020-01-07 stsp
216 7762fe12 2018-11-05 stsp static const struct got_error *
217 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
218 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
219 876c234b 2018-09-10 stsp {
220 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
221 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
222 3022d272 2019-11-14 stsp struct got_pathlist_head entries;
223 3022d272 2019-11-14 stsp int nentries = 0;
224 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
225 13c729f7 2018-12-24 stsp struct got_object_id id;
226 13c729f7 2018-12-24 stsp size_t datalen;
227 e7885405 2018-09-10 stsp
228 3022d272 2019-11-14 stsp TAILQ_INIT(&entries);
229 3022d272 2019-11-14 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 3022d272 2019-11-14 stsp err = got_privsep_send_tree(ibuf, &entries, nentries);
242 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_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 3840f4c9 2018-09-12 stsp receive_file(FILE **f, struct imsgbuf *ibuf, int 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 3840f4c9 2018-09-12 stsp }
290 55da3778 2018-09-10 stsp
291 3840f4c9 2018-09-12 stsp static const struct got_error *
292 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
293 3840f4c9 2018-09-12 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
294 3840f4c9 2018-09-12 stsp {
295 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
296 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
297 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
298 3840f4c9 2018-09-12 stsp FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
299 ebc55e2d 2018-12-24 stsp struct got_object_id id;
300 ebc55e2d 2018-12-24 stsp size_t datalen;
301 ac544f8c 2019-01-13 stsp uint64_t blob_size;
302 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
303 3840f4c9 2018-09-12 stsp
304 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
305 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
306 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
307 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
308 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
309 ebc55e2d 2018-12-24 stsp
310 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
311 704b89c4 2019-05-23 stsp if (obj) {
312 704b89c4 2019-05-23 stsp obj->refcnt++;
313 704b89c4 2019-05-23 stsp } else {
314 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
315 704b89c4 2019-05-23 stsp objcache);
316 704b89c4 2019-05-23 stsp if (err)
317 704b89c4 2019-05-23 stsp return err;
318 704b89c4 2019-05-23 stsp }
319 3840f4c9 2018-09-12 stsp
320 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
321 3840f4c9 2018-09-12 stsp if (err)
322 ac544f8c 2019-01-13 stsp goto done;
323 3840f4c9 2018-09-12 stsp err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
324 3840f4c9 2018-09-12 stsp if (err)
325 ac544f8c 2019-01-13 stsp goto done;
326 3840f4c9 2018-09-12 stsp err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
327 3840f4c9 2018-09-12 stsp if (err)
328 ac544f8c 2019-01-13 stsp goto done;
329 3840f4c9 2018-09-12 stsp
330 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
331 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
332 ac544f8c 2019-01-13 stsp if (err)
333 ac544f8c 2019-01-13 stsp goto done;
334 ac544f8c 2019-01-13 stsp } else
335 ac544f8c 2019-01-13 stsp blob_size = obj->size;
336 ac544f8c 2019-01-13 stsp
337 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
338 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
339 ac544f8c 2019-01-13 stsp obj, pack);
340 ac544f8c 2019-01-13 stsp else
341 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
342 ac544f8c 2019-01-13 stsp accumfile);
343 3840f4c9 2018-09-12 stsp if (err)
344 55da3778 2018-09-10 stsp goto done;
345 55da3778 2018-09-10 stsp
346 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
347 55da3778 2018-09-10 stsp done:
348 ac544f8c 2019-01-13 stsp free(buf);
349 fb43ecf1 2019-02-11 stsp if (outfile && fclose(outfile) != 0 && err == NULL)
350 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
351 638f9024 2019-05-13 stsp if (basefile && fclose(basefile) != 0 && err == NULL)
352 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
353 fb43ecf1 2019-02-11 stsp if (accumfile && fclose(accumfile) != 0 && err == NULL)
354 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
355 cb5e38fd 2019-05-23 stsp got_object_close(obj);
356 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
357 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
358 55da3778 2018-09-10 stsp
359 55da3778 2018-09-10 stsp return err;
360 876c234b 2018-09-10 stsp }
361 876c234b 2018-09-10 stsp
362 876c234b 2018-09-10 stsp static const struct got_error *
363 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
364 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
365 f4a881ce 2018-11-17 stsp {
366 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
367 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
368 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
369 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
370 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
371 f4a881ce 2018-11-17 stsp size_t len;
372 268f7291 2018-12-24 stsp struct got_object_id id;
373 268f7291 2018-12-24 stsp size_t datalen;
374 f4a881ce 2018-11-17 stsp
375 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
376 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
377 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
378 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
379 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
380 268f7291 2018-12-24 stsp
381 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
382 704b89c4 2019-05-23 stsp if (obj) {
383 704b89c4 2019-05-23 stsp obj->refcnt++;
384 704b89c4 2019-05-23 stsp } else {
385 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
386 704b89c4 2019-05-23 stsp objcache);
387 704b89c4 2019-05-23 stsp if (err)
388 704b89c4 2019-05-23 stsp return err;
389 704b89c4 2019-05-23 stsp }
390 f4a881ce 2018-11-17 stsp
391 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
392 f4a881ce 2018-11-17 stsp if (err)
393 cb5e38fd 2019-05-23 stsp goto done;
394 f4a881ce 2018-11-17 stsp
395 f4a881ce 2018-11-17 stsp obj->size = len;
396 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
397 0ae4af15 2019-02-01 stsp if (err)
398 cb5e38fd 2019-05-23 stsp goto done;
399 f4a881ce 2018-11-17 stsp
400 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
401 cb5e38fd 2019-05-23 stsp done:
402 cb5e38fd 2019-05-23 stsp free(buf);
403 cb5e38fd 2019-05-23 stsp got_object_close(obj);
404 cb5e38fd 2019-05-23 stsp if (tag)
405 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
406 ca6e02ac 2020-01-07 stsp if (err) {
407 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
408 ca6e02ac 2020-01-07 stsp err = NULL;
409 ca6e02ac 2020-01-07 stsp else
410 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
411 ca6e02ac 2020-01-07 stsp }
412 ca6e02ac 2020-01-07 stsp
413 ca6e02ac 2020-01-07 stsp return err;
414 ca6e02ac 2020-01-07 stsp }
415 ca6e02ac 2020-01-07 stsp
416 ca6e02ac 2020-01-07 stsp static struct got_parsed_tree_entry *
417 ca6e02ac 2020-01-07 stsp find_entry_by_name(struct got_pathlist_head *entries, int nentries,
418 ca6e02ac 2020-01-07 stsp const char *name, size_t len)
419 ca6e02ac 2020-01-07 stsp {
420 ca6e02ac 2020-01-07 stsp struct got_pathlist_entry *pe;
421 ca6e02ac 2020-01-07 stsp
422 ca6e02ac 2020-01-07 stsp /* Note that tree entries are sorted in strncmp() order. */
423 ca6e02ac 2020-01-07 stsp TAILQ_FOREACH(pe, entries, entry) {
424 ca6e02ac 2020-01-07 stsp int cmp = strncmp(pe->path, name, len);
425 ca6e02ac 2020-01-07 stsp if (cmp < 0)
426 ca6e02ac 2020-01-07 stsp continue;
427 ca6e02ac 2020-01-07 stsp if (cmp > 0)
428 ca6e02ac 2020-01-07 stsp break;
429 ca6e02ac 2020-01-07 stsp if (pe->path[len] == '\0')
430 ca6e02ac 2020-01-07 stsp return (struct got_parsed_tree_entry *)pe->data;
431 ca6e02ac 2020-01-07 stsp }
432 ca6e02ac 2020-01-07 stsp return NULL;
433 ca6e02ac 2020-01-07 stsp }
434 ca6e02ac 2020-01-07 stsp
435 ca6e02ac 2020-01-07 stsp const struct got_error *
436 ca6e02ac 2020-01-07 stsp tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
437 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries1, int *nentries1,
438 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries2, int *nentries2,
439 ca6e02ac 2020-01-07 stsp const char *path, struct got_pack *pack, struct got_packidx *packidx,
440 ca6e02ac 2020-01-07 stsp struct imsgbuf *ibuf, struct got_object_cache *objcache)
441 ca6e02ac 2020-01-07 stsp {
442 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
443 ca6e02ac 2020-01-07 stsp struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
444 ca6e02ac 2020-01-07 stsp const char *seg, *s;
445 ca6e02ac 2020-01-07 stsp size_t seglen;
446 ca6e02ac 2020-01-07 stsp
447 ca6e02ac 2020-01-07 stsp *changed = 0;
448 ca6e02ac 2020-01-07 stsp
449 ca6e02ac 2020-01-07 stsp /* We not do support comparing the root path. */
450 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
451 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
452 ca6e02ac 2020-01-07 stsp
453 ca6e02ac 2020-01-07 stsp s = path;
454 61a7d79f 2020-02-29 stsp while (*s == '/')
455 61a7d79f 2020-02-29 stsp s++;
456 ca6e02ac 2020-01-07 stsp seg = s;
457 ca6e02ac 2020-01-07 stsp seglen = 0;
458 ca6e02ac 2020-01-07 stsp while (*s) {
459 ca6e02ac 2020-01-07 stsp if (*s != '/') {
460 ca6e02ac 2020-01-07 stsp s++;
461 ca6e02ac 2020-01-07 stsp seglen++;
462 ca6e02ac 2020-01-07 stsp if (*s)
463 ca6e02ac 2020-01-07 stsp continue;
464 ca6e02ac 2020-01-07 stsp }
465 ca6e02ac 2020-01-07 stsp
466 ca6e02ac 2020-01-07 stsp pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
467 ca6e02ac 2020-01-07 stsp if (pte1 == NULL) {
468 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_NO_OBJ);
469 ca6e02ac 2020-01-07 stsp break;
470 ca6e02ac 2020-01-07 stsp }
471 ca6e02ac 2020-01-07 stsp
472 ca6e02ac 2020-01-07 stsp pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
473 ca6e02ac 2020-01-07 stsp if (pte2 == NULL) {
474 ca6e02ac 2020-01-07 stsp *changed = 1;
475 ca6e02ac 2020-01-07 stsp break;
476 ca6e02ac 2020-01-07 stsp }
477 ca6e02ac 2020-01-07 stsp
478 ca6e02ac 2020-01-07 stsp if (pte1->mode != pte2->mode) {
479 ca6e02ac 2020-01-07 stsp *changed = 1;
480 ca6e02ac 2020-01-07 stsp break;
481 ca6e02ac 2020-01-07 stsp }
482 ca6e02ac 2020-01-07 stsp
483 ca6e02ac 2020-01-07 stsp if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
484 ca6e02ac 2020-01-07 stsp *changed = 0;
485 ca6e02ac 2020-01-07 stsp break;
486 ca6e02ac 2020-01-07 stsp }
487 ca6e02ac 2020-01-07 stsp
488 ca6e02ac 2020-01-07 stsp if (*s == '\0') { /* final path element */
489 ca6e02ac 2020-01-07 stsp *changed = 1;
490 ca6e02ac 2020-01-07 stsp break;
491 ca6e02ac 2020-01-07 stsp }
492 ca6e02ac 2020-01-07 stsp
493 ca6e02ac 2020-01-07 stsp seg = s + 1;
494 ca6e02ac 2020-01-07 stsp s++;
495 ca6e02ac 2020-01-07 stsp seglen = 0;
496 ca6e02ac 2020-01-07 stsp if (*s) {
497 ca6e02ac 2020-01-07 stsp struct got_object_id id1, id2;
498 ca6e02ac 2020-01-07 stsp int idx;
499 ca6e02ac 2020-01-07 stsp
500 ded8fbb8 2020-04-19 stsp memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
501 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id1);
502 ca6e02ac 2020-01-07 stsp if (idx == -1) {
503 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id1);
504 ca6e02ac 2020-01-07 stsp break;
505 ca6e02ac 2020-01-07 stsp }
506 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries1);
507 ca6e02ac 2020-01-07 stsp *nentries1 = 0;
508 ca6e02ac 2020-01-07 stsp free(*buf1);
509 ca6e02ac 2020-01-07 stsp *buf1 = NULL;
510 ca6e02ac 2020-01-07 stsp err = open_tree(buf1, entries1, nentries1, pack,
511 ca6e02ac 2020-01-07 stsp packidx, idx, &id1, objcache);
512 ca6e02ac 2020-01-07 stsp pte1 = NULL;
513 ca6e02ac 2020-01-07 stsp if (err)
514 ca6e02ac 2020-01-07 stsp break;
515 ca6e02ac 2020-01-07 stsp
516 ded8fbb8 2020-04-19 stsp memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
517 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id2);
518 ca6e02ac 2020-01-07 stsp if (idx == -1) {
519 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id2);
520 ca6e02ac 2020-01-07 stsp break;
521 ca6e02ac 2020-01-07 stsp }
522 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries2);
523 ca6e02ac 2020-01-07 stsp *nentries2 = 0;
524 ca6e02ac 2020-01-07 stsp free(*buf2);
525 ca6e02ac 2020-01-07 stsp *buf2 = NULL;
526 ca6e02ac 2020-01-07 stsp err = open_tree(buf2, entries2, nentries2, pack,
527 ca6e02ac 2020-01-07 stsp packidx, idx, &id2, objcache);
528 ca6e02ac 2020-01-07 stsp pte2 = NULL;
529 ca6e02ac 2020-01-07 stsp if (err)
530 ca6e02ac 2020-01-07 stsp break;
531 ca6e02ac 2020-01-07 stsp }
532 ca6e02ac 2020-01-07 stsp }
533 ca6e02ac 2020-01-07 stsp
534 ca6e02ac 2020-01-07 stsp return err;
535 ca6e02ac 2020-01-07 stsp }
536 ca6e02ac 2020-01-07 stsp
537 ca6e02ac 2020-01-07 stsp static const struct got_error *
538 e70bf110 2020-03-22 stsp send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
539 e70bf110 2020-03-22 stsp struct imsgbuf *ibuf)
540 e70bf110 2020-03-22 stsp {
541 e70bf110 2020-03-22 stsp const struct got_error *err;
542 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
543 e70bf110 2020-03-22 stsp int i;
544 e70bf110 2020-03-22 stsp
545 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
546 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
547 e70bf110 2020-03-22 stsp ncommits * SHA1_DIGEST_LENGTH);
548 e70bf110 2020-03-22 stsp if (wbuf == NULL)
549 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
550 e70bf110 2020-03-22 stsp
551 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
552 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
553 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
554 e70bf110 2020-03-22 stsp return err;
555 e70bf110 2020-03-22 stsp }
556 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
557 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
558 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
559 e70bf110 2020-03-22 stsp err = got_error_from_errno(
560 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
561 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
562 e70bf110 2020-03-22 stsp return err;
563 e70bf110 2020-03-22 stsp }
564 e70bf110 2020-03-22 stsp }
565 e70bf110 2020-03-22 stsp
566 e70bf110 2020-03-22 stsp wbuf->fd = -1;
567 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
568 e70bf110 2020-03-22 stsp
569 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
570 e70bf110 2020-03-22 stsp }
571 e70bf110 2020-03-22 stsp
572 e70bf110 2020-03-22 stsp static const struct got_error *
573 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
574 e70bf110 2020-03-22 stsp {
575 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
576 e70bf110 2020-03-22 stsp NULL, 0) == -1)
577 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
578 e70bf110 2020-03-22 stsp
579 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
580 e70bf110 2020-03-22 stsp }
581 e70bf110 2020-03-22 stsp
582 e70bf110 2020-03-22 stsp
583 e70bf110 2020-03-22 stsp static const struct got_error *
584 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
585 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
586 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
587 ca6e02ac 2020-01-07 stsp {
588 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
589 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
590 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
591 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
592 ca6e02ac 2020-01-07 stsp struct got_pathlist_head entries, pentries;
593 ca6e02ac 2020-01-07 stsp int nentries = 0, pnentries = 0;
594 ca6e02ac 2020-01-07 stsp struct got_object_id id;
595 ca6e02ac 2020-01-07 stsp size_t datalen, path_len;
596 ca6e02ac 2020-01-07 stsp char *path = NULL;
597 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
598 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
599 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
600 ca6e02ac 2020-01-07 stsp
601 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&entries);
602 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&pentries);
603 ca6e02ac 2020-01-07 stsp
604 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
605 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(iobj))
606 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
607 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
608 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
609 ca6e02ac 2020-01-07 stsp
610 ca6e02ac 2020-01-07 stsp path_len = datalen - sizeof(iobj) - 1;
611 ca6e02ac 2020-01-07 stsp if (path_len < 0)
612 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
613 ca6e02ac 2020-01-07 stsp if (path_len > 0) {
614 ca6e02ac 2020-01-07 stsp path = imsg->data + sizeof(iobj);
615 ca6e02ac 2020-01-07 stsp if (path[path_len] != '\0')
616 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
617 ca6e02ac 2020-01-07 stsp }
618 ca6e02ac 2020-01-07 stsp
619 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
620 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
621 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
622 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
623 ca6e02ac 2020-01-07 stsp
624 ca6e02ac 2020-01-07 stsp do {
625 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
626 ca6e02ac 2020-01-07 stsp int idx;
627 ca6e02ac 2020-01-07 stsp
628 ca6e02ac 2020-01-07 stsp if (sigint_received) {
629 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
630 ca6e02ac 2020-01-07 stsp goto done;
631 ca6e02ac 2020-01-07 stsp }
632 ca6e02ac 2020-01-07 stsp
633 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
634 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
635 ca6e02ac 2020-01-07 stsp if (idx == -1)
636 ca6e02ac 2020-01-07 stsp break;
637 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
638 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
639 ca6e02ac 2020-01-07 stsp if (err) {
640 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
641 ca6e02ac 2020-01-07 stsp goto done;
642 ca6e02ac 2020-01-07 stsp err = NULL;
643 ca6e02ac 2020-01-07 stsp break;
644 ca6e02ac 2020-01-07 stsp }
645 ca6e02ac 2020-01-07 stsp }
646 ca6e02ac 2020-01-07 stsp
647 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
648 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
649 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
650 e70bf110 2020-03-22 stsp ibuf);
651 ca6e02ac 2020-01-07 stsp if (err)
652 ca6e02ac 2020-01-07 stsp goto done;
653 ca6e02ac 2020-01-07 stsp ncommits = 0;
654 ca6e02ac 2020-01-07 stsp }
655 ca6e02ac 2020-01-07 stsp ncommits++;
656 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
657 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
658 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
659 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
660 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
661 ca6e02ac 2020-01-07 stsp if (new == NULL) {
662 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
663 ca6e02ac 2020-01-07 stsp goto done;
664 ca6e02ac 2020-01-07 stsp }
665 ca6e02ac 2020-01-07 stsp commit_ids = new;
666 ca6e02ac 2020-01-07 stsp }
667 ca6e02ac 2020-01-07 stsp memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
668 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
669 ca6e02ac 2020-01-07 stsp
670 ca6e02ac 2020-01-07 stsp pid = SIMPLEQ_FIRST(&commit->parent_ids);
671 ca6e02ac 2020-01-07 stsp if (pid == NULL)
672 ca6e02ac 2020-01-07 stsp break;
673 ca6e02ac 2020-01-07 stsp
674 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, pid->id);
675 ca6e02ac 2020-01-07 stsp if (idx == -1)
676 ca6e02ac 2020-01-07 stsp break;
677 ca6e02ac 2020-01-07 stsp
678 ca6e02ac 2020-01-07 stsp err = open_commit(&pcommit, pack, packidx, idx, pid->id,
679 ca6e02ac 2020-01-07 stsp objcache);
680 ca6e02ac 2020-01-07 stsp if (err) {
681 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
682 ca6e02ac 2020-01-07 stsp goto done;
683 ca6e02ac 2020-01-07 stsp err = NULL;
684 ca6e02ac 2020-01-07 stsp break;
685 ca6e02ac 2020-01-07 stsp }
686 ca6e02ac 2020-01-07 stsp
687 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
688 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
689 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
690 ca6e02ac 2020-01-07 stsp changed = 1;
691 ca6e02ac 2020-01-07 stsp break;
692 ca6e02ac 2020-01-07 stsp }
693 ca6e02ac 2020-01-07 stsp } else {
694 ca6e02ac 2020-01-07 stsp int pidx;
695 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
696 ca6e02ac 2020-01-07 stsp
697 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
698 ca6e02ac 2020-01-07 stsp commit->tree_id);
699 ca6e02ac 2020-01-07 stsp if (idx == -1)
700 ca6e02ac 2020-01-07 stsp break;
701 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
702 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
703 ca6e02ac 2020-01-07 stsp if (pidx == -1)
704 ca6e02ac 2020-01-07 stsp break;
705 ca6e02ac 2020-01-07 stsp
706 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack,
707 ca6e02ac 2020-01-07 stsp packidx, idx, commit->tree_id, objcache);
708 ca6e02ac 2020-01-07 stsp if (err)
709 ca6e02ac 2020-01-07 stsp goto done;
710 ca6e02ac 2020-01-07 stsp err = open_tree(&pbuf, &pentries, &pnentries, pack,
711 ca6e02ac 2020-01-07 stsp packidx, pidx, pcommit->tree_id, objcache);
712 ca6e02ac 2020-01-07 stsp if (err) {
713 ca6e02ac 2020-01-07 stsp free(buf);
714 ca6e02ac 2020-01-07 stsp goto done;
715 ca6e02ac 2020-01-07 stsp }
716 ca6e02ac 2020-01-07 stsp
717 ca6e02ac 2020-01-07 stsp err = tree_path_changed(&changed, &buf, &pbuf,
718 ca6e02ac 2020-01-07 stsp &entries, &nentries, &pentries, &pnentries, path,
719 ca6e02ac 2020-01-07 stsp pack, packidx, ibuf, objcache);
720 ca6e02ac 2020-01-07 stsp
721 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
722 ca6e02ac 2020-01-07 stsp nentries = 0;
723 ca6e02ac 2020-01-07 stsp free(buf);
724 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
725 ca6e02ac 2020-01-07 stsp pnentries = 0;
726 ca6e02ac 2020-01-07 stsp free(pbuf);
727 ca6e02ac 2020-01-07 stsp if (err) {
728 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
729 ca6e02ac 2020-01-07 stsp goto done;
730 ca6e02ac 2020-01-07 stsp err = NULL;
731 ca6e02ac 2020-01-07 stsp break;
732 ca6e02ac 2020-01-07 stsp }
733 ca6e02ac 2020-01-07 stsp }
734 ca6e02ac 2020-01-07 stsp
735 ca6e02ac 2020-01-07 stsp if (!changed) {
736 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
737 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
738 ca6e02ac 2020-01-07 stsp commit = pcommit;
739 ca6e02ac 2020-01-07 stsp pcommit = NULL;
740 ca6e02ac 2020-01-07 stsp }
741 ca6e02ac 2020-01-07 stsp } while (!changed);
742 ca6e02ac 2020-01-07 stsp
743 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
744 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
745 ca6e02ac 2020-01-07 stsp if (err)
746 ca6e02ac 2020-01-07 stsp goto done;
747 ca6e02ac 2020-01-07 stsp
748 ca6e02ac 2020-01-07 stsp if (changed) {
749 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
750 ca6e02ac 2020-01-07 stsp if (err)
751 ca6e02ac 2020-01-07 stsp goto done;
752 ca6e02ac 2020-01-07 stsp }
753 ca6e02ac 2020-01-07 stsp }
754 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
755 ca6e02ac 2020-01-07 stsp done:
756 ca6e02ac 2020-01-07 stsp free(commit_ids);
757 ca6e02ac 2020-01-07 stsp if (commit)
758 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
759 ca6e02ac 2020-01-07 stsp if (pcommit)
760 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
761 ca6e02ac 2020-01-07 stsp if (nentries != 0)
762 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
763 ca6e02ac 2020-01-07 stsp if (pnentries != 0)
764 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
765 f4a881ce 2018-11-17 stsp if (err) {
766 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
767 f4a881ce 2018-11-17 stsp err = NULL;
768 f4a881ce 2018-11-17 stsp else
769 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
770 f4a881ce 2018-11-17 stsp }
771 f4a881ce 2018-11-17 stsp
772 f4a881ce 2018-11-17 stsp return err;
773 f4a881ce 2018-11-17 stsp }
774 f4a881ce 2018-11-17 stsp
775 f4a881ce 2018-11-17 stsp static const struct got_error *
776 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
777 876c234b 2018-09-10 stsp {
778 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
779 876c234b 2018-09-10 stsp struct imsg imsg;
780 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
781 876c234b 2018-09-10 stsp size_t datalen;
782 876c234b 2018-09-10 stsp struct got_packidx *p;
783 876c234b 2018-09-10 stsp
784 876c234b 2018-09-10 stsp *packidx = NULL;
785 876c234b 2018-09-10 stsp
786 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
787 876c234b 2018-09-10 stsp if (err)
788 876c234b 2018-09-10 stsp return err;
789 876c234b 2018-09-10 stsp
790 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
791 876c234b 2018-09-10 stsp if (p == NULL) {
792 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
793 876c234b 2018-09-10 stsp goto done;
794 876c234b 2018-09-10 stsp }
795 876c234b 2018-09-10 stsp
796 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
797 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
798 876c234b 2018-09-10 stsp goto done;
799 876c234b 2018-09-10 stsp }
800 876c234b 2018-09-10 stsp
801 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
802 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
803 876c234b 2018-09-10 stsp goto done;
804 876c234b 2018-09-10 stsp }
805 876c234b 2018-09-10 stsp
806 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
807 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
808 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
809 876c234b 2018-09-10 stsp goto done;
810 876c234b 2018-09-10 stsp }
811 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
812 876c234b 2018-09-10 stsp
813 876c234b 2018-09-10 stsp p->len = ipackidx.len;
814 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
815 876c234b 2018-09-10 stsp if (p->fd == -1) {
816 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
817 56bef47a 2018-09-15 stsp goto done;
818 56bef47a 2018-09-15 stsp }
819 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
820 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
821 876c234b 2018-09-10 stsp goto done;
822 876c234b 2018-09-10 stsp }
823 876c234b 2018-09-10 stsp
824 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
825 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
826 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
827 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
828 876c234b 2018-09-10 stsp #endif
829 876c234b 2018-09-10 stsp err = got_packidx_init_hdr(p, 1);
830 876c234b 2018-09-10 stsp done:
831 876c234b 2018-09-10 stsp if (err) {
832 876c234b 2018-09-10 stsp if (imsg.fd != -1)
833 876c234b 2018-09-10 stsp close(imsg.fd);
834 876c234b 2018-09-10 stsp got_packidx_close(p);
835 876c234b 2018-09-10 stsp } else
836 876c234b 2018-09-10 stsp *packidx = p;
837 876c234b 2018-09-10 stsp imsg_free(&imsg);
838 876c234b 2018-09-10 stsp return err;
839 876c234b 2018-09-10 stsp }
840 876c234b 2018-09-10 stsp
841 876c234b 2018-09-10 stsp static const struct got_error *
842 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
843 876c234b 2018-09-10 stsp {
844 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
845 876c234b 2018-09-10 stsp struct imsg imsg;
846 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
847 876c234b 2018-09-10 stsp size_t datalen;
848 876c234b 2018-09-10 stsp struct got_pack *pack;
849 876c234b 2018-09-10 stsp
850 876c234b 2018-09-10 stsp *packp = NULL;
851 876c234b 2018-09-10 stsp
852 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
853 876c234b 2018-09-10 stsp if (err)
854 876c234b 2018-09-10 stsp return err;
855 876c234b 2018-09-10 stsp
856 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
857 876c234b 2018-09-10 stsp if (pack == NULL) {
858 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
859 876c234b 2018-09-10 stsp goto done;
860 876c234b 2018-09-10 stsp }
861 876c234b 2018-09-10 stsp
862 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
863 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
864 876c234b 2018-09-10 stsp goto done;
865 876c234b 2018-09-10 stsp }
866 876c234b 2018-09-10 stsp
867 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
868 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
869 876c234b 2018-09-10 stsp goto done;
870 876c234b 2018-09-10 stsp }
871 876c234b 2018-09-10 stsp
872 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
873 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
874 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
875 876c234b 2018-09-10 stsp goto done;
876 876c234b 2018-09-10 stsp }
877 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
878 876c234b 2018-09-10 stsp
879 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
880 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
881 876c234b 2018-09-10 stsp if (pack->fd == -1) {
882 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
883 876c234b 2018-09-10 stsp goto done;
884 876c234b 2018-09-10 stsp }
885 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
886 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
887 56bef47a 2018-09-15 stsp goto done;
888 56bef47a 2018-09-15 stsp }
889 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
890 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
891 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
892 ab2f42e7 2019-11-10 stsp goto done;
893 ab2f42e7 2019-11-10 stsp }
894 ab2f42e7 2019-11-10 stsp
895 ab2f42e7 2019-11-10 stsp pack->delta_cache = got_delta_cache_alloc(100,
896 ab2f42e7 2019-11-10 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
897 ab2f42e7 2019-11-10 stsp if (pack->delta_cache == NULL) {
898 ab2f42e7 2019-11-10 stsp err = got_error_from_errno("got_delta_cache_alloc");
899 876c234b 2018-09-10 stsp goto done;
900 876c234b 2018-09-10 stsp }
901 876c234b 2018-09-10 stsp
902 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
903 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
904 876c234b 2018-09-10 stsp pack->fd, 0);
905 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
906 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
907 876c234b 2018-09-10 stsp #endif
908 876c234b 2018-09-10 stsp done:
909 876c234b 2018-09-10 stsp if (err) {
910 876c234b 2018-09-10 stsp if (imsg.fd != -1)
911 876c234b 2018-09-10 stsp close(imsg.fd);
912 876c234b 2018-09-10 stsp free(pack);
913 876c234b 2018-09-10 stsp } else
914 876c234b 2018-09-10 stsp *packp = pack;
915 876c234b 2018-09-10 stsp imsg_free(&imsg);
916 876c234b 2018-09-10 stsp return err;
917 876c234b 2018-09-10 stsp }
918 876c234b 2018-09-10 stsp
919 876c234b 2018-09-10 stsp int
920 876c234b 2018-09-10 stsp main(int argc, char *argv[])
921 876c234b 2018-09-10 stsp {
922 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
923 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
924 876c234b 2018-09-10 stsp struct imsg imsg;
925 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
926 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
927 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
928 876c234b 2018-09-10 stsp
929 876c234b 2018-09-10 stsp //static int attached;
930 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
931 876c234b 2018-09-10 stsp
932 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
933 99437157 2018-11-11 stsp
934 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
935 876c234b 2018-09-10 stsp
936 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
937 c59b3346 2018-09-11 stsp if (err) {
938 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
939 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
940 c59b3346 2018-09-11 stsp return 1;
941 c59b3346 2018-09-11 stsp }
942 c59b3346 2018-09-11 stsp
943 2ff12563 2018-09-15 stsp #ifndef PROFILE
944 876c234b 2018-09-10 stsp /* revoke access to most system calls */
945 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
946 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
947 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
948 876c234b 2018-09-10 stsp return 1;
949 876c234b 2018-09-10 stsp }
950 2ff12563 2018-09-15 stsp #endif
951 876c234b 2018-09-10 stsp
952 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
953 876c234b 2018-09-10 stsp if (err) {
954 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
955 876c234b 2018-09-10 stsp return 1;
956 876c234b 2018-09-10 stsp }
957 876c234b 2018-09-10 stsp
958 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
959 876c234b 2018-09-10 stsp if (err) {
960 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
961 876c234b 2018-09-10 stsp return 1;
962 876c234b 2018-09-10 stsp }
963 876c234b 2018-09-10 stsp
964 656b1f76 2019-05-11 jcs for (;;) {
965 876c234b 2018-09-10 stsp imsg.fd = -1;
966 99437157 2018-11-11 stsp
967 99437157 2018-11-11 stsp if (sigint_received) {
968 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
969 99437157 2018-11-11 stsp break;
970 99437157 2018-11-11 stsp }
971 876c234b 2018-09-10 stsp
972 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
973 876c234b 2018-09-10 stsp if (err) {
974 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
975 876c234b 2018-09-10 stsp err = NULL;
976 876c234b 2018-09-10 stsp break;
977 876c234b 2018-09-10 stsp }
978 876c234b 2018-09-10 stsp
979 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
980 876c234b 2018-09-10 stsp break;
981 876c234b 2018-09-10 stsp
982 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
983 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
984 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
985 c59b3346 2018-09-11 stsp &objcache);
986 876c234b 2018-09-10 stsp break;
987 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
988 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
989 7762fe12 2018-11-05 stsp &objcache);
990 7762fe12 2018-11-05 stsp break;
991 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
992 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
993 62d463ca 2020-10-20 naddy &objcache);
994 876c234b 2018-09-10 stsp break;
995 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
996 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
997 62d463ca 2020-10-20 naddy &objcache);
998 876c234b 2018-09-10 stsp break;
999 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
1000 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
1001 62d463ca 2020-10-20 naddy &objcache);
1002 f4a881ce 2018-11-17 stsp break;
1003 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1004 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
1005 ca6e02ac 2020-01-07 stsp packidx, &objcache);
1006 ca6e02ac 2020-01-07 stsp break;
1007 876c234b 2018-09-10 stsp default:
1008 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1009 876c234b 2018-09-10 stsp break;
1010 876c234b 2018-09-10 stsp }
1011 876c234b 2018-09-10 stsp
1012 3a6ce05a 2019-02-11 stsp if (imsg.fd != -1 && close(imsg.fd) != 0 && err == NULL)
1013 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1014 876c234b 2018-09-10 stsp imsg_free(&imsg);
1015 99437157 2018-11-11 stsp if (err)
1016 876c234b 2018-09-10 stsp break;
1017 876c234b 2018-09-10 stsp }
1018 876c234b 2018-09-10 stsp
1019 c59b3346 2018-09-11 stsp if (packidx)
1020 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
1021 c59b3346 2018-09-11 stsp if (pack)
1022 c59b3346 2018-09-11 stsp got_pack_close(pack);
1023 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
1024 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
1025 99437157 2018-11-11 stsp if (err) {
1026 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1027 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1028 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
1029 80d5f134 2018-11-11 stsp }
1030 99437157 2018-11-11 stsp }
1031 3a6ce05a 2019-02-11 stsp if (close(GOT_IMSG_FD_CHILD) != 0 && err == NULL)
1032 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1033 876c234b 2018-09-10 stsp return err ? 1 : 0;
1034 876c234b 2018-09-10 stsp }