Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001 Miklos Szeredi (mszeredi@inf.bme.hu) |
| 4 | |
| 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
| 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | #include <linux/fuse.h> |
| 11 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 12 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #include <unistd.h> |
| 15 | #include <errno.h> |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 16 | #include <assert.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 17 | |
| 18 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 19 | static inline struct node *get_node(fino_t ino) |
| 20 | { |
| 21 | return (struct node *) ((ino << 3) + 0x8000000); |
| 22 | } |
| 23 | |
| 24 | static inline fino_t get_ino(struct node *node) |
| 25 | { |
| 26 | return (((fino_t) node) - 0x8000000) >> 3; |
| 27 | } |
| 28 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 29 | static guint name_hash(const struct node *node) |
| 30 | { |
| 31 | return g_str_hash(node->name) ^ node->parent; |
| 32 | } |
| 33 | |
| 34 | static gint name_compare(const struct node *node1, const struct node *node2) |
| 35 | { |
| 36 | return |
| 37 | node1->parent == node2->parent && |
| 38 | strcmp(node1->name, node2->name) == 0; |
| 39 | } |
| 40 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 41 | static int free_node(struct node *node) |
| 42 | { |
| 43 | g_free(node->name); |
| 44 | g_free(node); |
| 45 | return 1; |
| 46 | } |
| 47 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 48 | static struct node *lookup_node(struct fuse *f, fino_t parent, |
| 49 | const char *name) |
| 50 | { |
| 51 | struct node tmp; |
| 52 | |
| 53 | tmp.name = (char *) name; |
| 54 | tmp.parent = parent; |
| 55 | |
| 56 | return g_hash_table_lookup(f->nametab, &tmp); |
| 57 | } |
| 58 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 59 | static void hash_node(struct fuse *f, struct node *node, fino_t parent, |
| 60 | const char *name) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 61 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 62 | node->name = g_strdup(name); |
| 63 | node->parent = parent; |
| 64 | g_hash_table_insert(f->nametab, node, node); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 67 | static void unhash_node(struct fuse *f, struct node *node) |
| 68 | { |
| 69 | if(node->name != NULL) { |
| 70 | g_hash_table_remove(f->nametab, node); |
| 71 | g_free(node->name); |
| 72 | node->parent = 0; |
| 73 | node->name = NULL; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | static fino_t find_node(struct fuse *f, fino_t parent, char *name, |
| 78 | struct fuse_attr *attr, int version) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 79 | { |
| 80 | struct node *node; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 81 | int mode = attr->mode & S_IFMT; |
| 82 | int rdev = 0; |
| 83 | |
| 84 | if(S_ISCHR(mode) || S_ISBLK(mode)) |
| 85 | rdev = attr->rdev; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 86 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 87 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 88 | node = lookup_node(f, parent, name); |
| 89 | if(node != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 90 | if(node->mode == mode && node->rdev == rdev) |
| 91 | goto out; |
| 92 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 93 | unhash_node(f, node); |
| 94 | } |
| 95 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 96 | node = g_new0(struct node, 1); |
| 97 | node->mode = mode; |
| 98 | node->rdev = rdev; |
| 99 | hash_node(f, node, parent, name); |
| 100 | |
| 101 | out: |
| 102 | node->version = version; |
| 103 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 104 | return get_ino(node); |
| 105 | } |
| 106 | |
| 107 | static fino_t find_node_dir(struct fuse *f, fino_t parent, char *name) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 108 | { |
| 109 | struct node *node; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 110 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 111 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 112 | node = lookup_node(f, parent, name); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 113 | pthread_mutex_unlock(&f->lock); |
| 114 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 115 | if(node != NULL) |
| 116 | return get_ino(node); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 117 | else |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 118 | return (fino_t) -1; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 121 | static char *get_path(struct fuse *f, fino_t ino) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 122 | { |
| 123 | GString *s; |
| 124 | char *ss; |
| 125 | |
| 126 | s = g_string_new(""); |
| 127 | if(ino == FUSE_ROOT_INO) |
| 128 | g_string_prepend_c(s, '/'); |
| 129 | else { |
| 130 | struct node *node; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 131 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 132 | for(; ino != FUSE_ROOT_INO; ino = node->parent) { |
| 133 | node = get_node(ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 134 | if(node->name == NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 135 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 136 | g_string_free(s, TRUE); |
| 137 | return NULL; |
| 138 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 139 | g_string_prepend(s, node->name); |
| 140 | g_string_prepend_c(s, '/'); |
| 141 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 142 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | ss = s->str; |
| 146 | g_string_free(s, FALSE); |
| 147 | |
| 148 | return ss; |
| 149 | } |
| 150 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 151 | 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] | 152 | { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 153 | char *path2; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 154 | char *path; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 155 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 156 | if(ino == FUSE_ROOT_INO) |
| 157 | return g_strconcat("/", name, NULL); |
| 158 | |
| 159 | path = get_path(f, ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 160 | if(path == NULL) |
| 161 | return NULL; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 162 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 163 | path2 = g_strconcat(path, "/", name, NULL); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 164 | g_free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 165 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 166 | return path2; |
| 167 | } |
| 168 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 169 | static void destroy_node(struct fuse *f, fino_t ino, int version) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 170 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 171 | struct node *node; |
| 172 | |
| 173 | pthread_mutex_lock(&f->lock); |
| 174 | node = get_node(ino); |
| 175 | if(node->version == version) { |
| 176 | unhash_node(f, node); |
| 177 | free_node(node); |
| 178 | } |
| 179 | pthread_mutex_unlock(&f->lock); |
| 180 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 183 | static void remove_node(struct fuse *f, fino_t dir, const char *name) |
| 184 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 185 | struct node *node; |
| 186 | |
| 187 | pthread_mutex_lock(&f->lock); |
| 188 | node = lookup_node(f, dir, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 189 | assert(node != NULL); |
| 190 | unhash_node(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 191 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 194 | static void rename_node(struct fuse *f, fino_t olddir, const char *oldname, |
| 195 | fino_t newdir, const char *newname) |
| 196 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 197 | struct node *node; |
| 198 | struct node *newnode; |
| 199 | |
| 200 | pthread_mutex_lock(&f->lock); |
| 201 | node = lookup_node(f, olddir, oldname); |
| 202 | newnode = lookup_node(f, newdir, newname); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 203 | assert(node != NULL); |
| 204 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 205 | if(newnode != NULL) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 206 | unhash_node(f, newnode); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 207 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 208 | unhash_node(f, node); |
| 209 | hash_node(f, node, newdir, newname); |
| 210 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 214 | static void convert_stat(struct stat *stbuf, struct fuse_attr *attr) |
| 215 | { |
| 216 | attr->mode = stbuf->st_mode; |
| 217 | attr->nlink = stbuf->st_nlink; |
| 218 | attr->uid = stbuf->st_uid; |
| 219 | attr->gid = stbuf->st_gid; |
| 220 | attr->rdev = stbuf->st_rdev; |
| 221 | attr->size = stbuf->st_size; |
| 222 | attr->blksize = stbuf->st_blksize; |
| 223 | attr->blocks = stbuf->st_blocks; |
| 224 | attr->atime = stbuf->st_atime; |
| 225 | attr->mtime = stbuf->st_mtime; |
| 226 | attr->ctime = stbuf->st_ctime; |
| 227 | } |
| 228 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 229 | static int fill_dir(struct fuse_dirhandle *dh, char *name, int type) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 230 | { |
| 231 | struct fuse_dirent dirent; |
| 232 | size_t reclen; |
| 233 | size_t res; |
| 234 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 235 | dirent.ino = find_node_dir(dh->fuse, dh->dir, name); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 236 | dirent.namelen = strlen(name); |
| 237 | strncpy(dirent.name, name, sizeof(dirent.name)); |
| 238 | dirent.type = type; |
| 239 | reclen = FUSE_DIRENT_SIZE(&dirent); |
| 240 | res = fwrite(&dirent, reclen, 1, dh->fp); |
| 241 | if(res == 0) { |
| 242 | perror("writing directory file"); |
| 243 | return -EIO; |
| 244 | } |
| 245 | return 0; |
| 246 | } |
| 247 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 248 | static void send_reply(struct fuse *f, struct fuse_in_header *in, int error, |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 249 | void *arg, size_t argsize) |
| 250 | { |
| 251 | int res; |
| 252 | char *outbuf; |
| 253 | size_t outsize; |
| 254 | struct fuse_out_header *out; |
| 255 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 256 | if(error > 0) { |
| 257 | fprintf(stderr, "positive error code: %i\n", error); |
| 258 | error = -ERANGE; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 261 | if(error) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 262 | argsize = 0; |
| 263 | |
| 264 | outsize = sizeof(struct fuse_out_header) + argsize; |
| 265 | outbuf = (char *) g_malloc(outsize); |
| 266 | out = (struct fuse_out_header *) outbuf; |
| 267 | out->unique = in->unique; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 268 | out->error = error; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 269 | if(argsize != 0) |
| 270 | memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize); |
| 271 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 272 | printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique, |
| 273 | out->error, strerror(-out->error), outsize); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 274 | fflush(stdout); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 275 | |
| 276 | res = write(f->fd, outbuf, outsize); |
| 277 | if(res == -1) |
| 278 | perror("writing fuse device"); |
| 279 | |
| 280 | g_free(outbuf); |
| 281 | } |
| 282 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 283 | static void fill_cred(struct fuse_in_header *in, struct fuse_cred *cred) |
| 284 | { |
| 285 | cred->uid = in->uid; |
| 286 | cred->gid = in->gid; |
| 287 | } |
| 288 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 289 | static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name) |
| 290 | { |
| 291 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 292 | char *path; |
| 293 | struct stat buf; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 294 | struct fuse_lookup_out arg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 295 | struct fuse_cred cred; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 296 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 297 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 298 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 299 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 300 | if(path != NULL) { |
| 301 | res = -ENOSYS; |
| 302 | if(f->op.getattr) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 303 | res = f->op.getattr(&cred, path, &buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 304 | g_free(path); |
| 305 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 306 | if(res == 0) { |
| 307 | convert_stat(&buf, &arg.attr); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 308 | arg.ino = find_node(f, in->ino, name, &arg.attr, in->unique); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 309 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 310 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 311 | } |
| 312 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 313 | static void do_forget(struct fuse *f, struct fuse_in_header *in, |
| 314 | struct fuse_forget_in *arg) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 315 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 316 | destroy_node(f, in->ino, arg->version); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | static void do_getattr(struct fuse *f, struct fuse_in_header *in) |
| 320 | { |
| 321 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 322 | char *path; |
| 323 | struct stat buf; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 324 | struct fuse_getattr_out arg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 325 | struct fuse_cred cred; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 326 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 327 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 328 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 329 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 330 | if(path != NULL) { |
| 331 | res = -ENOSYS; |
| 332 | if(f->op.getattr) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 333 | res = f->op.getattr(&cred, path, &buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 334 | g_free(path); |
| 335 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 336 | if(res == 0) |
| 337 | convert_stat(&buf, &arg.attr); |
| 338 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 339 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 340 | } |
| 341 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 342 | static void do_setattr(struct fuse *f, struct fuse_in_header *in, |
| 343 | struct fuse_setattr_in *arg) |
| 344 | { |
| 345 | int res; |
| 346 | char *path; |
| 347 | int valid = arg->valid; |
| 348 | struct fuse_attr *attr = &arg->attr; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 349 | struct fuse_setattr_out outarg; |
| 350 | struct fuse_cred cred; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 351 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 352 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 353 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 354 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 355 | if(path != NULL) { |
| 356 | res = 0; |
| 357 | if(!res && (valid & FATTR_MODE)) { |
| 358 | res = -ENOSYS; |
| 359 | if(f->op.chmod) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 360 | res = f->op.chmod(&cred, path, attr->mode); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 361 | } |
| 362 | if(!res && (valid & (FATTR_UID | FATTR_GID))) { |
| 363 | uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1; |
| 364 | gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1; |
| 365 | |
| 366 | res = -ENOSYS; |
| 367 | if(f->op.chown) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 368 | res = f->op.chown(&cred, path, uid, gid); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 369 | } |
| 370 | if(!res && (valid & FATTR_SIZE)) { |
| 371 | res = -ENOSYS; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 372 | if(f->op.truncate && f->op.getattr) { |
| 373 | res = f->op.truncate(&cred, path, attr->size); |
| 374 | if(!res) { |
| 375 | struct stat buf; |
| 376 | res = f->op.getattr(&cred, path, &buf); |
| 377 | outarg.newsize = buf.st_size; |
| 378 | } |
| 379 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 380 | } |
| 381 | if(!res && (valid & FATTR_UTIME)) { |
| 382 | struct utimbuf buf; |
| 383 | buf.actime = attr->atime; |
| 384 | buf.modtime = attr->mtime; |
| 385 | res = -ENOSYS; |
| 386 | if(f->op.utime) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 387 | res = f->op.utime(&cred, path, &buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 388 | } |
| 389 | g_free(path); |
| 390 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 391 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 392 | } |
| 393 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 394 | static void do_readlink(struct fuse *f, struct fuse_in_header *in) |
| 395 | { |
| 396 | int res; |
| 397 | char link[PATH_MAX + 1]; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 398 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 399 | struct fuse_cred cred; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 400 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 401 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 402 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 403 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 404 | if(path != NULL) { |
| 405 | res = -ENOSYS; |
| 406 | if(f->op.readlink) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 407 | res = f->op.readlink(&cred, path, link, sizeof(link)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 408 | g_free(path); |
| 409 | } |
| 410 | send_reply(f, in, res, link, !res ? strlen(link) : 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | static void do_getdir(struct fuse *f, struct fuse_in_header *in) |
| 414 | { |
| 415 | int res; |
| 416 | struct fuse_getdir_out arg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 417 | struct fuse_dirhandle dh; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 418 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 419 | struct fuse_cred cred; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 420 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 421 | fill_cred(in, &cred); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 422 | dh.fuse = f; |
| 423 | dh.fp = tmpfile(); |
| 424 | dh.dir = in->ino; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 425 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 426 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 427 | if(path != NULL) { |
| 428 | res = -ENOSYS; |
| 429 | if(f->op.getdir) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 430 | res = f->op.getdir(&cred, path, &dh, (fuse_dirfil_t) fill_dir); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 431 | g_free(path); |
| 432 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 433 | fflush(dh.fp); |
| 434 | arg.fd = fileno(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 435 | send_reply(f, in, res, &arg, sizeof(arg)); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 436 | fclose(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 437 | } |
| 438 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 439 | static void do_mknod(struct fuse *f, struct fuse_in_header *in, |
| 440 | struct fuse_mknod_in *inarg) |
| 441 | { |
| 442 | int res; |
| 443 | char *path; |
| 444 | struct fuse_mknod_out outarg; |
| 445 | struct stat buf; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 446 | struct fuse_cred cred; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 447 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 448 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 449 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 450 | path = get_path_name(f, in->ino, inarg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 451 | if(path != NULL) { |
| 452 | res = -ENOSYS; |
| 453 | if(f->op.mknod && f->op.getattr) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 454 | res = f->op.mknod(&cred, path, inarg->mode, inarg->rdev); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 455 | if(res == 0) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 456 | res = f->op.getattr(&cred, path, &buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 457 | } |
| 458 | g_free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 459 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 460 | if(res == 0) { |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 461 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 462 | outarg.ino = find_node(f, in->ino, inarg->name, &outarg.attr, |
| 463 | in->unique); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 464 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 465 | |
| 466 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 467 | } |
| 468 | |
| 469 | static void do_mkdir(struct fuse *f, struct fuse_in_header *in, |
| 470 | struct fuse_mkdir_in *inarg) |
| 471 | { |
| 472 | int res; |
| 473 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 474 | struct fuse_cred cred; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 475 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 476 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 477 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 478 | path = get_path_name(f, in->ino, inarg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 479 | if(path != NULL) { |
| 480 | res = -ENOSYS; |
| 481 | if(f->op.mkdir) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 482 | res = f->op.mkdir(&cred, path, inarg->mode); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 483 | g_free(path); |
| 484 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 485 | send_reply(f, in, res, NULL, 0); |
| 486 | } |
| 487 | |
| 488 | static void do_remove(struct fuse *f, struct fuse_in_header *in, char *name) |
| 489 | { |
| 490 | int res; |
| 491 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 492 | struct fuse_cred cred; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 493 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 494 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 495 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 496 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 497 | if(path != NULL) { |
| 498 | res = -ENOSYS; |
| 499 | if(in->opcode == FUSE_UNLINK) { |
| 500 | if(f->op.unlink) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 501 | res = f->op.unlink(&cred, path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 502 | } |
| 503 | else { |
| 504 | if(f->op.rmdir) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 505 | res = f->op.rmdir(&cred, path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 506 | } |
| 507 | g_free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 508 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 509 | if(res == 0) |
| 510 | remove_node(f, in->ino, name); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 511 | send_reply(f, in, res, NULL, 0); |
| 512 | } |
| 513 | |
| 514 | static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name, |
| 515 | char *link) |
| 516 | { |
| 517 | int res; |
| 518 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 519 | struct fuse_cred cred; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 520 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 521 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 522 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 523 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 524 | if(path != NULL) { |
| 525 | res = -ENOSYS; |
| 526 | if(f->op.symlink) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 527 | res = f->op.symlink(&cred, link, path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 528 | g_free(path); |
| 529 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 530 | send_reply(f, in, res, NULL, 0); |
| 531 | } |
| 532 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 533 | static void do_rename(struct fuse *f, struct fuse_in_header *in, |
| 534 | struct fuse_rename_in *inarg) |
| 535 | { |
| 536 | int res; |
| 537 | fino_t olddir = in->ino; |
| 538 | fino_t newdir = inarg->newdir; |
| 539 | char *oldname = inarg->names; |
| 540 | char *newname = inarg->names + strlen(oldname) + 1; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 541 | char *oldpath; |
| 542 | char *newpath; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 543 | struct fuse_cred cred; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 544 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 545 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 546 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 547 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 548 | if(oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 549 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 550 | if(newpath != NULL) { |
| 551 | res = -ENOSYS; |
| 552 | if(f->op.rename) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 553 | res = f->op.rename(&cred, oldpath, newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 554 | if(res == 0) |
| 555 | rename_node(f, olddir, oldname, newdir, newname); |
| 556 | g_free(newpath); |
| 557 | } |
| 558 | g_free(oldpath); |
| 559 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 560 | send_reply(f, in, res, NULL, 0); |
| 561 | } |
| 562 | |
| 563 | static void do_link(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 564 | struct fuse_link_in *arg) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 565 | { |
| 566 | int res; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 567 | char *oldpath; |
| 568 | char *newpath; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 569 | struct fuse_cred cred; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 570 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 571 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 572 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 573 | oldpath = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 574 | if(oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 575 | newpath = get_path_name(f, arg->newdir, arg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 576 | if(newpath != NULL) { |
| 577 | res = -ENOSYS; |
| 578 | if(f->op.link) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 579 | res = f->op.link(&cred, oldpath, newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 580 | g_free(newpath); |
| 581 | } |
| 582 | g_free(oldpath); |
| 583 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 584 | send_reply(f, in, res, NULL, 0); |
| 585 | } |
| 586 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 587 | static void do_open(struct fuse *f, struct fuse_in_header *in, |
| 588 | struct fuse_open_in *arg) |
| 589 | { |
| 590 | int res; |
| 591 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 592 | struct fuse_cred cred; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 593 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 594 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 595 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 596 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 597 | if(path != NULL) { |
| 598 | res = -ENOSYS; |
| 599 | if(f->op.open) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 600 | res = f->op.open(&cred, path, arg->flags); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 601 | g_free(path); |
| 602 | } |
| 603 | send_reply(f, in, res, NULL, 0); |
| 604 | } |
| 605 | |
| 606 | static void do_read(struct fuse *f, struct fuse_in_header *in, |
| 607 | struct fuse_read_in *arg) |
| 608 | { |
| 609 | int res; |
| 610 | char *path; |
| 611 | char *buf = g_malloc(arg->size); |
| 612 | size_t size; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 613 | struct fuse_cred cred; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 614 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 615 | fill_cred(in, &cred); |
| 616 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 617 | if(path != NULL) { |
| 618 | res = -ENOSYS; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 619 | if(f->op.read) |
| 620 | res = f->op.read(&cred, path, buf, arg->size, arg->offset); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 621 | g_free(path); |
| 622 | } |
| 623 | |
| 624 | size = 0; |
| 625 | if(res > 0) { |
| 626 | size = res; |
| 627 | res = 0; |
| 628 | } |
| 629 | |
| 630 | send_reply(f, in, res, buf, size); |
| 631 | g_free(buf); |
| 632 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 633 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 634 | static void do_write(struct fuse *f, struct fuse_in_header *in, |
| 635 | struct fuse_write_in *arg) |
| 636 | { |
| 637 | int res; |
| 638 | char *path; |
| 639 | struct fuse_cred cred; |
| 640 | |
| 641 | fill_cred(in, &cred); |
| 642 | path = get_path(f, in->ino); |
| 643 | if(path != NULL) { |
| 644 | res = -ENOSYS; |
| 645 | if(f->op.write) |
| 646 | res = f->op.write(&cred, path, arg->buf, arg->size, arg->offset); |
| 647 | g_free(path); |
| 648 | } |
| 649 | |
| 650 | if(res > 0) { |
| 651 | if((size_t) res != arg->size) { |
| 652 | fprintf(stderr, "short write: %u (should be %u)\n", res, |
| 653 | arg->size); |
| 654 | res = -EIO; |
| 655 | } |
| 656 | else |
| 657 | res = 0; |
| 658 | } |
| 659 | |
| 660 | send_reply(f, in, res, NULL, 0); |
| 661 | } |
| 662 | |
| 663 | struct cmd { |
| 664 | struct fuse *f; |
| 665 | char *buf; |
| 666 | size_t buflen; |
| 667 | }; |
| 668 | |
| 669 | static void *do_command(void *data) |
| 670 | { |
| 671 | struct cmd *cmd = (struct cmd *) data; |
| 672 | struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf; |
| 673 | void *inarg = cmd->buf + sizeof(struct fuse_in_header); |
| 674 | size_t argsize; |
| 675 | struct fuse *f = cmd->f; |
| 676 | |
| 677 | printf("unique: %i, opcode: %i, ino: %li, insize: %i\n", in->unique, |
| 678 | in->opcode, in->ino, cmd->buflen); |
| 679 | fflush(stdout); |
| 680 | |
| 681 | argsize = cmd->buflen - sizeof(struct fuse_in_header); |
| 682 | |
| 683 | switch(in->opcode) { |
| 684 | case FUSE_LOOKUP: |
| 685 | do_lookup(f, in, (char *) inarg); |
| 686 | break; |
| 687 | |
| 688 | case FUSE_FORGET: |
| 689 | do_forget(f, in, (struct fuse_forget_in *) inarg); |
| 690 | break; |
| 691 | |
| 692 | case FUSE_GETATTR: |
| 693 | do_getattr(f, in); |
| 694 | break; |
| 695 | |
| 696 | case FUSE_SETATTR: |
| 697 | do_setattr(f, in, (struct fuse_setattr_in *) inarg); |
| 698 | break; |
| 699 | |
| 700 | case FUSE_READLINK: |
| 701 | do_readlink(f, in); |
| 702 | break; |
| 703 | |
| 704 | case FUSE_GETDIR: |
| 705 | do_getdir(f, in); |
| 706 | break; |
| 707 | |
| 708 | case FUSE_MKNOD: |
| 709 | do_mknod(f, in, (struct fuse_mknod_in *) inarg); |
| 710 | break; |
| 711 | |
| 712 | case FUSE_MKDIR: |
| 713 | do_mkdir(f, in, (struct fuse_mkdir_in *) inarg); |
| 714 | break; |
| 715 | |
| 716 | case FUSE_UNLINK: |
| 717 | case FUSE_RMDIR: |
| 718 | do_remove(f, in, (char *) inarg); |
| 719 | break; |
| 720 | |
| 721 | case FUSE_SYMLINK: |
| 722 | do_symlink(f, in, (char *) inarg, |
| 723 | ((char *) inarg) + strlen((char *) inarg) + 1); |
| 724 | break; |
| 725 | |
| 726 | case FUSE_RENAME: |
| 727 | do_rename(f, in, (struct fuse_rename_in *) inarg); |
| 728 | break; |
| 729 | |
| 730 | case FUSE_LINK: |
| 731 | do_link(f, in, (struct fuse_link_in *) inarg); |
| 732 | break; |
| 733 | |
| 734 | case FUSE_OPEN: |
| 735 | do_open(f, in, (struct fuse_open_in *) inarg); |
| 736 | break; |
| 737 | |
| 738 | case FUSE_READ: |
| 739 | do_read(f, in, (struct fuse_read_in *) inarg); |
| 740 | break; |
| 741 | |
| 742 | case FUSE_WRITE: |
| 743 | do_write(f, in, (struct fuse_write_in *) inarg); |
| 744 | break; |
| 745 | |
| 746 | default: |
| 747 | fprintf(stderr, "Operation %i not implemented\n", in->opcode); |
| 748 | /* No need to send reply to async requests */ |
| 749 | if(in->unique != 0) |
| 750 | send_reply(f, in, -ENOSYS, NULL, 0); |
| 751 | } |
| 752 | |
| 753 | g_free(cmd->buf); |
| 754 | g_free(cmd); |
| 755 | |
| 756 | return NULL; |
| 757 | } |
| 758 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 759 | void fuse_loop(struct fuse *f) |
| 760 | { |
| 761 | int res; |
| 762 | char inbuf[FUSE_MAX_IN]; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 763 | pthread_attr_t attr; |
| 764 | pthread_t thrid; |
| 765 | |
| 766 | pthread_attr_init(&attr); |
| 767 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 768 | |
| 769 | while(1) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 770 | struct cmd *cmd; |
| 771 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 772 | res = read(f->fd, inbuf, sizeof(inbuf)); |
| 773 | if(res == -1) { |
| 774 | perror("reading fuse device"); |
| 775 | continue; |
| 776 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 777 | if((size_t) res < sizeof(struct fuse_in_header)) { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 778 | fprintf(stderr, "short read on fuse device\n"); |
| 779 | continue; |
| 780 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 781 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 782 | cmd = g_new0(struct cmd, 1); |
| 783 | cmd->f = f; |
| 784 | cmd->buflen = res; |
| 785 | cmd->buf = g_malloc(cmd->buflen); |
| 786 | memcpy(cmd->buf, inbuf, cmd->buflen); |
| 787 | |
| 788 | if(f->flags & FUSE_MULTITHREAD) { |
| 789 | res = pthread_create(&thrid, &attr, do_command, cmd); |
| 790 | if(res != 0) { |
| 791 | fprintf(stderr, "Error creating thread: %s\n", |
| 792 | strerror(errno)); |
| 793 | exit(1); |
| 794 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 795 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 796 | else |
| 797 | do_command(cmd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 801 | struct fuse *fuse_new(int flags) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 802 | { |
| 803 | struct fuse *f = g_new0(struct fuse, 1); |
| 804 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 805 | f->flags = flags; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 806 | f->fd = -1; |
| 807 | f->dir = NULL; |
| 808 | f->nametab = g_hash_table_new((GHashFunc) name_hash, |
| 809 | (GCompareFunc) name_compare); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 810 | pthread_mutex_init(&f->lock, NULL); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 811 | |
| 812 | return f; |
| 813 | } |
| 814 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 815 | void fuse_set_operations(struct fuse *f, const struct fuse_operations *op) |
| 816 | { |
| 817 | f->op = *op; |
| 818 | } |
| 819 | |
| 820 | void fuse_destroy(struct fuse *f) |
| 821 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 822 | close(f->fd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 823 | g_hash_table_foreach_remove(f->nametab, (GHRFunc) free_node, NULL); |
| 824 | g_hash_table_destroy(f->nametab); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 825 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 826 | g_free(f); |
| 827 | } |