From d49ba652e2e1eefc5a3b6684485e802bea727e44 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Fri, 31 Aug 2018 21:10:36 +0100 Subject: [PATCH] local: fix mkdir error when trying to copy files to the root of a drive on windows This was causing errors which looked like this when copying a file to the root of a drive: mkdir \\?: The filename, directory name, or volume label syntax is incorrect. This was caused by an incorrect path splitting routine which was removing \ of the end of UNC paths when it shouldn't have been. Fixed by using the standard library `filepath.Dir` instead. --- backend/local/local.go | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/backend/local/local.go b/backend/local/local.go index 1be99a144..26ae8ede1 100644 --- a/backend/local/local.go +++ b/backend/local/local.go @@ -151,7 +151,7 @@ func NewFs(name, root string, m configmap.Mapper) (fs.Fs, error) { } if err == nil && fi.Mode().IsRegular() { // It is a file, so use the parent as the root - f.root, _ = getDirFile(f.root) + f.root = filepath.Dir(f.root) // return an error with an fs which points to the parent return f, fs.ErrorIsFile } @@ -565,7 +565,7 @@ func (f *Fs) DirMove(src fs.Fs, srcRemote, dstRemote string) error { } // Create parent of destination - dstParentPath, _ := getDirFile(dstPath) + dstParentPath := filepath.Dir(dstPath) err = os.MkdirAll(dstParentPath, 0777) if err != nil { return err @@ -784,7 +784,7 @@ func (o *Object) Open(options ...fs.OpenOption) (in io.ReadCloser, err error) { // mkdirAll makes all the directories needed to store the object func (o *Object) mkdirAll() error { - dir, _ := getDirFile(o.path) + dir := filepath.Dir(o.path) return os.MkdirAll(dir, 0777) } @@ -872,17 +872,6 @@ func (o *Object) Remove() error { return remove(o.path) } -// Return the directory and file from an OS path. Assumes -// os.PathSeparator is used. -func getDirFile(s string) (string, string) { - i := strings.LastIndex(s, string(os.PathSeparator)) - dir, file := s[:i], s[i+1:] - if dir == "" { - dir = string(os.PathSeparator) - } - return dir, file -} - // cleanPathFragment cleans an OS path fragment which is part of a // bigger path and not necessarily absolute func cleanPathFragment(s string) string {