Allow IPv6 address entry in tools>ping - Loosens valid character check
[tomato/davidwu.git] / release / src / router / ntfs-3g / libfuse-lite / fuse_session.c
blob3773303a55ff379aa5dc131bdf52e68e519e4d20
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 "config.h"
10 #include "fuse_lowlevel.h"
11 #include "fuse_lowlevel_compat.h"
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <assert.h>
17 #include <errno.h>
19 struct fuse_session {
20 struct fuse_session_ops op;
22 void *data;
24 volatile int exited;
26 struct fuse_chan *ch;
29 struct fuse_chan {
30 struct fuse_chan_ops op;
32 struct fuse_session *se;
34 int fd;
36 size_t bufsize;
38 void *data;
41 struct fuse_session *fuse_session_new(struct fuse_session_ops *op, void *data)
43 struct fuse_session *se = (struct fuse_session *) malloc(sizeof(*se));
44 if (se == NULL) {
45 fprintf(stderr, "fuse: failed to allocate session\n");
46 return NULL;
49 memset(se, 0, sizeof(*se));
50 se->op = *op;
51 se->data = data;
53 return se;
56 void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch)
58 assert(se->ch == NULL);
59 assert(ch->se == NULL);
60 se->ch = ch;
61 ch->se = se;
64 void fuse_session_remove_chan(struct fuse_chan *ch)
66 struct fuse_session *se = ch->se;
67 if (se) {
68 assert(se->ch == ch);
69 se->ch = NULL;
70 ch->se = NULL;
74 struct fuse_chan *fuse_session_next_chan(struct fuse_session *se,
75 struct fuse_chan *ch)
77 assert(ch == NULL || ch == se->ch);
78 if (ch == NULL)
79 return se->ch;
80 else
81 return NULL;
84 void fuse_session_process(struct fuse_session *se, const char *buf, size_t len,
85 struct fuse_chan *ch)
87 se->op.process(se->data, buf, len, ch);
90 void fuse_session_destroy(struct fuse_session *se)
92 if (se->op.destroy)
93 se->op.destroy(se->data);
94 if (se->ch != NULL)
95 fuse_chan_destroy(se->ch);
96 free(se);
99 void fuse_session_exit(struct fuse_session *se)
101 if (se->op.exit)
102 se->op.exit(se->data, 1);
103 se->exited = 1;
106 void fuse_session_reset(struct fuse_session *se)
108 if (se->op.exit)
109 se->op.exit(se->data, 0);
110 se->exited = 0;
113 int fuse_session_exited(struct fuse_session *se)
115 if (se->op.exited)
116 return se->op.exited(se->data);
117 else
118 return se->exited;
121 static struct fuse_chan *fuse_chan_new_common(struct fuse_chan_ops *op, int fd,
122 size_t bufsize, void *data)
124 struct fuse_chan *ch = (struct fuse_chan *) malloc(sizeof(*ch));
125 if (ch == NULL) {
126 fprintf(stderr, "fuse: failed to allocate channel\n");
127 return NULL;
130 memset(ch, 0, sizeof(*ch));
131 ch->op = *op;
132 ch->fd = fd;
133 ch->bufsize = bufsize;
134 ch->data = data;
136 return ch;
139 struct fuse_chan *fuse_chan_new(struct fuse_chan_ops *op, int fd,
140 size_t bufsize, void *data)
142 return fuse_chan_new_common(op, fd, bufsize, data);
145 int fuse_chan_fd(struct fuse_chan *ch)
147 return ch->fd;
150 size_t fuse_chan_bufsize(struct fuse_chan *ch)
152 return ch->bufsize;
155 void *fuse_chan_data(struct fuse_chan *ch)
157 return ch->data;
160 struct fuse_session *fuse_chan_session(struct fuse_chan *ch)
162 return ch->se;
165 int fuse_chan_recv(struct fuse_chan **chp, char *buf, size_t size)
167 struct fuse_chan *ch = *chp;
169 return ch->op.receive(chp, buf, size);
172 #ifdef __SOLARIS__
173 int fuse_chan_receive(struct fuse_chan *ch, char *buf, size_t size)
175 int res;
177 res = fuse_chan_recv(&ch, buf, size);
178 return res >= 0 ? res : (res != -EINTR && res != -EAGAIN) ? -1 : 0;
180 #endif /* __SOLARIS__ */
182 int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[], size_t count)
184 return ch->op.send(ch, iov, count);
187 void fuse_chan_destroy(struct fuse_chan *ch)
189 fuse_session_remove_chan(ch);
190 if (ch->op.destroy)
191 ch->op.destroy(ch);
192 free(ch);