fix block until exists function

This commit is contained in:
miraclesu 2015-12-09 15:29:06 +08:00
parent 157f8ef18f
commit 6aef373ea6
3 changed files with 146 additions and 42 deletions

View File

@ -155,7 +155,7 @@ func TestLocationEnd(_t *testing.T) {
func TestLocationMiddle(_t *testing.T) { func TestLocationMiddle(_t *testing.T) {
// Test reading from middle. // Test reading from middle.
t := NewTailTest("location-end", _t) t := NewTailTest("location-middle", _t)
t.CreateFile("test.txt", "hello\nworld\n") t.CreateFile("test.txt", "hello\nworld\n")
tail := t.StartTail("test.txt", Config{Follow: true, Location: &SeekInfo{-6, os.SEEK_END}}) tail := t.StartTail("test.txt", Config{Follow: true, Location: &SeekInfo{-6, os.SEEK_END}})
go t.VerifyTailOutput(tail, []string{"world", "more", "data"}) go t.VerifyTailOutput(tail, []string{"world", "more", "data"})
@ -331,6 +331,28 @@ func TestTell(_t *testing.T) {
tail.Cleanup() tail.Cleanup()
} }
func TestBlockUntilExists(_t *testing.T) {
t := NewTailTest("block-until-file-exists", _t)
config := Config{
Follow: true,
}
tail := t.StartTail("test.txt", config)
go func() {
time.Sleep(100 * time.Millisecond)
t.CreateFile("test.txt", "hello world\n")
}()
for l := range tail.Lines {
if l.Text != "hello world" {
t.Fatalf("mismatch; expected hello world, but got %s",
l.Text)
}
break
}
t.RemoveFile("test.txt")
tail.Stop()
tail.Cleanup()
}
// Test library // Test library
type TailTest struct { type TailTest struct {

View File

@ -20,19 +20,16 @@ type InotifyFileWatcher struct {
} }
func NewInotifyFileWatcher(filename string) *InotifyFileWatcher { func NewInotifyFileWatcher(filename string) *InotifyFileWatcher {
fw := &InotifyFileWatcher{filename, 0} fw := &InotifyFileWatcher{filepath.Clean(filename), 0}
return fw return fw
} }
func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error { func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
dirname := filepath.Dir(fw.Filename) err := WatchCreate(fw.Filename)
// Watch for new files to be created in the parent directory.
err := Watch(dirname)
if err != nil { if err != nil {
return err return err
} }
defer RemoveWatch(dirname) defer RemoveWatchCreate(fw.Filename)
// Do a real check now as the file might have been created before // Do a real check now as the file might have been created before
// calling `WatchFlags` above. // calling `WatchFlags` above.
@ -48,7 +45,7 @@ func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
case evt, ok := <-events: case evt, ok := <-events:
if !ok { if !ok {
return fmt.Errorf("inotify watcher has been closed") return fmt.Errorf("inotify watcher has been closed")
} else if evt.Name == fw.Filename { } else if filepath.Clean(evt.Name) == fw.Filename {
return nil return nil
} }
case <-t.Dying(): case <-t.Dying():

View File

@ -5,6 +5,7 @@ package watch
import ( import (
"log" "log"
"os" "os"
"path/filepath"
"sync" "sync"
"syscall" "syscall"
@ -18,15 +19,21 @@ type InotifyTracker struct {
watcher *fsnotify.Watcher watcher *fsnotify.Watcher
chans map[string]chan fsnotify.Event chans map[string]chan fsnotify.Event
done map[string]chan bool done map[string]chan bool
watchNums map[string]int
watch chan *watchInfo watch chan *watchInfo
remove chan string remove chan *watchInfo
error chan error error chan error
} }
type watchInfo struct { type watchInfo struct {
op fsnotify.Op
fname string fname string
} }
func (this *watchInfo) isCreate() bool {
return this.op == fsnotify.Create
}
var ( var (
// globally shared InotifyTracker; ensures only one fsnotify.Watcher is used // globally shared InotifyTracker; ensures only one fsnotify.Watcher is used
shared *InotifyTracker shared *InotifyTracker
@ -38,8 +45,9 @@ var (
mux: sync.Mutex{}, mux: sync.Mutex{},
chans: make(map[string]chan fsnotify.Event), chans: make(map[string]chan fsnotify.Event),
done: make(map[string]chan bool), done: make(map[string]chan bool),
watchNums: make(map[string]int),
watch: make(chan *watchInfo), watch: make(chan *watchInfo),
remove: make(chan string), remove: make(chan *watchInfo),
error: make(chan error), error: make(chan error),
} }
go shared.run() go shared.run()
@ -50,29 +58,58 @@ var (
// Watch signals the run goroutine to begin watching the input filename // Watch signals the run goroutine to begin watching the input filename
func Watch(fname string) error { func Watch(fname string) error {
return watch(&watchInfo{
fname: fname,
})
}
// Watch create signals the run goroutine to begin watching the input filename
// if call the WatchCreate function, don't call the Cleanup, call the RemoveWatchCreate
func WatchCreate(fname string) error {
return watch(&watchInfo{
op: fsnotify.Create,
fname: fname,
})
}
func watch(winfo *watchInfo) 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{ winfo.fname = filepath.Clean(winfo.fname)
fname: fname, shared.watch <- winfo
}
return <-shared.error return <-shared.error
} }
// RemoveWatch signals the run goroutine to remove the watch for the input filename // RemoveWatch signals the run goroutine to remove the watch for the input filename
func RemoveWatch(fname string) { func RemoveWatch(fname string) {
remove(&watchInfo{
fname: fname,
})
}
// RemoveWatch create signals the run goroutine to remove the watch for the input filename
func RemoveWatchCreate(fname string) {
remove(&watchInfo{
op: fsnotify.Create,
fname: fname,
})
}
func remove(winfo *watchInfo) {
// start running the shared InotifyTracker if not already running // start running the shared InotifyTracker if not already running
once.Do(goRun) once.Do(goRun)
winfo.fname = filepath.Clean(winfo.fname)
shared.mux.Lock() shared.mux.Lock()
done := shared.done[fname] done := shared.done[winfo.fname]
if done != nil { if done != nil {
delete(shared.done, fname) delete(shared.done, winfo.fname)
close(done) close(done)
} }
shared.mux.Unlock() shared.mux.Unlock()
shared.remove <- fname shared.remove <- winfo
} }
// Events returns a channel to which FileEvents corresponding to the input filename // Events returns a channel to which FileEvents corresponding to the input filename
@ -92,36 +129,84 @@ 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) addWatch(fname string) error { func (shared *InotifyTracker) addWatch(winfo *watchInfo) error {
shared.mux.Lock() shared.mux.Lock()
defer shared.mux.Unlock() defer shared.mux.Unlock()
if shared.chans[fname] == nil { if shared.chans[winfo.fname] == nil {
shared.chans[fname] = make(chan fsnotify.Event) shared.chans[winfo.fname] = make(chan fsnotify.Event)
shared.done[fname] = make(chan bool) shared.done[winfo.fname] = make(chan bool)
} }
return shared.watcher.Add(fname)
fname := winfo.fname
if winfo.isCreate() {
// Watch for new files to be created in the parent directory.
fname = filepath.Dir(fname)
}
// already in inotify watch
if shared.watchNums[fname] > 0 {
shared.watchNums[fname]++
if winfo.isCreate() {
shared.watchNums[winfo.fname]++
}
return nil
}
err := shared.watcher.Add(fname)
if err == nil {
shared.watchNums[fname]++
if winfo.isCreate() {
shared.watchNums[winfo.fname]++
}
}
return err
} }
// removeWatch calls fsnotify.RemoveWatch for the input filename and closes the // removeWatch calls fsnotify.RemoveWatch for the input filename and closes the
// corresponding events channel. // corresponding events channel.
func (shared *InotifyTracker) removeWatch(fname string) { func (shared *InotifyTracker) removeWatch(winfo *watchInfo) {
shared.mux.Lock() shared.mux.Lock()
defer shared.mux.Unlock() defer shared.mux.Unlock()
if ch := shared.chans[fname]; ch != nil { ch := shared.chans[winfo.fname]
shared.watcher.Remove(fname) if ch == nil {
return
}
delete(shared.chans, fname) fname := winfo.fname
if winfo.isCreate() {
// Watch for new files to be created in the parent directory.
fname = filepath.Dir(fname)
}
shared.watchNums[fname]--
if shared.watchNums[fname] == 0 {
delete(shared.watchNums, fname)
// TODO: handle error
shared.watcher.Remove(fname)
}
delete(shared.chans, winfo.fname)
close(ch) close(ch)
if !winfo.isCreate() {
return
}
shared.watchNums[winfo.fname]--
if shared.watchNums[winfo.fname] == 0 {
delete(shared.watchNums, winfo.fname)
} }
} }
// sendEvent sends the input event to the appropriate Tail. // sendEvent sends the input event to the appropriate Tail.
func (shared *InotifyTracker) sendEvent(event fsnotify.Event) { func (shared *InotifyTracker) sendEvent(event fsnotify.Event) {
name := filepath.Clean(event.Name)
shared.mux.Lock() shared.mux.Lock()
ch := shared.chans[event.Name] ch := shared.chans[name]
done := shared.done[event.Name] done := shared.done[name]
shared.mux.Unlock() shared.mux.Unlock()
if ch != nil && done != nil { if ch != nil && done != nil {
@ -144,10 +229,10 @@ func (shared *InotifyTracker) run() {
for { for {
select { select {
case winfo := <-shared.watch: case winfo := <-shared.watch:
shared.error <- shared.addWatch(winfo.fname) shared.error <- shared.addWatch(winfo)
case fname := <-shared.remove: case winfo := <-shared.remove:
shared.removeWatch(fname) shared.removeWatch(winfo)
case event, open := <-shared.watcher.Events: case event, open := <-shared.watcher.Events:
if !open { if !open {