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