From 5ccafcc3d6731f0c462cd9d5c1386f4187f779f4 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Tue, 28 May 2013 12:31:46 -0700 Subject: [PATCH] fixes on top of Florian's truncation detection (1ff299bc2) * initialize `Size` with the then-size of the file when ChangeEvents is called. remember that this function is expected to be called many times, one after another. this also passes the TestReSeekInotify test. * do not ignore errors from os.Stat. we panic errors like these now, but ideally should switch to tomb.Tomb for letting the user handle them. --- watch.go | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/watch.go b/watch.go index cf653d6..83f2d8c 100644 --- a/watch.go +++ b/watch.go @@ -8,6 +8,7 @@ import ( "path/filepath" "time" "sync" + // "fmt" ) // FileWatcher monitors file-level events. @@ -45,6 +46,7 @@ func (fw *InotifyFileWatcher) BlockUntilExists() error { defer w.RemoveWatch(filepath.Dir(fw.Filename)) for { evt := <-w.Event + // fmt.Printf("block until exits (inotify) evt: %v\n", evt) if evt.Name == fw.Filename { break } @@ -53,7 +55,7 @@ func (fw *InotifyFileWatcher) BlockUntilExists() error { } // ChangeEvents returns a channel that gets updated when the file is ready to be read. -func (fw *InotifyFileWatcher) ChangeEvents(_ os.FileInfo) chan bool { +func (fw *InotifyFileWatcher) ChangeEvents(fi os.FileInfo) chan bool { w, err := fsnotify.NewWatcher() if err != nil { panic(err) @@ -65,6 +67,8 @@ func (fw *InotifyFileWatcher) ChangeEvents(_ os.FileInfo) chan bool { ch := make(chan bool) + fw.Size = fi.Size() + go func() { defer w.Close() defer w.RemoveWatch(fw.Filename) @@ -74,6 +78,7 @@ func (fw *InotifyFileWatcher) ChangeEvents(_ os.FileInfo) chan bool { prevSize := fw.Size evt := <-w.Event + // fmt.Printf("inotify change evt: %v\n", evt) switch { case evt.IsDelete(): fallthrough @@ -82,9 +87,14 @@ func (fw *InotifyFileWatcher) ChangeEvents(_ os.FileInfo) chan bool { return case evt.IsModify(): - fi, _ := os.Stat(fw.Filename) + fi, err := os.Stat(fw.Filename) + if err != nil { + // XXX: no panic here + panic(err) + } fw.Size = fi.Size() + // fmt.Printf("WATCH: prevSize=%d; fs.Size=%d\n", prevSize, fw.Size) if prevSize > 0 && prevSize > fw.Size { return } @@ -124,6 +134,7 @@ func (fw *PollingFileWatcher) BlockUntilExists() error { return err } time.Sleep(POLL_DURATION) + println("blocking..") } panic("unreachable") }