diff --git a/tests/inotify b/tests/inotify new file mode 100755 index 0000000..2c1bcab Binary files /dev/null and b/tests/inotify differ diff --git a/tests/inotify.c b/tests/inotify.c new file mode 100644 index 0000000..2f227e1 --- /dev/null +++ b/tests/inotify.c @@ -0,0 +1,66 @@ +/*This is the sample program to notify us for the file creation and file deletion takes place in “/tmp” directory*/ +#include +#include +#include +#include +#include +#include + +#define EVENT_SIZE ( sizeof (struct inotify_event) ) +#define EVENT_BUF_LEN ( 1024 * ( EVENT_SIZE + 16 ) ) + +int main( ) +{ + int length, i = 0; + int fd; + int wd; + char buffer[EVENT_BUF_LEN]; + + /*creating the INOTIFY instance*/ + fd = inotify_init1(IN_NONBLOCK); + + /*checking for error*/ + if ( fd < 0 ) { + perror( "inotify_init" ); + } + + /*adding the “/tmp” directory into watch list. Here, the suggestion is to validate the existence of the directory before adding into monitoring list.*/ + wd = inotify_add_watch( fd, "/dev/input/", IN_CREATE | IN_DELETE ); + + printf("%d\t%d\n", fd, wd); + /*read to determine the event change happens on “/tmp” directory. Actually this read blocks until the change event occurs*/ + while ((length = read( fd, buffer, EVENT_BUF_LEN )) < 0); + + /*checking for error*/ + if ( length < 0 ) { + perror( "read" ); + } + + /*actually read return the list of change events happens. Here, read the change event one by one and process it accordingly.*/ + while ( i < length ) { struct inotify_event *event = ( struct inotify_event * ) &buffer[ i ]; if ( event->len ) { + if ( event->mask & IN_CREATE ) { + if ( event->mask & IN_ISDIR ) { + printf( "New directory %s created.\n", event->name ); + } + else { + printf( "New file %s created.\n", event->name ); + } + } + else if ( event->mask & IN_DELETE ) { + if ( event->mask & IN_ISDIR ) { + printf( "Directory %s deleted.\n", event->name ); + } + else { + printf( "File %s deleted.\n", event->name ); + } + } + } + i += EVENT_SIZE + event->len; + } + /*removing the “/tmp” directory from the watch list.*/ + inotify_rm_watch( fd, wd ); + + /*closing the INOTIFY instance*/ + close( fd ); + +} diff --git a/tests/makefile b/tests/makefile index cdaecf1..70a608f 100644 --- a/tests/makefile +++ b/tests/makefile @@ -7,5 +7,7 @@ ioctl: ioctl.c evtest: evtest.c +inotify: inotify.c + clean: - rm *.o parse ioctl 2> /dev/null + rm *.o parse ioctl inotify 2> /dev/null