From 8b4773e24eaaf3c98cb0e516a9d2ec6aaff9e193 Mon Sep 17 00:00:00 2001 From: Alex Liu Date: Thu, 10 Sep 2015 16:47:13 -0700 Subject: [PATCH] Upgrade to fsnotify.v1 --- .travis.yml | 2 +- watch/inotify.go | 12 ++++++------ watch/inotify_tracker.go | 37 ++++++++++++++----------------------- 3 files changed, 21 insertions(+), 30 deletions(-) diff --git a/.travis.yml b/.travis.yml index 5407eba..57c00e2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,4 +11,4 @@ go: - 1.4.2 install: - - go get gopkg.in/fsnotify.v0 + - go get gopkg.in/fsnotify.v1 diff --git a/watch/inotify.go b/watch/inotify.go index aecf172..0676ac3 100644 --- a/watch/inotify.go +++ b/watch/inotify.go @@ -9,7 +9,7 @@ import ( "github.com/hpcloud/tail/util" - "gopkg.in/fsnotify.v0" + "gopkg.in/fsnotify.v1" "gopkg.in/tomb.v1" ) @@ -28,7 +28,7 @@ func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error { dirname := filepath.Dir(fw.Filename) // Watch for new files to be created in the parent directory. - err := WatchFlags(dirname, fsnotify.FSN_CREATE) + err := Watch(dirname) if err != nil { return err } @@ -77,7 +77,7 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileCh for { prevSize := fw.Size - var evt *fsnotify.FileEvent + var evt *fsnotify.Event var ok bool select { @@ -90,14 +90,14 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileCh } switch { - case evt.IsDelete(): + case evt.Op&fsnotify.Remove == fsnotify.Remove: fallthrough - case evt.IsRename(): + case evt.Op&fsnotify.Rename == fsnotify.Rename: changes.NotifyDeleted() return - case evt.IsModify(): + case evt.Op&fsnotify.Write == fsnotify.Write: fi, err := os.Stat(fw.Filename) if err != nil { if os.IsNotExist(err) { diff --git a/watch/inotify_tracker.go b/watch/inotify_tracker.go index ffb8267..d47bc5d 100644 --- a/watch/inotify_tracker.go +++ b/watch/inotify_tracker.go @@ -10,13 +10,13 @@ import ( "github.com/hpcloud/tail/util" - "gopkg.in/fsnotify.v0" + "gopkg.in/fsnotify.v1" ) type InotifyTracker struct { mux sync.Mutex watcher *fsnotify.Watcher - chans map[string]chan *fsnotify.FileEvent + chans map[string]chan *fsnotify.Event done map[string]chan bool watch chan *watchInfo remove chan string @@ -25,7 +25,6 @@ type InotifyTracker struct { type watchInfo struct { fname string - flags uint32 } var ( @@ -37,7 +36,7 @@ var ( goRun = func() { shared = &InotifyTracker{ mux: sync.Mutex{}, - chans: make(map[string]chan *fsnotify.FileEvent), + chans: make(map[string]chan *fsnotify.Event), done: make(map[string]chan bool), watch: make(chan *watchInfo), remove: make(chan string), @@ -49,21 +48,13 @@ var ( logger = log.New(os.Stderr, "", log.LstdFlags) ) -// WatchFlags signals the run goroutine to begin watching the input filename using -// using all flags. +// Watch signals the run goroutine to begin watching the input filename func Watch(fname string) error { - return WatchFlags(fname, fsnotify.FSN_ALL) -} - -// WatchFlags signals the run goroutine to begin watching the input filename using -// using the input flags. -func WatchFlags(fname string, flags uint32) error { // start running the shared InotifyTracker if not already running once.Do(goRun) shared.watch <- &watchInfo{ fname: fname, - flags: flags, } return <-shared.error } @@ -87,7 +78,7 @@ func RemoveWatch(fname string) { // Events returns a channel to which FileEvents corresponding to the input filename // will be sent. This channel will be closed when removeWatch is called on this // filename. -func Events(fname string) chan *fsnotify.FileEvent { +func Events(fname string) chan *fsnotify.Event { shared.mux.Lock() defer shared.mux.Unlock() @@ -101,15 +92,15 @@ func Cleanup(fname string) { // watchFlags calls fsnotify.WatchFlags for the input filename and flags, creating // a new Watcher if the previous Watcher was closed. -func (shared *InotifyTracker) watchFlags(fname string, flags uint32) error { +func (shared *InotifyTracker) addWatch(fname string) error { shared.mux.Lock() defer shared.mux.Unlock() if shared.chans[fname] == nil { - shared.chans[fname] = make(chan *fsnotify.FileEvent) + shared.chans[fname] = make(chan *fsnotify.Event) shared.done[fname] = make(chan bool) } - return shared.watcher.WatchFlags(fname, flags) + return shared.watcher.Add(fname) } // removeWatch calls fsnotify.RemoveWatch for the input filename and closes the @@ -119,7 +110,7 @@ func (shared *InotifyTracker) removeWatch(fname string) { defer shared.mux.Unlock() if ch := shared.chans[fname]; ch != nil { - shared.watcher.RemoveWatch(fname) + shared.watcher.Remove(fname) delete(shared.chans, fname) close(ch) @@ -127,7 +118,7 @@ func (shared *InotifyTracker) removeWatch(fname string) { } // sendEvent sends the input event to the appropriate Tail. -func (shared *InotifyTracker) sendEvent(event *fsnotify.FileEvent) { +func (shared *InotifyTracker) sendEvent(event *fsnotify.Event) { shared.mux.Lock() ch := shared.chans[event.Name] done := shared.done[event.Name] @@ -153,18 +144,18 @@ func (shared *InotifyTracker) run() { for { select { case winfo := <-shared.watch: - shared.error <- shared.watchFlags(winfo.fname, winfo.flags) + shared.error <- shared.addWatch(winfo.fname) case fname := <-shared.remove: shared.removeWatch(fname) - case event, open := <-shared.watcher.Event: + case event, open := <-shared.watcher.Events: if !open { return } - shared.sendEvent(event) + shared.sendEvent(&event) - case err, open := <-shared.watcher.Error: + case err, open := <-shared.watcher.Errors: if !open { return } else if err != nil {