~james-w/+junk/fuse-debian-upstream

« back to all changes in this revision

Viewing changes to example/fusexmp_fh.c

  • Committer: James Westby
  • Date: 2008-05-16 12:57:48 UTC
  • Revision ID: jw+debian@jameswestby.net-20080516125748-b8m5dtnbbyjtjpkg
Tags: upstream-debian-2.3.0
Import upstream from fuse_2.3.0.orig.tar.gz

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    FUSE: Filesystem in Userspace
 
3
    Copyright (C) 2001-2005  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
static int xmp_opendir(const char *path, struct fuse_file_info *fi)
 
52
{
 
53
    DIR *dp = opendir(path);
 
54
    if (dp == NULL)
 
55
        return -errno;
 
56
 
 
57
    fi->fh = (unsigned long) dp;
 
58
    return 0;
 
59
}
 
60
 
 
61
static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 
62
                       off_t offset, struct fuse_file_info *fi)
 
63
{
 
64
    DIR *dp = (DIR *) fi->fh;
 
65
    struct dirent *de;
 
66
 
 
67
    (void) path;
 
68
    seekdir(dp, offset);
 
69
    while ((de = readdir(dp)) != NULL) {
 
70
        struct stat st;
 
71
        memset(&st, 0, sizeof(st));
 
72
        st.st_ino = de->d_ino;
 
73
        st.st_mode = de->d_type << 12;
 
74
        if (filler(buf, de->d_name, &st, de->d_off))
 
75
            break;
 
76
    }
 
77
 
 
78
    return 0;
 
79
}
 
80
 
 
81
static int xmp_releasedir(const char *path, struct fuse_file_info *fi)
 
82
{
 
83
    DIR *dp = (DIR *) fi->fh;
 
84
    (void) path;
 
85
    closedir(dp);
 
86
    return 0;
 
87
}
 
88
 
 
89
static int xmp_mknod(const char *path, mode_t mode, dev_t rdev)
 
90
{
 
91
    int res;
 
92
 
 
93
    res = mknod(path, mode, rdev);
 
94
    if(res == -1)
 
95
        return -errno;
 
96
 
 
97
    return 0;
 
98
}
 
99
 
 
100
static int xmp_mkdir(const char *path, mode_t mode)
 
101
{
 
102
    int res;
 
103
 
 
104
    res = mkdir(path, mode);
 
105
    if(res == -1)
 
106
        return -errno;
 
107
 
 
108
    return 0;
 
109
}
 
110
 
 
111
static int xmp_unlink(const char *path)
 
112
{
 
113
    int res;
 
114
 
 
115
    res = unlink(path);
 
116
    if(res == -1)
 
117
        return -errno;
 
118
 
 
119
    return 0;
 
120
}
 
121
 
 
122
static int xmp_rmdir(const char *path)
 
123
{
 
124
    int res;
 
125
 
 
126
    res = rmdir(path);
 
127
    if(res == -1)
 
128
        return -errno;
 
129
 
 
130
    return 0;
 
131
}
 
132
 
 
133
static int xmp_symlink(const char *from, const char *to)
 
134
{
 
135
    int res;
 
136
 
 
137
    res = symlink(from, to);
 
138
    if(res == -1)
 
139
        return -errno;
 
140
 
 
141
    return 0;
 
142
}
 
143
 
 
144
static int xmp_rename(const char *from, const char *to)
 
145
{
 
146
    int res;
 
147
 
 
148
    res = rename(from, to);
 
149
    if(res == -1)
 
150
        return -errno;
 
151
 
 
152
    return 0;
 
153
}
 
154
 
 
155
static int xmp_link(const char *from, const char *to)
 
156
{
 
157
    int res;
 
158
 
 
159
    res = link(from, to);
 
160
    if(res == -1)
 
161
        return -errno;
 
162
 
 
163
    return 0;
 
164
}
 
165
 
 
166
static int xmp_chmod(const char *path, mode_t mode)
 
167
{
 
168
    int res;
 
169
 
 
170
    res = chmod(path, mode);
 
171
    if(res == -1)
 
172
        return -errno;
 
173
 
 
174
    return 0;
 
175
}
 
176
 
 
177
static int xmp_chown(const char *path, uid_t uid, gid_t gid)
 
178
{
 
179
    int res;
 
180
 
 
181
    res = lchown(path, uid, gid);
 
182
    if(res == -1)
 
183
        return -errno;
 
184
 
 
185
    return 0;
 
186
}
 
187
 
 
188
static int xmp_truncate(const char *path, off_t size)
 
189
{
 
190
    int res;
 
191
 
 
192
    res = truncate(path, size);
 
193
    if(res == -1)
 
194
        return -errno;
 
195
 
 
196
    return 0;
 
197
}
 
198
 
 
199
static int xmp_utime(const char *path, struct utimbuf *buf)
 
200
{
 
201
    int res;
 
202
 
 
203
    res = utime(path, buf);
 
204
    if(res == -1)
 
205
        return -errno;
 
206
 
 
207
    return 0;
 
208
}
 
