lib/fs: Check events against both the user and eval root (#6013)

This commit is contained in:
Simon Frei
2019-09-22 09:03:22 +02:00
committed by Jakob Borg
parent 7127c13f18
commit 35b699dc77
6 changed files with 62 additions and 37 deletions

View File

@@ -156,17 +156,19 @@ func (f *BasicFilesystem) Roots() ([]string, error) {
// unrooted) or an error 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, root string) (string, error) {
func (f *BasicFilesystem) unrootedChecked(absPath string, roots []string) (string, error) {
absPath = f.resolveWin83(absPath)
lowerAbsPath := UnicodeLowercase(absPath)
lowerRoot := UnicodeLowercase(root)
if lowerAbsPath+string(PathSeparator) == lowerRoot {
return ".", nil
for _, root := range roots {
lowerRoot := UnicodeLowercase(root)
if lowerAbsPath+string(PathSeparator) == lowerRoot {
return ".", nil
}
if strings.HasPrefix(lowerAbsPath, lowerRoot) {
return rel(absPath, root), nil
}
}
if !strings.HasPrefix(lowerAbsPath, lowerRoot) {
return "", f.newErrWatchEventOutsideRoot(lowerAbsPath, lowerRoot)
}
return rel(absPath, root), nil
return "", f.newErrWatchEventOutsideRoot(lowerAbsPath, roots)
}
func rel(path, prefix string) string {
@@ -294,10 +296,10 @@ func evalSymlinks(in string) (string, error) {
// watchPaths adjust the folder root for use with the notify backend and the
// corresponding absolute path to be passed to notify to watch name.
func (f *BasicFilesystem) watchPaths(name string) (string, string, error) {
func (f *BasicFilesystem) watchPaths(name string) (string, []string, error) {
root, err := evalSymlinks(f.root)
if err != nil {
return "", "", err
return "", nil, err
}
// Remove `\\?\` prefix if the path is just a drive letter as a dirty
@@ -308,11 +310,17 @@ func (f *BasicFilesystem) watchPaths(name string) (string, string, error) {
absName, err := rooted(name, root)
if err != nil {
return "", "", err
return "", nil, err
}
root = f.resolveWin83(root)
roots := []string{f.resolveWin83(root)}
absName = f.resolveWin83(absName)
return filepath.Join(absName, "..."), root, nil
// Events returned from fs watching are all over the place, so allow
// both the user's input and the result of "canonicalizing" the path.
if roots[0] != f.root {
roots = append(roots, f.root)
}
return filepath.Join(absName, "..."), roots, nil
}