mirror of
https://github.com/rclone/rclone.git
synced 2024-11-22 16:34:30 +01:00
fspath: make JoinRootPath convert backslashes to slashes on Windows
The function is used for contructing remotes which may have backslashes in on Windows.
This commit is contained in:
parent
23c826db52
commit
3affc2e066
@ -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
|
// JoinRootPath joins any number of path elements into a single path, adding a
|
||||||
// separating slash if necessary. The result is Cleaned; in particular,
|
// separating slash if necessary. The result is Cleaned; in particular,
|
||||||
// all empty strings are ignored.
|
// all empty strings are ignored.
|
||||||
|
//
|
||||||
// If the first non empty element has a leading "//" this is preserved.
|
// 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 {
|
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 e != "" {
|
||||||
if strings.HasPrefix(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 ""
|
return ""
|
||||||
|
@ -2,6 +2,7 @@ package fspath
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
@ -153,6 +154,7 @@ func TestJoinRootPath(t *testing.T) {
|
|||||||
{[]string{"", "//server/sub", "path"}, "//server/sub/path"},
|
{[]string{"", "//server/sub", "path"}, "//server/sub/path"},
|
||||||
{[]string{"", "//server", "//path"}, "//server/path"},
|
{[]string{"", "//server", "//path"}, "//server/path"},
|
||||||
{[]string{"", "//server/sub", "//path"}, "//server/sub/path"},
|
{[]string{"", "//server/sub", "//path"}, "//server/sub/path"},
|
||||||
|
{[]string{"", filepath.FromSlash("//server/sub"), filepath.FromSlash("//path")}, "//server/sub/path"},
|
||||||
} {
|
} {
|
||||||
got := JoinRootPath(test.elements...)
|
got := JoinRootPath(test.elements...)
|
||||||
assert.Equal(t, test.want, got)
|
assert.Equal(t, test.want, got)
|
||||||
|
Loading…
Reference in New Issue
Block a user