wgnd-watch.pl
changeset 14 7b4903ebdee8
parent 13 71d654155a56
child 19 a9b77650cbc4
equal deleted inserted replaced
13:71d654155a56 14:7b4903ebdee8
       
     1 #!/usr/bin/perl
       
     2 
       
     3 #    Copyright (C) 2012  Matthias Förste
       
     4 #
       
     5 #    This program is free software: you can redistribute it and/or modify
       
     6 #    it under the terms of the GNU General Public License as published by
       
     7 #    the Free Software Foundation, either version 3 of the License, or
       
     8 #    (at your option) any later version.
       
     9 #
       
    10 #    This program is distributed in the hope that it will be useful,
       
    11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 #    GNU General Public License for more details.
       
    14 #
       
    15 #    You should have received a copy of the GNU General Public License
       
    16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    17 #
       
    18 #    Matthias Förste <foerste@schlittermann.de>
       
    19 
       
    20 =encoding utf8
       
    21 =cut
       
    22 
       
    23 use strict;
       
    24 use warnings;
       
    25 
       
    26 my $VERSION = '0.1';
       
    27 my $ME = $0;
       
    28 use Getopt::Long;
       
    29 use Pod::Usage;
       
    30 use Linux::Inotify2;
       
    31 # File::Rsync in squeeze does not support --xattrs yet
       
    32 #use File::Rsync;
       
    33 
       
    34 my $map = 'wgnd-watch.map.pl';
       
    35 
       
    36 BEGIN {
       
    37 
       
    38     use Sys::Syslog;
       
    39     openlog($ME, 'ndelay,nowait,pid', 'LOG_USER');
       
    40     $SIG{__WARN__} = sub {
       
    41         warn @_ if not defined $^S;
       
    42         print STDERR "@_";
       
    43         syslog('warning', "@_");
       
    44     };
       
    45     $SIG{__DIE__} = sub {
       
    46         die @_ if not defined $^S;
       
    47         print STDERR "@_";
       
    48         syslog('err', "@_");
       
    49         exit $?;
       
    50     };
       
    51 
       
    52     sub dolog { print "@_"; syslog('info', "@_"); }
       
    53     sub trace { print @_ if $ENV{DEBUG}; }
       
    54 
       
    55 }
       
    56 
       
    57 GetOptions(
       
    58     "map=s"  => \$map,
       
    59     "h|help" => sub { pod2usage( -verbose => 0, -exitval => 0 ) },
       
    60     "m|man"  => sub {
       
    61         pod2usage(
       
    62             -verbose   => 2,
       
    63             -exitval   => 0,
       
    64             -noperldoc => ( `perldoc -V 2>/dev/null`, $? != 0 )[-1]
       
    65         );
       
    66     },
       
    67 ) or pod2usage();
       
    68 
       
    69 our $source;
       
    70 use lib ('.', $ENV{HOME}, '/etc'); 
       
    71 require $map;
       
    72 
       
    73 my $inotify = new Linux::Inotify2
       
    74     or die "Can't create new inotify object: $!";
       
    75 my @rsync = qw(/usr/bin/rsync -ihv -aAX);
       
    76 
       
    77 for (keys %{$source}) {
       
    78 
       
    79     # add watchers
       
    80     $inotify->watch ("$_", IN_CREATE, sub {
       
    81 
       
    82             my $pid = fork;
       
    83 
       
    84             if (not defined $pid) {
       
    85                 warn "Can't fork: $!\n";
       
    86             } elsif ($pid == 0) {
       
    87                 my $e = shift;
       
    88                 my $name = $e->fullname;
       
    89                 dolog "$name was created\n" if $e->IN_CREATE;
       
    90                 dolog "$name is no longer mounted\n" if $e->IN_UNMOUNT;
       
    91                 dolog "$name is gone\n" if $e->IN_IGNORED;
       
    92                 dolog "events for $name have been lost\n" if $e->IN_Q_OVERFLOW;
       
    93                 exec @rsync, $source->{$e->{w}->{name}}, $name;
       
    94                 warn "Can't exec: $!\n";
       
    95             }
       
    96 
       
    97         }) or die "Can't add watch: $!\n";
       
    98 }
       
    99 
       
   100 while (1) {
       
   101 
       
   102     $inotify->poll;
       
   103     while (-1 != (my $pid = wait)) {
       
   104         my $e = $? >> 8;
       
   105         dolog "child ${ME}[$pid]: exit $e\n";
       
   106     }
       
   107 
       
   108 }
       
   109 
       
   110 END {
       
   111     closelog;
       
   112 }
       
   113 
       
   114 __END__
       
   115 
       
   116 =pod
       
   117 
       
   118 =head1 NAME
       
   119 
       
   120 wgnd-watch - inotify actions for wiegand
       
   121 
       
   122 =head1 SYNOPSIS
       
   123 
       
   124 wgnd-watch [--map filename]
       
   125 
       
   126 wgnd-watch -m|--man
       
   127            -h|--help
       
   128 
       
   129 =head1 DESCRIPTION
       
   130 
       
   131 wgnd-watch currently watches some directories for newly created subdirectories
       
   132 and synchronises these from a given template directory.
       
   133 
       
   134 =head1 OPTIONS
       
   135 
       
   136 =over
       
   137 
       
   138 =item B<--map> I<filename>
       
   139 
       
   140 Name of a file containing mappings between templates and directories. Defaults
       
   141 to F<wgnd-watch.map.pl>.
       
   142 
       
   143 =back
       
   144 
       
   145 =head1 FILES
       
   146 
       
   147 =over
       
   148 
       
   149 =item F<wgnd-watch.map.pl>
       
   150 
       
   151 default for B<--map>
       
   152 
       
   153 =back
       
   154 
       
   155 =head1 AUTHOR
       
   156 
       
   157 Matthias Förste <foerste@schlittermann.de>
       
   158 
       
   159 =cut