Interpret octal escape codes in options
[fuse.git] / lib / fuse_kern_chan.c
blob5f77bbf5628d443feb8c7792ea85f18c7bf228bc
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB
7 */
9 #include "fuse_lowlevel.h"
10 #include "fuse_kernel.h"
11 #include "fuse_i.h"
13 #include <stdio.h>
14 #include <errno.h>
15 #include <unistd.h>
16 #include <assert.h>
18 static int fuse_kern_chan_receive(struct fuse_chan **chp, char *buf,
19 size_t size)
21 struct fuse_chan *ch = *chp;
22 int err;
23 ssize_t res;
24 struct fuse_session *se = fuse_chan_session(ch);
25 assert(se != NULL);
27 restart:
28 res = read(fuse_chan_fd(ch), buf, size);
29 err = errno;
31 if (fuse_session_exited(se))
32 return 0;
33 if (res == -1) {
34 /* ENOENT means the operation was interrupted, it's safe
35 to restart */
36 if (err == ENOENT)
37 goto restart;
39 if (err == ENODEV) {
40 fuse_session_exit(se);
41 return 0;
43 /* Errors occurring during normal operation: EINTR (read
44 interrupted), EAGAIN (nonblocking I/O), ENODEV (filesystem
45 umounted) */
46 if (err != EINTR && err != EAGAIN)
47 perror("fuse: reading device");
48 return -err;
50 if ((size_t) res < sizeof(struct fuse_in_header)) {
51 fprintf(stderr, "short read on fuse device\n");
52 return -EIO;
54 return res;
57 static int fuse_kern_chan_send(struct fuse_chan *ch, const struct iovec iov[],
58 size_t count)
60 if (iov) {
61 ssize_t res = writev(fuse_chan_fd(ch), iov, count);
62 int err = errno;
64 if (res == -1) {
65 struct fuse_session *se = fuse_chan_session(ch);
67 assert(se != NULL);
69 /* ENOENT means the operation was interrupted */
70 if (!fuse_session_exited(se) && err != ENOENT)
71 perror("fuse: writing device");
72 return -err;
75 return 0;
78 static void fuse_kern_chan_destroy(struct fuse_chan *ch)
80 close(fuse_chan_fd(ch));
83 #define MIN_BUFSIZE 0x21000
85 struct fuse_chan *fuse_kern_chan_new(int fd)
87 struct fuse_chan_ops op = {
88 .receive = fuse_kern_chan_receive,
89 .send = fuse_kern_chan_send,
90 .destroy = fuse_kern_chan_destroy,
92 size_t bufsize = getpagesize() + 0x1000;
93 bufsize = bufsize < MIN_BUFSIZE ? MIN_BUFSIZE : bufsize;
94 return fuse_chan_new(&op, fd, bufsize, NULL);