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

« back to all changes in this revision

Viewing changes to include/fuse.h

  • Committer: James Westby
  • Date: 2008-05-16 12:57:40 UTC
  • Revision ID: jw+debian@jameswestby.net-20080516125740-fn2iqsxtfd3olmib
Tags: upstream-debian-2.2.1
Import upstream from fuse_2.2.1.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 LGPL.
 
6
    See the file COPYING.LIB.
 
7
*/
 
8
 
 
9
#ifndef _FUSE_H_
 
10
#define _FUSE_H_
 
11
 
 
12
/* This file defines the library interface of FUSE */
 
13
 
 
14
/* IMPORTANT: you should define FUSE_USE_VERSION before including this
 
15
   header.  To use the new API define it to 22 (recommended for any
 
16
   new application), to use the old API define it to 21 (this is the
 
17
   default), to use the even older 1.X API define it to 11. */
 
18
 
 
19
#ifndef FUSE_USE_VERSION
 
20
#define FUSE_USE_VERSION 21
 
21
#endif
 
22
 
 
23
/** Major version of FUSE library interface */
 
24
#define FUSE_MAJOR_VERSION 2
 
25
 
 
26
/** Minor version of FUSE library interface */
 
27
#define FUSE_MINOR_VERSION 2
 
28
 
 
29
/* This interface uses 64 bit off_t */
 
30
#if _FILE_OFFSET_BITS != 64
 
31
#error Please add -D_FILE_OFFSET_BITS=64 to your compile flags!
 
32
#endif
 
33
 
 
34
#include <sys/types.h>
 
35
#include <sys/stat.h>
 
36
#include <sys/statfs.h>
 
37
#include <utime.h>
 
38
 
 
39
#ifdef __cplusplus
 
