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;
52 struct got_object_id id;
55 datalen = imsg->hdr.len - IMSG_HEADER_SIZE;
56 if (datalen != sizeof(iobj))
57 return got_error(GOT_ERR_PRIVSEP_LEN);
58 memcpy(&iobj, imsg->data, sizeof(iobj));
59 memcpy(id.sha1, iobj.id, SHA1_DIGEST_LENGTH);
61 err = got_packfile_open_object(&obj, pack, packidx, iobj.idx, &id);
66 err = got_object_cache_add(objcache, &obj->id, obj);
71 err = got_privsep_send_obj(ibuf, obj);
73 got_object_close(obj);
77 static const struct got_error *
78 get_object(struct got_object **obj, struct imsg *imsg, struct imsgbuf *ibuf,
79 struct got_pack *pack, struct got_packidx *packidx,
80 struct got_object_cache *objcache, int type)
82 const struct got_error *err = NULL;
83 struct got_object *iobj;
85 err = got_privsep_get_imsg_obj(&iobj, imsg, ibuf);
89 if (iobj->type != type) {
90 err = got_error(GOT_ERR_OBJ_TYPE);
94 if ((iobj->flags & GOT_OBJ_FLAG_PACKED) == 0)
95 return got_error(GOT_ERR_OBJ_NOT_PACKED);
97 *obj = got_object_cache_get(objcache, &iobj->id);
99 err = got_packfile_open_object(obj, pack, packidx,
100 iobj->pack_idx, &iobj->id);
106 got_object_close(iobj);
110 static const struct got_error *
111 commit_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
112 struct got_packidx *packidx, struct got_object_cache *objcache)
114 const struct got_error *err = NULL;
115 struct got_object *obj = NULL;
116 struct got_commit_object *commit = NULL;
120 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
121 GOT_OBJ_TYPE_COMMIT);
125 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
130 err = got_object_parse_commit(&commit, buf, len);
133 err = got_privsep_send_commit(ibuf, commit);
135 got_object_close(obj);
136 got_object_commit_close(commit);
138 if (err->code == GOT_ERR_PRIVSEP_PIPE)
141 got_privsep_send_error(ibuf, err);
147 static const struct got_error *
148 tree_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
149 struct got_packidx *packidx, struct got_object_cache *objcache)
151 const struct got_error *err = NULL;
152 struct got_object *obj = NULL;
153 struct got_tree_object *tree = NULL;
157 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
162 err = got_packfile_extract_object_to_mem(&buf, &len, obj, pack);
167 err = got_object_parse_tree(&tree, buf, len);
170 err = got_privsep_send_tree(ibuf, tree);
172 got_object_close(obj);
173 got_object_tree_close(tree);
175 if (err->code == GOT_ERR_PRIVSEP_PIPE)
178 got_privsep_send_error(ibuf, err);
184 static const struct got_error *
185 receive_file(FILE **f, struct imsgbuf *ibuf, int imsg_code)
187 const struct got_error *err;
191 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
195 if (imsg.hdr.type != imsg_code) {
196 err = got_error(GOT_ERR_PRIVSEP_MSG);
200 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
202 err = got_error(GOT_ERR_PRIVSEP_LEN);
206 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
210 *f = fdopen(imsg.fd, "w+");
213 err = got_error_from_errno();
221 static const struct got_error *
222 blob_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
223 struct got_packidx *packidx, struct got_object_cache *objcache)
225 const struct got_error *err = NULL;
226 struct got_object *obj = NULL;
227 FILE *outfile = NULL, *basefile = NULL, *accumfile = NULL;
229 err = get_object(&obj, imsg, ibuf, pack, packidx, objcache,
234 err = receive_file(&outfile, ibuf, GOT_IMSG_BLOB_OUTFD);
237 err = receive_file(&basefile, ibuf, GOT_IMSG_TMPFD);
240 err = receive_file(&accumfile, ibuf, GOT_IMSG_TMPFD);
244 err = got_packfile_extract_object(pack, obj, outfile, basefile,
249 err = got_privsep_send_blob(ibuf, obj->size);
258 got_object_close(obj);
259 if (err && err->code != GOT_ERR_PRIVSEP_PIPE)
260 got_privsep_send_error(ibuf, err);
265 static const struct got_error *
266 receive_packidx(struct got_packidx **packidx, struct imsgbuf *ibuf)
268 const struct got_error *err = NULL;
270 struct got_imsg_packidx ipackidx;
272 struct got_packidx *p;
276 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
280 p = calloc(1, sizeof(*p));
282 err = got_error_from_errno();
286 if (imsg.hdr.type != GOT_IMSG_PACKIDX) {
287 err = got_error(GOT_ERR_PRIVSEP_MSG);
292 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
296 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
297 if (datalen != sizeof(ipackidx)) {
298 err = got_error(GOT_ERR_PRIVSEP_LEN);
301 memcpy(&ipackidx, imsg.data, sizeof(ipackidx));
303 p->len = ipackidx.len;
304 p->fd = dup(imsg.fd);
306 err = got_error_from_errno();
309 if (lseek(p->fd, 0, SEEK_SET) == -1) {
310 err = got_error_from_errno();
314 #ifndef GOT_PACK_NO_MMAP
315 p->map = mmap(NULL, p->len, PROT_READ, MAP_PRIVATE, p->fd, 0);
316 if (p->map == MAP_FAILED)
317 p->map = NULL; /* fall back to read(2) */
319 err = got_packidx_init_hdr(p, 1);
324 got_packidx_close(p);
331 static const struct got_error *
332 receive_pack(struct got_pack **packp, struct imsgbuf *ibuf)
334 const struct got_error *err = NULL;
336 struct got_imsg_pack ipack;
338 struct got_pack *pack;
342 err = got_privsep_recv_imsg(&imsg, ibuf, 0);
346 pack = calloc(1, sizeof(*pack));
348 err = got_error_from_errno();
352 if (imsg.hdr.type != GOT_IMSG_PACK) {
353 err = got_error(GOT_ERR_PRIVSEP_MSG);
358 err = got_error(GOT_ERR_PRIVSEP_NO_FD);
362 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
363 if (datalen != sizeof(ipack)) {
364 err = got_error(GOT_ERR_PRIVSEP_LEN);
367 memcpy(&ipack, imsg.data, sizeof(ipack));
369 pack->filesize = ipack.filesize;
370 pack->fd = dup(imsg.fd);
371 if (pack->fd == -1) {
372 err = got_error_from_errno();
375 if (lseek(pack->fd, 0, SEEK_SET) == -1) {
376 err = got_error_from_errno();
379 pack->path_packfile = strdup(ipack.path_packfile);
380 if (pack->path_packfile == NULL) {
381 err = got_error_from_errno();
385 #ifndef GOT_PACK_NO_MMAP
386 pack->map = mmap(NULL, pack->filesize, PROT_READ, MAP_PRIVATE,
388 if (pack->map == MAP_FAILED)
389 pack->map = NULL; /* fall back to read(2) */
403 main(int argc, char *argv[])
405 const struct got_error *err = NULL;
408 struct got_packidx *packidx = NULL;
409 struct got_pack *pack = NULL;
410 struct got_object_cache objcache;
412 //static int attached;
413 //while (!attached) sleep(1);
415 imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
417 err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
419 err = got_error_from_errno();
420 got_privsep_send_error(&ibuf, err);
425 /* revoke access to most system calls */
426 if (pledge("stdio recvfd", NULL) == -1) {
427 err = got_error_from_errno();
428 got_privsep_send_error(&ibuf, err);
433 err = receive_packidx(&packidx, &ibuf);
435 got_privsep_send_error(&ibuf, err);
439 err = receive_pack(&pack, &ibuf);
441 got_privsep_send_error(&ibuf, err);
448 err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
450 if (err->code == GOT_ERR_PRIVSEP_PIPE)
455 if (imsg.hdr.type == GOT_IMSG_STOP)
458 switch (imsg.hdr.type) {
459 case GOT_IMSG_PACKED_OBJECT_REQUEST:
460 err = object_request(&imsg, &ibuf, pack, packidx,
463 case GOT_IMSG_COMMIT_REQUEST:
464 err = commit_request(&imsg, &ibuf, pack, packidx,
467 case GOT_IMSG_TREE_REQUEST:
468 err = tree_request(&imsg, &ibuf, pack, packidx,
471 case GOT_IMSG_BLOB_REQUEST:
472 err = blob_request(&imsg, &ibuf, pack, packidx,
476 err = got_error(GOT_ERR_PRIVSEP_MSG);
484 if (err->code == GOT_ERR_PRIVSEP_PIPE)
487 got_privsep_send_error(&ibuf, err);
493 got_packidx_close(packidx);
495 got_pack_close(pack);
496 got_object_cache_close(&objcache);
499 fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
500 close(GOT_IMSG_FD_CHILD);