2018-01-13 00:30:54 +08:00
|
|
|
// Package fspath contains routines for fspath manipulation
|
|
|
|
package fspath
|
2017-06-07 19:27:33 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"path"
|
2018-06-18 17:39:00 +08:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2018-10-09 19:35:27 +08:00
|
|
|
"strings"
|
2018-06-18 17:39:00 +08:00
|
|
|
|
2019-07-29 01:47:38 +08:00
|
|
|
"github.com/rclone/rclone/fs/driveletter"
|
2017-06-07 19:27:33 +08:00
|
|
|
)
|
|
|
|
|
2018-06-18 17:39:00 +08:00
|
|
|
// Matcher is a pattern to match an rclone URL
|
2018-08-08 05:51:06 +08:00
|
|
|
var Matcher = regexp.MustCompile(`^(:?[\w_ -]+):(.*)$`)
|
2018-06-18 17:39:00 +08:00
|
|
|
|
|
|
|
// Parse deconstructs a remote path into configName and fsPath
|
|
|
|
//
|
|
|
|
// If the path is a local path then configName will be returned as "".
|
|
|
|
//
|
|
|
|
// So "remote:path/to/dir" will return "remote", "path/to/dir"
|
|
|
|
// and "/path/to/local" will return ("", "/path/to/local")
|
|
|
|
//
|
|
|
|
// Note that this will turn \ into / in the fsPath on Windows
|
|
|
|
func Parse(path string) (configName, fsPath string) {
|
|
|
|
parts := Matcher.FindStringSubmatch(path)
|
|
|
|
configName, fsPath = "", path
|
|
|
|
if parts != nil && !driveletter.IsDriveLetter(parts[1]) {
|
|
|
|
configName, fsPath = parts[1], parts[2]
|
|
|
|
}
|
|
|
|
// change native directory separators to / if there are any
|
|
|
|
fsPath = filepath.ToSlash(fsPath)
|
|
|
|
return configName, fsPath
|
|
|
|
}
|
|
|
|
|
|
|
|
// Split splits a remote into a parent and a leaf
|
2017-06-07 19:27:33 +08:00
|
|
|
//
|
|
|
|
// if it returns leaf as an empty string then remote is a directory
|
|
|
|
//
|
|
|
|
// if it returns parent as an empty string then that means the current directory
|
|
|
|
//
|
|
|
|
// The returned values have the property that parent + leaf == remote
|
2018-06-18 17:39:00 +08:00
|
|
|
// (except under Windows where \ will be translated into /)
|
|
|
|
func Split(remote string) (parent string, leaf string) {
|
|
|
|
remoteName, remotePath := Parse(remote)
|
|
|
|
if remoteName != "" {
|
|
|
|
remoteName += ":"
|
2017-06-07 19:27:33 +08:00
|
|
|
}
|
|
|
|
// Construct new remote name without last segment
|
|
|
|
parent, leaf = path.Split(remotePath)
|
|
|
|
return remoteName + parent, leaf
|
|
|
|
}
|
2018-10-09 19:35:27 +08:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
func JoinRootPath(elem ...string) string {
|
|
|
|
for i, e := range elem {
|
|
|
|
if e != "" {
|
|
|
|
if strings.HasPrefix(e, "//") {
|
|
|
|
return "/" + path.Clean(strings.Join(elem[i:], "/"))
|
|
|
|
}
|
|
|
|
return path.Clean(strings.Join(elem[i:], "/"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|