From ea00cd6975df1fed314a3f9e663266ca1ace1297 Mon Sep 17 00:00:00 2001 From: Jon Miller Date: Thu, 11 Feb 2016 11:20:13 -0800 Subject: [PATCH] Fixing dotnet/corefx#2279 to make the Linux FSW ignore root folder changes Commit migrated from https://github.com/dotnet/corefx/commit/777e88a068c9e7509fd56bfcce20fbfe52f687f7 --- .../src/System/IO/FileSystemWatcher.Linux.cs | 7 +++++++ .../tests/FileSystemWatcher.Changed.cs | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs index 28144cb0c46..fc22b3c2e6c 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/src/System/IO/FileSystemWatcher.Linux.cs @@ -572,6 +572,13 @@ namespace System.IO expandedName = associatedDirectoryEntry.GetPath(true, nextEvent.name); } + // To match Windows, ignore all changes that happen on the root folder itself + if (string.IsNullOrEmpty(expandedName)) + { + watcher = null; + continue; + } + // Determine whether the affected object is a directory (rather than a file). // If it is, we may need to do special processing, such as adding a watch for new // directories if IncludeSubdirectories is enabled. Since we're only watching diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Changed.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Changed.cs index 4cf5253f82f..256547e133d 100644 --- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Changed.cs +++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Changed.cs @@ -250,4 +250,22 @@ public class ChangedTests Utility.ExpectNoEvent(are, "symlink'd file change"); } } + + [Fact] + public static void FileSystemWatcher_Changed_RootFolderChangeDoesNotFireEvent() + { + using (var dir = Utility.CreateTestDirectory()) + using (var watcher = new FileSystemWatcher()) + { + AutoResetEvent are = Utility.WatchForEvents(watcher, WatcherChangeTypes.Changed); + + // Setup the watcher + watcher.Path = Path.GetFullPath(dir.Path); + watcher.Filter = "*"; + watcher.EnableRaisingEvents = true; + + Directory.SetLastWriteTime(dir.Path, DateTime.Now.AddSeconds(10)); + Utility.ExpectNoEvent(are, "Root Directory Change"); + } + } }