2019-11-01 10:38:54 -07:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 12:54:44 -07:00
|
|
|
using System;
|
2013-02-20 18:33:05 -07:00
|
|
|
using System.Collections.Concurrent;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using System.Threading.Tasks;
|
2020-08-20 03:16:24 -07:00
|
|
|
using Emby.Server.Implementations.Library;
|
2016-11-10 21:25:21 -07:00
|
|
|
using MediaBrowser.Controller.Configuration;
|
|
|
|
using MediaBrowser.Controller.Entities;
|
|
|
|
using MediaBrowser.Controller.Library;
|
2016-10-25 12:02:04 -07:00
|
|
|
using MediaBrowser.Model.IO;
|
2019-01-13 12:21:32 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2017-08-09 12:56:38 -07:00
|
|
|
namespace Emby.Server.Implementations.IO
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2014-01-28 14:25:10 -07:00
|
|
|
public class LibraryMonitor : ILibraryMonitor
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2020-06-05 17:15:56 -07:00
|
|
|
private readonly ILogger<LibraryMonitor> _logger;
|
2020-04-04 14:33:00 -07:00
|
|
|
private readonly ILibraryManager _libraryManager;
|
|
|
|
private readonly IServerConfigurationManager _configurationManager;
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <summary>
|
2019-11-01 10:38:54 -07:00
|
|
|
/// The file system watchers.
|
2013-02-20 18:33:05 -07:00
|
|
|
/// </summary>
|
2013-09-19 08:13:34 -07:00
|
|
|
private readonly ConcurrentDictionary<string, FileSystemWatcher> _fileSystemWatchers = new ConcurrentDictionary<string, FileSystemWatcher>(StringComparer.OrdinalIgnoreCase);
|
2019-03-28 15:19:56 -07:00
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <summary>
|
2019-11-01 10:38:54 -07:00
|
|
|
/// The affected paths.
|
2013-02-20 18:33:05 -07:00
|
|
|
/// </summary>
|
2016-05-26 13:54:05 -07:00
|
|
|
private readonly List<FileRefresher> _activeRefreshers = new List<FileRefresher>();
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
/// <summary>
|
2019-11-01 10:38:54 -07:00
|
|
|
/// A dynamic list of paths that should be ignored. Added to during our own file system modifications.
|
2013-02-20 18:33:05 -07:00
|
|
|
/// </summary>
|
2013-04-28 06:24:20 -07:00
|
|
|
private readonly ConcurrentDictionary<string, string> _tempIgnoredPaths = new ConcurrentDictionary<string, string>(StringComparer.OrdinalIgnoreCase);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2020-08-20 03:16:24 -07:00
|
|
|
private bool _disposed = false;
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <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>
|
2014-01-28 14:25:10 -07:00
|
|
|
private void TemporarilyIgnore(string path)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2013-03-07 22:08:27 -07:00
|
|
|
_tempIgnoredPaths[path] = path;
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
2014-01-28 14:25:10 -07:00
|
|
|
public void ReportFileSystemChangeBeginning(string path)
|
|
|
|
{
|
2014-01-28 14:51:47 -07:00
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(path));
|
2014-01-28 14:51:47 -07:00
|
|
|
}
|
|
|
|
|
2014-01-28 14:25:10 -07:00
|
|
|
TemporarilyIgnore(path);
|
|
|
|
}
|
|
|
|
|
2016-02-24 11:45:20 -07:00
|
|
|
public bool IsPathLocked(string path)
|
|
|
|
{
|
2017-08-23 09:45:58 -07:00
|
|
|
// This method is not used by the core but it used by auto-organize
|
|
|
|
|
2016-02-24 11:45:20 -07:00
|
|
|
var lockedPaths = _tempIgnoredPaths.Keys.ToList();
|
2017-01-11 10:56:26 -07:00
|
|
|
return lockedPaths.Any(i => _fileSystem.AreEqual(i, path) || _fileSystem.ContainsSubPath(i, path));
|
2016-02-24 11:45:20 -07:00
|
|
|
}
|
|
|
|
|
2014-01-28 22:17:58 -07:00
|
|
|
public async void ReportFileSystemChangeComplete(string path, bool refreshPath)
|
2014-01-28 14:25:10 -07:00
|
|
|
{
|
2014-01-28 14:51:47 -07:00
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(path));
|
2014-01-28 14:51:47 -07:00
|
|
|
}
|
|
|
|
|
2019-03-28 15:19:56 -07:00
|
|
|
// 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.
|
2015-05-24 11:33:28 -07:00
|
|
|
// Seeing long delays in some situations, especially over the network, sometimes up to 45 seconds
|
2015-06-02 22:30:14 -07:00
|
|
|
// But if we make this delay too high, we risk missing legitimate changes, such as user adding a new file, or hand-editing metadata
|
2016-07-21 22:17:09 -07:00
|
|
|
await Task.Delay(45000).ConfigureAwait(false);
|
2014-01-28 22:17:58 -07:00
|
|
|
|
2019-01-13 13:46:33 -07:00
|
|
|
_tempIgnoredPaths.TryRemove(path, out var val);
|
2014-01-28 14:25:10 -07:00
|
|
|
|
|
|
|
if (refreshPath)
|
|
|
|
{
|
2016-09-15 13:30:46 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
ReportFileSystemChanged(path);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-08-31 13:20:19 -07:00
|
|
|
_logger.LogError(ex, "Error in ReportFileSystemChanged for {Path}", path);
|
2016-09-15 13:30:46 -07:00
|
|
|
}
|
2014-01-28 14:25:10 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <summary>
|
2014-01-28 14:25:10 -07:00
|
|
|
/// Initializes a new instance of the <see cref="LibraryMonitor" /> class.
|
2013-02-20 18:33:05 -07:00
|
|
|
/// </summary>
|
2019-01-25 14:41:43 -07:00
|
|
|
public LibraryMonitor(
|
2020-04-04 14:33:00 -07:00
|
|
|
ILogger<LibraryMonitor> logger,
|
2019-01-25 14:41:43 -07:00
|
|
|
ILibraryManager libraryManager,
|
|
|
|
IServerConfigurationManager configurationManager,
|
2019-03-07 09:39:40 -07:00
|
|
|
IFileSystem fileSystem)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2020-04-04 14:33:00 -07:00
|
|
|
_libraryManager = libraryManager;
|
|
|
|
_logger = logger;
|
|
|
|
_configurationManager = configurationManager;
|
2014-01-28 18:46:04 -07:00
|
|
|
_fileSystem = fileSystem;
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
2013-04-28 06:24:20 -07:00
|
|
|
|
2019-01-25 14:41:43 -07:00
|
|
|
private bool IsLibraryMonitorEnabled(BaseItem item)
|
2014-01-29 13:30:26 -07:00
|
|
|
{
|
2016-10-23 13:04:31 -07:00
|
|
|
if (item is BasePluginFolder)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-04-04 14:33:00 -07:00
|
|
|
var options = _libraryManager.GetLibraryOptions(item);
|
2016-08-24 13:46:26 -07:00
|
|
|
|
2016-09-11 00:33:53 -07:00
|
|
|
if (options != null)
|
2014-01-29 13:30:26 -07:00
|
|
|
{
|
2016-08-24 13:46:26 -07:00
|
|
|
return options.EnableRealtimeMonitor;
|
2014-01-29 13:30:26 -07:00
|
|
|
}
|
2016-08-24 13:46:26 -07:00
|
|
|
|
2016-09-11 00:33:53 -07:00
|
|
|
return false;
|
2014-01-29 13:30:26 -07:00
|
|
|
}
|
|
|
|
|
2016-08-24 13:46:26 -07:00
|
|
|
public void Start()
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2020-04-04 14:33:00 -07:00
|
|
|
_libraryManager.ItemAdded += OnLibraryManagerItemAdded;
|
|
|
|
_libraryManager.ItemRemoved += OnLibraryManagerItemRemoved;
|
2013-02-28 12:32:41 -07:00
|
|
|
|
2019-03-28 15:19:56 -07:00
|
|
|
var pathsToWatch = new List<string>();
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2020-04-04 14:33:00 -07:00
|
|
|
var paths = _libraryManager
|
2013-08-28 12:56:06 -07:00
|
|
|
.RootFolder
|
|
|
|
.Children
|
2019-01-25 14:41:43 -07:00
|
|
|
.Where(IsLibraryMonitorEnabled)
|
2013-08-28 12:56:06 -07:00
|
|
|
.OfType<Folder>()
|
2014-02-05 21:39:16 -07:00
|
|
|
.SelectMany(f => f.PhysicalLocations)
|
2013-08-28 12:56:06 -07:00
|
|
|
.Distinct(StringComparer.OrdinalIgnoreCase)
|
|
|
|
.OrderBy(i => i)
|
|
|
|
.ToList();
|
2013-02-20 18:33:05 -07:00
|
|
|
|
|
|
|
foreach (var path in paths)
|
|
|
|
{
|
|
|
|
if (!ContainsParentFolder(pathsToWatch, path))
|
|
|
|
{
|
|
|
|
pathsToWatch.Add(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var path in pathsToWatch)
|
|
|
|
{
|
|
|
|
StartWatchingPath(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-24 13:46:26 -07:00
|
|
|
private void StartWatching(BaseItem item)
|
|
|
|
{
|
2019-01-25 14:41:43 -07:00
|
|
|
if (IsLibraryMonitorEnabled(item))
|
2016-08-24 13:46:26 -07:00
|
|
|
{
|
|
|
|
StartWatchingPath(item.Path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-02 21:10:11 -07:00
|
|
|
/// <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>
|
2019-03-28 15:19:56 -07:00
|
|
|
private void OnLibraryManagerItemRemoved(object sender, ItemChangeEventArgs e)
|
2013-05-02 21:10:11 -07:00
|
|
|
{
|
2017-11-25 21:48:12 -07:00
|
|
|
if (e.Parent is AggregateFolder)
|
2013-05-02 21:10:11 -07:00
|
|
|
{
|
|
|
|
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>
|
2019-03-28 15:19:56 -07:00
|
|
|
private void OnLibraryManagerItemAdded(object sender, ItemChangeEventArgs e)
|
2013-05-02 21:10:11 -07:00
|
|
|
{
|
2017-11-25 21:48:12 -07:00
|
|
|
if (e.Parent is AggregateFolder)
|
2013-05-02 21:10:11 -07:00
|
|
|
{
|
2016-08-24 13:46:26 -07:00
|
|
|
StartWatching(e.Item);
|
2013-05-02 21:10:11 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
/// <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>
|
2019-01-13 13:37:13 -07:00
|
|
|
/// <exception cref="ArgumentNullException">path</exception>
|
2013-02-20 18:33:05 -07:00
|
|
|
private static bool ContainsParentFolder(IEnumerable<string> lst, string path)
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
if (string.IsNullOrEmpty(path))
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(path));
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
path = path.TrimEnd(Path.DirectorySeparatorChar);
|
|
|
|
|
|
|
|
return lst.Any(str =>
|
|
|
|
{
|
2019-03-28 15:19:56 -07:00
|
|
|
// this should be a little quicker than examining each actual parent folder...
|
2013-02-20 18:33:05 -07:00
|
|
|
var compare = str.TrimEnd(Path.DirectorySeparatorChar);
|
|
|
|
|
2016-03-27 14:11:27 -07:00
|
|
|
return path.Equals(compare, StringComparison.OrdinalIgnoreCase) || (path.StartsWith(compare, StringComparison.OrdinalIgnoreCase) && path[compare.Length] == Path.DirectorySeparatorChar);
|
2013-02-20 18:33:05 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Starts the watching path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
private void StartWatchingPath(string path)
|
|
|
|
{
|
2019-01-26 14:59:53 -07:00
|
|
|
if (!Directory.Exists(path))
|
2017-02-03 13:52:56 -07:00
|
|
|
{
|
|
|
|
// Seeing a crash in the mono runtime due to an exception being thrown on a different thread
|
2020-04-04 14:33:00 -07:00
|
|
|
_logger.LogInformation("Skipping realtime monitor for {Path} because the path does not exist", path);
|
2017-02-03 13:52:56 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Already being watched
|
|
|
|
if (_fileSystemWatchers.ContainsKey(path))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-02-20 18:33:05 -07:00
|
|
|
// 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(() =>
|
|
|
|
{
|
2014-03-27 12:30:21 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var newWatcher = new FileSystemWatcher(path, "*")
|
|
|
|
{
|
2019-03-28 15:19:56 -07:00
|
|
|
IncludeSubdirectories = true,
|
|
|
|
InternalBufferSize = 65536,
|
|
|
|
NotifyFilter = NotifyFilters.CreationTime |
|
|
|
|
NotifyFilters.DirectoryName |
|
|
|
|
NotifyFilters.FileName |
|
|
|
|
NotifyFilters.LastWrite |
|
|
|
|
NotifyFilters.Size |
|
|
|
|
NotifyFilters.Attributes
|
2014-03-27 12:30:21 -07:00
|
|
|
};
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2019-03-28 15:19:56 -07:00
|
|
|
newWatcher.Created += OnWatcherChanged;
|
|
|
|
newWatcher.Deleted += OnWatcherChanged;
|
|
|
|
newWatcher.Renamed += OnWatcherChanged;
|
|
|
|
newWatcher.Changed += OnWatcherChanged;
|
|
|
|
newWatcher.Error += OnWatcherError;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2013-05-05 06:40:44 -07:00
|
|
|
if (_fileSystemWatchers.TryAdd(path, newWatcher))
|
|
|
|
{
|
|
|
|
newWatcher.EnableRaisingEvents = true;
|
2020-04-04 14:33:00 -07:00
|
|
|
_logger.LogInformation("Watching directory " + path);
|
2013-05-05 06:40:44 -07:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
DisposeWatcher(newWatcher, false);
|
2013-05-05 06:40:44 -07:00
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
2014-03-27 12:30:21 -07:00
|
|
|
catch (Exception ex)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2020-04-04 14:33:00 -07:00
|
|
|
_logger.LogError(ex, "Error watching path: {path}", path);
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Stops the watching path.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="path">The path.</param>
|
|
|
|
private void StopWatchingPath(string path)
|
|
|
|
{
|
2019-01-13 13:46:33 -07:00
|
|
|
if (_fileSystemWatchers.TryGetValue(path, out var watcher))
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
DisposeWatcher(watcher, true);
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Disposes the watcher.
|
|
|
|
/// </summary>
|
2018-09-12 10:26:21 -07:00
|
|
|
private void DisposeWatcher(FileSystemWatcher watcher, bool removeFromList)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2014-10-13 13:14:53 -07:00
|
|
|
try
|
|
|
|
{
|
|
|
|
using (watcher)
|
|
|
|
{
|
2020-04-04 14:33:00 -07:00
|
|
|
_logger.LogInformation("Stopping directory watching for path {Path}", watcher.Path);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2019-03-28 15:19:56 -07:00
|
|
|
watcher.Created -= OnWatcherChanged;
|
|
|
|
watcher.Deleted -= OnWatcherChanged;
|
|
|
|
watcher.Renamed -= OnWatcherChanged;
|
|
|
|
watcher.Changed -= OnWatcherChanged;
|
|
|
|
watcher.Error -= OnWatcherError;
|
2018-09-12 10:26:21 -07:00
|
|
|
|
2019-03-28 15:19:56 -07:00
|
|
|
watcher.EnableRaisingEvents = false;
|
2014-10-13 13:14:53 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
if (removeFromList)
|
|
|
|
{
|
|
|
|
RemoveWatcherFromList(watcher);
|
|
|
|
}
|
2014-10-13 13:14:53 -07:00
|
|
|
}
|
2013-05-05 06:40:44 -07:00
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2013-05-05 06:40:44 -07:00
|
|
|
/// <summary>
|
|
|
|
/// Removes the watcher from list.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="watcher">The watcher.</param>
|
|
|
|
private void RemoveWatcherFromList(FileSystemWatcher watcher)
|
|
|
|
{
|
2019-03-28 15:19:56 -07:00
|
|
|
_fileSystemWatchers.TryRemove(watcher.Path, out _);
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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>
|
2019-03-28 15:19:56 -07:00
|
|
|
private void OnWatcherError(object sender, ErrorEventArgs e)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
var ex = e.GetException();
|
2013-04-28 06:24:20 -07:00
|
|
|
var dw = (FileSystemWatcher)sender;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2020-04-04 14:33:00 -07:00
|
|
|
_logger.LogError(ex, "Error in Directory watcher for: {Path}", dw.Path);
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
DisposeWatcher(dw, true);
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <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>
|
2019-03-28 15:19:56 -07:00
|
|
|
private void OnWatcherChanged(object sender, FileSystemEventArgs e)
|
2013-10-31 07:03:23 -07:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-03-28 15:19:56 -07:00
|
|
|
ReportFileSystemChanged(e.FullPath);
|
2013-10-31 07:03:23 -07:00
|
|
|
}
|
2014-01-28 14:25:10 -07:00
|
|
|
catch (Exception ex)
|
2013-10-31 07:03:23 -07:00
|
|
|
{
|
2020-04-04 14:33:00 -07:00
|
|
|
_logger.LogError(ex, "Exception in ReportFileSystemChanged. Path: {FullPath}", e.FullPath);
|
2013-10-31 07:03:23 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-28 14:25:10 -07:00
|
|
|
public void ReportFileSystemChanged(string path)
|
|
|
|
{
|
2014-01-28 14:51:47 -07:00
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
{
|
2019-01-06 13:50:43 -07:00
|
|
|
throw new ArgumentNullException(nameof(path));
|
2014-01-28 14:51:47 -07:00
|
|
|
}
|
2014-01-29 13:30:26 -07:00
|
|
|
|
2020-05-12 14:33:06 -07:00
|
|
|
var monitorPath = !IgnorePatterns.ShouldIgnore(path);
|
2013-04-13 14:45:29 -07:00
|
|
|
|
2014-03-27 12:30:21 -07:00
|
|
|
// Ignore certain files
|
2013-05-19 16:44:05 -07:00
|
|
|
var tempIgnorePaths = _tempIgnoredPaths.Keys.ToList();
|
|
|
|
|
2013-11-04 09:20:44 -07:00
|
|
|
// If the parent of an ignored path has a change event, ignore that too
|
|
|
|
if (tempIgnorePaths.Any(i =>
|
2013-05-19 16:44:05 -07:00
|
|
|
{
|
2017-05-04 00:00:52 -07:00
|
|
|
if (_fileSystem.AreEqual(i, path))
|
2013-11-02 12:30:29 -07:00
|
|
|
{
|
2020-04-04 14:33:00 -07:00
|
|
|
_logger.LogDebug("Ignoring change to {Path}", path);
|
2013-11-04 09:20:44 -07:00
|
|
|
return true;
|
|
|
|
}
|
2013-11-02 12:30:29 -07:00
|
|
|
|
2014-01-28 14:51:47 -07:00
|
|
|
if (_fileSystem.ContainsSubPath(i, path))
|
2013-11-04 09:20:44 -07:00
|
|
|
{
|
2020-04-04 14:33:00 -07:00
|
|
|
_logger.LogDebug("Ignoring change to {Path}", path);
|
2013-11-04 09:20:44 -07:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-28 14:51:47 -07:00
|
|
|
// Go up a level
|
2019-01-26 13:47:11 -07:00
|
|
|
var parent = Path.GetDirectoryName(i);
|
2019-03-28 15:19:56 -07:00
|
|
|
if (!string.IsNullOrEmpty(parent) && _fileSystem.AreEqual(parent, path))
|
2013-11-04 09:20:44 -07:00
|
|
|
{
|
2020-04-04 14:33:00 -07:00
|
|
|
_logger.LogDebug("Ignoring change to {Path}", path);
|
2019-03-28 15:19:56 -07:00
|
|
|
return true;
|
2013-11-07 08:57:12 -07:00
|
|
|
}
|
|
|
|
|
2013-11-04 09:20:44 -07:00
|
|
|
return false;
|
|
|
|
}))
|
|
|
|
{
|
2014-03-27 12:30:21 -07:00
|
|
|
monitorPath = false;
|
2013-05-19 16:44:05 -07:00
|
|
|
}
|
|
|
|
|
2014-03-27 12:30:21 -07:00
|
|
|
if (monitorPath)
|
|
|
|
{
|
|
|
|
// Avoid implicitly captured closure
|
2016-05-26 13:54:05 -07:00
|
|
|
CreateRefresher(path);
|
2014-03-09 19:33:49 -07:00
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
2016-05-26 13:54:05 -07:00
|
|
|
private void CreateRefresher(string path)
|
2014-04-02 14:55:19 -07:00
|
|
|
{
|
2019-01-26 13:47:11 -07:00
|
|
|
var parentPath = Path.GetDirectoryName(path);
|
2016-01-27 11:42:19 -07:00
|
|
|
|
2016-05-26 13:54:05 -07:00
|
|
|
lock (_activeRefreshers)
|
2014-04-02 14:55:19 -07:00
|
|
|
{
|
2019-03-28 15:19:56 -07:00
|
|
|
foreach (var refresher in _activeRefreshers)
|
2014-04-02 14:55:19 -07:00
|
|
|
{
|
2016-05-26 13:54:05 -07:00
|
|
|
// Path is already being refreshed
|
2017-05-04 00:00:52 -07:00
|
|
|
if (_fileSystem.AreEqual(path, refresher.Path))
|
2016-05-26 13:54:05 -07:00
|
|
|
{
|
|
|
|
refresher.RestartTimer();
|
|
|
|
return;
|
|
|
|
}
|
2015-11-10 21:11:03 -07:00
|
|
|
|
2016-05-26 13:54:05 -07:00
|
|
|
// Parent folder is already being refreshed
|
|
|
|
if (_fileSystem.ContainsSubPath(refresher.Path, path))
|
2014-04-02 14:55:19 -07:00
|
|
|
{
|
2016-05-26 13:54:05 -07:00
|
|
|
refresher.AddPath(path);
|
|
|
|
return;
|
2014-04-02 14:55:19 -07:00
|
|
|
}
|
|
|
|
|
2016-05-26 13:54:05 -07:00
|
|
|
// New path is a parent
|
|
|
|
if (_fileSystem.ContainsSubPath(path, refresher.Path))
|
|
|
|
{
|
|
|
|
refresher.ResetPath(path, null);
|
|
|
|
return;
|
|
|
|
}
|
2014-04-02 14:55:19 -07:00
|
|
|
|
2016-05-26 20:46:21 -07:00
|
|
|
// They are siblings. Rebase the refresher to the parent folder.
|
2019-01-26 13:47:11 -07:00
|
|
|
if (string.Equals(parentPath, Path.GetDirectoryName(refresher.Path), StringComparison.Ordinal))
|
2016-05-26 13:54:05 -07:00
|
|
|
{
|
|
|
|
refresher.ResetPath(parentPath, path);
|
|
|
|
return;
|
|
|
|
}
|
2013-05-07 11:57:27 -07:00
|
|
|
}
|
2013-04-28 06:24:20 -07:00
|
|
|
|
2020-04-04 14:33:00 -07:00
|
|
|
var newRefresher = new FileRefresher(path, _configurationManager, _libraryManager, _logger);
|
2016-05-26 13:54:05 -07:00
|
|
|
newRefresher.Completed += NewRefresher_Completed;
|
|
|
|
_activeRefreshers.Add(newRefresher);
|
2013-12-29 19:41:22 -07:00
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
2016-05-26 13:54:05 -07:00
|
|
|
private void NewRefresher_Completed(object sender, EventArgs e)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2016-05-26 13:54:05 -07:00
|
|
|
var refresher = (FileRefresher)sender;
|
|
|
|
DisposeRefresher(refresher);
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Stops this instance.
|
|
|
|
/// </summary>
|
2013-03-07 22:08:27 -07:00
|
|
|
public void Stop()
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2020-04-04 14:33:00 -07:00
|
|
|
_libraryManager.ItemAdded -= OnLibraryManagerItemAdded;
|
|
|
|
_libraryManager.ItemRemoved -= OnLibraryManagerItemRemoved;
|
2013-02-20 18:33:05 -07:00
|
|
|
|
2013-05-05 06:40:44 -07:00
|
|
|
foreach (var watcher in _fileSystemWatchers.Values.ToList())
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2018-09-12 10:26:21 -07:00
|
|
|
DisposeWatcher(watcher, false);
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
2013-08-13 07:41:45 -07:00
|
|
|
_fileSystemWatchers.Clear();
|
2016-05-26 13:54:05 -07:00
|
|
|
DisposeRefreshers();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DisposeRefresher(FileRefresher refresher)
|
|
|
|
{
|
|
|
|
lock (_activeRefreshers)
|
|
|
|
{
|
|
|
|
refresher.Dispose();
|
|
|
|
_activeRefreshers.Remove(refresher);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void DisposeRefreshers()
|
|
|
|
{
|
|
|
|
lock (_activeRefreshers)
|
|
|
|
{
|
|
|
|
foreach (var refresher in _activeRefreshers.ToList())
|
|
|
|
{
|
|
|
|
refresher.Dispose();
|
|
|
|
}
|
2019-03-28 15:19:56 -07:00
|
|
|
|
2016-05-26 13:54:05 -07:00
|
|
|
_activeRefreshers.Clear();
|
|
|
|
}
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
|
|
|
|
/// </summary>
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
Dispose(true);
|
2019-03-28 15:19:56 -07:00
|
|
|
GC.SuppressFinalize(this);
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Releases unmanaged and - optionally - managed resources.
|
|
|
|
/// </summary>
|
2019-02-01 13:56:50 -07:00
|
|
|
/// <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)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
2019-02-01 13:56:50 -07:00
|
|
|
if (_disposed)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (disposing)
|
2013-02-20 18:33:05 -07:00
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
}
|
2019-02-01 13:56:50 -07:00
|
|
|
|
|
|
|
_disposed = true;
|
2013-02-20 18:33:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|