Skip to content

Commit

Permalink
Added inotify to FSEvent.
Browse files Browse the repository at this point in the history
  • Loading branch information
brixen committed Sep 1, 2014
1 parent e97d56f commit eabd8e1
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
50 changes: 45 additions & 5 deletions vm/builtin/fsevent.cpp
Expand Up @@ -5,7 +5,11 @@

#include "ontology.hpp"

#include "util/logger.hpp"

namespace rubinius {
using namespace utilities;

void FSEvent::init(STATE) {
GO(fsevent).set(ontology::new_class_under(state, "FSEvent", G(rubinius)));
G(fsevent)->set_object_type(state, FSEventType);
Expand All @@ -25,9 +29,11 @@ namespace rubinius {

FSEvent* FSEvent::create(STATE) {
FSEvent* fsevent = state->new_object<FSEvent>(G(fsevent));
fsevent->kq_ = kqueue();

state->memory()->needs_finalization(fsevent, (FinalizerFunction)&FSEvent::finalize);
if((fsevent->kq_ = kqueue()) < 0) {
logger::error("%s: unable to create kqueue", strerror(errno));
} else {
state->memory()->needs_finalization(fsevent, (FinalizerFunction)&FSEvent::finalize);
}

return fsevent;
}
Expand Down Expand Up @@ -66,24 +72,58 @@ namespace rubinius {
#elif HAVE_INOTIFY

void FSEvent::finalize(STATE, FSEvent* fsevent) {
if(fsevent->in_ > 0) ::close(fsevent->in_);
}

FSEvent* FSEvent::create(STATE) {
FSEvent* fsevent = state->new_object<FSEvent>(G(fsevent));
// TODO: set inotify state
fsevent->watch_set_ = false;
if((fsevent->in_ = inotify_init()) < 0) {
logger::error("%s: unable to create inotify", strerror(errno));
} else {
state->memory()->needs_finalization(fsevent, (FinalizerFunction)&FSEvent::finalize);
}

return fsevent;
}

Object* FSEvent::watch_file(STATE, Fixnum* fd, String* path) {
this->fileno(state, fd);
this->path(state, path);

inotify_add_watch(in_, path->c_str(state), IN_MODIFY | IN_ONESHOT);
watch_set_ = true;

return cNil;
}

Object* FSEvent::watch_file(STATE, int fd, const char* path) {
return watch_file(state, Fixnum::from(fd), String::create(state, path));
}

#ifdef NAME_MAX
#define RBX_FSEVENT_NAME_MAX NAME_MAX
#else
#define RBX_FSEVENT_NAME_MAX 65535
#endif

#define RBX_FSEVENT_BUF_LEN (sizeof(struct inotify_event) + RBX_FSEVENT_NAME_MAX)

Object* FSEvent::wait_for_event(STATE) {
return cNil;
char buf[RBX_FSEVENT_BUF_LEN];
int status;

if(!watch_set_) {
inotify_add_watch(in_, path_->c_str(state), IN_MODIFY | IN_ONESHOT);
} else {
watch_set_ = false;
}

status = read(in_, buf, RBX_FSEVENT_BUF_LEN);

if(status <= 0) return cNil;

return cTrue;
}

#endif
Expand Down
5 changes: 5 additions & 0 deletions vm/builtin/fsevent.hpp
Expand Up @@ -8,6 +8,8 @@
#include <sys/event.h>
#include <sys/time.h>
#elif HAVE_INOTIFY
#include <unistd.h>
#include <sys/inotify.h>
#else
#error "No file system event utility is available. Please submit an issue."
#endif
Expand Down Expand Up @@ -36,6 +38,9 @@ namespace rubinius {
struct kevent filter_;

#elif HAVE_INOTIFY
private:
int in_;
bool watch_set_;
#endif

public:
Expand Down

0 comments on commit eabd8e1

Please sign in to comment.