2 5aa81393 2020-01-06 stsp * Copyright (c) 2018, 2019, 2020 Stefan Sperling <stsp@openbsd.org>
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.
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.
17 876c234b 2018-09-10 stsp #include <sys/types.h>
18 876c234b 2018-09-10 stsp #include <sys/uio.h>
19 876c234b 2018-09-10 stsp #include <sys/time.h>
20 876c234b 2018-09-10 stsp #include <sys/mman.h>
22 876c234b 2018-09-10 stsp #include <limits.h>
23 99437157 2018-11-11 stsp #include <signal.h>
24 876c234b 2018-09-10 stsp #include <stdint.h>
25 876c234b 2018-09-10 stsp #include <stdio.h>
26 876c234b 2018-09-10 stsp #include <stdlib.h>
27 876c234b 2018-09-10 stsp #include <string.h>
28 81a12da5 2020-09-09 naddy #include <unistd.h>
29 876c234b 2018-09-10 stsp #include <zlib.h>
31 dd038bc6 2021-09-21 thomas.ad #include "got_compat.h"
33 876c234b 2018-09-10 stsp #include "got_error.h"
34 876c234b 2018-09-10 stsp #include "got_object.h"
35 3022d272 2019-11-14 stsp #include "got_path.h"
37 876c234b 2018-09-10 stsp #include "got_lib_delta.h"
38 ab2f42e7 2019-11-10 stsp #include "got_lib_delta_cache.h"
39 876c234b 2018-09-10 stsp #include "got_lib_object.h"
40 c59b3346 2018-09-11 stsp #include "got_lib_object_cache.h"
41 876c234b 2018-09-10 stsp #include "got_lib_object_parse.h"
42 876c234b 2018-09-10 stsp #include "got_lib_privsep.h"
43 876c234b 2018-09-10 stsp #include "got_lib_pack.h"
45 99437157 2018-11-11 stsp static volatile sig_atomic_t sigint_received;
48 99437157 2018-11-11 stsp catch_sigint(int signo)
50 99437157 2018-11-11 stsp sigint_received = 1;
53 876c234b 2018-09-10 stsp static const struct got_error *
54 704b89c4 2019-05-23 stsp open_object(struct got_object **obj, struct got_pack *pack,
55 704b89c4 2019-05-23 stsp struct got_packidx *packidx, int idx, struct got_object_id *id,
56 704b89c4 2019-05-23 stsp struct got_object_cache *objcache)
58 704b89c4 2019-05-23 stsp const struct got_error *err;
60 704b89c4 2019-05-23 stsp err = got_packfile_open_object(obj, pack, packidx, idx, id);
63 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
65 704b89c4 2019-05-23 stsp err = got_object_cache_add(objcache, id, *obj);
67 79c99a64 2019-05-23 stsp if (err->code == GOT_ERR_OBJ_EXISTS ||
68 79c99a64 2019-05-23 stsp err->code == GOT_ERR_OBJ_TOO_LARGE)
72 704b89c4 2019-05-23 stsp (*obj)->refcnt++;
73 704b89c4 2019-05-23 stsp return NULL;
76 704b89c4 2019-05-23 stsp static const struct got_error *
77 876c234b 2018-09-10 stsp object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
78 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
80 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
81 876c234b 2018-09-10 stsp struct got_imsg_packed_object iobj;
82 876c234b 2018-09-10 stsp struct got_object *obj;
83 106807b4 2018-09-15 stsp struct got_object_id id;
84 876c234b 2018-09-10 stsp size_t datalen;
86 876c234b 2018-09-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
87 876c234b 2018-09-10 stsp if (datalen != sizeof(iobj))
88 876c234b 2018-09-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
89 876c234b 2018-09-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
90 106807b4 2018-09-15 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
92 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
94 704b89c4 2019-05-23 stsp obj->refcnt++;
96 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
102 876c234b 2018-09-10 stsp err = got_privsep_send_obj(ibuf, obj);
104 876c234b 2018-09-10 stsp got_object_close(obj);
105 876c234b 2018-09-10 stsp return err;
108 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
109 ca6e02ac 2020-01-07 stsp open_commit(struct got_commit_object **commit, struct got_pack *pack,
110 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, int obj_idx, struct got_object_id *id,
111 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
113 cfd633c2 2018-09-10 stsp const struct got_error *err = NULL;
114 cb5e38fd 2019-05-23 stsp struct got_object *obj = NULL;
115 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
116 cfd633c2 2018-09-10 stsp size_t len;
118 ca6e02ac 2020-01-07 stsp *commit = NULL;
120 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
122 704b89c4 2019-05-23 stsp obj->refcnt++;
124 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
127 704b89c4 2019-05-23 stsp return err;
130 cfd633c2 2018-09-10 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
134 cfd633c2 2018-09-10 stsp obj->size = len;
136 ca6e02ac 2020-01-07 stsp err = got_object_parse_commit(commit, buf, len);
138 ca6e02ac 2020-01-07 stsp got_object_close(obj);
140 ca6e02ac 2020-01-07 stsp return err;
143 ca6e02ac 2020-01-07 stsp static const struct got_error *
144 ca6e02ac 2020-01-07 stsp commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
145 ca6e02ac 2020-01-07 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
147 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
148 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
149 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL;
150 ca6e02ac 2020-01-07 stsp struct got_object_id id;
151 ca6e02ac 2020-01-07 stsp size_t datalen;
153 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
154 ca6e02ac 2020-01-07 stsp if (datalen != sizeof(iobj))
155 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
156 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
157 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
159 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx, iobj.idx, &id, objcache);
163 cfd633c2 2018-09-10 stsp err = got_privsep_send_commit(ibuf, commit);
165 cb5e38fd 2019-05-23 stsp if (commit)
166 cb5e38fd 2019-05-23 stsp got_object_commit_close(commit);
168 7762fe12 2018-11-05 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
169 7762fe12 2018-11-05 stsp err = NULL;
171 7762fe12 2018-11-05 stsp got_privsep_send_error(ibuf, err);
174 7762fe12 2018-11-05 stsp return err;
177 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
178 ca6e02ac 2020-01-07 stsp open_tree(uint8_t **buf, struct got_pathlist_head *entries, int *nentries,
179 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx, int obj_idx,
180 ca6e02ac 2020-01-07 stsp struct got_object_id *id, struct got_object_cache *objcache)
182 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
183 ca6e02ac 2020-01-07 stsp struct got_object *obj = NULL;
184 ca6e02ac 2020-01-07 stsp size_t len;
186 ca6e02ac 2020-01-07 stsp *buf = NULL;
187 ca6e02ac 2020-01-07 stsp *nentries = 0;
189 ca6e02ac 2020-01-07 stsp obj = got_object_cache_get(objcache, id);
191 ca6e02ac 2020-01-07 stsp obj->refcnt++;
193 ca6e02ac 2020-01-07 stsp err = open_object(&obj, pack, packidx, obj_idx, id,
196 ca6e02ac 2020-01-07 stsp return err;
199 ca6e02ac 2020-01-07 stsp err = got_packfile_extract_object_to_mem(buf, &len, obj, pack);
203 ca6e02ac 2020-01-07 stsp obj->size = len;
205 ca6e02ac 2020-01-07 stsp err = got_object_parse_tree(entries, nentries, *buf, len);
207 ca6e02ac 2020-01-07 stsp got_object_close(obj);
209 ca6e02ac 2020-01-07 stsp free(*buf);
210 ca6e02ac 2020-01-07 stsp *buf = NULL;
212 ca6e02ac 2020-01-07 stsp return err;
215 7762fe12 2018-11-05 stsp static const struct got_error *
216 876c234b 2018-09-10 stsp tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
217 c59b3346 2018-09-11 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
219 e7885405 2018-09-10 stsp const struct got_error *err = NULL;
220 13c729f7 2018-12-24 stsp struct got_imsg_packed_object iobj;
221 3022d272 2019-11-14 stsp struct got_pathlist_head entries;
222 3022d272 2019-11-14 stsp int nentries = 0;
223 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
224 13c729f7 2018-12-24 stsp struct got_object_id id;
225 13c729f7 2018-12-24 stsp size_t datalen;
227 3022d272 2019-11-14 stsp TAILQ_INIT(&entries);
229 13c729f7 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
230 13c729f7 2018-12-24 stsp if (datalen != sizeof(iobj))
231 13c729f7 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
232 13c729f7 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
233 13c729f7 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
235 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack, packidx, iobj.idx,
236 62d463ca 2020-10-20 naddy &id, objcache);
238 ca6e02ac 2020-01-07 stsp return err;
240 3022d272 2019-11-14 stsp err = got_privsep_send_tree(ibuf, &entries, nentries);
241 b87b4170 2020-01-06 stsp got_object_parsed_tree_entries_free(&entries);
244 e7885405 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
245 e7885405 2018-09-10 stsp err = NULL;
247 e7885405 2018-09-10 stsp got_privsep_send_error(ibuf, err);
250 e7885405 2018-09-10 stsp return err;
253 876c234b 2018-09-10 stsp static const struct got_error *
254 01bb5a15 2021-09-25 thomas.ad receive_file(FILE **f, struct imsgbuf *ibuf, uint32_t imsg_code)
256 3840f4c9 2018-09-12 stsp const struct got_error *err;
257 3840f4c9 2018-09-12 stsp struct imsg imsg;
258 55da3778 2018-09-10 stsp size_t datalen;
260 3840f4c9 2018-09-12 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
262 55da3778 2018-09-10 stsp return err;
264 3840f4c9 2018-09-12 stsp if (imsg.hdr.type != imsg_code) {
265 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
269 3840f4c9 2018-09-12 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
270 55da3778 2018-09-10 stsp if (datalen != 0) {
271 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
274 3840f4c9 2018-09-12 stsp if (imsg.fd == -1) {
275 55da3778 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
279 3840f4c9 2018-09-12 stsp *f = fdopen(imsg.fd, "w+");
280 3840f4c9 2018-09-12 stsp if (*f == NULL) {
281 638f9024 2019-05-13 stsp err = got_error_from_errno("fdopen");
282 3a6ce05a 2019-02-11 stsp close(imsg.fd);
286 3840f4c9 2018-09-12 stsp imsg_free(&imsg);
287 3840f4c9 2018-09-12 stsp return err;
290 bc1f382f 2022-01-05 thomas static const struct got_error *
291 f9c2e8e5 2022-02-13 thomas receive_tempfile(FILE **f, const char *mode, struct imsg *imsg,
292 bc1f382f 2022-01-05 thomas struct imsgbuf *ibuf)
294 bc1f382f 2022-01-05 thomas size_t datalen;
296 bc1f382f 2022-01-05 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
297 bc1f382f 2022-01-05 thomas if (datalen != 0)
298 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
300 bc1f382f 2022-01-05 thomas if (imsg->fd == -1)
301 bc1f382f 2022-01-05 thomas return got_error(GOT_ERR_PRIVSEP_NO_FD);
303 f9c2e8e5 2022-02-13 thomas *f = fdopen(imsg->fd, mode);
304 bc1f382f 2022-01-05 thomas if (*f == NULL)
305 bc1f382f 2022-01-05 thomas return got_error_from_errno("fdopen");
306 bc1f382f 2022-01-05 thomas imsg->fd = -1;
308 bc1f382f 2022-01-05 thomas return NULL;
311 3840f4c9 2018-09-12 stsp static const struct got_error *
312 3840f4c9 2018-09-12 stsp blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
313 bc1f382f 2022-01-05 thomas struct got_packidx *packidx, struct got_object_cache *objcache,
314 bc1f382f 2022-01-05 thomas FILE *basefile, FILE *accumfile)
316 3840f4c9 2018-09-12 stsp const struct got_error *err = NULL;
317 ebc55e2d 2018-12-24 stsp struct got_imsg_packed_object iobj;
318 3840f4c9 2018-09-12 stsp struct got_object *obj = NULL;
319 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
320 ebc55e2d 2018-12-24 stsp struct got_object_id id;
321 ebc55e2d 2018-12-24 stsp size_t datalen;
322 ac544f8c 2019-01-13 stsp uint64_t blob_size;
323 ac544f8c 2019-01-13 stsp uint8_t *buf = NULL;
325 ebc55e2d 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
326 ebc55e2d 2018-12-24 stsp if (datalen != sizeof(iobj))
327 ebc55e2d 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
328 ebc55e2d 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
329 ebc55e2d 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
331 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
333 704b89c4 2019-05-23 stsp obj->refcnt++;
335 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
338 704b89c4 2019-05-23 stsp return err;
341 3840f4c9 2018-09-12 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
345 ac544f8c 2019-01-13 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
346 42c69117 2019-11-10 stsp err = got_pack_get_max_delta_object_size(&blob_size, obj, pack);
350 ac544f8c 2019-01-13 stsp blob_size = obj->size;
352 ac544f8c 2019-01-13 stsp if (blob_size <= GOT_PRIVSEP_INLINE_BLOB_DATA_MAX)
353 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
354 ac544f8c 2019-01-13 stsp obj, pack);
356 ac544f8c 2019-01-13 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
357 ac544f8c 2019-01-13 stsp accumfile);
361 ac544f8c 2019-01-13 stsp err = got_privsep_send_blob(ibuf, obj->size, obj->hdrlen, buf);
364 56b63ca4 2021-01-22 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
365 638f9024 2019-05-13 stsp err = got_error_from_errno("fclose");
366 cb5e38fd 2019-05-23 stsp got_object_close(obj);
367 3840f4c9 2018-09-12 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
368 3840f4c9 2018-09-12 stsp got_privsep_send_error(ibuf, err);
370 55da3778 2018-09-10 stsp return err;
373 876c234b 2018-09-10 stsp static const struct got_error *
374 f4a881ce 2018-11-17 stsp tag_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
375 f4a881ce 2018-11-17 stsp struct got_packidx *packidx, struct got_object_cache *objcache)
377 f4a881ce 2018-11-17 stsp const struct got_error *err = NULL;
378 268f7291 2018-12-24 stsp struct got_imsg_packed_object iobj;
379 f4a881ce 2018-11-17 stsp struct got_object *obj = NULL;
380 f4a881ce 2018-11-17 stsp struct got_tag_object *tag = NULL;
381 cb5e38fd 2019-05-23 stsp uint8_t *buf = NULL;
382 f4a881ce 2018-11-17 stsp size_t len;
383 268f7291 2018-12-24 stsp struct got_object_id id;
384 268f7291 2018-12-24 stsp size_t datalen;
386 268f7291 2018-12-24 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
387 268f7291 2018-12-24 stsp if (datalen != sizeof(iobj))
388 268f7291 2018-12-24 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
389 268f7291 2018-12-24 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
390 268f7291 2018-12-24 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
392 704b89c4 2019-05-23 stsp obj = got_object_cache_get(objcache, &id);
394 704b89c4 2019-05-23 stsp obj->refcnt++;
396 704b89c4 2019-05-23 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
399 704b89c4 2019-05-23 stsp return err;
402 f4a881ce 2018-11-17 stsp err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
406 f4a881ce 2018-11-17 stsp obj->size = len;
407 f4a881ce 2018-11-17 stsp err = got_object_parse_tag(&tag, buf, len);
411 f4a881ce 2018-11-17 stsp err = got_privsep_send_tag(ibuf, tag);
414 cb5e38fd 2019-05-23 stsp got_object_close(obj);
416 cb5e38fd 2019-05-23 stsp got_object_tag_close(tag);
418 ca6e02ac 2020-01-07 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
419 ca6e02ac 2020-01-07 stsp err = NULL;
421 ca6e02ac 2020-01-07 stsp got_privsep_send_error(ibuf, err);
424 ca6e02ac 2020-01-07 stsp return err;
427 ca6e02ac 2020-01-07 stsp static struct got_parsed_tree_entry *
428 ca6e02ac 2020-01-07 stsp find_entry_by_name(struct got_pathlist_head *entries, int nentries,
429 ca6e02ac 2020-01-07 stsp const char *name, size_t len)
431 ca6e02ac 2020-01-07 stsp struct got_pathlist_entry *pe;
433 ca6e02ac 2020-01-07 stsp /* Note that tree entries are sorted in strncmp() order. */
434 ca6e02ac 2020-01-07 stsp TAILQ_FOREACH(pe, entries, entry) {
435 ca6e02ac 2020-01-07 stsp int cmp = strncmp(pe->path, name, len);
436 ca6e02ac 2020-01-07 stsp if (cmp < 0)
438 ca6e02ac 2020-01-07 stsp if (cmp > 0)
440 ca6e02ac 2020-01-07 stsp if (pe->path[len] == '\0')
441 ca6e02ac 2020-01-07 stsp return (struct got_parsed_tree_entry *)pe->data;
443 ca6e02ac 2020-01-07 stsp return NULL;
446 8f1c06eb 2021-09-25 thomas.ad static const struct got_error *
447 ca6e02ac 2020-01-07 stsp tree_path_changed(int *changed, uint8_t **buf1, uint8_t **buf2,
448 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries1, int *nentries1,
449 ca6e02ac 2020-01-07 stsp struct got_pathlist_head *entries2, int *nentries2,
450 ca6e02ac 2020-01-07 stsp const char *path, struct got_pack *pack, struct got_packidx *packidx,
451 ca6e02ac 2020-01-07 stsp struct imsgbuf *ibuf, struct got_object_cache *objcache)
453 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
454 ca6e02ac 2020-01-07 stsp struct got_parsed_tree_entry *pte1 = NULL, *pte2 = NULL;
455 ca6e02ac 2020-01-07 stsp const char *seg, *s;
456 ca6e02ac 2020-01-07 stsp size_t seglen;
458 ca6e02ac 2020-01-07 stsp *changed = 0;
460 ca6e02ac 2020-01-07 stsp /* We not do support comparing the root path. */
461 61a7d79f 2020-02-29 stsp if (got_path_is_root_dir(path))
462 63f810e6 2020-02-29 stsp return got_error_path(path, GOT_ERR_BAD_PATH);
465 61a7d79f 2020-02-29 stsp while (*s == '/')
468 ca6e02ac 2020-01-07 stsp seglen = 0;
469 ca6e02ac 2020-01-07 stsp while (*s) {
470 ca6e02ac 2020-01-07 stsp if (*s != '/') {
477 ca6e02ac 2020-01-07 stsp pte1 = find_entry_by_name(entries1, *nentries1, seg, seglen);
478 ca6e02ac 2020-01-07 stsp if (pte1 == NULL) {
479 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_NO_OBJ);
483 ca6e02ac 2020-01-07 stsp pte2 = find_entry_by_name(entries2, *nentries2, seg, seglen);
484 ca6e02ac 2020-01-07 stsp if (pte2 == NULL) {
485 ca6e02ac 2020-01-07 stsp *changed = 1;
489 ca6e02ac 2020-01-07 stsp if (pte1->mode != pte2->mode) {
490 ca6e02ac 2020-01-07 stsp *changed = 1;
494 ca6e02ac 2020-01-07 stsp if (memcmp(pte1->id, pte2->id, SHA1_DIGEST_LENGTH) == 0) {
495 ca6e02ac 2020-01-07 stsp *changed = 0;
499 ca6e02ac 2020-01-07 stsp if (*s == '\0') { /* final path element */
500 ca6e02ac 2020-01-07 stsp *changed = 1;
504 ca6e02ac 2020-01-07 stsp seg = s + 1;
506 ca6e02ac 2020-01-07 stsp seglen = 0;
508 ca6e02ac 2020-01-07 stsp struct got_object_id id1, id2;
511 ded8fbb8 2020-04-19 stsp memcpy(id1.sha1, pte1->id, SHA1_DIGEST_LENGTH);
512 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id1);
513 ca6e02ac 2020-01-07 stsp if (idx == -1) {
514 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id1);
517 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries1);
518 ca6e02ac 2020-01-07 stsp *nentries1 = 0;
519 ca6e02ac 2020-01-07 stsp free(*buf1);
520 ca6e02ac 2020-01-07 stsp *buf1 = NULL;
521 ca6e02ac 2020-01-07 stsp err = open_tree(buf1, entries1, nentries1, pack,
522 ca6e02ac 2020-01-07 stsp packidx, idx, &id1, objcache);
523 ca6e02ac 2020-01-07 stsp pte1 = NULL;
527 ded8fbb8 2020-04-19 stsp memcpy(id2.sha1, pte2->id, SHA1_DIGEST_LENGTH);
528 00927983 2020-04-19 stsp idx = got_packidx_get_object_idx(packidx, &id2);
529 ca6e02ac 2020-01-07 stsp if (idx == -1) {
530 ded8fbb8 2020-04-19 stsp err = got_error_no_obj(&id2);
533 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(entries2);
534 ca6e02ac 2020-01-07 stsp *nentries2 = 0;
535 ca6e02ac 2020-01-07 stsp free(*buf2);
536 ca6e02ac 2020-01-07 stsp *buf2 = NULL;
537 ca6e02ac 2020-01-07 stsp err = open_tree(buf2, entries2, nentries2, pack,
538 ca6e02ac 2020-01-07 stsp packidx, idx, &id2, objcache);
539 ca6e02ac 2020-01-07 stsp pte2 = NULL;
545 ca6e02ac 2020-01-07 stsp return err;
548 ca6e02ac 2020-01-07 stsp static const struct got_error *
549 e70bf110 2020-03-22 stsp send_traversed_commits(struct got_object_id *commit_ids, size_t ncommits,
550 e70bf110 2020-03-22 stsp struct imsgbuf *ibuf)
552 e70bf110 2020-03-22 stsp const struct got_error *err;
553 e70bf110 2020-03-22 stsp struct ibuf *wbuf;
554 01bb5a15 2021-09-25 thomas.ad size_t i;
556 e70bf110 2020-03-22 stsp wbuf = imsg_create(ibuf, GOT_IMSG_TRAVERSED_COMMITS, 0, 0,
557 e70bf110 2020-03-22 stsp sizeof(struct got_imsg_traversed_commits) +
558 e70bf110 2020-03-22 stsp ncommits * SHA1_DIGEST_LENGTH);
559 e70bf110 2020-03-22 stsp if (wbuf == NULL)
560 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_create TRAVERSED_COMMITS");
562 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, &ncommits, sizeof(ncommits)) == -1) {
563 e70bf110 2020-03-22 stsp err = got_error_from_errno("imsg_add TRAVERSED_COMMITS");
564 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
565 e70bf110 2020-03-22 stsp return err;
567 e70bf110 2020-03-22 stsp for (i = 0; i < ncommits; i++) {
568 e70bf110 2020-03-22 stsp struct got_object_id *id = &commit_ids[i];
569 e70bf110 2020-03-22 stsp if (imsg_add(wbuf, id->sha1, SHA1_DIGEST_LENGTH) == -1) {
570 e70bf110 2020-03-22 stsp err = got_error_from_errno(
571 e70bf110 2020-03-22 stsp "imsg_add TRAVERSED_COMMITS");
572 e70bf110 2020-03-22 stsp ibuf_free(wbuf);
573 e70bf110 2020-03-22 stsp return err;
577 e70bf110 2020-03-22 stsp wbuf->fd = -1;
578 e70bf110 2020-03-22 stsp imsg_close(ibuf, wbuf);
580 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
583 e70bf110 2020-03-22 stsp static const struct got_error *
584 e70bf110 2020-03-22 stsp send_commit_traversal_done(struct imsgbuf *ibuf)
586 e70bf110 2020-03-22 stsp if (imsg_compose(ibuf, GOT_IMSG_COMMIT_TRAVERSAL_DONE, 0, 0, -1,
587 e70bf110 2020-03-22 stsp NULL, 0) == -1)
588 e70bf110 2020-03-22 stsp return got_error_from_errno("imsg_compose TRAVERSAL_DONE");
590 e70bf110 2020-03-22 stsp return got_privsep_flush_imsg(ibuf);
594 e70bf110 2020-03-22 stsp static const struct got_error *
595 ca6e02ac 2020-01-07 stsp commit_traversal_request(struct imsg *imsg, struct imsgbuf *ibuf,
596 ca6e02ac 2020-01-07 stsp struct got_pack *pack, struct got_packidx *packidx,
597 ca6e02ac 2020-01-07 stsp struct got_object_cache *objcache)
599 ca6e02ac 2020-01-07 stsp const struct got_error *err = NULL;
600 ca6e02ac 2020-01-07 stsp struct got_imsg_packed_object iobj;
601 ca6e02ac 2020-01-07 stsp struct got_object_qid *pid;
602 ca6e02ac 2020-01-07 stsp struct got_commit_object *commit = NULL, *pcommit = NULL;
603 ca6e02ac 2020-01-07 stsp struct got_pathlist_head entries, pentries;
604 ca6e02ac 2020-01-07 stsp int nentries = 0, pnentries = 0;
605 ca6e02ac 2020-01-07 stsp struct got_object_id id;
606 ca6e02ac 2020-01-07 stsp size_t datalen, path_len;
607 ca6e02ac 2020-01-07 stsp char *path = NULL;
608 ca6e02ac 2020-01-07 stsp const int min_alloc = 64;
609 ca6e02ac 2020-01-07 stsp int changed = 0, ncommits = 0, nallocated = 0;
610 ca6e02ac 2020-01-07 stsp struct got_object_id *commit_ids = NULL;
612 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&entries);
613 ca6e02ac 2020-01-07 stsp TAILQ_INIT(&pentries);
615 ca6e02ac 2020-01-07 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
616 ca6e02ac 2020-01-07 stsp if (datalen < sizeof(iobj))
617 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
618 ca6e02ac 2020-01-07 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
619 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
621 ca6e02ac 2020-01-07 stsp path_len = datalen - sizeof(iobj) - 1;
622 ca6e02ac 2020-01-07 stsp if (path_len < 0)
623 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
624 ca6e02ac 2020-01-07 stsp if (path_len > 0) {
625 ca6e02ac 2020-01-07 stsp path = imsg->data + sizeof(iobj);
626 ca6e02ac 2020-01-07 stsp if (path[path_len] != '\0')
627 ca6e02ac 2020-01-07 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
630 ca6e02ac 2020-01-07 stsp nallocated = min_alloc;
631 ca6e02ac 2020-01-07 stsp commit_ids = reallocarray(NULL, nallocated, sizeof(*commit_ids));
632 ca6e02ac 2020-01-07 stsp if (commit_ids == NULL)
633 ca6e02ac 2020-01-07 stsp return got_error_from_errno("reallocarray");
636 ca6e02ac 2020-01-07 stsp const size_t max_datalen = MAX_IMSGSIZE - IMSG_HEADER_SIZE;
639 ca6e02ac 2020-01-07 stsp if (sigint_received) {
640 ca6e02ac 2020-01-07 stsp err = got_error(GOT_ERR_CANCELLED);
644 ca6e02ac 2020-01-07 stsp if (commit == NULL) {
645 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, &id);
646 ca6e02ac 2020-01-07 stsp if (idx == -1)
648 ca6e02ac 2020-01-07 stsp err = open_commit(&commit, pack, packidx,
649 ca6e02ac 2020-01-07 stsp idx, &id, objcache);
651 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
653 ca6e02ac 2020-01-07 stsp err = NULL;
658 ca6e02ac 2020-01-07 stsp if (sizeof(struct got_imsg_traversed_commits) +
659 ca6e02ac 2020-01-07 stsp ncommits * SHA1_DIGEST_LENGTH >= max_datalen) {
660 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits,
664 ca6e02ac 2020-01-07 stsp ncommits = 0;
666 ca6e02ac 2020-01-07 stsp ncommits++;
667 ca6e02ac 2020-01-07 stsp if (ncommits > nallocated) {
668 ca6e02ac 2020-01-07 stsp struct got_object_id *new;
669 ca6e02ac 2020-01-07 stsp nallocated += min_alloc;
670 ca6e02ac 2020-01-07 stsp new = reallocarray(commit_ids, nallocated,
671 ca6e02ac 2020-01-07 stsp sizeof(*commit_ids));
672 ca6e02ac 2020-01-07 stsp if (new == NULL) {
673 ca6e02ac 2020-01-07 stsp err = got_error_from_errno("reallocarray");
676 ca6e02ac 2020-01-07 stsp commit_ids = new;
678 ca6e02ac 2020-01-07 stsp memcpy(commit_ids[ncommits - 1].sha1, id.sha1,
679 ca6e02ac 2020-01-07 stsp SHA1_DIGEST_LENGTH);
681 dbdddfee 2021-06-23 naddy pid = STAILQ_FIRST(&commit->parent_ids);
682 ca6e02ac 2020-01-07 stsp if (pid == NULL)
685 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx, pid->id);
686 ca6e02ac 2020-01-07 stsp if (idx == -1)
689 ca6e02ac 2020-01-07 stsp err = open_commit(&pcommit, pack, packidx, idx, pid->id,
692 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
694 ca6e02ac 2020-01-07 stsp err = NULL;
698 ca6e02ac 2020-01-07 stsp if (path[0] == '/' && path[1] == '\0') {
699 ca6e02ac 2020-01-07 stsp if (got_object_id_cmp(pcommit->tree_id,
700 ca6e02ac 2020-01-07 stsp commit->tree_id) != 0) {
701 ca6e02ac 2020-01-07 stsp changed = 1;
706 ca6e02ac 2020-01-07 stsp uint8_t *buf = NULL, *pbuf = NULL;
708 ca6e02ac 2020-01-07 stsp idx = got_packidx_get_object_idx(packidx,
709 ca6e02ac 2020-01-07 stsp commit->tree_id);
710 ca6e02ac 2020-01-07 stsp if (idx == -1)
712 ca6e02ac 2020-01-07 stsp pidx = got_packidx_get_object_idx(packidx,
713 ca6e02ac 2020-01-07 stsp pcommit->tree_id);
714 ca6e02ac 2020-01-07 stsp if (pidx == -1)
717 ca6e02ac 2020-01-07 stsp err = open_tree(&buf, &entries, &nentries, pack,
718 ca6e02ac 2020-01-07 stsp packidx, idx, commit->tree_id, objcache);
721 ca6e02ac 2020-01-07 stsp err = open_tree(&pbuf, &pentries, &pnentries, pack,
722 ca6e02ac 2020-01-07 stsp packidx, pidx, pcommit->tree_id, objcache);
728 ca6e02ac 2020-01-07 stsp err = tree_path_changed(&changed, &buf, &pbuf,
729 ca6e02ac 2020-01-07 stsp &entries, &nentries, &pentries, &pnentries, path,
730 ca6e02ac 2020-01-07 stsp pack, packidx, ibuf, objcache);
732 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
733 ca6e02ac 2020-01-07 stsp nentries = 0;
735 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
736 ca6e02ac 2020-01-07 stsp pnentries = 0;
737 ca6e02ac 2020-01-07 stsp free(pbuf);
739 ca6e02ac 2020-01-07 stsp if (err->code != GOT_ERR_NO_OBJ)
741 ca6e02ac 2020-01-07 stsp err = NULL;
746 ca6e02ac 2020-01-07 stsp if (!changed) {
747 ca6e02ac 2020-01-07 stsp memcpy(id.sha1, pid->id->sha1, SHA1_DIGEST_LENGTH);
748 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
749 ca6e02ac 2020-01-07 stsp commit = pcommit;
750 ca6e02ac 2020-01-07 stsp pcommit = NULL;
752 ca6e02ac 2020-01-07 stsp } while (!changed);
754 ca6e02ac 2020-01-07 stsp if (ncommits > 0) {
755 e70bf110 2020-03-22 stsp err = send_traversed_commits(commit_ids, ncommits, ibuf);
759 ca6e02ac 2020-01-07 stsp if (changed) {
760 ca6e02ac 2020-01-07 stsp err = got_privsep_send_commit(ibuf, commit);
765 e70bf110 2020-03-22 stsp err = send_commit_traversal_done(ibuf);
767 ca6e02ac 2020-01-07 stsp free(commit_ids);
768 ca6e02ac 2020-01-07 stsp if (commit)
769 ca6e02ac 2020-01-07 stsp got_object_commit_close(commit);
770 ca6e02ac 2020-01-07 stsp if (pcommit)
771 ca6e02ac 2020-01-07 stsp got_object_commit_close(pcommit);
772 ca6e02ac 2020-01-07 stsp if (nentries != 0)
773 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&entries);
774 ca6e02ac 2020-01-07 stsp if (pnentries != 0)
775 ca6e02ac 2020-01-07 stsp got_object_parsed_tree_entries_free(&pentries);
777 f4a881ce 2018-11-17 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
778 f4a881ce 2018-11-17 stsp err = NULL;
780 f4a881ce 2018-11-17 stsp got_privsep_send_error(ibuf, err);
783 f4a881ce 2018-11-17 stsp return err;
786 f4a881ce 2018-11-17 stsp static const struct got_error *
787 48b4f239 2021-12-31 thomas raw_object_request(struct imsg *imsg, struct imsgbuf *ibuf,
788 48b4f239 2021-12-31 thomas struct got_pack *pack, struct got_packidx *packidx,
789 bc1f382f 2022-01-05 thomas struct got_object_cache *objcache, FILE *basefile, FILE *accumfile)
791 59d1e4a0 2021-03-10 stsp const struct got_error *err = NULL;
792 59d1e4a0 2021-03-10 stsp uint8_t *buf = NULL;
793 59d1e4a0 2021-03-10 stsp uint64_t size = 0;
794 bc1f382f 2022-01-05 thomas FILE *outfile = NULL;
795 59d1e4a0 2021-03-10 stsp struct got_imsg_packed_object iobj;
796 59d1e4a0 2021-03-10 stsp struct got_object *obj;
797 59d1e4a0 2021-03-10 stsp struct got_object_id id;
798 59d1e4a0 2021-03-10 stsp size_t datalen;
800 59d1e4a0 2021-03-10 stsp datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
801 59d1e4a0 2021-03-10 stsp if (datalen != sizeof(iobj))
802 59d1e4a0 2021-03-10 stsp return got_error(GOT_ERR_PRIVSEP_LEN);
803 59d1e4a0 2021-03-10 stsp memcpy(&iobj, imsg->data, sizeof(iobj));
804 59d1e4a0 2021-03-10 stsp memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
806 59d1e4a0 2021-03-10 stsp obj = got_object_cache_get(objcache, &id);
808 59d1e4a0 2021-03-10 stsp obj->refcnt++;
810 59d1e4a0 2021-03-10 stsp err = open_object(&obj, pack, packidx, iobj.idx, &id,
813 59d1e4a0 2021-03-10 stsp return err;
816 59d1e4a0 2021-03-10 stsp err = receive_file(&outfile, ibuf, GOT_IMSG_RAW_OBJECT_OUTFD);
818 59d1e4a0 2021-03-10 stsp return err;
820 59d1e4a0 2021-03-10 stsp if (obj->flags & GOT_OBJ_FLAG_DELTIFIED) {
821 59d1e4a0 2021-03-10 stsp err = got_pack_get_max_delta_object_size(&size, obj, pack);
825 59d1e4a0 2021-03-10 stsp size = obj->size;
827 59d1e4a0 2021-03-10 stsp if (size <= GOT_PRIVSEP_INLINE_OBJECT_DATA_MAX)
828 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object_to_mem(&buf, &obj->size,
829 59d1e4a0 2021-03-10 stsp obj, pack);
831 59d1e4a0 2021-03-10 stsp err = got_packfile_extract_object(pack, obj, outfile, basefile,
832 59d1e4a0 2021-03-10 stsp accumfile);
836 40e3cb72 2021-06-22 stsp err = got_privsep_send_raw_obj(ibuf, obj->size, obj->hdrlen, buf);
839 59d1e4a0 2021-03-10 stsp if (outfile && fclose(outfile) == EOF && err == NULL)
840 59d1e4a0 2021-03-10 stsp err = got_error_from_errno("fclose");
841 59d1e4a0 2021-03-10 stsp got_object_close(obj);
842 59d1e4a0 2021-03-10 stsp if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
843 59d1e4a0 2021-03-10 stsp got_privsep_send_error(ibuf, err);
845 59d1e4a0 2021-03-10 stsp return err;
848 f9c2e8e5 2022-02-13 thomas static const struct got_error *
849 f9c2e8e5 2022-02-13 thomas get_base_object_id(struct got_object_id *base_id, struct got_packidx *packidx,
850 f9c2e8e5 2022-02-13 thomas off_t base_offset)
852 f9c2e8e5 2022-02-13 thomas const struct got_error *err;
855 f9c2e8e5 2022-02-13 thomas err = got_packidx_get_offset_idx(&idx, packidx, base_offset);
857 f9c2e8e5 2022-02-13 thomas return err;
858 f9c2e8e5 2022-02-13 thomas if (idx == -1)
859 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_BAD_PACKIDX);
861 f9c2e8e5 2022-02-13 thomas return got_packidx_get_object_id(base_id, packidx, idx);
864 59d1e4a0 2021-03-10 stsp static const struct got_error *
865 f9c2e8e5 2022-02-13 thomas raw_delta_request(struct imsg *imsg, struct imsgbuf *ibuf,
866 f9c2e8e5 2022-02-13 thomas FILE *delta_outfile, struct got_pack *pack,
867 f9c2e8e5 2022-02-13 thomas struct got_packidx *packidx)
869 f9c2e8e5 2022-02-13 thomas const struct got_error *err = NULL;
870 f9c2e8e5 2022-02-13 thomas struct got_imsg_raw_delta_request req;
871 f9c2e8e5 2022-02-13 thomas size_t datalen, delta_size;
872 f9c2e8e5 2022-02-13 thomas off_t delta_offset;
873 f9c2e8e5 2022-02-13 thomas uint8_t *delta_buf = NULL;
874 f9c2e8e5 2022-02-13 thomas struct got_object_id id, base_id;
875 f9c2e8e5 2022-02-13 thomas off_t base_offset, delta_out_offset = 0;
876 f9c2e8e5 2022-02-13 thomas uint64_t base_size = 0, result_size = 0;
877 f9c2e8e5 2022-02-13 thomas size_t w;
879 f9c2e8e5 2022-02-13 thomas datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
880 f9c2e8e5 2022-02-13 thomas if (datalen != sizeof(req))
881 f9c2e8e5 2022-02-13 thomas return got_error(GOT_ERR_PRIVSEP_LEN);
882 f9c2e8e5 2022-02-13 thomas memcpy(&req, imsg->data, sizeof(req));
883 f9c2e8e5 2022-02-13 thomas memcpy(id.sha1, req.id, SHA1_DIGEST_LENGTH);
885 f9c2e8e5 2022-02-13 thomas imsg->fd = -1;
887 f9c2e8e5 2022-02-13 thomas err = got_packfile_extract_raw_delta(&delta_buf, &delta_size,
888 f9c2e8e5 2022-02-13 thomas &delta_offset, &base_offset, &base_id, &base_size, &result_size,
889 f9c2e8e5 2022-02-13 thomas pack, packidx, req.idx);
891 f9c2e8e5 2022-02-13 thomas goto done;
894 f9c2e8e5 2022-02-13 thomas * If this is an offset delta we must determine the base
895 f9c2e8e5 2022-02-13 thomas * object ID ourselves.
897 f9c2e8e5 2022-02-13 thomas if (base_offset != 0) {
898 f9c2e8e5 2022-02-13 thomas err = get_base_object_id(&base_id, packidx, base_offset);
900 f9c2e8e5 2022-02-13 thomas goto done;
903 f9c2e8e5 2022-02-13 thomas delta_out_offset = ftello(delta_outfile);
904 f9c2e8e5 2022-02-13 thomas w = fwrite(delta_buf, 1, delta_size, delta_outfile);
905 f9c2e8e5 2022-02-13 thomas if (w != delta_size) {
906 f9c2e8e5 2022-02-13 thomas err = got_ferror(delta_outfile, GOT_ERR_IO);
907 f9c2e8e5 2022-02-13 thomas goto done;
909 f9c2e8e5 2022-02-13 thomas if (fflush(delta_outfile) == -1) {
910 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fflush");
911 f9c2e8e5 2022-02-13 thomas goto done;
914 f9c2e8e5 2022-02-13 thomas err = got_privsep_send_raw_delta(ibuf, base_size, result_size,
915 f9c2e8e5 2022-02-13 thomas delta_size, delta_offset, delta_out_offset, &base_id);
917 f9c2e8e5 2022-02-13 thomas free(delta_buf);
918 f9c2e8e5 2022-02-13 thomas return err;
921 f9c2e8e5 2022-02-13 thomas static const struct got_error *
922 876c234b 2018-09-10 stsp receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
924 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
925 876c234b 2018-09-10 stsp struct imsg imsg;
926 876c234b 2018-09-10 stsp struct got_imsg_packidx ipackidx;
927 876c234b 2018-09-10 stsp size_t datalen;
928 876c234b 2018-09-10 stsp struct got_packidx *p;
930 876c234b 2018-09-10 stsp *packidx = NULL;
932 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
934 876c234b 2018-09-10 stsp return err;
936 876c234b 2018-09-10 stsp p = calloc(1, sizeof(*p));
937 876c234b 2018-09-10 stsp if (p == NULL) {
938 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
942 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
943 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
947 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
948 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
952 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
953 876c234b 2018-09-10 stsp if (datalen != sizeof(ipackidx)) {
954 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
957 876c234b 2018-09-10 stsp memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
959 876c234b 2018-09-10 stsp p->len = ipackidx.len;
960 876c234b 2018-09-10 stsp p->fd = dup(imsg.fd);
961 876c234b 2018-09-10 stsp if (p->fd == -1) {
962 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
965 56bef47a 2018-09-15 stsp if (lseek(p->fd, 0, SEEK_SET) == -1) {
966 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
970 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
971 876c234b 2018-09-10 stsp p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
972 876c234b 2018-09-10 stsp if (p->map == MAP_FAILED)
973 876c234b 2018-09-10 stsp p->map = NULL; /* fall back to read(2) */
975 c3564dfa 2021-07-15 stsp err = got_packidx_init_hdr(p, 1, ipackidx.packfile_size);
978 876c234b 2018-09-10 stsp if (imsg.fd != -1)
979 876c234b 2018-09-10 stsp close(imsg.fd);
980 876c234b 2018-09-10 stsp got_packidx_close(p);
982 876c234b 2018-09-10 stsp *packidx = p;
983 876c234b 2018-09-10 stsp imsg_free(&imsg);
984 876c234b 2018-09-10 stsp return err;
987 876c234b 2018-09-10 stsp static const struct got_error *
988 876c234b 2018-09-10 stsp receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
990 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
991 876c234b 2018-09-10 stsp struct imsg imsg;
992 876c234b 2018-09-10 stsp struct got_imsg_pack ipack;
993 876c234b 2018-09-10 stsp size_t datalen;
994 876c234b 2018-09-10 stsp struct got_pack *pack;
996 876c234b 2018-09-10 stsp *packp = NULL;
998 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, ibuf, 0);
1000 876c234b 2018-09-10 stsp return err;
1002 876c234b 2018-09-10 stsp pack = calloc(1, sizeof(*pack));
1003 876c234b 2018-09-10 stsp if (pack == NULL) {
1004 638f9024 2019-05-13 stsp err = got_error_from_errno("calloc");
1005 876c234b 2018-09-10 stsp goto done;
1008 876c234b 2018-09-10 stsp if (imsg.hdr.type != GOT_IMSG_PACK) {
1009 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1010 876c234b 2018-09-10 stsp goto done;
1013 876c234b 2018-09-10 stsp if (imsg.fd == -1) {
1014 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1015 876c234b 2018-09-10 stsp goto done;
1018 876c234b 2018-09-10 stsp datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
1019 876c234b 2018-09-10 stsp if (datalen != sizeof(ipack)) {
1020 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_LEN);
1021 876c234b 2018-09-10 stsp goto done;
1023 876c234b 2018-09-10 stsp memcpy(&ipack, imsg.data, sizeof(ipack));
1025 876c234b 2018-09-10 stsp pack->filesize = ipack.filesize;
1026 876c234b 2018-09-10 stsp pack->fd = dup(imsg.fd);
1027 876c234b 2018-09-10 stsp if (pack->fd == -1) {
1028 638f9024 2019-05-13 stsp err = got_error_from_errno("dup");
1029 876c234b 2018-09-10 stsp goto done;
1031 56bef47a 2018-09-15 stsp if (lseek(pack->fd, 0, SEEK_SET) == -1) {
1032 638f9024 2019-05-13 stsp err = got_error_from_errno("lseek");
1033 56bef47a 2018-09-15 stsp goto done;
1035 876c234b 2018-09-10 stsp pack->path_packfile = strdup(ipack.path_packfile);
1036 876c234b 2018-09-10 stsp if (pack->path_packfile == NULL) {
1037 638f9024 2019-05-13 stsp err = got_error_from_errno("strdup");
1038 ab2f42e7 2019-11-10 stsp goto done;
1041 ab2f42e7 2019-11-10 stsp pack->delta_cache = got_delta_cache_alloc(100,
1042 ab2f42e7 2019-11-10 stsp GOT_DELTA_RESULT_SIZE_CACHED_MAX);
1043 ab2f42e7 2019-11-10 stsp if (pack->delta_cache == NULL) {
1044 ab2f42e7 2019-11-10 stsp err = got_error_from_errno("got_delta_cache_alloc");
1045 876c234b 2018-09-10 stsp goto done;
1048 876c234b 2018-09-10 stsp #ifndef GOT_PACK_NO_MMAP
1049 876c234b 2018-09-10 stsp pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
1050 876c234b 2018-09-10 stsp pack->fd, 0);
1051 876c234b 2018-09-10 stsp if (pack->map == MAP_FAILED)
1052 876c234b 2018-09-10 stsp pack->map = NULL; /* fall back to read(2) */
1055 876c234b 2018-09-10 stsp if (err) {
1056 876c234b 2018-09-10 stsp if (imsg.fd != -1)
1057 876c234b 2018-09-10 stsp close(imsg.fd);
1058 876c234b 2018-09-10 stsp free(pack);
1060 876c234b 2018-09-10 stsp *packp = pack;
1061 876c234b 2018-09-10 stsp imsg_free(&imsg);
1062 876c234b 2018-09-10 stsp return err;
1066 876c234b 2018-09-10 stsp main(int argc, char *argv[])
1068 876c234b 2018-09-10 stsp const struct got_error *err = NULL;
1069 876c234b 2018-09-10 stsp struct imsgbuf ibuf;
1070 876c234b 2018-09-10 stsp struct imsg imsg;
1071 c59b3346 2018-09-11 stsp struct got_packidx *packidx = NULL;
1072 c59b3346 2018-09-11 stsp struct got_pack *pack = NULL;
1073 c59b3346 2018-09-11 stsp struct got_object_cache objcache;
1074 f9c2e8e5 2022-02-13 thomas FILE *basefile = NULL, *accumfile = NULL, *delta_outfile = NULL;
1076 876c234b 2018-09-10 stsp //static int attached;
1077 876c234b 2018-09-10 stsp //while (!attached) sleep(1);
1079 99437157 2018-11-11 stsp signal(SIGINT, catch_sigint);
1081 876c234b 2018-09-10 stsp imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
1083 c59b3346 2018-09-11 stsp err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
1084 c59b3346 2018-09-11 stsp if (err) {
1085 638f9024 2019-05-13 stsp err = got_error_from_errno("got_object_cache_init");
1086 c59b3346 2018-09-11 stsp got_privsep_send_error(&ibuf, err);
1090 2ff12563 2018-09-15 stsp #ifndef PROFILE
1091 876c234b 2018-09-10 stsp /* revoke access to most system calls */
1092 876c234b 2018-09-10 stsp if (pledge("stdio recvfd", NULL) == -1) {
1093 638f9024 2019-05-13 stsp err = got_error_from_errno("pledge");
1094 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1098 97799ccd 2022-02-06 thomas /* revoke fs access */
1099 97799ccd 2022-02-06 thomas if (landlock_no_fs() == -1) {
1100 97799ccd 2022-02-06 thomas err = got_error_from_errno("landlock_no_fs");
1101 97799ccd 2022-02-06 thomas got_privsep_send_error(&ibuf, err);
1102 97799ccd 2022-02-06 thomas return 1;
1106 876c234b 2018-09-10 stsp err = receive_packidx(&packidx, &ibuf);
1107 876c234b 2018-09-10 stsp if (err) {
1108 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1112 876c234b 2018-09-10 stsp err = receive_pack(&pack, &ibuf);
1113 876c234b 2018-09-10 stsp if (err) {
1114 876c234b 2018-09-10 stsp got_privsep_send_error(&ibuf, err);
1119 876c234b 2018-09-10 stsp imsg.fd = -1;
1121 99437157 2018-11-11 stsp if (sigint_received) {
1122 99437157 2018-11-11 stsp err = got_error(GOT_ERR_CANCELLED);
1126 876c234b 2018-09-10 stsp err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
1127 876c234b 2018-09-10 stsp if (err) {
1128 876c234b 2018-09-10 stsp if (err->code == GOT_ERR_PRIVSEP_PIPE)
1129 876c234b 2018-09-10 stsp err = NULL;
1133 876c234b 2018-09-10 stsp if (imsg.hdr.type == GOT_IMSG_STOP)
1136 876c234b 2018-09-10 stsp switch (imsg.hdr.type) {
1137 bc1f382f 2022-01-05 thomas case GOT_IMSG_TMPFD:
1138 f9c2e8e5 2022-02-13 thomas if (basefile == NULL) {
1139 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&basefile, "w+",
1140 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1141 f9c2e8e5 2022-02-13 thomas } else if (accumfile == NULL) {
1142 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&accumfile, "w+",
1143 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1145 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1147 876c234b 2018-09-10 stsp case GOT_IMSG_PACKED_OBJECT_REQUEST:
1148 c59b3346 2018-09-11 stsp err = object_request(&imsg, &ibuf, pack, packidx,
1149 c59b3346 2018-09-11 stsp &objcache);
1151 59d1e4a0 2021-03-10 stsp case GOT_IMSG_PACKED_RAW_OBJECT_REQUEST:
1152 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
1153 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1156 59d1e4a0 2021-03-10 stsp err = raw_object_request(&imsg, &ibuf, pack, packidx,
1157 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
1159 f9c2e8e5 2022-02-13 thomas case GOT_IMSG_RAW_DELTA_OUTFD:
1160 f9c2e8e5 2022-02-13 thomas if (delta_outfile != NULL) {
1161 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1164 f9c2e8e5 2022-02-13 thomas err = receive_tempfile(&delta_outfile, "w",
1165 f9c2e8e5 2022-02-13 thomas &imsg, &ibuf);
1167 f9c2e8e5 2022-02-13 thomas case GOT_IMSG_RAW_DELTA_REQUEST:
1168 f9c2e8e5 2022-02-13 thomas if (delta_outfile == NULL) {
1169 f9c2e8e5 2022-02-13 thomas err = got_error(GOT_ERR_PRIVSEP_NO_FD);
1172 f9c2e8e5 2022-02-13 thomas err = raw_delta_request(&imsg, &ibuf, delta_outfile,
1173 f9c2e8e5 2022-02-13 thomas pack, packidx);
1175 876c234b 2018-09-10 stsp case GOT_IMSG_COMMIT_REQUEST:
1176 c59b3346 2018-09-11 stsp err = commit_request(&imsg, &ibuf, pack, packidx,
1177 7762fe12 2018-11-05 stsp &objcache);
1179 876c234b 2018-09-10 stsp case GOT_IMSG_TREE_REQUEST:
1180 c59b3346 2018-09-11 stsp err = tree_request(&imsg, &ibuf, pack, packidx,
1181 62d463ca 2020-10-20 naddy &objcache);
1183 876c234b 2018-09-10 stsp case GOT_IMSG_BLOB_REQUEST:
1184 bc1f382f 2022-01-05 thomas if (basefile == NULL || accumfile == NULL) {
1185 bc1f382f 2022-01-05 thomas err = got_error(GOT_ERR_PRIVSEP_MSG);
1188 c59b3346 2018-09-11 stsp err = blob_request(&imsg, &ibuf, pack, packidx,
1189 bc1f382f 2022-01-05 thomas &objcache, basefile, accumfile);
1191 f4a881ce 2018-11-17 stsp case GOT_IMSG_TAG_REQUEST:
1192 f4a881ce 2018-11-17 stsp err = tag_request(&imsg, &ibuf, pack, packidx,
1193 62d463ca 2020-10-20 naddy &objcache);
1195 ca6e02ac 2020-01-07 stsp case GOT_IMSG_COMMIT_TRAVERSAL_REQUEST:
1196 ca6e02ac 2020-01-07 stsp err = commit_traversal_request(&imsg, &ibuf, pack,
1197 ca6e02ac 2020-01-07 stsp packidx, &objcache);
1200 876c234b 2018-09-10 stsp err = got_error(GOT_ERR_PRIVSEP_MSG);
1204 08578a35 2021-01-22 stsp if (imsg.fd != -1 && close(imsg.fd) == -1 && err == NULL)
1205 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1206 876c234b 2018-09-10 stsp imsg_free(&imsg);
1211 c59b3346 2018-09-11 stsp if (packidx)
1212 c59b3346 2018-09-11 stsp got_packidx_close(packidx);
1214 c59b3346 2018-09-11 stsp got_pack_close(pack);
1215 48d5fe42 2018-09-15 stsp got_object_cache_close(&objcache);
1216 876c234b 2018-09-10 stsp imsg_clear(&ibuf);
1217 bc1f382f 2022-01-05 thomas if (basefile && fclose(basefile) == EOF && err == NULL)
1218 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
1219 bc1f382f 2022-01-05 thomas if (accumfile && fclose(accumfile) == EOF && err == NULL)
1220 bc1f382f 2022-01-05 thomas err = got_error_from_errno("fclose");
1221 f9c2e8e5 2022-02-13 thomas if (delta_outfile && fclose(delta_outfile) == EOF && err == NULL)
1222 f9c2e8e5 2022-02-13 thomas err = got_error_from_errno("fclose");
1223 99437157 2018-11-11 stsp if (err) {
1224 80d5f134 2018-11-11 stsp if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE) {
1225 80d5f134 2018-11-11 stsp fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
1226 99437157 2018-11-11 stsp got_privsep_send_error(&ibuf, err);
1229 08578a35 2021-01-22 stsp if (close(GOT_IMSG_FD_CHILD) == -1 && err == NULL)
1230 638f9024 2019-05-13 stsp err = got_error_from_errno("close");
1231 876c234b 2018-09-10 stsp return err ? 1 : 0;