7 use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);
8 use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET);
9 require 'syscall.ph'; # for SYS_mknod and SYS_lchown
12 my $tmp_path = "/tmp/fusetest-" . $ENV{LOGNAME};
14 mkdir($tmp_path) || die "can't create $tmp_path: $!";
17 sub fixup { return $tmp_path . shift }
20 my ($file) = fixup(shift);
21 my (@list) = lstat($file);
22 return -$! unless @list;
27 my ($dirname) = fixup(shift);
28 unless(opendir(DIRHANDLE,$dirname)) {
31 my (@files) = readdir(DIRHANDLE);
37 my ($file) = fixup(shift);
39 return -$! unless sysopen(FILE,$file,$mode);
45 my ($file,$bufsize,$off) = @_;
47 my ($handle) = new IO::File;
48 return -ENOENT() unless -e ($file = fixup($file));
49 my ($fsize) = -s $file;
50 return -ENOSYS() unless open($handle,$file);
51 if(seek($handle,$off,SEEK_SET)) {
52 read($handle,$rv,$bufsize);
58 my ($file,$buf,$off) = @_;
60 return -ENOENT() unless -e ($file = fixup($file));
61 my ($fsize) = -s $file;
62 return -ENOSYS() unless open(FILE,'+<',$file);
63 if($rv = seek(FILE,$off,SEEK_SET)) {
64 $rv = print(FILE $buf);
66 $rv = -ENOSYS() unless $rv;
71 sub err { return (-shift || -$!) }
73 sub x_readlink { return readlink(fixup(shift) ); }
74 sub x_unlink { return unlink(fixup(shift)) ? 0 : -$!; }
75 sub x_rmdir { return err(rmdir(fixup(shift)) ); }
77 sub x_symlink { print "symlink\n"; return symlink(shift,fixup(shift)) ? 0 : -$!; }
80 my ($old) = fixup(shift);
81 my ($new) = fixup(shift);
82 my ($err) = rename($old,$new) ? 0 : -ENOENT();
88 print "link $from -> $to\n";
89 my ($err) = link(fixup($from),fixup($to)) ? 0 : -$!;
93 my ($fn) = fixup(shift);
94 print "nonexistent $fn\n" unless -e $fn;
96 # perl's chown() does not chown symlinks, it chowns the symlink's
97 # target. it fails when the link's target doesn't exist, because
98 # the stat64() syscall fails.
99 # this causes error messages when unpacking symlinks in tarballs.
100 my ($err) = syscall(&SYS_lchown,$fn,$uid,$gid,$fn) ? -$! : 0;
104 my ($fn) = fixup(shift);
106 my ($err) = chmod($mode,$fn) ? 0 : -$!;
109 sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }
110 sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
112 sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
113 sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
116 # since this is called for ALL files, not just devices, I'll do some checks
117 # and possibly run the real mknod command.
118 my ($file, $modes, $dev) = @_;
119 $file = fixup($file);
121 syscall(&SYS_mknod,$file,$modes,$dev);
127 my $name = fixup(shift);
128 my($bsize, $frsize, $blocks, $bfree, $bavail,
129 $files, $ffree, $favail, $fsid, $basetype, $flag,
130 $namemax, $fstr) = statvfs("/tmp") || return -$!;
131 return ($namemax, $files, $ffree, $blocks, $bavail, $bsize);
134 my ($mountpoint) = "";
135 $mountpoint = shift(@ARGV) if @ARGV;
137 mountpoint=>$mountpoint,
138 getattr =>"main::x_getattr",
139 readlink=>"main::x_readlink",
140 getdir =>"main::x_getdir",
141 mknod =>"main::x_mknod",
142 mkdir =>"main::x_mkdir",
143 unlink =>"main::x_unlink",
144 rmdir =>"main::x_rmdir",
145 symlink =>"main::x_symlink",
146 rename =>"main::x_rename",
147 link =>"main::x_link",
148 chmod =>"main::x_chmod",
149 chown =>"main::x_chown",
150 truncate=>"main::x_truncate",
151 utime =>"main::x_utime",
152 open =>"main::x_open",
153 read =>"main::x_read",
154 write =>"main::x_write",
155 statfs =>"main::x_statfs",