2013-05-29 07:34:36 +08:00
|
|
|
// Copyright (c) 2013 ActiveState Software Inc. All rights reserved.
|
|
|
|
|
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/howeyc/fsnotify"
|
2013-09-23 15:03:51 +08:00
|
|
|
"launchpad.net/tomb"
|
2013-05-29 07:34:36 +08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
)
|
|
|
|
|
|
|
|
// InotifyFileWatcher uses inotify to monitor file changes.
|
|
|
|
type InotifyFileWatcher struct {
|
|
|
|
Filename string
|
|
|
|
Size int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewInotifyFileWatcher(filename string) *InotifyFileWatcher {
|
|
|
|
fw := &InotifyFileWatcher{filename, 0}
|
|
|
|
return fw
|
|
|
|
}
|
|
|
|
|
2013-09-23 15:03:51 +08:00
|
|
|
func (fw *InotifyFileWatcher) BlockUntilExists(t *tomb.Tomb) error {
|
2013-05-29 07:34:36 +08:00
|
|
|
w, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
dirname := filepath.Dir(fw.Filename)
|
|
|
|
|
|
|
|
// Watch for new files to be created in the parent directory.
|
|
|
|
err = w.WatchFlags(dirname, fsnotify.FSN_CREATE)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer w.RemoveWatch(filepath.Dir(fw.Filename))
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
2013-05-29 10:33:52 +08:00
|
|
|
select {
|
|
|
|
case evt := <-w.Event:
|
|
|
|
if evt.Name == fw.Filename {
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-09-23 15:03:51 +08:00
|
|
|
func (fw *InotifyFileWatcher) ChangeEvents(t *tomb.Tomb, fi os.FileInfo) *FileChanges {
|
2013-05-30 05:32:59 +08:00
|
|
|
changes := NewFileChanges()
|
2013-09-23 15:03:51 +08:00
|
|
|
|
2013-05-29 07:34:36 +08:00
|
|
|
w, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
err = w.Watch(fw.Filename)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
fw.Size = fi.Size()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer w.Close()
|
|
|
|
defer w.RemoveWatch(fw.Filename)
|
2013-05-30 05:32:59 +08:00
|
|
|
defer changes.Close()
|
2013-05-29 07:34:36 +08:00
|
|
|
|
|
|
|
for {
|
|
|
|
prevSize := fw.Size
|
|
|
|
|
2013-05-30 02:35:27 +08:00
|
|
|
var evt *fsnotify.FileEvent
|
|
|
|
|
|
|
|
select {
|
|
|
|
case evt = <-w.Event:
|
|
|
|
case <-t.Dying():
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-05-29 07:34:36 +08:00
|
|
|
switch {
|
|
|
|
case evt.IsDelete():
|
|
|
|
fallthrough
|
|
|
|
|
|
|
|
case evt.IsRename():
|
2013-05-30 05:32:59 +08:00
|
|
|
changes.NotifyDeleted()
|
2013-05-29 07:34:36 +08:00
|
|
|
return
|
|
|
|
|
|
|
|
case evt.IsModify():
|
|
|
|
fi, err := os.Stat(fw.Filename)
|
|
|
|
if err != nil {
|
|
|
|
// XXX: no panic here
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2013-05-30 05:32:59 +08:00
|
|
|
return changes
|
2013-05-29 07:34:36 +08:00
|
|
|
}
|