Fuse-Class

 view release on metacpan or  search on metacpan

lib/Fuse/Class.pm  view on Meta::CPAN

Start a main loop. Filesystem is mounted to the mountpoint pointed by
option "mountpoint".

Options are taken as key=>value pair selected from following:

=over

=item debug => boolean

This option controls tracing on or off. (Default is off).

=item mountpoint => "path_to_mountpoint"

Directory name to mount filesystem like "/mnt/mypoint".
This option has no default value and is mandatory.

=item mountopts => "opt1,op2,..."

Comma separated options for FUSE kernel module.

=item nullpath_ok => boolean

If true, empty pathname is passed to the methods like read, write, flush,
release, fsync, readdir, releasedir, fsyncdir, ftruncate, fgetattr and lock.

To use this option, you must return file/directory handle from
open, opendir and create, and you must operate file/directory by
that handle insted of pathname.

Only effective on Fuse 2.8 or later.

=back

For more information, see the documentation of Fuse.

=cut

sub main {
    my $self = shift;
    my %attr = @_;

    my @args;
    for my $opt (qw(debug mountpoint mountopts nullpath_ok)) {
	push(@args, $opt, $attr{$opt}) if (defined($attr{$opt}));
    }

    local $_Module = $self;

    my %fnmap;
    foreach my $fnname (@callback) {
        if ($_Module->can($fnname)) {
            $fnmap{$fnname} = __PACKAGE__ . '::_' . $fnname;
        }
    }

    Fuse::main(@args, %fnmap);
}

BEGIN {
    @callback = qw (getattr readlink getdir mknod mkdir unlink
		    rmdir symlink rename link chmod chown truncate
		    utime open read write statfs flush release fsync
		    setxattr getxattr listxattr removexattr);
    if (Fuse->can('fuse_version')) {
        my $fuse_version = Fuse::fuse_version();
        if ($fuse_version >= 2.3) {
            push(@callback, qw(opendir readdir releasedir fsyncdir init destroy));
        }
        if ($fuse_version >= 2.5) {
            push(@callback, qw(access create ftruncate fgetattr));
        }
        if ($fuse_version >= 2.6) {
            push(@callback, qw(lock utimens bmap));
        }
    }

    no strict "refs";
    for my $m (@callback) {
	my $method = __PACKAGE__ . "::_$m";

	*$method = sub {
	    my $method_name = $m;

	    if ($_Module->can($method_name)) {
		my @ret = eval {
		    $_Module->$m(@_);
		};
		if ($@) {
		  return $! ? -$! : -Errno::EPERM();
		}
		else {
		  return (wantarray() ? @ret : $ret[0]);
		}
	    }
	    else {
		return -Errno::EPERM();
	    }
	}
    }
}

=head1 METHODS MAY BE OVERRIDDEN

=cut

=head2 getattr(PATH_NAME)

Return a list of file attributes. Meaning of fields are same as
"stat" function like this:

  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
   $atime,$mtime,$ctime,$blksize,$blocks)

On error, return scalar value like -ENOENT().

=head2 readlink(PATH_NAME)

This method is called to dereference symbolic link.
Return a destination path string or numeric error value.

By Default implementation, returns -ENOENT().
You can leave this method if your FS does not have symlink.

=cut

sub readlink {
    return -Errno::ENOENT();
}

=head2 getdir(DIRECTORY_NAME)

Return a list of file/directory names and an errno (0 if success).
ex: ('..', '.', 'a', 'b', 0)

If 'readdir' method is implemented, this function will never be called.

=head2 mknod(PATH_NAME, MODE, DEVNO)

Return an errno (0 if success).
This method is called to create an entity (device or file).

=head2 mkdir(DIRECTORY_NAME, MODE)

Return an errno (0 if success).
This method is called to create a directory.

=head2 unlink(PATH_NAME)

Return an errno (0 if success).
This method is called to remove an entity (device, file or symlink).

=head2 rmdir(PATH_NAME)

Return an errno (0 if success).
This method is called to remove a directory.

=head2 symlink(EXISTING_PATH_NAME, SYMLINK_NAME)

Return an errno (0 if success).
This method is called to create a symbolic link.

=head2 rename(OLD_NAME, NEW_NAME)

Return an errno (0 if success).
This method is called to rename/move a entity.

=head2 link(EXISTING_PATH_NAME, HADLINK_NAME)

Return an errno (0 if success).
This method is called to create a hard link.

=head2 chmod(PATH_NAME, MODE).

Return an errno (0 if success).
This method is called to change permissions on a entity.

=head2 chown(PATH_NAME, UID, GID).

Return an errno (0 if success).
This method is called to change ownership of a entity.

=head2 truncate(PATH_NAME, OFFSET).

Return an errno (0 if success).
This method is called to truncate a file at the given offset.

=head2 utime(PATH_NAME, ACCESS_TIME, MODIFIED_TIME).

Return an errno (0 if success).
This method is called to change atime/mtime on a entity.

=head2 open(PATH_NAME, FLAGS, FILE_INFO)

Return an errno, and a file handle (optional)

First style means like this:

  return 0; # success

and second one is following:

  return (0, $file_handle_you_made); # success and handle

FLAGS is an OR-combined value of flags (O_RDONLY, O_SYNC, etc).
FILE_INFO is a hashref.

Returned file handle will be passed to subsequent method call
to operate on opend file.

=head2 read(PATH_NAME, SIZE, OFFSET, FILE_HANDLE)

Return an errno, or string scalar of read data.

This method is called to read data (SIZE bytes)
at the given offset of opened file.

=head2 write(PATH_NAME, BUFFER, OFFSET, FILE_HANDLE)

Return a written byte size or an errno.

This method is called to write data (BUFFER)
at the given offset of opened file.

=head2 statfs

Return status of filesystem in one of follwing style:

=over

=item -ENOANO()

or

=item $namelen, $files, $files_free, $blocks, $blocks_avail, $blocksize

or



( run in 1.684 second using v1.01-cache-2.11-cpan-71847e10f99 )