From ef338cca05b3bda553678b2030ea12ae33794c72 Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Mon, 5 Sep 2022 17:17:46 +0200 Subject: [PATCH] resources: Properly truncate files Previously when rewriting specific template files they were not overwritten properly. This commit updates the behaviour to use the right os.Open flags. --- distillery/resources.go | 4 +--- distillery/resources_template.go | 4 +--- 2 files changed, 2 insertions(+), 6 deletions(-) diff --git a/distillery/resources.go b/distillery/resources.go index 9487607..eeb43dd 100644 --- a/distillery/resources.go +++ b/distillery/resources.go @@ -54,10 +54,8 @@ func installFile(dst string, fsys embed.FS, src string, onInstallFile func(dst, // determine if we need to create the destination file, or if it already exists dstStat, dstErr := os.Stat(dst) - flag := os.O_WRONLY switch { case os.IsNotExist(dstErr): - flag |= os.O_CREATE case dstErr != nil: return errors.Wrapf(dstErr, "Error calling stat on destination %s", dst) case dstStat.IsDir(): @@ -65,7 +63,7 @@ func installFile(dst string, fsys embed.FS, src string, onInstallFile func(dst, } // Open the file - dstFile, err := os.OpenFile(dst, flag, srcStat.Mode()) + dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcStat.Mode()) if err != nil { return errors.Wrapf(err, "Error opening destination %s", dst) } diff --git a/distillery/resources_template.go b/distillery/resources_template.go index 6de7215..f51ea34 100644 --- a/distillery/resources_template.go +++ b/distillery/resources_template.go @@ -27,10 +27,8 @@ func InstallTemplate(dst, src string, context map[string]string) error { // determine if we need to create the destination file, or if it already exists dstStat, dstErr := os.Stat(dst) - flag := os.O_WRONLY switch { case os.IsNotExist(dstErr): - flag |= os.O_CREATE case dstErr != nil: return errors.Wrapf(dstErr, "Error calling stat on destination %s", dst) case dstStat.IsDir(): @@ -38,7 +36,7 @@ func InstallTemplate(dst, src string, context map[string]string) error { } // open and write the destination file - dstFile, err := os.OpenFile(dst, flag, srcMode) + dstFile, err := os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, srcMode) if err != nil { return errors.Wrapf(err, "Unable to open file %s", dst) }