Lion compat for -lfuse_ino64 hack.
[perl-fuse.git] / examples / example.pl
1 #!/usr/bin/perl -w
2 use strict;
3
4 use Data::Dumper;
5
6 #use blib;
7 use Fuse qw(fuse_get_context);
8 use POSIX qw(ENOENT EISDIR EINVAL);
9
10 my (%files) = (
11         '.' => {
12                 type => 0040,
13                 mode => 0755,
14                 ctime => time()-1000
15         },
16         a => {
17                 cont => "File 'a'.\n",
18                 type => 0100,
19                 mode => 0755,
20                 ctime => time()-2000
21         },
22         b => {
23                 cont => "This is file 'b'.\n",
24                 type => 0100,
25                 mode => 0644,
26                 ctime => time()-1000
27         },
28         me => {
29                 size => 45,
30                 type => 0100,
31                 mode => 0644,
32                 ctime => time()-1000
33         },
34 );
35
36 sub filename_fixup {
37         my ($file) = shift;
38         $file =~ s,^/,,;
39         $file = '.' unless length($file);
40         return $file;
41 }
42
43 sub e_getattr {
44         my ($file) = filename_fixup(shift);
45         $file =~ s,^/,,;
46         $file = '.' unless length($file);
47         return -ENOENT() unless exists($files{$file});
48         my ($size) = exists($files{$file}{cont}) ? length($files{$file}{cont}) : 0;
49         $size = $files{$file}{size} if exists $files{$file}{size};
50         my ($modes) = ($files{$file}{type}<<9) + $files{$file}{mode};
51         my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = (0,0,0,1,0,0,1,1024);
52         my ($atime, $ctime, $mtime);
53         $atime = $ctime = $mtime = $files{$file}{ctime};
54         # 2 possible types of return values:
55         #return -ENOENT(); # or any other error you care to
56         #print(join(",",($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)),"\n");
57         return ($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
58 }
59
60 sub e_getdir {
61         # return as many text filenames as you like, followed by the retval.
62         print((scalar keys %files)."\n");
63         return (keys %files),0;
64 }
65
66 sub e_open {
67         # VFS sanity check; it keeps all the necessary state, not much to do here.
68     my $file = filename_fixup(shift);
69     my ($flags, $fileinfo) = @_;
70     print("open called $file, $flags, $fileinfo\n");
71         return -ENOENT() unless exists($files{$file});
72         return -EISDIR() if $files{$file}{type} & 0040;
73     
74     my $fh = [ rand() ];
75     
76     print("open ok (handle $fh)\n");
77     return (0, $fh);
78 }
79
80 sub e_read {
81         # return an error numeric, or binary/text string.  (note: 0 means EOF, "0" will
82         # give a byte (ascii "0") to the reading program)
83         my ($file) = filename_fixup(shift);
84     my ($buf, $off, $fh) = @_;
85     print "read from $file, $buf \@ $off\n";
86     print "file handle:\n", Dumper($fh);
87         return -ENOENT() unless exists($files{$file});
88         if(!exists($files{$file}{cont})) {
89                 return -EINVAL() if $off > 0;
90                 my $context = fuse_get_context();
91                 return sprintf("pid=0x%08x uid=0x%08x gid=0x%08x\n",@$context{'pid','uid','gid'});
92         }
93         return -EINVAL() if $off > length($files{$file}{cont});
94         return 0 if $off == length($files{$file}{cont});
95         return substr($files{$file}{cont},$off,$buf);
96 }
97
98 sub e_statfs { return 255, 1, 1, 1, 1, 2 }
99
100 # If you run the script directly, it will run fusermount, which will in turn
101 # re-run this script.  Hence the funky semantics.
102 my ($mountpoint) = "";
103 $mountpoint = shift(@ARGV) if @ARGV;
104 Fuse::main(
105         mountpoint=>$mountpoint,
106         getattr=>"main::e_getattr",
107         getdir =>"main::e_getdir",
108         open   =>"main::e_open",
109         statfs =>"main::e_statfs",
110         read   =>"main::e_read",
111         threaded=>0
112 );