2021-10-14 17:39:21 +01:00
|
|
|
package aghos
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"path/filepath"
|
|
|
|
|
2024-04-03 11:44:51 +01:00
|
|
|
"github.com/AdguardTeam/golibs/container"
|
2021-10-14 17:39:21 +01:00
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
|
|
"github.com/AdguardTeam/golibs/log"
|
2024-02-13 10:19:22 +00:00
|
|
|
"github.com/AdguardTeam/golibs/osutil"
|
2021-10-14 17:39:21 +01:00
|
|
|
"github.com/fsnotify/fsnotify"
|
|
|
|
)
|
|
|
|
|
|
|
|
// event is a convenient alias for an empty struct to signal that watching
|
|
|
|
// event happened.
|
|
|
|
type event = struct{}
|
|
|
|
|
|
|
|
// FSWatcher tracks all the fyle system events and notifies about those.
|
|
|
|
//
|
|
|
|
// TODO(e.burkov, a.garipov): Move into another package like aghfs.
|
2024-02-13 10:19:22 +00:00
|
|
|
//
|
|
|
|
// TODO(e.burkov): Add tests.
|
2021-10-14 17:39:21 +01:00
|
|
|
type FSWatcher interface {
|
2024-02-13 10:19:22 +00:00
|
|
|
// Start starts watching the added files.
|
|
|
|
Start() (err error)
|
|
|
|
|
|
|
|
// Close stops watching the files and closes an update channel.
|
2021-10-14 17:39:21 +01:00
|
|
|
io.Closer
|
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
// Events returns the channel to notify about the file system events.
|
2021-10-14 17:39:21 +01:00
|
|
|
Events() (e <-chan event)
|
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
// Add starts tracking the file. It returns an error if the file can't be
|
|
|
|
// tracked. It must not be called after Start.
|
2021-10-14 17:39:21 +01:00
|
|
|
Add(name string) (err error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// osWatcher tracks the file system provided by the OS.
|
|
|
|
type osWatcher struct {
|
2024-02-13 10:19:22 +00:00
|
|
|
// watcher is the actual notifier that is handled by osWatcher.
|
|
|
|
watcher *fsnotify.Watcher
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
// events is the channel to notify.
|
|
|
|
events chan event
|
2024-02-13 10:19:22 +00:00
|
|
|
|
|
|
|
// files is the set of tracked files.
|
2024-04-03 11:44:51 +01:00
|
|
|
files *container.MapSet[string]
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
// osWatcherPref is a prefix for logging and wrapping errors in osWathcer's
|
|
|
|
// methods.
|
|
|
|
const osWatcherPref = "os watcher"
|
2021-10-14 17:39:21 +01:00
|
|
|
|
|
|
|
// NewOSWritesWatcher creates FSWatcher that tracks the real file system of the
|
|
|
|
// OS and notifies only about writing events.
|
|
|
|
func NewOSWritesWatcher() (w FSWatcher, err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "%s: %w", osWatcherPref) }()
|
|
|
|
|
|
|
|
var watcher *fsnotify.Watcher
|
|
|
|
watcher, err = fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("creating watcher: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
return &osWatcher{
|
|
|
|
watcher: watcher,
|
|
|
|
events: make(chan event, 1),
|
2024-04-03 11:44:51 +01:00
|
|
|
files: container.NewMapSet[string](),
|
2024-02-13 10:19:22 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
// type check
|
|
|
|
var _ FSWatcher = (*osWatcher)(nil)
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
// Start implements the FSWatcher interface for *osWatcher.
|
|
|
|
func (w *osWatcher) Start() (err error) {
|
|
|
|
go w.handleErrors()
|
|
|
|
go w.handleEvents()
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
// Close implements the FSWatcher interface for *osWatcher.
|
|
|
|
func (w *osWatcher) Close() (err error) {
|
|
|
|
return w.watcher.Close()
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Events implements the FSWatcher interface for *osWatcher.
|
|
|
|
func (w *osWatcher) Events() (e <-chan event) {
|
|
|
|
return w.events
|
|
|
|
}
|
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
// Add implements the [FSWatcher] interface for *osWatcher.
|
2021-11-17 14:21:10 +00:00
|
|
|
//
|
|
|
|
// TODO(e.burkov): Make it accept non-existing files to detect it's creating.
|
2021-10-14 17:39:21 +01:00
|
|
|
func (w *osWatcher) Add(name string) (err error) {
|
|
|
|
defer func() { err = errors.Annotate(err, "%s: %w", osWatcherPref) }()
|
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
fi, err := fs.Stat(osutil.RootDirFS(), name)
|
|
|
|
if err != nil {
|
2021-10-14 17:39:21 +01:00
|
|
|
return fmt.Errorf("checking file %q: %w", name, err)
|
|
|
|
}
|
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
name = filepath.Join("/", name)
|
|
|
|
w.files.Add(name)
|
2021-10-14 17:39:21 +01:00
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
// Watch the directory and filter the events by the file name, since the
|
|
|
|
// common recomendation to the fsnotify package is to watch the directory
|
|
|
|
// instead of the file itself.
|
|
|
|
//
|
|
|
|
// See https://pkg.go.dev/github.com/fsnotify/fsnotify@v1.7.0#readme-watching-a-file-doesn-t-work-well.
|
|
|
|
if !fi.IsDir() {
|
|
|
|
name = filepath.Dir(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.watcher.Add(name)
|
2021-10-14 17:39:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// handleEvents notifies about the received file system's event if needed. It
|
2024-02-12 15:45:51 +00:00
|
|
|
// is intended to be used as a goroutine.
|
2021-10-14 17:39:21 +01:00
|
|
|
func (w *osWatcher) handleEvents() {
|
|
|
|
defer log.OnPanic(fmt.Sprintf("%s: handling events", osWatcherPref))
|
|
|
|
|
|
|
|
defer close(w.events)
|
|
|
|
|
2024-02-13 10:19:22 +00:00
|
|
|
ch := w.watcher.Events
|
2021-10-14 17:39:21 +01:00
|
|
|
for e := range ch {
|
2024-02-13 10:19:22 +00:00
|
|
|
if e.Op&fsnotify.Write == 0 || !w.files.Has(e.Name) {
|
2021-10-14 17:39:21 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the following events assuming that sometimes the same event
|
2023-09-06 17:42:34 +01:00
|
|
|
// occurs several times.
|
2021-10-14 17:39:21 +01:00
|
|
|
for ok := true; ok; {
|
|
|
|
select {
|
|
|
|
case _, ok = <-ch:
|
|
|
|
// Go on.
|
|
|
|
default:
|
|
|
|
ok = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case w.events <- event{}:
|
|
|
|
// Go on.
|
|
|
|
default:
|
|
|
|
log.Debug("%s: events buffer is full", osWatcherPref)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-02-13 10:19:22 +00:00
|
|
|
|
|
|
|
// handleErrors handles accompanying errors. It used to be called in a separate
|
|
|
|
// goroutine.
|
|
|
|
func (w *osWatcher) handleErrors() {
|
|
|
|
defer log.OnPanic(fmt.Sprintf("%s: handling errors", osWatcherPref))
|
|
|
|
|
|
|
|
for err := range w.watcher.Errors {
|
|
|
|
log.Error("%s: %s", osWatcherPref, err)
|
|
|
|
}
|
|
|
|
}
|
2024-06-17 17:34:46 +01:00
|
|
|
|
|
|
|
// EmptyFSWatcher is a no-op implementation of the [FSWatcher] interface. It
|
|
|
|
// may be used on systems not supporting filesystem events.
|
|
|
|
type EmptyFSWatcher struct{}
|
|
|
|
|
|
|
|
// type check
|
|
|
|
var _ FSWatcher = EmptyFSWatcher{}
|
|
|
|
|
|
|
|
// Start implements the [FSWatcher] interface for EmptyFSWatcher. It always
|
|
|
|
// returns nil error.
|
|
|
|
func (EmptyFSWatcher) Start() (err error) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements the [FSWatcher] interface for EmptyFSWatcher. It always
|
|
|
|
// returns nil error.
|
|
|
|
func (EmptyFSWatcher) Close() (err error) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Events implements the [FSWatcher] interface for EmptyFSWatcher. It always
|
|
|
|
// returns nil channel.
|
|
|
|
func (EmptyFSWatcher) Events() (e <-chan event) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add implements the [FSWatcher] interface for EmptyFSWatcher. It always
|
|
|
|
// returns nil error.
|
|
|
|
func (EmptyFSWatcher) Add(_ string) (err error) {
|
|
|
|
return nil
|
|
|
|
}
|