2018-01-12 16:30:54 +00:00
|
|
|
// Package fspath contains routines for fspath manipulation
|
|
|
|
package fspath
|
2017-06-07 11:27:33 +00:00
|
|
|
|
|
|
|
import (
|
2019-09-05 10:01:04 +00:00
|
|
|
"errors"
|
2017-06-07 11:27:33 +00:00
|
|
|
"path"
|
2018-06-18 09:39:00 +00:00
|
|
|
"path/filepath"
|
|
|
|
"regexp"
|
2018-10-09 11:35:27 +00:00
|
|
|
"strings"
|
2018-06-18 09:39:00 +00:00
|
|
|
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/driveletter"
|
2017-06-07 11:27:33 +00:00
|
|
|
)
|
|
|
|
|
2019-09-05 10:01:04 +00:00
|
|
|
const (
|
|
|
|
configNameRe = `[\w_ -]+`
|
|
|
|
remoteNameRe = `^(:?` + configNameRe + `):`
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-06-16 14:08:09 +00:00
|
|
|
errInvalidCharacters = errors.New("config name contains invalid characters - may only contain 0-9, A-Z ,a-z ,_ , - and space")
|
2020-05-19 11:34:23 +00:00
|
|
|
errCantBeEmpty = errors.New("can't use empty string as a path")
|
2020-06-16 10:34:03 +00:00
|
|
|
errCantStartWithDash = errors.New("config name starts with -")
|
2019-09-05 10:01:04 +00:00
|
|
|
|
|
|
|
// urlMatcher is a pattern to match an rclone URL
|
|
|
|
// note that this matches invalid remoteNames
|
|
|
|
urlMatcher = regexp.MustCompile(`^(:?[^\\/:]*):(.*)$`)
|
|
|
|
|
|
|
|
// configNameMatcher is a pattern to match an rclone config name
|
|
|
|
configNameMatcher = regexp.MustCompile(`^` + configNameRe + `$`)
|
|
|
|
|
|
|
|
// remoteNameMatcher is a pattern to match an rclone remote name
|
|
|
|
remoteNameMatcher = regexp.MustCompile(remoteNameRe + `$`)
|
|
|
|
)
|
|
|
|
|
|
|
|
// CheckConfigName returns an error if configName is invalid
|
|
|
|
func CheckConfigName(configName string) error {
|
|
|
|
if !configNameMatcher.MatchString(configName) {
|
|
|
|
return errInvalidCharacters
|
|
|
|
}
|
2020-06-16 10:34:03 +00:00
|
|
|
// Reject configName, if it starts with -, complicates usage. (#4261)
|
|
|
|
if strings.HasPrefix(configName, "-") {
|
|
|
|
return errCantStartWithDash
|
|
|
|
}
|
2019-09-05 10:01:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CheckRemoteName returns an error if remoteName is invalid
|
|
|
|
func CheckRemoteName(remoteName string) error {
|
|
|
|
if !remoteNameMatcher.MatchString(remoteName) {
|
|
|
|
return errInvalidCharacters
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2018-06-18 09:39:00 +00: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
|
2019-09-05 10:01:04 +00:00
|
|
|
//
|
2020-05-19 11:34:23 +00:00
|
|
|
// An error may be returned if the remote name has invalid characters
|
|
|
|
// in it or if the path is empty.
|
2019-09-05 10:01:04 +00:00
|
|
|
func Parse(path string) (configName, fsPath string, err error) {
|
2020-05-19 11:34:23 +00:00
|
|
|
if path == "" {
|
|
|
|
return "", "", errCantBeEmpty
|
|
|
|
}
|
2019-09-05 10:01:04 +00:00
|
|
|
parts := urlMatcher.FindStringSubmatch(path)
|
2018-06-18 09:39:00 +00:00
|
|
|
configName, fsPath = "", path
|
|
|
|
if parts != nil && !driveletter.IsDriveLetter(parts[1]) {
|
|
|
|
configName, fsPath = parts[1], parts[2]
|
2019-09-05 10:01:04 +00:00
|
|
|
err = CheckRemoteName(configName + ":")
|
|
|
|
if err != nil {
|
|
|
|
return configName, fsPath, errInvalidCharacters
|
|
|
|
}
|
2018-06-18 09:39:00 +00:00
|
|
|
}
|
|
|
|
// change native directory separators to / if there are any
|
|
|
|
fsPath = filepath.ToSlash(fsPath)
|
2019-09-05 10:01:04 +00:00
|
|
|
return configName, fsPath, nil
|
2018-06-18 09:39:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Split splits a remote into a parent and a leaf
|
2017-06-07 11:27:33 +00: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 09:39:00 +00:00
|
|
|
// (except under Windows where \ will be translated into /)
|
2019-09-05 10:01:04 +00:00
|
|
|
func Split(remote string) (parent string, leaf string, err error) {
|
|
|
|
remoteName, remotePath, err := Parse(remote)
|
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2018-06-18 09:39:00 +00:00
|
|
|
if remoteName != "" {
|
|
|
|
remoteName += ":"
|
2017-06-07 11:27:33 +00:00
|
|
|
}
|
|
|
|
// Construct new remote name without last segment
|
|
|
|
parent, leaf = path.Split(remotePath)
|
2019-09-05 10:01:04 +00:00
|
|
|
return remoteName + parent, leaf, nil
|
2017-06-07 11:27:33 +00:00
|
|
|
}
|
2018-10-09 11:35:27 +00: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 ""
|
|
|
|
}
|