add truncation detection to PollingFileWatcher

This commit is contained in:
Sridhar Ratnakumar 2013-05-28 12:54:53 -07:00
parent 5ccafcc3d6
commit c90cd7b8db
1 changed files with 17 additions and 4 deletions

View File

@ -8,7 +8,7 @@ import (
"path/filepath" "path/filepath"
"time" "time"
"sync" "sync"
// "fmt" "fmt"
) )
// FileWatcher monitors file-level events. // FileWatcher monitors file-level events.
@ -46,7 +46,7 @@ func (fw *InotifyFileWatcher) BlockUntilExists() error {
defer w.RemoveWatch(filepath.Dir(fw.Filename)) defer w.RemoveWatch(filepath.Dir(fw.Filename))
for { for {
evt := <-w.Event evt := <-w.Event
// fmt.Printf("block until exits (inotify) evt: %v\n", evt) fmt.Printf("block until exits (inotify) evt: %v\n", evt)
if evt.Name == fw.Filename { if evt.Name == fw.Filename {
break break
} }
@ -78,7 +78,7 @@ func (fw *InotifyFileWatcher) ChangeEvents(fi os.FileInfo) chan bool {
prevSize := fw.Size prevSize := fw.Size
evt := <-w.Event evt := <-w.Event
// fmt.Printf("inotify change evt: %v\n", evt) fmt.Printf("inotify change evt: %v\n", evt)
switch { switch {
case evt.IsDelete(): case evt.IsDelete():
fallthrough fallthrough
@ -94,7 +94,7 @@ func (fw *InotifyFileWatcher) ChangeEvents(fi os.FileInfo) chan bool {
} }
fw.Size = fi.Size() fw.Size = fi.Size()
// fmt.Printf("WATCH: prevSize=%d; fs.Size=%d\n", prevSize, fw.Size) fmt.Printf("WATCH(inotify): prevSize=%d; fs.Size=%d\n", prevSize, fw.Size)
if prevSize > 0 && prevSize > fw.Size { if prevSize > 0 && prevSize > fw.Size {
return return
} }
@ -155,7 +155,10 @@ func (fw *PollingFileWatcher) ChangeEvents(origFi os.FileInfo) chan bool {
}() }()
} }
fw.Size = origFi.Size()
go func() { go func() {
prevSize := fw.Size
for { for {
select { select {
case <-stop: case <-stop:
@ -180,6 +183,14 @@ func (fw *PollingFileWatcher) ChangeEvents(origFi os.FileInfo) chan bool {
continue continue
} }
// Was the file truncated?
fw.Size = fi.Size()
fmt.Printf("WATCH(poll): prevSize=%d; fs.Size=%d\n", prevSize, fw.Size)
if prevSize > 0 && prevSize > fw.Size {
once.Do(stopAndClose)
continue
}
// If the file was changed since last check, notify. // If the file was changed since last check, notify.
modTime := fi.ModTime() modTime := fi.ModTime()
if modTime != prevModTime { if modTime != prevModTime {
@ -188,6 +199,8 @@ func (fw *PollingFileWatcher) ChangeEvents(origFi os.FileInfo) chan bool {
case ch <- true: case ch <- true:
default: default:
} }
}else{
fmt.Printf("polling; not modified: %v == %v\n", modTime, prevModTime)
} }
} }
}() }()