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
10 #include "fuse_misc.h"
11 #include "fuse_common_compat.h"
12 #include "fuse_lowlevel_compat.h"
21 struct fuse_chan_ops op
;
23 struct fuse_session
*se
;
34 struct fuse_session
*fuse_session_new(struct fuse_session_ops
*op
, void *data
)
36 struct fuse_session
*se
= (struct fuse_session
*) malloc(sizeof(*se
));
38 fprintf(stderr
, "fuse: failed to allocate session\n");
42 memset(se
, 0, sizeof(*se
));
49 void fuse_session_add_chan(struct fuse_session
*se
, struct fuse_chan
*ch
)
51 assert(se
->ch
== NULL
);
52 assert(ch
->se
== NULL
);
57 void fuse_session_remove_chan(struct fuse_chan
*ch
)
59 struct fuse_session
*se
= ch
->se
;
67 struct fuse_chan
*fuse_session_next_chan(struct fuse_session
*se
,
70 assert(ch
== NULL
|| ch
== se
->ch
);
77 void fuse_session_process(struct fuse_session
*se
, const char *buf
, size_t len
,
80 se
->op
.process(se
->data
, buf
, len
, ch
);
83 void fuse_session_process_buf(struct fuse_session
*se
,
84 const struct fuse_buf
*buf
, struct fuse_chan
*ch
)
86 if (se
->process_buf
) {
87 se
->process_buf(se
->data
, buf
, ch
);
89 assert(!(buf
->flags
& FUSE_BUF_IS_FD
));
90 fuse_session_process(se
->data
, buf
->mem
, buf
->size
, ch
);
94 int fuse_session_receive_buf(struct fuse_session
*se
, struct fuse_buf
*buf
,
95 struct fuse_chan
**chp
)
99 if (se
->receive_buf
) {
100 res
= se
->receive_buf(se
, buf
, chp
);
102 res
= fuse_chan_recv(chp
, buf
->mem
, buf
->size
);
111 void fuse_session_destroy(struct fuse_session
*se
)
114 se
->op
.destroy(se
->data
);
116 fuse_chan_destroy(se
->ch
);
120 void fuse_session_exit(struct fuse_session
*se
)
123 se
->op
.exit(se
->data
, 1);
127 void fuse_session_reset(struct fuse_session
*se
)
130 se
->op
.exit(se
->data
, 0);
134 int fuse_session_exited(struct fuse_session
*se
)
137 return se
->op
.exited(se
->data
);
142 void *fuse_session_data(struct fuse_session
*se
)
147 static struct fuse_chan
*fuse_chan_new_common(struct fuse_chan_ops
*op
, int fd
,
148 size_t bufsize
, void *data
,
151 struct fuse_chan
*ch
= (struct fuse_chan
*) malloc(sizeof(*ch
));
153 fprintf(stderr
, "fuse: failed to allocate channel\n");
157 memset(ch
, 0, sizeof(*ch
));
160 ch
->bufsize
= bufsize
;
167 struct fuse_chan
*fuse_chan_new(struct fuse_chan_ops
*op
, int fd
,
168 size_t bufsize
, void *data
)
170 return fuse_chan_new_common(op
, fd
, bufsize
, data
, 0);
173 struct fuse_chan
*fuse_chan_new_compat24(struct fuse_chan_ops_compat24
*op
,
174 int fd
, size_t bufsize
, void *data
)
176 return fuse_chan_new_common((struct fuse_chan_ops
*) op
, fd
, bufsize
,
180 int fuse_chan_fd(struct fuse_chan
*ch
)
185 size_t fuse_chan_bufsize(struct fuse_chan
*ch
)
190 void *fuse_chan_data(struct fuse_chan
*ch
)
195 struct fuse_session
*fuse_chan_session(struct fuse_chan
*ch
)
200 int fuse_chan_recv(struct fuse_chan
**chp
, char *buf
, size_t size
)
202 struct fuse_chan
*ch
= *chp
;
204 return ((struct fuse_chan_ops_compat24
*) &ch
->op
)
205 ->receive(ch
, buf
, size
);
207 return ch
->op
.receive(chp
, buf
, size
);
210 int fuse_chan_receive(struct fuse_chan
*ch
, char *buf
, size_t size
)
214 res
= fuse_chan_recv(&ch
, buf
, size
);
215 return res
>= 0 ? res
: (res
!= -EINTR
&& res
!= -EAGAIN
) ? -1 : 0;
218 int fuse_chan_send(struct fuse_chan
*ch
, const struct iovec iov
[], size_t count
)
220 return ch
->op
.send(ch
, iov
, count
);
223 void fuse_chan_destroy(struct fuse_chan
*ch
)
225 fuse_session_remove_chan(ch
);
232 FUSE_SYMVER(".symver fuse_chan_new_compat24,fuse_chan_new@FUSE_2.4");