5 use POSIX qw(ENOENT ENOSYS EEXIST EPERM O_RDONLY O_RDWR O_APPEND O_CREAT);
6 use Fcntl qw(S_ISBLK S_ISCHR S_ISFIFO SEEK_SET);
8 require 'syscall.ph'; # for SYS_mknod and SYS_lchown
10 my ($rootdir) = @ARGV;
12 # strip leading and trailing slashes
13 $rootdir = $1 if($rootdir =~ /^\/?(.*)\/?$/);
15 sub fixup { return "/$rootdir" . shift }
18 my ($file) = fixup(shift);
19 my (@list) = lstat($file);
20 return -$! unless @list;
25 my ($dirname) = fixup(shift);
26 unless(opendir(DIRHANDLE,$dirname)) {
29 my (@files) = readdir(DIRHANDLE);
35 my ($file) = fixup(shift);
37 return -$! unless sysopen(FILE,$file,$mode);
43 my ($file,$bufsize,$off) = @_;
45 my ($handle) = new IO::File;
46 return -ENOENT() unless -e ($file = fixup($file));
47 my ($fsize) = -s $file;
48 return -ENOSYS() unless open($handle,$file);
49 if(seek($handle,$off,SEEK_SET)) {
50 read($handle,$rv,$bufsize);
56 my ($file,$buf,$off) = @_;
58 return -ENOENT() unless -e ($file = fixup($file));
59 my ($fsize) = -s $file;
60 return -ENOSYS() unless open(FILE,'+<',$file);
61 if($rv = seek(FILE,$off,SEEK_SET)) {
62 $rv = print(FILE $buf);
64 $rv = -ENOSYS() unless $rv;
69 sub err { return (-shift || -$!) }
71 sub x_readlink { return readlink(fixup(shift) ); }
72 sub x_unlink { return unlink(fixup(shift)) ? 0 : -$!; }
73 sub x_rmdir { return err(rmdir(fixup(shift)) ); }
75 sub x_symlink { print "symlink\n"; return symlink(shift,fixup(shift)) ? 0 : -$!; }
78 my ($old) = fixup(shift);
79 my ($new) = fixup(shift);
80 my ($err) = rename($old,$new) ? 0 : -ENOENT();
83 sub x_link { return link(fixup(shift),fixup(shift)) ? 0 : -$! }
85 my ($fn) = fixup(shift);
86 print "nonexistent $fn\n" unless -e $fn;
88 # perl's chown() does not chown symlinks, it chowns the symlink's
89 # target. it fails when the link's target doesn't exist, because
90 # the stat64() syscall fails.
91 # this causes error messages when unpacking symlinks in tarballs.
92 my ($err) = syscall(&SYS_lchown,$fn,$uid,$gid,$fn) ? -$! : 0;
96 my ($fn) = fixup(shift);
98 my ($err) = chmod($mode,$fn) ? 0 : -$!;
101 sub x_truncate { return truncate(fixup(shift),shift) ? 0 : -$! ; }
102 sub x_utime { return utime($_[1],$_[2],fixup($_[0])) ? 0:-$!; }
104 sub x_mkdir { my ($name, $perm) = @_; return 0 if mkdir(fixup($name),$perm); return -$!; }
105 sub x_rmdir { return 0 if rmdir fixup(shift); return -$!; }
108 # since this is called for ALL files, not just devices, I'll do some checks
109 # and possibly run the real mknod command.
110 my ($file, $modes, $dev) = @_;
111 $file = fixup($file);
113 syscall(&SYS_mknod,$file,$modes,$dev);
118 sub x_statfs {return 255,1000000,500000,1000000,500000,4096}
122 while(read(STDIN,$len,9) == 9) {
124 my ($data,$VAR1,@args);
126 $SIG{ALRM} = sub { die "timeout\n"};
129 read(STDIN,$data,$len-length($data),length($data))
130 while(length($data) < $len);
136 my $cmd = shift(@args);
137 exit 0 if $cmd eq "bye";
138 die "cannot find command $cmd\n" unless exists($main::{"x_$cmd"});
139 @args = $main::{"x_$cmd"}(@args);
140 $cmd = Dumper(\@args)."\n";
141 $cmd = sprintf("%08i\n%s",length($cmd),$cmd);