From 5cb83c86d036ffe21ce916af7a2d0748db4d2fd1 Mon Sep 17 00:00:00 2001 From: Sean Callahan Date: Tue, 19 Oct 2021 10:56:43 -0700 Subject: [PATCH] bootstrap: truncate config file when saving Before this change config files would have remnants of the previous file's data. When saving, open file using os.Create() to truncate and/or create the file before writing. In cases when the file is read only, open using os.Open() which is shorthand for opening with the os.O_RDWR flag. --- src/bootstrap/config.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/src/bootstrap/config.go b/src/bootstrap/config.go index 2424202..8c281a7 100644 --- a/src/bootstrap/config.go +++ b/src/bootstrap/config.go @@ -4,14 +4,15 @@ import ( "encoding/base64" "encoding/json" "fmt" - "github.com/gorilla/securecookie" - "github.com/jessevdk/go-flags" "log" "math/rand" "os" "path/filepath" "runtime" "time" + + "github.com/gorilla/securecookie" + "github.com/jessevdk/go-flags" ) type Flags struct { @@ -59,7 +60,7 @@ type Config struct { GlibcLibLoc string `json:"-"` Autostart string `json:"-"` ConsoleCacheSize int `json:"console_cache_size,omitempty"` // the amount of cached lines, inside the factorio output cache - Secure bool `json:"secure"` // set to `false` to use this tool without SSL/TLS (Default: `true`) + Secure bool `json:"secure"` // set to `false` to use this tool without SSL/TLS (Default: `true`) } // set Configs default values. JSON unmarshal will replace when it found something different @@ -88,7 +89,7 @@ func GetConfig() Config { } func (config *Config) updateConfigFile() { - file, err := os.OpenFile(config.ConfFile, os.O_RDONLY, 0) + file, err := os.Open(config.ConfFile) failOnError(err, "Error opening file") defer file.Close() @@ -138,7 +139,7 @@ func (config *Config) updateConfigFile() { if resave { // save json file again - file, err = os.OpenFile(config.ConfFile, os.O_WRONLY, 0) + file, err = os.Create(config.ConfFile) failOnError(err, "Error opening file for writing") defer file.Close() @@ -156,7 +157,7 @@ func (config *Config) loadServerConfig() { // load and potentially update conf.json config.updateConfigFile() - file, err := os.OpenFile(config.ConfFile, os.O_RDWR, 0) + file, err := os.Open(config.ConfFile) failOnError(err, "Error loading config file.") defer file.Close()