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 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 293 | static void convert_stat(struct stat *stbuf, struct fuse_attr *attr) |
| 294 | { |
| 295 | attr->mode = stbuf->st_mode; |
| 296 | attr->nlink = stbuf->st_nlink; |
| 297 | attr->uid = stbuf->st_uid; |
| 298 | attr->gid = stbuf->st_gid; |
| 299 | attr->rdev = stbuf->st_rdev; |
| 300 | attr->size = stbuf->st_size; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 301 | attr->blocks = stbuf->st_blocks; |
| 302 | attr->atime = stbuf->st_atime; |
| 303 | attr->mtime = stbuf->st_mtime; |
| 304 | attr->ctime = stbuf->st_ctime; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 305 | attr->_dummy = 4096; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 306 | } |
| 307 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 308 | static int fill_dir(struct fuse_dirhandle *dh, char *name, int type) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 309 | { |
| 310 | struct fuse_dirent dirent; |
| 311 | size_t reclen; |
| 312 | size_t res; |
| 313 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 314 | dirent.ino = (unsigned long) -1; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 315 | dirent.namelen = strlen(name); |
| 316 | strncpy(dirent.name, name, sizeof(dirent.name)); |
| 317 | dirent.type = type; |
| 318 | reclen = FUSE_DIRENT_SIZE(&dirent); |
| 319 | res = fwrite(&dirent, reclen, 1, dh->fp); |
| 320 | if(res == 0) { |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 321 | perror("fuse: writing directory file"); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 322 | return -EIO; |
| 323 | } |
| 324 | return 0; |
| 325 | } |
| 326 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 327 | static void send_reply_raw(struct fuse *f, char *outbuf, size_t outsize) |
| 328 | { |
| 329 | int res; |
| 330 | |
| 331 | if((f->flags & FUSE_DEBUG)) { |
| 332 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 333 | printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique, |
| 334 | out->error, strerror(-out->error), outsize); |
| 335 | fflush(stdout); |
| 336 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 337 | |
| 338 | /* This needs to be done before the reply because otherwise the |
| 339 | scheduler can tricks with us, and only let the counter be increased |
| 340 | long after the operation is done */ |
| 341 | inc_avail(f); |
| 342 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 343 | res = write(f->fd, outbuf, outsize); |
| 344 | if(res == -1) { |
| 345 | /* ENOENT means the operation was interrupted */ |
| 346 | if(errno != ENOENT) |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 347 | perror("fuse: writing device"); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 351 | 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] | 352 | void *arg, size_t argsize) |
| 353 | { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 354 | char *outbuf; |
| 355 | size_t outsize; |
| 356 | struct fuse_out_header *out; |
| 357 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 358 | if(error > 0) { |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 359 | fprintf(stderr, "fuse: positive error code: %i\n", error); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 360 | error = -ERANGE; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 363 | if(error) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 364 | argsize = 0; |
| 365 | |
| 366 | outsize = sizeof(struct fuse_out_header) + argsize; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 367 | outbuf = (char *) malloc(outsize); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 368 | out = (struct fuse_out_header *) outbuf; |
| 369 | out->unique = in->unique; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 370 | out->error = error; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 371 | if(argsize != 0) |
| 372 | memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize); |
| 373 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 374 | send_reply_raw(f, outbuf, outsize); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 375 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 376 | free(outbuf); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name) |
| 380 | { |
| 381 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 382 | char *path; |
| 383 | struct stat buf; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 384 | struct fuse_lookup_out arg; |
| 385 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 386 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 387 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 388 | if(path != NULL) { |
| 389 | res = -ENOSYS; |
| 390 | if(f->op.getattr) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 391 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 392 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 393 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 394 | if(res == 0) { |
| 395 | convert_stat(&buf, &arg.attr); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 396 | arg.ino = find_node(f, in->ino, name, &arg.attr, in->unique); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 397 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 398 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 399 | } |
| 400 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 401 | static void do_forget(struct fuse *f, struct fuse_in_header *in, |
| 402 | struct fuse_forget_in *arg) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 403 | { |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 404 | if(f->flags & FUSE_DEBUG) { |
| 405 | printf("FORGET %li/%i\n", in->ino, arg->version); |
| 406 | fflush(stdout); |
| 407 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 408 | destroy_node(f, in->ino, arg->version); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | static void do_getattr(struct fuse *f, struct fuse_in_header *in) |
| 412 | { |
| 413 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 414 | char *path; |
| 415 | struct stat buf; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 416 | struct fuse_getattr_out arg; |
| 417 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 418 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 419 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 420 | if(path != NULL) { |
| 421 | res = -ENOSYS; |
| 422 | if(f->op.getattr) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 423 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 424 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 425 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 426 | if(res == 0) |
| 427 | convert_stat(&buf, &arg.attr); |
| 428 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 429 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 430 | } |
| 431 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 432 | 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] | 433 | { |
| 434 | int res; |
| 435 | |
| 436 | res = -ENOSYS; |
| 437 | if(f->op.chmod) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 438 | res = f->op.chmod(path, attr->mode); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 439 | |
| 440 | return res; |
| 441 | } |
| 442 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 443 | static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr, |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame^] | 444 | int valid) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 445 | { |
| 446 | int res; |
| 447 | uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1; |
| 448 | gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1; |
| 449 | |
| 450 | res = -ENOSYS; |
| 451 | if(f->op.chown) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 452 | res = f->op.chown(path, uid, gid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 453 | |
| 454 | return res; |
| 455 | } |
| 456 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 457 | static int do_truncate(struct fuse *f, const char *path, |
| 458 | struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 459 | { |
| 460 | int res; |
| 461 | |
| 462 | res = -ENOSYS; |
| 463 | if(f->op.truncate) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 464 | res = f->op.truncate(path, attr->size); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 465 | |
| 466 | return res; |
| 467 | } |
| 468 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 469 | 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] | 470 | { |
| 471 | int res; |
| 472 | struct utimbuf buf; |
| 473 | buf.actime = attr->atime; |
| 474 | buf.modtime = attr->mtime; |
| 475 | res = -ENOSYS; |
| 476 | if(f->op.utime) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 477 | res = f->op.utime(path, &buf); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 478 | |
| 479 | return res; |
| 480 | } |
| 481 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 482 | static void do_setattr(struct fuse *f, struct fuse_in_header *in, |
| 483 | struct fuse_setattr_in *arg) |
| 484 | { |
| 485 | int res; |
| 486 | char *path; |
| 487 | int valid = arg->valid; |
| 488 | struct fuse_attr *attr = &arg->attr; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 489 | struct fuse_setattr_out outarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 490 | |
| 491 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 492 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 493 | if(path != NULL) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 494 | res = -ENOSYS; |
| 495 | if(f->op.getattr) { |
| 496 | res = 0; |
| 497 | if(!res && (valid & FATTR_MODE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 498 | res = do_chmod(f, path, attr); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 499 | if(!res && (valid & (FATTR_UID | FATTR_GID))) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 500 | res = do_chown(f, path, attr, valid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 501 | if(!res && (valid & FATTR_SIZE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 502 | res = do_truncate(f, path, attr); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 503 | if(!res && (valid & FATTR_UTIME)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 504 | res = do_utime(f, path, attr); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 505 | if(!res) { |
| 506 | struct stat buf; |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 507 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 508 | if(!res) |
| 509 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 510 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 511 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 512 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 513 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 514 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 515 | } |
| 516 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 517 | static void do_readlink(struct fuse *f, struct fuse_in_header *in) |
| 518 | { |
| 519 | int res; |
| 520 | char link[PATH_MAX + 1]; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 521 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 522 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 523 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 524 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 525 | if(path != NULL) { |
| 526 | res = -ENOSYS; |
| 527 | if(f->op.readlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 528 | res = f->op.readlink(path, link, sizeof(link)); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 529 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 530 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 531 | link[PATH_MAX] = '\0'; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 532 | send_reply(f, in, res, link, !res ? strlen(link) : 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | static void do_getdir(struct fuse *f, struct fuse_in_header *in) |
| 536 | { |
| 537 | int res; |
| 538 | struct fuse_getdir_out arg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 539 | struct fuse_dirhandle dh; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 540 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 541 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 542 | dh.fuse = f; |
| 543 | dh.fp = tmpfile(); |
| 544 | dh.dir = in->ino; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 545 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 546 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 547 | if(path != NULL) { |
| 548 | res = -ENOSYS; |
| 549 | if(f->op.getdir) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 550 | res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 551 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 552 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 553 | fflush(dh.fp); |
| 554 | arg.fd = fileno(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 555 | send_reply(f, in, res, &arg, sizeof(arg)); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 556 | fclose(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 557 | } |
| 558 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 559 | static void do_mknod(struct fuse *f, struct fuse_in_header *in, |
| 560 | struct fuse_mknod_in *inarg) |
| 561 | { |
| 562 | int res; |
| 563 | char *path; |
| 564 | struct fuse_mknod_out outarg; |
| 565 | struct stat buf; |
| 566 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 567 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 568 | path = get_path_name(f, in->ino, inarg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 569 | if(path != NULL) { |
| 570 | res = -ENOSYS; |
| 571 | if(f->op.mknod && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 572 | res = f->op.mknod(path, inarg->mode, inarg->rdev); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 573 | if(res == 0) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 574 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 575 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 576 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 577 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 578 | if(res == 0) { |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 579 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 580 | outarg.ino = find_node(f, in->ino, inarg->name, &outarg.attr, |
| 581 | in->unique); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 582 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 583 | |
| 584 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 585 | } |
| 586 | |
| 587 | static void do_mkdir(struct fuse *f, struct fuse_in_header *in, |
| 588 | struct fuse_mkdir_in *inarg) |
| 589 | { |
| 590 | int res; |
| 591 | char *path; |
| 592 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 593 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 594 | path = get_path_name(f, in->ino, inarg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 595 | if(path != NULL) { |
| 596 | res = -ENOSYS; |
| 597 | if(f->op.mkdir) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 598 | res = f->op.mkdir(path, inarg->mode); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 599 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 600 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 601 | send_reply(f, in, res, NULL, 0); |
| 602 | } |
| 603 | |
| 604 | static void do_remove(struct fuse *f, struct fuse_in_header *in, char *name) |
| 605 | { |
| 606 | int res; |
| 607 | char *path; |
| 608 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 609 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 610 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 611 | if(path != NULL) { |
| 612 | res = -ENOSYS; |
| 613 | if(in->opcode == FUSE_UNLINK) { |
| 614 | if(f->op.unlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 615 | res = f->op.unlink(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 616 | } |
| 617 | else { |
| 618 | if(f->op.rmdir) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 619 | res = f->op.rmdir(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 620 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 621 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 622 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 623 | if(res == 0) |
| 624 | remove_node(f, in->ino, name); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 625 | send_reply(f, in, res, NULL, 0); |
| 626 | } |
| 627 | |
| 628 | static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name, |
| 629 | char *link) |
| 630 | { |
| 631 | int res; |
| 632 | char *path; |
| 633 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 634 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 635 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 636 | if(path != NULL) { |
| 637 | res = -ENOSYS; |
| 638 | if(f->op.symlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 639 | res = f->op.symlink(link, path); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 640 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 641 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 642 | send_reply(f, in, res, NULL, 0); |
| 643 | } |
| 644 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 645 | static void do_rename(struct fuse *f, struct fuse_in_header *in, |
| 646 | struct fuse_rename_in *inarg) |
| 647 | { |
| 648 | int res; |
| 649 | fino_t olddir = in->ino; |
| 650 | fino_t newdir = inarg->newdir; |
| 651 | char *oldname = inarg->names; |
| 652 | char *newname = inarg->names + strlen(oldname) + 1; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 653 | char *oldpath; |
| 654 | char *newpath; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 655 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 656 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 657 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 658 | if(oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 659 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 660 | if(newpath != NULL) { |
| 661 | res = -ENOSYS; |
| 662 | if(f->op.rename) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 663 | res = f->op.rename(oldpath, newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 664 | if(res == 0) |
| 665 | rename_node(f, olddir, oldname, newdir, newname); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 666 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 667 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 668 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 669 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 670 | send_reply(f, in, res, NULL, 0); |
| 671 | } |
| 672 | |
| 673 | static void do_link(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 674 | struct fuse_link_in *arg) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 675 | { |
| 676 | int res; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 677 | char *oldpath; |
| 678 | char *newpath; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 679 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 680 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 681 | oldpath = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 682 | if(oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 683 | newpath = get_path_name(f, arg->newdir, arg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 684 | if(newpath != NULL) { |
| 685 | res = -ENOSYS; |
| 686 | if(f->op.link) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 687 | res = f->op.link(oldpath, newpath); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 688 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 689 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 690 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 691 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 692 | send_reply(f, in, res, NULL, 0); |
| 693 | } |
| 694 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 695 | static void do_open(struct fuse *f, struct fuse_in_header *in, |
| 696 | struct fuse_open_in *arg) |
| 697 | { |
| 698 | int res; |
| 699 | char *path; |
| 700 | |
| 701 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 702 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 703 | if(path != NULL) { |
| 704 | res = -ENOSYS; |
| 705 | if(f->op.open) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 706 | res = f->op.open(path, arg->flags); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 707 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 708 | } |
| 709 | send_reply(f, in, res, NULL, 0); |
| 710 | } |
| 711 | |
| 712 | static void do_read(struct fuse *f, struct fuse_in_header *in, |
| 713 | struct fuse_read_in *arg) |
| 714 | { |
| 715 | int res; |
| 716 | char *path; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 717 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size); |
| 718 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 719 | char *buf = outbuf + sizeof(struct fuse_out_header); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 720 | size_t size; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 721 | size_t outsize; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 722 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 723 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 724 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 725 | if(path != NULL) { |
| 726 | res = -ENOSYS; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 727 | if(f->op.read) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 728 | res = f->op.read(path, buf, arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 729 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 730 | } |
| 731 | |
| 732 | size = 0; |
| 733 | if(res > 0) { |
| 734 | size = res; |
| 735 | res = 0; |
| 736 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 737 | out->unique = in->unique; |
| 738 | out->error = res; |
| 739 | outsize = sizeof(struct fuse_out_header) + size; |
| 740 | |
| 741 | send_reply_raw(f, outbuf, outsize); |
| 742 | free(outbuf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 743 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 744 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 745 | static void do_write(struct fuse *f, struct fuse_in_header *in, |
| 746 | struct fuse_write_in *arg) |
| 747 | { |
| 748 | int res; |
| 749 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 750 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 751 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 752 | path = get_path(f, in->ino); |
| 753 | if(path != NULL) { |
| 754 | res = -ENOSYS; |
| 755 | if(f->op.write) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 756 | res = f->op.write(path, arg->buf, arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 757 | free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 758 | } |
| 759 | |
| 760 | if(res > 0) { |
| 761 | if((size_t) res != arg->size) { |
| 762 | fprintf(stderr, "short write: %u (should be %u)\n", res, |
| 763 | arg->size); |
| 764 | res = -EIO; |
| 765 | } |
| 766 | else |
| 767 | res = 0; |
| 768 | } |
| 769 | |
| 770 | send_reply(f, in, res, NULL, 0); |
| 771 | } |
| 772 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 773 | static void free_cmd(struct fuse_cmd *cmd) |
| 774 | { |
| 775 | free(cmd->buf); |
| 776 | free(cmd); |
| 777 | } |
| 778 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 779 | void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 780 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 781 | struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf; |
| 782 | void *inarg = cmd->buf + sizeof(struct fuse_in_header); |
| 783 | size_t argsize; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 784 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 785 | dec_avail(f); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 786 | |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 787 | if((f->flags & FUSE_DEBUG)) { |
| 788 | printf("unique: %i, opcode: %i, ino: %li, insize: %i\n", in->unique, |
| 789 | in->opcode, in->ino, cmd->buflen); |
| 790 | fflush(stdout); |
| 791 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 792 | |
| 793 | argsize = cmd->buflen - sizeof(struct fuse_in_header); |
| 794 | |
| 795 | switch(in->opcode) { |
| 796 | case FUSE_LOOKUP: |
| 797 | do_lookup(f, in, (char *) inarg); |
| 798 | break; |
| 799 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 800 | case FUSE_GETATTR: |
| 801 | do_getattr(f, in); |
| 802 | break; |
| 803 | |
| 804 | case FUSE_SETATTR: |
| 805 | do_setattr(f, in, (struct fuse_setattr_in *) inarg); |
| 806 | break; |
| 807 | |
| 808 | case FUSE_READLINK: |
| 809 | do_readlink(f, in); |
| 810 | break; |
| 811 | |
| 812 | case FUSE_GETDIR: |
| 813 | do_getdir(f, in); |
| 814 | break; |
| 815 | |
| 816 | case FUSE_MKNOD: |
| 817 | do_mknod(f, in, (struct fuse_mknod_in *) inarg); |
| 818 | break; |
| 819 | |
| 820 | case FUSE_MKDIR: |
| 821 | do_mkdir(f, in, (struct fuse_mkdir_in *) inarg); |
| 822 | break; |
| 823 | |
| 824 | case FUSE_UNLINK: |
| 825 | case FUSE_RMDIR: |
| 826 | do_remove(f, in, (char *) inarg); |
| 827 | break; |
| 828 | |
| 829 | case FUSE_SYMLINK: |
| 830 | do_symlink(f, in, (char *) inarg, |
| 831 | ((char *) inarg) + strlen((char *) inarg) + 1); |
| 832 | break; |
| 833 | |
| 834 | case FUSE_RENAME: |
| 835 | do_rename(f, in, (struct fuse_rename_in *) inarg); |
| 836 | break; |
| 837 | |
| 838 | case FUSE_LINK: |
| 839 | do_link(f, in, (struct fuse_link_in *) inarg); |
| 840 | break; |
| 841 | |
| 842 | case FUSE_OPEN: |
| 843 | do_open(f, in, (struct fuse_open_in *) inarg); |
| 844 | break; |
| 845 | |
| 846 | case FUSE_READ: |
| 847 | do_read(f, in, (struct fuse_read_in *) inarg); |
| 848 | break; |
| 849 | |
| 850 | case FUSE_WRITE: |
| 851 | do_write(f, in, (struct fuse_write_in *) inarg); |
| 852 | break; |
| 853 | |
| 854 | default: |
| 855 | fprintf(stderr, "Operation %i not implemented\n", in->opcode); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 856 | send_reply(f, in, -ENOSYS, NULL, 0); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 857 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 858 | |
| 859 | free_cmd(cmd); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 862 | struct fuse_cmd *__fuse_read_cmd(struct fuse *f) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 863 | { |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 864 | ssize_t res; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 865 | struct fuse_cmd *cmd; |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 866 | struct fuse_in_header *in; |
| 867 | void *inarg; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 868 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 869 | cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd)); |
| 870 | cmd->buf = (char *) malloc(FUSE_MAX_IN); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 871 | in = (struct fuse_in_header *) cmd->buf; |
| 872 | inarg = cmd->buf + sizeof(struct fuse_in_header); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 873 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 874 | do { |
| 875 | res = read(f->fd, cmd->buf, FUSE_MAX_IN); |
| 876 | if(res == -1) { |
| 877 | /* ENODEV means we got unmounted, so we silenty return failure */ |
| 878 | if(errno != ENODEV) { |
| 879 | perror("fuse: reading device"); |
| 880 | /* BAD... This will happen again */ |
| 881 | } |
| 882 | free_cmd(cmd); |
| 883 | return NULL; |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 884 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 885 | if((size_t) res < sizeof(struct fuse_in_header)) { |
| 886 | fprintf(stderr, "short read on fuse device\n"); |
| 887 | /* Cannot happen */ |
| 888 | free_cmd(cmd); |
| 889 | return NULL; |
| 890 | } |
| 891 | cmd->buflen = res; |
| 892 | |
| 893 | /* Forget is special, it can be done without messing with threads. */ |
| 894 | if(in->opcode == FUSE_FORGET) |
| 895 | do_forget(f, in, (struct fuse_forget_in *) inarg); |
| 896 | |
| 897 | } while(in->opcode == FUSE_FORGET); |
| 898 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 899 | return cmd; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 902 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 903 | void fuse_loop(struct fuse *f) |
| 904 | { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 905 | while(1) { |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 906 | struct fuse_cmd *cmd = __fuse_read_cmd(f); |
| 907 | if(cmd == NULL) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 908 | exit(1); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 909 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 910 | __fuse_process_cmd(f, cmd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 911 | } |
| 912 | } |
| 913 | |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame^] | 914 | struct fuse_context *fuse_get_context(struct fuse *f) |
| 915 | { |
| 916 | if(f->getcontext) |
| 917 | return f->getcontext(f); |
| 918 | else |
| 919 | return &f->context; |
| 920 | } |
| 921 | |
| 922 | struct fuse *fuse_new(int fd, int flags, const struct fuse_operations *op) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 923 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 924 | struct fuse *f; |
| 925 | struct node *root; |
Miklos Szeredi | fe42812 | 2001-11-20 19:12:28 +0000 | [diff] [blame] | 926 | char verstr[128]; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 927 | char *realver = getenv(FUSE_KERNEL_VERSION_ENV); |
Miklos Szeredi | fe42812 | 2001-11-20 19:12:28 +0000 | [diff] [blame] | 928 | |
| 929 | if(realver != NULL) { |
| 930 | sprintf(verstr, "%i", FUSE_KERNEL_VERSION); |
| 931 | if(strcmp(verstr, realver) != 0) { |
| 932 | fprintf(stderr, |
| 933 | "Warning: FUSE version mismatch: using %s, kernel is %s\n", |
| 934 | realver, verstr); |
| 935 | } |
| 936 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 937 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 938 | f = (struct fuse *) calloc(1, sizeof(struct fuse)); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 939 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 940 | f->flags = flags; |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 941 | f->fd = fd; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 942 | f->ctr = 0; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 943 | /* FIXME: Dynamic hash table */ |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 944 | f->name_table_size = 14057; |
| 945 | f->name_table = (struct node **) |
| 946 | calloc(1, sizeof(struct node *) * f->name_table_size); |
| 947 | f->ino_table_size = 14057; |
| 948 | f->ino_table = (struct node **) |
| 949 | calloc(1, sizeof(struct node *) * f->ino_table_size); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 950 | pthread_mutex_init(&f->lock, NULL); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 951 | f->numworker = 0; |
| 952 | f->numavail = 0; |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame^] | 953 | f->op = *op; |
| 954 | f->getcontext = NULL; |
| 955 | f->context.uid = 0; |
| 956 | f->context.gid = 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 957 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 958 | root = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 959 | root->mode = 0; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 960 | root->rdev = 0; |
| 961 | root->name = strdup("/"); |
| 962 | root->parent = 0; |
| 963 | hash_ino(f, root, FUSE_ROOT_INO); |
| 964 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 965 | return f; |
| 966 | } |
| 967 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 968 | void fuse_destroy(struct fuse *f) |
| 969 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 970 | size_t i; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 971 | for(i = 0; i < f->ino_table_size; i++) { |
| 972 | struct node *node; |
| 973 | struct node *next; |
| 974 | for(node = f->ino_table[i]; node != NULL; node = next) { |
| 975 | next = node->ino_next; |
| 976 | free_node(node); |
| 977 | } |
| 978 | } |
| 979 | free(f->ino_table); |
| 980 | free(f->name_table); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 981 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 982 | free(f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 983 | } |