cmd: create parent directories if writing files

This commit is contained in:
Christoph Polcin 2018-05-25 12:15:16 +02:00
parent 1786d37966
commit a98da9745e

View File

@ -7,6 +7,7 @@ import (
d "github.com/studio-b12/gowebdav" d "github.com/studio-b12/gowebdav"
"io" "io"
"os" "os"
"path/filepath"
"strings" "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 { 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) f, err := os.Create(path)
defer f.Close()
if err != nil { if err != nil {
return err return err
} }
defer f.Close()
_, err = f.Write(bytes) _, err = f.Write(bytes)
return err return err