2 #include <sys/inotify.h> // for INOTIFY duh
3 #include <stdio.h> // for stdio, NULL
4 #include <stdlib.h> // for malloc, etc
5 #include <unistd.h> // for close
7 #include "pnd_notify.h"
10 int fd; // notify API file descriptor
13 static void pnd_notify_hookup ( int fd );
15 pnd_notify_handle pnd_notify_init ( void ) {
25 p = malloc ( sizeof(pnd_notify_t) );
29 return ( NULL ); // uhh..
34 // setup some default watches
35 pnd_notify_hookup ( fd );
40 void pnd_notify_shutdown ( pnd_notify_handle h ) {
41 pnd_notify_t *p = (pnd_notify_t*) h;
48 static void pnd_notify_hookup ( int fd ) {
50 inotify_add_watch ( fd, "./testdata", IN_CREATE | IN_DELETE | IN_UNMOUNT );
55 unsigned char pnd_notify_rediscover_p ( pnd_notify_handle h ) {
56 pnd_notify_t *p = (pnd_notify_t*) h;
62 // don't block for long..
64 t.tv_usec = 0; //5000;
66 // only for our useful fd
68 FD_SET ( (p->fd), &rfds );
71 retcode = select ( (p->fd) + 1, &rfds, NULL, NULL, &t );
74 return ( 0 ); // hmm.. need a better error code handler
75 } else if ( retcode == 0 ) {
76 return ( 0 ); // timeout
79 if ( ! FD_ISSET ( (p->fd), &rfds ) ) {
83 // by this point, something must have happened on our watch
84 #define BINBUFLEN ( 100 * ( sizeof(struct inotify_event) + 30 ) ) /* approx 100 events in a shot? */
85 unsigned char binbuf [ BINBUFLEN ];
88 actuallen = read ( (p->fd), binbuf, BINBUFLEN );
90 if ( actuallen < 0 ) {
91 return ( 0 ); // error
92 } else if ( actuallen == 0 ) {
93 return ( 0 ); // nothing, or overflow, or .. whatever.
97 struct inotify_event *e;
99 while ( i < actuallen ) {
100 e = (struct inotify_event *) &binbuf [ i ];
106 printf ( "Got event against '%s'\n", e -> name );
113 i += ( sizeof(struct inotify_event) + e -> len );