From 3affc2e066de0253810b8ca2d3c1af827817e8d2 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 1 Sep 2020 17:55:06 +0100 Subject: [PATCH] fspath: make JoinRootPath convert backslashes to slashes on Windows The function is used for contructing remotes which may have backslashes in on Windows. --- fs/fspath/path.go | 13 ++++++++++--- fs/fspath/path_test.go | 2 ++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/fs/fspath/path.go b/fs/fspath/path.go index d16c29310..a07b7ceea 100644 --- a/fs/fspath/path.go +++ b/fs/fspath/path.go @@ -105,14 +105,21 @@ func Split(remote string) (parent string, leaf string, err error) { // JoinRootPath joins any number of path elements into a single path, adding a // separating slash if necessary. The result is Cleaned; in particular, // all empty strings are ignored. +// // If the first non empty element has a leading "//" this is preserved. +// +// If the path contains \ these will be converted to / on Windows. func JoinRootPath(elem ...string) string { - for i, e := range elem { + es := make([]string, len(elem)) + for i := range es { + es[i] = filepath.ToSlash(elem[i]) + } + for i, e := range es { if e != "" { if strings.HasPrefix(e, "//") { - return "/" + path.Clean(strings.Join(elem[i:], "/")) + return "/" + path.Clean(strings.Join(es[i:], "/")) } - return path.Clean(strings.Join(elem[i:], "/")) + return path.Clean(strings.Join(es[i:], "/")) } } return "" diff --git a/fs/fspath/path_test.go b/fs/fspath/path_test.go index 135708ec8..c07379030 100644 --- a/fs/fspath/path_test.go +++ b/fs/fspath/path_test.go @@ -2,6 +2,7 @@ package fspath import ( "fmt" + "path/filepath" "runtime" "strings" "testing" @@ -153,6 +154,7 @@ func TestJoinRootPath(t *testing.T) { {[]string{"", "//server/sub", "path"}, "//server/sub/path"}, {[]string{"", "//server", "//path"}, "//server/path"}, {[]string{"", "//server/sub", "//path"}, "//server/sub/path"}, + {[]string{"", filepath.FromSlash("//server/sub"), filepath.FromSlash("//path")}, "//server/sub/path"}, } { got := JoinRootPath(test.elements...) assert.Equal(t, test.want, got)