2021-11-08 08:52:39 +00:00
|
|
|
//go:build linux
|
|
|
|
// +build linux
|
2019-08-19 15:28:24 +08:00
|
|
|
|
|
|
|
package file
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/fsnotify/fsnotify"
|
2024-06-04 21:40:43 +01:00
|
|
|
"go-micro.dev/v5/config/source"
|
2019-08-19 15:28:24 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type watcher struct {
|
|
|
|
f *file
|
|
|
|
|
2023-04-26 02:19:52 +02:00
|
|
|
fw *fsnotify.Watcher
|
2019-08-19 15:28:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func newWatcher(f *file) (source.Watcher, error) {
|
|
|
|
fw, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
fw.Add(f.path)
|
|
|
|
|
|
|
|
return &watcher{
|
2023-04-26 02:19:52 +02:00
|
|
|
f: f,
|
|
|
|
fw: fw,
|
2019-08-19 15:28:24 +08:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) Next() (*source.ChangeSet, error) {
|
|
|
|
// try get the event
|
|
|
|
select {
|
2023-04-26 02:19:52 +02:00
|
|
|
case event, ok := <-w.fw.Events:
|
|
|
|
// check if channel was closed (i.e. Watcher.Close() was called).
|
|
|
|
if !ok {
|
|
|
|
return nil, source.ErrWatcherStopped
|
|
|
|
}
|
|
|
|
|
|
|
|
if event.Has(fsnotify.Rename) {
|
2019-08-19 15:28:24 +08:00
|
|
|
// check existence of file, and add watch again
|
|
|
|
_, err := os.Stat(event.Name)
|
|
|
|
if err == nil || os.IsExist(err) {
|
|
|
|
w.fw.Add(event.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := w.f.Read()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// add path again for the event bug of fsnotify
|
|
|
|
w.fw.Add(w.f.path)
|
|
|
|
|
|
|
|
return c, nil
|
2023-04-26 02:19:52 +02:00
|
|
|
case err, ok := <-w.fw.Errors:
|
|
|
|
// check if channel was closed (i.e. Watcher.Close() was called).
|
|
|
|
if !ok {
|
|
|
|
return nil, source.ErrWatcherStopped
|
|
|
|
}
|
|
|
|
|
2019-08-19 15:28:24 +08:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *watcher) Stop() error {
|
|
|
|
return w.fw.Close()
|
|
|
|
}
|