blob: ed352c6cde097fb83bf2c70e62f484355e6761b3 [file] [log] [blame]
Miklos Szeredi85c74fc2001-10-28 19:44:14 +00001/*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001 Miklos Szeredi (mszeredi@inf.bme.hu)
4
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10#include <linux/fuse.h>
11
12#include <string.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000013#include <stdlib.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000014#include <unistd.h>
Miklos Szeredi97c61e92001-11-07 12:09:43 +000015#include <limits.h>
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000016#include <errno.h>
17
Miklos Szeredi97c61e92001-11-07 12:09:43 +000018#define FUSE_MAX_PATH 4096
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000019
Miklos Szeredi97c61e92001-11-07 12:09:43 +000020static struct node *__get_node(struct fuse *f, fino_t ino)
Miklos Szeredia181e612001-11-06 12:03:23 +000021{
Miklos Szeredi97c61e92001-11-07 12:09:43 +000022 size_t hash = ino % f->ino_table_size;
23 struct node *node;
24
25 for(node = f->ino_table[hash]; node != NULL; node = node->ino_next)
26 if(node->ino == ino)
27 return node;
28
29 return NULL;
Miklos Szeredia181e612001-11-06 12:03:23 +000030}
31
Miklos Szeredi97c61e92001-11-07 12:09:43 +000032static struct node *get_node(struct fuse *f, fino_t ino)
Miklos Szeredia181e612001-11-06 12:03:23 +000033{
Miklos Szeredi97c61e92001-11-07 12:09:43 +000034 struct node *node = __get_node(f, ino);
35 if(node != NULL)
36 return node;
37
38 fprintf(stderr, "fuse internal error: inode %lu not found\n", ino);
39 abort();
Miklos Szeredia181e612001-11-06 12:03:23 +000040}
41
Miklos Szeredi97c61e92001-11-07 12:09:43 +000042static void hash_ino(struct fuse *f, struct node *node, fino_t ino)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000043{
Miklos Szeredi97c61e92001-11-07 12:09:43 +000044 size_t hash = ino % f->ino_table_size;
45 node->ino = ino;
46
47 node->ino_next = f->ino_table[hash];
48 f->ino_table[hash] = node;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000049}
50
Miklos Szeredi97c61e92001-11-07 12:09:43 +000051static void unhash_ino(struct fuse *f, struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000052{
Miklos Szeredi97c61e92001-11-07 12:09:43 +000053 size_t hash = node->ino % f->ino_table_size;
54 struct node **nodep = &f->ino_table[hash];
55
56 for(; *nodep != NULL; nodep = &(*nodep)->ino_next)
57 if(*nodep == node) {
58 *nodep = node->ino_next;
59 return;
60 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000061}
62
Miklos Szeredi97c61e92001-11-07 12:09:43 +000063static fino_t get_ino(struct node *node)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000064{
Miklos Szeredi97c61e92001-11-07 12:09:43 +000065 return node->ino;
66}
67
68static fino_t next_ino(struct fuse *f)
69{
70 while(f->ctr == 0 || __get_node(f, f->ctr) != NULL)
71 f->ctr++;
72
73 return f->ctr;
74}
75
76static void free_node(struct node *node)
77{
78 free(node->name);
79 free(node);
80}
81
82static unsigned int name_hash(struct fuse *f, fino_t parent, const char *name)
83{
84 unsigned int hash = *name;
85
86 if(hash)
87 for(name += 1; *name != '\0'; name++)
88 hash = (hash << 5) - hash + *name;
89
90 return (hash + parent) % f->name_table_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +000091}
92
Miklos Szeredi19dff1b2001-10-30 15:06:52 +000093static struct node *lookup_node(struct fuse *f, fino_t parent,
94 const char *name)
95{
Miklos Szeredi97c61e92001-11-07 12:09:43 +000096 size_t hash = name_hash(f, parent, name);
97 struct node *node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +000098
Miklos Szeredi97c61e92001-11-07 12:09:43 +000099 for(node = f->name_table[hash]; node != NULL; node = node->name_next)
100 if(node->parent == parent && strcmp(node->name, name) == 0)
101 return node;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000102
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000103 return NULL;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000104}
105
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000106static void hash_name(struct fuse *f, struct node *node, fino_t parent,
Miklos Szeredia181e612001-11-06 12:03:23 +0000107 const char *name)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000108{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000109 size_t hash = name_hash(f, parent, name);
Miklos Szeredia181e612001-11-06 12:03:23 +0000110 node->parent = parent;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000111 node->name = strdup(name);
112 node->name_next = f->name_table[hash];
113 f->name_table[hash] = node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000114}
115
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000116static void unhash_name(struct fuse *f, struct node *node)
Miklos Szeredia181e612001-11-06 12:03:23 +0000117{
118 if(node->name != NULL) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000119 size_t hash = name_hash(f, node->parent, node->name);
120 struct node **nodep = &f->name_table[hash];
121
122 for(; *nodep != NULL; nodep = &(*nodep)->name_next)
123 if(*nodep == node) {
124 *nodep = node->name_next;
125 node->name_next = NULL;
126 free(node->name);
127 node->name = NULL;
128 node->parent = 0;
129 return;
130 }
131 fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n",
132 node->ino);
133 abort();
Miklos Szeredia181e612001-11-06 12:03:23 +0000134 }
135}
136
137static fino_t find_node(struct fuse *f, fino_t parent, char *name,
138 struct fuse_attr *attr, int version)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000139{
140 struct node *node;
Miklos Szeredia181e612001-11-06 12:03:23 +0000141 int mode = attr->mode & S_IFMT;
142 int rdev = 0;
143
144 if(S_ISCHR(mode) || S_ISBLK(mode))
145 rdev = attr->rdev;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000146
Miklos Szeredia181e612001-11-06 12:03:23 +0000147 pthread_mutex_lock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000148 node = lookup_node(f, parent, name);
149 if(node != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000150 if(node->mode == mode && node->rdev == rdev)
151 goto out;
152
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000153 unhash_name(f, node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000154 }
155
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000156 node = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredia181e612001-11-06 12:03:23 +0000157 node->mode = mode;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000158 node->rdev = rdev;
159 hash_ino(f, node, next_ino(f));
160 hash_name(f, node, parent, name);
Miklos Szeredia181e612001-11-06 12:03:23 +0000161
162 out:
163 node->version = version;
164 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000165 return get_ino(node);
166}
167
168static fino_t find_node_dir(struct fuse *f, fino_t parent, char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000169{
170 struct node *node;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000171
Miklos Szeredia181e612001-11-06 12:03:23 +0000172 pthread_mutex_lock(&f->lock);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000173 node = lookup_node(f, parent, name);
Miklos Szeredia181e612001-11-06 12:03:23 +0000174 pthread_mutex_unlock(&f->lock);
175
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000176 if(node != NULL)
177 return get_ino(node);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000178 else
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000179 return (fino_t) -1;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000180}
181
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000182static char *add_name(char *buf, char *s, const char *name)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000183{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000184 size_t len = strlen(name);
185 s -= len;
186 if(s <= buf) {
187 fprintf(stderr, "fuse: path too long: ...%s\n", s + len);
188 return NULL;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000189 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000190 strncpy(s, name, len);
191 s--;
192 *s = '/';
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000193
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000194 return s;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000195}
196
Miklos Szeredia181e612001-11-06 12:03:23 +0000197static char *get_path_name(struct fuse *f, fino_t ino, const char *name)
Miklos Szeredib483c932001-10-29 14:57:57 +0000198{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000199 char buf[FUSE_MAX_PATH];
200 char *s = buf + FUSE_MAX_PATH - 1;
201 struct node *node;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000202
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000203 *s = '\0';
Miklos Szeredia181e612001-11-06 12:03:23 +0000204
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000205 if(name != NULL) {
206 s = add_name(buf, s, name);
207 if(s == NULL)
208 return NULL;
209 }
210
211 pthread_mutex_lock(&f->lock);
212 for(node = get_node(f, ino); node->ino != FUSE_ROOT_INO;
213 node = get_node(f, node->parent)) {
214 if(node->name == NULL) {
215 s = NULL;
216 break;
217 }
218
219 s = add_name(buf, s, node->name);
220 if(s == NULL)
221 break;
222 }
223 pthread_mutex_unlock(&f->lock);
224
225 if(s == NULL)
Miklos Szeredi5e183482001-10-31 14:52:35 +0000226 return NULL;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000227 else if(*s == '\0')
228 return strdup("/");
229 else
230 return strdup(s);
231}
Miklos Szeredia181e612001-11-06 12:03:23 +0000232
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000233static char *get_path(struct fuse *f, fino_t ino)
234{
235 return get_path_name(f, ino, NULL);
Miklos Szeredib483c932001-10-29 14:57:57 +0000236}
237
Miklos Szeredia181e612001-11-06 12:03:23 +0000238static void destroy_node(struct fuse *f, fino_t ino, int version)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000239{
Miklos Szeredia181e612001-11-06 12:03:23 +0000240 struct node *node;
241
242 pthread_mutex_lock(&f->lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000243 node = get_node(f, ino);
Miklos Szeredi39f28672001-11-14 14:52:54 +0000244 if(node->version == version && ino != FUSE_ROOT_INO) {
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000245 unhash_name(f, node);
246 unhash_ino(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000247 free_node(node);
248 }
249 pthread_mutex_unlock(&f->lock);
250
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000251}
252
Miklos Szeredi5e183482001-10-31 14:52:35 +0000253static void remove_node(struct fuse *f, fino_t dir, const char *name)
254{
Miklos Szeredia181e612001-11-06 12:03:23 +0000255 struct node *node;
256
257 pthread_mutex_lock(&f->lock);
258 node = lookup_node(f, dir, name);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000259 if(node == NULL) {
260 fprintf(stderr, "fuse internal error: unable to remove node %lu/%s\n",
261 dir, name);
262 abort();
263 }
264 unhash_name(f, node);
Miklos Szeredia181e612001-11-06 12:03:23 +0000265 pthread_mutex_unlock(&f->lock);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000266}
267
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000268static void rename_node(struct fuse *f, fino_t olddir, const char *oldname,
269 fino_t newdir, const char *newname)
270{
Miklos Szeredia181e612001-11-06 12:03:23 +0000271 struct node *node;
272 struct node *newnode;
273
274 pthread_mutex_lock(&f->lock);
275 node = lookup_node(f, olddir, oldname);
276 newnode = lookup_node(f, newdir, newname);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000277 if(node == NULL) {
278 fprintf(stderr, "fuse internal error: unable to rename node %lu/%s\n",
279 olddir, oldname);
280 abort();
281 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000282
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000283 if(newnode != NULL)
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000284 unhash_name(f, newnode);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000285
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000286 unhash_name(f, node);
287 hash_name(f, node, newdir, newname);
Miklos Szeredia181e612001-11-06 12:03:23 +0000288 pthread_mutex_unlock(&f->lock);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000289}
290
291
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000292static void convert_stat(struct stat *stbuf, struct fuse_attr *attr)
293{
294 attr->mode = stbuf->st_mode;
295 attr->nlink = stbuf->st_nlink;
296 attr->uid = stbuf->st_uid;
297 attr->gid = stbuf->st_gid;
298 attr->rdev = stbuf->st_rdev;
299 attr->size = stbuf->st_size;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000300 attr->blocks = stbuf->st_blocks;
301 attr->atime = stbuf->st_atime;
302 attr->mtime = stbuf->st_mtime;
303 attr->ctime = stbuf->st_ctime;
304}
305
Miklos Szeredia181e612001-11-06 12:03:23 +0000306static int fill_dir(struct fuse_dirhandle *dh, char *name, int type)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000307{
308 struct fuse_dirent dirent;
309 size_t reclen;
310 size_t res;
311
Miklos Szeredi5e183482001-10-31 14:52:35 +0000312 dirent.ino = find_node_dir(dh->fuse, dh->dir, name);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000313 dirent.namelen = strlen(name);
314 strncpy(dirent.name, name, sizeof(dirent.name));
315 dirent.type = type;
316 reclen = FUSE_DIRENT_SIZE(&dirent);
317 res = fwrite(&dirent, reclen, 1, dh->fp);
318 if(res == 0) {
319 perror("writing directory file");
320 return -EIO;
321 }
322 return 0;
323}
324
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000325static void send_reply(struct fuse *f, struct fuse_in_header *in, int error,
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000326 void *arg, size_t argsize)
327{
328 int res;
329 char *outbuf;
330 size_t outsize;
331 struct fuse_out_header *out;
332
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000333 if(error > 0) {
334 fprintf(stderr, "positive error code: %i\n", error);
335 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000336 }
337
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000338 if(error)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000339 argsize = 0;
340
341 outsize = sizeof(struct fuse_out_header) + argsize;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000342 outbuf = (char *) malloc(outsize);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000343 out = (struct fuse_out_header *) outbuf;
344 out->unique = in->unique;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000345 out->error = error;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000346 if(argsize != 0)
347 memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
348
Miklos Szeredic0938ea2001-11-07 12:35:06 +0000349 if((f->flags & FUSE_DEBUG)) {
350 printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique,
351 out->error, strerror(-out->error), outsize);
352 fflush(stdout);
353 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000354
355 res = write(f->fd, outbuf, outsize);
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000356 if(res == -1) {
357 /* ENOENT means the operation was interrupted */
358 if(errno != ENOENT)
359 perror("writing fuse device");
360 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000361
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000362 free(outbuf);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000363}
364
365static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
366{
367 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000368 char *path;
369 struct stat buf;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000370 struct fuse_lookup_out arg;
371
Miklos Szeredi5e183482001-10-31 14:52:35 +0000372 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000373 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000374 if(path != NULL) {
375 res = -ENOSYS;
376 if(f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000377 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000378 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000379 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000380 if(res == 0) {
381 convert_stat(&buf, &arg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000382 arg.ino = find_node(f, in->ino, name, &arg.attr, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000383 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000384 send_reply(f, in, res, &arg, sizeof(arg));
385}
386
Miklos Szeredia181e612001-11-06 12:03:23 +0000387static void do_forget(struct fuse *f, struct fuse_in_header *in,
388 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000389{
Miklos Szeredia181e612001-11-06 12:03:23 +0000390 destroy_node(f, in->ino, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000391}
392
393static void do_getattr(struct fuse *f, struct fuse_in_header *in)
394{
395 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000396 char *path;
397 struct stat buf;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000398 struct fuse_getattr_out arg;
399
Miklos Szeredi5e183482001-10-31 14:52:35 +0000400 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000401 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000402 if(path != NULL) {
403 res = -ENOSYS;
404 if(f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000405 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000406 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000407 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000408 if(res == 0)
409 convert_stat(&buf, &arg.attr);
410
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000411 send_reply(f, in, res, &arg, sizeof(arg));
412}
413
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000414int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000415{
416 int res;
417
418 res = -ENOSYS;
419 if(f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000420 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000421
422 return res;
423}
424
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000425int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
426 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000427{
428 int res;
429 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
430 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
431
432 res = -ENOSYS;
433 if(f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000434 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000435
436 return res;
437}
438
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000439int do_truncate(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000440{
441 int res;
442
443 res = -ENOSYS;
444 if(f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000445 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000446
447 return res;
448}
449
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000450int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000451{
452 int res;
453 struct utimbuf buf;
454 buf.actime = attr->atime;
455 buf.modtime = attr->mtime;
456 res = -ENOSYS;
457 if(f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000458 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000459
460 return res;
461}
462
Miklos Szeredi5e183482001-10-31 14:52:35 +0000463static void do_setattr(struct fuse *f, struct fuse_in_header *in,
464 struct fuse_setattr_in *arg)
465{
466 int res;
467 char *path;
468 int valid = arg->valid;
469 struct fuse_attr *attr = &arg->attr;
Miklos Szeredia181e612001-11-06 12:03:23 +0000470 struct fuse_setattr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000471
472 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000473 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000474 if(path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000475 res = -ENOSYS;
476 if(f->op.getattr) {
477 res = 0;
478 if(!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000479 res = do_chmod(f, path, attr);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000480 if(!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000481 res = do_chown(f, path, attr, valid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000482 if(!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000483 res = do_truncate(f, path, attr);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000484 if(!res && (valid & FATTR_UTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000485 res = do_utime(f, path, attr);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000486 if(!res) {
487 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000488 res = f->op.getattr(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000489 if(!res)
490 convert_stat(&buf, &outarg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000491 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000492 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000493 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000494 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000495 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000496}
497
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000498static void do_readlink(struct fuse *f, struct fuse_in_header *in)
499{
500 int res;
501 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000502 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000503
Miklos Szeredi5e183482001-10-31 14:52:35 +0000504 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000505 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000506 if(path != NULL) {
507 res = -ENOSYS;
508 if(f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000509 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000510 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000511 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000512 link[PATH_MAX] = '\0';
Miklos Szeredi5e183482001-10-31 14:52:35 +0000513 send_reply(f, in, res, link, !res ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000514}
515
516static void do_getdir(struct fuse *f, struct fuse_in_header *in)
517{
518 int res;
519 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000520 struct fuse_dirhandle dh;
Miklos Szeredib483c932001-10-29 14:57:57 +0000521 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000522
Miklos Szeredib483c932001-10-29 14:57:57 +0000523 dh.fuse = f;
524 dh.fp = tmpfile();
525 dh.dir = in->ino;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000526 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000527 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000528 if(path != NULL) {
529 res = -ENOSYS;
530 if(f->op.getdir)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000531 res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000532 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000533 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000534 fflush(dh.fp);
535 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000536 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredib483c932001-10-29 14:57:57 +0000537 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000538}
539
Miklos Szeredib483c932001-10-29 14:57:57 +0000540static void do_mknod(struct fuse *f, struct fuse_in_header *in,
541 struct fuse_mknod_in *inarg)
542{
543 int res;
544 char *path;
545 struct fuse_mknod_out outarg;
546 struct stat buf;
547
Miklos Szeredi5e183482001-10-31 14:52:35 +0000548 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000549 path = get_path_name(f, in->ino, inarg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000550 if(path != NULL) {
551 res = -ENOSYS;
552 if(f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000553 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000554 if(res == 0)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000555 res = f->op.getattr(path, &buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000556 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000557 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000558 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000559 if(res == 0) {
Miklos Szeredib483c932001-10-29 14:57:57 +0000560 convert_stat(&buf, &outarg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000561 outarg.ino = find_node(f, in->ino, inarg->name, &outarg.attr,
562 in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000563 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000564
565 send_reply(f, in, res, &outarg, sizeof(outarg));
566}
567
568static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
569 struct fuse_mkdir_in *inarg)
570{
571 int res;
572 char *path;
573
Miklos Szeredi5e183482001-10-31 14:52:35 +0000574 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000575 path = get_path_name(f, in->ino, inarg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000576 if(path != NULL) {
577 res = -ENOSYS;
578 if(f->op.mkdir)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000579 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000580 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000581 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000582 send_reply(f, in, res, NULL, 0);
583}
584
585static void do_remove(struct fuse *f, struct fuse_in_header *in, char *name)
586{
587 int res;
588 char *path;
589
Miklos Szeredi5e183482001-10-31 14:52:35 +0000590 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000591 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000592 if(path != NULL) {
593 res = -ENOSYS;
594 if(in->opcode == FUSE_UNLINK) {
595 if(f->op.unlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000596 res = f->op.unlink(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000597 }
598 else {
599 if(f->op.rmdir)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000600 res = f->op.rmdir(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000601 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000602 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000603 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000604 if(res == 0)
605 remove_node(f, in->ino, name);
Miklos Szeredib483c932001-10-29 14:57:57 +0000606 send_reply(f, in, res, NULL, 0);
607}
608
609static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
610 char *link)
611{
612 int res;
613 char *path;
614
Miklos Szeredi5e183482001-10-31 14:52:35 +0000615 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000616 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000617 if(path != NULL) {
618 res = -ENOSYS;
619 if(f->op.symlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000620 res = f->op.symlink(link, path);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000621 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000622 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000623 send_reply(f, in, res, NULL, 0);
624}
625
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000626static void do_rename(struct fuse *f, struct fuse_in_header *in,
627 struct fuse_rename_in *inarg)
628{
629 int res;
630 fino_t olddir = in->ino;
631 fino_t newdir = inarg->newdir;
632 char *oldname = inarg->names;
633 char *newname = inarg->names + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000634 char *oldpath;
635 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000636
Miklos Szeredi5e183482001-10-31 14:52:35 +0000637 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000638 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000639 if(oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000640 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000641 if(newpath != NULL) {
642 res = -ENOSYS;
643 if(f->op.rename)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000644 res = f->op.rename(oldpath, newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000645 if(res == 0)
646 rename_node(f, olddir, oldname, newdir, newname);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000647 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000648 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000649 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000650 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000651 send_reply(f, in, res, NULL, 0);
652}
653
654static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +0000655 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000656{
657 int res;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000658 char *oldpath;
659 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000660
Miklos Szeredi5e183482001-10-31 14:52:35 +0000661 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000662 oldpath = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000663 if(oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000664 newpath = get_path_name(f, arg->newdir, arg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000665 if(newpath != NULL) {
666 res = -ENOSYS;
667 if(f->op.link)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000668 res = f->op.link(oldpath, newpath);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000669 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000670 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000671 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000672 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000673 send_reply(f, in, res, NULL, 0);
674}
675
Miklos Szeredi5e183482001-10-31 14:52:35 +0000676static void do_open(struct fuse *f, struct fuse_in_header *in,
677 struct fuse_open_in *arg)
678{
679 int res;
680 char *path;
681
682 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000683 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000684 if(path != NULL) {
685 res = -ENOSYS;
686 if(f->op.open)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000687 res = f->op.open(path, arg->flags);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000688 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000689 }
690 send_reply(f, in, res, NULL, 0);
691}
692
693static void do_read(struct fuse *f, struct fuse_in_header *in,
694 struct fuse_read_in *arg)
695{
696 int res;
697 char *path;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000698 char *buf = (char *) malloc(arg->size);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000699 size_t size;
700
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000701 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000702 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000703 if(path != NULL) {
704 res = -ENOSYS;
Miklos Szeredia181e612001-11-06 12:03:23 +0000705 if(f->op.read)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000706 res = f->op.read(path, buf, arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000707 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000708 }
709
710 size = 0;
711 if(res > 0) {
712 size = res;
713 res = 0;
714 }
715
716 send_reply(f, in, res, buf, size);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000717 free(buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000718}
Miklos Szeredib483c932001-10-29 14:57:57 +0000719
Miklos Szeredia181e612001-11-06 12:03:23 +0000720static void do_write(struct fuse *f, struct fuse_in_header *in,
721 struct fuse_write_in *arg)
722{
723 int res;
724 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000725
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000726 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000727 path = get_path(f, in->ino);
728 if(path != NULL) {
729 res = -ENOSYS;
730 if(f->op.write)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000731 res = f->op.write(path, arg->buf, arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000732 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +0000733 }
734
735 if(res > 0) {
736 if((size_t) res != arg->size) {
737 fprintf(stderr, "short write: %u (should be %u)\n", res,
738 arg->size);
739 res = -EIO;
740 }
741 else
742 res = 0;
743 }
744
745 send_reply(f, in, res, NULL, 0);
746}
747
748struct cmd {
749 struct fuse *f;
750 char *buf;
751 size_t buflen;
752};
753
754static void *do_command(void *data)
755{
756 struct cmd *cmd = (struct cmd *) data;
757 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
758 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
759 size_t argsize;
760 struct fuse *f = cmd->f;
761
Miklos Szeredic0938ea2001-11-07 12:35:06 +0000762 if((f->flags & FUSE_DEBUG)) {
763 printf("unique: %i, opcode: %i, ino: %li, insize: %i\n", in->unique,
764 in->opcode, in->ino, cmd->buflen);
765 fflush(stdout);
766 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000767
768 argsize = cmd->buflen - sizeof(struct fuse_in_header);
769
770 switch(in->opcode) {
771 case FUSE_LOOKUP:
772 do_lookup(f, in, (char *) inarg);
773 break;
774
775 case FUSE_FORGET:
776 do_forget(f, in, (struct fuse_forget_in *) inarg);
777 break;
778
779 case FUSE_GETATTR:
780 do_getattr(f, in);
781 break;
782
783 case FUSE_SETATTR:
784 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
785 break;
786
787 case FUSE_READLINK:
788 do_readlink(f, in);
789 break;
790
791 case FUSE_GETDIR:
792 do_getdir(f, in);
793 break;
794
795 case FUSE_MKNOD:
796 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
797 break;
798
799 case FUSE_MKDIR:
800 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
801 break;
802
803 case FUSE_UNLINK:
804 case FUSE_RMDIR:
805 do_remove(f, in, (char *) inarg);
806 break;
807
808 case FUSE_SYMLINK:
809 do_symlink(f, in, (char *) inarg,
810 ((char *) inarg) + strlen((char *) inarg) + 1);
811 break;
812
813 case FUSE_RENAME:
814 do_rename(f, in, (struct fuse_rename_in *) inarg);
815 break;
816
817 case FUSE_LINK:
818 do_link(f, in, (struct fuse_link_in *) inarg);
819 break;
820
821 case FUSE_OPEN:
822 do_open(f, in, (struct fuse_open_in *) inarg);
823 break;
824
825 case FUSE_READ:
826 do_read(f, in, (struct fuse_read_in *) inarg);
827 break;
828
829 case FUSE_WRITE:
830 do_write(f, in, (struct fuse_write_in *) inarg);
831 break;
832
833 default:
834 fprintf(stderr, "Operation %i not implemented\n", in->opcode);
835 /* No need to send reply to async requests */
836 if(in->unique != 0)
837 send_reply(f, in, -ENOSYS, NULL, 0);
838 }
839
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000840 free(cmd->buf);
841 free(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +0000842
843 return NULL;
844}
845
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000846/* This hack makes it possible to link FUSE with or without the
847 pthread library */
848__attribute__((weak))
849int pthread_create(pthread_t *thrid __attribute__((unused)),
850 const pthread_attr_t *attr __attribute__((unused)),
851 void *(*func)(void *) __attribute__((unused)),
852 void *arg __attribute__((unused)))
853{
854 return ENOSYS;
855}
856
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000857void fuse_loop(struct fuse *f)
858{
859 int res;
860 char inbuf[FUSE_MAX_IN];
Miklos Szeredia181e612001-11-06 12:03:23 +0000861 pthread_attr_t attr;
862 pthread_t thrid;
863
864 pthread_attr_init(&attr);
865 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000866
867 while(1) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000868 struct cmd *cmd;
869
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000870 res = read(f->fd, inbuf, sizeof(inbuf));
871 if(res == -1) {
872 perror("reading fuse device");
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000873 /* BAD... This will happen again */
874 exit(1);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000875 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000876 if((size_t) res < sizeof(struct fuse_in_header)) {
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000877 fprintf(stderr, "short read on fuse device\n");
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000878 /* Cannot happen */
879 exit(1);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000880 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000881
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000882 cmd = (struct cmd *) malloc(sizeof(struct cmd));
Miklos Szeredia181e612001-11-06 12:03:23 +0000883 cmd->f = f;
884 cmd->buflen = res;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000885 cmd->buf = (char *) malloc(cmd->buflen);
Miklos Szeredia181e612001-11-06 12:03:23 +0000886 memcpy(cmd->buf, inbuf, cmd->buflen);
887
888 if(f->flags & FUSE_MULTITHREAD) {
889 res = pthread_create(&thrid, &attr, do_command, cmd);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000890 if(res == 0)
891 continue;
892
893 fprintf(stderr, "Error creating thread: %s\n", strerror(res));
894 fprintf(stderr, "Will run in single thread mode\n");
895 f->flags &= ~FUSE_MULTITHREAD;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000896 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000897
898 do_command(cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000899 }
900}
901
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000902struct fuse *fuse_new(int fd, int flags)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000903{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000904 struct fuse *f;
905 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000906
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000907 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000908
Miklos Szeredia181e612001-11-06 12:03:23 +0000909 f->flags = flags;
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000910 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000911 f->ctr = 0;
912 f->name_table_size = 14057;
913 f->name_table = (struct node **)
914 calloc(1, sizeof(struct node *) * f->name_table_size);
915 f->ino_table_size = 14057;
916 f->ino_table = (struct node **)
917 calloc(1, sizeof(struct node *) * f->ino_table_size);
Miklos Szeredia181e612001-11-06 12:03:23 +0000918 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000919
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000920 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000921 root->mode = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000922 root->rdev = 0;
923 root->name = strdup("/");
924 root->parent = 0;
925 hash_ino(f, root, FUSE_ROOT_INO);
926
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000927 return f;
928}
929
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000930void fuse_set_operations(struct fuse *f, const struct fuse_operations *op)
931{
932 f->op = *op;
933}
934
935void fuse_destroy(struct fuse *f)
936{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000937 size_t i;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000938 for(i = 0; i < f->ino_table_size; i++) {
939 struct node *node;
940 struct node *next;
941 for(node = f->ino_table[i]; node != NULL; node = next) {
942 next = node->ino_next;
943 free_node(node);
944 }
945 }
946 free(f->ino_table);
947 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +0000948 pthread_mutex_destroy(&f->lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000949 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000950}