minisync/r.pl

53 lines
1.2 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;
$|++;
$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;
2010-09-27 13:40:21 +02:00
if( 1 == $cmd) {
2010-09-27 15:34:24 +02:00
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");
2010-09-27 16:33:31 +02:00
my($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
2010-09-27 13:40:21 +02:00
$atime,$mtime,$ctime,$blksize,$blocks) = stat F;
2010-09-27 16:33:31 +02:00
print STDERR "r >> s\n";
2010-09-27 13:40:21 +02:00
print pack( 'N', $size);
}
elsif( 2 == $cmd) {
2010-09-27 16:33:31 +02:00
print STDERR "r >> f\n";
2010-09-27 15:34:24 +02:00
print F $data;
}
else {
die( "{proc: \"r\", error: \"unknown_command\", command: $cmd}\n");
2010-09-27 13:40:21 +02:00
}
}
2010-09-27 15:34:24 +02:00
print STDERR "{proc: \"r\", exit: 0}\n";
exit 0;