This commit is contained in:
Denis Knauf 2010-09-27 16:33:31 +02:00
parent 7d602e5e31
commit f4680dfd8d
3 changed files with 76 additions and 22 deletions

20
c.rb
View file

@ -1,5 +1,7 @@
#!/usr/bin/env ruby
require 'json'
class IO
def readall
buf = ''
@ -25,15 +27,25 @@ class File
end
end
tor = IO.pipe
tos = IO.pipe
$tor = tor = IO.pipe
$tos = tos = IO.pipe
fork do
if Process.fork
$stdin.reopen tor.first
tor.last.close
$stdout.reopen tos.last
tos.first.close
$stderr.puts( {proc: 'c', connect: ARGV[0], args: ARGV[1]}.to_json)
exec 'ssh', ARGV[0], 'perl', '-e', File.readall( 's.pl').shdump, ARGV[1].shdump
end
else
#$stdout.puts 'test'
#$stderr.print '>> '
#$stdin.each_line {|l| p eval( l); print '>> ' }
$stdin.reopen tos.first
tos.last.close
$stdout.reopen tor.last
tor.first.close
$stderr.puts( {proc: 'c', exec: 'reciever', destination: ARGV[2]}.to_json)
exec 'perl', 'r.pl', ARGV[2].shdump
end

37
r.pl
View file

@ -1,16 +1,47 @@
#!/usr/bin/env perl
while( read( STDIN, my $data, 6)) {
my( $cmd, $length) = unpack 'nN', $data;
read STDIN, my $data, $length;
use strict;
use warnings;
$|++;
$SIG{CLD} = sub {
printf STDERR "{proc: \"r\", action: \"child_died\"}\n";
wait;
printf STDERR "{proc: \"r\", action: \"exit\", code: 0}\n";
exit 0;
};
my @cmds = (
1 => 'file',
2 => 'content'
);
print STDERR "r << s";
sub readcmd {
my $data = '';
while( length( $data) < 6) {
read( STDIN, $data, 6-length($data), length($data)) or return(0);
}
$data;
}
while( my$data = readcmd) {
printf STDERR "{proc: \"r\", read_length: %d}\n", length( $data);
(my$cmd, my$length) = unpack( 'nN', $data);
print STDERR "{cmd: $cmd, length: $length}\n";
print STDERR "r << s\n";
read STDIN, $data, $length;
if( 1 == $cmd) {
print STDERR "{proc: \"r\", action: \"open_file\", file: \"$data\"}\n";
open( F, '>>', $data) or print STDERR ("{proc: \"r\", error: \"unable_to_open_file\", message: \"Can't open file <$data>.\"}\n");
my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks) = stat F;
print STDERR "r >> s\n";
print pack( 'N', $size);
}
elsif( 2 == $cmd) {
print STDERR "r >> f\n";
print F $data;
}
else {

29
s.pl
View file

@ -1,22 +1,33 @@
#!/usr/bin/env perl
use strict;
use warnings;
$|++;
my%cmds = (file => 1, content => 2);
my$dir = shift @ARGV;
$dir =~ /^(\/.*)\/([^\/]+)$/ or die( "{proc: \"s\", error: \"invalid_path_expression\", message: \"Path-Expression is invalid.\"}\n");
($dir, my( $fexpr)) = ($1, $2);
print STDERR $fexpr;
($dir, my$fexpr) = ($1, $2);
chdir( $dir) or die( "{proc: \"s\", error: \"change_directory\", value: \"$dir\", message: \"$!\"}\n");
opendir( my( $dh), '.') || die "{proc: \"s\", error: \"dir_not_found\", message: \"Directory not found.\"}\n";
while( $filename = readdir( $dh)) {
print STDERR "{proc: \"s\", action: \"find\", file: \"$filename\"}\n";
opendir( my$dh, '.') || die "{proc: \"s\", error: \"dir_not_found\", message: \"Directory not found.\"}\n";
while( my$filename = readdir( $dh)) {
$filename =~ /$fexpr/ or next;
print pack( 'nN/(A*)', 1, $filename);
-f $filename or next;
print STDERR "s >> r\n";
print pack( 'nN/A*', $cmds{file}, $filename);
print STDERR "{proc: \"s\", action: \"open\", file: \"$filename\"}\n";
open F, $filename;
print STDERR "s << r\n";
read STDIN, my$length, 4; # Was wenn < 4 ?
seek F, $length, SEEK_SET;
print pack( 'nN/(A*)', 2, $r) while read( F, $r, 2048);
$length = unpack 'N', $length;
print STDERR "s: seek $length\n";
seek F, $length, 0;
print STDERR "s >>>> r\n";
while( read( F, my$r, 2048)) {
print pack( 'nN/(A*)', $cmds{content}, $r);
}
print STDERR "{proc: \"s\", action: \"close\", file: \"$filename\"}\n";
close F;
}