From 18a81f043ca4e3471fd5992eff2ea624531bc5e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6ran=20Karl?= <3951388+JoeKar@users.noreply.github.com> Date: Thu, 30 May 2024 21:14:04 +0200 Subject: [PATCH] util: Generalize the file mode of 0666 with `util.FileMode` --- cmd/micro/debug.go | 2 +- internal/action/bindings.go | 7 ++++--- internal/buffer/save.go | 2 +- internal/config/settings.go | 4 ++-- internal/util/util.go | 3 +++ tools/info-plist.go | 2 +- 6 files changed, 12 insertions(+), 8 deletions(-) diff --git a/cmd/micro/debug.go b/cmd/micro/debug.go index 5dc708ab..1504a03d 100644 --- a/cmd/micro/debug.go +++ b/cmd/micro/debug.go @@ -18,7 +18,7 @@ func (NullWriter) Write(data []byte) (n int, err error) { // InitLog sets up the debug log system for micro if it has been enabled by compile-time variables func InitLog() { if util.Debug == "ON" { - f, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666) + f, err := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_TRUNC, util.FileMode) if err != nil { log.Fatalf("error opening file: %v", err) } diff --git a/internal/action/bindings.go b/internal/action/bindings.go index 1eb2a6f0..02804bb6 100644 --- a/internal/action/bindings.go +++ b/internal/action/bindings.go @@ -15,6 +15,7 @@ import ( "github.com/micro-editor/json5" "github.com/zyedidia/micro/v2/internal/config" "github.com/zyedidia/micro/v2/internal/screen" + "github.com/zyedidia/micro/v2/internal/util" "github.com/micro-editor/tcell/v2" ) @@ -26,7 +27,7 @@ var Binder = map[string]func(e Event, action string){ func createBindingsIfNotExist(fname string) { if _, e := os.Stat(fname); errors.Is(e, fs.ErrNotExist) { - ioutil.WriteFile(fname, []byte("{}"), 0644) + ioutil.WriteFile(fname, []byte("{}"), util.FileMode) } } @@ -305,7 +306,7 @@ func TryBindKey(k, v string, overwrite bool) (bool, error) { BindKey(k, v, Binder["buffer"]) txt, _ := json.MarshalIndent(parsed, "", " ") - return true, ioutil.WriteFile(filename, append(txt, '\n'), 0644) + return true, ioutil.WriteFile(filename, append(txt, '\n'), util.FileMode) } return false, e } @@ -355,7 +356,7 @@ func UnbindKey(k string) error { } txt, _ := json.MarshalIndent(parsed, "", " ") - return ioutil.WriteFile(filename, append(txt, '\n'), 0644) + return ioutil.WriteFile(filename, append(txt, '\n'), util.FileMode) } return e } diff --git a/internal/buffer/save.go b/internal/buffer/save.go index e927ae14..29143e4d 100644 --- a/internal/buffer/save.go +++ b/internal/buffer/save.go @@ -57,7 +57,7 @@ func overwriteFile(name string, enc encoding.Encoding, fn func(io.Writer) error, return } - } else if writeCloser, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666); err != nil { + } else if writeCloser, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, util.FileMode); err != nil { return } diff --git a/internal/config/settings.go b/internal/config/settings.go index 7630915f..cebf1c92 100644 --- a/internal/config/settings.go +++ b/internal/config/settings.go @@ -356,7 +356,7 @@ func WriteSettings(filename string) error { } txt, _ := json.MarshalIndent(parsedSettings, "", " ") - err = ioutil.WriteFile(filename, append(txt, '\n'), 0644) + err = ioutil.WriteFile(filename, append(txt, '\n'), util.FileMode) } return err } @@ -378,7 +378,7 @@ func OverwriteSettings(filename string) error { } txt, _ := json.MarshalIndent(settings, "", " ") - err = ioutil.WriteFile(filename, append(txt, '\n'), 0644) + err = ioutil.WriteFile(filename, append(txt, '\n'), util.FileMode) } return err } diff --git a/internal/util/util.go b/internal/util/util.go index 6f8f6325..34f49652 100644 --- a/internal/util/util.go +++ b/internal/util/util.go @@ -46,6 +46,9 @@ var ( Sigterm chan os.Signal ) +// To be used for file writes before umask is applied +const FileMode os.FileMode = 0666 + func init() { var err error SemVersion, err = semver.Make(Version) diff --git a/tools/info-plist.go b/tools/info-plist.go index 1707d6de..33e55291 100644 --- a/tools/info-plist.go +++ b/tools/info-plist.go @@ -37,7 +37,7 @@ func main() { ` - err := os.WriteFile("/tmp/micro-info.plist", []byte(rawInfoPlist), 0644) + err := os.WriteFile("/tmp/micro-info.plist", []byte(rawInfoPlist), 0666) if err != nil { panic(err) }