1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
4 
5   This program can be distributed under the terms of the GNU LGPLv2.
6   See the file COPYING.LIB.
7 */
8 
9 /** @file
10  *
11  * This file defines the library interface of FUSE
12  *
13  * IMPORTANT: you should define FUSE_USE_VERSION before including this
14  * header.  To use the newest API define it to 26 (recommended for any
15  * new application), to use the old API define it to 21 (default) 22
16  * or 25, to use the even older 1.X API define it to 11.
17  */
18 
19 /// D usage note: See `FuseCompat` at the bottom of this file for a
20 /// D-friendly way to use the FUSE API in a backwards-compatible
21 /// manner.
22 
23 module c.fuse.fuse;
24 
25 public import c.fuse.fuse_common;
26 
27 import core.sys.posix.fcntl;
28 import core.sys.posix.sys.statvfs;
29 import core.sys.posix.fcntl;
30 import core.sys.posix.time;
31 import core.sys.posix.utime;
32 
33 import std.bitmanip;
34 import std.stdint;
35 
36 extern (C):
37 
38 struct fuse;
39 struct fuse_cmd;
40 
41 /** Function to add an entry in a readdir() operation
42  *
43  * @param buf the buffer passed to the readdir() operation
44  * @param name the file name of the directory entry
45  * @param stat file attributes, can be NULL
46  * @param off offset of the next entry or zero
47  * @return 1 if buffer is full, zero otherwise
48  */
49 alias fuse_fill_dir_t =
50     int function(void *buf, char *name, stat_t *stbuf, off_t off) nothrow @nogc;
51 
52 /* Used by deprecated getdir() method */
53 struct fuse_dirhandle;
54 alias fuse_dirh_t = fuse_dirhandle*;
55 alias fuse_dirfil_t =
56     int function(fuse_dirh_t, const char *name, int type, ino_t ino);
57 
58 /**
59  * The file system operations:
60  *
61  * Most of these should work very similarly to the well known UNIX
62  * file system operations.  A major exception is that instead of
63  * returning an error in 'errno', the operation should return the
64  * negated error value (-errno) directly.
65  *
66  * All methods are optional, but some are essential for a useful
67  * filesystem (e.g. getattr).  Open, flush, release, fsync, opendir,
68  * releasedir, fsyncdir, access, create, ftruncate, fgetattr, lock,
69  * init and destroy are special purpose methods, without which a full
70  * featured filesystem can still be implemented.
71  *
72  * Almost all operations take a path which can be of any length.
73  *
74  * Changed in fuse 2.8.0 (regardless of API version)
75  * Previously, paths were limited to a length of PATH_MAX.
76  *
77  * See http://fuse.sourceforge.net/wiki/ for more information.  There
78  * is also a snapshot of the relevant wiki pages in the doc/ folder.
79  */
80 struct fuse_operations
81 {
82 	/** Get file attributes.
83 	 *
84 	 * Similar to stat().  The 'st_dev' and 'st_blksize' fields are
85 	 * ignored.	 The 'st_ino' field is ignored except if the 'use_ino'
86 	 * mount option is given.
87 	 */
88     int function(const char*, stat_t*) getattr;
89 
90 	/** Read the target of a symbolic link
91 	 *
92 	 * The buffer should be filled with a null terminated string.  The
93 	 * buffer size argument includes the space for the terminating
94 	 * null character.	If the linkname is too long to fit in the
95 	 * buffer, it should be truncated.	The return value should be 0
96 	 * for success.
97 	 */
98     int function(const char*, char *, size_t) readlink;
99 
100 	/* Deprecated, use readdir() instead */
101     int function(const char*, fuse_dirh_t, fuse_dirfil_t) getdir;
102 
103 	/** Create a file node
104 	 *
105 	 * This is called for creation of all non-directory, non-symlink
106 	 * nodes.  If the filesystem defines a create() method, then for
107 	 * regular files that will be called instead.
108 	 */
109     int function(const char*, mode_t, dev_t) mknod;
110 
111 	/** Create a directory 
112 	 *
113 	 * Note that the mode argument may not have the type specification
114 	 * bits set, i.e. S_ISDIR(mode) can be false.  To obtain the
115 	 * correct directory type bits use  mode|S_IFDIR
116 	 * */
117     int function(const char*, mode_t) mkdir;
118 
119 	/** Remove a file */
120     int function(const char*) unlink;
121 
122 	/** Remove a directory */
123     int function(const char*) rmdir;
124 
125 	/** Create a symbolic link */
126     int function(const char*, char*) symlink;
127 
128 	/** Rename a file */
129     int function(const char*, const char*) rename;
130 
131 	/** Create a hard link to a file */
132     int function(const char*, char*) link;
133 
134 	/** Change the permission bits of a file */
135     int function(const char*, mode_t) chmod;
136 
137 	/** Change the owner and group of a file */
138     int function(const char*, uid_t, gid_t) chown;
139 
140 	/** Change the size of a file */
141     int function(const char*, off_t) truncate;
142 
143 	/** Change the access and/or modification times of a file
144 	 *
145 	 * Deprecated, use utimens() instead.
146 	 */
147     int function(const char*, utimbuf *) utime;
148 
149 	/** File open operation
150 	 *
151 	 * No creation (O_CREAT, O_EXCL) and by default also no
152 	 * truncation (O_TRUNC) flags will be passed to open(). If an
153 	 * application specifies O_TRUNC, fuse first calls truncate()
154 	 * and then open(). Only if 'atomic_o_trunc' has been
155 	 * specified and kernel version is 2.6.24 or later, O_TRUNC is
156 	 * passed on to open.
157 	 *
158 	 * Unless the 'default_permissions' mount option is given,
159 	 * open should check if the operation is permitted for the
160 	 * given flags. Optionally open may also return an arbitrary
161 	 * filehandle in the fuse_file_info structure, which will be
162 	 * passed to all file operations.
163 	 *
164 	 * Changed in version 2.2
165 	 */
166     int function(const char*, fuse_file_info *) open;
167 
168 	/** Read data from an open file
169 	 *
170 	 * Read should return exactly the number of bytes requested except
171 	 * on EOF or error, otherwise the rest of the data will be
172 	 * substituted with zeroes.	 An exception to this is when the
173 	 * 'direct_io' mount option is specified, in which case the return
174 	 * value of the read system call will reflect the return value of
175 	 * this operation.
176 	 *
177 	 * Changed in version 2.2
178 	 */
179     int function(const char*, char*, size_t, off_t, fuse_file_info*) read;
180 
181 	/** Write data to an open file
182 	 *
183 	 * Write should return exactly the number of bytes requested
184 	 * except on error.	 An exception to this is when the 'direct_io'
185 	 * mount option is specified (see read operation).
186 	 *
187 	 * Changed in version 2.2
188 	 */
189     int function(const char*, char*, size_t, off_t, fuse_file_info*) write;
190 
191 	/** Get file system statistics
192 	 *
193 	 * The 'f_frsize', 'f_favail', 'f_fsid' and 'f_flag' fields are ignored
194 	 *
195 	 * Replaced 'struct statfs' parameter with 'struct statvfs' in
196 	 * version 2.5
197 	 */
198     int function(const char*, statvfs_t*) statfs;
199 
200 	/** Possibly flush cached data
201 	 *
202 	 * BIG NOTE: This is not equivalent to fsync().  It's not a
203 	 * request to sync dirty data.
204 	 *
205 	 * Flush is called on each close() of a file descriptor.  So if a
206 	 * filesystem wants to return write errors in close() and the file
207 	 * has cached dirty data, this is a good place to write back data
208 	 * and return any errors.  Since many applications ignore close()
209 	 * errors this is not always useful.
210 	 *
211 	 * NOTE: The flush() method may be called more than once for each
212 	 * open().	This happens if more than one file descriptor refers
213 	 * to an opened file due to dup(), dup2() or fork() calls.	It is
214 	 * not possible to determine if a flush is final, so each flush
215 	 * should be treated equally.  Multiple write-flush sequences are
216 	 * relatively rare, so this shouldn't be a problem.
217 	 *
218 	 * Filesystems shouldn't assume that flush will always be called
219 	 * after some writes, or that if will be called at all.
220 	 *
221 	 * Changed in version 2.2
222 	 */
223     int function(const char*, fuse_file_info*) flush;
224 
225 	/** Release an open file
226 	 *
227 	 * Release is called when there are no more references to an open
228 	 * file: all file descriptors are closed and all memory mappings
229 	 * are unmapped.
230 	 *
231 	 * For every open() call there will be exactly one release() call
232 	 * with the same flags and file descriptor.	 It is possible to
233 	 * have a file opened more than once, in which case only the last
234 	 * release will mean, that no more reads/writes will happen on the
235 	 * file.  The return value of release is ignored.
236 	 *
237 	 * Changed in version 2.2
238 	 */
239     int function(const char*, fuse_file_info*) release;
240 
241 	/** Synchronize file contents
242 	 *
243 	 * If the datasync parameter is non-zero, then only the user data
244 	 * should be flushed, not the meta data.
245 	 *
246 	 * Changed in version 2.2
247 	 */
248     int function(const char*, int, fuse_file_info*) fsync;
249 
250 	/** Set extended attributes */
251     int function(const char*, char*, char*, size_t, int) setxattr;
252 
253 	/** Get extended attributes */
254     int function(const char*, char*, char*, size_t) getxattr;
255 
256 	/** List extended attributes */
257     int function(const char*, char*, size_t) listxattr;
258 
259 	/** Remove extended attributes */
260     int function(const char*, char*) removexattr;
261 
262 	/** Open directory
263 	 *
264 	 * Unless the 'default_permissions' mount option is given,
265 	 * this method should check if opendir is permitted for this
266 	 * directory. Optionally opendir may also return an arbitrary
267 	 * filehandle in the fuse_file_info structure, which will be
268 	 * passed to readdir, releasedir and fsyncdir.
269 	 *
270 	 * Introduced in version 2.3
271 	 */
272     int function(const char*, fuse_file_info*) opendir;
273 
274 	/** Read directory
275 	 *
276 	 * This supersedes the old getdir() interface.  New applications
277 	 * should use this.
278 	 *
279 	 * The filesystem may choose between two modes of operation:
280 	 *
281 	 * 1) The readdir implementation ignores the offset parameter, and
282 	 * passes zero to the filler function's offset.  The filler
283 	 * function will not return '1' (unless an error happens), so the
284 	 * whole directory is read in a single readdir operation.  This
285 	 * works just like the old getdir() method.
286 	 *
287 	 * 2) The readdir implementation keeps track of the offsets of the
288 	 * directory entries.  It uses the offset parameter and always
289 	 * passes non-zero offset to the filler function.  When the buffer
290 	 * is full (or an error happens) the filler function will return
291 	 * '1'.
292 	 *
293 	 * Introduced in version 2.3
294 	 */
295     int function(const char*, void*, fuse_fill_dir_t, off_t,
296             fuse_file_info*) readdir;
297 
298 	/** Release directory
299 	 *
300 	 * Introduced in version 2.3
301 	 */
302     int function(const char*, fuse_file_info*) releasedir;
303 
304 	/** Synchronize directory contents
305 	 *
306 	 * If the datasync parameter is non-zero, then only the user data
307 	 * should be flushed, not the meta data
308 	 *
309 	 * Introduced in version 2.3
310 	 */
311     int function(const char*, int, fuse_file_info*) fsyncdir;
312 
313 	/**
314 	 * Initialize filesystem
315 	 *
316 	 * The return value will passed in the private_data field of
317 	 * fuse_context to all file operations and as a parameter to the
318 	 * destroy() method.
319 	 *
320 	 * Introduced in version 2.3
321 	 * Changed in version 2.6
322 	 */
323     void* function(fuse_conn_info* conn) init;
324 
325 	/**
326 	 * Clean up filesystem
327 	 *
328 	 * Called on filesystem exit.
329 	 *
330 	 * Introduced in version 2.3
331 	 */
332     void function(void*) destroy;
333 
334 	/**
335 	 * Check file access permissions
336 	 *
337 	 * This will be called for the access() system call.  If the
338 	 * 'default_permissions' mount option is given, this method is not
339 	 * called.
340 	 *
341 	 * This method is not called under Linux kernel versions 2.4.x
342 	 *
343 	 * Introduced in version 2.5
344 	 */
345     int function(const char*, int) access;
346 
347 	/**
348 	 * Create and open a file
349 	 *
350 	 * If the file does not exist, first create it with the specified
351 	 * mode, and then open it.
352 	 *
353 	 * If this method is not implemented or under Linux kernel
354 	 * versions earlier than 2.6.15, the mknod() and open() methods
355 	 * will be called instead.
356 	 *
357 	 * Introduced in version 2.5
358 	 */
359     int function(const char*, mode_t, fuse_file_info*) create;
360 
361 	/**
362 	 * Change the size of an open file
363 	 *
364 	 * This method is called instead of the truncate() method if the
365 	 * truncation was invoked from an ftruncate() system call.
366 	 *
367 	 * If this method is not implemented or under Linux kernel
368 	 * versions earlier than 2.6.15, the truncate() method will be
369 	 * called instead.
370 	 *
371 	 * Introduced in version 2.5
372 	 */
373     int function(const char*, off_t, fuse_file_info*)  ftruncate;
374 
375 	/**
376 	 * Get attributes from an open file
377 	 *
378 	 * This method is called instead of the getattr() method if the
379 	 * file information is available.
380 	 *
381 	 * Currently this is only called after the create() method if that
382 	 * is implemented (see above).  Later it may be called for
383 	 * invocations of fstat() too.
384 	 *
385 	 * Introduced in version 2.5
386 	 */
387     int function(const char*, stat_t*, fuse_file_info*) fgetattr;
388 
389 	/**
390 	 * Perform POSIX file locking operation
391 	 *
392 	 * The cmd argument will be either F_GETLK, F_SETLK or F_SETLKW.
393 	 *
394 	 * For the meaning of fields in 'struct flock' see the man page
395 	 * for fcntl(2).  The l_whence field will always be set to
396 	 * SEEK_SET.
397 	 *
398 	 * For checking lock ownership, the 'fuse_file_info->owner'
399 	 * argument must be used.
400 	 *
401 	 * For F_GETLK operation, the library will first check currently
402 	 * held locks, and if a conflicting lock is found it will return
403 	 * information without calling this method.	 This ensures, that
404 	 * for local locks the l_pid field is correctly filled in.	The
405 	 * results may not be accurate in case of race conditions and in
406 	 * the presence of hard links, but it's unlikely that an
407 	 * application would rely on accurate GETLK results in these
408 	 * cases.  If a conflicting lock is not found, this method will be
409 	 * called, and the filesystem may fill out l_pid by a meaningful
410 	 * value, or it may leave this field zero.
411 	 *
412 	 * For F_SETLK and F_SETLKW the l_pid field will be set to the pid
413 	 * of the process performing the locking operation.
414 	 *
415 	 * Note: if this method is not implemented, the kernel will still
416 	 * allow file locking to work locally.  Hence it is only
417 	 * interesting for network filesystems and similar.
418 	 *
419 	 * Introduced in version 2.6
420 	 */
421     int function(const char*, fuse_file_info*, int cmd, .flock*) lock;
422 
423 	/**
424 	 * Change the access and modification times of a file with
425 	 * nanosecond resolution
426 	 *
427 	 * This supersedes the old utime() interface.  New applications
428 	 * should use this.
429 	 *
430 	 * See the utimensat(2) man page for details.
431 	 *
432 	 * Introduced in version 2.6
433 	 */
434     int function(const char*, const ref timespec[2]) utimens;
435 
436 	/**
437 	 * Map block index within file to block index within device
438 	 *
439 	 * Note: This makes sense only for block device backed filesystems
440 	 * mounted with the 'blkdev' option
441 	 *
442 	 * Introduced in version 2.6
443 	 */
444     int function(const char*, size_t, uint64_t*) bmap;
445 
446     mixin(bitfields!(
447         /**
448          * Flag indicating that the filesystem can accept a NULL path
449          * as the first argument for the following operations:
450          *
451          * read, write, flush, release, fsync, readdir, releasedir,
452          * fsyncdir, ftruncate, fgetattr, lock, ioctl and poll
453          *
454          * If this flag is set these operations continue to work on
455          * unlinked files even if "-ohard_remove" option was specified.
456          */
457         uint, q{flag_nullpath_ok}, 1,
458 
459         /**
460          * Flag indicating that the path need not be calculated for
461          * the following operations:
462          *
463          * read, write, flush, release, fsync, readdir, releasedir,
464          * fsyncdir, ftruncate, fgetattr, lock, ioctl and poll
465          *
466          * Closely related to flag_nullpath_ok, but if this flag is
467          * set then the path will not be calculaged even if the file
468          * wasn't unlinked.  However the path can still be non-NULL if
469          * it needs to be calculated for some other reason.
470          */
471         uint, q{flag_nopath}, 1,
472 
473         /**
474          * Flag indicating that the filesystem accepts special
475          * UTIME_NOW and UTIME_OMIT values in its utimens operation.
476          */
477         uint, q{flag_utime_omit_ok}, 1,
478 
479         /**
480          * Reserved flags, don't set
481          */
482         uint, q{flag_reserved}, 29,
483     ));
484 
485 	/**
486 	 * Ioctl
487 	 *
488 	 * flags will have FUSE_IOCTL_COMPAT set for 32bit ioctls in
489 	 * 64bit environment.  The size and direction of data is
490 	 * determined by _IOC_*() decoding of cmd.  For _IOC_NONE,
491 	 * data will be NULL, for _IOC_WRITE data is out area, for
492 	 * _IOC_READ in area and if both are set in/out area.  In all
493 	 * non-NULL cases, the area is of _IOC_SIZE(cmd) bytes.
494 	 *
495 	 * If flags has FUSE_IOCTL_DIR then the fuse_file_info refers to a
496 	 * directory file handle.
497 	 *
498 	 * Introduced in version 2.8
499 	 */
500     int function(const char*, int, void*, fuse_file_info*, uint, void*)
501         ioctl;
502 
503 	/**
504 	 * Poll for IO readiness events
505 	 *
506 	 * Note: If ph is non-NULL, the client should notify
507 	 * when IO readiness events occur by calling
508 	 * fuse_notify_poll() with the specified ph.
509 	 *
510 	 * Regardless of the number of times poll with a non-NULL ph
511 	 * is received, single notification is enough to clear all.
512 	 * Notifying more times incurs overhead but doesn't harm
513 	 * correctness.
514 	 *
515 	 * The callee is responsible for destroying ph with
516 	 * fuse_pollhandle_destroy() when no longer in use.
517 	 *
518 	 * Introduced in version 2.8
519 	 */
520     int function(const char*, fuse_file_info*, fuse_pollhandle*, uint*)
521         poll;
522 
523 	/** Write contents of buffer to an open file
524 	 *
525 	 * Similar to the write() method, but data is supplied in a
526 	 * generic buffer.  Use fuse_buf_copy() to transfer data to
527 	 * the destination.
528 	 *
529 	 * Introduced in version 2.9
530 	 */
531     int function(const char*, fuse_bufvec*, off_t, fuse_file_info*) write_buf;
532 
533 	/** Store data from an open file in a buffer
534 	 *
535 	 * Similar to the read() method, but data is stored and
536 	 * returned in a generic buffer.
537 	 *
538 	 * No actual copying of data has to take place, the source
539 	 * file descriptor may simply be stored in the buffer for
540 	 * later data transfer.
541 	 *
542 	 * The buffer must be allocated dynamically and stored at the
543 	 * location pointed to by bufp.  If the buffer contains memory
544 	 * regions, they too must be allocated using malloc().  The
545 	 * allocated memory will be freed by the caller.
546 	 *
547 	 * Introduced in version 2.9
548 	 */
549 	int function(const char*, fuse_bufvec** bufp,
550 			 size_t size, off_t off, fuse_file_info*) read_buf;
551 
552 	/**
553 	 * Perform BSD file locking operation
554 	 *
555 	 * The op argument will be either LOCK_SH, LOCK_EX or LOCK_UN
556 	 *
557 	 * Nonblocking requests will be indicated by ORing LOCK_NB to
558 	 * the above operations
559 	 *
560 	 * For more information see the flock(2) manual page.
561 	 *
562 	 * Additionally fi->owner will be set to a value unique to
563 	 * this open file.  This same value will be supplied to
564 	 * ->release() when the file is released.
565 	 *
566 	 * Note: if this method is not implemented, the kernel will still
567 	 * allow file locking to work locally.  Hence it is only
568 	 * interesting for network filesystems and similar.
569 	 *
570 	 * Introduced in version 2.9
571 	 */
572 	int function(const char*, fuse_file_info*, int op) flock;
573 
574 	/**
575 	 * Allocates space for an open file
576 	 *
577 	 * This function ensures that required space is allocated for specified
578 	 * file.  If this function returns success then any subsequent write
579 	 * request to specified range is guaranteed not to fail because of lack
580 	 * of space on the file system media.
581 	 *
582 	 * Introduced in version 2.9.1
583 	 */
584 	int function(const char*, int, off_t, off_t,
585 			  fuse_file_info*) fallocate;
586 }
587 
588 /** Extra context that may be needed by some filesystems
589  *
590  * The uid, gid and pid fields are not filled in case of a writepage
591  * operation.
592  */
593 struct fuse_context
594 {
595 	/** Pointer to the fuse object */
596 	.fuse *fuse;
597 
598 	/** User ID of the calling process */
599 	uid_t uid;
600 
601 	/** Group ID of the calling process */
602 	gid_t gid;
603 
604 	/** Thread ID of the calling process */
605 	pid_t pid;
606 
607 	/** Private filesystem data */
608 	void *private_data;
609 
610 	/** Umask of the calling process (introduced in version 2.8) */
611 	mode_t umask;
612 }
613 
614 /**
615  * Main function of FUSE.
616  *
617  * This is for the lazy.  This is all that has to be called from the
618  * main() function.
619  *
620  * This function does the following:
621  *   - parses command line options (-d -s and -h)
622  *   - passes relevant mount options to the fuse_mount()
623  *   - installs signal handlers for INT, HUP, TERM and PIPE
624  *   - registers an exit handler to unmount the filesystem on program exit
625  *   - creates a fuse handle
626  *   - registers the operations
627  *   - calls either the single-threaded or the multi-threaded event loop
628  *
629  * Note: this is currently implemented as a macro.
630  *
631  * @param argc the argument counter passed to the main() function
632  * @param argv the argument vector passed to the main() function
633  * @param op the file system operation
634  * @param user_data user data supplied in the context during the init() method
635  * @return 0 on success, nonzero on failure
636  */
637 /*
638   int fuse_main(int argc, char *argv[], const struct fuse_operations *op,
639   void *user_data);
640 */
641 int fuse_main()(int argc, char** argv, fuse_operations* op, void* user_data)
642 {
643     return fuse_main_real(argc, argv, op, fuse_operations.sizeof, user_data);
644 }
645 
646 /* ----------------------------------------------------------- *
647  * More detailed API					       *
648  * ----------------------------------------------------------- */
649 
650 /**
651  * Create a new FUSE filesystem.
652  *
653  * @param ch the communication channel
654  * @param args argument vector
655  * @param op the filesystem operations
656  * @param op_size the size of the fuse_operations structure
657  * @param user_data user data supplied in the context during the init() method
658  * @return the created FUSE handle
659  */
660 fuse *fuse_new(fuse_chan* ch, fuse_args* args,
661 		      const fuse_operations* op, size_t op_size,
662 		      void *user_data);
663 
664 /**
665  * Destroy the FUSE handle.
666  *
667  * The communication channel attached to the handle is also destroyed.
668  *
669  * NOTE: This function does not unmount the filesystem.	 If this is
670  * needed, call fuse_unmount() before calling this function.
671  *
672  * @param f the FUSE handle
673  */
674 void fuse_destroy(fuse* f);
675 
676 /**
677  * FUSE event loop.
678  *
679  * Requests from the kernel are processed, and the appropriate
680  * operations are called.
681  *
682  * @param f the FUSE handle
683  * @return 0 if no error occurred, -1 otherwise
684  */
685 int fuse_loop(fuse* f);
686 
687 /**
688  * Exit from event loop
689  *
690  * @param f the FUSE handle
691  */
692 void fuse_exit(fuse* f);
693 
694 /**
695  * FUSE event loop with multiple threads
696  *
697  * Requests from the kernel are processed, and the appropriate
698  * operations are called.  Request are processed in parallel by
699  * distributing them between multiple threads.
700  *
701  * Calling this function requires the pthreads library to be linked to
702  * the application.
703  *
704  * @param f the FUSE handle
705  * @return 0 if no error occurred, -1 otherwise
706  */
707 int fuse_loop_mt(fuse* f);
708 
709 /**
710  * Get the current context
711  *
712  * The context is only valid for the duration of a filesystem
713  * operation, and thus must not be stored and used later.
714  *
715  * @return the context
716  */
717 fuse_context* fuse_get_context();
718 
719 /**
720  * Get the current supplementary group IDs for the current request
721  *
722  * Similar to the getgroups(2) system call, except the return value is
723  * always the total number of group IDs, even if it is larger than the
724  * specified size.
725  *
726  * The current fuse kernel module in linux (as of 2.6.30) doesn't pass
727  * the group list to userspace, hence this function needs to parse
728  * "/proc/$TID/task/$TID/status" to get the group IDs.
729  *
730  * This feature may not be supported on all operating systems.  In
731  * such a case this function will return -ENOSYS.
732  *
733  * @param size size of given array
734  * @param list array of group IDs to be filled in
735  * @return the total number of supplementary group IDs or -errno on failure
736  */
737 int fuse_getgroups(int size, gid_t* list);
738 
739 /**
740  * Check if the current request has already been interrupted
741  *
742  * @return 1 if the request has been interrupted, 0 otherwise
743  */
744 int fuse_interrupted();
745 
746 /**
747  * Obsolete, doesn't do anything
748  *
749  * @return -EINVAL
750  */
751 int fuse_invalidate(fuse* f, const char* path);
752 
753 /* Deprecated, don't use */
754 int fuse_is_lib_option(const char* opt);
755 
756 /**
757  * The real main function
758  *
759  * Do not call this directly, use fuse_main()
760  */
761 int fuse_main_real(int argc, char** argv, const fuse_operations *op,
762 		   size_t op_size, void *user_data);
763 
764 /**
765  * Start the cleanup thread when using option "remember".
766  *
767  * This is done automatically by fuse_loop_mt()
768  * @param fuse struct fuse pointer for fuse instance
769  * @return 0 on success and -1 on error
770  */
771 int fuse_start_cleanup_thread(fuse* fuse);
772 
773 /**
774  * Stop the cleanup thread when using option "remember".
775  *
776  * This is done automatically by fuse_loop_mt()
777  * @param fuse struct fuse pointer for fuse instance
778  */
779 void fuse_stop_cleanup_thread(fuse* fuse);
780 
781 /**
782  * Iterate over cache removing stale entries
783  * use in conjunction with "-oremember"
784  *
785  * NOTE: This is already done for the standard sessions
786  *
787  * @param fuse struct fuse pointer for fuse instance
788  * @return the number of seconds until the next cleanup
789  */
790 int fuse_clean_cache(fuse* fuse);
791 
792 /*
793  * Stacking API
794  */
795 
796 /**
797  * Fuse filesystem object
798  *
799  * This is opaque object represents a filesystem layer
800  */
801 struct fuse_fs;
802 
803 /*
804  * These functions call the relevant filesystem operation, and return
805  * the result.
806  *
807  * If the operation is not defined, they return -ENOSYS, with the
808  * exception of fuse_fs_open, fuse_fs_release, fuse_fs_opendir,
809  * fuse_fs_releasedir and fuse_fs_statfs, which return 0.
810  */
811 
812 int fuse_fs_getattr(fuse_fs* fs, const char* path, stat_t* buf);
813 int fuse_fs_fgetattr(fuse_fs* fs, const char* path, stat_t* buf,
814 		     fuse_file_info* fi);
815 int fuse_fs_rename(fuse_fs* fs, const char* oldpath,
816 		   const char* newpath);
817 int fuse_fs_unlink(fuse_fs* fs, const char* path);
818 int fuse_fs_rmdir(fuse_fs* fs, const char* path);
819 int fuse_fs_symlink(fuse_fs* fs, const char* linkname,
820 		    const char* path);
821 int fuse_fs_link(fuse_fs* fs, const char* oldpath, const char* newpath);
822 int fuse_fs_release(fuse_fs* fs,	 const char* path,
823 		    fuse_file_info* fi);
824 int fuse_fs_open(fuse_fs* fs, const char* path,
825 		 fuse_file_info* fi);
826 int fuse_fs_read(fuse_fs* fs, const char* path, char* buf, size_t size,
827 		 off_t off, fuse_file_info* fi);
828 int fuse_fs_read_buf(fuse_fs* fs, const char* path,
829 		     fuse_bufvec* *bufp, size_t size, off_t off,
830 		     fuse_file_info* fi);
831 int fuse_fs_write(fuse_fs* fs, const char* path, const char* buf,
832 		  size_t size, off_t off, fuse_file_info* fi);
833 int fuse_fs_write_buf(fuse_fs* fs, const char* path,
834 		      fuse_bufvec* buf, off_t off,
835 		      fuse_file_info* fi);
836 int fuse_fs_fsync(fuse_fs* fs, const char* path, int datasync,
837 		  fuse_file_info* fi);
838 int fuse_fs_flush(fuse_fs* fs, const char* path,
839 		  fuse_file_info* fi);
840 int fuse_fs_statfs(fuse_fs* fs, const char* path, statvfs_t* buf);
841 int fuse_fs_opendir(fuse_fs* fs, const char* path,
842 		    fuse_file_info* fi);
843 int fuse_fs_readdir(fuse_fs* fs, const char* path, void* buf,
844 		    fuse_fill_dir_t filler, off_t off,
845 		    fuse_file_info* fi);
846 int fuse_fs_fsyncdir(fuse_fs* fs, const char* path, int datasync,
847 		     fuse_file_info* fi);
848 int fuse_fs_releasedir(fuse_fs* fs, const char* path,
849 		       fuse_file_info* fi);
850 int fuse_fs_create(fuse_fs* fs, const char* path, mode_t mode,
851 		   fuse_file_info* fi);
852 int fuse_fs_lock(fuse_fs* fs, const char* path,
853 		 fuse_file_info* fi, int cmd, flock* lock);
854 int fuse_fs_flock(fuse_fs* fs, const char* path,
855 		  fuse_file_info* fi, int op);
856 int fuse_fs_chmod(fuse_fs* fs, const char* path, mode_t mode);
857 int fuse_fs_chown(fuse_fs* fs, const char* path, uid_t uid, gid_t gid);
858 int fuse_fs_truncate(fuse_fs* fs, const char* path, off_t size);
859 int fuse_fs_ftruncate(fuse_fs* fs, const char* path, off_t size,
860 		      fuse_file_info* fi);
861 int fuse_fs_utimens(fuse_fs* fs, const char* path,
862 		    const timespec* tv);
863 int fuse_fs_access(fuse_fs* fs, const char* path, int mask);
864 int fuse_fs_readlink(fuse_fs* fs, const char* path, char* buf,
865 		     size_t len);
866 int fuse_fs_mknod(fuse_fs* fs, const char* path, mode_t mode,
867 		  dev_t rdev);
868 int fuse_fs_mkdir(fuse_fs* fs, const char* path, mode_t mode);
869 int fuse_fs_setxattr(fuse_fs* fs, const char* path, const char* name,
870 		     const char* value, size_t size, int flags);
871 int fuse_fs_getxattr(fuse_fs* fs, const char* path, const char* name,
872 		     char* value, size_t size);
873 int fuse_fs_listxattr(fuse_fs* fs, const char* path, char* list,
874 		      size_t size);
875 int fuse_fs_removexattr(fuse_fs* fs, const char* path,
876 			const char* name);
877 int fuse_fs_bmap(fuse_fs* fs, const char* path, size_t blocksize,
878 		 uint64_t* idx);
879 int fuse_fs_ioctl(fuse_fs* fs, const char* path, int cmd, void* arg,
880 		  fuse_file_info* fi, uint flags, void* data);
881 int fuse_fs_poll(fuse_fs* fs, const char* path,
882 		 fuse_file_info* fi, fuse_pollhandle* ph,
883 		 uint* reventsp);
884 int fuse_fs_fallocate(fuse_fs* fs, const char* path, int mode,
885 		 off_t offset, off_t length, fuse_file_info* fi);
886 void fuse_fs_init(fuse_fs* fs, fuse_conn_info* conn);
887 void fuse_fs_destroy(fuse_fs* fs);
888 
889 int fuse_notify_poll(fuse_pollhandle* ph);
890 
891 /**
892  * Create a new fuse filesystem object
893  *
894  * This is usually called from the factory of a fuse module to create
895  * a new instance of a filesystem.
896  *
897  * @param op the filesystem operations
898  * @param op_size the size of the fuse_operations structure
899  * @param user_data user data supplied in the context during the init() method
900  * @return a new filesystem object
901  */
902 fuse_fs* fuse_fs_new(const fuse_operations* op, size_t op_size,
903 			    void* user_data);
904 
905 struct fusemod_so;
906 
907 /**
908  * Filesystem module
909  *
910  * Filesystem modules are registered with the FUSE_REGISTER_MODULE()
911  * macro.
912  *
913  * If the "-omodules=modname:..." option is present, filesystem
914  * objects are created and pushed onto the stack with the 'factory'
915  * function.
916  */
917 struct fuse_module
918 {
919 	/**
920 	 * Name of filesystem
921 	 */
922 	const char* name;
923 
924 	/**
925 	 * Factory for creating filesystem objects
926 	 *
927 	 * The function may use and remove options from 'args' that belong
928 	 * to this module.
929 	 *
930 	 * For now the 'fs' vector always contains exactly one filesystem.
931 	 * This is the filesystem which will be below the newly created
932 	 * filesystem in the stack.
933 	 *
934 	 * @param args the command line arguments
935 	 * @param fs NULL terminated filesystem object vector
936 	 * @return the new filesystem object
937 	 */
938 	fuse_fs* function(fuse_args* args,
939 				   fuse_fs** fs) factory;
940 
941 	fuse_module* next;
942 	fusemod_so* so;
943 	int ctr;
944 }
945 
946 /**
947  * Register a filesystem module
948  *
949  * This function is used by FUSE_REGISTER_MODULE and there's usually
950  * no need to call it directly
951  */
952 void fuse_register_module(fuse_module* mod);
953 
954 /**
955  * Register filesystem module
956  *
957  * For the parameters, see description of the fields in 'struct
958  * fuse_module'
959  */
960 mixin template FUSE_REGISTER_MODULE(const char* name, alias factory)
961 {
962     shared static this()
963     {
964         static fuse_module mod =
965 			{ name, factory, null, null, 0 };
966 		fuse_register_module(&mod);
967 	}
968 }
969 
970 
971 /* ----------------------------------------------------------- *
972  * Advanced API for event handling, don't worry about this...  *
973  * ----------------------------------------------------------- */
974 
975 /* NOTE: the following functions are deprecated, and will be removed
976    from the 3.0 API.  Use the lowlevel session functions instead */
977 
978 /** Function type used to process commands */
979 alias fuse_processor_t = void function(fuse*, fuse_cmd*, void*);
980 
981 /** This is the part of fuse_main() before the event loop */
982 fuse* fuse_setup(int argc, char** argv,
983 			const fuse_operations* op, size_t op_size,
984 			char **mountpoint, int* multithreaded,
985 			void* user_data);
986 
987 /** This is the part of fuse_main() after the event loop */
988 void fuse_teardown(fuse* fuse, char* mountpoint);
989 
990 /** Read a single command.  If none are read, return NULL */
991 fuse_cmd* fuse_read_cmd(fuse* f);
992 
993 /** Process a single command */
994 void fuse_process_cmd(fuse* f, fuse_cmd* cmd);
995 
996 /** Multi threaded event loop, which calls the custom command
997     processor function */
998 int fuse_loop_mt_proc(fuse* f, fuse_processor_t proc, void* data);
999 
1000 /** Return the exited flag, which indicates if fuse_exit() has been
1001     called */
1002 int fuse_exited(fuse* f);
1003 
1004 /** This function is obsolete and implemented as a no-op */
1005 void fuse_set_getcontext_func(fuse_context* function());
1006 
1007 /** Get session from fuse object */
1008 fuse_session* fuse_get_session(fuse* f);
1009 
1010 /* ----------------------------------------------------------- *
1011  * Compatibility stuff					       *
1012  * ----------------------------------------------------------- */
1013 
1014 /**
1015    D usage note:
1016    ---
1017    static import c.fuse.fuse;
1018    mixin c.fuse.fuse.FuseCompat!25;
1019    ---
1020    Substitute 25 with desired FUSE_USE_VERSION.
1021 */
1022 
1023 mixin template FuseCompat(uint FUSE_USE_VERSION = default_FUSE_USE_VERSION)
1024 {
1025     mixin FuseCommonCompat!FUSE_USE_VERSION;
1026 
1027     static if (FUSE_USE_VERSION < 26)
1028     {
1029         import c.fuse.fuse_compat;
1030         static if (FUSE_USE_VERSION == 25)
1031         {
1032             alias fuse_new = fuse_new_compat25;
1033             alias fuse_setup = fuse_setup_compat25;
1034             alias fuse_teardown = fuse_teardown_compat22;
1035             alias fuse_operations = fuse_operations_compat25;
1036             int fuse_main()(int argc, char** argv, fuse_operations* op)
1037             {
1038                 return fuse_main_real_compat25(argc, argv, op, fuse_operations.sizeof);
1039             }
1040         }
1041         else static if (FUSE_USE_VERSION == 22)
1042         {
1043             alias fuse_new = fuse_new_compat22;
1044             alias fuse_setup = fuse_setup_compat22;
1045             alias fuse_teardown = fuse_teardown_compat22;
1046             alias fuse_operations = fuse_operations_compat22;
1047             alias fuse_file_info = fuse_file_info_compat;
1048             int fuse_main()(int argc, char** argv, fuse_operations* op)
1049             {
1050                 return fuse_main_real_compat22(argc, argv, op, fuse_operations.sizeof);
1051             }
1052         }
1053         else static if (FUSE_USE_VERSION == 24)
1054             static assert(false, "Compatibility with high-level API version 24 not supported");
1055         else
1056         {
1057             alias fuse_dirfil_t = fuse_dirfil_t_compat;
1058             alias __fuse_read_cmd = fuse_read_cmd;
1059             alias __fuse_process_cmd = fuse_process_cmd;
1060             alias __fuse_loop_mt = fuse_loop_mt_proc;
1061             static if (FUSE_USE_VERSION == 21)
1062             {
1063                 alias fuse_operations = fuse_operations_compat2;
1064                 alias fuse_main = fuse_main_compat2;
1065                 alias fuse_new = fuse_new_compat2;
1066                 alias __fuse_setup = fuse_setup_compat2;
1067                 alias __fuse_teardown = fuse_teardown_compat22;
1068                 alias __fuse_exited = fuse_exited;
1069                 alias __fuse_set_getcontext_func = fuse_set_getcontext_func;
1070             }
1071             else
1072             {
1073                 alias fuse_statfs = fuse_statfs_compat1;
1074                 alias fuse_operations = fuse_operations_compat1;
1075                 alias fuse_main = fuse_main_compat1;
1076                 alias fuse_new = fuse_new_compat1;
1077                 enum FUSE_DEBUG = FUSE_DEBUG_COMPAT1;
1078             }
1079         }
1080     }
1081 }