| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 | 
							- #nullable disable
 
- #pragma warning disable CS1591
 
- using System;
 
- using System.Collections.Concurrent;
 
- using System.Collections.Generic;
 
- using System.IO;
 
- using System.Linq;
 
- using System.Threading.Tasks;
 
- using Emby.Server.Implementations.Library;
 
- using MediaBrowser.Controller.Configuration;
 
- using MediaBrowser.Controller.Entities;
 
- using MediaBrowser.Controller.Library;
 
- using MediaBrowser.Model.IO;
 
- using Microsoft.Extensions.Logging;
 
- namespace Emby.Server.Implementations.IO
 
- {
 
-     public class LibraryMonitor : ILibraryMonitor
 
-     {
 
-         private readonly ILogger<LibraryMonitor> _logger;
 
-         private readonly ILibraryManager _libraryManager;
 
-         private readonly IServerConfigurationManager _configurationManager;
 
-         private readonly IFileSystem _fileSystem;
 
-         /// <summary>
 
-         /// The file system watchers.
 
-         /// </summary>
 
-         private readonly ConcurrentDictionary<string, FileSystemWatcher> _fileSystemWatchers = new ConcurrentDictionary<string, FileSystemWatcher>(StringComparer.OrdinalIgnoreCase);
 
-         /// <summary>
 
-         /// The affected paths.
 
-         /// </summary>
 
-         private readonly List<FileRefresher> _activeRefreshers = new List<FileRefresher>();
 
-         /// <summary>
 
-         /// A dynamic list of paths that should be ignored.  Added to during our own file system modifications.
 
-         /// </summary>
 
-         private readonly ConcurrentDictionary<string, string> _tempIgnoredPaths = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
 
-         private bool _disposed = false;
 
-         /// <summary>
 
-         /// Initializes a new instance of the <see cref="LibraryMonitor" /> class.
 
-         /// </summary>
 
-         /// <param name="logger">The logger.</param>
 
-         /// <param name="libraryManager">The library manager.</param>
 
-         /// <param name="configurationManager">The configuration manager.</param>
 
-         /// <param name="fileSystem">The filesystem.</param>
 
-         public LibraryMonitor(
 
-             ILogger<LibraryMonitor> logger,
 
-             ILibraryManager libraryManager,
 
-             IServerConfigurationManager configurationManager,
 
-             IFileSystem fileSystem)
 
-         {
 
-             _libraryManager = libraryManager;
 
-             _logger = logger;
 
-             _configurationManager = configurationManager;
 
-             _fileSystem = fileSystem;
 
-         }
 
-         /// <summary>
 
-         /// Add the path to our temporary ignore list.  Use when writing to a path within our listening scope.
 
-         /// </summary>
 
-         /// <param name="path">The path.</param>
 
-         private void TemporarilyIgnore(string path)
 
-         {
 
-             _tempIgnoredPaths[path] = path;
 
-         }
 
-         public void ReportFileSystemChangeBeginning(string path)
 
-         {
 
-             if (string.IsNullOrEmpty(path))
 
-             {
 
-                 throw new ArgumentNullException(nameof(path));
 
-             }
 
-             TemporarilyIgnore(path);
 
-         }
 
-         public bool IsPathLocked(string path)
 
-         {
 
-             // This method is not used by the core but it used by auto-organize
 
-             var lockedPaths = _tempIgnoredPaths.Keys.ToList();
 
-             return lockedPaths.Any(i => _fileSystem.AreEqual(i, path) || _fileSystem.ContainsSubPath(i, path));
 
-         }
 
-         public async void ReportFileSystemChangeComplete(string path, bool refreshPath)
 
-         {
 
-             if (string.IsNullOrEmpty(path))
 
-             {
 
-                 throw new ArgumentNullException(nameof(path));
 
-             }
 
-             // This is an arbitrary amount of time, but delay it because file system writes often trigger events long after the file was actually written to.
 
-             // Seeing long delays in some situations, especially over the network, sometimes up to 45 seconds
 
-             // But if we make this delay too high, we risk missing legitimate changes, such as user adding a new file, or hand-editing metadata
 
-             await Task.Delay(45000).ConfigureAwait(false);
 
-             _tempIgnoredPaths.TryRemove(path, out _);
 
-             if (refreshPath)
 
-             {
 
-                 try
 
-                 {
 
-                     ReportFileSystemChanged(path);
 
-                 }
 
-                 catch (Exception ex)
 
-                 {
 
-                     _logger.LogError(ex, "Error in ReportFileSystemChanged for {Path}", path);
 
-                 }
 
-             }
 
-         }
 
-         private bool IsLibraryMonitorEnabled(BaseItem item)
 
-         {
 
-             if (item is BasePluginFolder)
 
-             {
 
-                 return false;
 
-             }
 
-             var options = _libraryManager.GetLibraryOptions(item);
 
-             if (options != null)
 
-             {
 
-                 return options.EnableRealtimeMonitor;
 
-             }
 
-             return false;
 
-         }
 
-         public void Start()
 
-         {
 
-             _libraryManager.ItemAdded += OnLibraryManagerItemAdded;
 
-             _libraryManager.ItemRemoved += OnLibraryManagerItemRemoved;
 
-             var pathsToWatch = new List<string>();
 
-             var paths = _libraryManager
 
-                 .RootFolder
 
-                 .Children
 
-                 .Where(IsLibraryMonitorEnabled)
 
-                 .OfType<Folder>()
 
-                 .SelectMany(f => f.PhysicalLocations)
 
-                 .Distinct(StringComparer.OrdinalIgnoreCase)
 
-                 .OrderBy(i => i)
 
-                 .ToList();
 
-             foreach (var path in paths)
 
-             {
 
-                 if (!ContainsParentFolder(pathsToWatch, path))
 
-                 {
 
-                     pathsToWatch.Add(path);
 
-                 }
 
-             }
 
-             foreach (var path in pathsToWatch)
 
-             {
 
-                 StartWatchingPath(path);
 
-             }
 
-         }
 
-         private void StartWatching(BaseItem item)
 
-         {
 
-             if (IsLibraryMonitorEnabled(item))
 
-             {
 
-                 StartWatchingPath(item.Path);
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Handles the ItemRemoved event of the LibraryManager control.
 
-         /// </summary>
 
-         /// <param name="sender">The source of the event.</param>
 
-         /// <param name="e">The <see cref="ItemChangeEventArgs"/> instance containing the event data.</param>
 
-         private void OnLibraryManagerItemRemoved(object sender, ItemChangeEventArgs e)
 
-         {
 
-             if (e.Parent is AggregateFolder)
 
-             {
 
-                 StopWatchingPath(e.Item.Path);
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Handles the ItemAdded event of the LibraryManager control.
 
-         /// </summary>
 
-         /// <param name="sender">The source of the event.</param>
 
-         /// <param name="e">The <see cref="ItemChangeEventArgs"/> instance containing the event data.</param>
 
-         private void OnLibraryManagerItemAdded(object sender, ItemChangeEventArgs e)
 
-         {
 
-             if (e.Parent is AggregateFolder)
 
-             {
 
-                 StartWatching(e.Item);
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Examine a list of strings assumed to be file paths to see if it contains a parent of
 
-         /// the provided path.
 
-         /// </summary>
 
-         /// <param name="lst">The LST.</param>
 
-         /// <param name="path">The path.</param>
 
-         /// <returns><c>true</c> if [contains parent folder] [the specified LST]; otherwise, <c>false</c>.</returns>
 
-         /// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c>.</exception>
 
-         private static bool ContainsParentFolder(IEnumerable<string> lst, string path)
 
-         {
 
-             if (string.IsNullOrEmpty(path))
 
-             {
 
-                 throw new ArgumentNullException(nameof(path));
 
-             }
 
-             path = path.TrimEnd(Path.DirectorySeparatorChar);
 
-             return lst.Any(str =>
 
-             {
 
-                 // this should be a little quicker than examining each actual parent folder...
 
-                 var compare = str.TrimEnd(Path.DirectorySeparatorChar);
 
-                 return path.Equals(compare, StringComparison.OrdinalIgnoreCase) || (path.StartsWith(compare, StringComparison.OrdinalIgnoreCase) && path[compare.Length] == Path.DirectorySeparatorChar);
 
-             });
 
-         }
 
-         /// <summary>
 
-         /// Starts the watching path.
 
-         /// </summary>
 
-         /// <param name="path">The path.</param>
 
-         private void StartWatchingPath(string path)
 
-         {
 
-             if (!Directory.Exists(path))
 
-             {
 
-                 // Seeing a crash in the mono runtime due to an exception being thrown on a different thread
 
-                 _logger.LogInformation("Skipping realtime monitor for {Path} because the path does not exist", path);
 
-                 return;
 
-             }
 
-             // Already being watched
 
-             if (_fileSystemWatchers.ContainsKey(path))
 
-             {
 
-                 return;
 
-             }
 
-             // Creating a FileSystemWatcher over the LAN can take hundreds of milliseconds, so wrap it in a Task to do them all in parallel
 
-             Task.Run(() =>
 
-             {
 
-                 try
 
-                 {
 
-                     var newWatcher = new FileSystemWatcher(path, "*")
 
-                     {
 
-                         IncludeSubdirectories = true,
 
-                         InternalBufferSize = 65536,
 
-                         NotifyFilter = NotifyFilters.CreationTime |
 
-                                        NotifyFilters.DirectoryName |
 
-                                        NotifyFilters.FileName |
 
-                                        NotifyFilters.LastWrite |
 
-                                        NotifyFilters.Size |
 
-                                        NotifyFilters.Attributes
 
-                     };
 
-                     newWatcher.Created += OnWatcherChanged;
 
-                     newWatcher.Deleted += OnWatcherChanged;
 
-                     newWatcher.Renamed += OnWatcherChanged;
 
-                     newWatcher.Changed += OnWatcherChanged;
 
-                     newWatcher.Error += OnWatcherError;
 
-                     if (_fileSystemWatchers.TryAdd(path, newWatcher))
 
-                     {
 
-                         newWatcher.EnableRaisingEvents = true;
 
-                         _logger.LogInformation("Watching directory {Path}", path);
 
-                     }
 
-                     else
 
-                     {
 
-                         DisposeWatcher(newWatcher, false);
 
-                     }
 
-                 }
 
-                 catch (Exception ex)
 
-                 {
 
-                     _logger.LogError(ex, "Error watching path: {Path}", path);
 
-                 }
 
-             });
 
-         }
 
-         /// <summary>
 
-         /// Stops the watching path.
 
-         /// </summary>
 
-         /// <param name="path">The path.</param>
 
-         private void StopWatchingPath(string path)
 
-         {
 
-             if (_fileSystemWatchers.TryGetValue(path, out var watcher))
 
-             {
 
-                 DisposeWatcher(watcher, true);
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Disposes the watcher.
 
-         /// </summary>
 
-         private void DisposeWatcher(FileSystemWatcher watcher, bool removeFromList)
 
-         {
 
-             try
 
-             {
 
-                 using (watcher)
 
-                 {
 
-                     _logger.LogInformation("Stopping directory watching for path {Path}", watcher.Path);
 
-                     watcher.Created -= OnWatcherChanged;
 
-                     watcher.Deleted -= OnWatcherChanged;
 
-                     watcher.Renamed -= OnWatcherChanged;
 
-                     watcher.Changed -= OnWatcherChanged;
 
-                     watcher.Error -= OnWatcherError;
 
-                     watcher.EnableRaisingEvents = false;
 
-                 }
 
-             }
 
-             finally
 
-             {
 
-                 if (removeFromList)
 
-                 {
 
-                     RemoveWatcherFromList(watcher);
 
-                 }
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Removes the watcher from list.
 
-         /// </summary>
 
-         /// <param name="watcher">The watcher.</param>
 
-         private void RemoveWatcherFromList(FileSystemWatcher watcher)
 
-         {
 
-             _fileSystemWatchers.TryRemove(watcher.Path, out _);
 
-         }
 
-         /// <summary>
 
-         /// Handles the Error event of the watcher control.
 
-         /// </summary>
 
-         /// <param name="sender">The source of the event.</param>
 
-         /// <param name="e">The <see cref="ErrorEventArgs" /> instance containing the event data.</param>
 
-         private void OnWatcherError(object sender, ErrorEventArgs e)
 
-         {
 
-             var ex = e.GetException();
 
-             var dw = (FileSystemWatcher)sender;
 
-             _logger.LogError(ex, "Error in Directory watcher for: {Path}", dw.Path);
 
-             DisposeWatcher(dw, true);
 
-         }
 
-         /// <summary>
 
-         /// Handles the Changed event of the watcher control.
 
-         /// </summary>
 
-         /// <param name="sender">The source of the event.</param>
 
-         /// <param name="e">The <see cref="FileSystemEventArgs" /> instance containing the event data.</param>
 
-         private void OnWatcherChanged(object sender, FileSystemEventArgs e)
 
-         {
 
-             try
 
-             {
 
-                 ReportFileSystemChanged(e.FullPath);
 
-             }
 
-             catch (Exception ex)
 
-             {
 
-                 _logger.LogError(ex, "Exception in ReportFileSystemChanged. Path: {FullPath}", e.FullPath);
 
-             }
 
-         }
 
-         public void ReportFileSystemChanged(string path)
 
-         {
 
-             if (string.IsNullOrEmpty(path))
 
-             {
 
-                 throw new ArgumentNullException(nameof(path));
 
-             }
 
-             var monitorPath = !IgnorePatterns.ShouldIgnore(path);
 
-             // Ignore certain files
 
-             var tempIgnorePaths = _tempIgnoredPaths.Keys.ToList();
 
-             // If the parent of an ignored path has a change event, ignore that too
 
-             if (tempIgnorePaths.Any(i =>
 
-             {
 
-                 if (_fileSystem.AreEqual(i, path))
 
-                 {
 
-                     _logger.LogDebug("Ignoring change to {Path}", path);
 
-                     return true;
 
-                 }
 
-                 if (_fileSystem.ContainsSubPath(i, path))
 
-                 {
 
-                     _logger.LogDebug("Ignoring change to {Path}", path);
 
-                     return true;
 
-                 }
 
-                 // Go up a level
 
-                 var parent = Path.GetDirectoryName(i);
 
-                 if (!string.IsNullOrEmpty(parent) && _fileSystem.AreEqual(parent, path))
 
-                 {
 
-                     _logger.LogDebug("Ignoring change to {Path}", path);
 
-                     return true;
 
-                 }
 
-                 return false;
 
-             }))
 
-             {
 
-                 monitorPath = false;
 
-             }
 
-             if (monitorPath)
 
-             {
 
-                 // Avoid implicitly captured closure
 
-                 CreateRefresher(path);
 
-             }
 
-         }
 
-         private void CreateRefresher(string path)
 
-         {
 
-             var parentPath = Path.GetDirectoryName(path);
 
-             lock (_activeRefreshers)
 
-             {
 
-                 foreach (var refresher in _activeRefreshers)
 
-                 {
 
-                     // Path is already being refreshed
 
-                     if (_fileSystem.AreEqual(path, refresher.Path))
 
-                     {
 
-                         refresher.RestartTimer();
 
-                         return;
 
-                     }
 
-                     // Parent folder is already being refreshed
 
-                     if (_fileSystem.ContainsSubPath(refresher.Path, path))
 
-                     {
 
-                         refresher.AddPath(path);
 
-                         return;
 
-                     }
 
-                     // New path is a parent
 
-                     if (_fileSystem.ContainsSubPath(path, refresher.Path))
 
-                     {
 
-                         refresher.ResetPath(path, null);
 
-                         return;
 
-                     }
 
-                     // They are siblings. Rebase the refresher to the parent folder.
 
-                     if (string.Equals(parentPath, Path.GetDirectoryName(refresher.Path), StringComparison.Ordinal))
 
-                     {
 
-                         refresher.ResetPath(parentPath, path);
 
-                         return;
 
-                     }
 
-                 }
 
-                 var newRefresher = new FileRefresher(path, _configurationManager, _libraryManager, _logger);
 
-                 newRefresher.Completed += OnNewRefresherCompleted;
 
-                 _activeRefreshers.Add(newRefresher);
 
-             }
 
-         }
 
-         private void OnNewRefresherCompleted(object sender, EventArgs e)
 
-         {
 
-             var refresher = (FileRefresher)sender;
 
-             DisposeRefresher(refresher);
 
-         }
 
-         /// <summary>
 
-         /// Stops this instance.
 
-         /// </summary>
 
-         public void Stop()
 
-         {
 
-             _libraryManager.ItemAdded -= OnLibraryManagerItemAdded;
 
-             _libraryManager.ItemRemoved -= OnLibraryManagerItemRemoved;
 
-             foreach (var watcher in _fileSystemWatchers.Values.ToList())
 
-             {
 
-                 DisposeWatcher(watcher, false);
 
-             }
 
-             _fileSystemWatchers.Clear();
 
-             DisposeRefreshers();
 
-         }
 
-         private void DisposeRefresher(FileRefresher refresher)
 
-         {
 
-             lock (_activeRefreshers)
 
-             {
 
-                 refresher.Completed -= OnNewRefresherCompleted;
 
-                 refresher.Dispose();
 
-                 _activeRefreshers.Remove(refresher);
 
-             }
 
-         }
 
-         private void DisposeRefreshers()
 
-         {
 
-             lock (_activeRefreshers)
 
-             {
 
-                 foreach (var refresher in _activeRefreshers.ToList())
 
-                 {
 
-                     refresher.Completed -= OnNewRefresherCompleted;
 
-                     refresher.Dispose();
 
-                 }
 
-                 _activeRefreshers.Clear();
 
-             }
 
-         }
 
-         /// <summary>
 
-         /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
 
-         /// </summary>
 
-         public void Dispose()
 
-         {
 
-             Dispose(true);
 
-             GC.SuppressFinalize(this);
 
-         }
 
-         /// <summary>
 
-         /// Releases unmanaged and - optionally - managed resources.
 
-         /// </summary>
 
-         /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
 
-         protected virtual void Dispose(bool disposing)
 
-         {
 
-             if (_disposed)
 
-             {
 
-                 return;
 
-             }
 
-             if (disposing)
 
-             {
 
-                 Stop();
 
-             }
 
-             _disposed = true;
 
-         }
 
-     }
 
- }
 
 
  |