c.rb removed. r.pl does the job.

This commit is contained in:
Denis Knauf 2010-10-01 00:26:37 +02:00
parent 500d522e2d
commit a830506cfe
3 changed files with 54 additions and 63 deletions

View file

@ -1,12 +1,8 @@
MiniSync
========
This small sync-tool has only 3 files with lesser than 1000Byte.
This small sync-tool has only 2 small files.
c.rb
----
It will start *s.pl* on a remote machine via ssh and (via exec) *r.pl* local.
s.pl
----
@ -16,24 +12,28 @@ It seeks to this Position and sends till *EOF*.
The complete sourcecode will be transfered via ssh and calls it via **perl -e**, you may not copy it.
r.pl
----
This programm is the main-part. It will connect via ssh to the remote machine and
starts *s.pl* remote via **perl -e**.
On the local machine, it will wait, till *s.pl* will request, how many Bytes the local file has.
*r.pl* opens and stats this file and responses. Every content it will append to this file,
till *s.pl* will announce an other file.
Install
=======
First install ruby on your machine.
Perl - i think - is already installed on all your machines.
Usally, there is nothing to do.
Usage
=====
./c.rb <MACHINE> DIR/REGEXP DEST
./r.pl <MACHINE> DIR/REGEXP DEST
This will copy files, which match expression REGEXP in DIR on MACHINE to local-dir DEST.

46
c.rb
View file

@ -1,46 +0,0 @@
#!/usr/bin/env ruby
libexec = File.expand_path File.dirname( __FILE__)
machine, source, destination = ARGV[0...3]
class IO
def readall
buf = ''
loop do
buf << begin
self.sysread 4096
rescue EOFError
return buf
end
end
end
end
class File
def self.readall file
open( file) {|f| f.readall }
end
end
class String
def shdump
"'#{gsub( /[\\']/, '\'\\\\\&\'')}'"
end
end
$tor = tor = IO.pipe
$tos = tos = IO.pipe
Process.fork do
$stdin.reopen tor.first
tor.last.close
$stdout.reopen tos.last
tos.first.close
exec 'ssh', machine, 'perl', '-e', File.readall( File.join( libexec, 's.pl')).shdump, source.shdump
end
$stdin.reopen tos.first
tos.last.close
$stdout.reopen tor.last
tor.first.close
exec 'perl', File.join( libexec, 'r.pl'), destination

55
r.pl
View file

@ -3,14 +3,19 @@
use strict;
use warnings;
use POSIX qw(strftime);
$|++;
END { wait; };
my$child = 0;
__FILE__ =~ /^(.*\/)?[^\/]*?$/;
my$libexec = $1 || '.';
$SIG{CLD} = sub {
wait;
END {
print STDERR "\n";
kill $child, 15;
waitpid $child, 0;
exit $?;
};
}
$SIG{CLD} = $SIG{INT} = sub { exit; };
sub mj {
%_ = @_;
@ -25,20 +30,52 @@ sub readg {
$data;
}
chdir $ARGV[0] or die( mj( error=>'cannot_chdir', exception=>$!));
sub shdump {
my$r = $_[0];
$r =~ s/[\\']/\'\\$&\'/g;
"'".$r."'";
}
my( $machine, $source, $destination) = @ARGV;
pipe SIN, ROUT;
pipe RIN, SOUT;
unless( $child = fork()) {
open F, '<', "$libexec/s.pl" or die( mj( error=>'cannot_open_s.pl', exception=>$!));
sysread F, my $s_pl, -s F;
close F;
close ROUT;
close RIN;
open STDOUT, '>&', \*SOUT;
open STDIN, '<&', \*SIN;
exec 'ssh', $machine, 'perl', '-e', shdump($s_pl), shdump($source);
}
close SOUT;
close SIN;
open STDOUT, '>&', \*ROUT;
open STDIN, '<&', \*RIN;
$|++;
my@f;
chdir $destination or die( mj( error=>'cannot_chdir', exception=>$!));
while( my$data = readg(6)) {
my( $cmd, $length) = unpack( 'nN', $data);
$data = readg $length;
if( 1 == $cmd) {
print STDERR "\n" if $f[0];
open( F, '>>', $data) or die( mj( error=>'unable_to_open_file', message=>"Can't open file <$data>"));
my@stat = stat F;
print pack( 'N', $stat[7]);
@f = ($data, -s F);
print pack( 'N', $f[1]);
}
elsif( 2 == $cmd) {
$f[1] += length $data;
print F $data;
}
else {
die( mj( error=>'unknown_command', command=>$cmd));
}
printf STDERR "\r% 14d %s", $f[1], $f[0];
}
exit 0;
exit;