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