Skip to content

Commit 4ebbb8e

Browse files
PhysicalFileProvider: Use active polling instead of FileSystemWatcher on iOS/tvOS (#58165)
In #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 #57931 Co-authored-by: Alexander Köplinger <[email protected]>
1 parent 0669a53 commit 4ebbb8e

File tree

12 files changed

+70
-45
lines changed

12 files changed

+70
-45
lines changed

src/libraries/Microsoft.Extensions.Configuration/tests/FunctionalTests/ConfigurationTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ public void GetDefaultBasePathForSources()
903903

904904
[Fact]
905905
[ActiveIssue("https://github.com/dotnet/runtime/issues/34582", TestPlatforms.Windows, TargetFrameworkMonikers.Netcoreapp, TestRuntimes.Mono)]
906-
[SkipOnPlatform(TestPlatforms.Browser, "System.IO.FileSystem.Watcher is not supported on Browser")]
906+
[SkipOnPlatform(TestPlatforms.Browser | TestPlatforms.iOS | TestPlatforms.tvOS, "System.IO.FileSystem.Watcher is not supported on Browser/iOS/tvOS")]
907907
public void CanEnumerateProviders()
908908
{
909909
var config = CreateBuilder()

src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFileProvider.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,8 @@ internal PhysicalFilesWatcher CreateFileWatcher()
162162

163163
FileSystemWatcher watcher;
164164
#if NETCOREAPP
165-
// For browser we will proactively fallback to polling since FileSystemWatcher is not supported.
166-
if (OperatingSystem.IsBrowser())
165+
// For browser/iOS/tvOS we will proactively fallback to polling since FileSystemWatcher is not supported.
166+
if (OperatingSystem.IsBrowser() || (OperatingSystem.IsIOS() && !OperatingSystem.IsMacCatalyst()) || OperatingSystem.IsTvOS())
167167
{
168168
UsePollingFileWatcher = true;
169169
UseActivePolling = true;

src/libraries/Microsoft.Extensions.FileProviders.Physical/src/PhysicalFilesWatcher.cs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public PhysicalFilesWatcher(
9090
if (fileSystemWatcher != null)
9191
{
9292
#if NETCOREAPP
93-
if (OperatingSystem.IsBrowser())
93+
if (OperatingSystem.IsBrowser() || (OperatingSystem.IsIOS() && !OperatingSystem.IsMacCatalyst()) || OperatingSystem.IsTvOS())
9494
{
9595
throw new PlatformNotSupportedException(SR.Format(SR.FileSystemWatcher_PlatformNotSupported, typeof(FileSystemWatcher)));
9696
}
@@ -148,7 +148,7 @@ public IChangeToken CreateFileChangeToken(string filter)
148148
}
149149

150150
IChangeToken changeToken = GetOrAddChangeToken(filter);
151-
// We made sure that browser never uses FileSystemWatcher.
151+
// We made sure that browser/iOS/tvOS never uses FileSystemWatcher.
152152
#pragma warning disable CA1416 // Validate platform compatibility
153153
TryEnableFileSystemWatcher();
154154
#pragma warning restore CA1416 // Validate platform compatibility
@@ -276,6 +276,9 @@ protected virtual void Dispose(bool disposing)
276276
}
277277

278278
[UnsupportedOSPlatform("browser")]
279+
[UnsupportedOSPlatform("ios")]
280+
[UnsupportedOSPlatform("tvos")]
281+
[SupportedOSPlatform("maccatalyst")]
279282
private void OnRenamed(object sender, RenamedEventArgs e)
280283
{
281284
// For a file name change or a directory's name change notify registered tokens.
@@ -309,12 +312,18 @@ ex is DirectoryNotFoundException ||
309312
}
310313

311314
[UnsupportedOSPlatform("browser")]
315+
[UnsupportedOSPlatform("ios")]
316+
[UnsupportedOSPlatform("tvos")]
317+
[SupportedOSPlatform("maccatalyst")]
312318
private void OnChanged(object sender, FileSystemEventArgs e)
313319
{
314320
OnFileSystemEntryChange(e.FullPath);
315321
}
316322

317323
[UnsupportedOSPlatform("browser")]
324+
[UnsupportedOSPlatform("ios")]
325+
[UnsupportedOSPlatform("tvos")]
326+
[SupportedOSPlatform("maccatalyst")]
318327
private void OnError(object sender, ErrorEventArgs e)
319328
{
320329
// Notify all cache entries on error.
@@ -325,6 +334,9 @@ private void OnError(object sender, ErrorEventArgs e)
325334
}
326335

327336
[UnsupportedOSPlatform("browser")]
337+
[UnsupportedOSPlatform("ios")]
338+
[UnsupportedOSPlatform("tvos")]
339+
[SupportedOSPlatform("maccatalyst")]
328340
private void OnFileSystemEntryChange(string fullPath)
329341
{
330342
try
@@ -348,6 +360,9 @@ ex is SecurityException ||
348360
}
349361

350362
[UnsupportedOSPlatform("browser")]
363+
[UnsupportedOSPlatform("ios")]
364+
[UnsupportedOSPlatform("tvos")]
365+
[SupportedOSPlatform("maccatalyst")]
351366
private void ReportChangeForMatchedEntries(string path)
352367
{
353368
if (string.IsNullOrEmpty(path))
@@ -385,6 +400,9 @@ private void ReportChangeForMatchedEntries(string path)
385400
}
386401

387402
[UnsupportedOSPlatform("browser")]
403+
[UnsupportedOSPlatform("ios")]
404+
[UnsupportedOSPlatform("tvos")]
405+
[SupportedOSPlatform("maccatalyst")]
388406
private void TryDisableFileSystemWatcher()
389407
{
390408
if (_fileWatcher != null)
@@ -403,6 +421,9 @@ private void TryDisableFileSystemWatcher()
403421
}
404422

405423
[UnsupportedOSPlatform("browser")]
424+
[UnsupportedOSPlatform("ios")]
425+
[UnsupportedOSPlatform("tvos")]
426+
[SupportedOSPlatform("maccatalyst")]
406427
private void TryEnableFileSystemWatcher()
407428
{
408429
if (_fileWatcher != null)

0 commit comments

Comments
 (0)