mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-11-17 02:48:28 -07:00
9bac4b3db2
Merge in DNS/adguard-home from 3845-hosts-fatality to master Updates #3845. Squashed commit of the following: commit 1447efcc4066e0226feaebde01fcc632cb7b7432 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Nov 17 17:14:35 2021 +0300 home: imp readability commit e934499072e983e1111b6c976eb93e1d6017981b Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Nov 17 13:35:10 2021 +0300 aghnet: imp more commit ed9995ee52bd9ec3fa130f3f56989619184a6669 Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Wed Nov 17 13:05:56 2021 +0300 all: imp docs, code commit 7b0718a1a4a58a4fd5f1ba24c33792b0610c334f Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Nov 16 20:32:24 2021 +0300 all: reduce hosts container fatality
134 lines
3.1 KiB
Go
134 lines
3.1 KiB
Go
package aghos
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"path/filepath"
|
|
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
"github.com/AdguardTeam/golibs/log"
|
|
"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.
|
|
type FSWatcher interface {
|
|
io.Closer
|
|
|
|
// Events should return a read-only channel which notifies about events.
|
|
Events() (e <-chan event)
|
|
|
|
// Add should check if the file named name is accessible and starts tracking
|
|
// it.
|
|
Add(name string) (err error)
|
|
}
|
|
|
|
// osWatcher tracks the file system provided by the OS.
|
|
type osWatcher struct {
|
|
// w is the actual notifier that is handled by osWatcher.
|
|
w *fsnotify.Watcher
|
|
|
|
// events is the channel to notify.
|
|
events chan event
|
|
}
|
|
|
|
const (
|
|
// osWatcherPref is a prefix for logging and wrapping errors in osWathcer's
|
|
// methods.
|
|
osWatcherPref = "os watcher"
|
|
)
|
|
|
|
// 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)
|
|
}
|
|
|
|
fsw := &osWatcher{
|
|
w: watcher,
|
|
events: make(chan event, 1),
|
|
}
|
|
|
|
go fsw.handleErrors()
|
|
go fsw.handleEvents()
|
|
|
|
return fsw, nil
|
|
}
|
|
|
|
// 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.w.Errors {
|
|
log.Error("%s: %s", osWatcherPref, err)
|
|
}
|
|
}
|
|
|
|
// Events implements the FSWatcher interface for *osWatcher.
|
|
func (w *osWatcher) Events() (e <-chan event) {
|
|
return w.events
|
|
}
|
|
|
|
// Add implements the FSWatcher interface for *osWatcher.
|
|
//
|
|
// TODO(e.burkov): Make it accept non-existing files to detect it's creating.
|
|
func (w *osWatcher) Add(name string) (err error) {
|
|
defer func() { err = errors.Annotate(err, "%s: %w", osWatcherPref) }()
|
|
|
|
if _, err = fs.Stat(RootDirFS(), name); err != nil {
|
|
return fmt.Errorf("checking file %q: %w", name, err)
|
|
}
|
|
|
|
return w.w.Add(filepath.Join("/", name))
|
|
}
|
|
|
|
// Close implements the FSWatcher interface for *osWatcher.
|
|
func (w *osWatcher) Close() (err error) {
|
|
return w.w.Close()
|
|
}
|
|
|
|
// handleEvents notifies about the received file system's event if needed. It
|
|
// used to be called in a separate goroutine.
|
|
func (w *osWatcher) handleEvents() {
|
|
defer log.OnPanic(fmt.Sprintf("%s: handling events", osWatcherPref))
|
|
|
|
defer close(w.events)
|
|
|
|
ch := w.w.Events
|
|
for e := range ch {
|
|
if e.Op&fsnotify.Write == 0 {
|
|
continue
|
|
}
|
|
|
|
// Skip the following events assuming that sometimes the same event
|
|
// occurrs several times.
|
|
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)
|
|
}
|
|
}
|
|
}
|