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 50127d69 2021-09-25 stsp static 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 50127d69 2021-09-25 stsp static 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 030daac8 2021-09-25 stsp receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
256 876c234b 2018-09-10 stsp {
257 3840f4c9 2018-09-12 stsp const struct got_error *err;
258 3840f4c9 2018-09-12 stsp struct imsg imsg;
259 55da3778 2018-09-10 stsp size_t datalen;
260 55da3778 2018-09-10 stsp
261 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
262 55da3778 2018-09-10 stsp if (err)
263 55da3778 2018-09-10 stsp return err;
264 55da3778 2018-09-10 stsp
265 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
266 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
267 55da3778 2018-09-10 stsp goto done;
268 55da3778 2018-09-10 stsp }
269 55da3778 2018-09-10 stsp
270 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
271 55da3778 2018-09-10 stsp if (datalen != 0) {
272 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
273 55da3778 2018-09-10 stsp goto done;
274 55da3778 2018-09-10 stsp }
275 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
276 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
277 55da3778 2018-09-10 stsp goto done;
278 55da3778 2018-09-10 stsp }
279 55da3778 2018-09-10 stsp
280 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
281 3840f4c9 2018-09-12 stsp if (*f == NULL) {
282 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
283 3a6ce05a 2019-02-11 stsp close(imsg.fd);
284 55da3778 2018-09-10 stsp goto done;
285 55da3778 2018-09-10 stsp }
286 3840f4c9 2018-09-12 stsp done:
287 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
288 3840f4c9 2018-09-12 stsp return err;
289 db696021 2022-01-04 stsp }
290 db696021 2022-01-04 stsp
291 db696021 2022-01-04 stsp static const struct got_error *
292 67fd6849 2022-02-13 stsp receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
293 db696021 2022-01-04 stsp struct imsgbuf *ibuf)
294 db696021 2022-01-04 stsp {
295 db696021 2022-01-04 stsp size_t datalen;
296 db696021 2022-01-04 stsp
297 db696021 2022-01-04 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
298 db696021 2022-01-04 stsp if (datalen != 0)
299 db696021 2022-01-04 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
300 db696021 2022-01-04 stsp
301 db696021 2022-01-04 stsp if (imsg->fd == -1)
302 db696021 2022-01-04 stsp return got_error(GOT_ERR_PRIVSEP_NO_FD);
303 db696021 2022-01-04 stsp
304 67fd6849 2022-02-13 stsp *f = fdopen(imsg->fd, mode);
305 db696021 2022-01-04 stsp if (*f == NULL)
306 db696021 2022-01-04 stsp return got_error_from_errno("fdopen");
307 db696021 2022-01-04 stsp imsg->fd = -1;
308 db696021 2022-01-04 stsp
309 db696021 2022-01-04 stsp return NULL;
310 3840f4c9 2018-09-12 stsp }
311 55da3778 2018-09-10 stsp
312 3840f4c9 2018-09-12 stsp static const struct got_error *
313 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
314 db696021 2022-01-04 stsp struct got_packidx *packidx, struct got_object_cache *objcache,
315 db696021 2022-01-04 stsp FILE *basefile, FILE *accumfile)
316 3840f4c9 2018-09-12 stsp {
317 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
318 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
319 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
320 db696021 2022-01-04 stsp FILE *outfile = NULL;
321 ebc55e2d 2018-12-24 stsp struct got_object_id id;
322 ebc55e2d 2018-12-24 stsp size_t datalen;
323 ac544f8c 2019-01-13 stsp uint64_t blob_size;
324 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
325 3840f4c9 2018-09-12 stsp
326 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
327 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
328 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
329 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
330 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
331 ebc55e2d 2018-12-24 stsp
332 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
333 704b89c4 2019-05-23 stsp if (obj) {
334 704b89c4 2019-05-23 stsp obj->refcnt++;
335 704b89c4 2019-05-23 stsp } else {
336 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
337 704b89c4 2019-05-23 stsp objcache);
338 704b89c4 2019-05-23 stsp if (err)
339 704b89c4 2019-05-23 stsp return err;
340 704b89c4 2019-05-23 stsp }
341 3840f4c9 2018-09-12 stsp
342 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
343 3840f4c9 2018-09-12 stsp if (err)
344 ac544f8c 2019-01-13 stsp goto done;
345 3840f4c9 2018-09-12 stsp
346 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
347 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
348 ac544f8c 2019-01-13 stsp if (err)
349 ac544f8c 2019-01-13 stsp goto done;
350 ac544f8c 2019-01-13 stsp } else
351 ac544f8c 2019-01-13 stsp blob_size = obj->size;
352 ac544f8c 2019-01-13 stsp
353 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
354 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
355 ac544f8c 2019-01-13 stsp obj, pack);
356 ac544f8c 2019-01-13 stsp else
357 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
358 ac544f8c 2019-01-13 stsp accumfile);
359 3840f4c9 2018-09-12 stsp if (err)
360 55da3778 2018-09-10 stsp goto done;
361 55da3778 2018-09-10 stsp
362 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
363 55da3778 2018-09-10 stsp done:
364 ac544f8c 2019-01-13 stsp free(buf);
365 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
366 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
367 cb5e38fd 2019-05-23 stsp got_object_close(obj);
368 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
369 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
370 55da3778 2018-09-10 stsp
371 55da3778 2018-09-10 stsp return err;
372 876c234b 2018-09-10 stsp }
373 876c234b 2018-09-10 stsp
374 876c234b 2018-09-10 stsp static const struct got_error *
375 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
376 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
377 f4a881ce 2018-11-17 stsp {
378 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
379 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
380 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
381 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
382 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
383 f4a881ce 2018-11-17 stsp size_t len;
384 268f7291 2018-12-24 stsp struct got_object_id id;
385 268f7291 2018-12-24 stsp size_t datalen;
386 f4a881ce 2018-11-17 stsp
387 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
388 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
389 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
390 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
391 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
392 268f7291 2018-12-24 stsp
393 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
394 704b89c4 2019-05-23 stsp if (obj) {
395 704b89c4 2019-05-23 stsp obj->refcnt++;
396 704b89c4 2019-05-23 stsp } else {
397 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
398 704b89c4 2019-05-23 stsp objcache);
399 704b89c4 2019-05-23 stsp if (err)
400 704b89c4 2019-05-23 stsp return err;
401 704b89c4 2019-05-23 stsp }
402 f4a881ce 2018-11-17 stsp
403 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
404 f4a881ce 2018-11-17 stsp if (err)
405 cb5e38fd 2019-05-23 stsp goto done;
406 f4a881ce 2018-11-17 stsp
407 f4a881ce 2018-11-17 stsp obj->size = len;
408 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
409 0ae4af15 2019-02-01 stsp if (err)
410 cb5e38fd 2019-05-23 stsp goto done;
411 f4a881ce 2018-11-17 stsp
412 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
413 cb5e38fd 2019-05-23 stsp done:
414 cb5e38fd 2019-05-23 stsp free(buf);
415 cb5e38fd 2019-05-23 stsp got_object_close(obj);
416 cb5e38fd 2019-05-23 stsp if (tag)
417 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
418 ca6e02ac 2020-01-07 stsp if (err) {
419 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
420 ca6e02ac 2020-01-07 stsp err = NULL;
421 ca6e02ac 2020-01-07 stsp else
422 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
423 ca6e02ac 2020-01-07 stsp }
424 ca6e02ac 2020-01-07 stsp
425 ca6e02ac 2020-01-07 stsp return err;
426 ca6e02ac 2020-01-07 stsp }
427 ca6e02ac 2020-01-07 stsp
428 ca6e02ac 2020-01-07 stsp static struct got_parsed_tree_entry *
429 ca6e02ac 2020-01-07 stsp find_entry_by_name(struct got_pathlist_head *entries, int nentries,
430 ca6e02ac 2020-01-07 stsp const char *name, size_t len)
431 ca6e02ac 2020-01-07 stsp {
432 ca6e02ac 2020-01-07 stsp struct got_pathlist_entry *pe;
433 ca6e02ac 2020-01-07 stsp
434 ca6e02ac 2020-01-07 stsp /* Note that tree entries are sorted in strncmp() order. */
435 ca6e02ac 2020-01-07 stsp TAILQ_FOREACH(pe, entries, entry) {
436 ca6e02ac 2020-01-07 stsp int cmp = strncmp(pe->path, name, len);
437 ca6e02ac 2020-01-07 stsp if (cmp < 0)
438 ca6e02ac 2020-01-07 stsp continue;
439 ca6e02ac 2020-01-07 stsp if (cmp > 0)
440 ca6e02ac 2020-01-07 stsp break;
441 ca6e02ac 2020-01-07 stsp if (pe->path[len] == '\0')
442 ca6e02ac 2020-01-07 stsp return (struct got_parsed_tree_entry *)pe->data;
443 ca6e02ac 2020-01-07 stsp }
444 ca6e02ac 2020-01-07 stsp return NULL;
445 ca6e02ac 2020-01-07 stsp }
446 ca6e02ac 2020-01-07 stsp
447 50127d69 2021-09-25 stsp static const struct got_error *
448 ca6e02ac 2020-01-07 stsp tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
449 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries1, int *nentries1,
450 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries2, int *nentries2,
451 ca6e02ac 2020-01-07 stsp const char *path, struct got_pack *pack, struct got_packidx *packidx,
452 ca6e02ac 2020-01-07 stsp struct imsgbuf *ibuf, struct got_object_cache *objcache)
453 ca6e02ac 2020-01-07 stsp {
454 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
455 ca6e02ac 2020-01-07 stsp struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
456 ca6e02ac 2020-01-07 stsp const char *seg, *s;
457 ca6e02ac 2020-01-07 stsp size_t seglen;
458 ca6e02ac 2020-01-07 stsp
459 ca6e02ac 2020-01-07 stsp *changed = 0;
460 ca6e02ac 2020-01-07 stsp
461 ca6e02ac 2020-01-07 stsp /* We not do support comparing the root path. */
462 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
463 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
464 ca6e02ac 2020-01-07 stsp
465 ca6e02ac 2020-01-07 stsp s = path;
466 61a7d79f 2020-02-29 stsp while (*s == '/')
467 61a7d79f 2020-02-29 stsp s++;
468 ca6e02ac 2020-01-07 stsp seg = s;
469 ca6e02ac 2020-01-07 stsp seglen = 0;
470 ca6e02ac 2020-01-07 stsp while (*s) {
471 ca6e02ac 2020-01-07 stsp if (*s != '/') {
472 ca6e02ac 2020-01-07 stsp s++;
473 ca6e02ac 2020-01-07 stsp seglen++;
474 ca6e02ac 2020-01-07 stsp if (*s)
475 ca6e02ac 2020-01-07 stsp continue;
476 ca6e02ac 2020-01-07 stsp }
477 ca6e02ac 2020-01-07 stsp
478 ca6e02ac 2020-01-07 stsp pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
479 ca6e02ac 2020-01-07 stsp if (pte1 == NULL) {
480 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_NO_OBJ);
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 pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
485 ca6e02ac 2020-01-07 stsp if (pte2 == NULL) {
486 ca6e02ac 2020-01-07 stsp *changed = 1;
487 ca6e02ac 2020-01-07 stsp break;
488 ca6e02ac 2020-01-07 stsp }
489 ca6e02ac 2020-01-07 stsp
490 ca6e02ac 2020-01-07 stsp if (pte1->mode != pte2->mode) {
491 ca6e02ac 2020-01-07 stsp *changed = 1;
492 ca6e02ac 2020-01-07 stsp break;
493 ca6e02ac 2020-01-07 stsp }
494 ca6e02ac 2020-01-07 stsp
495 ca6e02ac 2020-01-07 stsp if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
496 ca6e02ac 2020-01-07 stsp *changed = 0;
497 ca6e02ac 2020-01-07 stsp break;
498 ca6e02ac 2020-01-07 stsp }
499 ca6e02ac 2020-01-07 stsp
500 ca6e02ac 2020-01-07 stsp if (*s == '\0') { /* final path element */
501 ca6e02ac 2020-01-07 stsp *changed = 1;
502 ca6e02ac 2020-01-07 stsp break;
503 ca6e02ac 2020-01-07 stsp }
504 ca6e02ac 2020-01-07 stsp
505 ca6e02ac 2020-01-07 stsp seg = s + 1;
506 ca6e02ac 2020-01-07 stsp s++;
507 ca6e02ac 2020-01-07 stsp seglen = 0;
508 ca6e02ac 2020-01-07 stsp if (*s) {
509 ca6e02ac 2020-01-07 stsp struct got_object_id id1, id2;
510 ca6e02ac 2020-01-07 stsp int idx;
511 ca6e02ac 2020-01-07 stsp
512 ded8fbb8 2020-04-19 stsp memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
513 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id1);
514 ca6e02ac 2020-01-07 stsp if (idx == -1) {
515 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id1);
516 ca6e02ac 2020-01-07 stsp break;
517 ca6e02ac 2020-01-07 stsp }
518 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries1);
519 ca6e02ac 2020-01-07 stsp *nentries1 = 0;
520 ca6e02ac 2020-01-07 stsp free(*buf1);
521 ca6e02ac 2020-01-07 stsp *buf1 = NULL;
522 ca6e02ac 2020-01-07 stsp err = open_tree(buf1, entries1, nentries1, pack,
523 ca6e02ac 2020-01-07 stsp packidx, idx, &id1, objcache);
524 ca6e02ac 2020-01-07 stsp pte1 = NULL;
525 ca6e02ac 2020-01-07 stsp if (err)
526 ca6e02ac 2020-01-07 stsp break;
527 ca6e02ac 2020-01-07 stsp
528 ded8fbb8 2020-04-19 stsp memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
529 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id2);
530 ca6e02ac 2020-01-07 stsp if (idx == -1) {
531 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id2);
532 ca6e02ac 2020-01-07 stsp break;
533 ca6e02ac 2020-01-07 stsp }
534 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries2);
535 ca6e02ac 2020-01-07 stsp *nentries2 = 0;
536 ca6e02ac 2020-01-07 stsp free(*buf2);
537 ca6e02ac 2020-01-07 stsp *buf2 = NULL;
538 ca6e02ac 2020-01-07 stsp err = open_tree(buf2, entries2, nentries2, pack,
539 ca6e02ac 2020-01-07 stsp packidx, idx, &id2, objcache);
540 ca6e02ac 2020-01-07 stsp pte2 = NULL;
541 ca6e02ac 2020-01-07 stsp if (err)
542 ca6e02ac 2020-01-07 stsp break;
543 ca6e02ac 2020-01-07 stsp }
544 ca6e02ac 2020-01-07 stsp }
545 ca6e02ac 2020-01-07 stsp
546 ca6e02ac 2020-01-07 stsp return err;
547 ca6e02ac 2020-01-07 stsp }
548 ca6e02ac 2020-01-07 stsp
549 ca6e02ac 2020-01-07 stsp static const struct got_error *
550 e70bf110 2020-03-22 stsp send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
551 e70bf110 2020-03-22 stsp struct imsgbuf *ibuf)
552 e70bf110 2020-03-22 stsp {
553 e70bf110 2020-03-22 stsp const struct got_error *err;
554 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
555 030daac8 2021-09-25 stsp size_t i;
556 e70bf110 2020-03-22 stsp
557 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
558 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
559 e70bf110 2020-03-22 stsp ncommits * SHA1_DIGEST_LENGTH);
560 e70bf110 2020-03-22 stsp if (wbuf == NULL)
561 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
562 e70bf110 2020-03-22 stsp
563 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
564 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
565 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
566 e70bf110 2020-03-22 stsp return err;
567 e70bf110 2020-03-22 stsp }
568 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
569 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
570 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
571 e70bf110 2020-03-22 stsp err = got_error_from_errno(
572 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
573 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
574 e70bf110 2020-03-22 stsp return err;
575 e70bf110 2020-03-22 stsp }
576 e70bf110 2020-03-22 stsp }
577 e70bf110 2020-03-22 stsp
578 e70bf110 2020-03-22 stsp wbuf->fd = -1;
579 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
580 e70bf110 2020-03-22 stsp
581 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
582 e70bf110 2020-03-22 stsp }
583 e70bf110 2020-03-22 stsp
584 e70bf110 2020-03-22 stsp static const struct got_error *
585 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
586 e70bf110 2020-03-22 stsp {
587 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
588 e70bf110 2020-03-22 stsp NULL, 0) == -1)
589 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
590 e70bf110 2020-03-22 stsp
591 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
592 e70bf110 2020-03-22 stsp }
593 e70bf110 2020-03-22 stsp
594 e70bf110 2020-03-22 stsp
595 e70bf110 2020-03-22 stsp static const struct got_error *
596 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
597 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
598 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
599 ca6e02ac 2020-01-07 stsp {
600 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
601 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
602 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
603 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
604 ca6e02ac 2020-01-07 stsp struct got_pathlist_head entries, pentries;
605 ca6e02ac 2020-01-07 stsp int nentries = 0, pnentries = 0;
606 ca6e02ac 2020-01-07 stsp struct got_object_id id;
607 ca6e02ac 2020-01-07 stsp size_t datalen, path_len;
608 ca6e02ac 2020-01-07 stsp char *path = NULL;
609 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
610 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
611 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
612 ca6e02ac 2020-01-07 stsp
613 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&entries);
614 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&pentries);
615 ca6e02ac 2020-01-07 stsp
616 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
617 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(iobj))
618 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
619 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
620 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
621 ca6e02ac 2020-01-07 stsp
622 ca6e02ac 2020-01-07 stsp path_len = datalen - sizeof(iobj) - 1;
623 ca6e02ac 2020-01-07 stsp if (path_len < 0)
624 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
625 ca6e02ac 2020-01-07 stsp if (path_len > 0) {
626 ca6e02ac 2020-01-07 stsp path = imsg->data + sizeof(iobj);
627 ca6e02ac 2020-01-07 stsp if (path[path_len] != '\0')
628 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
629 ca6e02ac 2020-01-07 stsp }
630 ca6e02ac 2020-01-07 stsp
631 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
632 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
633 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
634 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
635 ca6e02ac 2020-01-07 stsp
636 ca6e02ac 2020-01-07 stsp do {
637 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
638 ca6e02ac 2020-01-07 stsp int idx;
639 ca6e02ac 2020-01-07 stsp
640 ca6e02ac 2020-01-07 stsp if (sigint_received) {
641 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
642 ca6e02ac 2020-01-07 stsp goto done;
643 ca6e02ac 2020-01-07 stsp }
644 ca6e02ac 2020-01-07 stsp
645 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
646 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
647 ca6e02ac 2020-01-07 stsp if (idx == -1)
648 ca6e02ac 2020-01-07 stsp break;
649 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
650 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
651 ca6e02ac 2020-01-07 stsp if (err) {
652 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
653 ca6e02ac 2020-01-07 stsp goto done;
654 ca6e02ac 2020-01-07 stsp err = NULL;
655 ca6e02ac 2020-01-07 stsp break;
656 ca6e02ac 2020-01-07 stsp }
657 ca6e02ac 2020-01-07 stsp }
658 ca6e02ac 2020-01-07 stsp
659 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
660 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
661 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
662 e70bf110 2020-03-22 stsp ibuf);
663 ca6e02ac 2020-01-07 stsp if (err)
664 ca6e02ac 2020-01-07 stsp goto done;
665 ca6e02ac 2020-01-07 stsp ncommits = 0;
666 ca6e02ac 2020-01-07 stsp }
667 ca6e02ac 2020-01-07 stsp ncommits++;
668 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
669 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
670 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
671 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
672 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
673 ca6e02ac 2020-01-07 stsp if (new == NULL) {
674 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
675 ca6e02ac 2020-01-07 stsp goto done;
676 ca6e02ac 2020-01-07 stsp }
677 ca6e02ac 2020-01-07 stsp commit_ids = new;
678 ca6e02ac 2020-01-07 stsp }
679 ca6e02ac 2020-01-07 stsp memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
680 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
681 ca6e02ac 2020-01-07 stsp
682 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
683 ca6e02ac 2020-01-07 stsp if (pid == NULL)
684 ca6e02ac 2020-01-07 stsp break;
685 ca6e02ac 2020-01-07 stsp
686 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, pid->id);
687 ca6e02ac 2020-01-07 stsp if (idx == -1)
688 ca6e02ac 2020-01-07 stsp break;
689 ca6e02ac 2020-01-07 stsp
690 ca6e02ac 2020-01-07 stsp err = open_commit(&pcommit, pack, packidx, idx, pid->id,
691 ca6e02ac 2020-01-07 stsp objcache);
692 ca6e02ac 2020-01-07 stsp if (err) {
693 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
694 ca6e02ac 2020-01-07 stsp goto done;
695 ca6e02ac 2020-01-07 stsp err = NULL;
696 ca6e02ac 2020-01-07 stsp break;
697 ca6e02ac 2020-01-07 stsp }
698 ca6e02ac 2020-01-07 stsp
699 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
700 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
701 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
702 ca6e02ac 2020-01-07 stsp changed = 1;
703 ca6e02ac 2020-01-07 stsp break;
704 ca6e02ac 2020-01-07 stsp }
705 ca6e02ac 2020-01-07 stsp } else {
706 ca6e02ac 2020-01-07 stsp int pidx;
707 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
708 ca6e02ac 2020-01-07 stsp
709 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
710 ca6e02ac 2020-01-07 stsp commit->tree_id);
711 ca6e02ac 2020-01-07 stsp if (idx == -1)
712 ca6e02ac 2020-01-07 stsp break;
713 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
714 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
715 ca6e02ac 2020-01-07 stsp if (pidx == -1)
716 ca6e02ac 2020-01-07 stsp break;
717 ca6e02ac 2020-01-07 stsp
718 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack,
719 ca6e02ac 2020-01-07 stsp packidx, idx, commit->tree_id, objcache);
720 ca6e02ac 2020-01-07 stsp if (err)
721 ca6e02ac 2020-01-07 stsp goto done;
722 ca6e02ac 2020-01-07 stsp err = open_tree(&pbuf, &pentries, &pnentries, pack,
723 ca6e02ac 2020-01-07 stsp packidx, pidx, pcommit->tree_id, objcache);
724 ca6e02ac 2020-01-07 stsp if (err) {
725 ca6e02ac 2020-01-07 stsp free(buf);
726 ca6e02ac 2020-01-07 stsp goto done;
727 ca6e02ac 2020-01-07 stsp }
728 ca6e02ac 2020-01-07 stsp
729 ca6e02ac 2020-01-07 stsp err = tree_path_changed(&changed, &buf, &pbuf,
730 ca6e02ac 2020-01-07 stsp &entries, &nentries, &pentries, &pnentries, path,
731 ca6e02ac 2020-01-07 stsp pack, packidx, ibuf, objcache);
732 ca6e02ac 2020-01-07 stsp
733 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
734 ca6e02ac 2020-01-07 stsp nentries = 0;
735 ca6e02ac 2020-01-07 stsp free(buf);
736 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
737 ca6e02ac 2020-01-07 stsp pnentries = 0;
738 ca6e02ac 2020-01-07 stsp free(pbuf);
739 ca6e02ac 2020-01-07 stsp if (err) {
740 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
741 ca6e02ac 2020-01-07 stsp goto done;
742 ca6e02ac 2020-01-07 stsp err = NULL;
743 ca6e02ac 2020-01-07 stsp break;
744 ca6e02ac 2020-01-07 stsp }
745 ca6e02ac 2020-01-07 stsp }
746 ca6e02ac 2020-01-07 stsp
747 ca6e02ac 2020-01-07 stsp if (!changed) {
748 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
749 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
750 ca6e02ac 2020-01-07 stsp commit = pcommit;
751 ca6e02ac 2020-01-07 stsp pcommit = NULL;
752 ca6e02ac 2020-01-07 stsp }
753 ca6e02ac 2020-01-07 stsp } while (!changed);
754 ca6e02ac 2020-01-07 stsp
755 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
756 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
757 ca6e02ac 2020-01-07 stsp if (err)
758 ca6e02ac 2020-01-07 stsp goto done;
759 ca6e02ac 2020-01-07 stsp
760 ca6e02ac 2020-01-07 stsp if (changed) {
761 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
762 ca6e02ac 2020-01-07 stsp if (err)
763 ca6e02ac 2020-01-07 stsp goto done;
764 ca6e02ac 2020-01-07 stsp }
765 ca6e02ac 2020-01-07 stsp }
766 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
767 ca6e02ac 2020-01-07 stsp done:
768 ca6e02ac 2020-01-07 stsp free(commit_ids);
769 ca6e02ac 2020-01-07 stsp if (commit)
770 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
771 ca6e02ac 2020-01-07 stsp if (pcommit)
772 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
773 ca6e02ac 2020-01-07 stsp if (nentries != 0)
774 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
775 ca6e02ac 2020-01-07 stsp if (pnentries != 0)
776 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
777 f4a881ce 2018-11-17 stsp if (err) {
778 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
779 f4a881ce 2018-11-17 stsp err = NULL;
780 f4a881ce 2018-11-17 stsp else
781 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
782 f4a881ce 2018-11-17 stsp }
783 f4a881ce 2018-11-17 stsp
784 f4a881ce 2018-11-17 stsp return err;
785 f4a881ce 2018-11-17 stsp }
786 f4a881ce 2018-11-17 stsp
787 f4a881ce 2018-11-17 stsp static const struct got_error *
788 c0df5966 2021-12-31 stsp raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
789 c0df5966 2021-12-31 stsp struct got_pack *pack, struct got_packidx *packidx,
790 db696021 2022-01-04 stsp struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
791 59d1e4a0 2021-03-10 stsp {
792 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
793 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
794 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
795 db696021 2022-01-04 stsp FILE *outfile = NULL;
796 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
797 59d1e4a0 2021-03-10 stsp struct got_object *obj;
798 59d1e4a0 2021-03-10 stsp struct got_object_id id;
799 59d1e4a0 2021-03-10 stsp size_t datalen;
800 59d1e4a0 2021-03-10 stsp
801 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
802 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
803 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
804 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
805 59d1e4a0 2021-03-10 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
806 59d1e4a0 2021-03-10 stsp
807 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
808 59d1e4a0 2021-03-10 stsp if (obj) {
809 59d1e4a0 2021-03-10 stsp obj->refcnt++;
810 59d1e4a0 2021-03-10 stsp } else {
811 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
812 59d1e4a0 2021-03-10 stsp objcache);
813 59d1e4a0 2021-03-10 stsp if (err)
814 59d1e4a0 2021-03-10 stsp return err;
815 59d1e4a0 2021-03-10 stsp }
816 59d1e4a0 2021-03-10 stsp
817 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
818 59d1e4a0 2021-03-10 stsp if (err)
819 59d1e4a0 2021-03-10 stsp return err;
820 59d1e4a0 2021-03-10 stsp
821 59d1e4a0 2021-03-10 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
822 59d1e4a0 2021-03-10 stsp err = got_pack_get_max_delta_object_size(&size, obj, pack);
823 59d1e4a0 2021-03-10 stsp if (err)
824 59d1e4a0 2021-03-10 stsp goto done;
825 59d1e4a0 2021-03-10 stsp } else
826 59d1e4a0 2021-03-10 stsp size = obj->size;
827 59d1e4a0 2021-03-10 stsp
828 59d1e4a0 2021-03-10 stsp if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
829 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
830 59d1e4a0 2021-03-10 stsp obj, pack);
831 59d1e4a0 2021-03-10 stsp else
832 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
833 59d1e4a0 2021-03-10 stsp accumfile);
834 59d1e4a0 2021-03-10 stsp if (err)
835 59d1e4a0 2021-03-10 stsp goto done;
836 59d1e4a0 2021-03-10 stsp
837 40e3cb72 2021-06-22 stsp err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
838 59d1e4a0 2021-03-10 stsp done:
839 59d1e4a0 2021-03-10 stsp free(buf);
840 59d1e4a0 2021-03-10 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
841 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
842 59d1e4a0 2021-03-10 stsp got_object_close(obj);
843 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
844 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
845 59d1e4a0 2021-03-10 stsp
846 59d1e4a0 2021-03-10 stsp return err;
847 59d1e4a0 2021-03-10 stsp }
848 67fd6849 2022-02-13 stsp
849 67fd6849 2022-02-13 stsp static const struct got_error *
850 67fd6849 2022-02-13 stsp get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
851 67fd6849 2022-02-13 stsp off_t base_offset)
852 67fd6849 2022-02-13 stsp {
853 67fd6849 2022-02-13 stsp const struct got_error *err;
854 67fd6849 2022-02-13 stsp int idx;
855 59d1e4a0 2021-03-10 stsp
856 67fd6849 2022-02-13 stsp err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
857 67fd6849 2022-02-13 stsp if (err)
858 67fd6849 2022-02-13 stsp return err;
859 67fd6849 2022-02-13 stsp if (idx == -1)
860 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_BAD_PACKIDX);
861 59d1e4a0 2021-03-10 stsp
862 67fd6849 2022-02-13 stsp return got_packidx_get_object_id(base_id, packidx, idx);
863 67fd6849 2022-02-13 stsp }
864 59d1e4a0 2021-03-10 stsp
865 59d1e4a0 2021-03-10 stsp static const struct got_error *
866 67fd6849 2022-02-13 stsp raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
867 67fd6849 2022-02-13 stsp FILE *delta_outfile, struct got_pack *pack,
868 67fd6849 2022-02-13 stsp struct got_packidx *packidx)
869 67fd6849 2022-02-13 stsp {
870 67fd6849 2022-02-13 stsp const struct got_error *err = NULL;
871 67fd6849 2022-02-13 stsp struct got_imsg_raw_delta_request req;
872 67fd6849 2022-02-13 stsp size_t datalen, delta_size;
873 67fd6849 2022-02-13 stsp off_t delta_offset;
874 67fd6849 2022-02-13 stsp uint8_t *delta_buf = NULL;
875 67fd6849 2022-02-13 stsp struct got_object_id id, base_id;
876 67fd6849 2022-02-13 stsp off_t base_offset, delta_out_offset = 0;
877 67fd6849 2022-02-13 stsp uint64_t base_size = 0, result_size = 0;
878 67fd6849 2022-02-13 stsp size_t w;
879 67fd6849 2022-02-13 stsp
880 67fd6849 2022-02-13 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
881 67fd6849 2022-02-13 stsp if (datalen != sizeof(req))
882 67fd6849 2022-02-13 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
883 67fd6849 2022-02-13 stsp memcpy(&req, imsg->data, sizeof(req));
884 67fd6849 2022-02-13 stsp memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
885 67fd6849 2022-02-13 stsp
886 67fd6849 2022-02-13 stsp imsg->fd = -1;
887 67fd6849 2022-02-13 stsp
888 67fd6849 2022-02-13 stsp err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
889 67fd6849 2022-02-13 stsp &delta_offset, &base_offset, &base_id, &base_size, &result_size,
890 67fd6849 2022-02-13 stsp pack, packidx, req.idx);
891 67fd6849 2022-02-13 stsp if (err)
892 67fd6849 2022-02-13 stsp goto done;
893 67fd6849 2022-02-13 stsp
894 67fd6849 2022-02-13 stsp /*
895 67fd6849 2022-02-13 stsp * If this is an offset delta we must determine the base
896 67fd6849 2022-02-13 stsp * object ID ourselves.
897 67fd6849 2022-02-13 stsp */
898 67fd6849 2022-02-13 stsp if (base_offset != 0) {
899 67fd6849 2022-02-13 stsp err = get_base_object_id(&base_id, packidx, base_offset);
900 67fd6849 2022-02-13 stsp if (err)
901 67fd6849 2022-02-13 stsp goto done;
902 67fd6849 2022-02-13 stsp }
903 67fd6849 2022-02-13 stsp
904 67fd6849 2022-02-13 stsp delta_out_offset = ftello(delta_outfile);
905 67fd6849 2022-02-13 stsp w = fwrite(delta_buf, 1, delta_size, delta_outfile);
906 67fd6849 2022-02-13 stsp if (w != delta_size) {
907 67fd6849 2022-02-13 stsp err = got_ferror(delta_outfile, GOT_ERR_IO);
908 67fd6849 2022-02-13 stsp goto done;
909 67fd6849 2022-02-13 stsp }
910 67fd6849 2022-02-13 stsp if (fflush(delta_outfile) == -1) {
911 67fd6849 2022-02-13 stsp err = got_error_from_errno("fflush");
912 67fd6849 2022-02-13 stsp goto done;
913 67fd6849 2022-02-13 stsp }
914 67fd6849 2022-02-13 stsp
915 67fd6849 2022-02-13 stsp err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
916 67fd6849 2022-02-13 stsp delta_size, delta_offset, delta_out_offset, &base_id);
917 67fd6849 2022-02-13 stsp done:
918 67fd6849 2022-02-13 stsp free(delta_buf);
919 67fd6849 2022-02-13 stsp return err;
920 67fd6849 2022-02-13 stsp }
921 67fd6849 2022-02-13 stsp
922 67fd6849 2022-02-13 stsp static const struct got_error *
923 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
924 876c234b 2018-09-10 stsp {
925 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
926 876c234b 2018-09-10 stsp struct imsg imsg;
927 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
928 876c234b 2018-09-10 stsp size_t datalen;
929 876c234b 2018-09-10 stsp struct got_packidx *p;
930 876c234b 2018-09-10 stsp
931 876c234b 2018-09-10 stsp *packidx = NULL;
932 876c234b 2018-09-10 stsp
933 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
934 876c234b 2018-09-10 stsp if (err)
935 876c234b 2018-09-10 stsp return err;
936 876c234b 2018-09-10 stsp
937 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
938 876c234b 2018-09-10 stsp if (p == NULL) {
939 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
940 876c234b 2018-09-10 stsp goto done;
941 876c234b 2018-09-10 stsp }
942 876c234b 2018-09-10 stsp
943 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
944 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
945 876c234b 2018-09-10 stsp goto done;
946 876c234b 2018-09-10 stsp }
947 876c234b 2018-09-10 stsp
948 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
949 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
950 876c234b 2018-09-10 stsp goto done;
951 876c234b 2018-09-10 stsp }
952 876c234b 2018-09-10 stsp
953 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
954 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
955 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
956 876c234b 2018-09-10 stsp goto done;
957 876c234b 2018-09-10 stsp }
958 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
959 876c234b 2018-09-10 stsp
960 876c234b 2018-09-10 stsp p->len = ipackidx.len;
961 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
962 876c234b 2018-09-10 stsp if (p->fd == -1) {
963 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
964 56bef47a 2018-09-15 stsp goto done;
965 56bef47a 2018-09-15 stsp }
966 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
967 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
968 876c234b 2018-09-10 stsp goto done;
969 876c234b 2018-09-10 stsp }
970 876c234b 2018-09-10 stsp
971 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
972 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
973 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
974 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
975 876c234b 2018-09-10 stsp #endif
976 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
977 876c234b 2018-09-10 stsp done:
978 876c234b 2018-09-10 stsp if (err) {
979 876c234b 2018-09-10 stsp if (imsg.fd != -1)
980 876c234b 2018-09-10 stsp close(imsg.fd);
981 876c234b 2018-09-10 stsp got_packidx_close(p);
982 876c234b 2018-09-10 stsp } else
983 876c234b 2018-09-10 stsp *packidx = p;
984 876c234b 2018-09-10 stsp imsg_free(&imsg);
985 876c234b 2018-09-10 stsp return err;
986 876c234b 2018-09-10 stsp }
987 876c234b 2018-09-10 stsp
988 876c234b 2018-09-10 stsp static const struct got_error *
989 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
990 876c234b 2018-09-10 stsp {
991 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
992 876c234b 2018-09-10 stsp struct imsg imsg;
993 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
994 876c234b 2018-09-10 stsp size_t datalen;
995 876c234b 2018-09-10 stsp struct got_pack *pack;
996 876c234b 2018-09-10 stsp
997 876c234b 2018-09-10 stsp *packp = NULL;
998 876c234b 2018-09-10 stsp
999 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1000 876c234b 2018-09-10 stsp if (err)
1001 876c234b 2018-09-10 stsp return err;
1002 876c234b 2018-09-10 stsp
1003 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
1004 876c234b 2018-09-10 stsp if (pack == NULL) {
1005 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1006 876c234b 2018-09-10 stsp goto done;
1007 876c234b 2018-09-10 stsp }
1008 876c234b 2018-09-10 stsp
1009 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
1010 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1011 876c234b 2018-09-10 stsp goto done;
1012 876c234b 2018-09-10 stsp }
1013 876c234b 2018-09-10 stsp
1014 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1015 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1016 876c234b 2018-09-10 stsp goto done;
1017 876c234b 2018-09-10 stsp }
1018 876c234b 2018-09-10 stsp
1019 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1020 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
1021 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1022 876c234b 2018-09-10 stsp goto done;
1023 876c234b 2018-09-10 stsp }
1024 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
1025 876c234b 2018-09-10 stsp
1026 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
1027 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
1028 876c234b 2018-09-10 stsp if (pack->fd == -1) {
1029 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1030 876c234b 2018-09-10 stsp goto done;
1031 876c234b 2018-09-10 stsp }
1032 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1033 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1034 56bef47a 2018-09-15 stsp goto done;
1035 56bef47a 2018-09-15 stsp }
1036 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
1037 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
1038 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1039 ab2f42e7 2019-11-10 stsp goto done;
1040 ab2f42e7 2019-11-10 stsp }
1041 ab2f42e7 2019-11-10 stsp
1042 ab2f42e7 2019-11-10 stsp pack->delta_cache = got_delta_cache_alloc(100,
1043 ab2f42e7 2019-11-10 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
1044 ab2f42e7 2019-11-10 stsp if (pack->delta_cache == NULL) {
1045 ab2f42e7 2019-11-10 stsp err = got_error_from_errno("got_delta_cache_alloc");
1046 876c234b 2018-09-10 stsp goto done;
1047 876c234b 2018-09-10 stsp }
1048 876c234b 2018-09-10 stsp
1049 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1050 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1051 876c234b 2018-09-10 stsp pack->fd, 0);
1052 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
1053 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
1054 876c234b 2018-09-10 stsp #endif
1055 876c234b 2018-09-10 stsp done:
1056 876c234b 2018-09-10 stsp if (err) {
1057 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1058 876c234b 2018-09-10 stsp close(imsg.fd);
1059 876c234b 2018-09-10 stsp free(pack);
1060 876c234b 2018-09-10 stsp } else
1061 876c234b 2018-09-10 stsp *packp = pack;
1062 876c234b 2018-09-10 stsp imsg_free(&imsg);
1063 876c234b 2018-09-10 stsp return err;
1064 876c234b 2018-09-10 stsp }
1065 876c234b 2018-09-10 stsp
1066 876c234b 2018-09-10 stsp int
1067 876c234b 2018-09-10 stsp main(int argc, char *argv[])
1068 876c234b 2018-09-10 stsp {
1069 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1070 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
1071 876c234b 2018-09-10 stsp struct imsg imsg;
1072 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
1073 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
1074 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1075 67fd6849 2022-02-13 stsp FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1076 876c234b 2018-09-10 stsp
1077 876c234b 2018-09-10 stsp //static int attached;
1078 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1079 876c234b 2018-09-10 stsp
1080 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1081 99437157 2018-11-11 stsp
1082 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1083 876c234b 2018-09-10 stsp
1084 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1085 c59b3346 2018-09-11 stsp if (err) {
1086 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1087 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1088 c59b3346 2018-09-11 stsp return 1;
1089 c59b3346 2018-09-11 stsp }
1090 c59b3346 2018-09-11 stsp
1091 2ff12563 2018-09-15 stsp #ifndef PROFILE
1092 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1093 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1094 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1095 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1096 876c234b 2018-09-10 stsp return 1;
1097 876c234b 2018-09-10 stsp }
1098 2ff12563 2018-09-15 stsp #endif
1099 876c234b 2018-09-10 stsp
1100 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1101 876c234b 2018-09-10 stsp if (err) {
1102 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1103 876c234b 2018-09-10 stsp return 1;
1104 876c234b 2018-09-10 stsp }
1105 876c234b 2018-09-10 stsp
1106 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1107 876c234b 2018-09-10 stsp if (err) {
1108 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1109 876c234b 2018-09-10 stsp return 1;
1110 876c234b 2018-09-10 stsp }
1111 876c234b 2018-09-10 stsp
1112 656b1f76 2019-05-11 jcs for (;;) {
1113 876c234b 2018-09-10 stsp imsg.fd = -1;
1114 99437157 2018-11-11 stsp
1115 99437157 2018-11-11 stsp if (sigint_received) {
1116 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1117 99437157 2018-11-11 stsp break;
1118 99437157 2018-11-11 stsp }
1119 876c234b 2018-09-10 stsp
1120 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1121 876c234b 2018-09-10 stsp if (err) {
1122 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1123 876c234b 2018-09-10 stsp err = NULL;
1124 876c234b 2018-09-10 stsp break;
1125 876c234b 2018-09-10 stsp }
1126 876c234b 2018-09-10 stsp
1127 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1128 876c234b 2018-09-10 stsp break;
1129 876c234b 2018-09-10 stsp
1130 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1131 db696021 2022-01-04 stsp case GOT_IMSG_TMPFD:
1132 67fd6849 2022-02-13 stsp if (basefile == NULL) {
1133 67fd6849 2022-02-13 stsp err = receive_tempfile(&basefile, "w+",
1134 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1135 67fd6849 2022-02-13 stsp } else if (accumfile == NULL) {
1136 67fd6849 2022-02-13 stsp err = receive_tempfile(&accumfile, "w+",
1137 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1138 67fd6849 2022-02-13 stsp } else
1139 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1140 db696021 2022-01-04 stsp break;
1141 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
1142 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
1143 c59b3346 2018-09-11 stsp &objcache);
1144 876c234b 2018-09-10 stsp break;
1145 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1146 db696021 2022-01-04 stsp if (basefile == NULL || accumfile == NULL) {
1147 db696021 2022-01-04 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1148 db696021 2022-01-04 stsp break;
1149 db696021 2022-01-04 stsp }
1150 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
1151 db696021 2022-01-04 stsp &objcache, basefile, accumfile);
1152 59d1e4a0 2021-03-10 stsp break;
1153 67fd6849 2022-02-13 stsp case GOT_IMSG_RAW_DELTA_OUTFD:
1154 67fd6849 2022-02-13 stsp if (delta_outfile != NULL) {
1155 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1156 67fd6849 2022-02-13 stsp break;
1157 67fd6849 2022-02-13 stsp }
1158 67fd6849 2022-02-13 stsp err = receive_tempfile(&delta_outfile, "w",
1159 67fd6849 2022-02-13 stsp &imsg, &ibuf);
1160 67fd6849 2022-02-13 stsp break;
1161 67fd6849 2022-02-13 stsp case GOT_IMSG_RAW_DELTA_REQUEST:
1162 67fd6849 2022-02-13 stsp if (delta_outfile == NULL) {
1163 67fd6849 2022-02-13 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1164 67fd6849 2022-02-13 stsp break;
1165 67fd6849 2022-02-13 stsp }
1166 67fd6849 2022-02-13 stsp err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1167 67fd6849 2022-02-13 stsp pack, packidx);
1168 67fd6849 2022-02-13 stsp break;
1169 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
1170 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
1171 7762fe12 2018-11-05 stsp &objcache);
1172 7762fe12 2018-11-05 stsp break;
1173 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
1174 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
1175 62d463ca 2020-10-20 naddy &objcache);
1176 876c234b 2018-09-10 stsp break;
1177 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
1178 db696021 2022-01-04 stsp if (basefile == NULL || accumfile == NULL) {
1179 db696021 2022-01-04 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1180 db696021 2022-01-04 stsp break;
1181 db696021 2022-01-04 stsp }
1182 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
1183 db696021 2022-01-04 stsp &objcache, basefile, accumfile);
1184 876c234b 2018-09-10 stsp break;
1185 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
1186 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
1187 62d463ca 2020-10-20 naddy &objcache);
1188 f4a881ce 2018-11-17 stsp break;
1189 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1190 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
1191 ca6e02ac 2020-01-07 stsp packidx, &objcache);
1192 ca6e02ac 2020-01-07 stsp break;
1193 876c234b 2018-09-10 stsp default:
1194 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1195 876c234b 2018-09-10 stsp break;
1196 876c234b 2018-09-10 stsp }
1197 876c234b 2018-09-10 stsp
1198 08578a35 2021-01-22 stsp if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1199 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1200 876c234b 2018-09-10 stsp imsg_free(&imsg);
1201 99437157 2018-11-11 stsp if (err)
1202 876c234b 2018-09-10 stsp break;
1203 876c234b 2018-09-10 stsp }
1204 876c234b 2018-09-10 stsp
1205 c59b3346 2018-09-11 stsp if (packidx)
1206 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
1207 c59b3346 2018-09-11 stsp if (pack)
1208 c59b3346 2018-09-11 stsp got_pack_close(pack);
1209 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
1210 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
1211 db696021 2022-01-04 stsp if (basefile && fclose(basefile) == EOF && err == NULL)
1212 db696021 2022-01-04 stsp err = got_error_from_errno("fclose");
1213 db696021 2022-01-04 stsp if (accumfile && fclose(accumfile) == EOF && err == NULL)
1214 db696021 2022-01-04 stsp err = got_error_from_errno("fclose");
1215 67fd6849 2022-02-13 stsp if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1216 67fd6849 2022-02-13 stsp err = got_error_from_errno("fclose");
1217 99437157 2018-11-11 stsp if (err) {
1218 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1219 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1220 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
1221 80d5f134 2018-11-11 stsp }
1222 99437157 2018-11-11 stsp }
1223 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1224 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1225 876c234b 2018-09-10 stsp return err ? 1 : 0;
1226 876c234b 2018-09-10 stsp }