Skip to content

Commit

Permalink
PhysicalFileProvider: Use active polling instead of FileSystemWatcher…
Browse files Browse the repository at this point in the history
… on iOS/tvOS (dotnet#58142)

In dotnet#57931 we found out that the FSEventStream APIs which are used to implement System.IO.FileSystemWatcher aren't allowed on the App Store.
According to [Apple's docs](https://developer.apple.com/documentation/coreservices/1443980-fseventstreamcreate) these APIs are only supported on macOS and Mac Catalyst.

Mark System.IO.FileSystemWatcher as unsupported on iOS/tvOS and throw PNSE.
Switch PhysicalFileProvider to use active polling instead, like we do for Browser.

Addresses dotnet#57931
  • Loading branch information
akoeplinger authored Aug 26, 2021
1 parent 5eaa911 commit 4809039
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -903,7 +903,7 @@ public void GetDefaultBasePathForSources()

[Fact]
[ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
[SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")]
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.tvOS, "System.IO.FileSystem.Watcher is not supported on Browser/iOS/tvOS")]
public void CanEnumerateProviders()
{
var config = CreateBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,8 @@ internal PhysicalFilesWatcher CreateFileWatcher()

FileSystemWatcher? watcher;
#if NETCOREAPP
// For browser we will proactively fallback to polling since FileSystemWatcher is not supported.
if (OperatingSystem.IsBrowser())
// For browser/iOS/tvOS we will proactively fallback to polling since FileSystemWatcher is not supported.
if (OperatingSystem.IsBrowser() || (OperatingSystem.IsIOS() && !OperatingSystem.IsMacCatalyst()) || OperatingSystem.IsTvOS())
{
UsePollingFileWatcher = true;
UseActivePolling = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public PhysicalFilesWatcher(
if (fileSystemWatcher != null)
{
#if NETCOREAPP
if (OperatingSystem.IsBrowser())
if (OperatingSystem.IsBrowser() || (OperatingSystem.IsIOS() && !OperatingSystem.IsMacCatalyst()) || OperatingSystem.IsTvOS())
{
throw new PlatformNotSupportedException(SR.Format(SR.FileSystemWatcher_PlatformNotSupported, typeof(FileSystemWatcher)));
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public IChangeToken CreateFileChangeToken(string filter)
}

IChangeToken changeToken = GetOrAddChangeToken(filter);
// We made sure that browser never uses FileSystemWatcher.
// We made sure that browser/iOS/tvOS never uses FileSystemWatcher.
#pragma warning disable CA1416 // Validate platform compatibility
TryEnableFileSystemWatcher();
#pragma warning restore CA1416 // Validate platform compatibility
Expand Down Expand Up @@ -275,6 +275,9 @@ protected virtual void Dispose(bool disposing)
}

[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
private void OnRenamed(object sender, RenamedEventArgs e)
{
// For a file name change or a directory's name change notify registered tokens.
Expand Down Expand Up @@ -308,12 +311,18 @@ ex is DirectoryNotFoundException ||
}

[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
private void OnChanged(object sender, FileSystemEventArgs e)
{
OnFileSystemEntryChange(e.FullPath);
}

[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
private void OnError(object sender, ErrorEventArgs e)
{
// Notify all cache entries on error.
Expand All @@ -324,6 +333,9 @@ private void OnError(object sender, ErrorEventArgs e)
}

[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
private void OnFileSystemEntryChange(string fullPath)
{
try
Expand All @@ -347,6 +359,9 @@ ex is SecurityException ||
}

[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
private void ReportChangeForMatchedEntries(string path)
{
if (string.IsNullOrEmpty(path))
Expand Down Expand Up @@ -384,6 +399,9 @@ private void ReportChangeForMatchedEntries(string path)
}

[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
private void TryDisableFileSystemWatcher()
{
if (_fileWatcher != null)
Expand All @@ -402,6 +420,9 @@ private void TryDisableFileSystemWatcher()
}

[UnsupportedOSPlatform("browser")]
[UnsupportedOSPlatform("ios")]
[UnsupportedOSPlatform("tvos")]
[SupportedOSPlatform("maccatalyst")]
private void TryEnableFileSystemWatcher()
{
if (_fileWatcher != null)
Expand Down
Loading

0 comments on commit 4809039

Please sign in to comment.