Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
| 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 Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 14 | #include <unistd.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 15 | #include <limits.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 18 | #define FUSE_MAX_PATH 4096 |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 19 | |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 20 | #define FUSE_KERNEL_VERSION_ENV "_FUSE_KERNEL_VERSION" |
| 21 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 22 | static inline void inc_avail(struct fuse *f) |
| 23 | { |
| 24 | pthread_mutex_lock(&f->lock); |
| 25 | f->numavail ++; |
| 26 | pthread_mutex_unlock(&f->lock); |
| 27 | } |
| 28 | |
| 29 | static inline void dec_avail(struct fuse *f) |
| 30 | { |
| 31 | pthread_mutex_lock(&f->lock); |
| 32 | f->numavail --; |
| 33 | pthread_mutex_unlock(&f->lock); |
| 34 | } |
| 35 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 36 | static struct node *__get_node(struct fuse *f, fino_t ino) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 37 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 38 | size_t hash = ino % f->ino_table_size; |
| 39 | struct node *node; |
| 40 | |
| 41 | for(node = f->ino_table[hash]; node != NULL; node = node->ino_next) |
| 42 | if(node->ino == ino) |
| 43 | return node; |
| 44 | |
| 45 | return NULL; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 48 | static struct node *get_node(struct fuse *f, fino_t ino) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 49 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 50 | struct node *node = __get_node(f, ino); |
| 51 | if(node != NULL) |
| 52 | return node; |
| 53 | |
| 54 | fprintf(stderr, "fuse internal error: inode %lu not found\n", ino); |
| 55 | abort(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 56 | } |
| 57 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 58 | static void hash_ino(struct fuse *f, struct node *node, fino_t ino) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 59 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 60 | size_t hash = ino % f->ino_table_size; |
| 61 | node->ino = ino; |
| 62 | |
| 63 | node->ino_next = f->ino_table[hash]; |
| 64 | f->ino_table[hash] = node; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 67 | static void unhash_ino(struct fuse *f, struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 68 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 69 | size_t hash = node->ino % f->ino_table_size; |
| 70 | struct node **nodep = &f->ino_table[hash]; |
| 71 | |
| 72 | for(; *nodep != NULL; nodep = &(*nodep)->ino_next) |
| 73 | if(*nodep == node) { |
| 74 | *nodep = node->ino_next; |
| 75 | return; |
| 76 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 77 | } |
| 78 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 79 | static fino_t get_ino(struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 80 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 81 | return node->ino; |
| 82 | } |
| 83 | |
| 84 | static fino_t next_ino(struct fuse *f) |
| 85 | { |
| 86 | while(f->ctr == 0 || __get_node(f, f->ctr) != NULL) |
| 87 | f->ctr++; |
| 88 | |
| 89 | return f->ctr; |
| 90 | } |
| 91 | |
| 92 | static void free_node(struct node *node) |
| 93 | { |
| 94 | free(node->name); |
| 95 | free(node); |
| 96 | } |
| 97 | |
| 98 | static unsigned int name_hash(struct fuse *f, fino_t parent, const char *name) |
| 99 | { |
| 100 | unsigned int hash = *name; |
| 101 | |
| 102 | if(hash) |
| 103 | for(name += 1; *name != '\0'; name++) |
| 104 | hash = (hash << 5) - hash + *name; |
| 105 | |
| 106 | return (hash + parent) % f->name_table_size; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 109 | static struct node *lookup_node(struct fuse *f, fino_t parent, |
| 110 | const char *name) |
| 111 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 112 | size_t hash = name_hash(f, parent, name); |
| 113 | struct node *node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 114 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 115 | for(node = f->name_table[hash]; node != NULL; node = node->name_next) |
| 116 | if(node->parent == parent && strcmp(node->name, name) == 0) |
| 117 | return node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 118 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 119 | return NULL; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 122 | static void hash_name(struct fuse *f, struct node *node, fino_t parent, |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 123 | const char *name) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 124 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 125 | size_t hash = name_hash(f, parent, name); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 126 | node->parent = parent; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 127 | node->name = strdup(name); |
| 128 | node->name_next = f->name_table[hash]; |
| 129 | f->name_table[hash] = node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 132 | static void unhash_name(struct fuse *f, struct node *node) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 133 | { |
| 134 | if(node->name != NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 135 | size_t hash = name_hash(f, node->parent, node->name); |
| 136 | struct node **nodep = &f->name_table[hash]; |
| 137 | |
| 138 | for(; *nodep != NULL; nodep = &(*nodep)->name_next) |
| 139 | if(*nodep == node) { |
| 140 | *nodep = node->name_next; |
| 141 | node->name_next = NULL; |
| 142 | free(node->name); |
| 143 | node->name = NULL; |
| 144 | node->parent = 0; |
| 145 | return; |
| 146 | } |
| 147 | fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n", |
| 148 | node->ino); |
| 149 | abort(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 150 | } |
| 151 | } |
| 152 | |
| 153 | static fino_t find_node(struct fuse *f, fino_t parent, char *name, |
| 154 | struct fuse_attr *attr, int version) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 155 | { |
| 156 | struct node *node; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 157 | int mode = attr->mode & S_IFMT; |
| 158 | int rdev = 0; |
| 159 | |
| 160 | if(S_ISCHR(mode) || S_ISBLK(mode)) |
| 161 | rdev = attr->rdev; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 162 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 163 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 164 | node = lookup_node(f, parent, name); |
| 165 | if(node != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 166 | if(node->mode == mode && node->rdev == rdev) |
| 167 | goto out; |
| 168 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 169 | unhash_name(f, node); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 172 | node = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 173 | node->mode = mode; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 174 | node->rdev = rdev; |
| 175 | hash_ino(f, node, next_ino(f)); |
| 176 | hash_name(f, node, parent, name); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 177 | |
| 178 | out: |
| 179 | node->version = version; |
| 180 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 181 | return get_ino(node); |
| 182 | } |
| 183 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 184 | static char *add_name(char *buf, char *s, const char *name) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 185 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 186 | size_t len = strlen(name); |
| 187 | s -= len; |
| 188 | if(s <= buf) { |
| 189 | fprintf(stderr, "fuse: path too long: ...%s\n", s + len); |
| 190 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 191 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 192 | strncpy(s, name, len); |
| 193 | s--; |
| 194 | *s = '/'; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 195 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 196 | return s; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 199 | static char *get_path_name(struct fuse *f, fino_t ino, const char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 200 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 201 | char buf[FUSE_MAX_PATH]; |
| 202 | char *s = buf + FUSE_MAX_PATH - 1; |
| 203 | struct node *node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 204 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 205 | *s = '\0'; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 206 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 207 | if(name != NULL) { |
| 208 | s = add_name(buf, s, name); |
| 209 | if(s == NULL) |
| 210 | return NULL; |
| 211 | } |
| 212 | |
| 213 | pthread_mutex_lock(&f->lock); |
| 214 | for(node = get_node(f, ino); node->ino != FUSE_ROOT_INO; |
| 215 | node = get_node(f, node->parent)) { |
| 216 | if(node->name == NULL) { |
| 217 | s = NULL; |
| 218 | break; |
| 219 | } |
| 220 | |
| 221 | s = add_name(buf, s, node->name); |
| 222 | if(s == NULL) |
| 223 | break; |
| 224 | } |
| 225 | pthread_mutex_unlock(&f->lock); |
| 226 | |
| 227 | if(s == NULL) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 228 | return NULL; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 229 | else if(*s == '\0') |
| 230 | return strdup("/"); |
| 231 | else |
| 232 | return strdup(s); |
| 233 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 234 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 235 | static char *get_path(struct fuse *f, fino_t ino) |
| 236 | { |
| 237 | return get_path_name(f, ino, NULL); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 238 | } |
| 239 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 240 | static void destroy_node(struct fuse *f, fino_t ino, int version) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 241 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 242 | struct node *node; |
| 243 | |
| 244 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 245 | node = get_node(f, ino); |
Miklos Szeredi | 39f2867 | 2001-11-14 14:52:54 +0000 | [diff] [blame] | 246 | if(node->version == version && ino != FUSE_ROOT_INO) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 247 | unhash_name(f, node); |
| 248 | unhash_ino(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 249 | free_node(node); |
| 250 | } |
| 251 | pthread_mutex_unlock(&f->lock); |
| 252 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 255 | static void remove_node(struct fuse *f, fino_t dir, const char *name) |
| 256 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 257 | struct node *node; |
| 258 | |
| 259 | pthread_mutex_lock(&f->lock); |
| 260 | node = lookup_node(f, dir, name); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 261 | if(node == NULL) { |
| 262 | fprintf(stderr, "fuse internal error: unable to remove node %lu/%s\n", |
| 263 | dir, name); |
| 264 | abort(); |
| 265 | } |
| 266 | unhash_name(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 267 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 270 | static void rename_node(struct fuse *f, fino_t olddir, const char *oldname, |
| 271 | fino_t newdir, const char *newname) |
| 272 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 273 | struct node *node; |
| 274 | struct node *newnode; |
| 275 | |
| 276 | pthread_mutex_lock(&f->lock); |
| 277 | node = lookup_node(f, olddir, oldname); |
| 278 | newnode = lookup_node(f, newdir, newname); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 279 | if(node == NULL) { |
| 280 | fprintf(stderr, "fuse internal error: unable to rename node %lu/%s\n", |
| 281 | olddir, oldname); |
| 282 | abort(); |
| 283 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 284 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 285 | if(newnode != NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 286 | unhash_name(f, newnode); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 287 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 288 | unhash_name(f, node); |
| 289 | hash_name(f, node, newdir, newname); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 290 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 294 | static void convert_stat(struct stat *stbuf, struct fuse_attr *attr) |
| 295 | { |
| 296 | attr->mode = stbuf->st_mode; |
| 297 | attr->nlink = stbuf->st_nlink; |
| 298 | attr->uid = stbuf->st_uid; |
| 299 | attr->gid = stbuf->st_gid; |
| 300 | attr->rdev = stbuf->st_rdev; |
| 301 | attr->size = stbuf->st_size; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 302 | attr->blocks = stbuf->st_blocks; |
| 303 | attr->atime = stbuf->st_atime; |
| 304 | attr->mtime = stbuf->st_mtime; |
| 305 | attr->ctime = stbuf->st_ctime; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 306 | attr->_dummy = 4096; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 307 | } |
| 308 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 309 | static int fill_dir(struct fuse_dirhandle *dh, char *name, int type) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 310 | { |
| 311 | struct fuse_dirent dirent; |
| 312 | size_t reclen; |
| 313 | size_t res; |
| 314 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 315 | dirent.ino = (unsigned long) -1; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 316 | dirent.namelen = strlen(name); |
| 317 | strncpy(dirent.name, name, sizeof(dirent.name)); |
| 318 | dirent.type = type; |
| 319 | reclen = FUSE_DIRENT_SIZE(&dirent); |
| 320 | res = fwrite(&dirent, reclen, 1, dh->fp); |
| 321 | if(res == 0) { |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 322 | perror("fuse: writing directory file"); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 323 | return -EIO; |
| 324 | } |
| 325 | return 0; |
| 326 | } |
| 327 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 328 | static void send_reply_raw(struct fuse *f, char *outbuf, size_t outsize) |
| 329 | { |
| 330 | int res; |
| 331 | |
| 332 | if((f->flags & FUSE_DEBUG)) { |
| 333 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 334 | printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique, |
| 335 | out->error, strerror(-out->error), outsize); |
| 336 | fflush(stdout); |
| 337 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 338 | |
| 339 | /* This needs to be done before the reply because otherwise the |
| 340 | scheduler can tricks with us, and only let the counter be increased |
| 341 | long after the operation is done */ |
| 342 | inc_avail(f); |
| 343 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 344 | res = write(f->fd, outbuf, outsize); |
| 345 | if(res == -1) { |
| 346 | /* ENOENT means the operation was interrupted */ |
| 347 | if(errno != ENOENT) |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 348 | perror("fuse: writing device"); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
| 351 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 352 | static void send_reply(struct fuse *f, struct fuse_in_header *in, int error, |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 353 | void *arg, size_t argsize) |
| 354 | { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 355 | char *outbuf; |
| 356 | size_t outsize; |
| 357 | struct fuse_out_header *out; |
| 358 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 359 | if(error > 0) { |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 360 | fprintf(stderr, "fuse: positive error code: %i\n", error); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 361 | error = -ERANGE; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 364 | if(error) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 365 | argsize = 0; |
| 366 | |
| 367 | outsize = sizeof(struct fuse_out_header) + argsize; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 368 | outbuf = (char *) malloc(outsize); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 369 | out = (struct fuse_out_header *) outbuf; |
| 370 | out->unique = in->unique; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 371 | out->error = error; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 372 | if(argsize != 0) |
| 373 | memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize); |
| 374 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 375 | send_reply_raw(f, outbuf, outsize); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 376 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 377 | free(outbuf); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name) |
| 381 | { |
| 382 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 383 | char *path; |
| 384 | struct stat buf; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 385 | struct fuse_lookup_out arg; |
| 386 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 387 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 388 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 389 | if(path != NULL) { |
| 390 | res = -ENOSYS; |
| 391 | if(f->op.getattr) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 392 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 393 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 394 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 395 | if(res == 0) { |
| 396 | convert_stat(&buf, &arg.attr); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 397 | arg.ino = find_node(f, in->ino, name, &arg.attr, in->unique); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 398 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 399 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 400 | } |
| 401 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 402 | static void do_forget(struct fuse *f, struct fuse_in_header *in, |
| 403 | struct fuse_forget_in *arg) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 404 | { |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 405 | if(f->flags & FUSE_DEBUG) { |
| 406 | printf("FORGET %li/%i\n", in->ino, arg->version); |
| 407 | fflush(stdout); |
| 408 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 409 | destroy_node(f, in->ino, arg->version); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | static void do_getattr(struct fuse *f, struct fuse_in_header *in) |
| 413 | { |
| 414 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 415 | char *path; |
| 416 | struct stat buf; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 417 | struct fuse_getattr_out arg; |
| 418 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 419 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 420 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 421 | if(path != NULL) { |
| 422 | res = -ENOSYS; |
| 423 | if(f->op.getattr) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 424 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 425 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 426 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 427 | if(res == 0) |
| 428 | convert_stat(&buf, &arg.attr); |
| 429 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 430 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 431 | } |
| 432 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 433 | static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 434 | { |
| 435 | int res; |
| 436 | |
| 437 | res = -ENOSYS; |
| 438 | if(f->op.chmod) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 439 | res = f->op.chmod(path, attr->mode); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 440 | |
| 441 | return res; |
| 442 | } |
| 443 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 444 | static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr, |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 445 | int valid) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 446 | { |
| 447 | int res; |
| 448 | uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1; |
| 449 | gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1; |
| 450 | |
| 451 | res = -ENOSYS; |
| 452 | if(f->op.chown) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 453 | res = f->op.chown(path, uid, gid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 454 | |
| 455 | return res; |
| 456 | } |
| 457 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 458 | static int do_truncate(struct fuse *f, const char *path, |
| 459 | struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 460 | { |
| 461 | int res; |
| 462 | |
| 463 | res = -ENOSYS; |
| 464 | if(f->op.truncate) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 465 | res = f->op.truncate(path, attr->size); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 466 | |
| 467 | return res; |
| 468 | } |
| 469 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 470 | static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 471 | { |
| 472 | int res; |
| 473 | struct utimbuf buf; |
| 474 | buf.actime = attr->atime; |
| 475 | buf.modtime = attr->mtime; |
| 476 | res = -ENOSYS; |
| 477 | if(f->op.utime) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 478 | res = f->op.utime(path, &buf); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 479 | |
| 480 | return res; |
| 481 | } |
| 482 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 483 | static void do_setattr(struct fuse *f, struct fuse_in_header *in, |
| 484 | struct fuse_setattr_in *arg) |
| 485 | { |
| 486 | int res; |
| 487 | char *path; |
| 488 | int valid = arg->valid; |
| 489 | struct fuse_attr *attr = &arg->attr; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 490 | struct fuse_setattr_out outarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 491 | |
| 492 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 493 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 494 | if(path != NULL) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 495 | res = -ENOSYS; |
| 496 | if(f->op.getattr) { |
| 497 | res = 0; |
| 498 | if(!res && (valid & FATTR_MODE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 499 | res = do_chmod(f, path, attr); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 500 | if(!res && (valid & (FATTR_UID | FATTR_GID))) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 501 | res = do_chown(f, path, attr, valid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 502 | if(!res && (valid & FATTR_SIZE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 503 | res = do_truncate(f, path, attr); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 504 | if(!res && (valid & FATTR_UTIME)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 505 | res = do_utime(f, path, attr); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 506 | if(!res) { |
| 507 | struct stat buf; |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 508 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 509 | if(!res) |
| 510 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 511 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 512 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 513 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 514 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 515 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 516 | } |
| 517 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 518 | static void do_readlink(struct fuse *f, struct fuse_in_header *in) |
| 519 | { |
| 520 | int res; |
| 521 | char link[PATH_MAX + 1]; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 522 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 523 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 524 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 525 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 526 | if(path != NULL) { |
| 527 | res = -ENOSYS; |
| 528 | if(f->op.readlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 529 | res = f->op.readlink(path, link, sizeof(link)); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 530 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 531 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 532 | link[PATH_MAX] = '\0'; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 533 | send_reply(f, in, res, link, !res ? strlen(link) : 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 534 | } |
| 535 | |
| 536 | static void do_getdir(struct fuse *f, struct fuse_in_header *in) |
| 537 | { |
| 538 | int res; |
| 539 | struct fuse_getdir_out arg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 540 | struct fuse_dirhandle dh; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 541 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 542 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 543 | dh.fuse = f; |
| 544 | dh.fp = tmpfile(); |
| 545 | dh.dir = in->ino; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 546 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 547 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 548 | if(path != NULL) { |
| 549 | res = -ENOSYS; |
| 550 | if(f->op.getdir) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 551 | res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 552 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 553 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 554 | fflush(dh.fp); |
| 555 | arg.fd = fileno(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 556 | send_reply(f, in, res, &arg, sizeof(arg)); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 557 | fclose(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 560 | static void do_mknod(struct fuse *f, struct fuse_in_header *in, |
| 561 | struct fuse_mknod_in *inarg) |
| 562 | { |
| 563 | int res; |
| 564 | char *path; |
| 565 | struct fuse_mknod_out outarg; |
| 566 | struct stat buf; |
| 567 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 568 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 569 | path = get_path_name(f, in->ino, inarg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 570 | if(path != NULL) { |
| 571 | res = -ENOSYS; |
| 572 | if(f->op.mknod && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 573 | res = f->op.mknod(path, inarg->mode, inarg->rdev); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 574 | if(res == 0) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 575 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 576 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 577 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 578 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 579 | if(res == 0) { |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 580 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 581 | outarg.ino = find_node(f, in->ino, inarg->name, &outarg.attr, |
| 582 | in->unique); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 583 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 584 | |
| 585 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 586 | } |
| 587 | |
| 588 | static void do_mkdir(struct fuse *f, struct fuse_in_header *in, |
| 589 | struct fuse_mkdir_in *inarg) |
| 590 | { |
| 591 | int res; |
| 592 | char *path; |
| 593 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 594 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 595 | path = get_path_name(f, in->ino, inarg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 596 | if(path != NULL) { |
| 597 | res = -ENOSYS; |
| 598 | if(f->op.mkdir) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 599 | res = f->op.mkdir(path, inarg->mode); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 600 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 601 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 602 | send_reply(f, in, res, NULL, 0); |
| 603 | } |
| 604 | |
| 605 | static void do_remove(struct fuse *f, struct fuse_in_header *in, char *name) |
| 606 | { |
| 607 | int res; |
| 608 | char *path; |
| 609 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 610 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 611 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 612 | if(path != NULL) { |
| 613 | res = -ENOSYS; |
| 614 | if(in->opcode == FUSE_UNLINK) { |
| 615 | if(f->op.unlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 616 | res = f->op.unlink(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 617 | } |
| 618 | else { |
| 619 | if(f->op.rmdir) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 620 | res = f->op.rmdir(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 621 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 622 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 623 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 624 | if(res == 0) |
| 625 | remove_node(f, in->ino, name); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 626 | send_reply(f, in, res, NULL, 0); |
| 627 | } |
| 628 | |
| 629 | static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name, |
| 630 | char *link) |
| 631 | { |
| 632 | int res; |
| 633 | char *path; |
| 634 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 635 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 636 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 637 | if(path != NULL) { |
| 638 | res = -ENOSYS; |
| 639 | if(f->op.symlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 640 | res = f->op.symlink(link, path); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 641 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 642 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 643 | send_reply(f, in, res, NULL, 0); |
| 644 | } |
| 645 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 646 | static void do_rename(struct fuse *f, struct fuse_in_header *in, |
| 647 | struct fuse_rename_in *inarg) |
| 648 | { |
| 649 | int res; |
| 650 | fino_t olddir = in->ino; |
| 651 | fino_t newdir = inarg->newdir; |
| 652 | char *oldname = inarg->names; |
| 653 | char *newname = inarg->names + strlen(oldname) + 1; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 654 | char *oldpath; |
| 655 | char *newpath; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 656 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 657 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 658 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 659 | if(oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 660 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 661 | if(newpath != NULL) { |
| 662 | res = -ENOSYS; |
| 663 | if(f->op.rename) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 664 | res = f->op.rename(oldpath, newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 665 | if(res == 0) |
| 666 | rename_node(f, olddir, oldname, newdir, newname); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 667 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 668 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 669 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 670 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 671 | send_reply(f, in, res, NULL, 0); |
| 672 | } |
| 673 | |
| 674 | static void do_link(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 675 | struct fuse_link_in *arg) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 676 | { |
| 677 | int res; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 678 | char *oldpath; |
| 679 | char *newpath; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 680 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 681 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 682 | oldpath = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 683 | if(oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 684 | newpath = get_path_name(f, arg->newdir, arg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 685 | if(newpath != NULL) { |
| 686 | res = -ENOSYS; |
| 687 | if(f->op.link) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 688 | res = f->op.link(oldpath, newpath); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 689 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 690 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 691 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 692 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 693 | send_reply(f, in, res, NULL, 0); |
| 694 | } |
| 695 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 696 | static void do_open(struct fuse *f, struct fuse_in_header *in, |
| 697 | struct fuse_open_in *arg) |
| 698 | { |
| 699 | int res; |
| 700 | char *path; |
| 701 | |
| 702 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 703 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 704 | if(path != NULL) { |
| 705 | res = -ENOSYS; |
| 706 | if(f->op.open) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 707 | res = f->op.open(path, arg->flags); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 708 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 709 | } |
| 710 | send_reply(f, in, res, NULL, 0); |
| 711 | } |
| 712 | |
| 713 | static void do_read(struct fuse *f, struct fuse_in_header *in, |
| 714 | struct fuse_read_in *arg) |
| 715 | { |
| 716 | int res; |
| 717 | char *path; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 718 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size); |
| 719 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 720 | char *buf = outbuf + sizeof(struct fuse_out_header); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 721 | size_t size; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 722 | size_t outsize; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 723 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 724 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 725 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 726 | if(path != NULL) { |
| 727 | res = -ENOSYS; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 728 | if(f->op.read) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 729 | res = f->op.read(path, buf, arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 730 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | size = 0; |
| 734 | if(res > 0) { |
| 735 | size = res; |
| 736 | res = 0; |
| 737 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 738 | out->unique = in->unique; |
| 739 | out->error = res; |
| 740 | outsize = sizeof(struct fuse_out_header) + size; |
| 741 | |
| 742 | send_reply_raw(f, outbuf, outsize); |
| 743 | free(outbuf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 744 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 745 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 746 | static void do_write(struct fuse *f, struct fuse_in_header *in, |
| 747 | struct fuse_write_in *arg) |
| 748 | { |
| 749 | int res; |
| 750 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 751 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 752 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 753 | path = get_path(f, in->ino); |
| 754 | if(path != NULL) { |
| 755 | res = -ENOSYS; |
| 756 | if(f->op.write) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 757 | res = f->op.write(path, arg->buf, arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 758 | free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | if(res > 0) { |
| 762 | if((size_t) res != arg->size) { |
| 763 | fprintf(stderr, "short write: %u (should be %u)\n", res, |
| 764 | arg->size); |
| 765 | res = -EIO; |
| 766 | } |
| 767 | else |
| 768 | res = 0; |
| 769 | } |
| 770 | |
| 771 | send_reply(f, in, res, NULL, 0); |
| 772 | } |
| 773 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 774 | static void free_cmd(struct fuse_cmd *cmd) |
| 775 | { |
| 776 | free(cmd->buf); |
| 777 | free(cmd); |
| 778 | } |
| 779 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 780 | void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 781 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 782 | struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf; |
| 783 | void *inarg = cmd->buf + sizeof(struct fuse_in_header); |
| 784 | size_t argsize; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 785 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 786 | dec_avail(f); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 787 | |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 788 | if((f->flags & FUSE_DEBUG)) { |
| 789 | printf("unique: %i, opcode: %i, ino: %li, insize: %i\n", in->unique, |
| 790 | in->opcode, in->ino, cmd->buflen); |
| 791 | fflush(stdout); |
| 792 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 793 | |
| 794 | argsize = cmd->buflen - sizeof(struct fuse_in_header); |
| 795 | |
| 796 | switch(in->opcode) { |
| 797 | case FUSE_LOOKUP: |
| 798 | do_lookup(f, in, (char *) inarg); |
| 799 | break; |
| 800 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 801 | case FUSE_GETATTR: |
| 802 | do_getattr(f, in); |
| 803 | break; |
| 804 | |
| 805 | case FUSE_SETATTR: |
| 806 | do_setattr(f, in, (struct fuse_setattr_in *) inarg); |
| 807 | break; |
| 808 | |
| 809 | case FUSE_READLINK: |
| 810 | do_readlink(f, in); |
| 811 | break; |
| 812 | |
| 813 | case FUSE_GETDIR: |
| 814 | do_getdir(f, in); |
| 815 | break; |
| 816 | |
| 817 | case FUSE_MKNOD: |
| 818 | do_mknod(f, in, (struct fuse_mknod_in *) inarg); |
| 819 | break; |
| 820 | |
| 821 | case FUSE_MKDIR: |
| 822 | do_mkdir(f, in, (struct fuse_mkdir_in *) inarg); |
| 823 | break; |
| 824 | |
| 825 | case FUSE_UNLINK: |
| 826 | case FUSE_RMDIR: |
| 827 | do_remove(f, in, (char *) inarg); |
| 828 | break; |
| 829 | |
| 830 | case FUSE_SYMLINK: |
| 831 | do_symlink(f, in, (char *) inarg, |
| 832 | ((char *) inarg) + strlen((char *) inarg) + 1); |
| 833 | break; |
| 834 | |
| 835 | case FUSE_RENAME: |
| 836 | do_rename(f, in, (struct fuse_rename_in *) inarg); |
| 837 | break; |
| 838 | |
| 839 | case FUSE_LINK: |
| 840 | do_link(f, in, (struct fuse_link_in *) inarg); |
| 841 | break; |
| 842 | |
| 843 | case FUSE_OPEN: |
| 844 | do_open(f, in, (struct fuse_open_in *) inarg); |
| 845 | break; |
| 846 | |
| 847 | case FUSE_READ: |
| 848 | do_read(f, in, (struct fuse_read_in *) inarg); |
| 849 | break; |
| 850 | |
| 851 | case FUSE_WRITE: |
| 852 | do_write(f, in, (struct fuse_write_in *) inarg); |
| 853 | break; |
| 854 | |
| 855 | default: |
| 856 | fprintf(stderr, "Operation %i not implemented\n", in->opcode); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 857 | send_reply(f, in, -ENOSYS, NULL, 0); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 858 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 859 | |
| 860 | free_cmd(cmd); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 861 | } |
| 862 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 863 | struct fuse_cmd *__fuse_read_cmd(struct fuse *f) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 864 | { |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 865 | ssize_t res; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 866 | struct fuse_cmd *cmd; |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 867 | struct fuse_in_header *in; |
| 868 | void *inarg; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 869 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 870 | cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd)); |
| 871 | cmd->buf = (char *) malloc(FUSE_MAX_IN); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 872 | in = (struct fuse_in_header *) cmd->buf; |
| 873 | inarg = cmd->buf + sizeof(struct fuse_in_header); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 874 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 875 | do { |
| 876 | res = read(f->fd, cmd->buf, FUSE_MAX_IN); |
| 877 | if(res == -1) { |
| 878 | /* ENODEV means we got unmounted, so we silenty return failure */ |
| 879 | if(errno != ENODEV) { |
| 880 | perror("fuse: reading device"); |
| 881 | /* BAD... This will happen again */ |
| 882 | } |
| 883 | free_cmd(cmd); |
| 884 | return NULL; |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 885 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 886 | if((size_t) res < sizeof(struct fuse_in_header)) { |
| 887 | fprintf(stderr, "short read on fuse device\n"); |
| 888 | /* Cannot happen */ |
| 889 | free_cmd(cmd); |
| 890 | return NULL; |
| 891 | } |
| 892 | cmd->buflen = res; |
| 893 | |
| 894 | /* Forget is special, it can be done without messing with threads. */ |
| 895 | if(in->opcode == FUSE_FORGET) |
| 896 | do_forget(f, in, (struct fuse_forget_in *) inarg); |
| 897 | |
| 898 | } while(in->opcode == FUSE_FORGET); |
| 899 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 900 | return cmd; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 901 | } |
| 902 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 903 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 904 | void fuse_loop(struct fuse *f) |
| 905 | { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 906 | while(1) { |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 907 | struct fuse_cmd *cmd = __fuse_read_cmd(f); |
| 908 | if(cmd == NULL) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 909 | exit(1); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 910 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 911 | __fuse_process_cmd(f, cmd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 912 | } |
| 913 | } |
| 914 | |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 915 | struct fuse *fuse_new(int fd, int flags) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 916 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 917 | struct fuse *f; |
| 918 | struct node *root; |
Miklos Szeredi | fe42812 | 2001-11-20 19:12:28 +0000 | [diff] [blame] | 919 | char verstr[128]; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 920 | char *realver = getenv(FUSE_KERNEL_VERSION_ENV); |
Miklos Szeredi | fe42812 | 2001-11-20 19:12:28 +0000 | [diff] [blame] | 921 | |
| 922 | if(realver != NULL) { |
| 923 | sprintf(verstr, "%i", FUSE_KERNEL_VERSION); |
| 924 | if(strcmp(verstr, realver) != 0) { |
| 925 | fprintf(stderr, |
| 926 | "Warning: FUSE version mismatch: using %s, kernel is %s\n", |
| 927 | realver, verstr); |
| 928 | } |
| 929 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 930 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 931 | f = (struct fuse *) calloc(1, sizeof(struct fuse)); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 932 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 933 | f->flags = flags; |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 934 | f->fd = fd; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 935 | f->ctr = 0; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 936 | /* FIXME: Dynamic hash table */ |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 937 | f->name_table_size = 14057; |
| 938 | f->name_table = (struct node **) |
| 939 | calloc(1, sizeof(struct node *) * f->name_table_size); |
| 940 | f->ino_table_size = 14057; |
| 941 | f->ino_table = (struct node **) |
| 942 | calloc(1, sizeof(struct node *) * f->ino_table_size); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 943 | pthread_mutex_init(&f->lock, NULL); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 944 | f->numworker = 0; |
| 945 | f->numavail = 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 946 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 947 | root = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 948 | root->mode = 0; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 949 | root->rdev = 0; |
| 950 | root->name = strdup("/"); |
| 951 | root->parent = 0; |
| 952 | hash_ino(f, root, FUSE_ROOT_INO); |
| 953 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 954 | return f; |
| 955 | } |
| 956 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 957 | void fuse_set_operations(struct fuse *f, const struct fuse_operations *op) |
| 958 | { |
| 959 | f->op = *op; |
| 960 | } |
| 961 | |
| 962 | void fuse_destroy(struct fuse *f) |
| 963 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 964 | size_t i; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 965 | for(i = 0; i < f->ino_table_size; i++) { |
| 966 | struct node *node; |
| 967 | struct node *next; |
| 968 | for(node = f->ino_table[i]; node != NULL; node = next) { |
| 969 | next = node->ino_next; |
| 970 | free_node(node); |
| 971 | } |
| 972 | } |
| 973 | free(f->ino_table); |
| 974 | free(f->name_table); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 975 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 976 | free(f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 977 | } |