Add support for ioctl on directories
[fuse.git] / lib / fuse_session.c
blobc55f250747172582028a834a78af8eaf452c6b83
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_i.h"
10 #include "fuse_misc.h"
11 #include "fuse_common_compat.h"
12 #include "fuse_lowlevel_compat.h"
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <assert.h>
18 #include <errno.h>
20 struct fuse_chan {
21 struct fuse_chan_ops op;
23 struct fuse_session *se;
25 int fd;
27 size_t bufsize;
29 void *data;
31 int compat;
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));
37 if (se == NULL) {
38 fprintf(stderr, "fuse: failed to allocate session\n");
39 return NULL;
42 memset(se, 0, sizeof(*se));
43 se->op = *op;
44 se->data = data;
46 return 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);
53 se->ch = ch;
54 ch->se = se;
57 void fuse_session_remove_chan(struct fuse_chan *ch)
59 struct fuse_session *se = ch->se;
60 if (se) {
61 assert(se->ch == ch);
62 se->ch = NULL;
63 ch->se = NULL;
67 struct fuse_chan *fuse_session_next_chan(struct fuse_session *se,
68 struct fuse_chan *ch)
70 assert(ch == NULL || ch == se->ch);
71 if (ch == NULL)
72 return se->ch;
73 else
74 return NULL;
77 void fuse_session_process(struct fuse_session *se, const char *buf, size_t len,
78 struct fuse_chan *ch)
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);
88 } else {
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)
97 int res;
99 if (se->receive_buf) {
100 res = se->receive_buf(se, buf, chp);
101 } else {
102 res = fuse_chan_recv(chp, buf->mem, buf->size);
103 if (res > 0)
104 buf->size = res;
107 return res;
111 void fuse_session_destroy(struct fuse_session *se)
113 if (se->op.destroy)
114 se->op.destroy(se->data);
115 if (se->ch != NULL)
116 fuse_chan_destroy(se->ch);
117 free(se);
120 void fuse_session_exit(struct fuse_session *se)
122 if (se->op.exit)
123 se->op.exit(se->data, 1);
124 se->exited = 1;
127 void fuse_session_reset(struct fuse_session *se)
129 if (se->op.exit)
130 se->op.exit(se->data, 0);
131 se->exited = 0;
134 int fuse_session_exited(struct fuse_session *se)
136 if (se->op.exited)
137 return se->op.exited(se->data);
138 else
139 return se->exited;
142 void *fuse_session_data(struct fuse_session *se)
144 return se->data;
147 static struct fuse_chan *fuse_chan_new_common(struct fuse_chan_ops *op, int fd,
148 size_t bufsize, void *data,
149 int compat)
151 struct fuse_chan *ch = (struct fuse_chan *) malloc(sizeof(*ch));
152 if (ch == NULL) {
153 fprintf(stderr, "fuse: failed to allocate channel\n");
154 return NULL;
157 memset(ch, 0, sizeof(*ch));
158 ch->op = *op;
159 ch->fd = fd;
160 ch->bufsize = bufsize;
161 ch->data = data;
162 ch->compat = compat;
164 return ch;
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,
177 data, 24);
180 int fuse_chan_fd(struct fuse_chan *ch)
182 return ch->fd;
185 size_t fuse_chan_bufsize(struct fuse_chan *ch)
187 return ch->bufsize;
190 void *fuse_chan_data(struct fuse_chan *ch)
192 return ch->data;
195 struct fuse_session *fuse_chan_session(struct fuse_chan *ch)
197 return ch->se;
200 int fuse_chan_recv(struct fuse_chan **chp, char *buf, size_t size)
202 struct fuse_chan *ch = *chp;
203 if (ch->compat)
204 return ((struct fuse_chan_ops_compat24 *) &ch->op)
205 ->receive(ch, buf, size);
206 else
207 return ch->op.receive(chp, buf, size);
210 int fuse_chan_receive(struct fuse_chan *ch, char *buf, size_t size)
212 int res;
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);
226 if (ch->op.destroy)
227 ch->op.destroy(ch);
228 free(ch);
231 #ifndef __FreeBSD__
232 FUSE_SYMVER(".symver fuse_chan_new_compat24,fuse_chan_new@FUSE_2.4");
233 #endif