From 20ad96f3cd5b469bac8e2a6bf2fae91dfc255995 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Sat, 7 Feb 2015 15:49:09 +0000 Subject: [PATCH] windows: Stop drive letters (eg C:) getting mixed up with remotes (eg drive:) This was done by stopping the user configuring single letter remotes and making sure we don't treat single letter remotes as a remote name, but as a drive letter. --- fs/config.go | 16 +++++++++++++--- fs/driveletter.go | 12 ++++++++++++ fs/driveletter_windows.go | 13 +++++++++++++ fs/fs.go | 11 +++++++---- 4 files changed, 45 insertions(+), 7 deletions(-) create mode 100644 fs/driveletter.go create mode 100644 fs/driveletter_windows.go diff --git a/fs/config.go b/fs/config.go index f1ba64fde..01dcb9e7a 100644 --- a/fs/config.go +++ b/fs/config.go @@ -330,9 +330,19 @@ func EditConfig() { name := ChooseRemote() EditRemote(name) case 'n': - fmt.Printf("name> ") - name := ReadLine() - NewRemote(name) + for { + fmt.Printf("name> ") + name := ReadLine() + switch { + case name == "": + fmt.Printf("Can't use empty name\n") + case isDriveLetter(name): + fmt.Printf("Can't use %q as it can be confused a drive letter\n", name) + default: + NewRemote(name) + break + } + } case 'd': name := ChooseRemote() DeleteRemote(name) diff --git a/fs/driveletter.go b/fs/driveletter.go new file mode 100644 index 000000000..65b4e2567 --- /dev/null +++ b/fs/driveletter.go @@ -0,0 +1,12 @@ +// +build !windows + +package fs + +// isDriveLetter returns a bool indicating whether name is a valid +// Windows drive letter +// +// On non windows platforms we don't have drive letters so we always +// return false +func isDriveLetter(name string) bool { + return false +} diff --git a/fs/driveletter_windows.go b/fs/driveletter_windows.go new file mode 100644 index 000000000..ee9160a37 --- /dev/null +++ b/fs/driveletter_windows.go @@ -0,0 +1,13 @@ +// +build windows + +package fs + +// isDriveLetter returns a bool indicating whether name is a valid +// Windows drive letter +func isDriveLetter(name string) bool { + if len(name) != 1 { + return false + } + c := name[0] + return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') +} diff --git a/fs/fs.go b/fs/fs.go index e107d4b5a..0a148c52f 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -196,9 +196,6 @@ type Dir struct { // A channel of Dir objects type DirChan chan *Dir -// Pattern to match a url -var matcher = regexp.MustCompile(`^([\w_-]+):(.*)$`) - // Finds a FsInfo object for the name passed in // // Services are looked up in the config file @@ -211,16 +208,22 @@ func Find(name string) (*FsInfo, error) { return nil, fmt.Errorf("Didn't find filing system for %q", name) } +// Pattern to match an rclone url +var matcher = regexp.MustCompile(`^([\w_-]+):(.*)$`) + // NewFs makes a new Fs object from the path // // The path is of the form remote:path // // Remotes are looked up in the config file. If the remote isn't // found then NotFoundInConfigFile will be returned. +// +// On Windows avoid single character remote names as they can be mixed +// up with drive letters. func NewFs(path string) (Fs, error) { parts := matcher.FindStringSubmatch(path) fsName, configName, fsPath := "local", "local", path - if parts != nil { + if parts != nil && !isDriveLetter(parts[1]) { configName, fsPath = parts[1], parts[2] var err error fsName, err = ConfigFile.GetValue(configName, "type")