forked from pbuyle/docker-elodie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inotifywait-polling.sh
218 lines (201 loc) · 7.36 KB
/
inotifywait-polling.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#!/usr/bin/env bash
##
# inotifywait-polling
#
# REQUIRES: inotifywait in full BASH.
#
# Copyright (c) 2020 Francesco Bianco <[email protected]>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
##
[[ -z "${LCOV_DEBUG}" ]] || set -x
set -e
#set -xv
export LC_ALL=C
#trap '[[ -z "$(jobs -p)" ]] || kill $(jobs -p)' EXIT
trap 'kill $(jobs -p) > /dev/null 2>&1' EXIT
usage () {
cat <<'EOF'
inotifywait 3.14
Wait for a particular event on a file or set of files.
Usage: inotifywait [ options ] file1 [ file2 ] [ file3 ] [ ... ]
Options:
-h|--help Show this help text.
@<file> Exclude the specified file from being watched.
--exclude <pattern>
Exclude all events on files matching the
extended regular expression <pattern>.
--excludei <pattern>
Like --exclude but case insensitive.
-m|--monitor Keep listening for events forever. Without
this option, inotifywait will exit after one
event is received.
-d|--daemon Same as --monitor, except run in the background
logging events to a file specified by --outfile.
Implies --syslog.
-r|--recursive Watch directories recursively.
--fromfile <file>
Read files to watch from <file> or `-' for stdin.
-o|--outfile <file>
Print events to <file> rather than stdout.
-s|--syslog Send errors to syslog rather than stderr.
-q|--quiet Print less (only print events).
-qq Print nothing (not even events).
--format <fmt> Print using a specified printf-like format
string; read the man page for more details.
--timefmt <fmt> strftime-compatible format string for use with
%T in --format string.
-c|--csv Print events in CSV format.
-t|--timeout <seconds>
When listening for a single event, time out after
waiting for an event for <seconds> seconds.
If <seconds> is 0, inotifywait will never time out.
-e|--event <event1> [ -e|--event <event2> ... ]
Listen for specific event(s). If omitted, all events are
listened for.
Exit status:
0 - An event you asked to watch for was received.
1 - An event you did not ask to watch for was received
(usually delete_self or unmount), or some error occurred.
2 - The --timeout option was given and no events occurred
in the specified interval of time.
Events:
access file or directory contents were read
modify file or directory contents were written
attrib file or directory attributes changed
close_write file or directory closed, after being opened in
writable mode
close_nowrite file or directory closed, after being opened in
read-only mode
close file or directory closed, regardless of read/write mode
open file or directory opened
moved_to file or directory moved to watched directory
moved_from file or directory moved from watched directory
move file or directory moved to or from watched directory
create file or directory created within watched directory
delete file or directory deleted within watched directory
delete_self file or directory was deleted
unmount file system containing file or directory unmounted
EOF
}
quiet=
recursive=
options=$(getopt -n inotifywait -o qrhme: -l help -- "$@" && true)
eval set -- "${options}"
#echo "options: ${options}"
while true; do
case "$1" in
-q) quiet=1 ;;
-r) recursive=1 ;;
-e) shift; events=${1^^} ;;
-h|--help) usage; exit ;;
--) shift; break ;;
esac
shift
done
##
#
##
watch () {
#echo "watch $1"
find $1 -printf "%s %y %p %T@\\n" | sort -k3 - > $1.inotifywait
while true; do
sleep 2
sign=
last=$(cat $1.inotifywait)
#mv $1.inotifywait $1.$(date +%s).inotifywait
find $1 -printf "%s %y %p %T@\\n" | sort -k3 - > $1.inotifywait
meta=$(diff <(echo "${last}") <(cat "$1.inotifywait")) && true
[[ -z "${meta}" ]] && continue
echo -e "${meta}\n." | while IFS= read line || [[ -n "${line}" ]]; do
echo "line: ${line}"
if [[ "${line}" == "." ]]; then
echo "sign: ${sign}"
for item in $(tr ';' '\n' <<< "${sign}"); do
echo "item: ${item}"
event=$(echo ${item} | cut -s -d':' -f1)
focus=$(echo ${item} | cut -s -d':' -f2)
dir=$(dirname "${focus}")/
echo "focus: ${focus}"
file=$(basename "${focus}")
print_event ${dir} ${event} ${file}
done
break
fi
flag=$(echo ${line} | cut -s -d' ' -f1)
file=$(echo ${line} | cut -s -d' ' -f3-)
[[ -n "${file}" ]] || continue
#echo "${file} -- ${file: -12}"
[[ "${file: -12}" != ".inotifywait" ]] || continue
case ${flag} in
"<")
event=DELETE
;;
">")
event=CREATE
if [[ "${sign}" == *"DELETE:${file};"* ]]; then
event=MODIFY
sign=$(echo "${sign}" | sed "s#DELETE:${file};##g")
elif [[ "${sign}" == *"DELETE:"* ]]; then
event=MOVED_TO
sign=$(echo "${sign}" | sed "s#DELETE:.*;##g")
fi
;;
esac
sign+="${event}:${file};"
done
done
return 0
}
##
#
##
print_event () {
[[ -z "${events}" || "${events}" == *"$2"* ]] && echo "$1 $2 $3"
case "$2" in
CREATE)
[[ -z "${events}" || "${events}" == *"OPEN"* ]] && echo "$1 OPEN $3"
[[ -z "${events}" || "${events}" == *"MODIFY"* ]] && echo "$1 MODIFY $3"
[[ -z "${events}" || "${events}" == *"CLOSE"* ]] && echo "$1 CLOSE_WRITE,CLOSE $3"
;;
esac
return 0
}
##
# Entrypoint
##
main () {
if [[ -z "$1" ]]; then
>&2 echo "No files specified to watch!"
exit 1
fi
[[ -z "${quiet}" ]] && >&2 echo "Setting up watches."
for file in "$@"; do
if [[ ! -e "${file}" ]]; then
echo "Couldn't watch $1: No such file or directory"
exit 1
fi
watch ${file} &
done
[[ -z "${quiet}" ]] && >&2 echo "Watches established."
sleep infinity
exit 0
}
##
main "$@"