1 |
/* |
2 |
FUSE: Filesystem in Userspace |
3 |
Copyright (C) 2001-2004 Miklos Szeredi <miklos@szeredi.hu> |
4 |
|
5 |
This program can be distributed under the terms of the GNU GPL. |
6 |
See the file COPYING. |
7 |
*/ |
8 |
|
9 |
#include <config.h> |
10 |
|
11 |
#ifdef linux |
12 |
/* For pread()/pwrite() */ |
13 |
#define _XOPEN_SOURCE 500 |
14 |
#endif |
15 |
|
16 |
#include <fuse.h> |
17 |
#include <stdio.h> |
18 |
#include <string.h> |
19 |
#include <unistd.h> |
20 |
#include <fcntl.h> |
21 |
#include <dirent.h> |
22 |
#include <errno.h> |
23 |
#include <sys/statfs.h> |
24 |
#ifdef HAVE_SETXATTR |
25 |
#include <sys/xattr.h> |
26 |
#endif |
27 |
|
28 |
static int xmp_getattr(const char *path, struct stat *stbuf) |
29 |
{ |
30 |
int res; |
31 |
|
32 |
res = lstat(path, stbuf); |
33 |
if(res == -1) |
34 |
return -errno; |
35 |
|
36 |
return 0; |
37 |
} |
38 |
|
39 |
static int xmp_readlink(const char *path, char *buf, size_t size) |
40 |
{ |
41 |
int res; |
42 |
|
43 |
res = readlink(path, buf, size - 1); |
44 |
if(res == -1) |
45 |
return -errno; |
46 |
|
47 |
buf[res] = '\0'; |
48 |
return 0; |
49 |
} |
50 |
|
51 |
|
52 |
static int xmp_getdir(const char *path, fuse_dirh_t h, fuse_dirfil_t filler) |
53 |
{ |
54 |
DIR *dp; |
55 |
struct dirent *de; |
56 |
int res = 0; |
57 |
|
58 |
dp = opendir(path); |
59 |
if(dp == NULL) |
60 |
return -errno; |
61 |
|
62 |
while((de = readdir(dp)) != NULL) { |
63 |
res = filler(h, de->d_name, de->d_type); |
64 |
if(res != 0) |
65 |
break; |
66 |
} |
67 |
|
68 |
closedir(dp); |
69 |
return res; |
70 |
} |
71 |
|
72 |
static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) |
73 |
{ |
74 |
int res; |
75 |
|
76 |
res = mknod(path, mode, rdev); |
77 |
if(res == -1) |
78 |
return -errno; |
79 |
|
80 |
return 0; |
81 |
} |
82 |
|
83 |
static int xmp_mkdir(const char *path, mode_t mode) |
84 |
{ |
85 |
int res; |
86 |
|
87 |
res = mkdir(path, mode); |
88 |
if(res == -1) |
89 |
return -errno; |
90 |
|
91 |
return 0; |
92 |
} |
93 |
|
94 |
static int xmp_unlink(const char *path) |
95 |
{ |
96 |
int res; |
97 |
|
98 |
res = unlink(path); |
99 |
if(res == -1) |
100 |
return -errno; |
101 |
|
102 |
return 0; |
103 |
} |
104 |
|
105 |
static int xmp_rmdir(const char *path) |
106 |
{ |
107 |
int res; |
108 |
|
109 |
res = rmdir(path); |
110 |
if(res == -1) |
111 |
return -errno; |
112 |
|
113 |
return 0; |
114 |
} |
115 |
|
116 |
static int xmp_symlink(const char *from, const char *to) |
117 |
{ |
118 |
int res; |
119 |
|
120 |
res = symlink(from, to); |
121 |
if(res == -1) |
122 |
return -errno; |
123 |
|
124 |
return 0; |
125 |
} |
126 |
|
127 |
static int xmp_rename(const char *from, const char *to) |
128 |
{ |
129 |
int res; |
130 |
|
131 |
res = rename(from, to); |
132 |
if(res == -1) |
133 |
return -errno; |
134 |
|
135 |
return 0; |
136 |
} |
137 |
|
138 |
static int xmp_link(const char *from, const char *to) |
139 |
{ |
140 |
int res; |
141 |
|
142 |
res = link(from, to); |
143 |
if(res == -1) |
144 |
return -errno; |
145 |
|
146 |
return 0; |
147 |
} |
148 |
|
149 |
static int xmp_chmod(const char *path, mode_t mode) |
150 |
{ |
151 |
int res; |
152 |
|
153 |
res = chmod(path, mode); |
154 |
if(res == -1) |
155 |
return -errno; |
156 |
|
157 |
return 0; |
158 |
} |
159 |
|
160 |
static int xmp_chown(const char *path, uid_t uid, gid_t gid) |
161 |
{ |
162 |
int res; |
163 |
|
164 |
res = lchown(path, uid, gid); |
165 |
if(res == -1) |
166 |
return -errno; |
167 |
|
168 |
return 0; |
169 |
} |
170 |
|
171 |
static int xmp_truncate(const char *path, off_t size) |
172 |
{ |
173 |
int res; |
174 |
|
175 |
res = truncate(path, size); |
176 |
if(res == -1) |
177 |
return -errno; |
178 |
|
179 |
return 0; |
180 |
} |
181 |
|
182 |
static int xmp_utime(const char *path, struct utimbuf *buf) |
183 |
{ |
184 |
int res; |
185 |
|
186 |
res = utime(path, buf); |
187 |
if(res == -1) |
188 |
return -errno; |
189 |
|
190 |
return 0; |
191 |
} |
192 |
|
193 |
|
194 |
static int xmp_open(const char *path, int flags) |
195 |
{ |
196 |
int res; |
197 |
|
198 |
res = open(path, flags); |
199 |
if(res == -1) |
200 |
return -errno; |
201 |
|
202 |
close(res); |
203 |
return 0; |
204 |
} |
205 |
|
206 |
static int xmp_read(const char *path, char *buf, size_t size, off_t offset) |
207 |
{ |
208 |
int fd; |
209 |
int res; |
210 |
|
211 |
fd = open(path, O_RDONLY); |
212 |
if(fd == -1) |
213 |
return -errno; |
214 |
|
215 |
res = pread(fd, buf, size, offset); |
216 |
if(res == -1) |
217 |
res = -errno; |
218 |
|
219 |
close(fd); |
220 |
return res; |
221 |
} |
222 |
|
223 |
static int xmp_write(const char *path, const char *buf, size_t size, |
224 |
off_t offset) |
225 |
{ |
226 |
int fd; |
227 |
int res; |
228 |
|
229 |
fd = open(path, O_WRONLY); |
230 |
if(fd == -1) |
231 |
return -errno; |
232 |
|
233 |
res = pwrite(fd, buf, size, offset); |
234 |
if(res == -1) |
235 |
res = -errno; |
236 |
|
237 |
close(fd); |
238 |
return res; |
239 |
} |
240 |
|
241 |
static int xmp_statfs(const char *path, struct statfs *stbuf) |
242 |
{ |
243 |
int res; |
244 |
|
245 |
res = statfs(path, stbuf); |
246 |
if(res == -1) |
247 |
return -errno; |
248 |
|
249 |
return 0; |
250 |
} |
251 |
|
252 |
static int xmp_release(const char *path, int flags) |
253 |
{ |
254 |
/* Just a stub. This method is optional and can safely be left |
255 |
unimplemented */ |
256 |
|
257 |
(void) path; |
258 |
(void) flags; |
259 |
return 0; |
260 |
} |
261 |
|
262 |
static int xmp_fsync(const char *path, int isdatasync) |
263 |
{ |
264 |
/* Just a stub. This method is optional and can safely be left |
265 |
unimplemented */ |
266 |
|
267 |
(void) path; |
268 |
(void) isdatasync; |
269 |
return 0; |
270 |
} |
271 |
|
272 |
#ifdef HAVE_SETXATTR |
273 |
/* xattr operations are optional and can safely be left unimplemented */ |
274 |
static int xmp_setxattr(const char *path, const char *name, const char *value, |
275 |
size_t size, int flags) |
276 |
{ |
277 |
int res = lsetxattr(path, name, value, size, flags); |
278 |
if(res == -1) |
279 |
return -errno; |
280 |
return 0; |
281 |
} |
282 |
|
283 |
static int xmp_getxattr(const char *path, const char *name, char *value, |
284 |
size_t size) |
285 |
{ |
286 |
int res = lgetxattr(path, name, value, size); |
287 |
if(res == -1) |
288 |
return -errno; |
289 |
return res; |
290 |
} |
291 |
|
292 |
static int xmp_listxattr(const char *path, char *list, size_t size) |
293 |
{ |
294 |
int res = llistxattr(path, list, size); |
295 |
if(res == -1) |
296 |
return -errno; |
297 |
return res; |
298 |
} |
299 |
|
300 |
static int xmp_removexattr(const char *path, const char *name) |
301 |
{ |
302 |
int res = lremovexattr(path, name); |
303 |
if(res == -1) |
304 |
return -errno; |
305 |
return 0; |
306 |
} |
307 |
#endif /* HAVE_SETXATTR */ |
308 |
|
309 |
static struct fuse_operations xmp_oper = { |
310 |
.getattr = xmp_getattr, |
311 |
.readlink = xmp_readlink, |
312 |
.getdir = xmp_getdir, |
313 |
.mknod = xmp_mknod, |
314 |
.mkdir = xmp_mkdir, |
315 |
.symlink = xmp_symlink, |
316 |
.unlink = xmp_unlink, |
317 |
.rmdir = xmp_rmdir, |
318 |
.rename = xmp_rename, |
319 |
.link = xmp_link, |
320 |
.chmod = xmp_chmod, |
321 |
.chown = xmp_chown, |
322 |
.truncate = xmp_truncate, |
323 |
.utime = xmp_utime, |
324 |
.open = xmp_open, |
325 |
.read = xmp_read, |
326 |
.write = xmp_write, |
327 |
.statfs = xmp_statfs, |
328 |
.release = xmp_release, |
329 |
.fsync = xmp_fsync, |
330 |
#ifdef HAVE_SETXATTR |
331 |
.setxattr = xmp_setxattr, |
332 |
.getxattr = xmp_getxattr, |
333 |
.listxattr = xmp_listxattr, |
334 |
.removexattr= xmp_removexattr, |
335 |
#endif |
336 |
}; |
337 |
|
338 |
int main(int argc, char *argv[]) |
339 |
{ |
340 |
fuse_main(argc, argv, &xmp_oper); |
341 |
return 0; |
342 |
} |