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