40
extern "C" {
 
41
#endif
 
42
 
 
43
/* ----------------------------------------------------------- *
 
44
 * Basic FUSE API                                              *
 
45
 * ----------------------------------------------------------- */
 
46
 
 
47
/** Handle for a FUSE filesystem */
 
48
struct fuse;
 
49
 
 
50
/** Handle for a getdir() operation */
 
51
typedef struct fuse_dirhandle *fuse_dirh_t;
 
52
 
 
53
/** Function to add an entry in a getdir() operation
 
54
 *
 
55
 * @param h the handle passed to the getdir() operation
 
56
 * @param name the file name of the directory entry
 
57
 * @param type the file type (0 if unknown)  see <dirent.h>
 
58
 * @param ino the inode number, ignored if "use_ino" mount option is
 
59
 *            not specified
 
60
 * @return 0 on success, -errno on error
 
61
 */
 
62
typedef int (*fuse_dirfil_t) (fuse_dirh_t h, const char *name, int type,
 
63
                              ino_t ino);
 
64
 
 
65
/** Information about open files */
 
66
struct fuse_file_info {
 
67
    /** Open flags.  Available in open() and release() */
 
68
    int flags;
 
69
 
 
70
    /** File handle.  May be filled in by filesystem in open().
 
71
        Available in all other file operations */
 
72
    unsigned long fh;
 
73
 
 
74
    /** In case of a write operation indicates if this was caused by a
 
75
        writepage */
 
76
    int writepage;
 
77
};
 
78
 
 
79
/**
 
80
 * The file system operations:
 
81
 *
 
82
 * Most of these should work very similarly to the well known UNIX
 
83
 * file system operations.  A major exception is that instead of
 
84
 * returning an error in 'errno', the operation should return the
 
85
 * negated error value (-errno) directly.
 
86
 *
 
87
 * All methods are optional, but some are essential for a useful
 
88
 * filesystem (e.g. getattr).  Flush, release and fsync are special
 
89
 * purpose methods, without which a full featured filesystem can still
 
90
 * be implemented.
 
91
 */
 
92
struct fuse_operations {
 
93
    /** Get file attributes.
 
94
     *
 
95
     * Similar to stat().  The 'st_dev' and 'st_blksize' fields are
 
96
     * ignored.  The 'st_ino' field is ignored except if the 'use_ino'
 
97
     * mount option is given.
 
98
     */
 
99
    int (*getattr) (const char *, struct stat *);
 
100
 
 
101
    /** Read the target of a symbolic link
 
102
     *
 
103
     * The buffer should be filled with a null terminated string.  The
 
104
     * buffer size argument includes the space for the terminating
 
105
     * null character.  If the linkname is too long to fit in the
 
106
     * buffer, it should be truncated.  The return value should be 0
 
107
     * for success.
 
108
     */
 
109
    int (*readlink) (const char *, char *, size_t);
 
110
 
 
111
    /** Read the contents of a directory
 
112
     *
 
113
     * This operation is the opendir(), readdir(), ..., closedir()
 
114
     * sequence in one call. For each directory entry the filldir
 
115
     * function should be called.
 
116
     */
 
117
    int (*getdir) (const char *, fuse_dirh_t, fuse_dirfil_t);
 
118
 
 
119
    /** Create a file node
 
120
     *
 
121
     * There is no create() operation, mknod() will be called for
 
122
     * creation of all non-directory, non-symlink nodes.
 
123
     */
 
124
    int (*mknod) (const char *, mode_t, dev_t);
 
125
 
 
126
    /** Create a directory */
 
127
    int (*mkdir) (const char *, mode_t);
 
128
 
 
129
    /** Remove a file */
 
130
    int (*unlink) (const char *);
 
131
 
 
132
    /** Remove a directory */
 
133
    int (*rmdir) (const char *);
 
134
 
 
135
    /** Create a symbolic link */
 
136
    int (*symlink) (const char *, const char *);
 
137
 
 
138
    /** Rename a file */
 
139
    int (*rename) (const char *, const char *);
 
140
 
 
141
    /** Create a hard link to a file */
 
142
    int (*link) (const char *, const char *);
 
143
 
 
144
    /** Change the permission bits of a file */
 
145
    int (*chmod) (const char *, mode_t);
 
146
 
 
147
    /** Change the owner and group of a file */
 
148
    int (*chown) (const char *, uid_t, gid_t);
 
149
 
 
150
    /** Change the size of a file */
 
151
    int (*truncate) (const char *, off_t);
 
152
 
 
153
    /** Change the access and/or modification times of a file */
 
154
    int (*utime) (const char *, struct utimbuf *);
 
155
 
 
156
    /** File open operation
 
157
     *
 
158
     * No creation, or trunctation flags (O_CREAT, O_EXCL, O_TRUNC)
 
159
     * will be passed to open().  Open should check if the operation
 
160
     * is permitted for the given flags.  Optionally open may also
 
161
     * return an arbitary filehandle in the fuse_file_info structure,
 
162
     * which will be passed to all file operations.
 
163
     */
 
164
    int (*open) (const char *, struct fuse_file_info *);
 
165
 
 
166
    /** Read data from an open file
 
167
     *
 
168
     * Read should return exactly the number of bytes requested except
 
169
     * on EOF or error, otherwise the rest of the data will be
 
170
     * substituted with zeroes.  An exception to this is when the
 
171
     * 'direct_io' mount option is specified, in which case the return
 
172
     * value of the read system call will reflect the return value of
 
173
     * this operation.
 
174
     */
 
175
    int (*read) (const char *, char *, size_t, off_t, struct fuse_file_info *);
 
176
 
 
177
    /** Write data to an open file
 
178
     *
 
179
     * Write should return exactly the number of bytes requested
 
180
     * except on error.  An exception to this is when the 'direct_io'
 
181
     * mount option is specified (see read operation).
 
182
     */
 
183
    int (*write) (const char *, const char *, size_t, off_t,
 
184
                  struct fuse_file_info *);
 
185
 
 
186
    /** Get file system statistics
 
187
     *
 
188
     * The 'f_type' and 'f_fsid' fields are ignored
 
189
     */
 
190
    int (*statfs) (const char *, struct statfs *);
 
191
 
 
192
    /** Possibly flush cached data
 
193
     *
 
194
     * BIG NOTE: This is not equivalent to fsync().  It's not a
 
195
     * request to sync dirty data.
 
196
     *
 
197
     * Flush is called on each close() of a file descriptor.  So if a
 
198
     * filesystem wants to return write errors in close() and the file
 
199
     * has cached dirty data, this is a good place to write back data
 
200
     * and return any errors.  Since many applications ignore close()
 
201
     * errors this is not always useful.
 
202
     *
 
203
     * NOTE: The flush() method may be called more than once for each
 
204
     * open().  This happens if more than one file descriptor refers
 
205
     * to an opened file due to dup(), dup2() or fork() calls.  It is
 
206
     * not possible to determine if a flush is final, so each flush
 
207
     * should be treated equally.  Multiple write-flush sequences are
 
208
     * relatively rare, so this shouldn't be a problem.
 
209
     */
 
210
    int (*flush) (const char *, struct fuse_file_info *);
 
211
 
 
212
    /** Release an open file
 
213
     *
 
214
     * Release is called when there are no more references to an open
 
215
     * file: all file descriptors are closed and all memory mappings
 
216
     * are unmapped.
 
217
     *
 
218
     * For every open() call there will be exactly one release() call
 
219
     * with the same flags and file descriptor.  It is possible to
 
220
     * have a file opened more than once, in which case only the last
 
221
     * release will mean, that no more reads/writes will happen on the
 
222
     * file.  The return value of release is ignored.
 
223
     */
 
224
    int (*release) (const char *, struct fuse_file_info *);
 
225
 
 
226
    /** Synchronize file contents
 
227
     *
 
228
     * If the datasync parameter is non-zero, then only the user data
 
229
     * should be flushed, not the meta data.
 
230
     */
 
231
    int (*fsync) (const char *, int, struct fuse_file_info *);
 
232
 
 
233
    /** Set extended attributes */
 
234
    int (*setxattr) (const char *, const char *, const char *, size_t, int);
 
235
 
 
236
    /** Get extended attributes */
 
237
    int (*getxattr) (const char *, const char *, char *, size_t);
 
238
 
 
239
    /** List extended attributes */
 
240
    int (*listxattr) (const char *, char *, size_t);
 
241
 
 
242
    /** Remove extended attributes */
 
243
    int (*removexattr) (const char *, const char *);
 
244
};
 
245
 
 
246
/** Extra context that may be needed by some filesystems
 
247
 *
 
248
 * The uid, gid and pid fields are not filled in case of a writepage
 
249
 * operation.
 
250
 */
 
251
struct fuse_context {
 
252
    /** Pointer to the fuse object */
 
253
    struct fuse *fuse;
 
254
 
 
255
    /** User ID of the calling process */
 
256
    uid_t uid;
 
257
 
 
258
    /** Group ID of the calling process */
 
259
    gid_t gid;
 
260
 
 
261
    /** Thread ID of the calling process */
 
262
    pid_t pid;
 
263
 
 
264
    /** Currently unused */
 
265
    void *private_data;
 
266
};
 
267
 
 
268
/*
 
269
 * Main function of FUSE.
 
270
 *
 
271
 * This is for the lazy.  This is all that has to be called from the
 
272
 * main() function.
 
273
 *
 
274
 * This function does the following:
 
275
 *   - parses command line options (-d -s and -h)
 
276
 *   - passes relevant mount options to the fuse_mount()
 
277
 *   - installs signal handlers for INT, HUP, TERM and PIPE
 
278
 *   - registers an exit handler to unmount the filesystem on program exit
 
279
 *   - creates a fuse handle
 
280
 *   - registers the operations
 
281
 *   - calls either the single-threaded or the multi-threaded event loop
 
282
 *
 
283
 * Note: this is currently implemented as a macro.
 
284
 *
 
285
 * @param argc the argument counter passed to the main() function
 
286
 * @param argv the argument vector passed to the main() function
 
287
 * @param op the file system operation
 
288
 * @return 0 on success, nonzero on failure
 
289
 */
 
290
/*
 
291
int fuse_main(int argc, char *argv[], const struct fuse_operations *op);
 
292
*/
 
293
#define fuse_main(argc, argv, op) \
 
294
            fuse_main_real(argc, argv, op, sizeof(*(op)))
 
295
 
 
296
/* ----------------------------------------------------------- *
 
297
 * More detailed API                                           *
 
298
 * ----------------------------------------------------------- */
 
299
 
 
300
/*
 
301
 * Create a FUSE mountpoint
 
302
 *
 
303
 * Returns a control file descriptor suitable for passing to
 
304
 * fuse_new()
 
305
 *
 
306
 * @param mountpoint the mount point path
 
307
 * @param opts a comma separated list of mount options.  Can be NULL.
 
308
 * @return the control file descriptor on success, -1 on failure
 
309
 */
 
310
int fuse_mount(const char *mountpoint, const char *opts);
 
311
 
 
312
/*
 
313
 * Umount a FUSE mountpoint
 
314
 *
 
315
 * @param mountpoint the mount point path
 
316
 */
 
317
void fuse_unmount(const char *mountpoint);
 
318
 
 
319
/**
 
320
 * Create a new FUSE filesystem.
 
321
 *
 
322
 * @param fd the control file descriptor
 
323
 * @param opts mount options to be used by the library
 
324
 * @param op the operations
 
325
 * @param op_size the size of the fuse_operations structure
 
326
 * @return the created FUSE handle
 
327
 */
 
328
struct fuse *fuse_new(int fd, const char *opts,
 
329
                      const struct fuse_operations *op, size_t op_size);
 
330
 
 
331
/**
 
332
 * Destroy the FUSE handle.
 
333
 *
 
334
 * The filesystem is not unmounted.
 
335
 *
 
336
 * @param f the FUSE handle
 
337
 */
 
338
void fuse_destroy(struct fuse *f);
 
339
 
 
340
/**
 
341
 * FUSE event loop.
 
342
 *
 
343
 * Requests from the kernel are processed, and the apropriate
 
344
 * operations are called.
 
345
 *
 
346
 * @param f the FUSE handle
 
347
 * @return 0 if no error occured, -1 otherwise
 
348
 */
 
349
int fuse_loop(struct fuse *f);
 
350
 
 
351
/**
 
352
 * Exit from event loop
 
353
 *
 
354
 * @param f the FUSE handle
 
355
 */
 
356
void fuse_exit(struct fuse *f);
 
357
 
 
358
/**
 
359
 * FUSE event loop with multiple threads
 
360
 *
 
361
 * Requests from the kernel are processed, and the apropriate
 
362
 * operations are called.  Request are processed in parallel by
 
363
 * distributing them between multiple threads.
 
364
 *
 
365
 * Calling this function requires the pthreads library to be linked to
 
366
 * the application.
 
367
 *
 
368
 * @param f the FUSE handle
 
369
 * @return 0 if no error occured, -1 otherwise
 
370
 */
 
371
int fuse_loop_mt(struct fuse *f);
 
372
 
 
373
/**
 
374
 * Get the current context
 
375
 *
 
376
 * The context is only valid for the duration of a filesystem
 
377
 * operation, and thus must not be stored and used later.
 
378
 *
 
379
 * @param f the FUSE handle
 
380
 * @return the context
 
381
 */
 
382
struct fuse_context *fuse_get_context(void);
 
383
 
 
384
/**
 
385
 * Invalidate cached data of a file.
 
386
 *
 
387
 * Useful if the 'kernel_cache' mount option is given, since in that
 
388
 * case the cache is not invalidated on file open.
 
389
 *
 
390
 * @return 0 on success or -errno on failure
 
391
 */
 
392
int fuse_invalidate(struct fuse *f, const char *path);
 
393
 
 
394
/**
 
395
 * Check whether a mount option should be passed to the kernel or the
 
396
 * library
 
397
 *
 
398
 * @param opt the option to check
 
399
 * @return 1 if it is a library option, 0 otherwise
 
400
 */
 
401
int fuse_is_lib_option(const char *opt);
 
402
 
 
403
/**
 
404
 * The real main function
 
405
 *
 
406
 * Do not call this directly, use fuse_main()
 
407
 */
 
408
int fuse_main_real(int argc, char *argv[], const struct fuse_operations *op,
 
409
                   size_t op_size);
 
410
 
 
411
/* ----------------------------------------------------------- *
 
412
 * Advanced API for event handling, don't worry about this...  *
 
413
 * ----------------------------------------------------------- */
 
414
 
 
415
/** Structure containing a raw command */
 
416
struct fuse_cmd;
 
417
 
 
418
/** Function type used to process commands */
 
419
typedef void (*fuse_processor_t)(struct fuse *, struct fuse_cmd *, void *);
 
420
 
 
421
/** This is the part of fuse_main() before the event loop */
 
422
struct fuse *fuse_setup(int argc, char *argv[],
 
423
                        const struct fuse_operations *op, size_t op_size,
 
424
                          char **mountpoint, int *multithreaded, int *fd);
 
425
 
 
426
/** This is the part of fuse_main() after the event loop */
 
427
void fuse_teardown(struct fuse *fuse, int fd, char *mountpoint);
 
428
 
 
429
/** Read a single command.  If none are read, return NULL */
 
430
struct fuse_cmd *fuse_read_cmd(struct fuse *f);
 
431
 
 
432
/** Process a single command */
 
433
void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd);
 
