lib/fs: Evaluate root when watching not on fs creation (fixes #5043) (#5105)

This commit is contained in:
Simon Frei
2018-08-11 22:24:36 +02:00
committed by Jakob Borg
parent b37c05c6b8
commit e20679afe1
5 changed files with 83 additions and 53 deletions

View File

@@ -13,6 +13,7 @@ import (
"errors"
"fmt"
"path/filepath"
"runtime"
"strings"
"github.com/syncthing/notify"
@@ -24,7 +25,15 @@ import (
var backendBuffer = 500
func (f *BasicFilesystem) Watch(name string, ignore Matcher, ctx context.Context, ignorePerms bool) (<-chan Event, error) {
absName, err := f.rootedSymlinkEvaluated(name)
evalRoot, err := filepath.EvalSymlinks(f.root)
if err != nil {
return nil, err
}
if runtime.GOOS == "windows" {
evalRoot = longFilenameSupport(evalRoot)
}
absName, err := rooted(name, evalRoot)
if err != nil {
return nil, err
}
@@ -39,7 +48,7 @@ func (f *BasicFilesystem) Watch(name string, ignore Matcher, ctx context.Context
if ignore.SkipIgnoredDirs() {
absShouldIgnore := func(absPath string) bool {
return ignore.ShouldIgnore(f.unrootedChecked(absPath))
return ignore.ShouldIgnore(f.unrootedChecked(absPath, evalRoot))
}
err = notify.WatchWithFilter(filepath.Join(absName, "..."), backendChan, absShouldIgnore, eventMask)
} else {
@@ -53,12 +62,12 @@ func (f *BasicFilesystem) Watch(name string, ignore Matcher, ctx context.Context
return nil, err
}
go f.watchLoop(name, backendChan, outChan, ignore, ctx)
go f.watchLoop(name, evalRoot, backendChan, outChan, ignore, ctx)
return outChan, nil
}
func (f *BasicFilesystem) watchLoop(name string, backendChan chan notify.EventInfo, outChan chan<- Event, ignore Matcher, ctx context.Context) {
func (f *BasicFilesystem) watchLoop(name, evalRoot string, backendChan chan notify.EventInfo, outChan chan<- Event, ignore Matcher, ctx context.Context) {
for {
// Detect channel overflow
if len(backendChan) == backendBuffer {
@@ -77,7 +86,7 @@ func (f *BasicFilesystem) watchLoop(name string, backendChan chan notify.EventIn
select {
case ev := <-backendChan:
relPath := f.unrootedChecked(ev.Path())
relPath := f.unrootedChecked(ev.Path(), evalRoot)
if ignore.ShouldIgnore(relPath) {
l.Debugln(f.Type(), f.URI(), "Watch: Ignoring", relPath)
continue
@@ -110,12 +119,13 @@ func (f *BasicFilesystem) eventType(notifyType notify.Event) EventType {
// unrooted). It panics if the given path is not a subpath and handles the
// special case when the given path is the folder root without a trailing
// pathseparator.
func (f *BasicFilesystem) unrootedChecked(absPath string) string {
if absPath+string(PathSeparator) == f.rootSymlinkEvaluated {
func (f *BasicFilesystem) unrootedChecked(absPath, root string) string {
absPath = f.resolveWin83(absPath)
if absPath+string(PathSeparator) == root {
return "."
}
if !strings.HasPrefix(absPath, f.rootSymlinkEvaluated) {
panic(fmt.Sprintf("bug: Notify backend is processing a change outside of the filesystem root: root==%v, rootSymEval==%v, path==%v", f.root, f.rootSymlinkEvaluated, absPath))
if !strings.HasPrefix(absPath, root) {
panic(fmt.Sprintf("bug: Notify backend is processing a change outside of the filesystem root: f.root==%v, root==%v, path==%v", f.root, root, absPath))
}
return f.unrootedSymlinkEvaluated(f.resolveWin83(absPath))
return rel(absPath, root)
}