github.com/xhghs/rclone@v1.51.1-0.20200430155106-e186a28cced8/fs/fspath/path.go (about) 1 // Package fspath contains routines for fspath manipulation 2 package fspath 3 4 import ( 5 "errors" 6 "path" 7 "path/filepath" 8 "regexp" 9 "strings" 10 11 "github.com/rclone/rclone/fs/driveletter" 12 ) 13 14 const ( 15 configNameRe = `[\w_ -]+` 16 remoteNameRe = `^(:?` + configNameRe + `):` 17 ) 18 19 var ( 20 errInvalidCharacters = errors.New("config name contains invalid characters - may only contain 0-9, A-Z ,a-z ,_ , - and space ") 21 22 // urlMatcher is a pattern to match an rclone URL 23 // note that this matches invalid remoteNames 24 urlMatcher = regexp.MustCompile(`^(:?[^\\/:]*):(.*)$`) 25 26 // configNameMatcher is a pattern to match an rclone config name 27 configNameMatcher = regexp.MustCompile(`^` + configNameRe + `$`) 28 29 // remoteNameMatcher is a pattern to match an rclone remote name 30 remoteNameMatcher = regexp.MustCompile(remoteNameRe + `$`) 31 ) 32 33 // CheckConfigName returns an error if configName is invalid 34 func CheckConfigName(configName string) error { 35 if !configNameMatcher.MatchString(configName) { 36 return errInvalidCharacters 37 } 38 return nil 39 } 40 41 // CheckRemoteName returns an error if remoteName is invalid 42 func CheckRemoteName(remoteName string) error { 43 if !remoteNameMatcher.MatchString(remoteName) { 44 return errInvalidCharacters 45 } 46 return nil 47 } 48 49 // Parse deconstructs a remote path into configName and fsPath 50 // 51 // If the path is a local path then configName will be returned as "". 52 // 53 // So "remote:path/to/dir" will return "remote", "path/to/dir" 54 // and "/path/to/local" will return ("", "/path/to/local") 55 // 56 // Note that this will turn \ into / in the fsPath on Windows 57 // 58 // An error may be returned if the remote name has invalid characters in it. 59 func Parse(path string) (configName, fsPath string, err error) { 60 parts := urlMatcher.FindStringSubmatch(path) 61 configName, fsPath = "", path 62 if parts != nil && !driveletter.IsDriveLetter(parts[1]) { 63 configName, fsPath = parts[1], parts[2] 64 err = CheckRemoteName(configName + ":") 65 if err != nil { 66 return configName, fsPath, errInvalidCharacters 67 } 68 } 69 // change native directory separators to / if there are any 70 fsPath = filepath.ToSlash(fsPath) 71 return configName, fsPath, nil 72 } 73 74 // Split splits a remote into a parent and a leaf 75 // 76 // if it returns leaf as an empty string then remote is a directory 77 // 78 // if it returns parent as an empty string then that means the current directory 79 // 80 // The returned values have the property that parent + leaf == remote 81 // (except under Windows where \ will be translated into /) 82 func Split(remote string) (parent string, leaf string, err error) { 83 remoteName, remotePath, err := Parse(remote) 84 if err != nil { 85 return "", "", err 86 } 87 if remoteName != "" { 88 remoteName += ":" 89 } 90 // Construct new remote name without last segment 91 parent, leaf = path.Split(remotePath) 92 return remoteName + parent, leaf, nil 93 } 94 95 // JoinRootPath joins any number of path elements into a single path, adding a 96 // separating slash if necessary. The result is Cleaned; in particular, 97 // all empty strings are ignored. 98 // If the first non empty element has a leading "//" this is preserved. 99 func JoinRootPath(elem ...string) string { 100 for i, e := range elem { 101 if e != "" { 102 if strings.HasPrefix(e, "//") { 103 return "/" + path.Clean(strings.Join(elem[i:], "/")) 104 } 105 return path.Clean(strings.Join(elem[i:], "/")) 106 } 107 } 108 return "" 109 }