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"
|
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"
|
|
|
|
|
2015-09-11 07:47:13 +08:00
|
|
|
"gopkg.in/fsnotify.v1"
|
2013-11-13 12:15:27 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type InotifyTracker struct {
|
2015-07-31 03:06:40 +08:00
|
|
|
mux sync.Mutex
|
|
|
|
watcher *fsnotify.Watcher
|
2015-09-11 07:47:13 +08:00
|
|
|
chans map[string]chan *fsnotify.Event
|
2015-08-06 05:03:38 +08:00
|
|
|
done map[string]chan bool
|
2015-08-05 08:42:45 +08:00
|
|
|
watch chan *watchInfo
|
|
|
|
remove chan string
|
|
|
|
error chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
type watchInfo struct {
|
|
|
|
fname string
|
2013-11-13 12:15:27 +08:00
|
|
|
}
|
|
|
|
|
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{
|
|
|
|
mux: sync.Mutex{},
|
2015-09-11 07:47:13 +08:00
|
|
|
chans: make(map[string]chan *fsnotify.Event),
|
2015-08-06 06:48:10 +08:00
|
|
|
done: make(map[string]chan bool),
|
|
|
|
watch: make(chan *watchInfo),
|
|
|
|
remove: make(chan string),
|
|
|
|
error: make(chan error),
|
|
|
|
}
|
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-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-08-05 08:42:45 +08:00
|
|
|
shared.watch <- &watchInfo{
|
|
|
|
fname: fname,
|
2015-07-31 03:06:40 +08:00
|
|
|
}
|
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
|
2015-08-06 06:48:10 +08:00
|
|
|
func RemoveWatch(fname string) {
|
2015-08-05 08:42:45 +08:00
|
|
|
// start running the shared InotifyTracker if not already running
|
|
|
|
once.Do(goRun)
|
|
|
|
|
2015-08-06 05:03:38 +08:00
|
|
|
shared.mux.Lock()
|
|
|
|
done := shared.done[fname]
|
|
|
|
if done != nil {
|
|
|
|
delete(shared.done, fname)
|
|
|
|
close(done)
|
|
|
|
}
|
|
|
|
shared.mux.Unlock()
|
|
|
|
|
2015-08-05 08:42:45 +08:00
|
|
|
shared.remove <- fname
|
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.
|
2015-09-11 07:47:13 +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.
|
2015-08-05 08:42:45 +08:00
|
|
|
func Cleanup(fname string) {
|
2015-08-06 06:48:10 +08:00
|
|
|
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-09-11 07:47:13 +08:00
|
|
|
func (shared *InotifyTracker) addWatch(fname string) 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-08-05 08:42:45 +08:00
|
|
|
if shared.chans[fname] == nil {
|
2015-09-11 07:47:13 +08:00
|
|
|
shared.chans[fname] = make(chan *fsnotify.Event)
|
2015-08-06 05:03:38 +08:00
|
|
|
shared.done[fname] = make(chan bool)
|
2013-11-13 12:15:27 +08:00
|
|
|
}
|
2015-09-11 07:47:13 +08:00
|
|
|
return shared.watcher.Add(fname)
|
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.
|
|
|
|
func (shared *InotifyTracker) removeWatch(fname string) {
|
2015-07-31 03:06:40 +08:00
|
|
|
shared.mux.Lock()
|
|
|
|
defer shared.mux.Unlock()
|
|
|
|
|
2015-08-05 08:42:45 +08:00
|
|
|
if ch := shared.chans[fname]; ch != nil {
|
2015-09-11 07:47:13 +08:00
|
|
|
shared.watcher.Remove(fname)
|
2015-08-05 08:42:45 +08:00
|
|
|
|
|
|
|
delete(shared.chans, fname)
|
|
|
|
close(ch)
|
|
|
|
}
|
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:47:13 +08:00
|
|
|
func (shared *InotifyTracker) sendEvent(event *fsnotify.Event) {
|
2015-08-06 05:03:38 +08:00
|
|
|
shared.mux.Lock()
|
|
|
|
ch := shared.chans[event.Name]
|
|
|
|
done := shared.done[event.Name]
|
|
|
|
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-09-11 07:47:13 +08:00
|
|
|
shared.error <- shared.addWatch(winfo.fname)
|
2015-08-05 08:42:45 +08:00
|
|
|
|
|
|
|
case fname := <-shared.remove:
|
|
|
|
shared.removeWatch(fname)
|
|
|
|
|
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:47:13 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|