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