// Watcher watches a set of files, delivering events to a channel.
type Watcher struct {
- Events chan Event
- Errors chan error
- isClosed bool // Set to true when Close() is first called
- mu sync.Mutex // Map access
- port syscall.Handle // Handle to completion port
- watches watchMap // Map of watches (key: i-number)
- input chan *input // Inputs to the reader are sent on this channel
- quit chan chan<- error
+ Events chan Event
+ Errors chan error
+
+ port syscall.Handle // Handle to completion port
+ input chan *input // Inputs to the reader are sent on this channel
+ quit chan chan<- error
+
+ mu sync.Mutex // Protects access to watches, isClosed
+ watches watchMap // Map of watches (key: i-number)
+ isClosed bool // Set to true when Close() is first called
}
// NewWatcher establishes a new watcher with the underlying OS and begins waiting for events.
// Close removes all watches and closes the events channel.
func (w *Watcher) Close() error {
+ w.mu.Lock()
+ defer w.mu.Unlock()
+
if w.isClosed {
return nil
}
// Add starts watching the named file or directory (non-recursively).
func (w *Watcher) Add(name string) error {
+ w.mu.Lock()
if w.isClosed {
return errors.New("watcher already closed")
}
+ w.mu.Unlock()
+
in := &input{
op: opAddWatch,
path: filepath.Clean(name),