file_watch: only use inotify on linux

Fixes build on *BSD platforms.

Fixes #262, Closes #261

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2019-11-11 20:46:06 +00:00
parent a1e6686455
commit 7328b3f891
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
2 changed files with 28 additions and 5 deletions

View File

@ -1,6 +1,8 @@
#include <errno.h>
#include <string.h>
#ifdef HAS_INOTIFY
#include <sys/inotify.h>
#endif
#include <ev.h>
#include <uthash.h>
@ -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) {
@ -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;
@ -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);
@ -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;

View File

@ -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,