Created
March 12, 2014 20:28
-
-
Save matutter/9515634 to your computer and use it in GitHub Desktop.
Detecting file modifications the efficient way using the inotify library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/inotify.h> //filesystem events | |
class tinyListener { | |
public: | |
string name, list_name; | |
void addListener(string s) { | |
list_name = s; | |
} | |
bool good() { | |
return (list_name == name); | |
} | |
bool listenModify() { | |
long int event_size = sizeof(struct inotify_event), | |
buf_size = 1024 * ( event_size + 16 ) ; | |
struct inotify_event *event; | |
int length, i=0; | |
char buffer[buf_size]; | |
int feed = inotify_init(); | |
if( feed < 0 ) { return 0; /*cant get inode base*/ } // perm error | |
int watch = inotify_add_watch(feed, ".", IN_MODIFY); | |
length = read( feed, buffer, buf_size ); | |
if( length < 0 ) { return 0; /*cant read directory*/ } // bad spelling | |
i = 0; | |
while ( i < length ) { | |
event = ( struct inotify_event * ) &buffer[ i ]; | |
if ( event->len ) { | |
if ( event->mask && IN_MODIFY ) { | |
name = event->name; | |
return 1; | |
} | |
} | |
i += event_size + event->len; | |
} | |
inotify_rm_watch(feed, watch); | |
close(feed); | |
return 0; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment