Skip to content

Commit

Permalink
file_watch: only use inotify on linux
Browse files Browse the repository at this point in the history
Fixes build on *BSD platforms.

Fixes yshui#262, Closes yshui#261

Signed-off-by: Yuxuan Shui <[email protected]>
  • Loading branch information
yshui committed Nov 11, 2019
1 parent a1e6686 commit 7328b3f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
25 changes: 20 additions & 5 deletions src/file_watch.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <errno.h>
#include <string.h>
#ifdef HAS_INOTIFY
#include <sys/inotify.h>
#endif

#include <ev.h>
#include <uthash.h>
Expand All @@ -26,9 +28,11 @@ struct file_watch_registry {

static void file_watch_ev_cb(EV_P_ struct ev_io *w, int revent attr_unused) {
auto fwr = (struct file_watch_registry *)w;
struct inotify_event inotify_event;

while (true) {
int wd = -1;
#ifdef HAS_INOTIFY
struct inotify_event inotify_event;
auto ret = read(w->fd, &inotify_event, sizeof(struct inotify_event));
if (ret < 0) {
if (errno != EAGAIN) {
Expand All @@ -37,9 +41,11 @@ static void file_watch_ev_cb(EV_P_ struct ev_io *w, int revent attr_unused) {
}
break;
}
wd = inotify_event.wd;
#endif

struct watched_file *wf = NULL;
HASH_FIND_INT(fwr->reg, &inotify_event.wd, wf);
HASH_FIND_INT(fwr->reg, &wd, wf);
if (!wf) {
log_warn("Got notification for a file I didn't watch.");
continue;
Expand All @@ -50,11 +56,17 @@ static void file_watch_ev_cb(EV_P_ struct ev_io *w, int revent attr_unused) {

void *file_watch_init(EV_P) {
log_debug("Starting watching for file changes");
int fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
int fd = -1;
#ifdef HAS_INOTIFY
fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
if (fd < 0) {
log_error("inotify_init1 failed: %s", strerror(errno));
return NULL;
}
#else
log_info("No file watching support found on the host system.");
return NULL;
#endif
auto fwr = ccalloc(1, struct file_watch_registry);
ev_io_init(&fwr->w, file_watch_ev_cb, fd, EV_READ);
ev_io_start(EV_A_ & fwr->w);
Expand All @@ -80,12 +92,15 @@ void file_watch_destroy(EV_P_ void *_fwr) {
bool file_watch_add(void *_fwr, const char *filename, file_watch_cb_t cb, void *ud) {
log_debug("Adding \"%s\" to watched files", filename);
auto fwr = (struct file_watch_registry *)_fwr;
int wd = inotify_add_watch(fwr->w.fd, filename,
IN_CLOSE_WRITE | IN_MOVE_SELF | IN_DELETE_SELF);
int wd = -1;
#ifdef HAS_INOTIFY
wd = inotify_add_watch(fwr->w.fd, filename,
IN_CLOSE_WRITE | IN_MOVE_SELF | IN_DELETE_SELF);
if (wd < 0) {
log_error("Failed to watch file \"%s\": %s", filename, strerror(errno));
return false;
}
#endif

auto w = ccalloc(1, struct watched_file);
w->wd = wd;
Expand Down
8 changes: 8 additions & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ if get_option('unittest')
cflags += ['-DUNIT_TEST']
endif

host_system = host_machine.system()
if host_system == 'linux'
cflags += ['-DHAS_INOTIFY']
elif host_system == 'freebsd' or host_system == 'netbsd' or
host_system == 'dragonfly' or host_system == 'openbsd'
cflags += ['-DHAS_KQUEUE']
endif

subdir('backend')

picom = executable('picom', srcs, c_args: cflags,
Expand Down

0 comments on commit 7328b3f

Please sign in to comment.