434
 
 
435
/** Multi threaded event loop, which calls the custom command
 
436
    processor function */
 
437
int fuse_loop_mt_proc(struct fuse *f, fuse_processor_t proc, void *data);
 
438
 
 
439
/** Return the exited flag, which indicates if fuse_exit() has been
 
440
    called */
 
441
int fuse_exited(struct fuse* f);
 
442
 
 
443
/** Set function which can be used to get the current context */
 
444
void fuse_set_getcontext_func(struct fuse_context *(*func)(void));
 
445
 
 
446
/* ----------------------------------------------------------- *
 
447
 * Compatibility stuff                                         *
 
448
 * ----------------------------------------------------------- */
 
449
 
 
450
#if FUSE_USE_VERSION == 21 || FUSE_USE_VERSION == 11
 
451
#  include "fuse_compat.h"
 
452
#  define fuse_dirfil_t fuse_dirfil_t_compat
 
453
#  define __fuse_read_cmd fuse_read_cmd
 
454
#  define __fuse_process_cmd fuse_process_cmd
 
455
#  define __fuse_loop_mt fuse_loop_mt_proc
 
456
#  undef fuse_main
 
457
#  undef FUSE_MINOR_VERSION
 
458
#  undef FUSE_MAJOR_VERSION
 
459
#  if FUSE_USE_VERSION == 21
 
