--- a/tele-watch.pl Mon Dec 06 22:41:18 2010 +0100
+++ b/tele-watch.pl Tue Dec 07 00:14:03 2010 +0100
@@ -3,7 +3,7 @@
# Script to watch directories and to perform some actions on
# changes, highly specialized on DTELE.
#
-# Copyright (C) 2009 Heiko Schlittermann
+# Copyright (C) 2009 2010 Heiko Schlittermann
#
# 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
@@ -19,6 +19,8 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# Heiko Schlittermann <hs@schlittermann.de>
+# Source: (Mercurial) https://keller.schlittermann.de/hg/ius/tele-watch
+
use strict;
use warnings;
@@ -30,6 +32,7 @@
use Cwd qw(abs_path);
use File::Temp qw(tempfile);
use POSIX qw(setsid);
+use File::Find; # homepage hook
my $ME = basename $0;
my $VERSION = "<VERSION>";
@@ -47,6 +50,24 @@
sub timestamp();
sub dir($);
+my %HOOK = (
+ homepage => sub {
+ my $dir = shift;
+ my ($uid, $gid, $mode) = (stat $dir)[4, 5, 2];
+ defined(my $pid = fork()) or die "Can't fork: $!\n";
+ if ($pid == 0) {
+ chdir $dir or die "Can't chdir to $dir: $!\n";
+ system("bzr init");
+ finddepth(sub {
+ chown($uid, $gid, $_);
+ chmod($mode & 07777, $_);
+ }, ".bzr");
+ exec "true"; # avoid execution of END{ } blocks
+ }
+ wait;
+ },
+);
+
MAIN: {
my @_ARGV = @ARGV;
my %TARGET;
@@ -84,6 +105,10 @@
$w = abs_path($w);
$t = abs_path($t);
$TARGET{$w} = $t;
+
+ die "$ME: $w: $!\n" if not -d $w;
+ die "$ME: $t: $!\n" if not -d $t;
+
}
writef($opt_pidfile, $$) if $opt_pidfile;
@@ -104,7 +129,7 @@
# cleanup code
END {
foreach (keys %TARGET) {
- if (readf("$_/.watched") == $$) {
+ if (readf("$_/.watched") || 0 == $$) {
unlink "$_/.watched";
system("chattr", "+i" => "$_") if $opt_block;
syslog(LOG_NOTICE, "cleaned $_/.watched");
@@ -177,10 +202,13 @@
# find the owner and permissions
my ($uid, $gid, $mode) = (stat $fullname)[ 4, 5, 2 ];
+ # create a link to each directory found in $target/
foreach my $t (map { basename($_) } grep { -d } dir "$target/")
{
my $dir = "$target/$t/$e->{name}";
my $link = "$fullname/$t";
+ my $hook = $HOOK{$t} if exists $HOOK{$t}
+ and ref $HOOK{$t} eq "CODE";
if (!-e $dir) {
notice "mkdir $dir";
@@ -192,6 +220,9 @@
notice "symlink $dir <= $link";
unlink $link;
symlink $dir => $link;
+
+ $hook->($dir) if defined $hook;
+
}
chmod(0555 => $fullname);
next EVENT;
@@ -293,6 +324,8 @@
or die "Can't get temp file name in dir " . dirname($fn) . ": $!\n";
print {$fh} @_;
close $fh;
+
+ # rename() should do the same job
link($tmpfn, $fn) or do die "Can't rename $tmpfn => $fn: $!\n";
unlink($tmpfn);
}