minisync/r.pl

45 lines
1.1 KiB
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
$|++;
2010-09-30 16:12:00 +02:00
sub ts {
strftime '%Y-%m-%d %H:%M:%S', localtime;
}
2010-09-27 16:33:31 +02:00
$SIG{CLD} = sub {
wait;
2010-09-30 16:58:20 +02:00
#printf STDERR "{ts: \"".ts."\", proc: \"r\", action: \"exit\", code: $?, error: \"child_died\"}\n";
2010-09-27 16:56:02 +02:00
exit $?;
2010-09-27 16:33:31 +02:00
};
sub readcmd {
my $data = '';
while( length( $data) < 6) {
read( STDIN, $data, 6-length($data), length($data)) or return(0);
}
$data;
}
2010-09-30 16:12:00 +02:00
chdir $ARGV[0] or die( "{ts: \"".ts."\", proc: \"r\", error: \"cannot_chdir\", exception: \"$!\"}\n");
2010-09-27 16:33:31 +02:00
while( my$data = readcmd) {
(my$cmd, my$length) = unpack( 'nN', $data);
2010-09-30 16:58:20 +02:00
#print STDERR "{ts: \"".ts."\", cmd: $cmd, length: $length}\n";
2010-09-27 16:33:31 +02:00
read STDIN, $data, $length;
2010-09-27 13:40:21 +02:00
if( 1 == $cmd) {
2010-09-30 16:12:00 +02:00
open( F, '>>', $data) or die( "{ts: \"".ts."\", proc: \"r\", error: \"unable_to_open_file\", message: \"Can't open file <$data>.\"}\n");
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 {
2010-09-30 16:12:00 +02:00
die( "{ts: \"".ts."\", proc: \"r\", error: \"unknown_command\", command: $cmd}\n");
2010-09-27 13:40:21 +02:00
}
}
2010-09-30 16:58:20 +02:00
#print STDERR "{ts: \"".ts."\", proc: \"r\", exit: 0}\n";
2010-09-27 15:34:24 +02:00
exit 0;