vfs: fix OS vs Unix path confusion - fixes ChangeNotify on Windows

See: https://forum.rclone.org/t/windows-mount-polling-not-recognising-all-changes-made-by-another-box/16708
This commit is contained in:
Nick Craig-Wood 2020-06-02 19:25:59 +01:00
parent 151f03378f
commit 50e31c6636
2 changed files with 25 additions and 6 deletions

View File

@ -4,6 +4,7 @@ package vfscache
import ( import (
"context" "context"
"os" "os"
"path"
"path/filepath" "path/filepath"
"runtime" "runtime"
"sort" "sort"
@ -78,9 +79,11 @@ func New(ctx context.Context, fremote fs.Fs, opt *vfscommon.Options) (*Cache, er
} }
// clean returns the cleaned version of name for use in the index map // clean returns the cleaned version of name for use in the index map
//
// name should be a remote path not an osPath
func clean(name string) string { func clean(name string) string {
name = strings.Trim(name, "/") name = strings.Trim(name, "/")
name = filepath.Clean(name) name = path.Clean(name)
if name == "." || name == "/" { if name == "." || name == "/" {
name = "" name = ""
} }
@ -94,9 +97,11 @@ func (c *Cache) ToOSPath(name string) string {
// Mkdir makes the directory for name in the cache and returns an os // Mkdir makes the directory for name in the cache and returns an os
// path for the file // path for the file
//
// name should be a remote path not an osPath
func (c *Cache) Mkdir(name string) (string, error) { func (c *Cache) Mkdir(name string) (string, error) {
parent := vfscommon.FindParent(name) parent := vfscommon.FindParent(name)
leaf := filepath.Base(name) leaf := path.Base(name)
parentPath := c.ToOSPath(parent) parentPath := c.ToOSPath(parent)
err := os.MkdirAll(parentPath, 0700) err := os.MkdirAll(parentPath, 0700)
if err != nil { if err != nil {
@ -244,7 +249,7 @@ func (c *Cache) Rename(name string, newName string) (err error) {
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
return errors.Wrapf(err, "Failed to stat destination: %s", osNewPath) return errors.Wrapf(err, "Failed to stat destination: %s", osNewPath)
} }
parent := vfscommon.FindParent(osNewPath) parent := vfscommon.OsFindParent(osNewPath)
err = os.MkdirAll(parent, 0700) err = os.MkdirAll(parent, 0700)
if err != nil { if err != nil {
return errors.Wrapf(err, "Failed to create parent dir: %s", parent) return errors.Wrapf(err, "Failed to create parent dir: %s", parent)

View File

@ -1,12 +1,26 @@
package vfscommon package vfscommon
import "path/filepath" import (
"path"
"path/filepath"
)
// FindParent returns the parent directory of name, or "" for the root // OsFindParent returns the parent directory of name, or "" for the
func FindParent(name string) string { // root for OS native paths.
func OsFindParent(name string) string {
parent := filepath.Dir(name) parent := filepath.Dir(name)
if parent == "." || parent == "/" { if parent == "." || parent == "/" {
parent = "" parent = ""
} }
return parent return parent
} }
// FindParent returns the parent directory of name, or "" for the root
// for rclone paths.
func FindParent(name string) string {
parent := path.Dir(name)
if parent == "." || parent == "/" {
parent = ""
}
return parent
}