minisync/r.pl

45 lines
876 B
Perl
Raw Normal View History

2010-09-27 13:40:21 +02:00
#!/usr/bin/env perl
2010-09-27 16:33:31 +02:00
use strict;
use warnings;
2010-09-30 16:12:00 +02:00
use POSIX qw(strftime);
2010-09-27 16:33:31 +02:00
$|++;
END { wait; };
2010-09-30 16:12:00 +02:00
2010-09-27 16:33:31 +02:00
$SIG{CLD} = sub {
wait;
2010-09-27 16:56:02 +02:00
exit $?;
2010-09-27 16:33:31 +02:00
};
sub mj {
%_ = @_;
'{ts:"'.strftime( '%Y-%m-%d %H:%M:%S', localtime).'", proc:"s", '.join( ', ', map "$_:\"$_{$_}\"", keys%_)."}\n";
}
sub readg {
2010-09-27 16:33:31 +02:00
my $data = '';
while( length( $data) < $_[0]) {
read( STDIN, $data, $_[0]-length($data), length($data)) or return(0);
2010-09-27 16:33:31 +02:00
}
$data;
}
chdir $ARGV[0] or die( mj( error=>'cannot_chdir', exception=>$!));
while( my$data = readg(6)) {
my( $cmd, $length) = unpack( 'nN', $data);
$data = readg $length;
2010-09-27 13:40:21 +02:00
if( 1 == $cmd) {
open( F, '>>', $data) or die( mj( error=>'unable_to_open_file', message=>"Can't open file <$data>"));
2010-09-27 16:56:02 +02:00
my@stat = stat F;
print pack( 'N', $stat[7]);
2010-09-27 13:40:21 +02:00
}
elsif( 2 == $cmd) {
2010-09-27 15:34:24 +02:00
print F $data;
}
else {
die( mj( error=>'unknown_command', command=>$cmd));
2010-09-27 13:40:21 +02:00
}
}
2010-09-27 15:34:24 +02:00
exit 0;