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