/[fuse.before_github]/perl-llin/Fuse.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Diff of /perl-llin/Fuse.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

perl/trunk/Fuse.pm revision 7 by dpavlin, Thu Nov 18 12:09:10 2004 UTC perl-llin/Fuse.pm revision 120 by dpavlin, Sun Mar 16 13:40:44 2008 UTC
# Line 5  use strict; Line 5  use strict;
5  use warnings;  use warnings;
6  use Errno;  use Errno;
7  use Carp;  use Carp;
8    use Config;
9    
10  require Exporter;  require Exporter;
11  require DynaLoader;  require DynaLoader;
# Line 19  our @ISA = qw(Exporter DynaLoader); Line 20  our @ISA = qw(Exporter DynaLoader);
20  # This allows declaration       use Fuse ':all';  # This allows declaration       use Fuse ':all';
21  # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK  # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
22  # will save memory.  # will save memory.
23  our %EXPORT_TAGS = ( 'all' => [ qw(  our %EXPORT_TAGS = (
24          FUSE_DEBUG                      'all' => [ qw(XATTR_CREATE XATTR_REPLACE fuse_get_context) ],
25  ) ] );                      'xattr' => [ qw(XATTR_CREATE XATTR_REPLACE) ]
26                        );
27    
28  our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );  our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
29    
30  our @EXPORT = qw(  our @EXPORT = ();
31          FUSE_DEBUG  our $VERSION = '0.09_2';
 );  
 our $VERSION = '0.04';  
32    
33  sub AUTOLOAD {  sub AUTOLOAD {
34      # This AUTOLOAD is used to 'autoload' constants from the constant()      # This AUTOLOAD is used to 'autoload' constants from the constant()
# Line 62  sub AUTOLOAD { Line 62  sub AUTOLOAD {
62      goto &$AUTOLOAD;      goto &$AUTOLOAD;
63  }  }
64    
65    sub XATTR_CREATE {
66        # See <sys/xattr.h>.
67        return 1;
68    }
69    
70    sub XATTR_REPLACE {
71        # See <sys/xattr.h>.
72        return 2;
73    }
74    
75  bootstrap Fuse $VERSION;  bootstrap Fuse $VERSION;
76    
77  sub main {  sub main {
78          my (@subs) = (0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);          my @names = qw(getattr readlink getdir mknod mkdir unlink rmdir symlink
79          my (@names) = qw(getattr readlink getdir mknod mkdir unlink rmdir symlink                          rename link chmod chown truncate utime open read write statfs
80                           rename link chmod chown truncate utime open read write statfs);                          flush release fsync setxattr getxattr listxattr removexattr);
81          my ($tmp) = 0;          my @subs = map {undef} @names;
82          my (%mapping) = map { $_ => $tmp++ } (@names);          my @validOpts = qw(ro allow_other default_permissions fsname use_ino nonempty);
83          my (%otherargs) = (debug=>0, mountpoint=>"");          my $tmp = 0;
84            my %mapping = map { $_ => $tmp++ } @names;
85            my %optmap  = map { $_ => 1 } @validOpts;
86            my @otherargs = qw(debug threaded mountpoint mountopts);
87            my %otherargs = (debug=>0, threaded=>0, mountpoint=>"", mountopts=>"");
88          while(my $name = shift) {          while(my $name = shift) {
89                  my ($subref) = shift;                  my ($subref) = shift;
90                  if(exists($otherargs{$name})) {                  if(exists($otherargs{$name})) {
91                          $otherargs{$name} = $subref;                          $otherargs{$name} = $subref;
92                  } else {                  } else {
93                          croak "There is no function $name" unless exists($mapping{$name});                          croak "There is no function $name" unless exists($mapping{$name});
94                          croak "Usage: Fuse::main(getattr => &my_getattr, ...)" unless $subref;                          croak "Usage: Fuse::main(getattr => \"main::my_getattr\", ...)" unless $subref;
                         croak "Usage: Fuse::main(getattr => &my_getattr, ...)" unless ref($subref);  
                         croak "Usage: Fuse::main(getattr => &my_getattr, ...)" unless ref($subref) eq "CODE";  
95                          $subs[$mapping{$name}] = $subref;                          $subs[$mapping{$name}] = $subref;
96                  }                  }
97          }          }
98          perl_fuse_main($otherargs{debug},$otherargs{mountpoint},@subs);          foreach my $opt ( map {m/^([^=]*)/; $1} split(/,/,$otherargs{mountopts}) ) {
99              next if exists($optmap{$opt});
100              croak "Fuse::main: invalid '$opt' argument in mountopts";
101            }
102            if($otherargs{threaded}) {
103                    # make sure threads are both available, and loaded.
104                    if($Config{useithreads}) {
105                            if(exists($threads::{VERSION})) {
106                                    if(exists($threads::shared::{VERSION})) {
107                                            # threads will work.
108                                    } else {
109                                            carp("Thread support requires you to use threads::shared.\nThreads are disabled.\n");
110                                            $otherargs{threaded} = 0;
111                                    }
112                            } else {
113                                    carp("Thread support requires you to use threads and threads::shared.\nThreads are disabled.\n");
114                                    $otherargs{threaded} = 0;
115                            }
116                    } else {
117                            carp("Thread support was not compiled into this build of perl.\nThreads are disabled.\n");
118                            $otherargs{threaded} = 0;
119                    }
120            }
121            perl_fuse_main(@otherargs{@otherargs},@subs);
122  }  }
123    
124  # Autoload methods go after =cut, and are processed by the autosplit program.  # Autoload methods go after =cut, and are processed by the autosplit program.
# Line 100  Fuse - write filesystems in Perl using F Line 135  Fuse - write filesystems in Perl using F
135    use Fuse;    use Fuse;
136    my ($mountpoint) = "";    my ($mountpoint) = "";
137    $mountpoint = shift(@ARGV) if @ARGV;    $mountpoint = shift(@ARGV) if @ARGV;
138    Fuse::main(mountpoint=>$mountpoint, getattr=>\&my_getattr, getdir=>\&my_getdir, ...);    Fuse::main(mountpoint=>$mountpoint, getattr=>"main::my_getattr", getdir=>"main::my_getdir", ...);
139    
140  =head1 DESCRIPTION  =head1 DESCRIPTION
141    
# Line 109  This lets you implement filesystems in p Line 144  This lets you implement filesystems in p
144    
145  FUSE expects you to implement callbacks for the various functions.  FUSE expects you to implement callbacks for the various functions.
146    
 NOTE:  I have only tested the things implemented in example.pl!  
 It should work, but some things may not.  
   
