2016-04-27 06:47:46 +08:00
|
|
|
// Copyright (c) 2015 HPE Software Inc. All rights reserved.
|
2014-04-13 07:59:40 +08:00
|
|
|
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
|
|
|
|
|
2013-11-13 12:15:27 +08:00
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2015-07-31 03:06:40 +08:00
|
|
|
"os"
|
2015-12-09 15:29:06 +08:00
|
|
|
"path/filepath"
|
2013-11-13 12:15:27 +08:00
|
|
|
"sync"
|
2015-08-04 05:38:42 +08:00
|
|
|
"syscall"
|
2015-07-31 03:06:40 +08:00
|
|
|
|
|
|
|
"github.com/hpcloud/tail/util"
|
|
|
|
|
2018-03-21 23:04:36 +08:00
|
|
|
"gopkg.in/fsnotify/fsnotify.v1"
|
2013-11-13 12:15:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type InotifyTracker struct {
|
2015-12-09 15:29:06 +08:00
|
|
|
mux sync.Mutex
|
|
|
|
watcher *fsnotify.Watcher
|
|
|
|
chans map[string]chan fsnotify.Event
|
|
|
|
done map[string]chan bool
|
|
|
|
watchNums map[string]int
|
|
|
|
watch chan *watchInfo
|
|
|
|
remove chan *watchInfo
|
|
|
|
error chan error
|
2015-08-05 08:42:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type watchInfo struct {
|
2015-12-09 15:29:06 +08:00
|
|
|
op fsnotify.Op
|
2015-08-05 08:42:45 +08:00
|
|
|
fname string
|
2013-11-13 12:15:27 +08:00
|
|
|
}
|
|
|
|
|
2015-12-09 15:29:06 +08:00
|
|
|
func (this *watchInfo) isCreate() bool {
|
|
|
|
return this.op == fsnotify.Create
|
|
|
|
}
|
|
|
|
|
2015-07-31 03:06:40 +08:00
|
|
|
var (
|
2015-08-05 08:42:45 +08:00
|
|
|
// globally shared InotifyTracker; ensures only one fsnotify.Watcher is used
|
2015-08-06 06:48:10 +08:00
|
|
|
shared *InotifyTracker
|
2015-08-05 08:42:45 +08:00
|
|
|
|
|
|
|
// these are used to ensure the shared InotifyTracker is run exactly once
|
2015-08-06 06:48:10 +08:00
|
|
|
once = sync.Once{}
|
2015-08-05 08:42:45 +08:00
|
|
|
goRun = func() {
|
2015-08-06 06:48:10 +08:00
|
|
|
shared = &InotifyTracker{
|
2015-12-09 15:29:06 +08:00
|
|
|
mux: sync.Mutex{},
|
|
|
|
chans: make(map[string]chan fsnotify.Event),
|
|
|
|
done: make(map[string]chan bool),
|
|
|
|
watchNums: make(map[string]int),
|
|
|
|
watch: make(chan *watchInfo),
|
|
|
|
remove: make(chan *watchInfo),
|
|
|
|
error: make(chan error),
|
2015-08-06 06:48:10 +08:00
|
|
|
}
|
2015-08-05 08:42:45 +08:00
|
|
|
go shared.run()
|
2015-07-31 03:06:40 +08:00
|
|
|
}
|
2015-08-05 08:42:45 +08:00
|
|
|
|
2015-07-31 03:06:40 +08:00
|
|
|
logger = log.New(os.Stderr, "", log.LstdFlags)
|
|
|
|
)
|
|
|
|
|
2015-09-11 07:47:13 +08:00
|
|
|
// Watch signals the run goroutine to begin watching the input filename
|
2015-08-06 06:48:10 +08:00
|
|
|
func Watch(fname string) error {
|
2015-12-09 15:29:06 +08:00
|
|
|
return watch(&watchInfo{
|
|
|
|
fname: fname,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch create signals the run goroutine to begin watching the input filename
|
|
|
|
// if call the WatchCreate function, don't call the Cleanup, call the RemoveWatchCreate
|
|
|
|
func WatchCreate(fname string) error {
|
|
|
|
return watch(&watchInfo{
|
|
|
|
op: fsnotify.Create,
|
|
|
|
fname: fname,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func watch(winfo *watchInfo) error {
|
2015-08-05 08:42:45 +08:00
|
|
|
// start running the shared InotifyTracker if not already running
|
|
|
|
once.Do(goRun)
|
2015-07-31 03:06:40 +08:00
|
|
|
|
2015-12-09 15:29:06 +08:00
|
|
|
winfo.fname = filepath.Clean(winfo.fname)
|
|
|
|
shared.watch <- winfo
|
2015-08-05 08:42:45 +08:00
|
|
|
return <-shared.error
|
|
|
|
}
|
2015-07-31 03:06:40 +08:00
|
|
|
|
2015-08-05 08:42:45 +08:00
|
|
|
// RemoveWatch signals the run goroutine to remove the watch for the input filename
|
2016-09-03 04:15:29 +08:00
|
|
|
func RemoveWatch(fname string) error {
|
|
|
|
return remove(&watchInfo{
|
2015-12-09 15:29:06 +08:00
|
|
|
fname: fname,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveWatch create signals the run goroutine to remove the watch for the input filename
|
2016-09-03 04:15:29 +08:00
|
|
|
func RemoveWatchCreate(fname string) error {
|
|
|
|
return remove(&watchInfo{
|
2015-12-09 15:29:06 +08:00
|
|
|
op: fsnotify.Create,
|
|
|
|
fname: fname,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-09-03 04:15:29 +08:00
|
|
|
func remove(winfo *watchInfo) error {
|
2015-08-05 08:42:45 +08:00
|
|
|
// start running the shared InotifyTracker if not already running
|
|
|
|
once.Do(goRun)
|
|
|
|
|
2015-12-09 15:29:06 +08:00
|
|
|
winfo.fname = filepath.Clean(winfo.fname)
|
2015-08-06 05:03:38 +08:00
|
|
|
shared.mux.Lock()
|
2015-12-09 15:29:06 +08:00
|
|
|
done := shared.done[winfo.fname]
|
2015-08-06 05:03:38 +08:00
|
|
|
if done != nil {
|
2015-12-09 15:29:06 +08:00
|
|
|
delete(shared.done, winfo.fname)
|
2015-08-06 05:03:38 +08:00
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
shared.mux.Unlock()
|
|
|
|
|
2015-12-09 15:29:06 +08:00
|
|
|
shared.remove <- winfo
|
2017-03-21 08:50:07 +08:00
|
|
|
return <-shared.error
|
2013-11-13 12:15:27 +08:00
|
|
|
}
|
|
|
|
|
2015-08-05 08:42:45 +08:00
|
|
|
// Events returns a channel to which FileEvents corresponding to the input filename
|
|
|
|
// will be sent. This channel will be closed when removeWatch is called on this
|
|
|
|
// filename.
|
2016-03-01 14:44:16 +08:00
|
|
|
func Events(fname string) <-chan fsnotify.Event {
|
2015-07-31 03:06:40 +08:00
|
|
|
shared.mux.Lock()
|
|
|
|
defer shared.mux.Unlock()
|
|
|
|
|
2015-08-05 08:42:45 +08:00
|
|
|
return shared.chans[fname]
|
|
|
|
}
|
|
|
|
|
2015-08-06 06:48:10 +08:00
|
|
|
// Cleanup removes the watch for the input filename if necessary.
|
2016-09-03 04:15:29 +08:00
|
|
|
func Cleanup(fname string) error {
|
|
|
|
return RemoveWatch(fname)
|
2015-08-05 08:42:45 +08:00
|
|
|
}
|
2015-07-31 03:06:40 +08:00
|
|
|
|
2015-08-05 08:42:45 +08:00
|
|
|
// watchFlags calls fsnotify.WatchFlags for the input filename and flags, creating
|
|
|
|
// a new Watcher if the previous Watcher was closed.
|
2015-12-09 15:29:06 +08:00
|
|
|
func (shared *InotifyTracker) addWatch(winfo *watchInfo) error {
|
2015-08-05 08:42:45 +08:00
|
|
|
shared.mux.Lock()
|
|
|
|
defer shared.mux.Unlock()
|
2015-07-31 03:06:40 +08:00
|
|
|
|
2015-12-09 15:29:06 +08:00
|
|
|
if shared.chans[winfo.fname] == nil {
|
|
|
|
shared.chans[winfo.fname] = make(chan fsnotify.Event)
|
2017-03-19 05:20:41 +08:00
|
|
|
}
|
|
|
|
if shared.done[winfo.fname] == nil {
|
2015-12-09 15:29:06 +08:00
|
|
|
shared.done[winfo.fname] = make(chan bool)
|
2013-11-13 12:15:27 +08:00
|
|
|
}
|
2015-12-09 15:29:06 +08:00
|
|
|
|
|
|
|
fname := winfo.fname
|
|
|
|
if winfo.isCreate() {
|
|
|
|
// Watch for new files to be created in the parent directory.
|
|
|
|
fname = filepath.Dir(fname)
|
|
|
|
}
|
|
|
|
|
2017-03-21 08:50:07 +08:00
|
|
|
var err error
|
2015-12-09 15:29:06 +08:00
|
|
|
// already in inotify watch
|
2017-03-21 08:50:07 +08:00
|
|
|
if shared.watchNums[fname] == 0 {
|
|
|
|
err = shared.watcher.Add(fname)
|
2015-12-09 15:29:06 +08:00
|
|
|
}
|
2017-03-21 09:08:26 +08:00
|
|
|
if err == nil {
|
|
|
|
shared.watchNums[fname]++
|
|
|
|
}
|
2015-12-09 15:29:06 +08:00
|
|
|
return err
|
2013-11-13 12:15:27 +08:00
|
|
|
}
|
|
|
|
|
2015-08-05 08:42:45 +08:00
|
|
|
// removeWatch calls fsnotify.RemoveWatch for the input filename and closes the
|
|
|
|
// corresponding events channel.
|
2017-03-21 08:50:07 +08:00
|
|
|
func (shared *InotifyTracker) removeWatch(winfo *watchInfo) error {
|
2015-07-31 03:06:40 +08:00
|
|
|
shared.mux.Lock()
|
|
|
|
|
2015-12-09 15:29:06 +08:00
|
|
|
ch := shared.chans[winfo.fname]
|
2017-03-21 08:50:07 +08:00
|
|
|
if ch != nil {
|
|
|
|
delete(shared.chans, winfo.fname)
|
|
|
|
close(ch)
|
2015-12-09 15:29:06 +08:00
|
|
|
}
|
|
|
|
|
2017-03-21 08:50:07 +08:00
|
|
|
fname := winfo.fname
|
|
|
|
if winfo.isCreate() {
|
|
|
|
// Watch for new files to be created in the parent directory.
|
|
|
|
fname = filepath.Dir(fname)
|
|
|
|
}
|
|
|
|
shared.watchNums[fname]--
|
|
|
|
watchNum := shared.watchNums[fname]
|
|
|
|
if watchNum == 0 {
|
|
|
|
delete(shared.watchNums, fname)
|
2015-12-09 15:29:06 +08:00
|
|
|
}
|
2017-03-21 08:50:07 +08:00
|
|
|
shared.mux.Unlock()
|
2015-12-09 15:29:06 +08:00
|
|
|
|
2017-03-21 08:50:07 +08:00
|
|
|
var err error
|
|
|
|
// If we were the last ones to watch this file, unsubscribe from inotify.
|
|
|
|
// This needs to happen after releasing the lock because fsnotify waits
|
|
|
|
// synchronously for the kernel to acknowledge the removal of the watch
|
|
|
|
// for this file, which causes us to deadlock if we still held the lock.
|
|
|
|
if watchNum == 0 {
|
|
|
|
err = shared.watcher.Remove(fname)
|
2015-08-05 08:42:45 +08:00
|
|
|
}
|
2017-03-21 08:50:07 +08:00
|
|
|
|
|
|
|
return err
|
2015-07-31 03:06:40 +08:00
|
|
|
}
|
|
|
|
|
2015-08-05 08:42:45 +08:00
|
|
|
// sendEvent sends the input event to the appropriate Tail.
|
2015-09-11 07:56:51 +08:00
|
|
|
func (shared *InotifyTracker) sendEvent(event fsnotify.Event) {
|
2015-12-09 15:29:06 +08:00
|
|
|
name := filepath.Clean(event.Name)
|
|
|
|
|
2015-08-06 05:03:38 +08:00
|
|
|
shared.mux.Lock()
|
2015-12-09 15:29:06 +08:00
|
|
|
ch := shared.chans[name]
|
|
|
|
done := shared.done[name]
|
2015-08-06 05:03:38 +08:00
|
|
|
shared.mux.Unlock()
|
|
|
|
|
|
|
|
if ch != nil && done != nil {
|
2015-08-05 08:42:45 +08:00
|
|
|
select {
|
|
|
|
case ch <- event:
|
2015-08-06 05:03:38 +08:00
|
|
|
case <-done:
|
2015-08-05 08:42:45 +08:00
|
|
|
}
|
|
|
|
}
|
2015-07-31 03:06:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// run starts the goroutine in which the shared struct reads events from its
|
|
|
|
// Watcher's Event channel and sends the events to the appropriate Tail.
|
|
|
|
func (shared *InotifyTracker) run() {
|
2015-08-05 08:42:45 +08:00
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
util.Fatal("failed to create Watcher")
|
|
|
|
}
|
|
|
|
shared.watcher = watcher
|
|
|
|
|
2015-07-31 03:06:40 +08:00
|
|
|
for {
|
|
|
|
select {
|
2015-08-05 08:42:45 +08:00
|
|
|
case winfo := <-shared.watch:
|
2015-12-09 15:29:06 +08:00
|
|
|
shared.error <- shared.addWatch(winfo)
|
2015-08-05 08:42:45 +08:00
|
|
|
|
2015-12-09 15:29:06 +08:00
|
|
|
case winfo := <-shared.remove:
|
2017-03-21 08:50:07 +08:00
|
|
|
shared.error <- shared.removeWatch(winfo)
|
2015-08-05 08:42:45 +08:00
|
|
|
|
2015-09-11 07:47:13 +08:00
|
|
|
case event, open := <-shared.watcher.Events:
|
2015-07-31 03:06:40 +08:00
|
|
|
if !open {
|
|
|
|
return
|
|
|
|
}
|
2015-09-11 07:56:51 +08:00
|
|
|
shared.sendEvent(event)
|
2015-07-31 03:06:40 +08:00
|
|
|
|
2015-09-11 07:47:13 +08:00
|
|
|
case err, open := <-shared.watcher.Errors:
|
2015-07-31 03:06:40 +08:00
|
|
|
if !open {
|
|
|
|
return
|
2015-08-04 05:38:42 +08:00
|
|
|
} else if err != nil {
|
|
|
|
sysErr, ok := err.(*os.SyscallError)
|
|
|
|
if !ok || sysErr.Err != syscall.EINTR {
|
|
|
|
logger.Printf("Error in Watcher Error channel: %s", err)
|
|
|
|
}
|
2015-07-31 03:06:40 +08:00
|
|
|
}
|
2013-11-13 12:15:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|