wgnd-watch.pl
changeset 30 c5026a0ae757
parent 29 a589fb3599fb
--- a/wgnd-watch.pl	Wed Feb 15 12:45:01 2017 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,238 +0,0 @@
-#!/usr/bin/perl
-
-#    Copyright (C) 2012-2017  Matthias Förste
-#
-#    This program is free software: you can redistribute it and/or modify
-#    it under the terms of the GNU General Public License as published by
-#    the Free Software Foundation, either version 3 of the License, or
-#    (at your option) any later version.
-#
-#    This program is distributed in the hope that it will be useful,
-#    but WITHOUT ANY WARRANTY; without even the implied warranty of
-#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-#    GNU General Public License for more details.
-#
-#    You should have received a copy of the GNU General Public License
-#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
-#
-#    Matthias Förste <foerste@schlittermann.de>
-
-=encoding utf8
-=cut
-
-use strict;
-use warnings;
-
-my $VERSION = '0.1';
-my $ME      = $0;
-
-use File::Basename;
-
-# File::Rsync in squeeze does not support --xattrs yet
-#use File::Rsync;
-use Getopt::Long;
-use Pod::Usage;
-use Linux::Inotify2;
-use Sys::Syslog;
-
-my $master;
-
-BEGIN {
-
-    openlog($ME, 'ndelay,nowait,pid', 'LOG_USER');
-    $SIG{__WARN__} = sub {
-        warn @_ if not defined $^S;
-        print STDERR "@_";
-        syslog('warning', "@_");
-    };
-    $SIG{__DIE__} = sub {
-        die @_ if not defined $^S;
-        print STDERR "@_";
-        syslog('err', "@_");
-        exit $?;
-    };
-    sub dolog { print "@_"; syslog('info', "@_"); }
-    sub trace { print @_ if $ENV{DEBUG}; }
-
-    use sigtrap qw(die normal-signals);
-
-}
-
-my %opts = (
-    daemon  => 1,
-    map     => 'wgnd-watch.map.pl',
-    pidfile => '/var/run/wgnd-watch.pid',
-    'rsync-args' => [ qw(-ihv -aAX --no-times) ],
-    'rsync-cmd' => '/usr/bin/rsync'
-);
-
-GetOptions(
-    "map=s"     => \$opts{map},
-    "daemon!"   => \$opts{daemon},
-    "pidfile=s" => \$opts{pidfile},
-    "rsync-args=s@" => \$opts{'rsync-args'},
-    "rsync-cmd=s" => \$opts{'rsync-cmd'},
-    "h|help!"   => sub { pod2usage(-verbose => 0, -exitval => 0) },
-    "m|man!"    => sub {
-        pod2usage(
-            -verbose   => 2,
-            -exitval   => 0,
-            -noperldoc => (`perldoc -V 2>/dev/null`, $? != 0)[-1]
-        );
-    },
-) or pod2usage();
-
-our $source;
-use lib grep { defined } ('.', $ENV{HOME}, '/etc');
-require $opts{map};
-
-$0 = "$ME @ARGV";
-
-chdir("/") or die "Can't chdir to /: $!\n";
-
-if ($opts{daemon}) {
-
-    open STDIN,  "</dev/null" or die "Can't redir STDIN: $!\n";
-    open STDOUT, ">/dev/null" or die "Can't redir STDOUT: $!\n";
-
-    defined(my $pid = fork) or die "Can't fork: $!\n";
-    if ($pid) {
-        dolog "Child is [$pid]";
-        exit 0;
-    }
-    POSIX::setsid()
-      or die "Can't setsid: $!\n";
-    open(STDERR, ">&STDOUT") or die "Can't dup stdout: $!\n";
-
-    open(P, '>', $opts{pidfile})
-      or die "Can't open '>', '$opts{pidfile}': $!\n";
-    print P $$;
-    close P;
-    $master = $$;
-
-}
-
-my $inotify = new Linux::Inotify2
-  or die "Can't create new inotify object: $!\n";
-my @rsync = ($opts{'rsync-cmd'}, @{$opts{'rsync-args'}});
-
-for (keys %{$source}) {
-
-    # add watchers
-    $inotify->watch(
-        "$_",
-        IN_CREATE,
-        sub {
-
-            my $pid = fork;
-
-            if (not defined $pid) {
-                warn "Can't fork: $!\n";
-            } elsif ($pid == 0) {
-                my $e    = shift;
-                my $name = $e->fullname;
-                dolog "$name was created\n"               if $e->IN_CREATE;
-                dolog "$name is no longer mounted\n"      if $e->IN_UNMOUNT;
-                dolog "$name is gone\n"                   if $e->IN_IGNORED;
-                dolog "events for $name have been lost\n" if $e->IN_Q_OVERFLOW;
-                exec @rsync, $source->{ $e->{w}->{name} }, $name;
-                warn "Can't exec: $!\n";
-            }
-
-        }
-    ) or die "Can't add watch: $!\n";
-}
-
-while (1) {
-
-    $inotify->poll;
-    while (-1 != (my $pid = wait)) {
-        my $e = $? >> 8;
-        dolog "Child ${ME}[$pid]: exit $e\n";
-    }
-
-}
-
-END {
-
-    unlink $opts{pidfile}
-      if $opts{daemon}
-          and defined $master
-          and $master eq $$;
-    dolog "exit";
-    closelog;
-
-}
-
-__END__
-
-=pod
-
-=head1 NAME
-
-wgnd-watch - inotify actions for wiegand
-
-=head1 SYNOPSIS
-
-wgnd-watch [--map filename]
-
-wgnd-watch -m|--man
-           -h|--help
-
-=head1 DESCRIPTION
-
-wgnd-watch currently watches some directories for newly created subdirectories
-and synchronises these from a given template directory.
-
-=head1 OPTIONS
-
-=over
-
-=item B<--[no]daemon>
-
-[Don't] run as a daemon. Default: do.
-
-=item B<--map> I<filename>
-
-Name of a file containing mappings between templates and directories. Defaults
-to F<wgnd-watch.map.pl>.
-
-=item B<--pidfile> I<filename>
-
-Location of the pid file. This is ignored unless we are running as a daemon.
-Defaults to F</var/run/wgnd-watch.pid>.
-
-=item B<--pidfile> I<filename>
-
-Location of the pid file. This is ignored unless we are running as a daemon.
-Defaults to F</var/run/wgnd-watch.pid>.
-
-=item B<--rsync-args> I<args>
-
-Arguments to pass to the rsync command. May be given multiple times. Defaults to B<-ihv -aAX --no-times>.
-
-=item B<--rsync-cmd> I<cmd>
-
-Rsync Command. Defaults to F</usr/bin/rsync>.
-
-=back
-
-=head1 FILES
-
-=over
-
-=item F<wgnd-watch.map.pl>
-
-default for B<--map>
-
-=item F</var/run/wgnd-watch.pid>
-
-default for B<--pidfile>
-
-=back
-
-=head1 AUTHOR
-
-Matthias Förste <foerste@schlittermann.de>
-
-=cut