|
1 #! /bin/bash -e |
|
2 # (c) Heiko Schlittermann <hs@schlittermann.de> |
|
3 |
|
4 ME=$(basename $0) |
|
5 WATCHPOINTS= |
|
6 |
|
7 function abs_path() { |
|
8 local a="$1" |
|
9 case "$a" in |
|
10 /*) echo "$a";; |
|
11 *) t="$(cd $t && pwd)";; |
|
12 esac |
|
13 } |
|
14 |
|
15 if test "$#" = 0; then |
|
16 echo "need at least one directory to watch" |
|
17 exit 1 |
|
18 fi |
|
19 |
|
20 for watch; do |
|
21 w=${watch%:*} |
|
22 t=${watch#*:} |
|
23 |
|
24 test "$w:$t" = "$watch" || { |
|
25 echo "to many ':' in \"$watch\"" >&2 |
|
26 exit 1 |
|
27 } |
|
28 |
|
29 w=$(abs_path "$w") |
|
30 t=$(abs_path "$t") |
|
31 |
|
32 if test -f "$w/.target"; then |
|
33 read pid target <"$w/.target" |
|
34 |
|
35 kill -0 $pid 2>/dev/null && { |
|
36 echo "already watched by $pid for $target" >&2 |
|
37 exit 1 |
|
38 } |
|
39 fi |
|
40 |
|
41 echo "$$ $t" > "$w/.target" |
|
42 WATCHPOINTS="${WATCHPOINTS:+$WATCHPOINTS }$w" |
|
43 done |
|
44 |
|
45 inotifywait -q -m --format "%e %w %f" -e create,move,delete $WATCHPOINTS | while read EVENT WATCHER NAME |
|
46 do |
|
47 echo "$EVENT -- $WATCHER $NAME" |
|
48 |
|
49 case "$EVENT" in |
|
50 *ISDIR*) ;; |
|
51 *) continue;; |
|
52 esac |
|
53 |
|
54 read dummy TARGET <$WATCHER/.target |
|
55 DIRS=$(cd $TARGET && echo *) |
|
56 |
|
57 case "$EVENT" in |
|
58 *CREATE*) |
|
59 for dir in $DIRS; do |
|
60 mkdir "/.m/$dir/$NAME" |
|
61 echo "$NAME" >"$WATCHER/$NAME/.name" |
|
62 ln -s "/.m/$dir/$NAME" "$WATCHER/$NAME/$dir" |
|
63 done |
|
64 ;; |
|
65 *MOVED_TO*) |
|
66 name=$(tail -1 "$WATCHER/$NAME/.name") |
|
67 for link in "$WATCHER/$NAME/"*; do |
|
68 test -L "$link" || continue |
|
69 dst=$(readlink $link) |
|
70 if test "$(basename $dst)" = "$name"; then |
|
71 new="$(dirname $dst)/$NAME" |
|
72 mv -v "$dst" "$new" |
|
73 ln -vsf "$new" "$link" |
|
74 fi |
|
75 done |
|
76 echo "$NAME" >>"$WATCHER/$NAME/.name" |
|
77 ;; |
|
78 *DELETE*) |
|
79 for dir in $DIRS; do |
|
80 mkdir -p "/.m/$dir/,old" |
|
81 mv -v "/.m/$dir/$NAME" "/.m/$dir/,old/$NAME-$(date +%Y%m%d-%H%M%S)" |
|
82 done |
|
83 esac |
|
84 done |
|
85 exit |
|
86 |
|
87 =head1 NAME |
|
88 |
|
89 tele-watch - guard the dtele directory policy |
|
90 |
|
91 =head1 SYNOPSIS |
|
92 |
|
93 tele-watch "<dir:target>"... |
|
94 |
|
95 =head1 DESCRIPTION |
|
96 |
|
97 B<tele-watch> should run as a daemon. |
|
98 |
|
99 B<tele-watch> watches the list of directories I<dir>... (absolute path names) |
|
100 via "inotify" and performs some actions on: |
|
101 |
|
102 =over |
|
103 |
|
104 =item CREATION of new directory |
|
105 |
|
106 It checks F</.m/*> and assumes, that all directories there should |
|
107 reflect in the newly created directory: |
|
108 |
|
109 <NEW1>/_tmp -> /.m/_tmp/NEW1/ |
|
110 <NEW1>/homepage -> /.m/homepage/NEW1/ |
|
111 ... |
|
112 |
|
113 After done this it writes the name of the newly created directory into |
|
114 the file F<< <NEW1>/.name >> |
|
115 |
|
116 =item RENAMING of a directory |
|
117 |
|
118 If the directory gets renamed, the above links needs to be updated. |
|
119 |
|
120 =item DELETION of a directory |
|
121 |
|
122 If the root directory is removed, the targets of the former links should |
|
123 be removed, we do our best, to do this. (Actually not removing the |
|
124 targets, but moving them into an F</.m/_tmp/,old> folder.) |
|
125 |
|
126 =back |
|
127 |
|
128 =head1 AUTHOR |
|
129 |
|
130 Heiko Schlittermann <hs@schlittermann.de> |
|
131 |
|
132 =cut |
|
133 |
|
134 # vim:tw=72 sts=4 ts=4 sw=4 aw ai sm: |