147  In the following definitions, "errno" can be 0 (for a success),  In the following definitions, "errno" can be 0 (for a success),
148  -EINVAL, -ENOENT, -EONFIRE, any integer less than 1 really.  -EINVAL, -ENOENT, -EONFIRE, any integer less than 1 really.
149    
# Line 122  Every constant you need (file types, ope Line 154  Every constant you need (file types, ope
154  etc) can be imported either from POSIX or from Fcntl, often both.  etc) can be imported either from POSIX or from Fcntl, often both.
155  See their respective documentations, for more information.  See their respective documentations, for more information.
156    
157  =head2 EXPORT  =head2 EXPORTED SYMBOLS
158    
159  None by default.  None by default.
160    
161  =head2 EXPORTABLE CONSTANTS  You can request all exportable symbols by using the tag ":all".
162    
163  None.  You can request the extended attribute symbols by using the tag ":xattr".
164    This will export XATTR_CREATE and XATTR_REPLACE.
165    
166  =head2 FUNCTIONS  =head2 FUNCTIONS
167    
# Line 157  specify this.  An example would be '/mnt Line 190  specify this.  An example would be '/mnt
190    
191  =back  =back
192    
193  unthreaded => boolean  mountopts => string
194    
195    =over 1
196    
197    This is a comma seperated list of mount options to pass to the FUSE kernel
198    module.
199    
200    At present, it allows the specification of the allow_other
201    argument when mounting the new FUSE filesystem. To use this, you will also
202    need 'user_allow_other' in /etc/fuse.conf as per the FUSE documention
203    
204      mountopts => "allow_other" or
205      mountopts => ""
206    
207    =back
208    
209    threaded => boolean
210    
211  =over 1  =over 1
212    
213  This turns FUSE multithreading off and on.  NOTE: This perlmodule does not  This turns FUSE multithreading on and off.  The default is 0, meaning your FUSE
214  currently work properly in multithreaded mode!  The author is unfortunately  script will run in single-threaded mode.  Note that single-threaded mode also
215  not familiar enough with perl-threads internals, and according to the  means that you will not have to worry about reentrancy, though you will have to
216  documentation available at time of writing (2002-03-08), those internals are  worry about recursive lookups.  In single-threaded mode, FUSE holds a global
217  subject to changing anyway.  Note that singlethreaded mode also means that  lock on your filesystem, and will wait for one callback to return before
218  you will not have to worry about reentrancy, though you will have to worry  calling another.  This can lead to deadlocks, if your script makes any attempt
219  about recursive lookups (since the kernel holds a global lock on your  to access files or directories in the filesystem it is providing.  (This
220  filesystem and blocks waiting for one callback to complete before calling  includes calling stat() on the mount-point, statfs() calls from the 'df'
221  another).  command, and so on and so forth.)  It is worth paying a little attention and
222    being careful about this.
223  I hope to add full multithreading functionality later, but for now, I  
224  recommend you leave this option at the default, 1 (which means  Enabling multithreading will cause FUSE to make multiple simultaneous calls
225  unthreaded, no threads will be used and no reentrancy is needed).  into the various callback functions of your perl script.  If you enable
226    threaded mode, you can enjoy all the parallel execution and interactive
227    response benefits of threads, and you get to enjoy all the benefits of race
228    conditions and locking bugs, too.  Please also ensure any other perl modules
229    you're using are also thread-safe.
230    
231    (If enabled, this option will cause a warning if your perl interpreter was not
232    built with USE_ITHREADS, or if you have failed to use threads or
233    threads::shared.)
234    
235  =back  =back
236    
237    =head3 Fuse::fuse_get_context
238    
239     use Fuse "fuse_get_context";
240     my $caller_uid = fuse_get_context()->{"uid"};
241     my $caller_gid = fuse_get_context()->{"gid"};
242     my $caller_pid = fuse_get_context()->{"pid"};
243    
244    Access context information about the current Fuse operation.
245    
246  =head2 FUNCTIONS YOUR FILESYSTEM MAY IMPLEMENT  =head2 FUNCTIONS YOUR FILESYSTEM MAY IMPLEMENT
247    
248  =head3 getattr  =head3 getattr
# Line 349  or Line 415  or
415    
416  -ENOANO(), $namelen, $files, $files_free, $blocks, $blocks_avail, $blocksize  -ENOANO(), $namelen, $files, $files_free, $blocks, $blocks_avail, $blocksize
417    
418    =head3 flush
419    
420    Arguments: Pathname
421    Returns an errno or 0 on success.
422    
423    Called to synchronise any cached data. This is called before the file
424    is closed. It may be called multiple times before a file is closed.
425    
426    =head3 release
427    
428    Arguments: Pathname, numeric flags passed to open
429    Returns an errno or 0 on success.
430    
431    Called to indicate that there are no more references to the file. Called once
432    for every file with the same pathname and flags as were passed to open.
433    
434    =head3 fsync
435    
436    Arguments: Pathname, numeric flags
437    Returns an errno or 0 on success.
438    
439    Called to synchronise the file's contents. If flags is non-zero,
440    only synchronise the user data. Otherwise synchronise the user and meta data.
441    
442    =head3 setxattr
443    
444    Arguments: Pathname, extended attribute's name, extended attribute's value, numeric flags (which is an OR-ing of XATTR_CREATE and XATTR_REPLACE
445    Returns an errno or 0 on success.
446    
447    Called to set the value of the named extended attribute.
448    
449    If you wish to reject setting of a particular form of extended attribute name
450    (e.g.: regexps matching user\..* or security\..*), then return - EOPNOTSUPP.
451    
452    If flags is set to XATTR_CREATE and the extended attribute already exists,
453    this should fail with - EEXIST. If flags is set to XATTR_REPLACE
454    and the extended attribute doesn't exist, this should fail with - ENOATTR.
455    
456    XATTR_CREATE and XATTR_REPLACE are provided by this module, but not exported
457    by default. To import them:
458    
459        use Fuse ':xattr';
460    
461    or:
462    
463        use Fuse ':all';
464    
465    =head3 getxattr
466    
467    Arguments: Pathname, extended attribute's name
468    Returns an errno, 0 if there was no value, or the extended attribute's value.
469    
470    Called to get the value of the named extended attribute.
471    
472    =head3 listxattr
473    
474    Arguments: Pathname
475    Returns a list: 0 or more text strings (the extended attribute names), followed by a numeric errno (usually 0).
476    
477    =head3 removexattr
478    
479    Arguments: Pathname, extended attribute's name
480    Returns an errno or 0 on success.
481    
482  =head1 AUTHOR  =head1 AUTHOR
483    
484  Mark Glines, E<lt>mark@glines.orgE<gt>  Mark Glines, E<lt>mark@glines.orgE<gt>

Legend:
Removed from v.7  
changed lines
  Added in v.120

  ViewVC Help
Powered by ViewVC 1.1.26