Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 2e6b6f2 | 2004-07-07 19:19:53 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2004 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 4 | |
Miklos Szeredi | 8b39a9f | 2002-10-25 12:41:16 +0000 | [diff] [blame] | 5 | This program can be distributed under the terms of the GNU LGPL. |
| 6 | See the file COPYING.LIB |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Miklos Szeredi | cb26451 | 2004-06-23 18:52:50 +0000 | [diff] [blame] | 9 | #include <config.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 10 | #include "fuse_i.h" |
| 11 | #include <linux/fuse.h> |
| 12 | |
| 13 | #include <string.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 16 | #include <limits.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 17 | #include <errno.h> |
Miklos Szeredi | 019b4e9 | 2001-12-26 18:08:09 +0000 | [diff] [blame] | 18 | #include <sys/param.h> |
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 | #define FUSE_MAX_PATH 4096 |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 21 | #define PARAM(inarg) (((char *)(inarg)) + sizeof(*inarg)) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 22 | |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 23 | #define ENTRY_REVALIDATE_TIME 1 /* sec */ |
| 24 | #define ATTR_REVALIDATE_TIME 1 /* sec */ |
| 25 | |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 26 | static const char *opname(enum fuse_opcode opcode) |
| 27 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 28 | switch (opcode) { |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 29 | case FUSE_LOOKUP: return "LOOKUP"; |
| 30 | case FUSE_FORGET: return "FORGET"; |
| 31 | case FUSE_GETATTR: return "GETATTR"; |
| 32 | case FUSE_SETATTR: return "SETATTR"; |
| 33 | case FUSE_READLINK: return "READLINK"; |
| 34 | case FUSE_SYMLINK: return "SYMLINK"; |
| 35 | case FUSE_GETDIR: return "GETDIR"; |
| 36 | case FUSE_MKNOD: return "MKNOD"; |
| 37 | case FUSE_MKDIR: return "MKDIR"; |
| 38 | case FUSE_UNLINK: return "UNLINK"; |
| 39 | case FUSE_RMDIR: return "RMDIR"; |
| 40 | case FUSE_RENAME: return "RENAME"; |
| 41 | case FUSE_LINK: return "LINK"; |
| 42 | case FUSE_OPEN: return "OPEN"; |
| 43 | case FUSE_READ: return "READ"; |
| 44 | case FUSE_WRITE: return "WRITE"; |
| 45 | case FUSE_STATFS: return "STATFS"; |
Miklos Szeredi | 99f2074 | 2004-05-19 08:01:10 +0000 | [diff] [blame] | 46 | case FUSE_FLUSH: return "FLUSH"; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 47 | case FUSE_RELEASE: return "RELEASE"; |
| 48 | case FUSE_FSYNC: return "FSYNC"; |
| 49 | case FUSE_SETXATTR: return "SETXATTR"; |
| 50 | case FUSE_GETXATTR: return "GETXATTR"; |
| 51 | case FUSE_LISTXATTR: return "LISTXATTR"; |
| 52 | case FUSE_REMOVEXATTR: return "REMOVEXATTR"; |
Miklos Szeredi | 99f2074 | 2004-05-19 08:01:10 +0000 | [diff] [blame] | 53 | default: return "???"; |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 57 | |
| 58 | static inline void dec_avail(struct fuse *f) |
| 59 | { |
| 60 | pthread_mutex_lock(&f->lock); |
| 61 | f->numavail --; |
| 62 | pthread_mutex_unlock(&f->lock); |
| 63 | } |
| 64 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 65 | static struct node *__get_node(struct fuse *f, fino_t ino) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 66 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 67 | size_t hash = ino % f->ino_table_size; |
| 68 | struct node *node; |
| 69 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 70 | for (node = f->ino_table[hash]; node != NULL; node = node->ino_next) |
| 71 | if (node->ino == ino) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 72 | return node; |
| 73 | |
| 74 | return NULL; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 75 | } |
| 76 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 77 | static struct node *get_node(struct fuse *f, fino_t ino) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 78 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 79 | struct node *node = __get_node(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 80 | if (node != NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 81 | return node; |
| 82 | |
| 83 | fprintf(stderr, "fuse internal error: inode %lu not found\n", ino); |
| 84 | abort(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 87 | static void hash_ino(struct fuse *f, struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 88 | { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 89 | size_t hash = node->ino % f->ino_table_size; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 90 | node->ino_next = f->ino_table[hash]; |
| 91 | f->ino_table[hash] = node; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 94 | static void unhash_ino(struct fuse *f, struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 95 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 96 | size_t hash = node->ino % f->ino_table_size; |
| 97 | struct node **nodep = &f->ino_table[hash]; |
| 98 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 99 | for (; *nodep != NULL; nodep = &(*nodep)->ino_next) |
| 100 | if (*nodep == node) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 101 | *nodep = node->ino_next; |
| 102 | return; |
| 103 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 106 | static fino_t next_ino(struct fuse *f) |
| 107 | { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 108 | do { |
| 109 | f->ctr++; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 110 | if (!f->ctr) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 111 | f->generation ++; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 112 | } while (f->ctr == 0 || __get_node(f, f->ctr) != NULL); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 113 | return f->ctr; |
| 114 | } |
| 115 | |
| 116 | static void free_node(struct node *node) |
| 117 | { |
| 118 | free(node->name); |
| 119 | free(node); |
| 120 | } |
| 121 | |
| 122 | static unsigned int name_hash(struct fuse *f, fino_t parent, const char *name) |
| 123 | { |
| 124 | unsigned int hash = *name; |
| 125 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 126 | if (hash) |
| 127 | for (name += 1; *name != '\0'; name++) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 128 | hash = (hash << 5) - hash + *name; |
| 129 | |
| 130 | return (hash + parent) % f->name_table_size; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 133 | static struct node *__lookup_node(struct fuse *f, fino_t parent, |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 134 | const char *name) |
| 135 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 136 | size_t hash = name_hash(f, parent, name); |
| 137 | struct node *node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 138 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 139 | for (node = f->name_table[hash]; node != NULL; node = node->name_next) |
| 140 | if (node->parent == parent && strcmp(node->name, name) == 0) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 141 | return node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 142 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 143 | return NULL; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 146 | static struct node *lookup_node(struct fuse *f, fino_t parent, |
| 147 | const char *name) |
| 148 | { |
| 149 | struct node *node; |
| 150 | |
| 151 | pthread_mutex_lock(&f->lock); |
| 152 | node = __lookup_node(f, parent, name); |
| 153 | pthread_mutex_unlock(&f->lock); |
| 154 | if (node != NULL) |
| 155 | return node; |
| 156 | |
| 157 | fprintf(stderr, "fuse internal error: node %lu/%s not found\n", parent, |
| 158 | name); |
| 159 | abort(); |
| 160 | } |
| 161 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 162 | 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] | 163 | const char *name) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 164 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 165 | size_t hash = name_hash(f, parent, name); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 166 | node->parent = parent; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 167 | node->name = strdup(name); |
| 168 | node->name_next = f->name_table[hash]; |
| 169 | f->name_table[hash] = node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 170 | } |
| 171 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 172 | static void unhash_name(struct fuse *f, struct node *node) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 173 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 174 | if (node->name != NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 175 | size_t hash = name_hash(f, node->parent, node->name); |
| 176 | struct node **nodep = &f->name_table[hash]; |
| 177 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 178 | for (; *nodep != NULL; nodep = &(*nodep)->name_next) |
| 179 | if (*nodep == node) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 180 | *nodep = node->name_next; |
| 181 | node->name_next = NULL; |
| 182 | free(node->name); |
| 183 | node->name = NULL; |
| 184 | node->parent = 0; |
| 185 | return; |
| 186 | } |
| 187 | fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n", |
| 188 | node->ino); |
| 189 | abort(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 193 | static struct node *find_node(struct fuse *f, fino_t parent, char *name, |
| 194 | struct fuse_attr *attr, int version) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 195 | { |
| 196 | struct node *node; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 197 | int mode = attr->mode & S_IFMT; |
| 198 | int rdev = 0; |
| 199 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 200 | if (S_ISCHR(mode) || S_ISBLK(mode)) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 201 | rdev = attr->rdev; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 202 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 203 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 204 | node = __lookup_node(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 205 | if (node != NULL) { |
| 206 | if (node->mode == mode && node->rdev == rdev) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 207 | goto out; |
| 208 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 209 | unhash_name(f, node); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 210 | } |
| 211 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 212 | node = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 213 | node->mode = mode; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 214 | node->rdev = rdev; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 215 | node->open_count = 0; |
| 216 | node->is_hidden = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 217 | node->ino = next_ino(f); |
| 218 | node->generation = f->generation; |
| 219 | hash_ino(f, node); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 220 | hash_name(f, node, parent, name); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 221 | |
| 222 | out: |
| 223 | node->version = version; |
| 224 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 225 | return node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 228 | static int path_lookup(struct fuse *f, const char *path, fino_t *inop) |
| 229 | { |
| 230 | fino_t ino; |
| 231 | int err; |
| 232 | char *s; |
| 233 | char *name; |
| 234 | char *tmp = strdup(path); |
| 235 | if (!tmp) |
| 236 | return -ENOMEM; |
| 237 | |
| 238 | pthread_mutex_lock(&f->lock); |
| 239 | ino = FUSE_ROOT_INO; |
| 240 | err = 0; |
| 241 | for (s = tmp; (name = strsep(&s, "/")) != NULL; ) { |
| 242 | if (name[0]) { |
| 243 | struct node *node = __lookup_node(f, ino, name); |
| 244 | if (node == NULL) { |
| 245 | err = -ENOENT; |
| 246 | break; |
| 247 | } |
| 248 | ino = node->ino; |
| 249 | } |
| 250 | } |
| 251 | pthread_mutex_unlock(&f->lock); |
| 252 | free(tmp); |
| 253 | if (!err) |
| 254 | *inop = ino; |
| 255 | |
| 256 | return err; |
| 257 | } |
| 258 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 259 | static char *add_name(char *buf, char *s, const char *name) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 260 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 261 | size_t len = strlen(name); |
| 262 | s -= len; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 263 | if (s <= buf) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 264 | fprintf(stderr, "fuse: path too long: ...%s\n", s + len); |
| 265 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 266 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 267 | strncpy(s, name, len); |
| 268 | s--; |
| 269 | *s = '/'; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 270 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 271 | return s; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 274 | 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] | 275 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 276 | char buf[FUSE_MAX_PATH]; |
| 277 | char *s = buf + FUSE_MAX_PATH - 1; |
| 278 | struct node *node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 279 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 280 | *s = '\0'; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 281 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 282 | if (name != NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 283 | s = add_name(buf, s, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 284 | if (s == NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 289 | for (node = get_node(f, ino); node->ino != FUSE_ROOT_INO; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 290 | node = get_node(f, node->parent)) { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 291 | if (node->name == NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 292 | s = NULL; |
| 293 | break; |
| 294 | } |
| 295 | |
| 296 | s = add_name(buf, s, node->name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 297 | if (s == NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 298 | break; |
| 299 | } |
| 300 | pthread_mutex_unlock(&f->lock); |
| 301 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 302 | if (s == NULL) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 303 | return NULL; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 304 | else if (*s == '\0') |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 305 | return strdup("/"); |
| 306 | else |
| 307 | return strdup(s); |
| 308 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 309 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 310 | static char *get_path(struct fuse *f, fino_t ino) |
| 311 | { |
| 312 | return get_path_name(f, ino, NULL); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 313 | } |
| 314 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 315 | static void destroy_node(struct fuse *f, fino_t ino, int version) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 316 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 317 | struct node *node; |
| 318 | |
| 319 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 8b2d333 | 2004-09-09 08:44:01 +0000 | [diff] [blame] | 320 | node = __get_node(f, ino); |
| 321 | if (node && node->version == version && ino != FUSE_ROOT_INO) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 322 | unhash_name(f, node); |
| 323 | unhash_ino(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 324 | free_node(node); |
| 325 | } |
| 326 | pthread_mutex_unlock(&f->lock); |
| 327 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 328 | } |
| 329 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 330 | static void remove_node(struct fuse *f, fino_t dir, const char *name) |
| 331 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 332 | struct node *node; |
| 333 | |
| 334 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 335 | node = __lookup_node(f, dir, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 336 | if (node == NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 337 | fprintf(stderr, "fuse internal error: unable to remove node %lu/%s\n", |
| 338 | dir, name); |
| 339 | abort(); |
| 340 | } |
| 341 | unhash_name(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 342 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 345 | static int rename_node(struct fuse *f, fino_t olddir, const char *oldname, |
| 346 | fino_t newdir, const char *newname, int hide) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 347 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 348 | struct node *node; |
| 349 | struct node *newnode; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 350 | int err = 0; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 351 | |
| 352 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 353 | node = __lookup_node(f, olddir, oldname); |
| 354 | newnode = __lookup_node(f, newdir, newname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 355 | if (node == NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 356 | fprintf(stderr, "fuse internal error: unable to rename node %lu/%s\n", |
| 357 | olddir, oldname); |
| 358 | abort(); |
| 359 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 360 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 361 | if (newnode != NULL) { |
| 362 | if (hide) { |
| 363 | fprintf(stderr, "fuse: hidden file got created during hiding\n"); |
| 364 | err = -1; |
| 365 | goto out; |
| 366 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 367 | unhash_name(f, newnode); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 368 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 369 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 370 | unhash_name(f, node); |
| 371 | hash_name(f, node, newdir, newname); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 372 | if (hide) |
| 373 | node->is_hidden = 1; |
| 374 | |
| 375 | out: |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 376 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 377 | return err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 380 | static void convert_stat(struct stat *stbuf, struct fuse_attr *attr) |
| 381 | { |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 382 | attr->mode = stbuf->st_mode; |
| 383 | attr->nlink = stbuf->st_nlink; |
| 384 | attr->uid = stbuf->st_uid; |
| 385 | attr->gid = stbuf->st_gid; |
| 386 | attr->rdev = stbuf->st_rdev; |
| 387 | attr->size = stbuf->st_size; |
| 388 | attr->blocks = stbuf->st_blocks; |
| 389 | attr->atime = stbuf->st_atime; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 390 | attr->mtime = stbuf->st_mtime; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 391 | attr->ctime = stbuf->st_ctime; |
Miklos Szeredi | cb26451 | 2004-06-23 18:52:50 +0000 | [diff] [blame] | 392 | #ifdef HAVE_STRUCT_STAT_ST_ATIM |
| 393 | attr->atimensec = stbuf->st_atim.tv_nsec; |
| 394 | attr->mtimensec = stbuf->st_mtim.tv_nsec; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 395 | attr->ctimensec = stbuf->st_ctim.tv_nsec; |
Miklos Szeredi | cb26451 | 2004-06-23 18:52:50 +0000 | [diff] [blame] | 396 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 399 | static int fill_dir(struct fuse_dirhandle *dh, char *name, int type) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 400 | { |
| 401 | struct fuse_dirent dirent; |
| 402 | size_t reclen; |
| 403 | size_t res; |
| 404 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 405 | dirent.ino = (unsigned long) -1; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 406 | dirent.namelen = strlen(name); |
| 407 | strncpy(dirent.name, name, sizeof(dirent.name)); |
| 408 | dirent.type = type; |
| 409 | reclen = FUSE_DIRENT_SIZE(&dirent); |
| 410 | res = fwrite(&dirent, reclen, 1, dh->fp); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 411 | if (res == 0) { |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 412 | perror("fuse: writing directory file"); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 413 | return -EIO; |
| 414 | } |
| 415 | return 0; |
| 416 | } |
| 417 | |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 418 | static int send_reply_raw(struct fuse *f, char *outbuf, size_t outsize, |
| 419 | int locked) |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 420 | { |
| 421 | int res; |
| 422 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 423 | if ((f->flags & FUSE_DEBUG)) { |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 424 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 425 | printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique, |
| 426 | out->error, strerror(-out->error), outsize); |
| 427 | fflush(stdout); |
| 428 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 429 | |
Miklos Szeredi | 4e71c9f | 2004-02-09 12:05:14 +0000 | [diff] [blame] | 430 | /* This needs to be done before the reply, otherwise the scheduler |
| 431 | could play tricks with us, and only let the counter be increased |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 432 | long after the operation is done */ |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 433 | if (!locked) |
| 434 | pthread_mutex_lock(&f->lock); |
| 435 | f->numavail ++; |
| 436 | if (!locked) |
| 437 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 438 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 439 | res = write(f->fd, outbuf, outsize); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 440 | if (res == -1) { |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 441 | /* ENOENT means the operation was interrupted */ |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 442 | if (!f->exited && errno != ENOENT) |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 443 | perror("fuse: writing device"); |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 444 | return -errno; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 445 | } |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 446 | return 0; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 449 | static int __send_reply(struct fuse *f, struct fuse_in_header *in, int error, |
| 450 | void *arg, size_t argsize, int locked) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 451 | { |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 452 | int res; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 453 | char *outbuf; |
| 454 | size_t outsize; |
| 455 | struct fuse_out_header *out; |
| 456 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 457 | if (error <= -1000 || error > 0) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 458 | fprintf(stderr, "fuse: bad error value: %i\n", error); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 459 | error = -ERANGE; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 462 | if (error) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 463 | argsize = 0; |
| 464 | |
| 465 | outsize = sizeof(struct fuse_out_header) + argsize; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 466 | outbuf = (char *) malloc(outsize); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 467 | out = (struct fuse_out_header *) outbuf; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 468 | memset(out, 0, sizeof(struct fuse_out_header)); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 469 | out->unique = in->unique; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 470 | out->error = error; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 471 | if (argsize != 0) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 472 | memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize); |
| 473 | |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 474 | res = send_reply_raw(f, outbuf, outsize, locked); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 475 | free(outbuf); |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 476 | |
| 477 | return res; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 478 | } |
| 479 | |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 480 | static int send_reply(struct fuse *f, struct fuse_in_header *in, int error, |
| 481 | void *arg, size_t argsize) |
| 482 | { |
| 483 | return __send_reply(f, in, error, arg, argsize, 0); |
| 484 | } |
| 485 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 486 | static int is_open(struct fuse *f, fino_t dir, const char *name) |
| 487 | { |
| 488 | struct node *node; |
| 489 | int isopen = 0; |
| 490 | pthread_mutex_lock(&f->lock); |
| 491 | node = __lookup_node(f, dir, name); |
| 492 | if (node && node->open_count > 0) |
| 493 | isopen = 1; |
| 494 | pthread_mutex_unlock(&f->lock); |
| 495 | return isopen; |
| 496 | } |
| 497 | |
| 498 | static char *hidden_name(struct fuse *f, fino_t dir, const char *oldname, |
| 499 | char *newname, size_t bufsize) |
| 500 | { |
| 501 | struct stat buf; |
| 502 | struct node *node; |
| 503 | struct node *newnode; |
| 504 | char *newpath; |
| 505 | int res; |
| 506 | int failctr = 10; |
| 507 | |
| 508 | if (!f->op.getattr) |
| 509 | return NULL; |
| 510 | |
| 511 | do { |
| 512 | node = lookup_node(f, dir, oldname); |
| 513 | pthread_mutex_lock(&f->lock); |
| 514 | do { |
| 515 | f->hidectr ++; |
| 516 | snprintf(newname, bufsize, ".fuse_hidden%08x%08x", |
| 517 | (unsigned int) node->ino, f->hidectr); |
| 518 | newnode = __lookup_node(f, dir, newname); |
| 519 | } while(newnode); |
| 520 | pthread_mutex_unlock(&f->lock); |
| 521 | |
| 522 | newpath = get_path_name(f, dir, newname); |
| 523 | if (!newpath) |
| 524 | break; |
| 525 | |
| 526 | res = f->op.getattr(newpath, &buf); |
| 527 | if (res != 0) |
| 528 | break; |
| 529 | free(newpath); |
| 530 | newpath = NULL; |
| 531 | } while(--failctr); |
| 532 | |
| 533 | return newpath; |
| 534 | } |
| 535 | |
| 536 | static int hide_node(struct fuse *f, const char *oldpath, fino_t dir, |
| 537 | const char *oldname) |
| 538 | { |
| 539 | char newname[64]; |
| 540 | char *newpath; |
| 541 | int err = -1; |
| 542 | |
| 543 | if (!f->op.rename || !f->op.unlink) |
| 544 | return -EBUSY; |
| 545 | |
| 546 | newpath = hidden_name(f, dir, oldname, newname, sizeof(newname)); |
| 547 | if (newpath) { |
| 548 | err = f->op.rename(oldpath, newpath); |
| 549 | if (!err) |
| 550 | err = rename_node(f, dir, oldname, dir, newname, 1); |
| 551 | free(newpath); |
| 552 | } |
| 553 | if (err) |
| 554 | return -EBUSY; |
| 555 | |
| 556 | return 0; |
| 557 | } |
| 558 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 559 | static int lookup_path(struct fuse *f, fino_t ino, int version, char *name, |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 560 | const char *path, struct fuse_entry_out *arg) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 561 | { |
| 562 | int res; |
| 563 | struct stat buf; |
| 564 | |
| 565 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 566 | if (res == 0) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 567 | struct node *node; |
| 568 | |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 569 | memset(arg, 0, sizeof(struct fuse_entry_out)); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 570 | convert_stat(&buf, &arg->attr); |
| 571 | node = find_node(f, ino, name, &arg->attr, version); |
| 572 | arg->ino = node->ino; |
| 573 | arg->generation = node->generation; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 574 | arg->entry_valid = ENTRY_REVALIDATE_TIME; |
| 575 | arg->entry_valid_nsec = 0; |
| 576 | arg->attr_valid = ATTR_REVALIDATE_TIME; |
| 577 | arg->attr_valid_nsec = 0; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 578 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 579 | printf(" INO: %li\n", arg->ino); |
| 580 | fflush(stdout); |
| 581 | } |
| 582 | } |
| 583 | return res; |
| 584 | } |
| 585 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 586 | static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name) |
| 587 | { |
| 588 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 589 | int res2; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 590 | char *path; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 591 | struct fuse_entry_out arg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 592 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 593 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 594 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 595 | if (path != NULL) { |
| 596 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 597 | printf("LOOKUP %s\n", path); |
| 598 | fflush(stdout); |
| 599 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 600 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 601 | if (f->op.getattr) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 602 | res = lookup_path(f, in->ino, in->unique, name, path, &arg); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 603 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 604 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 605 | res2 = send_reply(f, in, res, &arg, sizeof(arg)); |
| 606 | if (res == 0 && res2 == -ENOENT) |
| 607 | destroy_node(f, arg.ino, in->unique); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 608 | } |
| 609 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 610 | static void do_forget(struct fuse *f, struct fuse_in_header *in, |
| 611 | struct fuse_forget_in *arg) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 612 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 613 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 614 | printf("FORGET %li/%i\n", in->ino, arg->version); |
| 615 | fflush(stdout); |
| 616 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 617 | destroy_node(f, in->ino, arg->version); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | static void do_getattr(struct fuse *f, struct fuse_in_header *in) |
| 621 | { |
| 622 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 623 | char *path; |
| 624 | struct stat buf; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 625 | struct fuse_attr_out arg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 626 | |
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 | path = get_path(f, in->ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 629 | if (path != NULL) { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 630 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 631 | if (f->op.getattr) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 632 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 633 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 634 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 635 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 636 | if (res == 0) { |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 637 | memset(&arg, 0, sizeof(struct fuse_attr_out)); |
| 638 | arg.attr_valid = ATTR_REVALIDATE_TIME; |
| 639 | arg.attr_valid_nsec = 0; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 640 | convert_stat(&buf, &arg.attr); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 641 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 642 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 643 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 644 | } |
| 645 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 646 | 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] | 647 | { |
| 648 | int res; |
| 649 | |
| 650 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 651 | if (f->op.chmod) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 652 | res = f->op.chmod(path, attr->mode); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 653 | |
| 654 | return res; |
| 655 | } |
| 656 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 657 | 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] | 658 | int valid) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 659 | { |
| 660 | int res; |
| 661 | uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1; |
| 662 | gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1; |
| 663 | |
| 664 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 665 | if (f->op.chown) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 666 | res = f->op.chown(path, uid, gid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 667 | |
| 668 | return res; |
| 669 | } |
| 670 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 671 | static int do_truncate(struct fuse *f, const char *path, |
| 672 | struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 673 | { |
| 674 | int res; |
| 675 | |
| 676 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 677 | if (f->op.truncate) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 678 | res = f->op.truncate(path, attr->size); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 679 | |
| 680 | return res; |
| 681 | } |
| 682 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 683 | 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] | 684 | { |
| 685 | int res; |
| 686 | struct utimbuf buf; |
| 687 | buf.actime = attr->atime; |
| 688 | buf.modtime = attr->mtime; |
| 689 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 690 | if (f->op.utime) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 691 | res = f->op.utime(path, &buf); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 692 | |
| 693 | return res; |
| 694 | } |
| 695 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 696 | static void do_setattr(struct fuse *f, struct fuse_in_header *in, |
| 697 | struct fuse_setattr_in *arg) |
| 698 | { |
| 699 | int res; |
| 700 | char *path; |
| 701 | int valid = arg->valid; |
| 702 | struct fuse_attr *attr = &arg->attr; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 703 | struct fuse_attr_out outarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 704 | |
| 705 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 706 | path = get_path(f, in->ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 707 | if (path != NULL) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 708 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 709 | if (f->op.getattr) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 710 | res = 0; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 711 | if (!res && (valid & FATTR_MODE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 712 | res = do_chmod(f, path, attr); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 713 | if (!res && (valid & (FATTR_UID | FATTR_GID))) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 714 | res = do_chown(f, path, attr, valid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 715 | if (!res && (valid & FATTR_SIZE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 716 | res = do_truncate(f, path, attr); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 717 | if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) == |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 718 | (FATTR_ATIME | FATTR_MTIME)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 719 | res = do_utime(f, path, attr); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 720 | if (!res) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 721 | struct stat buf; |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 722 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 723 | if (!res) { |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 724 | memset(&outarg, 0, sizeof(struct fuse_attr_out)); |
| 725 | outarg.attr_valid = ATTR_REVALIDATE_TIME; |
| 726 | outarg.attr_valid_nsec = 0; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 727 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 728 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 729 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 730 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 731 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 732 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 733 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 734 | } |
| 735 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 736 | static void do_readlink(struct fuse *f, struct fuse_in_header *in) |
| 737 | { |
| 738 | int res; |
| 739 | char link[PATH_MAX + 1]; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 740 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 741 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +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 | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 744 | if (path != NULL) { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 745 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 746 | if (f->op.readlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 747 | res = f->op.readlink(path, link, sizeof(link)); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 748 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 749 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 750 | link[PATH_MAX] = '\0'; |
Miklos Szeredi | 4b2bef4 | 2002-01-09 12:23:27 +0000 | [diff] [blame] | 751 | send_reply(f, in, res, link, res == 0 ? strlen(link) : 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | static void do_getdir(struct fuse *f, struct fuse_in_header *in) |
| 755 | { |
| 756 | int res; |
| 757 | struct fuse_getdir_out arg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 758 | struct fuse_dirhandle dh; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 759 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 760 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 761 | dh.fuse = f; |
| 762 | dh.fp = tmpfile(); |
| 763 | dh.dir = in->ino; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 764 | |
Miklos Szeredi | 65afea1 | 2004-09-14 07:13:45 +0000 | [diff] [blame^] | 765 | res = -EIO; |
| 766 | if (dh.fp == NULL) |
| 767 | perror("fuse: failed to create temporary file"); |
| 768 | else { |
| 769 | res = -ENOENT; |
| 770 | path = get_path(f, in->ino); |
| 771 | if (path != NULL) { |
| 772 | res = -ENOSYS; |
| 773 | if (f->op.getdir) |
| 774 | res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir); |
| 775 | free(path); |
| 776 | } |
| 777 | fflush(dh.fp); |
| 778 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 779 | memset(&arg, 0, sizeof(struct fuse_getdir_out)); |
Miklos Szeredi | 65afea1 | 2004-09-14 07:13:45 +0000 | [diff] [blame^] | 780 | if (res == 0) |
| 781 | arg.fd = fileno(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 782 | send_reply(f, in, res, &arg, sizeof(arg)); |
Miklos Szeredi | 65afea1 | 2004-09-14 07:13:45 +0000 | [diff] [blame^] | 783 | if (dh.fp != NULL) |
| 784 | fclose(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 785 | } |
| 786 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 787 | static void do_mknod(struct fuse *f, struct fuse_in_header *in, |
| 788 | struct fuse_mknod_in *inarg) |
| 789 | { |
| 790 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 791 | int res2; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 792 | char *path; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 793 | char *name = PARAM(inarg); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 794 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 795 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 796 | res = -ENOENT; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 797 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 798 | if (path != NULL) { |
| 799 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 800 | printf("MKNOD %s\n", path); |
| 801 | fflush(stdout); |
| 802 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 803 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 804 | if (f->op.mknod && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 805 | res = f->op.mknod(path, inarg->mode, inarg->rdev); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 806 | if (res == 0) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 807 | res = lookup_path(f, in->ino, in->unique, name, path, &outarg); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 808 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 809 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 810 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 811 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 812 | if (res == 0 && res2 == -ENOENT) |
| 813 | destroy_node(f, outarg.ino, in->unique); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | static void do_mkdir(struct fuse *f, struct fuse_in_header *in, |
| 817 | struct fuse_mkdir_in *inarg) |
| 818 | { |
| 819 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 820 | int res2; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 821 | char *path; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 822 | char *name = PARAM(inarg); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 823 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 824 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 825 | res = -ENOENT; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 826 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 827 | if (path != NULL) { |
| 828 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 829 | printf("MKDIR %s\n", path); |
| 830 | fflush(stdout); |
| 831 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 832 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 833 | if (f->op.mkdir && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 834 | res = f->op.mkdir(path, inarg->mode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 835 | if (res == 0) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 836 | res = lookup_path(f, in->ino, in->unique, name, path, &outarg); |
| 837 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 838 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 839 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 840 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 841 | if (res == 0 && res2 == -ENOENT) |
| 842 | destroy_node(f, outarg.ino, in->unique); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 843 | } |
| 844 | |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 845 | static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 846 | { |
| 847 | int res; |
| 848 | char *path; |
| 849 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 850 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 851 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 852 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 853 | if (f->flags & FUSE_DEBUG) { |
| 854 | printf("UNLINK %s\n", path); |
| 855 | fflush(stdout); |
| 856 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 857 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 858 | if (f->op.unlink) { |
Miklos Szeredi | 2529ca2 | 2004-07-13 15:36:52 +0000 | [diff] [blame] | 859 | if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->ino, name)) |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 860 | res = hide_node(f, path, in->ino, name); |
| 861 | else { |
| 862 | res = f->op.unlink(path); |
| 863 | if (res == 0) |
| 864 | remove_node(f, in->ino, name); |
| 865 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 866 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 867 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 868 | } |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 869 | send_reply(f, in, res, NULL, 0); |
| 870 | } |
| 871 | |
| 872 | static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name) |
| 873 | { |
| 874 | int res; |
| 875 | char *path; |
| 876 | |
| 877 | res = -ENOENT; |
| 878 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 879 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 880 | if (f->flags & FUSE_DEBUG) { |
| 881 | printf("RMDIR %s\n", path); |
| 882 | fflush(stdout); |
| 883 | } |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 884 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 885 | if (f->op.rmdir) { |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 886 | res = f->op.rmdir(path); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 887 | if (res == 0) |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 888 | remove_node(f, in->ino, name); |
| 889 | } |
| 890 | free(path); |
| 891 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 892 | send_reply(f, in, res, NULL, 0); |
| 893 | } |
| 894 | |
| 895 | static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name, |
| 896 | char *link) |
| 897 | { |
| 898 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 899 | int res2; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 900 | char *path; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 901 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 902 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 903 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 904 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 905 | if (path != NULL) { |
| 906 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 907 | printf("SYMLINK %s\n", path); |
| 908 | fflush(stdout); |
| 909 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 910 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 911 | if (f->op.symlink && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 912 | res = f->op.symlink(link, path); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 913 | if (res == 0) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 914 | res = lookup_path(f, in->ino, in->unique, name, path, &outarg); |
| 915 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 916 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 917 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 918 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 919 | if (res == 0 && res2 == -ENOENT) |
| 920 | destroy_node(f, outarg.ino, in->unique); |
| 921 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 922 | } |
| 923 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 924 | static void do_rename(struct fuse *f, struct fuse_in_header *in, |
| 925 | struct fuse_rename_in *inarg) |
| 926 | { |
| 927 | int res; |
| 928 | fino_t olddir = in->ino; |
| 929 | fino_t newdir = inarg->newdir; |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 930 | char *oldname = PARAM(inarg); |
| 931 | char *newname = oldname + strlen(oldname) + 1; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 932 | char *oldpath; |
| 933 | char *newpath; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 934 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 935 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 936 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 937 | if (oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 938 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 939 | if (newpath != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 940 | if (f->flags & FUSE_DEBUG) { |
| 941 | printf("RENAME %s -> %s\n", oldpath, newpath); |
| 942 | fflush(stdout); |
| 943 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 944 | res = -ENOSYS; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 945 | if (f->op.rename) { |
| 946 | res = 0; |
Miklos Szeredi | 2529ca2 | 2004-07-13 15:36:52 +0000 | [diff] [blame] | 947 | if (!(f->flags & FUSE_HARD_REMOVE) && |
| 948 | is_open(f, newdir, newname)) |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 949 | res = hide_node(f, newpath, newdir, newname); |
| 950 | if (res == 0) { |
| 951 | res = f->op.rename(oldpath, newpath); |
| 952 | if (res == 0) |
| 953 | rename_node(f, olddir, oldname, newdir, newname, 0); |
| 954 | } |
| 955 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 956 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 957 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 958 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 959 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 960 | send_reply(f, in, res, NULL, 0); |
| 961 | } |
| 962 | |
| 963 | static void do_link(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 964 | struct fuse_link_in *arg) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 965 | { |
| 966 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 967 | int res2; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 968 | char *oldpath; |
| 969 | char *newpath; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 970 | char *name = PARAM(arg); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 971 | struct fuse_entry_out outarg; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 972 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 973 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 974 | oldpath = get_path(f, in->ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 975 | if (oldpath != NULL) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 976 | newpath = get_path_name(f, arg->newdir, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 977 | if (newpath != NULL) { |
| 978 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 979 | printf("LINK %s\n", newpath); |
| 980 | fflush(stdout); |
| 981 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 982 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 983 | if (f->op.link && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 984 | res = f->op.link(oldpath, newpath); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 985 | if (res == 0) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 986 | res = lookup_path(f, arg->newdir, in->unique, name, |
| 987 | newpath, &outarg); |
| 988 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 989 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 990 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 991 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 992 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 993 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 994 | if (res == 0 && res2 == -ENOENT) |
| 995 | destroy_node(f, outarg.ino, in->unique); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 998 | static void do_open(struct fuse *f, struct fuse_in_header *in, |
| 999 | struct fuse_open_in *arg) |
| 1000 | { |
| 1001 | int res; |
| 1002 | char *path; |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1003 | struct fuse_open_out outarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1004 | |
| 1005 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1006 | path = get_path(f, in->ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1007 | if (path != NULL) { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1008 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1009 | if (f->op.open) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 1010 | res = f->op.open(path, arg->flags); |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 1011 | } |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1012 | if (res == 0) { |
| 1013 | int res2; |
| 1014 | |
| 1015 | /* If the request is interrupted the lock must be held until |
| 1016 | the cancellation is finished. Otherwise there could be |
| 1017 | races with rename/unlink, against which the kernel can't |
| 1018 | protect */ |
| 1019 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1020 | f->fh_ctr ++; |
| 1021 | outarg.fh = f->fh_ctr; |
| 1022 | if (f->flags & FUSE_DEBUG) { |
| 1023 | printf("OPEN[%u] flags: 0x%x\n", outarg.fh, arg->flags); |
| 1024 | fflush(stdout); |
| 1025 | } |
| 1026 | |
| 1027 | res2 = __send_reply(f, in, res, &outarg, sizeof(outarg), 1); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1028 | if(res2 == -ENOENT) { |
| 1029 | /* The open syscall was interrupted, so it must be cancelled */ |
| 1030 | if(f->op.release) |
| 1031 | f->op.release(path, arg->flags); |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1032 | } else |
| 1033 | get_node(f, in->ino)->open_count ++; |
| 1034 | pthread_mutex_unlock(&f->lock); |
| 1035 | |
| 1036 | } else |
| 1037 | send_reply(f, in, res, NULL, 0); |
| 1038 | |
Miklos Szeredi | 9a31cca | 2004-06-26 21:11:25 +0000 | [diff] [blame] | 1039 | if (path) |
| 1040 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1041 | } |
| 1042 | |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1043 | static void do_flush(struct fuse *f, struct fuse_in_header *in, |
| 1044 | struct fuse_flush_in *arg) |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1045 | { |
| 1046 | char *path; |
| 1047 | int res; |
| 1048 | |
| 1049 | res = -ENOENT; |
| 1050 | path = get_path(f, in->ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1051 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1052 | if (f->flags & FUSE_DEBUG) { |
| 1053 | printf("FLUSH[%u]\n", arg->fh); |
| 1054 | fflush(stdout); |
| 1055 | } |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1056 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1057 | if (f->op.flush) |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1058 | res = f->op.flush(path); |
| 1059 | free(path); |
| 1060 | } |
| 1061 | send_reply(f, in, res, NULL, 0); |
| 1062 | } |
| 1063 | |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 1064 | static void do_release(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1065 | struct fuse_release_in *arg) |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1066 | { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1067 | struct node *node; |
| 1068 | char *path; |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1069 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1070 | pthread_mutex_lock(&f->lock); |
| 1071 | node = get_node(f, in->ino); |
| 1072 | --node->open_count; |
| 1073 | pthread_mutex_unlock(&f->lock); |
| 1074 | |
| 1075 | path = get_path(f, in->ino); |
| 1076 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1077 | if (f->flags & FUSE_DEBUG) { |
| 1078 | printf("RELEASE[%u]\n", arg->fh); |
| 1079 | fflush(stdout); |
| 1080 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1081 | if (f->op.release) |
Miklos Szeredi | b321058 | 2004-06-23 13:54:33 +0000 | [diff] [blame] | 1082 | f->op.release(path, arg->flags); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1083 | |
| 1084 | if(node->is_hidden && node->open_count == 0) |
| 1085 | /* can now clean up this hidden file */ |
| 1086 | f->op.unlink(path); |
| 1087 | |
| 1088 | free(path); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1089 | } |
Miklos Szeredi | 556d03d | 2004-06-30 11:13:41 +0000 | [diff] [blame] | 1090 | send_reply(f, in, 0, NULL, 0); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1093 | static void do_read(struct fuse *f, struct fuse_in_header *in, |
| 1094 | struct fuse_read_in *arg) |
| 1095 | { |
| 1096 | int res; |
| 1097 | char *path; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1098 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size); |
| 1099 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 1100 | char *buf = outbuf + sizeof(struct fuse_out_header); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1101 | size_t size; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1102 | size_t outsize; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1103 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1104 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1105 | path = get_path(f, in->ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1106 | if (path != NULL) { |
| 1107 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1108 | printf("READ[%u] %u bytes from %llu\n", arg->fh, arg->size, |
| 1109 | arg->offset); |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1110 | fflush(stdout); |
| 1111 | } |
| 1112 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1113 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1114 | if (f->op.read) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 1115 | res = f->op.read(path, buf, arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1116 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1117 | } |
| 1118 | |
| 1119 | size = 0; |
Miklos Szeredi | 25385bb | 2004-07-06 22:27:36 +0000 | [diff] [blame] | 1120 | if (res >= 0) { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1121 | size = res; |
| 1122 | res = 0; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1123 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1124 | printf(" READ[%u] %u bytes\n", arg->fh, size); |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1125 | fflush(stdout); |
| 1126 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1127 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1128 | memset(out, 0, sizeof(struct fuse_out_header)); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1129 | out->unique = in->unique; |
| 1130 | out->error = res; |
| 1131 | outsize = sizeof(struct fuse_out_header) + size; |
| 1132 | |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1133 | send_reply_raw(f, outbuf, outsize, 0); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1134 | free(outbuf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1135 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1136 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1137 | static void do_write(struct fuse *f, struct fuse_in_header *in, |
| 1138 | struct fuse_write_in *arg) |
| 1139 | { |
| 1140 | int res; |
| 1141 | char *path; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 1142 | struct fuse_write_out outarg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1143 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1144 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1145 | path = get_path(f, in->ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1146 | if (path != NULL) { |
| 1147 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1148 | printf("WRITE%s[%u] %u bytes to %llu\n", |
| 1149 | arg->writepage ? "PAGE" : "", arg->fh, arg->size, |
| 1150 | arg->offset); |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1151 | fflush(stdout); |
| 1152 | } |
| 1153 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1154 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1155 | if (f->op.write) |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 1156 | res = f->op.write(path, PARAM(arg), arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1157 | free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 1160 | if (res >= 0) { |
| 1161 | outarg.size = res; |
| 1162 | res = 0; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 1165 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1168 | static int default_statfs(struct statfs *buf) |
| 1169 | { |
| 1170 | buf->f_namelen = 255; |
| 1171 | buf->f_bsize = 512; |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1175 | static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs) |
| 1176 | { |
| 1177 | kstatfs->bsize = statfs->f_bsize; |
| 1178 | kstatfs->blocks = statfs->f_blocks; |
| 1179 | kstatfs->bfree = statfs->f_bfree; |
| 1180 | kstatfs->bavail = statfs->f_bavail; |
| 1181 | kstatfs->files = statfs->f_files; |
| 1182 | kstatfs->ffree = statfs->f_ffree; |
| 1183 | kstatfs->namelen = statfs->f_namelen; |
| 1184 | } |
| 1185 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1186 | static void do_statfs(struct fuse *f, struct fuse_in_header *in) |
| 1187 | { |
| 1188 | int res; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1189 | struct fuse_statfs_out arg; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1190 | struct statfs buf; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1191 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1192 | memset(&buf, 0, sizeof(struct statfs)); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1193 | if (f->op.statfs) |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1194 | res = f->op.statfs("/", &buf); |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1195 | else |
| 1196 | res = default_statfs(&buf); |
| 1197 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1198 | if (res == 0) |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1199 | convert_statfs(&buf, &arg.st); |
Miklos Szeredi | 4b2bef4 | 2002-01-09 12:23:27 +0000 | [diff] [blame] | 1200 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1201 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 1202 | } |
| 1203 | |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1204 | static void do_fsync(struct fuse *f, struct fuse_in_header *in, |
| 1205 | struct fuse_fsync_in *inarg) |
| 1206 | { |
| 1207 | int res; |
| 1208 | char *path; |
| 1209 | |
| 1210 | res = -ENOENT; |
| 1211 | path = get_path(f, in->ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1212 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1213 | if (f->flags & FUSE_DEBUG) { |
| 1214 | printf("FSYNC[%u]\n", inarg->fh); |
| 1215 | fflush(stdout); |
| 1216 | } |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1217 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1218 | if (f->op.fsync) |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1219 | res = f->op.fsync(path, inarg->datasync); |
| 1220 | free(path); |
| 1221 | } |
| 1222 | send_reply(f, in, res, NULL, 0); |
| 1223 | } |
| 1224 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1225 | static void do_setxattr(struct fuse *f, struct fuse_in_header *in, |
| 1226 | struct fuse_setxattr_in *arg) |
| 1227 | { |
| 1228 | int res; |
| 1229 | char *path; |
| 1230 | char *name = PARAM(arg); |
| 1231 | unsigned char *value = name + strlen(name) + 1; |
| 1232 | |
| 1233 | res = -ENOENT; |
| 1234 | path = get_path(f, in->ino); |
| 1235 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1236 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1237 | if (f->op.setxattr) |
| 1238 | res = f->op.setxattr(path, name, value, arg->size, arg->flags); |
| 1239 | free(path); |
| 1240 | } |
| 1241 | send_reply(f, in, res, NULL, 0); |
| 1242 | } |
| 1243 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1244 | static int common_getxattr(struct fuse *f, struct fuse_in_header *in, |
| 1245 | const char *name, char *value, size_t size) |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1246 | { |
| 1247 | int res; |
| 1248 | char *path; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1249 | |
| 1250 | res = -ENOENT; |
| 1251 | path = get_path(f, in->ino); |
| 1252 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1253 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1254 | if (f->op.getxattr) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1255 | res = f->op.getxattr(path, name, value, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1256 | free(path); |
| 1257 | } |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1258 | return res; |
| 1259 | } |
| 1260 | |
| 1261 | static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in, |
| 1262 | const char *name, size_t size) |
| 1263 | { |
| 1264 | int res; |
| 1265 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size); |
| 1266 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 1267 | char *value = outbuf + sizeof(struct fuse_out_header); |
| 1268 | |
| 1269 | res = common_getxattr(f, in, name, value, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1270 | size = 0; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1271 | if (res > 0) { |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1272 | size = res; |
| 1273 | res = 0; |
| 1274 | } |
| 1275 | memset(out, 0, sizeof(struct fuse_out_header)); |
| 1276 | out->unique = in->unique; |
| 1277 | out->error = res; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1278 | |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1279 | send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1280 | free(outbuf); |
| 1281 | } |
| 1282 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1283 | static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in, |
| 1284 | const char *name) |
| 1285 | { |
| 1286 | int res; |
| 1287 | struct fuse_getxattr_out arg; |
| 1288 | |
| 1289 | res = common_getxattr(f, in, name, NULL, 0); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1290 | if (res >= 0) { |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1291 | arg.size = res; |
| 1292 | res = 0; |
| 1293 | } |
| 1294 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 1295 | } |
| 1296 | |
| 1297 | static void do_getxattr(struct fuse *f, struct fuse_in_header *in, |
| 1298 | struct fuse_getxattr_in *arg) |
| 1299 | { |
| 1300 | char *name = PARAM(arg); |
| 1301 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1302 | if (arg->size) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1303 | do_getxattr_read(f, in, name, arg->size); |
| 1304 | else |
| 1305 | do_getxattr_size(f, in, name); |
| 1306 | } |
| 1307 | |
| 1308 | static int common_listxattr(struct fuse *f, struct fuse_in_header *in, |
| 1309 | char *list, size_t size) |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1310 | { |
| 1311 | int res; |
| 1312 | char *path; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1313 | |
| 1314 | res = -ENOENT; |
| 1315 | path = get_path(f, in->ino); |
| 1316 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1317 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1318 | if (f->op.listxattr) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1319 | res = f->op.listxattr(path, list, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1320 | free(path); |
| 1321 | } |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1322 | return res; |
| 1323 | } |
| 1324 | |
| 1325 | static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in, |
| 1326 | size_t size) |
| 1327 | { |
| 1328 | int res; |
| 1329 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size); |
| 1330 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 1331 | char *list = outbuf + sizeof(struct fuse_out_header); |
| 1332 | |
| 1333 | res = common_listxattr(f, in, list, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1334 | size = 0; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1335 | if (res > 0) { |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1336 | size = res; |
| 1337 | res = 0; |
| 1338 | } |
| 1339 | memset(out, 0, sizeof(struct fuse_out_header)); |
| 1340 | out->unique = in->unique; |
| 1341 | out->error = res; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1342 | |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1343 | send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1344 | free(outbuf); |
| 1345 | } |
| 1346 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1347 | static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in) |
| 1348 | { |
| 1349 | int res; |
| 1350 | struct fuse_getxattr_out arg; |
| 1351 | |
| 1352 | res = common_listxattr(f, in, NULL, 0); |
| 1353 | if (res >= 0) { |
| 1354 | arg.size = res; |
| 1355 | res = 0; |
| 1356 | } |
| 1357 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 1358 | } |
| 1359 | |
| 1360 | static void do_listxattr(struct fuse *f, struct fuse_in_header *in, |
| 1361 | struct fuse_getxattr_in *arg) |
| 1362 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1363 | if (arg->size) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1364 | do_listxattr_read(f, in, arg->size); |
| 1365 | else |
| 1366 | do_listxattr_size(f, in); |
| 1367 | } |
| 1368 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1369 | static void do_removexattr(struct fuse *f, struct fuse_in_header *in, |
| 1370 | char *name) |
| 1371 | { |
| 1372 | int res; |
| 1373 | char *path; |
| 1374 | |
| 1375 | res = -ENOENT; |
| 1376 | path = get_path(f, in->ino); |
| 1377 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1378 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1379 | if (f->op.removexattr) |
| 1380 | res = f->op.removexattr(path, name); |
| 1381 | free(path); |
| 1382 | } |
| 1383 | send_reply(f, in, res, NULL, 0); |
| 1384 | } |
| 1385 | |
| 1386 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1387 | static void free_cmd(struct fuse_cmd *cmd) |
| 1388 | { |
| 1389 | free(cmd->buf); |
| 1390 | free(cmd); |
| 1391 | } |
| 1392 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1393 | void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1394 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1395 | struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf; |
| 1396 | void *inarg = cmd->buf + sizeof(struct fuse_in_header); |
| 1397 | size_t argsize; |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 1398 | struct fuse_context *ctx = fuse_get_context(f); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1399 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1400 | dec_avail(f); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 1401 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1402 | if ((f->flags & FUSE_DEBUG)) { |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1403 | printf("unique: %i, opcode: %s (%i), ino: %li, insize: %i\n", |
| 1404 | in->unique, opname(in->opcode), in->opcode, in->ino, |
| 1405 | cmd->buflen); |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 1406 | fflush(stdout); |
| 1407 | } |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 1408 | |
| 1409 | ctx->uid = in->uid; |
| 1410 | ctx->gid = in->gid; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1411 | |
| 1412 | argsize = cmd->buflen - sizeof(struct fuse_in_header); |
| 1413 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1414 | switch (in->opcode) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1415 | case FUSE_LOOKUP: |
| 1416 | do_lookup(f, in, (char *) inarg); |
| 1417 | break; |
| 1418 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1419 | case FUSE_GETATTR: |
| 1420 | do_getattr(f, in); |
| 1421 | break; |
| 1422 | |
| 1423 | case FUSE_SETATTR: |
| 1424 | do_setattr(f, in, (struct fuse_setattr_in *) inarg); |
| 1425 | break; |
| 1426 | |
| 1427 | case FUSE_READLINK: |
| 1428 | do_readlink(f, in); |
| 1429 | break; |
| 1430 | |
| 1431 | case FUSE_GETDIR: |
| 1432 | do_getdir(f, in); |
| 1433 | break; |
| 1434 | |
| 1435 | case FUSE_MKNOD: |
| 1436 | do_mknod(f, in, (struct fuse_mknod_in *) inarg); |
| 1437 | break; |
| 1438 | |
| 1439 | case FUSE_MKDIR: |
| 1440 | do_mkdir(f, in, (struct fuse_mkdir_in *) inarg); |
| 1441 | break; |
| 1442 | |
| 1443 | case FUSE_UNLINK: |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1444 | do_unlink(f, in, (char *) inarg); |
| 1445 | break; |
| 1446 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1447 | case FUSE_RMDIR: |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1448 | do_rmdir(f, in, (char *) inarg); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1449 | break; |
| 1450 | |
| 1451 | case FUSE_SYMLINK: |
| 1452 | do_symlink(f, in, (char *) inarg, |
| 1453 | ((char *) inarg) + strlen((char *) inarg) + 1); |
| 1454 | break; |
| 1455 | |
| 1456 | case FUSE_RENAME: |
| 1457 | do_rename(f, in, (struct fuse_rename_in *) inarg); |
| 1458 | break; |
| 1459 | |
| 1460 | case FUSE_LINK: |
| 1461 | do_link(f, in, (struct fuse_link_in *) inarg); |
| 1462 | break; |
| 1463 | |
| 1464 | case FUSE_OPEN: |
| 1465 | do_open(f, in, (struct fuse_open_in *) inarg); |
| 1466 | break; |
| 1467 | |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1468 | case FUSE_FLUSH: |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1469 | do_flush(f, in, (struct fuse_flush_in *) inarg); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1470 | break; |
| 1471 | |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 1472 | case FUSE_RELEASE: |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1473 | do_release(f, in, (struct fuse_release_in *) inarg); |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 1474 | break; |
| 1475 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1476 | case FUSE_READ: |
| 1477 | do_read(f, in, (struct fuse_read_in *) inarg); |
| 1478 | break; |
| 1479 | |
| 1480 | case FUSE_WRITE: |
| 1481 | do_write(f, in, (struct fuse_write_in *) inarg); |
| 1482 | break; |
| 1483 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1484 | case FUSE_STATFS: |
| 1485 | do_statfs(f, in); |
| 1486 | break; |
| 1487 | |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1488 | case FUSE_FSYNC: |
| 1489 | do_fsync(f, in, (struct fuse_fsync_in *) inarg); |
| 1490 | break; |
| 1491 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1492 | case FUSE_SETXATTR: |
| 1493 | do_setxattr(f, in, (struct fuse_setxattr_in *) inarg); |
| 1494 | break; |
| 1495 | |
| 1496 | case FUSE_GETXATTR: |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1497 | do_getxattr(f, in, (struct fuse_getxattr_in *) inarg); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1498 | break; |
| 1499 | |
| 1500 | case FUSE_LISTXATTR: |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1501 | do_listxattr(f, in, (struct fuse_getxattr_in *) inarg); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1502 | break; |
| 1503 | |
| 1504 | case FUSE_REMOVEXATTR: |
| 1505 | do_removexattr(f, in, (char *) inarg); |
| 1506 | break; |
| 1507 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1508 | default: |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1509 | send_reply(f, in, -ENOSYS, NULL, 0); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1510 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1511 | |
| 1512 | free_cmd(cmd); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1515 | int __fuse_exited(struct fuse* f) |
| 1516 | { |
| 1517 | return f->exited; |
| 1518 | } |
| 1519 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1520 | struct fuse_cmd *__fuse_read_cmd(struct fuse *f) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1521 | { |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1522 | ssize_t res; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1523 | struct fuse_cmd *cmd; |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1524 | struct fuse_in_header *in; |
| 1525 | void *inarg; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1526 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1527 | cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd)); |
| 1528 | cmd->buf = (char *) malloc(FUSE_MAX_IN); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1529 | in = (struct fuse_in_header *) cmd->buf; |
| 1530 | inarg = cmd->buf + sizeof(struct fuse_in_header); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1531 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1532 | res = read(f->fd, cmd->buf, FUSE_MAX_IN); |
| 1533 | if (res == -1) { |
| 1534 | free_cmd(cmd); |
| 1535 | if (__fuse_exited(f) || errno == EINTR) |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1536 | return NULL; |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1537 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1538 | /* ENODEV means we got unmounted, so we silenty return failure */ |
| 1539 | if (errno != ENODEV) { |
| 1540 | /* BAD... This will happen again */ |
| 1541 | perror("fuse: reading device"); |
| 1542 | } |
| 1543 | |
| 1544 | fuse_exit(f); |
| 1545 | return NULL; |
| 1546 | } |
| 1547 | if ((size_t) res < sizeof(struct fuse_in_header)) { |
| 1548 | free_cmd(cmd); |
| 1549 | /* Cannot happen */ |
| 1550 | fprintf(stderr, "short read on fuse device\n"); |
| 1551 | fuse_exit(f); |
| 1552 | return NULL; |
| 1553 | } |
| 1554 | cmd->buflen = res; |
| 1555 | |
| 1556 | /* Forget is special, it can be done without messing with threads. */ |
| 1557 | if (in->opcode == FUSE_FORGET) { |
| 1558 | do_forget(f, in, (struct fuse_forget_in *) inarg); |
| 1559 | free_cmd(cmd); |
| 1560 | return NULL; |
| 1561 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1562 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1563 | return cmd; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1564 | } |
| 1565 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1566 | void fuse_loop(struct fuse *f) |
| 1567 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1568 | if (f == NULL) |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1569 | return; |
| 1570 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1571 | while (1) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1572 | struct fuse_cmd *cmd; |
| 1573 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1574 | if (__fuse_exited(f)) |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1575 | return; |
| 1576 | |
| 1577 | cmd = __fuse_read_cmd(f); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1578 | if (cmd == NULL) |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1579 | continue; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1580 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1581 | __fuse_process_cmd(f, cmd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1582 | } |
| 1583 | } |
| 1584 | |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1585 | int fuse_invalidate(struct fuse *f, const char *path) |
| 1586 | { |
| 1587 | int res; |
| 1588 | int err; |
| 1589 | fino_t ino; |
| 1590 | struct fuse_user_header h; |
| 1591 | |
| 1592 | err = path_lookup(f, path, &ino); |
| 1593 | if (err) { |
| 1594 | if (err == -ENOENT) |
| 1595 | return 0; |
| 1596 | else |
| 1597 | return err; |
| 1598 | } |
| 1599 | |
| 1600 | memset(&h, 0, sizeof(struct fuse_user_header)); |
| 1601 | h.opcode = FUSE_INVALIDATE; |
| 1602 | h.ino = ino; |
| 1603 | |
| 1604 | if ((f->flags & FUSE_DEBUG)) { |
| 1605 | printf("INVALIDATE ino: %li\n", ino); |
| 1606 | fflush(stdout); |
| 1607 | } |
| 1608 | |
| 1609 | res = write(f->fd, &h, sizeof(struct fuse_user_header)); |
| 1610 | if (res == -1) { |
| 1611 | if (errno != ENOENT) { |
| 1612 | perror("fuse: writing device"); |
| 1613 | return -errno; |
| 1614 | } |
| 1615 | } |
| 1616 | return 0; |
| 1617 | } |
| 1618 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1619 | void fuse_exit(struct fuse *f) |
| 1620 | { |
| 1621 | f->exited = 1; |
| 1622 | } |
| 1623 | |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1624 | struct fuse_context *fuse_get_context(struct fuse *f) |
| 1625 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1626 | if (f->getcontext) |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1627 | return f->getcontext(f); |
| 1628 | else |
| 1629 | return &f->context; |
| 1630 | } |
| 1631 | |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1632 | static int check_version(struct fuse *f) |
| 1633 | { |
| 1634 | int res; |
| 1635 | FILE *vf = fopen(FUSE_VERSION_FILE, "r"); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1636 | if (vf == NULL) { |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1637 | fprintf(stderr, "fuse: kernel interface too old, need >= %i.%i\n", |
| 1638 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); |
| 1639 | return -1; |
| 1640 | } |
| 1641 | res = fscanf(vf, "%i.%i", &f->majorver, &f->minorver); |
| 1642 | fclose(vf); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1643 | if (res != 2) { |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1644 | fprintf(stderr, "fuse: error reading %s\n", FUSE_VERSION_FILE); |
| 1645 | return -1; |
| 1646 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1647 | if (f->majorver != FUSE_KERNEL_VERSION) { |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1648 | fprintf(stderr, "fuse: bad kernel interface major version: needs %i\n", |
| 1649 | FUSE_KERNEL_VERSION); |
| 1650 | return -1; |
| 1651 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1652 | if (f->minorver < FUSE_KERNEL_MINOR_VERSION) { |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1653 | fprintf(stderr, "fuse: kernel interface too old: need >= %i.%i", |
| 1654 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); |
| 1655 | return -1; |
| 1656 | } |
| 1657 | |
| 1658 | return 0; |
| 1659 | } |
| 1660 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1661 | |
| 1662 | int fuse_is_lib_option(const char *opt) |
| 1663 | { |
| 1664 | if (strcmp(opt, "debug") == 0 || |
| 1665 | strcmp(opt, "hard_remove") == 0) |
| 1666 | return 1; |
| 1667 | else |
| 1668 | return 0; |
| 1669 | } |
| 1670 | |
| 1671 | static void parse_lib_opts(struct fuse *f, const char *opts) |
| 1672 | { |
| 1673 | if (opts) { |
| 1674 | char *xopts = strdup(opts); |
| 1675 | char *s = xopts; |
| 1676 | char *opt; |
| 1677 | |
| 1678 | while((opt = strsep(&s, ","))) { |
| 1679 | if (strcmp(opt, "debug") == 0) |
| 1680 | f->flags |= FUSE_DEBUG; |
| 1681 | else if (strcmp(opt, "hard_remove") == 0) |
| 1682 | f->flags |= FUSE_HARD_REMOVE; |
| 1683 | else |
| 1684 | fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt); |
| 1685 | } |
| 1686 | free(xopts); |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | struct fuse *fuse_new(int fd, const char *opts, const struct fuse_operations *op) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1691 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1692 | struct fuse *f; |
| 1693 | struct node *root; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1694 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1695 | f = (struct fuse *) calloc(1, sizeof(struct fuse)); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1696 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1697 | if (check_version(f) == -1) { |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1698 | free(f); |
| 1699 | return NULL; |
| 1700 | } |
| 1701 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1702 | parse_lib_opts(f, opts); |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 1703 | f->fd = fd; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1704 | f->ctr = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1705 | f->generation = 0; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1706 | /* FIXME: Dynamic hash table */ |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1707 | f->name_table_size = 14057; |
| 1708 | f->name_table = (struct node **) |
| 1709 | calloc(1, sizeof(struct node *) * f->name_table_size); |
| 1710 | f->ino_table_size = 14057; |
| 1711 | f->ino_table = (struct node **) |
| 1712 | calloc(1, sizeof(struct node *) * f->ino_table_size); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1713 | pthread_mutex_init(&f->lock, NULL); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 1714 | f->numworker = 0; |
| 1715 | f->numavail = 0; |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1716 | f->op = *op; |
| 1717 | f->getcontext = NULL; |
| 1718 | f->context.uid = 0; |
| 1719 | f->context.gid = 0; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1720 | f->exited = 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1721 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1722 | root = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 1723 | root->mode = 0; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1724 | root->rdev = 0; |
| 1725 | root->name = strdup("/"); |
| 1726 | root->parent = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1727 | root->ino = FUSE_ROOT_INO; |
| 1728 | root->generation = 0; |
| 1729 | hash_ino(f, root); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1730 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1731 | return f; |
| 1732 | } |
| 1733 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1734 | void fuse_destroy(struct fuse *f) |
| 1735 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1736 | size_t i; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1737 | for (i = 0; i < f->ino_table_size; i++) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1738 | struct node *node; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1739 | |
| 1740 | for (node = f->ino_table[i]; node != NULL; node = node->ino_next) { |
| 1741 | if (node->is_hidden) { |
| 1742 | char *path = get_path(f, node->ino); |
| 1743 | if (path) |
| 1744 | f->op.unlink(path); |
| 1745 | } |
| 1746 | } |
| 1747 | } |
| 1748 | for (i = 0; i < f->ino_table_size; i++) { |
| 1749 | struct node *node; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1750 | struct node *next; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1751 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1752 | for (node = f->ino_table[i]; node != NULL; node = next) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1753 | next = node->ino_next; |
| 1754 | free_node(node); |
| 1755 | } |
| 1756 | } |
| 1757 | free(f->ino_table); |
| 1758 | free(f->name_table); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1759 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1760 | free(f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1761 | } |