diff -r b4b46fe7bf9f -r a54c42c041e6 tele-watch.sh --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tele-watch.sh Sat Feb 28 22:04:02 2009 +0100 @@ -0,0 +1,134 @@ +#! /bin/bash -e +# (c) Heiko Schlittermann + +ME=$(basename $0) +WATCHPOINTS= + +function abs_path() { + local a="$1" + case "$a" in + /*) echo "$a";; + *) t="$(cd $t && pwd)";; + esac +} + +if test "$#" = 0; then + echo "need at least one directory to watch" + exit 1 +fi + +for watch; do + w=${watch%:*} + t=${watch#*:} + + test "$w:$t" = "$watch" || { + echo "to many ':' in \"$watch\"" >&2 + exit 1 + } + + w=$(abs_path "$w") + t=$(abs_path "$t") + + if test -f "$w/.target"; then + read pid target <"$w/.target" + + kill -0 $pid 2>/dev/null && { + echo "already watched by $pid for $target" >&2 + exit 1 + } + fi + + echo "$$ $t" > "$w/.target" + WATCHPOINTS="${WATCHPOINTS:+$WATCHPOINTS }$w" +done + +inotifywait -q -m --format "%e %w %f" -e create,move,delete $WATCHPOINTS | while read EVENT WATCHER NAME +do + echo "$EVENT -- $WATCHER $NAME" + + case "$EVENT" in + *ISDIR*) ;; + *) continue;; + esac + + read dummy TARGET <$WATCHER/.target + DIRS=$(cd $TARGET && echo *) + + case "$EVENT" in + *CREATE*) + for dir in $DIRS; do + mkdir "/.m/$dir/$NAME" + echo "$NAME" >"$WATCHER/$NAME/.name" + ln -s "/.m/$dir/$NAME" "$WATCHER/$NAME/$dir" + done + ;; + *MOVED_TO*) + name=$(tail -1 "$WATCHER/$NAME/.name") + for link in "$WATCHER/$NAME/"*; do + test -L "$link" || continue + dst=$(readlink $link) + if test "$(basename $dst)" = "$name"; then + new="$(dirname $dst)/$NAME" + mv -v "$dst" "$new" + ln -vsf "$new" "$link" + fi + done + echo "$NAME" >>"$WATCHER/$NAME/.name" + ;; + *DELETE*) + for dir in $DIRS; do + mkdir -p "/.m/$dir/,old" + mv -v "/.m/$dir/$NAME" "/.m/$dir/,old/$NAME-$(date +%Y%m%d-%H%M%S)" + done + esac +done +exit + +=head1 NAME + +tele-watch - guard the dtele directory policy + +=head1 SYNOPSIS + + tele-watch ""... + +=head1 DESCRIPTION + +B should run as a daemon. + +B watches the list of directories I... (absolute path names) +via "inotify" and performs some actions on: + +=over + +=item CREATION of new directory + +It checks F and assumes, that all directories there should +reflect in the newly created directory: + + /_tmp -> /.m/_tmp/NEW1/ + /homepage -> /.m/homepage/NEW1/ + ... + +After done this it writes the name of the newly created directory into +the file F<< /.name >> + +=item RENAMING of a directory + +If the directory gets renamed, the above links needs to be updated. + +=item DELETION of a directory + +If the root directory is removed, the targets of the former links should +be removed, we do our best, to do this. (Actually not removing the +targets, but moving them into an F folder.) + +=back + +=head1 AUTHOR + +Heiko Schlittermann + +=cut + +# vim:tw=72 sts=4 ts=4 sw=4 aw ai sm: