From: Richard Dawe <rich(at)phekda(dot)gotadsl(dot)co(dot)uk>
[perl-fuse.git] / examples / loopback.pl
1 #!/usr/bin/perl
2
3 use strict;
4 use Fuse;
5 use IO::File;
6 use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);
7 use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET);
8 require 'syscall.ph'; # for SYS_mknod and SYS_lchown
9
10 sub fixup { return "/tmp/fusetest-" . $ENV{LOGNAME} . shift }
11
12 sub x_getattr {
13         my ($file) = fixup(shift);
14         my (@list) = lstat($file);
15         return -$! unless @list;
16         return @list;
17 }
18
19 sub x_getdir {
20         my ($dirname) = fixup(shift);
21         unless(opendir(DIRHANDLE,$dirname)) {
22                 return -ENOENT();
23         }
24         my (@files) = readdir(DIRHANDLE);
25         closedir(DIRHANDLE);
26         return (@files, 0);
27 }
28
29 sub x_open {
30         my ($file) = fixup(shift);
31         my ($mode) = shift;
32         return -$! unless sysopen(FILE,$file,$mode);
33         close(FILE);
34         return 0;
35 }
36
37 sub x_read {
38         my ($file,$bufsize,$off) = @_;
39         my ($rv) = -ENOSYS();
40         my ($handle) = new IO::File;
41         return -ENOENT() unless -e ($file = fixup($file));
42         my ($fsize) = -s $file;
43         return -ENOSYS() unless open($handle,$file);
44         if(seek($handle,$off,SEEK_SET)) {
45                 read($handle,$rv,$bufsize);
46         }
47         return $rv;
48 }
49
50 sub x_write {
51         my ($file,$buf,$off) = @_;
52         my ($rv);
53         return -ENOENT() unless -e ($file = fixup($file));
54         my ($fsize) = -s $file;
55         return -ENOSYS() unless open(FILE,'+<',$file);
56         if($rv = seek(FILE,$off,SEEK_SET)) {
57                 $rv = print(FILE $buf);
58         }
59         $rv = -ENOSYS() unless $rv;
60         close(FILE);
61         return length($buf);
62 }
63
64 sub err { return (-shift || -$!) }
65
66 sub x_readlink { return readlink(fixup(shift)                 ); }
67 sub x_unlink { return unlink(fixup(shift)) ? 0 : -$!;          }
68 sub x_rmdir { return err(rmdir(fixup(shift))               ); }
69
70 sub x_symlink { print "symlink\n"; return symlink(shift,fixup(shift)) ? 0 : -$!; }
71
72 sub x_rename {
73         my ($old) = fixup(shift);
74         my ($new) = fixup(shift);
75         my ($err) = rename($old,$new) ? 0 : -ENOENT();
76         return $err;
77 }
78 sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
79 sub x_chown {
80         my ($fn) = fixup(shift);
81         print "nonexistent $fn\n" unless -e $fn;
82         my ($uid,$gid) = @_;
83         # perl's chown() does not chown symlinks, it chowns the symlink's
84         # target.  it fails when the link's target doesn't exist, because
85         # the stat64() syscall fails.
86         # this causes error messages when unpacking symlinks in tarballs.
87         my ($err) = syscall(&SYS_lchown,$fn,$uid,$gid,$fn) ? -$! : 0;
88         return $err;
89 }
90 sub x_chmod {
91         my ($fn) = fixup(shift);
92         my ($mode) = shift;
93         my ($err) = chmod($mode,$fn) ? 0 : -$!;
94         return $err;
95 }
96 sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }
97 sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
98
99 sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
100 sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
101
102 sub x_mknod {
103         # since this is called for ALL files, not just devices, I'll do some checks
104         # and possibly run the real mknod command.
105         my ($file, $modes, $dev) = @_;
106         $file = fixup($file);
107         $! = 0;
108         syscall(&SYS_mknod,$file,$modes,$dev);
109         return -$!;
110 }
111
112 # kludge
113 sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
114 my ($mountpoint) = "";
115 $mountpoint = shift(@ARGV) if @ARGV;
116 Fuse::main(
117         mountpoint=>$mountpoint,
118         getattr=>\&x_getattr,
119         readlink=>\&x_readlink,
120         getdir=>\&x_getdir,
121         mknod=>\&x_mknod,
122         mkdir=>\&x_mkdir,
123         unlink=>\&x_unlink,
124         rmdir=>\&x_rmdir,
125         symlink=>\&x_symlink,
126         rename=>\&x_rename,
127         link=>\&x_link,
128         chmod=>\&x_chmod,
129         chown=>\&x_chown,
130         truncate=>\&x_truncate,
131         utime=>\&x_utime,
132         open=>\&x_open,
133         read=>\&x_read,
134         write=>\&x_write,
135         statfs=>\&x_statfs,
136 );