2022-09-05 03:06:08 -04:00
|
|
|
/*
|
|
|
|
Copyright (c) Edgeless Systems GmbH
|
|
|
|
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2022-05-23 05:36:54 -04:00
|
|
|
package watcher
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-09-21 07:47:57 -04:00
|
|
|
"github.com/edgelesssys/constellation/v2/internal/logger"
|
2022-05-23 05:36:54 -04:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2022-06-28 10:51:30 -04:00
|
|
|
"go.uber.org/zap"
|
2022-05-23 05:36:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// FileWatcher watches for changes to the file and calls the waiter's Update method.
|
|
|
|
type FileWatcher struct {
|
2022-06-28 10:51:30 -04:00
|
|
|
log *logger.Logger
|
2022-05-23 05:36:54 -04:00
|
|
|
updater updater
|
|
|
|
watcher eventWatcher
|
|
|
|
done chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new FileWatcher for the given validator.
|
2022-06-28 10:51:30 -04:00
|
|
|
func New(log *logger.Logger, updater updater) (*FileWatcher, error) {
|
2022-05-23 05:36:54 -04:00
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &FileWatcher{
|
2022-06-28 10:51:30 -04:00
|
|
|
log: log,
|
2022-05-23 05:36:54 -04:00
|
|
|
watcher: &fsnotifyWatcher{watcher},
|
|
|
|
updater: updater,
|
|
|
|
done: make(chan struct{}, 1),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the watcher.
|
|
|
|
// It should only be called once.
|
|
|
|
func (f *FileWatcher) Close() error {
|
|
|
|
err := f.watcher.Close()
|
|
|
|
<-f.done
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch starts watching the file at the given path.
|
|
|
|
// It will call the watcher's Update method when the file is modified.
|
|
|
|
func (f *FileWatcher) Watch(file string) error {
|
2022-06-28 10:51:30 -04:00
|
|
|
log := f.log.With("file", file)
|
2022-05-23 05:36:54 -04:00
|
|
|
defer func() { f.done <- struct{}{} }()
|
|
|
|
if err := f.watcher.Add(file); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event, ok := <-f.watcher.Events():
|
|
|
|
if !ok {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.Infof("Watcher closed")
|
2022-05-23 05:36:54 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// file changes may be indicated by either a WRITE, CHMOD, CREATE or RENAME event
|
|
|
|
if event.Op&(fsnotify.Write|fsnotify.Chmod|fsnotify.Create|fsnotify.Rename) != 0 {
|
|
|
|
if err := f.updater.Update(); err != nil {
|
2022-06-29 10:13:01 -04:00
|
|
|
log.With(zap.Error(err)).Errorf("Update failed")
|
2022-05-23 05:36:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if a file gets removed, e.g. by a rename event, we need to re-add the file to the watcher
|
2022-10-20 04:16:20 -04:00
|
|
|
if event.Has(fsnotify.Remove) {
|
2022-05-23 05:36:54 -04:00
|
|
|
if err := f.watcher.Add(event.Name); err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error(err)).Errorf("Failed to re-add file to watcher")
|
2022-05-23 05:36:54 -04:00
|
|
|
return fmt.Errorf("failed to re-add file %q to watcher: %w", event.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case err := <-f.watcher.Errors():
|
|
|
|
if err != nil {
|
2022-06-28 10:51:30 -04:00
|
|
|
log.With(zap.Error(err)).Errorf("Watching for measurements updates")
|
2022-05-23 05:36:54 -04:00
|
|
|
return fmt.Errorf("watching for measurements updates: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type updater interface {
|
|
|
|
Update() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type eventWatcher interface {
|
|
|
|
Add(string) error
|
|
|
|
Close() error
|
|
|
|
Events() <-chan fsnotify.Event
|
|
|
|
Errors() <-chan error
|
|
|
|
}
|
|
|
|
|
|
|
|
type fsnotifyWatcher struct {
|
|
|
|
watcher *fsnotify.Watcher
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fsnotifyWatcher) Add(file string) error {
|
|
|
|
return w.watcher.Add(file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fsnotifyWatcher) Close() error {
|
|
|
|
return w.watcher.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fsnotifyWatcher) Events() <-chan fsnotify.Event {
|
|
|
|
return w.watcher.Events
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *fsnotifyWatcher) Errors() <-chan error {
|
|
|
|
return w.watcher.Errors
|
|
|
|
}
|