wgnd-mkstruct.pl
changeset 9 0ed7846bef6f
parent 8 5bb3d096ca22
child 13 71d654155a56
equal deleted inserted replaced
8:5bb3d096ca22 9:0ed7846bef6f
       
     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-mkstruct.map.pl';
       
    35 
       
    36 sub trace { print @_ if $ENV{DEBUG}; }
       
    37 
       
    38 GetOptions(
       
    39     "map=s"  => \$map,
       
    40     "h|help" => sub { pod2usage( -verbose => 0, -exitval => 0 ) },
       
    41     "m|man"  => sub {
       
    42         pod2usage(
       
    43             -verbose   => 2,
       
    44             -exitval   => 0,
       
    45             -noperldoc => ( `perldoc -V 2>/dev/null`, $? != 0 )[-1]
       
    46         );
       
    47     },
       
    48 ) or pod2usage();
       
    49 
       
    50 our $source;
       
    51 use lib ('.', $ENV{HOME}, '/etc'); 
       
    52 require $map;
       
    53 
       
    54 my $inotify = new Linux::Inotify2
       
    55     or die "Can't create new inotify object: $!";
       
    56 my @rsync = qw(/usr/bin/rsync -ihv -aAX);
       
    57 
       
    58 for (keys %{$source}) {
       
    59 
       
    60     # add watchers
       
    61     $inotify->watch ("$_", IN_CREATE, sub {
       
    62 
       
    63             my $pid = fork;
       
    64 
       
    65             if (not defined $pid) {
       
    66                 warn "Can't fork: $!\n";
       
    67             } elsif ($pid == 0) {
       
    68                 my $e = shift;
       
    69                 my $name = $e->fullname;
       
    70                 print "$name was created\n" if $e->IN_CREATE;
       
    71                 print "$name is no longer mounted\n" if $e->IN_UNMOUNT;
       
    72                 print "$name is gone\n" if $e->IN_IGNORED;
       
    73                 print "events for $name have been lost\n" if $e->IN_Q_OVERFLOW;
       
    74                 exec @rsync, $source->{$e->{w}->{name}}, $name;
       
    75                 warn "Can't exec: $!\n";
       
    76             }
       
    77 
       
    78         }) or die "Can't add watch: $!\n";
       
    79 }
       
    80 
       
    81 while (1) {
       
    82 
       
    83     $inotify->poll;
       
    84     while (-1 != (my $pid = wait)) {
       
    85         my $e = $? >> 8;
       
    86         print "${ME}[$pid]: exit $e\n";
       
    87     }
       
    88 
       
    89 }
       
    90 
       
    91 
       
    92 __END__
       
    93 
       
    94 =pod
       
    95 
       
    96 =head1 NAME
       
    97 
       
    98 wgnd-mkstruct - instantiate a directory structure template
       
    99 
       
   100 =head1 SYNOPSIS
       
   101 
       
   102 wgnd-mkstruct [--map filename]
       
   103 
       
   104 wgnd-mkstruct -m|--man
       
   105               -h|--help
       
   106 
       
   107 =head1 DESCRIPTION
       
   108 
       
   109 wgnd-mkstruct watches some directories for newly created subdirectories and
       
   110 synchronises these from a given template directory.
       
   111 
       
   112 =head1 OPTIONS
       
   113 
       
   114 =over
       
   115 
       
   116 =item B<--map> I<filename>
       
   117 
       
   118 Name of a file containing mappings between templates and directories. Defaults
       
   119 to F<wgnd-mkstruct.map.pl>.
       
   120 
       
   121 =back
       
   122 
       
   123 =head1 FILES
       
   124 
       
   125 =over
       
   126 
       
   127 =item F<wgnd-mkstruct.map.pl>
       
   128 
       
   129 default for B<--map>
       
   130 
       
   131 =back
       
   132 
       
   133 =head1 AUTHOR
       
   134 
       
   135 Matthias Förste <foerste@schlittermann.de>
       
   136 
       
   137 =cut