diff --git a/src/libraries/Common/src/Interop/Linux/libc/Interop.inotify.cs b/src/libraries/Common/src/Interop/Linux/libc/Interop.inotify.cs
index 4b49062fa66..94f3cd9e568 100644
--- a/src/libraries/Common/src/Interop/Linux/libc/Interop.inotify.cs
+++ b/src/libraries/Common/src/Interop/Linux/libc/Interop.inotify.cs
@@ -14,8 +14,30 @@ internal static partial class Interop
[DllImport(Libraries.Libc, SetLastError = true)]
internal static extern int inotify_add_watch(int fd, string pathname, uint mask);
- [DllImport(Libraries.Libc, SetLastError = true)]
- internal static extern int inotify_rm_watch(int fd, int wd);
+ [DllImport(Libraries.Libc, SetLastError = true, EntryPoint = "inotify_rm_watch")]
+ private static extern int inotify_rm_watch_extern(int fd, int wd);
+
+ internal static int inotify_rm_watch(int fd, int wd)
+ {
+ int result = Interop.libc.inotify_rm_watch_extern(fd, wd);
+ if (result < 0)
+ {
+ int hr = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
+ if (hr == Interop.Errors.EINVAL)
+ {
+ // This specific case means that there was a deleted event in the queue that was not processed
+ // so this call is expected to fail since the WatchDescriptor is no longer valid and was cleaned
+ // up automatically by the OS.
+ result = 0;
+ }
+ else
+ {
+ System.Diagnostics.Debug.Fail("inotify_rm_watch failed with " + hr);
+ }
+ }
+
+ return result;
+ }
[Flags]
internal enum NotifyEvents
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 4306814cc42..f14f3b57fbf 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
@@ -10,6 +10,9 @@ using System.Threading.Tasks;
namespace System.IO
{
+ // Note: This class has an OS Limitation where the inotify API can miss events if a directory is created and immediately has
+ // changes underneath. This is due to the inotify* APIs not being recursive and needing to call inotify_add_watch on
+ // each subdirectory, causing a race between adding the watch and file system events happening.
public partial class FileSystemWatcher
{
/// Starts a new watch operation if one is not currently running.
@@ -166,7 +169,7 @@ namespace System.IO
}
///
- /// State and processing associatd with an active watch operation. This state is kept separate from FileSystemWatcher to avoid
+ /// State and processing associated with an active watch operation. This state is kept separate from FileSystemWatcher to avoid
/// race conditions when a user starts/stops/starts/stops/etc. in quick succession, resulting in the potential for multiple
/// active operations. It also helps with avoiding rooted cycles and enabling proper finalization.
///
@@ -470,7 +473,6 @@ namespace System.IO
}
uint mask = nextEvent.mask;
- bool addWatch = false;
string expandedName = null;
WatchedDirectory associatedDirectoryEntry = null;
@@ -531,6 +533,15 @@ namespace System.IO
previousEventCookie = 0;
}
+ // If the event signaled that there's a new subdirectory and if we're monitoring subdirectories,
+ // add a watch for it.
+ const Interop.libc.NotifyEvents AddMaskFilters = Interop.libc.NotifyEvents.IN_CREATE | Interop.libc.NotifyEvents.IN_MOVED_TO;
+ bool addWatch = ((mask & (uint)AddMaskFilters) != 0);
+ if (addWatch && isDir && _includeSubdirectories)
+ {
+ AddDirectoryWatch(associatedDirectoryEntry, nextEvent.name);
+ }
+
const Interop.libc.NotifyEvents switchMask =
Interop.libc.NotifyEvents.IN_Q_OVERFLOW | Interop.libc.NotifyEvents.IN_IGNORED |
Interop.libc.NotifyEvents.IN_CREATE | Interop.libc.NotifyEvents.IN_DELETE |
@@ -543,7 +554,6 @@ namespace System.IO
break;
case Interop.libc.NotifyEvents.IN_CREATE:
watcher.NotifyFileSystemEventArgs(WatcherChangeTypes.Created, expandedName);
- addWatch = true;
break;
case Interop.libc.NotifyEvents.IN_IGNORED:
// We're getting an IN_IGNORED because a directory watch was removed.
@@ -582,17 +592,9 @@ namespace System.IO
previousEventName = null;
previousEventParent = null;
previousEventCookie = 0;
- addWatch = true; // for either rename or creation, we need to update our state
break;
}
- // If the event signaled that there's a new subdirectory and if we're monitoring subdirectories,
- // add a watch for it.
- if (addWatch && isDir && _includeSubdirectories)
- {
- AddDirectoryWatch(associatedDirectoryEntry, nextEvent.name);
- }
-
// Drop our strong reference to the watcher now that we're potentially going to block again for another read
watcher = null;
}
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 e82620c8022..66ce3e8db07 100644
--- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Changed.cs
+++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Changed.cs
@@ -77,7 +77,7 @@ public partial class FileSystemWatcher_4000_Tests
}
[Fact, ActiveIssue(2279)]
- public static void FileSystemWatcher_ChangeWatchedFolder()
+ public static void FileSystemWatcher_Changed_WatchedFolder()
{
using (var dir = Utility.CreateTestDirectory())
using (var watcher = new FileSystemWatcher())
@@ -93,4 +93,29 @@ public partial class FileSystemWatcher_4000_Tests
Utility.ExpectEvent(eventOccured, "changed");
}
}
+
+ [Fact]
+ public static void FileSystemWatcher_Changed_NestedDirectories()
+ {
+ Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Changed, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
+ {
+ Directory.SetLastAccessTime(ttd.Path, DateTime.Now);
+ Utility.ExpectEvent(are, "changed");
+ },
+ NotifyFilters.DirectoryName | NotifyFilters.LastAccess);
+ }
+
+ [Fact]
+ public static void FileSystemWatcher_Changed_FileInNestedDirectory()
+ {
+ Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Changed, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
+ {
+ using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile")))
+ {
+ Directory.SetLastAccessTime(nestedFile.Path, DateTime.Now);
+ Utility.ExpectEvent(are, "changed");
+ }
+ },
+ NotifyFilters.DirectoryName | NotifyFilters.LastAccess | NotifyFilters.FileName);
+ }
}
diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Created.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Created.cs
index 3b685bc6765..3707cbfd567 100644
--- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Created.cs
+++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Created.cs
@@ -111,4 +111,16 @@ public partial class FileSystemWatcher_4000_Tests
Utility.ExpectNoEvent(eventOccured, "created");
}
}
+
+ [Fact]
+ public static void FileSystemWatcher_Created_FileCreatedInNestedDirectory()
+ {
+ Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Created, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
+ {
+ using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile")))
+ {
+ Utility.ExpectEvent(are, "nested file created", 1000 * 30);
+ }
+ });
+ }
}
diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Deleted.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Deleted.cs
index 8331ee9cfc1..3692a624220 100644
--- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Deleted.cs
+++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Deleted.cs
@@ -80,4 +80,78 @@ public partial class FileSystemWatcher_4000_Tests
}
}
}
+
+ [Fact]
+ public static void FileSystemWatcher_Deleted_NestedDirectories()
+ {
+ using (var dir = Utility.CreateTestDirectory())
+ using (var watcher = new FileSystemWatcher())
+ using (AutoResetEvent createOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created))
+ using (AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted))
+ {
+ watcher.Path = Path.GetFullPath(dir.Path);
+ watcher.Filter = "*";
+ watcher.IncludeSubdirectories = true;
+ watcher.EnableRaisingEvents = true;
+
+ using (var firstDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1")))
+ {
+ // Wait for the created event
+ Utility.ExpectEvent(createOccured, "create", 1000 * 30);
+
+ using (var secondDir = new TemporaryTestDirectory(Path.Combine(firstDir.Path, "dir2")))
+ {
+ // Wait for the created event
+ Utility.ExpectEvent(createOccured, "create", 1000 * 30);
+
+ using (var nestedDir = new TemporaryTestDirectory(Path.Combine(secondDir.Path, "nested")))
+ {
+ // Wait for the created event
+ Utility.ExpectEvent(createOccured, "create", 1000 * 30);
+ }
+
+ Utility.ExpectEvent(eventOccured, "deleted");
+ }
+
+ Utility.ExpectEvent(eventOccured, "deleted");
+ }
+
+ Utility.ExpectEvent(eventOccured, "deleted");
+ }
+ }
+
+ [Fact]
+ public static void FileSystemWatcher_Deleted_FileDeletedInNestedDirectory()
+ {
+ using (var dir = Utility.CreateTestDirectory())
+ using (var watcher = new FileSystemWatcher())
+ using (AutoResetEvent createOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created))
+ using (AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Deleted))
+ {
+ watcher.Path = Path.GetFullPath(dir.Path);
+ watcher.Filter = "*";
+ watcher.IncludeSubdirectories = true;
+ watcher.EnableRaisingEvents = true;
+
+ using (var firstDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1")))
+ {
+ // Wait for the created event
+ Utility.ExpectEvent(createOccured, "create", 1000 * 30);
+
+ using (var secondDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir2")))
+ {
+ // Wait for the created event
+ Utility.ExpectEvent(createOccured, "create", 1000 * 30);
+
+ using (var nestedDir = new TemporaryTestFile(Path.Combine(secondDir.Path, "nestedFile"))) { }
+
+ Utility.ExpectEvent(eventOccured, "deleted");
+ }
+
+ Utility.ExpectEvent(eventOccured, "deleted");
+ }
+
+ Utility.ExpectEvent(eventOccured, "deleted");
+ }
+ }
}
diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Renamed.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Renamed.cs
index 674f2b680a1..32986c7b972 100644
--- a/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Renamed.cs
+++ b/src/libraries/System.IO.FileSystem.Watcher/tests/FileSystemWatcher.Renamed.cs
@@ -1,7 +1,6 @@
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-using System;
using System.IO;
using System.Threading;
using Xunit;
@@ -79,4 +78,27 @@ public partial class FileSystemWatcher_4000_Tests
Utility.ExpectNoEvent(eventOccured, "created");
}
}
+
+ [Fact]
+ public static void FileSystemWatcher_Renamed_NestedDirectory()
+ {
+ Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Renamed, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
+ {
+ ttd.Move(ttd.Path + "_2");
+ Utility.ExpectEvent(are, "renamed");
+ });
+ }
+
+ [Fact]
+ public static void FileSystemWatcher_Renamed_FileInNestedDirectory()
+ {
+ Utility.TestNestedDirectoriesHelper(WatcherChangeTypes.Renamed, (AutoResetEvent are, TemporaryTestDirectory ttd) =>
+ {
+ using (var nestedFile = new TemporaryTestFile(Path.Combine(ttd.Path, "nestedFile")))
+ {
+ nestedFile.Move(nestedFile.Path + "_2");
+ Utility.ExpectEvent(are, "renamed");
+ }
+ });
+ }
}
diff --git a/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/Utility.cs b/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/Utility.cs
index 74eabeb3c05..89ac58567e6 100644
--- a/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/Utility.cs
+++ b/src/libraries/System.IO.FileSystem.Watcher/tests/Utility/Utility.cs
@@ -12,6 +12,7 @@ public static class Utility
// events are reported asynchronously by the OS, so allow an amount of time for
// them to arrive before testing an assertion.
public const int Timeout = 500;
+ public const int WaitForCreationTimeoutInMs = 1000 * 30;
public static TemporaryTestFile CreateTestFile([CallerMemberName] string path = null)
{
@@ -91,12 +92,42 @@ public static class Utility
public static void ExpectEvent(WaitHandle eventOccured, string eventName, int timeout = Utility.Timeout)
{
string message = String.Format("Didn't observe a {0} event within {1}ms", eventName, timeout);
- Assert.True(eventOccured.WaitOne(Utility.Timeout), message);
+ Assert.True(eventOccured.WaitOne(timeout), message);
}
public static void ExpectNoEvent(WaitHandle eventOccured, string eventName, int timeout = Utility.Timeout)
{
string message = String.Format("Should not observe a {0} event", eventName);
- Assert.False(eventOccured.WaitOne(Utility.Timeout), message);
+ Assert.False(eventOccured.WaitOne(timeout), message);
+ }
+
+ public static void TestNestedDirectoriesHelper(
+ WatcherChangeTypes change,
+ Action action,
+ NotifyFilters changeFilers = NotifyFilters.LastWrite | NotifyFilters.FileName | NotifyFilters.DirectoryName)
+ {
+ using (var dir = Utility.CreateTestDirectory())
+ using (var watcher = new FileSystemWatcher())
+ using (AutoResetEvent createdOccured = Utility.WatchForEvents(watcher, WatcherChangeTypes.Created))
+ using (AutoResetEvent eventOccured = Utility.WatchForEvents(watcher, change))
+ {
+ watcher.Path = Path.GetFullPath(dir.Path);
+ watcher.Filter = "*";
+ watcher.NotifyFilter = changeFilers;
+ watcher.IncludeSubdirectories = true;
+ watcher.EnableRaisingEvents = true;
+
+ using (var firstDir = new TemporaryTestDirectory(Path.Combine(dir.Path, "dir1")))
+ {
+ Utility.ExpectEvent(createdOccured, "dir1 created", WaitForCreationTimeoutInMs);
+
+ using (var nestedDir = new TemporaryTestDirectory(Path.Combine(firstDir.Path, "nested")))
+ {
+ Utility.ExpectEvent(createdOccured, "nested created", WaitForCreationTimeoutInMs);
+
+ action(eventOccured, nestedDir);
+ }
+ }
+ }
}
}