Skip to content
This repository has been archived by the owner on Sep 10, 2021. It is now read-only.

Commit

Permalink
Inotify fixes: file_delete only fires after fds have been closed, use…
Browse files Browse the repository at this point in the history
… syscall hackery for older linux distributions (*cough* debian)
  • Loading branch information
tmm1 committed Mar 17, 2009
1 parent 25d61be commit 4058fe9
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion ext/em.cpp
Expand Up @@ -2054,7 +2054,7 @@ void EventMachine_t::_ReadInotifyEvents()
msg = "";

if (EventCallback && strlen(msg) > 0)
(*EventCallback)(Watches [event->wd]->GetBinding().c_str(), EM_CONNECTION_READ, msg, strlen(msg));
(*EventCallback)(Watches [event.wd]->GetBinding().c_str(), EM_CONNECTION_READ, msg, strlen(msg));
}
#endif
}
Expand Down
3 changes: 2 additions & 1 deletion ext/extconf.rb
Expand Up @@ -15,7 +15,8 @@ def add_define(name)
add_define 'BUILD_FOR_RUBY'
add_define 'HAVE_RBTRAP' if have_var('rb_trap_immediate', ['ruby.h', 'rubysig.h'])
add_define "HAVE_TBR" if have_func('rb_thread_blocking_region')# and have_macro('RUBY_UBF_IO', 'ruby.h')
add_define "HAVE_INOTIFY" if have_func('inotify_init', ['sys/inotify.h'])
add_define "HAVE_INOTIFY" if inotify = have_func('inotify_init', 'sys/inotify.h')
add_define "HAVE_OLD_INOTIFY" if !inotify && have_macro('__NR_inotify_init', 'sys/syscall.h')

# Minor platform details between *nix and Windows:

Expand Down
21 changes: 17 additions & 4 deletions ext/project.h
Expand Up @@ -55,10 +55,6 @@ See the file COPYING for complete licensing information.
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <pwd.h>
#ifdef HAVE_INOTIFY
#include <sys/inotify.h>
#define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
#endif
typedef int SOCKET;
#define closesocket close
#define INVALID_SOCKET -1
Expand Down Expand Up @@ -106,6 +102,23 @@ using namespace std;
#include <sys/queue.h>
#endif

#ifdef HAVE_INOTIFY
#include <sys/inotify.h>
#endif

#ifdef HAVE_OLD_INOTIFY
#include <sys/syscall.h>
#include <linux/inotify.h>
static inline int inotify_init (void) { return syscall (__NR_inotify_init); }
static inline int inotify_add_watch (int fd, const char *name, __u32 mask) { return syscall (__NR_inotify_add_watch, fd, name, mask); }
static inline int inotify_rm_watch (int fd, __u32 wd) { return syscall (__NR_inotify_rm_watch, fd, wd); }
#define HAVE_INOTIFY 1
#endif

#ifdef HAVE_INOTIFY
#define INOTIFY_EVENT_SIZE (sizeof(struct inotify_event))
#endif

#include "binder.h"
#include "em.h"
#include "epoll.h"
Expand Down
3 changes: 3 additions & 0 deletions lib/em/filewatcher.rb
Expand Up @@ -29,6 +29,9 @@ def file_modified
# Should be redefined with the user's custom callback that will be fired when the file is deleted.
# When the file is deleted, stop_watching will be called after this to make sure everything is
# cleaned up correctly.
#
# Note that on linux (with inotify), file_deleted will not be called until all open file descriptors to
# the file have been closed.
def file_deleted
end

Expand Down
2 changes: 1 addition & 1 deletion tests/test_file_watch.rb
Expand Up @@ -38,7 +38,7 @@ def test_events
File.open(file.path, 'w'){ |f| f.puts 'hi' }

# delete it
EM.add_timer(0.25){ file.delete }
EM.add_timer(0.25){ file.close; file.delete }
}

assert_equal($path, $tmp_path)
Expand Down

0 comments on commit 4058fe9

Please sign in to comment.