blob: 2776adc4c7cf170ded762738ac60d5b8b24f0ade [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 Szeredia181e612001-11-06 12:03:23 +0000244 if(node->version == version) {
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;
300 attr->blksize = stbuf->st_blksize;
301 attr->blocks = stbuf->st_blocks;
302 attr->atime = stbuf->st_atime;
303 attr->mtime = stbuf->st_mtime;
304 attr->ctime = stbuf->st_ctime;
305}
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 Szeredi19dff1b2001-10-30 15:06:52 +0000350 printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique,
351 out->error, strerror(-out->error), outsize);
Miklos Szeredia181e612001-11-06 12:03:23 +0000352 fflush(stdout);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000353
354 res = write(f->fd, outbuf, outsize);
355 if(res == -1)
356 perror("writing fuse device");
357
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000358 free(outbuf);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000359}
360
Miklos Szeredia181e612001-11-06 12:03:23 +0000361static void fill_cred(struct fuse_in_header *in, struct fuse_cred *cred)
362{
363 cred->uid = in->uid;
364 cred->gid = in->gid;
365}
366
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000367static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name)
368{
369 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000370 char *path;
371 struct stat buf;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000372 struct fuse_lookup_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000373 struct fuse_cred cred;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000374
Miklos Szeredia181e612001-11-06 12:03:23 +0000375 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000376 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000377 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000378 if(path != NULL) {
379 res = -ENOSYS;
380 if(f->op.getattr)
Miklos Szeredia181e612001-11-06 12:03:23 +0000381 res = f->op.getattr(&cred, path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000382 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000383 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000384 if(res == 0) {
385 convert_stat(&buf, &arg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000386 arg.ino = find_node(f, in->ino, name, &arg.attr, in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000387 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000388 send_reply(f, in, res, &arg, sizeof(arg));
389}
390
Miklos Szeredia181e612001-11-06 12:03:23 +0000391static void do_forget(struct fuse *f, struct fuse_in_header *in,
392 struct fuse_forget_in *arg)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000393{
Miklos Szeredia181e612001-11-06 12:03:23 +0000394 destroy_node(f, in->ino, arg->version);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000395}
396
397static void do_getattr(struct fuse *f, struct fuse_in_header *in)
398{
399 int res;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000400 char *path;
401 struct stat buf;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000402 struct fuse_getattr_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000403 struct fuse_cred cred;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000404
Miklos Szeredia181e612001-11-06 12:03:23 +0000405 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000406 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000407 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000408 if(path != NULL) {
409 res = -ENOSYS;
410 if(f->op.getattr)
Miklos Szeredia181e612001-11-06 12:03:23 +0000411 res = f->op.getattr(&cred, path, &buf);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000412 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000413 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000414 if(res == 0)
415 convert_stat(&buf, &arg.attr);
416
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000417 send_reply(f, in, res, &arg, sizeof(arg));
418}
419
Miklos Szeredi5e183482001-10-31 14:52:35 +0000420static void do_setattr(struct fuse *f, struct fuse_in_header *in,
421 struct fuse_setattr_in *arg)
422{
423 int res;
424 char *path;
425 int valid = arg->valid;
426 struct fuse_attr *attr = &arg->attr;
Miklos Szeredia181e612001-11-06 12:03:23 +0000427 struct fuse_setattr_out outarg;
428 struct fuse_cred cred;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000429
Miklos Szeredia181e612001-11-06 12:03:23 +0000430 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000431 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000432 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000433 if(path != NULL) {
434 res = 0;
435 if(!res && (valid & FATTR_MODE)) {
436 res = -ENOSYS;
437 if(f->op.chmod)
Miklos Szeredia181e612001-11-06 12:03:23 +0000438 res = f->op.chmod(&cred, path, attr->mode);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000439 }
440 if(!res && (valid & (FATTR_UID | FATTR_GID))) {
441 uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1;
442 gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1;
443
444 res = -ENOSYS;
445 if(f->op.chown)
Miklos Szeredia181e612001-11-06 12:03:23 +0000446 res = f->op.chown(&cred, path, uid, gid);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000447 }
448 if(!res && (valid & FATTR_SIZE)) {
449 res = -ENOSYS;
Miklos Szeredia181e612001-11-06 12:03:23 +0000450 if(f->op.truncate && f->op.getattr) {
451 res = f->op.truncate(&cred, path, attr->size);
452 if(!res) {
453 struct stat buf;
454 res = f->op.getattr(&cred, path, &buf);
455 outarg.newsize = buf.st_size;
456 }
457 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000458 }
459 if(!res && (valid & FATTR_UTIME)) {
460 struct utimbuf buf;
461 buf.actime = attr->atime;
462 buf.modtime = attr->mtime;
463 res = -ENOSYS;
464 if(f->op.utime)
Miklos Szeredia181e612001-11-06 12:03:23 +0000465 res = f->op.utime(&cred, path, &buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000466 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000467 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000468 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000469 send_reply(f, in, res, &outarg, sizeof(outarg));
Miklos Szeredi5e183482001-10-31 14:52:35 +0000470}
471
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000472static void do_readlink(struct fuse *f, struct fuse_in_header *in)
473{
474 int res;
475 char link[PATH_MAX + 1];
Miklos Szeredib483c932001-10-29 14:57:57 +0000476 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000477 struct fuse_cred cred;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000478
Miklos Szeredia181e612001-11-06 12:03:23 +0000479 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000480 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000481 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000482 if(path != NULL) {
483 res = -ENOSYS;
484 if(f->op.readlink)
Miklos Szeredia181e612001-11-06 12:03:23 +0000485 res = f->op.readlink(&cred, path, link, sizeof(link));
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000486 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000487 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000488 link[PATH_MAX] = '\0';
Miklos Szeredi5e183482001-10-31 14:52:35 +0000489 send_reply(f, in, res, link, !res ? strlen(link) : 0);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000490}
491
492static void do_getdir(struct fuse *f, struct fuse_in_header *in)
493{
494 int res;
495 struct fuse_getdir_out arg;
Miklos Szeredia181e612001-11-06 12:03:23 +0000496 struct fuse_dirhandle dh;
Miklos Szeredib483c932001-10-29 14:57:57 +0000497 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000498 struct fuse_cred cred;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000499
Miklos Szeredia181e612001-11-06 12:03:23 +0000500 fill_cred(in, &cred);
Miklos Szeredib483c932001-10-29 14:57:57 +0000501 dh.fuse = f;
502 dh.fp = tmpfile();
503 dh.dir = in->ino;
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.getdir)
Miklos Szeredia181e612001-11-06 12:03:23 +0000509 res = f->op.getdir(&cred, path, &dh, (fuse_dirfil_t) fill_dir);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000510 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000511 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000512 fflush(dh.fp);
513 arg.fd = fileno(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000514 send_reply(f, in, res, &arg, sizeof(arg));
Miklos Szeredib483c932001-10-29 14:57:57 +0000515 fclose(dh.fp);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000516}
517
Miklos Szeredib483c932001-10-29 14:57:57 +0000518static void do_mknod(struct fuse *f, struct fuse_in_header *in,
519 struct fuse_mknod_in *inarg)
520{
521 int res;
522 char *path;
523 struct fuse_mknod_out outarg;
524 struct stat buf;
Miklos Szeredia181e612001-11-06 12:03:23 +0000525 struct fuse_cred cred;
Miklos Szeredib483c932001-10-29 14:57:57 +0000526
Miklos Szeredia181e612001-11-06 12:03:23 +0000527 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000528 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000529 path = get_path_name(f, in->ino, inarg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000530 if(path != NULL) {
531 res = -ENOSYS;
532 if(f->op.mknod && f->op.getattr) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000533 res = f->op.mknod(&cred, path, inarg->mode, inarg->rdev);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000534 if(res == 0)
Miklos Szeredia181e612001-11-06 12:03:23 +0000535 res = f->op.getattr(&cred, path, &buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000536 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000537 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000538 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000539 if(res == 0) {
Miklos Szeredib483c932001-10-29 14:57:57 +0000540 convert_stat(&buf, &outarg.attr);
Miklos Szeredia181e612001-11-06 12:03:23 +0000541 outarg.ino = find_node(f, in->ino, inarg->name, &outarg.attr,
542 in->unique);
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000543 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000544
545 send_reply(f, in, res, &outarg, sizeof(outarg));
546}
547
548static void do_mkdir(struct fuse *f, struct fuse_in_header *in,
549 struct fuse_mkdir_in *inarg)
550{
551 int res;
552 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000553 struct fuse_cred cred;
Miklos Szeredib483c932001-10-29 14:57:57 +0000554
Miklos Szeredia181e612001-11-06 12:03:23 +0000555 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000556 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000557 path = get_path_name(f, in->ino, inarg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000558 if(path != NULL) {
559 res = -ENOSYS;
560 if(f->op.mkdir)
Miklos Szeredia181e612001-11-06 12:03:23 +0000561 res = f->op.mkdir(&cred, path, inarg->mode);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000562 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000563 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000564 send_reply(f, in, res, NULL, 0);
565}
566
567static void do_remove(struct fuse *f, struct fuse_in_header *in, char *name)
568{
569 int res;
570 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000571 struct fuse_cred cred;
Miklos Szeredib483c932001-10-29 14:57:57 +0000572
Miklos Szeredia181e612001-11-06 12:03:23 +0000573 fill_cred(in, &cred);
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, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000576 if(path != NULL) {
577 res = -ENOSYS;
578 if(in->opcode == FUSE_UNLINK) {
579 if(f->op.unlink)
Miklos Szeredia181e612001-11-06 12:03:23 +0000580 res = f->op.unlink(&cred, path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000581 }
582 else {
583 if(f->op.rmdir)
Miklos Szeredia181e612001-11-06 12:03:23 +0000584 res = f->op.rmdir(&cred, path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000585 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000586 free(path);
Miklos Szeredib483c932001-10-29 14:57:57 +0000587 }
Miklos Szeredi5e183482001-10-31 14:52:35 +0000588 if(res == 0)
589 remove_node(f, in->ino, name);
Miklos Szeredib483c932001-10-29 14:57:57 +0000590 send_reply(f, in, res, NULL, 0);
591}
592
593static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name,
594 char *link)
595{
596 int res;
597 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000598 struct fuse_cred cred;
Miklos Szeredib483c932001-10-29 14:57:57 +0000599
Miklos Szeredia181e612001-11-06 12:03:23 +0000600 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000601 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000602 path = get_path_name(f, in->ino, name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000603 if(path != NULL) {
604 res = -ENOSYS;
605 if(f->op.symlink)
Miklos Szeredia181e612001-11-06 12:03:23 +0000606 res = f->op.symlink(&cred, link, path);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000607 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000608 }
Miklos Szeredib483c932001-10-29 14:57:57 +0000609 send_reply(f, in, res, NULL, 0);
610}
611
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000612static void do_rename(struct fuse *f, struct fuse_in_header *in,
613 struct fuse_rename_in *inarg)
614{
615 int res;
616 fino_t olddir = in->ino;
617 fino_t newdir = inarg->newdir;
618 char *oldname = inarg->names;
619 char *newname = inarg->names + strlen(oldname) + 1;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000620 char *oldpath;
621 char *newpath;
Miklos Szeredia181e612001-11-06 12:03:23 +0000622 struct fuse_cred cred;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000623
Miklos Szeredia181e612001-11-06 12:03:23 +0000624 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000625 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000626 oldpath = get_path_name(f, olddir, oldname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000627 if(oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000628 newpath = get_path_name(f, newdir, newname);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000629 if(newpath != NULL) {
630 res = -ENOSYS;
631 if(f->op.rename)
Miklos Szeredia181e612001-11-06 12:03:23 +0000632 res = f->op.rename(&cred, oldpath, newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000633 if(res == 0)
634 rename_node(f, olddir, oldname, newdir, newname);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000635 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000636 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000637 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000638 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000639 send_reply(f, in, res, NULL, 0);
640}
641
642static void do_link(struct fuse *f, struct fuse_in_header *in,
Miklos Szeredi5e183482001-10-31 14:52:35 +0000643 struct fuse_link_in *arg)
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000644{
645 int res;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000646 char *oldpath;
647 char *newpath;
Miklos Szeredia181e612001-11-06 12:03:23 +0000648 struct fuse_cred cred;
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000649
Miklos Szeredia181e612001-11-06 12:03:23 +0000650 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000651 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000652 oldpath = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000653 if(oldpath != NULL) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000654 newpath = get_path_name(f, arg->newdir, arg->name);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000655 if(newpath != NULL) {
656 res = -ENOSYS;
657 if(f->op.link)
Miklos Szeredia181e612001-11-06 12:03:23 +0000658 res = f->op.link(&cred, oldpath, newpath);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000659 free(newpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000660 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000661 free(oldpath);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000662 }
Miklos Szeredi19dff1b2001-10-30 15:06:52 +0000663 send_reply(f, in, res, NULL, 0);
664}
665
Miklos Szeredi5e183482001-10-31 14:52:35 +0000666static void do_open(struct fuse *f, struct fuse_in_header *in,
667 struct fuse_open_in *arg)
668{
669 int res;
670 char *path;
Miklos Szeredia181e612001-11-06 12:03:23 +0000671 struct fuse_cred cred;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000672
Miklos Szeredia181e612001-11-06 12:03:23 +0000673 fill_cred(in, &cred);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000674 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000675 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000676 if(path != NULL) {
677 res = -ENOSYS;
678 if(f->op.open)
Miklos Szeredia181e612001-11-06 12:03:23 +0000679 res = f->op.open(&cred, path, arg->flags);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000680 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000681 }
682 send_reply(f, in, res, NULL, 0);
683}
684
685static void do_read(struct fuse *f, struct fuse_in_header *in,
686 struct fuse_read_in *arg)
687{
688 int res;
689 char *path;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000690 char *buf = (char *) malloc(arg->size);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000691 size_t size;
Miklos Szeredia181e612001-11-06 12:03:23 +0000692 struct fuse_cred cred;
Miklos Szeredi5e183482001-10-31 14:52:35 +0000693
Miklos Szeredia181e612001-11-06 12:03:23 +0000694 fill_cred(in, &cred);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000695 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000696 path = get_path(f, in->ino);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000697 if(path != NULL) {
698 res = -ENOSYS;
Miklos Szeredia181e612001-11-06 12:03:23 +0000699 if(f->op.read)
700 res = f->op.read(&cred, path, buf, arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000701 free(path);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000702 }
703
704 size = 0;
705 if(res > 0) {
706 size = res;
707 res = 0;
708 }
709
710 send_reply(f, in, res, buf, size);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000711 free(buf);
Miklos Szeredi5e183482001-10-31 14:52:35 +0000712}
Miklos Szeredib483c932001-10-29 14:57:57 +0000713
Miklos Szeredia181e612001-11-06 12:03:23 +0000714static void do_write(struct fuse *f, struct fuse_in_header *in,
715 struct fuse_write_in *arg)
716{
717 int res;
718 char *path;
719 struct fuse_cred cred;
720
721 fill_cred(in, &cred);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000722 res = -ENOENT;
Miklos Szeredia181e612001-11-06 12:03:23 +0000723 path = get_path(f, in->ino);
724 if(path != NULL) {
725 res = -ENOSYS;
726 if(f->op.write)
727 res = f->op.write(&cred, path, arg->buf, arg->size, arg->offset);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000728 free(path);
Miklos Szeredia181e612001-11-06 12:03:23 +0000729 }
730
731 if(res > 0) {
732 if((size_t) res != arg->size) {
733 fprintf(stderr, "short write: %u (should be %u)\n", res,
734 arg->size);
735 res = -EIO;
736 }
737 else
738 res = 0;
739 }
740
741 send_reply(f, in, res, NULL, 0);
742}
743
744struct cmd {
745 struct fuse *f;
746 char *buf;
747 size_t buflen;
748};
749
750static void *do_command(void *data)
751{
752 struct cmd *cmd = (struct cmd *) data;
753 struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf;
754 void *inarg = cmd->buf + sizeof(struct fuse_in_header);
755 size_t argsize;
756 struct fuse *f = cmd->f;
757
758 printf("unique: %i, opcode: %i, ino: %li, insize: %i\n", in->unique,
759 in->opcode, in->ino, cmd->buflen);
760 fflush(stdout);
761
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 }
833
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000834 free(cmd->buf);
835 free(cmd);
Miklos Szeredia181e612001-11-06 12:03:23 +0000836
837 return NULL;
838}
839
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000840/* This hack makes it possible to link FUSE with or without the
841 pthread library */
842__attribute__((weak))
843int pthread_create(pthread_t *thrid __attribute__((unused)),
844 const pthread_attr_t *attr __attribute__((unused)),
845 void *(*func)(void *) __attribute__((unused)),
846 void *arg __attribute__((unused)))
847{
848 return ENOSYS;
849}
850
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000851void fuse_loop(struct fuse *f)
852{
853 int res;
854 char inbuf[FUSE_MAX_IN];
Miklos Szeredia181e612001-11-06 12:03:23 +0000855 pthread_attr_t attr;
856 pthread_t thrid;
857
858 pthread_attr_init(&attr);
859 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000860
861 while(1) {
Miklos Szeredia181e612001-11-06 12:03:23 +0000862 struct cmd *cmd;
863
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000864 res = read(f->fd, inbuf, sizeof(inbuf));
865 if(res == -1) {
866 perror("reading fuse device");
867 continue;
868 }
Miklos Szeredia181e612001-11-06 12:03:23 +0000869 if((size_t) res < sizeof(struct fuse_in_header)) {
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000870 fprintf(stderr, "short read on fuse device\n");
871 continue;
872 }
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000873
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000874 cmd = (struct cmd *) malloc(sizeof(struct cmd));
Miklos Szeredia181e612001-11-06 12:03:23 +0000875 cmd->f = f;
876 cmd->buflen = res;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000877 cmd->buf = (char *) malloc(cmd->buflen);
Miklos Szeredia181e612001-11-06 12:03:23 +0000878 memcpy(cmd->buf, inbuf, cmd->buflen);
879
880 if(f->flags & FUSE_MULTITHREAD) {
881 res = pthread_create(&thrid, &attr, do_command, cmd);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000882 if(res == 0)
883 continue;
884
885 fprintf(stderr, "Error creating thread: %s\n", strerror(res));
886 fprintf(stderr, "Will run in single thread mode\n");
887 f->flags &= ~FUSE_MULTITHREAD;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000888 }
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000889
890 do_command(cmd);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000891 }
892}
893
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000894struct fuse *fuse_new(int flags, mode_t rootmode)
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000895{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000896 struct fuse *f;
897 struct node *root;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000898
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000899 f = (struct fuse *) calloc(1, sizeof(struct fuse));
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000900
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000901 if(!rootmode)
902 rootmode = S_IFDIR;
903
904 if(!S_ISDIR(rootmode) && !S_ISREG(rootmode)) {
905 fprintf(stderr, "Invalid mode for root: 0%o\n", rootmode);
906 rootmode = S_IFDIR;
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000907 }
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000908 rootmode &= S_IFMT;
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000909
Miklos Szeredia181e612001-11-06 12:03:23 +0000910 f->flags = flags;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000911 f->rootmode = rootmode;
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000912 f->fd = -1;
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000913 f->mnt = NULL;
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000914 f->ctr = 0;
915 f->name_table_size = 14057;
916 f->name_table = (struct node **)
917 calloc(1, sizeof(struct node *) * f->name_table_size);
918 f->ino_table_size = 14057;
919 f->ino_table = (struct node **)
920 calloc(1, sizeof(struct node *) * f->ino_table_size);
Miklos Szeredia181e612001-11-06 12:03:23 +0000921 pthread_mutex_init(&f->lock, NULL);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000922
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000923 root = (struct node *) calloc(1, sizeof(struct node));
924 root->mode = rootmode;
925 root->rdev = 0;
926 root->name = strdup("/");
927 root->parent = 0;
928 hash_ino(f, root, FUSE_ROOT_INO);
929
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000930 return f;
931}
932
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000933void fuse_set_operations(struct fuse *f, const struct fuse_operations *op)
934{
935 f->op = *op;
936}
937
938void fuse_destroy(struct fuse *f)
939{
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000940 size_t i;
Miklos Szeredia181e612001-11-06 12:03:23 +0000941 close(f->fd);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000942 for(i = 0; i < f->ino_table_size; i++) {
943 struct node *node;
944 struct node *next;
945 for(node = f->ino_table[i]; node != NULL; node = next) {
946 next = node->ino_next;
947 free_node(node);
948 }
949 }
950 free(f->ino_table);
951 free(f->name_table);
Miklos Szeredia181e612001-11-06 12:03:23 +0000952 pthread_mutex_destroy(&f->lock);
Miklos Szeredi97c61e92001-11-07 12:09:43 +0000953 free(f);
Miklos Szeredi85c74fc2001-10-28 19:44:14 +0000954}