From 4025d2fa9ae3e368439f388430ba9e7cc3a6c8f8 Mon Sep 17 00:00:00 2001 From: sawka Date: Tue, 30 Jul 2024 15:54:59 -0700 Subject: [PATCH] filter fs events for valid names. don't respond to chmod events --- pkg/wconfig/filewatcher.go | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/pkg/wconfig/filewatcher.go b/pkg/wconfig/filewatcher.go index fe9dcbb6..785664a3 100644 --- a/pkg/wconfig/filewatcher.go +++ b/pkg/wconfig/filewatcher.go @@ -10,6 +10,7 @@ import ( "log" "os" "path/filepath" + "regexp" "strings" "sync" @@ -245,7 +246,12 @@ func (w *Watcher) handleEvent(event fsnotify.Event) { defer w.mutex.Unlock() fileName := filepath.ToSlash(event.Name) - + if event.Op == fsnotify.Chmod { + return + } + if !isValidSubSettingsFileName(fileName) { + return + } if isInDirectory(fileName, termThemesDirAbsPath) { w.handleTermThemesEvent(event, fileName) } else if filepath.Base(fileName) == filepath.Base(settingsAbsPath) { @@ -253,6 +259,16 @@ func (w *Watcher) handleEvent(event fsnotify.Event) { } } +var validFileRe = regexp.MustCompile(`^[a-zA-Z0-9_@.-]+\.json$`) + +func isValidSubSettingsFileName(fileName string) bool { + if filepath.Ext(fileName) != ".json" { + return false + } + baseName := filepath.Base(fileName) + return validFileRe.MatchString(baseName) +} + func (w *Watcher) handleTermThemesEvent(event fsnotify.Event, fileName string) { settings, err := LoadFullSettings() if err != nil {