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