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