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