From 03d0f0dc340764b95112080471b28414ef6cff3b Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Wed, 26 Sep 2018 20:28:20 +0200 Subject: [PATCH] lib/fs: Try EvalSymlinks without '\\?\' prefix on failure (fixes #5226) (#5227) --- lib/fs/basicfs_unix.go | 3 +++ lib/fs/basicfs_watch.go | 6 +----- lib/fs/basicfs_watch_test.go | 2 +- lib/fs/basicfs_windows.go | 12 ++++++++++++ 4 files changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/fs/basicfs_unix.go b/lib/fs/basicfs_unix.go index 8a4d6b1a..814093a5 100644 --- a/lib/fs/basicfs_unix.go +++ b/lib/fs/basicfs_unix.go @@ -11,6 +11,7 @@ package fs import ( "fmt" "os" + "path/filepath" "strings" ) @@ -73,3 +74,5 @@ func (f *BasicFilesystem) unrootedChecked(absPath, root string) string { func rel(path, prefix string) string { return strings.TrimPrefix(strings.TrimPrefix(path, prefix), string(PathSeparator)) } + +var evalSymlinks = filepath.EvalSymlinks diff --git a/lib/fs/basicfs_watch.go b/lib/fs/basicfs_watch.go index 1eec1684..f00e8206 100644 --- a/lib/fs/basicfs_watch.go +++ b/lib/fs/basicfs_watch.go @@ -12,7 +12,6 @@ import ( "context" "errors" "path/filepath" - "runtime" "github.com/syncthing/notify" ) @@ -23,13 +22,10 @@ import ( var backendBuffer = 500 func (f *BasicFilesystem) Watch(name string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, error) { - evalRoot, err := filepath.EvalSymlinks(f.root) + evalRoot, err := evalSymlinks(f.root) if err != nil { return nil, err } - if runtime.GOOS == "windows" { - evalRoot = longFilenameSupport(evalRoot) - } absName, err := rooted(name, evalRoot) if err != nil { diff --git a/lib/fs/basicfs_watch_test.go b/lib/fs/basicfs_watch_test.go index 768885aa..128bfb9e 100644 --- a/lib/fs/basicfs_watch_test.go +++ b/lib/fs/basicfs_watch_test.go @@ -34,7 +34,7 @@ func TestMain(m *testing.M) { panic("Cannot get absolute path to working dir") } - dir, err = filepath.EvalSymlinks(dir) + dir, err = evalSymlinks(dir) if err != nil { panic("Cannot get real path to working dir") } diff --git a/lib/fs/basicfs_windows.go b/lib/fs/basicfs_windows.go index a79c2e92..ad44b7bb 100644 --- a/lib/fs/basicfs_windows.go +++ b/lib/fs/basicfs_windows.go @@ -210,3 +210,15 @@ func isMaybeWin83(absPath string) bool { } return strings.Contains(strings.TrimPrefix(filepath.Base(absPath), WindowsTempPrefix), "~") } + +func evalSymlinks(in string) (string, error) { + out, err := filepath.EvalSymlinks(in) + if err != nil && strings.HasPrefix(in, `\\?\`) { + // Try again without the `\\?\` prefix + out, err = filepath.EvalSymlinks(in[4:]) + } + if err != nil { + return "", err + } + return longFilenameSupport(out), nil +}