Changes from Marc to implement fuse_get_context():
[perl-fuse.git] / examples / example.pl
index 9ba1117..ed7ce57 100644 (file)
@@ -1,6 +1,8 @@
-#!/usr/bin/perl
+#!/usr/bin/perl -w
+use strict;
 
-use Fuse;
+use blib;
+use Fuse qw(fuse_get_context);
 use POSIX qw(ENOENT EISDIR EINVAL);
 
 my (%files) = (
@@ -21,6 +23,12 @@ my (%files) = (
                mode => 0644,
                ctime => time()-1000
        },
+       me => {
+               size => 45,
+               type => 0100,
+               mode => 0644,
+               ctime => time()-1000
+       },
 );
 
 sub filename_fixup {
@@ -36,6 +44,7 @@ sub e_getattr {
        $file = '.' unless length($file);
        return -ENOENT() unless exists($files{$file});
        my ($size) = exists($files{$file}{cont}) ? length($files{$file}{cont}) : 0;
+       $size = $files{$file}{size} if exists $files{$file}{size};
        my ($modes) = ($files{$file}{type}<<9) + $files{$file}{mode};
        my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = (0,0,0,1,0,0,1,1024);
        my ($atime, $ctime, $mtime);
@@ -57,7 +66,7 @@ sub e_open {
        my ($file) = filename_fixup(shift);
        print("open called\n");
        return -ENOENT() unless exists($files{$file});
-       return -EISDIR() unless exists($files{$file}{cont});
+       return -EISDIR() if $files{$file}{type} & 0040;
        print("open ok\n");
        return 0;
 }
@@ -68,6 +77,11 @@ sub e_read {
        my ($file) = filename_fixup(shift);
        my ($buf,$off) = @_;
        return -ENOENT() unless exists($files{$file});
+       if(!exists($files{$file}{cont})) {
+               return -EINVAL() if $off > 0;
+               my $context = fuse_get_context();
+               return sprintf("pid=0x%08x uid=0x%08x gid=0x%08x\n",@$context{'pid','uid','gid'});
+       }
        return -EINVAL() if $off > length($files{$file}{cont});
        return 0 if $off == length($files{$file}{cont});
        return substr($files{$file}{cont},$off,$buf);
@@ -81,10 +95,10 @@ my ($mountpoint) = "";
 $mountpoint = shift(@ARGV) if @ARGV;
 Fuse::main(
        mountpoint=>$mountpoint,
-       getattr=>\&e_getattr,
-       getdir=>\&e_getdir,
-       open=>\&e_open,
-       statfs=>\&e_statfs,
-       read=>\&e_read,
-       #debug=>1, threaded=>0
+       getattr=>"main::e_getattr",
+       getdir =>"main::e_getdir",
+       open   =>"main::e_open",
+       statfs =>"main::e_statfs",
+       read   =>"main::e_read",
+       threaded=>0
 );