wait for child. mj (mini-json, very pure)

master
Denis Knauf 2010-09-30 22:57:06 +02:00
parent aa55caba36
commit 500d522e2d
3 changed files with 31 additions and 31 deletions

4
c.rb
View File

@ -3,10 +3,6 @@
libexec = File.expand_path File.dirname( __FILE__) libexec = File.expand_path File.dirname( __FILE__)
machine, source, destination = ARGV[0...3] machine, source, destination = ARGV[0...3]
def ts
Time.now.strftime '%Y-%m-%d %H:%M:%S'
end
class IO class IO
def readall def readall
buf = '' buf = ''

30
r.pl
View File

@ -5,31 +5,32 @@ use warnings;
use POSIX qw(strftime); use POSIX qw(strftime);
$|++; $|++;
sub ts { END { wait; };
strftime '%Y-%m-%d %H:%M:%S', localtime;
}
$SIG{CLD} = sub { $SIG{CLD} = sub {
wait; wait;
#printf STDERR "{ts: \"".ts."\", proc: \"r\", action: \"exit\", code: $?, error: \"child_died\"}\n";
exit $?; exit $?;
}; };
sub readcmd { sub mj {
%_ = @_;
'{ts:"'.strftime( '%Y-%m-%d %H:%M:%S', localtime).'", proc:"s", '.join( ', ', map "$_:\"$_{$_}\"", keys%_)."}\n";
}
sub readg {
my $data = ''; my $data = '';
while( length( $data) < 6) { while( length( $data) < $_[0]) {
read( STDIN, $data, 6-length($data), length($data)) or return(0); read( STDIN, $data, $_[0]-length($data), length($data)) or return(0);
} }
$data; $data;
} }
chdir $ARGV[0] or die( "{ts: \"".ts."\", proc: \"r\", error: \"cannot_chdir\", exception: \"$!\"}\n"); chdir $ARGV[0] or die( mj( error=>'cannot_chdir', exception=>$!));
while( my$data = readcmd) { while( my$data = readg(6)) {
(my$cmd, my$length) = unpack( 'nN', $data); my( $cmd, $length) = unpack( 'nN', $data);
#print STDERR "{ts: \"".ts."\", cmd: $cmd, length: $length}\n"; $data = readg $length;
read STDIN, $data, $length;
if( 1 == $cmd) { if( 1 == $cmd) {
open( F, '>>', $data) or die( "{ts: \"".ts."\", proc: \"r\", error: \"unable_to_open_file\", message: \"Can't open file <$data>.\"}\n"); open( F, '>>', $data) or die( mj( error=>'unable_to_open_file', message=>"Can't open file <$data>"));
my@stat = stat F; my@stat = stat F;
print pack( 'N', $stat[7]); print pack( 'N', $stat[7]);
} }
@ -37,8 +38,7 @@ while( my$data = readcmd) {
print F $data; print F $data;
} }
else { else {
die( "{ts: \"".ts."\", proc: \"r\", error: \"unknown_command\", command: $cmd}\n"); die( mj( error=>'unknown_command', command=>$cmd));
} }
} }
#print STDERR "{ts: \"".ts."\", proc: \"r\", exit: 0}\n";
exit 0; exit 0;

28
s.pl
View File

@ -1,35 +1,39 @@
#!/usr/bin/env perl #!/usr/bin/env perl
use strict; use strict;
#use warnings; # Old perl
use POSIX qw(strftime); use POSIX qw(strftime);
$|++; $|++;
sub ts { sub mj {
strftime '%Y-%m-%d %H:%M:%S', localtime; %_ = @_;
'{ts:"'.strftime( '%Y-%m-%d %H:%M:%S', localtime).'", proc:"s", '.join( ', ', map "$_:\"$_{$_}\"", keys%_)."}\n";
}
sub readg {
my $data = '';
while( length( $data) < $_[0]) {
read( STDIN, $data, $_[0]-length($data), length($data)) or return(0);
}
$data;
} }
my$dir = shift @ARGV; my$dir = shift @ARGV;
$dir =~ /^(\/.*)\/([^\/]+)$/ or die( "{ts: \"".ts."\", proc: \"s\", error: \"invalid_path_expression\", message: \"Path-Expression is invalid.\"}\n"); $dir =~ /^(\/.*)\/([^\/]+)$/ or die( mj( error=>'invalid_path_expression', message=>'Path-Expression is invalid.'));
($dir, my$fexpr) = ($1, $2); ($dir, my$fexpr) = ($1, $2);
chdir( $dir) or die( "{ts: \"".ts."\", proc: \"s\", error: \"change_directory\", value: \"$dir\", message: \"$!\"}\n"); chdir( $dir) or die( mj( error=>'change_directory', value=>$dir, message=>$!));
opendir( DH, '.') || die "{ts: \"".ts."\", proc: \"s\", error: \"dir_not_found\", message: \"Directory not found.\"}\n"; opendir( DH, '.') or die( mj( error=>'dir_not_found', message=>'Directory not found.'));
while( my$filename = readdir( DH)) { while( my$filename = readdir( DH)) {
$filename =~ /$fexpr/ or next; next unless $filename =~ /$fexpr/ and -f $filename;
-f $filename or next;
print pack( 'nNA*', 1, length($filename), $filename); print pack( 'nNA*', 1, length($filename), $filename);
print STDERR "{ts: \"".ts."\", proc: \"s\", action: \"open\", file: \"$filename\"}\n";
open F, $filename; open F, $filename;
read STDIN, my$length, 4; # Was wenn < 4 ? my$length = readg 4;
$length = unpack 'N', $length; $length = unpack 'N', $length;
seek F, $length, 0; seek F, $length, 0;
while( read( F, my$r, 1400)) { while( read( F, my$r, 1400)) {
print pack( 'nNA*', 2, length($r), $r); print pack( 'nNA*', 2, length($r), $r);
} }
print STDERR "{ts: \"".ts."\", proc: \"s\", action: \"close\", file: \"$filename\"}\n";
close F; close F;
} }
closedir DH; closedir DH;
#print STDERR "{ts: \"".ts."\", proc: \"s\", exit: 0}\n";
exit( 0); exit( 0);