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