2014-04-13 07:59:40 +08:00
|
|
|
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
|
|
|
|
|
2013-11-13 12:15:27 +08:00
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
2015-06-29 11:23:06 +08:00
|
|
|
"gopkg.in/fsnotify.v0"
|
2013-11-13 12:15:27 +08:00
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InotifyTracker struct {
|
|
|
|
mux sync.Mutex
|
|
|
|
watchers map[*fsnotify.Watcher]bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInotifyTracker() *InotifyTracker {
|
|
|
|
t := new(InotifyTracker)
|
|
|
|
t.watchers = make(map[*fsnotify.Watcher]bool)
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *InotifyTracker) NewWatcher() (*fsnotify.Watcher, error) {
|
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
w, err := fsnotify.NewWatcher()
|
|
|
|
if err == nil {
|
|
|
|
t.watchers[w] = true
|
|
|
|
}
|
|
|
|
return w, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *InotifyTracker) CloseWatcher(w *fsnotify.Watcher) (err error) {
|
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
if _, ok := t.watchers[w]; ok {
|
|
|
|
err = w.Close()
|
|
|
|
delete(t.watchers, w)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *InotifyTracker) CloseAll() {
|
|
|
|
t.mux.Lock()
|
|
|
|
defer t.mux.Unlock()
|
|
|
|
for w, _ := range t.watchers {
|
|
|
|
if err := w.Close(); err != nil {
|
|
|
|
log.Printf("Error closing watcher: %v", err)
|
|
|
|
}
|
|
|
|
delete(t.watchers, w)
|
|
|
|
}
|
|
|
|
}
|