blob: 8ea13ad791d1af7af2f8c60fb8db87f5a1e39060 [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;
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000304 attr->_dummy = 4096;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000305}
306
Miklos Szeredia181e612001-11-06 12:03:23 +0000307static int fill_dir(struct fuse_dirhandle *dh, char *name, int type)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000308{
309 struct fuse_dirent dirent;
310 size_t reclen;
311 size_t res;
312
Miklos Szeredi5e183482001-10-31 14:52:35 +0000313 dirent.ino = find_node_dir(dh->fuse, dh->dir, name);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000314 dirent.namelen = strlen(name);
315 strncpy(dirent.name, name, sizeof(dirent.name));
316 dirent.type = type;
317 reclen = FUSE_DIRENT_SIZE(&dirent);
318 res = fwrite(&dirent, reclen, 1, dh->fp);
319 if(res == 0) {
320 perror("writing directory file");
321 return -EIO;
322 }
323 return 0;
324}
325
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000326static void send_reply(struct fuse *f, struct fuse_in_header *in, int error,
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000327 void *arg, size_t argsize)
328{
329 int res;
330 char *outbuf;
331 size_t outsize;
332 struct fuse_out_header *out;
333
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000334 if(error > 0) {
335 fprintf(stderr, "positive error code: %i\n", error);
336 error = -ERANGE;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000337 }
338
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000339 if(error)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000340 argsize = 0;
341
342 outsize = sizeof(struct fuse_out_header) + argsize;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000343 outbuf = (char *) malloc(outsize);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000344 out = (struct fuse_out_header *) outbuf;
345 out->unique = in->unique;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000346 out->error = error;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000347 if(argsize != 0)
348 memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize);
349
Miklos Szeredic0938ea2001-11-07 12:35:06 +0000350 if((f->flags & FUSE_DEBUG)) {
351 printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique,
352 out->error, strerror(-out->error), outsize);
353 fflush(stdout);
354 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000355
356 res = write(f->fd, outbuf, outsize);
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000357 if(res == -1) {
358 /* ENOENT means the operation was interrupted */
359 if(errno != ENOENT)
360 perror("writing fuse device");
361 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000362
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000363 free(outbuf);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000364}
365
366static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
367{
368 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000369 char *path;
370 struct stat buf;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000371 struct fuse_lookup_out arg;
372
Miklos Szeredi5e183482001-10-31 14:52:35 +0000373 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000374 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000375 if(path != NULL) {
376 res = -ENOSYS;
377 if(f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000378 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000379 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000380 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000381 if(res == 0) {
382 convert_stat(&buf, &arg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000383 arg.ino = find_node(f, in->ino, name, &arg.attr, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000384 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000385 send_reply(f, in, res, &arg, sizeof(arg));
386}
387
Miklos Szeredia181e612001-11-06 12:03:23 +0000388static void do_forget(struct fuse *f, struct fuse_in_header *in,
389 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000390{
Miklos Szeredia181e612001-11-06 12:03:23 +0000391 destroy_node(f, in->ino, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000392}
393
394static void do_getattr(struct fuse *f, struct fuse_in_header *in)
395{
396 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000397 char *path;
398 struct stat buf;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000399 struct fuse_getattr_out arg;
400
Miklos Szeredi5e183482001-10-31 14:52:35 +0000401 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000402 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000403 if(path != NULL) {
404 res = -ENOSYS;
405 if(f->op.getattr)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000406 res = f->op.getattr(path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000407 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000408 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000409 if(res == 0)
410 convert_stat(&buf, &arg.attr);
411
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000412 send_reply(f, in, res, &arg, sizeof(arg));
413}
414
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000415static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000416{
417 int res;
418
419 res = -ENOSYS;
420 if(f->op.chmod)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000421 res = f->op.chmod(path, attr->mode);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000422
423 return res;
424}
425
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000426static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000427 int valid)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000428{
429 int res;
430 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
431 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
432
433 res = -ENOSYS;
434 if(f->op.chown)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000435 res = f->op.chown(path, uid, gid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000436
437 return res;
438}
439
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000440static int do_truncate(struct fuse *f, const char *path,
441 struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000442{
443 int res;
444
445 res = -ENOSYS;
446 if(f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000447 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000448
449 return res;
450}
451
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000452static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000453{
454 int res;
455 struct utimbuf buf;
456 buf.actime = attr->atime;
457 buf.modtime = attr->mtime;
458 res = -ENOSYS;
459 if(f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000460 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000461
462 return res;
463}
464
Miklos Szeredi5e183482001-10-31 14:52:35 +0000465static void do_setattr(struct fuse *f, struct fuse_in_header *in,
466 struct fuse_setattr_in *arg)
467{
468 int res;
469 char *path;
470 int valid = arg->valid;
471 struct fuse_attr *attr = &arg->attr;
Miklos Szeredia181e612001-11-06 12:03:23 +0000472 struct fuse_setattr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000473
474 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000475 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000476 if(path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000477 res = -ENOSYS;
478 if(f->op.getattr) {
479 res = 0;
480 if(!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000481 res = do_chmod(f, path, attr);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000482 if(!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000483 res = do_chown(f, path, attr, valid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000484 if(!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000485 res = do_truncate(f, path, attr);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000486 if(!res && (valid & FATTR_UTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000487 res = do_utime(f, path, attr);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000488 if(!res) {
489 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000490 res = f->op.getattr(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000491 if(!res)
492 convert_stat(&buf, &outarg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000493 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000494 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000495 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000496 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000497 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000498}
499
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000500static void do_readlink(struct fuse *f, struct fuse_in_header *in)
501{
502 int res;
503 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000504 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000505
Miklos Szeredi5e183482001-10-31 14:52:35 +0000506 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000507 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000508 if(path != NULL) {
509 res = -ENOSYS;
510 if(f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000511 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000512 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000513 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000514 link[PATH_MAX] = '\0';
Miklos Szeredi5e183482001-10-31 14:52:35 +0000515 send_reply(f, in, res, link, !res ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000516}
517
518static void do_getdir(struct fuse *f, struct fuse_in_header *in)
519{
520 int res;
521 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000522 struct fuse_dirhandle dh;
Miklos Szeredib483c932001-10-29 14:57:57 +0000523 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000524
Miklos Szeredib483c932001-10-29 14:57:57 +0000525 dh.fuse = f;
526 dh.fp = tmpfile();
527 dh.dir = in->ino;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000528 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000529 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000530 if(path != NULL) {
531 res = -ENOSYS;
532 if(f->op.getdir)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000533 res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000534 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000535 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000536 fflush(dh.fp);
537 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000538 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredib483c932001-10-29 14:57:57 +0000539 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000540}
541
Miklos Szeredib483c932001-10-29 14:57:57 +0000542static void do_mknod(struct fuse *f, struct fuse_in_header *in,
543 struct fuse_mknod_in *inarg)
544{
545 int res;
546 char *path;
547 struct fuse_mknod_out outarg;
548 struct stat buf;
549
Miklos Szeredi5e183482001-10-31 14:52:35 +0000550 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000551 path = get_path_name(f, in->ino, inarg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000552 if(path != NULL) {
553 res = -ENOSYS;
554 if(f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000555 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000556 if(res == 0)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000557 res = f->op.getattr(path, &buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000558 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000559 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000560 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000561 if(res == 0) {
Miklos Szeredib483c932001-10-29 14:57:57 +0000562 convert_stat(&buf, &outarg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000563 outarg.ino = find_node(f, in->ino, inarg->name, &outarg.attr,
564 in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000565 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000566
567 send_reply(f, in, res, &outarg, sizeof(outarg));
568}
569
570static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
571 struct fuse_mkdir_in *inarg)
572{
573 int res;
574 char *path;
575
Miklos Szeredi5e183482001-10-31 14:52:35 +0000576 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000577 path = get_path_name(f, in->ino, inarg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000578 if(path != NULL) {
579 res = -ENOSYS;
580 if(f->op.mkdir)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000581 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000582 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000583 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000584 send_reply(f, in, res, NULL, 0);
585}
586
587static void do_remove(struct fuse *f, struct fuse_in_header *in, char *name)
588{
589 int res;
590 char *path;
591
Miklos Szeredi5e183482001-10-31 14:52:35 +0000592 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000593 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000594 if(path != NULL) {
595 res = -ENOSYS;
596 if(in->opcode == FUSE_UNLINK) {
597 if(f->op.unlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000598 res = f->op.unlink(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000599 }
600 else {
601 if(f->op.rmdir)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000602 res = f->op.rmdir(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000603 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000604 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000605 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000606 if(res == 0)
607 remove_node(f, in->ino, name);
Miklos Szeredib483c932001-10-29 14:57:57 +0000608 send_reply(f, in, res, NULL, 0);
609}
610
611static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
612 char *link)
613{
614 int res;
615 char *path;
616
Miklos Szeredi5e183482001-10-31 14:52:35 +0000617 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000618 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000619 if(path != NULL) {
620 res = -ENOSYS;
621 if(f->op.symlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000622 res = f->op.symlink(link, path);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000623 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000624 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000625 send_reply(f, in, res, NULL, 0);
626}
627
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000628static void do_rename(struct fuse *f, struct fuse_in_header *in,
629 struct fuse_rename_in *inarg)
630{
631 int res;
632 fino_t olddir = in->ino;
633 fino_t newdir = inarg->newdir;
634 char *oldname = inarg->names;
635 char *newname = inarg->names + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000636 char *oldpath;
637 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000638
Miklos Szeredi5e183482001-10-31 14:52:35 +0000639 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000640 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000641 if(oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000642 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000643 if(newpath != NULL) {
644 res = -ENOSYS;
645 if(f->op.rename)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000646 res = f->op.rename(oldpath, newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000647 if(res == 0)
648 rename_node(f, olddir, oldname, newdir, newname);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000649 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000650 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000651 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000652 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000653 send_reply(f, in, res, NULL, 0);
654}
655
656static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +0000657 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000658{
659 int res;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000660 char *oldpath;
661 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000662
Miklos Szeredi5e183482001-10-31 14:52:35 +0000663 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000664 oldpath = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000665 if(oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000666 newpath = get_path_name(f, arg->newdir, arg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000667 if(newpath != NULL) {
668 res = -ENOSYS;
669 if(f->op.link)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000670 res = f->op.link(oldpath, newpath);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000671 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000672 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000673 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000674 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000675 send_reply(f, in, res, NULL, 0);
676}
677
Miklos Szeredi5e183482001-10-31 14:52:35 +0000678static void do_open(struct fuse *f, struct fuse_in_header *in,
679 struct fuse_open_in *arg)
680{
681 int res;
682 char *path;
683
684 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000685 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000686 if(path != NULL) {
687 res = -ENOSYS;
688 if(f->op.open)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000689 res = f->op.open(path, arg->flags);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000690 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000691 }
692 send_reply(f, in, res, NULL, 0);
693}
694
695static void do_read(struct fuse *f, struct fuse_in_header *in,
696 struct fuse_read_in *arg)
697{
698 int res;
699 char *path;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000700 char *buf = (char *) malloc(arg->size);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000701 size_t size;
702
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000703 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000704 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000705 if(path != NULL) {
706 res = -ENOSYS;
Miklos Szeredia181e612001-11-06 12:03:23 +0000707 if(f->op.read)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000708 res = f->op.read(path, buf, arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000709 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000710 }
711
712 size = 0;
713 if(res > 0) {
714 size = res;
715 res = 0;
716 }
717
718 send_reply(f, in, res, buf, size);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000719 free(buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000720}
Miklos Szeredib483c932001-10-29 14:57:57 +0000721
Miklos Szeredia181e612001-11-06 12:03:23 +0000722static void do_write(struct fuse *f, struct fuse_in_header *in,
723 struct fuse_write_in *arg)
724{
725 int res;
726 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000727
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000728 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000729 path = get_path(f, in->ino);
730 if(path != NULL) {
731 res = -ENOSYS;
732 if(f->op.write)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000733 res = f->op.write(path, arg->buf, arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000734 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +0000735 }
736
737 if(res > 0) {
738 if((size_t) res != arg->size) {
739 fprintf(stderr, "short write: %u (should be %u)\n", res,
740 arg->size);
741 res = -EIO;
742 }
743 else
744 res = 0;
745 }
746
747 send_reply(f, in, res, NULL, 0);
748}
749
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000750void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +0000751{
Miklos Szeredia181e612001-11-06 12:03:23 +0000752 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
753 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
754 size_t argsize;
Miklos Szeredia181e612001-11-06 12:03:23 +0000755
Miklos Szeredic0938ea2001-11-07 12:35:06 +0000756 if((f->flags & FUSE_DEBUG)) {
757 printf("unique: %i, opcode: %i, ino: %li, insize: %i\n", in->unique,
758 in->opcode, in->ino, cmd->buflen);
759 fflush(stdout);
760 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000761
762 argsize = cmd->buflen - sizeof(struct fuse_in_header);
763
764 switch(in->opcode) {
765 case FUSE_LOOKUP:
766 do_lookup(f, in, (char *) inarg);
767 break;
768
769 case FUSE_FORGET:
770 do_forget(f, in, (struct fuse_forget_in *) inarg);
771 break;
772
773 case FUSE_GETATTR:
774 do_getattr(f, in);
775 break;
776
777 case FUSE_SETATTR:
778 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
779 break;
780
781 case FUSE_READLINK:
782 do_readlink(f, in);
783 break;
784
785 case FUSE_GETDIR:
786 do_getdir(f, in);
787 break;
788
789 case FUSE_MKNOD:
790 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
791 break;
792
793 case FUSE_MKDIR:
794 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
795 break;
796
797 case FUSE_UNLINK:
798 case FUSE_RMDIR:
799 do_remove(f, in, (char *) inarg);
800 break;
801
802 case FUSE_SYMLINK:
803 do_symlink(f, in, (char *) inarg,
804 ((char *) inarg) + strlen((char *) inarg) + 1);
805 break;
806
807 case FUSE_RENAME:
808 do_rename(f, in, (struct fuse_rename_in *) inarg);
809 break;
810
811 case FUSE_LINK:
812 do_link(f, in, (struct fuse_link_in *) inarg);
813 break;
814
815 case FUSE_OPEN:
816 do_open(f, in, (struct fuse_open_in *) inarg);
817 break;
818
819 case FUSE_READ:
820 do_read(f, in, (struct fuse_read_in *) inarg);
821 break;
822
823 case FUSE_WRITE:
824 do_write(f, in, (struct fuse_write_in *) inarg);
825 break;
826
827 default:
828 fprintf(stderr, "Operation %i not implemented\n", in->opcode);
829 /* No need to send reply to async requests */
830 if(in->unique != 0)
831 send_reply(f, in, -ENOSYS, NULL, 0);
832 }
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000833
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000834 free(cmd->buf);
835 free(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +0000836}
837
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000838struct fuse_cmd *__fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000839{
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000840 ssize_t res;
841 char inbuf[FUSE_MAX_IN];
842 struct fuse_cmd *cmd;
843
844 res = read(f->fd, inbuf, sizeof(inbuf));
845 if(res == -1) {
846 perror("reading fuse device");
847 /* BAD... This will happen again */
848 return NULL;
849 }
850 if((size_t) res < sizeof(struct fuse_in_header)) {
851 fprintf(stderr, "short read on fuse device\n");
852 /* Cannot happen */
853 return NULL;
854 }
855
856 cmd = (struct fuse_cmd *) malloc(sizeof(*cmd));
857 cmd->buflen = res;
858 cmd->buf = (char *) malloc(cmd->buflen);
859 memcpy(cmd->buf, inbuf, cmd->buflen);
860
861 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000862}
863
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000864
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000865void fuse_loop(struct fuse *f)
866{
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000867 while(1) {
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000868 struct fuse_cmd *cmd = __fuse_read_cmd(f);
869 if(cmd == NULL)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000870 exit(1);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000871
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000872 __fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000873 }
874}
875
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000876struct fuse *fuse_new(int fd, int flags)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000877{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000878 struct fuse *f;
879 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000880
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000881 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000882
Miklos Szeredia181e612001-11-06 12:03:23 +0000883 f->flags = flags;
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000884 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000885 f->ctr = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000886 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000887 f->name_table_size = 14057;
888 f->name_table = (struct node **)
889 calloc(1, sizeof(struct node *) * f->name_table_size);
890 f->ino_table_size = 14057;
891 f->ino_table = (struct node **)
892 calloc(1, sizeof(struct node *) * f->ino_table_size);
Miklos Szeredia181e612001-11-06 12:03:23 +0000893 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000894
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000895 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000896 root->mode = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000897 root->rdev = 0;
898 root->name = strdup("/");
899 root->parent = 0;
900 hash_ino(f, root, FUSE_ROOT_INO);
901
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000902 return f;
903}
904
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000905void fuse_set_operations(struct fuse *f, const struct fuse_operations *op)
906{
907 f->op = *op;
908}
909
910void fuse_destroy(struct fuse *f)
911{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000912 size_t i;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000913 for(i = 0; i < f->ino_table_size; i++) {
914 struct node *node;
915 struct node *next;
916 for(node = f->ino_table[i]; node != NULL; node = next) {
917 next = node->ino_next;
918 free_node(node);
919 }
920 }
921 free(f->ino_table);
922 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +0000923 pthread_mutex_destroy(&f->lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000924 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000925}