2 * Copyright (c) 2018 Stefan Sperling <stsp@openbsd.org>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 #include <sys/types.h>
18 #include <sys/queue.h>
21 #include <sys/limits.h>
22 #include <sys/syslimits.h>
34 #include "got_error.h"
35 #include "got_object.h"
37 #include "got_lib_delta.h"
38 #include "got_lib_inflate.h"
39 #include "got_lib_object.h"
40 #include "got_lib_object_cache.h"
41 #include "got_lib_object_parse.h"
42 #include "got_lib_privsep.h"
43 #include "got_lib_pack.h"
45 static const struct got_error *
46 object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
47 struct got_packidx *packidx, struct got_object_cache *objcache)
49 const struct got_error *err = NULL;
50 struct got_imsg_packed_object iobj;
51 struct got_object *obj;
54 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
55 if (datalen != sizeof(iobj))
56 return got_error(GOT_ERR_PRIVSEP_LEN);
57 memcpy(&iobj, imsg->data, sizeof(iobj));
59 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, NULL);
64 err = got_object_cache_add(objcache, &obj->id, obj);
69 err = got_privsep_send_obj(ibuf, obj);
71 got_object_close(obj);
75 static const struct got_error *
76 get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
77 struct got_pack *pack, struct got_packidx *packidx,
78 struct got_object_cache *objcache, int type)
80 const struct got_error *err = NULL;
81 struct got_object *iobj;
83 err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
87 if (iobj->type != type) {
88 err = got_error(GOT_ERR_OBJ_TYPE);
92 if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
93 return got_error(GOT_ERR_OBJ_NOT_PACKED);
95 *obj = got_object_cache_get(objcache, &iobj->id);
97 err = got_packfile_open_object(obj, pack, packidx,
98 iobj->pack_idx, &iobj->id);
104 got_object_close(iobj);
108 static const struct got_error *
109 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
110 struct got_packidx *packidx, struct got_object_cache *objcache)
112 const struct got_error *err = NULL;
113 struct got_object *obj = NULL;
114 struct got_commit_object *commit = NULL;
118 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
119 GOT_OBJ_TYPE_COMMIT);
123 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
128 err = got_object_parse_commit(&commit, buf, len);
131 err = got_privsep_send_commit(ibuf, commit);
133 got_object_close(obj);
134 got_object_commit_close(commit);
136 if (err->code == GOT_ERR_PRIVSEP_PIPE)
139 got_privsep_send_error(ibuf, err);
145 static const struct got_error *
146 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
147 struct got_packidx *packidx, struct got_object_cache *objcache)
149 const struct got_error *err = NULL;
150 struct got_object *obj = NULL;
151 struct got_tree_object *tree = NULL;
155 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
160 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
165 err = got_object_parse_tree(&tree, buf, len);
168 err = got_privsep_send_tree(ibuf, tree);
170 got_object_close(obj);
171 got_object_tree_close(tree);
173 if (err->code == GOT_ERR_PRIVSEP_PIPE)
176 got_privsep_send_error(ibuf, err);
182 static const struct got_error *
183 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
185 const struct got_error *err;
189 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
193 if (imsg.hdr.type != imsg_code) {
194 err = got_error(GOT_ERR_PRIVSEP_MSG);
198 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
200 err = got_error(GOT_ERR_PRIVSEP_LEN);
204 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
208 *f = fdopen(imsg.fd, "w+");
211 err = got_error_from_errno();
219 static const struct got_error *
220 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
221 struct got_packidx *packidx, struct got_object_cache *objcache)
223 const struct got_error *err = NULL;
224 struct got_object *obj = NULL;
225 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
227 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
232 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
235 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
238 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
242 err = got_packfile_extract_object(pack, obj, outfile, basefile,
247 err = got_privsep_send_blob(ibuf, obj->size);
256 got_object_close(obj);
257 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
258 got_privsep_send_error(ibuf, err);
263 static const struct got_error *
264 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
266 const struct got_error *err = NULL;
268 struct got_imsg_packidx ipackidx;
270 struct got_packidx *p;
274 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
278 p = calloc(1, sizeof(*p));
280 err = got_error_from_errno();
284 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
285 err = got_error(GOT_ERR_PRIVSEP_MSG);
290 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
294 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
295 if (datalen != sizeof(ipackidx)) {
296 err = got_error(GOT_ERR_PRIVSEP_LEN);
299 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
301 p->len = ipackidx.len;
302 p->fd = dup(imsg.fd);
304 err = got_error_from_errno();
307 if (lseek(p->fd, 0, SEEK_SET) == -1) {
308 err = got_error_from_errno();
312 #ifndef GOT_PACK_NO_MMAP
313 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
314 if (p->map == MAP_FAILED)
315 p->map = NULL; /* fall back to read(2) */
317 err = got_packidx_init_hdr(p, 1);
322 got_packidx_close(p);
329 static const struct got_error *
330 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
332 const struct got_error *err = NULL;
334 struct got_imsg_pack ipack;
336 struct got_pack *pack;
340 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
344 pack = calloc(1, sizeof(*pack));
346 err = got_error_from_errno();
350 if (imsg.hdr.type != GOT_IMSG_PACK) {
351 err = got_error(GOT_ERR_PRIVSEP_MSG);
356 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
360 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
361 if (datalen != sizeof(ipack)) {
362 err = got_error(GOT_ERR_PRIVSEP_LEN);
365 memcpy(&ipack, imsg.data, sizeof(ipack));
367 pack->filesize = ipack.filesize;
368 pack->fd = dup(imsg.fd);
369 if (pack->fd == -1) {
370 err = got_error_from_errno();
373 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
374 err = got_error_from_errno();
377 pack->path_packfile = strdup(ipack.path_packfile);
378 if (pack->path_packfile == NULL) {
379 err = got_error_from_errno();
383 #ifndef GOT_PACK_NO_MMAP
384 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
386 if (pack->map == MAP_FAILED)
387 pack->map = NULL; /* fall back to read(2) */
401 main(int argc, char *argv[])
403 const struct got_error *err = NULL;
406 struct got_packidx *packidx = NULL;
407 struct got_pack *pack = NULL;
408 struct got_object_cache objcache;
410 //static int attached;
411 //while (!attached) sleep(1);
413 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
415 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
417 err = got_error_from_errno();
418 got_privsep_send_error(&ibuf, err);
423 /* revoke access to most system calls */
424 if (pledge("stdio recvfd", NULL) == -1) {
425 err = got_error_from_errno();
426 got_privsep_send_error(&ibuf, err);
431 err = receive_packidx(&packidx, &ibuf);
433 got_privsep_send_error(&ibuf, err);
437 err = receive_pack(&pack, &ibuf);
439 got_privsep_send_error(&ibuf, err);
446 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
448 if (err->code == GOT_ERR_PRIVSEP_PIPE)
453 if (imsg.hdr.type == GOT_IMSG_STOP)
456 switch (imsg.hdr.type) {
457 case GOT_IMSG_PACKED_OBJECT_REQUEST:
458 err = object_request(&imsg, &ibuf, pack, packidx,
461 case GOT_IMSG_COMMIT_REQUEST:
462 err = commit_request(&imsg, &ibuf, pack, packidx,
465 case GOT_IMSG_TREE_REQUEST:
466 err = tree_request(&imsg, &ibuf, pack, packidx,
469 case GOT_IMSG_BLOB_REQUEST:
470 err = blob_request(&imsg, &ibuf, pack, packidx,
474 err = got_error(GOT_ERR_PRIVSEP_MSG);
482 if (err->code == GOT_ERR_PRIVSEP_PIPE)
485 got_privsep_send_error(&ibuf, err);
491 got_packidx_close(packidx);
493 got_pack_close(pack);
496 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
497 close(GOT_IMSG_FD_CHILD);