int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle, const char* filename, uv_fs_event_cb cb, int flags) { int fd; loop->counters.fs_event_init++; /* We don't support any flags yet. */ assert(!flags); if (cb == NULL) { uv__set_sys_error(loop, EINVAL); return -1; } /* TODO open asynchronously - but how do we report back errors? */ if ((fd = open(filename, O_RDONLY)) == -1) { uv__set_sys_error(loop, errno); return -1; } uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT); handle->filename = strdup(filename); handle->fflags = 0; handle->cb = cb; handle->fd = fd; uv__fs_event_start(handle); return 0; }
int uv_fs_event_init(uv_loop_t* loop, uv_fs_event_t* handle, const char* filename, uv_fs_event_cb cb, int flags) { int fd; #if defined(__APPLE__) struct stat statbuf; #endif /* defined(__APPLE__) */ /* We don't support any flags yet. */ assert(!flags); /* TODO open asynchronously - but how do we report back errors? */ if ((fd = open(filename, O_RDONLY)) == -1) { uv__set_sys_error(loop, errno); return -1; } uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT); uv__handle_start(handle); /* FIXME shouldn't start automatically */ handle->filename = strdup(filename); handle->fflags = 0; handle->cb = cb; handle->fd = fd; #if defined(__APPLE__) /* Nullify field to perform checks later */ handle->cf_eventstream = NULL; if (fstat(fd, &statbuf)) goto fallback; /* FSEvents works only with directories */ if (!(statbuf.st_mode & S_IFDIR)) goto fallback; return uv__fsevents_init(handle); fallback: #endif /* defined(__APPLE__) */ uv__fs_event_start(handle); return 0; }
static void uv__fs_event(EV_P_ ev_io* w, int revents) { uv_fs_event_t* handle; int events; assert(revents == EV_LIBUV_KQUEUE_HACK); handle = container_of(w, uv_fs_event_t, event_watcher); if (handle->fflags & (NOTE_ATTRIB | NOTE_EXTEND)) events = UV_CHANGE; else events = UV_RENAME; handle->cb(handle, NULL, events, 0); uv__fs_event_stop(handle); /* File watcher operates in one-shot mode, re-arm it. */ if (handle->fd != -1) uv__fs_event_start(handle); }