+.ie n .Sh """ev_stat"" \- did the file attributes just change?"
+.el .Sh "\f(CWev_stat\fP \- did the file attributes just change?"
+.IX Subsection "ev_stat - did the file attributes just change?"
+This watches a filesystem path for attribute changes. That is, it calls
+\&\f(CW\*(C`stat\*(C'\fR regularly (or when the \s-1OS\s0 says it changed) and sees if it changed
+compared to the last time, invoking the callback if it did.
+.PP
+The path does not need to exist: changing from \*(L"path exists\*(R" to \*(L"path does
+not exist\*(R" is a status change like any other. The condition \*(L"path does
+not exist\*(R" is signified by the \f(CW\*(C`st_nlink\*(C'\fR field being zero (which is
+otherwise always forced to be at least one) and all the other fields of
+the stat buffer having unspecified contents.
+.PP
+The path \fIshould\fR be absolute and \fImust not\fR end in a slash. If it is
+relative and your working directory changes, the behaviour is undefined.
+.PP
+Since there is no standard to do this, the portable implementation simply
+calls \f(CW\*(C`stat (2)\*(C'\fR regularly on the path to see if it changed somehow. You
+can specify a recommended polling interval for this case. If you specify
+a polling interval of \f(CW0\fR (highly recommended!) then a \fIsuitable,
+unspecified default\fR value will be used (which you can expect to be around
+five seconds, although this might change dynamically). Libev will also
+impose a minimum interval which is currently around \f(CW0.1\fR, but thats
+usually overkill.
+.PP
+This watcher type is not meant for massive numbers of stat watchers,
+as even with OS-supported change notifications, this can be
+resource\-intensive.
+.PP
+At the time of this writing, only the Linux inotify interface is
+implemented (implementing kqueue support is left as an exercise for the
+reader). Inotify will be used to give hints only and should not change the
+semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers, which means that libev sometimes needs
+to fall back to regular polling again even with inotify, but changes are
+usually detected immediately, and if the file exists there will be no
+polling.
+.PP
+\fIInotify\fR
+.IX Subsection "Inotify"
+.PP
+When \f(CW\*(C`inotify (7)\*(C'\fR support has been compiled into libev (generally only
+available on Linux) and present at runtime, it will be used to speed up
+change detection where possible. The inotify descriptor will be created lazily
+when the first \f(CW\*(C`ev_stat\*(C'\fR watcher is being started.
+.PP
+Inotify presense does not change the semantics of \f(CW\*(C`ev_stat\*(C'\fR watchers
+except that changes might be detected earlier, and in some cases, to avoid
+making regular \f(CW\*(C`stat\*(C'\fR calls. Even in the presense of inotify support
+there are many cases where libev has to resort to regular \f(CW\*(C`stat\*(C'\fR polling.
+.PP
+(There is no support for kqueue, as apparently it cannot be used to
+implement this functionality, due to the requirement of having a file
+descriptor open on the object at all times).
+.PP
+\fIThe special problem of stat time resolution\fR
+.IX Subsection "The special problem of stat time resolution"
+.PP
+The \f(CW\*(C`stat ()\*(C'\fR syscall only supports full-second resolution portably, and
+even on systems where the resolution is higher, many filesystems still
+only support whole seconds.
+.PP
+That means that, if the time is the only thing that changes, you might
+miss updates: on the first update, \f(CW\*(C`ev_stat\*(C'\fR detects a change and calls
+your callback, which does something. When there is another update within
+the same second, \f(CW\*(C`ev_stat\*(C'\fR will be unable to detect it.
+.PP
+The solution to this is to delay acting on a change for a second (or till
+the next second boundary), using a roughly one-second delay \f(CW\*(C`ev_timer\*(C'\fR
+(\f(CW\*(C`ev_timer_set (w, 0., 1.01); ev_timer_again (loop, w)\*(C'\fR). The \f(CW.01\fR
+is added to work around small timing inconsistencies of some operating
+systems.
+.PP
+\fIWatcher-Specific Functions and Data Members\fR
+.IX Subsection "Watcher-Specific Functions and Data Members"
+.IP "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)" 4
+.IX Item "ev_stat_init (ev_stat *, callback, const char *path, ev_tstamp interval)"
+.PD 0
+.IP "ev_stat_set (ev_stat *, const char *path, ev_tstamp interval)" 4
+.IX Item "ev_stat_set (ev_stat *, const char *path, ev_tstamp interval)"
+.PD
+Configures the watcher to wait for status changes of the given
+\&\f(CW\*(C`path\*(C'\fR. The \f(CW\*(C`interval\*(C'\fR is a hint on how quickly a change is expected to
+be detected and should normally be specified as \f(CW0\fR to let libev choose
+a suitable value. The memory pointed to by \f(CW\*(C`path\*(C'\fR must point to the same
+path for as long as the watcher is active.
+.Sp
+The callback will be receive \f(CW\*(C`EV_STAT\*(C'\fR when a change was detected,
+relative to the attributes at the time the watcher was started (or the
+last change was detected).
+.IP "ev_stat_stat (ev_stat *)" 4
+.IX Item "ev_stat_stat (ev_stat *)"
+Updates the stat buffer immediately with new values. If you change the
+watched path in your callback, you could call this fucntion to avoid
+detecting this change (while introducing a race condition). Can also be
+useful simply to find out the new values.
+.IP "ev_statdata attr [read\-only]" 4
+.IX Item "ev_statdata attr [read-only]"
+The most-recently detected attributes of the file. Although the type is of
+\&\f(CW\*(C`ev_statdata\*(C'\fR, this is usually the (or one of the) \f(CW\*(C`struct stat\*(C'\fR types
+suitable for your system. If the \f(CW\*(C`st_nlink\*(C'\fR member is \f(CW0\fR, then there
+was some error while \f(CW\*(C`stat\*(C'\fRing the file.
+.IP "ev_statdata prev [read\-only]" 4
+.IX Item "ev_statdata prev [read-only]"
+The previous attributes of the file. The callback gets invoked whenever
+\&\f(CW\*(C`prev\*(C'\fR != \f(CW\*(C`attr\*(C'\fR.
+.IP "ev_tstamp interval [read\-only]" 4
+.IX Item "ev_tstamp interval [read-only]"
+The specified interval.
+.IP "const char *path [read\-only]" 4
+.IX Item "const char *path [read-only]"
+The filesystem path that is being watched.
+.PP
+\fIExamples\fR
+.IX Subsection "Examples"
+.PP
+Example: Watch \f(CW\*(C`/etc/passwd\*(C'\fR for attribute changes.
+.PP
+.Vb 15
+\& static void
+\& passwd_cb (struct ev_loop *loop, ev_stat *w, int revents)
+\& {
+\& /* /etc/passwd changed in some way */
+\& if (w->attr.st_nlink)
+\& {
+\& printf ("passwd current size %ld\en", (long)w->attr.st_size);
+\& printf ("passwd current atime %ld\en", (long)w->attr.st_mtime);
+\& printf ("passwd current mtime %ld\en", (long)w->attr.st_mtime);
+\& }
+\& else
+\& /* you shalt not abuse printf for puts */
+\& puts ("wow, /etc/passwd is not there, expect problems. "
+\& "if this is windows, they already arrived\en");
+\& }
+.Ve
+.PP
+.Vb 2
+\& ...
+\& ev_stat passwd;
+.Ve
+.PP
+.Vb 2
+\& ev_stat_init (&passwd, passwd_cb, "/etc/passwd", 0.);
+\& ev_stat_start (loop, &passwd);
+.Ve
+.PP
+Example: Like above, but additionally use a one-second delay so we do not
+miss updates (however, frequent updates will delay processing, too, so
+one might do the work both on \f(CW\*(C`ev_stat\*(C'\fR callback invocation \fIand\fR on
+\&\f(CW\*(C`ev_timer\*(C'\fR callback invocation).
+.PP
+.Vb 2
+\& static ev_stat passwd;
+\& static ev_timer timer;
+.Ve
+.PP
+.Vb 4
+\& static void
+\& timer_cb (EV_P_ ev_timer *w, int revents)
+\& {
+\& ev_timer_stop (EV_A_ w);
+.Ve
+.PP
+.Vb 2
+\& /* now it's one second after the most recent passwd change */
+\& }
+.Ve
+.PP
+.Vb 6
+\& static void
+\& stat_cb (EV_P_ ev_stat *w, int revents)
+\& {
+\& /* reset the one-second timer */
+\& ev_timer_again (EV_A_ &timer);
+\& }
+.Ve
+.PP
+.Vb 4
+\& ...
+\& ev_stat_init (&passwd, stat_cb, "/etc/passwd", 0.);
+\& ev_stat_start (loop, &passwd);
+\& ev_timer_init (&timer, timer_cb, 0., 1.01);
+.Ve