Skip to content
This repository was archived by the owner on Nov 6, 2018. It is now read-only.

Commit 98ea34e

Browse files
author
N. Taylor Mullen
committed
Swallow IO related exceptions on file rename events.
- When enumerating files in the `e.FullPath` directory it's possible to get IO exceptions due to renames, folder structure changes and file locks. This swallows those exceptions. #187
1 parent 1224f5a commit 98ea34e

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Concurrent;
66
using System.Collections.Generic;
77
using System.IO;
8+
using System.Security;
89
using System.Threading;
910
using System.Threading.Tasks;
1011
using Microsoft.Extensions.FileSystemGlobbing;
@@ -129,13 +130,24 @@ private void OnRenamed(object sender, RenamedEventArgs e)
129130

130131
if (Directory.Exists(e.FullPath))
131132
{
132-
// If the renamed entity is a directory then notify tokens for every sub item.
133-
foreach (var newLocation in Directory.EnumerateFileSystemEntries(e.FullPath, "*", SearchOption.AllDirectories))
133+
try
134+
{
135+
// If the renamed entity is a directory then notify tokens for every sub item.
136+
foreach (var newLocation in Directory.EnumerateFileSystemEntries(e.FullPath, "*", SearchOption.AllDirectories))
137+
{
138+
// Calculated previous path of this moved item.
139+
var oldLocation = Path.Combine(e.OldFullPath, newLocation.Substring(e.FullPath.Length + 1));
140+
OnFileSystemEntryChange(oldLocation);
141+
OnFileSystemEntryChange(newLocation);
142+
}
143+
}
144+
catch (Exception ex) when (
145+
ex is IOException ||
146+
ex is SecurityException ||
147+
ex is DirectoryNotFoundException ||
148+
ex is UnauthorizedAccessException)
134149
{
135-
// Calculated previous path of this moved item.
136-
var oldLocation = Path.Combine(e.OldFullPath, newLocation.Substring(e.FullPath.Length + 1));
137-
OnFileSystemEntryChange(oldLocation);
138-
OnFileSystemEntryChange(newLocation);
150+
// Swallow the exception.
139151
}
140152
}
141153
}

0 commit comments

Comments
 (0)