Module: Inotify
- Defined in:
- ext/inotify/inotify.c
Class Method Summary collapse
-
.inotify_watch(path, processor) ⇒ Object
Calls the block with the changed paths when something changes in the specified path.
Class Method Details
.inotify_watch(path, processor) ⇒ Object
Calls the block with the changed paths when something changes in the specified path. This method blocks the thread forever.
Inotify.watch('/tmp', Proc.new { |files| p files })
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 |
# File 'ext/inotify/inotify.c', line 41
static VALUE inotify_watch(VALUE self, VALUE path, VALUE processor)
{
int length, i;
int notify;
int watch;
char buffer[BUF_LEN];
VALUE mFile = rb_const_get(rb_cObject, rb_intern("File"));
fd_set rfds;
struct timeval tv;
int retval;
notify = inotify_init();
if (notify < 0) perror("inotify_init");
watch = inotify_add_watch(notify, RSTRING_PTR(path), IN_MODIFY | IN_CREATE | IN_DELETE );
for (;;)
{
FD_ZERO(&rfds);
FD_SET(notify, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 500;
retval = select(notify + 1, &rfds, NULL, NULL, &tv);
if (retval < 0) {
perror("select()");
return Qfalse;
} else if (FD_ISSET(notify, &rfds)) {
length = read(notify, buffer, BUF_LEN);
if (length < 0) perror("read");
i = 0;
while(i < length) {
struct inotify_event *event = (struct inotify_event *)&buffer[i];
if (event->len) {
rb_funcall(processor, rb_intern("call"), 2,
rb_funcall(mFile, rb_intern("join"), 2, path, rb_str_new2(event->name)),
inotify_event_flags(event->mask)
);
}
i += EVENT_SIZE + event->len;
}
}
}
inotify_rm_watch(notify, watch);
close(notify);
return Qnil;
}
|