460
#    define FUSE_MAJOR_VERSION 2
 
461
#    define FUSE_MINOR_VERSION 1
 
462
#    define fuse_operations fuse_operations_compat2
 
463
#    define fuse_main fuse_main_compat2
 
464
#    define fuse_new fuse_new_compat2
 
465
#    define __fuse_setup fuse_setup_compat2
 
466
#    define __fuse_teardown fuse_teardown
 
467
#    define __fuse_exited fuse_exited
 
468
#    define __fuse_set_getcontext_func fuse_set_getcontext_func
 
469
#  else
 
470
#    define FUSE_MAJOR_VERSION 1
 
471
#    define FUSE_MINOR_VERSION 1
 
472
#    define fuse_statfs fuse_statfs_compat1
 
473
#    define fuse_operations fuse_operations_compat1
 
474
#    define fuse_main fuse_main_compat1
 
475
#    define fuse_new fuse_new_compat1
 
476
#    define fuse_mount fuse_mount_compat1
 
477
#    define FUSE_DEBUG FUSE_DEBUG_COMPAT1
 
478
#  endif
 
479
#elif FUSE_USE_VERSION < 22
 
480
#  error Compatibility with API version other than 21 and 11 not supported
 
481
#endif
 
482
 
 
483
#ifdef __cplusplus
 
484
}
 
485
#endif
 
486
 
 
487
#endif /* _FUSE_H_ */