|
| 1 | +package file |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "io/fs" |
| 8 | + "os" |
| 9 | + "sync" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/fsnotify/fsnotify" |
| 13 | + "github.com/open-feature/flagd/core/pkg/logger" |
| 14 | +) |
| 15 | + |
| 16 | +// Implements file.Watcher using a timer and os.FileInfo |
| 17 | +type fileInfoWatcher struct { |
| 18 | + // Event Chan |
| 19 | + evChan chan fsnotify.Event |
| 20 | + // Errors Chan |
| 21 | + erChan chan error |
| 22 | + // logger |
| 23 | + logger *logger.Logger |
| 24 | + // Func to wrap os.Stat (injection point for test helpers) |
| 25 | + statFunc func(string) (fs.FileInfo, error) |
| 26 | + // thread-safe interface to underlying files we are watching |
| 27 | + mu sync.RWMutex |
| 28 | + watches map[string]fs.FileInfo // filename -> info |
| 29 | +} |
| 30 | + |
| 31 | +// NewFsNotifyWatcher returns a new fsNotifyWatcher |
| 32 | +func NewFileInfoWatcher(ctx context.Context, logger *logger.Logger) Watcher { |
| 33 | + fiw := &fileInfoWatcher{ |
| 34 | + evChan: make(chan fsnotify.Event, 32), |
| 35 | + erChan: make(chan error, 32), |
| 36 | + statFunc: getFileInfo, |
| 37 | + logger: logger, |
| 38 | + watches: make(map[string]fs.FileInfo), |
| 39 | + } |
| 40 | + fiw.run(ctx, (1 * time.Second)) |
| 41 | + return fiw |
| 42 | +} |
| 43 | + |
| 44 | +// fileInfoWatcher explicitly implements file.Watcher |
| 45 | +var _ Watcher = &fileInfoWatcher{} |
| 46 | + |
| 47 | +// Close calls close on the underlying fsnotify.Watcher |
| 48 | +func (f *fileInfoWatcher) Close() error { |
| 49 | + // close all channels and exit |
| 50 | + close(f.evChan) |
| 51 | + close(f.erChan) |
| 52 | + return nil |
| 53 | +} |
| 54 | + |
| 55 | +// Add calls Add on the underlying fsnotify.Watcher |
| 56 | +func (f *fileInfoWatcher) Add(name string) error { |
| 57 | + f.mu.Lock() |
| 58 | + defer f.mu.Unlock() |
| 59 | + |
| 60 | + // exit early if name already exists |
| 61 | + if _, ok := f.watches[name]; ok { |
| 62 | + return nil |
| 63 | + } |
| 64 | + |
| 65 | + info, err := f.statFunc(name) |
| 66 | + if err != nil { |
| 67 | + return err |
| 68 | + } |
| 69 | + |
| 70 | + f.watches[name] = info |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// Remove calls Remove on the underlying fsnotify.Watcher |
| 76 | +func (f *fileInfoWatcher) Remove(name string) error { |
| 77 | + f.mu.Lock() |
| 78 | + defer f.mu.Unlock() |
| 79 | + |
| 80 | + // no need to exit early, deleting non-existent key is a no-op |
| 81 | + delete(f.watches, name) |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +// Watchlist calls watchlist on the underlying fsnotify.Watcher |
| 87 | +func (f *fileInfoWatcher) WatchList() []string { |
| 88 | + f.mu.RLock() |
| 89 | + defer f.mu.RUnlock() |
| 90 | + out := []string{} |
| 91 | + for name := range f.watches { |
| 92 | + n := name |
| 93 | + out = append(out, n) |
| 94 | + } |
| 95 | + return out |
| 96 | +} |
| 97 | + |
| 98 | +// Events returns the underlying watcher's Events chan |
| 99 | +func (f *fileInfoWatcher) Events() chan fsnotify.Event { |
| 100 | + return f.evChan |
| 101 | +} |
| 102 | + |
| 103 | +// Errors returns the underlying watcher's Errors chan |
| 104 | +func (f *fileInfoWatcher) Errors() chan error { |
| 105 | + return f.erChan |
| 106 | +} |
| 107 | + |
| 108 | +// run is a blocking function that starts the filewatcher's timer thread |
| 109 | +func (f *fileInfoWatcher) run(ctx context.Context, s time.Duration) { |
| 110 | + // timer thread |
| 111 | + go func() { |
| 112 | + // execute update on the configured interval of time |
| 113 | + ticker := time.NewTicker(s) |
| 114 | + defer ticker.Stop() |
| 115 | + |
| 116 | + for { |
| 117 | + select { |
| 118 | + case <-ctx.Done(): |
| 119 | + return |
| 120 | + case <-ticker.C: |
| 121 | + if err := f.update(); err != nil { |
| 122 | + f.erChan <- err |
| 123 | + return |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + }() |
| 128 | +} |
| 129 | + |
| 130 | +func (f *fileInfoWatcher) update() error { |
| 131 | + f.mu.Lock() |
| 132 | + defer f.mu.Unlock() |
| 133 | + |
| 134 | + for path, info := range f.watches { |
| 135 | + newInfo, err := f.statFunc(path) |
| 136 | + if err != nil { |
| 137 | + // if the file isn't there, it must have been removed |
| 138 | + // fire off a remove event and remove it from the watches |
| 139 | + if errors.Is(err, os.ErrNotExist) { |
| 140 | + f.evChan <- fsnotify.Event{ |
| 141 | + Name: path, |
| 142 | + Op: fsnotify.Remove, |
| 143 | + } |
| 144 | + delete(f.watches, path) |
| 145 | + continue |
| 146 | + } |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + // if the new stat doesn't match the old stat, figure out what changed |
| 151 | + if info != newInfo { |
| 152 | + event := f.generateEvent(path, newInfo) |
| 153 | + if event != nil { |
| 154 | + f.evChan <- *event |
| 155 | + } |
| 156 | + f.watches[path] = newInfo |
| 157 | + } |
| 158 | + } |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +// generateEvent figures out what changed and generates an fsnotify.Event for it. (if we care) |
| 163 | +// file removal are handled above in the update() method |
| 164 | +func (f *fileInfoWatcher) generateEvent(path string, newInfo fs.FileInfo) *fsnotify.Event { |
| 165 | + info := f.watches[path] |
| 166 | + switch { |
| 167 | + // new mod time is more recent than old mod time, generate a write event |
| 168 | + case newInfo.ModTime().After(info.ModTime()): |
| 169 | + return &fsnotify.Event{ |
| 170 | + Name: path, |
| 171 | + Op: fsnotify.Write, |
| 172 | + } |
| 173 | + // the file modes changed, generate a chmod event |
| 174 | + case info.Mode() != newInfo.Mode(): |
| 175 | + return &fsnotify.Event{ |
| 176 | + Name: path, |
| 177 | + Op: fsnotify.Chmod, |
| 178 | + } |
| 179 | + // nothing changed that we care about |
| 180 | + default: |
| 181 | + return nil |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +// getFileInfo returns the fs.FileInfo for the given path |
| 186 | +func getFileInfo(path string) (fs.FileInfo, error) { |
| 187 | + f, err := os.Open(path) |
| 188 | + if err != nil { |
| 189 | + return nil, fmt.Errorf("error from os.Open(%s): %w", path, err) |
| 190 | + } |
| 191 | + |
| 192 | + info, err := f.Stat() |
| 193 | + if err != nil { |
| 194 | + return info, fmt.Errorf("error from fs.Stat(%s): %w", path, err) |
| 195 | + } |
| 196 | + |
| 197 | + if err := f.Close(); err != nil { |
| 198 | + return info, fmt.Errorf("err from fs.Close(%s): %w", path, err) |
| 199 | + } |
| 200 | + |
| 201 | + return info, nil |
| 202 | +} |
0 commit comments