From cdab3b687ac7a23dbb95e7b9f6d18bec12cae443 Mon Sep 17 00:00:00 2001 From: Steve Pfister Date: Tue, 2 Jul 2019 11:52:40 -0400 Subject: Add Empty Path Check to the Start of FileSystemWatcher Notify Events (#305) * Fixes https://github.com/mono/mono/issues/15434 by checking if the path is effectively empty before interacting with any Path methods. We needed to modify the fork at this point because Mono still supports netfx Path rules, which cannot contain spaces (CoreFX allows this on nix). * Review tweaks --- .../src/System/IO/FileSystemWatcher.cs | 18 ++++++++++++++++++ .../tests/FileSystemWatcher.unit.cs | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+) (limited to 'src') diff --git a/src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs b/src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs index 930816c553..45df8be254 100644 --- a/src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs +++ b/src/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.cs @@ -391,12 +391,30 @@ namespace System.IO throw new ArgumentException(SR.Format(SR.InvalidDirName_NotExists, path), nameof(path)); } +#if MONO + /// + /// Returns true if the path is effectively empty for the current OS. + /// This function is borrowed from PathInternal.Windows because Mono + /// on the desktop still needs to support the netfx behavior. + /// EffectivelyEmpty means an empty span, null, or just spaces ((char)32). + /// + private static bool IsEffectivelyEmpty(ReadOnlySpan path) + { + return (path.IsEmpty || path.IndexOf(' ') > -1); + } +#endif + /// /// Sees if the name given matches the name filter we have. /// private bool MatchPattern(ReadOnlySpan relativePath) { +#if MONO + if (IsEffectivelyEmpty (relativePath)) + return false; +#endif ReadOnlySpan name = IO.Path.GetFileName(relativePath); + if (name.Length == 0) return false; diff --git a/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs b/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs index 5035709528..003a54c9e4 100644 --- a/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs +++ b/src/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.unit.cs @@ -637,5 +637,24 @@ namespace System.IO.Tests } } } + +#if MONO && (!MOBILE || MOBILE_DESKTOP_HOST) + [Fact] + [PlatformSpecific(TestPlatforms.AnyUnix)] + public void FileSystemWatcher_WatchFileWithSpaces() + { + using (var testDirectory = new TempDirectory(GetTestFilePath())) + using (var file = new TempFile(Path.Combine(testDirectory.Path, " "))) + using (var watcher = new FileSystemWatcher(testDirectory.Path, "*")) + { + NotifyFilters filter = NotifyFilters.LastWrite | NotifyFilters.FileName; + watcher.NotifyFilter = filter; + + Action action = () => File.AppendAllText(file.Path, "longText!"); + + ExpectNoEvent(watcher, WatcherChangeTypes.Changed, action, expectedPath: file.Path); + } + } +#endif } } -- cgit v1.2.3