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