func (w *Watcher) Add(name string, ignoreEvents EventMask) error
This would allow stopping useless events at the sender instead of filtering them out at the receiver. It also allows a simple for the most basic case.
Subtree paths can be aggregated later in a smart implementation and even the implementation switched on the fly, if we are brave. So I wouldn't worry too much about those now.
func (w *Watcher) WatchFlags(path string, flags uint32) error
Take a look at how WatchFlags is implemented. A function called purgeEvents forwards events from an internal channel to the external Event channel. The flags are stored in a map with a mutex and some bookkeeping to apply the same flags to files within a directory watch. There are currently no tests for WatchFlags and probably some subtle bugs.
Rather than all this extra machinery to configure the watcher, we can let it send all events and apply a filter at the point where we receive them.
for event := range watcher.Events {
if IsOp(event, fsnotify.Create|fsnotify.Write) {
// do something
}
}
If you actually need to apply Op filters differently depending on the file/directory watched, it's easy enough check event.Name here too.
The truth of the matter is, filtering based on file operation is just one of many desirable filters. The ones I found useful are:
There are sure to be other possibilities as well. People have mentioned regular expression matches or excluding folders that begin with an underscore.
I am not talking about filtering out events in user space, but about the operating system kernel not having to generate them, not having to wakeup this thread/process.
This is especially important for background activity. I guess most users of this API actually are background activities.
I agree that smart filtering belongs to a third party library. Not bothering the kernel with useless work is an orthogonal matter which the current API is not solving.
"Your stream will receive events about individual files in the hierarchy you're watching instead of only receiving directory level notifications. Use this flag with care as it will generate significantly more events than without it."
This evening I spent some time with kqueue, rewriting a small part of fsnotify and inspecting the raw output under both OS X 10.9 and FreeBSD 9.1.When watching the path to a directory I found that:* adding, removing or saving a file resulted in a WRITE note on the directory
"Some of you may be familiar with existing file monitoring solutions... they are a pain in the butt to use, they are complex, they are not user friendly, and they have a lot of race conditions that you have to be aware of as a developer."
"We've extensively tested it, we rolled it out for several months to all our developers... what's actually on disk, what does Watchman think is on disk... when we found discrepancies we fixed it."