From a98da9745e96c61d6bd2f95c5771c11d1c8c75f7 Mon Sep 17 00:00:00 2001 From: Christoph Polcin Date: Fri, 25 May 2018 12:15:16 +0200 Subject: [PATCH] cmd: create parent directories if writing files --- cmd/gowebdav/main.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cmd/gowebdav/main.go b/cmd/gowebdav/main.go index 0861d5a..74f9997 100644 --- a/cmd/gowebdav/main.go +++ b/cmd/gowebdav/main.go @@ -7,6 +7,7 @@ import ( d "github.com/studio-b12/gowebdav" "io" "os" + "path/filepath" "strings" ) @@ -175,11 +176,18 @@ func cmdPut(c *d.Client, p0, p1 string) (err error) { } func writeFile(path string, bytes []byte, mode os.FileMode) error { + parent := filepath.Dir(path) + if _, e := os.Stat(parent); os.IsNotExist(e) { + if e := os.MkdirAll(parent, os.ModePerm); e != nil { + return e + } + } + f, err := os.Create(path) - defer f.Close() if err != nil { return err } + defer f.Close() _, err = f.Write(bytes) return err