blob: 6bd32aeec3455c60adf61d670ebb36ed606ad2b9 [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 Szeredi0a7077f2001-11-11 18:20:17 +0000415int 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 Szeredi0a7077f2001-11-11 18:20:17 +0000426int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr,
427 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 Szeredi0a7077f2001-11-11 18:20:17 +0000440int do_truncate(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000441{
442 int res;
443
444 res = -ENOSYS;
445 if(f->op.truncate)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000446 res = f->op.truncate(path, attr->size);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000447
448 return res;
449}
450
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000451int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr)
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000452{
453 int res;
454 struct utimbuf buf;
455 buf.actime = attr->atime;
456 buf.modtime = attr->mtime;
457 res = -ENOSYS;
458 if(f->op.utime)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000459 res = f->op.utime(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000460
461 return res;
462}
463
Miklos Szeredi5e183482001-10-31 14:52:35 +0000464static void do_setattr(struct fuse *f, struct fuse_in_header *in,
465 struct fuse_setattr_in *arg)
466{
467 int res;
468 char *path;
469 int valid = arg->valid;
470 struct fuse_attr *attr = &arg->attr;
Miklos Szeredia181e612001-11-06 12:03:23 +0000471 struct fuse_setattr_out outarg;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000472
473 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000474 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000475 if(path != NULL) {
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000476 res = -ENOSYS;
477 if(f->op.getattr) {
478 res = 0;
479 if(!res && (valid & FATTR_MODE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000480 res = do_chmod(f, path, attr);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000481 if(!res && (valid & (FATTR_UID | FATTR_GID)))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000482 res = do_chown(f, path, attr, valid);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000483 if(!res && (valid & FATTR_SIZE))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000484 res = do_truncate(f, path, attr);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000485 if(!res && (valid & FATTR_UTIME))
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000486 res = do_utime(f, path, attr);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000487 if(!res) {
488 struct stat buf;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000489 res = f->op.getattr(path, &buf);
Miklos Szeredif3ea83b2001-11-07 14:55:16 +0000490 if(!res)
491 convert_stat(&buf, &outarg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000492 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000493 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000494 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000495 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000496 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000497}
498
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000499static void do_readlink(struct fuse *f, struct fuse_in_header *in)
500{
501 int res;
502 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000503 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000504
Miklos Szeredi5e183482001-10-31 14:52:35 +0000505 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000506 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000507 if(path != NULL) {
508 res = -ENOSYS;
509 if(f->op.readlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000510 res = f->op.readlink(path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000511 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000512 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000513 link[PATH_MAX] = '\0';
Miklos Szeredi5e183482001-10-31 14:52:35 +0000514 send_reply(f, in, res, link, !res ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000515}
516
517static void do_getdir(struct fuse *f, struct fuse_in_header *in)
518{
519 int res;
520 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000521 struct fuse_dirhandle dh;
Miklos Szeredib483c932001-10-29 14:57:57 +0000522 char *path;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000523
Miklos Szeredib483c932001-10-29 14:57:57 +0000524 dh.fuse = f;
525 dh.fp = tmpfile();
526 dh.dir = in->ino;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000527 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000528 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000529 if(path != NULL) {
530 res = -ENOSYS;
531 if(f->op.getdir)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000532 res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000533 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000534 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000535 fflush(dh.fp);
536 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000537 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredib483c932001-10-29 14:57:57 +0000538 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000539}
540
Miklos Szeredib483c932001-10-29 14:57:57 +0000541static void do_mknod(struct fuse *f, struct fuse_in_header *in,
542 struct fuse_mknod_in *inarg)
543{
544 int res;
545 char *path;
546 struct fuse_mknod_out outarg;
547 struct stat buf;
548
Miklos Szeredi5e183482001-10-31 14:52:35 +0000549 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000550 path = get_path_name(f, in->ino, inarg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000551 if(path != NULL) {
552 res = -ENOSYS;
553 if(f->op.mknod && f->op.getattr) {
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000554 res = f->op.mknod(path, inarg->mode, inarg->rdev);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000555 if(res == 0)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000556 res = f->op.getattr(path, &buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000557 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000558 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000559 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000560 if(res == 0) {
Miklos Szeredib483c932001-10-29 14:57:57 +0000561 convert_stat(&buf, &outarg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000562 outarg.ino = find_node(f, in->ino, inarg->name, &outarg.attr,
563 in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000564 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000565
566 send_reply(f, in, res, &outarg, sizeof(outarg));
567}
568
569static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
570 struct fuse_mkdir_in *inarg)
571{
572 int res;
573 char *path;
574
Miklos Szeredi5e183482001-10-31 14:52:35 +0000575 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000576 path = get_path_name(f, in->ino, inarg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000577 if(path != NULL) {
578 res = -ENOSYS;
579 if(f->op.mkdir)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000580 res = f->op.mkdir(path, inarg->mode);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000581 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000582 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000583 send_reply(f, in, res, NULL, 0);
584}
585
586static void do_remove(struct fuse *f, struct fuse_in_header *in, char *name)
587{
588 int res;
589 char *path;
590
Miklos Szeredi5e183482001-10-31 14:52:35 +0000591 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000592 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000593 if(path != NULL) {
594 res = -ENOSYS;
595 if(in->opcode == FUSE_UNLINK) {
596 if(f->op.unlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000597 res = f->op.unlink(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000598 }
599 else {
600 if(f->op.rmdir)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000601 res = f->op.rmdir(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000602 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000603 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000604 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000605 if(res == 0)
606 remove_node(f, in->ino, name);
Miklos Szeredib483c932001-10-29 14:57:57 +0000607 send_reply(f, in, res, NULL, 0);
608}
609
610static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
611 char *link)
612{
613 int res;
614 char *path;
615
Miklos Szeredi5e183482001-10-31 14:52:35 +0000616 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000617 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000618 if(path != NULL) {
619 res = -ENOSYS;
620 if(f->op.symlink)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000621 res = f->op.symlink(link, path);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000622 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000623 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000624 send_reply(f, in, res, NULL, 0);
625}
626
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000627static void do_rename(struct fuse *f, struct fuse_in_header *in,
628 struct fuse_rename_in *inarg)
629{
630 int res;
631 fino_t olddir = in->ino;
632 fino_t newdir = inarg->newdir;
633 char *oldname = inarg->names;
634 char *newname = inarg->names + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000635 char *oldpath;
636 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000637
Miklos Szeredi5e183482001-10-31 14:52:35 +0000638 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000639 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000640 if(oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000641 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000642 if(newpath != NULL) {
643 res = -ENOSYS;
644 if(f->op.rename)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000645 res = f->op.rename(oldpath, newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000646 if(res == 0)
647 rename_node(f, olddir, oldname, newdir, newname);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000648 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000649 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000650 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000651 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000652 send_reply(f, in, res, NULL, 0);
653}
654
655static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +0000656 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000657{
658 int res;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000659 char *oldpath;
660 char *newpath;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000661
Miklos Szeredi5e183482001-10-31 14:52:35 +0000662 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000663 oldpath = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000664 if(oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000665 newpath = get_path_name(f, arg->newdir, arg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000666 if(newpath != NULL) {
667 res = -ENOSYS;
668 if(f->op.link)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000669 res = f->op.link(oldpath, newpath);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000670 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000671 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000672 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000673 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000674 send_reply(f, in, res, NULL, 0);
675}
676
Miklos Szeredi5e183482001-10-31 14:52:35 +0000677static void do_open(struct fuse *f, struct fuse_in_header *in,
678 struct fuse_open_in *arg)
679{
680 int res;
681 char *path;
682
683 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000684 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000685 if(path != NULL) {
686 res = -ENOSYS;
687 if(f->op.open)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000688 res = f->op.open(path, arg->flags);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000689 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000690 }
691 send_reply(f, in, res, NULL, 0);
692}
693
694static void do_read(struct fuse *f, struct fuse_in_header *in,
695 struct fuse_read_in *arg)
696{
697 int res;
698 char *path;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000699 char *buf = (char *) malloc(arg->size);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000700 size_t size;
701
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000702 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000703 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000704 if(path != NULL) {
705 res = -ENOSYS;
Miklos Szeredia181e612001-11-06 12:03:23 +0000706 if(f->op.read)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000707 res = f->op.read(path, buf, arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000708 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000709 }
710
711 size = 0;
712 if(res > 0) {
713 size = res;
714 res = 0;
715 }
716
717 send_reply(f, in, res, buf, size);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000718 free(buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000719}
Miklos Szeredib483c932001-10-29 14:57:57 +0000720
Miklos Szeredia181e612001-11-06 12:03:23 +0000721static void do_write(struct fuse *f, struct fuse_in_header *in,
722 struct fuse_write_in *arg)
723{
724 int res;
725 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000726
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000727 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000728 path = get_path(f, in->ino);
729 if(path != NULL) {
730 res = -ENOSYS;
731 if(f->op.write)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000732 res = f->op.write(path, arg->buf, arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000733 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +0000734 }
735
736 if(res > 0) {
737 if((size_t) res != arg->size) {
738 fprintf(stderr, "short write: %u (should be %u)\n", res,
739 arg->size);
740 res = -EIO;
741 }
742 else
743 res = 0;
744 }
745
746 send_reply(f, in, res, NULL, 0);
747}
748
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000749void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd)
Miklos Szeredia181e612001-11-06 12:03:23 +0000750{
Miklos Szeredia181e612001-11-06 12:03:23 +0000751 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
752 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
753 size_t argsize;
Miklos Szeredia181e612001-11-06 12:03:23 +0000754
Miklos Szeredic0938ea2001-11-07 12:35:06 +0000755 if((f->flags & FUSE_DEBUG)) {
756 printf("unique: %i, opcode: %i, ino: %li, insize: %i\n", in->unique,
757 in->opcode, in->ino, cmd->buflen);
758 fflush(stdout);
759 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000760
761 argsize = cmd->buflen - sizeof(struct fuse_in_header);
762
763 switch(in->opcode) {
764 case FUSE_LOOKUP:
765 do_lookup(f, in, (char *) inarg);
766 break;
767
768 case FUSE_FORGET:
769 do_forget(f, in, (struct fuse_forget_in *) inarg);
770 break;
771
772 case FUSE_GETATTR:
773 do_getattr(f, in);
774 break;
775
776 case FUSE_SETATTR:
777 do_setattr(f, in, (struct fuse_setattr_in *) inarg);
778 break;
779
780 case FUSE_READLINK:
781 do_readlink(f, in);
782 break;
783
784 case FUSE_GETDIR:
785 do_getdir(f, in);
786 break;
787
788 case FUSE_MKNOD:
789 do_mknod(f, in, (struct fuse_mknod_in *) inarg);
790 break;
791
792 case FUSE_MKDIR:
793 do_mkdir(f, in, (struct fuse_mkdir_in *) inarg);
794 break;
795
796 case FUSE_UNLINK:
797 case FUSE_RMDIR:
798 do_remove(f, in, (char *) inarg);
799 break;
800
801 case FUSE_SYMLINK:
802 do_symlink(f, in, (char *) inarg,
803 ((char *) inarg) + strlen((char *) inarg) + 1);
804 break;
805
806 case FUSE_RENAME:
807 do_rename(f, in, (struct fuse_rename_in *) inarg);
808 break;
809
810 case FUSE_LINK:
811 do_link(f, in, (struct fuse_link_in *) inarg);
812 break;
813
814 case FUSE_OPEN:
815 do_open(f, in, (struct fuse_open_in *) inarg);
816 break;
817
818 case FUSE_READ:
819 do_read(f, in, (struct fuse_read_in *) inarg);
820 break;
821
822 case FUSE_WRITE:
823 do_write(f, in, (struct fuse_write_in *) inarg);
824 break;
825
826 default:
827 fprintf(stderr, "Operation %i not implemented\n", in->opcode);
828 /* No need to send reply to async requests */
829 if(in->unique != 0)
830 send_reply(f, in, -ENOSYS, NULL, 0);
831 }
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000832
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000833 free(cmd->buf);
834 free(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +0000835}
836
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000837struct fuse_cmd *__fuse_read_cmd(struct fuse *f)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000838{
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000839 ssize_t res;
840 char inbuf[FUSE_MAX_IN];
841 struct fuse_cmd *cmd;
842
843 res = read(f->fd, inbuf, sizeof(inbuf));
844 if(res == -1) {
845 perror("reading fuse device");
846 /* BAD... This will happen again */
847 return NULL;
848 }
849 if((size_t) res < sizeof(struct fuse_in_header)) {
850 fprintf(stderr, "short read on fuse device\n");
851 /* Cannot happen */
852 return NULL;
853 }
854
855 cmd = (struct fuse_cmd *) malloc(sizeof(*cmd));
856 cmd->buflen = res;
857 cmd->buf = (char *) malloc(cmd->buflen);
858 memcpy(cmd->buf, inbuf, cmd->buflen);
859
860 return cmd;
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000861}
862
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000863
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000864void fuse_loop(struct fuse *f)
865{
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000866 while(1) {
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000867 struct fuse_cmd *cmd = __fuse_read_cmd(f);
868 if(cmd == NULL)
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000869 exit(1);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000870
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000871 __fuse_process_cmd(f, cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000872 }
873}
874
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000875struct fuse *fuse_new(int fd, int flags)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000876{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000877 struct fuse *f;
878 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000879
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000880 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000881
Miklos Szeredia181e612001-11-06 12:03:23 +0000882 f->flags = flags;
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000883 f->fd = fd;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000884 f->ctr = 0;
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000885 /* FIXME: Dynamic hash table */
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000886 f->name_table_size = 14057;
887 f->name_table = (struct node **)
888 calloc(1, sizeof(struct node *) * f->name_table_size);
889 f->ino_table_size = 14057;
890 f->ino_table = (struct node **)
891 calloc(1, sizeof(struct node *) * f->ino_table_size);
Miklos Szeredia181e612001-11-06 12:03:23 +0000892 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000893
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000894 root = (struct node *) calloc(1, sizeof(struct node));
Miklos Szeredi8cffdb92001-11-09 14:49:18 +0000895 root->mode = 0;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000896 root->rdev = 0;
897 root->name = strdup("/");
898 root->parent = 0;
899 hash_ino(f, root, FUSE_ROOT_INO);
900
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000901 return f;
902}
903
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000904void fuse_set_operations(struct fuse *f, const struct fuse_operations *op)
905{
906 f->op = *op;
907}
908
909void fuse_destroy(struct fuse *f)
910{
Miklos Szeredifff56ab2001-11-16 10:12:59 +0000911 /* FIXME: Kill all threads... */
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}