From 59b343a9bf4b62cec4cccb3350bf390612d339b9 Mon Sep 17 00:00:00 2001 From: greatroar <@> Date: Mon, 2 Mar 2020 21:54:53 +0100 Subject: [PATCH] Remove OS-specific versions of fs.MkdirAll Go has supported Windows paths correctly since 1.11, see https://github.com/golang/go/issues/10900 and the commit referenced there. --- internal/fs/file.go | 8 ++++++ internal/fs/file_unix.go | 8 ------ internal/fs/file_windows.go | 57 ------------------------------------- 3 files changed, 8 insertions(+), 65 deletions(-) diff --git a/internal/fs/file.go b/internal/fs/file.go index f1ff451ed..e438857b2 100644 --- a/internal/fs/file.go +++ b/internal/fs/file.go @@ -12,6 +12,14 @@ func Mkdir(name string, perm os.FileMode) error { return os.Mkdir(fixpath(name), perm) } +// MkdirAll creates a directory named path, along with any necessary parents, +// and returns nil, or else returns an error. The permission bits perm are used +// for all directories that MkdirAll creates. If path is already a directory, +// MkdirAll does nothing and returns nil. +func MkdirAll(path string, perm os.FileMode) error { + return os.MkdirAll(fixpath(path), perm) +} + // Readlink returns the destination of the named symbolic link. // If there is an error, it will be of type *PathError. func Readlink(name string) (string, error) { diff --git a/internal/fs/file_unix.go b/internal/fs/file_unix.go index 612465670..f5ea36696 100644 --- a/internal/fs/file_unix.go +++ b/internal/fs/file_unix.go @@ -14,14 +14,6 @@ func fixpath(name string) string { return name } -// MkdirAll creates a directory named path, along with any necessary parents, -// and returns nil, or else returns an error. The permission bits perm are used -// for all directories that MkdirAll creates. If path is already a directory, -// MkdirAll does nothing and returns nil. -func MkdirAll(path string, perm os.FileMode) error { - return os.MkdirAll(fixpath(path), perm) -} - // TempFile creates a temporary file which has already been deleted (on // supported platforms) func TempFile(dir, prefix string) (f *os.File, err error) { diff --git a/internal/fs/file_windows.go b/internal/fs/file_windows.go index dd53cde5e..8a4d01fb0 100644 --- a/internal/fs/file_windows.go +++ b/internal/fs/file_windows.go @@ -5,7 +5,6 @@ import ( "os" "path/filepath" "strings" - "syscall" ) // fixpath returns an absolute path on windows, so restic can open long file @@ -31,62 +30,6 @@ func fixpath(name string) string { return name } -// MkdirAll creates a directory named path, along with any necessary parents, -// and returns nil, or else returns an error. The permission bits perm are used -// for all directories that MkdirAll creates. If path is already a directory, -// MkdirAll does nothing and returns nil. -// -// Adapted from the stdlib MkdirAll, added test for volume name. -func MkdirAll(path string, perm os.FileMode) error { - // Fast path: if we can tell whether path is a directory or file, stop with success or error. - dir, err := os.Stat(path) - if err == nil { - if dir.IsDir() { - return nil - } - return &os.PathError{ - Op: "mkdir", - Path: path, - Err: syscall.ENOTDIR, - } - } - - // Slow path: make sure parent exists and then call Mkdir for path. - i := len(path) - for i > 0 && os.IsPathSeparator(path[i-1]) { // Skip trailing path separator. - i-- - } - - j := i - for j > 0 && !os.IsPathSeparator(path[j-1]) { // Scan backward over element. - j-- - } - - if j > 1 { - // Create parent - parent := path[0 : j-1] - if parent != filepath.VolumeName(parent) { - err = MkdirAll(parent, perm) - if err != nil { - return err - } - } - } - - // Parent now exists; invoke Mkdir and use its result. - err = os.Mkdir(path, perm) - if err != nil { - // Handle arguments like "foo/." by - // double-checking that directory doesn't exist. - dir, err1 := os.Lstat(path) - if err1 == nil && dir.IsDir() { - return nil - } - return err - } - return nil -} - // TempFile creates a temporary file. func TempFile(dir, prefix string) (f *os.File, err error) { return ioutil.TempFile(dir, prefix)