bin/waitfordeb
branchrsync
changeset 42 73cb1d7a1bd8
parent 41 40df28fd3562
child 43 6d4d4ff0796e
equal deleted inserted replaced
41:40df28fd3562 42:73cb1d7a1bd8
     9 
     9 
    10 #my $to = 600;
    10 #my $to = 600;
    11 my $to = 60;
    11 my $to = 60;
    12 
    12 
    13 my $inotify = new Linux::Inotify2
    13 my $inotify = new Linux::Inotify2
    14     or die "unable to create new inotify object: $!";
    14   or die "unable to create new inotify object: $!";
    15 $inotify->blocking(0);
    15 $inotify->blocking(0);
    16 
    16 
    17 my %size;
    17 my %size;
    18 my $exit_after;
    18 my $exit_after;
    19 
    19 
    20 my $ME = basename $0;
    20 my $ME = basename $0;
    21 
    21 
    22 sub delay_exit { my $fn = shift; $exit_after = time + $to if $size{basename $fn}; }
    22 sub delay_exit {
    23 sub verify_size { 
    23     my $fn = shift;
       
    24     $exit_after = time + $to if $size{ basename $fn};
       
    25 }
       
    26 
       
    27 sub verify_size {
    24     my $fn = shift;
    28     my $fn = shift;
    25     delay_exit $fn;
    29     delay_exit $fn;
    26     my $bn = basename $fn;
    30     my $bn = basename $fn;
    27     my $s = -s $fn;
    31     my $s  = -s $fn;
    28     delete $size{$bn} if defined $s and $s >= $size{$bn};
    32     delete $size{$bn} if defined $s and $s >= $size{$bn};
    29 }
    33 }
    30 
    34 
    31 my $incoming = "$ENV{HOME}/incoming";
    35 my $incoming = "$ENV{HOME}/incoming";
    32 my $nfiles = 0;
    36 my $nfiles   = 0;
    33 my $c = "$incoming/$ARGV[0]";
    37 my $c        = "$incoming/$ARGV[0]";
    34 
    38 
    35 # first setup a file creation watch on the incoming directory (we cannot set
    39 # first setup a file creation watch on the incoming directory (we cannot set
    36 # watches on objects that dont exist yet); then *if* a file which is listed in
    40 # watches on objects that dont exist yet); then *if* a file which is listed in
    37 # the changes file is created inside that directory then setup file
    41 # the changes file is created inside that directory then setup file
    38 # {modification,close} watches on it; we need the modification watch to reset
    42 # {modification,close} watches on it; we need the modification watch to reset
    42 my $skip = 1;
    46 my $skip = 1;
    43 while (<C>) {
    47 while (<C>) {
    44     no warnings qw(syntax);
    48     no warnings qw(syntax);
    45     if (/^files:\s*$/i) { $skip = 0; next; }
    49     if (/^files:\s*$/i) { $skip = 0; next; }
    46     next if $skip;
    50     next if $skip;
    47     unless (/^ ([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ \n]+) *$/) { $skip = 1; next; }
    51     unless (/^ ([^ ]+) +([^ ]+) +([^ ]+) +([^ ]+) +([^ \n]+) *$/) {
       
    52         $skip = 1;
       
    53         next;
       
    54     }
    48     use warnings;
    55     use warnings;
    49     $size{$5} = $2;
    56     $size{$5} = $2;
    50     $nfiles++;
    57     $nfiles++;
    51 }
    58 }
    52 close C;
    59 close C;
    53 warn "$ME: no files found in changes file.\n" unless $nfiles;
    60 warn "$ME: no files found in changes file.\n" unless $nfiles;
    54 
    61 
    55 $inotify->watch($incoming, IN_MODIFY, sub { my $e = shift; delay_exit $e->fullname; } ) or warn "Can't watch: $!";
    62 $inotify->watch($incoming, IN_MODIFY,
    56 $inotify->watch($incoming, IN_CLOSE, sub { my $e = shift; verify_size $e->fullname; } ) or warn "Can't watch: $!";
    63     sub { my $e = shift; delay_exit $e->fullname; })
       
    64   or warn "Can't watch: $!";
       
    65 $inotify->watch($incoming, IN_CLOSE,
       
    66     sub { my $e = shift; verify_size $e->fullname; })
       
    67   or warn "Can't watch: $!";
    57 
    68 
    58 # do initial check on file in case its upload is already finished
    69 # do initial check on file in case its upload is already finished
    59 verify_size "$incoming/$_" for keys %size;
    70 verify_size "$incoming/$_" for keys %size;
    60 
    71 
    61 my $done = 0;
    72 my $done = 0;