blob: c55f250747172582028a834a78af8eaf452c6b83 [file] [log] [blame]
bigbiff bigbiff9c754052013-01-09 09:09:08 -05001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
4
5 This program can be distributed under the terms of the GNU LGPLv2.
6 See the file COPYING.LIB
7*/
8
9#include "fuse_i.h"
10#include "fuse_misc.h"
11#include "fuse_common_compat.h"
12#include "fuse_lowlevel_compat.h"
13
14#include <stdio.h>
15#include <stdlib.h>
16#include <string.h>
17#include <assert.h>
18#include <errno.h>
19
20struct fuse_chan {
21 struct fuse_chan_ops op;
22
23 struct fuse_session *se;
24
25 int fd;
26
27 size_t bufsize;
28
29 void *data;
30
31 int compat;
32};
33
34struct fuse_session *fuse_session_new(struct fuse_session_ops *op, void *data)
35{
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;
40 }
41
42 memset(se, 0, sizeof(*se));
43 se->op = *op;
44 se->data = data;
45
46 return se;
47}
48
49void fuse_session_add_chan(struct fuse_session *se, struct fuse_chan *ch)
50{
51 assert(se->ch == NULL);
52 assert(ch->se == NULL);
53 se->ch = ch;
54 ch->se = se;
55}
56
57void fuse_session_remove_chan(struct fuse_chan *ch)
58{
59 struct fuse_session *se = ch->se;
60 if (se) {
61 assert(se->ch == ch);
62 se->ch = NULL;
63 ch->se = NULL;
64 }
65}
66
67struct fuse_chan *fuse_session_next_chan(struct fuse_session *se,
68 struct fuse_chan *ch)
69{
70 assert(ch == NULL || ch == se->ch);
71 if (ch == NULL)
72 return se->ch;
73 else
74 return NULL;
75}
76
77void fuse_session_process(struct fuse_session *se, const char *buf, size_t len,
78 struct fuse_chan *ch)
79{
80 se->op.process(se->data, buf, len, ch);
81}
82
Dees_Troye34c1332013-02-06 19:13:00 +000083void fuse_session_process_buf(struct fuse_session *se,
84 const struct fuse_buf *buf, struct fuse_chan *ch)
85{
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);
91 }
92}
93
94int fuse_session_receive_buf(struct fuse_session *se, struct fuse_buf *buf,
95 struct fuse_chan **chp)
96{
97 int res;
98
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;
105 }
106
107 return res;
108}
109
110
bigbiff bigbiff9c754052013-01-09 09:09:08 -0500111void fuse_session_destroy(struct fuse_session *se)
112{
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);
118}
119
120void fuse_session_exit(struct fuse_session *se)
121{
122 if (se->op.exit)
123 se->op.exit(se->data, 1);
124 se->exited = 1;
125}
126
127void fuse_session_reset(struct fuse_session *se)
128{
129 if (se->op.exit)
130 se->op.exit(se->data, 0);
131 se->exited = 0;
132}
133
134int fuse_session_exited(struct fuse_session *se)
135{
136 if (se->op.exited)
137 return se->op.exited(se->data);
138 else
139 return se->exited;
140}
141
142void *fuse_session_data(struct fuse_session *se)
143{
144 return se->data;
145}
146
147static struct fuse_chan *fuse_chan_new_common(struct fuse_chan_ops *op, int fd,
148 size_t bufsize, void *data,
149 int compat)
150{
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;
155 }
156
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;
163
164 return ch;
165}
166
167struct fuse_chan *fuse_chan_new(struct fuse_chan_ops *op, int fd,
168 size_t bufsize, void *data)
169{
170 return fuse_chan_new_common(op, fd, bufsize, data, 0);
171}
172
173struct fuse_chan *fuse_chan_new_compat24(struct fuse_chan_ops_compat24 *op,
174 int fd, size_t bufsize, void *data)
175{
176 return fuse_chan_new_common((struct fuse_chan_ops *) op, fd, bufsize,
177 data, 24);
178}
179
180int fuse_chan_fd(struct fuse_chan *ch)
181{
182 return ch->fd;
183}
184
185size_t fuse_chan_bufsize(struct fuse_chan *ch)
186{
187 return ch->bufsize;
188}
189
190void *fuse_chan_data(struct fuse_chan *ch)
191{
192 return ch->data;
193}
194
195struct fuse_session *fuse_chan_session(struct fuse_chan *ch)
196{
197 return ch->se;
198}
199
200int fuse_chan_recv(struct fuse_chan **chp, char *buf, size_t size)
201{
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);
208}
209
210int fuse_chan_receive(struct fuse_chan *ch, char *buf, size_t size)
211{
212 int res;
213
214 res = fuse_chan_recv(&ch, buf, size);
215 return res >= 0 ? res : (res != -EINTR && res != -EAGAIN) ? -1 : 0;
216}
217
218int fuse_chan_send(struct fuse_chan *ch, const struct iovec iov[], size_t count)
219{
220 return ch->op.send(ch, iov, count);
221}
222
223void fuse_chan_destroy(struct fuse_chan *ch)
224{
225 fuse_session_remove_chan(ch);
226 if (ch->op.destroy)
227 ch->op.destroy(ch);
228 free(ch);
229}
230
231#ifndef __FreeBSD__
232FUSE_SYMVER(".symver fuse_chan_new_compat24,fuse_chan_new@FUSE_2.4");
233#endif