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 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame^] | 410 | link[PATH_MAX] = '\0'; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 411 | send_reply(f, in, res, link, !res ? strlen(link) : 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | static void do_getdir(struct fuse *f, struct fuse_in_header *in) |
| 415 | { |
| 416 | int res; |
| 417 | struct fuse_getdir_out arg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 418 | struct fuse_dirhandle dh; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 419 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 420 | struct fuse_cred cred; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 421 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 422 | fill_cred(in, &cred); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 423 | dh.fuse = f; |
| 424 | dh.fp = tmpfile(); |
| 425 | dh.dir = in->ino; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 426 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 427 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 428 | if(path != NULL) { |
| 429 | res = -ENOSYS; |
| 430 | if(f->op.getdir) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 431 | res = f->op.getdir(&cred, path, &dh, (fuse_dirfil_t) fill_dir); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 432 | g_free(path); |
| 433 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 434 | fflush(dh.fp); |
| 435 | arg.fd = fileno(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 436 | send_reply(f, in, res, &arg, sizeof(arg)); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 437 | fclose(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 440 | static void do_mknod(struct fuse *f, struct fuse_in_header *in, |
| 441 | struct fuse_mknod_in *inarg) |
| 442 | { |
| 443 | int res; |
| 444 | char *path; |
| 445 | struct fuse_mknod_out outarg; |
| 446 | struct stat buf; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 447 | struct fuse_cred cred; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 448 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 449 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 450 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 451 | path = get_path_name(f, in->ino, inarg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 452 | if(path != NULL) { |
| 453 | res = -ENOSYS; |
| 454 | if(f->op.mknod && f->op.getattr) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 455 | res = f->op.mknod(&cred, path, inarg->mode, inarg->rdev); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 456 | if(res == 0) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 457 | res = f->op.getattr(&cred, path, &buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 458 | } |
| 459 | g_free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 460 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 461 | if(res == 0) { |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 462 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 463 | outarg.ino = find_node(f, in->ino, inarg->name, &outarg.attr, |
| 464 | in->unique); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 465 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 466 | |
| 467 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 468 | } |
| 469 | |
| 470 | static void do_mkdir(struct fuse *f, struct fuse_in_header *in, |
| 471 | struct fuse_mkdir_in *inarg) |
| 472 | { |
| 473 | int res; |
| 474 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 475 | struct fuse_cred cred; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 476 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 477 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 478 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 479 | path = get_path_name(f, in->ino, inarg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 480 | if(path != NULL) { |
| 481 | res = -ENOSYS; |
| 482 | if(f->op.mkdir) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 483 | res = f->op.mkdir(&cred, path, inarg->mode); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 484 | g_free(path); |
| 485 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 486 | send_reply(f, in, res, NULL, 0); |
| 487 | } |
| 488 | |
| 489 | static void do_remove(struct fuse *f, struct fuse_in_header *in, char *name) |
| 490 | { |
| 491 | int res; |
| 492 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 493 | struct fuse_cred cred; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 494 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 495 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 496 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 497 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 498 | if(path != NULL) { |
| 499 | res = -ENOSYS; |
| 500 | if(in->opcode == FUSE_UNLINK) { |
| 501 | if(f->op.unlink) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 502 | res = f->op.unlink(&cred, path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 503 | } |
| 504 | else { |
| 505 | if(f->op.rmdir) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 506 | res = f->op.rmdir(&cred, path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 507 | } |
| 508 | g_free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 509 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 510 | if(res == 0) |
| 511 | remove_node(f, in->ino, name); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 512 | send_reply(f, in, res, NULL, 0); |
| 513 | } |
| 514 | |
| 515 | static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name, |
| 516 | char *link) |
| 517 | { |
| 518 | int res; |
| 519 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 520 | struct fuse_cred cred; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 521 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 522 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 523 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 524 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 525 | if(path != NULL) { |
| 526 | res = -ENOSYS; |
| 527 | if(f->op.symlink) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 528 | res = f->op.symlink(&cred, link, path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 529 | g_free(path); |
| 530 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 531 | send_reply(f, in, res, NULL, 0); |
| 532 | } |
| 533 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 534 | static void do_rename(struct fuse *f, struct fuse_in_header *in, |
| 535 | struct fuse_rename_in *inarg) |
| 536 | { |
| 537 | int res; |
| 538 | fino_t olddir = in->ino; |
| 539 | fino_t newdir = inarg->newdir; |
| 540 | char *oldname = inarg->names; |
| 541 | char *newname = inarg->names + strlen(oldname) + 1; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 542 | char *oldpath; |
| 543 | char *newpath; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 544 | struct fuse_cred cred; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 545 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 546 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 547 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 548 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 549 | if(oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 550 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 551 | if(newpath != NULL) { |
| 552 | res = -ENOSYS; |
| 553 | if(f->op.rename) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 554 | res = f->op.rename(&cred, oldpath, newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 555 | if(res == 0) |
| 556 | rename_node(f, olddir, oldname, newdir, newname); |
| 557 | g_free(newpath); |
| 558 | } |
| 559 | g_free(oldpath); |
| 560 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 561 | send_reply(f, in, res, NULL, 0); |
| 562 | } |
| 563 | |
| 564 | static void do_link(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 565 | struct fuse_link_in *arg) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 566 | { |
| 567 | int res; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 568 | char *oldpath; |
| 569 | char *newpath; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 570 | struct fuse_cred cred; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 571 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 572 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 573 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 574 | oldpath = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 575 | if(oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 576 | newpath = get_path_name(f, arg->newdir, arg->name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 577 | if(newpath != NULL) { |
| 578 | res = -ENOSYS; |
| 579 | if(f->op.link) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 580 | res = f->op.link(&cred, oldpath, newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 581 | g_free(newpath); |
| 582 | } |
| 583 | g_free(oldpath); |
| 584 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 585 | send_reply(f, in, res, NULL, 0); |
| 586 | } |
| 587 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 588 | static void do_open(struct fuse *f, struct fuse_in_header *in, |
| 589 | struct fuse_open_in *arg) |
| 590 | { |
| 591 | int res; |
| 592 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 593 | struct fuse_cred cred; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 594 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 595 | fill_cred(in, &cred); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 596 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 597 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 598 | if(path != NULL) { |
| 599 | res = -ENOSYS; |
| 600 | if(f->op.open) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 601 | res = f->op.open(&cred, path, arg->flags); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 602 | g_free(path); |
| 603 | } |
| 604 | send_reply(f, in, res, NULL, 0); |
| 605 | } |
| 606 | |
| 607 | static void do_read(struct fuse *f, struct fuse_in_header *in, |
| 608 | struct fuse_read_in *arg) |
| 609 | { |
| 610 | int res; |
| 611 | char *path; |
| 612 | char *buf = g_malloc(arg->size); |
| 613 | size_t size; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 614 | struct fuse_cred cred; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 615 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 616 | fill_cred(in, &cred); |
| 617 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 618 | if(path != NULL) { |
| 619 | res = -ENOSYS; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 620 | if(f->op.read) |
| 621 | res = f->op.read(&cred, path, buf, arg->size, arg->offset); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 622 | g_free(path); |
| 623 | } |
| 624 | |
| 625 | size = 0; |
| 626 | if(res > 0) { |
| 627 | size = res; |
| 628 | res = 0; |
| 629 | } |
| 630 | |
| 631 | send_reply(f, in, res, buf, size); |
| 632 | g_free(buf); |
| 633 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 634 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 635 | static void do_write(struct fuse *f, struct fuse_in_header *in, |
| 636 | struct fuse_write_in *arg) |
| 637 | { |
| 638 | int res; |
| 639 | char *path; |
| 640 | struct fuse_cred cred; |
| 641 | |
| 642 | fill_cred(in, &cred); |
| 643 | path = get_path(f, in->ino); |
| 644 | if(path != NULL) { |
| 645 | res = -ENOSYS; |
| 646 | if(f->op.write) |
| 647 | res = f->op.write(&cred, path, arg->buf, arg->size, arg->offset); |
| 648 | g_free(path); |
| 649 | } |
| 650 | |
| 651 | if(res > 0) { |
| 652 | if((size_t) res != arg->size) { |
| 653 | fprintf(stderr, "short write: %u (should be %u)\n", res, |
| 654 | arg->size); |
| 655 | res = -EIO; |
| 656 | } |
| 657 | else |
| 658 | res = 0; |
| 659 | } |
| 660 | |
| 661 | send_reply(f, in, res, NULL, 0); |
| 662 | } |
| 663 | |
| 664 | struct cmd { |
| 665 | struct fuse *f; |
| 666 | char *buf; |
| 667 | size_t buflen; |
| 668 | }; |
| 669 | |
| 670 | static void *do_command(void *data) |
| 671 | { |
| 672 | struct cmd *cmd = (struct cmd *) data; |
| 673 | struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf; |
| 674 | void *inarg = cmd->buf + sizeof(struct fuse_in_header); |
| 675 | size_t argsize; |
| 676 | struct fuse *f = cmd->f; |
| 677 | |
| 678 | printf("unique: %i, opcode: %i, ino: %li, insize: %i\n", in->unique, |
| 679 | in->opcode, in->ino, cmd->buflen); |
| 680 | fflush(stdout); |
| 681 | |
| 682 | argsize = cmd->buflen - sizeof(struct fuse_in_header); |
| 683 | |
| 684 | switch(in->opcode) { |
| 685 | case FUSE_LOOKUP: |
| 686 | do_lookup(f, in, (char *) inarg); |
| 687 | break; |
| 688 | |
| 689 | case FUSE_FORGET: |
| 690 | do_forget(f, in, (struct fuse_forget_in *) inarg); |
| 691 | break; |
| 692 | |
| 693 | case FUSE_GETATTR: |
| 694 | do_getattr(f, in); |
| 695 | break; |
| 696 | |
| 697 | case FUSE_SETATTR: |
| 698 | do_setattr(f, in, (struct fuse_setattr_in *) inarg); |
| 699 | break; |
| 700 | |
| 701 | case FUSE_READLINK: |
| 702 | do_readlink(f, in); |
| 703 | break; |
| 704 | |
| 705 | case FUSE_GETDIR: |
| 706 | do_getdir(f, in); |
| 707 | break; |
| 708 | |
| 709 | case FUSE_MKNOD: |
| 710 | do_mknod(f, in, (struct fuse_mknod_in *) inarg); |
| 711 | break; |
| 712 | |
| 713 | case FUSE_MKDIR: |
| 714 | do_mkdir(f, in, (struct fuse_mkdir_in *) inarg); |
| 715 | break; |
| 716 | |
| 717 | case FUSE_UNLINK: |
| 718 | case FUSE_RMDIR: |
| 719 | do_remove(f, in, (char *) inarg); |
| 720 | break; |
| 721 | |
| 722 | case FUSE_SYMLINK: |
| 723 | do_symlink(f, in, (char *) inarg, |
| 724 | ((char *) inarg) + strlen((char *) inarg) + 1); |
| 725 | break; |
| 726 | |
| 727 | case FUSE_RENAME: |
| 728 | do_rename(f, in, (struct fuse_rename_in *) inarg); |
| 729 | break; |
| 730 | |
| 731 | case FUSE_LINK: |
| 732 | do_link(f, in, (struct fuse_link_in *) inarg); |
| 733 | break; |
| 734 | |
| 735 | case FUSE_OPEN: |
| 736 | do_open(f, in, (struct fuse_open_in *) inarg); |
| 737 | break; |
| 738 | |
| 739 | case FUSE_READ: |
| 740 | do_read(f, in, (struct fuse_read_in *) inarg); |
| 741 | break; |
| 742 | |
| 743 | case FUSE_WRITE: |
| 744 | do_write(f, in, (struct fuse_write_in *) inarg); |
| 745 | break; |
| 746 | |
| 747 | default: |
| 748 | fprintf(stderr, "Operation %i not implemented\n", in->opcode); |
| 749 | /* No need to send reply to async requests */ |
| 750 | if(in->unique != 0) |
| 751 | send_reply(f, in, -ENOSYS, NULL, 0); |
| 752 | } |
| 753 | |
| 754 | g_free(cmd->buf); |
| 755 | g_free(cmd); |
| 756 | |
| 757 | return NULL; |
| 758 | } |
| 759 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame^] | 760 | /* This hack makes it possible to link FUSE with or without the |
| 761 | pthread library */ |
| 762 | __attribute__((weak)) |
| 763 | int pthread_create(pthread_t *thrid __attribute__((unused)), |
| 764 | const pthread_attr_t *attr __attribute__((unused)), |
| 765 | void *(*func)(void *) __attribute__((unused)), |
| 766 | void *arg __attribute__((unused))) |
| 767 | { |
| 768 | return ENOSYS; |
| 769 | } |
| 770 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 771 | void fuse_loop(struct fuse *f) |
| 772 | { |
| 773 | int res; |
| 774 | char inbuf[FUSE_MAX_IN]; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 775 | pthread_attr_t attr; |
| 776 | pthread_t thrid; |
| 777 | |
| 778 | pthread_attr_init(&attr); |
| 779 | pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 780 | |
| 781 | while(1) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 782 | struct cmd *cmd; |
| 783 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 784 | res = read(f->fd, inbuf, sizeof(inbuf)); |
| 785 | if(res == -1) { |
| 786 | perror("reading fuse device"); |
| 787 | continue; |
| 788 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 789 | if((size_t) res < sizeof(struct fuse_in_header)) { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 790 | fprintf(stderr, "short read on fuse device\n"); |
| 791 | continue; |
| 792 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 793 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 794 | cmd = g_new0(struct cmd, 1); |
| 795 | cmd->f = f; |
| 796 | cmd->buflen = res; |
| 797 | cmd->buf = g_malloc(cmd->buflen); |
| 798 | memcpy(cmd->buf, inbuf, cmd->buflen); |
| 799 | |
| 800 | if(f->flags & FUSE_MULTITHREAD) { |
| 801 | res = pthread_create(&thrid, &attr, do_command, cmd); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame^] | 802 | if(res == 0) |
| 803 | continue; |
| 804 | |
| 805 | fprintf(stderr, "Error creating thread: %s\n", strerror(res)); |
| 806 | fprintf(stderr, "Will run in single thread mode\n"); |
| 807 | f->flags &= ~FUSE_MULTITHREAD; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 808 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame^] | 809 | |
| 810 | do_command(cmd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 811 | } |
| 812 | } |
| 813 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame^] | 814 | struct fuse *fuse_new(int flags, mode_t root) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 815 | { |
| 816 | struct fuse *f = g_new0(struct fuse, 1); |
| 817 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame^] | 818 | if(!root) |
| 819 | root = S_IFDIR; |
| 820 | |
| 821 | if(!S_ISDIR(root) && !S_ISREG(root)) { |
| 822 | fprintf(stderr, "Invalid mode for root: 0%o\n", root); |
| 823 | root = S_IFDIR; |
| 824 | } |
| 825 | root &= S_IFMT; |
| 826 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 827 | f->flags = flags; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame^] | 828 | f->rootmode = root; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 829 | f->fd = -1; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame^] | 830 | f->mnt = NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 831 | f->nametab = g_hash_table_new((GHashFunc) name_hash, |
| 832 | (GCompareFunc) name_compare); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 833 | pthread_mutex_init(&f->lock, NULL); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 834 | |
| 835 | return f; |
| 836 | } |
| 837 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 838 | void fuse_set_operations(struct fuse *f, const struct fuse_operations *op) |
| 839 | { |
| 840 | f->op = *op; |
| 841 | } |
| 842 | |
| 843 | void fuse_destroy(struct fuse *f) |
| 844 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 845 | close(f->fd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 846 | g_hash_table_foreach_remove(f->nametab, (GHRFunc) free_node, NULL); |
| 847 | g_hash_table_destroy(f->nametab); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 848 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 849 | g_free(f); |
| 850 | } |