2013-05-29 07:34:36 +08:00
|
|
|
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
|
|
|
|
|
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
2015-09-30 22:48:17 +08:00
|
|
|
"github.com/hpcloud/tail/util"
|
2014-06-15 14:39:34 +08:00
|
|
|
"gopkg.in/tomb.v1"
|
2013-05-29 07:34:36 +08:00
|
|
|
"os"
|
2015-09-29 21:51:16 +08:00
|
|
|
"runtime"
|
2013-05-29 07:34:36 +08:00
|
|
|
"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 {
|
2015-09-30 22:48:17 +08:00
|
|
|
// Windows cannot delete a file if a handle is still open (tail keeps one open)
|
2015-09-29 21:51:16 +08:00
|
|
|
// so it gives access denied to anything trying to read it until all handles are released.
|
|
|
|
if os.IsNotExist(err) || (runtime.GOOS == "windows" && os.IsPermission(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
|
|
|
}
|
2014-06-26 21:47:21 +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
|
|
|
|
}
|