+ w->wd = -2;
+ slot = wd & (EV_INOTIFY_HASHSIZE - 1);
+ wlist_del (&fs_hash [slot].head, (WL)w);
+
+ /* remove this watcher, if others are watching it, they will rearm */
+ inotify_rm_watch (fs_fd, wd);
+}
+
+static void noinline
+infy_wd (EV_P_ int slot, int wd, struct inotify_event *ev)
+{
+ if (slot < 0)
+ /* overflow, need to check for all hahs slots */
+ for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
+ infy_wd (EV_A_ slot, wd, ev);
+ else
+ {
+ WL w_;
+
+ for (w_ = fs_hash [slot & (EV_INOTIFY_HASHSIZE - 1)].head; w_; )
+ {
+ ev_stat *w = (ev_stat *)w_;
+ w_ = w_->next; /* lets us remove this watcher and all before it */
+
+ if (w->wd == wd || wd == -1)
+ {
+ if (ev->mask & (IN_IGNORED | IN_UNMOUNT | IN_DELETE_SELF))
+ {
+ w->wd = -1;
+ infy_add (EV_A_ w); /* re-add, no matter what */
+ }
+
+ stat_timer_cb (EV_A_ &w->timer, 0);
+ }
+ }
+ }
+}
+
+static void
+infy_cb (EV_P_ ev_io *w, int revents)
+{
+ char buf [EV_INOTIFY_BUFSIZE];
+ struct inotify_event *ev = (struct inotify_event *)buf;
+ int ofs;
+ int len = read (fs_fd, buf, sizeof (buf));
+
+ for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len)
+ infy_wd (EV_A_ ev->wd, ev->wd, ev);
+}
+
+void inline_size
+infy_init (EV_P)
+{
+ if (fs_fd != -2)
+ return;
+
+ fs_fd = inotify_init ();
+
+ if (fs_fd >= 0)
+ {
+ ev_io_init (&fs_w, infy_cb, fs_fd, EV_READ);
+ ev_set_priority (&fs_w, EV_MAXPRI);
+ ev_io_start (EV_A_ &fs_w);
+ }
+}
+
+void inline_size
+infy_fork (EV_P)
+{
+ int slot;
+
+ if (fs_fd < 0)
+ return;
+
+ close (fs_fd);
+ fs_fd = inotify_init ();
+
+ for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
+ {
+ WL w_ = fs_hash [slot].head;
+ fs_hash [slot].head = 0;
+
+ while (w_)
+ {
+ ev_stat *w = (ev_stat *)w_;
+ w_ = w_->next; /* lets us add this watcher */
+
+ w->wd = -1;
+
+ if (fs_fd >= 0)
+ infy_add (EV_A_ w); /* re-add, no matter what */
+ else
+ ev_timer_start (EV_A_ &w->timer);
+ }
+
+ }
+}
+
+#endif
+
+void
+ev_stat_stat (EV_P_ ev_stat *w)
+{
+ if (lstat (w->path, &w->attr) < 0)
+ w->attr.st_nlink = 0;
+ else if (!w->attr.st_nlink)
+ w->attr.st_nlink = 1;
+}
+
+static void noinline
+stat_timer_cb (EV_P_ ev_timer *w_, int revents)
+{
+ ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
+
+ /* we copy this here each the time so that */
+ /* prev has the old value when the callback gets invoked */
+ w->prev = w->attr;
+ ev_stat_stat (EV_A_ w);
+
+ /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
+ if (
+ w->prev.st_dev != w->attr.st_dev
+ || w->prev.st_ino != w->attr.st_ino
+ || w->prev.st_mode != w->attr.st_mode
+ || w->prev.st_nlink != w->attr.st_nlink
+ || w->prev.st_uid != w->attr.st_uid
+ || w->prev.st_gid != w->attr.st_gid
+ || w->prev.st_rdev != w->attr.st_rdev
+ || w->prev.st_size != w->attr.st_size
+ || w->prev.st_atime != w->attr.st_atime
+ || w->prev.st_mtime != w->attr.st_mtime
+ || w->prev.st_ctime != w->attr.st_ctime
+ ) {
+ #if EV_USE_INOTIFY
+ infy_del (EV_A_ w);
+ infy_add (EV_A_ w);
+ ev_stat_stat (EV_A_ w); /* avoid race... */
+ #endif
+
+ ev_feed_event (EV_A_ w, EV_STAT);
+ }