209
 
 
210
 
 
211
static int xmp_open(const char *path, struct fuse_file_info *fi)
 
212
{
 
213
    int res;
 
214
 
 
215
    res = open(path, fi->flags);
 
216
    if(res == -1)
 
217
        return -errno;
 
218
 
 
219
    fi->fh = res;
 
220
    return 0;
 
221
}
 
222
 
 
223
static int xmp_read(const char *path, char *buf, size_t size, off_t offset,
 
224
                    struct fuse_file_info *fi)
 
225
{
 
226
    int res;
 
227
 
 
228
    (void) path;
 
229
    res = pread(fi->fh, buf, size, offset);
 
230
    if(res == -1)
 
231
        res = -errno;
 
232
 
 
233
    return res;
 
234
}
 
235
 
 
236
static int xmp_write(const char *path, const char *buf, size_t size,
 
237
                     off_t offset, struct fuse_file_info *fi)
 
238
{
 
239
    int res;
 
240
 
 
241
    (void) path;
 
242
    res = pwrite(fi->fh, buf, size, offset);
 
243
    if(res == -1)
 
244
        res = -errno;
 
245
 
 
246
    return res;
 
247
}
 
248
 
 
249
static int xmp_statfs(const char *path, struct statfs *stbuf)
 
250
{
 
251
    int res;
 
252
 
 
253
    res = statfs(path, stbuf);
 
254
    if(res == -1)
 
255
        return -errno;
 
256
 
 
257
    return 0;
 
258
}
 
259
 
 
260
static int xmp_release(const char *path, struct fuse_file_info *fi)
 
261
{
 
262
    (void) path;
 
263
    close(fi->fh);
 
264
 
 
265
    return 0;
 
266
}
 
267
 
 
268
static int xmp_fsync(const char *path, int isdatasync,
 
269
                     struct fuse_file_info *fi)
 
270
{
 
271
    int res;
 
272
    (void) path;
 
273
 
 
274
    if (isdatasync)
 
275
        res = fdatasync(fi->fh);
 
276
    else
 
277
        res = fsync(fi->fh);
 
278
    if(res == -1)
 
279
        return -errno;
 
280
 
 
281
    return 0;
 
282
}
 
283
 
 
284
#ifdef HAVE_SETXATTR
 
285
/* xattr operations are optional and can safely be left unimplemented */
 
286
static int xmp_setxattr(const char *path, const char *name, const char *value,
 
287
                        size_t size, int flags)
 
288
{
 
289
    int res = lsetxattr(path, name, value, size, flags);
 
290
    if(res == -1)
 
291
        return -errno;
 
292
    return 0;
 
293
}
 
294
 
 
295
static int xmp_getxattr(const char *path, const char *name, char *value,
 
296
                    size_t size)
 
297
{
 
298
    int res = lgetxattr(path, name, value, size);
 
299
    if(res == -1)
 
300
        return -errno;
 
301
    return res;
 
302
}
 
303
 
 
304
static int xmp_listxattr(const char *path, char *list, size_t size)
 
305
{
 
306
    int res = llistxattr(path, list, size);
 
307
    if(res == -1)
 
308
        return -errno;
 
309
    return res;
 
310
}
 
311
 
 
312
static int xmp_removexattr(const char *path, const char *name)
 
313
{
 
314
    int res = lremovexattr(path, name);
 
315
    if(res == -1)
 
316
        return -errno;
 
317
    return 0;
 
318
}
 
319
#endif /* HAVE_SETXATTR */
 
320
 
 
321
static struct fuse_operations xmp_oper = {
 
322
    .getattr    = xmp_getattr,
 
323
    .readlink   = xmp_readlink,
 
324
    .opendir    = xmp_opendir,
 
325
    .readdir    = xmp_readdir,
 
326
    .releasedir = xmp_releasedir,
 
327
    .mknod      = xmp_mknod,
 
328
    .mkdir      = xmp_mkdir,
 
329
    .symlink    = xmp_symlink,
 
330
    .unlink     = xmp_unlink,
 
331
    .rmdir      = xmp_rmdir,
 
332
    .rename     = xmp_rename,
 
333
    .link       = xmp_link,
 
334
    .chmod      = xmp_chmod,
 
335
    .chown      = xmp_chown,
 
336
    .truncate   = xmp_truncate,
 
337
    .utime      = xmp_utime,
 
338
    .open       = xmp_open,
 
339
    .read       = xmp_read,
 
340
    .write      = xmp_write,
 
341
    .statfs     = xmp_statfs,
 
342
    .release    = xmp_release,
 
343
    .fsync      = xmp_fsync,
 
344
#ifdef HAVE_SETXATTR
 
345
    .setxattr   = xmp_setxattr,
 
346
    .getxattr   = xmp_getxattr,
 
347
    .listxattr  = xmp_listxattr,
 
348
    .removexattr= xmp_removexattr,
 
349
#endif
 
350
};
 
351
 
 
352
int main(int argc, char *argv[])
 
353
{
 
354
    return fuse_main(argc, argv, &xmp_oper);
 
355
}