Upgrade to fsnotify.v1

This commit is contained in:
Alex Liu 2015-09-10 16:47:13 -07:00 committed by Benoit Sigoure
parent d46611791d
commit 8b4773e24e
3 changed files with 21 additions and 30 deletions

View File

@ -11,4 +11,4 @@ go:
- 1.4.2 - 1.4.2
install: install:
- go get gopkg.in/fsnotify.v0 - go get gopkg.in/fsnotify.v1

View File

@ -9,7 +9,7 @@ import (
"github.com/hpcloud/tail/util" "github.com/hpcloud/tail/util"
"gopkg.in/fsnotify.v0" "gopkg.in/fsnotify.v1"
"gopkg.in/tomb.v1" "gopkg.in/tomb.v1"
) )
@ -28,7 +28,7 @@ func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
dirname := filepath.Dir(fw.Filename) dirname := filepath.Dir(fw.Filename)
// Watch for new files to be created in the parent directory. // Watch for new files to be created in the parent directory.
err := WatchFlags(dirname, fsnotify.FSN_CREATE) err := Watch(dirname)
if err != nil { if err != nil {
return err return err
} }
@ -77,7 +77,7 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileCh
for { for {
prevSize := fw.Size prevSize := fw.Size
var evt *fsnotify.FileEvent var evt *fsnotify.Event
var ok bool var ok bool
select { select {
@ -90,14 +90,14 @@ func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileCh
} }
switch { switch {
case evt.IsDelete(): case evt.Op&fsnotify.Remove == fsnotify.Remove:
fallthrough fallthrough
case evt.IsRename(): case evt.Op&fsnotify.Rename == fsnotify.Rename:
changes.NotifyDeleted() changes.NotifyDeleted()
return return
case evt.IsModify(): case evt.Op&fsnotify.Write == fsnotify.Write:
fi, err := os.Stat(fw.Filename) fi, err := os.Stat(fw.Filename)
if err != nil { if err != nil {
if os.IsNotExist(err) { if os.IsNotExist(err) {

View File

@ -10,13 +10,13 @@ import (
"github.com/hpcloud/tail/util" "github.com/hpcloud/tail/util"
"gopkg.in/fsnotify.v0" "gopkg.in/fsnotify.v1"
) )
type InotifyTracker struct { type InotifyTracker struct {
mux sync.Mutex mux sync.Mutex
watcher *fsnotify.Watcher watcher *fsnotify.Watcher
chans map[string]chan *fsnotify.FileEvent chans map[string]chan *fsnotify.Event
done map[string]chan bool done map[string]chan bool
watch chan *watchInfo watch chan *watchInfo
remove chan string remove chan string
@ -25,7 +25,6 @@ type InotifyTracker struct {
type watchInfo struct { type watchInfo struct {
fname string fname string
flags uint32
} }
var ( var (
@ -37,7 +36,7 @@ var (
goRun = func() { goRun = func() {
shared = &InotifyTracker{ shared = &InotifyTracker{
mux: sync.Mutex{}, mux: sync.Mutex{},
chans: make(map[string]chan *fsnotify.FileEvent), chans: make(map[string]chan *fsnotify.Event),
done: make(map[string]chan bool), done: make(map[string]chan bool),
watch: make(chan *watchInfo), watch: make(chan *watchInfo),
remove: make(chan string), remove: make(chan string),
@ -49,21 +48,13 @@ var (
logger = log.New(os.Stderr, "", log.LstdFlags) logger = log.New(os.Stderr, "", log.LstdFlags)
) )
// WatchFlags signals the run goroutine to begin watching the input filename using // Watch signals the run goroutine to begin watching the input filename
// using all flags.
func Watch(fname string) error { 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 // start running the shared InotifyTracker if not already running
once.Do(goRun) once.Do(goRun)
shared.watch <- &watchInfo{ shared.watch <- &watchInfo{
fname: fname, fname: fname,
flags: flags,
} }
return <-shared.error return <-shared.error
} }
@ -87,7 +78,7 @@ func RemoveWatch(fname string) {
// Events returns a channel to which FileEvents corresponding to the input filename // 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 // will be sent. This channel will be closed when removeWatch is called on this
// filename. // filename.
func Events(fname string) chan *fsnotify.FileEvent { func Events(fname string) chan *fsnotify.Event {
shared.mux.Lock() shared.mux.Lock()
defer shared.mux.Unlock() defer shared.mux.Unlock()
@ -101,15 +92,15 @@ func Cleanup(fname string) {
// watchFlags calls fsnotify.WatchFlags for the input filename and flags, creating // watchFlags calls fsnotify.WatchFlags for the input filename and flags, creating
// a new Watcher if the previous Watcher was closed. // 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() shared.mux.Lock()
defer shared.mux.Unlock() defer shared.mux.Unlock()
if shared.chans[fname] == nil { 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) 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 // 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() defer shared.mux.Unlock()
if ch := shared.chans[fname]; ch != nil { if ch := shared.chans[fname]; ch != nil {
shared.watcher.RemoveWatch(fname) shared.watcher.Remove(fname)
delete(shared.chans, fname) delete(shared.chans, fname)
close(ch) close(ch)
@ -127,7 +118,7 @@ func (shared *InotifyTracker) removeWatch(fname string) {
} }
// sendEvent sends the input event to the appropriate Tail. // 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() shared.mux.Lock()
ch := shared.chans[event.Name] ch := shared.chans[event.Name]
done := shared.done[event.Name] done := shared.done[event.Name]
@ -153,18 +144,18 @@ func (shared *InotifyTracker) run() {
for { for {
select { select {
case winfo := <-shared.watch: case winfo := <-shared.watch:
shared.error <- shared.watchFlags(winfo.fname, winfo.flags) shared.error <- shared.addWatch(winfo.fname)
case fname := <-shared.remove: case fname := <-shared.remove:
shared.removeWatch(fname) shared.removeWatch(fname)
case event, open := <-shared.watcher.Event: case event, open := <-shared.watcher.Events:
if !open { if !open {
return return
} }
shared.sendEvent(event) shared.sendEvent(&event)
case err, open := <-shared.watcher.Error: case err, open := <-shared.watcher.Errors:
if !open { if !open {
return return
} else if err != nil { } else if err != nil {