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