2016-04-27 06:47:46 +08:00
|
|
|
// Copyright (c) 2015 HPE Software Inc. All rights reserved.
|
2013-05-29 07:34:36 +08:00
|
|
|
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
|
|
|
|
|
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
2013-11-14 09:38:23 +08:00
|
|
|
"fmt"
|
2015-07-01 23:15:15 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2015-09-30 22:48:17 +08:00
|
|
|
"github.com/hpcloud/tail/util"
|
2015-07-31 03:06:40 +08:00
|
|
|
|
2015-09-11 07:47:13 +08:00
|
|
|
"gopkg.in/fsnotify.v1"
|
2014-06-15 14:39:34 +08:00
|
|
|
"gopkg.in/tomb.v1"
|
2013-05-29 07:34:36 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
// InotifyFileWatcher uses inotify to monitor file changes.
|
|
|
|
type InotifyFileWatcher struct {
|
|
|
|
Filename string
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
2015-07-31 03:06:40 +08:00
|
|
|
func NewInotifyFileWatcher(filename string) *InotifyFileWatcher {
|
2015-12-09 15:29:06 +08:00
|
|
|
fw := &InotifyFileWatcher{filepath.Clean(filename), 0}
|
2013-05-29 07:34:36 +08:00
|
|
|
return fw
|
|
|
|
}
|
|
|
|
|
2013-09-23 15:03:51 +08:00
|
|
|
func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
|
2015-12-09 15:29:06 +08:00
|
|
|
err := WatchCreate(fw.Filename)
|
2013-05-29 07:34:36 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-09 15:29:06 +08:00
|
|
|
defer RemoveWatchCreate(fw.Filename)
|
2013-05-29 07:34:36 +08:00
|
|
|
|
|
|
|
// Do a real check now as the file might have been created before
|
|
|
|
// calling `WatchFlags` above.
|
|
|
|
if _, err = os.Stat(fw.Filename); !os.IsNotExist(err) {
|
|
|
|
// file exists, or stat returned an error.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-08-06 06:48:10 +08:00
|
|
|
events := Events(fw.Filename)
|
2015-07-31 03:06:40 +08:00
|
|
|
|
2013-05-29 07:34:36 +08:00
|
|
|
for {
|
2013-05-29 10:33:52 +08:00
|
|
|
select {
|
2015-07-31 03:06:40 +08:00
|
|
|
case evt, ok := <-events:
|
2013-11-13 12:15:27 +08:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("inotify watcher has been closed")
|
2016-04-05 06:51:37 +08:00
|
|
|
}
|
|
|
|
evtName, err := filepath.Abs(evt.Name)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fwFilename, err := filepath.Abs(fw.Filename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if evtName == fwFilename {
|
2013-05-29 10:33:52 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
case <-t.Dying():
|
2013-05-30 02:35:27 +08:00
|
|
|
return tomb.ErrDying
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|
|
|
|
}
|
2013-05-29 10:33:52 +08:00
|
|
|
panic("unreachable")
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|
|
|
|
|
2015-12-24 16:47:46 +08:00
|
|
|
func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, pos int64) (*FileChanges, error) {
|
2015-08-06 06:48:10 +08:00
|
|
|
err := Watch(fw.Filename)
|
2013-05-29 07:34:36 +08:00
|
|
|
if err != nil {
|
2015-12-24 16:47:46 +08:00
|
|
|
return nil, err
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|
|
|
|
|
2015-12-24 16:47:46 +08:00
|
|
|
changes := NewFileChanges()
|
2015-10-28 06:45:55 +08:00
|
|
|
fw.Size = pos
|
2013-05-29 07:34:36 +08:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
|
2015-08-06 06:48:10 +08:00
|
|
|
events := Events(fw.Filename)
|
2015-07-31 03:06:40 +08:00
|
|
|
|
2013-05-29 07:34:36 +08:00
|
|
|
for {
|
|
|
|
prevSize := fw.Size
|
|
|
|
|
2015-09-11 07:56:51 +08:00
|
|
|
var evt fsnotify.Event
|
2013-11-13 12:15:27 +08:00
|
|
|
var ok bool
|
2013-05-30 02:35:27 +08:00
|
|
|
|
|
|
|
select {
|
2015-07-31 03:06:40 +08:00
|
|
|
case evt, ok = <-events:
|
2013-11-13 12:15:27 +08:00
|
|
|
if !ok {
|
2016-08-01 14:48:42 +08:00
|
|
|
RemoveWatch(fw.Filename)
|
2013-11-13 12:15:27 +08:00
|
|
|
return
|
|
|
|
}
|
2013-05-30 02:35:27 +08:00
|
|
|
case <-t.Dying():
|
2016-08-01 14:48:42 +08:00
|
|
|
RemoveWatch(fw.Filename)
|
2013-05-30 02:35:27 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-29 07:34:36 +08:00
|
|
|
switch {
|
2016-09-09 08:51:15 +08:00
|
|
|
//With an open fd, unlink(fd) - inotify returns IN_ATTRIB (==fsnotify.Chmod)
|
|
|
|
case evt.Op&fsnotify.Chmod == fsnotify.Chmod:
|
|
|
|
if _, err := os.Stat(fw.Filename); err != nil {
|
|
|
|
if ! os.IsNotExist(err) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fallthrough
|
|
|
|
|
2015-09-11 07:47:13 +08:00
|
|
|
case evt.Op&fsnotify.Remove == fsnotify.Remove:
|
2013-05-29 07:34:36 +08:00
|
|
|
fallthrough
|
|
|
|
|
2015-09-11 07:47:13 +08:00
|
|
|
case evt.Op&fsnotify.Rename == fsnotify.Rename:
|
2016-08-01 14:48:42 +08:00
|
|
|
RemoveWatch(fw.Filename)
|
2013-05-30 05:32:59 +08:00
|
|
|
changes.NotifyDeleted()
|
2013-05-29 07:34:36 +08:00
|
|
|
return
|
|
|
|
|
2015-09-11 07:47:13 +08:00
|
|
|
case evt.Op&fsnotify.Write == fsnotify.Write:
|
2013-05-29 07:34:36 +08:00
|
|
|
fi, err := os.Stat(fw.Filename)
|
|
|
|
if err != nil {
|
2013-09-21 00:47:55 +08:00
|
|
|
if os.IsNotExist(err) {
|
2016-08-01 14:48:42 +08:00
|
|
|
RemoveWatch(fw.Filename)
|
2013-09-21 00:47:55 +08:00
|
|
|
changes.NotifyDeleted()
|
|
|
|
return
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
fw.Size = fi.Size()
|
|
|
|
|
|
|
|
if prevSize > 0 && prevSize > fw.Size {
|
2013-05-30 05:32:59 +08:00
|
|
|
changes.NotifyTruncated()
|
2013-09-23 15:03:51 +08:00
|
|
|
} else {
|
2013-05-30 05:32:59 +08:00
|
|
|
changes.NotifyModified()
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|
2013-05-30 06:08:24 +08:00
|
|
|
prevSize = fw.Size
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2015-12-24 16:47:46 +08:00
|
|
|
return changes, nil
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|