fuse.pas 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947
  1. {
  2. FUSE: Filesystem in Userspace -- Free Pascal bindings.
  3. Copyright (C) 2001-2007 Miklos Szeredi <[email protected]>
  4. Copyright (C) 2008 Danny Milosavljevic <[email protected]>
  5. Copyright (C) 2009 Michael A. Green <[email protected]>
  6. This program is free software; you can redistribute it and/or modify it under
  7. the terms of the GNU Lesser General Public License as published by the Free
  8. Software Foundation; either version 2 of the License, or (at your option) any
  9. later version.
  10. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12. PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. }
  17. unit fuse;
  18. {$ASSERTIONS ON}
  19. {$MACRO ON}
  20. {$MODE OBJFPC}
  21. {$PACKRECORDS C}
  22. {$ifndef FUSE_USE_VERSION}
  23. {$define FUSE_USE_VERSION := 26}
  24. {$endif}
  25. {$if (fuse_use_version < 26)}
  26. {$error Only FUSE Library 2.6 or better is supported}
  27. {$endif}
  28. {$if defined(fs32bit)}
  29. {$error FUSE expects sizeof(off_t)=8 (64-bits). Your libc does not support this}
  30. {$endif}
  31. { TODO: Add low level functions }
  32. interface
  33. uses BaseUNIX, UNIXtype;
  34. const
  35. FUSE_ROOT_ID = 1; // The node ID of the root inode
  36. { STAT Values }
  37. ST_RDONLY = 1; // Mount read-only. - f_flag
  38. ST_NOSUID = 2; // Ignore suid and sgid bits. - f_flag
  39. ST_NODEV = 4; // Disallow access to device special files.
  40. ST_NOEXEC = 8; // Disallow program execution.
  41. ST_SYNCHRONOUS = 16; // Writes are synced at once.
  42. ST_MANDLOCK = 64; // Allow mandatory locks on an FS.
  43. ST_WRITE = 128; // Write on file/directory/symlink.
  44. ST_APPEND = 256; // Append-only file.
  45. ST_IMMUTABLE = 512; // Immutable file.
  46. ST_NOATIME = 1024; // Do not update access times.
  47. ST_NODIRATIME = 2048; // Do not update directory access times.
  48. ST_RELATIME = 4096; // Update atime relative to mtime/ctime.
  49. { File Info Flags (see TFuseFileInfo)
  50. FUSE_FI_NONE - No Flags Set
  51. FUSE_FI_DIRECT_IO - Can be filled in by open, to use direct I/O on this
  52. file. Introduced in version 2.4
  53. FUSE_FI_KEEP_CACHE - Can be filled in by open, to indicate, that cached
  54. file data need not be invalidated. Introduced in
  55. version 2.4
  56. FUSE_FI_FLUSH - Indicates a flush operation. Set in flush operation,
  57. also maybe set in highlevel lock operation and
  58. lowlevel release operation. Introduced in version 2.6
  59. FUSE_FI_NONSEEKABLE - Can be filled in by open, to indicate that the file is
  60. not seekable. Introduced in version 2.9
  61. }
  62. FUSE_FI_NONE = 0;
  63. FUSE_FI_DIRECT_IO = 1;
  64. FUSE_FI_KEEP_CACHE = 2;
  65. FUSE_FI_FLUSH = 4;
  66. {$if (fuse_use_version >= 29)}
  67. FUSE_FI_NONSEEKABLE = 8
  68. {$endif}
  69. { Operation Flags (see TFuseOpereation)
  70. FUSE_OP_NONE - No Flags Set
  71. FUSE_OP_NULLPATHOK - Flag indicating, that the filesystem can accept a NULL
  72. path as the first argument for the following
  73. operations:
  74. read, write, flush, release, fsync, readdir,
  75. releasedir, fsyncdir, ftruncate, fgetattr and lock
  76. }
  77. {$if (fuse_use_version >= 28)}
  78. FUSE_OP_NONE = 0;
  79. FUSE_OP_NULLPATHOK = $1;
  80. {$endif}
  81. { Capability Flags (see TFuseConnInfo.Capable and TFuseConnInfo.Want)
  82. FUSE_CAP_NONE - No capability bits set
  83. FUSE_CAP_ASYNC_READ - filesystem supports asynchronous read requests
  84. FUSE_CAP_POSIX_LOCKS - filesystem supports "remote" locking
  85. FUSE_CAP_ATOMIC_O_TRUNC - filesystem handles the O_TRUNC open flag
  86. FUSE_CAP_EXPORT_SUPPORT - filesystem handles lookups of "." and ".."
  87. FUSE_CAP_BIG_WRITES - filesystem can handle write size larger than 4kB
  88. FUSE_CAP_DONT_MASK - don't apply umask to file mode on create
  89. operations
  90. }
  91. FUSE_CAP_NONE = 0;
  92. FUSE_CAP_ASYNC_READ = 1;
  93. FUSE_CAP_POSIX_LOCKS = 2;
  94. FUSE_CAP_ATOMIC_O_TRUNC = 8;
  95. FUSE_CAP_EXPORT_SUPPORT = 16;
  96. FUSE_CAP_BIG_WRITES = 32;
  97. FUSE_CAP_DONT_MASK = 64;
  98. { Ioctl Flags (see TFuseOperations.ioctl)
  99. FUSE_IOCTL_COMPAT - 32bit compat ioctl on 64bit machine
  100. FUSE_IOCTL_UNRESTRICTED - not restricted to well-formed ioctls, retry
  101. allowed
  102. FUSE_IOCTL_RETRY - retry with new iovecs
  103. FUSE_IOCTL_MAX_IOV - maximum of in_iovecs + out_iovecs
  104. Introduced in version 2.8
  105. }
  106. {$if (fuse_use_version >= 28)}
  107. FUSE_IOCTL_NONE = 0;
  108. FUSE_IOCTL_COMPAT = 1;
  109. FUSE_IOCTL_UNRESTRICTED = 2;
  110. FUSE_IOCTL_RETRY = 4;
  111. FUSE_IOCTL_MAX_IOV = 256;
  112. {$endif}
  113. type
  114. { Common Type Defines }
  115. PFuse = type pointer;
  116. PFuseSession = type pointer;
  117. PFuseChan = type pointer;
  118. PPFuseChan = ^PFuseChan;
  119. PFuseRequest = type pointer;
  120. PFuseCmd = type pointer;
  121. {$if (fuse_use_version >= 28)}
  122. PFusePollHandle = type pointer;
  123. {$endif}
  124. __fsblkcnt64_t = cuint64;
  125. __fsfilcnt64_t = cuint64;
  126. fsblkcnt_t = __fsblkcnt64_t; // 64-bit (LFS)
  127. fsfilcnt_t = __fsfilcnt64_t; // 64-bit (LFS)
  128. { VFS File System information structure
  129. <sys/statvfs.h>
  130. }
  131. TStatVFS = record
  132. f_bsize : culong; // File system block size
  133. f_frsize : culong; // Fudamental file system block size
  134. f_blocks, // Total number of blocks on file system in
  135. // units of f_frsize.
  136. f_bfree, // Total number of free blocks.
  137. f_bavail : fsblkcnt_t; // Number of free blocks available to
  138. // non-privileged process.
  139. f_files, // Total number of file serial numbers.
  140. f_ffree, // Total number of free file serial numbers
  141. f_favail : fsfilcnt_t; // Number of file serial numbers available
  142. // to non-privileged process.
  143. f_fsid : culong; // File system ID.
  144. __f_unused : cint; // Unused
  145. f_flag : culong; // Bit mask of f_flag values.
  146. f_namemax : culong; // Maximum filename length.
  147. __f_spare : array[0..5] of cint; // Spare
  148. end;
  149. PStatVFS = ^TStatVFS;
  150. TStatVFS64 = TStatVFS; PStatVFS64 = ^TStatVFS;
  151. { Information about open files }
  152. TFuseFileInfo = record
  153. flags : cint; // Open flags. Available in open() and
  154. // release()
  155. fh_old : culong deprecated; // Old file handle, don't use
  156. writepage : cint; // In case of a write oprtation indicates
  157. // if this was caused by a writepage
  158. fi_flags : cuint; // See FUSE_FI_ flags
  159. fh : cuint64; // File handle. May be filled in by
  160. // filesystem in open().
  161. // Available in all other file operations
  162. lock_owner : cuint64; // Lock owner id. Available in locking
  163. // operations and flush
  164. end;
  165. PFuseFileInfo = ^TFuseFileInfo;
  166. { Connection information, passed to the ->init() method
  167. Some of the elements are read-write, these can be changed to indicate the
  168. value requested by the filesystem. The requested value must usually be
  169. smaller than the indicated value.
  170. }
  171. TFuseConnInfo = record
  172. ProtoMajor : cunsigned; // Major version of the protocol (read-only)
  173. ProtoMinor : cunsigned; // Minor version of the protocol (read-only)
  174. AsyncRead : cunsigned; // Is asynchronous read supported (read-write)
  175. MaxWrite : cunsigned; // Maximum size of the write buffer
  176. MaxReadahead : cunsigned; // Maximum readahead
  177. Capable : cunsigned; // Capability flags, that the kernel supports
  178. Want : cunsigned; // Capability flags, that the filesystem wants
  179. // to enable. See FUSE_CAP_ flags
  180. Reserved : array [0..24] of cunsigned; // For future use.
  181. end;
  182. PFuseConnInfo = ^TFuseConnInfo;
  183. { Time Tuple for utimens() }
  184. TFuseTimeTuple = record
  185. AccessedTime,
  186. ModifiedTime : timespec;
  187. end;
  188. PFuseTimeTuple = ^TFuseTimeTuple;
  189. { Function to add an entry in a readdir() operation
  190. @param aBuffer the buffer passed to the readdir() operation
  191. @param aName the file name of the directory entry
  192. @param aStat file attributes, can be NULL
  193. @param aFileOffset offset of the next entry or zero
  194. @return 1 if buffer is full, zero otherwise
  195. }
  196. TFuseFillDir = function(aBuffer : pointer; const aName : PChar; const aStat : PStat; aFileOffset : TOff) : cint; cdecl;
  197. { Used by deprecated getdir() method }
  198. TFuseDirfil = function(aHandle : pointer; const aName : PChar; aType : cint; aIno : TIno) : cint deprecated; cdecl;
  199. { The file system operations:
  200. Most of these should work very similarly to the well known UNIX file system
  201. operations. A major exception is that instead of returning an error in
  202. 'errno', the operation should return the negated error value (-errno)
  203. directly.
  204. All methods are optional, but some are essential for a useful filesystem
  205. (e.g. getattr). Open, flush, release, fsync, opendir, releasedir, fsyncdir,
  206. access, create, ftruncate, fgetattr, lock, init and destroy are special
  207. purpose methods, without which a full featured filesystem can still be
  208. implemented.
  209. Almost all operations take a path which can be of any length.
  210. Changed in fuse 2.8.0 (regardless of API version)
  211. Previously, paths were limited to a length of PATH_MAX.
  212. See http://fuse.sourceforge.net/wiki/ for more information.
  213. }
  214. TFuseOperations = record
  215. { Get file attributes.
  216. Similar to stat(). The 'st_dev' and 'st_blksize' fields are ignored.
  217. The 'st_ino' field is ignored except if the 'use_ino' mount option is given.
  218. }
  219. getattr : function(const aName : PChar; var aStat : TStat) : cint; cdecl;
  220. { Read the target of a symbolic link
  221. The buffer should be filled with a null terminated string. The buffer size
  222. argument includes the space for the terminating null character. If the
  223. linkname is too long to fit in the buffer, it should be truncated.
  224. The return value should be 0 for success.
  225. }
  226. readlink : function(const aName : PChar; aLinksToName : PChar; aLinksToNameSize: TSize) : cint; cdecl;
  227. { Deprecated, use readdir() instead }
  228. getdir : function(const aName : PChar; aDirectoryHandle : pointer; aDirfilFunc : TFuseDirfil) : cint deprecated; cdecl;
  229. { Create a file node
  230. This is called for creation of all non-directory, non-symlink nodes.If the
  231. filesystem defines a create() method, then for regular files that will be
  232. called instead
  233. }
  234. mknod : function(const aName : PChar; aMode : TMode; aDevice : TDev) : cint; cdecl;
  235. { Create a directory
  236. Note that the mode argument may not have the type specification bits set,
  237. i.e. S_ISDIR(mode) can be false. To obtain the correct directory type bits
  238. use mode|S_IFDIR
  239. }
  240. mkdir : function(const aDirectoryName : PChar; aMode : TMode) : cint; cdecl;
  241. { Remove a file }
  242. unlink : function(const aName : PChar) : cint; cdecl;
  243. { Remove a directory }
  244. rmdir : function(const aName : PChar) : cint; cdecl;
  245. { Create a symbolic link }
  246. symlink : function(const aLinksToName, aName : PChar) : cint; cdecl;
  247. { Rename a file }
  248. rename : function(const aName, aNewName : PChar) : cint; cdecl;
  249. { Create a hard link to a file }
  250. link : function(const aLinksToName, aName : PChar) : cint; cdecl;
  251. { Change the permission bits of a file }
  252. chmod : function(const aName : PChar; aMode : TMode) : cint; cdecl;
  253. { Change the owner and group of a file }
  254. chown : function(const aName : PChar; aUID : TUid; aGID : TGid) : cint; cdecl;
  255. { Change the size of a file }
  256. truncate : function(const aName : PChar; aNewSize : TOff) : cint; cdecl;
  257. { Change the access and/or modification times of a file
  258. Deprecated, use utimens() instead.
  259. }
  260. utime : function(aName : PChar; aTime : Putimbuf) : cint deprecated; cdecl;
  261. { File open operation
  262. No creation (O_CREAT, O_EXCL) and by default also no truncation (O_TRUNC)
  263. flags will be passed to open(). If an application specifies O_TRUNC, fuse
  264. first calls truncate() and then open(). Only if 'atomic_o_trunc' has been
  265. specified and kernel version is 2.6.24 or later, O_TRUNC is passed on to
  266. open.
  267. Unless the 'default_permissions' mount option is given, open should check
  268. if the operation is permitted for the given flags. Optionally open may
  269. also return an arbitrary filehandle in the fuse_file_info structure, which
  270. will be passed to all file operations.
  271. Changed in version 2.2
  272. }
  273. open : function(const aName : PChar; aFileInfo : PFuseFileInfo) : cint; cdecl;
  274. { Read data from an open file
  275. Read should return exactly the number of bytes requested except on EOF or
  276. error, otherwise the rest of the data will be substituted with zeroes. An
  277. exception to this is when the 'direct_io' mount option is specified, in
  278. which case the return value of the read system call will reflect the
  279. return value of this operation.
  280. Changed in version 2.2
  281. }
  282. read : function(const aName : PChar; aBuffer : pointer; aBufferSize : TSize; aFileOffset : TOff; aFileInfo : PFuseFileInfo) : cint; cdecl;
  283. { Write data to an open file
  284. Write should return exactly the number of bytes requested except on error.
  285. An exception to this is when the 'direct_io' mount option is specified
  286. (see read operation).
  287. Changed in version 2.2
  288. }
  289. write : function(const aName : PChar; const aBuffer : Pointer; aBufferSize : TSize; aFileOffset : TOff; aFileInfo : PFuseFileInfo) : cint; cdecl;
  290. { Get file system statistics
  291. The 'f_frsize', 'f_favail', 'f_fsid' and 'f_flag' fields are ignored
  292. Replaced 'struct statfs' parameter with 'struct statvfs' in version 2.5
  293. }
  294. statfs : function(const aName : PChar; aStatVFS : PStatVFS) : cint; cdecl;
  295. { Possibly flush cached data
  296. BIG NOTE: This is not equivalent to fsync(). It's not a request to sync
  297. dirty data.
  298. Flush is called on each close() of a file descriptor. So if a filesystem
  299. wants to return write errors in close() and the file has cached dirty
  300. data, this is a good place to write back data and return any errors. Since
  301. many applications ignore close() errors this is not always useful.
  302. NOTE: The flush() method may be called more than once for each open().
  303. This happens if more than one file descriptor refers to an opened file due
  304. to dup(), dup2() or fork() calls. It is not possible to determine if a
  305. flush is final, so each flush should be treated equally. Multiple
  306. write-flush sequences are relatively rare, so this shouldn't be a problem.
  307. Filesystems shouldn't assume that flush will always be called after some
  308. writes, or that if will be called at all.
  309. Changed in version 2.2
  310. }
  311. flush : function(const aName : PChar; aFileInfo : PFuseFileInfo) : cint; cdecl;
  312. { Release an open file
  313. Release is called when there are no more references to an open file: all
  314. file descriptors are closed and all memory mappings are unmapped.
  315. For every open() call there will be exactly one release() call with the
  316. same flags and file descriptor. It is possible to have a file opened more
  317. than once, in which case only the last release will mean, that no more
  318. reads/writes will happen on the file. The return value of release is
  319. ignored.
  320. Changed in version 2.2
  321. }
  322. release : function(const aName : PChar; aFileInfo : PFuseFileInfo) : cint; cdecl;
  323. { Synchronize file contents
  324. If the datasync parameter is non-zero, then only the user data should be
  325. flushed, not the meta data.
  326. Changed in version 2.2
  327. }
  328. fsync : function(const aName : PChar; aDataSync : cint; aFileInfo : PFuseFileInfo) : cint; cdecl;
  329. { Set Extended Attributes }
  330. setxattr : function(const aName, aKey, aValue : PChar; aValueSize : TSize; Flags : cint) : cint; cdecl;
  331. { Get Extended Attributes }
  332. getxattr : function(const aName, aKey : PChar; aValue : PChar; aValueSize : TSize) : cint; cdecl;
  333. { List Extended Attributes }
  334. listxattr : function(const aName : PChar; aList : PChar; aListSize : TSize) : cint; cdecl;
  335. { Remove Extended Attributes }
  336. removexattr : function(const aName, aKey : PChar) : cint; cdecl;
  337. { Open directory
  338. Unless the 'default_permissions' mount option is given, this method should
  339. check if opendir is permitted for this directory. Optionally opendir may
  340. also return an arbitrary filehandle in the fuse_file_info structure, which
  341. will be passed to readdir, closedir and fsyncdir.
  342. Introduced in version 2.3
  343. }
  344. opendir : function(const aName : PChar; aFileInfo : PFuseFileInfo) : cint; cdecl;
  345. { Read directory
  346. This supersedes the old getdir() interface. New applications should use
  347. this.
  348. The filesystem may choose between two modes of operation:
  349. 1) The readdir implementation ignores the offset parameter, and passes
  350. zero to the filler function's offset. The filler function will not
  351. return '1' (unless an error happens), so the whole directory is read in a
  352. single readdir operation. This works just like the old getdir() method.
  353. 2) The readdir implementation keeps track of the offsets of the directory
  354. entries. It uses the offset parameter and always passes non-zero offset to
  355. the filler function. When the buffer is full (or an error happens) the
  356. filler function will return '1'.
  357. Introduced in version 2.3
  358. }
  359. readdir : function(const aName : PChar; aBuffer : pointer; aFillDirFunc : TFuseFillDir; aFileOffset : TOff; aFileInfo : PFuseFileInfo) : cint; cdecl;
  360. { Release directory
  361. Introduced in version 2.3
  362. }
  363. releasedir : function(const aName : PChar; aFileInfo : PFuseFileInfo) : cint; cdecl;
  364. { Synchronize directory contents
  365. If the datasync parameter is non-zero, then only the user data should be
  366. flushed, not the meta data
  367. Introduced in version 2.3
  368. }
  369. fsyncdir : function(const aName : PChar; aDataSync : Integer; aFileInfo : PFuseFileInfo) : cint; cdecl;
  370. { Initialize filesystem
  371. The return value will passed in the private_data field of fuse_context to
  372. all file operations and as a parameter to the destroy() method.
  373. Introduced in version 2.3
  374. Changed in version 2.6
  375. }
  376. init : function(var aConnectionInfo : TFuseConnInfo) : pointer; cdecl;
  377. { Clean up filesystem
  378. Called on filesystem exit.
  379. Introduced in version 2.3
  380. }
  381. destroy : procedure(aUserData : pointer); cdecl;
  382. { Check file access permissions
  383. This will be called for the access() system call. If the
  384. 'default_permissions' mount option is given, this method is not called.
  385. This method is not called under Linux kernel versions 2.4.x
  386. Introduced in version 2.5
  387. }
  388. access : function(const aName : PChar; aMode : cint) : cint; cdecl;
  389. { Create and open a file
  390. If the file does not exist, first create it with the specified mode, and
  391. then open it.
  392. If this method is not implemented or under Linux kernel versions earlier
  393. than 2.6.15, the mknod() and open() methods will be called instead.
  394. Introduced in version 2.5
  395. }
  396. create : function(const aName : PChar; aMode : TMode; aFileInfo : PFuseFileInfo) : cint; cdecl;
  397. { Change the size of an open file
  398. This method is called instead of the truncate() method if the truncation
  399. was invoked from an ftruncate() system call.
  400. If this method is not implemented or under Linux kernel versions earlier
  401. than 2.6.15, the truncate() method will be called instead.
  402. Introduced in version 2.5
  403. }
  404. ftruncate : function(const aName : PChar; aSize : TOff; aFileInfo : PFuseFileInfo) : cint; cdecl;
  405. { Get attributes from an open file
  406. This method is called instead of the getattr() method if the file
  407. information is available.
  408. Currently this is only called after the create() method if that is
  409. implemented (see above). Later it may be called for invocations of fstat()
  410. too.
  411. Introduced in version 2.5
  412. }
  413. fgetattr : function(constaName : PChar; aOutStat : PStat; PFileInfo : PFuseFileInfo) : cint; cdecl;
  414. { Perform POSIX file locking operation
  415. The cmd argument will be either F_GETLK, F_SETLK or F_SETLKW.
  416. For the meaning of fields in 'struct flock' see the man page for fcntl(2).
  417. The l_whence field will always be set to SEEK_SET.
  418. For checking lock ownership, the 'fuse_file_info->owner'argument must be
  419. used.
  420. For F_GETLK operation, the library will first check currently held locks,
  421. and if a conflicting lock is found it will return information without
  422. calling this method. This ensures, that for local locks the l_pid field is
  423. correctly filled in. The results may not be accurate in case of race
  424. conditions and inthe presence of hard links, but it's unlikly that an
  425. application would rely on accurate GETLK results in these cases. If a
  426. conflicting lock is not found, this method will be called, and the
  427. filesystem may fill out l_pid by a meaningful value, or it may leave this
  428. field zero.
  429. or F_SETLK and F_SETLKW the l_pid field will be set to the pid of the
  430. process performing the locking operation.
  431. Note: if this method is not implemented, the kernel will still allow file
  432. locking to work locally. Hence it is only interesting for network
  433. filesystems and similar.
  434. Introduced in version 2.6
  435. }
  436. lock : function(const aName : PChar; aFileInfo : PFuseFileInfo; aCMD : cint; var aLock : FLock) : cint; cdecl;
  437. { Change the access and modification times of a file with nanosecond
  438. resolution
  439. Introduced in version 2.6
  440. }
  441. utimens : function(const aName : PChar; const aTime : TFuseTimeTuple) : cint; cdecl;
  442. { Map block index within file to block index within device
  443. Note: This makes sense only for block device backed filesystems mounted
  444. with the 'blkdev' option
  445. Introduced in version 2.6
  446. }
  447. bmap : function(const aName : PChar; aBlockSize : TSize; aIndex : cuint64) : cint; cdecl;
  448. {$if (fuse_use_version >= 28)}
  449. { See FUSE_OP_ flags }
  450. operation_flags : cuint; // 32-bits!
  451. { Ioctl
  452. flags will have FUSE_IOCTL_COMPAT set for 32bit ioctls in 64bit
  453. environment. The size and direction of data is determined by _IOC_*()
  454. decoding of cmd. For _IOC_NONE, data will be NULL, for _IOC_WRITE data is
  455. out area, for _IOC_READ in area and if both are set in/out area. In all
  456. non-NULL cases, the area is of _IOC_SIZE(cmd) bytes.
  457. Introduced in version 2.8
  458. }
  459. ioctl : function(const aName : PChar; aCmd : cint; aArg : pointer; aFileInfo : PFuseFileInfo; aFlags : cuint; aData : pointer) : cint; cdecl;
  460. { Poll for IO readiness events
  461. Note: If ph is non-NULL, the client should notify when IO readiness events
  462. occur by calling fuse_notify_poll() with the specified ph.
  463. Regardless of the number of times poll with a non-NULL ph is received,
  464. single notification is enough to clear all. Notifying more times incurs
  465. overhead but doesn't harm correctness.
  466. The callee is responsible for destroying ph with fuse_pollhandle_destroy()
  467. when no longer in use.
  468. Introduced in version 2.8
  469. }
  470. poll : function(const aName : PChar; aFileInfo : PFuseFileInfo; aPH : PFusePollHandle; aREventSP : pcunsigned) : cint; cdecl;
  471. {$endif}
  472. end;
  473. PFuseOperations = ^TFuseOperations;
  474. { Extra context that may be needed by some filesystems
  475. The uid, gid and pid fields are not filled in case of a writepage operation.
  476. }
  477. TFuseContext = record
  478. fuse : PFuse; // Pointer to the fuse object
  479. uid : TUid; // User ID of the calling process
  480. gid : TGid; // Group ID of the calling process
  481. pid : TPid; // Thread ID of the calling process
  482. UserData : pointer; // Private filesystem data (private_data)
  483. umask : TMode; // Umask of the calling process (introduced in version 2.8)
  484. end;
  485. PFuseContext = ^TFuseContext;
  486. { Argument list }
  487. TFuseArgs = record
  488. argc : cint; // Argument count
  489. argv : PPChar; // Argument vector. NULL termiated
  490. allocated : cint; // Is 'argv' allocated?
  491. end;
  492. PFuseArgs = ^TFuseArgs;
  493. { Function type used to process commands
  494. Will be deprecated in 3.0 API (see fuse_loop_mt_proc)
  495. }
  496. TFuseProcessor = procedure(aFuse : PFuse; aFuseCmd : PFuseCmd; aData : pointer); deprecated;
  497. { -----------------------------------------------------------
  498. Common FUSE functions
  499. ----------------------------------------------------------- }
  500. { Create a FUSE mountpoint
  501. Returns a control file descriptor suitable for passing tofuse_new()
  502. @param aMountpoint the mount point path
  503. @param aFuseArgs argument vector
  504. @return the communication channel on success, NULL on failure
  505. }
  506. function fuse_mount(const aMountpoint : PChar; aFuseArgs : PFuseArgs) : PFuseChan; cdecl;
  507. { Umount a FUSE mountpoint
  508. @param aMountpoint the mount point path
  509. @param aFuseChan the communication channel
  510. }
  511. procedure fuse_unmount(const mountpoint : PChar; aFuseChan : PFuseChan); cdecl;
  512. { Parse common options
  513. The following options are parsed:
  514. '-f' foreground
  515. '-d' '-odebug' foreground, but keep the debug option
  516. '-s' single threaded
  517. '-h' '--help' help
  518. '-ho' help without header
  519. '-ofsname=..' file system name, if not present, then set to the program
  520. name
  521. All parameters may be NULL
  522. @param aFuseArgs argument vector
  523. @param aMountpoint the returned mountpoint, should be freed after use
  524. @param aMultithreaded set to 1 unless the '-s' option is present
  525. @param aForeground set to 1 if one of the relevant options is present
  526. @return 0 on success, -1 on failure
  527. }
  528. function fuse_parse_cmdline(aFuseArgs : PFuseArgs; var aMountpoint : PChar; var aMultithreaded : cint; var aForeground : cint) : cint; cdecl;
  529. { Go into the background
  530. @param foreground if true, stay in the foreground
  531. @return 0 on success, -1 on failure
  532. }
  533. function fuse_daemonize(aForeground : cint) : cint; cdecl;
  534. { Get the version of the library
  535. @return the version
  536. }
  537. function fuse_version : cint; cdecl;
  538. { Destroy poll handle
  539. @param ph the poll handle
  540. }
  541. {$if (fuse_use_version >= 28)}
  542. procedure fuse_pollhandle_destroy(aFusePollHandle : PFusePollHandle); cdecl;
  543. {$endif}
  544. { -----------------------------------------------------------
  545. Signal handling
  546. ----------------------------------------------------------- }
  547. { Exit session on HUP, TERM and INT signals and ignore PIPE signal
  548. Stores session in a global variable. May only be called once per process until
  549. fuse_remove_signal_handlers() is called.
  550. @param aSession the session to exit
  551. @return 0 on success, -1 on failure
  552. }
  553. function fuse_set_signal_handlers(aSession : PFuseSession) : cint; cdecl;
  554. { Restore default signal handlers
  555. Resets global session. After this fuse_set_signal_handlers() may be called
  556. again.
  557. @param aSession the same session as given in fuse_set_signal_handlers()
  558. }
  559. procedure fuse_remove_signal_handlers(aSession : PFuseSession); cdecl;
  560. { *** *** *** }
  561. { Main function of FUSE
  562. This is for the lazy. This is all that has to be called from the main
  563. program block.
  564. This function does the following:
  565. - parses command line options (-d -s and -h)
  566. - passes relevant mount options to the fuse_mount()
  567. - installs signal handlers for INT, HUP, TERM and PIPE
  568. - registers an exit handler to unmount the filesystem on program exit
  569. - creates a fuse handle
  570. - registers the operations
  571. - calls either the single-threaded or the multi-threaded event loop
  572. @param aArgc the argument counter passed to the main program block
  573. @param aArgv the argument vector passed to the main program block
  574. @param aFuseOperations the file system operation
  575. @param aUserData user data supplied in the context during the init() method
  576. @return 0 on success, nonzero on failure
  577. }
  578. function fuse_main(aArgC : cint; aArgV: PPChar; aFuseOperations : PFuseOperations; aFuseOperationsSize : TSize; aUserData : pointer) : cint;
  579. { -----------------------------------------------------------
  580. More detailed API
  581. ----------------------------------------------------------- }
  582. { Create a new FUSE filesystem.
  583. @param aFuseChan the communication channel
  584. @param aFuseArgs argument vector
  585. @param aFuseOperations the filesystem operations
  586. @param aFuseOperationsSize the size of the fuse_operations structure
  587. @param aUserData user data supplied in the context during the init() method
  588. @return the created FUSE handle
  589. }
  590. function fuse_new(aFuseChan : PFuseChan; aFuseArgs : PFuseArgs; aFuseOperations : PFuseOperations; aFuseOperationsSize : TSize; aUserData : pointer) : PFuse; cdecl;
  591. { Destroy the FUSE handle.
  592. The communication channel attached to the handle is also destroyed.
  593. NOTE: This function does not unmount the filesystem. If this is needed, call
  594. fuse_unmount() before calling this function.
  595. @param aFuse the FUSE handle
  596. }
  597. procedure fuse_destroy(aFuse : PFuse); cdecl;
  598. { FUSE event loop.
  599. Requests from the kernel are processed, and the appropriate operations are
  600. called.
  601. @param aFuse the FUSE handle
  602. @return 0 if no error occurred, -1 otherwise
  603. }
  604. function fuse_loop(aFuse : PFuse): cint; cdecl;
  605. { Exit from event loop
  606. @param aFuse the FUSE handle
  607. }
  608. procedure fuse_exit(aFuse : PFuse); cdecl;
  609. { FUSE event loop with multiple threads
  610. Requests from the kernel are processed, and the appropriate operations are
  611. called. Request are processed in parallel by distributing them between
  612. multiple threads.
  613. Calling this function requires the pthreads library to be linked to the
  614. application.
  615. @param aFuse the FUSE handle
  616. @return 0 if no error occurred, -1 otherwise
  617. }
  618. function fuse_loop_mt(aFuse : PFuse) : cint; cdecl;
  619. { Get the current context
  620. The context is only valid for the duration of a filesystem operation, and thus
  621. must not be stored and used later.
  622. @return the context
  623. }
  624. function fuse_get_context : PFuseContext; cdecl;
  625. { Get the current supplementary group IDs for the current request
  626. Currently unsupported and will return -ENOSYS.
  627. }
  628. function fuse_getgroups(aSize : cint; aList : array of TGid) : cint;
  629. { Check if the current request has already been interrupted
  630. @return 1 if the request has been interrupted, 0 otherwise
  631. }
  632. function fuse_interrupted : cint; cdecl;
  633. { Obsolete, doesn't do anything
  634. @return -EINVAL
  635. }
  636. function fuse_invalidate(aFuse : PFuse; const aPath : PChar) : cint; deprecated;
  637. { Deprecated, don't use }
  638. function fuse_is_lib_option(const aOpt : PChar) : cint; deprecated;
  639. { -----------------------------------------------------------
  640. Advanced API for event handling, don't worry about this...
  641. -----------------------------------------------------------
  642. NOTE: the following functions are deprecated, and will be removed
  643. from the 3.0 API. Use the lowlevel session functions instead
  644. }
  645. { This is the part of fuse_main() before the event loop }
  646. function fuse_setup(aArgC : cint; aArgV : PPChar; aFuseOperations : PFuseOperations; aFuseOperationsSize : TSize; aMountpoint : pointer; aMultithreaded : pcint; aUserData : pointer) : PFuse; cdecl; deprecated;
  647. { This is the part of fuse_main() after the event loop }
  648. procedure fuse_teardown(aFuse : PFuse; aMountpoint : pointer); cdecl; deprecated;
  649. { Read a single command. If none are read, return NULL }
  650. function fuse_read_cmd(aFuse : PFuse) : PFuseCmd; cdecl; deprecated;
  651. { Process a single command }
  652. procedure fuse_process_cmd(aFuse : PFuse; aCmd : PFuseCmd); cdecl; deprecated;
  653. { Multi threaded event loop, which calls the custom command processor function }
  654. function fuse_loop_mt_proc(aFuse : PFuse; aFuseProcessor : TFuseProcessor; aData : pointer) : cint; cdecl; deprecated;
  655. { Return the exited flag, which indicates if fuse_exit() has been called }
  656. function fuse_exited(aFuse : PFuse) : cint; cdecl; deprecated;
  657. { This function is obsolete and implemented as a no-op }
  658. procedure fuse_set_getcontext_func(aFuseContext : pointer); cdecl; deprecated;
  659. { Get session from fuse object }
  660. function fuse_get_session(aFuse : PFuse) : PFuseSession; cdecl; deprecated;
  661. { ---------------------------------------------------------------------------- }
  662. implementation
  663. const
  664. {$if defined(linux)}
  665. FUSELIBFile = 'libfuse.so.2';
  666. {$else}
  667. {$error Unsupported Target}
  668. {$endif}
  669. { --- Common --- }
  670. function fuse_mount(const aMountpoint : PChar; aFuseArgs : PFuseArgs) : PFuseChan; cdecl; external FUSELIBFile;
  671. procedure fuse_unmount(const mountpoint : PChar; aFuseChan : PFuseChan); cdecl; external FUSELIBFile;
  672. function fuse_parse_cmdline(aFuseArgs : PFuseArgs; var aMountpoint : PChar; var aMultithreaded : cint; var aForeground : cint) : cint; cdecl; external FUSELIBFile;
  673. function fuse_daemonize(aForeground : cint) : cint; cdecl; external FUSELIBFile;
  674. function fuse_version : cint; cdecl; external FUSELIBFile;
  675. {$if (fuse_use_version >= 28)}
  676. procedure fuse_pollhandle_destroy(aFusePollHandle : PFusePollHandle); cdecl; external FUSELIBFile;
  677. {$endif}
  678. function fuse_set_signal_handlers(aSession : PFuseSession) : cint; cdecl; external FUSELIBFile;
  679. procedure fuse_remove_signal_handlers(aSession : PFuseSession); cdecl; external FUSELIBFile;
  680. { --- Main --- }
  681. function fuse_main_real(aArgC : cint; aArgV: PPChar; aFuseOperations : PFuseOperations; aFuseOperationsSize : TSize; aUserData : pointer) : cint; cdecl; external FUSELIBFile;
  682. function fuse_main(aArgC : cint; aArgV: PPChar; aFuseOperations : PFuseOperations; aFuseOperationsSize : TSize; aUserData : pointer) : cint;
  683. begin
  684. Result := fuse_main_real(aArgC, aArgV, aFuseOperations, aFuseOperationsSize, aUserData);
  685. end;
  686. function fuse_new(aFuseChan : PFuseChan; aFuseArgs : PFuseArgs; aFuseOperations : PFuseOperations; aFuseOperationsSize : TSize; aUserData : pointer) : PFuse; cdecl; external FUSELIBFile;
  687. procedure fuse_destroy(aFuse : PFuse); cdecl; external FUSELIBFile;
  688. function fuse_loop(aFuse : PFuse): cint; cdecl; external FUSELIBFile;
  689. procedure fuse_exit(aFuse : PFuse); cdecl; external FUSELIBFile;
  690. function fuse_loop_mt(aFuse : PFuse) : cint; cdecl; external FUSELIBFile;
  691. function fuse_get_context : PFuseContext; cdecl; external FUSELIBFile;
  692. function fuse_getgroups(aSize : cint; aList : array of TGid) : cint;
  693. begin
  694. Result := -ESysENOSYS;
  695. end;
  696. function fuse_interrupted : cint; cdecl; external FUSELIBFile;
  697. function fuse_invalidate(aFuse : PFuse; const aPath : PChar) : cint;
  698. begin
  699. Result := -ESysEINVAL;
  700. end;
  701. function fuse_is_lib_option(const aOpt : PChar) : cint;
  702. begin
  703. Result := -ESysEINVAL;
  704. end;
  705. { --- Deprecated --- }
  706. function fuse_setup(aArgC : cint; aArgV : PPChar; aFuseOperations : PFuseOperations; aFuseOperationsSize : TSize; aMountpoint : pointer; aMultithreaded : Pcint; aUserData : pointer) : PFuse; cdecl; external FUSELIBFile;
  707. procedure fuse_teardown(aFuse : PFuse; aMountpoint : pointer); cdecl; external FUSELIBFile;
  708. function fuse_read_cmd(aFuse : PFuse) : PFuseCmd; cdecl; external FUSELIBFile;
  709. procedure fuse_process_cmd(aFuse : PFuse; aCmd : PFuseCmd); cdecl; external FUSELIBFile;
  710. function fuse_loop_mt_proc(aFuse : PFuse; aFuseProcessor : TFuseProcessor; aData : pointer) : cint; cdecl; external FUSELIBFile;
  711. function fuse_exited(aFuse : PFuse) : cint; cdecl; external FUSELIBFile;
  712. procedure fuse_set_getcontext_func(aFuseContext : pointer); cdecl; external FUSELIBFile;
  713. function fuse_get_session(aFuse : PFuse) : PFuseSession; cdecl; external FUSELIBFile;
  714. initialization
  715. assert(sizeof(off_t)=8, 'Assertion Failed - FUSE expects 64 bit file offsets (_FILE_OFFSET_BITS 64)');
  716. end.