From 46056dbce9cb968135d05c17d78e23fbfe2aa305 Mon Sep 17 00:00:00 2001 From: Dane Everitt Date: Sat, 25 Apr 2020 13:12:17 -0700 Subject: [PATCH] Fix truncation when saving file changes; closes pterodactyl/panel#1956 --- parser/parser.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/parser/parser.go b/parser/parser.go index 61d63cf..9f861c7 100644 --- a/parser/parser.go +++ b/parser/parser.go @@ -204,6 +204,11 @@ func (f *ConfigurationFile) parseXmlFile(path string) error { // Ensure the XML is indented properly. doc.Indent(2) + // Truncate the file before attempting to write the changes. + if err := os.Truncate(path, 0); err != nil { + return err + } + // Write the XML to the file. _, err = doc.WriteTo(file) @@ -261,6 +266,11 @@ func (f *ConfigurationFile) parseIniFile(path string) error { } } + // Truncate the file before attempting to write the changes. + if err := os.Truncate(path, 0); err != nil { + return err + } + if _, err := cfg.WriteTo(file); err != nil { return err } @@ -387,7 +397,7 @@ func (f *ConfigurationFile) parsePropertiesFile(path string) error { } } - w, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0644) + w, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644) if err != nil { return err }