--- /dev/null
+#!/usr/bin/perl -w
+use strict;
+use threads;
+use threads::shared;
+
+use Fuse;
+use POSIX qw(ENOENT EISDIR EINVAL);
+
+my (%files) = (
+ '.' => {
+ type => 0040,
+ mode => 0755,
+ ctime => time()-1000
+ },
+ a => {
+ cont => "File 'a'.\n",
+ type => 0100,
+ mode => 0755,
+ ctime => time()-2000
+ },
+ b => {
+ cont => "This is file 'b'.\n",
+ type => 0100,
+ mode => 0644,
+ ctime => time()-1000
+ },
+);
+
+sub filename_fixup {
+ my ($file) = shift;
+ $file =~ s,^/,,;
+ $file = '.' unless length($file);
+ return $file;
+}
+
+sub e_getattr {
+ my ($file) = filename_fixup(shift);
+ $file =~ s,^/,,;
+ $file = '.' unless length($file);
+ return -ENOENT() unless exists($files{$file});
+ my ($size) = exists($files{$file}{cont}) ? length($files{$file}{cont}) : 0;
+ my ($modes) = ($files{$file}{type}<<9) + $files{$file}{mode};
+ my ($dev, $ino, $rdev, $blocks, $gid, $uid, $nlink, $blksize) = (0,0,0,1,0,0,1,1024);
+ my ($atime, $ctime, $mtime);
+ $atime = $ctime = $mtime = $files{$file}{ctime};
+ # 2 possible types of return values:
+ #return -ENOENT(); # or any other error you care to
+ #print(join(",",($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks)),"\n");
+ return ($dev,$ino,$modes,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks);
+}
+
+sub e_getdir {
+ # return as many text filenames as you like, followed by the retval.
+ print((scalar keys %files)."\n");
+ return (keys %files),0;
+}
+
+sub e_open {
+ # VFS sanity check; it keeps all the necessary state, not much to do here.
+ my ($file) = filename_fixup(shift);
+ print("open called\n");
+ return -ENOENT() unless exists($files{$file});
+ return -EISDIR() unless exists($files{$file}{cont});
+ print("open ok\n");
+ return 0;
+}
+
+sub e_read {
+ # return an error numeric, or binary/text string. (note: 0 means EOF, "0" will
+ # give a byte (ascii "0") to the reading program)
+ my ($file) = filename_fixup(shift);
+ my ($buf,$off) = @_;
+ return -ENOENT() unless exists($files{$file});
+ return -EINVAL() if $off > length($files{$file}{cont});
+ return 0 if $off == length($files{$file}{cont});
+ return substr($files{$file}{cont},$off,$buf);
+}
+
+sub e_statfs { return 255, 1, 1, 1, 1, 2 }
+
+# If you run the script directly, it will run fusermount, which will in turn
+# re-run this script. Hence the funky semantics.
+my ($mountpoint) = "";
+$mountpoint = shift(@ARGV) if @ARGV;
+Fuse::main(
+ mountpoint=>$mountpoint,
+ getattr=>"main::e_getattr",
+ getdir =>"main::e_getdir",
+ open =>"main::e_open",
+ statfs =>"main::e_statfs",
+ read =>"main::e_read",
+ threaded=>1
+);
--- /dev/null
+#!/usr/bin/perl -w
+use strict;
+use threads;
+use threads::shared;
+
+use Fuse;
+use IO::File;
+use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);
+use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET);
+require 'syscall.ph'; # for SYS_mknod and SYS_lchown
+
+sub fixup { return "/tmp/fusetest-" . $ENV{LOGNAME} . shift }
+
+sub x_getattr {
+ my ($file) = fixup(shift);
+ my (@list) = lstat($file);
+ return -$! unless @list;
+ return @list;
+}
+
+sub x_getdir {
+ my ($dirname) = fixup(shift);
+ unless(opendir(DIRHANDLE,$dirname)) {
+ return -ENOENT();
+ }
+ my (@files) = readdir(DIRHANDLE);
+ closedir(DIRHANDLE);
+ return (@files, 0);
+}
+
+sub x_open {
+ my ($file) = fixup(shift);
+ my ($mode) = shift;
+ return -$! unless sysopen(FILE,$file,$mode);
+ close(FILE);
+ return 0;
+}
+
+sub x_read {
+ my ($file,$bufsize,$off) = @_;
+ my ($rv) = -ENOSYS();
+ my ($handle) = new IO::File;
+ return -ENOENT() unless -e ($file = fixup($file));
+ my ($fsize) = -s $file;
+ return -ENOSYS() unless open($handle,$file);
+ if(seek($handle,$off,SEEK_SET)) {
+ read($handle,$rv,$bufsize);
+ }
+ return $rv;
+}
+
+sub x_write {
+ my ($file,$buf,$off) = @_;
+ my ($rv);
+ return -ENOENT() unless -e ($file = fixup($file));
+ my ($fsize) = -s $file;
+ return -ENOSYS() unless open(FILE,'+<',$file);
+ if($rv = seek(FILE,$off,SEEK_SET)) {
+ $rv = print(FILE $buf);
+ }
+ $rv = -ENOSYS() unless $rv;
+ close(FILE);
+ return length($buf);
+}
+
+sub err { return (-shift || -$!) }
+
+sub x_readlink { return readlink(fixup(shift)); }
+sub x_unlink { return unlink(fixup(shift)) ? 0 : -$!; }
+
+sub x_symlink { print "symlink\n"; return symlink(shift,fixup(shift)) ? 0 : -$!; }
+
+sub x_rename {
+ my ($old) = fixup(shift);
+ my ($new) = fixup(shift);
+ my ($err) = rename($old,$new) ? 0 : -ENOENT();
+ return $err;
+}
+sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
+sub x_chown {
+ my ($fn) = fixup(shift);
+ print "nonexistent $fn\n" unless -e $fn;
+ my ($uid,$gid) = @_;
+ # perl's chown() does not chown symlinks, it chowns the symlink's
+ # target. it fails when the link's target doesn't exist, because
+ # the stat64() syscall fails.
+ # this causes error messages when unpacking symlinks in tarballs.
+ my ($err) = syscall(&SYS_lchown,$fn,$uid,$gid,$fn) ? -$! : 0;
+ return $err;
+}
+sub x_chmod {
+ my ($fn) = fixup(shift);
+ my ($mode) = shift;
+ my ($err) = chmod($mode,$fn) ? 0 : -$!;
+ return $err;
+}
+sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }
+sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
+
+sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
+sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
+
+sub x_mknod {
+ # since this is called for ALL files, not just devices, I'll do some checks
+ # and possibly run the real mknod command.
+ my ($file, $modes, $dev) = @_;
+ $file = fixup($file);
+ $! = 0;
+ syscall(&SYS_mknod,$file,$modes,$dev);
+ return -$!;
+}
+
+# kludge
+sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
+my ($mountpoint) = "";
+$mountpoint = shift(@ARGV) if @ARGV;
+Fuse::main(
+ mountpoint=>$mountpoint,
+ getattr =>"main::x_getattr",
+ readlink=>"main::x_readlink",
+ getdir =>"main::x_getdir",
+ mknod =>"main::x_mknod",
+ mkdir =>"main::x_mkdir",
+ unlink =>"main::x_unlink",
+ rmdir =>"main::x_rmdir",
+ symlink =>"main::x_symlink",
+ rename =>"main::x_rename",
+ link =>"main::x_link",
+ chmod =>"main::x_chmod",
+ chown =>"main::x_chown",
+ truncate=>"main::x_truncate",
+ utime =>"main::x_utime",
+ open =>"main::x_open",
+ read =>"main::x_read",
+ write =>"main::x_write",
+ statfs =>"main::x_statfs",
+ threaded=>1,
+);