diff --git a/internal/config/settings.go b/internal/config/settings.go index 717165b101..af22887f61 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "io/fs" "os" "path/filepath" "reflect" @@ -150,6 +151,13 @@ var ( VolatileSettings map[string]bool ) +func writeFile(name string, txt []byte) error { + if _, err := os.Stat(name); errors.Is(err, fs.ErrNotExist) { + return os.WriteFile(name, txt, util.FileMode) + } + return util.SafeWriteInternal(name, txt, false) +} + func init() { ModifiedSettings = make(map[string]bool) VolatileSettings = make(map[string]bool) @@ -341,7 +349,8 @@ func WriteSettings(filename string) error { } txt, _ := json.MarshalIndent(parsedSettings, "", " ") - err = os.WriteFile(filename, append(txt, '\n'), util.FileMode) + txt = append(txt, '\n') + err = writeFile(filename, txt) } return err } @@ -362,8 +371,9 @@ func OverwriteSettings(filename string) error { } } - txt, _ := json.MarshalIndent(settings, "", " ") - err = os.WriteFile(filename, append(txt, '\n'), util.FileMode) + txt, _ := json.MarshalIndent(parsedSettings, "", " ") + txt = append(txt, '\n') + err = writeFile(filename, txt) } return err }