diff options
author | T.C. Ferguson <tcf909@gmail.com> | 2020-06-21 02:11:16 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-21 10:11:16 +0100 |
commit | e38d78e5f4c53d5a16c9ab74940cee0dc402c038 (patch) | |
tree | 183b36ccbd28ef15119f47f480e22585de472b54 | |
parent | 8672a5e26237555dcf6c3f009e63066b04b680b5 (diff) | |
download | inotify-tools-master.zip inotify-tools-master.tar.gz inotify-tools-master.tar.bz2 |
Makes unique id (cookie) available using "%c" as a formatting option.
Original code by hamoid
Add test for cookie (%c) format option
-rw-r--r-- | libinotifytools/src/inotifytools.c | 14 | ||||
-rwxr-xr-x | t/inotifywait-format-option-cookie.t | 54 |
2 files changed, 68 insertions, 0 deletions
diff --git a/libinotifytools/src/inotifytools.c b/libinotifytools/src/inotifytools.c index 609f749..f76f4ea 100644 --- a/libinotifytools/src/inotifytools.c +++ b/libinotifytools/src/inotifytools.c @@ -1655,6 +1655,8 @@ int inotifytools_get_num_watches() { * The following tokens will be replaced with the specified string: * \li \c \%w - This will be replaced with the name of the Watched file on * which an event occurred. + * \li \c \%c - This will be replaced with the cookie of the Watched file on + * which an event occurred. * \li \c \%f - When an event occurs within a directory, this will be replaced * with the name of the File which caused the event to occur. * Otherwise, this will be replaced with an empty string. @@ -1700,6 +1702,8 @@ int inotifytools_printf( struct inotify_event* event, char* fmt ) { * The following tokens will be replaced with the specified string: * \li \c \%w - This will be replaced with the name of the Watched file on * which an event occurred. + * \li \c \%c - This will be replaced with the cookie of the Watched file on + * which an event occurred. * \li \c \%f - When an event occurs within a directory, this will be replaced * with the name of the File which caused the event to occur. * Otherwise, this will be replaced with an empty string. @@ -1754,6 +1758,8 @@ int inotifytools_fprintf( FILE* file, struct inotify_event* event, char* fmt ) { * The following tokens will be replaced with the specified string: * \li \c \%w - This will be replaced with the name of the Watched file on * which an event occurred. + * \li \c \%c - This will be replaced with the cookie of the Watched file on + * which an event occurred. * \li \c \%f - When an event occurs within a directory, this will be replaced * with the name of the File which caused the event to occur. * Otherwise, this will be replaced with an empty string. @@ -1806,6 +1812,8 @@ int inotifytools_sprintf( char * out, struct inotify_event* event, char* fmt ) { * The following tokens will be replaced with the specified string: * \li \c \%w - This will be replaced with the name of the Watched file on * which an event occurred. + * \li \c \%c - This will be replaced with cookie of the Watched file on + * which an event occurred. * \li \c \%f - When an event occurs within a directory, this will be replaced * with the name of the File which caused the event to occur. * Otherwise, this will be replaced with an empty string. @@ -1901,6 +1909,12 @@ int inotifytools_snprintf( char * out, int size, continue; } + if ( ch1 == 'c' ) { + ind += snprintf( &out[ind], size-ind, "%x", event->cookie); + ++i; + continue; + } + if ( ch1 == 'e' ) { eventstr = inotifytools_event_to_str( event->mask ); strncpy( &out[ind], eventstr, size - ind ); diff --git a/t/inotifywait-format-option-cookie.t b/t/inotifywait-format-option-cookie.t new file mode 100755 index 0000000..ca3cd1f --- /dev/null +++ b/t/inotifywait-format-option-cookie.t @@ -0,0 +1,54 @@ +#!/bin/bash + +test_description='Resolves Issue #72 + +Make transaction id (cookie) available as part of the format string using %c' + +. ./sharness.sh + +logfile="log" + +run_() { + # Setup code, defer an ATTRIB event for after + # inotifywait has been set up. + touch $logfile + + export LD_LIBRARY_PATH="../../libinotifytools/src/.libs/" + + ../../src/.libs/inotifywait \ + --monitor \ + --daemon \ + --quiet \ + --outfile $logfile \ + --format '%c %e %w%f' \ + --event create \ + --event moved_to \ + --event moved_from \ + $(realpath ./) + + PID="$!" + + touch test-file-src + + mv test-file-src test-file-dst + + kill ${PID} +} + +test_expect_success \ + 'event logged' \ + ' + set -e + trap "set +e" RETURN + run_ + local NONCOOKIE="$(cat "${logfile}" | sed -n 1p | grep -Eo "^[^ ]+")" + #Make sure cookie is 0 for single events + [[ "${NONCOOKIE}" == "0" ]] || return 1 + local COOKIE_A="$(cat "${logfile}" | sed -n 2p | grep -Eo "^[^ ]+")" + [[ -n "${COOKIE_A}" ]] || return 1 + local COOKIE_B="$(cat "${logfile}" | sed -n 3p | grep -Eo "^[^ ]+")" + [[ "${COOKIE_A}" == "${COOKIE_B}" ]] || return 1 + return 0 + ' + +test_done
\ No newline at end of file |