-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinotify-sub
executable file
·139 lines (127 loc) · 3.09 KB
/
inotify-sub
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
#!/bin/sh
# Copyright 2016 W. Trevor King <[email protected]>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# usage: inotify-sub [OPTIONS] [DIRECTORY]
#
# Subscribe to messages being published under DIRECTORY, printing
# events to stdout as they are published. If EVENT is set, only print
# events matching that label. If it is not set, print all events.
EVENT=
EVENT_DIR=
WATCHING=0
LAST_OLD_EVENT=
MONITOR=0
HISTORIC=0
show_matching_event() {
FILENAME="${1}"
FILE_EVENT="${FILENAME#[^_]*_}"
if test -z "${EVENT}" -o "_${EVENT}" = "_${FILE_EVENT}"
then
cat "${EVENT_DIR}/${FILENAME}" &&
return 0
fi
return 1
}
less_than() {
if test "_${1}" = "_${2}"
then
return 1
fi
A=$(printf '%s\n%s\n' "${1}" "${2}")
B=$(echo "${A}" | sort)
test "${A}" = "${B}"
return "${?}"
}
while test -n "${1}"
do
case "${1}" in
-m|--monitor)
MONITOR=1
;;
-e|--event)
shift
EVENT="${1}"
;;
-H|--historic)
HISTORIC=1
;;
-*)
cat <<-EOF >&2
unrecognized argument: '${1}'
usage: inotify-sub [OPTIONS] [DIRECTORY]
Options:
-m, --monitor
Instead of exiting after receiving a single event, execute
indefinitely. The default behavior is to exit after the first
event occurs.
-e <event>, --event <event>
Listen for specific events only. If ommitted, all events are
listened for.
-H, --historic
Replay historic events. If ommitted, only new events are
output.
EOF
exit 1
esac
shift
done
DIRECTORY="${1:-.}"
FIFO_DIR="${DIRECTORY}/fifo"
EVENT_DIR="${DIRECTORY}/events"
mkdir -p "${EVENT_DIR}" &&
mkdir -p "${FIFO_DIR}" &&
inotifywait -m -e moved_to --format '%f' "${EVENT_DIR}" 2>&1 |
while read -r FILENAME
do
if test "${WATCHING}" -eq 0
then
if test 'Watches established.' = "${FILENAME}"
then
WATCHING=1
if test "${HISTORIC}" -eq 1
then
DATE=$(TZ=UTC date --iso-8601=ns) &&
mkfifo -m 0600 "${FIFO_DIR}/${DATE}"
ls "${EVENT_DIR}" | (
LAST_OLD_EVENT=
while read -r FNAME
do
LAST_OLD_EVENT="${FNAME}"
if show_matching_event "${FNAME}" && test "${MONITOR}" -eq 0
then
break
fi
done &&
echo "${LAST_OLD_EVENT}" >> "${FIFO_DIR}/${DATE}"
) &
LAST_OLD_EVENT=$(cat "${FIFO_DIR}/${DATE}") &&
wait &&
rm "${FIFO_DIR}/${DATE}" &&
if test -n "${LAST_OLD_EVENT}" -a "${MONITOR}" -eq 0
then
pkill --parent "${$}" inotifywait &&
break
fi
fi
fi
elif test -z "${LAST_OLD_EVENT}" ||
less_than "${LAST_OLD_EVENT}" "${FILENAME}"
then
if show_matching_event "${FILENAME}" && test "${MONITOR}" -eq 0
then
pkill --parent "${$}" inotifywait &&
break
fi
fi
done