Miklos Szeredi | f830a7f | 2001-11-16 17:46:45 +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 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 9 | #ifdef linux |
| 10 | /* For pread()/pwrite() */ |
| 11 | #define _XOPEN_SOURCE 500 |
| 12 | #endif |
| 13 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 14 | #include <fuse.h> |
| 15 | #include <stdio.h> |
Miklos Szeredi | 074b4b9 | 2002-01-11 08:25:52 +0000 | [diff] [blame] | 16 | #include <string.h> |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 17 | #include <unistd.h> |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 18 | #include <fcntl.h> |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <errno.h> |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 21 | #include <sys/statfs.h> |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 22 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 23 | static int xmp_getattr(const char *path, struct stat *stbuf) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 24 | { |
| 25 | int res; |
| 26 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 27 | res = lstat(path, stbuf); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 28 | if(res == -1) |
| 29 | return -errno; |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 34 | static int xmp_readlink(const char *path, char *buf, size_t size) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 35 | { |
| 36 | int res; |
| 37 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 38 | res = readlink(path, buf, size - 1); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 39 | if(res == -1) |
| 40 | return -errno; |
| 41 | |
| 42 | buf[res] = '\0'; |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 47 | static int xmp_getdir(const char *path, fuse_dirh_t h, fuse_dirfil_t filler) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 48 | { |
| 49 | DIR *dp; |
| 50 | struct dirent *de; |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 51 | int res = 0; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 52 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 53 | dp = opendir(path); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 54 | if(dp == NULL) |
| 55 | return -errno; |
| 56 | |
| 57 | while((de = readdir(dp)) != NULL) { |
| 58 | res = filler(h, de->d_name, de->d_type); |
| 59 | if(res != 0) |
| 60 | break; |
| 61 | } |
| 62 | |
| 63 | closedir(dp); |
| 64 | return res; |
| 65 | } |
| 66 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 67 | static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 68 | { |
| 69 | int res; |
| 70 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 71 | res = mknod(path, mode, rdev); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 72 | if(res == -1) |
| 73 | return -errno; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 78 | static int xmp_mkdir(const char *path, mode_t mode) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 79 | { |
| 80 | int res; |
| 81 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 82 | res = mkdir(path, mode); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 83 | if(res == -1) |
| 84 | return -errno; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 89 | static int xmp_unlink(const char *path) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 90 | { |
| 91 | int res; |
| 92 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 93 | res = unlink(path); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 94 | if(res == -1) |
| 95 | return -errno; |
| 96 | |
| 97 | return 0; |
| 98 | } |
| 99 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 100 | static int xmp_rmdir(const char *path) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 101 | { |
| 102 | int res; |
| 103 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 104 | res = rmdir(path); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 105 | if(res == -1) |
| 106 | return -errno; |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 111 | static int xmp_symlink(const char *from, const char *to) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 112 | { |
| 113 | int res; |
| 114 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 115 | res = symlink(from, to); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 116 | if(res == -1) |
| 117 | return -errno; |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 122 | static int xmp_rename(const char *from, const char *to) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 123 | { |
| 124 | int res; |
| 125 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 126 | res = rename(from, to); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 127 | if(res == -1) |
| 128 | return -errno; |
| 129 | |
| 130 | return 0; |
| 131 | } |
| 132 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 133 | static int xmp_link(const char *from, const char *to) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 134 | { |
| 135 | int res; |
| 136 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 137 | res = link(from, to); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 138 | if(res == -1) |
| 139 | return -errno; |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 144 | static int xmp_chmod(const char *path, mode_t mode) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 145 | { |
| 146 | int res; |
| 147 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 148 | res = chmod(path, mode); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 149 | if(res == -1) |
| 150 | return -errno; |
| 151 | |
| 152 | return 0; |
| 153 | } |
| 154 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 155 | static int xmp_chown(const char *path, uid_t uid, gid_t gid) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 156 | { |
| 157 | int res; |
| 158 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 159 | res = lchown(path, uid, gid); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 160 | if(res == -1) |
| 161 | return -errno; |
| 162 | |
| 163 | return 0; |
| 164 | } |
| 165 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 166 | static int xmp_truncate(const char *path, off_t size) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 167 | { |
| 168 | int res; |
| 169 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 170 | res = truncate(path, size); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 171 | if(res == -1) |
| 172 | return -errno; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 177 | static int xmp_utime(const char *path, struct utimbuf *buf) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 178 | { |
| 179 | int res; |
| 180 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 181 | res = utime(path, buf); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 182 | if(res == -1) |
| 183 | return -errno; |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 189 | static int xmp_open(const char *path, int flags) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 190 | { |
| 191 | int res; |
| 192 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 193 | res = open(path, flags); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 194 | if(res == -1) |
| 195 | return -errno; |
| 196 | |
| 197 | close(res); |
| 198 | return 0; |
| 199 | } |
| 200 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 201 | static int xmp_read(const char *path, char *buf, size_t size, off_t offset) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 202 | { |
| 203 | int fd; |
| 204 | int res; |
| 205 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 206 | fd = open(path, O_RDONLY); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 207 | if(fd == -1) |
| 208 | return -errno; |
| 209 | |
| 210 | res = pread(fd, buf, size, offset); |
| 211 | if(res == -1) |
| 212 | res = -errno; |
| 213 | |
| 214 | close(fd); |
| 215 | return res; |
| 216 | } |
| 217 | |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 218 | static int xmp_write(const char *path, const char *buf, size_t size, |
| 219 | off_t offset) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 220 | { |
| 221 | int fd; |
| 222 | int res; |
| 223 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 224 | fd = open(path, O_WRONLY); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 225 | if(fd == -1) |
| 226 | return -errno; |
| 227 | |
| 228 | res = pwrite(fd, buf, size, offset); |
| 229 | if(res == -1) |
| 230 | res = -errno; |
| 231 | |
| 232 | close(fd); |
| 233 | return res; |
| 234 | } |
| 235 | |
Mark Glines | 85801bb | 2002-03-17 06:58:33 +0000 | [diff] [blame] | 236 | static int xmp_statfs(struct fuse_statfs *fst) |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 237 | { |
| 238 | struct statfs st; |
| 239 | int rv = statfs("/",&st); |
Mark Glines | 85801bb | 2002-03-17 06:58:33 +0000 | [diff] [blame] | 240 | if(!rv) { |
| 241 | fst->block_size = st.f_bsize; |
| 242 | fst->blocks = st.f_blocks; |
| 243 | fst->blocks_free = st.f_bavail; |
| 244 | fst->files = st.f_files; |
| 245 | fst->files_free = st.f_ffree; |
| 246 | fst->namelen = st.f_namelen; |
| 247 | } |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 248 | return rv; |
| 249 | } |
| 250 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 251 | static struct fuse_operations xmp_oper = { |
| 252 | getattr: xmp_getattr, |
| 253 | readlink: xmp_readlink, |
| 254 | getdir: xmp_getdir, |
| 255 | mknod: xmp_mknod, |
| 256 | mkdir: xmp_mkdir, |
| 257 | symlink: xmp_symlink, |
| 258 | unlink: xmp_unlink, |
| 259 | rmdir: xmp_rmdir, |
| 260 | rename: xmp_rename, |
| 261 | link: xmp_link, |
| 262 | chmod: xmp_chmod, |
| 263 | chown: xmp_chown, |
| 264 | truncate: xmp_truncate, |
| 265 | utime: xmp_utime, |
| 266 | open: xmp_open, |
| 267 | read: xmp_read, |
| 268 | write: xmp_write, |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 269 | statfs: xmp_statfs, |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame^] | 270 | release: NULL |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 271 | }; |
| 272 | |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 273 | int main(int argc, char *argv[]) |
| 274 | { |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 275 | fuse_main(argc, argv, &xmp_oper); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 276 | return 0; |
| 277 | } |