2013-05-29 07:34:36 +08:00
|
|
|
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
|
|
|
|
|
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
2013-10-12 09:19:43 +08:00
|
|
|
"github.com/ActiveState/tail/util"
|
2013-05-29 10:33:52 +08:00
|
|
|
"launchpad.net/tomb"
|
2013-05-29 07:34:36 +08:00
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PollingFileWatcher polls the file for changes.
|
|
|
|
type PollingFileWatcher struct {
|
|
|
|
Filename string
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPollingFileWatcher(filename string) *PollingFileWatcher {
|
|
|
|
fw := &PollingFileWatcher{filename, 0}
|
|
|
|
return fw
|
|
|
|
}
|
|
|
|
|
|
|
|
var POLL_DURATION time.Duration
|
|
|
|
|
2013-09-23 15:03:51 +08:00
|
|
|
func (fw *PollingFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
|
2013-05-29 07:34:36 +08:00
|
|
|
for {
|
|
|
|
if _, err := os.Stat(fw.Filename); err == nil {
|
|
|
|
return nil
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-29 10:33:52 +08:00
|
|
|
select {
|
|
|
|
case <-time.After(POLL_DURATION):
|
|
|
|
continue
|
|
|
|
case <-t.Dying():
|
2013-05-30 02:35:27 +08:00
|
|
|
return tomb.ErrDying
|
2013-05-29 10:33:52 +08:00
|
|
|
}
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|
|
|
|
panic("unreachable")
|
|
|
|
}
|
|
|
|
|
2013-09-23 15:03:51 +08:00
|
|
|
func (fw *PollingFileWatcher) ChangeEvents(t *tomb.Tomb, origFi os.FileInfo) *FileChanges {
|
2013-05-30 05:32:59 +08:00
|
|
|
changes := NewFileChanges()
|
2013-05-29 07:34:36 +08:00
|
|
|
var prevModTime time.Time
|
|
|
|
|
|
|
|
// XXX: use tomb.Tomb to cleanly manage these goroutines. replace
|
2013-10-12 09:19:43 +08:00
|
|
|
// the fatal (below) with tomb's Kill.
|
2013-05-29 07:34:36 +08:00
|
|
|
|
|
|
|
fw.Size = origFi.Size()
|
|
|
|
|
|
|
|
go func() {
|
2013-05-30 05:32:59 +08:00
|
|
|
defer changes.Close()
|
2013-09-23 15:03:51 +08:00
|
|
|
|
2013-05-29 07:34:36 +08:00
|
|
|
prevSize := fw.Size
|
|
|
|
for {
|
|
|
|
select {
|
2013-05-30 02:35:27 +08:00
|
|
|
case <-t.Dying():
|
2013-05-30 05:32:59 +08:00
|
|
|
return
|
2013-05-29 07:34:36 +08:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(POLL_DURATION)
|
|
|
|
fi, err := os.Stat(fw.Filename)
|
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2013-05-30 05:32:59 +08:00
|
|
|
// File does not exist (has been deleted).
|
|
|
|
changes.NotifyDeleted()
|
|
|
|
return
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|
2013-10-12 09:19:43 +08:00
|
|
|
// XXX: report this error back to the user
|
|
|
|
util.Fatal("Failed to stat file %v: %v", fw.Filename, err)
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|
|
|
|
|
2013-05-30 05:32:59 +08:00
|
|
|
// File got moved/renamed?
|
2013-05-29 07:34:36 +08:00
|
|
|
if !os.SameFile(origFi, fi) {
|
2013-05-30 05:32:59 +08:00
|
|
|
changes.NotifyDeleted()
|
|
|
|
return
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|
|
|
|
|
2013-05-30 05:32:59 +08:00
|
|
|
// File got truncated?
|
2013-05-29 07:34:36 +08:00
|
|
|
fw.Size = fi.Size()
|
|
|
|
if prevSize > 0 && prevSize > fw.Size {
|
2013-05-30 05:32:59 +08:00
|
|
|
changes.NotifyTruncated()
|
2013-05-30 06:08:24 +08:00
|
|
|
prevSize = fw.Size
|
2013-05-29 07:34:36 +08:00
|
|
|
continue
|
|
|
|
}
|
2014-06-12 06:22:14 +08:00
|
|
|
// File got bigger?
|
|
|
|
if prevSize > 0 && prevSize < fw.Size {
|
|
|
|
changes.NotifyModified()
|
|
|
|
prevSize = fw.Size
|
|
|
|
continue
|
|
|
|
}
|
2013-05-30 06:08:24 +08:00
|
|
|
prevSize = fw.Size
|
2013-05-29 07:34:36 +08:00
|
|
|
|
2013-05-30 05:32:59 +08:00
|
|
|
// File was appended to (changed)?
|
2013-05-29 07:34:36 +08:00
|
|
|
modTime := fi.ModTime()
|
|
|
|
if modTime != prevModTime {
|
|
|
|
prevModTime = modTime
|
2013-05-30 05:32:59 +08:00
|
|
|
changes.NotifyModified()
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-05-30 05:32:59 +08:00
|
|
|
return changes
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
POLL_DURATION = 250 * time.Millisecond
|
|
|
|
}
|