From f06b04ede4eff5c46f4e239d71f9fd1903dbabe2 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Tue, 27 Jul 2021 10:26:15 -0600 Subject: [PATCH] Fixed possible race condition. --- FileManager/BackgroundFileSystem.cs | 47 ++++++++++++++--------------- 1 file changed, 22 insertions(+), 25 deletions(-) diff --git a/FileManager/BackgroundFileSystem.cs b/FileManager/BackgroundFileSystem.cs index 66bb76f7..1b828999 100644 --- a/FileManager/BackgroundFileSystem.cs +++ b/FileManager/BackgroundFileSystem.cs @@ -76,43 +76,40 @@ namespace FileManager private void UpdateLocalCache(FileSystemEventArgs change) { - if (change.ChangeType == WatcherChangeTypes.Deleted) + lock (fsCache) { - RemovePath(change.FullPath); - } - else if (change.ChangeType == WatcherChangeTypes.Created) - { - AddPath(change.FullPath); - } - else if (change.ChangeType == WatcherChangeTypes.Renamed) - { - var renameChange = change as RenamedEventArgs; + if (change.ChangeType == WatcherChangeTypes.Deleted) + { + RemovePath(change.FullPath); + } + else if (change.ChangeType == WatcherChangeTypes.Created) + { + AddPath(change.FullPath); + } + else if (change.ChangeType == WatcherChangeTypes.Renamed) + { + var renameChange = change as RenamedEventArgs; - RemovePath(renameChange.OldFullPath); - AddPath(renameChange.FullPath); + RemovePath(renameChange.OldFullPath); + AddPath(renameChange.FullPath); + } } } private void RemovePath(string path) { - lock (fsCache) - { - var pathsToRemove = fsCache.Where(p => p.StartsWith(path)).ToArray(); + var pathsToRemove = fsCache.Where(p => p.StartsWith(path)).ToArray(); - foreach (var p in pathsToRemove) - fsCache.Remove(p); - } + foreach (var p in pathsToRemove) + fsCache.Remove(p); } private void AddPath(string path) { - lock (fsCache) - { - if (File.GetAttributes(path).HasFlag(FileAttributes.Directory)) - fsCache.AddRange(Directory.EnumerateFiles(path, SearchPattern, SearchOption)); - else - fsCache.Add(path); - } + if (File.GetAttributes(path).HasFlag(FileAttributes.Directory)) + fsCache.AddRange(Directory.EnumerateFiles(path, SearchPattern, SearchOption)); + else + fsCache.Add(path); } #endregion