blob: e783423e38d57a0fbcc55ddd0ecf6ab046b58f63 [file] [log] [blame]
Miklos Szeredif830a7f2001-11-16 17:46:45 +00001/*
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 Szeredi2df1c042001-11-06 15:07:17 +00009#ifdef linux
10/* For pread()/pwrite() */
11#define _XOPEN_SOURCE 500
12#endif
13
Miklos Szeredi2df1c042001-11-06 15:07:17 +000014#include <fuse.h>
15#include <stdio.h>
Miklos Szeredi074b4b92002-01-11 08:25:52 +000016#include <string.h>
Miklos Szeredi2df1c042001-11-06 15:07:17 +000017#include <unistd.h>
Miklos Szeredicc8c9752001-11-21 10:03:39 +000018#include <fcntl.h>
Miklos Szeredi2df1c042001-11-06 15:07:17 +000019#include <dirent.h>
20#include <errno.h>
Mark Glinesd84b39a2002-01-07 16:32:02 +000021#include <sys/statfs.h>
Miklos Szeredi2df1c042001-11-06 15:07:17 +000022
Miklos Szeredi0a7077f2001-11-11 18:20:17 +000023static int xmp_getattr(const char *path, struct stat *stbuf)
Miklos Szeredi2df1c042001-11-06 15:07:17 +000024{
25 int res;
26
Miklos Szeredi2df1c042001-11-06 15:07:17 +000027 res = lstat(path, stbuf);
Miklos Szeredi2df1c042001-11-06 15:07:17 +000028 if(res == -1)
29 return -errno;
30
31 return 0;
32}
33
Miklos Szeredi0a7077f2001-11-11 18:20:17 +000034static int xmp_readlink(const char *path, char *buf, size_t size)
Miklos Szeredi2df1c042001-11-06 15:07:17 +000035{
36 int res;
37
Miklos Szeredi2df1c042001-11-06 15:07:17 +000038 res = readlink(path, buf, size - 1);
Miklos Szeredi2df1c042001-11-06 15:07:17 +000039 if(res == -1)
40 return -errno;
41
42 buf[res] = '\0';
43 return 0;
44}
45
46
Miklos Szeredi0a7077f2001-11-11 18:20:17 +000047static int xmp_getdir(const char *path, fuse_dirh_t h, fuse_dirfil_t filler)
Miklos Szeredi2df1c042001-11-06 15:07:17 +000048{
49 DIR *dp;
50 struct dirent *de;
Miklos Szeredi0a7077f2001-11-11 18:20:17 +000051 int res = 0;
Miklos Szeredi2df1c042001-11-06 15:07:17 +000052
Miklos Szeredi2df1c042001-11-06 15:07:17 +000053 dp = opendir(path);
Miklos Szeredi2df1c042001-11-06 15:07:17 +000054 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 Szeredi0a7077f2001-11-11 18:20:17 +000067static int xmp_mknod(const char *path, mode_t mode, dev_t rdev)
Miklos Szeredi2df1c042001-11-06 15:07:17 +000068{
69 int res;
70
Miklos Szeredi2df1c042001-11-06 15:07:17 +000071 res = mknod(path, mode, rdev);
Miklos Szeredi2df1c042001-11-06 15:07:17 +000072 if(res == -1)
73 return -errno;
74
75 return 0;
76}
77
Miklos Szeredi0a7077f2001-11-11 18:20:17 +000078static int xmp_mkdir(const char *path, mode_t mode)
Miklos Szeredi2df1c042001-11-06 15:07:17 +000079{
80 int res;
81
Miklos Szeredi2df1c042001-11-06 15:07:17 +000082 res = mkdir(path, mode);
Miklos Szeredi2df1c042001-11-06 15:07:17 +000083 if(res == -1)
84 return -errno;
85
86 return 0;
87}
88
Miklos Szeredi0a7077f2001-11-11 18:20:17 +000089static int xmp_unlink(const char *path)
Miklos Szeredi2df1c042001-11-06 15:07:17 +000090{
91 int res;
92
Miklos Szeredi2df1c042001-11-06 15:07:17 +000093 res = unlink(path);
Miklos Szeredi2df1c042001-11-06 15:07:17 +000094 if(res == -1)
95 return -errno;
96
97 return 0;
98}
99
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000100static int xmp_rmdir(const char *path)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000101{
102 int res;
103
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000104 res = rmdir(path);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000105 if(res == -1)
106 return -errno;
107
108 return 0;
109}
110
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000111static int xmp_symlink(const char *from, const char *to)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000112{
113 int res;
114
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000115 res = symlink(from, to);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000116 if(res == -1)
117 return -errno;
118
119 return 0;
120}
121
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000122static int xmp_rename(const char *from, const char *to)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000123{
124 int res;
125
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000126 res = rename(from, to);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000127 if(res == -1)
128 return -errno;
129
130 return 0;
131}
132
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000133static int xmp_link(const char *from, const char *to)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000134{
135 int res;
136
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000137 res = link(from, to);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000138 if(res == -1)
139 return -errno;
140
141 return 0;
142}
143
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000144static int xmp_chmod(const char *path, mode_t mode)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000145{
146 int res;
147
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000148 res = chmod(path, mode);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000149 if(res == -1)
150 return -errno;
151
152 return 0;
153}
154
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000155static int xmp_chown(const char *path, uid_t uid, gid_t gid)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000156{
157 int res;
158
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000159 res = lchown(path, uid, gid);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000160 if(res == -1)
161 return -errno;
162
163 return 0;
164}
165
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000166static int xmp_truncate(const char *path, off_t size)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000167{
168 int res;
169
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000170 res = truncate(path, size);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000171 if(res == -1)
172 return -errno;
173
174 return 0;
175}
176
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000177static int xmp_utime(const char *path, struct utimbuf *buf)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000178{
179 int res;
180
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000181 res = utime(path, buf);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000182 if(res == -1)
183 return -errno;
184
185 return 0;
186}
187
188
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000189static int xmp_open(const char *path, int flags)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000190{
191 int res;
192
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000193 res = open(path, flags);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000194 if(res == -1)
195 return -errno;
196
197 close(res);
198 return 0;
199}
200
Miklos Szeredi0a7077f2001-11-11 18:20:17 +0000201static int xmp_read(const char *path, char *buf, size_t size, off_t offset)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000202{
203 int fd;
204 int res;
205
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000206 fd = open(path, O_RDONLY);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000207 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 Szeredi0a7077f2001-11-11 18:20:17 +0000218static int xmp_write(const char *path, const char *buf, size_t size,
219 off_t offset)
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000220{
221 int fd;
222 int res;
223
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000224 fd = open(path, O_WRONLY);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000225 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 Glines85801bb2002-03-17 06:58:33 +0000236static int xmp_statfs(struct fuse_statfs *fst)
Mark Glinesd84b39a2002-01-07 16:32:02 +0000237{
238 struct statfs st;
239 int rv = statfs("/",&st);
Mark Glines85801bb2002-03-17 06:58:33 +0000240 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 Glinesd84b39a2002-01-07 16:32:02 +0000248 return rv;
249}
250
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000251static 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 Glinesd84b39a2002-01-07 16:32:02 +0000269 statfs: xmp_statfs,
Miklos Szeredic8ba2372002-12-10 12:26:00 +0000270 release: NULL
Miklos Szeredi680a69a2001-11-16 13:31:14 +0000271};
272
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000273int main(int argc, char *argv[])
274{
Miklos Szeredicc8c9752001-11-21 10:03:39 +0000275 fuse_main(argc, argv, &xmp_oper);
Miklos Szeredi2df1c042001-11-06 15:07:17 +0000276 return 0;
277}