Skip to content

Commit

Permalink
lib/fs: Remove When watching remove \?\ for drive letters (fixes sync…
Browse files Browse the repository at this point in the history
  • Loading branch information
imsodin committed May 9, 2019
1 parent 62a6d61 commit f2194b0
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/fs/basicfs_watch.go
Expand Up @@ -12,6 +12,7 @@ import (
"context"
"errors"
"path/filepath"
"runtime"

"github.com/syncthing/notify"
)
Expand All @@ -22,7 +23,14 @@ import (
var backendBuffer = 500

func (f *BasicFilesystem) Watch(name string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, error) {
evalRoot, err := evalSymlinks(f.root)
root := f.root
// Remove `\\?\` prefix if the path is just a drive letter as a dirty
// fix for https://github.com/syncthing/syncthing/issues/5578
if runtime.GOOS == "windows" && filepath.Clean(name) == "." && len(root) <= 7 && len(root) > 4 && root[:4] == `\\?\` {
root = root[4:]
}

evalRoot, err := evalSymlinks(root)
if err != nil {
return nil, err
}
Expand Down
42 changes: 42 additions & 0 deletions lib/fs/basicfs_watch_test.go
Expand Up @@ -149,6 +149,48 @@ func TestWatchRename(t *testing.T) {
testScenario(t, name, testCase, expectedEvents, allowedEvents, fakeMatcher{})
}

// TestWatchWinRoot checks that a watch at a drive letter does not panic due to
// out of root event on every event.
// https://github.com/syncthing/syncthing/issues/5695
func TestWatchWinRoot(t *testing.T) {
if runtime.GOOS != "windows" {
t.Skip("Windows specific test")
}

outChan := make(chan Event)
backendChan := make(chan notify.EventInfo, backendBuffer)

ctx, cancel := context.WithCancel(context.Background())

// testFs is Filesystem, but we need BasicFilesystem here
root := "D:"
fs := newBasicFilesystem(root)

go func() {
defer func() {
if r := recover(); r != nil {
t.Fatal(r)
}
cancel()
}()
fs.watchLoop(".", testDirAbs, backendChan, outChan, fakeMatcher{}, ctx)
}()

path := filepath.Join(root, "foo")
backendChan <- fakeEventInfo(path)

select {
case <-time.After(10 * time.Second):
cancel()
t.Errorf("Timed out before receiving event")
case ev := <-outChan:
if ev.Name != path {
t.Errorf("Unexpected event %v, expected %v", ev.Name, path)
}
case <-ctx.Done():
}
}

// TestWatchOutside checks that no changes from outside the folder make it in
func TestWatchOutside(t *testing.T) {
outChan := make(chan Event)
Expand Down

0 comments on commit f2194b0

Please sign in to comment.