@@ -8,7 +8,6 @@ package file
88import (
99 "context"
1010 "errors"
11- "fmt"
1211 "log/slog"
1312 "os"
1413 "path/filepath"
@@ -34,13 +33,12 @@ type FileUpdateMessage struct {
3433}
3534
3635type FileWatcherService struct {
37- agentConfig * config.Config
38- watcher * fsnotify.Watcher
39- directoriesBeingWatched * sync.Map
40- filesChanged * atomic.Bool
41- enabled * atomic.Bool
42- directoriesThatDontExistYet * sync.Map
43- mu sync.Mutex
36+ agentConfig * config.Config
37+ watcher * fsnotify.Watcher
38+ filesChanged * atomic.Bool
39+ enabled * atomic.Bool
40+ directoriesToWatch map [string ]struct {}
41+ mu sync.Mutex
4442}
4543
4644func NewFileWatcherService (agentConfig * config.Config ) * FileWatcherService {
@@ -51,11 +49,10 @@ func NewFileWatcherService(agentConfig *config.Config) *FileWatcherService {
5149 filesChanged .Store (false )
5250
5351 return & FileWatcherService {
54- agentConfig : agentConfig ,
55- directoriesBeingWatched : & sync.Map {},
56- directoriesThatDontExistYet : & sync.Map {},
57- enabled : enabled ,
58- filesChanged : filesChanged ,
52+ agentConfig : agentConfig ,
53+ directoriesToWatch : make (map [string ]struct {}),
54+ enabled : enabled ,
55+ filesChanged : filesChanged ,
5956 }
6057}
6158
@@ -114,25 +111,21 @@ func (fws *FileWatcherService) Update(ctx context.Context, nginxConfigContext *m
114111 directoriesToWatch [filepath .Dir (file )] = struct {}{}
115112 }
116113
117- // If watcher does not exist yet add directories to directoriesThatDontExistYet so that watchers can be created
118- // at the next file watcher monitoring period
119- if fws .watcher == nil {
120- for dir := range directoriesToWatch {
121- fws .directoriesThatDontExistYet .Store (dir , struct {}{})
122- }
123- } else {
114+ fws .directoriesToWatch = directoriesToWatch
115+
116+ if fws .watcher != nil {
124117 slog .InfoContext (ctx , "Updating file watcher" , "allowed" , fws .agentConfig .AllowedDirectories )
125118
126119 // Start watching new directories
127- fws .addWatchers (ctx , directoriesToWatch )
120+ fws .addWatchers (ctx )
128121
129122 // Check if directories no longer need to be watched
130- fws .removeWatchers (ctx , directoriesToWatch )
123+ fws .removeWatchers (ctx )
131124 }
132125}
133126
134- func (fws * FileWatcherService ) addWatchers (ctx context.Context , directoriesToWatch map [ string ] struct {} ) {
135- for directory := range directoriesToWatch {
127+ func (fws * FileWatcherService ) addWatchers (ctx context.Context ) {
128+ for directory := range fws . directoriesToWatch {
136129 if ! fws .agentConfig .IsDirectoryAllowed (directory ) {
137130 slog .WarnContext (
138131 ctx ,
@@ -142,45 +135,26 @@ func (fws *FileWatcherService) addWatchers(ctx context.Context, directoriesToWat
142135
143136 continue
144137 }
145- if fws .addWatcher (ctx , directory ) {
146- fws .directoriesThatDontExistYet .Delete (directory )
147- } else {
148- fws .directoriesThatDontExistYet .Store (directory , struct {}{})
149- }
138+
139+ fws .addWatcher (ctx , directory )
150140 }
151141}
152142
153- func (fws * FileWatcherService ) removeWatchers (ctx context.Context , directoriesToWatch map [string ]struct {}) {
154- fws .directoriesBeingWatched .Range (func (key , value interface {}) bool {
155- directory , ok := key .(string )
156-
157- if _ , exists := directoriesToWatch [directory ]; ! exists && ok {
158- fws .removeWatcher (ctx , directory )
159- fws .directoriesBeingWatched .Delete (directory )
143+ func (fws * FileWatcherService ) removeWatchers (ctx context.Context ) {
144+ for _ , directoryBeingWatched := range fws .watcher .WatchList () {
145+ if _ , ok := fws .directoriesToWatch [directoryBeingWatched ]; ! ok {
146+ fws .removeWatcher (ctx , directoryBeingWatched )
160147 }
161-
162- return true
163- })
148+ }
164149}
165150
166151func (fws * FileWatcherService ) handleEvent (ctx context.Context , event fsnotify.Event ) {
167- fmt .Printf ("Processing FSNotify event %v \n " , event )
168-
169152 if fws .enabled .Load () {
170153 if fws .isEventSkippable (event ) {
171154 return
172155 }
173156
174157 slog .DebugContext (ctx , "Processing FSNotify event" , "event" , event )
175-
176- if event .Has (fsnotify .Remove ) || event .Has (fsnotify .Rename ) {
177- if _ , ok := fws .directoriesBeingWatched .Load (event .Name ); ok {
178- fws .directoriesBeingWatched .Delete (event .Name )
179- }
180-
181- fws .directoriesThatDontExistYet .Store (event .Name , struct {}{})
182- }
183-
184158 fws .filesChanged .Store (true )
185159 }
186160}
@@ -201,18 +175,11 @@ func (fws *FileWatcherService) checkForUpdates(ctx context.Context, ch chan<- Fi
201175 fws .watcher = watcher
202176 }
203177
204- fws .directoriesThatDontExistYet .Range (func (key , value interface {}) bool {
205- directory , ok := key .(string )
206- if ! ok {
207- return true
208- }
209-
210- if fws .addWatcher (ctx , directory ) {
211- fws .directoriesThatDontExistYet .Delete (directory )
212- }
178+ // Start watching new directories
179+ fws .addWatchers (ctx )
213180
214- return true
215- } )
181+ // Check if directories no longer need to be watched
182+ fws . removeWatchers ( ctx )
216183
217184 if fws .filesChanged .Load () {
218185 newCtx := context .WithValue (
@@ -227,39 +194,21 @@ func (fws *FileWatcherService) checkForUpdates(ctx context.Context, ch chan<- Fi
227194 }
228195}
229196
230- func (fws * FileWatcherService ) addWatcher (ctx context.Context , directory string ) bool {
197+ func (fws * FileWatcherService ) addWatcher (ctx context.Context , directory string ) {
231198 slog .DebugContext (ctx , "Checking if file watcher needs to be added" , "directory" , directory )
232199
233- if _ , ok := fws .directoriesBeingWatched .Load (directory ); ! ok {
234- if _ , err := os .Stat (directory ); errors .Is (err , os .ErrNotExist ) {
235- slog .DebugContext (
236- ctx , "Unable to watch directory that does not exist" ,
237- "directory" , directory , "error" , err ,
238- )
239-
240- return false
241- }
242-
243- slog .DebugContext (ctx , "Adding watcher" , "directory" , directory )
244-
245- if err := fws .watcher .Add (directory ); err != nil {
246- slog .ErrorContext (ctx , "Failed to add file watcher" , "directory" , directory , "error" , err )
247- removeError := fws .watcher .Remove (directory )
248- if removeError != nil {
249- slog .ErrorContext (
250- ctx ,
251- "Failed to remove file watcher" ,
252- "directory" , directory , "error" , removeError ,
253- )
254- }
255-
256- return false
257- }
200+ if _ , err := os .Stat (directory ); errors .Is (err , os .ErrNotExist ) {
201+ slog .DebugContext (
202+ ctx , "Unable to watch directory that does not exist" ,
203+ "directory" , directory , "error" , err ,
204+ )
258205 }
259206
260- fws . directoriesBeingWatched . Store ( directory , struct {}{} )
207+ slog . DebugContext ( ctx , "Adding watcher" , " directory" , directory )
261208
262- return true
209+ if err := fws .watcher .Add (directory ); err != nil {
210+ slog .ErrorContext (ctx , "Failed to add file watcher" , "directory" , directory , "error" , err )
211+ }
263212}
264213
265214func (fws * FileWatcherService ) removeWatcher (ctx context.Context , path string ) {
0 commit comments