From b349b3b537d974860da29cbfd1ec29ce6e9b2e3d Mon Sep 17 00:00:00 2001 From: superdarki Date: Wed, 9 Oct 2024 14:11:57 +0200 Subject: [PATCH 1/3] added server ConfigurationFile flag "create_file" --- parser/parser.go | 18 +++++++++++++++--- server/config_parser.go | 8 ++++++-- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index bef4bfa6..d176702a 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -104,9 +104,10 @@ func (cp ConfigurationParser) String() string { // ConfigurationFile defines a configuration file for the server startup. These // will be looped over and modified before the server finishes booting. type ConfigurationFile struct { - FileName string `json:"file"` - Parser ConfigurationParser `json:"parser"` - Replace []ConfigurationFileReplacement `json:"replace"` + FileName string `json:"file"` + Parser ConfigurationParser `json:"parser"` + Replace []ConfigurationFileReplacement `json:"replace"` + AllowCreateFile bool `json:"create_file"` // assumed true by unmarshal as it was the original behaviour // Tracks Wings' configuration so that we can quickly get values // out of it when variables request it. @@ -137,6 +138,17 @@ func (f *ConfigurationFile) UnmarshalJSON(data []byte) error { f.Replace = []ConfigurationFileReplacement{} } + // test if "create_file" exists, if not just assume true + if val, exists := m["create_file"]; exists && val != nil { + if err := json.Unmarshal(*val, &f.AllowCreateFile); err != nil { + log.WithField("file", f.FileName).WithField("error", err).Warn("create_file unmarshal failed assumed true") + f.AllowCreateFile = true + } + } else { + log.WithField("file", f.FileName).Info("create_file not specified assumed true") + f.AllowCreateFile = true + } + return nil } diff --git a/server/config_parser.go b/server/config_parser.go index 6d982bd3..5051c04c 100644 --- a/server/config_parser.go +++ b/server/config_parser.go @@ -20,9 +20,13 @@ func (s *Server) UpdateConfigurationFiles() { f := cf pool.Submit(func() { - file, err := s.Filesystem().UnixFS().Touch(f.FileName, ufs.O_RDWR|ufs.O_CREATE, 0o644) + flags := ufs.O_RDWR + if f.AllowCreateFile { + flags |= ufs.O_CREATE + } + file, err := s.Filesystem().UnixFS().Touch(f.FileName, flags, 0o644) if err != nil { - s.Log().WithField("file_name", f.FileName).WithField("error", err).Error("failed to open file for configuration") + s.Log().WithField("file_name", f.FileName).WithField("error", err).WithField("create_file", f.AllowCreateFile).Error("failed to open file for configuration") return } defer file.Close() From 1114601179a09e259fbd4c564f400b9c551ce13d Mon Sep 17 00:00:00 2001 From: superdarki Date: Wed, 9 Oct 2024 14:39:43 +0200 Subject: [PATCH 2/3] add IsNotExist error handling to file open --- server/config_parser.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/server/config_parser.go b/server/config_parser.go index 5051c04c..5828cfc9 100644 --- a/server/config_parser.go +++ b/server/config_parser.go @@ -1,6 +1,7 @@ package server import ( + "os" "runtime" "github.com/gammazero/workerpool" @@ -26,7 +27,9 @@ func (s *Server) UpdateConfigurationFiles() { } file, err := s.Filesystem().UnixFS().Touch(f.FileName, flags, 0o644) if err != nil { - s.Log().WithField("file_name", f.FileName).WithField("error", err).WithField("create_file", f.AllowCreateFile).Error("failed to open file for configuration") + if !os.IsNotExist(err) || f.AllowCreateFile { + s.Log().WithField("file_name", f.FileName).WithField("error", err).Error("failed to open file for configuration") + } return } defer file.Close() From 257f84360d6178b3574787b54903eae3f5c952ce Mon Sep 17 00:00:00 2001 From: superdarki Date: Thu, 10 Oct 2024 14:25:36 +0200 Subject: [PATCH 3/3] Changed to use "Open" instead of "Touch" with flags --- parser/parser.go | 4 ++-- server/config_parser.go | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/parser/parser.go b/parser/parser.go index d176702a..b7ce1177 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -141,11 +141,11 @@ func (f *ConfigurationFile) UnmarshalJSON(data []byte) error { // test if "create_file" exists, if not just assume true if val, exists := m["create_file"]; exists && val != nil { if err := json.Unmarshal(*val, &f.AllowCreateFile); err != nil { - log.WithField("file", f.FileName).WithField("error", err).Warn("create_file unmarshal failed assumed true") + log.WithField("file", f.FileName).WithField("error", err).Warn("create_file unmarshal failed") f.AllowCreateFile = true } } else { - log.WithField("file", f.FileName).Info("create_file not specified assumed true") + log.WithField("file", f.FileName).Debug("create_file not specified assumed true") f.AllowCreateFile = true } diff --git a/server/config_parser.go b/server/config_parser.go index 5828cfc9..294e8e5f 100644 --- a/server/config_parser.go +++ b/server/config_parser.go @@ -21,14 +21,18 @@ func (s *Server) UpdateConfigurationFiles() { f := cf pool.Submit(func() { - flags := ufs.O_RDWR + var file ufs.File + var err error if f.AllowCreateFile { - flags |= ufs.O_CREATE + file, err = s.Filesystem().UnixFS().Touch(f.FileName, ufs.O_RDWR|ufs.O_CREATE, 0o644) + } else { + file, err = s.Filesystem().UnixFS().Open(f.FileName) } - file, err := s.Filesystem().UnixFS().Touch(f.FileName, flags, 0o644) if err != nil { if !os.IsNotExist(err) || f.AllowCreateFile { s.Log().WithField("file_name", f.FileName).WithField("error", err).Error("failed to open file for configuration") + } else { + s.Log().WithField("file_name", f.FileName).Debug("file not created") } return }