From 045940e2927d179a3cc44c7661cc5d600334c0cb Mon Sep 17 00:00:00 2001 From: James Mills <1290234+prologic@users.noreply.github.com> Date: Sat, 1 Apr 2023 21:18:28 +1000 Subject: [PATCH] Fix and preserve file permissions when copying raw files (Fixes #15) --- main.go | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/main.go b/main.go index 8da7525..bc0378f 100644 --- a/main.go +++ b/main.go @@ -545,21 +545,38 @@ func buildHTML(path string, w io.Writer, vars Vars) error { // Copies file as is from path to writer func buildRaw(path string, w io.Writer) error { - in, err := os.Open(path) + r, err := os.Open(path) if err != nil { return err } - defer in.Close() + defer r.Close() + if w == nil { - out, err := os.Create(filepath.Join(PUBDIR, path)) + stat, err := os.Stat(path) + if err != nil { + return err + } + + fn := filepath.Join(PUBDIR, path) + + out, err := os.Create(fn) if err != nil { return err } defer out.Close() + + if err := os.Chmod(fn, stat.Mode()); err != nil { + return err + } + w = out } - _, err = io.Copy(w, in) - return err + + if _, err := io.Copy(w, r); err != nil { + return err + } + + return nil } func build(path string, w io.Writer, vars Vars) error {